diff --git a/cores/nRF5/nordic/nrfx_config.h b/cores/nRF5/nordic/nrfx_config.h index 4405f442..e3572f17 100644 --- a/cores/nRF5/nordic/nrfx_config.h +++ b/cores/nRF5/nordic/nrfx_config.h @@ -1,15 +1,26 @@ #ifndef NRFX_CONFIG_H__ #define NRFX_CONFIG_H__ -#define NRFX_POWER_ENABLED 1 -#define NRFX_POWER_CONFIG_IRQ_PRIORITY 7 +#define NRFX_POWER_ENABLED 1 +#define NRFX_POWER_CONFIG_IRQ_PRIORITY 7 //#define NRFX_CLOCK_ENABLED 0 +#define NRFX_SPIM_ENABLED 1 +#define NRFX_SPIM_MISO_PULL_CFG 1 // pulldown +#define NRFX_SPIM_EXTENDED_ENABLED 0 + +#define NRFX_SPIM0_ENABLED 0 // used as I2C +#define NRFX_SPIM1_ENABLED 0 // used as I2C +#define NRFX_SPIM2_ENABLED 1 + #ifdef NRF52840_XXAA -#define NRFX_QSPI_ENABLED 1 + #define NRFX_QSPI_ENABLED 1 + #define NRFX_SPIM3_ENABLED 1 #else -#define NRFX_QSPI_ENABLED 0 + #define NRFX_QSPI_ENABLED 0 + #define NRFX_SPIM3_ENABLED 0 #endif + #endif // NRFX_CONFIG_H__ diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino index a7f5ff2a..2bb3854a 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino @@ -239,6 +239,7 @@ void bleuart_rx_callback(uint16_t conn_hdl) if ( totalPixel == imageWidth*imageHeight ) { uint8_t crc = bleuart.read(); + (void) crc; // do checksum later // print speed summary @@ -310,6 +311,9 @@ void print_summary(uint32_t count, uint32_t ms) void bleuart_overflow_callback(uint16_t conn_hdl, uint16_t leftover) { + (void) conn_hdl; + (void) leftover; + Serial.println("BLEUART rx buffer OVERFLOWED!"); Serial.println("Please increase buffer size for bleuart"); @@ -335,6 +339,7 @@ void bleuart_overflow_callback(uint16_t conn_hdl, uint16_t leftover) */ void disconnect_callback(uint16_t conn_handle, uint8_t reason) { + (void) conn_handle; (void) reason; tft.fillScreen(COLOR_BLACK); diff --git a/libraries/Bluefruit52Lib/examples/Projects/homekit/homekit_lightbulb/.skip b/libraries/Bluefruit52Lib/examples/Projects/homekit/homekit_lightbulb/.skip new file mode 100644 index 00000000..e69de29b diff --git a/libraries/Bluefruit52Lib/examples/Projects/homekit/homekit_lightbulb/homekit_lightbulb.ino.cpp b/libraries/Bluefruit52Lib/examples/Projects/homekit/homekit_lightbulb/homekit_lightbulb.ino similarity index 100% rename from libraries/Bluefruit52Lib/examples/Projects/homekit/homekit_lightbulb/homekit_lightbulb.ino.cpp rename to libraries/Bluefruit52Lib/examples/Projects/homekit/homekit_lightbulb/homekit_lightbulb.ino diff --git a/libraries/PDM/examples/PDMSerialPlotter/PDMSerialPlotter.ino b/libraries/PDM/examples/PDMSerialPlotter/PDMSerialPlotter.ino index 977a3835..1c966a38 100644 --- a/libraries/PDM/examples/PDMSerialPlotter/PDMSerialPlotter.ino +++ b/libraries/PDM/examples/PDMSerialPlotter/PDMSerialPlotter.ino @@ -12,7 +12,7 @@ // The default Circuit Playground Bluefruit pins // data pin, clock pin, power pin (-1 if not used) -PDMClass PDM(33, 34, -1); +PDMClass PDM(24, 25, -1); // buffer to read samples into, each sample is 16-bits short sampleBuffer[256]; @@ -41,13 +41,13 @@ void setup() { void loop() { // wait for samples to be read - if (samplesRead) { + PDM.IrqHandler(); + if (samplesRead) { // print samples to the serial monitor or plotter for (int i = 0; i < samplesRead; i++) { Serial.println(sampleBuffer[i]); } - // clear the read count samplesRead = 0; } @@ -62,4 +62,4 @@ void onPDMdata() { // 16-bit, 2 bytes per sample samplesRead = bytesAvailable / 2; -} +} \ No newline at end of file diff --git a/libraries/SPI/SPI.cpp b/libraries/SPI/SPI.cpp index e0c4a2a5..7b15d492 100644 --- a/libraries/SPI/SPI.cpp +++ b/libraries/SPI/SPI.cpp @@ -2,6 +2,7 @@ * SPI Master library for nRF5x. * Copyright (c) 2015 Arduino LLC * Copyright (c) 2016 Sandeep Mistry All right reserved. + * Copyright (c) 2019 Ha Thach for Adafruit Industries. All right reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -18,22 +19,49 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +// Due to nRF52832 Errata with SPIM, we will only use SPIM for 840 variant +// https://infocenter.nordicsemi.com/topic/errata_nRF52832_Rev2/ERR/nRF52832/Rev2/latest/anomaly_832_58.html +#ifdef NRF52840_XXAA + #include "SPI.h" #include #include #include -#define SPI_IMODE_NONE 0 -#define SPI_IMODE_EXTINT 1 -#define SPI_IMODE_GLOBAL 2 - -const SPISettings DEFAULT_SPI_SETTINGS = SPISettings(); - -SPIClass::SPIClass(NRF_SPI_Type *p_spi, uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI) +SPIClass::SPIClass(NRF_SPIM_Type *p_spi, uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI) { initialized = false; assert(p_spi != NULL); - _p_spi = p_spi; + + _spim.p_reg = p_spi; + + // not used by SPIM module + _p_spi = NULL; + + // SPIM0 & SPIM1 are used for I2C +#if NRFX_SPIM0_ENABLED + if ( NRF_SPIM0 == p_spi ) { + _spim.drv_inst_idx = NRFX_SPIM0_INST_IDX; + } +#endif + +#if NRFX_SPIM1_ENABLED + if ( NRF_SPIM1 == p_spi ) { + _spim.drv_inst_idx = NRFX_SPIM1_INST_IDX; + } +#endif + +#if NRFX_SPIM2_ENABLED + if ( NRF_SPIM2 == p_spi ) { + _spim.drv_inst_idx = NRFX_SPIM2_INST_IDX; + } +#endif + +#if NRFX_SPIM3_ENABLED + if ( NRF_SPIM3 == p_spi ) { + _spim.drv_inst_idx = NRFX_SPIM3_INST_IDX; + } +#endif // pins _uc_pinMiso = g_ADigitalPinMap[uc_pinMISO]; @@ -41,69 +69,39 @@ SPIClass::SPIClass(NRF_SPI_Type *p_spi, uint8_t uc_pinMISO, uint8_t uc_pinSCK, u _uc_pinMosi = g_ADigitalPinMap[uc_pinMOSI]; _dataMode = SPI_MODE0; - _bitOrder = SPI_CONFIG_ORDER_MsbFirst; + _bitOrder = NRF_SPIM_BIT_ORDER_MSB_FIRST; } void SPIClass::begin() { - init(); - - _p_spi->PSELSCK = _uc_pinSCK; - _p_spi->PSELMOSI = _uc_pinMosi; - _p_spi->PSELMISO = _uc_pinMiso; - - config(DEFAULT_SPI_SETTINGS); -} - -void SPIClass::init() -{ - if (initialized) - return; - interruptMode = SPI_IMODE_NONE; - interruptSave = 0; - interruptMask = 0; + if (initialized) return; initialized = true; -} -void SPIClass::config(SPISettings settings) -{ - _p_spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos); + nrfx_spim_config_t cfg = + { + .sck_pin = _uc_pinSCK, + .mosi_pin = _uc_pinMosi, + .miso_pin = _uc_pinMiso, + .ss_pin = NRFX_SPIM_PIN_NOT_USED, + .ss_active_high = false, + .irq_priority = 3, + .orc = 0xFF, + // default setting 4 Mhz, Mode 0, MSB first + .frequency = NRF_SPIM_FREQ_4M, + .mode = NRF_SPIM_MODE_0, + .bit_order = NRF_SPIM_BIT_ORDER_MSB_FIRST, + }; - uint32_t config = settings.bitOrder; + _dataMode = SPI_MODE0; + _bitOrder = NRF_SPIM_BIT_ORDER_MSB_FIRST; - switch (settings.dataMode) { - default: - case SPI_MODE0: - config |= (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos); - config |= (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos); - break; - - case SPI_MODE1: - config |= (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos); - config |= (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos); - break; - - case SPI_MODE2: - config |= (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos); - config |= (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos); - break; - - case SPI_MODE3: - config |= (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos); - config |= (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos); - break; - } - - _p_spi->CONFIG = config; - _p_spi->FREQUENCY = settings.clockFreq; - - _p_spi->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos); + // blocking + nrfx_spim_init(&_spim, &cfg, NULL, NULL); } void SPIClass::end() { - _p_spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos); - + nrfx_spim_uninit(&_spim); initialized = false; } @@ -113,121 +111,110 @@ void SPIClass::usingInterrupt(int /*interruptNumber*/) void SPIClass::beginTransaction(SPISettings settings) { - config(settings); + nrf_spim_disable(_spim.p_reg); + + this->_dataMode = settings.dataMode; + this->_bitOrder = (settings.bitOrder == MSBFIRST ? NRF_SPIM_BIT_ORDER_MSB_FIRST : NRF_SPIM_BIT_ORDER_LSB_FIRST); + + nrf_spim_configure(_spim.p_reg, (nrf_spim_mode_t) _dataMode, (nrf_spim_bit_order_t) _bitOrder); + + setClockDivider(F_CPU / settings.clockFreq); + + nrf_spim_enable(_spim.p_reg); } void SPIClass::endTransaction(void) { - _p_spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos); + nrf_spim_disable(_spim.p_reg); } void SPIClass::setBitOrder(BitOrder order) { - this->_bitOrder = (order == MSBFIRST ? SPI_CONFIG_ORDER_MsbFirst : SPI_CONFIG_ORDER_LsbFirst); - - uint32_t config = this->_bitOrder; - - switch (this->_dataMode) { - default: - case SPI_MODE0: - config |= (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos); - config |= (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos); - break; - - case SPI_MODE1: - config |= (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos); - config |= (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos); - break; - - case SPI_MODE2: - config |= (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos); - config |= (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos); - break; - - case SPI_MODE3: - config |= (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos); - config |= (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos); - break; - } - - _p_spi->CONFIG = config; + this->_bitOrder = (order == MSBFIRST ? NRF_SPIM_BIT_ORDER_MSB_FIRST : NRF_SPIM_BIT_ORDER_LSB_FIRST); + nrf_spim_configure(_spim.p_reg, (nrf_spim_mode_t) _dataMode, (nrf_spim_bit_order_t) _bitOrder); } void SPIClass::setDataMode(uint8_t mode) { this->_dataMode = mode; - - uint32_t config = this->_bitOrder; - - switch (this->_dataMode) { - default: - case SPI_MODE0: - config |= (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos); - config |= (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos); - break; - - case SPI_MODE1: - config |= (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos); - config |= (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos); - break; - - case SPI_MODE2: - config |= (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos); - config |= (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos); - break; - - case SPI_MODE3: - config |= (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos); - config |= (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos); - break; - } - - _p_spi->CONFIG = config; + nrf_spim_configure(_spim.p_reg, (nrf_spim_mode_t) _dataMode, (nrf_spim_bit_order_t) _bitOrder); } -void SPIClass::setClockDivider(uint8_t div) +void SPIClass::setClockDivider(uint32_t div) { - uint32_t clockFreq; + nrf_spim_frequency_t clockFreq; // Adafruit Note: nrf52 run at 64MHz if (div >= SPI_CLOCK_DIV512) { - clockFreq = SPI_FREQUENCY_FREQUENCY_K125; + clockFreq = NRF_SPIM_FREQ_125K; } else if (div >= SPI_CLOCK_DIV256) { - clockFreq = SPI_FREQUENCY_FREQUENCY_K250; + clockFreq = NRF_SPIM_FREQ_250K; } else if (div >= SPI_CLOCK_DIV128) { - clockFreq = SPI_FREQUENCY_FREQUENCY_K500; + clockFreq = NRF_SPIM_FREQ_500K; } else if (div >= SPI_CLOCK_DIV64) { - clockFreq = SPI_FREQUENCY_FREQUENCY_M1; + clockFreq = NRF_SPIM_FREQ_1M; } else if (div >= SPI_CLOCK_DIV32) { - clockFreq = SPI_FREQUENCY_FREQUENCY_M2; + clockFreq = NRF_SPIM_FREQ_2M; } else if (div >= SPI_CLOCK_DIV16) { - clockFreq = SPI_FREQUENCY_FREQUENCY_M4; + clockFreq = NRF_SPIM_FREQ_4M; + } else if (div >= SPI_CLOCK_DIV8) { + clockFreq = NRF_SPIM_FREQ_8M; } else { - clockFreq = SPI_FREQUENCY_FREQUENCY_M8; + if ( _spim.p_reg == NRF_SPIM3 ) + { + if (div >= SPI_CLOCK_DIV4) { + clockFreq = NRF_SPIM_FREQ_16M; + }else { + clockFreq = NRF_SPIM_FREQ_32M; + } + }else + { + clockFreq = NRF_SPIM_FREQ_8M; + } } - _p_spi->FREQUENCY = clockFreq; + nrf_spim_frequency_set(_spim.p_reg, clockFreq); +} + +void SPIClass::transfer(const void *tx_buf, void *rx_buf, size_t count) +{ + nrfx_spim_xfer_desc_t xfer_desc = + { + .p_tx_buffer = (uint8_t*) tx_buf, + .tx_length = tx_buf ? count : 0, + .p_rx_buffer = (uint8_t*) rx_buf, + .rx_length = rx_buf ? count : 0, + }; + + nrfx_spim_xfer(&_spim, &xfer_desc, 0); +} + +void SPIClass::transfer(void *buf, size_t count) +{ + nrfx_spim_xfer_desc_t xfer_desc = + { + .p_tx_buffer = (uint8_t*) buf, + .tx_length = count, + .p_rx_buffer = (uint8_t*) buf, + .rx_length = count, + }; + + nrfx_spim_xfer(&_spim, &xfer_desc, 0); } byte SPIClass::transfer(uint8_t data) { - _p_spi->TXD = data; - - while(!_p_spi->EVENTS_READY); - - data = _p_spi->RXD; - - _p_spi->EVENTS_READY = 0x0UL; - + transfer(&data, 1); return data; } uint16_t SPIClass::transfer16(uint16_t data) { + union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } t; t.val = data; - if (_bitOrder == SPI_CONFIG_ORDER_LsbFirst) { + if (_bitOrder == NRF_SPIM_BIT_ORDER_LSB_FIRST) { t.lsb = transfer(t.lsb); t.msb = transfer(t.msb); } else { @@ -247,8 +234,12 @@ void SPIClass::detachInterrupt() { } #if SPI_INTERFACES_COUNT > 0 -SPIClass SPI (NRF_SPI0, PIN_SPI_MISO, PIN_SPI_SCK, PIN_SPI_MOSI); +// use SPIM3 for highspeed 32Mhz +SPIClass SPI(NRF_SPIM3, PIN_SPI_MISO, PIN_SPI_SCK, PIN_SPI_MOSI); #endif + #if SPI_INTERFACES_COUNT > 1 -SPIClass SPI1(NRF_SPI1, PIN_SPI1_MISO, PIN_SPI1_SCK, PIN_SPI1_MOSI); +SPIClass SPI1(NRF_SPIM2, PIN_SPI1_MISO, PIN_SPI1_SCK, PIN_SPI1_MOSI); #endif + +#endif // NRF52840_XXAA diff --git a/libraries/SPI/SPI.h b/libraries/SPI/SPI.h index 778575bc..8f7eba02 100644 --- a/libraries/SPI/SPI.h +++ b/libraries/SPI/SPI.h @@ -23,6 +23,8 @@ #include +#include "nrfx_spim.h" + // SPI_HAS_TRANSACTION means SPI has // - beginTransaction() // - endTransaction() @@ -30,113 +32,82 @@ // - SPISetting(clock, bitOrder, dataMode) #define SPI_HAS_TRANSACTION 1 -#define SPI_MODE0 0x02 -#define SPI_MODE1 0x00 -#define SPI_MODE2 0x03 -#define SPI_MODE3 0x01 +#define SPI_MODE0 0x00 +#define SPI_MODE1 0x01 +#define SPI_MODE2 0x02 +#define SPI_MODE3 0x03 class SPISettings { public: - SPISettings(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) { - if (__builtin_constant_p(clock)) { - init_AlwaysInline(clock, bitOrder, dataMode); - } else { - init_MightInline(clock, bitOrder, dataMode); + SPISettings(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) { + this->clockFreq = clock; + this->bitOrder = bitOrder; + this->dataMode = dataMode; } - } - // Default speed set to 4MHz, SPI mode set to MODE 0 and Bit order set to MSB first. - SPISettings() { init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0); } + // Default speed set to 4MHz, SPI mode set to MODE 0 and Bit order set to MSB first. + SPISettings() { + this->clockFreq = 4000000; + this->bitOrder = MSBFIRST; + this->dataMode = SPI_MODE0; + } private: - void init_MightInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) { - init_AlwaysInline(clock, bitOrder, dataMode); - } + uint32_t clockFreq; + uint8_t dataMode; + uint8_t bitOrder; - void init_AlwaysInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) __attribute__((__always_inline__)) { - if (clock <= 125000) { - this->clockFreq = SPI_FREQUENCY_FREQUENCY_K125; - } else if (clock <= 250000) { - this->clockFreq = SPI_FREQUENCY_FREQUENCY_K250; - } else if (clock <= 500000) { - this->clockFreq = SPI_FREQUENCY_FREQUENCY_K500; - } else if (clock <= 1000000) { - this->clockFreq = SPI_FREQUENCY_FREQUENCY_M1; - } else if (clock <= 2000000) { - this->clockFreq = SPI_FREQUENCY_FREQUENCY_M2; - } else if (clock <= 4000000) { - this->clockFreq = SPI_FREQUENCY_FREQUENCY_M4; - } else { - this->clockFreq = SPI_FREQUENCY_FREQUENCY_M8; - } - - this->bitOrder = (bitOrder == MSBFIRST ? SPI_CONFIG_ORDER_MsbFirst : SPI_CONFIG_ORDER_LsbFirst); - this->dataMode = dataMode; - } - - uint32_t clockFreq; - uint8_t dataMode; - uint32_t bitOrder; - - friend class SPIClass; + friend class SPIClass; }; class SPIClass { public: - SPIClass(NRF_SPI_Type *p_spi, uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI); + SPIClass(NRF_SPIM_Type *p_spi, uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI); + byte transfer(uint8_t data); + uint16_t transfer16(uint16_t data); + void transfer(void *buf, size_t count); + void transfer(const void *tx_buf, void *rx_buf, size_t count); - byte transfer(uint8_t data); - uint16_t transfer16(uint16_t data); - inline void transfer(void *buf, size_t count); + // Transaction Functions + void usingInterrupt(int interruptNumber); + void beginTransaction(SPISettings settings); + void endTransaction(void); - // Transaction Functions - void usingInterrupt(int interruptNumber); - void beginTransaction(SPISettings settings); - void endTransaction(void); + // SPI Configuration methods + void attachInterrupt(); + void detachInterrupt(); - // SPI Configuration methods - void attachInterrupt(); - void detachInterrupt(); + void begin(); + void end(); - void begin(); - void end(); - - void setBitOrder(BitOrder order); - void setDataMode(uint8_t uc_mode); - void setClockDivider(uint8_t uc_div); + void setBitOrder(BitOrder order); + void setDataMode(uint8_t uc_mode); + void setClockDivider(uint32_t uc_div); private: - void init(); - void config(SPISettings settings); + nrfx_spim_t _spim; + NRF_SPI_Type *_p_spi; - NRF_SPI_Type *_p_spi; - uint8_t _uc_pinMiso; - uint8_t _uc_pinMosi; - uint8_t _uc_pinSCK; + uint8_t _uc_pinMiso; + uint8_t _uc_pinMosi; + uint8_t _uc_pinSCK; - uint8_t _dataMode; - uint32_t _bitOrder; + uint8_t _dataMode; + uint8_t _bitOrder; - bool initialized; - uint8_t interruptMode; - char interruptSave; - uint32_t interruptMask; + bool initialized; }; -void SPIClass::transfer(void *buf, size_t count) -{ - // TODO: Optimize for faster block-transfer - uint8_t *buffer = reinterpret_cast(buf); - for (size_t i=0; i 0 extern SPIClass SPI; #endif +#if SPI_INTERFACES_COUNT > 1 +extern SPIClass SPI1; +#endif + // For compatibility with sketches designed for AVR @ 64 MHz // New programs should use SPI.beginTransaction to set the SPI clock #if F_CPU == 64000000 // feather52 run @ 64Mhz diff --git a/libraries/SPI/SPI_nrf52832.cpp b/libraries/SPI/SPI_nrf52832.cpp new file mode 100644 index 00000000..7a9e8bd0 --- /dev/null +++ b/libraries/SPI/SPI_nrf52832.cpp @@ -0,0 +1,222 @@ +/* + * SPI Master library for nRF5x. + * Copyright (c) 2015 Arduino LLC + * Copyright (c) 2016 Sandeep Mistry All right reserved. + * Copyright (c) 2019 Ha Thach for Adafruit Industries. All right reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +// Due to nRF52832 Errata with SPIM, we use the legacy SPI for 832 variant +// https://infocenter.nordicsemi.com/topic/errata_nRF52832_Rev2/ERR/nRF52832/Rev2/latest/anomaly_832_58.html +#ifdef NRF52832_XXAA + +#include "SPI.h" +#include +#include +#include + + +const SPISettings DEFAULT_SPI_SETTINGS = SPISettings(); + +uint32_t mode2config(uint8_t mode) +{ + uint32_t config = 0; + + switch (mode) + { + default: + case SPI_MODE0: + config |= (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos); + config |= (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos); + break; + + case SPI_MODE1: + config |= (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos); + config |= (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos); + break; + + case SPI_MODE2: + config |= (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos); + config |= (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos); + break; + + case SPI_MODE3: + config |= (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos); + config |= (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos); + break; + } + + return config; +} + +SPIClass::SPIClass(NRF_SPIM_Type *p_spi, uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI) +{ + initialized = false; + assert(p_spi != NULL); + + _p_spi = (NRF_SPI_Type*) p_spi; + +// not used by 832 +// _spim.p_reg = p_spi; +// _spim.drv_inst_idx = 0; + + // pins + _uc_pinMiso = g_ADigitalPinMap[uc_pinMISO]; + _uc_pinSCK = g_ADigitalPinMap[uc_pinSCK]; + _uc_pinMosi = g_ADigitalPinMap[uc_pinMOSI]; + + _dataMode = SPI_MODE0; + _bitOrder = SPI_CONFIG_ORDER_MsbFirst; +} + +void SPIClass::begin() +{ + if (initialized) return; + initialized = true; + + _p_spi->PSELSCK = _uc_pinSCK; + _p_spi->PSELMOSI = _uc_pinMosi; + _p_spi->PSELMISO = _uc_pinMiso; + + beginTransaction(DEFAULT_SPI_SETTINGS); +} + +void SPIClass::end() +{ + _p_spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos); + + initialized = false; +} + +void SPIClass::usingInterrupt(int /*interruptNumber*/) +{ +} + +void SPIClass::beginTransaction(SPISettings settings) +{ + _p_spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos); + + this->_bitOrder = (settings.bitOrder == MSBFIRST ? SPI_CONFIG_ORDER_MsbFirst : SPI_CONFIG_ORDER_LsbFirst); + this->_dataMode = settings.dataMode; + + _p_spi->CONFIG = this->_bitOrder | mode2config(this->_dataMode); + setClockDivider(F_CPU / settings.clockFreq); + + _p_spi->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos); +} + +void SPIClass::endTransaction(void) +{ + _p_spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos); +} + +void SPIClass::setBitOrder(BitOrder order) +{ + this->_bitOrder = (order == MSBFIRST ? SPI_CONFIG_ORDER_MsbFirst : SPI_CONFIG_ORDER_LsbFirst); + + _p_spi->CONFIG = this->_bitOrder | mode2config(this->_dataMode); +} + +void SPIClass::setDataMode(uint8_t mode) +{ + this->_dataMode = mode; + _p_spi->CONFIG = this->_bitOrder | mode2config(this->_dataMode); +} + +void SPIClass::setClockDivider(uint32_t div) +{ + uint32_t clockFreq; + + // Adafruit Note: nrf52 run at 64MHz + if (div >= SPI_CLOCK_DIV512) { + clockFreq = SPI_FREQUENCY_FREQUENCY_K125; + } else if (div >= SPI_CLOCK_DIV256) { + clockFreq = SPI_FREQUENCY_FREQUENCY_K250; + } else if (div >= SPI_CLOCK_DIV128) { + clockFreq = SPI_FREQUENCY_FREQUENCY_K500; + } else if (div >= SPI_CLOCK_DIV64) { + clockFreq = SPI_FREQUENCY_FREQUENCY_M1; + } else if (div >= SPI_CLOCK_DIV32) { + clockFreq = SPI_FREQUENCY_FREQUENCY_M2; + } else if (div >= SPI_CLOCK_DIV16) { + clockFreq = SPI_FREQUENCY_FREQUENCY_M4; + } else { + clockFreq = SPI_FREQUENCY_FREQUENCY_M8; + } + + _p_spi->FREQUENCY = clockFreq; +} + +byte SPIClass::transfer(uint8_t data) +{ + _p_spi->TXD = data; + + while(!_p_spi->EVENTS_READY); + + data = _p_spi->RXD; + + _p_spi->EVENTS_READY = 0x0UL; + + return data; +} + +uint16_t SPIClass::transfer16(uint16_t data) { + union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } t; + + t.val = data; + + if (_bitOrder == SPI_CONFIG_ORDER_LsbFirst) { + t.lsb = transfer(t.lsb); + t.msb = transfer(t.msb); + } else { + t.msb = transfer(t.msb); + t.lsb = transfer(t.lsb); + } + + return t.val; +} + +void SPIClass::transfer(const void *tx_buf, void *rx_buf, size_t count) +{ + uint8_t *tx_buf8 = (uint8_t *) tx_buf; + uint8_t *rx_buf8 = (uint8_t *) rx_buf; + + for (size_t i=0; i 0 extern "C" { - void SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQHandler(void) + void SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler(void) { Wire.onService(); } diff --git a/tools/build_all.py b/tools/build_all.py index 2ce47a0e..76689772 100644 --- a/tools/build_all.py +++ b/tools/build_all.py @@ -37,7 +37,7 @@ def build_examples(variant): for sketch in glob.iglob('libraries/**/*.ino', recursive=True): start_time = time.monotonic() - if os.path.exists(os.path.dirname(sketch) + '/.skip.' + variant): + if os.path.exists(os.path.dirname(sketch) + '/.skip') or os.path.exists(os.path.dirname(sketch) + '/.skip.' + variant): success = "skipped" else: build_result = subprocess.run("arduino --verify {}".format(sketch), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) diff --git a/variants/feather_nrf52832/variant.cpp b/variants/feather_nrf52832/variant.cpp index 88709381..4d48b971 100644 --- a/variants/feather_nrf52832/variant.cpp +++ b/variants/feather_nrf52832/variant.cpp @@ -67,7 +67,6 @@ const uint32_t g_ADigitalPinMap[] = { 29, // A5 30, // A6 31, // A7 - }; void initVariant() diff --git a/variants/feather_nrf52832/variant.h b/variants/feather_nrf52832/variant.h index 005bc3eb..25c9a6c5 100644 --- a/variants/feather_nrf52832/variant.h +++ b/variants/feather_nrf52832/variant.h @@ -100,7 +100,7 @@ static const uint8_t AREF = PIN_AREF; #define PIN_SPI_MOSI (13) #define PIN_SPI_SCK (12) -static const uint8_t SS = 18 ; +static const uint8_t SS = 27 ; static const uint8_t MOSI = PIN_SPI_MOSI ; static const uint8_t MISO = PIN_SPI_MISO ; static const uint8_t SCK = PIN_SPI_SCK ;