38
38
#include "tick.h"
39
39
#include "stm32f4xx_hal.h"
40
40
41
-
42
41
STATIC bool reserved_uart [MAX_UART ];
43
42
44
43
void uart_reset (void ) {
45
44
#ifdef USART1
46
45
reserved_uart [0 ] = false;
47
46
MP_STATE_PORT (cpy_uart_obj_all )[0 ] = NULL ;
48
47
__HAL_RCC_USART1_CLK_DISABLE ();
49
- HAL_NVIC_DisableIRQ (USART1_IRQn );
48
+ // HAL_NVIC_DisableIRQ(USART1_IRQn);
50
49
#endif
51
50
#ifdef USART2
52
51
reserved_uart [1 ] = false;
53
52
MP_STATE_PORT (cpy_uart_obj_all )[1 ] = NULL ;
54
53
__HAL_RCC_USART2_CLK_DISABLE ();
55
- HAL_NVIC_DisableIRQ (USART2_IRQn );
54
+ // HAL_NVIC_DisableIRQ(USART2_IRQn);
56
55
#endif
57
56
#ifdef USART3
58
57
reserved_uart [2 ] = false;
59
58
MP_STATE_PORT (cpy_uart_obj_all )[2 ] = NULL ;
60
59
__HAL_RCC_USART3_CLK_DISABLE ();
61
- HAL_NVIC_DisableIRQ (USART3_IRQn );
60
+ // HAL_NVIC_DisableIRQ(USART3_IRQn);
62
61
#endif
63
62
#ifdef UART4
64
63
reserved_uart [3 ] = false;
65
64
MP_STATE_PORT (cpy_uart_obj_all )[3 ] = NULL ;
66
65
__HAL_RCC_UART4_CLK_DISABLE ();
67
- HAL_NVIC_DisableIRQ (UART4_IRQn );
66
+ // HAL_NVIC_DisableIRQ(UART4_IRQn);
68
67
#endif
69
68
#ifdef UART5
70
69
reserved_uart [4 ] = false;
71
70
MP_STATE_PORT (cpy_uart_obj_all )[4 ] = NULL ;
72
71
__HAL_RCC_UART5_CLK_DISABLE ();
73
- HAL_NVIC_DisableIRQ (UART5_IRQn );
72
+ // HAL_NVIC_DisableIRQ(UART5_IRQn);
74
73
#endif
75
74
#ifdef USART6
76
75
reserved_uart [5 ] = false;
77
76
MP_STATE_PORT (cpy_uart_obj_all )[5 ] = NULL ;
78
77
__HAL_RCC_USART6_CLK_DISABLE ();
79
- HAL_NVIC_DisableIRQ (USART6_IRQn );
78
+ // HAL_NVIC_DisableIRQ(USART6_IRQn);
80
79
#endif
81
80
//TODO: this technically needs to go to 10 to support F413. Any way to condense?
82
81
}
@@ -97,52 +96,70 @@ STATIC USART_TypeDef * assign_uart_or_throw(busio_uart_obj_t *self, bool pin_eva
97
96
}
98
97
99
98
100
- STATIC void uart_clk_irq_enable (USART_TypeDef * USARTx ) {
99
+ STATIC void uart_clk_irq_enable (busio_uart_obj_t * self , USART_TypeDef * USARTx ) {
101
100
#ifdef USART1
102
101
if (USARTx == USART1 ) {
103
102
reserved_uart [0 ] = true;
104
103
__HAL_RCC_USART1_CLK_ENABLE ();
105
- //HAL_NVIC_SetPriority(USART1_IRQn, 0, 1);
104
+ self -> irq = USART1_IRQn ;
105
+ //HAL_NVIC_SetPriority(USART1_IRQn, 2,1);
106
+ NVIC_SetPriority (USART1_IRQn , 7 );
107
+ NVIC_ClearPendingIRQ (USART1_IRQn );
106
108
HAL_NVIC_EnableIRQ (USART1_IRQn );
107
109
}
108
110
#endif
109
- #ifdef UART2
111
+ #ifdef USART2
110
112
if (USARTx == USART2 ) {
111
113
reserved_uart [1 ] = true;
112
114
__HAL_RCC_USART2_CLK_ENABLE ();
113
- //HAL_NVIC_SetPriority(USART2_IRQn, 0, 1);
115
+ self -> irq = USART2_IRQn ;
116
+ //HAL_NVIC_SetPriority(USART2_IRQn, 2,1);
117
+ NVIC_SetPriority (USART2_IRQn , 7 );
118
+ NVIC_ClearPendingIRQ (USART2_IRQn );
114
119
HAL_NVIC_EnableIRQ (USART2_IRQn );
115
120
}
116
121
#endif
117
122
#ifdef USART3
118
123
if (USARTx == USART3 ) {
119
124
reserved_uart [2 ] = true;
120
125
__HAL_RCC_USART3_CLK_ENABLE ();
121
- //HAL_NVIC_SetPriority(USART3_IRQn, 0, 1);
126
+ self -> irq = USART3_IRQn ;
127
+ //HAL_NVIC_SetPriority(USART3_IRQn, 2,1);
128
+ NVIC_SetPriority (USART3_IRQn , 7 );
129
+ NVIC_ClearPendingIRQ (USART3_IRQn );
122
130
HAL_NVIC_EnableIRQ (USART3_IRQn );
123
131
}
124
132
#endif
125
133
#ifdef UART4
126
134
if (USARTx == UART4 ) {
127
135
reserved_uart [3 ] = true;
128
136
__HAL_RCC_UART4_CLK_ENABLE ();
129
- //HAL_NVIC_SetPriority(UART4_IRQn, 0, 1);
137
+ self -> irq = UART4_IRQn ;
138
+ //HAL_NVIC_SetPriority(UART4_IRQn, 2,1);
139
+ NVIC_SetPriority (UART4_IRQn , 7 );
140
+ NVIC_ClearPendingIRQ (UART4_IRQn );
130
141
HAL_NVIC_EnableIRQ (UART4_IRQn );
131
142
}
132
143
#endif
133
144
#ifdef UART5
134
145
if (USARTx == UART5 ) {
135
146
reserved_uart [4 ] = true;
136
147
__HAL_RCC_UART5_CLK_ENABLE ();
137
- //HAL_NVIC_SetPriority(UART5_IRQn, 0, 1);
148
+ self -> irq = UART5_IRQn ;
149
+ //NVIC_SetPriority(UART5_IRQn, 7);
150
+ NVIC_SetPriority (UART5_IRQn , 7 );
151
+ NVIC_ClearPendingIRQ (UART5_IRQn );
138
152
HAL_NVIC_EnableIRQ (UART5_IRQn );
139
153
}
140
154
#endif
141
155
#ifdef USART6
142
156
if (USARTx == USART6 ) {
143
157
reserved_uart [5 ] = true;
144
158
__HAL_RCC_USART6_CLK_ENABLE ();
145
- //HAL_NVIC_SetPriority(USART6_IRQn, 0, 1);
159
+ self -> irq = USART6_IRQn ;
160
+ //NVIC_SetPriority(USART6_IRQn, 7);
161
+ NVIC_SetPriority (USART6_IRQn , 7 );
162
+ NVIC_ClearPendingIRQ (USART6_IRQn );
146
163
HAL_NVIC_EnableIRQ (USART6_IRQn );
147
164
}
148
165
#endif
@@ -248,7 +265,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
248
265
HAL_GPIO_Init (pin_port (rx -> port ), & GPIO_InitStruct );
249
266
}
250
267
251
- uart_clk_irq_enable (USARTx );
268
+ uart_clk_irq_enable (self , USARTx );
252
269
253
270
self -> handle .Instance = USARTx ;
254
271
self -> handle .Init .BaudRate = baudrate ;
@@ -297,6 +314,9 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
297
314
reset_pin_number (self -> rx -> pin -> port ,self -> rx -> pin -> number );
298
315
self -> tx = mp_const_none ;
299
316
self -> rx = mp_const_none ;
317
+ gc_free (self -> rbuf .buf );
318
+ self -> rbuf .size = 0 ;
319
+ self -> rbuf .iput = self -> rbuf .iget = 0 ;
300
320
}
301
321
302
322
// Read characters.
@@ -318,7 +338,8 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
318
338
}
319
339
320
340
// Halt reception
321
- HAL_UART_AbortReceive_IT (& self -> handle );
341
+ //HAL_UART_AbortReceive_IT(&self->handle);
342
+ NVIC_DisableIRQ (self -> irq );
322
343
323
344
// copy received data
324
345
rx_bytes = ringbuf_count (& self -> rbuf );
@@ -327,9 +348,10 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
327
348
data [i ] = ringbuf_get (& self -> rbuf );
328
349
}
329
350
330
- if (HAL_UART_Receive_IT (& self -> handle , & self -> rx_char , 1 ) != HAL_OK ) {
331
- mp_raise_ValueError (translate ("HAL recieve IT start error" ));
332
- }
351
+ NVIC_EnableIRQ (self -> irq );
352
+ // if (HAL_UART_Receive_IT(&self->handle, &self->rx_char, 1) != HAL_OK) {
353
+ // mp_raise_ValueError(translate("HAL recieve IT re-start error"));
354
+ // }
333
355
334
356
if (rx_bytes == 0 ) {
335
357
* errcode = EAGAIN ;
@@ -359,15 +381,18 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle)
359
381
busio_uart_obj_t * context = (busio_uart_obj_t * )MP_STATE_PORT (cpy_uart_obj_all )[i ];
360
382
if (handle == & context -> handle ) {
361
383
ringbuf_put_n (& context -> rbuf , & context -> rx_char , 1 );
362
- HAL_UART_Receive_IT (handle , & context -> rx_char , 1 );
363
- return ;
384
+ HAL_StatusTypeDef result = HAL_UART_Receive_IT (handle , & context -> rx_char , 1 );
385
+ if (result != HAL_OK ) {
386
+ mp_raise_RuntimeError (translate ("UART rx restart error" ));
387
+ }
388
+ break ;
364
389
}
365
390
}
366
391
}
367
392
368
393
void HAL_UART_ErrorCallback (UART_HandleTypeDef * UartHandle )
369
394
{
370
- mp_raise_RuntimeError (translate ("UART Callback Error " ));
395
+ mp_raise_RuntimeError (translate ("UART Error Callback hit " ));
371
396
}
372
397
373
398
uint32_t common_hal_busio_uart_get_baudrate (busio_uart_obj_t * self ) {
@@ -396,16 +421,18 @@ uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self) {
396
421
397
422
void common_hal_busio_uart_clear_rx_buffer (busio_uart_obj_t * self ) {
398
423
// Halt reception
399
- HAL_UART_AbortReceive_IT (& self -> handle );
424
+ //HAL_UART_AbortReceive_IT(&self->handle);
425
+ NVIC_DisableIRQ (self -> irq );
400
426
ringbuf_clear (& self -> rbuf );
401
- HAL_UART_Receive_IT (& self -> handle , & self -> rx_char , 1 );
427
+ NVIC_EnableIRQ (self -> irq );
428
+ //HAL_UART_Receive_IT(&self->handle, &self->rx_char, 1);
402
429
}
403
430
404
431
bool common_hal_busio_uart_ready_to_tx (busio_uart_obj_t * self ) {
405
432
return true;
406
433
}
407
434
408
- static void call_hal_irq (int uart_num ) {
435
+ STATIC void call_hal_irq (int uart_num ) {
409
436
//Create casted context pointer
410
437
busio_uart_obj_t * context = (busio_uart_obj_t * )MP_STATE_PORT (cpy_uart_obj_all )[uart_num - 1 ];
411
438
if (context != NULL ) {
@@ -439,4 +466,3 @@ void UART5_IRQHandler(void) {
439
466
void USART6_IRQHandler (void ) {
440
467
call_hal_irq (6 );
441
468
}
442
-
0 commit comments