Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
dgr6878
rt-thread
提交
59653699
R
rt-thread
项目概览
dgr6878
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
5
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
59653699
编写于
10月 30, 2015
作者:
B
Bernard Xiong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update uart.c
上级
bb5f57f9
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
251 addition
and
250 deletion
+251
-250
bsp/lpc176x/drivers/uart.c
bsp/lpc176x/drivers/uart.c
+251
-250
未找到文件。
bsp/lpc176x/drivers/uart.c
浏览文件 @
59653699
...
@@ -37,7 +37,7 @@
...
@@ -37,7 +37,7 @@
#define LSR_RXFE 0x80
#define LSR_RXFE 0x80
/**
/**
* @addtogroup LPC1
1x
x
* @addtogroup LPC1
76
x
*/
*/
/*@{*/
/*@{*/
...
@@ -54,12 +54,12 @@ struct rt_uart_lpc
...
@@ -54,12 +54,12 @@ struct rt_uart_lpc
/* buffer for reception */
/* buffer for reception */
rt_uint8_t
read_index
,
save_index
;
rt_uint8_t
read_index
,
save_index
;
rt_uint8_t
rx_buffer
[
RT_UART_RX_BUFFER_SIZE
];
rt_uint8_t
rx_buffer
[
RT_UART_RX_BUFFER_SIZE
];
}
uart_device
;
}
uart_device
;
void
UART0_IRQHandler
(
void
)
void
UART0_IRQHandler
(
void
)
{
{
rt_ubase_t
level
,
iir
;
rt_ubase_t
level
,
iir
;
struct
rt_uart_lpc
*
uart
=
&
uart_device
;
struct
rt_uart_lpc
*
uart
=
&
uart_device
;
/* read IIR and clear it */
/* read IIR and clear it */
iir
=
LPC_UART
->
IIR
;
iir
=
LPC_UART
->
IIR
;
...
@@ -79,7 +79,7 @@ void UART0_IRQHandler(void)
...
@@ -79,7 +79,7 @@ void UART0_IRQHandler(void)
rt_hw_interrupt_enable
(
level
);
rt_hw_interrupt_enable
(
level
);
/* invoke callback */
/* invoke callback */
if
(
uart
->
parent
.
rx_indicate
!=
RT_NULL
)
if
(
uart
->
parent
.
rx_indicate
!=
RT_NULL
)
{
{
rt_size_t
length
;
rt_size_t
length
;
if
(
uart
->
read_index
>
uart
->
save_index
)
if
(
uart
->
read_index
>
uart
->
save_index
)
...
@@ -89,15 +89,16 @@ void UART0_IRQHandler(void)
...
@@ -89,15 +89,16 @@ void UART0_IRQHandler(void)
uart
->
parent
.
rx_indicate
(
&
uart
->
parent
,
length
);
uart
->
parent
.
rx_indicate
(
&
uart
->
parent
,
length
);
}
}
}
else
if
(
iir
==
IIR_RLS
)
}
else
if
(
iir
==
IIR_RLS
)
{
{
iir
=
LPC_Uart
->
LSR
;
//oe pe fe oe read for clear interrupt
iir
=
LPC_UART
->
LSR
;
//oe pe fe oe read for clear interrupt
}
}
return
;
return
;
}
}
static
rt_err_t
rt_uart_init
(
rt_device_t
dev
)
static
rt_err_t
rt_uart_init
(
rt_device_t
dev
)
{
{
rt_uint32_t
Fdiv
;
rt_uint32_t
Fdiv
;
rt_uint32_t
pclkdiv
,
pclk
;
rt_uint32_t
pclkdiv
,
pclk
;
...
@@ -111,31 +112,31 @@ static rt_err_t rt_uart_init (rt_device_t dev)
...
@@ -111,31 +112,31 @@ static rt_err_t rt_uart_init (rt_device_t dev)
all the peripherals is 1/4 of the SystemFrequency. */
all the peripherals is 1/4 of the SystemFrequency. */
/* Bit 6~7 is for UART0 */
/* Bit 6~7 is for UART0 */
pclkdiv
=
(
LPC_SC
->
PCLKSEL0
>>
6
)
&
0x03
;
pclkdiv
=
(
LPC_SC
->
PCLKSEL0
>>
6
)
&
0x03
;
switch
(
pclkdiv
)
switch
(
pclkdiv
)
{
{
case
0x00
:
case
0x00
:
default:
default:
pclk
=
SystemCoreClock
/
4
;
pclk
=
SystemCoreClock
/
4
;
break
;
break
;
case
0x01
:
case
0x01
:
pclk
=
SystemCoreClock
;
pclk
=
SystemCoreClock
;
break
;
break
;
case
0x02
:
case
0x02
:
pclk
=
SystemCoreClock
/
2
;
pclk
=
SystemCoreClock
/
2
;
break
;
break
;
case
0x03
:
case
0x03
:
pclk
=
SystemCoreClock
/
8
;
pclk
=
SystemCoreClock
/
8
;
break
;
break
;
}
}
LPC_UART0
->
LCR
=
0x83
;
/* 8 bits, no Parity, 1 Stop bit */
LPC_UART0
->
LCR
=
0x83
;
/* 8 bits, no Parity, 1 Stop bit */
Fdiv
=
(
pclk
/
16
)
/
UART_BAUDRATE
;
/*baud rate */
Fdiv
=
(
pclk
/
16
)
/
UART_BAUDRATE
;
/*baud rate */
LPC_UART0
->
DLM
=
Fdiv
/
256
;
LPC_UART0
->
DLM
=
Fdiv
/
256
;
LPC_UART0
->
DLL
=
Fdiv
%
256
;
LPC_UART0
->
DLL
=
Fdiv
%
256
;
LPC_UART0
->
LCR
=
0x03
;
/* DLAB = 0 */
LPC_UART0
->
LCR
=
0x03
;
/* DLAB = 0 */
LPC_UART0
->
FCR
=
0x07
;
/* Enable and reset TX and RX FIFO. */
LPC_UART0
->
FCR
=
0x07
;
/* Enable and reset TX and RX FIFO. */
}
}
else
if
((
LPC_UART1_TypeDef
*
)
LPC_UART
==
LPC_UART1
)
else
if
((
LPC_UART1_TypeDef
*
)
LPC_UART
==
LPC_UART1
)
{
{
LPC_PINCON
->
PINSEL4
&=
~
0x0000000F
;
LPC_PINCON
->
PINSEL4
&=
~
0x0000000F
;
LPC_PINCON
->
PINSEL4
|=
0x0000000A
;
/* Enable RxD1 P2.1, TxD1 P2.0 */
LPC_PINCON
->
PINSEL4
|=
0x0000000A
;
/* Enable RxD1 P2.1, TxD1 P2.0 */
...
@@ -144,25 +145,25 @@ static rt_err_t rt_uart_init (rt_device_t dev)
...
@@ -144,25 +145,25 @@ static rt_err_t rt_uart_init (rt_device_t dev)
all the peripherals is 1/4 of the SystemFrequency. */
all the peripherals is 1/4 of the SystemFrequency. */
/* Bit 8,9 are for UART1 */
/* Bit 8,9 are for UART1 */
pclkdiv
=
(
LPC_SC
->
PCLKSEL0
>>
8
)
&
0x03
;
pclkdiv
=
(
LPC_SC
->
PCLKSEL0
>>
8
)
&
0x03
;
switch
(
pclkdiv
)
switch
(
pclkdiv
)
{
{
case
0x00
:
case
0x00
:
default:
default:
pclk
=
SystemCoreClock
/
4
;
pclk
=
SystemCoreClock
/
4
;
break
;
break
;
case
0x01
:
case
0x01
:
pclk
=
SystemCoreClock
;
pclk
=
SystemCoreClock
;
break
;
break
;
case
0x02
:
case
0x02
:
pclk
=
SystemCoreClock
/
2
;
pclk
=
SystemCoreClock
/
2
;
break
;
break
;
case
0x03
:
case
0x03
:
pclk
=
SystemCoreClock
/
8
;
pclk
=
SystemCoreClock
/
8
;
break
;
break
;
}
}
LPC_UART1
->
LCR
=
0x83
;
/* 8 bits, no Parity, 1 Stop bit */
LPC_UART1
->
LCR
=
0x83
;
/* 8 bits, no Parity, 1 Stop bit */
Fdiv
=
(
pclk
/
16
)
/
UART_BAUDRATE
;
/*baud rate */
Fdiv
=
(
pclk
/
16
)
/
UART_BAUDRATE
;
/*baud rate */
LPC_UART1
->
DLM
=
Fdiv
/
256
;
LPC_UART1
->
DLM
=
Fdiv
/
256
;
LPC_UART1
->
DLL
=
Fdiv
%
256
;
LPC_UART1
->
DLL
=
Fdiv
%
256
;
LPC_UART1
->
LCR
=
0x03
;
/* DLAB = 0 */
LPC_UART1
->
LCR
=
0x03
;
/* DLAB = 0 */
...
@@ -170,8 +171,8 @@ static rt_err_t rt_uart_init (rt_device_t dev)
...
@@ -170,8 +171,8 @@ static rt_err_t rt_uart_init (rt_device_t dev)
}
}
/* Ensure a clean start, no data in either TX or RX FIFO. */
/* Ensure a clean start, no data in either TX or RX FIFO. */
while
((
LPC_UART
->
LSR
&
(
LSR_THRE
|
LSR_TEMT
))
!=
(
LSR_THRE
|
LSR_TEMT
)
);
while
((
LPC_UART
->
LSR
&
(
LSR_THRE
|
LSR_TEMT
))
!=
(
LSR_THRE
|
LSR_TEMT
)
);
while
(
LPC_UART
->
LSR
&
LSR_RDR
)
while
(
LPC_UART
->
LSR
&
LSR_RDR
)
{
{
Fdiv
=
LPC_UART
->
RBR
;
/* Dump data from RX FIFO */
Fdiv
=
LPC_UART
->
RBR
;
/* Dump data from RX FIFO */
}
}
...
@@ -204,14 +205,14 @@ static rt_err_t rt_uart_close(rt_device_t dev)
...
@@ -204,14 +205,14 @@ static rt_err_t rt_uart_close(rt_device_t dev)
return
RT_EOK
;
return
RT_EOK
;
}
}
static
rt_size_t
rt_uart_read
(
rt_device_t
dev
,
rt_off_t
pos
,
void
*
buffer
,
rt_size_t
size
)
static
rt_size_t
rt_uart_read
(
rt_device_t
dev
,
rt_off_t
pos
,
void
*
buffer
,
rt_size_t
size
)
{
{
rt_uint8_t
*
ptr
;
rt_uint8_t
*
ptr
;
struct
rt_uart_lpc
*
uart
=
(
struct
rt_uart_lpc
*
)
dev
;
struct
rt_uart_lpc
*
uart
=
(
struct
rt_uart_lpc
*
)
dev
;
RT_ASSERT
(
uart
!=
RT_NULL
);
RT_ASSERT
(
uart
!=
RT_NULL
);
/* point to buffer */
/* point to buffer */
ptr
=
(
rt_uint8_t
*
)
buffer
;
ptr
=
(
rt_uint8_t
*
)
buffer
;
if
(
dev
->
flag
&
RT_DEVICE_FLAG_INT_RX
)
if
(
dev
->
flag
&
RT_DEVICE_FLAG_INT_RX
)
{
{
while
(
size
)
while
(
size
)
...
@@ -251,10 +252,10 @@ static rt_size_t rt_uart_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_si
...
@@ -251,10 +252,10 @@ static rt_size_t rt_uart_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_si
return
0
;
return
0
;
}
}
static
rt_size_t
rt_uart_write
(
rt_device_t
dev
,
rt_off_t
pos
,
const
void
*
buffer
,
rt_size_t
size
)
static
rt_size_t
rt_uart_write
(
rt_device_t
dev
,
rt_off_t
pos
,
const
void
*
buffer
,
rt_size_t
size
)
{
{
char
*
ptr
;
char
*
ptr
;
ptr
=
(
char
*
)
buffer
;
ptr
=
(
char
*
)
buffer
;
if
(
dev
->
flag
&
RT_DEVICE_FLAG_STREAM
)
if
(
dev
->
flag
&
RT_DEVICE_FLAG_STREAM
)
{
{
...
@@ -264,13 +265,13 @@ static rt_size_t rt_uart_write(rt_device_t dev, rt_off_t pos, const void* buffer
...
@@ -264,13 +265,13 @@ static rt_size_t rt_uart_write(rt_device_t dev, rt_off_t pos, const void* buffer
if
(
*
ptr
==
'\n'
)
if
(
*
ptr
==
'\n'
)
{
{
/* THRE status, contain valid data */
/* THRE status, contain valid data */
while
(
!
(
LPC_UART
->
LSR
&
LSR_THRE
)
);
while
(
!
(
LPC_UART
->
LSR
&
LSR_THRE
)
);
/* write data */
/* write data */
LPC_UART
->
THR
=
'\r'
;
LPC_UART
->
THR
=
'\r'
;
}
}
/* THRE status, contain valid data */
/* THRE status, contain valid data */
while
(
!
(
LPC_UART
->
LSR
&
LSR_THRE
)
);
while
(
!
(
LPC_UART
->
LSR
&
LSR_THRE
));
/* write data */
/* write data */
LPC_UART
->
THR
=
*
ptr
;
LPC_UART
->
THR
=
*
ptr
;
...
@@ -280,10 +281,10 @@ static rt_size_t rt_uart_write(rt_device_t dev, rt_off_t pos, const void* buffer
...
@@ -280,10 +281,10 @@ static rt_size_t rt_uart_write(rt_device_t dev, rt_off_t pos, const void* buffer
}
}
else
else
{
{
while
(
size
!=
0
)
while
(
size
!=
0
)
{
{
/* THRE status, contain valid data */
/* THRE status, contain valid data */
while
(
!
(
LPC_UART
->
LSR
&
LSR_THRE
)
);
while
(
!
(
LPC_UART
->
LSR
&
LSR_THRE
)
);
/* write data */
/* write data */
LPC_UART
->
THR
=
*
ptr
;
LPC_UART
->
THR
=
*
ptr
;
...
@@ -298,7 +299,7 @@ static rt_size_t rt_uart_write(rt_device_t dev, rt_off_t pos, const void* buffer
...
@@ -298,7 +299,7 @@ static rt_size_t rt_uart_write(rt_device_t dev, rt_off_t pos, const void* buffer
void
rt_hw_uart_init
(
void
)
void
rt_hw_uart_init
(
void
)
{
{
struct
rt_uart_lpc
*
uart
;
struct
rt_uart_lpc
*
uart
;
/* get uart device */
/* get uart device */
uart
=
&
uart_device
;
uart
=
&
uart_device
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录