2018-03-18 22:43:59 +00:00
|
|
|
#include "esp.h"
|
|
|
|
|
|
|
|
void init_esp(void)
|
|
|
|
{
|
2018-04-03 15:18:28 +00:00
|
|
|
// Enable GPIOa and USART2 clocks
|
2018-03-18 22:43:59 +00:00
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
|
|
|
|
2018-04-03 15:18:28 +00:00
|
|
|
// Create gpio struct and fill it with default values
|
2018-03-18 22:43:59 +00:00
|
|
|
GPIO_InitTypeDef gpio;
|
|
|
|
GPIO_StructInit(&gpio);
|
|
|
|
|
2018-04-03 15:18:28 +00:00
|
|
|
// Set PB2 to alternate function push pull (Tx)
|
2018-03-18 22:43:59 +00:00
|
|
|
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
|
|
gpio.GPIO_Pin = GPIO_Pin_2;
|
|
|
|
GPIO_Init(GPIOA, &gpio);
|
|
|
|
|
2018-04-03 15:18:28 +00:00
|
|
|
// Set PB3 to input floating (Rx)
|
2018-03-18 22:43:59 +00:00
|
|
|
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
|
|
gpio.GPIO_Pin = GPIO_Pin_3;
|
|
|
|
GPIO_Init(GPIOA, &gpio);
|
|
|
|
|
2018-04-03 15:18:28 +00:00
|
|
|
// Create usart struct and init USART2 to 115 200 baud
|
2018-03-18 22:43:59 +00:00
|
|
|
USART_InitTypeDef usart;
|
|
|
|
USART_StructInit(&usart);
|
|
|
|
usart.USART_BaudRate = 115200;
|
|
|
|
USART_Init(USART2, &usart);
|
|
|
|
|
2018-04-03 15:18:28 +00:00
|
|
|
// Init USART2 clock
|
2018-03-18 22:43:59 +00:00
|
|
|
USART_ClockInitTypeDef usartclock;
|
|
|
|
USART_ClockStructInit(&usartclock);
|
|
|
|
USART_ClockInit(USART2, &usartclock);
|
|
|
|
|
2018-04-03 15:18:28 +00:00
|
|
|
// Init NVIC for USART2 RXNE to see stuff sent to the TCP server. The ISR is in main.c
|
2018-03-18 22:43:59 +00:00
|
|
|
NVIC_InitTypeDef nvic;
|
|
|
|
nvic.NVIC_IRQChannel = USART2_IRQn;
|
|
|
|
nvic.NVIC_IRQChannelCmd = ENABLE;
|
|
|
|
nvic.NVIC_IRQChannelPreemptionPriority = 0;
|
|
|
|
nvic.NVIC_IRQChannelSubPriority = 2;
|
|
|
|
NVIC_Init(&nvic);
|
|
|
|
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
|
|
|
|
|
2018-04-03 15:18:28 +00:00
|
|
|
// Enable USART2
|
2018-03-18 22:43:59 +00:00
|
|
|
USART_Cmd(USART2, ENABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_wait()
|
|
|
|
{
|
2018-04-03 15:18:28 +00:00
|
|
|
// Wait some time so the ESP can execute the commands sent
|
2018-03-18 22:43:59 +00:00
|
|
|
int i, j;
|
|
|
|
for(j=0;j<500;j++) {
|
|
|
|
for(i=0;i<65536;i++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_str(char *str) {
|
2018-04-03 15:18:28 +00:00
|
|
|
// Sends a string over UART
|
2018-03-18 22:43:59 +00:00
|
|
|
while(*str) {
|
|
|
|
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
|
|
|
|
USART_SendData(USART2, *str++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_esp(void)
|
|
|
|
{
|
2018-04-03 15:18:28 +00:00
|
|
|
// Restore the ESP to defaults, then wait
|
2018-03-18 22:43:59 +00:00
|
|
|
send_str("AT+RESTORE\r\n");
|
|
|
|
esp_wait();
|
2018-04-03 15:18:28 +00:00
|
|
|
// Print out the current mode
|
2018-03-18 22:43:59 +00:00
|
|
|
send_str("AT+CWMODE?\r\n");
|
|
|
|
esp_wait();
|
2018-04-03 15:18:28 +00:00
|
|
|
// Set mode to access point
|
2018-03-18 22:43:59 +00:00
|
|
|
send_str("AT+CWMODE=2\r\n");
|
|
|
|
esp_wait();
|
2018-04-03 15:18:28 +00:00
|
|
|
// Reset
|
2018-03-18 22:43:59 +00:00
|
|
|
send_str("AT+RST\r\n");
|
|
|
|
esp_wait();
|
2018-04-03 15:18:28 +00:00
|
|
|
// Enable access point
|
2018-03-18 22:43:59 +00:00
|
|
|
send_str("AT+CIPMUX=1\r\n");
|
|
|
|
esp_wait();
|
2018-04-03 15:18:28 +00:00
|
|
|
// Start TCP server on port 2526
|
2018-03-18 22:43:59 +00:00
|
|
|
send_str("AT+CIPSERVER=1,2526\r\n");
|
|
|
|
esp_wait();
|
2018-04-03 15:18:28 +00:00
|
|
|
// Print out the current IP address
|
2018-03-18 22:43:59 +00:00
|
|
|
send_str("AT+CIPAP_CUR?\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void deinit_esp(void)
|
|
|
|
{
|
2018-04-03 15:18:28 +00:00
|
|
|
// Disable the interrupt, then disable USART2
|
2018-03-18 22:43:59 +00:00
|
|
|
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
|
|
|
|
USART_Cmd(USART2, DISABLE);
|
|
|
|
USART_DeInit(USART2);
|
|
|
|
}
|