comparison Small_CPU/Src/uart.c @ 662:1b995079c045 Betatest

PSCR Mode
author heinrichs weikamp
date Tue, 14 Dec 2021 15:36:10 +0100
parents 5f11787b4f42
children fca2bd25e6e2
comparison
equal deleted inserted replaced
661:87bee7cc77b3 662:1b995079c045
18 * 18 *
19 ****************************************************************************** 19 ******************************************************************************
20 */ 20 */
21 /* Includes ------------------------------------------------------------------*/ 21 /* Includes ------------------------------------------------------------------*/
22 #include "uart.h" 22 #include "uart.h"
23 #include "externalInterface.h"
24 #include "data_exchange.h"
23 25
24 /* Private variables ---------------------------------------------------------*/ 26 /* Private variables ---------------------------------------------------------*/
25 27
26 UART_HandleTypeDef huart2; 28 #define CHUNK_SIZE (20u) /* the DMA will handle chunk size transfers */
27 29 #define CHUNKS_PER_BUFFER (3u)
28 30 UART_HandleTypeDef huart1;
31
32 DMA_HandleTypeDef hdma_usart1_rx;
33
34 uint8_t rxBuffer[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow fariations in buffer read time */
35 static uint8_t rxWriteIndex; /* Index of the data item which is analysed */
36 static uint8_t rxReadIndex; /* Index at which new data is stared */
37 static uint8_t lastCmdIndex; /* Index of last command which has not been completly received */
38 static uint8_t dmaActive; /* Indicator if DMA receiption needs to be started */
39
40 float LED_Level = 0.0; /* Normalized LED value which may be used as indication for the health status of the sensor */
41 float LED_ZeroOffset = 0.0;
42 float pCO2 = 0.0;
29 /* Exported functions --------------------------------------------------------*/ 43 /* Exported functions --------------------------------------------------------*/
30 44
31 void MX_USART2_UART_Init(void) 45 void MX_USART1_UART_Init(void)
32 { 46 {
33 /* pullup special */
34 GPIO_InitTypeDef GPIO_InitStructure;
35 __GPIOA_CLK_ENABLE();
36 GPIO_InitStructure.Pin = GPIO_PIN_2;
37 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
38 GPIO_InitStructure.Pull = GPIO_PULLUP;
39 GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
40 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
41
42 /* regular init */ 47 /* regular init */
43 huart2.Instance = USART2; 48
44 huart2.Init.BaudRate = 1200; 49 huart1.Instance = USART1;
45 huart2.Init.WordLength = UART_WORDLENGTH_8B; 50 huart1.Init.BaudRate = 9600;
46 huart2.Init.StopBits = UART_STOPBITS_1; 51 huart1.Init.WordLength = UART_WORDLENGTH_8B;
47 huart2.Init.Parity = UART_PARITY_NONE; 52 huart1.Init.StopBits = UART_STOPBITS_1;
48 huart2.Init.Mode = UART_MODE_TX_RX; 53 huart1.Init.Parity = UART_PARITY_NONE;
49 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; 54 huart1.Init.Mode = UART_MODE_TX_RX;
50 huart2.Init.OverSampling = UART_OVERSAMPLING_16; 55 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
51 HAL_UART_Init(&huart2); 56 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
52 } 57
53 58 HAL_UART_Init(&huart1);
54 59
55 uint8_t UART_ButtonAdjust(uint8_t *array) 60 rxReadIndex = 0;
56 { 61 lastCmdIndex = 0;
57 uint8_t answer[4]; 62 rxWriteIndex = 0;
58 63 dmaActive = 0;
59 HAL_UART_Transmit(&huart2,array,4,1000); 64 }
60 HAL_UART_Receive(&huart2,answer,4,2000); 65
61 if( (answer[0] == array[0]) 66 void MX_USART1_UART_DeInit(void)
62 &&(answer[1] == array[1]) 67 {
63 &&(answer[2] == array[2]) 68 HAL_DMA_DeInit(&hdma_usart1_rx);
64 &&(answer[3] == array[3])) 69 HAL_UART_DeInit(&huart1);
65 return 1; 70 }
66 else 71
67 return 0; 72 void MX_USART1_DMA_Init()
68 } 73 {
69 74 /* DMA controller clock enable */
70 void MX_USART2_UART_DeInit(void) 75 __DMA2_CLK_ENABLE();
71 { 76
72 HAL_UART_DeInit(&huart2); 77 /* Peripheral DMA init*/
73 } 78 hdma_usart1_rx.Instance = DMA2_Stream5;
79 hdma_usart1_rx.Init.Channel = DMA_CHANNEL_4;
80 hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; //DMA_MEMORY_TO_PERIPH;
81 hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
82 hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
83 hdma_usart1_rx.Init.PeriphDataAlignment = DMA_MDATAALIGN_BYTE;
84 hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
85 hdma_usart1_rx.Init.Mode = DMA_NORMAL;
86 hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW;
87 hdma_usart1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
88 HAL_DMA_Init(&hdma_usart1_rx);
89
90 __HAL_LINKDMA(&huart1,hdmarx,hdma_usart1_rx);
91
92 /* DMA interrupt init */
93 HAL_NVIC_SetPriority(DMA2_Stream5_IRQn, 0, 0);
94 HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn);
95 }
96
97
98 uint32_t dataValue = 0;
99
100 void HandleUARTData(void)
101 {
102 uint8_t localRX = rxReadIndex;
103 uint8_t dataType = 0;
104 static receiveState_t rxState = RX_Ready;
105 static uint32_t lastReceiveTick = 0;
106
107 while(localRX != rxWriteIndex)
108 {
109 lastReceiveTick = HAL_GetTick();
110 if(rxState == RX_Ready) /* identify data content */
111 {
112 switch(rxBuffer[localRX])
113 {
114 case 'l':
115 case 'D':
116 case 'Z':
117 dataType = rxBuffer[localRX];
118 rxState = RX_Data0;
119 dataValue = 0;
120 break;
121
122 default: /* unknown or corrupted => ignore */
123 break;
124 }
125 }
126 else if((rxState >= RX_Data0) && (rxState <= RX_Data4))
127 {
128 if((rxBuffer[localRX] >= '0') && (rxBuffer[localRX] <= '9'))
129 {
130 dataValue = dataValue * 10 + (rxBuffer[localRX] - '0');
131 rxState++;
132 }
133 }
134 if((rxBuffer[localRX] == ' ') || (rxBuffer[localRX] == '\n')) /* Abort data detection */
135 {
136 if(rxState == RX_DataComplete)
137 {
138 if(externalInterface_GetCO2State() == 0)
139 {
140 externalInterface_SetCO2State(EXT_INTERFACE_33V_ON);
141 }
142 switch(dataType)
143 {
144 case 'D': externalInterface_SetCO2SignalStrength(dataValue);
145 break;
146 case 'l': LED_ZeroOffset = dataValue;
147 break;
148 case 'Z': externalInterface_SetCO2Value(dataValue);
149 break;
150 default: break;
151 }
152 }
153 if(rxState != RX_Data0) /* reset state machine because message in wrong format */
154 {
155 rxState = RX_Ready;
156 }
157 }
158
159 localRX++;
160 rxReadIndex++;
161 if(rxReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
162 {
163 localRX = 0;
164 rxReadIndex = 0;
165 }
166 }
167
168 if(time_elapsed_ms(lastReceiveTick,HAL_GetTick()) > 2000) /* check for communication timeout */
169 {
170 externalInterface_SetCO2State(0);
171 }
172
173 if((dmaActive == 0) && (externalInterface_isEnabledPower33())) /* Should never happen in normal operation => restart in case of communication error */
174 {
175 if(HAL_OK == HAL_UART_Receive_DMA (&huart1, &rxBuffer[rxWriteIndex], CHUNK_SIZE))
176 {
177 dmaActive = 1;
178 }
179 }
180 }
181
182 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
183 {
184 if(huart == &huart1)
185 {
186 dmaActive = 0;
187 rxWriteIndex+=CHUNK_SIZE;
188 if(rxWriteIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
189 {
190 rxWriteIndex = 0;
191 }
192 if((rxWriteIndex / CHUNK_SIZE) != (rxReadIndex / CHUNK_SIZE)) /* start next transfer if we did not catch up with read index */
193 {
194 if(externalInterface_isEnabledPower33())
195 {
196 if(HAL_OK == HAL_UART_Receive_DMA (&huart1, &rxBuffer[rxWriteIndex], CHUNK_SIZE))
197 {
198 dmaActive = 1;
199 }
200 }
201 }
202 }
203 }
204
205
206
207
208
209
74 210
75 211
76 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ 212 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/