Skip to content

Commit

Permalink
stmhal/uart: Add support for UART7 and UART8 on F7 MCUs.
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgeorge committed Dec 5, 2016
1 parent 27a503f commit aaab6a9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
4 changes: 3 additions & 1 deletion stmhal/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,10 @@ extern const struct _mp_obj_module_t mp_module_network;

#if defined(MCU_SERIES_F7)
#define PYB_EXTI_NUM_VECTORS (24)
#define MICROPY_HW_MAX_UART (8)
#else
#define PYB_EXTI_NUM_VECTORS (23)
#define MICROPY_HW_MAX_UART (6)
#endif

#define MP_STATE_PORT MP_STATE_VM
Expand All @@ -215,7 +217,7 @@ extern const struct _mp_obj_module_t mp_module_network;
struct _pyb_uart_obj_t *pyb_stdio_uart; \
\
/* pointers to all UART objects (if they have been created) */ \
struct _pyb_uart_obj_t *pyb_uart_obj_all[6]; \
struct _pyb_uart_obj_t *pyb_uart_obj_all[MICROPY_HW_MAX_UART]; \
\
/* pointers to all CAN objects (if they have been created) */ \
struct _pyb_can_obj_t *pyb_can_obj_all[2]; \
Expand Down
16 changes: 16 additions & 0 deletions stmhal/stm32_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,22 @@ void USART6_IRQHandler(void) {
IRQ_EXIT(USART6_IRQn);
}

#if defined(MICROPY_HW_UART7_TX)
void UART7_IRQHandler(void) {
IRQ_ENTER(UART7_IRQn);
uart_irq_handler(7);
IRQ_EXIT(UART7_IRQn);
}
#endif

#if defined(MICROPY_HW_UART8_TX)
void UART8_IRQHandler(void) {
IRQ_ENTER(UART8_IRQn);
uart_irq_handler(8);
IRQ_EXIT(UART8_IRQn);
}
#endif

#if MICROPY_HW_ENABLE_CAN
void CAN1_RX0_IRQHandler(void) {
IRQ_ENTER(CAN1_RX0_IRQn);
Expand Down
53 changes: 50 additions & 3 deletions stmhal/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
#include "irq.h"
#include "genhdr/pins.h"

//TODO: Add UART7/8 support for MCU_SERIES_F7

/// \moduleref pyb
/// \class UART - duplex serial communication bus
///
Expand Down Expand Up @@ -206,6 +204,28 @@ STATIC bool uart_init2(pyb_uart_obj_t *uart_obj) {
break;
#endif

#if defined(MICROPY_HW_UART7_TX) && defined(MICROPY_HW_UART7_RX)
case PYB_UART_7:
uart_unit = 7;
UARTx = UART7;
irqn = UART7_IRQn;
pins[0] = &MICROPY_HW_UART7_TX;
pins[1] = &MICROPY_HW_UART7_RX;
__UART7_CLK_ENABLE();
break;
#endif

#if defined(MICROPY_HW_UART8_TX) && defined(MICROPY_HW_UART8_RX)
case PYB_UART_8:
uart_unit = 8;
UARTx = UART8;
irqn = UART8_IRQn;
pins[0] = &MICROPY_HW_UART8_TX;
pins[1] = &MICROPY_HW_UART8_RX;
__UART8_CLK_ENABLE();
break;
#endif

default:
// UART does not exist or is not configured for this board
return false;
Expand Down Expand Up @@ -537,7 +557,19 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con

// compute actual baudrate that was configured
// (this formula assumes UART_OVERSAMPLING_16)
uint32_t actual_baudrate;
uint32_t actual_baudrate = 0;
#if defined(MCU_SERIES_F7)
UART_ClockSourceTypeDef clocksource = UART_CLOCKSOURCE_UNDEFINED;
UART_GETCLOCKSOURCE(&self->uart, clocksource);
switch (clocksource) {
case UART_CLOCKSOURCE_PCLK1: actual_baudrate = HAL_RCC_GetPCLK1Freq(); break;
case UART_CLOCKSOURCE_PCLK2: actual_baudrate = HAL_RCC_GetPCLK2Freq(); break;
case UART_CLOCKSOURCE_HSI: actual_baudrate = HSI_VALUE; break;
case UART_CLOCKSOURCE_SYSCLK: actual_baudrate = HAL_RCC_GetSysClockFreq(); break;
case UART_CLOCKSOURCE_LSE: actual_baudrate = LSE_VALUE; break;
case UART_CLOCKSOURCE_UNDEFINED: break;
}
#else
if (self->uart.Instance == USART1
#if defined(USART6)
|| self->uart.Instance == USART6
Expand All @@ -547,6 +579,7 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con
} else {
actual_baudrate = HAL_RCC_GetPCLK1Freq();
}
#endif
actual_baudrate /= self->uart.Instance->BRR;

// check we could set the baudrate within 5%
Expand Down Expand Up @@ -694,6 +727,20 @@ STATIC mp_obj_t pyb_uart_deinit(mp_obj_t self_in) {
__USART6_RELEASE_RESET();
__USART6_CLK_DISABLE();
#endif
#if defined(UART7)
} else if (uart->Instance == UART7) {
HAL_NVIC_DisableIRQ(UART7_IRQn);
__UART7_FORCE_RESET();
__UART7_RELEASE_RESET();
__UART7_CLK_DISABLE();
#endif
#if defined(UART8)
} else if (uart->Instance == UART8) {
HAL_NVIC_DisableIRQ(UART8_IRQn);
__UART8_FORCE_RESET();
__UART8_RELEASE_RESET();
__UART8_CLK_DISABLE();
#endif
}
return mp_const_none;
}
Expand Down
2 changes: 2 additions & 0 deletions stmhal/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ typedef enum {
PYB_UART_4 = 4,
PYB_UART_5 = 5,
PYB_UART_6 = 6,
PYB_UART_7 = 7,
PYB_UART_8 = 8,
} pyb_uart_t;

typedef struct _pyb_uart_obj_t pyb_uart_obj_t;
Expand Down

0 comments on commit aaab6a9

Please sign in to comment.