summaryrefslogtreecommitdiff
path: root/shared-bindings
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2018-01-29 22:52:23 -0500
committerDan Halbert <halbert@halwitz.org>2018-01-30 12:08:41 -0500
commite550b024c5d3d6d5a7e739ae9dd2175756591f6e (patch)
tree01fb3799bbd801ad26a3c7213ea62bac59e1c9db /shared-bindings
parenta1e279a9d8bf537439ce643179b596e1da13daad (diff)
atmel-samd: Correct computation of SPI baud rate.
all: Add .frequency read-only property for busio.SPI to return actual frequency. Fix esp8266/posix_helpers.c, which was not up to date for the new long-lived/short-lived heap allocation scheme.
Diffstat (limited to 'shared-bindings')
-rw-r--r--shared-bindings/busio/SPI.c26
-rw-r--r--shared-bindings/busio/SPI.h3
2 files changed, 27 insertions, 2 deletions
diff --git a/shared-bindings/busio/SPI.c b/shared-bindings/busio/SPI.c
index c4c139105..d68b61d9b 100644
--- a/shared-bindings/busio/SPI.c
+++ b/shared-bindings/busio/SPI.c
@@ -36,7 +36,7 @@
#include "lib/utils/buffer_helper.h"
#include "lib/utils/context_manager_helpers.h"
#include "py/mperrno.h"
-#include "py/nlr.h"
+#include "py/objproperty.h"
#include "py/runtime.h"
//| .. currentmodule:: busio
@@ -137,7 +137,9 @@ static void check_lock(busio_spi_obj_t *self) {
//|
//| Configures the SPI bus. Only valid when locked.
//|
-//| :param int baudrate: the clock rate in Hertz
+//| :param int baudrate: the desired clock rate in Hertz. The actual clock rate may be higher or lower
+//| due to the granularity of available clock settings.
+//| Check the `frequency` attribute for the actual clock rate.
//| :param int polarity: the base state of the clock line (0 or 1)
//| :param int phase: the edge of the clock that data is captured. First (0)
//| or second (1). Rising or falling depends on clock polarity.
@@ -350,6 +352,25 @@ STATIC mp_obj_t busio_spi_write_readinto(size_t n_args, const mp_obj_t *pos_args
}
MP_DEFINE_CONST_FUN_OBJ_KW(busio_spi_write_readinto_obj, 2, busio_spi_write_readinto);
+//| .. attribute:: frequency
+//|
+//| The actual SPI bus frequency. This may not match the frequency requested
+//| due to internal limitations.
+//|
+STATIC mp_obj_t busio_spi_obj_get_frequency(mp_obj_t self_in) {
+ busio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ raise_error_if_deinited(common_hal_busio_spi_deinited(self));
+ return MP_OBJ_NEW_SMALL_INT(common_hal_busio_spi_get_frequency(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_get_frequency_obj, busio_spi_obj_get_frequency);
+
+const mp_obj_property_t busio_spi_frequency_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&busio_spi_get_frequency_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
STATIC const mp_rom_map_elem_t busio_spi_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_spi_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
@@ -362,6 +383,7 @@ STATIC const mp_rom_map_elem_t busio_spi_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&busio_spi_readinto_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&busio_spi_write_obj) },
{ MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&busio_spi_write_readinto_obj) },
+ { MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&busio_spi_frequency_obj) }
};
STATIC MP_DEFINE_CONST_DICT(busio_spi_locals_dict, busio_spi_locals_dict_table);
diff --git a/shared-bindings/busio/SPI.h b/shared-bindings/busio/SPI.h
index b6e5c9b1b..555f32c92 100644
--- a/shared-bindings/busio/SPI.h
+++ b/shared-bindings/busio/SPI.h
@@ -58,4 +58,7 @@ extern bool common_hal_busio_spi_read(busio_spi_obj_t *self, uint8_t *data, size
// Reads and write len bytes simultaneously.
extern bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, uint8_t *data_out, uint8_t *data_in, size_t len);
+// Return actual SPI bus frequency.
+uint32_t common_hal_busio_spi_get_frequency(busio_spi_obj_t* self);
+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_SPI_H