[DemoCode下载] UC240 UART接口驱动ESP8266

[复制链接]
745|1
 楼主| xinxianshi 发表于 2023-2-12 19:52 | 显示全部楼层 |阅读模式
EC_NUC240_UART_ESP8266_V1.00.zip (1.28 MB, 下载次数: 1)
ESP-01S是Espressif公司生产的一种基于ESP8266的小型模块。
ESP-01S ESP8266 Wi-Fi模块是一个独立的SOC,具有集成的TCP/IP协议栈,可以让任何微控制器访问Wi-Fi网络。ESP8266能够独立运行应用程序,或者从另一个应用程序处理器分担所有Wi-Fi网络功能。每个ESP-01S ESP8266模块都预置有AT命令集软件。
TCP传输控制协议是Internet协议套件的主要协议之一。它起源于最初的网络实现,它补充了互联网协议(IP)。因此,整个套件通常称为TCP/IP。TCP在通过IP网络通信的主机上运行的应用程序之间提供可靠的、有序的和经过错误检查的八位字节流。主要的internet应用程序,如万维网、电子邮件、远程管理和文件传输都依赖于TCP。不需要可靠数据流服务的应用程序可以使用用户数据报协议(UDP),该协议提供无连接数据报服务,强调降低延迟而不是可靠性。
本范例代码使用NUC240系列芯片,通过UART0接口驱动ESP-01S模组,利用ESP8266内置的TCP协议栈实现TCP服务器的功能。当电脑通过Wi-Fi连接ESP8266模块后,用户可以使用电脑工具串口及网络助手工具查看执行结果。
将ESP8266设置为TCP服务器,使能多IP连接,因为可能有多个客户端连接到ESP8266。
如下展示了当ESP8266在softAP模式下工作时如何实现TCP服务器。

397763e8d2d6f10cc.png
6449763e8d2e1f04b1.png
 楼主| xinxianshi 发表于 2023-2-12 19:52 | 显示全部楼层

  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url] main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url] V1.00
  4. * $Date: 8/20/19 5:36p $
  5. * [url=home.php?mod=space&uid=247401]@brief[/url] Use UART to drive ESP8266 wifi chip and communication.
  6. *
  7. * @note
  8. * Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  9. *****************************************************************************/
  10. #include <stdio.h>
  11. #include "NUC230_240.h"
  12. #include <string.h>                     //because using memset function
  13. /*---------------------------------------------------------------------------*/
  14. /* Define                                                                    */
  15. /*---------------------------------------------------------------------------*/
  16. #define PLL_CLOCK           50000000
  17. #define SYSTICK_TIMER       10000       // 10ms

  18. #define KEY1 PA0
  19. #define KEY2 PA1
  20. #define KEY3 PA2
  21. #define KEY4 PA3

  22. #define KEY_NONE 0
  23. #define KEY_SW1 1
  24. #define KEY_SW2 2
  25. #define KEY_SW3 3
  26. #define KEY_SW4 4

  27. #define RECEIVE_MAX_CHAR_NUMBER 100
  28. #define RECEIVE_MAX_STRING  10
  29. #define RECEIVE_MAX_VALIDCHAR_NUMBER 80

  30. typedef struct
  31. {
  32.     uint8_t  u8CharNum;
  33.     uint8_t *pu8string;
  34. } ATCMD_Type;

  35. typedef struct
  36. {
  37.     uint8_t  u8String[RECEIVE_MAX_CHAR_NUMBER];
  38. } ATREVSTRING_Type;

  39. /*---------------------------------------------------------------------------*/
  40. /* Global variables                                                          */
  41. /*---------------------------------------------------------------------------*/
  42. uint32_t u32SysTimerCounter = 0;
  43. uint32_t u32TimerOutCounter = 0;
  44. uint32_t u32SysTimerKeyCounter = 0;
  45. uint8_t u8KeyScan;
  46. uint8_t u8KeyScanPrev = KEY_NONE;
  47. uint8_t u8KeyGet;

  48. uint8_t AT_StringCmdRst[] = {"AT+RST\r\n"};
  49. uint8_t AT_StringCmdCwmode1[] = {"AT+CWMODE=1\r\n"};
  50. uint8_t AT_StringCmdCwmode2[] = {"AT+CWMODE=2\r\n"};
  51. uint8_t AT_StringCmdCwmode3[] = {"AT+CWMODE=3\r\n"};
  52. uint8_t AT_StringCmdCipMux0[] = {"AT+CIPMUX=0\r\n"};
  53. uint8_t AT_StringCmdCipMux1[] = {"AT+CIPMUX=1\r\n"};
  54. uint8_t AT_StringCmdCipServer0[] = {"AT+CIPSERVER=0\r\n"};
  55. uint8_t AT_StringCmdCipServer1[] = {"AT+CIPSERVER=1,8080\r\n"};
  56. uint8_t AT_StringCmdAP[] = {"AT+CWSAP_CUR="Nuvoton_AP","",5,0,4,0\r\n"};
  57. uint8_t AT_StringIPD[] = {"\n+IPD,0,"};

  58. ATCMD_Type atCmdRst = {sizeof(AT_StringCmdRst) / sizeof(uint8_t), AT_StringCmdRst};
  59. ATCMD_Type atCmdCwmode1 = {sizeof(AT_StringCmdCwmode1) / sizeof(uint8_t), AT_StringCmdCwmode1};
  60. ATCMD_Type atCmdCwmode2 = {sizeof(AT_StringCmdCwmode2) / sizeof(uint8_t), AT_StringCmdCwmode2};
  61. ATCMD_Type atCmdCwmode3 = {sizeof(AT_StringCmdCwmode3) / sizeof(uint8_t), AT_StringCmdCwmode3};
  62. ATCMD_Type atCmdCipmux0 = {sizeof(AT_StringCmdCipMux0) / sizeof(uint8_t), AT_StringCmdCipMux0};
  63. ATCMD_Type atCmdCipmux1 = {sizeof(AT_StringCmdCipMux1) / sizeof(uint8_t), AT_StringCmdCipMux1};
  64. ATCMD_Type atCmdCipserver0 = {sizeof(AT_StringCmdCipServer0) / sizeof(uint8_t), AT_StringCmdCipServer0};
  65. ATCMD_Type atCmdCipserver1 = {sizeof(AT_StringCmdCipServer1) / sizeof(uint8_t), AT_StringCmdCipServer1};
  66. ATCMD_Type atCmdAp = {sizeof(AT_StringCmdAP) / sizeof(uint8_t), AT_StringCmdAP};

  67. ATREVSTRING_Type ReceiveArray[RECEIVE_MAX_STRING];
  68. uint8_t u8ReceiveStringArrayHeader;
  69. uint8_t u8ReceiveStringArrayTail;
  70. uint8_t u8StringIndex;
  71. uint8_t u8ReceiveData[RECEIVE_MAX_VALIDCHAR_NUMBER];
  72. uint8_t u8ReceiveDataLength;
  73. /*---------------------------------------------------------------------------*/
  74. /* Functions                                                                 */
  75. /*---------------------------------------------------------------------------*/
  76. __STATIC_INLINE void CLK_SysTickInit(uint32_t u32us)
  77. {
  78.     SysTick->LOAD = u32us * CyclesPerUs;
  79.     SysTick->VAL  = (0x00);
  80.     SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
  81. }

  82. void SysTick_Handler(void)
  83. {
  84.     u32SysTimerCounter++;

  85.     if (u32TimerOutCounter > 0)
  86.     {
  87.         u32TimerOutCounter --;
  88.     }
  89. }
  90. /*---------------------------------------------------------------------------------------------------------*/
  91. /* ISR to handle UART Channel 0 interrupt event                                                            */
  92. /*---------------------------------------------------------------------------------------------------------*/
  93. void UART02_IRQHandler(void)
  94. {
  95.     uint32_t u32IntSts = UART0->ISR;
  96.     uint8_t u8Data;

  97.     if (u32IntSts & UART_ISR_RDA_INT_Msk)
  98.     {
  99.         u8Data = UART_READ(UART0);

  100.         while (UART1->FSR & UART_FSR_TX_FULL_Msk);

  101.         UART_WRITE(UART1, u8Data);

  102.         if (u8Data == '\r')
  103.         {
  104.             ReceiveArray[u8ReceiveStringArrayTail].u8String[u8StringIndex++] = u8Data;
  105.             u8ReceiveStringArrayTail++;
  106.             u8StringIndex = 0;

  107.             if (u8ReceiveStringArrayTail >= RECEIVE_MAX_STRING)
  108.             {
  109.                 if (u8ReceiveStringArrayHeader == 0)
  110.                 {
  111.                     u8ReceiveStringArrayTail = 0;
  112.                     u8ReceiveStringArrayHeader++;
  113.                 }
  114.                 else
  115.                 {
  116.                     u8ReceiveStringArrayTail = 0;
  117.                 }
  118.             }
  119.             else if (u8ReceiveStringArrayTail == u8ReceiveStringArrayHeader)
  120.             {
  121.                 u8ReceiveStringArrayHeader++;

  122.                 if (u8ReceiveStringArrayHeader >= RECEIVE_MAX_STRING)
  123.                 {
  124.                     u8ReceiveStringArrayHeader = 0;
  125.                 }
  126.             }
  127.         }
  128.         else
  129.         {
  130.             ReceiveArray[u8ReceiveStringArrayTail].u8String[u8StringIndex++] = u8Data;

  131.             if (u8StringIndex >= RECEIVE_MAX_CHAR_NUMBER)
  132.             {
  133.                 u8StringIndex--;
  134.             }
  135.         }
  136.     }
  137.     else
  138.     {
  139.         UART0->FSR |= UART_FSR_BIF_Msk | UART_FSR_FEF_Msk | UART_FSR_PEF_Msk;
  140.         u8Data = UART_READ(UART0);
  141.     }
  142. }
  143. /*---------------------------------------------------------------------------------------------------------*/
  144. /* ISR to handle UART Channel 1 interrupt event                                                            */
  145. /*---------------------------------------------------------------------------------------------------------*/
  146. void UART1_IRQHandler(void)
  147. {
  148.     uint32_t u32IntSts = UART1->ISR;
  149.     uint8_t u8Data;

  150.     if (u32IntSts & UART_ISR_RDA_INT_Msk)
  151.     {
  152.         u8Data = UART_READ(UART1);

  153.         while (UART0->FSR & UART_FSR_TX_FULL_Msk);

  154.         UART_WRITE(UART0, u8Data);
  155.     }
  156.     else
  157.     {
  158.         UART1->FSR |= UART_FSR_BIF_Msk | UART_FSR_FEF_Msk | UART_FSR_PEF_Msk;
  159.         u8Data = UART_READ(UART0);
  160.     }

  161. }

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

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

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

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

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

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

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

  179.     /* Enable UART module clock */
  180.     CLK_EnableModuleClock(UART0_MODULE);
  181.     CLK_EnableModuleClock(UART1_MODULE);

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

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

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

  191.     /* Set GPB multi-function pins for UART1 RXD and TXD */
  192.     SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB4_Msk | SYS_GPB_MFP_PB5_Msk);
  193.     SYS->GPB_MFP |= SYS_GPB_MFP_PB4_UART1_RXD | SYS_GPB_MFP_PB5_UART1_TXD;
  194. }

  195. void UART_Init()
  196. {
  197.     /*---------------------------------------------------------------------------------------------------------*/
  198.     /* Init UART                                                                                               */
  199.     /*---------------------------------------------------------------------------------------------------------*/
  200.     /* Reset IP */
  201.     SYS_ResetModule(UART0_RST);
  202.     SYS_ResetModule(UART1_RST);

  203.     /* Configure UART0/UART1 and set UART0/UART1 Baudrate */
  204.     UART_Open(UART0, 115200);
  205.     UART_Open(UART1, 115200);

  206.     /* Enable Interrupt and install the call back function */
  207.     //set RX FIFO INT TRIGGER LEVEL
  208.     UART0->FCR &= ~(UART_FCR_RFITL_Msk | UART_FCR_RTS_TRI_LEV_Msk);
  209.     UART0->FCR |= ((0x0ul << UART_FCR_RFITL_Pos));
  210.     UART_DisableInt(UART0, (UART_IER_RDA_IEN_Msk | UART_IER_THRE_IEN_Msk | UART_IER_RLS_IEN_Msk));
  211.     UART_EnableInt(UART0, (UART_IER_RDA_IEN_Msk | UART_IER_RLS_IEN_Msk));

  212.     UART1->FCR &= ~(UART_FCR_RFITL_Msk | UART_FCR_RTS_TRI_LEV_Msk);
  213.     UART1->FCR |= ((0x0ul << UART_FCR_RFITL_Pos));
  214.     UART_DisableInt(UART1, (UART_IER_RDA_IEN_Msk | UART_IER_THRE_IEN_Msk | UART_IER_RLS_IEN_Msk));
  215.     UART_EnableInt(UART1, (UART_IER_RDA_IEN_Msk | UART_IER_RLS_IEN_Msk));
  216. }

  217. void Initial_Key_Input(void)
  218. {
  219.     GPIO_SetMode(PA, 0, GPIO_PMD_QUASI);
  220.     GPIO_SetMode(PA, 1, GPIO_PMD_QUASI);
  221.     GPIO_SetMode(PA, 2, GPIO_PMD_QUASI);
  222.     GPIO_SetMode(PA, 3, GPIO_PMD_QUASI);
  223. }

  224. uint8_t Get_Key_Input(void)
  225. {
  226.     uint8_t u8temp = KEY_NONE;

  227.     if (KEY1 == 0)
  228.     {
  229.         u8temp = KEY_SW1;
  230.     }

  231.     if (KEY2 == 0)
  232.     {
  233.         u8temp = KEY_SW2;
  234.     }

  235.     if (KEY3 == 0)
  236.     {
  237.         u8temp = KEY_SW3;
  238.     }

  239.     if (KEY4 == 0)
  240.     {
  241.         u8temp = KEY_SW4;
  242.     }

  243.     return u8temp;
  244. }

  245. void Key_Process(uint8_t u8Key)
  246. {
  247.     uint32_t u32cnt;

  248.     if (u8Key == KEY_NONE)
  249.     {
  250.         return;
  251.     }

  252.     switch (u8Key)
  253.     {
  254.         case KEY_SW1:
  255.             UART_Write(UART0, atCmdRst.pu8string, atCmdRst.u8CharNum);            // Reset ESP8266
  256.             break;

  257.         case KEY_SW2:
  258.             UART_Write(UART0, atCmdCwmode3.pu8string, atCmdCwmode3.u8CharNum);    // Set ESP8266 STA+AP mode

  259.             for (u32cnt = 0; u32cnt < 0xffff; u32cnt++);

  260.             UART_Write(UART0, atCmdCipmux1.pu8string, atCmdCipmux1.u8CharNum);    // Set ESP8266 Multi-connection

  261.             for (u32cnt = 0; u32cnt < 0xffff; u32cnt++);

  262.             UART_Write(UART0, atCmdAp.pu8string, atCmdAp.u8CharNum);              // Set ESP8266 AP Name, No Password

  263.             for (u32cnt = 0; u32cnt < 0xffff; u32cnt++);

  264.             UART_Write(UART0, atCmdCipserver1.pu8string, atCmdCipserver1.u8CharNum);  // Set ESP8266 TCP Server mode, Port number 8080

  265.             for (u32cnt = 0; u32cnt < 0xffff; u32cnt++);

  266.             u8ReceiveStringArrayHeader = u8ReceiveStringArrayTail = 0;
  267.             memset(ReceiveArray, 0, sizeof(ReceiveArray));

  268.         default:
  269.             break;
  270.     }
  271. }

  272. uint32_t CharConvertToNumber(uint8_t *pu81, uint8_t u8number)
  273. {
  274.     uint32_t uChar = 0;
  275.     uint8_t u8cnt;

  276.     for (u8cnt = 0; u8cnt < u8number; u8cnt++)
  277.     {
  278.         if ((*(pu81 + u8cnt) >= '0') && (*(pu81 + u8cnt) <= '9'))
  279.         {
  280.             uChar = uChar * 10 + (*(pu81 + u8cnt) - '0');
  281.         }
  282.         else
  283.         {
  284.             uChar = 0xFFFFFFFF;
  285.             break;
  286.         }
  287.     }

  288.     return uChar;
  289. }

  290. uint8_t StringCompare(uint8_t *pu81, uint8_t *pu82)
  291. {
  292.     uint8_t u8cnt = 0, j;

  293.     uint8_t u8LengthString[4];
  294.     uint32_t  u32Length;

  295.     for (u8cnt = 0;; u8cnt++)
  296.     {
  297.         if (u8cnt >= RECEIVE_MAX_CHAR_NUMBER)
  298.         {
  299.             return FALSE;
  300.         }

  301.         if (*(pu82 + u8cnt) == '\0')
  302.         {
  303.             break;
  304.         }

  305.         if (*(pu81 + u8cnt) != *(pu82 + u8cnt))
  306.         {
  307.             return FALSE;
  308.         }
  309.     }

  310.     for (j = 0; j < 5; j++)
  311.     {
  312.         if (*(pu81 + u8cnt + j) != ':')
  313.         {
  314.             u8LengthString[j] = *(pu81 + u8cnt + j);
  315.         }
  316.         else
  317.         {
  318.             break;
  319.         }
  320.     }

  321.     if (j == 5)
  322.     {
  323.         return FALSE; //error length
  324.     }
  325.     else
  326.     {
  327.         u32Length = CharConvertToNumber(u8LengthString, j);
  328.     }

  329.     u8cnt = u8cnt + j + 1; //+1 to filter ':'

  330.     if ((u8cnt + u32Length) >= RECEIVE_MAX_CHAR_NUMBER)
  331.     {
  332.         return FALSE;
  333.     }

  334.     for (j = 0; j < u32Length; j++)
  335.     {
  336.         u8ReceiveData[j] = *(pu81 + u8cnt + j);
  337.     }

  338.     u8ReceiveData[j] = '\0';
  339.     u8ReceiveDataLength = u32Length;

  340.     return TRUE;

  341. }

  342. int main(void)
  343. {
  344.     /* Unlock protected registers */
  345.     SYS_UnlockReg();
  346.     /* Init System, IP clock and multi-function I/O */
  347.     SYS_Init();
  348.     /* Lock protected registers */
  349.     SYS_LockReg();

  350.     /* Init uart */
  351.     UART_Init();

  352.     /* Config systick */
  353.     CLK_SysTickInit(SYSTICK_TIMER);

  354.     while (1)
  355.     {
  356.         if ((u32SysTimerCounter - u32SysTimerKeyCounter) >= 20) //20*SYSTICK_TIMER
  357.         {
  358.             u32SysTimerKeyCounter = u32SysTimerCounter;
  359.             u8KeyScan = Get_Key_Input();

  360.             if (u8KeyScan == KEY_NONE)
  361.             {
  362.                 u8KeyGet = u8KeyScanPrev;
  363.             }

  364.             u8KeyScanPrev = u8KeyScan;

  365.             if (u8KeyGet != KEY_NONE)
  366.             {
  367.                 Key_Process(u8KeyGet);
  368.                 u8KeyGet = KEY_NONE;
  369.             }
  370.         }

  371.         if (u8ReceiveStringArrayHeader != u8ReceiveStringArrayTail)
  372.         {
  373.             if (StringCompare(ReceiveArray[u8ReceiveStringArrayHeader].u8String, AT_StringIPD))
  374.             {
  375.                 printf("\n %s", u8ReceiveData);
  376.             }

  377.             u8ReceiveStringArrayHeader++;

  378.             if (u8ReceiveStringArrayHeader >= RECEIVE_MAX_STRING)
  379.             {
  380.                 u8ReceiveStringArrayHeader = 0;
  381.             }
  382.         }
  383.     }

  384. }
  385. /*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/
您需要登录后才可以回帖 登录 | 注册

本版积分规则

102

主题

1019

帖子

1

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