re-format the coding style in serial.c

1, Tabs to Spaces
2, File Format(CR/LF) using UNIX style
3, maximum line length = 80

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2281 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
dzzxzz@gmail.com 2012-09-07 08:54:56 +00:00
parent 095b0ae5c0
commit e699e09a11
1 changed files with 379 additions and 364 deletions

View File

@ -1,364 +1,379 @@
/* /*
* File : serial.c * File : serial.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE * http://www.rt-thread.org/license/LICENSE
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
* 2006-03-13 bernard first version * 2006-03-13 bernard first version
* 2012-05-15 lgnq modified according bernard's implementation. * 2012-05-15 lgnq modified according bernard's implementation.
* 2012-05-28 bernard code cleanup * 2012-05-28 bernard code cleanup
*/ */
#include <rthw.h> #include <rthw.h>
#include <rtthread.h> #include <rtthread.h>
#include <rtdevice.h> #include <rtdevice.h>
rt_inline void serial_ringbuffer_init(struct serial_ringbuffer *rbuffer) rt_inline void serial_ringbuffer_init(struct serial_ringbuffer *rbuffer)
{ {
rt_memset(rbuffer->buffer, 0, sizeof(rbuffer->buffer)); rt_memset(rbuffer->buffer, 0, sizeof(rbuffer->buffer));
rbuffer->put_index = 0; rbuffer->put_index = 0;
rbuffer->get_index = 0; rbuffer->get_index = 0;
} }
rt_inline void serial_ringbuffer_putc(struct serial_ringbuffer *rbuffer, char ch) rt_inline void serial_ringbuffer_putc(struct serial_ringbuffer *rbuffer,
{ char ch)
rt_base_t level; {
rt_base_t level;
/* disable interrupt */
level = rt_hw_interrupt_disable(); /* disable interrupt */
level = rt_hw_interrupt_disable();
rbuffer->buffer[rbuffer->put_index] = ch;
rbuffer->put_index = (rbuffer->put_index + 1) & (SERIAL_RBUFFER_SIZE - 1); rbuffer->buffer[rbuffer->put_index] = ch;
rbuffer->put_index = (rbuffer->put_index + 1) & (SERIAL_RBUFFER_SIZE - 1);
/* if the next position is read index, discard this 'read char' */
if (rbuffer->put_index == rbuffer->get_index) /* 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) & (SERIAL_RBUFFER_SIZE - 1); {
} rbuffer->get_index = (rbuffer->get_index + 1) & (SERIAL_RBUFFER_SIZE - 1);
}
/* enable interrupt */
rt_hw_interrupt_enable(level); /* enable interrupt */
} rt_hw_interrupt_enable(level);
}
rt_inline int serial_ringbuffer_putchar(struct serial_ringbuffer *rbuffer, char ch)
{ rt_inline int serial_ringbuffer_putchar(struct serial_ringbuffer *rbuffer,
rt_base_t level; char ch)
rt_uint16_t next_index; {
rt_base_t level;
/* disable interrupt */ rt_uint16_t next_index;
level = rt_hw_interrupt_disable();
/* disable interrupt */
next_index = (rbuffer->put_index + 1) & (SERIAL_RBUFFER_SIZE - 1); level = rt_hw_interrupt_disable();
if (next_index != rbuffer->get_index)
{ next_index = (rbuffer->put_index + 1) & (SERIAL_RBUFFER_SIZE - 1);
rbuffer->buffer[rbuffer->put_index] = ch; if (next_index != rbuffer->get_index)
rbuffer->put_index = next_index; {
} rbuffer->buffer[rbuffer->put_index] = ch;
else rbuffer->put_index = next_index;
{ }
/* enable interrupt */ else
rt_hw_interrupt_enable(level); {
return -1; /* enable interrupt */
} rt_hw_interrupt_enable(level);
/* enable interrupt */ return -1;
rt_hw_interrupt_enable(level); }
return 1;
} /* enable interrupt */
rt_hw_interrupt_enable(level);
rt_inline int serial_ringbuffer_getc(struct serial_ringbuffer *rbuffer)
{ return 1;
int ch; }
rt_base_t level;
rt_inline int serial_ringbuffer_getc(struct serial_ringbuffer *rbuffer)
ch = -1; {
/* disable interrupt */ int ch;
level = rt_hw_interrupt_disable(); rt_base_t level;
if (rbuffer->get_index != rbuffer->put_index)
{ ch = -1;
ch = rbuffer->buffer[rbuffer->get_index]; /* disable interrupt */
rbuffer->get_index = (rbuffer->get_index + 1) & (SERIAL_RBUFFER_SIZE - 1); level = rt_hw_interrupt_disable();
} if (rbuffer->get_index != rbuffer->put_index)
/* enable interrupt */ {
rt_hw_interrupt_enable(level); ch = rbuffer->buffer[rbuffer->get_index];
return ch; rbuffer->get_index = (rbuffer->get_index + 1) & (SERIAL_RBUFFER_SIZE - 1);
} }
/* enable interrupt */
rt_inline rt_uint32_t serial_ringbuffer_size(struct serial_ringbuffer *rbuffer) rt_hw_interrupt_enable(level);
{
rt_uint32_t size; return ch;
rt_base_t level; }
level = rt_hw_interrupt_disable(); rt_inline rt_uint32_t serial_ringbuffer_size(struct serial_ringbuffer *rbuffer)
size = (rbuffer->put_index - rbuffer->get_index) & (SERIAL_RBUFFER_SIZE - 1); {
rt_hw_interrupt_enable(level); rt_uint32_t size;
rt_base_t level;
return size;
} level = rt_hw_interrupt_disable();
size = (rbuffer->put_index - rbuffer->get_index) & (SERIAL_RBUFFER_SIZE - 1);
/* RT-Thread Device Interface */ rt_hw_interrupt_enable(level);
/* return size;
* This function initializes serial }
*/
static rt_err_t rt_serial_init(struct rt_device *dev) /* RT-Thread Device Interface */
{
rt_err_t result = RT_EOK; /*
struct rt_serial_device *serial; * This function initializes serial
*/
RT_ASSERT(dev != RT_NULL); static rt_err_t rt_serial_init(struct rt_device *dev)
serial = (struct rt_serial_device *)dev; {
rt_err_t result = RT_EOK;
if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED)) struct rt_serial_device *serial;
{
/* apply configuration */ RT_ASSERT(dev != RT_NULL);
if (serial->ops->configure) serial = (struct rt_serial_device *)dev;
result = serial->ops->configure(serial, &serial->config);
if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
if (result != RT_EOK) {
return result; /* apply configuration */
if (serial->ops->configure)
if (dev->flag & RT_DEVICE_FLAG_INT_RX) result = serial->ops->configure(serial, &serial->config);
serial_ringbuffer_init(serial->int_rx);
if (result != RT_EOK)
if (dev->flag & RT_DEVICE_FLAG_INT_TX) return result;
serial_ringbuffer_init(serial->int_tx);
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
/* set activated */ serial_ringbuffer_init(serial->int_rx);
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
} if (dev->flag & RT_DEVICE_FLAG_INT_TX)
serial_ringbuffer_init(serial->int_tx);
return result;
} /* set activated */
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag) }
{
struct rt_serial_device *serial; return result;
rt_uint32_t int_flags = 0; }
RT_ASSERT(dev != RT_NULL); static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
serial = (struct rt_serial_device *)dev; {
struct rt_serial_device *serial;
if (dev->flag & RT_DEVICE_FLAG_INT_RX) rt_uint32_t int_flags = 0;
int_flags = RT_SERIAL_RX_INT;
if (dev->flag & RT_DEVICE_FLAG_INT_TX) RT_ASSERT(dev != RT_NULL);
int_flags |= RT_SERIAL_TX_INT; serial = (struct rt_serial_device *)dev;
if (int_flags) if (dev->flag & RT_DEVICE_FLAG_INT_RX)
{ int_flags = RT_SERIAL_RX_INT;
serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)int_flags); if (dev->flag & RT_DEVICE_FLAG_INT_TX)
} int_flags |= RT_SERIAL_TX_INT;
return RT_EOK; if (int_flags)
} {
serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)int_flags);
static rt_err_t rt_serial_close(struct rt_device *dev) }
{
struct rt_serial_device *serial; return RT_EOK;
rt_uint32_t int_flags = 0; }
RT_ASSERT(dev != RT_NULL); static rt_err_t rt_serial_close(struct rt_device *dev)
serial = (struct rt_serial_device *)dev; {
struct rt_serial_device *serial;
if (dev->flag & RT_DEVICE_FLAG_INT_RX) rt_uint32_t int_flags = 0;
int_flags = RT_SERIAL_RX_INT;
if (dev->flag & RT_DEVICE_FLAG_INT_TX) RT_ASSERT(dev != RT_NULL);
int_flags |= RT_SERIAL_TX_INT; serial = (struct rt_serial_device *)dev;
if (int_flags) if (dev->flag & RT_DEVICE_FLAG_INT_RX)
{ int_flags = RT_SERIAL_RX_INT;
serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *)int_flags); if (dev->flag & RT_DEVICE_FLAG_INT_TX)
} int_flags |= RT_SERIAL_TX_INT;
return RT_EOK; if (int_flags)
} {
serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *)int_flags);
static rt_size_t rt_serial_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size) }
{
rt_uint8_t *ptr; return RT_EOK;
rt_uint32_t read_nbytes; }
struct rt_serial_device *serial;
static rt_size_t rt_serial_read(struct rt_device *dev,
RT_ASSERT(dev != RT_NULL); rt_off_t pos,
serial = (struct rt_serial_device *)dev; void *buffer,
rt_size_t size)
ptr = (rt_uint8_t *)buffer; {
rt_uint8_t *ptr;
if (dev->flag & RT_DEVICE_FLAG_INT_RX) rt_uint32_t read_nbytes;
{ struct rt_serial_device *serial;
/* interrupt mode Rx */
while (size) RT_ASSERT(dev != RT_NULL);
{ serial = (struct rt_serial_device *)dev;
int ch;
ptr = (rt_uint8_t *)buffer;
ch = serial_ringbuffer_getc(serial->int_rx);
if (ch == -1) if (dev->flag & RT_DEVICE_FLAG_INT_RX)
break; {
/* interrupt mode Rx */
*ptr = ch & 0xff; while (size)
ptr ++; {
size --; int ch;
}
} ch = serial_ringbuffer_getc(serial->int_rx);
else if (ch == -1)
{ break;
/* polling mode */
while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size) *ptr = ch & 0xff;
{ ptr ++;
*ptr = serial->ops->getc(serial); size --;
ptr ++; }
} }
} else
{
read_nbytes = (rt_uint32_t)ptr - (rt_uint32_t)buffer; /* polling mode */
/* set error code */ while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size)
if (read_nbytes == 0) {
{ *ptr = serial->ops->getc(serial);
rt_set_errno(-RT_EEMPTY); ptr ++;
} }
}
return read_nbytes;
} read_nbytes = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
/* set error code */
static rt_size_t rt_serial_write(struct rt_device *dev, rt_off_t pos, if (read_nbytes == 0)
const void *buffer, rt_size_t size) {
{ rt_set_errno(-RT_EEMPTY);
rt_uint8_t *ptr; }
rt_size_t write_nbytes = 0;
struct rt_serial_device *serial; return read_nbytes;
}
RT_ASSERT(dev != RT_NULL);
serial = (struct rt_serial_device *)dev; static rt_size_t rt_serial_write(struct rt_device *dev,
rt_off_t pos,
ptr = (rt_uint8_t*)buffer; const void *buffer,
rt_size_t size)
if (dev->flag & RT_DEVICE_FLAG_INT_TX) {
{ rt_uint8_t *ptr;
/* warning: data will be discarded if buffer is full */ rt_size_t write_nbytes = 0;
while (size) struct rt_serial_device *serial;
{
if (serial_ringbuffer_putchar(serial->int_tx, *ptr) != -1) RT_ASSERT(dev != RT_NULL);
{ serial = (struct rt_serial_device *)dev;
ptr ++;
size --; ptr = (rt_uint8_t*)buffer;
}
else if (dev->flag & RT_DEVICE_FLAG_INT_TX)
break; {
} /* warning: data will be discarded if buffer is full */
} while (size)
else {
{ if (serial_ringbuffer_putchar(serial->int_tx, *ptr) != -1)
/* polling mode */ {
while (size) ptr ++;
{ size --;
/* }
* to be polite with serial console add a line feed else
* to the carriage return character break;
*/ }
if (*ptr == '\n' && (dev->flag & RT_DEVICE_FLAG_STREAM)) }
{ else
serial->ops->putc(serial, '\r'); {
} /* polling mode */
while (size)
serial->ops->putc(serial, *ptr); {
/*
++ptr; * to be polite with serial console add a line feed
--size; * to the carriage return character
} */
} if (*ptr == '\n' && (dev->flag & RT_DEVICE_FLAG_STREAM))
{
write_nbytes = (rt_uint32_t)ptr - (rt_uint32_t)buffer; serial->ops->putc(serial, '\r');
if (write_nbytes == 0) }
{
rt_set_errno(-RT_EFULL); serial->ops->putc(serial, *ptr);
}
++ ptr;
return write_nbytes; -- size;
} }
}
static rt_err_t rt_serial_control(struct rt_device *dev, rt_uint8_t cmd, void *args)
{ write_nbytes = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
struct rt_serial_device *serial; if (write_nbytes == 0)
{
RT_ASSERT(dev != RT_NULL); rt_set_errno(-RT_EFULL);
serial = (struct rt_serial_device *)dev; }
switch (cmd) return write_nbytes;
{ }
case RT_DEVICE_CTRL_SUSPEND:
/* suspend device */ static rt_err_t rt_serial_control(struct rt_device *dev,
dev->flag |= RT_DEVICE_FLAG_SUSPENDED; rt_uint8_t cmd,
break; void *args)
{
case RT_DEVICE_CTRL_RESUME: struct rt_serial_device *serial;
/* resume device */
dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED; RT_ASSERT(dev != RT_NULL);
break; serial = (struct rt_serial_device *)dev;
case RT_DEVICE_CTRL_CONFIG: switch (cmd)
/* configure device */ {
serial->ops->configure(serial, (struct serial_configure *)args); case RT_DEVICE_CTRL_SUSPEND:
break; /* suspend device */
} dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
break;
return RT_EOK;
} case RT_DEVICE_CTRL_RESUME:
/* resume device */
/* dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
* serial register break;
*/
rt_err_t rt_hw_serial_register(struct rt_serial_device *serial, const char *name, rt_uint32_t flag, void *data) case RT_DEVICE_CTRL_CONFIG:
{ /* configure device */
struct rt_device *device; serial->ops->configure(serial, (struct serial_configure *)args);
RT_ASSERT(serial != RT_NULL); break;
}
device = &(serial->parent);
return RT_EOK;
device->type = RT_Device_Class_Char; }
device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL; /*
* serial register
device->init = rt_serial_init; */
device->open = rt_serial_open; rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
device->close = rt_serial_close; const char *name,
device->read = rt_serial_read; rt_uint32_t flag,
device->write = rt_serial_write; void *data)
device->control = rt_serial_control; {
device->user_data = data; struct rt_device *device;
RT_ASSERT(serial != RT_NULL);
/* register a character device */
return rt_device_register(device, name, flag); device = &(serial->parent);
}
device->type = RT_Device_Class_Char;
/* ISR for serial interrupt */ device->rx_indicate = RT_NULL;
void rt_hw_serial_isr(struct rt_serial_device *serial) device->tx_complete = RT_NULL;
{
int ch = -1; device->init = rt_serial_init;
device->open = rt_serial_open;
/* interrupt mode receive */ device->close = rt_serial_close;
RT_ASSERT(serial->parent.flag & RT_DEVICE_FLAG_INT_RX); device->read = rt_serial_read;
device->write = rt_serial_write;
while (1) device->control = rt_serial_control;
{ device->user_data = data;
ch = serial->ops->getc(serial);
if (ch == -1) /* register a character device */
break; return rt_device_register(device, name, flag);
}
serial_ringbuffer_putc(serial->int_rx, ch);
} /* ISR for serial interrupt */
void rt_hw_serial_isr(struct rt_serial_device *serial)
/* invoke callback */ {
if (serial->parent.rx_indicate != RT_NULL) int ch = -1;
{
rt_size_t rx_length; /* interrupt mode receive */
RT_ASSERT(serial->parent.flag & RT_DEVICE_FLAG_INT_RX);
/* get rx length */
rx_length = serial_ringbuffer_size(serial->int_rx); while (1)
serial->parent.rx_indicate(&serial->parent, rx_length); {
} ch = serial->ops->getc(serial);
} if (ch == -1)
break;
serial_ringbuffer_putc(serial->int_rx, ch);
}
/* invoke callback */
if (serial->parent.rx_indicate != RT_NULL)
{
rt_size_t rx_length;
/* get rx length */
rx_length = serial_ringbuffer_size(serial->int_rx);
serial->parent.rx_indicate(&serial->parent, rx_length);
}
}