summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2025-08-10 22:35:31 +1000
committerDamien George <damien@micropython.org>2025-09-26 13:44:43 +1000
commit03b4c60fc1cf85e10b88dad3d7b04046b6393a15 (patch)
tree9341e5fc3591a818db6a76cd3c7c4bc47f3fea3d
parent72867bce1d2cf39abe164bebf4d239c6974c9122 (diff)
nrf/modules/machine/spi: Print SPI baudrate, polarity and phase.
On most ports, printing an instance of machine.SPI gives something like: >>> machine.SPI(1) SPI(1, baudrate=328125, polarity=0, phase=0, bits=8) This commit makes the nrf port do the same. The reason for this change is: - make nrf consistent with other ports - allow the `tests/extmod/machine_spi_rate.py` to run on the nrf port (this tests parses the output of str(spi) to get the actual baudrate) Signed-off-by: Damien George <damien@micropython.org>
-rw-r--r--ports/nrf/modules/machine/spi.c24
1 files changed, 23 insertions, 1 deletions
diff --git a/ports/nrf/modules/machine/spi.c b/ports/nrf/modules/machine/spi.c
index b00a5706c..f474c8871 100644
--- a/ports/nrf/modules/machine/spi.c
+++ b/ports/nrf/modules/machine/spi.c
@@ -187,11 +187,33 @@ enum {
ARG_INIT_firstbit
};
+static inline uint32_t machine_hard_spi_get_baudrate(const machine_hard_spi_obj_t *self) {
+ MP_STATIC_ASSERT(NRF_SPI_FREQ_125K == (2 << 24));
+ MP_STATIC_ASSERT(NRF_SPI_FREQ_250K == (4 << 24));
+ MP_STATIC_ASSERT(NRF_SPI_FREQ_500K == (8 << 24));
+ MP_STATIC_ASSERT(NRF_SPI_FREQ_1M == (16 << 24));
+ MP_STATIC_ASSERT(NRF_SPI_FREQ_2M == (32 << 24));
+ MP_STATIC_ASSERT(NRF_SPI_FREQ_4M == (64 << 24));
+ MP_STATIC_ASSERT(NRF_SPI_FREQ_8M == (128 << 24));
+ #if defined(NRF52840_XXAA) && NRFX_SPIM_ENABLED
+ if (self->p_config->frequency == NRF_SPIM_FREQ_16M) {
+ return 16000000;
+ }
+ if (self->p_config->frequency == NRF_SPIM_FREQ_32M) {
+ return 32000000;
+ }
+ #endif
+ return 125000 * (self->p_config->frequency >> 25);
+}
+
static void machine_hard_spi_init_helper(const machine_hard_spi_obj_t *self, mp_arg_val_t *args);
static void machine_hard_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
machine_hard_spi_obj_t *self = self_in;
- mp_printf(print, "SPI(%u)", self->p_spi->drv_inst_idx);
+ unsigned int polarity = self->p_config->mode == NRF_SPI_MODE_0 || self->p_config->mode == NRF_SPI_MODE_1 ? 0 : 1;
+ unsigned int phase = self->p_config->mode == NRF_SPI_MODE_0 || self->p_config->mode == NRF_SPI_MODE_2 ? 0 : 1;
+ mp_printf(print, "SPI(%u, baudrate=%u, polarity=%u, phase=%u)",
+ self->p_spi->drv_inst_idx, machine_hard_spi_get_baudrate(self), polarity, phase);
}
static mp_obj_t machine_hard_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {