Skip to content

Commit 157cc85

Browse files
committed
nrf/modules/machine/spi: Print SPI baudrate, polarity and phase.
Signed-off-by: Damien George <damien@micropython.org>
1 parent c33a02f commit 157cc85

File tree

1 file changed

+23
-1
lines changed
  • ports/nrf/modules/machine

1 file changed

+23
-1
lines changed

ports/nrf/modules/machine/spi.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,33 @@ enum {
187187
ARG_INIT_firstbit
188188
};
189189

190+
static inline uint32_t machine_hard_spi_get_baudrate(const machine_hard_spi_obj_t *self) {
191+
MP_STATIC_ASSERT(NRF_SPI_FREQ_125K == (2 << 24));
192+
MP_STATIC_ASSERT(NRF_SPI_FREQ_250K == (4 << 24));
193+
MP_STATIC_ASSERT(NRF_SPI_FREQ_500K == (8 << 24));
194+
MP_STATIC_ASSERT(NRF_SPI_FREQ_1M == (16 << 24));
195+
MP_STATIC_ASSERT(NRF_SPI_FREQ_2M == (32 << 24));
196+
MP_STATIC_ASSERT(NRF_SPI_FREQ_4M == (64 << 24));
197+
MP_STATIC_ASSERT(NRF_SPI_FREQ_8M == (128 << 24));
198+
#if defined(NRF52840_XXAA) && NRFX_SPIM_ENABLED
199+
if (self->p_config->frequency == NRF_SPIM_FREQ_16M) {
200+
return 16000000;
201+
}
202+
if (self->p_config->frequency == NRF_SPIM_FREQ_32M) {
203+
return 32000000;
204+
}
205+
#endif
206+
return 125000 * (self->p_config->frequency >> 25);
207+
}
208+
190209
static void machine_hard_spi_init_helper(const machine_hard_spi_obj_t *self, mp_arg_val_t *args);
191210

192211
static void machine_hard_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
193212
machine_hard_spi_obj_t *self = self_in;
194-
mp_printf(print, "SPI(%u)", self->p_spi->drv_inst_idx);
213+
unsigned int polarity = self->p_config->mode == NRF_SPI_MODE_0 || self->p_config->mode == NRF_SPI_MODE_1 ? 0 : 1;
214+
unsigned int phase = self->p_config->mode == NRF_SPI_MODE_0 || self->p_config->mode == NRF_SPI_MODE_2 ? 0 : 1;
215+
mp_printf(print, "SPI(%u, baudrate=%u, polarity=%u, phase=%u)",
216+
self->p_spi->drv_inst_idx, machine_hard_spi_get_baudrate(self), polarity, phase);
195217
}
196218

197219
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) {

0 commit comments

Comments
 (0)