From 7b31ace47594971956be30914b4260e4a69661ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Wed, 15 Sep 2021 17:48:05 +0200 Subject: [PATCH] esp8266: make STM32F4 compatible --- lib/radio_esp8266.c | 32 +++++++++++++++++++------------- lib/radio_esp8266.h | 2 +- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/radio_esp8266.c b/lib/radio_esp8266.c index ba65719..08cdbd6 100644 --- a/lib/radio_esp8266.c +++ b/lib/radio_esp8266.c @@ -1,8 +1,8 @@ -/** library to send data using ESP8266 WiFi SoC (code) +/** library to send data using ESP8266 WiFi SoC * @file * @author King Kévin * @copyright SPDX-License-Identifier: GPL-3.0-or-later - * @date 2016 + * @date 2016-2021 * @note peripherals used: USART @ref radio_esp8266_usart */ @@ -26,6 +26,9 @@ * @{ */ #define RADIO_ESP8266_USART 2 /**< USART peripheral */ +#define RADIO_ESP8266_TX PA2 /**< pin used for USART TX */ +#define RADIO_ESP8266_RX PA3 /**< pin used for USART RX */ +#define RADIO_ESP8266_AF GPIO_AF7 /**< alternate function for UART pins */ /** @} */ /* input and output buffers and used memory */ @@ -58,25 +61,28 @@ static void radio_esp8266_transmit(uint8_t* data, uint8_t length) { void radio_esp8266_setup(void) { - /* enable USART I/O peripheral */ - rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (USART) - rcc_periph_clock_enable(USART_PORT_RCC(RADIO_ESP8266_USART)); // enable clock for USART port peripheral - rcc_periph_clock_enable(USART_RCC(RADIO_ESP8266_USART)); // enable clock for USART peripheral - 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 - 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 - gpio_set(USART_PORT(RADIO_ESP8266_USART), USART_PIN_RX(RADIO_ESP8266_USART)); // pull up to avoid noise when not connected + // configure pins + 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 - /* setup USART parameters for ESP8266 AT firmware */ - usart_set_baudrate(USART(RADIO_ESP8266_USART), 115200); // AT firmware 0.51 (SDK 1.5.0) uses 115200 bps + // configure USART + rcc_periph_clock_enable(RCC_USART(RADIO_ESP8266_USART)); // enable clock for USART peripheral + rcc_periph_reset_pulse(RST_USART(RADIO_ESP8266_USART)); // reset peripheral + usart_set_baudrate(USART(RADIO_ESP8266_USART), 115200); usart_set_databits(USART(RADIO_ESP8266_USART), 8); usart_set_stopbits(USART(RADIO_ESP8266_USART), USART_STOPBITS_1); usart_set_mode(USART(RADIO_ESP8266_USART), USART_MODE_TX_RX); usart_set_parity(USART(RADIO_ESP8266_USART), USART_PARITY_NONE); usart_set_flow_control(USART(RADIO_ESP8266_USART), USART_FLOWCONTROL_NONE); - nvic_enable_irq(USART_IRQ(RADIO_ESP8266_USART)); // enable the USART interrupt + nvic_enable_irq(USART_IRQ(RADIO_ESP8266_USART)); // enable the UART interrupt usart_enable_rx_interrupt(USART(RADIO_ESP8266_USART)); // enable receive interrupt - usart_enable(USART(RADIO_ESP8266_USART)); // enable USART + usart_enable(USART(RADIO_ESP8266_USART)); // enable UART /* reset buffer states */ rx_used = 0; diff --git a/lib/radio_esp8266.h b/lib/radio_esp8266.h index ab99beb..b66e7f5 100644 --- a/lib/radio_esp8266.h +++ b/lib/radio_esp8266.h @@ -2,7 +2,7 @@ * @file * @author King Kévin * @copyright SPDX-License-Identifier: GPL-3.0-or-later - * @date 2016 + * @date 2016-2021 * @note peripherals used: USART @ref radio_esp8266_usart */ #pragma once