[DemoCode下载] M451通过I2C实现收发

[复制链接]
1066|7
 楼主| dongnanxibei 发表于 2017-7-19 19:47 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V0.10
  4. * $Revision: 12 $
  5. * $Date: 15/09/02 10:04a $
  6. * @brief
  7. *           Configure SPI1 as I2S Master mode and demonstrate how I2S works in Master mode.
  8. *           This sample code needs to work with I2S_Slave.
  9. * @note
  10. * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
  11. *
  12. ******************************************************************************/
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "M451Series.h"

  16. #define PLL_CLOCK           72000000

  17. uint32_t g_u32TxValue;
  18. uint32_t g_u32DataCount;

  19. /* Function prototype declaration */
  20. void SYS_Init(void);

  21. /*---------------------------------------------------------------------------------------------------------*/
  22. /*  Main Function                                                                                          */
  23. /*---------------------------------------------------------------------------------------------------------*/
  24. int32_t main(void)
  25. {
  26.     uint32_t u32RxValue1, u32RxValue2;

  27.     /* Unlock protected registers */
  28.     SYS_UnlockReg();
  29.     /* Init System, IP clock and multi-function I/O. */
  30.     SYS_Init();
  31.     /* Lock protected registers */
  32.     SYS_LockReg();

  33.     /* Reset UART0 module */
  34.     SYS_ResetModule(UART0_RST);
  35.     /* Init UART0 to 115200-8n1 for printing messages */
  36.     UART_Open(UART0, 115200);

  37.     printf("+-----------------------------------------------------------+\n");
  38.     printf("|            I2S Driver Sample Code (master mode)           |\n");
  39.     printf("+-----------------------------------------------------------+\n");
  40.     printf("  I2S configuration:\n");
  41.     printf("      Sample rate 16 kHz\n");
  42.     printf("      Word width 16 bits\n");
  43.     printf("      Stereo mode\n");
  44.     printf("      I2S format\n");
  45.     printf("      TX value: 0x55005501, 0x55025503, ..., 0x55FE55FF, wraparound\n");
  46.     printf("  The I/O connection for I2S1 (SPI1):\n");
  47.     printf("      I2S1_LRCLK (PA4)\n      I2S1_BCLK(PA7)\n");
  48.     printf("      I2S1_DI (PA6)\n      I2S1_DO (PA5)\n\n");
  49.     printf("  NOTE: Connect with a I2S slave device.\n");
  50.     printf("        This sample code will transmit a TX value 50000 times, and then change to the next TX value.\n");
  51.     printf("        When TX value or the received value changes, the new TX value or the current TX value and the new received value will be printed.\n");
  52.     printf("  Press any key to start ...");
  53.     getchar();
  54.     printf("\n");

  55.     /* Master mode, 16-bit word width, stereo mode, I2S format. Set TX and RX FIFO threshold to middle value. */
  56.     I2S_Open(SPI1, I2S_MODE_MASTER, 16000, I2S_DATABIT_16, I2S_STEREO, I2S_FORMAT_I2S);

  57.     /* Initiate data counter */
  58.     g_u32DataCount = 0;
  59.     /* Initiate TX value and RX value */
  60.     g_u32TxValue = 0x55005501;
  61.     u32RxValue1 = 0;
  62.     u32RxValue2 = 0;
  63.     /* Enable TX threshold level interrupt */
  64.     I2S_EnableInt(SPI1, I2S_FIFO_TXTH_INT_MASK);
  65.     NVIC_EnableIRQ(SPI1_IRQn);

  66.     printf("Start I2S ...\nTX value: 0x%X\n", g_u32TxValue);

  67.     while(1)
  68.     {
  69.         /* Check RX FIFO empty flag */
  70.         if((SPI1->I2SSTS & SPI_I2SSTS_RXEMPTY_Msk) == 0)
  71.         {
  72.             /* Read RX FIFO */
  73.             u32RxValue2 = I2S_READ_RX_FIFO(SPI1);
  74.             if(u32RxValue1 != u32RxValue2)
  75.             {
  76.                 u32RxValue1 = u32RxValue2;
  77.                 /* If received value changes, print the current TX value and the new received value. */
  78.                 printf("TX value: 0x%X;  RX value: 0x%X\n", g_u32TxValue, u32RxValue1);
  79.             }
  80.         }
  81.         if(g_u32DataCount >= 50000)
  82.         {
  83.             g_u32TxValue = 0x55005500 | ((g_u32TxValue + 0x00020002) & 0x00FF00FF); /* g_u32TxValue: 0x55005501, 0x55025503, ..., 0x55FE55FF */
  84.             printf("TX value: 0x%X\n", g_u32TxValue);
  85.             g_u32DataCount = 0;
  86.         }
  87.     }
  88. }

  89. void SYS_Init(void)
  90. {
  91.     /*---------------------------------------------------------------------------------------------------------*/
  92.     /* Init System Clock                                                                                       */
  93.     /*---------------------------------------------------------------------------------------------------------*/

  94.     /* Enable HIRC clock */
  95.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  96.     /* Waiting for HIRC clock ready */
  97.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  98.     /* Switch HCLK clock source to HIRC */
  99.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  100.     /* Enable HXT clock (external XTAL 12MHz) */
  101.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  102.     /* Wait for HXT clock ready */
  103.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  104.     /* Configure PLL */
  105.     CLK_EnablePLL(CLK_PLLCTL_PLLSRC_HXT, PLL_CLOCK);

  106.     /* Switch HCLK clock source to PLL */
  107.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(1));

  108.     /* Select UART module clock source as HXT and UART module clock divider as 1 */
  109.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));

  110.     /* Select PCLK1 as the clock source of SPI1 */
  111.     CLK_SetModuleClock(SPI1_MODULE, CLK_CLKSEL2_SPI1SEL_PCLK1, MODULE_NoMsk);

  112.     /* Enable peripheral clock */
  113.     CLK_EnableModuleClock(UART0_MODULE);
  114.     CLK_EnableModuleClock(SPI1_MODULE);

  115.     /* Update System Core Clock */
  116.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CyclesPerUs automatically. */
  117.     SystemCoreClockUpdate();


  118.     /*---------------------------------------------------------------------------------------------------------*/
  119.     /* Init I/O Multi-function                                                                                 */
  120.     /*---------------------------------------------------------------------------------------------------------*/
  121.     /* Set PD multi-function pins for UART0 RXD and TXD */
  122.     SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
  123.     SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);

  124.     /* Configure SPI1 related multi-function pins. */
  125.     /* GPA[7:4] : SPI1_CLK (I2S1_BCLK), SPI1_MISO (I2S1_DI), SPI1_MOSI (I2S1_DO), SPI1_SS (I2S1_LRCLK). */
  126.     SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA4MFP_Msk | SYS_GPA_MFPL_PA5MFP_Msk | SYS_GPA_MFPL_PA6MFP_Msk | SYS_GPA_MFPL_PA7MFP_Msk);
  127.     SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA4MFP_SPI1_SS | SYS_GPA_MFPL_PA5MFP_SPI1_MOSI | SYS_GPA_MFPL_PA6MFP_SPI1_MISO | SYS_GPA_MFPL_PA7MFP_SPI1_CLK);

  128. }

  129. void SPI1_IRQHandler()
  130. {
  131.     /* Write 2 TX values to TX FIFO */
  132.     I2S_WRITE_TX_FIFO(SPI1, g_u32TxValue);
  133.     I2S_WRITE_TX_FIFO(SPI1, g_u32TxValue);
  134.     g_u32DataCount += 2;
  135. }

  136. /*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/


 楼主| dongnanxibei 发表于 2017-7-19 19:47 | 显示全部楼层
  1. /**************************************************************************//**
  2. * @file     main.c
  3. * @version  V0.10
  4. * $Revision: 11 $
  5. * $Date: 15/09/02 10:04a $
  6. * @brief
  7. *           Configure SPI1 as I2S Slave mode and demonstrate how I2S works in Slave mode.
  8. *           This sample code needs to work with I2S_Master.
  9. * @note
  10. * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
  11. *
  12. ******************************************************************************/
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "M451Series.h"

  16. #define PLL_CLOCK           72000000

  17. uint32_t g_u32TxValue;
  18. uint32_t g_u32DataCount;

  19. /* Function prototype declaration */
  20. void SYS_Init(void);

  21. /*---------------------------------------------------------------------------------------------------------*/
  22. /*  Main Function                                                                                          */
  23. /*---------------------------------------------------------------------------------------------------------*/
  24. int32_t main(void)
  25. {
  26.     uint32_t u32RxValue1, u32RxValue2;

  27.     /* Unlock protected registers */
  28.     SYS_UnlockReg();
  29.     /* Init System, IP clock and multi-function I/O. */
  30.     SYS_Init();
  31.     /* Lock protected registers */
  32.     SYS_LockReg();

  33.     /* Reset UART0 module */
  34.     SYS_ResetModule(UART0_RST);
  35.     /* Init UART0 to 115200-8n1 for printing messages */
  36.     UART_Open(UART0, 115200);

  37.     printf("+----------------------------------------------------------+\n");
  38.     printf("|            I2S Driver Sample Code (slave mode)           |\n");
  39.     printf("+----------------------------------------------------------+\n");
  40.     printf("  I2S configuration:\n");
  41.     printf("      Word width 16 bits\n");
  42.     printf("      Stereo mode\n");
  43.     printf("      I2S format\n");
  44.     printf("      TX value: 0xAA00AA01, 0xAA02AA03, ..., 0xAAFEAAFF, wraparound\n");
  45.     printf("  The I/O connection for I2S1 (SPI1):\n");
  46.     printf("      I2S1_LRCLK (PA4)\n      I2S1_BCLK(PA7)\n");
  47.     printf("      I2S1_DI (PA6)\n      I2S1_DO (PA5)\n\n");
  48.     printf("  NOTE: Connect with a I2S master.\n");
  49.     printf("        This sample code will transmit a TX value 50000 times, and then change to the next TX value.\n");
  50.     printf("        When TX value or the received value changes, the new TX value or the current TX value and the new received value will be printed.\n");
  51.     printf("  Press any key to start ...");
  52.     getchar();
  53.     printf("\n");

  54.     /* Slave mode, 16-bit word width, stereo mode, I2S format. Set TX and RX FIFO threshold to middle value. */
  55.     /* I2S peripheral clock rate is equal to PCLK1 clock rate. */
  56.     I2S_Open(SPI1, I2S_MODE_SLAVE, 0, I2S_DATABIT_16, I2S_STEREO, I2S_FORMAT_I2S);

  57.     /* Initiate data counter */
  58.     g_u32DataCount = 0;
  59.     /* Initiate TX value and RX value */
  60.     g_u32TxValue = 0xAA00AA01;
  61.     u32RxValue1 = 0;
  62.     u32RxValue2 = 0;
  63.     /* Enable TX threshold level interrupt */
  64.     I2S_EnableInt(SPI1, I2S_FIFO_TXTH_INT_MASK);
  65.     NVIC_EnableIRQ(SPI1_IRQn);

  66.     printf("Start I2S ...\nTX value: 0x%X\n", g_u32TxValue);

  67.     while(1)
  68.     {
  69.         /* Check RX FIFO empty flag */
  70.         if((SPI1->I2SSTS & SPI_I2SSTS_RXEMPTY_Msk) == 0)
  71.         {
  72.             /* Read RX FIFO */
  73.             u32RxValue2 = I2S_READ_RX_FIFO(SPI1);
  74.             if(u32RxValue1 != u32RxValue2)
  75.             {
  76.                 u32RxValue1 = u32RxValue2;
  77.                 /* If received value changes, print the current TX value and the new received value. */
  78.                 printf("TX value: 0x%X;  RX value: 0x%X\n", g_u32TxValue, u32RxValue1);
  79.             }
  80.         }
  81.         if(g_u32DataCount >= 50000)
  82.         {
  83.             g_u32TxValue = 0xAA00AA00 | ((g_u32TxValue + 0x00020002) & 0x00FF00FF); /* g_u32TxValue: 0xAA00AA01, 0xAA02AA03, ..., 0xAAFEAAFF */
  84.             printf("TX value: 0x%X\n", g_u32TxValue);
  85.             g_u32DataCount = 0;
  86.         }
  87.     }
  88. }

  89. void SYS_Init(void)
  90. {
  91.     /*---------------------------------------------------------------------------------------------------------*/
  92.     /* Init System Clock                                                                                       */
  93.     /*---------------------------------------------------------------------------------------------------------*/

  94.     /* Enable HIRC clock */
  95.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  96.     /* Waiting for HIRC clock ready */
  97.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  98.     /* Switch HCLK clock source to HIRC */
  99.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  100.     /* Enable HXT clock (external XTAL 12MHz) */
  101.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  102.     /* Wait for HXT clock ready */
  103.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  104.     /* Configure PLL */
  105.     CLK_EnablePLL(CLK_PLLCTL_PLLSRC_HXT, PLL_CLOCK);

  106.     /* Switch HCLK clock source to PLL */
  107.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(1));

  108.     /* Select UART module clock source as HXT and UART module clock divider as 1 */
  109.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));

  110.     /* Select PCLK1 as the clock source of SPI1 */
  111.     CLK_SetModuleClock(SPI1_MODULE, CLK_CLKSEL2_SPI1SEL_PCLK1, MODULE_NoMsk);

  112.     /* Enable peripheral clock */
  113.     CLK_EnableModuleClock(UART0_MODULE);
  114.     CLK_EnableModuleClock(SPI1_MODULE);

  115.     /* Update System Core Clock */
  116.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CyclesPerUs automatically. */
  117.     SystemCoreClockUpdate();

  118.     /*---------------------------------------------------------------------------------------------------------*/
  119.     /* Init I/O Multi-function                                                                                 */
  120.     /*---------------------------------------------------------------------------------------------------------*/
  121.     /* Set PD multi-function pins for UART0 RXD and TXD */
  122.     SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
  123.     SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);

  124.     /* Configure SPI1 related multi-function pins. */
  125.     /* GPA[7:4] : SPI1_CLK (I2S1_BCLK), SPI1_MISO (I2S1_DI), SPI1_MOSI (I2S1_DO), SPI1_SS (I2S1_LRCLK). */
  126.     SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA4MFP_Msk | SYS_GPA_MFPL_PA5MFP_Msk | SYS_GPA_MFPL_PA6MFP_Msk | SYS_GPA_MFPL_PA7MFP_Msk);
  127.     SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA4MFP_SPI1_SS | SYS_GPA_MFPL_PA5MFP_SPI1_MOSI | SYS_GPA_MFPL_PA6MFP_SPI1_MISO | SYS_GPA_MFPL_PA7MFP_SPI1_CLK);

  128. }

  129. void SPI1_IRQHandler()
  130. {
  131.     /* Write 2 TX values to TX FIFO */
  132.     I2S_WRITE_TX_FIFO(SPI1, g_u32TxValue);
  133.     I2S_WRITE_TX_FIFO(SPI1, g_u32TxValue);
  134.     g_u32DataCount += 2;
  135. }

  136. /*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/
 楼主| dongnanxibei 发表于 2017-7-19 20:02 | 显示全部楼层
I2S_Open(SPI1, I2S_MODE_SLAVE, 0, I2S_DATABIT_16, I2S_STEREO, I2S_FORMAT_I2S);
I2S_Open(SPI1, I2S_MODE_MASTER, 16000, I2S_DATABIT_16, I2S_STEREO, I2S_FORMAT_I2S);
这两个才是关键,搞两个例子,看晕人。
 楼主| dongnanxibei 发表于 2017-7-19 20:05 | 显示全部楼层
有时候关键点就是那么一点。
heisexingqisi 发表于 2017-7-19 20:17 | 显示全部楼层
上面分析的很到位,好多功能选择就是一个参数。
598330983 发表于 2017-7-19 22:15 | 显示全部楼层
看来要好好总结总结这些使用方法了。
zhuotuzi 发表于 2017-7-20 15:18 | 显示全部楼层
搞懂原理了就都简单了。
yiy 发表于 2017-7-24 21:11 | 显示全部楼层
I2C这种接口的外部芯片也是非常多的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

225

主题

3848

帖子

18

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