#include "bsp_esp8266.h"
#include "common.h"
#include "stdio.h"
#include "string.h"
#include "stdbool.h"
#include "bsp_SysTick.h"
static void ESP8266_GPIO_Config ( void );
static void ESP8266_USART_Config ( void );
static void ESP8266_USART_NVIC_Configuration ( void );
struct STRUCT_USARTx_Fram strEsp8266_Fram_Record = { 0 };
/**
* @brief ESP8266³õʼ»¯º¯Êý
* @param ÎÞ
* @retval ÎÞ
*/
void ESP8266_Init ( void )
{
ESP8266_GPIO_Config ();
ESP8266_USART_Config ();
macESP8266_RST_HIGH_LEVEL();
macESP8266_CH_DISABLE();
}
/**
* @brief ³õʼ»¯ESP8266Óõ½µÄGPIOÒý½Å
* @param ÎÞ
* @retval ÎÞ
*/
static void ESP8266_GPIO_Config ( void )
{
/*¶¨ÒåÒ»¸öGPIO_InitTypeDefÀàÐ͵ĽṹÌå*/
GPIO_InitTypeDef GPIO_InitStructure;
/* ÅäÖà CH_PD Òý½Å*/
macESP8266_CH_PD_APBxClock_FUN ( macESP8266_CH_PD_CLK, ENABLE );
GPIO_InitStructure.GPIO_Pin = macESP8266_CH_PD_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init ( macESP8266_CH_PD_PORT, & GPIO_InitStructure );
/* ÅäÖà RST Òý½Å*/
macESP8266_RST_APBxClock_FUN ( macESP8266_RST_CLK, ENABLE );
GPIO_InitStructure.GPIO_Pin = macESP8266_RST_PIN;
GPIO_Init ( macESP8266_RST_PORT, & GPIO_InitStructure );
}
/**
* @brief ³õʼ»¯ESP8266Óõ½µÄ USART
* @param ÎÞ
* @retval ÎÞ
*/
static void ESP8266_USART_Config ( void )
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* config USART clock */
macESP8266_USART_APBxClock_FUN ( macESP8266_USART_CLK, ENABLE );
macESP8266_USART_GPIO_APBxClock_FUN ( macESP8266_USART_GPIO_CLK, ENABLE );
/* USART GPIO config */
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = macESP8266_USART_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(macESP8266_USART_TX_PORT, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Pin = macESP8266_USART_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(macESP8266_USART_RX_PORT, &GPIO_InitStructure);
/* USART1 mode config */
USART_InitStructure.USART_BaudRate = macESP8266_USART_BAUD_RATE;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(macESP8266_USARTx, &USART_InitStructure);
/* ÖжÏÅäÖà */
USART_ITConfig ( macESP8266_USARTx, USART_IT_RXNE, ENABLE ); //ʹÄÜ´®¿Ú½ÓÊÕÖжÏ
USART_ITConfig ( macESP8266_USARTx, USART_IT_IDLE, ENABLE ); //ʹÄÜ´®¿Ú×ÜÏß¿ÕÏÐÖжÏ
ESP8266_USART_NVIC_Configuration ();
USART_Cmd(macESP8266_USARTx, ENABLE);
}
/**
* @brief ÅäÖà ESP8266 USART µÄ NVIC ÖжÏ
* @param ÎÞ
* @retval ÎÞ
*/
static void ESP8266_USART_NVIC_Configuration ( void )
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig ( macNVIC_PriorityGroup_x );
/* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = macESP8266_USART_IRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*
* º¯ÊýÃû£ºESP8266_Rst
* ÃèÊö £ºÖØÆôWF-ESP8266Ä£¿é
* ÊäÈë £ºÎÞ
* ·µ»Ø : ÎÞ
* µ÷Óà £º±» ESP8266_AT_Test µ÷ÓÃ
*/
void ESP8266_Rst ( void )
{
#if 0
ESP8266_Cmd ( "AT+RST", "OK", "ready", 2500 );
#else
macESP8266_RST_LOW_LEVEL();
Delay_ms ( 500 );
macESP8266_RST_HIGH_LEVEL();
#endif
}
/*
* º¯ÊýÃû£ºESP8266_Cmd
* ÃèÊö £º¶ÔWF-ESP8266Ä£¿é·¢ËÍATÖ¸Áî
* ÊäÈë £ºcmd£¬´ý·¢Ë͵ÄÖ¸Áî
* reply1£¬reply2£¬ÆÚ´ýµÄÏìÓ¦£¬ÎªNULL±í²»ÐèÏìÓ¦£¬Á½ÕßΪ»òÂß¼¹ØÏµ
* waittime£¬µÈ´ýÏìÓ¦µÄʱ¼ä
* ·µ»Ø : 1£¬Ö¸Áî·¢Ëͳɹ¦
* 0£¬Ö¸Áî·¢ËÍʧ°Ü
* µ÷Óà £º±»Íⲿµ÷ÓÃ
*/
bool ESP8266_Cmd ( char * cmd, char * reply1, char * reply2, u32 waittime )
{
strEsp8266_Fram_Record .InfBit .FramLength = 0; //´ÓпªÊ¼½ÓÊÕеÄÊý¾Ý°ü
macESP8266_Usart ( "%s\r\n", cmd );
if ( ( reply1 == 0 ) && ( reply2 == 0 ) ) //²»ÐèÒª½ÓÊÕÊý¾Ý
return true;
Delay_ms ( waittime ); //ÑÓʱ
strEsp8266_Fram_Record .Data_RX_BUF [ strEsp8266_Fram_Record .InfBit .FramLength ] = '\0';
macPC_Usart ( "%s", strEsp8266_Fram_Record .Data_RX_BUF );
if ( ( reply1 != 0 ) && ( reply2 != 0 ) )
return ( ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, reply1 ) ||
( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, reply2 ) );
else if ( reply1 != 0 )
return ( ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, reply1 ) );
else
return ( ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, reply2 ) );
}
/*
* º¯ÊýÃû£ºESP8266_AT_Test
* ÃèÊö £º¶ÔWF-ESP8266Ä£¿é½øÐÐAT²âÊÔÆô¶¯
* ÊäÈë £ºÎÞ
* ·µ»Ø : ÎÞ
* µ÷Óà £º±»Íⲿµ÷ÓÃ
*/
//void ESP8266_AT_Test ( void )
//{
// macESP8266_RST_HIGH_LEVEL();
//
// Delay_ms ( 1000 );
//
// while ( ! ESP8266_Cmd ( "AT", "OK", NULL, 500 ) ) ESP8266_Rst ();
//}
void ESP8266_AT_Test ( void )
{
char count=0;
macESP8266_RST_HIGH_LEVEL();
Delay_ms ( 1000 );
while ( count < 10 )
{
if( ESP8266_Cmd ( "AT", "OK", NULL, 500 ) ) return;
ESP8266_Rst();
++ count;
}
}
/*
* º¯ÊýÃû£ºESP8266_Net_Mode_Choose
* ÃèÊö £ºÑ¡ÔñWF-ESP8266Ä£¿éµÄ¹¤×÷ģʽ
* ÊäÈë £ºenumMode£¬¹¤×÷ģʽ
* ·µ»Ø : 1£¬Ñ¡Ôñ³É¹¦
* 0£¬Ñ¡Ôñʧ°Ü
* µ÷Óà £º±»Íⲿµ÷ÓÃ
*/
bool ESP8266_Net_Mode_Choose ( ENUM_Net_ModeTypeDef enumMode )
{
switch ( enumMode )
{
case STA:
return ESP8266_Cmd ( "AT+CWMODE=1", "OK", "no change", 2500 );
case AP:
return ESP8266_Cmd ( "AT+CWMODE=2", "OK", "no change", 2500 );
case STA_AP:
return ESP8266_Cmd ( "AT+CWMODE=3", "OK", "no change", 2500 );
default:
return false;
}
}
/*
* º¯ÊýÃû£ºESP8266_JoinAP
* ÃèÊö £ºWF-ESP8266Ä£¿éÁ¬½ÓÍⲿWiFi
* ÊäÈë £ºpSSID£¬WiFiÃû³Æ×Ö·û´®
* £ºpPassWord£¬WiFiÃÜÂë×Ö·û´®
* ·µ»Ø : 1£¬Á¬½Ó³É¹¦
* 0£¬Á¬½Óʧ°Ü
* µ÷Óà £º±»Íⲿµ÷ÓÃ
*/
bool ESP8266_JoinAP ( char * pSSID, char * pPassWord )
{
char cCmd [120];
sprintf ( cCmd, "AT+CWJAP=\"%s\",\"%s\"", pSSID, pPassWord );
return ESP8266_Cmd ( cCmd, "OK", NULL, 5000 );
}
/*
* º¯ÊýÃû£ºESP8266_BuildAP
* ÃèÊö £ºWF-ESP8266Ä£¿é´´½¨WiFiÈȵã
* ÊäÈë £ºpSSID£¬WiFiÃû³Æ×Ö·û´®
* £ºpPassWord£¬WiFiÃÜÂë×Ö·û´®
* £ºenunPsdMode£¬WiFi¼ÓÃÜ·½Ê½´úºÅ×Ö·û´®
* ·µ»Ø : 1£¬´´½¨³É¹¦
* 0£¬´´½¨Ê§°Ü
* µ÷Óà £º±»Íⲿµ÷ÓÃ
*/
bool ESP8266_BuildAP ( char * pSSID, char * pPassWord, ENUM_AP_PsdMode_TypeDef enunPsdMode )
{
char cCmd [120];
sprintf ( cCmd, "AT+CWSAP=\"%s\",\"%s\",1,%d", pSSID, pPassWord, enunPsdMode );
return ESP8266_Cmd ( cCmd, "OK", 0, 1000 );
}
/*
* º¯ÊýÃû£ºESP8266_Enable_MultipleId
* ÃèÊö £ºWF-ESP8266Ä£¿éÆô¶¯¶àÁ¬½Ó
* ÊäÈë £ºenumEnUnvarnishTx£¬ÅäÖÃÊÇ·ñ¶àÁ¬½Ó
* ·µ»Ø : 1£¬ÅäÖóɹ¦
* 0£¬ÅäÖÃʧ°Ü
* µ÷Óà £º±»Íⲿµ÷ÓÃ
*/
bool ESP8266_Enable_MultipleId ( FunctionalState enumEnUnvarnishTx )
{
char cStr [20];
sprintf ( cStr, "AT+CIPMUX=%d", ( enumEnUnvarnishTx ? 1 : 0 ) );
return ESP8266_Cmd ( cStr, "OK", 0, 500 );
}
/*
* º¯ÊýÃû£ºESP8266_Link_Server
* ÃèÊö £ºWF-ESP8266Ä£¿éÁ¬½ÓÍⲿ·þÎñÆ÷
* ÊäÈë £ºenumE£¬ÍøÂçÐÒé
* £ºip£¬·þÎñÆ÷IP×Ö·û´®
* £ºComNum£¬·þÎñÆ÷¶Ë¿Ú×Ö·û´®
* £ºid£¬Ä£¿éÁ¬½Ó·þÎñÆ÷µÄID
* ·µ»Ø : 1£¬Á¬½Ó³É¹¦
* 0£¬Á¬½Óʧ°Ü
* µ÷Óà £º±»Íⲿµ÷ÓÃ
*/
bool ESP8266_Link_Server ( ENUM_NetPro_TypeDef enumE, char * ip, char * ComNum, ENUM_ID_NO_TypeDef id)
{
char cStr [100] = { 0 }, cCmd [120];
switch ( enumE )
{
case enumTCP:
sprintf ( cStr, "\"%s\",\"%s\",%s", "TCP", ip, ComNum );
break;
case enumUDP:
sprintf ( cStr, "\"%s\",\"%s\",%s", "UDP", ip, ComNum );
break;
default:
break;
}
if ( id < 5 )
sprintf ( cCmd, "AT+CIPSTART=%d,%s", id, cStr);
else
sprintf ( cCmd, "AT+CIPSTART=%s", cStr );
return ESP8266_Cmd ( cCmd, "OK", "ALREAY CONNECT", 4000 );
}
/*
* º¯ÊýÃû£ºESP8266_StartOrShutServer
* ÃèÊö £ºWF-ESP8266Ä£¿é¿ªÆô»ò¹Ø±Õ·þÎñÆ÷ģʽ
* ÊäÈë £ºenumMode£¬¿ªÆô/¹Ø±Õ
* £ºpPortNum£¬·þÎñÆ÷¶Ë¿ÚºÅ×Ö·û´®
* £ºpTimeOver£¬·þÎñÆ÷³¬Ê±Ê±¼ä×Ö·û´®£¬µ¥Î»£ºÃë
* ·µ»Ø : 1£¬²Ù×÷³É¹¦
* 0£¬²Ù×÷ʧ°Ü
* µ÷Óà £º±»Íⲿµ÷ÓÃ
*/
bool ESP8266_StartOrShutServer ( FunctionalState enumMode, char * pPortNum, char * pTimeOver )
{
char cCmd1 [120], cCmd2 [120];
if ( enumMode )
{
sprintf ( cCmd1, "AT+CIPSERVER=%d,%s", 1, pPortNum );
sprintf ( cCmd2, "AT+CIPSTO=%s", pTimeOver );
return ( ESP8266_Cmd ( cCmd1, "OK", 0, 500 ) &&
ESP8266_Cmd ( cCmd2, "OK", 0, 500 ) );
}
else
{
sprintf ( cCmd1, "AT+CIPSERVER=%d,%s", 0, pPortNum );
return ESP8266_Cmd ( cCmd1, "OK", 0, 500 );
}
}
/*
* º¯ÊýÃû£ºESP8266_Get_LinkStatus
* ÃèÊö £º»ñÈ¡ WF-ESP8266 µÄÁ¬½Ó״̬£¬½ÏÊʺϵ¥¶Ë¿ÚʱʹÓÃ
* ÊäÈë £ºÎÞ
* ·µ»Ø : 2£¬»ñµÃip
* 3£¬½¨Á¢Á¬½Ó
* 3£¬Ê§È¥Á¬½Ó
* 0£¬»ñȡ״̬ʧ°Ü
* µ÷Óà £º±»Íⲿµ÷ÓÃ
*/
uint8_t ESP8266_Get_LinkStatus ( void )
{
if ( ESP8266_Cmd ( "AT+CIPSTATUS", "OK", 0, 500 ) )
{
if ( strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "STATUS:2\r\n" ) )
return 2;
else if ( strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "STATUS:3\r\n" ) )
return 3;
else if ( strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "STATUS:4\r\n" ) )
return 4;
}
return 0;
}
/*
* º¯ÊýÃû£ºESP8266_Get_IdLinkStatus
* ÃèÊö £º»ñÈ¡ WF-ESP8266 µÄ¶Ë¿Ú£¨Id£©Á¬½Ó״̬£¬½ÏÊʺ϶à¶Ë¿ÚʱʹÓÃ
* ÊäÈë £ºÎÞ
* ·µ»Ø : ¶Ë¿Ú£¨Id£©µÄÁ¬½Ó״̬£¬µÍ5λΪÓÐЧ룬·Ö±ð¶ÔÓ¦Id5~0£¬Ä³Î»ÈôÖÃ1±í¸ÃId½¨Á¢ÁËÁ¬½Ó£¬Èô±»Çå0±í¸ÃId佨Á¢Á¬½Ó
* µ÷Óà £º±»Íⲿµ÷ÓÃ
*/
uint8_t ESP8266_Get_IdLinkStatus ( void )
{
uint8_t ucIdLinkStatus = 0x00;
if ( ESP8266_Cmd ( "AT+CIPSTATUS", "OK", 0, 500 ) )
{
if ( strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "+CIPSTATUS:0," ) )
ucIdLinkStatus |= 0x01;
else
ucIdLinkStatus &= ~ 0x01;
if ( strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "+CIPSTATUS:1," ) )
ucIdLinkStatus |= 0x02;
else
ucIdLinkStatus &= ~ 0x02;
if ( strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "+CIPSTATUS:2," ) )
ucIdLinkStatus |= 0x04;
else
ucIdLinkStatus &= ~ 0x04;
if ( strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "+CIPSTATUS:3," ) )
ucIdLinkStatus |= 0x08;
else
ucIdLinkStatus &= ~ 0x08;
if ( strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "+CIPSTATUS:4," ) )
ucIdLinkStatus |= 0x10;
else
ucIdLinkStatus &= ~ 0x10;
}
return ucIdLinkStatus;
}
/*
* º¯ÊýÃû£ºESP8266_Inquire_ApIp
* ÃèÊö £º»ñÈ¡ F-ESP8266 µÄ AP IP
* ÊäÈë £ºpApIp£¬´æ·Å AP IP µÄÊý×éµÄÊ×µØÖ·
* ucArrayLength£¬´æ·Å AP IP µÄÊý×éµÄ³¤¶È
* ·µ»Ø : 0£¬»ñȡʧ°Ü
* 1£¬»ñÈ¡³É¹¦
* µ÷Óà £º±»Íⲿµ÷ÓÃ
*/
uint8_t ESP8266_Inquire_ApIp ( char * pApIp, uint8_t ucArrayLength )
{
char uc;
char * pCh;
ESP8266_Cmd ( "AT+CIFSR", "OK", 0, 500 );
pCh = strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "APIP,\"" );
if ( pCh )
pCh += 6;
else
return 0;
for ( uc = 0; uc < ucArrayLength; uc ++ )
{
pApIp [ uc ] = * ( pCh + uc);
if ( pApIp [ uc ] == '\"' )
{
pApIp [ uc ] = '\0';
break;
}
}
return 1;
}
/*
* º¯ÊýÃû£ºESP8266_UnvarnishSend
* ÃèÊö £ºÅäÖÃWF-ESP8266Ä£¿é½øÈë͸´«·¢ËÍ
* ÊäÈë £ºÎÞ
* ·µ»Ø : 1£¬ÅäÖóɹ¦
* 0£¬ÅäÖÃʧ°Ü
* µ÷Óà £º±»Íⲿµ÷ÓÃ
*/
bool ESP8266_UnvarnishSend ( void )
{
if ( ! ESP8266_Cmd ( "AT+CIPMODE=1", "OK", 0, 500 ) )
return false;
return
ESP8266_Cmd ( "AT+CIPSEND", "OK", ">", 500 );
}
/*
* º¯ÊýÃû£ºESP8266_ExitUnvarnishSend
* ÃèÊö £ºÅäÖÃWF-ESP8266Ä£¿éÍ˳ö͸´«Ä£Ê½
* ÊäÈë £ºÎÞ
* ·µ»Ø : ÎÞ
* µ÷Óà £º±»Íⲿµ÷ÓÃ
*/
void ESP8266_ExitUnvarnishSend ( void )
{
Delay_ms ( 1000 );
macESP8266_Usart ( "+++" );
Delay_ms ( 500 );
}
/*
* º¯ÊýÃû£ºESP8266_SendString
* ÃèÊö £ºWF-ESP8266Ä£¿é·¢ËÍ×Ö·û´®
* ÊäÈë £ºenumEnUnvarnishTx£¬ÉùÃ÷ÊÇ·ñÒÑʹÄÜÁË͸´«Ä£Ê½
* £ºpStr£¬Òª·¢Ë͵Ä×Ö·û´®
* £ºulStrLength£¬Òª·¢Ë͵Ä×Ö·û´®µÄ×Ö½ÚÊý
* £ºucId£¬ÄĸöID·¢Ë͵Ä×Ö·û´®
* ·µ»Ø : 1£¬·¢Ëͳɹ¦
* 0£¬·¢ËÍʧ°Ü
* µ÷Óà £º±»Íⲿµ÷ÓÃ
*/
bool ESP8266_SendString ( FunctionalState enumEnUnvarnishTx, char * pStr, u32 ulStrLength, ENUM_ID_NO_TypeDef ucId )
{
char cStr [20];
bool bRet = false;
if ( enumEnUnvarnishTx )
{
macESP8266_Usart ( "%s", pStr );
bRet = true;
}
else
{
if ( ucId < 5 )
sprintf ( cStr, "AT+CIPSEND=%d,%d", ucId, ulStrLength + 2 );
else
sprintf ( cStr, "AT+CIPSEND=%d", ulStrLength + 2 );
ESP8266_Cmd ( cStr, "> ", 0, 1000 );
bRet = ESP8266_Cmd ( pStr, "SEND OK", 0, 1000 );
}
return bRet;
}
/*
* º¯ÊýÃû£ºESP8266_ReceiveString
* ÃèÊö £ºWF-ESP8266Ä£¿é½ÓÊÕ×Ö·û´®
* ÊäÈë £ºenumEnUnvarnishTx£¬ÉùÃ÷ÊÇ·ñÒÑʹÄÜÁË͸´«Ä£Ê½
* ·µ»Ø : ½ÓÊÕµ½µÄ×Ö·û´®Ê×µØÖ·
* µ÷Óà £º±»Íⲿµ÷ÓÃ
*/
char * ESP8266_ReceiveString ( FunctionalState enumEnUnvarnishTx )
{
char * pRecStr = 0;
strEsp8266_Fram_Record .InfBit .FramLength = 0;
strEsp8266_Fram_Record .InfBit .FramFinishFlag = 0;
while ( ! strEsp8266_Fram_Record .InfBit .FramFinishFlag );
strEsp8266_Fram_Record .Data_RX_BUF [ strEsp8266_Fram_Record .InfBit .FramLength ] = '\0';
if ( enumEnUnvarnishTx )
pRecStr = strEsp8266_Fram_Record .Data_RX_BUF;
else
{
if ( strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "+IPD" ) )
pRecStr = strEsp8266_Fram_Record .Data_RX_BUF;
}
return pRecStr;
}
对每一行加上注释