summaryrefslogtreecommitdiff
path: root/ports/esp32s2/common-hal
diff options
context:
space:
mode:
Diffstat (limited to 'ports/esp32s2/common-hal')
-rw-r--r--ports/esp32s2/common-hal/analogio/AnalogIn.c98
-rw-r--r--ports/esp32s2/common-hal/analogio/AnalogIn.h42
-rw-r--r--ports/esp32s2/common-hal/analogio/AnalogOut.c73
-rw-r--r--ports/esp32s2/common-hal/analogio/AnalogOut.h43
-rw-r--r--ports/esp32s2/common-hal/analogio/__init__.c1
-rw-r--r--ports/esp32s2/common-hal/busio/I2C.c2
-rw-r--r--ports/esp32s2/common-hal/busio/I2C.h2
-rw-r--r--ports/esp32s2/common-hal/busio/SPI.c86
-rw-r--r--ports/esp32s2/common-hal/busio/SPI.h10
-rw-r--r--ports/esp32s2/common-hal/busio/UART.c2
-rw-r--r--ports/esp32s2/common-hal/busio/UART.h2
-rw-r--r--ports/esp32s2/common-hal/canio/CAN.c295
-rw-r--r--ports/esp32s2/common-hal/canio/CAN.h49
-rw-r--r--ports/esp32s2/common-hal/canio/Listener.c182
-rw-r--r--ports/esp32s2/common-hal/canio/Listener.h40
-rw-r--r--ports/esp32s2/common-hal/canio/__init__.c25
-rw-r--r--ports/esp32s2/common-hal/canio/__init__.h27
-rw-r--r--ports/esp32s2/common-hal/countio/Counter.c84
-rw-r--r--ports/esp32s2/common-hal/countio/Counter.h40
-rw-r--r--ports/esp32s2/common-hal/countio/__init__.c1
-rw-r--r--ports/esp32s2/common-hal/digitalio/DigitalInOut.c22
-rw-r--r--ports/esp32s2/common-hal/digitalio/DigitalInOut.h1
-rw-r--r--ports/esp32s2/common-hal/microcontroller/Pin.c4
-rw-r--r--ports/esp32s2/common-hal/microcontroller/Processor.c2
-rw-r--r--ports/esp32s2/common-hal/neopixel_write/__init__.c6
-rw-r--r--ports/esp32s2/common-hal/pulseio/PulseIn.h4
-rw-r--r--ports/esp32s2/common-hal/pulseio/PulseOut.h4
-rw-r--r--ports/esp32s2/common-hal/pwmio/PWMOut.c2
-rw-r--r--ports/esp32s2/common-hal/pwmio/PWMOut.h2
-rw-r--r--ports/esp32s2/common-hal/rtc/RTC.c52
-rw-r--r--ports/esp32s2/common-hal/rtc/RTC.h34
-rw-r--r--ports/esp32s2/common-hal/rtc/__init__.c0
-rw-r--r--ports/esp32s2/common-hal/socketpool/Socket.c22
-rw-r--r--ports/esp32s2/common-hal/socketpool/Socket.h2
-rw-r--r--ports/esp32s2/common-hal/socketpool/SocketPool.c2
-rw-r--r--ports/esp32s2/common-hal/ssl/SSLContext.h2
-rw-r--r--ports/esp32s2/common-hal/ssl/__init__.c2
-rw-r--r--ports/esp32s2/common-hal/touchio/TouchIn.c96
-rw-r--r--ports/esp32s2/common-hal/touchio/TouchIn.h42
-rw-r--r--ports/esp32s2/common-hal/touchio/__init__.c1
-rw-r--r--ports/esp32s2/common-hal/wifi/Network.c6
-rw-r--r--ports/esp32s2/common-hal/wifi/Network.h2
-rw-r--r--ports/esp32s2/common-hal/wifi/Radio.c97
-rw-r--r--ports/esp32s2/common-hal/wifi/Radio.h6
-rw-r--r--ports/esp32s2/common-hal/wifi/ScannedNetworks.c2
-rw-r--r--ports/esp32s2/common-hal/wifi/ScannedNetworks.h2
-rw-r--r--ports/esp32s2/common-hal/wifi/__init__.c14
47 files changed, 1460 insertions, 75 deletions
diff --git a/ports/esp32s2/common-hal/analogio/AnalogIn.c b/ports/esp32s2/common-hal/analogio/AnalogIn.c
new file mode 100644
index 000000000..bab1721ea
--- /dev/null
+++ b/ports/esp32s2/common-hal/analogio/AnalogIn.c
@@ -0,0 +1,98 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Lucian Copeland for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "common-hal/analogio/AnalogIn.h"
+#include "py/mperrno.h"
+#include "py/runtime.h"
+#include "supervisor/shared/translate.h"
+
+#include "components/driver/include/driver/adc_common.h"
+#include "components/esp_adc_cal/include/esp_adc_cal.h"
+
+#include "shared-bindings/microcontroller/Pin.h"
+
+#define DEFAULT_VREF 1100
+#define NO_OF_SAMPLES 64
+#define ATTENUATION ADC_ATTEN_DB_11
+#define DATA_WIDTH ADC_WIDTH_BIT_13
+
+void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self,
+ const mcu_pin_obj_t *pin) {
+ if (pin->adc_index == 0 || pin->adc_channel == ADC_CHANNEL_MAX) {
+ mp_raise_ValueError(translate("Pin does not have ADC capabilities"));
+ }
+ common_hal_mcu_pin_claim(pin);
+ self->pin = pin;
+}
+
+bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t *self) {
+ return self->pin == NULL;
+}
+
+void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) {
+ if (common_hal_analogio_analogin_deinited(self)) {
+ return;
+ }
+ reset_pin_number(self->pin->number);
+ self->pin = NULL;
+}
+
+uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
+ if (self->pin->adc_index == ADC_UNIT_1) {
+ adc1_config_width(DATA_WIDTH);
+ adc1_config_channel_atten((adc1_channel_t)self->pin->adc_channel, ATTENUATION);
+ } else if (self->pin->adc_index == ADC_UNIT_2) {
+ adc2_config_channel_atten((adc2_channel_t)self->pin->adc_channel, ATTENUATION);
+ }
+
+ // Automatically select calibration process depending on status of efuse
+ esp_adc_cal_characteristics_t *adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
+ esp_adc_cal_characterize(self->pin->adc_index, ATTENUATION, DATA_WIDTH, DEFAULT_VREF, adc_chars);
+
+ uint32_t adc_reading = 0;
+ //Multisampling
+ for (int i = 0; i < NO_OF_SAMPLES; i++) {
+ if (self->pin->adc_index == ADC_UNIT_1) {
+ adc_reading += adc1_get_raw((adc1_channel_t)self->pin->adc_channel);
+ } else {
+ int raw;
+ esp_err_t r = adc2_get_raw((adc2_channel_t)self->pin->adc_channel, DATA_WIDTH, &raw);
+ if ( r != ESP_OK ) {
+ mp_raise_ValueError(translate("ADC2 is being used by WiFi"));
+ }
+ adc_reading += raw;
+ }
+ }
+ adc_reading /= NO_OF_SAMPLES;
+
+ // This corrects non-linear regions of the ADC range with a LUT, so it's a better reading than raw
+ uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
+ return voltage * ((1 << 16) - 1)/3300;
+}
+
+float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) {
+ return 3.3f;
+}
diff --git a/ports/esp32s2/common-hal/analogio/AnalogIn.h b/ports/esp32s2/common-hal/analogio/AnalogIn.h
new file mode 100644
index 000000000..fed4d0217
--- /dev/null
+++ b/ports/esp32s2/common-hal/analogio/AnalogIn.h
@@ -0,0 +1,42 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Lucian Copeland for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ANALOGIO_ANALOGIN_H
+#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ANALOGIO_ANALOGIN_H
+
+#include "common-hal/microcontroller/Pin.h"
+
+#include "components/hal/include/hal/adc_types.h"
+#include "FreeRTOS.h"
+#include "freertos/semphr.h"
+#include "py/obj.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ const mcu_pin_obj_t * pin;
+} analogio_analogin_obj_t;
+
+#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ANALOGIO_ANALOGIN_H
diff --git a/ports/esp32s2/common-hal/analogio/AnalogOut.c b/ports/esp32s2/common-hal/analogio/AnalogOut.c
new file mode 100644
index 000000000..080a01b04
--- /dev/null
+++ b/ports/esp32s2/common-hal/analogio/AnalogOut.c
@@ -0,0 +1,73 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
+ * Copyright (c) 2019, Lucian Copeland for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdint.h>
+#include <string.h>
+
+#include "py/mperrno.h"
+#include "py/runtime.h"
+
+#include "shared-bindings/analogio/AnalogOut.h"
+#include "shared-bindings/microcontroller/Pin.h"
+#include "supervisor/shared/translate.h"
+
+#include "components/driver/include/driver/dac_common.h"
+
+#include "common-hal/microcontroller/Pin.h"
+
+void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
+ const mcu_pin_obj_t *pin) {
+ if (pin == &pin_GPIO17) {
+ self->channel = DAC_CHANNEL_1;
+ } else if (pin == &pin_GPIO18) {
+ self->channel = DAC_CHANNEL_2;
+ } else {
+ mp_raise_ValueError(translate("Invalid DAC pin supplied"));
+ }
+ dac_output_enable(self->channel);
+}
+
+bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {
+ return (self->channel == DAC_CHANNEL_MAX);
+}
+
+void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
+ dac_output_disable(self->channel);
+ self->channel = DAC_CHANNEL_MAX;
+}
+
+void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self,
+ uint16_t value) {
+ uint8_t dac_value = (value * 255) / 65535;
+ dac_output_enable(self->channel);
+ dac_output_voltage(self->channel, dac_value);
+}
+
+void analogout_reset(void) {
+ dac_output_disable(DAC_CHANNEL_1);
+ dac_output_disable(DAC_CHANNEL_2);
+}
diff --git a/ports/esp32s2/common-hal/analogio/AnalogOut.h b/ports/esp32s2/common-hal/analogio/AnalogOut.h
new file mode 100644
index 000000000..6285151ba
--- /dev/null
+++ b/ports/esp32s2/common-hal/analogio/AnalogOut.h
@@ -0,0 +1,43 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft
+ * Copyright (c) 2019 Lucian Copeland for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ANALOGIO_ANALOGOUT_H
+#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ANALOGIO_ANALOGOUT_H
+
+#include "common-hal/microcontroller/Pin.h"
+
+#include "py/obj.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ const mcu_pin_obj_t * pin;
+ uint8_t channel;
+} analogio_analogout_obj_t;
+
+void analogout_reset(void);
+
+#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_ANALOGIO_ANALOGOUT_H
diff --git a/ports/esp32s2/common-hal/analogio/__init__.c b/ports/esp32s2/common-hal/analogio/__init__.c
new file mode 100644
index 000000000..eea58c77d
--- /dev/null
+++ b/ports/esp32s2/common-hal/analogio/__init__.c
@@ -0,0 +1 @@
+// No analogio module functions.
diff --git a/ports/esp32s2/common-hal/busio/I2C.c b/ports/esp32s2/common-hal/busio/I2C.c
index 57270372f..6f1953128 100644
--- a/ports/esp32s2/common-hal/busio/I2C.c
+++ b/ports/esp32s2/common-hal/busio/I2C.c
@@ -28,7 +28,7 @@
#include "py/mperrno.h"
#include "py/runtime.h"
-#include "driver/i2c.h"
+#include "components/driver/include/driver/i2c.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
diff --git a/ports/esp32s2/common-hal/busio/I2C.h b/ports/esp32s2/common-hal/busio/I2C.h
index 24a3fd495..c39d6d744 100644
--- a/ports/esp32s2/common-hal/busio/I2C.h
+++ b/ports/esp32s2/common-hal/busio/I2C.h
@@ -29,7 +29,7 @@
#include "common-hal/microcontroller/Pin.h"
-#include "esp-idf/components/soc/include/hal/i2c_types.h"
+#include "components/hal/include/hal/i2c_types.h"
#include "FreeRTOS.h"
#include "freertos/semphr.h"
#include "py/obj.h"
diff --git a/ports/esp32s2/common-hal/busio/SPI.c b/ports/esp32s2/common-hal/busio/SPI.c
index a22075cd5..490419f34 100644
--- a/ports/esp32s2/common-hal/busio/SPI.c
+++ b/ports/esp32s2/common-hal/busio/SPI.c
@@ -115,6 +115,7 @@ static void spi_bus_intr_disable(void *self)
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi,
const mcu_pin_obj_t * miso) {
+
spi_bus_config_t bus_config;
bus_config.mosi_io_num = mosi != NULL ? mosi->number : -1;
bus_config.miso_io_num = miso != NULL ? miso->number : -1;
@@ -183,9 +184,6 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
// spi_hal_init clears the given hal context so set everything after.
spi_hal_init(hal, host_id);
- hal->dmadesc_tx = &self->tx_dma;
- hal->dmadesc_rx = &self->rx_dma;
- hal->dmadesc_n = 1;
// We don't use native CS.
// hal->cs_setup = 0;
@@ -196,7 +194,6 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
hal->half_duplex = 0;
// hal->tx_lsbfirst = 0;
// hal->rx_lsbfirst = 0;
- hal->dma_enabled = 1;
hal->no_compensate = 1;
// Ignore CS bits
@@ -216,8 +213,12 @@ void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
spi_never_reset[self->host_id] = true;
common_hal_never_reset_pin(self->clock_pin);
- common_hal_never_reset_pin(self->MOSI_pin);
- common_hal_never_reset_pin(self->MISO_pin);
+ if (self->MOSI_pin != NULL) {
+ common_hal_never_reset_pin(self->MOSI_pin);
+ }
+ if (self->MISO_pin != NULL) {
+ common_hal_never_reset_pin(self->MISO_pin);
+ }
}
bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) {
@@ -240,9 +241,15 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
spi_bus_free(self->host_id);
common_hal_reset_pin(self->clock_pin);
- common_hal_reset_pin(self->MOSI_pin);
- common_hal_reset_pin(self->MISO_pin);
+ if (self->MOSI_pin != NULL) {
+ common_hal_reset_pin(self->MOSI_pin);
+ }
+ if (self->MISO_pin != NULL) {
+ common_hal_reset_pin(self->MISO_pin);
+ }
self->clock_pin = NULL;
+ self->MISO_pin = NULL;
+ self->MOSI_pin = NULL;
}
bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
@@ -259,7 +266,7 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
self->bits = bits;
self->target_frequency = baudrate;
self->hal_context.timing_conf = &self->timing_conf;
- esp_err_t result = spi_hal_get_clock_conf(&self->hal_context,
+ esp_err_t result = spi_hal_cal_clock_conf(&self->hal_context,
self->target_frequency,
128 /* duty_cycle */,
self->connected_through_gpio,
@@ -297,34 +304,74 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
bool common_hal_busio_spi_write(busio_spi_obj_t *self,
const uint8_t *data, size_t len) {
+ if (self->MOSI_pin == NULL) {
+ mp_raise_ValueError(translate("No MOSI Pin"));
+ }
return common_hal_busio_spi_transfer(self, data, NULL, len);
}
bool common_hal_busio_spi_read(busio_spi_obj_t *self,
uint8_t *data, size_t len, uint8_t write_value) {
- return common_hal_busio_spi_transfer(self, NULL, data, len);
+
+ if (self->MISO_pin == NULL) {
+ mp_raise_ValueError(translate("No MISO Pin"));
+ }
+ if (self->MOSI_pin == NULL) {
+ return common_hal_busio_spi_transfer(self, NULL, data, len);
+ } else {
+ memset(data, write_value, len);
+ return common_hal_busio_spi_transfer(self, data, data, len);
+ }
}
bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, const uint8_t *data_out, uint8_t *data_in, size_t len) {
if (len == 0) {
return true;
}
+ // Other than the read special case, stop transfers that don't have a pin/array match
+ if (!self->MOSI_pin && (data_out != data_in)) {
+ mp_raise_ValueError(translate("No MOSI Pin"));
+ }
+ if (!self->MISO_pin && data_in) {
+ mp_raise_ValueError(translate("No MISO Pin"));
+ }
spi_hal_context_t* hal = &self->hal_context;
hal->send_buffer = NULL;
hal->rcv_buffer = NULL;
// Reset timing_conf in case we've moved since the last time we used it.
hal->timing_conf = &self->timing_conf;
+ lldesc_t tx_dma __attribute__((aligned(16)));
+ lldesc_t rx_dma __attribute__((aligned(16)));
+ hal->dmadesc_tx = &tx_dma;
+ hal->dmadesc_rx = &rx_dma;
+ hal->dmadesc_n = 1;
+
+ size_t burst_length;
+ // If both of the incoming pointers are DMA capable then use DMA. Otherwise, do
+ // bursts the size of the SPI data buffer without DMA.
+ if ((data_out == NULL || esp_ptr_dma_capable(data_out)) &&
+ (data_in == NULL || esp_ptr_dma_capable(data_out))) {
+ hal->dma_enabled = 1;
+ burst_length = LLDESC_MAX_NUM_PER_DESC;
+ } else {
+ hal->dma_enabled = 0;
+ burst_length = sizeof(hal->hw->data_buf);
+ // When switching to non-DMA, we need to make sure DMA is off. Otherwise,
+ // the S2 will transmit zeroes instead of our data.
+ spi_ll_txdma_disable(hal->hw);
+ }
+
// This rounds up.
- size_t dma_count = (len + LLDESC_MAX_NUM_PER_DESC - 1) / LLDESC_MAX_NUM_PER_DESC;
- for (size_t i = 0; i < dma_count; i++) {
- size_t offset = LLDESC_MAX_NUM_PER_DESC * i;
- size_t dma_len = len - offset;
- if (dma_len > LLDESC_MAX_NUM_PER_DESC) {
- dma_len = LLDESC_MAX_NUM_PER_DESC;
+ size_t burst_count = (len + burst_length - 1) / burst_length;
+ for (size_t i = 0; i < burst_count; i++) {
+ size_t offset = burst_length * i;
+ size_t this_length = len - offset;
+ if (this_length > burst_length) {
+ this_length = burst_length;
}
- hal->tx_bitlen = dma_len * self->bits;
- hal->rx_bitlen = dma_len * self->bits;
+ hal->tx_bitlen = this_length * self->bits;
+ hal->rx_bitlen = this_length * self->bits;
if (data_out != NULL) {
hal->send_buffer = (uint8_t*) data_out + offset;
}
@@ -341,6 +388,9 @@ bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, const uint8_t *data_ou
}
spi_hal_fetch_result(hal);
}
+ hal->dmadesc_tx = NULL;
+ hal->dmadesc_rx = NULL;
+ hal->dmadesc_n = 0;
return true;
}
diff --git a/ports/esp32s2/common-hal/busio/SPI.h b/ports/esp32s2/common-hal/busio/SPI.h
index 6d8203831..f6c1c344a 100644
--- a/ports/esp32s2/common-hal/busio/SPI.h
+++ b/ports/esp32s2/common-hal/busio/SPI.h
@@ -29,9 +29,9 @@
#include "common-hal/microcontroller/Pin.h"
-#include "esp-idf/components/driver/include/driver/spi_common_internal.h"
-#include "esp-idf/components/soc/include/hal/spi_hal.h"
-#include "esp-idf/components/soc/include/hal/spi_types.h"
+#include "components/driver/include/driver/spi_common_internal.h"
+#include "components/hal/include/hal/spi_hal.h"
+#include "components/hal/include/hal/spi_types.h"
#include "py/obj.h"
typedef struct {
@@ -44,10 +44,6 @@ typedef struct {
spi_hal_context_t hal_context;
spi_hal_timing_conf_t timing_conf;
intr_handle_t interrupt;
- // IDF allocates these in DMA accessible memory so they may need to move when
- // we use external RAM for CircuitPython.
- lldesc_t tx_dma;
- lldesc_t rx_dma;
uint32_t target_frequency;
int32_t real_frequency;
uint8_t polarity;
diff --git a/ports/esp32s2/common-hal/busio/UART.c b/ports/esp32s2/common-hal/busio/UART.c
index b0162adba..98cebba67 100644
--- a/ports/esp32s2/common-hal/busio/UART.c
+++ b/ports/esp32s2/common-hal/busio/UART.c
@@ -27,7 +27,7 @@
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/busio/UART.h"
-#include "driver/uart.h"
+#include "components/driver/include/driver/uart.h"
#include "mpconfigport.h"
#include "lib/utils/interrupt_char.h"
diff --git a/ports/esp32s2/common-hal/busio/UART.h b/ports/esp32s2/common-hal/busio/UART.h
index b3cc92966..1d7f13511 100644
--- a/ports/esp32s2/common-hal/busio/UART.h
+++ b/ports/esp32s2/common-hal/busio/UART.h
@@ -29,7 +29,7 @@
#include "common-hal/microcontroller/Pin.h"
-#include "esp-idf/components/soc/include/hal/uart_types.h"
+#include "components/hal/include/hal/uart_types.h"
#include "py/obj.h"
typedef struct {
diff --git a/ports/esp32s2/common-hal/canio/CAN.c b/ports/esp32s2/common-hal/canio/CAN.c
new file mode 100644
index 000000000..768fde243
--- /dev/null
+++ b/ports/esp32s2/common-hal/canio/CAN.c
@@ -0,0 +1,295 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <string.h>
+
+#include "py/runtime.h"
+#include "py/mperrno.h"
+
+#include "common-hal/canio/CAN.h"
+#include "shared-bindings/microcontroller/Pin.h"
+#include "shared-bindings/util.h"
+#include "supervisor/port.h"
+
+#include "hal/twai_types.h"
+
+STATIC bool reserved_can;
+
+twai_timing_config_t get_t_config(int baudrate) {
+ switch(baudrate) {
+ case 1000000:
+ {
+ // TWAI_TIMING_CONFIG_abc expands to a C designated initializer list
+ // { .brp = 4, ...}. This is only acceptable to the compiler as an
+ // initializer and 'return TWAI_TIMING_CONFIG_1MBITS()` is not valid.
+ // Instead, introduce a temporary, named variable and return it.
+ twai_timing_config_t t_config = TWAI_TIMING_CONFIG_1MBITS();
+ return t_config;
+ }
+ case 800000:
+ {
+ twai_timing_config_t t_config = TWAI_TIMING_CONFIG_800KBITS();
+ return t_config;
+ }
+ case 500000:
+ {
+ twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS();
+ return t_config;
+ }
+ case 250000:
+ {
+ twai_timing_config_t t_config = TWAI_TIMING_CONFIG_250KBITS();
+ return t_config;
+ }
+ case 125000:
+ {
+ twai_timing_config_t t_config = TWAI_TIMING_CONFIG_125KBITS();
+ return t_config;
+ }
+ case 100000:
+ {
+ twai_timing_config_t t_config = TWAI_TIMING_CONFIG_100KBITS();
+ return t_config;
+ }
+ case 50000:
+ {
+ twai_timing_config_t t_config = TWAI_TIMING_CONFIG_50KBITS();
+ return t_config;
+ }
+ case 25000:
+ {
+ twai_timing_config_t t_config = TWAI_TIMING_CONFIG_25KBITS();
+ return t_config;
+ }
+ case 20000:
+ {
+ twai_timing_config_t t_config = TWAI_TIMING_CONFIG_20KBITS();
+ return t_config;
+ }
+ case 16000:
+ {
+ twai_timing_config_t t_config = TWAI_TIMING_CONFIG_16KBITS();
+ return t_config;
+ }
+ case 12500:
+ {
+ twai_timing_config_t t_config = TWAI_TIMING_CONFIG_12_5KBITS();
+ return t_config;
+ }
+ case 10000:
+ {
+ twai_timing_config_t t_config = TWAI_TIMING_CONFIG_10KBITS();
+ return t_config;
+ }
+ case 5000:
+ {
+ twai_timing_config_t t_config = TWAI_TIMING_CONFIG_5KBITS();
+ return t_config;
+ }
+ case 1000:
+ {
+ twai_timing_config_t t_config = TWAI_TIMING_CONFIG_1KBITS();
+ return t_config;
+ }
+ default:
+ mp_raise_ValueError(translate("Baudrate not supported by peripheral"));
+ }
+}
+
+void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *tx, mcu_pin_obj_t *rx, int baudrate, bool loopback, bool silent)
+{
+#define DIV_ROUND(a, b) (((a) + (b)/2) / (b))
+#define DIV_ROUND_UP(a, b) (((a) + (b) - 1) / (b))
+ if (reserved_can) {
+ mp_raise_ValueError(translate("All CAN peripherals are in use"));
+ }
+
+ if (loopback && silent) {
+ mp_raise_ValueError(translate("loopback + silent mode not supported by peripheral"));
+ }
+
+ twai_timing_config_t t_config = get_t_config(baudrate);
+ twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(-1, -1, TWAI_MODE_NORMAL);
+ g_config.tx_io = tx->number;
+ g_config.rx_io = rx->number;
+ if (loopback) {
+ g_config.mode = TWAI_MODE_NO_ACK;
+ }
+ if (silent) {
+ g_config.mode = TWAI_MODE_LISTEN_ONLY;
+ }
+
+ twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
+
+ esp_err_t result = twai_driver_install(&g_config, &t_config, &f_config);
+ if (result == ESP_ERR_NO_MEM) {
+ mp_raise_msg(&mp_type_MemoryError, translate("ESP-IDF memory allocation failed"));
+ } else if (result == ESP_ERR_INVALID_ARG) {
+ mp_raise_ValueError(translate("Invalid pins"));
+ } else if (result != ESP_OK) {
+ mp_raise_OSError_msg_varg(translate("twai_driver_install returned esp-idf error #%d"), (int)result);
+ }
+
+ result = twai_start();
+ if (result != ESP_OK) {
+ mp_raise_OSError_msg_varg(translate("twai_start returned esp-idf error #%d"), (int)result);
+ }
+
+ self->silent = silent;
+ self->loopback = loopback;
+ self->baudrate = baudrate;
+ self->tx_pin = tx;
+ self->rx_pin = rx;
+
+ claim_pin(tx);
+ claim_pin(rx);
+
+ reserved_can = true;
+}
+
+bool common_hal_canio_can_loopback_get(canio_can_obj_t *self)
+{
+ return self->loopback;
+}
+
+int common_hal_canio_can_baudrate_get(canio_can_obj_t *self)
+{
+ return self->baudrate;
+}
+
+int common_hal_canio_can_transmit_error_count_get(canio_can_obj_t *self)
+{
+ twai_status_info_t info;
+ twai_get_status_info(&info);
+ return info.tx_error_counter;
+}
+
+int common_hal_canio_can_receive_error_count_get(canio_can_obj_t *self)
+{
+ twai_status_info_t info;
+ twai_get_status_info(&info);
+ return info.rx_error_counter;
+}
+
+canio_bus_state_t common_hal_canio_can_state_get(canio_can_obj_t *self) {
+ twai_status_info_t info;
+ twai_get_status_info(&info);
+ if (info.state == TWAI_STATE_BUS_OFF || info.state == TWAI_STATE_RECOVERING) {
+ return BUS_STATE_OFF;
+ }
+ if (info.tx_error_counter > 127 || info.rx_error_counter > 127) {
+ return BUS_STATE_ERROR_PASSIVE;
+ }
+ if (info.tx_error_counter > 96 || info.rx_error_counter > 96) {
+ return BUS_STATE_ERROR_WARNING;
+ }
+ return BUS_STATE_ERROR_ACTIVE;
+}
+
+static void can_restart(void) {
+ twai_status_info_t info;
+ twai_get_status_info(&info);
+ if (info.state != TWAI_STATE_BUS_OFF) {
+ return;
+ }
+ twai_initiate_recovery();
+ // wait 100ms (hard coded for now) for bus to recover
+ uint64_t deadline = port_get_raw_ticks(NULL) + 100;
+ do {
+ twai_get_status_info(&info);
+ } while (port_get_raw_ticks(NULL) < deadline && (info.state == TWAI_STATE_BUS_OFF || info.state == TWAI_STATE_RECOVERING));
+}
+
+void canio_maybe_auto_restart(canio_can_obj_t *self) {
+ if (self->auto_restart) can_restart();
+}
+
+void common_hal_canio_can_restart(canio_can_obj_t *self) {
+ if (!common_hal_canio_can_auto_restart_get(self)) {
+ can_restart();
+ }
+}
+
+bool common_hal_canio_can_auto_restart_get(canio_can_obj_t *self) {
+ return self->auto_restart;
+}
+
+void common_hal_canio_can_auto_restart_set(canio_can_obj_t *self, bool value) {
+ self->auto_restart = value;
+ canio_maybe_auto_restart(self);
+}
+
+void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message_in)
+{
+ canio_maybe_auto_restart(self);
+ canio_message_obj_t *message = message_in;
+ bool rtr = message->base.type == &canio_remote_transmission_request_type;
+ twai_message_t message_out = {
+ .extd = message->extended,
+ .rtr = rtr,
+ .self = self->loopback,
+ .identifier = message->id,
+ .data_length_code = message->size,
+ };
+ if (!rtr) {
+ memcpy(message_out.data, message->data, message->size);
+ }
+ // Allow transmission to occur in background
+ twai_transmit(&message_out, 0);
+}
+
+bool common_hal_canio_can_silent_get(canio_can_obj_t *self) {
+ return self->silent;
+}
+
+bool common_hal_canio_can_deinited(canio_can_obj_t *self) {
+ return !self->tx_pin;
+}
+
+void common_hal_canio_can_check_for_deinit(canio_can_obj_t *self) {
+ if (common_hal_canio_can_deinited(self)) {
+ raise_deinited_error();
+ }
+}
+
+void common_hal_canio_can_deinit(canio_can_obj_t *self)
+{
+ if (self->tx_pin) {
+ (void)twai_stop();
+ (void)twai_driver_uninstall();
+ reset_pin_number(self->tx_pin->number);
+ reset_pin_number(self->rx_pin->number);
+ reserved_can = false;
+ }
+ self->tx_pin = NULL;
+ self->rx_pin = NULL;
+}
+
+void common_hal_canio_reset(void) {
+ (void)twai_stop();
+ (void)twai_driver_uninstall();
+ reserved_can = false;
+}
diff --git a/ports/esp32s2/common-hal/canio/CAN.h b/ports/esp32s2/common-hal/canio/CAN.h
new file mode 100644
index 000000000..85fb6972b
--- /dev/null
+++ b/ports/esp32s2/common-hal/canio/CAN.h
@@ -0,0 +1,49 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include "py/obj.h"
+#include "shared-bindings/canio/__init__.h"
+#include "shared-bindings/canio/CAN.h"
+#include "common-hal/microcontroller/Pin.h"
+#include "common-hal/canio/__init__.h"
+#include "shared-module/canio/Message.h"
+
+#include "driver/twai.h"
+
+#define FILTER_BANK_COUNT (28)
+
+typedef struct canio_can_obj {
+ mp_obj_base_t base;
+ int baudrate;
+ const mcu_pin_obj_t *rx_pin;
+ const mcu_pin_obj_t *tx_pin;
+ bool loopback:1;
+ bool silent:1;
+ bool auto_restart:1;
+ bool fifo_in_use:1;
+} canio_can_obj_t;
diff --git a/ports/esp32s2/common-hal/canio/Listener.c b/ports/esp32s2/common-hal/canio/Listener.c
new file mode 100644
index 000000000..fddbfeb58
--- /dev/null
+++ b/ports/esp32s2/common-hal/canio/Listener.c
@@ -0,0 +1,182 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <math.h>
+#include <string.h>
+
+#include "py/obj.h"
+#include "py/runtime.h"
+
+#include "common-hal/canio/__init__.h"
+#include "common-hal/canio/Listener.h"
+#include "shared-bindings/canio/Listener.h"
+#include "shared-bindings/util.h"
+#include "supervisor/shared/tick.h"
+#include "supervisor/shared/safe_mode.h"
+
+#include "hal/twai_ll.h"
+
+// IDE = "extended ID" flag of packet header. We always add this bit to the
+// mask because a match is always for just one kind of address length
+#define FILTER16_IDE (1<<3)
+#define FILTER32_IDE (1<<2)
+
+// Work around a problem reported at
+// https://github.com/espressif/esp-idf/issues/6020 where
+// twai_ll_set_acc_filter does not work under -Os optimization
+__attribute__((optimize("O0")))
+__attribute__((noinline))
+static void canio_set_acc_filter(twai_dev_t* hw, uint32_t code, uint32_t mask, bool single_filter)
+{
+ uint32_t code_swapped = __builtin_bswap32(code);
+ uint32_t mask_swapped = __builtin_bswap32(mask);
+ for (int i = 0; i < 4; i++) {
+ hw->acceptance_filter.acr[i].val = ((code_swapped >> (i * 8)) & 0xFF);
+ hw->acceptance_filter.amr[i].val = ((mask_swapped >> (i * 8)) & 0xFF);
+ }
+ hw->mode_reg.afm = single_filter;
+}
+
+STATIC void install_standard_filter(canio_listener_obj_t *self, canio_match_obj_t *match) {
+ canio_set_acc_filter(&TWAI, match->id << 21, ~(match->mask << 21), true);
+ self->extended = false;
+ self->standard = true;
+}
+
+STATIC void install_extended_filter(canio_listener_obj_t *self, canio_match_obj_t *match) {
+ canio_set_acc_filter(&TWAI, match->id << 3, ~(match->mask << 3), true);
+ self->extended = true;
+ self->standard = false;
+}
+
+STATIC void install_all_match_filter(canio_listener_obj_t *self) {
+ canio_set_acc_filter(&TWAI, 0u, ~0u, true);
+ self->extended = true;
+ self->standard = true;
+}
+
+__attribute__((noinline,optimize("O0")))
+void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **matches) {
+ twai_ll_enter_reset_mode(&TWAI);
+
+ if (!nmatch) {
+ install_all_match_filter(self);
+ } else {
+ canio_match_obj_t *match = matches[0];
+ if (match->extended) {
+ install_extended_filter(self, match);
+ } else {
+ install_standard_filter(self, match);
+ }
+ }
+
+ twai_ll_exit_reset_mode(&TWAI);
+}
+
+
+void common_hal_canio_listener_construct(canio_listener_obj_t *self, canio_can_obj_t *can, size_t nmatch, canio_match_obj_t **matches, float timeout) {
+ if (can->fifo_in_use) {
+ mp_raise_ValueError(translate("All RX FIFOs in use"));
+ }
+ if (nmatch > 1) {
+ mp_raise_ValueError(translate("Filters too complex"));
+ }
+
+ // Nothing can fail now so it's safe to assign self->can
+ can->fifo_in_use = 1;
+ self->can = can;
+ self->pending = false;
+
+ set_filters(self, nmatch, matches);
+ self->extended = self->standard = true;
+
+ common_hal_canio_listener_set_timeout(self, timeout);
+}
+
+void common_hal_canio_listener_set_timeout(canio_listener_obj_t *self, float timeout) {
+ self->timeout_ms = (int)MICROPY_FLOAT_C_FUN(ceil)(timeout * 1000);
+}
+
+float common_hal_canio_listener_get_timeout(canio_listener_obj_t *self) {
+ return self->timeout_ms / 1000.0f;
+}
+
+void common_hal_canio_listener_check_for_deinit(canio_listener_obj_t *self) {
+ if (!self->can) {
+ raise_deinited_error();
+ }
+ common_hal_canio_can_check_for_deinit(self->can);
+}
+
+// The API has no peek method so we must receive a packet into a holding area
+// and then we can say that we have 1 message pending
+int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self) {
+ while (!self->pending && twai_receive(&self->message_in, 0) == ESP_OK) {
+ if (self->message_in.extd && self->extended) {
+ self->pending = true;
+ }
+ if (!self->message_in.extd && self->standard) {
+ self->pending = true;
+ }
+ }
+ return self->pending;
+}
+
+mp_obj_t common_hal_canio_listener_receive(canio_listener_obj_t *self) {
+ if (!common_hal_canio_listener_in_waiting(self)) {
+ uint64_t deadline = supervisor_ticks_ms64() + self->timeout_ms;
+ do {
+ if (supervisor_ticks_ms64() > deadline) {
+ return NULL;
+ }
+ } while (!common_hal_canio_listener_in_waiting(self));
+ }
+
+ bool rtr = self->message_in.rtr;
+
+ int dlc = self->message_in.data_length_code;
+ canio_message_obj_t *message = m_new_obj(canio_message_obj_t);
+ message->base.type = rtr ? &canio_remote_transmission_request_type : &canio_message_type;
+ message->extended = self->message_in.extd;
+ message->id = self->message_in.identifier;
+ message->size = dlc;
+
+ if (!rtr) {
+ MP_STATIC_ASSERT(sizeof(self->message_in.data) == sizeof(message->data));
+ memcpy(message->data, self->message_in.data, sizeof(message->data));
+ }
+
+ self->pending = false;
+
+ return message;
+}
+
+void common_hal_canio_listener_deinit(canio_listener_obj_t *self) {
+ if (self->can) {
+ self->can->fifo_in_use = false;
+ }
+ self->can = NULL;
+}
diff --git a/ports/esp32s2/common-hal/canio/Listener.h b/ports/esp32s2/common-hal/canio/Listener.h
new file mode 100644
index 000000000..d24900e43
--- /dev/null
+++ b/ports/esp32s2/common-hal/canio/Listener.h
@@ -0,0 +1,40 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include "common-hal/canio/CAN.h"
+#include "shared-module/canio/Match.h"
+
+typedef struct canio_listener_obj {
+ mp_obj_base_t base;
+ canio_can_obj_t *can;
+ bool extended:1;
+ bool standard:1;
+ bool pending:1;
+ twai_message_t message_in;
+ uint32_t timeout_ms;
+} canio_listener_obj_t;
diff --git a/ports/esp32s2/common-hal/canio/__init__.c b/ports/esp32s2/common-hal/canio/__init__.c
new file mode 100644
index 000000000..7932bfc2d
--- /dev/null
+++ b/ports/esp32s2/common-hal/canio/__init__.c
@@ -0,0 +1,25 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
diff --git a/ports/esp32s2/common-hal/canio/__init__.h b/ports/esp32s2/common-hal/canio/__init__.h
new file mode 100644
index 000000000..20b6638cd
--- /dev/null
+++ b/ports/esp32s2/common-hal/canio/__init__.h
@@ -0,0 +1,27 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
diff --git a/ports/esp32s2/common-hal/countio/Counter.c b/ports/esp32s2/common-hal/countio/Counter.c
new file mode 100644
index 000000000..fe52d93ad
--- /dev/null
+++ b/ports/esp32s2/common-hal/countio/Counter.c
@@ -0,0 +1,84 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 microDev
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "common-hal/countio/Counter.h"
+#include "common-hal/microcontroller/Pin.h"
+
+#include "py/runtime.h"
+#include "supervisor/shared/translate.h"
+
+void common_hal_countio_counter_construct(countio_counter_obj_t* self,
+ const mcu_pin_obj_t* pin) {
+ claim_pin(pin);
+
+ // Prepare configuration for the PCNT unit
+ const pcnt_config_t pcnt_config = {
+ // Set PCNT input signal and control GPIOs
+ .pulse_gpio_num = pin->number,
+ .ctrl_gpio_num = PCNT_PIN_NOT_USED,
+ .channel = PCNT_CHANNEL_0,
+ // What to do on the positive / negative edge of pulse input?
+ .pos_mode = PCNT_COUNT_INC, // Count up on the positive edge
+ .neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge
+ };
+
+ // Initialize PCNT unit
+ const int8_t unit = peripherals_pcnt_init(pcnt_config);
+ if (unit == -1) {
+ mp_raise_RuntimeError(translate("All PCNT units in use"));
+ }
+
+ self->pin = pin->number;
+ self->unit = (pcnt_unit_t)unit;
+}
+
+bool common_hal_countio_counter_deinited(countio_counter_obj_t* self) {
+ return self->unit == PCNT_UNIT_MAX;
+}
+
+void common_hal_countio_counter_deinit(countio_counter_obj_t* self) {
+ if (common_hal_countio_counter_deinited(self)) {
+ return;
+ }
+ reset_pin_number(self->pin);
+ peripherals_pcnt_deinit(&self->unit);
+}
+
+mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t* self) {
+ int16_t count;
+ pcnt_get_counter_value(self->unit, &count);
+ return count+self->count;
+}
+
+void common_hal_countio_counter_set_count(countio_counter_obj_t* self,
+ mp_int_t new_count) {
+ self->count = new_count;
+ pcnt_counter_clear(self->unit);
+}
+
+void common_hal_countio_counter_reset(countio_counter_obj_t* self) {
+ common_hal_countio_counter_set_count(self, 0);
+}
diff --git a/ports/esp32s2/common-hal/countio/Counter.h b/ports/esp32s2/common-hal/countio/Counter.h
new file mode 100644
index 000000000..20fe5b83e
--- /dev/null
+++ b/ports/esp32s2/common-hal/countio/Counter.h
@@ -0,0 +1,40 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 microDev
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNTER_H
+#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNTER_H
+
+#include "py/obj.h"
+#include "peripherals/pcnt.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ uint8_t pin;
+ mp_int_t count;
+ pcnt_unit_t unit;
+} countio_counter_obj_t;
+
+#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNT_H
diff --git a/ports/esp32s2/common-hal/countio/__init__.c b/ports/esp32s2/common-hal/countio/__init__.c
new file mode 100644
index 000000000..b95b20d15
--- /dev/null
+++ b/ports/esp32s2/common-hal/countio/__init__.c
@@ -0,0 +1 @@
+//No countio module functions
diff --git a/ports/esp32s2/common-hal/digitalio/DigitalInOut.c b/ports/esp32s2/common-hal/digitalio/DigitalInOut.c
index fb3ee10ad..152db1e71 100644
--- a/ports/esp32s2/common-hal/digitalio/DigitalInOut.c
+++ b/ports/esp32s2/common-hal/digitalio/DigitalInOut.c
@@ -28,9 +28,9 @@
#include "py/runtime.h"
#include "supervisor/shared/translate.h"
-#include "driver/gpio.h"
+#include "components/driver/include/driver/gpio.h"
-#include "esp-idf/components/soc/include/hal/gpio_hal.h"
+#include "components/hal/include/hal/gpio_hal.h"
void common_hal_digitalio_digitalinout_never_reset(
digitalio_digitalinout_obj_t *self) {
@@ -42,6 +42,16 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct(
claim_pin(pin);
self->pin = pin;
+ gpio_config_t config;
+ config.pin_bit_mask = 1ull << pin->number;
+ config.mode = GPIO_MODE_INPUT;
+ config.pull_up_en = GPIO_PULLUP_DISABLE;
+ config.pull_down_en = GPIO_PULLDOWN_DISABLE;
+ config.intr_type = GPIO_INTR_DISABLE;
+ if (gpio_config(&config) != ESP_OK) {
+ return DIGITALINOUT_PIN_BUSY;
+ }
+
return DIGITALINOUT_OK;
}
@@ -67,7 +77,7 @@ void common_hal_digitalio_digitalinout_switch_to_input(
digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output(
digitalio_digitalinout_obj_t *self, bool value,
digitalio_drive_mode_t drive_mode) {
- gpio_set_level(self->pin->number, value);
+ common_hal_digitalio_digitalinout_set_value(self, value);
return common_hal_digitalio_digitalinout_set_drive_mode(self, drive_mode);
}
@@ -82,12 +92,16 @@ digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
void common_hal_digitalio_digitalinout_set_value(
digitalio_digitalinout_obj_t *self, bool value) {
+ self->output_value = value;
gpio_set_level(self->pin->number, value);
}
bool common_hal_digitalio_digitalinout_get_value(
digitalio_digitalinout_obj_t *self) {
- return gpio_get_level(self->pin->number) == 1;
+ if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_INPUT) {
+ return gpio_get_level(self->pin->number) == 1;
+ }
+ return self->output_value;
}
digitalinout_result_t common_hal_digitalio_digitalinout_set_drive_mode(
diff --git a/ports/esp32s2/common-hal/digitalio/DigitalInOut.h b/ports/esp32s2/common-hal/digitalio/DigitalInOut.h
index 6877f1741..c09c5502d 100644
--- a/ports/esp32s2/common-hal/digitalio/DigitalInOut.h
+++ b/ports/esp32s2/common-hal/digitalio/DigitalInOut.h
@@ -33,6 +33,7 @@
typedef struct {
mp_obj_base_t base;
const mcu_pin_obj_t *pin;
+ bool output_value;
} digitalio_digitalinout_obj_t;
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_DIGITALIO_DIGITALINOUT_H
diff --git a/ports/esp32s2/common-hal/microcontroller/Pin.c b/ports/esp32s2/common-hal/microcontroller/Pin.c
index 546dca848..3c2611efe 100644
--- a/ports/esp32s2/common-hal/microcontroller/Pin.c
+++ b/ports/esp32s2/common-hal/microcontroller/Pin.c
@@ -31,8 +31,8 @@
#include "py/mphal.h"
-#include "esp-idf/components/driver/include/driver/gpio.h"
-#include "esp-idf/components/soc/include/hal/gpio_hal.h"
+#include "components/driver/include/driver/gpio.h"
+#include "components/hal/include/hal/gpio_hal.h"
#ifdef MICROPY_HW_NEOPIXEL
bool neopixel_in_use;
diff --git a/ports/esp32s2/common-hal/microcontroller/Processor.c b/ports/esp32s2/common-hal/microcontroller/Processor.c
index c64fa010e..b81521601 100644
--- a/ports/esp32s2/common-hal/microcontroller/Processor.c
+++ b/ports/esp32s2/common-hal/microcontroller/Processor.c
@@ -34,7 +34,7 @@
#include "soc/efuse_reg.h"
-#include "esp-idf/components/driver/esp32s2/include/driver/temp_sensor.h"
+#include "components/driver/esp32s2/include/driver/temp_sensor.h"
float common_hal_mcu_processor_get_temperature(void) {
float tsens_out;
diff --git a/ports/esp32s2/common-hal/neopixel_write/__init__.c b/ports/esp32s2/common-hal/neopixel_write/__init__.c
index 193d754f4..63d50bf14 100644
--- a/ports/esp32s2/common-hal/neopixel_write/__init__.c
+++ b/ports/esp32s2/common-hal/neopixel_write/__init__.c
@@ -43,8 +43,8 @@
#include "py/mphal.h"
#include "py/runtime.h"
#include "shared-bindings/neopixel_write/__init__.h"
-#include "driver/rmt.h"
-#include "rmt.h"
+#include "components/driver/include/driver/rmt.h"
+#include "peripherals/rmt.h"
#define WS2812_T0H_NS (350)
#define WS2812_T0L_NS (1000)
@@ -125,4 +125,6 @@ void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout
// Free channel again
esp32s2_peripherals_free_rmt(config.channel);
+ // Swap pin back to GPIO mode
+ gpio_set_direction(digitalinout->pin->number, GPIO_MODE_OUTPUT);
}
diff --git a/ports/esp32s2/common-hal/pulseio/PulseIn.h b/ports/esp32s2/common-hal/pulseio/PulseIn.h
index 97d70d4b0..289605ed0 100644
--- a/ports/esp32s2/common-hal/pulseio/PulseIn.h
+++ b/ports/esp32s2/common-hal/pulseio/PulseIn.h
@@ -30,8 +30,8 @@
#include "common-hal/microcontroller/Pin.h"
#include "py/obj.h"
-#include "driver/rmt.h"
-#include "rmt.h"
+#include "components/driver/include/driver/rmt.h"
+#include "peripherals/rmt.h"
typedef struct {
mp_obj_base_t base;
diff --git a/ports/esp32s2/common-hal/pulseio/PulseOut.h b/ports/esp32s2/common-hal/pulseio/PulseOut.h
index 629591af6..c3952452f 100644
--- a/ports/esp32s2/common-hal/pulseio/PulseOut.h
+++ b/ports/esp32s2/common-hal/pulseio/PulseOut.h
@@ -28,8 +28,8 @@
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_PULSEIO_PULSEOUT_H
#include "common-hal/microcontroller/Pin.h"
-#include "driver/rmt.h"
-#include "rmt.h"
+#include "components/driver/include/driver/rmt.h"
+#include "peripherals/rmt.h"
#include "py/obj.h"
diff --git a/ports/esp32s2/common-hal/pwmio/PWMOut.c b/ports/esp32s2/common-hal/pwmio/PWMOut.c
index d745b7789..e1fdd4760 100644
--- a/ports/esp32s2/common-hal/pwmio/PWMOut.c
+++ b/ports/esp32s2/common-hal/pwmio/PWMOut.c
@@ -28,7 +28,7 @@
#include "common-hal/pwmio/PWMOut.h"
#include "shared-bindings/pwmio/PWMOut.h"
#include "py/runtime.h"
-#include "driver/ledc.h"
+#include "components/driver/include/driver/ledc.h"
#define INDEX_EMPTY 0xFF
diff --git a/ports/esp32s2/common-hal/pwmio/PWMOut.h b/ports/esp32s2/common-hal/pwmio/PWMOut.h
index c489e5a27..9055dee8e 100644
--- a/ports/esp32s2/common-hal/pwmio/PWMOut.h
+++ b/ports/esp32s2/common-hal/pwmio/PWMOut.h
@@ -28,7 +28,7 @@
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_PWMIO_PWMOUT_H
#include "common-hal/microcontroller/Pin.h"
-#include "driver/ledc.h"
+#include "components/driver/include/driver/ledc.h"
typedef struct {
mp_obj_base_t base;
diff --git a/ports/esp32s2/common-hal/rtc/RTC.c b/ports/esp32s2/common-hal/rtc/RTC.c
new file mode 100644
index 000000000..4d8713639
--- /dev/null
+++ b/ports/esp32s2/common-hal/rtc/RTC.c
@@ -0,0 +1,52 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright 2019 Sony Semiconductor Solutions Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <sys/time.h>
+
+#include "py/obj.h"
+#include "py/runtime.h"
+#include "components/soc/soc/include/soc/rtc_periph.h"
+#include "shared-bindings/rtc/RTC.h"
+
+void common_hal_rtc_get_time(timeutils_struct_time_t *tm) {
+ struct timeval tv_now;
+ gettimeofday(&tv_now, NULL);
+ timeutils_seconds_since_2000_to_struct_time(tv_now.tv_sec, tm);
+}
+
+void common_hal_rtc_set_time(timeutils_struct_time_t *tm) {
+ struct timeval tv_now = {0};
+ tv_now.tv_sec = timeutils_seconds_since_2000(tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
+ settimeofday(&tv_now, NULL);
+}
+
+int common_hal_rtc_get_calibration(void) {
+ return 0;
+}
+
+void common_hal_rtc_set_calibration(int calibration) {
+ mp_raise_NotImplementedError(translate("RTC calibration is not supported on this board"));
+}
diff --git a/ports/esp32s2/common-hal/rtc/RTC.h b/ports/esp32s2/common-hal/rtc/RTC.h
new file mode 100644
index 000000000..e51f1f784
--- /dev/null
+++ b/ports/esp32s2/common-hal/rtc/RTC.h
@@ -0,0 +1,34 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 Noralf Trønnes
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_RTC_RTC_H
+#define MICROPY_INCLUDED_NRF_COMMON_HAL_RTC_RTC_H
+
+extern void rtc_init(void);
+extern void rtc_reset(void);
+extern void common_hal_rtc_init(void);
+
+#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_RTC_RTC_H
diff --git a/ports/esp32s2/common-hal/rtc/__init__.c b/ports/esp32s2/common-hal/rtc/__init__.c
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/ports/esp32s2/common-hal/rtc/__init__.c
diff --git a/ports/esp32s2/common-hal/socketpool/Socket.c b/ports/esp32s2/common-hal/socketpool/Socket.c
index 0a994c604..750415dc7 100644
--- a/ports/esp32s2/common-hal/socketpool/Socket.c
+++ b/ports/esp32s2/common-hal/socketpool/Socket.c
@@ -79,15 +79,15 @@ mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t* self, const
mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
size_t received = 0;
- ssize_t last_read = 1;
+ int status = 0;
uint64_t start_ticks = supervisor_ticks_ms64();
int sockfd;
esp_err_t err = esp_tls_get_conn_sockfd(self->tcp, &sockfd);
if (err != ESP_OK) {
mp_raise_OSError(MP_EBADF);
}
- while (received < len &&
- last_read > 0 &&
+ while (received == 0 &&
+ status >= 0 &&
(self->timeout_ms == 0 || supervisor_ticks_ms64() - start_ticks <= self->timeout_ms) &&
!mp_hal_is_interrupted()) {
RUN_BACKGROUND_TASKS;
@@ -95,27 +95,25 @@ mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self,
if (available == 0) {
// This reads the raw socket buffer and is used for non-TLS connections
// and between encrypted TLS blocks.
- int status = lwip_ioctl(sockfd, FIONREAD, &available);
- if (status < 0) {
- last_read = status;
- break;
- }
+ status = lwip_ioctl(sockfd, FIONREAD, &available);
}
size_t remaining = len - received;
if (available > remaining) {
available = remaining;
}
if (available > 0) {
- last_read = esp_tls_conn_read(self->tcp, (void*) buf + received, available);
- received += last_read;
+ status = esp_tls_conn_read(self->tcp, (void*) buf + received, available);
+ if (status > 0) {
+ received += status;
+ }
}
}
- if (last_read == 0) {
+ if (received == 0) {
// socket closed
common_hal_socketpool_socket_close(self);
}
- if (last_read < 0) {
+ if (status < 0) {
mp_raise_BrokenPipeError();
}
return received;
diff --git a/ports/esp32s2/common-hal/socketpool/Socket.h b/ports/esp32s2/common-hal/socketpool/Socket.h
index c23dd171c..62b5ded58 100644
--- a/ports/esp32s2/common-hal/socketpool/Socket.h
+++ b/ports/esp32s2/common-hal/socketpool/Socket.h
@@ -32,7 +32,7 @@
#include "common-hal/socketpool/SocketPool.h"
#include "common-hal/ssl/SSLContext.h"
-#include "esp-idf/components/esp-tls/esp_tls.h"
+#include "components/esp-tls/esp_tls.h"
typedef struct {
mp_obj_base_t base;
diff --git a/ports/esp32s2/common-hal/socketpool/SocketPool.c b/ports/esp32s2/common-hal/socketpool/SocketPool.c
index 4a07f21c4..1d4d04b03 100644
--- a/ports/esp32s2/common-hal/socketpool/SocketPool.c
+++ b/ports/esp32s2/common-hal/socketpool/SocketPool.c
@@ -29,7 +29,7 @@
#include "py/runtime.h"
#include "shared-bindings/wifi/__init__.h"
-#include "esp-idf/components/lwip/lwip/src/include/lwip/netdb.h"
+#include "components/lwip/lwip/src/include/lwip/netdb.h"
#include "bindings/espidf/__init__.h"
diff --git a/ports/esp32s2/common-hal/ssl/SSLContext.h b/ports/esp32s2/common-hal/ssl/SSLContext.h
index 41d199f08..e359ac0de 100644
--- a/ports/esp32s2/common-hal/ssl/SSLContext.h
+++ b/ports/esp32s2/common-hal/ssl/SSLContext.h
@@ -29,7 +29,7 @@
#include "py/obj.h"
-#include "esp-idf/components/esp-tls/esp_tls.h"
+#include "components/esp-tls/esp_tls.h"
typedef struct {
mp_obj_base_t base;
diff --git a/ports/esp32s2/common-hal/ssl/__init__.c b/ports/esp32s2/common-hal/ssl/__init__.c
index a105e5362..8d386d0c3 100644
--- a/ports/esp32s2/common-hal/ssl/__init__.c
+++ b/ports/esp32s2/common-hal/ssl/__init__.c
@@ -26,7 +26,7 @@
#include "shared-bindings/ssl/SSLContext.h"
-#include "esp-idf/components/mbedtls/esp_crt_bundle/include/esp_crt_bundle.h"
+#include "components/mbedtls/esp_crt_bundle/include/esp_crt_bundle.h"
void common_hal_ssl_create_default_context(ssl_sslcontext_obj_t* self) {
memset(&self->ssl_config, 0, sizeof(esp_tls_cfg_t));
diff --git a/ports/esp32s2/common-hal/touchio/TouchIn.c b/ports/esp32s2/common-hal/touchio/TouchIn.c
new file mode 100644
index 000000000..3e3e4b551
--- /dev/null
+++ b/ports/esp32s2/common-hal/touchio/TouchIn.c
@@ -0,0 +1,96 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "shared-bindings/touchio/TouchIn.h"
+#include "py/runtime.h"
+
+#include "driver/touch_pad.h"
+
+static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
+ uint32_t touch_value;
+ touch_pad_read_raw_data((touch_pad_t)self->pin->touch_channel, &touch_value);
+ if (touch_value > UINT16_MAX) {
+ return UINT16_MAX;
+ }
+ return touch_value;
+}
+
+void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self,
+ const mcu_pin_obj_t *pin) {
+ if (pin->touch_channel == TOUCH_PAD_MAX) {
+ mp_raise_ValueError(translate("Invalid pin"));
+ }
+ claim_pin(pin);
+
+ touch_pad_init();
+ touch_pad_config((touch_pad_t)pin->touch_channel);
+
+ touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
+ touch_pad_fsm_start();
+
+ // wait for "raw data" to reset
+ mp_hal_delay_ms(10);
+
+ // Initial values for pins will vary, depending on what peripherals the pins
+ // share on-chip.
+
+ // Set a "touched" threshold not too far above the initial value.
+ // For simple finger touch, the values may vary as much as a factor of two,
+ // but for touches using fruit or other objects, the difference is much less.
+
+ self->pin = pin;
+ self->threshold = get_raw_reading(self) + 100;
+}
+
+bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t* self) {
+ return self->pin == NULL;
+}
+
+void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self) {
+ if (common_hal_touchio_touchin_deinited(self)) {
+ return;
+ }
+ touch_pad_deinit();
+ reset_pin_number(self->pin->touch_channel);
+ self->pin = NULL;
+}
+
+bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) {
+ uint16_t reading = get_raw_reading(self);
+ return reading > self->threshold;
+}
+
+uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self) {
+ return get_raw_reading(self);
+}
+
+uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self) {
+ return self->threshold;
+}
+
+void common_hal_touchio_touchin_set_threshold(touchio_touchin_obj_t *self, uint16_t new_threshold) {
+ self->threshold = new_threshold;
+}
diff --git a/ports/esp32s2/common-hal/touchio/TouchIn.h b/ports/esp32s2/common-hal/touchio/TouchIn.h
new file mode 100644
index 000000000..585bb37bf
--- /dev/null
+++ b/ports/esp32s2/common-hal/touchio/TouchIn.h
@@ -0,0 +1,42 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_TOUCHIO_TOUCHIN_H
+#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_TOUCHIO_TOUCHIN_H
+
+#include "common-hal/microcontroller/Pin.h"
+
+#include "py/obj.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ const mcu_pin_obj_t * pin;
+ uint16_t threshold;
+} touchio_touchin_obj_t;
+
+void touchin_reset(void);
+
+#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_TOUCHIO_TOUCHIN_H
diff --git a/ports/esp32s2/common-hal/touchio/__init__.c b/ports/esp32s2/common-hal/touchio/__init__.c
new file mode 100644
index 000000000..d2290447c
--- /dev/null
+++ b/ports/esp32s2/common-hal/touchio/__init__.c
@@ -0,0 +1 @@
+// No touchio module functions.
diff --git a/ports/esp32s2/common-hal/wifi/Network.c b/ports/esp32s2/common-hal/wifi/Network.c
index 0320e8f92..e90b3d552 100644
--- a/ports/esp32s2/common-hal/wifi/Network.c
+++ b/ports/esp32s2/common-hal/wifi/Network.c
@@ -35,6 +35,12 @@ mp_obj_t common_hal_wifi_network_get_ssid(wifi_network_obj_t *self) {
return mp_obj_new_str(cstr, strlen(cstr));
}
+#define MAC_ADDRESS_LENGTH 6
+
+mp_obj_t common_hal_wifi_network_get_bssid(wifi_network_obj_t *self) {
+ return mp_obj_new_bytes(self->record.bssid, MAC_ADDRESS_LENGTH);
+}
+
mp_obj_t common_hal_wifi_network_get_rssi(wifi_network_obj_t *self) {
return mp_obj_new_int(self->record.rssi);
}
diff --git a/ports/esp32s2/common-hal/wifi/Network.h b/ports/esp32s2/common-hal/wifi/Network.h
index ab75fcafe..9345bb457 100644
--- a/ports/esp32s2/common-hal/wifi/Network.h
+++ b/ports/esp32s2/common-hal/wifi/Network.h
@@ -29,7 +29,7 @@
#include "py/obj.h"
-#include "esp-idf/components/esp_wifi/include/esp_wifi_types.h"
+#include "components/esp_wifi/include/esp_wifi_types.h"
typedef struct {
mp_obj_base_t base;
diff --git a/ports/esp32s2/common-hal/wifi/Radio.c b/ports/esp32s2/common-hal/wifi/Radio.c
index 3c6ab5757..275bae31a 100644
--- a/ports/esp32s2/common-hal/wifi/Radio.c
+++ b/ports/esp32s2/common-hal/wifi/Radio.c
@@ -25,6 +25,7 @@
*/
#include "shared-bindings/wifi/Radio.h"
+#include "shared-bindings/wifi/Network.h"
#include <string.h>
@@ -35,8 +36,10 @@
#include "shared-bindings/wifi/ScannedNetworks.h"
#include "shared-module/ipaddress/__init__.h"
-#include "esp-idf/components/esp_wifi/include/esp_wifi.h"
-#include "esp-idf/components/lwip/include/apps/ping/ping_sock.h"
+#include "components/esp_wifi/include/esp_wifi.h"
+#include "components/lwip/include/apps/ping/ping_sock.h"
+
+#define MAC_ADDRESS_LENGTH 6
static void start_station(wifi_radio_obj_t *self) {
if (self->sta_mode) {
@@ -50,6 +53,8 @@ static void start_station(wifi_radio_obj_t *self) {
}
esp_wifi_set_mode(next_mode);
+ self->sta_mode = 1;
+
esp_wifi_set_config(WIFI_MODE_STA, &self->sta_config);
}
@@ -73,8 +78,6 @@ void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) {
}
}
-#define MAC_ADDRESS_LENGTH 6
-
mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self) {
uint8_t mac[MAC_ADDRESS_LENGTH];
esp_wifi_get_mac(ESP_IF_WIFI_STA, mac);
@@ -104,14 +107,39 @@ void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self) {
self->current_scan = NULL;
}
-wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t* ssid, size_t ssid_len, uint8_t* password, size_t password_len, uint8_t channel, mp_float_t timeout) {
+mp_obj_t common_hal_wifi_radio_get_hostname(wifi_radio_obj_t *self) {
+ const char *hostname = NULL;
+ esp_netif_get_hostname(self->netif, &hostname);
+ if (hostname == NULL) {
+ return mp_const_none;
+ }
+ return mp_obj_new_str(hostname, strlen(hostname));
+}
+
+void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname) {
+ esp_netif_set_hostname(self->netif, hostname);
+}
+
+wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t* ssid, size_t ssid_len, uint8_t* password, size_t password_len, uint8_t channel, mp_float_t timeout, uint8_t* bssid, size_t bssid_len) {
// check enabled
+ start_station(self);
+
wifi_config_t* config = &self->sta_config;
memcpy(&config->sta.ssid, ssid, ssid_len);
config->sta.ssid[ssid_len] = 0;
memcpy(&config->sta.password, password, password_len);
config->sta.password[password_len] = 0;
config->sta.channel = channel;
+ // From esp_wifi_types.h:
+ // Generally, station_config.bssid_set needs to be 0; and it needs
+ // to be 1 only when users need to check the MAC address of the AP
+ if (bssid_len > 0){
+ memcpy(&config->sta.bssid, bssid, bssid_len);
+ config->sta.bssid[bssid_len] = 0;
+ config->sta.bssid_set = 1;
+ } else {
+ config->sta.bssid_set = 0;
+ }
esp_wifi_set_config(ESP_IF_WIFI_STA, config);
self->starting_retries = 5;
self->retries_left = 5;
@@ -137,13 +165,66 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
return WIFI_RADIO_ERROR_NONE;
}
+mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self) {
+ if (!esp_netif_is_netif_up(self->netif)) {
+ return mp_const_none;
+ }
+
+ // Make sure the interface is in STA mode
+ if (!self->sta_mode){
+ return mp_const_none;
+ }
+
+ wifi_network_obj_t *ap_info = m_new_obj(wifi_network_obj_t);
+ ap_info->base.type = &wifi_network_type;
+ // From esp_wifi.h, the possible return values (typos theirs):
+ // ESP_OK: succeed
+ // ESP_ERR_WIFI_CONN: The station interface don't initialized
+ // ESP_ERR_WIFI_NOT_CONNECT: The station is in disconnect status
+ if (esp_wifi_sta_get_ap_info(&self->ap_info.record) != ESP_OK){
+ return mp_const_none;
+ } else {
+ memcpy(&ap_info->record, &self->ap_info.record, sizeof(wifi_ap_record_t));
+ return MP_OBJ_FROM_PTR(ap_info);
+ }
+}
+
+mp_obj_t common_hal_wifi_radio_get_ipv4_gateway(wifi_radio_obj_t *self) {
+ if (!esp_netif_is_netif_up(self->netif)) {
+ return mp_const_none;
+ }
+ esp_netif_get_ip_info(self->netif, &self->ip_info);
+ return common_hal_ipaddress_new_ipv4address(self->ip_info.gw.addr);
+}
+
+mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self) {
+ if (!esp_netif_is_netif_up(self->netif)) {
+ return mp_const_none;
+ }
+ esp_netif_get_ip_info(self->netif, &self->ip_info);
+ return common_hal_ipaddress_new_ipv4address(self->ip_info.netmask.addr);
+}
+
mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) {
if (!esp_netif_is_netif_up(self->netif)) {
return mp_const_none;
}
- esp_netif_ip_info_t ip_info;
- esp_netif_get_ip_info(self->netif, &ip_info);
- return common_hal_ipaddress_new_ipv4address(ip_info.ip.addr);
+ esp_netif_get_ip_info(self->netif, &self->ip_info);
+ return common_hal_ipaddress_new_ipv4address(self->ip_info.ip.addr);
+}
+
+mp_obj_t common_hal_wifi_radio_get_ipv4_dns(wifi_radio_obj_t *self) {
+ if (!esp_netif_is_netif_up(self->netif)) {
+ return mp_const_none;
+ }
+
+ esp_netif_get_dns_info(self->netif, ESP_NETIF_DNS_MAIN, &self->dns_info);
+
+ // dns_info is of type esp_netif_dns_info_t, which is just ever so slightly
+ // different than esp_netif_ip_info_t used for
+ // common_hal_wifi_radio_get_ipv4_address (includes both ipv4 and 6),
+ // so some extra jumping is required to get to the actual address
+ return common_hal_ipaddress_new_ipv4address(self->dns_info.ip.u_addr.ip4.addr);
}
mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, mp_float_t timeout) {
diff --git a/ports/esp32s2/common-hal/wifi/Radio.h b/ports/esp32s2/common-hal/wifi/Radio.h
index 205aef176..c7f484eaf 100644
--- a/ports/esp32s2/common-hal/wifi/Radio.h
+++ b/ports/esp32s2/common-hal/wifi/Radio.h
@@ -29,9 +29,10 @@
#include "py/obj.h"
-#include "esp-idf/components/esp_event/include/esp_event.h"
+#include "components/esp_event/include/esp_event.h"
#include "shared-bindings/wifi/ScannedNetworks.h"
+#include "shared-bindings/wifi/Network.h"
// Event bits for the Radio event group.
#define WIFI_SCAN_DONE_BIT BIT0
@@ -46,6 +47,9 @@ typedef struct {
StaticEventGroup_t event_group;
EventGroupHandle_t event_group_handle;
wifi_config_t sta_config;
+ wifi_network_obj_t ap_info;
+ esp_netif_ip_info_t ip_info;
+ esp_netif_dns_info_t dns_info;
esp_netif_t *netif;
bool started;
bool ap_mode;
diff --git a/ports/esp32s2/common-hal/wifi/ScannedNetworks.c b/ports/esp32s2/common-hal/wifi/ScannedNetworks.c
index 507c6d186..cc733308d 100644
--- a/ports/esp32s2/common-hal/wifi/ScannedNetworks.c
+++ b/ports/esp32s2/common-hal/wifi/ScannedNetworks.c
@@ -37,7 +37,7 @@
#include "shared-bindings/wifi/Radio.h"
#include "shared-bindings/wifi/ScannedNetworks.h"
-#include "esp-idf/components/esp_wifi/include/esp_wifi.h"
+#include "components/esp_wifi/include/esp_wifi.h"
static void wifi_scannednetworks_done(wifi_scannednetworks_obj_t *self) {
self->done = true;
diff --git a/ports/esp32s2/common-hal/wifi/ScannedNetworks.h b/ports/esp32s2/common-hal/wifi/ScannedNetworks.h
index cd57e95f2..36da387f2 100644
--- a/ports/esp32s2/common-hal/wifi/ScannedNetworks.h
+++ b/ports/esp32s2/common-hal/wifi/ScannedNetworks.h
@@ -35,7 +35,7 @@
#include "FreeRTOS.h"
#include "freertos/event_groups.h"
-#include "esp-idf/components/esp_wifi/include/esp_wifi_types.h"
+#include "components/esp_wifi/include/esp_wifi_types.h"
typedef struct {
mp_obj_base_t base;
diff --git a/ports/esp32s2/common-hal/wifi/__init__.c b/ports/esp32s2/common-hal/wifi/__init__.c
index b9ea9da06..833ef0623 100644
--- a/ports/esp32s2/common-hal/wifi/__init__.c
+++ b/ports/esp32s2/common-hal/wifi/__init__.c
@@ -31,13 +31,14 @@
#include "py/runtime.h"
-#include "esp-idf/components/esp_wifi/include/esp_wifi.h"
+#include "components/esp_wifi/include/esp_wifi.h"
-#include "esp-idf/components/heap/include/esp_heap_caps.h"
+#include "components/heap/include/esp_heap_caps.h"
wifi_radio_obj_t common_hal_wifi_radio_obj;
-#include "esp_log.h"
+#include "components/log/include/esp_log.h"
+
static const char* TAG = "wifi";
static void event_handler(void* arg, esp_event_base_t event_base,
@@ -99,6 +100,13 @@ void common_hal_wifi_init(void) {
wifi_radio_obj_t* self = &common_hal_wifi_radio_obj;
self->netif = esp_netif_create_default_wifi_sta();
+ // Even though we just called esp_netif_create_default_wifi_sta,
+ // station mode isn't actually ready for use until esp_wifi_set_mode()
+ // is called and the configuration is loaded via esp_wifi_set_config().
+ // Set both convienence flags to false so it's not forgotten.
+ self->sta_mode = 0;
+ self->ap_mode = 0;
+
self->event_group_handle = xEventGroupCreateStatic(&self->event_group);
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,