diff --git a/lib/radio_esp8266.c b/lib/radio_esp8266.c index fec5f0a..bd66c23 100644 --- a/lib/radio_esp8266.c +++ b/lib/radio_esp8266.c @@ -23,6 +23,7 @@ #include // standard integer types #include // general utilities #include // string and memory utilities +#include // string utilities /* STM32 (including CM3) libraries */ #include // real-time control clock library @@ -49,6 +50,25 @@ static volatile uint16_t tx_used = 0; /**< number of bytes used in transmit buff volatile bool radio_esp8266_activity = false; volatile bool radio_esp8266_success = false; +/** transmit data to radio + * @param[in] data data to transmit + * @param[in] length length of data to transmit + */ +static void radio_esp8266_transmit(uint8_t* data, uint8_t length) { + while (tx_used || !usart_get_flag(USART(RADIO_ESP8266_USART), USART_SR_TXE)) { // wait until ongoing transmission completed + usart_enable_tx_interrupt(USART(RADIO_ESP8266_USART)); // enable transmit interrupt + __WFI(); // sleep until something happened + } + usart_disable_tx_interrupt(USART(RADIO_ESP8266_USART)); // ensure transmit interrupt is disable to prevent index corruption (the ISR should already have done it) + radio_esp8266_activity = false; // reset status because of new activity + for (tx_used=0; tx_used0) { + radio_esp8266_transmit((uint8_t*)command, length); } - usart_disable_tx_interrupt(USART(RADIO_ESP8266_USART)); // ensure transmit interrupt is disable to prevent index corruption (the ISR should already have done it) - radio_esp8266_activity = false; // reset status because of new activity - for (tx_used=0; tx_used0) { + radio_esp8266_transmit((uint8_t*)command, command_length); // transmit AT command + while (!radio_esp8266_activity || !radio_esp8266_success) { // wait for response + __WFI(); // sleep until something happened + } + if (!radio_esp8266_success) { // send AT command did not succeed + return; // don't transmit data + } + radio_esp8266_transmit(data, length); // transmit data } } +void radio_esp8266_close(void) +{ + radio_esp8266_transmit((uint8_t*)"AT+CIPCLOSE\r\n", 13); // send AT command to close established connection +} + /** USART interrupt service routine called when data has been transmitted or received */ void USART_ISR(RADIO_ESP8266_USART)(void) { @@ -126,13 +161,12 @@ void USART_ISR(RADIO_ESP8266_USART)(void) rx_used--; // update used buffer information } rx_buffer[rx_used++] = usart_recv(USART(RADIO_ESP8266_USART)); // put character in buffer - // if the used send a packet with these strings during the commands I'm fucked (AT commands are hard to handle perfectly) - // ideally memmem would not be done in ISR (takes quite long) - if (rx_used>=4 && memmem((char*)rx_buffer, rx_used, "OK\r\n", 4)) { // OK received + // 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) + if (rx_used>=4 && memcmp((char*)&rx_buffer[rx_used-4], "OK\r\n", 4)==0) { // OK received radio_esp8266_activity = true; // response received radio_esp8266_success = true; // command succeeded rx_used = 0; // reset buffer - } else if (rx_used>=7 && memmem((char*)rx_buffer, rx_used, "ERROR\r\n", 7)) { // ERROR received + } else if (rx_used>=7 && memcmp((char*)&rx_buffer[rx_used-7], "ERROR\r\n", 7)==0) { // ERROR received radio_esp8266_activity = true; // response received radio_esp8266_success = false; // command failed rx_used = 0; // reset buffer diff --git a/lib/radio_esp8266.h b/lib/radio_esp8266.h index 7842632..193db70 100644 --- a/lib/radio_esp8266.h +++ b/lib/radio_esp8266.h @@ -34,7 +34,7 @@ void radio_esp8266_setup(void); * @param[in] port TCP port to connect to * @note wait for activity to get success status */ -void radio_esp8266_tcp_open(char* host, uint32_t port); +void radio_esp8266_tcp_open(char* host, uint16_t port); /** send data (requires established connection) * @param[in] data data to send * @param[in] length size of data to send