关于K21的uart中断发送的问题

[复制链接]
1446|0
 楼主| LZR13179 发表于 2015-4-8 19:24 | 显示全部楼层 |阅读模式
UART, TE, se, TI, ui
如题官方是有使用uart中断发送的例子(非DMA)
我使用的代码如下,无法进行中段发送(但是进行跟踪发现是一直处于TC的状态,但是无法收到发送的数据)
具体代码如下
  1. /*
  2. * File:        uart.c
  3. * Purpose:     Provide common UART routines for serial IO
  4. *
  5. * Notes:      
  6. *              
  7. */

  8. #include "types.h"
  9. #include "uart.h"
  10.   

  11. uint_32 gu32UARTStatus;
  12. uint_32 gu32UATRxCounter = 0;
  13. uint_8 * gpu8UARTTx;
  14. uint_8 * gpu8UARTRx;
  15. uint_32 gu32TxDataSize;
  16. uint_32 gu32RxCounter = 0;
  17. /********************************************************************/
  18. /*
  19. * Initialize the UART for 8N1 operation, interrupts disabled, and
  20. * no hardware flow-control
  21. *
  22. * NOTE: Since the UARTs are pinned out in multiple locations on most
  23. *       Kinetis devices, this driver does not enable UART pin functions.
  24. *       The desired pins should be enabled before calling this init function.
  25. *
  26. * Parameters:
  27. *  uartch      UART channel to initialize
  28. *  sysclk      UART module Clock in kHz(used to calculate baud)
  29. *  baud        UART baud rate
  30. */

  31. void vfnUARTInit (uint_8 u8UARTPort, uint_32 u32SysCLK, uint_32 u32BaudRate)
  32. {
  33.     uint_16 u16SBR;
  34.     uint_16 u16BRFA;
  35.     uint_8 u8temp;
  36.     UART_MemMapPtr tUART;
  37.    
  38.         /* Enable the clock to the selected UART */   
  39.     if(u8UARTPort == UART0)
  40.     {
  41.                 SIM_SCGC4 |= SIM_SCGC4_UART0_MASK;
  42.                 tUART = UART0_BASE_PTR;
  43.     }
  44.     else
  45.     {
  46.             if (u8UARTPort == UART1)
  47.             {
  48.                         SIM_SCGC4 |= SIM_SCGC4_UART1_MASK;
  49.                         tUART = UART1_BASE_PTR;
  50.             }
  51.             else
  52.             {
  53.                     if (u8UARTPort == UART2)
  54.                     {
  55.                             SIM_SCGC4 |= SIM_SCGC4_UART2_MASK;
  56.                                 tUART = UART2_BASE_PTR;
  57.                     }
  58.                     else
  59.                     {
  60.                             if(u8UARTPort == UART3)
  61.                             {
  62.                                     SIM_SCGC4 |= SIM_SCGC4_UART3_MASK;
  63.                                         tUART = UART3_BASE_PTR;
  64.                             }
  65.                             else
  66.                             {
  67.                                     if(u8UARTPort == UART4)
  68.                                     {
  69.                                             SIM_SCGC1 |= SIM_SCGC1_UART4_MASK;       
  70.                                                 tUART = UART4_BASE_PTR;
  71.                                     }
  72.                                     else
  73.                                     {
  74.                                             SIM_SCGC1 |= SIM_SCGC1_UART5_MASK;
  75.                                                 tUART = UART5_BASE_PTR;
  76.                                     }
  77.                             }
  78.                     }
  79.             }
  80.     }                           
  81.     /* Make sure that the transmitter and receiver are disabled while we
  82.      * change settings.
  83.      */
  84.     UART_C2_REG(tUART) &= ~(UART_C2_TE_MASK
  85.                                 | UART_C2_RE_MASK );

  86.     /* Configure the UART for 8-bit mode, no parity */
  87.     UART_C1_REG(tUART) = 0;        /* We need all default settings, so entire register is cleared */
  88.    
  89.     /* Calculate baud settings */
  90.     u16SBR = (uint_16)((u32SysCLK*1000)/(u32BaudRate * 16));
  91.         
  92.     /* Save off the current value of the UARTx_BDH except for the SBR field */
  93.     u8temp = UART_BDH_REG(tUART) & ~(UART_BDH_SBR(0x1F));
  94.    
  95.     UART_BDH_REG(tUART) = u8temp |  UART_BDH_SBR(((u16SBR & 0x1F00) >> 8));
  96.     UART_BDL_REG(tUART) = (uint_8)(u16SBR & UART_BDL_SBR_MASK);
  97.    
  98.     /* Determine if a fractional divider is needed to get closer to the baud rate */
  99.     u16BRFA = (((u32SysCLK*32000)/(u32BaudRate * 16)) - (u16SBR * 32));
  100.    
  101.     /* Save off the current value of the UARTx_C4 register except for the BRFA field */
  102.     u8temp = UART_C4_REG(tUART) & ~(UART_C4_BRFA(0x1F));
  103.    
  104.     UART_C4_REG(tUART) = u8temp |  UART_C4_BRFA(u16BRFA);
  105.    
  106.     gu32UARTStatus = (1 << TX_COMPLETE) | (1 << RX_DATA);
  107.    
  108.     UART_C2_REG(tUART) |= UART_C2_RIE_MASK | UART_C2_RE_MASK | UART_C2_TE_MASK;
  109.    
  110.     /*NVIC*/
  111.     enable_irq(37); /* UART 3 */
  112.     /* Enable receiver and transmitter */
  113. }

  114. void vfnUARTTx(uint_8 * pu8Data, uint_32 u32DataSize)
  115. {
  116.         if((gu32UARTStatus & (gu32UARTStatus & (1 << TX_COMPLETE))))
  117.         {
  118.                 gu32TxDataSize = u32DataSize;
  119.                 gpu8UARTTx = pu8Data;
  120.                 gu32UARTStatus &= ~(1 << TX_COMPLETE);
  121.                 UART3_C2 |= UART_C2_TCIE_MASK;
  122.         }
  123. }

  124. void vfnUARTRx(uint_8 * pu8Data, uint_32 u32DataSize)
  125. {
  126.         if((gu32UARTStatus & (gu32UARTStatus & (1 << RX_DATA))))
  127.         {
  128.                 gpu8UARTRx = pu8Data;
  129.                 gu32RxCounter = u32DataSize;
  130.                 gu32UARTStatus &= ~(1 << RX_DATA);
  131.         }
  132. }

  133. void vfnUART3ISR(void)
  134. {
  135.         uint_8 u8temp = UART3_S1;

  136.         if(UART3_S1 & UART_S1_RDRF_MASK)
  137.         {
  138.                 *gpu8UARTRx = UART3_D;
  139.                 gpu8UARTRx++;
  140.                 gu32RxCounter--;
  141.                 if(!gu32RxCounter)
  142.                 {                               
  143.                         gu32UARTStatus |= (1 << RX_DATA);
  144.                 }
  145.         }
  146.         if(UART3_S1 & UART_S1_TC_MASK)
  147.         {
  148.                 if(gu32TxDataSize)
  149.                 {
  150.                         UART3_D = *gpu8UARTTx++;
  151.                         gu32TxDataSize--;
  152.                 }
  153.                 else
  154.                 {
  155.                         UART3_C2 &= ~UART_C2_TCIE_MASK;
  156.                         gu32UARTStatus |= (1 << TX_COMPLETE);
  157.                 }
  158.         }
  159. }

  1. /*
  2. * File:                uart.h
  3. * Purpose:     Provide common ColdFire UART routines for polled serial IO
  4. *
  5. * Notes:
  6. */

  7. #ifndef __UART_H__
  8. #define __UART_H__

  9. #include "derivative.h"

  10. extern uint_32 gu32UARTStatus;

  11. typedef enum
  12. {
  13.         TX_COMPLETE = 0,
  14.         RX_DATA,
  15. }eUARTStatus;

  16. #define RX_COMPLETE        (gu32UARTStatus & (1 << RX_DATA))
  17. #define TX_END        (gu32UARTStatus & (1 << TX_COMPLETE))
  18. #define UART0        0
  19. #define UART1        1
  20. #define UART2        2
  21. #define UART3        3
  22. #define UART4        4
  23. #define UART5        5

  24. #define BAUDRATE_115200        (115200)
  25. /********************************************************************/
  26. //#define  PETOOLKIT_FUNCTION
  27. void vfnUARTInit (uint_8 u8UARTPort, uint_32 u32SysCLK, uint_32 u32BaudRate);
  28. void vfnUARTTx(uint_8 * pu8Data, uint_32 u32DataSize);
  29. void vfnUARTRx(uint_8 * pu8Data, uint_32 u32DataSize);
  30. /********************************************************************/

  31. #endif /* __UART_H__ */
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

87

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部