找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3072|回复: 1
打印 上一主题 下一主题
收起左侧

M0的串口问题

[复制链接]
跳转到指定楼层
楼主
ID:82781 发表于 2015-6-13 16:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
前天拿到的芯片,端口驱动没有问题,偏偏是串口出了问题,刚开始是没有开始端口复用,而昨天一整天我都在研究为什么可以接受到数据,但是数据是错误的,一整天,就是找不到程序中哪里出了问题,昨晚上睡觉之前就在思考,终究不能知道为什么,因为他的寄存器什么的我都没时间仔细研究,心想着今天来一个一个的研究,今早却灵光一现,今天起得特别早,因为还惦记这个问题,睡不着,为什么不看看硬件呢?我就来到加了一块串口驱动板,哈哈令人兴奋。电脑瞑目上闪现的数字和我期待的是一样的,嘎嘎,证明软件的配置昨天一天都是对的,只是没想到硬件出问题!哎!加油吧,总算是搞定串口发送了。虽然微不足道,但是也足以令我兴奋十天半月!


王均伟
日照高科园,FUCK!
注意:CPU时钟和HCLK时钟是单独的,相互独立,所以得要单独开启,他不同于STM32,他只有一个外设桥,没有DMA通道,相对
的外设资源也是相对匮乏的,但是相对于8位单片机他已经非常强大了!现在感觉和STM32的操作越来越像!
加油吧!


下面是APP和部分程序
  1. E_DRVGPIO_PIN sPinNumber; //枚举引脚
  2. STR_UART_T config;
  3. uint32_t start[10];
  4. /*---------------------------------------------------------------------------------------------------------*/
  5. /* MAIN function                                                                                          */
  6. /*---------------------------------------------------------------------------------------------------------*/   
  7. int main (void)
  8. {
  9. uint8_t tab[12]={6,5,6,7,8,9};
  10. uint8_t tab1[10];


  11. while(DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1)!=0);//使能外部12MHZ晶振
  12.     while( DrvSYS_SelectHCLKSource(0) !=0);//HCLK选择外部12MHZ晶振
  13. while(DrvSYS_SelectSysTickSource(0)!=0);//选择系统嘀嗒时钟源为外部12MHZ晶振
  14.     DrvSYS_SetIPClock(E_SYS_UART0_CLK,1);//使能UART0时钟

  15.      while(DrvSYS_SetClockDivider(E_SYS_HCLK_DIV, 0)!=0); //1分频UART&HCLK
  16.      while(DrvSYS_SetClockDivider(E_SYS_UART_DIV, 0)!=0); //1分频UART&HCLK
  17.    DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC, 0);//UART选择外部12Mhz的时钟源
  18.    DrvGPIO_InitFunction(E_FUNC_UART0);//复用IO编程UART模式
  19.      /*关闭所有串口中断*/
  20.    DrvUART_DisableInt(UART_PORT0,DRVUART_WAKEUPINT|DRVUART_RLSINT|DRVUART_BUFERRINT|DRVUART_MOSINT|DRVUART_THREINT|DRVUART_RDAINT|DRVUART_TOUTINT);

  21.     /*设置串口参数*/
  22.   config.u32BaudRate=9600;  //波特率9600
  23.   config.u8cDataBits=DRVUART_DATABITS_8 ;//数据位8位
  24.   config.u8cStopBits=DRVUART_STOPBITS_1; //停止位1位
  25.   config.u8cParity=DRVUART_PARITY_NONE ; //NONE校验
  26.   config.u8cRxTriggerLevel=DRVUART_FIFO_8BYTES;//接受FIFO 8字节
  27.         DrvUART_Open(UART_PORT0, &config);//开启UART端口
  28.        /*端口模式*/
  29.     DrvGPIO_Open(E_PORT3,E_PIN6, E_IO_OUTPUT);
  30. DrvGPIO_Open(E_PORT0,E_PIN0, E_IO_INPUT);//input


  31. sPinNumber=E_PIN6;

  32. while(1)
  33. {
  34. // _DRVUART_SENDBYTE(UART_PORT0,2);  

  35.    if(DrvGPIO_GetBit(E_PORT0,E_PIN0 )==0)
  36.    {
  37. DrvUART_Write(UART_PORT0,tab,3);
  38.    
  39.     DrvGPIO_ClrBit(E_PORT3,sPinNumber);
  40.   DrvSYS_Delay(500000);
  41. DrvGPIO_SetBit(E_PORT3, E_PIN6);
  42.         DrvSYS_Delay(500000);
  43.       
  44.       
  45.       }
  46. }

  47. }

  48. static void BaudRateCalculator(uint32_t clk, uint32_t baudRate, E_UART_PORT u32Port)
  49. {
  50.    int32_t tmp;
  51. int32_t div;
  52.    UART_T * tUART;
  53. tUART = (UART_T *)((uint32_t)UART0 + u32Port);
  54.     if ((((clk / baudRate)%16) < 2)  &&  (clk>(baudRate*16*2)))  /* Source Clock mod 16 < 2 => Using Divider X =16 (MODE#0) */
  55. {         
  56.   tUART->BAUD.DIV_X_EN = 0;
  57.      tUART->BAUD.DIV_X_ONE   = 0;
  58.   tmp = clk / baudRate/16  -2;
  59. }
  60. else         /* Source Clock mod 16 >2 => Up 5% Error BaudRate */
  61. {   
  62.      tUART->BAUD.DIV_X_EN = 1;     /* Try to Set Divider X = 1 (MODE#2)*/
  63.      tUART->BAUD.DIV_X_ONE   = 1;
  64.   tmp = clk / baudRate  -2;
  65.   if(tmp > 0xFFFF)     /* If Divider > Range  */
  66.   {
  67.    tUART->BAUD.DIV_X_EN = 1;    /* Try to Set Divider X up 10 (MODE#1) */
  68.    tUART->BAUD.DIV_X_ONE   = 0;
  69.    for(div = 8; div <16;div++)
  70.    {
  71.     if(((clk / baudRate)%(div+1))<3)
  72.     {
  73.      tUART->BAUD.DIVIDER_X   = div;
  74.      tmp = clk / baudRate / (div+1) -2;
  75.      break;
  76.     }
  77.    }
  78.   }
  79. }
  80. tUART->BAUD.BRD = tmp;
  81. }

  82. /*---------------------------------------------------------------------------------------------------------*/
  83. /* Function:     GetUartClk                                                                            */
  84. /*                                                                                                         */
  85. /* Parameter:                                */
  86. /*              clk            -[in] Uart Source Clock                                                */
  87. /*              baudRate       -[in] User seting BaudRate                                             */
  88. /*              UART_BAUD_T *baud  -[in] Get User Settings                                                */
  89. /* Returns:                                                                                                */
  90. /*               Current Uart Clock  (Hz)                                                                  */
  91. /* Description:                                                                                            */
  92. /*               The function is used to get Uart clock                                                    */
  93. /*---------------------------------------------------------------------------------------------------------*/
  94. static uint32_t GetUartClock(void)
  95. {
  96.     uint32_t clk =0 , div;
  97.     div = SYSCLK->CLKDIV.UART_N;                    /* According PLL Clock and UART_Divider to get clock */                                                
  98.                                                    
  99.                                                     /* Check UART Clock Source Setting */
  100.     if(SYSCLK->CLKSEL1.UART_S == 0)        
  101.     {
  102.         clk = DrvSYS_GetExtClockFreq()/ (div+1);;   /* Get External Clock From DrvSYS Setting */
  103.     }
  104.     else if(SYSCLK->CLKSEL1.UART_S == 1)
  105.     {
  106.         clk = DrvSYS_GetPLLClockFreq()/ (div+1);
  107.     }
  108.     else
  109.         clk = __IRC22M/ (div+1);                    /* Clock 22Mhz  */
  110.     return clk;
  111. }

  112. /*---------------------------------------------------------------------------------------------------------*/
  113. /* Function:     DrvUART_SetRTS                                                                   */
  114. /*                                                                                                         */
  115. /* Parameter:                                */
  116. /*     u32Port   -[in]   UART Channel:  UART_PORT0 / UART_PORT1                             */
  117. /*               uint8_t     -[in]   RTS Value                                 */
  118. /*                           Set 0: Drive RTS pin to logic 1 (If the LEV_RTS set to low level triggered).  */
  119. /*                                  Drive RTS pin to logic 0 (If the LEV_RTS set to high level triggered)  */
  120. /*                           Set 1: Drive RTS pin to logic 0 (If the LEV_RTS set to low level triggered)   */
  121. /*                                   Drive RTS pin to logic 1 (If the LEV_RTS set to high level triggered) */
  122. /*                           Note. Lev_RTS is RTS Trigger Level. 0 is low level and 1 is high level        */
  123. /*                                                                                                         */
  124. /*               u16TriggerLevel - [in]   RTS Trigger Level :DRVUART_FIFO_1BYTES to DRVUART_FIFO_62BYTES   */
  125. /* Returns:                                                                                                */
  126. /*               None                                                                                      */
  127. /*                                                                                                         */
  128. /* Description:                                                                                            */
  129. /*               The function is used to set RTS information                                            */
  130. /*---------------------------------------------------------------------------------------------------------*/
  131. void DrvUART_SetRTS(E_UART_PORT u32Port,uint8_t u8Value,uint16_t u16TriggerLevel)
  132. {

  133. UART_T * tUART;
  134. tUART = (UART_T *)((uint32_t)UART0 + u32Port);
  135. tUART->MCR.RTS = u8Value;
  136. tUART->FCR.RTS_TRI_LEV = u16TriggerLevel;
  137. }


  138. /*---------------------------------------------------------------------------------------------------------*/
  139. /* Function:     DrvUART_Open                                                                            */
  140. /*                                                                                                         */
  141. /* Parameter:                                */
  142. /*     u32Port -[in] UART Channel:  UART_PORT0 / UART_PORT1                                */
  143. /*               sParam  -[in] the struct parameter to configure UART                                      */
  144. /*                         include of                                                                      */
  145. /*                           * u32BaudRate - Baud rate                 */               
  146. /*                           * u8cParity   - DRVUART_PARITY_NONE / DRVUART_PARITY_EVEN / DRVUART_PARITY_ODD*/
  147. /*                           * u8cDataBits - DRVUART_DATA_BITS_5 / DRVUART_DATA_BITS_6         */
  148. /*               DRVUART_DATA_BITS_7 / DRVUART_DATA_BITS_8              */
  149. /*                           * u8cStopBits - DRVUART_STOPBITS_1 / STOPBITS_1_5 / STOPBITS_2         */
  150. /*                           * u8cRxTriggerLevel   - LEVEL_1_BYTE to LEVEL_62_BYTES                        */
  151. /*                           * u8TimeOut - Time out value                                           */
  152. /*                                                                                                         */
  153. /*                                                                                                         */
  154. /* Returns:                                                                                                */
  155. /*               E_DRVUART_ERR_PORT_INVALID                            */
  156. /*               E_DRVUART_ERR_PARITY_INVALID                                 */
  157. /*               E_DRVUART_ERR_DATA_BITS_INVALID                             */
  158. /*               E_DRVUART_ERR_STOP_BITS_INVALID                             */
  159. /*               E_DRVUART_ERR_TRIGGERLEVEL_INVALID                     */
  160. /*               E_DRVUART_ERR_ALLOC_MEMORY_FAIL                              */
  161. /*               E_SUCCESS                                                                              */
  162. /*                                                                                                         */
  163. /* Description:                                                                                            */
  164. /*               The function is used to initialize UART. It consists of baud-rate, parity, data-bits,     */
  165. /*               stop-bits, rx-trigger-level and timeout interval settings.                                */
  166. /*                                                                                                         */
  167. /*---------------------------------------------------------------------------------------------------------*/
  168. int32_t DrvUART_Open(E_UART_PORT u32Port, STR_UART_T *sParam)
  169. {
  170. UART_T * tUART;
  171.   
  172.     /*-----------------------------------------------------------------------------------------------------*/
  173.     /* Check UART port                                                                                     */
  174.     /*-----------------------------------------------------------------------------------------------------*/
  175.     if ((u32Port != UART_PORT0) &&
  176.         (u32Port != UART_PORT1))
  177.     {
  178.   return E_DRVUART_ERR_PORT_INVALID;
  179.     }

  180.     /*-----------------------------------------------------------------------------------------------------*/
  181.     /* Check the supplied parity                                                                           */
  182.     /*-----------------------------------------------------------------------------------------------------*/
  183.     if ((sParam->u8cParity != DRVUART_PARITY_NONE) &&
  184.         (sParam->u8cParity != DRVUART_PARITY_EVEN) &&
  185.         (sParam->u8cParity != DRVUART_PARITY_ODD)  &&
  186.         (sParam->u8cParity != DRVUART_PARITY_MARK) &&
  187.         (sParam->u8cParity != DRVUART_PARITY_SPACE))
  188.     {
  189.    return E_DRVUART_ERR_PARITY_INVALID;  
  190.     }
  191.     /*-----------------------------------------------------------------------------------------------------*/
  192.     /* Check the supplied number of data bits                                                              */
  193.     /*-----------------------------------------------------------------------------------------------------*/
  194.     else if ((sParam->u8cDataBits != DRVUART_DATABITS_5) &&
  195.              (sParam->u8cDataBits != DRVUART_DATABITS_6) &&
  196.              (sParam->u8cDataBits != DRVUART_DATABITS_7) &&
  197.              (sParam->u8cDataBits != DRVUART_DATABITS_8))
  198.     {
  199.    return E_DRVUART_ERR_DATA_BITS_INVALID;
  200.     }
  201.     /*-----------------------------------------------------------------------------------------------------*/
  202.     /* Check the supplied number of stop bits                                                              */
  203.     /*-----------------------------------------------------------------------------------------------------*/
  204.     else if ((sParam->u8cStopBits != DRVUART_STOPBITS_1) &&
  205.              (sParam->u8cStopBits != DRVUART_STOPBITS_2) &&
  206.              (sParam->u8cStopBits != DRVUART_STOPBITS_1_5)
  207.              )
  208.     {
  209.        return E_DRVUART_ERR_STOP_BITS_INVALID;
  210.     }

  211.     /*-----------------------------------------------------------------------------------------------------*/
  212.     /* Check the supplied nember of trigger level bytes                                                    */
  213.     /*-----------------------------------------------------------------------------------------------------*/
  214.     else if ((sParam->u8cRxTriggerLevel != DRVUART_FIFO_1BYTES) &&
  215.              (sParam->u8cRxTriggerLevel != DRVUART_FIFO_4BYTES) &&
  216.              (sParam->u8cRxTriggerLevel != DRVUART_FIFO_8BYTES) &&
  217.              (sParam->u8cRxTriggerLevel != DRVUART_FIFO_14BYTES)&&
  218.              (sParam->u8cRxTriggerLevel != DRVUART_FIFO_30BYTES)&&
  219.              (sParam->u8cRxTriggerLevel != DRVUART_FIFO_46BYTES)&&
  220.              (sParam->u8cRxTriggerLevel != DRVUART_FIFO_62BYTES))
  221.     {
  222.   return E_DRVUART_ERR_TRIGGERLEVEL_INVALID;  
  223.     }   
  224. if(u32Port == UART_PORT0)
  225. {
  226.   /* Reset IP */
  227.   SYS->IPRSTC2.UART0_RST = 1;
  228.   SYS->IPRSTC2.UART0_RST = 0;
  229.   
  230.   /* Enable UART clock */
  231.      SYSCLK->APBCLK.UART0_EN = 1;
  232. }
  233. else
  234. {
  235.   /* Reset IP */
  236.   SYS->IPRSTC2.UART1_RST = 1;
  237.   SYS->IPRSTC2.UART1_RST = 0;
  238.   
  239.   /* Enable UART clock */
  240.      SYSCLK->APBCLK.UART1_EN = 1;
  241. }
  242. tUART = (UART_T *)((uint32_t)UART0 + u32Port);

  243. /* Tx FIFO Reset & Rx FIFO Reset & FIFO Mode Enable */
  244.    tUART->FCR.TFR =1;
  245.    tUART->FCR.RFR =1;
  246. /* Set Rx Trigger Level */
  247. tUART->FCR.RFITL = sParam->u8cRxTriggerLevel;

  248. /* Set Parity & Data bits & Stop bits */
  249. tUART->LCR.SPE =((sParam->u8cParity)&0x4)?1:0;
  250. tUART->LCR.EPE =((sParam->u8cParity)&0x2)?1:0;
  251. tUART->LCR.PBE =((sParam->u8cParity)&0x1)?1:0;
  252.   
  253. tUART->LCR.WLS =sParam->u8cDataBits;
  254. tUART->LCR.NSB =sParam->u8cStopBits;
  255.   
  256. /* Set Time-Out */
  257. tUART->TOR.TOIC =sParam->u8TimeOut;
  258.     /* Check Clock Source */
  259.     if(SYSCLK->PWRCON.XTL12M_EN)
  260.         SYSCLK->CLKSEL1.UART_S = 0;
  261.     else
  262.     {
  263.         if(SYSCLK->PLLCON.PD==0)
  264.             SYSCLK->CLKSEL1.UART_S = 1;
  265.         else
  266.             if(SYSCLK->PWRCON.OSC22M_EN)
  267.                 SYSCLK->CLKSEL1.UART_S = 3;
  268.             else
  269.                 return E_DRVUART_ERR_OPEN;
  270.     }
  271. /* Set BaudRate */
  272. BaudRateCalculator(GetUartClock(), sParam->u32BaudRate, u32Port);
  273. return E_SUCCESS;
  274. }

  275. /*---------------------------------------------------------------------------------------------------------*/
  276. /* Function:        DrvUART_Close                                                                          */
  277. /*                                                                                                         */
  278. /* Parameters:                                                                                             */
  279. /*      u32Port   -[in] UART Channel:  UART_PORT0 / UART_PORT1                              */
  280. /* RETURN                                                                                                  */
  281. /*                  None                                                                                   */
  282. /* Description:                                                                                            */
  283. /*                  The function is used to disable UART clock, disable ISR and clear callback       */
  284. /*                  function pointer after checking the TX empty.                                          */
  285. /*---------------------------------------------------------------------------------------------------------*/
  286. void DrvUART_Close(E_UART_PORT u32Port)
  287. {
  288. if(u32Port == UART_PORT1)
  289. {
  290.   while(!UART1->FSR.TE_FLAG);
  291.   SYSCLK->APBCLK.UART1_EN = 0;
  292.   g_pfnUART1callback = NULL;
  293.          NVIC_DisableIRQ (UART1_IRQn);
  294. }
  295. else
  296. {
  297.   while(!UART0->FSR.TE_FLAG);
  298.   SYSCLK->APBCLK.UART0_EN = 0;
  299.   g_pfnUART0callback = NULL;
  300.         NVIC_DisableIRQ(UART0_IRQn);     
  301. }
  302. }

  303. /*---------------------------------------------------------------------------------------------------------*/
  304. /* Function:     DrvUART_EnableInt                                                                         */
  305. /*                                                                                                         */
  306. /* Parameter:                                                                                              */
  307. /*     u32Port    -[in] UART Channel:  UART_PORT0 / UART_PORT1                             */
  308. /*               u32InterruptFlag -[in] DRVUART_WAKEUPINT/DRVUART_RLSINT/DRVUART_BUFERRINT                 */
  309. /*          DRVUART_MOSINT/DRVUART_THREINT/DRVUART_RDAINT/DRVUART_TOUTINT      */
  310. /*               pfncallback      -[in] A function pointer for callback function                           */
  311. /* Returns:                                                                                                */
  312. /*               None                                                                                      */
  313. /* Description:                                                                                            */
  314. /*               The function is used to enable specified UART interrupt, install the callback             */
  315. /*               function and enable NVIC UART IRQ                                                         */
  316. /* Remark:                                                                                                 */
  317. /*               When enable DRVUART_THREINT interrupt source, the interrup will happen continouly if      */
  318. /*               TX_FIFO is empty. Remind you thant be careful to use it.                                  */
  319. /*               Please reference TRM to get more information.                                             */
  320. /*---------------------------------------------------------------------------------------------------------*/
  321. void
  322. DrvUART_EnableInt(
  323. E_UART_PORT   u32Port,
  324. uint32_t  u32InterruptFlag,
  325. PFN_DRVUART_CALLBACK pfncallback
  326. )
  327. {
  328. UART_T * tUART;
  329.    
  330. tUART = (UART_T *)((uint32_t)UART0 + u32Port);
  331. tUART->IER.RDA_IEN  =(u32InterruptFlag & DRVUART_RDAINT)?1:0 ;
  332. tUART->IER.THRE_IEN  =(u32InterruptFlag & DRVUART_THREINT)?1:0;
  333. tUART->IER.RLS_IEN  =(u32InterruptFlag & DRVUART_RLSINT)?1:0;
  334. tUART->IER.MODEM_IEN =(u32InterruptFlag & DRVUART_MOSINT)?1:0;
  335.   
  336. tUART->IER.TIME_OUT_EN =(u32InterruptFlag & DRVUART_TOUTINT)?1:0;    /* Time-out count enable */
  337. tUART->IER.RTO_IEN  =(u32InterruptFlag & DRVUART_TOUTINT)?1:0;  /* Time-out INT enable */
  338. tUART->IER.BUF_ERR_IEN =(u32InterruptFlag & DRVUART_BUFERRINT)?1:0;
  339. tUART->IER.WAKE_EN  =(u32InterruptFlag & DRVUART_WAKEUPINT)?1:0;
  340.     if(u32Port == UART_PORT0)               /* Install Callback function */
  341.     {
  342.   g_pfnUART0callback = pfncallback;      
  343.   NVIC_SetPriority(UART0_IRQn, (1<<__NVIC_PRIO_BITS) - 2);
  344.   NVIC_EnableIRQ(UART0_IRQn);
  345.     }
  346. else
  347. {
  348.   g_pfnUART1callback = pfncallback;
  349.   NVIC_SetPriority(UART1_IRQn, (1<<__NVIC_PRIO_BITS) - 2);
  350.   NVIC_EnableIRQ(UART1_IRQn);
  351. }
  352. }
  353.                  
  354. /*---------------------------------------------------------------------------------------------------------*/
  355. /* Function:     DrvUART_IsIntEnabled                                                                     */
  356. /*                                                                                                         */
  357. /* Parameter:                                                                                              */
  358. /*     u32Port       -[in] UART Channel:  UART_PORT0 / UART_PORT1                             */
  359. /*               u32InterruptFlag -[in] DRVUART_WAKEUPINT/DRVUART_RLSINT/DRVUART_BUFERRINT                 */
  360. /*          DRVUART_MOSINT/DRVUART_THREINT/DRVUART_RDAINT/DRVUART_TOUTINT      */
  361. /* Returns:                                                                                                */
  362. /*               Specified Interrupt Flag Set or clear                                                     */
  363. /* Description:                                                                                            */
  364. /*               The function is used to get the "interrupt enable" status           */
  365. /*---------------------------------------------------------------------------------------------------------*/
  366. uint32_t DrvUART_IsIntEnabled(E_UART_PORT u32Port,uint32_t u32InterruptFlag)
  367. {
  368. if(u32Port == UART_PORT0)     /* Read IER Register and check specified flag is enable */
  369.   return ((inpw(&UART0->IER) &
  370.    (u32InterruptFlag & (DRVUART_WAKEUPINT |DRVUART_BUFERRINT|
  371.      DRVUART_TOUTINT | DRVUART_MOSINT | DRVUART_RLSINT| DRVUART_THREINT | DRVUART_RDAINT))))?1:0;
  372. else
  373.   return ((inpw(&UART1->IER) &
  374.   (u32InterruptFlag & ( DRVUART_WAKEUPINT |DRVUART_BUFERRINT|
  375.      DRVUART_TOUTINT | DRVUART_MOSINT | DRVUART_RLSINT | DRVUART_THREINT | DRVUART_RDAINT))))?1:0;
  376. }

  377. /*---------------------------------------------------------------------------------------------------------*/
  378. /* Function:     DrvUART_DisableInt                                                                       */
  379. /*                                                                                                         */
  380. /* Parameter:                                                                                              */
  381. /*     u32Port       -[in] UART Channel:  UART_PORT0 / UART_PORT1                             */
  382. /*               u32InterruptFlag -[in] DRVUART_WAKEUPINT/DRVUART_RLSINT/DRVUART_BUFERRINT                 */
  383. /*          DRVUART_MOSINT/DRVUART_THREINT/DRVUART_RDAINT/DRVUART_TOUTINT      */
  384. /* Returns:                                                                                                */
  385. /*               None                                                                                      */
  386. /* Description:                                                                                            */
  387. /*               The function is used to disable UART specified interrupt, uninstall the call back         */
  388. /*               function and disable NVIC UART IRQ                                                        */
  389. /*---------------------------------------------------------------------------------------------------------*/
  390. void DrvUART_DisableInt(E_UART_PORT u32Port,uint32_t u32InterruptFlag)
  391. {
  392. if(u32Port == UART_PORT0)     
  393. {
  394.   outpw(&UART0->IER + u32Port,inpw(&UART0->IER + u32Port) &~u32InterruptFlag);    /* Disable INT  */
  395. }

  396. if(u32InterruptFlag & DRVUART_TOUTINT)             /* Disable Counter Enable */
  397. {
  398.   if(u32Port == UART_PORT0)
  399.    UART0->IER.RTO_IEN = 0;
  400.   else
  401.    UART1->IER.RTO_IEN = 0;
  402. }

  403. switch (u32Port)                /* Disable Callback function and NVIC */
  404.     {
  405.         case UART_PORT0:
  406.    g_pfnUART0callback = NULL;
  407.             NVIC_DisableIRQ(UART0_IRQn);  
  408.             break;
  409.         case UART_PORT1:
  410.    g_pfnUART1callback = NULL;
  411.             NVIC_DisableIRQ(UART1_IRQn);   
  412.             break;
  413.       
  414.         default:
  415.             break;
  416.     }
  417. }

  418. /*---------------------------------------------------------------------------------------------------------*/
  419. /* Function:     DrvUART_ClearIntFlag                                                                      */
  420. /*                                                                                                         */
  421. /* Parameter:                                                                                              */
  422. /*               u32Port              -[in]   UART Channel:  UART_PORT0 / UART_PORT1                       */
  423. /*               u32InterruptFlag     -[in]   Interrupt Flag : DRVUART_RLSINT_FLAG/DRVUART_MOSINT_FLAG     */
  424. /*                                                             DRVUART_BUFERRINT_FLAG                      */
  425. /* Returns:                                                                                                */
  426. /*               E_SUCCESS      Successful                                                                 */
  427. /* Description:                                                                                            */
  428. /*               The function is used to clear UART specified interrupt flag                               */
  429. /*                                                                                                         */
  430. /*---------------------------------------------------------------------------------------------------------*/
  431. uint32_t DrvUART_ClearIntFlag(E_UART_PORT u32Port,uint32_t u32InterruptFlag)
  432. {
  433.     UART_T * tUART;
  434.     tUART = (UART_T *)((uint32_t)UART0 + u32Port);
  435.     if((u32InterruptFlag & DRVUART_RDAINT_FLAG)  ||
  436.        (u32InterruptFlag & DRVUART_THREINT_FLAG) ||
  437.        (u32InterruptFlag & DRVUART_TOUTINT_FLAG) )
  438.     {
  439.         return E_DRVUART_ARGUMENT;
  440.     }
  441.     if(u32InterruptFlag & DRVUART_RLSINT_FLAG)          /* clear Receive Line Status Interrupt */     
  442.     {
  443.         tUART->FSR.BIF = 1;
  444.         tUART->FSR.FEF = 1;
  445.         tUART->FSR.PEF = 1;
  446.     }
  447.     if(u32InterruptFlag & DRVUART_MOSINT_FLAG)          /* clear Modem Interrupt */   
  448.         tUART->MSR.DCTSF = 1;

  449.     if(u32InterruptFlag & DRVUART_BUFERRINT_FLAG)       /* clear Buffer ErrorInterrupt */     
  450.     {
  451.         tUART->FSR.TX_OVER_IF = 1;
  452.         tUART->FSR.RX_OVER_IF = 1;
  453.     }
  454.     return E_SUCCESS;
  455. }

  456. /*---------------------------------------------------------------------------------------------------------*/
  457. /* Function:     DrvUART_GetIntStatus                                                                     */
  458. /*                                                                                                         */
  459. /* Parameter:                                                                                              */
  460. /*     u32Port          -[in]   UART Channel:  UART_PORT0 / UART_PORT1                       */
  461. /*               u32InterruptFlag     -[in]   DRVUART_LININT_FLAG / DRVUART_BUFERRINT_FLAG           */
  462. /*             DRVUART_TOUTINT_FLAG/DRVUART_MOSINT_FLAG                     */
  463. /*                                            DRVUART_RLSINT_FLAG/DRVUART_THREINT_FLAG/DRVUART_RDAINT_FLAG */
  464. /*                                                                                                         */
  465. /* Returns:                                                                                                */
  466. /*               0: The specified interrupt is not happened.                                               */
  467. /*               1: The specified interrupt is happened.                                                   */
  468. /*               E_DRVUART_ARGUMENT: Error Parameter                                                       */
  469. /* Description:                                                                                            */
  470. /*               The function is used to get the interrupt status             */
  471. /*---------------------------------------------------------------------------------------------------------*/
  472. int32_t DrvUART_GetIntStatus(E_UART_PORT u32Port,uint32_t u32InterruptFlag)
  473. {
  474. UART_T * tUART;
  475. tUART = (UART_T *)((uint32_t)UART0 + u32Port);
  476.   
  477.     return (tUART->u32ISR & u32InterruptFlag)>>u32InterruptFlag ;
  478. }

  479. /*---------------------------------------------------------------------------------------------------------*/
  480. /* Function:     DrvUART_GetCTSInfo                                                               */
  481. /*                                                                                                         */
  482. /* Parameter:                                */
  483. /*     u32Port          -[in]   UART Channel:  UART_PORT0 / UART_PORT1                       */
  484. /*               pu8CTSValue       -[out]   Buffer to store the CTS Value                          */
  485. /*                                             return current CTS pin state                                */
  486. /*     pu8CTSChangeState   -[out]   Buffer to store the CTS Change State                        */
  487. /*                                             return CTS pin status is changed or not 1:Changed 0:Not yet */
  488. /* Returns:                                                                                                */
  489. /*               None                                                                                      */
  490. /* Description:                                                                                            */
  491. /*               The function is used to get CTS pin value and detect CTS change state                     */
  492. /*---------------------------------------------------------------------------------------------------------*/
  493. void DrvUART_GetCTS(E_UART_PORT u32Port,uint8_t *pu8CTSValue, uint8_t *pu8CTSChangeState)
  494. {
  495. UART_T * tUART;
  496. tUART = (UART_T *)((uint32_t)UART0 + u32Port);
  497.   *pu8CTSValue   = tUART->MSR.CTS_ST;
  498. *pu8CTSChangeState  = tUART->MSR.DCTSF;
  499. }

  500. /*---------------------------------------------------------------------------------------------------------*/
  501. /* Function:     DrvUART_Read                                                                              */
  502. /*                                                                                                         */
  503. /* Parameter:                                                                                              */
  504. /*     u32Port             -[in]   UART Channel:  UART_PORT0 / UART_PORT1                       */
  505. /*               pu8RxBuf       -[in]   Specify the buffer to receive the data of receive FIFO       */
  506. /*               u32ReadBytes      -[in]   Specify the bytes number of data.                      */
  507. /* Returns:                                                                                                */
  508. /*               E_SUCCESS                                                                              */
  509. /* Description:                                                                                            */
  510. /*               The function is used to read Rx data from RX FIFO and the data will be stored in pu8RxBuf */
  511. /*---------------------------------------------------------------------------------------------------------*/
  512. int32_t DrvUART_Read(E_UART_PORT u32Port,uint8_t *pu8RxBuf, uint32_t u32ReadBytes)
  513. {
  514.     uint32_t  u32Count, u32delayno;
  515. UART_T * tUART;
  516. tUART = (UART_T *)((uint32_t)UART0 + u32Port);
  517.     for (u32Count=0; u32Count < u32ReadBytes; u32Count++)
  518.     {
  519.    u32delayno = 0;
  520.       while (tUART->FSR.RX_EMPTY ==1)         /* Check RX empty => failed */   
  521.       {
  522.        u32delayno++;      
  523.          if ( u32delayno >= 0x40000000 )      
  524.           return E_DRVUART_ERR_TIMEOUT;              
  525.          }
  526.          pu8RxBuf[u32Count] = tUART->DATA;         /* Get Data from UART RX  */
  527.     }
  528.     return E_SUCCESS;

  529. }

  530. /*---------------------------------------------------------------------------------------------------------*/
  531. /* Function:     DrvUART_Write                                                                            */
  532. /*                                                                                                         */
  533. /* Parameter:                                                                                              */
  534. /*     u32Port             -[in]   UART Channel:  UART_PORT0 / UART_PORT1                       */
  535. /*               pu8RxBuf       -[in]   Specify the buffer to send the data to transmission FIFO.    */
  536. /*               u32ReadBytes      -[in]   Specify the byte number of data.                          */
  537. /* Returns:                                                                                                */
  538. /*               E_SUCCESS                                                                              */
  539. /*                                                                                                         */
  540. /* Description:                                                                                            */
  541. /*               TThe function is to write data to TX buffer to transmit data by UART                      */
  542. /* Note:                                                                                                   */
  543. /*               In IrDA Mode, the BAUD RATE configure MUST to use MODE # 0                                */
  544. /*---------------------------------------------------------------------------------------------------------*/
  545. int32_t DrvUART_Write(E_UART_PORT u32Port,uint8_t *pu8TxBuf,  uint32_t u32WriteBytes)
  546. {
  547.     uint32_t  u32Count, u32delayno;
  548. UART_T * tUART;
  549. tUART = (UART_T *)((uint32_t)UART0 + u32Port);
  550.     for (u32Count=0; u32Count<u32WriteBytes; u32Count++)
  551.     {
  552.        u32delayno = 0;
  553.     while (tUART->FSR.TE_FLAG !=1)           /* Wait Tx empty and Time-out manner */
  554.        {
  555.             u32delayno++;
  556.             if ( u32delayno >= 0x40000000 )         
  557.                return E_DRVUART_ERR_TIMEOUT;            
  558.       
  559.        }
  560.     tUART->DATA =pu8TxBuf[u32Count];      /* Send UART Data from buffer */
  561.     }
  562.     return E_SUCCESS;


  563. }

  564. /*---------------------------------------------------------------------------------------------------------*/
  565. /* Function:     DrvUART_SetFnIRDA                                                                         */
  566. /*                                                                                                         */
  567. /* Parameter:                                                                                              */
  568. /*     u32Port          -[in]   UART Channel:  UART_PORT0 / UART_PORT1                       */
  569. /*               STR_IRCR_T       -[in]   Ther stucture of IRCR                                */
  570. /*                                            It includes of                                               */
  571. /*                                               u8cTXSelect: 1: Enable Irda transmit function.(TX mode)   */
  572. /*                                                            0: Disable Irda transmit function.(RX mode)  */
  573. /*                                               u8cInvTX: Inverse TX signal                               */
  574. /*                                               u8cInvRX: Inverse RX signal                               */
  575. /*                                                                                                         */
  576. /* Returns:                                                                                                */
  577. /*               None                                                                                    */
  578. /*                                                                                                         */
  579. /* Description:                                                                                            */
  580. /*               The function is used to configure IRDA relative settings.                                 */
  581. /*               It consists of TX or RX mode and Inverse TX or RX signals.                                */
  582. /*---------------------------------------------------------------------------------------------------------*/
  583. void DrvUART_SetFnIRDA(E_UART_PORT u32Port,STR_IRCR_T *str_IRCR )
  584. {
  585. UART_T * tUART;
  586. tUART = (UART_T *)((uint32_t)UART0 + u32Port);
  587. tUART->FUNSEL.FUN_SEL  = FUN_IRCR;           /* Enable IrDA function and configure */
  588. tUART->IRCR.TX_SELECT = (str_IRCR->u8cTXSelect) ?1:0;
  589. tUART->IRCR.INV_TX  = str_IRCR->u8cInvTX ;
  590. tUART->IRCR.INV_RX  = str_IRCR->u8cInvRX ;
  591. }

  592. /*---------------------------------------------------------------------------------------------------------*/
  593. /* Function:     DrvUART_SetFnRS485                                                                        */
  594. /*                                                                                                         */
  595. /* Parameter:                                                                                              */
  596. /*     u32Port             -[in]   UART Channel:  UART_PORT0 / UART_PORT1                       */
  597. /*               STR_RS485_T       -[in]   Ther stucture of RS485                                */
  598. /*                                            It includes of                                               */
  599. /*                                               u8cModeSelect: MODE_RS485_AUD / MODE_RS485_AAD            */
  600. /*                                                              MODE_RS485_NMM                             */
  601. /*                                               u8cAddrEnable: Enable or Disable RS-485 Address Detection */
  602. /*                                               u8cAddrValue:  Match Address Value                        */
  603. /*                                               u8cDelayTime:  Set transmit delay time value              */
  604. /*                                               u8cRxDisable:  Enable or Disable receiver function        */
  605. /* Returns:                                                                                                */
  606. /*               None                                                                                   */
  607. /*                                                                                                         */
  608. /* Description:                                                                                            */
  609. /*               The function is to Set RS485 Control Register                                             */
  610. /*---------------------------------------------------------------------------------------------------------*/
  611. void DrvUART_SetFnRS485(E_UART_PORT u32Port,STR_RS485_T *str_RS485)
  612. {

  613. UART_T * tUART;
  614. tUART = (UART_T *)((uint32_t)UART0 + u32Port);
  615. tUART->FUNSEL.FUN_SEL    = FUN_RS485;     /* Enable RS485 function and configure */
  616. tUART->ALTCON.RS485_ADD_EN     =  str_RS485-> u8cAddrEnable ?1:0;
  617. tUART->ALTCON.ADDR_MATCH  =  str_RS485-> u8cAddrValue ;
  618. tUART->ALTCON.RS485_NMM      = (str_RS485-> u8cModeSelect & MODE_RS485_NMM)?1:0;
  619. tUART->ALTCON.RS485_AAD      = (str_RS485-> u8cModeSelect & MODE_RS485_AAD)?1:0;
  620. tUART->ALTCON.RS485_AUD      = (str_RS485-> u8cModeSelect & MODE_RS485_AUD)?1:0;
  621. tUART->TOR.DLY                  =  str_RS485-> u8cDelayTime;
  622. tUART->FCR.RX_DIS               =  str_RS485-> u8cRxDisable;
  623. tUART->MCR.LEV_RTS = 0;

  624. }
  625. /*---------------------------------------------------------------------------------------------------------*/
  626. /* Function:     DrvUART_GetVersion                                                                        */
  627. /*                                                                                                         */
  628. /* Parameter:                                */
  629. /*              None                                                                                     */
  630. /* Returns:                                                                                                */
  631. /*               Version Number                                                                            */
  632. /* Side effects:                                                                                           */
  633. /*                                                                                                         */
  634. /* Description:                                                                                            */
  635. /*               The function is used to get  DrvUART Version Number                                       */
  636. /*---------------------------------------------------------------------------------------------------------*/
  637. int32_t DrvUART_GetVersion(void)
  638. {
  639. return DRVUART_VERSION_NUM;

  640. }
复制代码




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:82781 发表于 2015-6-13 16:41 | 只看该作者
使用串口终端的时候,开启端口之后,必须开启端口之后才可以安装IRQ和回调函数。回调函数包含一个状态码,在函数中只要判断这个状态不等于NULL,就可以进行处理。最好设置一个全局标志位,标志接受的数据是否完成,每次接受都会进入,系统自动清除,要认清他的这个特性,确保中断的有效性!总之,又中断就用,尽量保证实时性!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表