OSStatus internal_uart_init( mico_uart_t uart, const mico_uart_config_t* config, ring_buffer_t* optional_rx_buffer ) { OSStatus err = kNoErr; require_action(optional_rx_buffer!=NULL, exit, err = kParamErr); #ifndef NO_MICO_RTOS mico_rtos_init_semaphore(&uart_interfaces[uart].tx_complete, 1); mico_rtos_init_semaphore(&uart_interfaces[uart].rx_complete, 1); mico_rtos_init_mutex(&uart_interfaces[uart].tx_mutex); #else uart_interfaces[uart].tx_complete = false; uart_interfaces[uart].rx_complete = false; #endif if(uart_mapping[uart].uart == FUART){ if( uart_mapping[uart].pin_tx->port == GPIOA && uart_mapping[uart].pin_tx->pin == 1 ) GpioFuartTxIoConfig(0); else if( uart_mapping[uart].pin_tx->port == GPIOB && uart_mapping[uart].pin_tx->pin == 7 ) GpioFuartTxIoConfig(1); else if( uart_mapping[uart].pin_tx->port == GPIOC && uart_mapping[uart].pin_tx->pin == 3 ) GpioFuartTxIoConfig(2); else return kUnsupportedErr; if( uart_mapping[uart].pin_rx->port == GPIOA && uart_mapping[uart].pin_rx->pin == 1 ) GpioFuartRxIoConfig(0); else if( uart_mapping[uart].pin_rx->port == GPIOB && uart_mapping[uart].pin_rx->pin == 6 ) GpioFuartRxIoConfig(1); else if( uart_mapping[uart].pin_rx->port == GPIOC && uart_mapping[uart].pin_rx->pin == 4 ) GpioFuartRxIoConfig(2); else return kUnsupportedErr; require_action( config->flow_control == FLOW_CONTROL_DISABLED, exit, err = kUnsupportedErr ); err = FuartInit(config->baud_rate, config->data_width + 5, config->parity, config->stop_bits + 1); require_noerr(err, exit); FuartIOctl(UART_IOCTL_RXINT_SET, 1); if (optional_rx_buffer != NULL){ /* Note that the ring_buffer should've been initialised first */ uart_interfaces[uart].rx_buffer = optional_rx_buffer; uart_interfaces[uart].rx_size = 0; //platform_uart_receive_bytes( uart, optional_rx_buffer->buffer, optional_rx_buffer->size, 0 ); } }else if(uart_mapping[uart].uart == BUART){ if( uart_mapping[uart].pin_tx->port == GPIOA && uart_mapping[uart].pin_tx->pin == 16 ) GpioBuartTxIoConfig(0); else if( uart_mapping[uart].pin_tx->port == GPIOA && uart_mapping[uart].pin_tx->pin == 25 ) GpioBuartTxIoConfig(1); else if( uart_mapping[uart].pin_tx->port == GPIOB && uart_mapping[uart].pin_tx->pin == 9 ) GpioBuartTxIoConfig(2); else if( uart_mapping[uart].pin_tx->port == GPIOB && uart_mapping[uart].pin_tx->pin == 28 ) GpioBuartTxIoConfig(3); else return kUnsupportedErr; if( uart_mapping[uart].pin_rx->port == GPIOA && uart_mapping[uart].pin_rx->pin == 13 ) GpioBuartRxIoConfig(0); else if( uart_mapping[uart].pin_rx->port == GPIOA && uart_mapping[uart].pin_rx->pin == 24 ) GpioBuartRxIoConfig(1); else if( uart_mapping[uart].pin_rx->port == GPIOB && uart_mapping[uart].pin_rx->pin == 8 ) GpioBuartRxIoConfig(2); else if( uart_mapping[uart].pin_rx->port == GPIOB && uart_mapping[uart].pin_rx->pin == 29 ) GpioBuartRxIoConfig(3); else return kUnsupportedErr; require_action( config->flow_control == FLOW_CONTROL_DISABLED, exit, err = kUnsupportedErr ); err = BuartExFifoInit( (32-2-1)*1024, 1024*2, 1024, 1); require_noerr(err, exit); err = BuartInit(config->baud_rate, config->data_width + 5, config->parity, config->stop_bits + 1); require_noerr(err, exit); BuartIOctl(UART_IOCTL_RXINT_SET, 1); BuartIOctl(UART_IOCTL_TXINT_SET, 1); }else return kUnsupportedErr; exit: return err; }
OSStatus platform_uart_init( platform_uart_driver_t* driver, const platform_uart_t* peripheral, const platform_uart_config_t* config, ring_buffer_t* optional_ring_buffer ) { OSStatus err = kNoErr; platform_mcu_powersave_disable(); require_action_quiet( ( driver != NULL ) && ( peripheral != NULL ) && ( config != NULL ), exit, err = kParamErr); driver->rx_size = 0; driver->tx_size = 0; driver->last_transmit_result = kNoErr; driver->last_receive_result = kNoErr; driver->peripheral = (platform_uart_t*)peripheral; driver->buart_fifo_head = 0; #ifndef NO_MICO_RTOS mico_rtos_init_semaphore( &driver->tx_complete, 1 ); mico_rtos_init_semaphore( &driver->rx_complete, 1 ); mico_rtos_init_mutex ( &driver->tx_mutex ); #else driver->tx_complete = false; driver->rx_complete = false; #endif if ( peripheral->uart == FUART ){ if( peripheral->pin_tx->port == GPIOA && peripheral->pin_tx->pin == 1 ) GpioFuartTxIoConfig(0); else if( peripheral->pin_tx->port == GPIOB && peripheral->pin_tx->pin == 7 ) GpioFuartTxIoConfig(1); else if( peripheral->pin_tx->port == GPIOC && peripheral->pin_tx->pin == 3 ) GpioFuartTxIoConfig(2); else return kUnsupportedErr; if( peripheral->pin_rx->port == GPIOA && peripheral->pin_rx->pin == 1 ) GpioFuartRxIoConfig(0); else if( peripheral->pin_rx->port == GPIOB && peripheral->pin_rx->pin == 6 ) GpioFuartRxIoConfig(1); else if( peripheral->pin_rx->port == GPIOC && peripheral->pin_rx->pin == 4 ) GpioFuartRxIoConfig(2); else return kUnsupportedErr; require_action( config->flow_control == FLOW_CONTROL_DISABLED, exit, err = kUnsupportedErr ); err = FuartInit(config->baud_rate, config->data_width + 5, config->parity, config->stop_bits + 1); require_noerr(err, exit); FuartIOctl(UART_IOCTL_RXINT_SET, 1); if (optional_ring_buffer != NULL){ /* Note that the ring_buffer should've been initialised first */ driver->rx_buffer = optional_ring_buffer; driver->rx_size = 0; //platform_uart_receive_bytes( uart, optional_rx_buffer->buffer, optional_rx_buffer->size, 0 ); } }else if( peripheral->uart == BUART ){ if( peripheral->pin_tx->port == GPIOA && peripheral->pin_tx->pin == 16 ) GpioBuartTxIoConfig(0); else if( peripheral->pin_tx->port == GPIOA && peripheral->pin_tx->pin == 25 ) GpioBuartTxIoConfig(1); else if( peripheral->pin_tx->port == GPIOB && peripheral->pin_tx->pin == 9 ) GpioBuartTxIoConfig(2); else if( peripheral->pin_tx->port == GPIOB && peripheral->pin_tx->pin == 28 ) GpioBuartTxIoConfig(3); else return kUnsupportedErr; if( peripheral->pin_rx->port == GPIOA && peripheral->pin_rx->pin == 13 ) GpioBuartRxIoConfig(0); else if( peripheral->pin_rx->port == GPIOA && peripheral->pin_rx->pin == 24 ) GpioBuartRxIoConfig(1); else if( peripheral->pin_rx->port == GPIOB && peripheral->pin_rx->pin == 8 ) GpioBuartRxIoConfig(2); else if( peripheral->pin_rx->port == GPIOB && peripheral->pin_rx->pin == 29 ) GpioBuartRxIoConfig(3); else return kUnsupportedErr; require_action( config->flow_control == FLOW_CONTROL_DISABLED, exit, err = kUnsupportedErr ); err = BuartExFifoInit( BUART_FIFO_START, BUART_RX_FIFO_SIZE, BUART_TX_FIFO_SIZE, 1 ); require_noerr(err, exit); err = BuartInit( config->baud_rate, config->data_width + 5, config->parity, config->stop_bits + 1 ); require_noerr(err, exit); BuartIOctl( UART_IOCTL_TXINT_SET, 1 ); }else return kUnsupportedErr; exit: return err; }