14
14
#include <stdio.h>
15
15
#include <stdint.h>
16
16
#include "stm32f4xx_hal.h"
17
+ #include "stm32f401xe.h" // stm32f401re
17
18
#include "sys_init.h"
18
19
20
+ // --- defines ---------------------------------------------------------------------------------------------------------
21
+ #define TIM1_COUNTDOWN_SEC 15 // 15 seconds timeout for the uart reception watchdog
22
+
19
23
// --- static variable definitions -------------------------------------------------------------------------------------
20
24
// This is the structure that will store the received buffer and the size of it. This will be used by the upper layers
21
25
// to receive the data.
@@ -27,9 +31,43 @@ static process_rx_data data_rx_cb = NULL;
27
31
UART_HandleTypeDef huart2 ;
28
32
29
33
// --- static function declarations ------------------------------------------------------------------------------------
34
+ static void uart_recv_it_init_wdg (void );
30
35
static void MX_USART2_UART_Init (void );
31
36
32
37
// --- static function definitions -------------------------------------------------------------------------------------
38
+ /**
39
+ * @brief This function initializes the timer1 peripheral to be used as a watchdog for the uart reception.
40
+ * TODO: GPA: We can set the timer to one-pulse mode, so that it will stop after the timeout. The timer can be
41
+ * re-enabled by the uart isr, only when needed.
42
+ *
43
+ */
44
+ static void
45
+ uart_recv_it_init_wdg (void )
46
+ {
47
+ // Enable the TIM1 clock
48
+ RCC -> APB2ENR |= RCC_APB2ENR_TIM1EN ;
49
+
50
+ TIM1 -> SMCR = 0 ;
51
+ TIM1 -> DIER = TIM_DIER_UIE ; // Enable update interrupt
52
+ TIM1 -> SR = 0 ; // Clear status register
53
+ TIM1 -> EGR = TIM_EGR_UG ; // Generate an update event to reload the prescaler
54
+ // Configure the prescaler such is to generate 1ms ticks. The systemclock is 84mhz
55
+ TIM1 -> PSC = 21000 - 1 ; // this means 1ms -> 4000 ticks
56
+
57
+ // Set the auto-reload value for a 15-second countdown
58
+ TIM1 -> ARR = TIM1_COUNTDOWN_SEC * 4000 - 1 ;
59
+ TIM1 -> CNT = TIM1 -> ARR ; // Set the counter to the auto-reload value
60
+ TIM1 -> CR2 = 0 ;
61
+ // CR1 config: URS = 1, DIR = 1, CEN = 1
62
+ TIM1 -> CR1 = (TIM_CR1_URS | TIM_CR1_DIR | TIM_CR1_CEN );
63
+
64
+ // Clear any pending update interrupt flags
65
+ TIM1 -> SR &= ~TIM_SR_UIF ;
66
+ // Enable the update interrupt for TIM1 in NVIC
67
+ NVIC_SetPriority (TIM1_UP_TIM10_IRQn , 0 ); // Set interrupt priority
68
+ NVIC_EnableIRQ (TIM1_UP_TIM10_IRQn );
69
+ }
70
+
33
71
/**
34
72
* @brief USART2 Initialization Function
35
73
* @param None
@@ -68,6 +106,8 @@ MX_USART2_UART_Init(void) // Change from MX_USART1_UART_Init to MX_USART2_UART_I
68
106
#endif
69
107
}
70
108
HAL_UART_Receive_IT (& huart2 , uart_buf .data_buffer , uart_buf .len ); // Start reception
109
+ // Init the uart watchdog
110
+ uart_recv_it_init_wdg ();
71
111
}
72
112
73
113
// --- function definitions --------------------------------------------------------------------------------------------
@@ -124,7 +164,8 @@ HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
124
164
{
125
165
if (huart -> Instance == USART2 )
126
166
{
127
- // TODO: GPA: Handle the possible errors and restart the reception
167
+ HAL_UART_DeInit (& huart2 );
168
+ HAL_UART_Init (& huart2 );
128
169
HAL_UART_Receive_IT (& huart2 , uart_buf .data_buffer , uart_buf .len );
129
170
}
130
171
}
@@ -139,6 +180,31 @@ uart_driver_init(void)
139
180
MX_USART2_UART_Init ();
140
181
}
141
182
183
+ /**
184
+ * @brief Function to feed the uart watchdog. Will be called by the uart isr, every time something is being received.
185
+ *
186
+ */
187
+ void
188
+ uart_driver_feed_wdg (void )
189
+ {
190
+ // Reset the uart wdg timer to not trigger the buffer reset.
191
+ TIM1 -> CNT = TIM1 -> ARR ;
192
+ }
193
+
194
+ /**
195
+ * @brief Function to recover the uart reception.
196
+ *
197
+ */
198
+ void
199
+ uart_driver_rx_recover (void )
200
+ {
201
+ HAL_UART_DeInit (& huart2 );
202
+ HAL_UART_Init (& huart2 );
203
+ // Restart the reception
204
+ HAL_UART_Receive_IT (& huart2 , uart_buf .data_buffer , uart_buf .len );
205
+ printf ("Recovering uart reception\r\n" );
206
+ }
207
+
142
208
/**
143
209
* @brief Function to register an rx callback function. This will be used by the com_protocol to register a callback
144
210
* function and process the received message.
0 commit comments