Skip to content

Commit 496e16d

Browse files
committed
Handle memory (hopefully). Short TX works
1 parent c5fa973 commit 496e16d

File tree

2 files changed

+74
-8
lines changed

2 files changed

+74
-8
lines changed

ports/esp32s2/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ $(BUILD)/firmware.uf2: $(BUILD)/circuitpython-firmware.bin
281281
flash: $(BUILD)/firmware.bin
282282
esptool.py --chip esp32s2 -p $(PORT) --no-stub -b 460800 --before=default_reset --after=hard_reset write_flash $(FLASH_FLAGS) 0x0000 $^
283283

284+
flash-circuitpython-only: $(BUILD)/circuitpython-firmware.bin
285+
esptool.py --chip esp32s2 -p $(PORT) --no-stub -b 460800 --before=default_reset --after=hard_reset write_flash $(FLASH_FLAGS) 0x10000 $^
286+
284287
include $(TOP)/py/mkrules.mk
285288

286289
# Print out the value of a make variable.

ports/esp32s2/common-hal/busio/SPI.c

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,35 @@
3232
#include "common-hal/microcontroller/Pin.h"
3333
#include "supervisor/shared/rgb_led_status.h"
3434

35+
#include "esp_log.h"
36+
37+
static const char* TAG = "CircuitPython SPI";
38+
3539
static bool spi_never_reset[SOC_SPI_PERIPH_NUM];
3640

41+
// Store one lock handle per device so that we can free it.
42+
static spi_bus_lock_dev_handle_t lock_dev_handle[SOC_SPI_PERIPH_NUM];
43+
static intr_handle_t intr_handle[SOC_SPI_PERIPH_NUM];
44+
3745
void spi_reset(void) {
3846
for (spi_host_device_t host_id = SPI2_HOST; host_id < SOC_SPI_PERIPH_NUM; host_id++) {
3947
if (spi_never_reset[host_id]) {
4048
continue;
4149
}
42-
spi_bus_free(host_id);
50+
bool in_use = false;
51+
if (lock_dev_handle[host_id] != NULL) {
52+
spi_bus_lock_unregister_dev(lock_dev_handle[host_id]);
53+
lock_dev_handle[host_id] = NULL;
54+
in_use = true;
55+
}
56+
if (intr_handle[host_id] != NULL) {
57+
esp_intr_free(intr_handle[host_id]);
58+
intr_handle[host_id] = NULL;
59+
in_use = true;
60+
}
61+
if (in_use) {
62+
spi_bus_free(host_id);
63+
}
4364
}
4465
}
4566

@@ -75,12 +96,24 @@ static bool bus_uses_iomux_pins(spi_host_device_t host, const spi_bus_config_t*
7596

7697
// End copied code.
7798

78-
static bool _spi_bus_free(spi_host_device_t host_id) {
99+
static bool spi_bus_is_free(spi_host_device_t host_id) {
79100
return spi_bus_get_attr(host_id) == NULL;
80101
}
81102

82103
static void spi_interrupt_handler(void *arg) {
104+
// busio_spi_obj_t *self = arg;
105+
}
106+
107+
// The interrupt may get invoked by the bus lock.
108+
static void spi_bus_intr_enable(void *self)
109+
{
110+
esp_intr_enable(((busio_spi_obj_t *)self)->interrupt);
111+
}
83112

113+
// The interrupt is always disabled by the ISR itself, not exposed
114+
static void spi_bus_intr_disable(void *self)
115+
{
116+
esp_intr_disable(((busio_spi_obj_t *)self)->interrupt);
84117
}
85118

86119
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
@@ -103,12 +136,12 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
103136
spi_host_device_t host_id = SPI1_HOST;
104137
self->connected_through_gpio = true;
105138
// Try and save SPI2 for pins that are on the IOMUX
106-
if (bus_uses_iomux_pins(SPI2_HOST, &bus_config) && _spi_bus_free(SPI2_HOST)) {
139+
if (bus_uses_iomux_pins(SPI2_HOST, &bus_config) && spi_bus_is_free(SPI2_HOST)) {
107140
host_id = SPI2_HOST;
108141
self->connected_through_gpio = false;
109-
} else if (_spi_bus_free(SPI3_HOST)) {
142+
} else if (spi_bus_is_free(SPI3_HOST)) {
110143
host_id = SPI3_HOST;
111-
} else if (_spi_bus_free(SPI2_HOST)) {
144+
} else if (spi_bus_is_free(SPI2_HOST)) {
112145
host_id = SPI2_HOST;
113146
}
114147
if (host_id == SPI1_HOST) {
@@ -121,21 +154,33 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
121154
} else if (result == ESP_ERR_INVALID_ARG) {
122155
mp_raise_ValueError(translate("Invalid pins"));
123156
}
157+
158+
// After this point, we need to deinit to free IDF memory so fill out self's pins.
159+
self->clock_pin = clock;
160+
self->MOSI_pin = mosi;
161+
self->MISO_pin = miso;
162+
124163
spi_bus_lock_dev_config_t config = { .flags = 0 };
164+
// The returned lock is stored in the bus lock but must be freed separately with
165+
// spi_bus_lock_unregister_dev.
125166
result = spi_bus_lock_register_dev(spi_bus_get_attr(host_id)->lock,
126167
&config,
127168
&self->lock);
128169
if (result == ESP_ERR_NO_MEM) {
170+
common_hal_busio_spi_deinit(self);
129171
mp_raise_msg(&mp_type_MemoryError, translate("ESP-IDF memory allocation failed"));
130172
}
131-
173+
lock_dev_handle[host_id] = self->lock;
132174

133175
result = esp_intr_alloc(spicommon_irqsource_for_host(host_id),
134176
bus_config.intr_flags | ESP_INTR_FLAG_INTRDISABLED,
135177
spi_interrupt_handler, self, &self->interrupt);
136178
if (result == ESP_ERR_NO_MEM) {
179+
common_hal_busio_spi_deinit(self);
137180
mp_raise_msg(&mp_type_MemoryError, translate("ESP-IDF memory allocation failed"));
138181
}
182+
intr_handle[host_id] = self->interrupt;
183+
spi_bus_lock_set_bg_control(spi_bus_get_attr(host_id)->lock, spi_bus_intr_enable, spi_bus_intr_disable, self);
139184

140185
spi_hal_context_t* hal = &self->hal_context;
141186
hal->hw = NULL; // Set by spi_hal_init
@@ -146,8 +191,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
146191
// We don't use native CS.
147192
hal->cs_setup = 0;
148193
hal->cs_hold = 0;
149-
hal->cs_pin_id = -1;
150-
hal->timing_conf = &self->timing_conf;
194+
hal->cs_pin_id = 0;
151195

152196
hal->sio = 1;
153197
hal->half_duplex = 0;
@@ -167,6 +211,13 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
167211
hal->io_mode = SPI_LL_IO_MODE_NORMAL;
168212

169213
spi_hal_init(hal, host_id);
214+
// This must be set after spi_hal_init.
215+
hal->timing_conf = &self->timing_conf;
216+
if (hal->hw == NULL) {
217+
ESP_LOGE(TAG, "SPI error %p", hal->hw);
218+
}
219+
220+
common_hal_busio_spi_configure(self, 250000, 0, 0, 8);
170221
}
171222

172223
void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
@@ -186,6 +237,14 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
186237
return;
187238
}
188239
spi_never_reset[self->host_id] = false;
240+
if (self->lock != NULL) {
241+
spi_bus_lock_unregister_dev(self->lock);
242+
lock_dev_handle[self->host_id] = NULL;
243+
}
244+
if (self->interrupt != NULL) {
245+
esp_intr_free(self->interrupt);
246+
intr_handle[self->host_id] = NULL;
247+
}
189248
spi_bus_free(self->host_id);
190249

191250
common_hal_reset_pin(self->clock_pin);
@@ -218,7 +277,11 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
218277
return false;
219278
}
220279

280+
ESP_LOGI(TAG, "configure");
281+
ESP_LOGI(TAG, "real frequency %d", self->real_frequency);
282+
ESP_LOGI(TAG, "timing_conf %p", self->hal_context.timing_conf);
221283
spi_hal_setup_device(&self->hal_context);
284+
ESP_LOGI(TAG, "setup done");
222285
return true;
223286
}
224287

0 commit comments

Comments
 (0)