查看: 2792|回复: 8

[求助] 关于K21的uart中断发送的问题

[复制链接]

该用户从未签到

4

主题

12

帖子

0

注册会员

Rank: 2

积分
72
最后登录
2015-4-16
发表于 2015-4-9 10:03:46 | 显示全部楼层 |阅读模式
如题官方是有使用uart中断发送的例子(非DMA)
我使用的代码如下,无法进行中段发送(但是进行跟踪发现是一直处于TC的状态,但是无法收到发送的数据)
具体代码如下
  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__ */
复制代码
  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. }

复制代码



我知道答案 目前已有8人回答
回复

使用道具 举报

  • TA的每日心情
    难过
    2021-12-15 16:01
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    305

    主题

    4701

    帖子

    0

    中级会员

    Rank: 3Rank: 3

    积分
    377
    最后登录
    2023-8-16
    发表于 2015-4-9 17:31:59 | 显示全部楼层
    UART_C2_REG(tUART) |= UART_C2_RIE_MASK | UART_C2_RE_MASK | UART_C2_TE_MASK;

    这里没有开启TIE中断.
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    4

    主题

    12

    帖子

    0

    注册会员

    Rank: 2

    积分
    72
    最后登录
    2015-4-16
     楼主| 发表于 2015-4-9 19:31:57 | 显示全部楼层
    安 发表于 2015-4-9 17:31
    UART_C2_REG(tUART) |= UART_C2_RIE_MASK | UART_C2_RE_MASK | UART_C2_TE_MASK;

    这里没有开启TIE中断. ...

    在后面的使用
    UART3_C2 |= UART_C2_TCIE_MASK;
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    4

    主题

    12

    帖子

    0

    注册会员

    Rank: 2

    积分
    72
    最后登录
    2015-4-16
     楼主| 发表于 2015-4-9 19:35:20 | 显示全部楼层
    安 发表于 2015-4-9 17:31
    UART_C2_REG(tUART) |= UART_C2_RIE_MASK | UART_C2_RE_MASK | UART_C2_TE_MASK;

    这里没有开启TIE中断. ...

    根据 UART interrupt sources中
    Interrupt      Source Flag    Local enable
    Transmitter   TDRE              TIE
    Transmitter    TC                TCIE
    对应关系
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    4

    主题

    12

    帖子

    0

    注册会员

    Rank: 2

    积分
    72
    最后登录
    2015-4-16
     楼主| 发表于 2015-4-9 19:40:22 | 显示全部楼层
    安 发表于 2015-4-9 17:31
    UART_C2_REG(tUART) |= UART_C2_RIE_MASK | UART_C2_RE_MASK | UART_C2_TE_MASK;

    这里没有开启TIE中断. ...

    temp =UART3_C2;
    UART3_C2=0;

    UART3_C2 |= UART_C2_TCIE_MASK;
    UART3_C2 |temp ;
    改成这样就能够正常中断发送了
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    难过
    2021-12-15 16:01
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    305

    主题

    4701

    帖子

    0

    中级会员

    Rank: 3Rank: 3

    积分
    377
    最后登录
    2023-8-16
    发表于 2015-4-10 09:12:37 | 显示全部楼层
    TCIE,是发生完成以后产生的中断,如果直接开启,串口并没有数据传输,理论他不会产生中断。楼主可以按以前的方法写,先随便发送一个数据,然后再看他是否能够进入中断。
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    124

    主题

    3600

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    5781
    最后登录
    1970-1-1
    发表于 2015-4-10 16:44:43 | 显示全部楼层
    本帖最后由 FSL_TICS_ZP 于 2015-4-10 16:54 编辑

    楼主,你原来的代码应该是可以用的,我在这个基础简化一下,是可以收到数据的,我的代码和完整的工程如下
    1. /**
    2.   \mainpage
    3.   \n Copyright (c) 2011 Freescale
    4.   \n Freescale Confidential Proprietary
    5.   \brief
    6.   \author           Freescale Semiconductor
    7.   \author   
    8.   \version      m.n
    9.   \date         Sep 14, 2011
    10.   
    11.   Put here all the information needed of the Project. Basic configuration as well as information on
    12.   the project definition will be very useful
    13. */
    14. /****************************************************************************************************/
    15. /*                                                                                                  */
    16. /* All software, source code, included documentation, and any implied know-how are property of      */
    17. /* Freescale Semiconductor and therefore considered CONFIDENTIAL INFORMATION.                       */
    18. /* This confidential information is disclosed FOR DEMONSTRATION PURPOSES ONLY.                      */
    19. /*                                                                                                  */
    20. /* All Confidential Information remains the property of Freescale Semiconductor and will not be     */
    21. /* copied or reproduced without the express written permission of the Discloser, except for copies  */
    22. /* that are absolutely necessary in order to fulfill the Purpose.                                   */
    23. /*                                                                                                  */
    24. /* Services performed by FREESCALE in this matter are performed AS IS and without any warranty.     */
    25. /* CUSTOMER retains the final decision relative to the total design and functionality of the end    */
    26. /* product.                                                                                         */
    27. /* FREESCALE neither guarantees nor will be held liable by CUSTOMER for the success of this project.*/
    28. /*                                                                                                  */
    29. /* FREESCALE disclaims all warranties, express, implied or statutory including, but not limited to, */
    30. /* implied warranty of merchantability or fitness for a particular purpose on any hardware,         */
    31. /* software ore advise supplied to the project by FREESCALE, and or any product resulting from      */
    32. /* FREESCALE services.                                                                              */
    33. /* In no event shall FREESCALE be liable for incidental or consequential damages arising out of     */
    34. /* this agreement. CUSTOMER agrees to hold FREESCALE harmless against any and all claims demands or */
    35. /* actions by anyone on account of any damage,or injury, whether commercial, contractual, or        */
    36. /* tortuous, rising directly or indirectly as a result of the advise or assistance supplied CUSTOMER*/
    37. /* in connectionwith product, services or goods supplied under this Agreement.                      */
    38. /*                                                                                                  */
    39. /****************************************************************************************************/

    40. /*****************************************************************************************************
    41. * Include files
    42. *****************************************************************************************************/
    43. #include "common.h"

    44. /*****************************************************************************************************
    45. * Declaration of module wide FUNCTIONs - NOT for use in other modules
    46. *****************************************************************************************************/

    47. /*****************************************************************************************************
    48. * Definition of module wide MACROs / #DEFINE-CONSTANTs - NOT for use in other modules
    49. *****************************************************************************************************/

    50. /*****************************************************************************************************
    51. * Declaration of module wide TYPEs - NOT for use in other modules
    52. *****************************************************************************************************/

    53. /*****************************************************************************************************
    54. * Definition of module wide VARIABLEs - NOT for use in other modules
    55. *****************************************************************************************************/

    56. /*****************************************************************************************************
    57. * Definition of module wide (CONST-) CONSTANTs - NOT for use in other modules
    58. *****************************************************************************************************/

    59. /*****************************************************************************************************
    60. * Code of project wide FUNCTIONS
    61. *****************************************************************************************************/


    62. #include "common.h"
    63. #include "isr.h"


    64. void UART_RIE_Enable(void);
    65. void UART_Isr(void);

    66. extern uint32 gu32UARTStatus;
    67. uint32 gu32UARTStatus;
    68. uint32 gu32UATRxCounter = 0;
    69. uint8 * gpu8UARTTx;
    70. uint8 * gpu8UARTRx;
    71. uint32 gu32TxDataSize=0x10;
    72. uint32 gu32RxCounter = 0x10;

    73. typedef enum
    74. {
    75.         TX_COMPLETE = 0,
    76.         RX_DATA,
    77. }eUARTStatus;

    78. #define RX_COMPLETE        (gu32UARTStatus & (1 << UART_S1_RDRF_SHIFT))
    79. #define TX_END        (gu32UARTStatus & (1 << UART_S1_TC_SHIFT))
    80. /**
    81. * \brief   main
    82. * \author  
    83. * \param  none
    84. * \return none
    85. * \todo
    86. * \warning
    87. */  
    88. int main (void)
    89. {
    90.         char ch;

    91. #ifdef KEIL
    92.         start();
    93. #endif
    94.         
    95.           //printf("\nRunning the 'hello world' project for the K21 50 MHz family\n");
    96. EnableInterrupts;      
    97. enable_irq(35);
    98.         UART_RIE_Enable();

    99.         while(1)
    100.         {
    101. //                ch = getchar();
    102. //                putchar(ch);
    103.                 //ch = in_char();
    104.                 //out_char(ch);         
    105.         }
    106. }
    107. /********************************************************************/

    108. void UART_RIE_Enable(void)
    109. {
    110.   
    111.   UART2_C2|= UART_C2_RIE_MASK | UART_C2_TCIE_MASK;   
    112.   
    113.   
    114. }

    115. void UART_Isr(void)
    116. {

    117.      uint8 u8temp = UART2_S1;
    118.      uint8 rev;

    119.         if(UART2_S1 & UART_S1_RDRF_MASK)
    120.         {
    121.                 rev = UART2_D;
    122.                
    123.                 //gpu8UARTRx++;
    124.                 gu32RxCounter--;
    125.                 if(!gu32RxCounter)
    126.                 {                                
    127.                   gu32UARTStatus |= (1 << RX_DATA);
    128.                   out_char('Y');
    129.                 }
    130.         }
    131.         if(UART2_S1 & UART_S1_TC_MASK)
    132.         {
    133.                 if(gu32TxDataSize)
    134.                 {
    135.                    UART2_D = 0x32;
    136.                    gu32TxDataSize--;
    137.                 }
    138.                 else
    139.                 {
    140.                         UART2_C2 &= ~UART_C2_TCIE_MASK;
    141.                         gu32UARTStatus |= (1 << TX_COMPLETE);
    142.                 }
    143.         }
    144. }
    复制代码
    TWR-K21D50M Sample Code.zip (9.34 MB, 下载次数: 13)
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    4

    主题

    12

    帖子

    0

    注册会员

    Rank: 2

    积分
    72
    最后登录
    2015-4-16
     楼主| 发表于 2015-4-10 17:03:32 | 显示全部楼层
    FSL_TICS_ZP 发表于 2015-4-10 16:44
    楼主,你原来的代码应该是可以用的,我在这个基础简化一下,是可以收到数据的,我的代码和完整的工程如下

    ...

    多谢了。
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    124

    主题

    3600

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    5781
    最后登录
    1970-1-1
    发表于 2015-5-13 17:16:57 | 显示全部楼层
       非常感谢你关于Kinetis的经验分享!
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-7-24 02:51 , Processed in 0.110752 second(s), 30 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

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