This commit is contained in:
Hierophect 2019-10-04 14:37:18 -04:00
parent 51901f7de0
commit 0b85172ba6
6 changed files with 158 additions and 16 deletions

View File

@ -152,6 +152,7 @@ SRC_STM32 = \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_utils.c \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_utils.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_exti.c \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_exti.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_usart.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c \
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c \ stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c \

View File

@ -63,7 +63,7 @@
#define HAL_SPI_MODULE_ENABLED #define HAL_SPI_MODULE_ENABLED
/* #define HAL_TIM_MODULE_ENABLED */ /* #define HAL_TIM_MODULE_ENABLED */
#define HAL_UART_MODULE_ENABLED #define HAL_UART_MODULE_ENABLED
/* #define HAL_USART_MODULE_ENABLED */ #define HAL_USART_MODULE_ENABLED
/* #define HAL_IRDA_MODULE_ENABLED */ /* #define HAL_IRDA_MODULE_ENABLED */
/* #define HAL_SMARTCARD_MODULE_ENABLED */ /* #define HAL_SMARTCARD_MODULE_ENABLED */
/* #define HAL_WWDG_MODULE_ENABLED */ /* #define HAL_WWDG_MODULE_ENABLED */

View File

@ -35,34 +35,157 @@
#include "py/stream.h" #include "py/stream.h"
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
#include "common-hal/microcontroller/Pin.h"
#include "stm32f4xx_hal.h"
#include "tick.h" #include "tick.h"
STATIC bool reserved_uart[10];
void uart_reset(void) {
//ugh. reduce this
#ifdef USART1
reserved_uart[0] = false;
__HAL_RCC_USART1_CLK_DISABLE();
#endif
#ifdef USART2
reserved_uart[1] = false;
__HAL_RCC_USART2_CLK_DISABLE();
#endif
#ifdef USART3
reserved_uart[2] = false;
__HAL_RCC_USART3_CLK_DISABLE();
#endif
#ifdef UART4
reserved_uart[3] = false;
__HAL_RCC_UART4_CLK_DISABLE();
#endif
#ifdef UART5
reserved_uart[4] = false;
__HAL_RCC_UART5_CLK_DISABLE();
#endif
#ifdef USART6
reserved_uart[5] = false;
__HAL_RCC_USART6_CLK_DISABLE();
#endif
}
void common_hal_busio_uart_construct(busio_uart_obj_t *self, void common_hal_busio_uart_construct(busio_uart_obj_t *self,
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate, const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate,
uint8_t bits, uart_parity_t parity, uint8_t stop, mp_float_t timeout, uint8_t bits, uart_parity_t parity, uint8_t stop, mp_float_t timeout,
uint16_t receiver_buffer_size) { uint16_t receiver_buffer_size) {
GPIO_InitStruct.Pin = pin_mask(10)|pin_mask(11); //match pins to UART objects
USART_TypeDef * USARTx;
uint8_t tx_len = sizeof(mcu_uart_tx_list)/sizeof(*mcu_uart_tx_list);
uint8_t rx_len = sizeof(mcu_uart_rx_list)/sizeof(*mcu_uart_rx_list);
bool uart_taken = false;
//tx
for(uint i=0; i<tx_len;i++) {
if (mcu_uart_tx_list[i].pin == tx) {
//rx
for(uint j=0; j<rx_len;j++) {
if (mcu_uart_rx_list[j].pin == rx
&& mcu_uart_rx_list[j].uart_index == mcu_uart_tx_list[i].uart_index) {
//keep looking if the UART is taken, edge case
if(reserved_uart[mcu_uart_tx_list[i].uart_index-1]) {
uart_taken = true;
continue;
}
//store pins if not
self->tx = &mcu_uart_tx_list[i];
self->rx = &mcu_uart_rx_list[j];
break;
}
}
}
}
//handle typedef selection, errors
if(self->tx!=NULL && self->rx!=NULL) {
USARTx = mcu_uart_banks[self->tx->uart_index-1];
} else {
if (uart_taken) {
mp_raise_ValueError(translate("Hardware busy, try alternative pins"));
} else {
mp_raise_ValueError(translate("Invalid UART pin selection"));
}
}
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin_mask(tx->number);
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART3; GPIO_InitStruct.Alternate = self->tx->altfn_index;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); HAL_GPIO_Init(pin_port(tx->port), &GPIO_InitStruct);
__HAL_RCC_USART2_CLK_ENABLE(); GPIO_InitStruct.Pin = pin_mask(rx->number);
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = self->rx->altfn_index;
HAL_GPIO_Init(pin_port(rx->port), &GPIO_InitStruct);
huart2.Instance = USART2; #ifdef USART1
huart2.Init.BaudRate = 115200; if(USARTx==USART1) {
huart2.Init.WordLength = UART_WORDLENGTH_8B; reserved_uart[0] = true;
huart2.Init.StopBits = UART_STOPBITS_1; __HAL_RCC_USART1_CLK_ENABLE();
huart2.Init.Parity = UART_PARITY_NONE; mp_printf(&mp_plat_print, "USART1");
huart2.Init.Mode = UART_MODE_TX_RX; }
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; #endif
huart2.Init.OverSampling = UART_OVERSAMPLING_16; #ifdef UART2
if (HAL_UART_Init(&huart2) != HAL_OK) if(USARTx==USART2) {
reserved_uart[1] = true;
__HAL_RCC_USART2_CLK_ENABLE();
mp_printf(&mp_plat_print, "USART2");
}
#endif
#ifdef USART3
if(USARTx==USART3) {
reserved_uart[2] = true;
__HAL_RCC_USART3_CLK_ENABLE();
mp_printf(&mp_plat_print, "USART3");
}
#endif
#ifdef UART4
if(USARTx==UART4) {
reserved_uart[3] = true;
__HAL_RCC_UART4_CLK_ENABLE();
mp_printf(&mp_plat_print, "USART4");
}
#endif
#ifdef UART5
if(USARTx==UART5) {
reserved_uart[4] = true;
__HAL_RCC_UART5_CLK_ENABLE();
mp_printf(&mp_plat_print, "USART5");
}
#endif
#ifdef USART6
if(USARTx==USART6) {
reserved_uart[5] = true;
__HAL_RCC_USART6_CLK_ENABLE();
mp_printf(&mp_plat_print, "USART6");
}
#endif
self->handle.Instance = USARTx;
self->handle.Init.BaudRate = 115200;
self->handle.Init.WordLength = UART_WORDLENGTH_8B;
self->handle.Init.StopBits = UART_STOPBITS_1;
self->handle.Init.Parity = UART_PARITY_NONE;
self->handle.Init.Mode = UART_MODE_TX_RX;
self->handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
self->handle.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&self->handle) != HAL_OK)
{ {
mp_raise_NotImplementedError(translate("UART explode")); mp_raise_ValueError(translate("UART Init Error"));
} }
claim_pin(tx);
claim_pin(rx);
} }
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) { bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
@ -74,11 +197,23 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
// Read characters. // Read characters.
size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) { size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) {
if (HAL_UART_Receive(&self->handle, data, (uint16_t)len, 500) == HAL_OK) {
return len;
} else {
mp_raise_ValueError(translate("UART read error"));
}
return 0; return 0;
} }
// Write characters. // Write characters.
size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) { size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) {
//const char aTxBuffer[] = "This is the internal message";
if (HAL_UART_Transmit(&self->handle, (uint8_t *)data, len, 500) == HAL_OK) {
return len;
} else {
mp_raise_ValueError(translate("UART write error"));
}
return 0; return 0;
} }

View File

@ -28,11 +28,13 @@
#define MICROPY_INCLUDED_STM32F4_COMMON_HAL_BUSIO_UART_H #define MICROPY_INCLUDED_STM32F4_COMMON_HAL_BUSIO_UART_H
#include "common-hal/microcontroller/Pin.h" #include "common-hal/microcontroller/Pin.h"
#include "stm32f4/periph.h"
#include "py/obj.h" #include "py/obj.h"
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
UART_HandleTypeDef handle;
const mcu_uart_tx_obj_t *tx; const mcu_uart_tx_obj_t *tx;
const mcu_uart_rx_obj_t *rx; const mcu_uart_rx_obj_t *rx;
uint8_t character_bits; uint8_t character_bits;
@ -43,4 +45,6 @@ typedef struct {
uint8_t* buffer; uint8_t* buffer;
} busio_uart_obj_t; } busio_uart_obj_t;
void uart_reset(void);
#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_BUSIO_UART_H #endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_BUSIO_UART_H

View File

@ -43,7 +43,7 @@ extern const mcu_spi_nss_obj_t mcu_spi_nss_list[6];
//UART //UART
extern USART_TypeDef * mcu_uart_banks[6]; extern USART_TypeDef * mcu_uart_banks[6];
bool mcu_uart_has_usart[6] bool mcu_uart_has_usart[6];
extern const mcu_uart_tx_obj_t mcu_uart_tx_list[12]; extern const mcu_uart_tx_obj_t mcu_uart_tx_list[12];
extern const mcu_uart_rx_obj_t mcu_uart_rx_list[12]; extern const mcu_uart_rx_obj_t mcu_uart_rx_list[12];

View File

@ -33,6 +33,7 @@
#include "common-hal/microcontroller/Pin.h" #include "common-hal/microcontroller/Pin.h"
#include "common-hal/busio/I2C.h" #include "common-hal/busio/I2C.h"
#include "common-hal/busio/SPI.h" #include "common-hal/busio/SPI.h"
#include "common-hal/busio/UART.h"
#include "stm32f4/clocks.h" #include "stm32f4/clocks.h"
#include "stm32f4/gpio.h" #include "stm32f4/gpio.h"
@ -57,6 +58,7 @@ void reset_port(void) {
reset_all_pins(); reset_all_pins();
i2c_reset(); i2c_reset();
spi_reset(); spi_reset();
uart_reset();
} }
void reset_to_bootloader(void) { void reset_to_bootloader(void) {