aboutsummaryrefslogtreecommitdiff
path: root/stm32f091/esp8266.c
diff options
context:
space:
mode:
Diffstat (limited to 'stm32f091/esp8266.c')
-rw-r--r--stm32f091/esp8266.c238
1 files changed, 58 insertions, 180 deletions
diff --git a/stm32f091/esp8266.c b/stm32f091/esp8266.c
index 4fdf7e6..74ec347 100644
--- a/stm32f091/esp8266.c
+++ b/stm32f091/esp8266.c
@@ -2,210 +2,88 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stm32f0xx.h>
#include "esp8266.h"
#include "setup.h"
#include "consts.h"
+#include "server.h"
+#include "util.h"
-void ws_esp8266_ATsendCommand(uint8_t* data){
- char dataChar[20];
- uint8_t Tx_send[]="AT+CIPSEND=0,";
-
- itoa(strlen((char*)data),dataChar,10);
- strcat((char*)Tx_send,dataChar);
- strcat((char*)Tx_send,"\r\n");
- HAL_UART_Transmit(&huart1, Tx_send, strlen((char*)Tx_send),1000);
- HAL_Delay(2000);
- HAL_UART_Transmit(&huart1, data, strlen((char*)data),1000);
- HAL_Delay(1000);
- HAL_UART_Transmit(&huart2, data, strlen((char*)data),1000);
- HAL_Delay(5000);
-}
-int ws_esp8266_checkOK(uint8_t *receiveData,int length){
- char *ret="";
- // char *ret1="";
- HAL_UART_Transmit(&huart2, receiveData,length,1000);
- ret = strstr((char*)receiveData,"OK");
- // ret = strstr((char*)receiveData,"change");
- // memset(receiveData,0,30);
- if((ret[0]='O') && (ret[1]=='K')){
- //HAL_UART_Transmit(&huart2, (uint8_t*)ret, sizeof(ret), 100);
- return 1;
-
- }
-// else if((ret1[0]='c') && (ret1[1]=='h')){
-// //HAL_UART_Transmit(&huart2, (uint8_t*)ret, sizeof(ret), 100);
-// return 1;
-//
-// }
- else{
- return 0;
- }
-
+// macro for concise sending of multiple commands
+#define ws_esp8266_send_seq(cmd) { \
+ uint8_t _cmd[] = cmd; \
+ ws_server_send(_cmd, sizeof(_cmd)); \
}
-int ws_esp8266_receivingMsg(uint8_t *receiveData,int length){
- char *ret="";
- HAL_UART_Transmit(&huart2, receiveData,length,1000);
- ret = strstr((char*)receiveData,"+IPD");
- // memset(receiveData,0,30);
-
- if((ret[0]='+') && (ret[1]=='I')){
- //HAL_UART_Transmit(&huart2, (uint8_t*)ret, sizeof(ret), 100);
- return 1;
-
- }
- else{
- return 0;
- }
-}
-int ws_esp8266_unlink(uint8_t *receiveData,int length){
- char *ret="";
- HAL_UART_Transmit(&huart2, receiveData,length,1000);
- ret = strstr((char*)receiveData,"UNLINK");
- // memset(receiveData,0,30);
- if((ret[0]='U') && (ret[1]=='N')){
- //HAL_UART_Transmit(&huart2, (uint8_t*)ret, sizeof(ret), 100);
- return 1;
+uint8_t g_ws_esp8266_dma_rx_buffer[WS_DMA_RX_BUFFER_SIZE];
+unsigned int g_ws_esp8266_dma_rx_head = 0;
+unsigned int g_ws_esp8266_dma_rx_tail = 0;
+uint8_t g_ws_esp8266_dma_tx_buffer[WS_DMA_TX_BUFFER_SIZE];
- }
- else{
- return 0;
- }
+void DMA1_Ch1_IRQHandler(void) { HAL_DMA_IRQHandler(&hdma_usart1_rx); }
+void DMA1_Ch2_3_DMA2_Ch1_2_IRQHandler(void) { HAL_DMA_IRQHandler(&hdma_usart1_tx); }
+void USART1_IRQHandler(void) {
+ if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE)) {
+ __HAL_UART_CLEAR_IDLEFLAG(&huart1);
+ // https://stackoverflow.com/questions/71039052/hal-uartex-rxeventcallback-circular-dma-what-address-is-the-data
+ g_ws_esp8266_dma_rx_head = huart1.RxXferSize - huart1.hdmarx->Instance->CNDTR;
+ ws_esp8266_incoming_data_chunk();
+ }
+ HAL_UART_IRQHandler(&huart1);
}
-void ws_esp8266_StartEsp(){
- uint8_t Tx_AT[]="AT\r\n";
- uint8_t Rx_buffer[10]={0};
- for(int i=0;i<3;i++){
- // HAL_UART_Transmit(&huart2, hier,sizeof(hier),100);
- HAL_UART_Transmit(&huart1, Tx_AT,strlen((char*)Tx_AT), 100);
- HAL_UART_Receive(&huart1, Rx_buffer, 10, 100);
-
-
- HAL_UART_Transmit(&huart2, Rx_buffer,10,100);
- HAL_Delay(5000);
- //memset(Rx_buffer,0,sizeof(Rx_buffer));
+void ws_esp8266_incoming_data_chunk() {
+ if (g_ws_esp8266_dma_rx_head == g_ws_esp8266_dma_rx_tail) return; // no new data
+ if (g_ws_esp8266_dma_rx_head > g_ws_esp8266_dma_rx_tail) {
+ // read from tail until head
+ ws_server_req_incoming(&g_ws_esp8266_dma_rx_buffer[g_ws_esp8266_dma_rx_tail],
+ g_ws_esp8266_dma_rx_head - g_ws_esp8266_dma_rx_tail);
+ } else /* if (g_ws_esp8266_dma_rx_head < g_ws_esp8266_dma_rx_tail) */ {
+ // read from tail until end of buffer
+ ws_server_req_incoming(&g_ws_esp8266_dma_rx_buffer[g_ws_esp8266_dma_rx_tail],
+ WS_DMA_RX_BUFFER_SIZE - g_ws_esp8266_dma_rx_tail);
+ // read from buffer begin until head
+ ws_server_req_incoming(&g_ws_esp8266_dma_rx_buffer[0], // yes i know this looks dumb
+ g_ws_esp8266_dma_rx_head);
}
-
+ // finish read by shifting tail forward
+ g_ws_esp8266_dma_rx_tail = g_ws_esp8266_dma_rx_head;
}
-void ws_esp8266_disconnect(){
- int ret;
- uint8_t Tx_disconnect[]="AT+CWQAP\r\n";uint8_t buffer[17]={0};
- while(ret!=1){
-
- HAL_UART_Transmit(&huart1, Tx_disconnect,strlen((char*)Tx_disconnect), 100);
- HAL_UART_Receive(&huart1, buffer, 17, 100);
- HAL_Delay(2000);
- if(ws_esp8266_checkOK(buffer,17)==1){
- ret=1;
- }
+void ws_esp8266_send(uint8_t* data, size_t size) {
+ size_t limited_size = WS_MIN(size, WS_DMA_TX_BUFFER_SIZE - 1);
+ memcpy(g_ws_esp8266_dma_tx_buffer, data, limited_size);
+ g_ws_esp8266_dma_tx_buffer[limited_size] = 0x00;
- }
+#ifdef WS_DBG_PRINT_ESP_OVER_USART2
+ ws_dbg_set_usart2_tty_color(WS_DBG_TTY_COLOR_TX);
+ HAL_UART_Transmit(&huart2, g_ws_esp8266_dma_tx_buffer, strlen((char*) g_ws_esp8266_dma_tx_buffer), 100);
+#endif
- HAL_Delay(5000);
+ HAL_UART_Transmit_DMA(&huart1, g_ws_esp8266_dma_tx_buffer, strlen((char*) g_ws_esp8266_dma_tx_buffer));
+ __HAL_UART_ENABLE_IT(&huart1, UART_IT_TXE);
}
-void ws_esp8266_mode(){
- int ret;
- uint8_t buffer1[20]={0}; uint8_t Tx_mode[]="AT+CWMODE=1\r\n";
- while(ret!=1){
-
- HAL_UART_Transmit(&huart1, Tx_mode,strlen((char*)Tx_mode), 100);
- HAL_UART_Receive(&huart1, buffer1, 20, 100);
- HAL_Delay(1000);
-
- if(ws_esp8266_checkOK(buffer1,20)==1){
- ret=1;
-
- }
-
- }
-
- HAL_Delay(1000);
+void ws_esp8266_connect() {
+ ws_esp8266_send_seq("AT+CWJAP=\"" WS_ESP8266_WLAN_SSID "\",\"" WS_ESP8266_WLAN_PASSWD "\"\r\n");
}
-void ws_esp8266_connect(){
- uint8_t Tx_network[]="AT+CWJAP=\"" WS_ESP8266_WLAN_SSID "\",\"" WS_ESP8266_WLAN_PASSWD "\"\r\n";
-
-
- HAL_UART_Transmit(&huart1, Tx_network,strlen((char*)Tx_network),1000);
- HAL_Delay(10000);
-// HAL_UART_Transmit(&huart1, Tx_network,sizeof(Tx_network),1000);
-// HAL_Delay(10000);
-
-
-
-
-}
-void ws_esp8266_serveraan(){
- int ret;
- uint8_t buffer1[30]={0}; uint8_t Tx_server[]="AT+CIPSERVER=1," WS_SERVER_PORT "\r\n";
-
- while(ret!=1){
-
- HAL_UART_Transmit(&huart1, Tx_server,strlen((char*)Tx_server), 100);
- HAL_UART_Receive(&huart1, buffer1, 30, 100);
- HAL_Delay(2000);
-
- if(ws_esp8266_checkOK(buffer1,30)==1){
- ret=1;
-
- }
-
- }
-
- HAL_Delay(1000);
+void ws_esp8266_ap_client_mode() {
+ ws_esp8266_send_seq("AT+CWMODE=1\r\n");
}
-void ws_esp8266_serveruit(){
- //int ret;
- //uint8_t buffer1[27]={0};
- uint8_t Tx_server[]="AT+CIPSERVER=0\r\n";
-//
-// while(ret!=1){
-
- HAL_UART_Transmit(&huart1, Tx_server,strlen((char*)Tx_server), 100);
-// HAL_UART_Receive(&huart1, buffer1, 27, 100);
- HAL_Delay(3000);
-
-// if(unlink(buffer1,27)==1){
-// ret=1;
-//
-// }
-//
-// }
- HAL_Delay(1000);
+void ws_esp8266_start_tcp_server() {
+ ws_esp8266_send_seq("AT+CIPSERVER=0\r\n"); // stop tcp server (if running)
+ ws_esp8266_send_seq("AT+CIPMUX=1\r\n"); // enable multiplexing (allow multiple connections)
+ ws_esp8266_send_seq("AT+CIPSERVER=1," WS_SERVER_PORT "\r\n"); // start tcp server
}
-void ws_esp8266_mux(){
- int ret;
- uint8_t buffer2[20]={0}; uint8_t Tx_mux[]="AT+CIPMUX=1\r\n";
- while(ret!=1){
-
- HAL_UART_Transmit(&huart1, Tx_mux,strlen((char*)Tx_mux), 100);
- HAL_UART_Receive(&huart1, buffer2, 20, 100);
- HAL_Delay(2000);
-
- if(ws_esp8266_checkOK(buffer2,20)==1){
- ret=1;
-
- }
-
- }
-
- HAL_Delay(5000);
+void ws_esp8266_set_mac() {
+ ws_esp8266_send_seq("AT+CIPSTAMAC=\"" WS_ESP8266_WLAN_MAC "\"\r\n");
}
-void ws_esp8266_close(){
-
- uint8_t Tx_close[]="AT+CIPCLOSE=0\r\n";
-
-
- HAL_UART_Transmit(&huart1, Tx_close,strlen((char*)Tx_close), 100);
-
- HAL_Delay(3000);
+void ws_esp8266_set_ip() {
+ ws_esp8266_send_seq("AT+CIPSTA=\"" WS_ESP8266_WLAN_IP "\"\r\n");
}
-