diff --git a/sw/airborne/arch/lpc21/usb_tunnel.c b/sw/airborne/arch/lpc21/usb_tunnel.c index 456495d363..f93af9dba2 100644 --- a/sw/airborne/arch/lpc21/usb_tunnel.c +++ b/sw/airborne/arch/lpc21/usb_tunnel.c @@ -59,34 +59,34 @@ int main( void ) { while(1) { if (T0TC > (rx_time+((PCLK / T0_PCLK_DIV) / BLINK_MIN))) LED_OFF(1); if (T0TC > (tx_time+((PCLK / T0_PCLK_DIV) / BLINK_MIN))) LED_OFF(2); - if (Uart0ChAvailable() && VCOM_check_free_space(1)) { + if (uart_char_available(&uart0) && VCOM_check_free_space(1)) { LED_ON(1); rx_time = T0TC; - inc = Uart0Getch(); + inc = uart_getch(&uart0); VCOM_putchar(inc); } - if (VCOM_check_available() && Uart0CheckFreeSpace(1)) { + if (VCOM_check_available() && uart_check_free_space(&uart0, 1)) { LED_ON(2); tx_time = T0TC; inc = VCOM_getchar(); - Uart0Transmit(inc); + uart_transmit(&uart0, inc); } } #else while(1) { if (T0TC > (rx_time+((PCLK / T0_PCLK_DIV) / BLINK_MIN))) LED_OFF(1); if (T0TC > (tx_time+((PCLK / T0_PCLK_DIV) / BLINK_MIN))) LED_OFF(2); - if (Uart1ChAvailable() && VCOM_check_free_space(1)) { + if (uart_char_available(&uart1) && VCOM_check_free_space(1)) { LED_ON(1); rx_time = T0TC; - inc = Uart1Getch(); + inc = uart_getch(&uart1); VCOM_putchar(inc); } - if (VCOM_check_available() && Uart1CheckFreeSpace(1)) { + if (VCOM_check_available() && uart_check_free_space(&uart1, 1)) { LED_ON(2); tx_time = T0TC; inc = VCOM_getchar(); - Uart1Transmit(inc); + uart_transmit(&uart1, inc); } } #endif diff --git a/sw/airborne/firmwares/logger/main_logger.c b/sw/airborne/firmwares/logger/main_logger.c index 7876f24c3b..b7fd9abd9b 100644 --- a/sw/airborne/firmwares/logger/main_logger.c +++ b/sw/airborne/firmwares/logger/main_logger.c @@ -449,10 +449,10 @@ int do_log(void) #ifdef USE_UART0 temp = 0; - while (Uart0ChAvailable() && (temp++ < 128)) + while (uart_char_available(&uart0) && (temp++ < 128)) { // LED_TOGGLE(LED_GREEN); - inc = Uart0Getch(); + inc = uart_getch(&uart1); #ifdef LOG_XBEE log_xbee(inc, LOG_SOURCE_UART0); #else @@ -466,10 +466,10 @@ int do_log(void) #endif #ifdef USE_UART1 temp = 0; - while (Uart1ChAvailable() && (temp++ < 128)) + while (uart_char_available(&uart1) && (temp++ < 128)) { // LED_TOGGLE(LED_GREEN); - inc = Uart1Getch(); + inc = uart_getch(&uart1); #ifdef LOG_XBEE log_xbee(inc, LOG_SOURCE_UART1); #else diff --git a/sw/airborne/lisa/lisa_stm_gps_passthrough_main.c b/sw/airborne/lisa/lisa_stm_gps_passthrough_main.c index 827b1278b3..6e56777abb 100644 --- a/sw/airborne/lisa/lisa_stm_gps_passthrough_main.c +++ b/sw/airborne/lisa/lisa_stm_gps_passthrough_main.c @@ -54,12 +54,12 @@ struct __attribute__ ((packed)) spistream_uart_msg { // Use 3 static instances of this struct in uart_transfer_event // instead of a myriad of repetitive static vars: struct uart_state { - struct spistream_uart_msg * msg; - uint32_t timeout; - uint32_t num_rx_bytes; - uint8_t enabled; - uint8_t has_data; - uint8_t sent; + struct spistream_uart_msg * msg; + uint32_t timeout; + uint32_t num_rx_bytes; + uint8_t enabled; + uint8_t has_data; + uint8_t sent; }; static struct spistream_uart_msg spistream_uart1_msg; @@ -103,7 +103,7 @@ static inline void main_init(void) { spistream_uart3_msg.uart_id = 3; #ifdef SPISTREAM_DEBUG - uart_debug_transfer_init(); + uart_debug_transfer_init(); #endif spistream_init(&on_spistream_msg_received, @@ -120,13 +120,13 @@ static inline void on_spistream_msg_received(uint8_t msg_id, uint8_t * data, uint16_t num_bytes) { - spistream_send_msg(data, num_bytes, SPISTREAM_NO_WAIT); + spistream_send_msg(data, num_bytes, SPISTREAM_NO_WAIT); } static inline void on_spistream_msg_sent(uint8_t msg_id) { - if(spistream_wait_for_num_transfers > 0) { + if(spistream_wait_for_num_transfers > 0) { spistream_wait_for_num_transfers--; - } + } } static inline void main_periodic(void) @@ -135,7 +135,7 @@ static inline void main_periodic(void) RunOnceEvery(1, { LED_PERIODIC(); - }); + }); } /** @@ -165,18 +165,18 @@ static inline void main_event(void) OveroLinkEvent(on_overo_link_msg_received, on_overo_link_crc_failed); #ifdef SPISTREAM_DEBUG - uart_debug_transfer_event(); + uart_debug_transfer_event(); #else - uart_transfer_event(); + uart_transfer_event(); #endif } #ifdef SPISTREAM_DEBUG static inline void uart_debug_transfer_init(void) { - uint16_t idx; - for(idx = 1; idx < 700; idx++) { - spistream_debug_msg.uart_data[idx] = idx % 40; - } + uint16_t idx; + for(idx = 1; idx < 700; idx++) { + spistream_debug_msg.uart_data[idx] = idx % 40; + } } #endif @@ -189,40 +189,40 @@ static inline void uart_debug_transfer_init(void) { */ #ifdef SPISTREAM_DEBUG static inline void uart_debug_transfer_event(void) { - static uint16_t len = 0; - static uint16_t timeout = 1; + static uint16_t len = 0; + static uint16_t timeout = 1; - if(timeout-- == 0) { - timeout = 8000; - if(spistream_wait_for_num_transfers == 0) - { - LED_OFF(6); - len++; - if(len > 700) { len = 500; } + if(timeout-- == 0) { + timeout = 8000; + if(spistream_wait_for_num_transfers == 0) + { + LED_OFF(6); + len++; + if(len > 700) { len = 500; } - spistream_debug_msg.uart_id = 1; - if(spistream_send_msg((uint8_t *)&spistream_debug_msg, - len+1-20, - SPISTREAM_WAIT_FOR_READ)) { // +1 for UART id byte - spistream_wait_for_num_transfers++; - } - spistream_debug_msg.uart_id = 2; - if(spistream_send_msg((uint8_t *)&spistream_debug_msg, - len+1, - SPISTREAM_WAIT_FOR_READ)) { // +1 for UART id byte - spistream_wait_for_num_transfers++; - } - spistream_debug_msg.uart_id = 3; - if(spistream_send_msg((uint8_t *)&spistream_debug_msg, - len+1+20, - SPISTREAM_WAIT_FOR_READ)) { // +1 for UART id byte - spistream_wait_for_num_transfers++; - } - } - else { - LED_ON(6); - } - } + spistream_debug_msg.uart_id = 1; + if(spistream_send_msg((uint8_t *)&spistream_debug_msg, + len+1-20, + SPISTREAM_WAIT_FOR_READ)) { // +1 for UART id byte + spistream_wait_for_num_transfers++; + } + spistream_debug_msg.uart_id = 2; + if(spistream_send_msg((uint8_t *)&spistream_debug_msg, + len+1, + SPISTREAM_WAIT_FOR_READ)) { // +1 for UART id byte + spistream_wait_for_num_transfers++; + } + spistream_debug_msg.uart_id = 3; + if(spistream_send_msg((uint8_t *)&spistream_debug_msg, + len+1+20, + SPISTREAM_WAIT_FOR_READ)) { // +1 for UART id byte + spistream_wait_for_num_transfers++; + } + } + else { + LED_ON(6); + } + } } #endif @@ -243,106 +243,106 @@ static inline void uart_transfer_event(void) { static uint8_t uart3_has_data = 0; static uint8_t trigger_send = 0; - static uint8_t uart1_enabled = 1; - static uint8_t uart2_enabled = 1; - static uint8_t uart3_enabled = 1; + static uint8_t uart1_enabled = 1; + static uint8_t uart2_enabled = 1; + static uint8_t uart3_enabled = 1; // We cache data availability, so it doesn't change between checks: - uart1_has_data = Uart1ChAvailable(); - uart2_has_data = Uart2ChAvailable(); - uart3_has_data = Uart3ChAvailable(); + uart1_has_data = uart_char_available(&uart1); + uart2_has_data = uart_char_available(&uart2); + uart3_has_data = uart_char_available(&uart3); - // Fill stage: Read data from UARTs into buffers, or increment - // their timeouts if no data is available: - if(!uart1_sent && uart1_has_data) { - spistream_uart1_msg.uart_data[uart1_num_rx_bytes] = Uart1Getch(); - timeout_uart1 = 0; - if(uart1_num_rx_bytes < SPISTREAM_MAX_MESSAGE_LENGTH) - { uart1_num_rx_bytes++; } - } else { if(timeout_uart1 < timeout_trig) { timeout_uart1++; } } + // Fill stage: Read data from UARTs into buffers, or increment + // their timeouts if no data is available: + if(!uart1_sent && uart1_has_data) { + spistream_uart1_msg.uart_data[uart1_num_rx_bytes] = uart_getch(&uart1); + timeout_uart1 = 0; + if(uart1_num_rx_bytes < SPISTREAM_MAX_MESSAGE_LENGTH) + { uart1_num_rx_bytes++; } + } else { if(timeout_uart1 < timeout_trig) { timeout_uart1++; } } - if(!uart2_sent && uart2_has_data) { - spistream_uart2_msg.uart_data[uart2_num_rx_bytes] = Uart2Getch(); - timeout_uart2 = 0; - if(uart2_num_rx_bytes < SPISTREAM_MAX_MESSAGE_LENGTH) - { uart2_num_rx_bytes++; } - } else { if(timeout_uart2 < timeout_trig) { timeout_uart2++; } } + if(!uart2_sent && uart2_has_data) { + spistream_uart2_msg.uart_data[uart2_num_rx_bytes] = uart_getch(&uart2); + timeout_uart2 = 0; + if(uart2_num_rx_bytes < SPISTREAM_MAX_MESSAGE_LENGTH) + { uart2_num_rx_bytes++; } + } else { if(timeout_uart2 < timeout_trig) { timeout_uart2++; } } - if(!uart3_sent && uart3_has_data) { - spistream_uart3_msg.uart_data[uart3_num_rx_bytes] = Uart3Getch(); - timeout_uart3 = 0; - if(uart3_num_rx_bytes < SPISTREAM_MAX_MESSAGE_LENGTH) - { uart3_num_rx_bytes++; } - } else { if(timeout_uart3 < timeout_trig) { timeout_uart3++; } } + if(!uart3_sent && uart3_has_data) { + spistream_uart3_msg.uart_data[uart3_num_rx_bytes] = uart_getch(&uart3); + timeout_uart3 = 0; + if(uart3_num_rx_bytes < SPISTREAM_MAX_MESSAGE_LENGTH) + { uart3_num_rx_bytes++; } + } else { if(timeout_uart3 < timeout_trig) { timeout_uart3++; } } - trigger_send = ((!uart1_enabled || - (timeout_uart1 >= timeout_trig)) && - (!uart2_enabled || - (timeout_uart2 >= timeout_trig)) && - (!uart3_enabled || - (timeout_uart3 >= timeout_trig))); + trigger_send = ((!uart1_enabled || + (timeout_uart1 >= timeout_trig)) && + (!uart2_enabled || + (timeout_uart2 >= timeout_trig)) && + (!uart3_enabled || + (timeout_uart3 >= timeout_trig))); - // Send stage: If all UART timeouts reach the timeout - // trigger value and have accumulated data to send - if(trigger_send) { + // Send stage: If all UART timeouts reach the timeout + // trigger value and have accumulated data to send + if(trigger_send) { // If there was no new data on any UART for some time // and there is data in every rx buffer: - if(spistream_wait_for_num_transfers > 0) - { - // Warning LED: Could not finish all transactions - // from last call. - LED_ON(6); - } - else - { - LED_OFF(6); + if(spistream_wait_for_num_transfers > 0) + { + // Warning LED: Could not finish all transactions + // from last call. + LED_ON(6); + } + else + { + LED_OFF(6); - uart1_sent = !uart1_enabled; // If we set uartX_sent to 1 here, it - uart2_sent = !uart2_enabled; // is just ignored for every read poll - uart3_sent = !uart3_enabled; // as it seems to have been read already. + uart1_sent = !uart1_enabled; // If we set uartX_sent to 1 here, it + uart2_sent = !uart2_enabled; // is just ignored for every read poll + uart3_sent = !uart3_enabled; // as it seems to have been read already. if(!uart1_sent && uart1_num_rx_bytes > 0) { - if(spistream_send_msg((uint8_t *)&spistream_uart1_msg, - uart1_num_rx_bytes+1, // +1 for UART id - SPISTREAM_WAIT_FOR_READ)) { - uart1_sent = 1; - spistream_wait_for_num_transfers++; - } - } + if(spistream_send_msg((uint8_t *)&spistream_uart1_msg, + uart1_num_rx_bytes+1, // +1 for UART id + SPISTREAM_WAIT_FOR_READ)) { + uart1_sent = 1; + spistream_wait_for_num_transfers++; + } + } if(!uart2_sent && uart1_num_rx_bytes > 0) { - if(spistream_send_msg((uint8_t *)&spistream_uart2_msg, - uart1_num_rx_bytes+1, // +1 for UART id - SPISTREAM_WAIT_FOR_READ)) { - uart2_sent = 1; - spistream_wait_for_num_transfers++; - } - } + if(spistream_send_msg((uint8_t *)&spistream_uart2_msg, + uart1_num_rx_bytes+1, // +1 for UART id + SPISTREAM_WAIT_FOR_READ)) { + uart2_sent = 1; + spistream_wait_for_num_transfers++; + } + } if(!uart3_sent && uart3_num_rx_bytes > 0) { - if(spistream_send_msg((uint8_t *)&spistream_uart3_msg, - uart3_num_rx_bytes+1, // +1 for UART id - SPISTREAM_WAIT_FOR_READ)) { - uart3_sent = 1; - spistream_wait_for_num_transfers++; - } - } + if(spistream_send_msg((uint8_t *)&spistream_uart3_msg, + uart3_num_rx_bytes+1, // +1 for UART id + SPISTREAM_WAIT_FOR_READ)) { + uart3_sent = 1; + spistream_wait_for_num_transfers++; + } + } - // Transaction completed, reset state. - // Note: Only reset when all uart buffers have been transmitted, - // otherwise the timeout would start from the beginning and the - // loop phase shifts (aka "you're in the deep"). - uart1_num_rx_bytes = 0; - uart2_num_rx_bytes = 0; - uart3_num_rx_bytes = 0; - timeout_uart1 = 0; - timeout_uart2 = 0; - timeout_uart3 = 0; - uart1_sent = 0; - uart2_sent = 0; - uart3_sent = 0; + // Transaction completed, reset state. + // Note: Only reset when all uart buffers have been transmitted, + // otherwise the timeout would start from the beginning and the + // loop phase shifts (aka "you're in the deep"). + uart1_num_rx_bytes = 0; + uart2_num_rx_bytes = 0; + uart3_num_rx_bytes = 0; + timeout_uart1 = 0; + timeout_uart2 = 0; + timeout_uart3 = 0; + uart1_sent = 0; + uart2_sent = 0; + uart3_sent = 0; } } } diff --git a/sw/airborne/lisa/test/lisa_tunnel.c b/sw/airborne/lisa/test/lisa_tunnel.c index d456dfc5a2..09b588d29e 100644 --- a/sw/airborne/lisa/test/lisa_tunnel.c +++ b/sw/airborne/lisa/test/lisa_tunnel.c @@ -56,11 +56,10 @@ static inline void main_periodic_task( void ) { static inline void main_event_task( void ) { - if (Uart2ChAvailable()) - Uart1Transmit(Uart2Getch()); - - if (Uart1ChAvailable()) - Uart2Transmit(Uart1Getch()); + if (uart_char_available(&uart2)) + uart_transmit(&uart1, uart_getch(&uart2)); + if (uart_char_available(&uart1)) + uart_transmit(&uart2, uart_getch(&uart1)); } diff --git a/sw/airborne/lisa/test/test_board.c b/sw/airborne/lisa/test/test_board.c index 6930867d69..a105d61306 100644 --- a/sw/airborne/lisa/test/test_board.c +++ b/sw/airborne/lisa/test/test_board.c @@ -267,9 +267,9 @@ static void test_uart_periodic(void) { if (idx_tx= len; } +uint8_t uart_getch(struct uart_periph* p) { + uint8_t ret = p->rx_buf[p->rx_extract_idx]; + p->rx_extract_idx = (p->rx_extract_idx + 1) % UART_RX_BUFFER_SIZE; + return ret; +} diff --git a/sw/airborne/mcu_periph/uart.h b/sw/airborne/mcu_periph/uart.h index 17ffe780d0..c3470357d5 100644 --- a/sw/airborne/mcu_periph/uart.h +++ b/sw/airborne/mcu_periph/uart.h @@ -73,14 +73,11 @@ extern void uart_periph_init(struct uart_periph* p); extern void uart_periph_set_baudrate(struct uart_periph* p, enum UartBaud, bool_t hw_flow_control); extern void uart_transmit(struct uart_periph* p, uint8_t data); extern bool_t uart_check_free_space(struct uart_periph* p, uint8_t len); +extern uint8_t uart_getch(struct uart_periph* p); -#define UartChAvailable(_p) (_p.rx_insert_idx != _p.rx_extract_idx) - -#define UartGetch(_p) ({ \ - uint8_t ret = _p.rx_buf[_p.rx_extract_idx]; \ - _p.rx_extract_idx = (_p.rx_extract_idx + 1)%UART_RX_BUFFER_SIZE; \ - ret; \ -}) +static inline bool_t uart_char_available(struct uart_periph* p) { + return (p->rx_insert_idx != p->rx_extract_idx); +} #ifdef USE_UART0 @@ -91,8 +88,8 @@ extern void uart0_init(void); #define Uart0CheckFreeSpace(_x) uart_check_free_space(&uart0, _x) #define Uart0Transmit(_x) uart_transmit(&uart0, _x) #define Uart0SendMessage() {} -#define Uart0ChAvailable() UartChAvailable(uart0) -#define Uart0Getch() UartGetch(uart0) +#define Uart0ChAvailable() uart_char_available(&uart0) +#define Uart0Getch() uart_getch(&uart0) #define Uart0TxRunning uart0.tx_running #define Uart0SetBaudrate(_b) uart_periph_set_baudrate(&uart0, _b, FALSE) @@ -115,8 +112,8 @@ extern void uart1_init(void); #define Uart1CheckFreeSpace(_x) uart_check_free_space(&uart1, _x) #define Uart1Transmit(_x) uart_transmit(&uart1, _x) #define Uart1SendMessage() {} -#define Uart1ChAvailable() UartChAvailable(uart1) -#define Uart1Getch() UartGetch(uart1) +#define Uart1ChAvailable() uart_char_available(&uart1) +#define Uart1Getch() uart_getch(&uart1) #define Uart1TxRunning uart1.tx_running #if UART1_HW_FLOW_CONTROL #define Uart1SetBaudrate(_b) uart_periph_set_baudrate(&uart1, _b, TRUE) @@ -143,8 +140,8 @@ extern void uart2_init(void); #define Uart2CheckFreeSpace(_x) uart_check_free_space(&uart2, _x) #define Uart2Transmit(_x) uart_transmit(&uart2, _x) #define Uart2SendMessage() {} -#define Uart2ChAvailable() UartChAvailable(uart2) -#define Uart2Getch() UartGetch(uart2) +#define Uart2ChAvailable() uart_char_available(&uart2) +#define Uart2Getch() uart_getch(&uart2) #define Uart2TxRunning uart2.tx_running #define Uart2SetBaudrate(_b) uart_periph_set_baudrate(&uart2, _b, FALSE) @@ -167,8 +164,8 @@ extern void uart3_init(void); #define Uart3CheckFreeSpace(_x) uart_check_free_space(&uart3, _x) #define Uart3Transmit(_x) uart_transmit(&uart3, _x) #define Uart3SendMessage() {} -#define Uart3ChAvailable() UartChAvailable(uart3) -#define Uart3Getch() UartGetch(uart3) +#define Uart3ChAvailable() uart_char_available(&uart3) +#define Uart3Getch() uart_getch(&uart3) #define Uart3TxRunning uart3.tx_running #define Uart3SetBaudrate(_b) uart_periph_set_baudrate(&uart3, _b, FALSE) @@ -191,8 +188,8 @@ extern void uart5_init(void); #define Uart5CheckFreeSpace(_x) uart_check_free_space(&uart5, _x) #define Uart5Transmit(_x) uart_transmit(&uart5, _x) #define Uart5SendMessage() {} -#define Uart5ChAvailable() UartChAvailable(uart5) -#define Uart5Getch() UartGetch(uart5) +#define Uart5ChAvailable() uart_char_available(&uart5) +#define Uart5Getch() uart_getch(&uart5) #define Uart5TxRunning uart5.tx_running #define Uart5SetBaudrate(_b) uart_periph_set_baudrate(&uart5, _b, FALSE) diff --git a/sw/airborne/sd_card/main.c b/sw/airborne/sd_card/main.c index 25342b9d7d..37da825c60 100644 --- a/sw/airborne/sd_card/main.c +++ b/sw/airborne/sd_card/main.c @@ -37,7 +37,7 @@ static inline void main_init( void ) { mcu_init(); sys_time_register_timer((1./PERIODIC_FREQUENCY), NULL); led_init(); - Uart0Init(); + uart_init(&uart0); spi_init(); sd_card_init(); diff --git a/sw/airborne/test/test_adcs.c b/sw/airborne/test/test_adcs.c index 01b1781149..ddf69c035b 100644 --- a/sw/airborne/test/test_adcs.c +++ b/sw/airborne/test/test_adcs.c @@ -42,10 +42,10 @@ int main (int argc, char** argv) { #endif #ifdef USE_UART0 - Uart0Init(); + uart_periph_init(&uart0); #endif #ifdef USE_UART1 - Uart1Init(); + uart_periph_init(&uart1); #endif mcu_int_enable();