Skip to content

Commit a596213

Browse files
committed
non-functional WIP
1 parent af28474 commit a596213

File tree

3 files changed

+67
-28
lines changed

3 files changed

+67
-28
lines changed

ports/stm32f4/common-hal/busio/UART.c

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,45 +38,44 @@
3838
#include "tick.h"
3939
#include "stm32f4xx_hal.h"
4040

41-
4241
STATIC bool reserved_uart[MAX_UART];
4342

4443
void uart_reset(void) {
4544
#ifdef USART1
4645
reserved_uart[0] = false;
4746
MP_STATE_PORT(cpy_uart_obj_all)[0] = NULL;
4847
__HAL_RCC_USART1_CLK_DISABLE();
49-
HAL_NVIC_DisableIRQ(USART1_IRQn);
48+
//HAL_NVIC_DisableIRQ(USART1_IRQn);
5049
#endif
5150
#ifdef USART2
5251
reserved_uart[1] = false;
5352
MP_STATE_PORT(cpy_uart_obj_all)[1] = NULL;
5453
__HAL_RCC_USART2_CLK_DISABLE();
55-
HAL_NVIC_DisableIRQ(USART2_IRQn);
54+
//HAL_NVIC_DisableIRQ(USART2_IRQn);
5655
#endif
5756
#ifdef USART3
5857
reserved_uart[2] = false;
5958
MP_STATE_PORT(cpy_uart_obj_all)[2] = NULL;
6059
__HAL_RCC_USART3_CLK_DISABLE();
61-
HAL_NVIC_DisableIRQ(USART3_IRQn);
60+
//HAL_NVIC_DisableIRQ(USART3_IRQn);
6261
#endif
6362
#ifdef UART4
6463
reserved_uart[3] = false;
6564
MP_STATE_PORT(cpy_uart_obj_all)[3] = NULL;
6665
__HAL_RCC_UART4_CLK_DISABLE();
67-
HAL_NVIC_DisableIRQ(UART4_IRQn);
66+
//HAL_NVIC_DisableIRQ(UART4_IRQn);
6867
#endif
6968
#ifdef UART5
7069
reserved_uart[4] = false;
7170
MP_STATE_PORT(cpy_uart_obj_all)[4] = NULL;
7271
__HAL_RCC_UART5_CLK_DISABLE();
73-
HAL_NVIC_DisableIRQ(UART5_IRQn);
72+
//HAL_NVIC_DisableIRQ(UART5_IRQn);
7473
#endif
7574
#ifdef USART6
7675
reserved_uart[5] = false;
7776
MP_STATE_PORT(cpy_uart_obj_all)[5] = NULL;
7877
__HAL_RCC_USART6_CLK_DISABLE();
79-
HAL_NVIC_DisableIRQ(USART6_IRQn);
78+
//HAL_NVIC_DisableIRQ(USART6_IRQn);
8079
#endif
8180
//TODO: this technically needs to go to 10 to support F413. Any way to condense?
8281
}
@@ -97,52 +96,70 @@ STATIC USART_TypeDef * assign_uart_or_throw(busio_uart_obj_t *self, bool pin_eva
9796
}
9897

9998

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) {
101100
#ifdef USART1
102101
if(USARTx==USART1) {
103102
reserved_uart[0] = true;
104103
__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);
106108
HAL_NVIC_EnableIRQ(USART1_IRQn);
107109
}
108110
#endif
109-
#ifdef UART2
111+
#ifdef USART2
110112
if(USARTx==USART2) {
111113
reserved_uart[1] = true;
112114
__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);
114119
HAL_NVIC_EnableIRQ(USART2_IRQn);
115120
}
116121
#endif
117122
#ifdef USART3
118123
if(USARTx==USART3) {
119124
reserved_uart[2] = true;
120125
__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);
122130
HAL_NVIC_EnableIRQ(USART3_IRQn);
123131
}
124132
#endif
125133
#ifdef UART4
126134
if(USARTx==UART4) {
127135
reserved_uart[3] = true;
128136
__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);
130141
HAL_NVIC_EnableIRQ(UART4_IRQn);
131142
}
132143
#endif
133144
#ifdef UART5
134145
if(USARTx==UART5) {
135146
reserved_uart[4] = true;
136147
__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);
138152
HAL_NVIC_EnableIRQ(UART5_IRQn);
139153
}
140154
#endif
141155
#ifdef USART6
142156
if(USARTx==USART6) {
143157
reserved_uart[5] = true;
144158
__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);
146163
HAL_NVIC_EnableIRQ(USART6_IRQn);
147164
}
148165
#endif
@@ -248,7 +265,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
248265
HAL_GPIO_Init(pin_port(rx->port), &GPIO_InitStruct);
249266
}
250267

251-
uart_clk_irq_enable(USARTx);
268+
uart_clk_irq_enable(self,USARTx);
252269

253270
self->handle.Instance = USARTx;
254271
self->handle.Init.BaudRate = baudrate;
@@ -297,6 +314,9 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
297314
reset_pin_number(self->rx->pin->port,self->rx->pin->number);
298315
self->tx = mp_const_none;
299316
self->rx = mp_const_none;
317+
gc_free(self->rbuf.buf);
318+
self->rbuf.size = 0;
319+
self->rbuf.iput = self->rbuf.iget = 0;
300320
}
301321

302322
// Read characters.
@@ -318,7 +338,8 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
318338
}
319339

320340
// Halt reception
321-
HAL_UART_AbortReceive_IT(&self->handle);
341+
//HAL_UART_AbortReceive_IT(&self->handle);
342+
NVIC_DisableIRQ(self->irq);
322343

323344
// copy received data
324345
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
327348
data[i] = ringbuf_get(&self->rbuf);
328349
}
329350

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+
// }
333355

334356
if (rx_bytes == 0) {
335357
*errcode = EAGAIN;
@@ -359,15 +381,18 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle)
359381
busio_uart_obj_t * context = (busio_uart_obj_t *)MP_STATE_PORT(cpy_uart_obj_all)[i];
360382
if(handle == &context->handle) {
361383
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;
364389
}
365390
}
366391
}
367392

368393
void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
369394
{
370-
mp_raise_RuntimeError(translate("UART Callback Error"));
395+
mp_raise_RuntimeError(translate("UART Error Callback hit"));
371396
}
372397

373398
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) {
396421

397422
void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self) {
398423
// Halt reception
399-
HAL_UART_AbortReceive_IT(&self->handle);
424+
//HAL_UART_AbortReceive_IT(&self->handle);
425+
NVIC_DisableIRQ(self->irq);
400426
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);
402429
}
403430

404431
bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self) {
405432
return true;
406433
}
407434

408-
static void call_hal_irq(int uart_num) {
435+
STATIC void call_hal_irq(int uart_num) {
409436
//Create casted context pointer
410437
busio_uart_obj_t * context = (busio_uart_obj_t *)MP_STATE_PORT(cpy_uart_obj_all)[uart_num-1];
411438
if(context != NULL) {
@@ -439,4 +466,3 @@ void UART5_IRQHandler(void) {
439466
void USART6_IRQHandler(void) {
440467
call_hal_irq(6);
441468
}
442-

ports/stm32f4/common-hal/busio/UART.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
typedef struct {
3838
mp_obj_base_t base;
3939
UART_HandleTypeDef handle;
40+
IRQn_Type irq;
4041
const mcu_uart_tx_obj_t *tx;
4142
const mcu_uart_rx_obj_t *rx;
4243

@@ -52,5 +53,8 @@ void uart_reset(void);
5253
void USART1_IRQHandler(void);
5354
void USART2_IRQHandler(void);
5455
void USART3_IRQHandler(void);
55-
void UART_IRQHandler(void);
56+
void UART4_IRQHandler(void);
57+
void UART5_IRQHandler(void);
58+
void USART6_IRQHandler(void);
59+
// void UART_IRQHandler(void);
5660
#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_BUSIO_UART_H

ports/stm32f4/tick.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ uint32_t HAL_GetTick(void) //override ST HAL
6262
void tick_init() {
6363
uint32_t ticks_per_ms = SystemCoreClock/ 1000;
6464
SysTick_Config(ticks_per_ms); // interrupt is enabled
65+
66+
NVIC_EnableIRQ(SysTick_IRQn);
67+
// Set all peripheral interrupt priorities to the lowest priority by default.
68+
// for (uint16_t i = 0; i < PERIPH_COUNT_IRQn; i++) {
69+
// NVIC_SetPriority(i, (1UL << __NVIC_PRIO_BITS) - 1UL);
70+
// }
71+
// Bump up the systick interrupt so nothing else interferes with timekeeping.
72+
NVIC_SetPriority(SysTick_IRQn, 0);
73+
NVIC_SetPriority(OTG_FS_IRQn, 1);
6574
}
6675

6776
void tick_delay(uint32_t us) {

0 commit comments

Comments
 (0)