///////////////////////////////////////////////////////////////////////////// //! Initializes UART interfaces //! \param[in] mode currently only mode 0 supported //! \return < 0 if initialisation failed //! \note Applications shouldn't call this function directly, instead please use \ref MIOS32_COM or \ref MIOS32_MIDI layer functions ///////////////////////////////////////////////////////////////////////////// s32 MIOS32_UART_Init(u32 mode) { // currently only mode 0 supported if( mode != 0 ) return -1; // unsupported mode #if MIOS32_UART_NUM == 0 return -1; // no UARTs #else // configure UART pins #if MIOS32_UART0_ASSIGNMENT != 0 MIOS32_UART0_TX_INIT; MIOS32_UART0_RX_INIT; #endif #if MIOS32_UART_NUM >= 2 && MIOS32_UART1_ASSIGNMENT != 0 MIOS32_UART1_TX_INIT; MIOS32_UART1_RX_INIT; #endif #if MIOS32_UART_NUM >= 3 && MIOS32_UART2_ASSIGNMENT != 0 MIOS32_UART2_TX_INIT; MIOS32_UART2_RX_INIT; #endif #if MIOS32_UART_NUM >= 4 && MIOS32_UART3_ASSIGNMENT != 0 MIOS32_UART3_TX_INIT; MIOS32_UART3_RX_INIT; #endif // clear buffer counters int i; for(i=0; i<MIOS32_UART_NUM; ++i) { rx_buffer_tail[i] = rx_buffer_head[i] = rx_buffer_size[i] = 0; tx_buffer_tail[i] = tx_buffer_head[i] = tx_buffer_size[i] = 0; } // UART configuration #if MIOS32_UART0_ASSIGNMENT != 0 MIOS32_UART_BaudrateSet(0, MIOS32_UART0_BAUDRATE); #endif #if MIOS32_UART_NUM >=2 && MIOS32_UART1_ASSIGNMENT != 0 MIOS32_UART_BaudrateSet(1, MIOS32_UART1_BAUDRATE); #endif #if MIOS32_UART_NUM >=3 && MIOS32_UART2_ASSIGNMENT != 0 MIOS32_UART_BaudrateSet(2, MIOS32_UART2_BAUDRATE); #endif #if MIOS32_UART_NUM >=4 && MIOS32_UART3_ASSIGNMENT != 0 MIOS32_UART_BaudrateSet(3, MIOS32_UART3_BAUDRATE); #endif // configure and enable UART interrupts #if MIOS32_UART0_ASSIGNMENT != 0 MIOS32_UART0->IER = (1 << 1) | (1 << 0); // enable RBR and THRE (receive/transmit buffer) interrupt MIOS32_IRQ_Install(MIOS32_UART0_IRQ_CHANNEL, MIOS32_IRQ_UART_PRIORITY); #endif #if MIOS32_UART_NUM >= 2 && MIOS32_UART1_ASSIGNMENT != 0 MIOS32_UART1->IER = (1 << 1) | (1 << 0); // enable RBR and THRE (receive/transmit buffer) interrupt MIOS32_IRQ_Install(MIOS32_UART1_IRQ_CHANNEL, MIOS32_IRQ_UART_PRIORITY); #endif #if MIOS32_UART_NUM >= 3 && MIOS32_UART2_ASSIGNMENT != 0 MIOS32_UART2->IER = (1 << 1) | (1 << 0); // enable RBR and THRE (receive/transmit buffer) interrupt MIOS32_IRQ_Install(MIOS32_UART2_IRQ_CHANNEL, MIOS32_IRQ_UART_PRIORITY); #endif #if MIOS32_UART_NUM >= 4 && MIOS32_UART2_ASSIGNMENT != 0 MIOS32_UART3->IER = (1 << 1) | (1 << 0); // enable RBR and THRE (receive/transmit buffer) interrupt MIOS32_IRQ_Install(MIOS32_UART3_IRQ_CHANNEL, MIOS32_IRQ_UART_PRIORITY); #endif return 0; // no error #endif }
///////////////////////////////////////////////////////////////////////////// //! Initializes a given UART interface based on given baudrate and TX output mode //! \param[in] uart UART number (0..2) //! \param[in] baudrate the baudrate //! \param[in] tx_pin_mode the TX pin mode //! <UL> //! <LI>MIOS32_BOARD_PIN_MODE_OUTPUT_PP: TX pin configured for push-pull mode //! <LI>MIOS32_BOARD_PIN_MODE_OUTPUT_OD: TX pin configured for open drain mode //! </UL> //! \param[in] is_midi MIDI or common UART interface? //! \return < 0 if initialisation failed ///////////////////////////////////////////////////////////////////////////// s32 MIOS32_UART_InitPort(u8 uart, u32 baudrate, mios32_board_pin_mode_t tx_pin_mode, u8 is_midi) { #if NUM_SUPPORTED_UARTS == 0 return -1; // no UART available #else GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; if( uart >= NUM_SUPPORTED_UARTS ) return -1; // unsupported UART // MIDI assignment if( is_midi ) { uart_assigned_to_midi |= (1 << uart); } else { uart_assigned_to_midi &= ~(1 << uart); } switch( uart ) { #if NUM_SUPPORTED_UARTS >= 1 && MIOS32_UART0_ASSIGNMENT != 0 case 0: { // output GPIO_InitStructure.GPIO_Pin = MIOS32_UART0_TX_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = (tx_pin_mode == MIOS32_BOARD_PIN_MODE_OUTPUT_PP) ? GPIO_OType_PP : GPIO_OType_OD; GPIO_Init(MIOS32_UART0_TX_PORT, &GPIO_InitStructure); // inputs with internal pull-up GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Pin = MIOS32_UART0_RX_PIN; GPIO_Init(MIOS32_UART0_RX_PORT, &GPIO_InitStructure); // UART configuration MIOS32_UART_BaudrateSet(uart, baudrate); } break; #endif #if NUM_SUPPORTED_UARTS >= 2 && MIOS32_UART1_ASSIGNMENT != 0 case 1: { // output GPIO_InitStructure.GPIO_Pin = MIOS32_UART1_TX_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = (tx_pin_mode == MIOS32_BOARD_PIN_MODE_OUTPUT_PP) ? GPIO_OType_PP : GPIO_OType_OD; GPIO_Init(MIOS32_UART1_TX_PORT, &GPIO_InitStructure); // inputs with internal pull-up GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Pin = MIOS32_UART1_RX_PIN; GPIO_Init(MIOS32_UART1_RX_PORT, &GPIO_InitStructure); // UART configuration MIOS32_UART_BaudrateSet(uart, baudrate); } break; #endif #if NUM_SUPPORTED_UARTS >= 3 && MIOS32_UART2_ASSIGNMENT != 0 case 2: { // output GPIO_InitStructure.GPIO_Pin = MIOS32_UART2_TX_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = (tx_pin_mode == MIOS32_BOARD_PIN_MODE_OUTPUT_PP) ? GPIO_OType_PP : GPIO_OType_OD; GPIO_Init(MIOS32_UART2_TX_PORT, &GPIO_InitStructure); // inputs with internal pull-up GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Pin = MIOS32_UART2_RX_PIN; GPIO_Init(MIOS32_UART2_RX_PORT, &GPIO_InitStructure); // UART configuration MIOS32_UART_BaudrateSet(uart, baudrate); } break; #endif #if NUM_SUPPORTED_UARTS >= 4 && MIOS32_UART3_ASSIGNMENT != 0 case 3: { // output GPIO_InitStructure.GPIO_Pin = MIOS32_UART3_TX_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = (tx_pin_mode == MIOS32_BOARD_PIN_MODE_OUTPUT_PP) ? GPIO_OType_PP : GPIO_OType_OD; GPIO_Init(MIOS32_UART3_TX_PORT, &GPIO_InitStructure); // inputs with internal pull-up GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Pin = MIOS32_UART3_RX_PIN; GPIO_Init(MIOS32_UART3_RX_PORT, &GPIO_InitStructure); // UART configuration MIOS32_UART_BaudrateSet(uart, baudrate); } break; #endif default: return -1; // unsupported UART } return 0; // no error #endif }