-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.c
90 lines (77 loc) · 2.21 KB
/
connection.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* conection.c
*
* Created on: 17.03.2021
* Author: symon
*/
#include "stm32l0xx.h"
#include "stm32l0xx_nucleo.h"
#include "global_constants.h"
#include "global_variables.h"
#include "global_functions.h"
#include "connection.h"
uint8_t table_of_bytes_to_sent[2 * ALL_ELEMENTS_TO_SEND + 4];
static volatile int32_t txTransmitted;
static volatile int32_t txSize;
static uint8_t bufor[50];
void DMA1_Channel2_3_IRQHandler(void) {
//if channel2 transfer USART1 is completed:
if (DMA1->ISR & DMA_ISR_TCIF2) {
DMA1->IFCR |= DMA_IFCR_CTCIF2;
DMA1_Channel2->CCR &= ~DMA_CCR_EN;
transmitting_is_Done = 1;
}
//if channel3 transfer I2C is completed:
if (DMA1->ISR & DMA_ISR_TCIF3) {
DMA1->IFCR |= DMA_IFCR_CTCIF3;
I2C1_read_write_flag = 1;
DMA1_Channel3->CCR &= ~DMA_CCR_EN;
if (ibus_received == 0) {
USART2->CR1 |= USART_CR1_RXNEIE;
//USART2->CR1 |= USART_CR1_IDLEIE;
}
}
}
void USART1_IRQHandler(void) {
if (0 != (USART_ISR_RXNE & USART1->ISR)) {
// read actual value of I-BUS (flag will be automatically removed):
static uint8_t i;
bufor[i] = USART1->RDR;
i++;
if (i >= 50) {
i = 0;
}
}
//TRANSMISJA:
// check if interrupt was generated by right flag:
if ((0 != (USART_CR1_TXEIE & USART1->CR1))
&& (0 != (USART_ISR_TXE & USART1->ISR))) {
// transmit data:
if ((0 != txSize) && (txTransmitted < txSize)) {
USART1->TDR = table_of_bytes_to_sent[txTransmitted];
txTransmitted += 1;
}
// if everything is transmitted, unable transmission interrupts:
if (txTransmitted == txSize) {
USART1->CR1 &= ~USART_CR1_TXEIE;
transmitting_is_Done = 1;
}
}
}
void print(uint16_t x[], uint8_t data_to_send) {
uint16_t sum = 0;
table_of_bytes_to_sent[0] = 0x20;
table_of_bytes_to_sent[1] = 0x40;
for (int i = 0; i < data_to_send; i++) {
table_of_bytes_to_sent[2 * i + 2] = x[i] >> 8;
table_of_bytes_to_sent[2 * i + 3] = x[i];
sum += x[i];
}
table_of_bytes_to_sent[2 * data_to_send + 2] = sum >> 8;
table_of_bytes_to_sent[2 * data_to_send + 3] = sum;
txSize = 2 * data_to_send + 4;
txTransmitted = 0;
transmitting_is_Done = 0;
New_data_to_send = 0;
DMA1_Channel2->CCR |= DMA_CCR_EN;
}