CuVoodoo STM32F1 firmware template
radio_esp8266.c
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
22 /* standard libraries */
23 #include <stdint.h> // standard integer types
24 #include <stdlib.h> // general utilities
25 #include <string.h> // string and memory utilities
26 #include <stdio.h> // string utilities
27 
28 /* STM32 (including CM3) libraries */
29 #include <libopencm3/stm32/rcc.h> // real-time control clock library
30 #include <libopencm3/stm32/gpio.h> // general purpose input output library
31 #include <libopencm3/stm32/usart.h> // universal synchronous asynchronous receiver transmitter library
32 #include <libopencm3/cm3/nvic.h> // interrupt handler
33 #include <libopencmsis/core_cm3.h> // Cortex M3 utilities
34 
35 #include "radio_esp8266.h" // radio header and definitions
36 #include "global.h" // common methods
37 
41 #define RADIO_ESP8266_USART 2
44 /* input and output buffers and used memory */
45 static uint8_t rx_buffer[24] = {0};
46 static volatile uint16_t rx_used = 0;
47 static uint8_t tx_buffer[256] = {0};
48 static volatile uint16_t tx_used = 0;
50 volatile bool radio_esp8266_activity = false;
51 volatile bool radio_esp8266_success = false;
52 
57 static void radio_esp8266_transmit(uint8_t* data, uint8_t length) {
58  while (tx_used || !usart_get_flag(USART(RADIO_ESP8266_USART), USART_SR_TXE)) { // wait until ongoing transmission completed
59  usart_enable_tx_interrupt(USART(RADIO_ESP8266_USART)); // enable transmit interrupt
60  __WFI(); // sleep until something happened
61  }
62  usart_disable_tx_interrupt(USART(RADIO_ESP8266_USART)); // ensure transmit interrupt is disable to prevent index corruption (the ISR should already have done it)
63  radio_esp8266_activity = false; // reset status because of new activity
64  for (tx_used=0; tx_used<length && tx_used<LENGTH(tx_buffer); tx_used++) { // copy data
65  tx_buffer[tx_used] = data[length-1-tx_used]; // put character in buffer (in reverse order)
66  }
67  if (tx_used) {
68  usart_enable_tx_interrupt(USART(RADIO_ESP8266_USART)); // enable interrupt to send bytes
69  }
70 }
71 
73 {
74  /* enable USART I/O peripheral */
75  rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (USART)
76  rcc_periph_clock_enable(USART_PORT_RCC(RADIO_ESP8266_USART)); // enable clock for USART port peripheral
77  rcc_periph_clock_enable(USART_RCC(RADIO_ESP8266_USART)); // enable clock for USART peripheral
78  gpio_set_mode(USART_PORT(RADIO_ESP8266_USART), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_PIN_TX(RADIO_ESP8266_USART)); // setup GPIO pin USART transmit
79  gpio_set_mode(USART_PORT(RADIO_ESP8266_USART), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_PIN_RX(RADIO_ESP8266_USART)); // setup GPIO pin USART receive
80  gpio_set(USART_PORT(RADIO_ESP8266_USART), USART_PIN_RX(RADIO_ESP8266_USART)); // pull up to avoid noise when not connected
81 
82  /* setup USART parameters for ESP8266 AT firmware */
83  usart_set_baudrate(USART(RADIO_ESP8266_USART), 115200); // AT firmware 0.51 (SDK 1.5.0) uses 115200 bps
84  usart_set_databits(USART(RADIO_ESP8266_USART), 8);
85  usart_set_stopbits(USART(RADIO_ESP8266_USART), USART_STOPBITS_1);
86  usart_set_mode(USART(RADIO_ESP8266_USART), USART_MODE_TX_RX);
87  usart_set_parity(USART(RADIO_ESP8266_USART), USART_PARITY_NONE);
88  usart_set_flow_control(USART(RADIO_ESP8266_USART), USART_FLOWCONTROL_NONE);
89 
90  nvic_enable_irq(USART_IRQ(RADIO_ESP8266_USART)); // enable the USART interrupt
91  usart_enable_rx_interrupt(USART(RADIO_ESP8266_USART)); // enable receive interrupt
92  usart_enable(USART(RADIO_ESP8266_USART)); // enable USART
93 
94  /* reset buffer states */
95  rx_used = 0;
96  tx_used = 0;
97  radio_esp8266_activity = false;
98  radio_esp8266_success = false;
99 
100  radio_esp8266_transmit((uint8_t*)"AT\r\n",4); // verify if module is present
101  while (!radio_esp8266_activity || !radio_esp8266_success) { // wait for response
102  __WFI(); // sleep until something happened
103  }
104  radio_esp8266_transmit((uint8_t*)"AT+RST\r\n",8); // reset module
105  while (!radio_esp8266_activity || !radio_esp8266_success) { // wait for response
106  __WFI(); // sleep until something happened
107  }
108  while(rx_used<13 || memcmp((char*)&rx_buffer[rx_used-13], "WIFI GOT IP\r\n", 13)!=0) { // wait to have IP
109  __WFI(); // sleep until something happened
110  }
111  radio_esp8266_transmit((uint8_t*)"ATE0\r\n",6); // disable echoing
112  while (!radio_esp8266_activity || !radio_esp8266_success) { // wait for response
113  __WFI(); // sleep until something happened
114  }
115 }
116 
117 void radio_esp8266_tcp_open(char* host, uint16_t port)
118 {
119  char command[256] = {0}; // string to create command
120  int length = snprintf(command, LENGTH(command), "AT+CIPSTART=\"TCP\",\"%s\",%u\r\n", host, port); // create AT command to establish a TCP connection
121  if (length>0) {
122  radio_esp8266_transmit((uint8_t*)command, length);
123  }
124 }
125 
126 void radio_esp8266_send(uint8_t* data, uint8_t length)
127 {
128  char command[16+1] = {0}; // string to create command
129  int command_length = snprintf(command, LENGTH(command), "AT+CIPSEND=%u\r\n", length); // create AT command to send data
130  if (command_length>0) {
131  radio_esp8266_transmit((uint8_t*)command, command_length); // transmit AT command
132  while (!radio_esp8266_activity || !radio_esp8266_success) { // wait for response
133  __WFI(); // sleep until something happened
134  }
135  if (!radio_esp8266_success) { // send AT command did not succeed
136  return; // don't transmit data
137  }
138  radio_esp8266_transmit(data, length); // transmit data
139  }
140 }
141 
143 {
144  radio_esp8266_transmit((uint8_t*)"AT+CIPCLOSE\r\n", 13); // send AT command to close established connection
145 }
146 
149 {
150  if (usart_get_interrupt_source(USART(RADIO_ESP8266_USART), USART_SR_TXE)) { // data has been transmitted
151  if (tx_used) { // there is still data in the buffer to transmit
152  usart_send(USART(RADIO_ESP8266_USART),tx_buffer[tx_used-1]); // put data in transmit register
153  tx_used--; // update used size
154  } else { // no data in the buffer to transmit
155  usart_disable_tx_interrupt(USART(RADIO_ESP8266_USART)); // disable transmit interrupt
156  }
157  }
158  if (usart_get_interrupt_source(USART(RADIO_ESP8266_USART), USART_SR_RXNE)) { // data has been received
159  while (rx_used>=LENGTH(rx_buffer)) { // if buffer is full
160  memmove(rx_buffer,&rx_buffer[1],LENGTH(rx_buffer)-1); // drop old data to make space (ring buffer are more efficient but harder to handle)
161  rx_used--; // update used buffer information
162  }
163  rx_buffer[rx_used++] = usart_recv(USART(RADIO_ESP8266_USART)); // put character in buffer
164  // if the used send a packet with these strings during the commands detection the AT command response will break (AT commands are hard to handle perfectly)
165  if (rx_used>=4 && memcmp((char*)&rx_buffer[rx_used-4], "OK\r\n", 4)==0) { // OK received
166  radio_esp8266_activity = true; // response received
167  radio_esp8266_success = true; // command succeeded
168  rx_used = 0; // reset buffer
169  } else if (rx_used>=7 && memcmp((char*)&rx_buffer[rx_used-7], "ERROR\r\n", 7)==0) { // ERROR received
170  radio_esp8266_activity = true; // response received
171  radio_esp8266_success = false; // command failed
172  rx_used = 0; // reset buffer
173  }
174  }
175 }
volatile bool radio_esp8266_activity
a response has been returned by the radio
Definition: radio_esp8266.c:50
void radio_esp8266_tcp_open(char *host, uint16_t port)
establish TCP connection
#define USART_ISR(x)
get interrupt service routine for USART based on USART identifier
Definition: global.h:188
volatile bool radio_esp8266_success
the last command has succeeded
Definition: radio_esp8266.c:51
static void radio_esp8266_transmit(uint8_t *data, uint8_t length)
transmit data to radio
Definition: radio_esp8266.c:57
library to send data using ESP8266 WiFi SoC (API)
global definitions and methods (API)
static volatile uint16_t rx_used
number of byte in receive buffer
Definition: radio_esp8266.c:46
#define USART_IRQ(x)
get NVIC IRQ for USART based on USART identifier
Definition: global.h:186
#define USART(x)
get USART based on USART identifier
Definition: global.h:182
#define USART_PORT(x)
get port for USART based on USART identifier
Definition: global.h:190
#define USART_PORT_RCC(x)
get RCC for USART port based on USART identifier
Definition: global.h:195
void radio_esp8266_setup(void)
setup peripherals to communicate with radio
Definition: radio_esp8266.c:72
void radio_esp8266_send(uint8_t *data, uint8_t length)
send data (requires established connection)
#define LENGTH(x)
get the length of an array
Definition: global.h:26
#define USART_RCC(x)
get RCC for USART based on USART identifier
Definition: global.h:184
#define USART_PIN_RX(x)
get receive pin for USART based on USART identifier
Definition: global.h:202
#define USART_PIN_TX(x)
get transmit pin for USART based on USART identifier
Definition: global.h:200
#define RADIO_ESP8266_USART
USART peripheral.
Definition: radio_esp8266.c:41
static uint8_t rx_buffer[24]
buffer for received data (we only expect AT responses)
Definition: radio_esp8266.c:45
static uint8_t tx_buffer[256]
buffer for data to transmit
Definition: radio_esp8266.c:47
void radio_esp8266_close(void)
close established connection
static volatile uint16_t tx_used
number of bytes used in transmit buffer
Definition: radio_esp8266.c:48