summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2018-10-03 13:31:48 -0400
committerGitHub <noreply@github.com>2018-10-03 13:31:48 -0400
commit6049776b2665c2bc7d490a0630cf514a13aecf99 (patch)
treee0a25a07a459a1b84610c4362fc5072a43b89da4
parenta47eaa521b39f90667da17a4ec7a1c256dec2ab5 (diff)
parent86ae707b0871f3b5f7766bfcd920635b337a6b29 (diff)
Merge branch 'master' into nrf52_uart_io
-rw-r--r--locale/circuitpython.pot8
-rw-r--r--locale/de_DE.po8
-rw-r--r--locale/en_US.po8
-rw-r--r--locale/es.po8
-rw-r--r--locale/fil.po10
-rw-r--r--locale/fr.po10
-rw-r--r--ports/nrf/common-hal/busio/SPI.c90
-rw-r--r--shared-bindings/busio/SPI.c15
8 files changed, 82 insertions, 75 deletions
diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot
index ca8d55dcb..232ad7c46 100644
--- a/locale/circuitpython.pot
+++ b/locale/circuitpython.pot
@@ -2007,19 +2007,19 @@ msgstr ""
msgid "Buffer must be at least length 1"
msgstr ""
-#: shared-bindings/bitbangio/SPI.c:151 shared-bindings/busio/SPI.c:168
+#: shared-bindings/bitbangio/SPI.c:151 shared-bindings/busio/SPI.c:175
msgid "Invalid polarity"
msgstr ""
-#: shared-bindings/bitbangio/SPI.c:155 shared-bindings/busio/SPI.c:172
+#: shared-bindings/bitbangio/SPI.c:155 shared-bindings/busio/SPI.c:179
msgid "Invalid phase"
msgstr ""
-#: shared-bindings/bitbangio/SPI.c:159 shared-bindings/busio/SPI.c:176
+#: shared-bindings/bitbangio/SPI.c:159 shared-bindings/busio/SPI.c:183
msgid "Invalid number of bits"
msgstr ""
-#: shared-bindings/bitbangio/SPI.c:284 shared-bindings/busio/SPI.c:341
+#: shared-bindings/bitbangio/SPI.c:284 shared-bindings/busio/SPI.c:348
msgid "buffer slices must be of equal length"
msgstr ""
diff --git a/locale/de_DE.po b/locale/de_DE.po
index 4579f8c67..790b23442 100644
--- a/locale/de_DE.po
+++ b/locale/de_DE.po
@@ -2020,19 +2020,19 @@ msgstr ""
msgid "Buffer must be at least length 1"
msgstr ""
-#: shared-bindings/bitbangio/SPI.c:151 shared-bindings/busio/SPI.c:168
+#: shared-bindings/bitbangio/SPI.c:151 shared-bindings/busio/SPI.c:175
msgid "Invalid polarity"
msgstr ""
-#: shared-bindings/bitbangio/SPI.c:155 shared-bindings/busio/SPI.c:172
+#: shared-bindings/bitbangio/SPI.c:155 shared-bindings/busio/SPI.c:179
msgid "Invalid phase"
msgstr ""
-#: shared-bindings/bitbangio/SPI.c:159 shared-bindings/busio/SPI.c:176
+#: shared-bindings/bitbangio/SPI.c:159 shared-bindings/busio/SPI.c:183
msgid "Invalid number of bits"
msgstr ""
-#: shared-bindings/bitbangio/SPI.c:284 shared-bindings/busio/SPI.c:341
+#: shared-bindings/bitbangio/SPI.c:284 shared-bindings/busio/SPI.c:348
msgid "buffer slices must be of equal length"
msgstr ""
diff --git a/locale/en_US.po b/locale/en_US.po
index 1a120e668..6e576f150 100644
--- a/locale/en_US.po
+++ b/locale/en_US.po
@@ -2007,19 +2007,19 @@ msgstr ""
msgid "Buffer must be at least length 1"
msgstr ""
-#: shared-bindings/bitbangio/SPI.c:151 shared-bindings/busio/SPI.c:168
+#: shared-bindings/bitbangio/SPI.c:151 shared-bindings/busio/SPI.c:175
msgid "Invalid polarity"
msgstr ""
-#: shared-bindings/bitbangio/SPI.c:155 shared-bindings/busio/SPI.c:172
+#: shared-bindings/bitbangio/SPI.c:155 shared-bindings/busio/SPI.c:179
msgid "Invalid phase"
msgstr ""
-#: shared-bindings/bitbangio/SPI.c:159 shared-bindings/busio/SPI.c:176
+#: shared-bindings/bitbangio/SPI.c:159 shared-bindings/busio/SPI.c:183
msgid "Invalid number of bits"
msgstr ""
-#: shared-bindings/bitbangio/SPI.c:284 shared-bindings/busio/SPI.c:341
+#: shared-bindings/bitbangio/SPI.c:284 shared-bindings/busio/SPI.c:348
msgid "buffer slices must be of equal length"
msgstr ""
diff --git a/locale/es.po b/locale/es.po
index 664fd9be7..2ac904943 100644
--- a/locale/es.po
+++ b/locale/es.po
@@ -2053,19 +2053,19 @@ msgstr ""
msgid "Buffer must be at least length 1"
msgstr ""
-#: shared-bindings/bitbangio/SPI.c:151 shared-bindings/busio/SPI.c:168
+#: shared-bindings/bitbangio/SPI.c:151 shared-bindings/busio/SPI.c:175
msgid "Invalid polarity"
msgstr ""
-#: shared-bindings/bitbangio/SPI.c:155 shared-bindings/busio/SPI.c:172
+#: shared-bindings/bitbangio/SPI.c:155 shared-bindings/busio/SPI.c:179
msgid "Invalid phase"
msgstr ""
-#: shared-bindings/bitbangio/SPI.c:159 shared-bindings/busio/SPI.c:176
+#: shared-bindings/bitbangio/SPI.c:159 shared-bindings/busio/SPI.c:183
msgid "Invalid number of bits"
msgstr ""
-#: shared-bindings/bitbangio/SPI.c:284 shared-bindings/busio/SPI.c:341
+#: shared-bindings/bitbangio/SPI.c:284 shared-bindings/busio/SPI.c:348
msgid "buffer slices must be of equal length"
msgstr ""
diff --git a/locale/fil.po b/locale/fil.po
index 783fb6771..7c8c4fdfe 100644
--- a/locale/fil.po
+++ b/locale/fil.po
@@ -745,7 +745,7 @@ msgstr "hindi sinusuportahan ang bytes > 8 bits"
#: ports/nrf/common-hal/busio/UART.c:364
#, fuzzy
msgid "busio.UART not available"
-msgstr "hindi pa implemented ang busio.UART"
+msgstr ""
#: ports/nrf/common-hal/microcontroller/Processor.c:49
#, c-format
@@ -2050,19 +2050,19 @@ msgstr "Function nangangailangan ng lock"
msgid "Buffer must be at least length 1"
msgstr "Buffer dapat ay hindi baba sa 1 na haba"
-#: shared-bindings/bitbangio/SPI.c:151 shared-bindings/busio/SPI.c:168
+#: shared-bindings/bitbangio/SPI.c:151 shared-bindings/busio/SPI.c:175
msgid "Invalid polarity"
msgstr "Mali ang polarity"
-#: shared-bindings/bitbangio/SPI.c:155 shared-bindings/busio/SPI.c:172
+#: shared-bindings/bitbangio/SPI.c:155 shared-bindings/busio/SPI.c:179
msgid "Invalid phase"
msgstr "Mali ang phase"
-#: shared-bindings/bitbangio/SPI.c:159 shared-bindings/busio/SPI.c:176
+#: shared-bindings/bitbangio/SPI.c:159 shared-bindings/busio/SPI.c:183
msgid "Invalid number of bits"
msgstr "Mali ang bilang ng bits"
-#: shared-bindings/bitbangio/SPI.c:284 shared-bindings/busio/SPI.c:341
+#: shared-bindings/bitbangio/SPI.c:284 shared-bindings/busio/SPI.c:348
msgid "buffer slices must be of equal length"
msgstr "aarehas na haba dapat ang buffer slices"
diff --git a/locale/fr.po b/locale/fr.po
index 3af5066e6..04d7a0665 100644
--- a/locale/fr.po
+++ b/locale/fr.po
@@ -741,7 +741,7 @@ msgstr "octets > 8 bits non supporté"
#: ports/nrf/common-hal/busio/UART.c:364
#, fuzzy
msgid "busio.UART not available"
-msgstr "busio.UART pas encore implémenté"
+msgstr "busio.UART n'est pas disponible"
#: ports/nrf/common-hal/microcontroller/Processor.c:49
#, c-format
@@ -2041,19 +2041,19 @@ msgstr "La fonction nécessite un verrou"
msgid "Buffer must be at least length 1"
msgstr "Le tampon doit être de longueur au moins 1"
-#: shared-bindings/bitbangio/SPI.c:151 shared-bindings/busio/SPI.c:168
+#: shared-bindings/bitbangio/SPI.c:151 shared-bindings/busio/SPI.c:175
msgid "Invalid polarity"
msgstr "Polarité invalide"
-#: shared-bindings/bitbangio/SPI.c:155 shared-bindings/busio/SPI.c:172
+#: shared-bindings/bitbangio/SPI.c:155 shared-bindings/busio/SPI.c:179
msgid "Invalid phase"
msgstr "Phase invalide"
-#: shared-bindings/bitbangio/SPI.c:159 shared-bindings/busio/SPI.c:176
+#: shared-bindings/bitbangio/SPI.c:159 shared-bindings/busio/SPI.c:183
msgid "Invalid number of bits"
msgstr "Nombre de bits invalide"
-#: shared-bindings/bitbangio/SPI.c:284 shared-bindings/busio/SPI.c:341
+#: shared-bindings/bitbangio/SPI.c:284 shared-bindings/busio/SPI.c:348
msgid "buffer slices must be of equal length"
msgstr "les slices de tampon doivent être de longueurs égales"
diff --git a/ports/nrf/common-hal/busio/SPI.c b/ports/nrf/common-hal/busio/SPI.c
index 54ae6e47c..405d19c23 100644
--- a/ports/nrf/common-hal/busio/SPI.c
+++ b/ports/nrf/common-hal/busio/SPI.c
@@ -65,40 +65,40 @@ void spi_reset(void) {
}
}
-// Convert frequency to clock-speed-dependent value
+// Convert frequency to clock-speed-dependent value. Choose the next lower baudrate if in between
+// available baudrates.
static nrf_spim_frequency_t baudrate_to_spim_frequency(const uint32_t baudrate) {
- if (baudrate <= 125000) {
- return NRF_SPIM_FREQ_125K;
- }
- if (baudrate <= 250000) {
- return NRF_SPIM_FREQ_250K;
- }
- if (baudrate <= 500000) {
- return NRF_SPIM_FREQ_500K;
- }
- if (baudrate <= 1000000) {
- return NRF_SPIM_FREQ_1M;
- }
- if (baudrate <= 2000000) {
- return NRF_SPIM_FREQ_2M;
- }
- if (baudrate <= 4000000) {
- return NRF_SPIM_FREQ_4M;
- }
- if (baudrate <= 8000000) {
- return NRF_SPIM_FREQ_8M;
- }
-#ifdef SPIM_FREQUENCY_FREQUENCY_M16
- if (baudrate <= 16000000) {
- return NRF_SPIM_FREQ_16M;
- }
-#endif
+ static const struct {
+ const uint32_t boundary;
+ nrf_spim_frequency_t spim_frequency;
+ } baudrate_map[] = {
#ifdef SPIM_FREQUENCY_FREQUENCY_M32
- return NRF_SPIM_FREQ_32M;
-#else
- return NRF_SPIM_FREQ_8M;
+ { 32000000, NRF_SPIM_FREQ_32M },
+#endif
+#ifdef SPIM_FREQUENCY_FREQUENCY_M16
+ { 16000000, NRF_SPIM_FREQ_16M },
#endif
+ { 8000000, NRF_SPIM_FREQ_8M },
+ { 4000000, NRF_SPIM_FREQ_4M },
+ { 2000000, NRF_SPIM_FREQ_2M },
+ { 1000000, NRF_SPIM_FREQ_1M },
+ { 500000, NRF_SPIM_FREQ_500K },
+ { 250000, NRF_SPIM_FREQ_250K },
+ { 0, NRF_SPIM_FREQ_125K },
+ };
+
+ size_t i = 0;
+ uint32_t boundary;
+ do {
+ boundary = baudrate_map[i].boundary;
+ if (baudrate >= boundary) {
+ return baudrate_map[i].spim_frequency;
+ }
+ i++;
+ } while (boundary != 0);
+ // Should not get here.
+ return 0;
}
void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi, const mcu_pin_obj_t * miso) {
@@ -168,26 +168,26 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
}
bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {
- // nrf52 does not support 16 bit
- if (bits != 8)
+ // nrf52 does not support 16 bit
+ if (bits != 8) {
return false;
+ }
- if (baudrate > self->spim_peripheral->max_frequency_MHz * 1000000) {
- mp_raise_ValueError(translate("Baud rate too high for this SPI peripheral"));
- return false;
- }
- nrf_spim_frequency_set(self->spim_peripheral->spim.p_reg, baudrate_to_spim_frequency(baudrate));
+ // Set desired frequency, rounding down, and don't go above available frequency for this SPIM.
+ nrf_spim_frequency_set(self->spim_peripheral->spim.p_reg,
+ baudrate_to_spim_frequency(MIN(baudrate,
+ self->spim_peripheral->max_frequency_MHz * 1000000)));
- nrf_spim_mode_t mode = NRF_SPIM_MODE_0;
- if (polarity) {
- mode = (phase) ? NRF_SPIM_MODE_3 : NRF_SPIM_MODE_2;
- } else {
- mode = (phase) ? NRF_SPIM_MODE_1 : NRF_SPIM_MODE_0;
- }
+ nrf_spim_mode_t mode = NRF_SPIM_MODE_0;
+ if (polarity) {
+ mode = (phase) ? NRF_SPIM_MODE_3 : NRF_SPIM_MODE_2;
+ } else {
+ mode = (phase) ? NRF_SPIM_MODE_1 : NRF_SPIM_MODE_0;
+ }
- nrf_spim_configure(self->spim_peripheral->spim.p_reg, mode, NRF_SPIM_BIT_ORDER_MSB_FIRST);
+ nrf_spim_configure(self->spim_peripheral->spim.p_reg, mode, NRF_SPIM_BIT_ORDER_MSB_FIRST);
- return true;
+ return true;
}
bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) {
diff --git a/shared-bindings/busio/SPI.c b/shared-bindings/busio/SPI.c
index 7983a3a56..d30bbbe06 100644
--- a/shared-bindings/busio/SPI.c
+++ b/shared-bindings/busio/SPI.c
@@ -136,19 +136,26 @@ static void check_lock(busio_spi_obj_t *self) {
//| .. method:: SPI.configure(\*, baudrate=100000, polarity=0, phase=0, bits=8)
//|
-//| Configures the SPI bus. Only valid when locked.
+//| Configures the SPI bus. The SPI object must be locked.
//|
//| :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.
-//| **Note:** on the SAMD21, it is possible to set the baud rate to 24 MHz, but that
-//| speed is not guaranteed to work. 12 MHz is the next available lower speed, and is
-//| within spec for the SAMD21.
//| :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.
//| :param int bits: the number of bits per word
//|
+//| .. note:: On the SAMD21, it is possible to set the baudrate to 24 MHz, but that
+//| speed is not guaranteed to work. 12 MHz is the next available lower speed, and is
+//| within spec for the SAMD21.
+//|
+//| .. note:: On the nRF52832, these baudrates are available: 125kHz, 250kHz, 1MHz, 2MHz, 4MHz,
+//| and 8MHz. On the nRF52840, 16MHz and 32MHz are also available, but only on the first
+//| `busio.SPI` object you create. Two more ``busio.SPI`` objects can be created, but they are restricted
+//| to 8MHz maximum. This is a hardware restriction: there is only one high-speed SPI peripheral.
+//| If you pick a a baudrate other than one of these, the nearest lower
+//| baudrate will be chosen, with a minimum of 125kHz.
STATIC mp_obj_t busio_spi_configure(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits };
static const mp_arg_t allowed_args[] = {