Chinaunix首页 | 论坛 | 博客
  • 博客访问: 424953
  • 博文数量: 103
  • 博客积分: 1455
  • 博客等级: 上尉
  • 技术积分: 1380
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-15 22:17
文章分类

全部博文(103)

文章存档

2013年(4)

2012年(99)

我的朋友

分类: C/C++

2012-10-11 19:36:01

今天笔试题里面有一个关于UART驱动的伪代码实现,实在是没有仔细看过,直接拿来就用了
回来打开VDSP里面UART实现的源码分析了一下,学习学习

点击(此处)折叠或打开

  1. /*******************************************************************
  2. * Function: PutChar
  3. * Description: Writes a character to the UART.
  4. *******************************************************************/

  5. int PutChar(const char cVal)
  6. {
  7.     int nStatus = 0;
  8.     unsigned int count = 0;
  9.     
  10.     do
  11.     {
  12.         if( (*pUART0LSR & UARTTHRE) )
  13.         {
  14.             *pUART0THR = cVal;
  15.             nStatus = 1;
  16.             break;
  17.         }
  18.         
  19.         count++;
  20.         
  21.     } while( count < 0x100000 );

  22.     return nStatus;
  23. }
*pUART0LSR & UARTTHRE表示uart0通道空闲可用 UART0_THR Empty
返回1
如果尝试0x100000次无法获得UART,就放弃返回0

点击(此处)折叠或打开

  1. /*******************************************************************
  2. * Function: GetChar
  3. * Description: Reads a character from the UART.
  4. *******************************************************************/

  5. int GetChar(char *const cVal)
  6. {
  7.     int nStatus = 0;
  8.     unsigned int count = 0x0;
  9.     
  10.     do{
  11.         if( 1 /*UARTDR == (*pUART0LSR & UARTDR)*/ )
  12.         {
  13.             *cVal = (char)*pUART0RBR;
  14.             nStatus = 1;
  15.             break;
  16.         }
  17.         
  18.         /*count++;*/
  19.         
  20.     }while( count < 0x100000 );

  21.     return nStatus;
  22. }

同样,getchar从寄存器*pUART0RBR中读取数据,这些都是在没有中断没有DMA的情况下使用UART的函数
如果要判断读取时是UARTDR位表示Data Ready
上层的调用程序会判断读取的是否为0,不为0则继续进行

点击(此处)折叠或打开

  1. while(UARTflag)
  2.             {
  3.                 if( 1 == GetChar( &Testputchar ) )
  4.                 {
  5.                     if( 0 != Testputchar&&Testputchar==TST_CHAR[i])
  6.                     {        
  7.                             UARTflag=0;
  8. //                            DEBUG_STATEMENT( "\nUartGerChar" );
  9. //                            DEBUG_PRINT("\nTestputchar is %c ",Testputchar);
  10.                             OK_number++;
  11.                     }
  12.                     else if( 'z' == Testputchar )
  13.                             break;
  14.                 }
  15.             
  16.             }

上层会忽视取回来是0的字符,这样就可以等到有用数据并且保存在Testputchar里
 

 
阅读(1609) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~