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/alarm/__init__.c23
-rw-r--r--ports/esp32s2/common-hal/alarm/time/TimeAlarm.c (renamed from ports/esp32s2/common-hal/alarm/time/MonotonicTimeAlarm.c)6
-rw-r--r--ports/esp32s2/common-hal/alarm/time/TimeAlarm.h (renamed from ports/esp32s2/common-hal/alarm/time/MonotonicTimeAlarm.h)2
-rw-r--r--ports/esp32s2/common-hal/frequencyio/FrequencyIn.c193
-rw-r--r--ports/esp32s2/common-hal/frequencyio/FrequencyIn.h45
-rw-r--r--ports/esp32s2/common-hal/frequencyio/__init__.c1
-rw-r--r--ports/esp32s2/common-hal/microcontroller/__init__.c12
-rw-r--r--ports/esp32s2/common-hal/nvm/ByteArray.c97
-rw-r--r--ports/esp32s2/common-hal/nvm/ByteArray.h38
-rw-r--r--ports/esp32s2/common-hal/nvm/__init__.c1
-rw-r--r--ports/esp32s2/common-hal/ps2io/Ps2.c392
-rw-r--r--ports/esp32s2/common-hal/ps2io/Ps2.h60
-rw-r--r--ports/esp32s2/common-hal/ps2io/__init__.c1
-rw-r--r--ports/esp32s2/common-hal/time/__init__.c2
-rw-r--r--ports/esp32s2/common-hal/wifi/Radio.c7
-rw-r--r--ports/esp32s2/common-hal/wifi/__init__.c1
16 files changed, 865 insertions, 16 deletions
diff --git a/ports/esp32s2/common-hal/alarm/__init__.c b/ports/esp32s2/common-hal/alarm/__init__.c
index 37d74c0be..7cf74a0ef 100644
--- a/ports/esp32s2/common-hal/alarm/__init__.c
+++ b/ports/esp32s2/common-hal/alarm/__init__.c
@@ -31,10 +31,11 @@
#include "shared-bindings/alarm/__init__.h"
#include "shared-bindings/alarm/pin/PinAlarm.h"
-#include "shared-bindings/alarm/time/MonotonicTimeAlarm.h"
+#include "shared-bindings/alarm/time/TimeAlarm.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/time/__init__.h"
+#include "esp_log.h"
#include "esp_sleep.h"
STATIC mp_obj_tuple_t *_deep_sleep_alarms;
@@ -51,8 +52,8 @@ mp_obj_t common_hal_alarm_get_wake_alarm(void) {
switch (esp_sleep_get_wakeup_cause()) {
case ESP_SLEEP_WAKEUP_TIMER: {
// Wake up from timer.
- alarm_time_monotonic_time_alarm_obj_t *timer = m_new_obj(alarm_time_monotonic_time_alarm_obj_t);
- timer->base.type = &alarm_time_monotonic_time_alarm_type;
+ alarm_time_time_alarm_obj_t *timer = m_new_obj(alarm_time_time_alarm_obj_t);
+ timer->base.type = &alarm_time_time_alarm_type;
return timer;
}
@@ -86,7 +87,7 @@ void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *ala
if (MP_OBJ_IS_TYPE(alarms[i], &alarm_pin_pin_alarm_type)) {
mp_raise_NotImplementedError(translate("PinAlarm deep sleep not yet implemented"));
}
- else if (MP_OBJ_IS_TYPE(alarms[i], &alarm_time_monotonic_time_alarm_type)) {
+ else if (MP_OBJ_IS_TYPE(alarms[i], &alarm_time_time_alarm_type)) {
if (time_alarm_set) {
mp_raise_ValueError(translate("Only one alarm.time alarm can be set."));
}
@@ -106,15 +107,15 @@ bool common_hal_alarm_enable_deep_sleep_alarms(void) {
// TODO: handle pin alarms
mp_raise_NotImplementedError(translate("PinAlarm deep sleep not yet implemented"));
}
- else if (MP_OBJ_IS_TYPE(alarm, &alarm_time_monotonic_time_alarm_type)) {
- alarm_time_monotonic_time_alarm_obj_t *monotonic_time_alarm = MP_OBJ_TO_PTR(alarm);
- mp_float_t now = uint64_to_float(common_hal_time_monotonic());
- // Compute a relative time in the future from now.
- mp_float_t duration_secs = now - monotonic_time_alarm->monotonic_time;
- if (duration_secs <= 0.0f) {
+ else if (MP_OBJ_IS_TYPE(alarm, &alarm_time_time_alarm_type)) {
+ alarm_time_time_alarm_obj_t *time_alarm = MP_OBJ_TO_PTR(alarm);
+ mp_float_t now_secs = uint64_to_float(common_hal_time_monotonic_ms()) / 1000.0f;
+ // Compute how long to actually sleep, considering hte time now.
+ mp_float_t wakeup_in_secs = time_alarm->monotonic_time - now_secs;
+ if (wakeup_in_secs <= 0.0f) {
return false;
}
- esp_sleep_enable_timer_wakeup((uint64_t) (duration_secs * 1000000));
+ esp_sleep_enable_timer_wakeup((uint64_t) (wakeup_in_secs * 1000000));
}
}
return true;
diff --git a/ports/esp32s2/common-hal/alarm/time/MonotonicTimeAlarm.c b/ports/esp32s2/common-hal/alarm/time/TimeAlarm.c
index 81864d99e..7af369463 100644
--- a/ports/esp32s2/common-hal/alarm/time/MonotonicTimeAlarm.c
+++ b/ports/esp32s2/common-hal/alarm/time/TimeAlarm.c
@@ -28,12 +28,12 @@
#include "py/runtime.h"
-#include "shared-bindings/alarm/time/MonotonicTimeAlarm.h"
+#include "shared-bindings/alarm/time/TimeAlarm.h"
-void common_hal_alarm_time_monotonic_time_alarm_construct(alarm_time_monotonic_time_alarm_obj_t *self, mp_float_t monotonic_time) {
+void common_hal_alarm_time_time_alarm_construct(alarm_time_time_alarm_obj_t *self, mp_float_t monotonic_time) {
self->monotonic_time = monotonic_time;
}
-mp_float_t common_hal_alarm_time_monotonic_time_alarm_get_monotonic_time(alarm_time_monotonic_time_alarm_obj_t *self) {
+mp_float_t common_hal_alarm_time_time_alarm_get_monotonic_time(alarm_time_time_alarm_obj_t *self) {
return self->monotonic_time;
}
diff --git a/ports/esp32s2/common-hal/alarm/time/MonotonicTimeAlarm.h b/ports/esp32s2/common-hal/alarm/time/TimeAlarm.h
index 5ff829450..b540541f4 100644
--- a/ports/esp32s2/common-hal/alarm/time/MonotonicTimeAlarm.h
+++ b/ports/esp32s2/common-hal/alarm/time/TimeAlarm.h
@@ -30,4 +30,4 @@
typedef struct {
mp_obj_base_t base;
mp_float_t monotonic_time; // values compatible with time.monotonic_time()
-} alarm_time_monotonic_time_alarm_obj_t;
+} alarm_time_time_alarm_obj_t;
diff --git a/ports/esp32s2/common-hal/frequencyio/FrequencyIn.c b/ports/esp32s2/common-hal/frequencyio/FrequencyIn.c
new file mode 100644
index 000000000..12d612abb
--- /dev/null
+++ b/ports/esp32s2/common-hal/frequencyio/FrequencyIn.c
@@ -0,0 +1,193 @@
+/*
+ * 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 "shared-bindings/frequencyio/FrequencyIn.h"
+
+#include "py/runtime.h"
+
+static void IRAM_ATTR pcnt_overflow_handler(void *self_in) {
+ frequencyio_frequencyin_obj_t * self = self_in;
+ // reset counter
+ pcnt_counter_clear(self->unit);
+
+ // increase multiplier
+ self->multiplier++;
+
+ // reset interrupt
+ PCNT.int_clr.val = BIT(self->unit);
+}
+
+static void IRAM_ATTR timer_interrupt_handler(void *self_in) {
+ frequencyio_frequencyin_obj_t * self = self_in;
+ // get counter value
+ int16_t count;
+ pcnt_get_counter_value(self->unit, &count);
+ self->frequency = ((count / 2.0) + (self->multiplier * INT16_MAX / 4.0)) / (self->capture_period);
+
+ // reset multiplier
+ self->multiplier = 0;
+
+ // reset counter
+ pcnt_counter_clear(self->unit);
+
+ // reset interrupt
+ timg_dev_t *device = self->timer.group ? &(TIMERG1) : &(TIMERG0);
+ if (self->timer.idx) {
+ device->int_clr.t1 = 1;
+ } else {
+ device->int_clr.t0 = 1;
+ }
+ device->hw_timer[self->timer.idx].config.alarm_en = 1;
+}
+
+static void init_pcnt(frequencyio_frequencyin_obj_t* self) {
+ // Prepare configuration for the PCNT unit
+ const pcnt_config_t pcnt_config = {
+ // Set PCNT input signal and control GPIOs
+ .pulse_gpio_num = self->pin,
+ .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 both rising and falling edges
+ .neg_mode = PCNT_COUNT_INC,
+ // Set counter limit
+ .counter_h_lim = INT16_MAX,
+ .counter_l_lim = 0,
+ };
+
+ // initialize PCNT
+ const int8_t unit = peripherals_pcnt_init(pcnt_config);
+ if (unit == -1) {
+ mp_raise_RuntimeError(translate("All PCNT units in use"));
+ }
+
+ // set the GPIO back to high-impedance, as pcnt_unit_config sets it as pull-up
+ gpio_set_pull_mode(self->pin, GPIO_FLOATING);
+
+ self->unit = (pcnt_unit_t)unit;
+
+ // enable pcnt interrupt
+ pcnt_event_enable(self->unit, PCNT_EVT_H_LIM);
+ pcnt_isr_register(pcnt_overflow_handler, (void *)self, ESP_INTR_FLAG_IRAM, &self->handle);
+ pcnt_intr_enable(self->unit);
+}
+
+static void init_timer(frequencyio_frequencyin_obj_t* self) {
+ // Prepare configuration for the timer module
+ const timer_config_t config = {
+ .alarm_en = true,
+ .counter_en = false,
+ .intr_type = TIMER_INTR_LEVEL,
+ .counter_dir = TIMER_COUNT_UP,
+ .auto_reload = true,
+ .divider = 80 // 1 us per tick
+ };
+
+ // initialize Timer
+ peripherals_timer_init(&config, &self->timer);
+ if (self->timer.idx == TIMER_MAX || self->timer.group == TIMER_GROUP_MAX) {
+ mp_raise_RuntimeError(translate("All timers in use"));
+ }
+
+ timer_idx_t idx = self->timer.idx;
+ timer_group_t group = self->timer.group;
+
+ // enable timer interrupt
+ timer_set_alarm_value(group, idx, self->capture_period * 1000000);
+ timer_isr_register(group, idx, timer_interrupt_handler, (void *)self, ESP_INTR_FLAG_IRAM, &self->handle);
+ timer_enable_intr(group, idx);
+
+ // start timer
+ timer_start(self->timer.group, self->timer.idx);
+}
+
+void common_hal_frequencyio_frequencyin_construct(frequencyio_frequencyin_obj_t* self,
+ const mcu_pin_obj_t* pin, const uint16_t capture_period) {
+ if ((capture_period == 0) || (capture_period > 500)) {
+ mp_raise_ValueError(translate("Invalid capture period. Valid range: 1 - 500"));
+ }
+
+ self->pin = pin->number;
+ self->handle = NULL;
+ self->multiplier = 0;
+ self->capture_period = capture_period;
+
+ // initialize pcnt and timer
+ init_pcnt(self);
+ init_timer(self);
+
+ claim_pin(pin);
+}
+
+bool common_hal_frequencyio_frequencyin_deinited(frequencyio_frequencyin_obj_t* self) {
+ return self->unit == PCNT_UNIT_MAX;
+}
+
+void common_hal_frequencyio_frequencyin_deinit(frequencyio_frequencyin_obj_t* self) {
+ if (common_hal_frequencyio_frequencyin_deinited(self)) {
+ return;
+ }
+ reset_pin_number(self->pin);
+ peripherals_pcnt_deinit(&self->unit);
+ peripherals_timer_deinit(&self->timer);
+ if (self->handle) {
+ esp_intr_free(self->handle);
+ self->handle = NULL;
+ }
+}
+
+uint32_t common_hal_frequencyio_frequencyin_get_item(frequencyio_frequencyin_obj_t* self) {
+ return self->frequency;
+}
+
+void common_hal_frequencyio_frequencyin_pause(frequencyio_frequencyin_obj_t* self) {
+ pcnt_counter_pause(self->unit);
+ timer_pause(self->timer.group, self->timer.idx);
+}
+
+void common_hal_frequencyio_frequencyin_resume(frequencyio_frequencyin_obj_t* self) {
+ pcnt_counter_resume(self->unit);
+ timer_start(self->timer.group, self->timer.idx);
+}
+
+void common_hal_frequencyio_frequencyin_clear(frequencyio_frequencyin_obj_t* self) {
+ self->frequency = 0;
+ pcnt_counter_clear(self->unit);
+ timer_set_counter_value(self->timer.group, self->timer.idx, 0);
+}
+
+uint16_t common_hal_frequencyio_frequencyin_get_capture_period(frequencyio_frequencyin_obj_t *self) {
+ return self->capture_period;
+}
+
+void common_hal_frequencyio_frequencyin_set_capture_period(frequencyio_frequencyin_obj_t *self, uint16_t capture_period) {
+ if ((capture_period == 0) || (capture_period > 500)) {
+ mp_raise_ValueError(translate("Invalid capture period. Valid range: 1 - 500"));
+ }
+ self->capture_period = capture_period;
+ common_hal_frequencyio_frequencyin_clear(self);
+ timer_set_alarm_value(self->timer.group, self->timer.idx, capture_period * 1000000);
+}
diff --git a/ports/esp32s2/common-hal/frequencyio/FrequencyIn.h b/ports/esp32s2/common-hal/frequencyio/FrequencyIn.h
new file mode 100644
index 000000000..cf9d2ae53
--- /dev/null
+++ b/ports/esp32s2/common-hal/frequencyio/FrequencyIn.h
@@ -0,0 +1,45 @@
+/*
+ * 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_FREQUENCYIO_FREQUENCYIN_H
+#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_FREQUENCYIO_FREQUENCYIN_H
+
+#include "py/obj.h"
+#include "peripherals/pcnt.h"
+#include "peripherals/timer.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ pcnt_unit_t unit;
+ timer_index_t timer;
+ intr_handle_t handle;
+ uint8_t pin;
+ uint8_t multiplier;
+ uint32_t frequency;
+ uint16_t capture_period;
+} frequencyio_frequencyin_obj_t;
+
+#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_FREQUENCYIO_FREQUENCYIN_H
diff --git a/ports/esp32s2/common-hal/frequencyio/__init__.c b/ports/esp32s2/common-hal/frequencyio/__init__.c
new file mode 100644
index 000000000..487814bd0
--- /dev/null
+++ b/ports/esp32s2/common-hal/frequencyio/__init__.c
@@ -0,0 +1 @@
+// No ferquencyio module functions.
diff --git a/ports/esp32s2/common-hal/microcontroller/__init__.c b/ports/esp32s2/common-hal/microcontroller/__init__.c
index 5aa0ff8ed..3578b86d0 100644
--- a/ports/esp32s2/common-hal/microcontroller/__init__.c
+++ b/ports/esp32s2/common-hal/microcontroller/__init__.c
@@ -35,6 +35,7 @@
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/microcontroller/Processor.h"
+#include "shared-bindings/nvm/ByteArray.h"
#include "supervisor/filesystem.h"
#include "supervisor/shared/safe_mode.h"
@@ -94,6 +95,17 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = {
},
};
+#if CIRCUITPY_INTERNAL_NVM_SIZE > 0
+// The singleton nvm.ByteArray object.
+const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
+ .base = {
+ .type = &nvm_bytearray_type,
+ },
+ .start_address = (uint8_t*) CIRCUITPY_INTERNAL_NVM_START_ADDR,
+ .len = CIRCUITPY_INTERNAL_NVM_SIZE,
+};
+#endif
+
#if CIRCUITPY_WATCHDOG
// The singleton watchdog.WatchDogTimer object.
watchdog_watchdogtimer_obj_t common_hal_mcu_watchdogtimer_obj = {
diff --git a/ports/esp32s2/common-hal/nvm/ByteArray.c b/ports/esp32s2/common-hal/nvm/ByteArray.c
new file mode 100644
index 000000000..71321e7e6
--- /dev/null
+++ b/ports/esp32s2/common-hal/nvm/ByteArray.c
@@ -0,0 +1,97 @@
+/*
+ * 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/nvm/ByteArray.h"
+
+#include "py/runtime.h"
+#include "nvs_flash.h"
+
+uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) {
+ return self->len;
+}
+
+static void get_nvs_handle(nvs_handle_t * nvs_handle) {
+ // Initialize NVS
+ esp_err_t err = nvs_flash_init();
+ if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
+ // NVS partition was truncated and needs to be erased
+ // Retry nvs_flash_init
+ ESP_ERROR_CHECK(nvs_flash_erase());
+ err = nvs_flash_init();
+ }
+ ESP_ERROR_CHECK(err);
+
+ // Open NVS handle
+ if (nvs_open("CPY", NVS_READWRITE, nvs_handle) != ESP_OK) {
+ mp_raise_RuntimeError(translate("NVS Error"));
+ }
+}
+
+bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
+ uint32_t start_index, uint8_t* values, uint32_t len) {
+ char index[9];
+
+ // start nvs
+ nvs_handle_t handle;
+ get_nvs_handle(&handle);
+
+ // stage flash changes
+ for (uint32_t i = 0; i < len; i++) {
+ sprintf(index, "%i", start_index + i);
+ if (nvs_set_u8(handle, (const char *)index, values[i]) != ESP_OK) {
+ return false;
+ }
+ }
+
+ // commit flash changes
+ if (nvs_commit(handle) != ESP_OK) {
+ return false;
+ }
+
+ // close nvs
+ nvs_close(handle);
+ return true;
+}
+
+void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self,
+ uint32_t start_index, uint32_t len, uint8_t* values) {
+ char index[9];
+
+ // start nvs
+ nvs_handle_t handle;
+ get_nvs_handle(&handle);
+
+ // get from flash
+ for (uint32_t i = 0; i < len; i++) {
+ sprintf(index, "%i", start_index + i);
+ if (nvs_get_u8(handle, (const char *)index, &values[i]) != ESP_OK) {
+ mp_raise_RuntimeError(translate("NVS Error"));
+ }
+ }
+
+ // close nvs
+ nvs_close(handle);
+}
diff --git a/ports/esp32s2/common-hal/nvm/ByteArray.h b/ports/esp32s2/common-hal/nvm/ByteArray.h
new file mode 100644
index 000000000..44b63d724
--- /dev/null
+++ b/ports/esp32s2/common-hal/nvm/ByteArray.h
@@ -0,0 +1,38 @@
+/*
+ * 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_NVM_BYTEARRAY_H
+#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_NVM_BYTEARRAY_H
+
+#include "py/obj.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ uint8_t* start_address;
+ uint32_t len;
+} nvm_bytearray_obj_t;
+
+#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_NVM_BYTEARRAY_H
diff --git a/ports/esp32s2/common-hal/nvm/__init__.c b/ports/esp32s2/common-hal/nvm/__init__.c
new file mode 100644
index 000000000..1b702a158
--- /dev/null
+++ b/ports/esp32s2/common-hal/nvm/__init__.c
@@ -0,0 +1 @@
+// No nvm module functions.
diff --git a/ports/esp32s2/common-hal/ps2io/Ps2.c b/ports/esp32s2/common-hal/ps2io/Ps2.c
new file mode 100644
index 000000000..003f0d827
--- /dev/null
+++ b/ports/esp32s2/common-hal/ps2io/Ps2.c
@@ -0,0 +1,392 @@
+/*
+ * 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/ps2io/Ps2.h"
+
+#include "supervisor/port.h"
+#include "shared-bindings/microcontroller/__init__.h"
+
+#define STATE_IDLE 0
+#define STATE_RECV 1
+#define STATE_RECV_PARITY 2
+#define STATE_RECV_STOP 3
+#define STATE_RECV_ERR 10
+
+#define ERROR_STARTBIT 0x01
+#define ERROR_TIMEOUT 0x02
+#define ERROR_PARITY 0x04
+#define ERROR_STOPBIT 0x08
+#define ERROR_BUFFER 0x10
+
+#define ERROR_TX_CLKLO 0x100
+#define ERROR_TX_CLKHI 0x200
+#define ERROR_TX_ACKDATA 0x400
+#define ERROR_TX_ACKCLK 0x800
+#define ERROR_TX_RTS 0x1000
+#define ERROR_TX_NORESP 0x2000
+
+void ps2_reset(void) {
+ gpio_uninstall_isr_service();
+}
+
+static void delay_us(uint32_t t) {
+ common_hal_mcu_delay_us(t);
+}
+
+/* interrupt handling */
+
+static void IRAM_ATTR ps2_interrupt_handler(void *self_in) {
+ // Grab the current time first.
+ uint64_t current_tick = port_get_raw_ticks(NULL);
+
+ ps2io_ps2_obj_t * self = self_in;
+ int data_bit = gpio_get_level(self->data_pin) ? 1 : 0;
+
+ // test for timeout
+ if (self->state != STATE_IDLE) {
+ int64_t diff_ms = current_tick - self->last_raw_ticks;
+ if (diff_ms > 1) { // a.k.a. > 1.001ms
+ self->last_errors |= ERROR_TIMEOUT;
+ self->state = STATE_IDLE;
+ }
+ }
+
+ self->last_raw_ticks = current_tick;
+
+ if (self->state == STATE_IDLE) {
+ self->bits = 0;
+ self->parity = false;
+ self->bitcount = 0;
+ self->state = STATE_RECV;
+ if (data_bit) {
+ // start bit should be 0
+ self->last_errors |= ERROR_STARTBIT;
+ self->state = STATE_RECV_ERR;
+ } else {
+ self->state = STATE_RECV;
+ }
+
+ } else if (self->state == STATE_RECV) {
+ if (data_bit) {
+ self->bits |= data_bit << self->bitcount;
+ self->parity = !self->parity;
+ }
+ ++self->bitcount;
+ if (self->bitcount >= 8) {
+ self->state = STATE_RECV_PARITY;
+ }
+
+ } else if (self->state == STATE_RECV_PARITY) {
+ ++self->bitcount;
+ if (data_bit) {
+ self->parity = !self->parity;
+ }
+ if (!self->parity) {
+ self->last_errors |= ERROR_PARITY;
+ self->state = STATE_RECV_ERR;
+ } else {
+ self->state = STATE_RECV_STOP;
+ }
+
+ } else if (self->state == STATE_RECV_STOP) {
+ ++self->bitcount;
+ if (! data_bit) {
+ self->last_errors |= ERROR_STOPBIT;
+ } else if (self->waiting_cmd_response) {
+ self->cmd_response = self->bits;
+ self->waiting_cmd_response = false;
+ } else if (self->bufcount >= sizeof(self->buffer)) {
+ self->last_errors |= ERROR_BUFFER;
+ } else {
+ self->buffer[self->bufposw] = self->bits;
+ self->bufposw = (self->bufposw + 1) % sizeof(self->buffer);
+ self->bufcount++;
+ }
+ self->state = STATE_IDLE;
+
+ } else if (self->state == STATE_RECV_ERR) {
+ // just count the bits until idle
+ if (++self->bitcount >= 10) {
+ self->state = STATE_IDLE;
+ }
+ }
+}
+
+static void enable_interrupt(ps2io_ps2_obj_t* self) {
+ // turn on falling edge interrupt
+ gpio_install_isr_service(ESP_INTR_FLAG_IRAM);
+ gpio_set_intr_type(self->clk_pin, GPIO_INTR_NEGEDGE);
+ gpio_isr_handler_add(self->clk_pin, ps2_interrupt_handler, (void*)self);
+}
+
+static void disable_interrupt(ps2io_ps2_obj_t* self) {
+ // turn off fallling edge interrupt
+ gpio_isr_handler_remove(self->clk_pin);
+}
+
+static void resume_interrupt(ps2io_ps2_obj_t* self) {
+ self->state = STATE_IDLE;
+ gpio_isr_handler_add(self->clk_pin, ps2_interrupt_handler, (void*)self);
+}
+
+/* gpio handling */
+
+static void clk_hi(ps2io_ps2_obj_t* self) {
+ // external pull-up
+ gpio_set_direction(self->clk_pin, GPIO_MODE_INPUT);
+}
+
+static bool wait_clk_lo(ps2io_ps2_obj_t* self, uint32_t us) {
+ clk_hi(self);
+ delay_us(1);
+ while (gpio_get_level(self->clk_pin) && us) {
+ --us;
+ delay_us(1);
+ }
+ return us;
+}
+
+static bool wait_clk_hi(ps2io_ps2_obj_t* self, uint32_t us) {
+ clk_hi(self);
+ delay_us(1);
+ while (!gpio_get_level(self->clk_pin) && us) {
+ --us;
+ delay_us(1);
+ }
+ return us;
+}
+
+static void clk_lo(ps2io_ps2_obj_t* self) {
+ gpio_set_direction(self->clk_pin, GPIO_MODE_OUTPUT);
+ gpio_set_level(self->clk_pin, 0);
+}
+
+static void data_hi(ps2io_ps2_obj_t* self) {
+ // external pull-up
+ gpio_set_direction(self->data_pin, GPIO_MODE_INPUT);
+}
+
+static bool wait_data_lo(ps2io_ps2_obj_t* self, uint32_t us) {
+ data_hi(self);
+ delay_us(1);
+ while (gpio_get_level(self->data_pin) && us) {
+ --us;
+ delay_us(1);
+ }
+ return us;
+}
+
+static bool wait_data_hi(ps2io_ps2_obj_t* self, uint32_t us) {
+ data_hi(self);
+ delay_us(1);
+ while (!gpio_get_level(self->data_pin) && us) {
+ --us;
+ delay_us(1);
+ }
+ return us;
+}
+
+static void data_lo(ps2io_ps2_obj_t* self) {
+ gpio_set_direction(self->data_pin, GPIO_MODE_OUTPUT);
+ gpio_set_level(self->data_pin, 0);
+}
+
+static void idle(ps2io_ps2_obj_t* self) {
+ clk_hi(self);
+ data_hi(self);
+}
+
+static void inhibit(ps2io_ps2_obj_t* self) {
+ clk_lo(self);
+ data_hi(self);
+}
+
+/* ps2io module functions */
+
+void common_hal_ps2io_ps2_construct(ps2io_ps2_obj_t* self,
+ const mcu_pin_obj_t* data_pin, const mcu_pin_obj_t* clk_pin) {
+ self->clk_pin = (gpio_num_t)clk_pin->number;
+ self->data_pin = (gpio_num_t)data_pin->number;
+ self->state = STATE_IDLE;
+ self->bufcount = 0;
+ self->bufposr = 0;
+ self->bufposw = 0;
+ self->waiting_cmd_response = false;
+
+ idle(self);
+ enable_interrupt(self);
+
+ claim_pin(clk_pin);
+ claim_pin(data_pin);
+}
+
+bool common_hal_ps2io_ps2_deinited(ps2io_ps2_obj_t* self) {
+ return self->clk_pin == GPIO_NUM_MAX;
+}
+
+void common_hal_ps2io_ps2_deinit(ps2io_ps2_obj_t* self) {
+ if (common_hal_ps2io_ps2_deinited(self)) {
+ return;
+ }
+ gpio_uninstall_isr_service();
+ reset_pin_number(self->clk_pin);
+ reset_pin_number(self->data_pin);
+ self->clk_pin = GPIO_NUM_MAX;
+ self->data_pin = GPIO_NUM_MAX;
+}
+
+uint16_t common_hal_ps2io_ps2_get_len(ps2io_ps2_obj_t* self) {
+ return self->bufcount;
+}
+
+int16_t common_hal_ps2io_ps2_popleft(ps2io_ps2_obj_t* self) {
+ common_hal_mcu_disable_interrupts();
+ if (self->bufcount <= 0) {
+ common_hal_mcu_enable_interrupts();
+ return -1;
+ }
+ uint8_t b = self->buffer[self->bufposr];
+ self->bufposr = (self->bufposr + 1) % sizeof(self->buffer);
+ self->bufcount -= 1;
+ common_hal_mcu_enable_interrupts();
+ return b;
+}
+
+uint16_t common_hal_ps2io_ps2_clear_errors(ps2io_ps2_obj_t* self) {
+ common_hal_mcu_disable_interrupts();
+ uint16_t errors = self->last_errors;
+ self->last_errors = 0;
+ common_hal_mcu_enable_interrupts();
+ return errors;
+}
+
+// Based upon TMK implementation of PS/2 protocol
+// https://github.com/tmk/tmk_keyboard/blob/master/tmk_core/protocol/ps2_interrupt.c
+
+int16_t common_hal_ps2io_ps2_sendcmd(ps2io_ps2_obj_t* self, uint8_t b) {
+ disable_interrupt(self);
+
+ /* terminate a transmission if we have */
+ inhibit(self);
+ delay_us(100);
+
+ /* RTS and start bit */
+ data_lo(self);
+ clk_hi(self);
+ if (!wait_clk_lo(self, 10000)) {
+ self->last_errors |= ERROR_TX_RTS;
+ goto ERROR;
+ }
+
+ bool parity = true;
+ for (uint8_t i = 0; i < 8; i++) {
+ delay_us(15);
+ if (b & (1 << i)) {
+ parity = !parity;
+ data_hi(self);
+ } else {
+ data_lo(self);
+ }
+ if (!wait_clk_hi(self, 50)) {
+ self->last_errors |= ERROR_TX_CLKHI;
+ goto ERROR;
+ }
+ if (!wait_clk_lo(self, 50)) {
+ self->last_errors |= ERROR_TX_CLKLO;
+ goto ERROR;
+ }
+ }
+
+ /* Parity bit */
+ delay_us(15);
+ if (parity) {
+ data_hi(self);
+ } else {
+ data_lo(self);
+ }
+ if (!wait_clk_hi(self, 50)) {
+ self->last_errors |= ERROR_TX_CLKHI;
+ goto ERROR;
+ }
+ if (!wait_clk_lo(self, 50)) {
+ self->last_errors |= ERROR_TX_CLKLO;
+ goto ERROR;
+ }
+
+ /* Stop bit */
+ delay_us(15);
+ data_hi(self);
+
+ /* Ack */
+ if (!wait_data_lo(self, 50)) {
+ self->last_errors |= ERROR_TX_ACKDATA;
+ goto ERROR;
+ }
+ if (!wait_clk_lo(self, 50)) {
+ self->last_errors |= ERROR_TX_ACKCLK;
+ goto ERROR;
+ }
+
+ /* wait for idle state */
+ if (!wait_clk_hi(self, 50)) {
+ self->last_errors |= ERROR_TX_ACKCLK;
+ goto ERROR;
+ }
+ if (!wait_data_hi(self, 50)) {
+ self->last_errors |= ERROR_TX_ACKDATA;
+ goto ERROR;
+ }
+
+ /* Wait for response byte */
+ self->waiting_cmd_response = true;
+ idle(self);
+ resume_interrupt(self);
+
+ for (int i = 0; i < 25; ++i) {
+ delay_us(1000);
+ common_hal_mcu_disable_interrupts();
+ bool has_response = !self->waiting_cmd_response;
+ uint8_t response = self->cmd_response;
+ common_hal_mcu_enable_interrupts();
+
+ if (has_response) {
+ return response;
+ }
+ }
+
+ /* No response */
+ common_hal_mcu_disable_interrupts();
+ self->waiting_cmd_response = false;
+ self->last_errors |= ERROR_TX_NORESP;
+ common_hal_mcu_enable_interrupts();
+ return -1;
+
+ /* Other errors */
+ERROR:
+ idle(self);
+ resume_interrupt(self);
+ return -1;
+}
diff --git a/ports/esp32s2/common-hal/ps2io/Ps2.h b/ports/esp32s2/common-hal/ps2io/Ps2.h
new file mode 100644
index 000000000..497952b25
--- /dev/null
+++ b/ports/esp32s2/common-hal/ps2io/Ps2.h
@@ -0,0 +1,60 @@
+/*
+ * 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_PS2IO_PS2_H
+#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_PS2IO_PS2_H
+
+#include "common-hal/microcontroller/Pin.h"
+
+#include "py/obj.h"
+#include "driver/gpio.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ gpio_num_t clk_pin;
+ gpio_num_t data_pin;
+
+ uint8_t state;
+ uint64_t last_raw_ticks;
+
+ uint16_t bits;
+ bool parity;
+ uint8_t bitcount;
+
+ uint8_t buffer[16];
+ uint8_t bufcount;
+ uint8_t bufposr;
+ uint8_t bufposw;
+
+ uint16_t last_errors;
+
+ bool waiting_cmd_response;
+ uint8_t cmd_response;
+} ps2io_ps2_obj_t;
+
+void ps2_reset(void);
+
+#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_PS2IO_PS2_H
diff --git a/ports/esp32s2/common-hal/ps2io/__init__.c b/ports/esp32s2/common-hal/ps2io/__init__.c
new file mode 100644
index 000000000..ba4b4249f
--- /dev/null
+++ b/ports/esp32s2/common-hal/ps2io/__init__.c
@@ -0,0 +1 @@
+// No ps2io module functions.
diff --git a/ports/esp32s2/common-hal/time/__init__.c b/ports/esp32s2/common-hal/time/__init__.c
index c85077868..398df0c1b 100644
--- a/ports/esp32s2/common-hal/time/__init__.c
+++ b/ports/esp32s2/common-hal/time/__init__.c
@@ -28,7 +28,7 @@
#include "tick.h"
-uint64_t common_hal_time_monotonic(void) {
+uint64_t common_hal_time_monotonic_ms(void) {
return supervisor_ticks_ms64();
}
diff --git a/ports/esp32s2/common-hal/wifi/Radio.c b/ports/esp32s2/common-hal/wifi/Radio.c
index 1945da207..f7c431a56 100644
--- a/ports/esp32s2/common-hal/wifi/Radio.c
+++ b/ports/esp32s2/common-hal/wifi/Radio.c
@@ -142,6 +142,13 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
} else {
config->sta.bssid_set = 0;
}
+ // If channel is 0 (default/unset) and BSSID is not given, do a full scan instead of fast scan
+ // This will ensure that the best AP in range is chosen automatically
+ if ((config->sta.bssid_set == 0) && (config->sta.channel == 0)) {
+ config->sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
+ } else {
+ config->sta.scan_method = WIFI_FAST_SCAN;
+ }
esp_wifi_set_config(ESP_IF_WIFI_STA, config);
self->starting_retries = 5;
self->retries_left = 5;
diff --git a/ports/esp32s2/common-hal/wifi/__init__.c b/ports/esp32s2/common-hal/wifi/__init__.c
index f53e68fc2..e06038c22 100644
--- a/ports/esp32s2/common-hal/wifi/__init__.c
+++ b/ports/esp32s2/common-hal/wifi/__init__.c
@@ -59,6 +59,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
ESP_EARLY_LOGW(TAG, "reason %d 0x%02x", reason, reason);
if (radio->retries_left > 0 &&
(reason == WIFI_REASON_AUTH_EXPIRE ||
+ reason == WIFI_REASON_NOT_AUTHED ||
reason == WIFI_REASON_ASSOC_EXPIRE ||
reason == WIFI_REASON_CONNECTION_FAIL ||
reason == WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT)) {