[DemoCode下载] CAN唤醒系统

[复制链接]
1505|6
 楼主| 21mengnan 发表于 2024-4-21 17:37 | 显示全部楼层 |阅读模式
  1. /****************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V2.0
  4. * $Revision: 7 $
  5. * $Date: 15/01/16 1:45p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show how to wake up system form Power-down mode by detecting a transition.
  7. * @note
  8. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  9. *
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "M071M.h"


  13. #define PLL_CLOCK       48000000

  14. /*---------------------------------------------------------------------------*/
  15. /*  Function Declare                                                         */
  16. /*---------------------------------------------------------------------------*/
  17. extern char GetChar(void);
  18. void CAN_ShowMsg(STR_CANMSG_T* Msg);

  19. /*---------------------------------------------------------------------------------------------------------*/
  20. /* Define global variables and constants                                                                   */
  21. /*---------------------------------------------------------------------------------------------------------*/
  22. /* Declare a CAN message structure */
  23. STR_CANMSG_T rrMsg;


  24. /*---------------------------------------------------------------------------------------------------------*/
  25. /* ISR to handle CAN interrupt event                                                                       */
  26. /*---------------------------------------------------------------------------------------------------------*/
  27. void CAN_MsgInterrupt(CAN_T *tCAN, uint32_t u32IIDR)
  28. {
  29.     if(u32IIDR == 1)
  30.     {
  31.         printf("Msg-0 INT and Callback\n");
  32.         CAN_Receive(tCAN, 0, &rrMsg);
  33.         CAN_ShowMsg(&rrMsg);
  34.     }
  35.     if(u32IIDR == 5 + 1)
  36.     {
  37.         printf("Msg-5 INT and Callback \n");
  38.         CAN_Receive(tCAN, 5, &rrMsg);
  39.         CAN_ShowMsg(&rrMsg);
  40.     }
  41.     if(u32IIDR == 31 + 1)
  42.     {
  43.         printf("Msg-31 INT and Callback \n");
  44.         CAN_Receive(tCAN, 31, &rrMsg);
  45.         CAN_ShowMsg(&rrMsg);
  46.         printf("Enter any key to exit\n");
  47.     }
  48. }

  49. /*---------------------------------------------------------------------------------------------------------*/
  50. /* CAN0 interrupt handler                                                                                  */
  51. /*---------------------------------------------------------------------------------------------------------*/
  52. void CAN0_IRQHandler(void)
  53. {
  54.     uint32_t u8IIDRstatus;

  55.     u8IIDRstatus = CAN0->IIDR;

  56.     if(u8IIDRstatus == 0x00008000)        /* Check Status Interrupt Flag (Error status Int and Status change Int) */
  57.     {
  58.         /**************************/
  59.         /* Status Change interrupt*/
  60.         /**************************/
  61.         if(CAN0->STATUS & CAN_STATUS_RXOK_Msk)
  62.         {
  63.             CAN0->STATUS &= ~CAN_STATUS_RXOK_Msk;   /* Clear RxOK status*/
  64.         }

  65.         if(CAN0->STATUS & CAN_STATUS_TXOK_Msk)
  66.         {
  67.             CAN0->STATUS &= ~CAN_STATUS_TXOK_Msk;    /* Clear TxOK status*/
  68.         }

  69.         /**************************/
  70.         /* Error Status interrupt */
  71.         /**************************/
  72.         if(CAN0->STATUS & CAN_STATUS_BOFF_Msk)
  73.         {
  74.             printf("BOFF INT\n") ;
  75.         }
  76.         else if(CAN0->STATUS & CAN_STATUS_EWARN_Msk)
  77.         {
  78.             printf("EWARN INT\n") ;
  79.         }
  80. //        else if((CAN0->ERR & CAN_ERR_TEC_Msk) != 0)
  81. //        {
  82. //            printf("Transmit error!\n") ;
  83. //        }
  84. //        else if((CAN0->ERR & CAN_ERR_REC_Msk) != 0)
  85. //        {
  86. //            printf("Receive error!\n") ;
  87. //        }
  88.     }
  89.     else if((u8IIDRstatus >= 0x1) || (u8IIDRstatus <= 0x20))
  90.     {
  91.         CAN_MsgInterrupt(CAN0, u8IIDRstatus);

  92.         CAN_CLR_INT_PENDING_BIT(CAN0, (u8IIDRstatus - 1)); /* Clear Interrupt Pending */
  93.     }

  94.     if(CAN0->WU_STATUS == 1)
  95.     {
  96.         printf("Wake-up from power down mode!\n");

  97.         CAN0->WU_STATUS = 0;    /* Write '0' to clear */
  98.     }

  99. }

  100. /*---------------------------------------------------------------------------------------------------------*/
  101. /* Reset message interface parameters                                                                      */
  102. /*---------------------------------------------------------------------------------------------------------*/
  103. void CAN_ResetIF(CAN_T *tCAN, uint8_t u8IF_Num)
  104. {
  105.     if(u8IF_Num > 1)
  106.         return;
  107.     tCAN->IF[u8IF_Num].CREQ     = 0x0;          // set bit15 for sending
  108.     tCAN->IF[u8IF_Num].CMASK    = 0x0;
  109.     tCAN->IF[u8IF_Num].MASK1    = 0x0;          // useless in basic mode
  110.     tCAN->IF[u8IF_Num].MASK2    = 0x0;          // useless in basic mode
  111.     tCAN->IF[u8IF_Num].ARB1     = 0x0;          // ID15~0
  112.     tCAN->IF[u8IF_Num].ARB2     = 0x0;          // MsgVal, eXt, xmt, ID28~16
  113.     tCAN->IF[u8IF_Num].MCON     = 0x0;          // DLC
  114.     tCAN->IF[u8IF_Num].DAT_A1   = 0x0;          // data0,1
  115.     tCAN->IF[u8IF_Num].DAT_A2   = 0x0;          // data2,3
  116.     tCAN->IF[u8IF_Num].DAT_B1   = 0x0;          // data4,5
  117.     tCAN->IF[u8IF_Num].DAT_B2   = 0x0;          // data6,7
  118. }

  119. /*---------------------------------------------------------------------------*/
  120. /*  Show Message Function                                                    */
  121. /*---------------------------------------------------------------------------*/
  122. void CAN_ShowMsg(STR_CANMSG_T* Msg)
  123. {
  124.     uint8_t i;

  125.     /* Show the message information */
  126.     printf("Read ID=0x%X, Type=%s, DLC=%d, Data=", Msg->Id, Msg->IdType ? "EXT" : "STD", Msg->DLC);
  127.     for(i = 0; i < Msg->DLC; i++)
  128.         printf("%X,", Msg->Data[i]);
  129.     printf("\n\n");
  130. }

  131. void SYS_Init(void)
  132. {
  133.     /*---------------------------------------------------------------------------------------------------------*/
  134.     /* Init System Clock                                                                                       */
  135.     /*---------------------------------------------------------------------------------------------------------*/

  136.     /* Enable Internal RC 22.1184MHz clock */
  137.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  138.     /* Waiting for Internal RC clock ready */
  139.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  140.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  141.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  142.     /* Enable external XTAL 12MHz clock */
  143.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  144.     /* Waiting for external XTAL clock ready */
  145.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  146.     /* Set core clock as PLL_CLOCK from PLL */
  147.     CLK_SetCoreClock(PLL_CLOCK);

  148.     /* Enable UART module clock */
  149.     CLK_EnableModuleClock(UART0_MODULE);

  150.     /* Enable CAN module clock */
  151.     CLK_EnableModuleClock(CAN0_MODULE);
  152.     //CLK_EnableModuleClock(CAN1_MODULE);

  153.     /* Select UART module clock source */
  154.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));

  155.     /*---------------------------------------------------------------------------------------------------------*/
  156.     /* Init I/O Multi-function                                                                                 */
  157.     /*---------------------------------------------------------------------------------------------------------*/

  158.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  159.     SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
  160.     SYS->GPB_MFP |= SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;

  161.     /* Set PD multi-function pins for CANTX0, CANRX0 */
  162.     SYS->GPD_MFP &= ~(SYS_GPD_MFP_PD6_Msk | SYS_GPD_MFP_PD7_Msk);
  163.     SYS->GPD_MFP |= SYS_GPD_MFP_PD6_CAN0_RXD | SYS_GPD_MFP_PD7_CAN0_TXD;

  164. }

  165. /*---------------------------------------------------------------------------------------------------------*/
  166. /* Init UART                                                                                               */
  167. /*---------------------------------------------------------------------------------------------------------*/
  168. void UART0_Init()
  169. {
  170.     /* Reset UART module */
  171.     SYS_ResetModule(UART0_RST);

  172.     /* Configure UART0 and set UART0 baud rate */
  173.     UART_Open(UART0, 115200);
  174. }

  175. /*---------------------------------------------------------------------------------------------------------*/
  176. /* Disable CAN Clock and Reset it                                                                          */
  177. /*---------------------------------------------------------------------------------------------------------*/
  178. void CAN_STOP(void)
  179. {
  180.     /* Disable CAN0 Clock and Reset it */
  181.     SYS_ResetModule(CAN0_RST);
  182.     CLK_DisableModuleClock(CAN0_MODULE);
  183. }

  184. /*----------------------------------------------------------------------------*/
  185. /*  Some description about how to create test environment                     */
  186. /*----------------------------------------------------------------------------*/
  187. void Note_Configure()
  188. {
  189.     printf("\n\n");
  190.     printf("+--------------------------------------------------------------------------+\n");
  191.     printf("|  About CAN sample code configure                                         |\n");
  192.     printf("+--------------------------------------------------------------------------+\n");
  193.     printf("|   The sample code provide a simple sample code for you study CAN         |\n");
  194.     printf("|   Before execute it, please check description as below                   |\n");
  195.     printf("|                                                                          |\n");
  196.     printf("|   1.CAN_TX and CAN_RX should be connected to your CAN transceiver        |\n");
  197.     printf("|   2.Using two module board and connect to the same CAN BUS               |\n");
  198.     printf("|   3.Check the terminal resistor of bus is connected                      |\n");
  199.     printf("|   4.Using UART0 as print message port                                    |\n");
  200.     printf("|                                                                          |\n");
  201.     printf("|  |--------|       |-----------|   CANBUS  |-----------|       |--------| |\n");
  202.     printf("|  |        |------>|           |<--------->|           |<------|        | |\n");
  203.     printf("|  |        |CAN_TX |    CAN    |   CAN_H   |   CAN     |CAN_TX |        | |\n");
  204.     printf("|  | M071M|       |Transceiver|           |Transceiver|       | M071M| |\n");
  205.     printf("|  |        |<------|           |<--------->|           |------>|        | |\n");
  206.     printf("|  |        |CAN_RX |           |   CAN_L   |           |CAN_RX |        | |\n");
  207.     printf("|  |--------|       |-----------|           |-----------|       |--------| |\n");
  208.     printf("|  |                                                            |          |\n");
  209.     printf("|  |                                                            |          |\n");
  210.     printf("|  V                                                            V          |\n");
  211.     printf("| UART0                                                         UART0      |\n");
  212.     printf("|(print message)                                          (print message)  |\n");
  213.     printf("+--------------------------------------------------------------------------+\n");
  214. }

  215. /*----------------------------------------------------------------------------*/
  216. /*  Check the real baud-rate                                                  */
  217. /*----------------------------------------------------------------------------*/
  218. void BaudRateCheck(uint32_t u32BaudRate, uint32_t u32RealBaudRate)
  219. {
  220.     /* Get Core Clock Frequency */
  221.     SystemCoreClockUpdate();

  222.     if(u32BaudRate != u32RealBaudRate)
  223.     {
  224.         printf("\nSet CAN baud-rate is fail\n");
  225.         printf("Real baud-rate value(bps): %d\n", u32RealBaudRate);
  226.         printf("CAN baud-rate calculation equation as below:\n");
  227.         printf("CAN baud-rate(bps) = Fin/(BPR+1)*(Tseg1+Tseg2+3)\n");
  228.         printf("where: Fin: System clock freq.(Hz)\n");
  229.         printf("       BRP: The baud rate prescale. It is composed of BRP (CAN_BTIME[5:0]) and BRPE (CAN_BRPE[3:0]).\n");
  230.         printf("       Tseg1: Time Segment before the sample point. You can set tseg1 (CAN_BTIME[11:8]).\n");
  231.         printf("       Tseg2: Time Segment after the sample point. You can set tseg2 (CAN_BTIME[14:12]).\n");

  232.         if(SystemCoreClock % u32BaudRate != 0)
  233.             printf("\nThe BPR does not calculate, the Fin must be a multiple of the CAN baud-rate.\n");

  234.         else
  235.             printf("\nThe BPR does not calculate, the (Fin/(CAN baud-rate)) must be a multiple of the (Tseg1+Tseg1+3).\n");
  236.     }
  237.     else
  238.         printf("\nReal baud-rate value(bps): %d\n", u32RealBaudRate);
  239. }

  240. /*----------------------------------------------------------------------------*/
  241. /*  Set the CAN speed                                                         */
  242. /*----------------------------------------------------------------------------*/
  243. void SelectCANSpeed(CAN_T  *tCAN)
  244. {
  245.     uint32_t unItem, BaudRate = 0, RealBaudRate = 0;

  246.     printf("Please select CAN speed you desired\n");
  247.     printf("[0] 1000Kbps\n");
  248.     printf("[1]  800Kbps\n");
  249.     printf("[2]  500Kbps\n");
  250.     printf("[3]  250Kbps\n");
  251.     printf("[4]  125Kbps\n");
  252.     printf("[5]  100Kbps\n");
  253.     printf("[6]   50Kbps\n");

  254.     unItem = GetChar();
  255.     printf("%c\n", unItem);

  256.     switch(unItem)
  257.     {
  258.         case '0':
  259.             BaudRate = 1000000;
  260.             RealBaudRate = CAN_Open(tCAN,  BaudRate, CAN_NORMAL_MODE);//Set target baud-rate and operation mode.
  261.             break;

  262.         case '1':
  263.             BaudRate = 800000;
  264.             RealBaudRate = CAN_Open(tCAN,  BaudRate, CAN_NORMAL_MODE);
  265.             break;

  266.         case '2':
  267.             BaudRate = 500000;
  268.             RealBaudRate = CAN_Open(tCAN,  BaudRate, CAN_NORMAL_MODE);
  269.             break;

  270.         case '3':
  271.             BaudRate = 250000;
  272.             RealBaudRate = CAN_Open(tCAN,  BaudRate, CAN_NORMAL_MODE);
  273.             break;

  274.         case '4':
  275.             BaudRate = 125000;
  276.             RealBaudRate = CAN_Open(tCAN,  BaudRate, CAN_NORMAL_MODE);
  277.             break;

  278.         case '5':
  279.             BaudRate = 100000;
  280.             RealBaudRate = CAN_Open(tCAN,  BaudRate, CAN_NORMAL_MODE);
  281.             break;

  282.         case '6':
  283.             BaudRate = 50000;
  284.             RealBaudRate = CAN_Open(tCAN,  BaudRate, CAN_NORMAL_MODE);
  285.             break;
  286.     }

  287.     /* Check the real baud-rate is OK */
  288.     BaudRateCheck(BaudRate, RealBaudRate);

  289. }

  290. /*----------------------------------------------------------------------------*/
  291. /*  Test Menu                                                                 */
  292. /*----------------------------------------------------------------------------*/
  293. void TestItem(void)
  294. {
  295.     printf("\n");
  296.     printf("+------------------------------------------------------------------ +\n");
  297.     printf("|  Nuvoton CAN BUS DRIVER DEMO                                      |\n");
  298.     printf("+-------------------------------------------------------------------+\n");
  299.     printf("|                                                                   |\n");
  300.     printf("|     Wake-up Test                                                  |\n");
  301.     printf("|     (At first, another module board should be set to              |\n");
  302.     printf("|     [CAN_NormalMode_Transmit] trigger the transmission)           |\n");
  303.     printf("|                                                                   |\n");
  304.     printf("+-------------------------------------------------------------------+\n");
  305. }

  306. /*----------------------------------------------------------------------------*/
  307. /*  Send Rx Msg by Normal Mode Function (With Message RAM)                    */
  308. /*----------------------------------------------------------------------------*/
  309. void WakeupTest(CAN_T *tCAN)
  310. {
  311.     /* Enable CAN interrupt and corresponding NVIC of CAN */
  312.     CAN_EnableInt(tCAN, CAN_CON_IE_Msk | CAN_CON_SIE_Msk);
  313.     NVIC_EnableIRQ(CAN0_IRQn);


  314.     if(CAN_SetRxMsg(tCAN, MSG(0), CAN_STD_ID, 0x7FF) == FALSE)
  315.     {
  316.         printf("Set Rx Msg Object failed\n");
  317.         return;
  318.     }

  319.     if(CAN_SetRxMsg(tCAN, MSG(5), CAN_EXT_ID, 0x12345) == FALSE)
  320.     {
  321.         printf("Set Rx Msg Object failed\n");
  322.         return;
  323.     }

  324.     if(CAN_SetRxMsg(tCAN, MSG(31), CAN_EXT_ID, 0x7FF01) == FALSE)
  325.     {
  326.         printf("Set Rx Msg Object failed\n");
  327.         return;
  328.     }

  329.     printf("Press any key to enter power down mode...\n");
  330.     GetChar();

  331.     /* Enable CAN wakeup function */
  332.     tCAN->WU_EN = CAN_WUEN_WAKUP_EN_Msk;

  333.     /* Enter to Power-down mode */
  334.     CLK_PowerDown();

  335.     GetChar();
  336. }

  337. /*---------------------------------------------------------------------------------------------------------*/
  338. /* MAIN function                                                                                           */
  339. /*---------------------------------------------------------------------------------------------------------*/
  340. int main(void)
  341. {
  342.     /* Unlock protected registers */
  343.     SYS_UnlockReg();

  344.     /* Init System, IP clock and multi-function I/O */
  345.     SYS_Init();

  346.     /* Init UART0 for printf */
  347.     UART0_Init();

  348.     /*---------------------------------------------------------------------------------------------------------*/
  349.     /* SAMPLE CODE                                                                                             */
  350.     /*---------------------------------------------------------------------------------------------------------*/

  351.     /* Some description about how to create test environment */
  352.     Note_Configure();

  353.     /* Configuring the Bit Timing */
  354.     SelectCANSpeed(CAN0);

  355.     /* Test Menu */
  356.     TestItem();

  357.     printf("This chip will be waked up from power down mode when detecting a transition.\n");
  358.     printf("The first transmission will success when the chip be woken.\n\n");

  359.     /* Send Rx Msg by Normal Mode Function (With Message RAM) and enter the power done mode*/
  360.     WakeupTest(CAN0);

  361.     /* Disable CAN */
  362.     CAN_Close(CAN0);

  363.     /* Disable CAN Clock and Reset it */
  364.     CAN_STOP();

  365.     while(1);

  366. }





 楼主| 21mengnan 发表于 2024-4-21 17:56 | 显示全部楼层
CAN的需求很多的,学好CAN使用方法很重要。
zhengshuai888 发表于 2024-4-21 19:28 来自手机 | 显示全部楼层
CAN接受数据中断来唤醒系统
jasontu 发表于 2024-4-26 14:04 | 显示全部楼层
m071應該沒有can bus才是
问天少年 发表于 2024-4-28 11:48 | 显示全部楼层
这个实用,可以做低功耗
黑心单片机 发表于 2024-4-28 14:42 | 显示全部楼层
外设唤醒非常实用
Henryko 发表于 2024-5-8 21:35 | 显示全部楼层
这个很常用做低功耗
您需要登录后才可以回帖 登录 | 注册

本版积分规则

88

主题

1151

帖子

1

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