共2条
1/1 1 跳转至页
lpc2132 俺写的lpc2132的串口驱动(中断方式)
问
#include "lpc213x.h"
#include "Includes.h"
#define BIT(x) (1<<x)
#define SET_BIT(x, y) (x|=(1<<y))
#define CLR_BIT(x, y) (x&=~(1<<y))
#define GET_BIT(x, y) (x&(1<<y))
volatile struct
{
INT8U rd_buf[32];
INT8U td_buf[2048];
INT16U rd_max;
INT16U td_max;
INT16U rd_cnt;
INT16U td_cnt;
} uart0;
OS_EVENT *uart0_rd_ok;
OS_EVENT *uart0_td_ok;
extern void UART0_Handler(void);
void UART0_Exception(void)
{
unsigned char i, k;
k = U0IIR&0x0F;
switch(k)
{
case 0x02:
for (i=0; i<16; i++)
{
if (uart0.td_cnt < uart0.td_max)
{
U0THR = uart0.td_buf[uart0.td_cnt];
uart0.td_cnt++;
}
else
{
U0IER = U0IER & (~0x02);
OSSemPost(uart0_td_ok);
goto UART0_Exception_RET;
}
}
break;
case 0x04:
for (i=0; i<8; i++)
{
uart0.rd_buf[uart0.rd_cnt] = U0RBR;
if (uart0.rd_buf[0] == 0xFD)
{
uart0.rd_cnt++;
if (uart0.rd_cnt == uart0.rd_max)
{
OSSemPost(uart0_rd_ok);
goto UART0_Exception_RET;
}
else if (uart0.rd_cnt > 31)
{
uart0.rd_cnt = 31;
}
}
}
break;
case 0x0C:
uart0.rd_buf[uart0.rd_cnt] = U0RBR;
if (uart0.rd_buf[0] == 0xFD)
{
uart0.rd_cnt++;
if (uart0.rd_cnt == uart0.rd_max)
{
OSSemPost(uart0_rd_ok);
goto UART0_Exception_RET;
}
else if (uart0.rd_cnt > 31)
{
uart0.rd_cnt = 31;
}
}
break;
default:
break;
}
UART0_Exception_RET:
VICVectAddr = 0x00;
}
void UART0_init(void)
{
INT16U i;
for (i=0; i<32; i++)
{
uart0.rd_buf[i] = 0x00;
}
for (i=0; i<2048; i++)
{
uart0.td_buf[i] = (unsigned char)i;
}
uart0.rd_max = 8;
uart0.td_max = 0;
uart0.rd_cnt = 0;
uart0.td_cnt = 0;
SET_BIT(PINSEL0, 0);
CLR_BIT(PINSEL0, 1);
SET_BIT(PINSEL0, 2);
CLR_BIT(PINSEL0, 3);
U0LCR = 0x83;
U0DLL = 0x06;
U0DLM = 0x00;
U0LCR = 0x03;
U0FCR = 0x87;
U0IER = 0x01;
VICIntSelect = 0x00000000;
VICVectCntl1 = 0x26;
VICVectAddr1 = (unsigned long)UART0_Handler;
VICIntEnable = (1<<6);
}
void UART0_putc(unsigned char c)
{
U0THR = c;
while ((U0LSR&0x40) == 0)
{
}
}
void UART0_puts(char *p)
{
while(*p)
{
UART0_putc(*p);
p++;
}
}
void UART0_send(INT16U len)
{
//unsigned short i;
//for (i=0; i<len; i++)
//{
// UART0_putc(uart0.td_buf[i]);
//}
uart0.td_cnt = 0;
uart0.td_max = len;
if (uart0.td_max > 0)
{
U0THR = uart0.td_buf[uart0.td_cnt];
uart0.td_cnt++;
U0IER = U0IER | 0x02; /* 允许发送中断 */
}
}
void UART0_task(void *pdata)
{
INT8U err;
pdata = pdata;
uart0_rd_ok = OSSemCreate(0);
uart0_td_ok = OSSemCreate(0);
UART0_init();
do
{
OSSemPend(uart0_rd_ok, 0, &err);
UART0_send(1024);
OSSemPend(uart0_td_ok, 0, &err);
uart0.rd_cnt = 0;
} while(1);
}
答 1: 谢谢啦中断看了几天都不明白,这下好了,虽然我的是2104 答 2: 在多任务中,串口驱动,当一个设备想使用串口时,必须保证当时串口是空闲,如果不空闲,必须等待,直到空间,或者指定时间内串口设备未空闲,提示信息,
楼上的串口驱动采用结构,比较不错
但是要是用在多任何中,如果用多个任何使用此串口,估计会有问题 答 3: 采用信号量太占用资源,不如用 Uart0Status=OSFlagCreate表示状态,另外,其实完全可以将串口的忙闲状态等信息一块封装在结构中,这样不断模块化,同时,一个状态只占用一个信息, 答 4: 呵呵,改了一下
#include "lpc213x.h"
#include "Includes.h"
#define BIT(x) (1<<x)
#define SET_BIT(x, y) (x|=(1<<y))
#define CLR_BIT(x, y) (x&=~(1<<y))
#define GET_BIT(x, y) (x&(1<<y))
volatile struct
{
INT8U rd_buf[32];
INT8U td_buf[2048];
INT16U rd_max;
INT16U td_max;
INT16U rd_cnt;
INT16U td_cnt;
OS_EVENT *rd_ok;
OS_EVENT *td_ok;
} uart0;
extern void UART0_Handler(void);
void UART0_Exception(void)
{
unsigned char i, k;
k = U0IIR&0x0F;
switch(k)
{
case 0x02:
for (i=0; i<16; i++)
{
if (uart0.td_cnt < uart0.td_max)
{
U0THR = uart0.td_buf[uart0.td_cnt];
uart0.td_cnt++;
}
else
{
U0IER = U0IER & (~0x02);
OSSemPost(uart0.td_ok);
goto UART0_Exception_RET;
}
}
break;
case 0x04:
for (i=0; i<8; i++)
{
uart0.rd_buf[uart0.rd_cnt] = U0RBR;
if (uart0.rd_buf[0] == 0xFD)
{
uart0.rd_cnt++;
if (uart0.rd_cnt == uart0.rd_max)
{
OSSemPost(uart0.rd_ok);
goto UART0_Exception_RET;
}
else if (uart0.rd_cnt > 31)
{
uart0.rd_cnt = 31;
}
}
}
break;
case 0x0C:
uart0.rd_buf[uart0.rd_cnt] = U0RBR;
if (uart0.rd_buf[0] == 0xFD)
{
uart0.rd_cnt++;
if (uart0.rd_cnt == uart0.rd_max)
{
OSSemPost(uart0.rd_ok);
goto UART0_Exception_RET;
}
else if (uart0.rd_cnt > 31)
{
uart0.rd_cnt = 31;
}
}
break;
default:
break;
}
UART0_Exception_RET:
VICVectAddr = 0x00;
}
void UART0_init(void)
{
INT16U i;
for (i=0; i<32; i++)
{
uart0.rd_buf[i] = 0x00;
}
for (i=0; i<2048; i++)
{
uart0.td_buf[i] = (unsigned char)i;
}
uart0.rd_max = 8;
uart0.td_max = 0;
uart0.rd_cnt = 0;
uart0.td_cnt = 0;
uart0.rd_ok = OSSemCreate(0);
uart0.td_ok = OSSemCreate(0);
SET_BIT(PINSEL0, 0);
CLR_BIT(PINSEL0, 1);
SET_BIT(PINSEL0, 2);
CLR_BIT(PINSEL0, 3);
U0LCR = 0x83;
U0DLL = 0x06;
U0DLM = 0x00;
U0LCR = 0x03;
U0FCR = 0x87;
U0IER = 0x01;
VICIntSelect = 0x00000000;
VICVectCntl1 = 0x26;
VICVectAddr1 = (unsigned long)UART0_Handler;
VICIntEnable = (1<<6);
}
void UART0_putc(unsigned char c)
{
U0THR = c;
while ((U0LSR&0x40) == 0)
{
}
}
void UART0_puts(char *p)
{
while(*p)
{
UART0_putc(*p);
p++;
}
}
void UART0_send(INT16U len)
{
uart0.td_cnt = 0;
uart0.td_max = len;
if (uart0.td_max > 0)
{
U0THR = uart0.td_buf[uart0.td_cnt];
uart0.td_cnt++;
U0IER = U0IER | 0x02;
}
}
void UART0_task(void *pdata)
{
INT8U err;
pdata = pdata;
UART0_init();
do
{
OSSemPend(uart0.rd_ok, 0, &err);
UART0_send(1024);
OSSemPend(uart0.td_ok, 0, &err);
uart0.rd_cnt = 0;
} while(1);
}
答 5: 好,一直想用双串口的驱动
#include "lpc213x.h"
#include "Includes.h"
#define BIT(x) (1<<x)
#define SET_BIT(x, y) (x|=(1<<y))
#define CLR_BIT(x, y) (x&=~(1<<y))
#define GET_BIT(x, y) (x&(1<<y))
volatile struct
{
INT8U rd_buf[32];
INT8U td_buf[2048];
INT16U rd_max;
INT16U td_max;
INT16U rd_cnt;
INT16U td_cnt;
} uart0;
OS_EVENT *uart0_rd_ok;
OS_EVENT *uart0_td_ok;
extern void UART0_Handler(void);
void UART0_Exception(void)
{
unsigned char i, k;
k = U0IIR&0x0F;
switch(k)
{
case 0x02:
for (i=0; i<16; i++)
{
if (uart0.td_cnt < uart0.td_max)
{
U0THR = uart0.td_buf[uart0.td_cnt];
uart0.td_cnt++;
}
else
{
U0IER = U0IER & (~0x02);
OSSemPost(uart0_td_ok);
goto UART0_Exception_RET;
}
}
break;
case 0x04:
for (i=0; i<8; i++)
{
uart0.rd_buf[uart0.rd_cnt] = U0RBR;
if (uart0.rd_buf[0] == 0xFD)
{
uart0.rd_cnt++;
if (uart0.rd_cnt == uart0.rd_max)
{
OSSemPost(uart0_rd_ok);
goto UART0_Exception_RET;
}
else if (uart0.rd_cnt > 31)
{
uart0.rd_cnt = 31;
}
}
}
break;
case 0x0C:
uart0.rd_buf[uart0.rd_cnt] = U0RBR;
if (uart0.rd_buf[0] == 0xFD)
{
uart0.rd_cnt++;
if (uart0.rd_cnt == uart0.rd_max)
{
OSSemPost(uart0_rd_ok);
goto UART0_Exception_RET;
}
else if (uart0.rd_cnt > 31)
{
uart0.rd_cnt = 31;
}
}
break;
default:
break;
}
UART0_Exception_RET:
VICVectAddr = 0x00;
}
void UART0_init(void)
{
INT16U i;
for (i=0; i<32; i++)
{
uart0.rd_buf[i] = 0x00;
}
for (i=0; i<2048; i++)
{
uart0.td_buf[i] = (unsigned char)i;
}
uart0.rd_max = 8;
uart0.td_max = 0;
uart0.rd_cnt = 0;
uart0.td_cnt = 0;
SET_BIT(PINSEL0, 0);
CLR_BIT(PINSEL0, 1);
SET_BIT(PINSEL0, 2);
CLR_BIT(PINSEL0, 3);
U0LCR = 0x83;
U0DLL = 0x06;
U0DLM = 0x00;
U0LCR = 0x03;
U0FCR = 0x87;
U0IER = 0x01;
VICIntSelect = 0x00000000;
VICVectCntl1 = 0x26;
VICVectAddr1 = (unsigned long)UART0_Handler;
VICIntEnable = (1<<6);
}
void UART0_putc(unsigned char c)
{
U0THR = c;
while ((U0LSR&0x40) == 0)
{
}
}
void UART0_puts(char *p)
{
while(*p)
{
UART0_putc(*p);
p++;
}
}
void UART0_send(INT16U len)
{
//unsigned short i;
//for (i=0; i<len; i++)
//{
// UART0_putc(uart0.td_buf[i]);
//}
uart0.td_cnt = 0;
uart0.td_max = len;
if (uart0.td_max > 0)
{
U0THR = uart0.td_buf[uart0.td_cnt];
uart0.td_cnt++;
U0IER = U0IER | 0x02; /* 允许发送中断 */
}
}
void UART0_task(void *pdata)
{
INT8U err;
pdata = pdata;
uart0_rd_ok = OSSemCreate(0);
uart0_td_ok = OSSemCreate(0);
UART0_init();
do
{
OSSemPend(uart0_rd_ok, 0, &err);
UART0_send(1024);
OSSemPend(uart0_td_ok, 0, &err);
uart0.rd_cnt = 0;
} while(1);
}
答 1: 谢谢啦中断看了几天都不明白,这下好了,虽然我的是2104 答 2: 在多任务中,串口驱动,当一个设备想使用串口时,必须保证当时串口是空闲,如果不空闲,必须等待,直到空间,或者指定时间内串口设备未空闲,提示信息,
楼上的串口驱动采用结构,比较不错
但是要是用在多任何中,如果用多个任何使用此串口,估计会有问题 答 3: 采用信号量太占用资源,不如用 Uart0Status=OSFlagCreate表示状态,另外,其实完全可以将串口的忙闲状态等信息一块封装在结构中,这样不断模块化,同时,一个状态只占用一个信息, 答 4: 呵呵,改了一下
#include "lpc213x.h"
#include "Includes.h"
#define BIT(x) (1<<x)
#define SET_BIT(x, y) (x|=(1<<y))
#define CLR_BIT(x, y) (x&=~(1<<y))
#define GET_BIT(x, y) (x&(1<<y))
volatile struct
{
INT8U rd_buf[32];
INT8U td_buf[2048];
INT16U rd_max;
INT16U td_max;
INT16U rd_cnt;
INT16U td_cnt;
OS_EVENT *rd_ok;
OS_EVENT *td_ok;
} uart0;
extern void UART0_Handler(void);
void UART0_Exception(void)
{
unsigned char i, k;
k = U0IIR&0x0F;
switch(k)
{
case 0x02:
for (i=0; i<16; i++)
{
if (uart0.td_cnt < uart0.td_max)
{
U0THR = uart0.td_buf[uart0.td_cnt];
uart0.td_cnt++;
}
else
{
U0IER = U0IER & (~0x02);
OSSemPost(uart0.td_ok);
goto UART0_Exception_RET;
}
}
break;
case 0x04:
for (i=0; i<8; i++)
{
uart0.rd_buf[uart0.rd_cnt] = U0RBR;
if (uart0.rd_buf[0] == 0xFD)
{
uart0.rd_cnt++;
if (uart0.rd_cnt == uart0.rd_max)
{
OSSemPost(uart0.rd_ok);
goto UART0_Exception_RET;
}
else if (uart0.rd_cnt > 31)
{
uart0.rd_cnt = 31;
}
}
}
break;
case 0x0C:
uart0.rd_buf[uart0.rd_cnt] = U0RBR;
if (uart0.rd_buf[0] == 0xFD)
{
uart0.rd_cnt++;
if (uart0.rd_cnt == uart0.rd_max)
{
OSSemPost(uart0.rd_ok);
goto UART0_Exception_RET;
}
else if (uart0.rd_cnt > 31)
{
uart0.rd_cnt = 31;
}
}
break;
default:
break;
}
UART0_Exception_RET:
VICVectAddr = 0x00;
}
void UART0_init(void)
{
INT16U i;
for (i=0; i<32; i++)
{
uart0.rd_buf[i] = 0x00;
}
for (i=0; i<2048; i++)
{
uart0.td_buf[i] = (unsigned char)i;
}
uart0.rd_max = 8;
uart0.td_max = 0;
uart0.rd_cnt = 0;
uart0.td_cnt = 0;
uart0.rd_ok = OSSemCreate(0);
uart0.td_ok = OSSemCreate(0);
SET_BIT(PINSEL0, 0);
CLR_BIT(PINSEL0, 1);
SET_BIT(PINSEL0, 2);
CLR_BIT(PINSEL0, 3);
U0LCR = 0x83;
U0DLL = 0x06;
U0DLM = 0x00;
U0LCR = 0x03;
U0FCR = 0x87;
U0IER = 0x01;
VICIntSelect = 0x00000000;
VICVectCntl1 = 0x26;
VICVectAddr1 = (unsigned long)UART0_Handler;
VICIntEnable = (1<<6);
}
void UART0_putc(unsigned char c)
{
U0THR = c;
while ((U0LSR&0x40) == 0)
{
}
}
void UART0_puts(char *p)
{
while(*p)
{
UART0_putc(*p);
p++;
}
}
void UART0_send(INT16U len)
{
uart0.td_cnt = 0;
uart0.td_max = len;
if (uart0.td_max > 0)
{
U0THR = uart0.td_buf[uart0.td_cnt];
uart0.td_cnt++;
U0IER = U0IER | 0x02;
}
}
void UART0_task(void *pdata)
{
INT8U err;
pdata = pdata;
UART0_init();
do
{
OSSemPend(uart0.rd_ok, 0, &err);
UART0_send(1024);
OSSemPend(uart0.td_ok, 0, &err);
uart0.rd_cnt = 0;
} while(1);
}
答 5: 好,一直想用双串口的驱动
共2条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
与电子爱好者谈读图二被打赏50分 | |
【FRDM-MCXN947评测】Core1适配运行FreeRtos被打赏50分 | |
【FRDM-MCXN947评测】双核调试被打赏50分 | |
【CPKCORRA8D1B评测】---移植CoreMark被打赏50分 | |
【CPKCORRA8D1B评测】---打开硬件定时器被打赏50分 | |
【FRDM-MCXA156评测】4、CAN loopback模式测试被打赏50分 | |
【CPKcorRA8D1评测】--搭建初始环境被打赏50分 | |
【FRDM-MCXA156评测】3、使用FlexIO模拟UART被打赏50分 | |
【FRDM-MCXA156评测】2、rt-thread MCXA156 BSP制作被打赏50分 | |
【FRDM-MCXN947评测】核间通信MUTEX被打赏50分 |