-
Notifications
You must be signed in to change notification settings - Fork 215
/
espif.cpp
799 lines (704 loc) · 24.7 KB
/
espif.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
#include "espif.h"
#include <algorithm>
#include <cstring>
#include <cstdint>
#include <atomic>
#include <cassert>
#include <cinttypes>
#include <timing.h>
#include <FreeRTOS.h>
#include <task.h>
#include <semphr.h>
#include "main.h"
#include "../metric.h"
#include "wui.h"
#include <tasks.h>
#include <option/has_embedded_esp32.h>
#include "stm32f4xx_hal_rng.h"
extern "C" {
#include "stm32_port.h"
}
#include "ff.h"
#include "wui_api.h"
#include <lwip/def.h>
#include <lwip/ethip6.h>
#include <lwip/etharp.h>
#include <lwip/sys.h>
#include "log.h"
LOG_COMPONENT_DEF(ESPIF, LOG_SEVERITY_INFO);
// TODO: C++20:
// #include <bit>
// static_assert(std::endian::native == std::endian::little, "STM<->ESP protocol assumes all involved CPUs are little endian.");
static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__, "STM<->ESP protocol assumes all involved CPUs are little endian.");
static_assert(ETHARP_HWADDR_LEN == 6);
/*
* UART and other pin configuration for ESP01 module
*
* UART: USART6
* STM32 TX (ESP RX): GPIOC, GPIO_PIN_6
* STM32 RX (ESP TX): GPIOC, GPIO_PIN_7
* RESET: GPIOC, GPIO_PIN_13
* GPIO0: GPIOE, GPIO_PIN_6
* GPIO2: not connected
* CH_PD: connected to board 3.3 V
*
* UART_DMA: DMA2
* UART_RX_STREAM STREAM_1
* UART_TX_STREAM STREAM_6
*/
/*
* ESP UART NIC
*
* This provides a LwIP NIC implementation on top of a simple UART protocol used to communicate MAC address, link
* status and packets with ESP8266 attached on the other end of the UART. This requires custom FW running in the ESP
* implementing the protocol.
*
* Known issues:
* - This does not use netif state. All the state is kept in static varibles -> only on NIC is supported
* (Maybe it is worh encapsulating the state just for the sake of code clarity.)
* - This runs at 1Mbaud even when ESP support 4.5Mbaud. There is some wierd coruption at higher speeds
* (ESP seems to miss part of the packet data)
* - This does not offload checksum computation to ESP. Would be nice to enable parity and make the protocol more
* robust (i.e using some counter to match packet begin and end while ensuring no data is lost). Provided UART
* can be trusted not to alternate packet content the ESP would be able to compute packet checksums.
*
*/
#define ESP_UART_HANDLE UART_HANDLE_FOR(esp)
enum ESPIFOperatingMode {
ESPIF_UNINITIALIZED_MODE,
ESPIF_WAIT_INIT,
ESPIF_NEED_AP,
ESPIF_RUNNING_MODE,
ESPIF_FLASHING_MODE,
ESPIF_WRONG_FW,
};
enum MessageType {
MSG_DEVINFO = 0,
MSG_LINK = 1,
MSG_GETLINK = 2,
MSG_CLIENTCONFIG = 3,
MSG_PACKET = 4,
MSG_INTRON = 5,
};
#if PRINTER_TYPE == PRINTER_PRUSA_XL
// ESP32 FW version
static const uint32_t SUPPORTED_FW_VERSION = 8;
#else
// ESP8266 FW version
static const uint32_t SUPPORTED_FW_VERSION = 9;
#endif
// NIC state
static std::atomic<uint16_t> fw_version;
static std::atomic<ESPIFOperatingMode> esp_operating_mode = ESPIF_UNINITIALIZED_MODE;
static std::atomic<bool> associated = false;
static std::atomic<TaskHandle_t> init_task_handle;
static std::atomic<netif *> active_esp_netif;
// 10 seconds (20 health-check loops spaced 500ms from each other)
static std::atomic<uint8_t> init_countdown = 20;
static std::atomic<bool> seen_intron = false;
// UART
static const uint32_t NIC_UART_BAUDRATE = 4600000;
static const uint32_t FLASH_UART_BAUDRATE = 115200;
static const uint32_t CHARACTER_TIMEOUT_MS = 10;
static std::atomic<bool> esp_detected;
// Have we seen the ESP alive at least once?
// (so we never ever report it as not there or no firmware or whatever).
static std::atomic<bool> esp_was_ok = false;
uint8_t dma_buffer_rx[RX_BUFFER_LEN];
static size_t old_dma_pos = 0;
SemaphoreHandle_t uart_write_mutex = NULL;
static bool uart_has_recovered_from_error = false;
static uint8_t intron[8] = { 'U', 'N', '\x00', '\x01', '\x02', '\x03', '\x04', '\x05' };
static void uart_input(uint8_t *data, size_t size, struct netif *netif);
void espif_receive_data(UART_HandleTypeDef *huart) {
LWIP_UNUSED_ARG(huart);
notify_esp_data();
}
static void hard_reset_device() {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
osDelay(100);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
esp_detected = false;
}
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) {
if (huart->Instance == UART_INSTANCE_FOR(esp) && (huart->ErrorCode & HAL_UART_ERROR_NE || huart->ErrorCode & HAL_UART_ERROR_FE)) {
__HAL_UART_DISABLE_IT(huart, UART_IT_IDLE);
HAL_UART_DeInit(huart);
if (HAL_UART_Init(huart) != HAL_OK) {
Error_Handler();
}
if (HAL_UART_Receive_DMA(huart, (uint8_t *)dma_buffer_rx, RX_BUFFER_LEN) != HAL_OK) {
Error_Handler();
}
old_dma_pos = 0;
__HAL_UART_ENABLE_IT(huart, UART_IT_IDLE);
uart_has_recovered_from_error = true;
esp_detected = true;
}
}
static bool is_running(ESPIFOperatingMode mode) {
switch (mode) {
case ESPIF_FLASHING_MODE:
case ESPIF_UNINITIALIZED_MODE:
case ESPIF_WRONG_FW:
return false;
case ESPIF_WAIT_INIT:
case ESPIF_NEED_AP:
case ESPIF_RUNNING_MODE:
return true;
}
assert(0);
return false;
}
/**
* \brief Send data to ESP device
* \param[in] data: Pointer to data to send
* \param[in] len: Number of bytes to send
* \return Operation result, ERR_OK if succeeded
*/
static err_t espif_transmit_data(uint8_t *data, size_t len) {
if (!is_running(esp_operating_mode)) {
return ERR_USE;
}
// record metrics↵
static metric_t metric_esp_out = METRIC("esp_out", METRIC_VALUE_CUSTOM, 1000, METRIC_HANDLER_ENABLE_ALL);
static uint32_t bytes_sent = 0;
bytes_sent += len;
metric_record_custom(&metric_esp_out, " sent=%" PRIu32 "i", bytes_sent);
int ret = HAL_UART_Transmit(&ESP_UART_HANDLE, data, len, len * CHARACTER_TIMEOUT_MS);
if (ret == HAL_OK) {
return ERR_OK;
} else {
log_error(ESPIF, "UART TX fail: %d", ret);
return ERR_TIMEOUT;
}
}
static err_t espif_reconfigure_uart(const uint32_t baudrate) {
log_info(ESPIF, "Reconfiguring UART for %d baud", baudrate);
ESP_UART_HANDLE.Init.BaudRate = baudrate;
int hal_uart_res = HAL_UART_Init(&ESP_UART_HANDLE);
if (hal_uart_res != HAL_OK) {
log_error(ESPIF, "ESP LL: HAL_UART_Init failed with: %d", hal_uart_res);
return ERR_IF;
}
int hal_dma_res = HAL_UART_Receive_DMA(&ESP_UART_HANDLE, (uint8_t *)dma_buffer_rx, RX_BUFFER_LEN);
if (hal_dma_res != HAL_OK) {
log_error(ESPIF, "ESP LL: HAL_UART_Receive_DMA failed with: %d", hal_dma_res);
return ERR_IF;
}
return ERR_OK;
}
void espif_input_once(struct netif *netif) {
/* Read data */
size_t pos = 0;
/* Read data */
uint32_t dma_bytes_left = __HAL_DMA_GET_COUNTER(ESP_UART_HANDLE.hdmarx); // no. of bytes left for buffer full
pos = sizeof(dma_buffer_rx) - dma_bytes_left;
if (pos != old_dma_pos && is_running(esp_operating_mode)) {
if (pos > old_dma_pos) {
uart_input(&dma_buffer_rx[old_dma_pos], pos - old_dma_pos, netif);
} else {
uart_input(&dma_buffer_rx[old_dma_pos], sizeof(dma_buffer_rx) - old_dma_pos, netif);
if (pos > 0) {
uart_input(&dma_buffer_rx[0], pos, netif);
}
}
old_dma_pos = pos;
if (old_dma_pos == sizeof(dma_buffer_rx)) {
old_dma_pos = 0;
}
}
}
static void generate_intron();
static void process_mac(uint8_t *data, struct netif *netif) {
log_info(ESPIF, "MAC: %02x:%02x:%02x:%02x:%02x:%02x", data[0], data[1], data[2], data[3], data[4], data[5]);
netif->hwaddr_len = ETHARP_HWADDR_LEN;
memcpy(netif->hwaddr, data, ETHARP_HWADDR_LEN);
ESPIFOperatingMode old = ESPIF_WAIT_INIT;
if (esp_operating_mode.compare_exchange_strong(old, ESPIF_NEED_AP)) {
uint16_t version = fw_version.load();
if (version != SUPPORTED_FW_VERSION) {
log_error(ESPIF, "ESP detected, FW not supported: %d != %d", version, SUPPORTED_FW_VERSION);
esp_operating_mode = ESPIF_WRONG_FW;
return;
}
log_info(ESPIF, "ESP up and running");
generate_intron();
esp_operating_mode = ESPIF_NEED_AP;
esp_was_ok = true;
}
}
bool espif_link() {
return associated;
}
static void process_link_change(bool link_up, struct netif *netif) {
assert(netif != nullptr);
if (link_up) {
if (!associated.exchange(true)) {
netif_set_link_up(netif);
log_info(ESPIF, "Link went up");
}
} else {
if (associated.exchange(false)) {
log_info(ESPIF, "Link went down");
netif_set_link_down(netif);
}
}
}
static void uart_input(uint8_t *data, size_t size, struct netif *netif) {
log_debug(ESPIF, "Received ESP data len: %d", size);
esp_detected = true;
// record metrics
static metric_t metric_esp_in = METRIC("esp_in", METRIC_VALUE_CUSTOM, 1000, METRIC_HANDLER_ENABLE_ALL);
static uint32_t bytes_received = 0;
bytes_received += size;
metric_record_custom(&metric_esp_in, " recv=%" PRIu32 "i", bytes_received);
static enum ProtocolState {
Intron,
MessageType,
Packet,
PacketLen,
PacketData,
PacketDataThrowaway,
Link,
DevInfo,
FWVersion,
MACData,
} state
= Intron;
static uint intron_read = 0;
static uint fw_version_read = 0;
static uint mac_read = 0; // Amount of MAC bytes already read
static uint8_t mac_data[ETHARP_HWADDR_LEN];
static uint32_t rx_len = 0; // Length of RX packet
static uint rx_len_read = 0; // Amount of rx_len bytes already read
static struct pbuf *rx_buff = NULL; // First RX pbuf for current packet (chain head)
static struct pbuf *rx_buff_cur = NULL; // Current pbuf for data receive (part of rx_buff chain)
static uint32_t rx_read = 0; // Amount of bytes already read into rx_buff_cur
const uint8_t *end = &data[size];
for (uint8_t *c = &data[0]; c < end;) {
if (size < 200) {
log_debug(ESPIF, "Processing data at %02x = %c", *c, *c);
}
switch (state) {
case Intron:
if (*c++ == intron[intron_read]) {
intron_read++;
log_debug(ESPIF, "Intron at %d", intron_read);
if (intron_read >= sizeof(intron)) {
log_debug(ESPIF, "Intron detected");
state = MessageType;
intron_read = 0;
seen_intron = true;
}
} else {
intron_read = 0;
}
break;
case MessageType:
switch (*c) {
case MSG_DEVINFO:
log_debug(ESPIF, "Incomming devinfo message");
state = DevInfo;
break;
case MSG_LINK:
log_debug(ESPIF, "Incomming linkstatus message");
state = Link;
break;
case MSG_PACKET:
log_debug(ESPIF, "Incomming packet message");
state = Packet;
break;
default:
log_error(ESPIF, "Unknown message type %d", *c);
state = Intron;
}
c++;
break;
case Link:
process_link_change(*c++, netif);
state = Intron;
break;
case DevInfo:
state = FWVersion;
fw_version.store(0);
fw_version_read = 0;
break;
case FWVersion: {
uint16_t version_part = 0;
((uint8_t *)&version_part)[fw_version_read++] = *c++;
uint16_t new_version = fw_version.fetch_or(version_part) | version_part;
if (fw_version_read == sizeof version_part) {
log_debug(ESPIF, "ESP FW version: %d", new_version);
(void)new_version; // Avoid warning in case log_debug is disabled in compilation
mac_read = 0;
state = MACData;
}
break;
}
case MACData:
while (c < end && mac_read < sizeof(mac_data)) {
log_debug(ESPIF, "Read MAC byte at %d: %02x", mac_read, *c);
mac_data[mac_read++] = *c++;
}
if (mac_read == sizeof(mac_data)) {
process_mac(mac_data, netif);
state = Intron;
}
break;
case Packet:
rx_len_read = 0;
state = PacketLen;
break;
case PacketLen:
if (rx_len_read < sizeof(rx_len)) {
((uint8_t *)&rx_len)[rx_len_read++] = *c++;
} else {
log_debug(ESPIF, "Reading packet size: %d", rx_len);
#if ETH_PAD_SIZE
rx_len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
#endif
rx_buff = pbuf_alloc(PBUF_RAW, rx_len, PBUF_POOL);
if (rx_buff) {
rx_buff_cur = rx_buff;
rx_read = 0;
state = PacketData;
} else {
log_error(ESPIF, "Dropping packet due to out of RAM");
rx_read = 0;
state = PacketDataThrowaway;
}
}
break;
case PacketData: {
// Copy input to current pbuf (until end of input or current pbuf)
const uint32_t to_read = std::min(rx_buff_cur->len - rx_read, (uint32_t)(end - c));
memcpy((uint8_t *)rx_buff_cur->payload + rx_read, c, to_read);
c += to_read;
rx_read += to_read;
// Switch to next pbuf
if (rx_read == rx_buff_cur->len) {
rx_buff_cur = rx_buff_cur->next;
rx_read = 0;
}
// Filled all pbufs in a packet (current set to next = NULL)
if (!rx_buff_cur) {
log_debug(ESPIF, "Read packet size: %d", rx_len);
if (netif->input(rx_buff, netif) != ERR_OK) {
log_error(ESPIF, "ethernetif_input: IP input error");
pbuf_free(rx_buff);
state = Intron;
break;
}
log_debug(ESPIF, "Input packet processed ok");
state = Intron;
}
} break;
case PacketDataThrowaway:
const uint32_t to_read = std::min(rx_len - rx_read, (uint32_t)(end - c));
c += to_read;
rx_read += to_read;
if (rx_read == rx_len) {
log_debug(ESPIF, "Dropped %d packet data", rx_len);
state = Intron;
}
}
}
log_debug(ESPIF, "Processed %d from UART", size);
}
/**
* @brief Send intron
*
* Send intron sequence to ESP
*
* To improve security ESPIF generates random intron.
*/
static void generate_intron() {
xSemaphoreTake(uart_write_mutex, portMAX_DELAY);
// Send message header using old intro to ESP
espif_transmit_data(intron, sizeof(intron));
uint8_t msg_type = MSG_INTRON;
// Generate new intron
for (uint i = 2; i < sizeof(intron); i++) {
intron[i] = HAL_RNG_GetRandomNumber(&hrng);
}
log_info(ESPIF, "New intron: %.*s", 8, intron);
// Send new intron data
espif_transmit_data(&msg_type, 1);
espif_transmit_data(intron, sizeof(intron));
xSemaphoreGive(uart_write_mutex);
}
/**
* @brief Send packet using ESPIF NIC
*
* @param netif Output NETIF handle
* @param p buffer (chain) to send
*/
static err_t low_level_output(struct netif *netif, struct pbuf *p) {
if (!is_running(esp_operating_mode)) {
log_error(ESPIF, "Cannot send packet, not in running mode.");
return ERR_IF;
}
uint32_t len = p->tot_len;
log_debug(ESPIF, "Low level output packet size: %d", len);
xSemaphoreTake(uart_write_mutex, portMAX_DELAY);
espif_transmit_data(intron, sizeof(intron));
uint8_t msg_type = MSG_PACKET;
espif_transmit_data(&msg_type, 1);
espif_transmit_data((uint8_t *)&len, sizeof(len));
while (p != NULL) {
if (espif_transmit_data((uint8_t *)p->payload, p->len) != ERR_OK) {
log_error(ESPIF, "Low level output packet failed");
xSemaphoreGive(uart_write_mutex);
return ERR_IF;
}
p = p->next;
}
xSemaphoreGive(uart_write_mutex);
return ERR_OK;
}
static void force_down() {
struct netif *iface = active_esp_netif; // Atomic load
assert(iface != nullptr); // Already initialized
process_link_change(false, iface);
}
static void reset(const bool take_down_interfaces = true) {
// Reset our expectation of the intron, the ESP will forget the
// auto-generated one.
xSemaphoreTake(uart_write_mutex, portMAX_DELAY);
for (uint i = 2; i < sizeof(intron); i++) {
intron[i] = i - 2;
}
xSemaphoreGive(uart_write_mutex);
if (take_down_interfaces) {
force_down();
}
// Capture this task handle to manage wakeup from input thread
init_task_handle = xTaskGetCurrentTaskHandle();
// Reset device to receive MAC address
log_debug(ESPIF, "Resetting ESP and wait for device info reponse");
hard_reset_device();
esp_operating_mode = ESPIF_WAIT_INIT;
}
err_t espif_init_hw() {
log_info(ESPIF, "LwIP init hw");
if (uart_write_mutex) {
log_error(ESPIF, "Already initialized !!!");
assert(0);
return ERR_ALREADY;
}
espif_reconfigure_uart(NIC_UART_BAUDRATE);
esp_operating_mode = ESPIF_WAIT_INIT;
// Create mutex to protect UART writes
uart_write_mutex = xSemaphoreCreateMutex();
if (!uart_write_mutex) {
return ERR_IF;
}
return ERR_OK;
};
/**
* @brief Initalize ESPIF network interface
*
* This initializes NET interface. This is supposed to be called at most once.
*
* @param netif Interface to initialize
* @return err_t Possible error encountered during initialization
*/
err_t espif_init(struct netif *netif) {
log_info(ESPIF, "LwIP init");
#if BOARD_VER_EQUAL_TO(0, 5, 0)
// This is temporary, remove once everyone has compatible hardware.
// Requires new sandwich rev. 06 or rev. 05 with R83 removed.
#if HAS_EMBEDDED_ESP32()
wait_for_dependecies(ESP_FLASHED);
#endif
#endif
struct netif *previous = active_esp_netif.exchange(netif);
assert(previous == nullptr);
(void)previous; // Avoid warnings in release
// Initialize lwip netif
netif->name[0] = 'w';
netif->name[1] = 'l';
netif->output = etharp_output;
#if LWIP_IPV6
netif->output_ip6 = ethip6_output;
#endif
netif->linkoutput = low_level_output;
// LL init
netif->hwaddr_len = 0;
// TODO: This assumes LwIP can live with hwaddr not being set until ESP reports it
netif->mtu = 1500;
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
reset();
return ERR_OK;
}
void espif_flash_initialize(const bool take_down_interfaces) {
// NOTE: There is no extra synchronization with reader thread. This assumes
// it is not a problem if reader thread reads some garbage until it notices
// operating mode change.
// NOTE: This holds the writer mutex only during this call. Holding this one
// all the time the ESP is being flashed might block LwIP thread and prevent
// ethernet from being serviced. Still, all the writers must have finished -
// this holds the lock and new writers will fail as mode is set to flashing.
xSemaphoreTake(uart_write_mutex, portMAX_DELAY);
esp_operating_mode = ESPIF_FLASHING_MODE;
espif_reconfigure_uart(FLASH_UART_BAUDRATE);
loader_stm32_config_t loader_config = {
.huart = &ESP_UART_HANDLE,
.port_io0 = GPIOE,
#if (BOARD_IS_XBUDDY || BOARD_IS_XLBUDDY)
.pin_num_io0 = GPIO_PIN_15,
#else
.pin_num_io0 = GPIO_PIN_6,
#endif
.port_rst = GPIOC,
.pin_num_rst = GPIO_PIN_13,
};
loader_port_stm32_init(&loader_config);
xSemaphoreGive(uart_write_mutex);
if (take_down_interfaces) {
force_down();
}
}
void espif_flash_deinitialize() {
espif_reconfigure_uart(NIC_UART_BAUDRATE);
esp_operating_mode = ESPIF_RUNNING_MODE;
reset(false);
}
/**
* @brief Ask ESP to join AP
*
* This, just sends join command. It is not a big problem if network interface is not configured.
*
* @param ssid SSID
* @param pass Password
* @return err_t
*/
err_t espif_join_ap(const char *ssid, const char *pass) {
if (!is_running(esp_operating_mode)) {
return ERR_IF;
}
log_info(ESPIF, "Joining AP %s:*(%d)", ssid, strlen(pass));
esp_operating_mode = ESPIF_RUNNING_MODE;
xSemaphoreTake(uart_write_mutex, portMAX_DELAY);
espif_transmit_data(intron, sizeof(intron));
uint8_t msg_type = MSG_CLIENTCONFIG;
espif_transmit_data(&msg_type, sizeof(msg_type));
uint8_t ssid_len = strlen(ssid);
uint8_t pass_len = strlen(pass);
uint8_t ssid_buf[ssid_len];
uint8_t pass_buf[pass_len];
// No idea why, but the HAL_UART_Transmit takes non-const uint8_t *.
// Casting const->non-const is sketchy at best, but probably an immediate
// UB.
//
// To avoid that, we just make a copy. Should be fine, these things are
// short.
memcpy(ssid_buf, ssid, ssid_len);
memcpy(pass_buf, pass, pass_len);
espif_transmit_data(&ssid_len, sizeof(ssid_len));
espif_transmit_data(ssid_buf, ssid_len);
espif_transmit_data(&pass_len, sizeof(pass_len));
espif_transmit_data(pass_buf, pass_len);
xSemaphoreGive(uart_write_mutex);
return ERR_OK;
}
bool espif_tick() {
const auto current_init = init_countdown.load();
if (current_init > 0) {
// In theory, this load - condition - store sequence is racy.
// Nevertheless, we have only one thread that writes in there and it's
// atomic to allow reading things at the same time.
init_countdown.store(current_init - 1);
}
if (uart_has_recovered_from_error) {
log_info(ESPIF, "Recovered from UART error");
uart_has_recovered_from_error = false;
}
if (espif_link()) {
xSemaphoreTake(uart_write_mutex, portMAX_DELAY);
const bool was_alive = seen_intron.exchange(false);
// Poke the ESP somewhat to see if it's still alive and provoke it to
// do some activity during next round.
espif_transmit_data(intron, sizeof(intron));
uint8_t msg_type = MSG_GETLINK;
espif_transmit_data(&msg_type, sizeof(msg_type));
xSemaphoreGive(uart_write_mutex);
return was_alive;
}
return false;
}
bool espif_need_ap() {
return esp_operating_mode == ESPIF_NEED_AP;
}
void espif_reset() {
// Don't touch it in case we are flashing right now. If so, it'll get reset
// when done.
if (esp_operating_mode != ESPIF_FLASHING_MODE) {
reset();
}
}
EspFwState esp_fw_state() {
ESPIFOperatingMode mode = esp_operating_mode.load();
const bool detected = esp_detected.load();
// Once we see the ESP work at least once, we never ever complain about
// it not having firmware or similar. If we didn't do this, we could report
// it to be missing just after it is reset for inactivity. It'll likely
// just wake up in a moment.
const bool seen_ok = esp_was_ok.load();
switch (mode) {
case ESPIF_UNINITIALIZED_MODE:
if (seen_ok) {
return EspFwState::Ok;
}
return EspFwState::Unknown;
case ESPIF_WAIT_INIT:
if (seen_ok) {
return EspFwState::Ok;
}
if (detected) {
if (init_countdown > 0) {
return EspFwState::Unknown;
} else {
return EspFwState::NoFirmware;
}
} else {
return EspFwState::NoEsp;
}
case ESPIF_NEED_AP:
case ESPIF_RUNNING_MODE:
return EspFwState::Ok;
case ESPIF_FLASHING_MODE:
return EspFwState::Flashing;
case ESPIF_WRONG_FW:
return EspFwState::WrongVersion;
}
assert(0);
return EspFwState::NoEsp;
}
EspLinkState esp_link_state() {
ESPIFOperatingMode mode = esp_operating_mode.load();
switch (mode) {
case ESPIF_WAIT_INIT:
case ESPIF_WRONG_FW:
case ESPIF_FLASHING_MODE:
case ESPIF_UNINITIALIZED_MODE:
return EspLinkState::Init;
case ESPIF_NEED_AP:
return EspLinkState::NoAp;
return EspLinkState::Down;
case ESPIF_RUNNING_MODE: {
if (espif_link()) {
if (seen_intron) {
return EspLinkState::Up;
} else {
return EspLinkState::Silent;
}
} else {
return EspLinkState::Down;
}
}
}
assert(0);
return EspLinkState::Init;
}