|
@@ -7,20 +7,59 @@
|
|
|
*/
|
|
|
|
|
|
#include <rthw.h>
|
|
|
-#include <rtthread.h>
|
|
|
|
|
|
#define _DEBUG_SERIAL 0
|
|
|
#include "serial.h"
|
|
|
#include <stdio.h>
|
|
|
-struct rt_device serial_device;
|
|
|
-//extern struct serial_int_rx serial_rx;
|
|
|
-struct serial_int_rx serial_rx;
|
|
|
|
|
|
#if 0
|
|
|
static FILE *fp = RT_NULL;
|
|
|
#endif
|
|
|
|
|
|
/*@{*/
|
|
|
+int seial_save_byte(unsigned char ch, struct serial_device * serial)
|
|
|
+{
|
|
|
+ /* save on rx buffer */
|
|
|
+ rt_base_t level;
|
|
|
+ struct rt_device * dev = RT_DEVICE(serial);
|
|
|
+ /* disable interrupt */
|
|
|
+ //暂时关闭中断,因为要操作uart数据结构
|
|
|
+ level = rt_hw_interrupt_disable();
|
|
|
+
|
|
|
+ /* save character */
|
|
|
+ serial->serial_rx.rx_buffer[serial->serial_rx.save_index] = ch;
|
|
|
+ serial->serial_rx.save_index ++;
|
|
|
+ //下面的代码检查save_index是否已经到到缓冲区尾部,如果是则回转到头部,称为一个环形缓冲区
|
|
|
+ if (serial->serial_rx.save_index >= SERIAL_RX_BUFFER_SIZE)
|
|
|
+ serial->serial_rx.save_index = 0;
|
|
|
+
|
|
|
+ //这种情况表示反转后的save_index追上了read_index,则增大read_index,丢弃一个旧的数据
|
|
|
+ /* if the next position is read index, discard this 'read char' */
|
|
|
+ if (serial->serial_rx.save_index == serial->serial_rx.read_index)
|
|
|
+ {
|
|
|
+ serial->serial_rx.read_index ++;
|
|
|
+ if (serial->serial_rx.read_index >= SERIAL_RX_BUFFER_SIZE)
|
|
|
+ serial->serial_rx.read_index = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* enable interrupt */
|
|
|
+ //uart数据结构已经操作完成,重新使能中断
|
|
|
+ rt_hw_interrupt_enable(level);
|
|
|
+
|
|
|
+ /* invoke callback */
|
|
|
+ if (dev->rx_indicate != RT_NULL)
|
|
|
+ {
|
|
|
+ rt_size_t rx_length;
|
|
|
+
|
|
|
+ /* get rx length */
|
|
|
+ rx_length = serial->serial_rx.read_index > serial->serial_rx.save_index ?
|
|
|
+ SERIAL_RX_BUFFER_SIZE - serial->serial_rx.read_index + serial->serial_rx.save_index :
|
|
|
+ serial->serial_rx.save_index - serial->serial_rx.read_index;
|
|
|
+
|
|
|
+ dev->rx_indicate(dev, rx_length);
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
|
|
|
/* RT-Thread Device Interface */
|
|
|
/**
|
|
@@ -28,14 +67,15 @@ static FILE *fp = RT_NULL;
|
|
|
*/
|
|
|
static rt_err_t rt_serial_init(rt_device_t dev)
|
|
|
{
|
|
|
+ struct serial_device * serial = SERIAL_DEVICE(dev);
|
|
|
if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
|
|
|
{
|
|
|
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
|
|
|
{
|
|
|
- rt_memset(serial_rx.rx_buffer, 0,
|
|
|
- sizeof(serial_rx.rx_buffer));
|
|
|
- serial_rx.read_index = 0;
|
|
|
- serial_rx.save_index = 0;
|
|
|
+ rt_memset(serial->serial_rx.rx_buffer, 0,
|
|
|
+ sizeof(serial->serial_rx.rx_buffer));
|
|
|
+ serial->serial_rx.read_index = 0;
|
|
|
+ serial->serial_rx.save_index = 0;
|
|
|
}
|
|
|
|
|
|
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
|
|
@@ -62,6 +102,7 @@ static rt_size_t rt_serial_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_
|
|
|
{
|
|
|
rt_uint8_t *ptr;
|
|
|
rt_err_t err_code;
|
|
|
+ struct serial_device * serial = SERIAL_DEVICE(dev);
|
|
|
|
|
|
ptr = buffer;
|
|
|
err_code = RT_EOK;
|
|
@@ -76,16 +117,16 @@ static rt_size_t rt_serial_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_
|
|
|
/* disable interrupt */
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
|
- if (serial_rx.read_index != serial_rx.save_index)
|
|
|
+ if (serial->serial_rx.read_index != serial->serial_rx.save_index)
|
|
|
{
|
|
|
/* read a character */
|
|
|
- *ptr++ = serial_rx.rx_buffer[serial_rx.read_index];
|
|
|
+ *ptr++ = serial->serial_rx.rx_buffer[serial->serial_rx.read_index];
|
|
|
size--;
|
|
|
|
|
|
/* move to next position */
|
|
|
- serial_rx.read_index ++;
|
|
|
- if (serial_rx.read_index >= SERIAL_RX_BUFFER_SIZE)
|
|
|
- serial_rx.read_index = 0;
|
|
|
+ serial->serial_rx.read_index ++;
|
|
|
+ if (serial->serial_rx.read_index >= SERIAL_RX_BUFFER_SIZE)
|
|
|
+ serial->serial_rx.read_index = 0;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -149,7 +190,7 @@ static rt_err_t rt_serial_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
|
|
/*
|
|
|
* serial register
|
|
|
*/
|
|
|
-static rt_err_t rt_hw_serial_register(rt_device_t device, const char *name, rt_uint32_t flag)
|
|
|
+rt_err_t rt_hw_serial_register(rt_device_t device, const char *name, rt_uint32_t flag)
|
|
|
{
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
#if _DEBUG_SERIAL==1
|
|
@@ -170,8 +211,8 @@ static rt_err_t rt_hw_serial_register(rt_device_t device, const char *name, rt_u
|
|
|
return rt_device_register(device, name, (rt_uint16_t)(RT_DEVICE_FLAG_RDWR | flag));
|
|
|
}
|
|
|
|
|
|
-rt_err_t rt_hw_serial_init(void)
|
|
|
+rt_err_t rt_hw_serial_init(struct serial_device * serial, char * name)
|
|
|
{
|
|
|
- return rt_hw_serial_register(&serial_device, RT_CONSOLE_DEVICE_NAME,
|
|
|
+ return rt_hw_serial_register(RT_DEVICE(serial), name,
|
|
|
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM);
|
|
|
}
|