提交 7fddcb0e 编写于 作者: D dzzxzz@gmail.com

add serial device driver framework

and implement the example in MB9BF506R branch

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2145 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 b862d16f
......@@ -18,7 +18,7 @@
#include "board.h"
#include "mcu.h"
#include "serial.h"
#include "fm3_uart.h"
#include "nand.h"
/**
......
#include <rtthread.h>
#include <serial.h>
#define RT_CONSOLE_WIDTH 240
#define RT_CONSOLE_HEIGHT 320
#define RT_CONSOLE_FONT_WIDTH 8
#define RT_CONSOLE_FONT_HEIGHT 16
#define RT_CONSOLE_COL (RT_CONSOLE_WIDTH/RT_CONSOLE_FONT_WIDTH)
#define RT_CONSOLE_ROW (RT_CONSOLE_HEIGHT/RT_CONSOLE_FONT_HEIGHT)
#define RT_CONSOLE_TAB 4
#define RT_CONSOLE_FOREPIXEL (0x001f)
extern struct serial_device uart0;
struct rt_console
{
rt_uint8_t *video_ptr;
rt_uint8_t *font_ptr;
/* bpp and pixel of width */
rt_uint8_t bpp;
rt_uint32_t pitch;
/* current cursor */
rt_uint8_t current_col;
rt_uint8_t current_row;
};
struct rt_console console;
void rt_hw_console_init(rt_uint8_t *video_ptr, rt_uint8_t *font_ptr, rt_uint8_t bpp);
void rt_hw_console_newline(void);
void rt_hw_console_putc(char c);
void rt_hw_console_clear(void);
void rt_hw_console_init(rt_uint8_t *video_ptr, rt_uint8_t *font_ptr, rt_uint8_t bpp)
{
rt_memset(&console, 0, sizeof(struct rt_console));
console.video_ptr = video_ptr;
console.font_ptr = font_ptr;
console.bpp = bpp;
console.pitch = console.bpp * RT_CONSOLE_WIDTH;
rt_hw_console_clear();
}
void rt_hw_console_putc(char c)
{
switch (c)
{
case 10:
case 11:
case 12:
case 13:
/* to next line */
rt_hw_console_newline();
console.current_col = 0;
break;
case 9:
console.current_col += RT_CONSOLE_TAB;
break;
default:
{
rt_uint8_t *font_ptr;
register rt_uint32_t cursor;
register rt_uint32_t i, j;
if (console.current_col == RT_CONSOLE_COL)
{
rt_hw_console_newline();
console.current_col = 0;
rt_hw_console_putc(c);
return;
}
font_ptr = console.font_ptr + c * RT_CONSOLE_FONT_HEIGHT;
cursor = (console.current_row * RT_CONSOLE_FONT_HEIGHT) * console.pitch
+ console.current_col * RT_CONSOLE_FONT_WIDTH * console.bpp;
for (i = 0; i < RT_CONSOLE_FONT_HEIGHT; i ++ )
{
for (j = 0; j < RT_CONSOLE_FONT_WIDTH; j ++)
{
if (((font_ptr[i] >> (7-j)) & 0x01) != 0)
{
/* draw a pixel */
rt_uint8_t *ptr = &(console.video_ptr[cursor + i * console.pitch + j * console.bpp]);
switch (console.bpp)
{
case 1:
*ptr = RT_CONSOLE_FOREPIXEL;
break;
case 2:
*(rt_uint16_t*)ptr = RT_CONSOLE_FOREPIXEL;
break;
case 3:
ptr[0] = RT_CONSOLE_FOREPIXEL & 0xff;
ptr[1] = (RT_CONSOLE_FOREPIXEL >> 8) & 0xff;
ptr[2] = (RT_CONSOLE_FOREPIXEL >> 16) & 0xff;
break;
case 4:
*(rt_uint32_t*)ptr = RT_CONSOLE_FOREPIXEL;
break;
}
}
}
}
console.current_col ++;
}
break;
}
}
void rt_hw_console_newline(void)
{
console.current_row ++;
if (console.current_row >= RT_CONSOLE_ROW)
{
rt_uint32_t i;
/* scroll to next line */
for (i = 0; i < RT_CONSOLE_ROW - 1; i ++)
{
rt_memcpy(console.video_ptr + i * RT_CONSOLE_FONT_HEIGHT * console.pitch,
console.video_ptr + (i + 1) * RT_CONSOLE_FONT_HEIGHT * console.pitch,
RT_CONSOLE_FONT_HEIGHT * console.pitch);
}
/* clear last line */
rt_memset(console.video_ptr + (RT_CONSOLE_ROW - 1) * RT_CONSOLE_FONT_HEIGHT * console.pitch,
0,
RT_CONSOLE_FONT_HEIGHT * console.pitch);
console.current_row = RT_CONSOLE_ROW - 1;
}
}
void rt_hw_console_clear(void)
{
console.current_col = 0;
console.current_row = 0;
rt_memset(console.video_ptr, 0, RT_CONSOLE_HEIGHT * console.pitch);
}
/* write one character to serial, must not trigger interrupt */
void rt_hw_serial_putc(const char c)
{
/*
to be polite with serial console add a line feed
to the carriage return character
*/
if (c=='\n')
rt_hw_serial_putc('\r');
while (!(uart0.uart_device->SSR & SSR_TDRE))
;
uart0.uart_device->TDR = (c & 0x1FF);
}
/**
* This function is used by rt_kprintf to display a string on console.
*
* @param str the displayed string
*/
void rt_hw_console_output(const char *str)
{
while (*str)
{
rt_hw_serial_putc(*str++);
}
}
/*
* File : serial.h
* File : fm3_uart.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006, RT-Thread Development Team
*
......@@ -13,12 +13,10 @@
* 2011-05-15 lgnq modified according bernard's implementaion.
*/
#ifndef __RT_HW_SERIAL_H__
#define __RT_HW_SERIAL_H__
#ifndef __FM3_UART_H__
#define __FM3_UART_H__
#include <rthw.h>
#include <rtthread.h>
#include "mb9bf506r.h"
#define SMR_SOE 0x01U
......@@ -57,22 +55,7 @@
#define ESCR_DATABITS_7 0x03U
#define ESCR_DATABITS_9 0x04U
#define BPS 115200 /* serial baudrate */
#define UART_RX_BUFFER_SIZE 64
#define UART_TX_BUFFER_SIZE 64
struct serial_int_rx
{
rt_uint8_t rx_buffer[UART_RX_BUFFER_SIZE];
rt_uint32_t read_index, save_index;
};
struct serial_int_tx
{
rt_uint8_t tx_buffer[UART_TX_BUFFER_SIZE];
rt_uint32_t write_index, save_index;
};
#define FIFO_SIZE 16
/*
* Enable/DISABLE Interrupt Controller
......@@ -81,19 +64,23 @@ struct serial_int_tx
#define UART_ENABLE_IRQ(n) NVIC_EnableIRQ((n))
#define UART_DISABLE_IRQ(n) NVIC_DisableIRQ((n))
struct serial_device
struct uart03_device
{
FM3_MFS03_UART_TypeDef *uart_device;
FM3_MFS03_UART_TypeDef *uart_regs;
/* irq number */
IRQn_Type rx_irq, tx_irq;
IRQn_Type rx_irq;
IRQn_Type tx_irq;
};
/* rx structure */
struct serial_int_rx *int_rx;
/* tx structure */
struct serial_int_tx *int_tx;
struct uart47_device
{
FM3_MFS47_UART_TypeDef *uart_regs;
/* irq number */
IRQn_Type rx_irq;
IRQn_Type tx_irq;
rt_uint8_t fifo_size;
};
void rt_hw_serial_isr(rt_device_t device);
void rt_hw_serial_init(void);
#endif
......@@ -301,6 +301,7 @@
<state>$PROJ_DIR$\applications</state>
<state>$PROJ_DIR$\libraries\CMSIS\RTOS</state>
<state>$PROJ_DIR$\..\..\libcpu\arm\cortex-m3</state>
<state>$PROJ_DIR$\..\..\components\drivers\include</state>
<state>$PROJ_DIR$\..\..\libcpu\arm\common</state>
<state>$PROJ_DIR$\..\..\components\init</state>
<state>$PROJ_DIR$\..\..\components\finsh</state>
......@@ -1218,6 +1219,7 @@
<state>$PROJ_DIR$\applications</state>
<state>$PROJ_DIR$\libraries\CMSIS\RTOS</state>
<state>$PROJ_DIR$\..\..\libcpu\arm\cortex-m3</state>
<state>$PROJ_DIR$\..\..\components\drivers\include</state>
<state>$PROJ_DIR$\..\..\libcpu\arm\common</state>
<state>$PROJ_DIR$\..\..\components\init</state>
<state>$PROJ_DIR$\..\..\components\finsh</state>
......@@ -1847,6 +1849,9 @@
</group>
<group>
<name>CMSIS</name>
<file>
<name>$PROJ_DIR$\libraries\CMSIS\RTOS\rtt_cmsis.c</name>
</file>
<file>
<name>$PROJ_DIR$\libraries\Device\FUJISTU\MB9BF50x\Source\IAR\startup_mb9bf50x.S</name>
</file>
......@@ -1878,13 +1883,19 @@
<name>$PROJ_DIR$\..\..\libcpu\arm\common\showmem.c</name>
</file>
</group>
<group>
<name>DeviceDrivers</name>
<file>
<name>$PROJ_DIR$\..\..\components\drivers\serial\serial.c</name>
</file>
</group>
<group>
<name>Drivers</name>
<file>
<name>$PROJ_DIR$\drivers\board.c</name>
</file>
<file>
<name>$PROJ_DIR$\drivers\console.c</name>
<name>$PROJ_DIR$\drivers\fm3_uart.c</name>
</file>
<file>
<name>$PROJ_DIR$\drivers\led.c</name>
......@@ -1892,9 +1903,6 @@
<file>
<name>$PROJ_DIR$\drivers\nand.c</name>
</file>
<file>
<name>$PROJ_DIR$\drivers\serial.c</name>
</file>
</group>
<group>
<name>finsh</name>
......
......@@ -183,10 +183,10 @@
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
<ColumnNumber>48</ColumnNumber>
<ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>3</TopLine>
<CurrentLine>12</CurrentLine>
<TopLine>1</TopLine>
<CurrentLine>1</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>applications\application.c</PathWithFileName>
<FilenameWithoutPath>application.c</FilenameWithoutPath>
......@@ -199,8 +199,8 @@
<Focus>0</Focus>
<ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>66</TopLine>
<CurrentLine>89</CurrentLine>
<TopLine>76</TopLine>
<CurrentLine>88</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>applications\startup.c</PathWithFileName>
<FilenameWithoutPath>startup.c</FilenameWithoutPath>
......@@ -218,10 +218,10 @@
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
<ColumnNumber>0</ColumnNumber>
<ColumnNumber>18</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>0</TopLine>
<CurrentLine>0</CurrentLine>
<TopLine>18</TopLine>
<CurrentLine>21</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>drivers\board.c</PathWithFileName>
<FilenameWithoutPath>board.c</FilenameWithoutPath>
......@@ -237,8 +237,8 @@
<TopLine>0</TopLine>
<CurrentLine>0</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>drivers\console.c</PathWithFileName>
<FilenameWithoutPath>console.c</FilenameWithoutPath>
<PathWithFileName>drivers\fm3_uart.c</PathWithFileName>
<FilenameWithoutPath>fm3_uart.c</FilenameWithoutPath>
</File>
<File>
<GroupNumber>2</GroupNumber>
......@@ -268,8 +268,15 @@
<PathWithFileName>drivers\nand.c</PathWithFileName>
<FilenameWithoutPath>nand.c</FilenameWithoutPath>
</File>
</Group>
<Group>
<GroupName>CMSIS</GroupName>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<File>
<GroupNumber>2</GroupNumber>
<GroupNumber>3</GroupNumber>
<FileNumber>7</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
......@@ -279,16 +286,9 @@
<TopLine>0</TopLine>
<CurrentLine>0</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>drivers\serial.c</PathWithFileName>
<FilenameWithoutPath>serial.c</FilenameWithoutPath>
<PathWithFileName>libraries\Device\FUJISTU\MB9BF50x\Source\system_mb9bf50x.c</PathWithFileName>
<FilenameWithoutPath>system_mb9bf50x.c</FilenameWithoutPath>
</File>
</Group>
<Group>
<GroupName>CMSIS</GroupName>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>8</FileNumber>
......@@ -300,8 +300,8 @@
<TopLine>0</TopLine>
<CurrentLine>0</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>libraries\Device\FUJISTU\MB9BF50x\Source\system_mb9bf50x.c</PathWithFileName>
<FilenameWithoutPath>system_mb9bf50x.c</FilenameWithoutPath>
<PathWithFileName>libraries\CMSIS\RTOS\rtt_cmsis.c</PathWithFileName>
<FilenameWithoutPath>rtt_cmsis.c</FilenameWithoutPath>
</File>
<File>
<GroupNumber>3</GroupNumber>
......@@ -311,7 +311,7 @@
<Focus>0</Focus>
<ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>139</TopLine>
<TopLine>143</TopLine>
<CurrentLine>149</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>libraries\Device\FUJISTU\MB9BF50x\Source\ARM\startup_mb9bf50x.S</PathWithFileName>
......@@ -360,8 +360,8 @@
<Focus>0</Focus>
<ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>141</TopLine>
<CurrentLine>148</CurrentLine>
<TopLine>58</TopLine>
<CurrentLine>65</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\..\src\idle.c</PathWithFileName>
<FilenameWithoutPath>idle.c</FilenameWithoutPath>
......@@ -586,7 +586,7 @@
</Group>
<Group>
<GroupName>finsh</GroupName>
<GroupName>DeviceDrivers</GroupName>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
......@@ -596,6 +596,27 @@
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
<ColumnNumber>24</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>339</TopLine>
<CurrentLine>353</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\..\components\drivers\serial\serial.c</PathWithFileName>
<FilenameWithoutPath>serial.c</FilenameWithoutPath>
</File>
</Group>
<Group>
<GroupName>finsh</GroupName>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<File>
<GroupNumber>7</GroupNumber>
<FileNumber>29</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
<ColumnNumber>0</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>0</TopLine>
......@@ -605,8 +626,8 @@
<FilenameWithoutPath>cmd.c</FilenameWithoutPath>
</File>
<File>
<GroupNumber>6</GroupNumber>
<FileNumber>29</FileNumber>
<GroupNumber>7</GroupNumber>
<FileNumber>30</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
......@@ -619,8 +640,8 @@
<FilenameWithoutPath>finsh_compiler.c</FilenameWithoutPath>
</File>
<File>
<GroupNumber>6</GroupNumber>
<FileNumber>30</FileNumber>
<GroupNumber>7</GroupNumber>
<FileNumber>31</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
......@@ -633,8 +654,8 @@
<FilenameWithoutPath>finsh_error.c</FilenameWithoutPath>
</File>
<File>
<GroupNumber>6</GroupNumber>
<FileNumber>31</FileNumber>
<GroupNumber>7</GroupNumber>
<FileNumber>32</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
......@@ -647,8 +668,8 @@
<FilenameWithoutPath>finsh_heap.c</FilenameWithoutPath>
</File>
<File>
<GroupNumber>6</GroupNumber>
<FileNumber>32</FileNumber>
<GroupNumber>7</GroupNumber>
<FileNumber>33</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
......@@ -661,8 +682,8 @@
<FilenameWithoutPath>finsh_init.c</FilenameWithoutPath>
</File>
<File>
<GroupNumber>6</GroupNumber>
<FileNumber>33</FileNumber>
<GroupNumber>7</GroupNumber>
<FileNumber>34</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
......@@ -675,8 +696,8 @@
<FilenameWithoutPath>finsh_node.c</FilenameWithoutPath>
</File>
<File>
<GroupNumber>6</GroupNumber>
<FileNumber>34</FileNumber>
<GroupNumber>7</GroupNumber>
<FileNumber>35</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
......@@ -689,8 +710,8 @@
<FilenameWithoutPath>finsh_ops.c</FilenameWithoutPath>
</File>
<File>
<GroupNumber>6</GroupNumber>
<FileNumber>35</FileNumber>
<GroupNumber>7</GroupNumber>
<FileNumber>36</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
......@@ -703,8 +724,8 @@
<FilenameWithoutPath>finsh_parser.c</FilenameWithoutPath>
</File>
<File>
<GroupNumber>6</GroupNumber>
<FileNumber>36</FileNumber>
<GroupNumber>7</GroupNumber>
<FileNumber>37</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
......@@ -717,8 +738,8 @@
<FilenameWithoutPath>finsh_token.c</FilenameWithoutPath>
</File>
<File>
<GroupNumber>6</GroupNumber>
<FileNumber>37</FileNumber>
<GroupNumber>7</GroupNumber>
<FileNumber>38</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
......@@ -731,8 +752,8 @@
<FilenameWithoutPath>finsh_var.c</FilenameWithoutPath>
</File>
<File>
<GroupNumber>6</GroupNumber>
<FileNumber>38</FileNumber>
<GroupNumber>7</GroupNumber>
<FileNumber>39</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
......@@ -745,8 +766,8 @@
<FilenameWithoutPath>finsh_vm.c</FilenameWithoutPath>
</File>
<File>
<GroupNumber>6</GroupNumber>
<FileNumber>39</FileNumber>
<GroupNumber>7</GroupNumber>
<FileNumber>40</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
......@@ -759,8 +780,8 @@
<FilenameWithoutPath>shell.c</FilenameWithoutPath>
</File>
<File>
<GroupNumber>6</GroupNumber>
<FileNumber>40</FileNumber>
<GroupNumber>7</GroupNumber>
<FileNumber>41</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
......@@ -780,15 +801,15 @@
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<File>
<GroupNumber>7</GroupNumber>
<FileNumber>41</FileNumber>
<GroupNumber>8</GroupNumber>
<FileNumber>42</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<Focus>0</Focus>
<ColumnNumber>15</ColumnNumber>
<ColumnNumber>26</ColumnNumber>
<tvExpOptDlg>0</tvExpOptDlg>
<TopLine>3</TopLine>
<CurrentLine>20</CurrentLine>
<TopLine>11</TopLine>
<CurrentLine>17</CurrentLine>
<bDave2>0</bDave2>
<PathWithFileName>..\..\components\init\components_init.c</PathWithFileName>
<FilenameWithoutPath>components_init.c</FilenameWithoutPath>
......
......@@ -348,7 +348,7 @@
<MiscControls></MiscControls>
<Define></Define>
<Undefine></Undefine>
<IncludePath>.;..\..\components\finsh;..\..\components\init;..\..\include;..\..\libcpu\arm\common;..\..\libcpu\arm\cortex-m3;applications;drivers;libraries\CMSIS\Include;libraries\CMSIS\RTOS;libraries\Device\FUJISTU\MB9BF50x\Include</IncludePath>
<IncludePath>.;..\..\components\drivers\include;..\..\components\finsh;..\..\components\init;..\..\include;..\..\libcpu\arm\common;..\..\libcpu\arm\cortex-m3;applications;drivers;libraries\CMSIS\Include;libraries\CMSIS\RTOS;libraries\Device\FUJISTU\MB9BF50x\Include</IncludePath>
</VariousControls>
</Cads>
<Aads>
......@@ -409,9 +409,9 @@
<FilePath>drivers\board.c</FilePath>
</File>
<File>
<FileName>console.c</FileName>
<FileName>fm3_uart.c</FileName>
<FileType>1</FileType>
<FilePath>drivers\console.c</FilePath>
<FilePath>drivers\fm3_uart.c</FilePath>
</File>
<File>
<FileName>led.c</FileName>
......@@ -423,11 +423,6 @@
<FileType>1</FileType>
<FilePath>drivers\nand.c</FilePath>
</File>
<File>
<FileName>serial.c</FileName>
<FileType>1</FileType>
<FilePath>drivers\serial.c</FilePath>
</File>
</Files>
</Group>
<Group>
......@@ -438,6 +433,11 @@
<FileType>1</FileType>
<FilePath>libraries\Device\FUJISTU\MB9BF50x\Source\system_mb9bf50x.c</FilePath>
</File>
<File>
<FileName>rtt_cmsis.c</FileName>
<FileType>1</FileType>
<FilePath>libraries\CMSIS\RTOS\rtt_cmsis.c</FilePath>
</File>
<File>
<FileName>startup_mb9bf50x.S</FileName>
<FileType>2</FileType>
......@@ -545,6 +545,16 @@
</File>
</Files>
</Group>
<Group>
<GroupName>DeviceDrivers</GroupName>
<Files>
<File>
<FileName>serial.c</FileName>
<FileType>1</FileType>
<FilePath>..\..\components\drivers\serial\serial.c</FilePath>
</File>
</Files>
</Group>
<Group>
<GroupName>finsh</GroupName>
<Files>
......
......@@ -62,6 +62,8 @@
// <section name="RT_USING_DEVICE" description="Using Device Driver Framework" default="true" >
#define RT_USING_DEVICE
// <bool name="RT_USING_SERIAL" description="Using Serial Device Driver Framework" default="true" />
#define RT_USING_SERIAL
// <bool name="RT_USING_UART0_0" description="Using UART0_0" default="true" />
#define RT_USING_UART0_0
// <bool name="RT_USING_UART0_1" description="Using UART0_1" default="false" />
......@@ -87,7 +89,7 @@
// <bool name="RT_USING_UART4_1" description="Using UART4_1" default="false" />
// #define RT_USING_UART4_1
// <bool name="RT_USING_UART4_2" description="Using UART4_2" default="false" />
// #define RT_USING_UART4_2
#define RT_USING_UART4_2
// <bool name="RT_USING_UART5_0" description="Using UART5_0" default="false" />
// #define RT_USING_UART5_0
// <bool name="RT_USING_UART5_1" description="Using UART5_1" default="false" />
......@@ -99,7 +101,7 @@
// <bool name="RT_USING_UART6_1" description="Using UART6_1" default="false" />
// #define RT_USING_UART6_1
// <bool name="RT_USING_UART7_0" description="Using UART7_0" default="false" />
// #define RT_USING_UART7_0
#define RT_USING_UART7_0
// <bool name="RT_USING_UART7_1" description="Using UART7_1" default="false" />
// #define RT_USING_UART7_1
// <integer name="RT_UART_RX_BUFFER_SIZE" description="The buffer size for UART reception" default="64" />
......
/*
* File : serial.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2012-05-15 lgnq first version.
* 2012-05-28 bernard chage interfaces
*/
#ifndef __SERIAL_H__
#define __SERIAL_H__
#include <rtthread.h>
#define BAUD_RATE_9600 9600
#define BAUD_RATE_115200 115200
#define DATA_BITS_5 5
#define DATA_BITS_6 6
#define DATA_BITS_7 7
#define DATA_BITS_8 8
#define DATA_BITS_9 9
#define STOP_BITS_1 1
#define STOP_BITS_2 2
#define STOP_BITS_3 3
#define STOP_BITS_4 4
#define PARITY_NONE 0
#define PARITY_ODD 1
#define PARITY_EVEN 2
#define BIT_ORDER_LSB 0
#define BIT_ORDER_MSB 1
#define NRZ_NORMAL 0 /* Non Return to Zero : normal mode */
#define NRZ_INVERTED 1 /* Non Return to Zero : inverted mode */
#define UART_RX_BUFFER_SIZE 64
#define UART_TX_BUFFER_SIZE 64
#define SERIAL_RBUFFER_SIZE 64
#define RT_DEVICE_CTRL_CONFIG 0x03 /* configure device */
#define RT_DEVICE_CTRL_SET_INT 0x10 /* enable receive irq */
#define RT_DEVICE_CTRL_CLR_INT 0x11 /* disable receive irq */
#define RT_DEVICE_CTRL_GET_INT 0x12
#define RT_SERIAL_RX_INT 0x01
#define RT_SERIAL_TX_INT 0x02
#define RT_SERIAL_ERR_OVERRUN 0x01
#define RT_SERIAL_ERR_FRAMING 0x02
#define RT_SERIAL_ERR_PARITY 0x03
struct serial_ringbuffer
{
rt_uint8_t buffer[SERIAL_RBUFFER_SIZE];
rt_uint16_t put_index, get_index;
};
struct serial_configure
{
rt_uint32_t baud_rate;
rt_uint32_t data_bits :4;
rt_uint32_t stop_bits :2;
rt_uint32_t parity :2;
rt_uint32_t bit_order :1;
rt_uint32_t invert :1;
rt_uint32_t reserved :20;
};
struct rt_serial_device
{
struct rt_device parent;
const struct rt_uart_ops *ops;
struct serial_configure config;
/* rx structure */
struct serial_ringbuffer *int_rx;
/* tx structure */
struct serial_ringbuffer *int_tx;
};
typedef struct rt_serial_device rt_serial_t;
/**
* uart operators
*/
struct rt_uart_ops
{
rt_err_t (*configure)(struct rt_serial_device *serial, struct serial_configure *cfg);
rt_err_t (*control)(struct rt_serial_device *serial, int cmd, void *arg);
int (*putc)(struct rt_serial_device *serial, char c);
int (*getc)(struct rt_serial_device *serial);
};
void rt_hw_serial_isr(struct rt_serial_device *serial);
rt_err_t rt_hw_serial_register(struct rt_serial_device *serial, const char *name, rt_uint32_t flag, void *data);
#endif
......@@ -63,4 +63,8 @@ rt_size_t rt_ringbuffer_emptry_size(struct rt_ringbuffer* rb);
#include "drivers/usb_host.h"
#endif
#ifdef RT_USING_SERIAL
#include "drivers/serial.h"
#endif
#endif
from building import *
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd + '/../include']
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_SERIAL'], CPPPATH = CPPPATH)
Return('group')
/*
* File : serial.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2006-03-13 bernard first version
* 2012-05-15 lgnq modified according bernard's implementation.
* 2012-05-28 bernard code cleanup
*/
#include <rthw.h>
#include <rtthread.h>
#include <drivers/serial.h>
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_inline void serial_ringbuffer_putc(struct serial_ringbuffer* rbuffer, char ch)
{
rt_base_t level;
/* disable interrupt */
level = rt_hw_interrupt_disable();
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)
{
rbuffer->get_index = (rbuffer->get_index + 1) & (SERIAL_RBUFFER_SIZE - 1);
}
/* enable interrupt */
rt_hw_interrupt_enable(level);
}
rt_inline int serial_ringbuffer_putchar(struct serial_ringbuffer* rbuffer, char ch)
{
rt_base_t level;
rt_uint16_t next_index;
/* disable interrupt */
level = rt_hw_interrupt_disable();
next_index = (rbuffer->put_index + 1) & (SERIAL_RBUFFER_SIZE - 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;
}
/* enable interrupt */
rt_hw_interrupt_enable(level);
return 1;
}
rt_inline int serial_ringbuffer_getc(struct serial_ringbuffer* rbuffer)
{
int ch;
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) & (SERIAL_RBUFFER_SIZE - 1);
}
/* enable interrupt */
rt_hw_interrupt_enable(level);
return ch;
}
rt_inline rt_uint32_t serial_ringbuffer_size(struct serial_ringbuffer* rbuffer)
{
rt_uint32_t size;
rt_base_t level;
level = rt_hw_interrupt_disable();
size = (rbuffer->put_index - rbuffer->get_index) & (SERIAL_RBUFFER_SIZE - 1);
rt_hw_interrupt_enable(level);
return size;
}
/* RT-Thread Device Interface */
/*
* This function initializes serial
*/
static rt_err_t rt_serial_init(struct rt_device *dev)
{
rt_err_t result = RT_EOK;
struct rt_serial_device *serial;
RT_ASSERT(dev != RT_NULL);
serial = (struct rt_serial_device*) dev;
if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
{
/* apply configuration */
if (serial->ops->configure)
result = serial->ops->configure(serial, &serial->config);
if (result != RT_EOK)
return result;
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
serial_ringbuffer_init(serial->int_rx);
if (dev->flag & RT_DEVICE_FLAG_INT_TX)
serial_ringbuffer_init(serial->int_tx);
/* set activated */
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
}
return result;
}
static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
{
struct rt_serial_device *serial;
rt_uint32_t int_flags = 0;
RT_ASSERT(dev != RT_NULL);
serial = (struct rt_serial_device*) dev;
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
int_flags = RT_SERIAL_RX_INT;
if (dev->flag & RT_DEVICE_FLAG_INT_TX)
int_flags |= RT_SERIAL_TX_INT;
if (int_flags)
{
serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void*)int_flags);
}
return RT_EOK;
}
static rt_err_t rt_serial_close(struct rt_device *dev)
{
struct rt_serial_device *serial;
rt_uint32_t int_flags = 0;
RT_ASSERT(dev != RT_NULL);
serial = (struct rt_serial_device*) dev;
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
int_flags = RT_SERIAL_RX_INT;
if (dev->flag & RT_DEVICE_FLAG_INT_TX)
int_flags |= RT_SERIAL_TX_INT;
if (int_flags)
{
serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void*)int_flags);
}
return RT_EOK;
}
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;
rt_uint32_t read_nbytes;
struct rt_serial_device *serial;
RT_ASSERT(dev != RT_NULL);
serial = (struct rt_serial_device*) dev;
ptr = (rt_uint8_t*)buffer;
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
{
/* interrupt mode Rx */
while (size)
{
int ch;
ch = serial_ringbuffer_getc(serial->int_rx);
if (ch == -1) break;
*ptr = ch & 0xff;
ptr ++;
size --;
}
}
else
{
/* polling mode */
while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size)
{
*ptr = serial->ops->getc(serial);
ptr ++;
}
}
read_nbytes = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
/* set error code */
if (read_nbytes == 0)
{
rt_set_errno(-RT_EEMPTY);
}
return read_nbytes;
}
static rt_size_t rt_serial_write(struct rt_device *dev, rt_off_t pos,
const void *buffer, rt_size_t size)
{
rt_uint8_t *ptr;
rt_size_t write_nbytes = 0;
struct rt_serial_device *serial;
RT_ASSERT(dev != RT_NULL);
serial = (struct rt_serial_device*) dev;
ptr = (rt_uint8_t*)buffer;
if (dev->flag & RT_DEVICE_FLAG_INT_TX)
{
/* warning: data will be discarded if buffer is full */
while (size)
{
if (serial_ringbuffer_putchar(serial->int_tx, *ptr) != -1)
{
ptr ++;
size --;
}
else break;
}
}
else
{
/* polling mode */
while (size)
{
/*
* to be polite with serial console add a line feed
* to the carriage return character
*/
if (*ptr == '\n' && (dev->flag & RT_DEVICE_FLAG_STREAM))
{
serial->ops->putc(serial, '\r');
}
serial->ops->putc(serial, *ptr);
++ptr;
--size;
}
}
write_nbytes = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
if (write_nbytes == 0)
{
rt_set_errno(-RT_EFULL);
}
return write_nbytes;
}
static rt_err_t rt_serial_control(struct rt_device *dev, rt_uint8_t cmd, void *args)
{
struct rt_serial_device *serial;
RT_ASSERT(serial != RT_NULL);
serial = (struct rt_serial_device*) dev;
switch (cmd)
{
case RT_DEVICE_CTRL_SUSPEND:
/* suspend device */
dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
break;
case RT_DEVICE_CTRL_RESUME:
/* resume device */
dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
break;
case RT_DEVICE_CTRL_CONFIG:
/* configure device */
serial->ops->configure(serial, (struct serial_configure *)args);
break;
}
return RT_EOK;
}
/*
* serial register
*/
rt_err_t rt_hw_serial_register(struct rt_serial_device *serial, const char *name, rt_uint32_t flag, void *data)
{
struct rt_device *device;
RT_ASSERT(serial != RT_NULL);
device = &(serial->parent);
device->type = RT_Device_Class_Char;
device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL;
device->init = rt_serial_init;
device->open = rt_serial_open;
device->close = rt_serial_close;
device->read = rt_serial_read;
device->write = rt_serial_write;
device->control = rt_serial_control;
device->user_data = data;
/* register a character device */
return rt_device_register(device, name, flag);
}
/* ISR for serial interrupt */
void rt_hw_serial_isr(struct rt_serial_device *serial)
{
int ch = -1;
/* interrupt mode receive */
RT_ASSERT(serial->parent.flag & RT_DEVICE_FLAG_INT_RX);
while (1)
{
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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册