@ -36,7 +36,7 @@
# include "supervisor/shared/translate.h"
# include "tick.h"
# include "stm32f4xx_hal.h"
# include "stm32f4xx_hal.h"
# define ALL_UARTS 0xFFFF
@ -55,7 +55,7 @@ void uart_reset(void) {
uart_clock_disable ( ALL_UARTS ) ;
}
STATIC USART_TypeDef * assign_uart_or_throw ( busio_uart_obj_t * self , bool pin_eval ,
STATIC USART_TypeDef * assign_uart_or_throw ( busio_uart_obj_t * self , bool pin_eval ,
int uart_index , bool uart_taken ) {
if ( pin_eval ) {
//assign a root pointer pointer for IRQ
@ -82,7 +82,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t* self,
uint8_t rx_len = sizeof ( mcu_uart_rx_list ) / sizeof ( * mcu_uart_rx_list ) ;
bool uart_taken = false ;
uint8_t uart_index = 0 ; //origin 0 corrected
//Can have both pins, or either
if ( ( tx ! = mp_const_none ) & & ( rx ! = mp_const_none ) ) {
//normal find loop if both pins exist
@ -90,7 +90,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t* self,
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
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 ] ) {
@ -106,7 +106,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t* self,
}
}
uart_index = self - > tx - > uart_index - 1 ;
USARTx = assign_uart_or_throw ( self , ( self - > tx ! = NULL & & self - > rx ! = NULL ) ,
USARTx = assign_uart_or_throw ( self , ( self - > tx ! = NULL & & self - > rx ! = NULL ) ,
uart_index , uart_taken ) ;
} else if ( tx = = mp_const_none ) {
//If there is no tx, run only rx
@ -123,7 +123,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t* self,
}
}
uart_index = self - > rx - > uart_index - 1 ;
USARTx = assign_uart_or_throw ( self , ( self - > rx ! = NULL ) ,
USARTx = assign_uart_or_throw ( self , ( self - > rx ! = NULL ) ,
uart_index , uart_taken ) ;
} else if ( rx = = mp_const_none ) {
//If there is no rx, run only tx
@ -140,7 +140,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t* self,
}
}
uart_index = self - > tx - > uart_index - 1 ;
USARTx = assign_uart_or_throw ( self , ( self - > tx ! = NULL ) ,
USARTx = assign_uart_or_throw ( self , ( self - > tx ! = NULL ) ,
uart_index , uart_taken ) ;
} else {
//both pins cannot be empty
@ -173,7 +173,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t* self,
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 ;
GPIO_InitStruct . Alternate = self - > rx - > altfn_index ;
HAL_GPIO_Init ( pin_port ( rx - > port ) , & GPIO_InitStruct ) ;
}
@ -186,7 +186,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t* self,
self - > handle . Init . BaudRate = baudrate ;
self - > handle . Init . WordLength = ( bits = = 9 ) ? UART_WORDLENGTH_9B : UART_WORDLENGTH_8B ;
self - > handle . Init . StopBits = ( stop > 1 ) ? UART_STOPBITS_2 : UART_STOPBITS_1 ;
self - > handle . Init . Parity = ( parity = = PARITY_ODD ) ? UART_PARITY_ODD :
self - > handle . Init . Parity = ( parity = = PARITY_ODD ) ? UART_PARITY_ODD :
( parity = = PARITY_EVEN ) ? UART_PARITY_EVEN :
UART_PARITY_NONE ;
self - > handle . Init . Mode = ( self - > tx ! = NULL & & self - > rx ! = NULL ) ? UART_MODE_TX_RX :
@ -234,7 +234,7 @@ bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
void common_hal_busio_uart_deinit ( busio_uart_obj_t * self ) {
if ( common_hal_busio_uart_deinited ( self ) ) return ;
reset_pin_number ( self - > tx - > pin - > port , self - > tx - > pin - > number ) ;
reset_pin_number ( self - > rx - > pin - > port , self - > rx - > pin - > number ) ;
self - > tx = mp_const_none ;
@ -279,7 +279,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
* errcode = EAGAIN ;
return MP_STREAM_ERROR ;
}
return rx_bytes ;
return rx_bytes ;
}
// Write characters.
@ -290,13 +290,15 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
bool write_err = false ; //write error shouldn't disable interrupts
HAL_NVIC_DisableIRQ ( self - > irq ) ;
if ( HAL_UART_Transmit ( & self - > handle , ( uint8_t * ) data , len , self- > timeout_ms ) ! = HAL_OK ) {
if ( HAL_UART_Transmit ( & self - > handle , ( uint8_t * ) data , len , HAL_MAX_DELAY ) ! = HAL_OK ) {
write_err = true ;
}
HAL_UART_Receive_IT ( & self - > handle , & self - > rx_char , 1 ) ;
HAL_NVIC_EnableIRQ ( self - > irq ) ;
if ( write_err ) mp_raise_ValueError ( translate ( " UART write error " ) ) ;
if ( write_err ) {
mp_raise_ValueError ( translate ( " UART write error " ) ) ;
}
return len ;
}
@ -312,7 +314,7 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *handle)
}
ringbuf_put_n ( & context - > rbuf , & context - > rx_char , 1 ) ;
errflag = HAL_UART_Receive_IT ( handle , & context - > rx_char , 1 ) ;
return ;
}
}
@ -360,6 +362,14 @@ void common_hal_busio_uart_set_baudrate(busio_uart_obj_t *self, uint32_t baudrat
self - > baudrate = baudrate ;
}
mp_float_t common_hal_busio_uart_get_timeout ( busio_uart_obj_t * self ) {
return ( mp_float_t ) ( self - > timeout_ms / 1000.0f ) ;
}
void common_hal_busio_uart_set_timeout ( busio_uart_obj_t * self , mp_float_t timeout ) {
self - > timeout_ms = timeout * 1000 ;
}
uint32_t common_hal_busio_uart_rx_characters_available ( busio_uart_obj_t * self ) {
return ringbuf_count ( & self - > rbuf ) ;
}
@ -414,71 +424,71 @@ STATIC void uart_clock_enable(uint16_t mask) {
if ( mask & ( 1 < < 0 ) ) {
__HAL_RCC_USART1_FORCE_RESET ( ) ;
__HAL_RCC_USART1_RELEASE_RESET ( ) ;
__HAL_RCC_USART1_CLK_ENABLE ( ) ;
__HAL_RCC_USART1_CLK_ENABLE ( ) ;
}
# endif
# ifdef USART2
if ( mask & ( 1 < < 1 ) ) {
__HAL_RCC_USART2_FORCE_RESET ( ) ;
__HAL_RCC_USART2_RELEASE_RESET ( ) ;
__HAL_RCC_USART2_CLK_ENABLE ( ) ;
__HAL_RCC_USART2_CLK_ENABLE ( ) ;
}
# endif
# ifdef USART3
if ( mask & ( 1 < < 2 ) ) {
__HAL_RCC_USART3_FORCE_RESET ( ) ;
__HAL_RCC_USART3_RELEASE_RESET ( ) ;
__HAL_RCC_USART3_CLK_ENABLE ( ) ;
__HAL_RCC_USART3_CLK_ENABLE ( ) ;
}
# endif
# ifdef UART4
if ( mask & ( 1 < < 3 ) ) {
__HAL_RCC_UART4_FORCE_RESET ( ) ;
__HAL_RCC_UART4_RELEASE_RESET ( ) ;
__HAL_RCC_UART4_CLK_ENABLE ( ) ;
__HAL_RCC_UART4_CLK_ENABLE ( ) ;
}
# endif
# ifdef UART5
if ( mask & ( 1 < < 4 ) ) {
__HAL_RCC_UART5_FORCE_RESET ( ) ;
__HAL_RCC_UART5_RELEASE_RESET ( ) ;
__HAL_RCC_UART5_CLK_ENABLE ( ) ;
__HAL_RCC_UART5_CLK_ENABLE ( ) ;
}
# endif
# ifdef USART6
if ( mask & ( 1 < < 5 ) ) {
__HAL_RCC_USART6_FORCE_RESET ( ) ;
__HAL_RCC_USART6_RELEASE_RESET ( ) ;
__HAL_RCC_USART6_CLK_ENABLE ( ) ;
}
__HAL_RCC_USART6_CLK_ENABLE ( ) ;
}
# endif
# ifdef UART7
if ( mask & ( 1 < < 6 ) ) {
__HAL_RCC_UART7_FORCE_RESET ( ) ;
__HAL_RCC_UART7_RELEASE_RESET ( ) ;
__HAL_RCC_UART7_CLK_ENABLE ( ) ;
}
__HAL_RCC_UART7_CLK_ENABLE ( ) ;
}
# endif
# ifdef UART8
if ( mask & ( 1 < < 7 ) ) {
__HAL_RCC_UART8_FORCE_RESET ( ) ;
__HAL_RCC_UART8_RELEASE_RESET ( ) ;
__HAL_RCC_UART8_CLK_ENABLE ( ) ;
}
__HAL_RCC_UART8_CLK_ENABLE ( ) ;
}
# endif
# ifdef UART9
if ( mask & ( 1 < < 8 ) ) {
__HAL_RCC_UART9_FORCE_RESET ( ) ;
__HAL_RCC_UART9_RELEASE_RESET ( ) ;
__HAL_RCC_UART9_CLK_ENABLE ( ) ;
}
__HAL_RCC_UART9_CLK_ENABLE ( ) ;
}
# endif
# ifdef UART10
if ( mask & ( 1 < < 9 ) ) {
__HAL_RCC_UART10_FORCE_RESET ( ) ;
__HAL_RCC_UART10_RELEASE_RESET ( ) ;
__HAL_RCC_UART10_CLK_ENABLE ( ) ;
}
__HAL_RCC_UART10_CLK_ENABLE ( ) ;
}
# endif
}
@ -487,71 +497,71 @@ STATIC void uart_clock_disable(uint16_t mask) {
if ( mask & ( 1 < < 0 ) ) {
__HAL_RCC_USART1_FORCE_RESET ( ) ;
__HAL_RCC_USART1_RELEASE_RESET ( ) ;
__HAL_RCC_USART1_CLK_DISABLE ( ) ;
__HAL_RCC_USART1_CLK_DISABLE ( ) ;
}
# endif
# ifdef USART2
if ( mask & ( 1 < < 1 ) ) {
__HAL_RCC_USART2_FORCE_RESET ( ) ;
__HAL_RCC_USART2_RELEASE_RESET ( ) ;
__HAL_RCC_USART2_CLK_DISABLE ( ) ;
__HAL_RCC_USART2_CLK_DISABLE ( ) ;
}
# endif
# ifdef USART3
if ( mask & ( 1 < < 2 ) ) {
__HAL_RCC_USART3_FORCE_RESET ( ) ;
__HAL_RCC_USART3_RELEASE_RESET ( ) ;
__HAL_RCC_USART3_CLK_DISABLE ( ) ;
__HAL_RCC_USART3_CLK_DISABLE ( ) ;
}
# endif
# ifdef UART4
if ( mask & ( 1 < < 3 ) ) {
__HAL_RCC_UART4_FORCE_RESET ( ) ;
__HAL_RCC_UART4_RELEASE_RESET ( ) ;
__HAL_RCC_UART4_CLK_DISABLE ( ) ;
__HAL_RCC_UART4_CLK_DISABLE ( ) ;
}
# endif
# ifdef UART5
if ( mask & ( 1 < < 4 ) ) {
__HAL_RCC_UART5_FORCE_RESET ( ) ;
__HAL_RCC_UART5_RELEASE_RESET ( ) ;
__HAL_RCC_UART5_CLK_DISABLE ( ) ;
__HAL_RCC_UART5_CLK_DISABLE ( ) ;
}
# endif
# ifdef USART6
if ( mask & ( 1 < < 5 ) ) {
__HAL_RCC_USART6_FORCE_RESET ( ) ;
__HAL_RCC_USART6_RELEASE_RESET ( ) ;
__HAL_RCC_USART6_CLK_DISABLE ( ) ;
}
__HAL_RCC_USART6_CLK_DISABLE ( ) ;
}
# endif
# ifdef UART7
if ( mask & ( 1 < < 6 ) ) {
__HAL_RCC_UART7_FORCE_RESET ( ) ;
__HAL_RCC_UART7_RELEASE_RESET ( ) ;
__HAL_RCC_UART7_CLK_DISABLE ( ) ;
}
__HAL_RCC_UART7_CLK_DISABLE ( ) ;
}
# endif
# ifdef UART8
if ( mask & ( 1 < < 7 ) ) {
__HAL_RCC_UART8_FORCE_RESET ( ) ;
__HAL_RCC_UART8_RELEASE_RESET ( ) ;
__HAL_RCC_UART8_CLK_DISABLE ( ) ;
}
__HAL_RCC_UART8_CLK_DISABLE ( ) ;
}
# endif
# ifdef UART9
if ( mask & ( 1 < < 8 ) ) {
__HAL_RCC_UART9_FORCE_RESET ( ) ;
__HAL_RCC_UART9_RELEASE_RESET ( ) ;
__HAL_RCC_UART9_CLK_DISABLE ( ) ;
}
__HAL_RCC_UART9_CLK_DISABLE ( ) ;
}
# endif
# ifdef UART10
if ( mask & ( 1 < < 9 ) ) {
__HAL_RCC_UART10_FORCE_RESET ( ) ;
__HAL_RCC_UART10_RELEASE_RESET ( ) ;
__HAL_RCC_UART10_CLK_DISABLE ( ) ;
}
__HAL_RCC_UART10_CLK_DISABLE ( ) ;
}
# endif
}