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_used<length&&tx_used<LENGTH(tx_buffer);tx_used++){// copy data
tx_buffer[tx_used]=data[length-1-tx_used];// put character in buffer (in reverse order)
}
if(tx_used){
usart_enable_tx_interrupt(USART(RADIO_ESP8266_USART));// enable interrupt to send bytes
rcc_periph_clock_enable(GPIO_RCC(RADIO_ESP8266_TX));// enable clock for USART TX pin port peripheral
gpio_mode_setup(GPIO_PORT(RADIO_ESP8266_TX),GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN(RADIO_ESP8266_TX));// set TX pin to alternate function
gpio_set_output_options(GPIO_PORT(RADIO_ESP8266_TX),GPIO_OTYPE_PP,GPIO_OSPEED_25MHZ,GPIO_PIN(RADIO_ESP8266_TX));// set TX pin output as push-pull
gpio_set_af(GPIO_PORT(RADIO_ESP8266_TX),RADIO_ESP8266_AF,GPIO_PIN(RADIO_ESP8266_TX));// set alternate function to USART
rcc_periph_clock_enable(GPIO_RCC(RADIO_ESP8266_RX));// enable clock for USART RX pin port peripheral
gpio_mode_setup(GPIO_PORT(RADIO_ESP8266_RX),GPIO_MODE_AF,GPIO_PUPD_PULLUP,GPIO_PIN(RADIO_ESP8266_RX));// set GPIO to alternate function, with pull up to avoid noise in case it is not connected
gpio_set_af(GPIO_PORT(RADIO_ESP8266_RX),RADIO_ESP8266_AF,GPIO_PIN(RADIO_ESP8266_RX));// set alternate function to USART
if(usart_get_interrupt_source(USART(RADIO_ESP8266_USART),USART_SR_RXNE)){// data has been received
while(rx_used>=LENGTH(rx_buffer)){// if buffer is full
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)
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 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
}elseif(rx_used>=7&&memcmp((char*)&rx_buffer[rx_used-7],"ERROR\r\n",7)==0){// ERROR received