summaryrefslogtreecommitdiff
path: root/shared-bindings
diff options
context:
space:
mode:
authorDan Halbert <halbert@adafruit.com>2020-12-23 22:05:02 -0500
committerGitHub <noreply@github.com>2020-12-23 22:05:02 -0500
commit8061a2574d27db6c0e1c35890879204381b20afe (patch)
tree6b682b7df38785cbb25d5725c855151fd6af9e89 /shared-bindings
parentc371119da4e6f56c15e46c53e914344bb89d026c (diff)
parenta1e3ab1293d0a7654427a49c7f5e2306e07cff4c (diff)
Merge branch 'main' into pin_alarm
Diffstat (limited to 'shared-bindings')
-rw-r--r--shared-bindings/_bleio/Adapter.c13
-rw-r--r--shared-bindings/_bleio/Characteristic.c4
-rw-r--r--shared-bindings/_bleio/Descriptor.c4
-rw-r--r--shared-bindings/adafruit_bus_device/I2CDevice.c8
-rw-r--r--shared-bindings/adafruit_bus_device/I2CDevice.h2
-rw-r--r--shared-bindings/camera/ImageFormat.c1
-rw-r--r--shared-bindings/camera/ImageFormat.h2
-rw-r--r--shared-bindings/dualbank/__init__.c115
-rw-r--r--shared-bindings/dualbank/__init__.h35
9 files changed, 170 insertions, 14 deletions
diff --git a/shared-bindings/_bleio/Adapter.c b/shared-bindings/_bleio/Adapter.c
index 682177093..7d7076aab 100644
--- a/shared-bindings/_bleio/Adapter.c
+++ b/shared-bindings/_bleio/Adapter.c
@@ -33,8 +33,8 @@
#include "shared-bindings/_bleio/Address.h"
#include "shared-bindings/_bleio/Adapter.h"
-#define ADV_INTERVAL_MIN (0.0020f)
-#define ADV_INTERVAL_MIN_STRING "0.0020"
+#define ADV_INTERVAL_MIN (0.02001f)
+#define ADV_INTERVAL_MIN_STRING "0.02001"
#define ADV_INTERVAL_MAX (10.24f)
#define ADV_INTERVAL_MAX_STRING "10.24"
// 20ms is recommended by Apple
@@ -307,7 +307,7 @@ STATIC mp_obj_t bleio_adapter_start_scan(size_t n_args, const mp_obj_t *pos_args
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
- mp_float_t timeout = 0;
+ mp_float_t timeout = 0.0f;
if (args[ARG_timeout].u_obj != mp_const_none) {
timeout = mp_obj_get_float(args[ARG_timeout].u_obj);
}
@@ -325,6 +325,13 @@ STATIC mp_obj_t bleio_adapter_start_scan(size_t n_args, const mp_obj_t *pos_args
mp_raise_ValueError_varg(translate("interval must be in range %s-%s"), INTERVAL_MIN_STRING, INTERVAL_MAX_STRING);
}
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wfloat-equal"
+ if (timeout != 0.0f && timeout < interval) {
+ mp_raise_ValueError(translate("non-zero timeout must be >= interval"));
+ }
+#pragma GCC diagnostic pop
+
const mp_float_t window = mp_obj_float_get(args[ARG_window].u_obj);
if (window > interval) {
mp_raise_ValueError(translate("window must be <= interval"));
diff --git a/shared-bindings/_bleio/Characteristic.c b/shared-bindings/_bleio/Characteristic.c
index 5e384a44c..a0751b7e3 100644
--- a/shared-bindings/_bleio/Characteristic.c
+++ b/shared-bindings/_bleio/Characteristic.c
@@ -110,8 +110,8 @@ STATIC mp_obj_t bleio_characteristic_add_to_service(size_t n_args, const mp_obj_
common_hal_bleio_attribute_security_mode_check_valid(write_perm);
const mp_int_t max_length_int = args[ARG_max_length].u_int;
- if (max_length_int <= 0) {
- mp_raise_ValueError(translate("max_length must be > 0"));
+ if (max_length_int < 0) {
+ mp_raise_ValueError(translate("max_length must be >= 0"));
}
const size_t max_length = (size_t) max_length_int;
const bool fixed_length = args[ARG_fixed_length].u_bool;
diff --git a/shared-bindings/_bleio/Descriptor.c b/shared-bindings/_bleio/Descriptor.c
index c313007c6..60f0acf44 100644
--- a/shared-bindings/_bleio/Descriptor.c
+++ b/shared-bindings/_bleio/Descriptor.c
@@ -101,8 +101,8 @@ STATIC mp_obj_t bleio_descriptor_add_to_characteristic(size_t n_args, const mp_o
common_hal_bleio_attribute_security_mode_check_valid(write_perm);
const mp_int_t max_length_int = args[ARG_max_length].u_int;
- if (max_length_int <= 0) {
- mp_raise_ValueError(translate("max_length must be > 0"));
+ if (max_length_int < 0) {
+ mp_raise_ValueError(translate("max_length must be >= 0"));
}
const size_t max_length = (size_t) max_length_int;
const bool fixed_length = args[ARG_fixed_length].u_bool;
diff --git a/shared-bindings/adafruit_bus_device/I2CDevice.c b/shared-bindings/adafruit_bus_device/I2CDevice.c
index 4c9086162..a4c04e198 100644
--- a/shared-bindings/adafruit_bus_device/I2CDevice.c
+++ b/shared-bindings/adafruit_bus_device/I2CDevice.c
@@ -163,7 +163,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(adafruit_bus_device_i2cdevice_readinto_obj, 2,
//| """
//| ...
//|
-STATIC void write(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, int32_t start, mp_int_t end) {
+STATIC void write(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, int32_t start, mp_int_t end, bool transmit_stop_bit) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_READ);
@@ -173,7 +173,7 @@ STATIC void write(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, in
mp_raise_ValueError(translate("Buffer must be at least length 1"));
}
- uint8_t status = common_hal_adafruit_bus_device_i2cdevice_write(MP_OBJ_TO_PTR(self), ((uint8_t*)bufinfo.buf) + start, length);
+ uint8_t status = common_hal_adafruit_bus_device_i2cdevice_write(MP_OBJ_TO_PTR(self), ((uint8_t*)bufinfo.buf) + start, length, transmit_stop_bit);
if (status != 0) {
mp_raise_OSError(status);
}
@@ -191,7 +191,7 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_write(size_t n_args, const mp_obj_
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
- write(self, args[ARG_buffer].u_obj, args[ARG_start].u_int, args[ARG_end].u_int);
+ write(self, args[ARG_buffer].u_obj, args[ARG_start].u_int, args[ARG_end].u_int, true);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(adafruit_bus_device_i2cdevice_write_obj, 2, adafruit_bus_device_i2cdevice_write);
@@ -233,7 +233,7 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_write_then_readinto(size_t n_args,
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
- write(self, args[ARG_out_buffer].u_obj, args[ARG_out_start].u_int, args[ARG_out_end].u_int);
+ write(self, args[ARG_out_buffer].u_obj, args[ARG_out_start].u_int, args[ARG_out_end].u_int, false);
readinto(self, args[ARG_in_buffer].u_obj, args[ARG_in_start].u_int, args[ARG_in_end].u_int);
diff --git a/shared-bindings/adafruit_bus_device/I2CDevice.h b/shared-bindings/adafruit_bus_device/I2CDevice.h
index 7b2182ff0..cf7b1321a 100644
--- a/shared-bindings/adafruit_bus_device/I2CDevice.h
+++ b/shared-bindings/adafruit_bus_device/I2CDevice.h
@@ -45,7 +45,7 @@ extern const mp_obj_type_t adafruit_bus_device_i2cdevice_type;
// Initializes the hardware peripheral.
extern void common_hal_adafruit_bus_device_i2cdevice_construct(adafruit_bus_device_i2cdevice_obj_t *self, busio_i2c_obj_t *i2c, uint8_t device_address);
extern uint8_t common_hal_adafruit_bus_device_i2cdevice_readinto(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length);
-extern uint8_t common_hal_adafruit_bus_device_i2cdevice_write(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length);
+extern uint8_t common_hal_adafruit_bus_device_i2cdevice_write(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length, bool transmit_stop_bit);
extern void common_hal_adafruit_bus_device_i2cdevice_lock(adafruit_bus_device_i2cdevice_obj_t *self);
extern void common_hal_adafruit_bus_device_i2cdevice_unlock(adafruit_bus_device_i2cdevice_obj_t *self);
extern void common_hal_adafruit_bus_device_i2cdevice_probe_for_device(adafruit_bus_device_i2cdevice_obj_t *self);
diff --git a/shared-bindings/camera/ImageFormat.c b/shared-bindings/camera/ImageFormat.c
index d4bdddc56..9f2f9617f 100644
--- a/shared-bindings/camera/ImageFormat.c
+++ b/shared-bindings/camera/ImageFormat.c
@@ -38,7 +38,6 @@
//| RGB565: ImageFormat
//| """RGB565 format."""
//|
-const mp_obj_type_t camera_imageformat_type;
const camera_imageformat_obj_t camera_imageformat_jpg_obj = {
{ &camera_imageformat_type },
diff --git a/shared-bindings/camera/ImageFormat.h b/shared-bindings/camera/ImageFormat.h
index 8abc88438..32a36354f 100644
--- a/shared-bindings/camera/ImageFormat.h
+++ b/shared-bindings/camera/ImageFormat.h
@@ -35,7 +35,7 @@ typedef enum {
IMAGEFORMAT_RGB565,
} camera_imageformat_t;
-const mp_obj_type_t camera_imageformat_type;
+extern const mp_obj_type_t camera_imageformat_type;
camera_imageformat_t camera_imageformat_obj_to_type(mp_obj_t obj);
mp_obj_t camera_imageformat_type_to_obj(camera_imageformat_t mode);
diff --git a/shared-bindings/dualbank/__init__.c b/shared-bindings/dualbank/__init__.c
new file mode 100644
index 000000000..8021ab18b
--- /dev/null
+++ b/shared-bindings/dualbank/__init__.c
@@ -0,0 +1,115 @@
+/*
+ * This file is part of the Micro Python 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/dualbank/__init__.h"
+
+//| """DUALBANK Module
+//|
+//| The `dualbank` module adds ability to update and switch
+//| between the two app partitions.
+//|
+//| There are two identical partitions, these contain different
+//| firmware versions.
+//| Having two partitions enables rollback functionality.
+//|
+//| The two partitions are defined as boot partition and
+//| next-update partition. Calling `dualbank.flash()` writes
+//| the next-update partition.
+//|
+//| After the next-update partition is written a validation
+//| check is performed and on a successful validation this
+//| partition is set as the boot partition. On next reset,
+//| firmware will be loaded from this partition.
+//|
+//| Here is the sequence of commands to follow:
+//|
+//| .. code-block:: python
+//|
+//| import dualbank
+//|
+//| dualbank.flash(buffer, offset)
+//| dualbank.switch()
+//| """
+//| ...
+//|
+
+//| def flash(*buffer: ReadableBuffer, offset: int=0) -> None:
+//| """Writes one of two app partitions at the given offset.
+//|
+//| This can be called multiple times when flashing the firmware
+//| in small chunks.
+//| """
+//| ...
+//|
+STATIC mp_obj_t dualbank_flash(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ enum { ARG_buffer, ARG_offset };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED },
+ { MP_QSTR_offset, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
+ };
+
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+ mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+ if (args[ARG_offset].u_int < 0) {
+ mp_raise_ValueError(translate("offset must be >= 0"));
+ }
+
+ mp_buffer_info_t bufinfo;
+ mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
+
+ common_hal_dualbank_flash(bufinfo.buf, bufinfo.len, args[ARG_offset].u_int);
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_KW(dualbank_flash_obj, 0, dualbank_flash);
+
+//| def switch() -> None:
+//| """Switches the boot partition.
+//|
+//| On next reset, firmware will be loaded from the partition
+//| just switched over to.
+//| """
+//| ...
+//|
+STATIC mp_obj_t dualbank_switch(void) {
+ common_hal_dualbank_switch();
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_0(dualbank_switch_obj, dualbank_switch);
+
+STATIC const mp_rom_map_elem_t dualbank_module_globals_table[] = {
+ // module name
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_dualbank) },
+ // module functions
+ { MP_ROM_QSTR(MP_QSTR_flash), MP_ROM_PTR(&dualbank_flash_obj) },
+ { MP_ROM_QSTR(MP_QSTR_switch), MP_ROM_PTR(&dualbank_switch_obj) },
+};
+STATIC MP_DEFINE_CONST_DICT(dualbank_module_globals, dualbank_module_globals_table);
+
+const mp_obj_module_t dualbank_module = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t*)&dualbank_module_globals,
+};
diff --git a/shared-bindings/dualbank/__init__.h b/shared-bindings/dualbank/__init__.h
new file mode 100644
index 000000000..7edbc6d68
--- /dev/null
+++ b/shared-bindings/dualbank/__init__.h
@@ -0,0 +1,35 @@
+/*
+ * 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_SHARED_BINDINGS_DUALBANK___INIT___H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_DUALBANK___INIT___H
+
+#include "py/runtime.h"
+
+extern void common_hal_dualbank_switch(void);
+extern void common_hal_dualbank_flash(const void *buf, const size_t len, const size_t offset);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DUALBANK___INIT___H