8000 drivers/esp_hosted: Fix pin IRQ. · micropython/micropython@379b583 · GitHub
[go: up one dir, main page]

Skip to content

Commit 379b583

Browse files
committed
drivers/esp_hosted: Fix pin IRQ.
This patch reinstalls the pin IRQ handler on WiFi init, as some ports disable/remove pin IRQs on soft-reboot, or deinit the pins. Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent 977dc9a commit 379b583

File tree

4 files changed

+55
-38
lines changed

4 files changed

+55
-38
lines changed

drivers/esp-hosted/esp_hosted_hal.c

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,9 @@
4141
#include "esp_hosted_hal.h"
4242
#include "esp_hosted_wifi.h"
4343

44-
#ifndef MICROPY_HW_WIFI_IRQ
45-
#define MICROPY_HW_WIFI_IRQ MICROPY_HW_WIFI_HANDSHAKE
46-
#endif
44+
extern void mod_network_poll_events(void);
4745

4846
STATIC mp_obj_t esp_hosted_pin_irq_callback(mp_obj_t self_in) {
49-
extern void mod_network_poll_events(void);
5047
mod_network_poll_events();
5148
return mp_const_none;
5249
}
@@ -64,34 +61,16 @@ MP_WEAK int esp_hosted_hal_init(uint32_t mode) {
6461
mp_hal_pin_input(MICROPY_HW_WIFI_HANDSHAKE);
6562
mp_hal_pin_input(MICROPY_HW_WIFI_DATAREADY);
6663

67-
// Enable Pin-IRQ for the handshake PIN to call esp_hosted_wifi_poll()
68-
mp_obj_t irq_rising_attr[2];
69-
mp_load_method_maybe((mp_obj_t)MICROPY_HW_WIFI_IRQ, MP_QSTR_IRQ_RISING, irq_rising_attr);
70-
71-
if (irq_rising_attr[0] != MP_OBJ_NULL && irq_rising_attr[1] == MP_OBJ_NULL) { // value for IRQ rising found
72-
mp_obj_t pin_args[] = {
73-
NULL, // Method pointer
74-
(mp_obj_t)MICROPY_HW_WIFI_IRQ, // Pin object
75-
(mp_obj_t)&esp_hosted_pin_irq_callback_obj, // Callback function object
76-
NULL, // The Rising edge value is set below.
77-
mp_const_true, // Hard IRQ, since the actual polling is scheduled.
78-
};
79-
pin_args[3] = irq_rising_attr[0];
80-
mp_load_method_maybe((mp_obj_t)MICROPY_HW_WIFI_IRQ, MP_QSTR_irq, pin_args);
81-
if (pin_args[0] != MP_OBJ_NULL && pin_args[1] != MP_OBJ_NULL) {
82-
mp_call_method_n_kw(3, 0, pin_args);
83-
}
84-
}
85-
8664
// Initialize SPI.
8765
mp_obj_t args[] = {
8866
MP_OBJ_NEW_SMALL_INT(MICROPY_HW_WIFI_SPI_ID),
8967
MP_OBJ_NEW_SMALL_INT(MICROPY_HW_WIFI_SPI_BAUDRATE),
68+
MP_OBJ_NEW_QSTR(MP_QSTR_phase), MP_OBJ_NEW_SMALL_INT(0),
9069
MP_OBJ_NEW_QSTR(MP_QSTR_polarity), MP_OBJ_NEW_SMALL_INT(1),
9170
};
9271

9372
MP_STATE_PORT(mp_wifi_spi) =
94-
MP_OBJ_TYPE_GET_SLOT(&machine_spi_type, make_new)((mp_obj_t)&machine_spi_type, 2, 1, args);
73+
MP_OBJ_TYPE_GET_SLOT(&machine_spi_type, make_new)((mp_obj_t)&machine_spi_type, 2, 2, args);
9574

9675
// SPI might change the direction/mode of CS pin,
9776
// set it to GPIO again just in case.
@@ -101,16 +80,8 @@ MP_WEAK int esp_hosted_hal_init(uint32_t mode) {
10180
}
10281

10382
MP_WEAK int esp_hosted_hal_deinit(void) {
104-
// Disable Pin-IRQ for the handshake PIN
105-
mp_obj_t pin_args[] = {
106-
NULL, // Method pointer
107-
(mp_obj_t)MICROPY_HW_WIFI_IRQ, // Pin object
108-
mp_const_none // Set to None
109-
};
110-
mp_load_method_maybe((mp_obj_t)MICROPY_HW_WIFI_IRQ, MP_QSTR_irq, pin_args);
111-
if (pin_args[0] && pin_args[1]) {
112-
mp_call_method_n_kw(1, 0, pin_args);
113-
}
83+
// Disable pin IRQ.
84+
esp_hosted_hal_irq_enable(false);
11485

11586
// Remove all network interfaces and reset wifi state.
11687
esp_hosted_wifi_deinit();
@@ -134,6 +105,43 @@ MP_WEAK int esp_hosted_hal_deinit(void) {
134105
return 0;
135106
}
136107

108+
MP_WEAK void esp_hosted_hal_irq_enable(bool enable) {
109+
#ifdef MICROPY_HW_WIFI_IRQ_PIN
110+
// Disable Pin-IRQ for the handshake PIN
111+
mp_obj_t pin_args[] = {
112+
NULL, // Method pointer
113+
(mp_obj_t)MICROPY_HW_WIFI_IRQ_PIN, // Pin object
114+
mp_const_none // Set to None
115+
};
116+
mp_load_method_maybe((mp_obj_t)MICROPY_HW_WIFI_IRQ_PIN, MP_QSTR_irq, pin_args);
117+
if (pin_args[0] && pin_args[1]) {
118+
mp_call_method_n_kw(1, 0, pin_args);
119+
}
120+
121+
if (enable) {
122+
// Enable Pin-IRQ for the handshake PIN to call esp_hosted_wifi_poll()
123+
mp_obj_t irq_rising_attr[2];
124+
mp_load_method_maybe((mp_obj_t)MICROPY_HW_WIFI_IRQ_PIN, MP_QSTR_IRQ_RISING, irq_rising_attr);
125+
126+
if (irq_rising_attr[0] != MP_OBJ_NULL && irq_rising_attr[1] == MP_OBJ_NULL) {
127+
// value for IRQ rising found
128+
mp_obj_t pin_args[] = {
129+
NULL, // Method pointer
130+
(mp_obj_t)MICROPY_HW_WIFI_IRQ_PIN, // Pin object
131+
(mp_obj_t)&esp_hosted_pin_irq_callback_obj, // Callback function object
132+
NULL, // The Rising edge value is set below.
133+
mp_const_true, // Hard IRQ, since the actual polling is scheduled.
134+
};
135+
pin_args[3] = irq_rising_attr[0];
136+
mp_load_method_maybe((mp_obj_t)MICROPY_HW_WIFI_IRQ_PIN, MP_QSTR_irq, pin_args);
137+
if (pin_args[0] != MP_OBJ_NULL && pin_args[1] != MP_OBJ_NULL) {
138+
mp_call_method_n_kw(3, 0, pin_args);
139+
}
140+
}
141+
}
142+
#endif
143+
}
144+
137145
MP_WEAK int esp_hosted_hal_atomic_enter(void) {
138146
#if MICROPY_ENABLE_SCHEDULER
139147
mp_sched_lock();
@@ -149,7 +157,7 @@ MP_WEAK int esp_hosted_hal_atomic_exit(void) {
149157
}
150158

151159
MP_WEAK bool esp_hosted_hal_data_ready(void) {
152-
return mp_hal F438 _pin_read(MICROPY_HW_WIFI_DATAREADY) && mp_hal_pin_read(MICROPY_HW_WIFI_HANDSHAKE);
160+
return mp_hal_pin_read(MICROPY_HW_WIFI_DATAREADY);
153161
}
154162

155163
MP_WEAK int esp_hosted_hal_spi_transfer(const uint8_t *tx_buf, uint8_t *rx_buf, uint32_t size) {
@@ -158,7 +166,8 @@ MP_WEAK int esp_hosted_hal_spi_transfer(const uint8_t *tx_buf, uint8_t *rx_buf,
158166

159167
// Wait for handshake pin to go high.
160168
for (mp_uint_t start = mp_hal_ticks_ms(); ; mp_hal_delay_ms(1)) {
161-
if (mp_hal_pin_read(MICROPY_HW_WIFI_HANDSHAKE)) {
169+
if (mp_hal_pin_read(MICROPY_HW_WIFI_HANDSHAKE) &&
170+
(rx_buf == NULL || mp_hal_pin_read(MICROPY_HW_WIFI_DATAREADY))) {
162171
break;
163172
}
164173
if ((mp_hal_ticks_ms() - start) >= 1000) {
@@ -171,6 +180,11 @@ MP_WEAK int esp_hosted_hal_spi_transfer(const uint8_t *tx_buf, uint8_t *rx_buf,
171180
mp_hal_delay_us(10);
172181
spi_proto->transfer(mp_wifi_spi, size, tx_buf, rx_buf);
173182
mp_hal_pin_write(MICROPY_HW_WIFI_SPI_CS, 1);
183+
mp_hal_delay_us(100);
184+
185+
if (esp_hosted_hal_data_ready()) {
186+
mod_network_poll_events();
187+
}
174188
return 0;
175189
}
176190

drivers/esp-hosted/esp_hosted_hal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ typedef enum {
6868
// Note A default machine-based implementation is provided in esp_hosted_hal.c.
6969
int esp_hosted_hal_init(uint32_t mode);
7070
int esp_hosted_hal_deinit(void);
71+
void esp_hosted_hal_irq_enable(bool enable);
7172
bool esp_hosted_hal_data_ready(void);
7273
int esp_hosted_hal_atomic_enter(void);
7374
int esp_hosted_hal_atomic_exit(void);

drivers/esp-hosted/esp_hosted_internal.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
#ifndef MICROPY_INCLUDED_DRIVERS_ESP_HOSTED_INTERNAL_H
3030
#define MICROPY_INCLUDED_DRIVERS_ESP_HOSTED_INTERNAL_H
3131

32-
#define ESP_HOSTED_DEBUG (0)
33-
3432
#define ESP_FRAME_MAX_SIZE (1600)
3533
#define ESP_FRAME_MAX_PAYLOAD (ESP_FRAME_MAX_SIZE - sizeof(esp_header_t))
3634
#define ESP_FRAME_FLAGS_FRAGMENT (1 << 0)

drivers/esp-hosted/esp_hosted_wifi.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@ int esp_hosted_wifi_init(uint32_t itf) {
476476
esp_hosted_netif_init(&esp_state, itf);
477477
info_printf("esp_hosted_init() initialized itf %lu\n", itf);
478478
}
479+
480+
// Re/enable IRQ pin.
481+
esp_hosted_hal_irq_enable(true);
482+
479483
return 0;
480484
}
481485

0 commit comments

Comments
 (0)
0