diff --git a/components/drivers/include/drivers/serial.h b/components/drivers/include/drivers/serial.h index 41132ed91..c966f5459 100644 --- a/components/drivers/include/drivers/serial.h +++ b/components/drivers/include/drivers/serial.h @@ -29,6 +29,7 @@ #define __SERIAL_H__ #include +#include #define BAUD_RATE_4800 4800 #define BAUD_RATE_9600 9600 @@ -55,6 +56,11 @@ #define NRZ_NORMAL 0 /* Non Return to Zero : normal mode */ #define NRZ_INVERTED 1 /* Non Return to Zero : inverted mode */ +#define HW_CONTROL_NONE 0 +#define HW_CONTROL_RTS 1 +#define HW_CONTROL_CTS 2 +#define HW_CONTROL_RTS_CTS 3 + #ifndef RT_SERIAL_RB_BUFSZ #define RT_SERIAL_RB_BUFSZ 64 #endif @@ -75,21 +81,23 @@ #define RT_SERIAL_TX_DATAQUEUE_LWM 30 /* Default config for serial_configure structure */ -#define RT_SERIAL_CONFIG_DEFAULT \ -{ \ - BAUD_RATE_115200, /* 115200 bits/s */ \ - DATA_BITS_8, /* 8 databits */ \ - STOP_BITS_1, /* 1 stopbit */ \ - PARITY_NONE, /* No parity */ \ - BIT_ORDER_LSB, /* LSB first sent */ \ - NRZ_NORMAL, /* Normal mode */ \ - 0 \ +#define RT_SERIAL_CONFIG_DEFAULT \ +{ \ + BAUD_RATE_115200, /* 115200 bits/s */ \ + DATA_BITS_8, /* 8 databits */ \ + STOP_BITS_1, /* 1 stopbit */ \ + PARITY_NONE, /* No parity */ \ + BIT_ORDER_LSB, /* LSB first sent */ \ + NRZ_NORMAL, /* Normal mode */ \ + HW_CONTROL_NONE, /* Hardware control */\ + 0 \ } struct serial_ringbuffer { - rt_uint8_t buffer[RT_SERIAL_RB_BUFSZ]; - rt_uint16_t put_index, get_index; + struct rt_ringbuffer rb; + rt_uint8_t *pool; + rt_uint16_t size; }; struct serial_configure @@ -100,7 +108,8 @@ struct serial_configure rt_uint32_t parity :2; rt_uint32_t bit_order :1; rt_uint32_t invert :1; - rt_uint32_t reserved :20; + rt_uint32_t hw_control :2; + rt_uint32_t reserved :18; }; struct rt_serial_device diff --git a/components/drivers/serial/serial.c b/components/drivers/serial/serial.c index 5e7a8ec81..a9b4e47b4 100644 --- a/components/drivers/serial/serial.c +++ b/components/drivers/serial/serial.c @@ -25,6 +25,9 @@ * 2012-11-23 bernard fix compiler warning. * 2013-02-20 bernard use RT_SERIAL_RB_BUFSZ to define * the size of ring buffer. + * 2014-02-26 bright use DeviceDriver ringbuffer. + * add hardware flow support. + * use new struct serial_ringbuffer. */ #include @@ -33,9 +36,7 @@ rt_inline void serial_ringbuffer_init(struct serial_ringbuffer *rbuffer) { - rt_memset(rbuffer->buffer, 0, sizeof(rbuffer->buffer)); - rbuffer->put_index = 0; - rbuffer->get_index = 0; + rt_ringbuffer_init(&rbuffer->rb, rbuffer->pool, rbuffer->size); } rt_inline void serial_ringbuffer_putc(struct serial_ringbuffer *rbuffer, @@ -45,17 +46,8 @@ rt_inline void serial_ringbuffer_putc(struct serial_ringbuffer *rbuffer, /* disable interrupt */ level = rt_hw_interrupt_disable(); - - rbuffer->buffer[rbuffer->put_index] = ch; - rbuffer->put_index = (rbuffer->put_index + 1) & (RT_SERIAL_RB_BUFSZ - 1); - - /* if the next position is read index, discard this 'read char' */ - if (rbuffer->put_index == rbuffer->get_index) - { - rbuffer->get_index = (rbuffer->get_index + 1) & (RT_SERIAL_RB_BUFSZ - 1); - } - - /* enable interrupt */ + rt_ringbuffer_putchar(&rbuffer->rb, ch); + // enable interrupt rt_hw_interrupt_enable(level); } @@ -63,25 +55,11 @@ rt_inline int serial_ringbuffer_putchar(struct serial_ringbuffer *rbuffer, char ch) { rt_base_t level; - rt_uint16_t next_index; + //rt_uint16_t next_index; /* disable interrupt */ level = rt_hw_interrupt_disable(); - - next_index = (rbuffer->put_index + 1) & (RT_SERIAL_RB_BUFSZ - 1); - if (next_index != rbuffer->get_index) - { - rbuffer->buffer[rbuffer->put_index] = ch; - rbuffer->put_index = next_index; - } - else - { - /* enable interrupt */ - rt_hw_interrupt_enable(level); - - return -1; - } - + rt_ringbuffer_putchar(&rbuffer->rb, ch); /* enable interrupt */ rt_hw_interrupt_enable(level); @@ -90,17 +68,14 @@ rt_inline int serial_ringbuffer_putchar(struct serial_ringbuffer *rbuffer, rt_inline int serial_ringbuffer_getc(struct serial_ringbuffer *rbuffer) { - int ch; + int ch = 0; rt_base_t level; - ch = -1; /* disable interrupt */ level = rt_hw_interrupt_disable(); - if (rbuffer->get_index != rbuffer->put_index) - { - ch = rbuffer->buffer[rbuffer->get_index]; - rbuffer->get_index = (rbuffer->get_index + 1) & (RT_SERIAL_RB_BUFSZ - 1); - } + /* get char */ + if (!rt_ringbuffer_getchar(&rbuffer->rb, (rt_uint8_t *)&ch)) + ch = -1; /* enable interrupt */ rt_hw_interrupt_enable(level); @@ -113,7 +88,10 @@ rt_inline rt_uint32_t serial_ringbuffer_size(struct serial_ringbuffer *rbuffer) rt_base_t level; level = rt_hw_interrupt_disable(); + size = rt_ringbuffer_space_len(&rbuffer->rb); + /* size = (rbuffer->put_index - rbuffer->get_index) & (RT_SERIAL_RB_BUFSZ - 1); + */ rt_hw_interrupt_enable(level); return size;