1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| #include "stm32f10x.h" #include <stdarg.h> #include <stdio.h> #include <string.h> #include "esp8266.h" #include "timer.h" #include "Systick.h"
#define USART2_MAX_SEND_LEN 600 uint8_t USART2_TX_BUF[USART2_MAX_SEND_LEN];
USART_Buffer ESP8266_Buffer; void USART2_IRQHandler(void) { if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { if (ESP8266_Buffer.Length < (USART_RX_BUF_SIZE - 1)) ESP8266_Buffer.Body[ESP8266_Buffer.Length++] = (char)USART_ReceiveData(USART2); } if (USART_GetITStatus(USART2, USART_IT_IDLE) != RESET) {
ESP8266_Buffer.Body[ESP8266_Buffer.Length] = '\0'; volatile uint16_t temp; temp = USART2->SR; temp = USART2->DR; TIM_SetCounter(TIM2,0); TIM_Cmd(TIM2,ENABLE);
} }
void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { ESP8266_Buffer.FinishFlag = 1; TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); TIM_Cmd(TIM2, DISABLE); } printf("Now Buffer:\r\n%s",(uint8_t*)ESP8266_Buffer.Body);
}
void ESP8266_Init(uint32_t baud) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); USART_DeInit(USART2); USART_InitTypeDef USART_InitStruture; USART_InitStruture.USART_BaudRate=baud; USART_InitStruture.USART_HardwareFlowControl=USART_HardwareFlowControl_None; USART_InitStruture.USART_Mode=USART_Mode_Tx | USART_Mode_Rx; USART_InitStruture.USART_Parity=USART_Parity_No; USART_InitStruture.USART_StopBits=USART_StopBits_1; USART_InitStruture.USART_WordLength=USART_WordLength_8b; USART_Init(USART2,&USART_InitStruture); USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2; NVIC_InitStructure.NVIC_IRQChannelSubPriority=3; NVIC_Init(&NVIC_InitStructure); USART_Cmd(USART2,ENABLE); Timer_Init(0,20000-1,7200-1); TIM_Cmd(TIM2,DISABLE); }
void ESP8266_SendByte(uint8_t byte) { USART_SendData(USART2,byte); while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET); }
void ESP8266_Printf(char *format,...) {
va_list arg; va_start(arg,format); vsprintf((char*)USART2_TX_BUF,format,arg); va_end(arg); u16 i,j; i=strlen((const char*)USART2_TX_BUF);
for(j=0;j<i;j++){ ESP8266_SendByte(USART2_TX_BUF[j]); } }
uint8_t ESP8266_Cmd(char *cmd,char *expect_ack,u16 waittime){ ESP8266_Printf("%s\r\n",cmd); uint8_t chk=0; while(waittime--) { Delay_ms(10); if(ESP8266_Buffer.FinishFlag) { chk=ESP8266_Check_Str(ESP8266_Buffer.Body,expect_ack); if(chk){ Delay_xms(20); ESP8266_Buffer.Length =0 ; ESP8266_Buffer.Body[ESP8266_Buffer.Length] = '\0'; break; } ESP8266_Buffer.FinishFlag = 0; } if(!waittime){ printf("Time Out!\r\n"); return 1; } } return 0; }
uint8_t ESP8266_Check_Str(char* check,char* espect){ char *strx=0; strx=strstr((const char*)check,(const char*)espect); if(strx) return 1; else return 0; }
|