[uart] less macros: uart_getch function and uart_char_available static inline function

if uart is hardcoded, use function directly
This commit is contained in:
Felix Ruess
2013-02-26 17:28:01 +01:00
parent 14944d71d7
commit 28565c3202
11 changed files with 196 additions and 195 deletions
+8 -8
View File
@@ -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
+4 -4
View File
@@ -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
@@ -248,28 +248,28 @@ static inline void uart_transfer_event(void) {
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();
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();
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();
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++; }
+4 -5
View File
@@ -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));
}
+6 -6
View File
@@ -267,8 +267,8 @@ static void test_uart_periodic(void) {
if (idx_tx<sizeof(buf_src)) {
switch (direction) {
case OneToThree : Uart1Transmit(buf_src[idx_tx]); break;
case ThreeToOne : Uart3Transmit(buf_src[idx_tx]); break;
case OneToThree : uart_transmit(&uart1, buf_src[idx_tx]); break;
case ThreeToOne : uart_transmit(&uart3, buf_src[idx_tx]); break;
default: break;
}
idx_tx++;
@@ -278,8 +278,8 @@ static void test_uart_periodic(void) {
static void test_uart_event(void) {
if (Uart3ChAvailable()) {
buf_dest[idx_rx] = Uart3Getch();
if (uart_char_available(&uart3)) {
buf_dest[idx_rx] = uart_getch(&uart3);
if (idx_rx<sizeof(buf_src)) {
DOWNLINK_SEND_DEBUG(DefaultChannel, DefaultDevice, sizeof(buf_src), buf_dest);
idx_rx++;
@@ -296,8 +296,8 @@ static void test_uart_event(void) {
}
}
if (Uart1ChAvailable()) {
buf_dest[idx_rx] = Uart1Getch();
if (uart_char_available(&uart1)) {
buf_dest[idx_rx] = uart_getch(&uart1);
if (idx_rx<sizeof(buf_src)) {
DOWNLINK_SEND_DEBUG(DefaultChannel, DefaultDevice, sizeof(buf_src), buf_dest);
idx_rx++;
+9 -9
View File
@@ -47,12 +47,12 @@ static inline void main_init( void ) {
static inline void main_periodic( void ) {
char ch;
Uart1Transmit('a');
Uart2Transmit('b');
Uart3Transmit('c');
uart_transmit(&uart1, 'a');
uart_transmit(&uart2, 'b');
uart_transmit(&uart3, 'c');
if (Uart1ChAvailable()) {
ch = Uart1Getch();
if (uart_char_available(&uart1)) {
ch = uart_getch(&uart1);
if (ch == 'a') {
LED_OFF(0);
LED_ON(1);
@@ -65,8 +65,8 @@ static inline void main_periodic( void ) {
LED_OFF(1);
}
if (Uart2ChAvailable()) {
ch = Uart2Getch();
if (uart_char_available(&uart2)) {
ch = uart_getch(&uart2);
if (ch == 'b') {
LED_OFF(2);
LED_ON(3);
@@ -79,8 +79,8 @@ static inline void main_periodic( void ) {
LED_OFF(3);
}
if (Uart3ChAvailable()) {
ch = Uart3Getch();
if (uart_char_available(&uart3)) {
ch = uart_getch(&uart3);
if (ch == 'c') {
LED_OFF(4);
LED_ON(5);
+12 -12
View File
@@ -55,16 +55,16 @@ static inline void main_init( void ) {
static inline void main_periodic( void ) {
char ch;
Uart1Transmit('a');
Uart2Transmit('b');
Uart3Transmit('c');
Uart5Transmit('d');
uart_transmit(&uart1, 'a');
uart_transmit(&uart2, 'b');
uart_transmit(&uart3, 'c');
uart_transmit(&uart5, 'd');
LED_OFF(1);
LED_OFF(2);
if (Uart1ChAvailable()) {
ch = Uart1Getch();
if (uart_char_available(&uart1)) {
ch = uart_getch(&uart1);
if (ch == 'a') {
LED_ON(1);
} else {
@@ -72,8 +72,8 @@ static inline void main_periodic( void ) {
}
}
if (Uart2ChAvailable()) {
ch = Uart2Getch();
if (uart_char_available(&uart2)) {
ch = uart_getch(&uart2);
if (ch == 'b') {
LED_ON(1);
} else {
@@ -81,8 +81,8 @@ static inline void main_periodic( void ) {
}
}
if (Uart3ChAvailable()) {
ch = Uart3Getch();
if (uart_char_available(&uart3)) {
ch = uart_getch(&uart3);
if (ch == 'c') {
LED_ON(1);
} else {
@@ -90,8 +90,8 @@ static inline void main_periodic( void ) {
}
}
if (Uart5ChAvailable()) {
ch = Uart5Getch();
if (uart_char_available(&uart5)) {
ch = uart_getch(&uart5);
if (ch == 'd') {
LED_ON(1);
} else {
+5
View File
@@ -57,3 +57,8 @@ bool_t uart_check_free_space(struct uart_periph* p, uint8_t len) {
return (uint16_t)(space - 1) >= 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;
}
+14 -17
View File
@@ -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)
+1 -1
View File
@@ -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();
+2 -2
View File
@@ -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();