[DemoCode下载] M0518使用I2C的唤醒功能

[复制链接]
1939|6
 楼主| yiyigirl2014 发表于 2017-5-29 13:47 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 2 $
  5. * $Date: 14/12/25 10:24a $
  6. * @brief
  7. *           Show how to wake up MCU from Power-down.
  8. *           This sample code needs to work with I2C_Wakeup_Slave.
  9. * @note
  10. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  11. *
  12. ******************************************************************************/
  13. #include <stdio.h>
  14. #include "M0518.h"

  15. #define PLLCON_SETTING      SYSCLK_PLLCON_50MHz_XTAL
  16. #define PLL_CLOCK           50000000


  17. /*---------------------------------------------------------------------------------------------------------*/
  18. /* Global variables                                                                                        */
  19. /*---------------------------------------------------------------------------------------------------------*/
  20. volatile uint8_t g_u8DeviceAddr;
  21. volatile uint8_t g_au8TxData[3];
  22. volatile uint8_t g_u8RxData;
  23. volatile uint8_t g_u8DataLen;
  24. volatile uint8_t g_u8EndFlag = 0;

  25. typedef void (*I2C_FUNC)(uint32_t u32Status);

  26. static volatile I2C_FUNC s_I2C0HandlerFn = NULL;

  27. /*---------------------------------------------------------------------------------------------------------*/
  28. /*  I2C0 IRQ Handler                                                                                       */
  29. /*---------------------------------------------------------------------------------------------------------*/
  30. void I2C0_IRQHandler(void)
  31. {
  32.     uint32_t u32Status;

  33.     u32Status = I2C_GET_STATUS(I2C0);

  34.     if(I2C_GET_TIMEOUT_FLAG(I2C0))
  35.     {
  36.         /* Clear I2C0 Timeout Flag */
  37.         I2C_ClearTimeoutFlag(I2C0);
  38.     }
  39.     else
  40.     {
  41.         if(s_I2C0HandlerFn != NULL)
  42.             s_I2C0HandlerFn(u32Status);
  43.     }
  44. }

  45. /*---------------------------------------------------------------------------------------------------------*/
  46. /*  I2C Master Tx Wake Up Callback Function                                                                        */
  47. /*---------------------------------------------------------------------------------------------------------*/
  48. void I2C_MasterTxWakeup(uint32_t u32Status)
  49. {
  50.     if(u32Status == 0x08)                       /* START has been transmitted */
  51.     {
  52.         I2C_SET_DATA(I2C0, (g_u8DeviceAddr << 1));    /* Write SLA+W to Register I2CDAT */
  53.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  54.     }
  55.     else if(u32Status == 0x18)                  /* SLA+W has been transmitted and ACK has been received */
  56.     {
  57.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STO_SI);
  58.         g_u8EndFlag = 1;
  59.     }
  60.     else if(u32Status == 0x20)                  /* SLA+W has been transmitted and NOT ACK has been received */
  61.     {
  62.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STO_SI);
  63.         g_u8EndFlag = 1;
  64.     }
  65.     else
  66.     {
  67.         /* TO DO */
  68.         printf("Status 0x%x is NOT processed\n", u32Status);
  69.     }
  70. }
  71. /*---------------------------------------------------------------------------------------------------------*/
  72. /*  I2C Rx Callback Function                                                                               */
  73. /*---------------------------------------------------------------------------------------------------------*/
  74. void I2C_MasterRx(uint32_t u32Status)
  75. {
  76.     if(u32Status == 0x08)                       /* START has been transmitted and prepare SLA+W */
  77.     {
  78.         I2C_SET_DATA(I2C0, (g_u8DeviceAddr << 1));    /* Write SLA+W to Register I2CDAT */
  79.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  80.     }
  81.     else if(u32Status == 0x18)                  /* SLA+W has been transmitted and ACK has been received */
  82.     {
  83.         I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
  84.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  85.     }
  86.     else if(u32Status == 0x20)                  /* SLA+W has been transmitted and NACK has been received */
  87.     {
  88.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STA_STO_SI);
  89.     }
  90.     else if(u32Status == 0x28)                  /* DATA has been transmitted and ACK has been received */
  91.     {
  92.         if(g_u8DataLen != 2)
  93.         {
  94.             I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
  95.             I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  96.         }
  97.         else
  98.         {
  99.             I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STA_SI);
  100.         }
  101.     }
  102.     else if(u32Status == 0x10)                  /* Repeat START has been transmitted and prepare SLA+R */
  103.     {
  104.         I2C_SET_DATA(I2C0, ((g_u8DeviceAddr << 1) | 0x01)); /* Write SLA+R to Register I2CDAT */
  105.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  106.     }
  107.     else if(u32Status == 0x40)                  /* SLA+R has been transmitted and ACK has been received */
  108.     {
  109.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  110.     }
  111.     else if(u32Status == 0x58)                  /* DATA has been received and NACK has been returned */
  112.     {
  113.         g_u8RxData = (unsigned char) I2C_GET_DATA(I2C0);
  114.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STO_SI);
  115.         g_u8EndFlag = 1;
  116.     }
  117.     else
  118.     {
  119.         /* TO DO */
  120.         printf("Status 0x%x is NOT processed\n", u32Status);
  121.     }
  122. }

  123. /*---------------------------------------------------------------------------------------------------------*/
  124. /*  I2C Tx Callback Function                                                                               */
  125. /*---------------------------------------------------------------------------------------------------------*/
  126. void I2C_MasterTx(uint32_t u32Status)
  127. {
  128.     if(u32Status == 0x08)                       /* START has been transmitted */
  129.     {
  130.         I2C_SET_DATA(I2C0, g_u8DeviceAddr << 1);    /* Write SLA+W to Register I2CDAT */
  131.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  132.     }
  133.     else if(u32Status == 0x18)                  /* SLA+W has been transmitted and ACK has been received */
  134.     {
  135.         I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
  136.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  137.     }
  138.     else if(u32Status == 0x20)                  /* SLA+W has been transmitted and NACK has been received */
  139.     {
  140.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STA_STO_SI);
  141.     }
  142.     else if(u32Status == 0x28)                  /* DATA has been transmitted and ACK has been received */
  143.     {
  144.         if(g_u8DataLen != 3)
  145.         {
  146.             I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
  147.             I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  148.         }
  149.         else
  150.         {
  151.             I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STO_SI);
  152.             g_u8EndFlag = 1;
  153.         }
  154.     }
  155.     else
  156.     {
  157.         /* TO DO */
  158.         printf("Status 0x%x is NOT processed\n", u32Status);
  159.     }
  160. }

  161. void SYS_Init(void)
  162. {
  163.     /*---------------------------------------------------------------------------------------------------------*/
  164.     /* Init System Clock                                                                                       */
  165.     /*---------------------------------------------------------------------------------------------------------*/

  166.     /* Enable Internal RC 22.1184MHz clock */
  167.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  168.     /* Waiting for Internal RC clock ready */
  169.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

  172.     /* Enable external XTAL 12MHz clock */
  173.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  174.     /* Waiting for external XTAL clock ready */
  175.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  176.     /* Set core clock as PLL_CLOCK from PLL */
  177.     CLK_SetCoreClock(PLL_CLOCK);

  178.     /* Enable UART module clock */
  179.     CLK_EnableModuleClock(UART0_MODULE);

  180.     /* Enable I2C0 module clock */
  181.     CLK_EnableModuleClock(I2C0_MODULE);

  182.     /* Select UART module clock source */
  183.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));


  184.     /*---------------------------------------------------------------------------------------------------------*/
  185.     /* Init I/O Multi-function                                                                                 */
  186.     /*---------------------------------------------------------------------------------------------------------*/

  187.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  188.     SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;

  189.     /* Set GPA multi-function pins for I2C0 SDA and SCL */
  190.     SYS->GPA_MFP = SYS_GPA_MFP_PA8_I2C0_SDA | SYS_GPA_MFP_PA9_I2C0_SCL;
  191. }

  192. void UART0_Init()
  193. {
  194.     /*---------------------------------------------------------------------------------------------------------*/
  195.     /* Init UART                                                                                               */
  196.     /*---------------------------------------------------------------------------------------------------------*/
  197.     /* Reset IP */
  198.     SYS_ResetModule(UART0_RST);

  199.     /* Configure UART0 and set UART0 Baudrate */
  200.     UART_Open(UART0, 115200);
  201. }

  202. void I2C0_Init(void)
  203. {
  204.     /* Open I2C module and set bus clock */
  205.     I2C_Open(I2C0, 100000);

  206.     /* Get I2C0 Bus Clock */
  207.     printf("I2C clock %d Hz\n", I2C_GetBusClockFreq(I2C0));

  208.     /* Set I2C 4 Slave Addresses */
  209.     I2C_SetSlaveAddr(I2C0, 0, 0x15, 0);   /* Slave Address : 0x15 */
  210.     I2C_SetSlaveAddr(I2C0, 1, 0x35, 0);   /* Slave Address : 0x35 */
  211.     I2C_SetSlaveAddr(I2C0, 2, 0x55, 0);   /* Slave Address : 0x55 */
  212.     I2C_SetSlaveAddr(I2C0, 3, 0x75, 0);   /* Slave Address : 0x75 */

  213.     /* Enable I2C interrupt */
  214.     I2C_EnableInt(I2C0);
  215.     NVIC_EnableIRQ(I2C0_IRQn);
  216. }

  217. void I2C0_Close(void)
  218. {
  219.     /* Disable I2C0 interrupt and clear corresponding NVIC bit */
  220.     I2C_DisableInt(I2C0);
  221.     NVIC_DisableIRQ(I2C0_IRQn);

  222.     /* Disable I2C0 and close I2C0 clock */
  223.     I2C_Close(I2C0);
  224.     CLK_DisableModuleClock(I2C0_MODULE);

  225. }

  226. int32_t I2C0_Read_Write_SLAVE(uint8_t slvaddr)
  227. {
  228.     uint32_t i;

  229.     g_u8DeviceAddr = slvaddr;

  230.     for(i = 0; i < 0x100; i++)
  231.     {
  232.         g_au8TxData[0] = (uint8_t)((i & 0xFF00) >> 8);
  233.         g_au8TxData[1] = (uint8_t)(i & 0x00FF);
  234.         g_au8TxData[2] = (uint8_t)(g_au8TxData[1] + 3);

  235.         g_u8DataLen = 0;
  236.         g_u8EndFlag = 0;

  237.         /* I2C function to write data to slave */
  238.         s_I2C0HandlerFn = (I2C_FUNC)I2C_MasterTx;

  239.         /* I2C as master sends START signal */
  240.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STA);

  241.         /* Wait I2C Tx Finish */
  242.         while(g_u8EndFlag == 0);
  243.         g_u8EndFlag = 0;

  244.         /* I2C function to read data from slave */
  245.         s_I2C0HandlerFn = (I2C_FUNC)I2C_MasterRx;

  246.         g_u8DataLen = 0;
  247.         g_u8DeviceAddr = slvaddr;

  248.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STA);

  249.         /* Wait I2C Rx Finish */
  250.         while(g_u8EndFlag == 0);

  251.         /* Compare data */
  252.         if(g_u8RxData != g_au8TxData[2])
  253.         {
  254.             printf("I2C Byte Write/Read Failed, Data 0x%x\n", g_u8RxData);
  255.             return -1;
  256.         }
  257.     }
  258.     printf("Master Access Slave (0x%X) Test OK\n", slvaddr);
  259.     return 0;
  260. }

  261. /*---------------------------------------------------------------------------------------------------------*/
  262. /*  Main Function                                                                                          */
  263. /*---------------------------------------------------------------------------------------------------------*/
  264. int32_t main(void)
  265. {
  266.     /* Unlock protected registers */
  267.     SYS_UnlockReg();

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

  270.     /* Init UART0 for printf */
  271.     UART0_Init();

  272.     /* Lock protected registers */
  273.     SYS_LockReg();

  274.     /*
  275.         This sample code sets I2C bus clock to 100kHz. After wake-up, then accesses Slave with Byte
  276.         Write and Byte Read operations, and check if the read data is equal to the programmed data.
  277.     */

  278.     printf("+------------------------------------------------------------------------+\n");
  279.     printf("| M0518 I2C Driver Sample Code (Master) for wake-up & access Slave test |\n");
  280.     printf("|                                                                        |\n");
  281.     printf("| I2C Master (I2C0) <---> I2C Slave(I2C0)                                |\n");
  282.     printf("+------------------------------------------------------------------------+\n");

  283.     printf("Configure I2C0 as a master.\n");
  284.     printf("The I/O connection for I2C0:\n");
  285.     printf("I2C0_SDA(P3.4), I2C0_SCL(P3.5)\n");

  286.     /* Init I2C0 to access Slave */
  287.     I2C0_Init();

  288.     printf("\n");
  289.     printf("Check I2C slave at power down status.\n");
  290.     printf("Press any key to Wake up slave.\n");
  291.     getchar();

  292.     /* Set the Slave address to wake-up*/
  293.     g_u8DeviceAddr = 0x15;

  294.     /* I2C function to wake-up slave*/
  295.     s_I2C0HandlerFn = (I2C_FUNC)I2C_MasterTxWakeup;

  296.     /* Send a START condition to bus */
  297.     I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STA);
  298.     while(g_u8EndFlag == 0);

  299.     printf("\n");
  300.     printf("Press any key to continue access slave.\n");
  301.     getchar();

  302.     /*Access to the corresponding address Slave*/
  303.     printf("\n");
  304.     printf(" == No Mask Address ==\n");
  305.     I2C0_Read_Write_SLAVE(0x15);
  306.     I2C0_Read_Write_SLAVE(0x35);
  307.     I2C0_Read_Write_SLAVE(0x55);
  308.     I2C0_Read_Write_SLAVE(0x75);
  309.     printf("SLAVE Address test OK.\n");


  310.     /* Access Slave with address mask */
  311.     printf("\n");
  312.     printf(" == Mask Address ==\n");
  313.     I2C0_Read_Write_SLAVE(0x15 & ~0x01);
  314.     I2C0_Read_Write_SLAVE(0x35 & ~0x04);
  315.     I2C0_Read_Write_SLAVE(0x55 & ~0x01);
  316.     I2C0_Read_Write_SLAVE(0x75 & ~0x04);
  317.     printf("SLAVE Address Mask test OK.\n");

  318.     s_I2C0HandlerFn = NULL;

  319.     /* Close I2C0 */
  320.     I2C0_Close();

  321.     return 0;
  322. }


 楼主| yiyigirl2014 发表于 2017-5-29 13:48 | 显示全部楼层
  1. /**************************************************************************//**
  2. * @file     main.c
  3. * @version  V3.00
  4. * $Revision: 3 $
  5. * $Date: 14/12/25 10:24a $
  6. * @brief
  7. *           Show how to wake up MCU from Power-down mode through I2C interface.
  8. *           This sample code needs to work with I2C_Wakeup_Master.
  9. * @note
  10. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  11. *
  12. ******************************************************************************/
  13. #include <stdio.h>
  14. #include "M0518.h"

  15. #define PLLCON_SETTING      SYSCLK_PLLCON_50MHz_XTAL
  16. #define PLL_CLOCK           50000000

  17. uint32_t slave_buff_addr;
  18. uint8_t g_u8SlvData[256];
  19. uint8_t g_au8RxData[3];
  20. /*---------------------------------------------------------------------------------------------------------*/
  21. /* Global variables                                                                                        */
  22. /*---------------------------------------------------------------------------------------------------------*/
  23. volatile uint8_t g_u8DeviceAddr;
  24. volatile uint8_t g_au8TxData[3];
  25. volatile uint8_t g_u8RxData;
  26. volatile uint8_t g_u8DataLen;

  27. typedef void (*I2C_FUNC)(uint32_t u32Status);

  28. static I2C_FUNC s_I2C0HandlerFn = NULL;

  29. /*---------------------------------------------------------------------------------------------------------*/
  30. /*  I2C0 IRQ Handler                                                                                       */
  31. /*---------------------------------------------------------------------------------------------------------*/
  32. void I2C0_IRQHandler(void)
  33. {
  34.     uint32_t u32Status;

  35.     /* Check I2C Wake-up interrupt flag set or not */
  36.     if(I2C_GET_WAKEUP_FLAG(I2C0))
  37.     {
  38.         /* Clear I2C Wake-up interrupt flag */
  39.         I2C_CLEAR_WAKEUP_FLAG(I2C0);
  40.         printf("I2C0 WAKE INT 0x%x\n", I2C0->I2CWKUPSTS);
  41.         return;
  42.     }

  43.     u32Status = I2C_GET_STATUS(I2C0);

  44.     if(I2C_GET_TIMEOUT_FLAG(I2C0))
  45.     {
  46.         /* Clear I2C0 Timeout Flag */
  47.         I2C_ClearTimeoutFlag(I2C0);
  48.     }
  49.     else
  50.     {
  51.         if(s_I2C0HandlerFn != NULL)
  52.             s_I2C0HandlerFn(u32Status);
  53.     }
  54. }
  55. /*---------------------------------------------------------------------------------------------------------*/
  56. /*  Power Wake-up IRQ Handler                                                                                       */
  57. /*---------------------------------------------------------------------------------------------------------*/
  58. void PWRWU_IRQHandler(void)
  59. {
  60.     /* Check system power down mode wake-up interrupt flag */
  61.     if(((CLK->PWRCON) & CLK_PWRCON_PD_WU_STS_Msk) != 0)
  62.     {
  63.         /* Clear system power down wake-up interrupt flag */
  64.         CLK->PWRCON |= CLK_PWRCON_PD_WU_STS_Msk;

  65.         printf("Power-down Wake-up INT 0x%x\n", ((CLK->PWRCON) & CLK_PWRCON_PD_WU_STS_Msk));
  66.     }
  67. }

  68. /*---------------------------------------------------------------------------------------------------------*/
  69. /*  I2C Slave Transmit/Receive Callback Function                                                                               */
  70. /*---------------------------------------------------------------------------------------------------------*/
  71. void I2C_SlaveTRx(uint32_t u32Status)
  72. {
  73.     if(u32Status == 0x60)                       /* Own SLA+W has been receive; ACK has been return */
  74.     {
  75.         g_u8DataLen = 0;
  76.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
  77.     }
  78.     else if(u32Status == 0x80)                 /* Previously address with own SLA address
  79.                                                    Data has been received; ACK has been returned*/
  80.     {
  81.         g_au8RxData[g_u8DataLen] = (unsigned char)I2C_GET_DATA(I2C0);
  82.         g_u8DataLen++;

  83.         if(g_u8DataLen == 2)
  84.         {
  85.             slave_buff_addr = (g_au8RxData[0] << 8) + g_au8RxData[1];
  86.         }
  87.         if(g_u8DataLen == 3)
  88.         {
  89.             g_u8SlvData[slave_buff_addr] = g_au8RxData[2];
  90.             g_u8DataLen = 0;
  91.         }
  92.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
  93.     }
  94.     else if(u32Status == 0xA8)                  /* Own SLA+R has been receive; ACK has been return */
  95.     {
  96.         I2C_SET_DATA(I2C0, g_u8SlvData[slave_buff_addr]);
  97.         slave_buff_addr++;
  98.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
  99.     }
  100.     else if(u32Status == 0xC0)                 /* Data byte or last data in I2CDAT has been transmitted
  101.                                                    Not ACK has been received */
  102.     {
  103.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
  104.     }
  105.     else if(u32Status == 0x88)                 /* Previously addressed with own SLA address; NOT ACK has
  106.                                                    been returned */
  107.     {
  108.         g_u8DataLen = 0;
  109.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
  110.     }
  111.     else if(u32Status == 0xA0)                 /* A STOP or repeated START has been received while still
  112.                                                    addressed as Slave/Receiver*/
  113.     {
  114.         g_u8DataLen = 0;
  115.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
  116.     }
  117.     else
  118.     {
  119.         /* TO DO */
  120.         printf("Status 0x%x is NOT processed\n", u32Status);
  121.     }
  122. }

  123. void SYS_Init(void)
  124. {
  125.     /*---------------------------------------------------------------------------------------------------------*/
  126.     /* Init System Clock                                                                                       */
  127.     /*---------------------------------------------------------------------------------------------------------*/

  128.     /* Enable Internal RC 22.1184MHz clock */
  129.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  130.     /* Waiting for Internal RC clock ready */
  131.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

  134.     /* Enable external XTAL 12MHz clock */
  135.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  136.     /* Waiting for external XTAL clock ready */
  137.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  138.     /* Set core clock as PLL_CLOCK from PLL */
  139.     CLK_SetCoreClock(PLL_CLOCK);

  140.     /* Enable UART module clock */
  141.     CLK_EnableModuleClock(UART0_MODULE);

  142.     /* Enable I2C0 module clock */
  143.     CLK_EnableModuleClock(I2C0_MODULE);

  144.     /* Select UART module clock source */
  145.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));


  146.     /*---------------------------------------------------------------------------------------------------------*/
  147.     /* Init I/O Multi-function                                                                                 */
  148.     /*---------------------------------------------------------------------------------------------------------*/

  149.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  150.     SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;

  151.     /* Set GPA multi-function pins for I2C0 SDA and SCL */
  152.     SYS->GPA_MFP = SYS_GPA_MFP_PA8_I2C0_SDA | SYS_GPA_MFP_PA9_I2C0_SCL;
  153. }

  154. void UART0_Init()
  155. {
  156.     /*---------------------------------------------------------------------------------------------------------*/
  157.     /* Init UART                                                                                               */
  158.     /*---------------------------------------------------------------------------------------------------------*/
  159.     /* Reset IP */
  160.     SYS_ResetModule(UART0_RST);

  161.     /* Configure UART0 and set UART0 Baudrate */
  162.     UART_Open(UART0, 115200);
  163. }

  164. void I2C0_Init(void)
  165. {
  166.     /* Open I2C module and set bus clock */
  167.     I2C_Open(I2C0, 100000);

  168.     /* Get I2C0 Bus Clock */
  169.     printf("I2C clock %d Hz\n", I2C_GetBusClockFreq(I2C0));

  170.     /* Set I2C 4 Slave Addresses */
  171.     I2C_SetSlaveAddr(I2C0, 0, 0x15, 0);   /* Slave Address : 0x15 */
  172.     I2C_SetSlaveAddr(I2C0, 1, 0x35, 0);   /* Slave Address : 0x35 */
  173.     I2C_SetSlaveAddr(I2C0, 2, 0x55, 0);   /* Slave Address : 0x55 */
  174.     I2C_SetSlaveAddr(I2C0, 3, 0x75, 0);   /* Slave Address : 0x75 */

  175.     /* Set I2C 4 Slave Addresses Mask */
  176.     I2C_SetSlaveAddrMask(I2C0, 0, 0x01);
  177.     I2C_SetSlaveAddrMask(I2C0, 1, 0x04);
  178.     I2C_SetSlaveAddrMask(I2C0, 2, 0x01);
  179.     I2C_SetSlaveAddrMask(I2C0, 3, 0x04);

  180.     /* Enable I2C interrupt */
  181.     I2C_EnableInt(I2C0);
  182.     NVIC_EnableIRQ(I2C0_IRQn);
  183. }

  184. void I2C0_Close(void)
  185. {
  186.     /* Disable I2C0 interrupt and clear corresponding NVIC bit */
  187.     I2C_DisableInt(I2C0);
  188.     NVIC_DisableIRQ(I2C0_IRQn);

  189.     /* Disable I2C0 and close I2C0 clock */
  190.     I2C_Close(I2C0);
  191.     CLK_DisableModuleClock(I2C0_MODULE);
  192. }

  193. /*---------------------------------------------------------------------------------------------------------*/
  194. /*  Main Function                                                                                          */
  195. /*---------------------------------------------------------------------------------------------------------*/
  196. int32_t main(void)
  197. {
  198.     uint32_t i;

  199.     /* Unlock protected registers */
  200.     SYS_UnlockReg();

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

  203.     /* Init UART0 for printf */
  204.     UART0_Init();

  205.     /* Lock protected registers */
  206.     SYS_LockReg();;

  207.     /*
  208.         This sample code is I2C SLAVE mode and it simulates EEPROM function
  209.     */
  210.     printf("\n");
  211.     printf("+----------------------------------------------------------------------+\n");
  212.     printf("| M0518 I2C Driver Sample Code (Slave) for wake-up & access Slave test |\n");
  213.     printf("|                                                                      |\n");
  214.     printf("| I2C Master (I2C0) <---> I2C Slave(I2C0)                              |\n");
  215.     printf("+----------------------------------------------------------------------+\n");

  216.     printf("Configure I2C0 as a slave.\n");
  217.     printf("The I/O connection for I2C0:\n");
  218.     printf("I2C0_SDA(P3.4), I2C0_SCL(P3.5)\n");

  219.     /* Init I2C0 */
  220.     I2C0_Init();

  221.     /* Set I2C0 enter Not Address SLAVE mode */
  222.     I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);

  223.     /* Unlock protected registers */
  224.     SYS_UnlockReg();

  225.     /* Enable power wake-up interrupt */
  226.     CLK->PWRCON |= CLK_PWRCON_PD_WU_INT_EN_Msk;
  227.     NVIC_EnableIRQ(PWRWU_IRQn);

  228.     /* Enable I2C wake-up */
  229.     I2C_EnableWakeup(I2C0);

  230.     /* Enable Chip enter power down mode */
  231.     CLK->PWRCON |= CLK_PWRCON_PD_WAIT_CPU_Msk;

  232.     /* Processor use deep sleep */
  233.     SCB->SCR = SCB_SCR_SLEEPDEEP_Msk;

  234.     /* System power down enable */
  235.     CLK->PWRCON |= CLK_PWRCON_PWR_DOWN_EN_Msk;

  236.     printf("\n");
  237.     printf("Enter PD 0x%x 0x%x\n", I2C0->I2CON , I2C0->I2CSTATUS);
  238.     printf("\n");
  239.     printf("CHIP enter power down status.\n");
  240.     /* Waiting for UART printf finish*/
  241.     while(((UART0->FSR) & UART_FSR_TE_FLAG_Msk) == 0);

  242.     if(((I2C0->I2CON)&I2C_I2CON_SI_Msk) != 0)
  243.     {
  244.         I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
  245.     }

  246.     /*  Use WFI instruction to idle the CPU. NOTE:
  247.         If ICE is attached, system will wakeup immediately because ICE is a wakeup event. */
  248.     __WFI();
  249.     __NOP();
  250.     __NOP();
  251.     __NOP();

  252.     /* Disable power wake-up interrupt */
  253.     CLK->PWRCON &= ~CLK_PWRCON_PD_WU_INT_EN_Msk;
  254.     NVIC_DisableIRQ(PWRWU_IRQn);

  255.     /* Lock protected registers */
  256.     SYS_LockReg();

  257.     printf("\n");
  258.     printf("Slave wake-up from power down status.\n");

  259.     for(i = 0; i < 0x100; i++)
  260.     {
  261.         g_u8SlvData[i] = 0;
  262.     }

  263.     /* I2C function to Transmit/Receive data as slave */
  264.     s_I2C0HandlerFn = I2C_SlaveTRx;

  265.     printf("\n");
  266.     printf("Slave Waiting for receiving data.\n");

  267.     while(1);
  268. }


 楼主| yiyigirl2014 发表于 2017-5-29 13:49 | 显示全部楼层
当系统休眠后还可以通过通信接口唤醒,非常适用
zhuomuniao110 发表于 2017-5-29 18:35 | 显示全部楼层
充分利用了串口的调试功能。
 楼主| yiyigirl2014 发表于 2017-6-5 19:18 | 显示全部楼层
必须的充分利用,不用再搞个显示屏了。
langziwuliao 发表于 2017-6-5 20:40 | 显示全部楼层
太长了,我居然没看完~
就算看完了也看不懂,哈哈
gejigeji521 发表于 2017-6-6 11:56 | 显示全部楼层
看的我头都大了,不过略懂了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

230

主题

3676

帖子

10

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