Merge pull request #360 from adafruit/spi-dma

use SPIM3 for nrf52840 with maximum speed at 32 Mhz
This commit is contained in:
hathach authored and GitHub committed 2019-10-18 10:42:35 +07:00
commit 64d8bcbc61
103 files changed
+7306 -5634

No files matched your search

@@ -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);
@@ -47,24 +47,23 @@ static int _internal_flash_read (const struct lfs_config *c, lfs_block_t block,
{
(void) c;
uint32_t addr = lba2addr(block) + off;
flash_nrf5x_read(buffer, addr, size);
uint32_t addr = lba2addr(block) + off;
VERIFY( flash_nrf5x_read(buffer, addr, size) > 0, -1);
return 0;
return 0;
}
// Program a region in a block. The block must have previously
// been erased. Negative error codes are propogated to the user.
// May return LFS_ERR_CORRUPT if the block should be considered bad.
static int _internal_flash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer,
lfs_size_t size)
static int _internal_flash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size)
{
(void) c;
uint32_t addr = lba2addr(block) + off;
flash_nrf5x_write(addr, buffer, size);
uint32_t addr = lba2addr(block) + off;
VERIFY( flash_nrf5x_write(addr, buffer, size), -1)
return 0;
return 0;
}
// Erase a block. A block must be erased before being programmed.
@@ -75,17 +74,17 @@ static int _internal_flash_erase (const struct lfs_config *c, lfs_block_t block)
{
(void) c;
uint32_t addr = lba2addr(block);
uint32_t addr = lba2addr(block);
// implement as write 0xff to whole block address
// implement as write 0xff to whole block address
for(int i=0; i <LFS_BLOCK_SIZE; i++)
{
flash_nrf5x_write8(addr + i, 0xFF);
}
// flash_nrf5x_flush();
// flash_nrf5x_flush();
return 0;
return 0;
}
// Sync the state of the underlying block device. Negative error codes
@@ -41,7 +41,7 @@ static inline uint32_t page_offset_of (uint32_t addr)
return addr & (FLASH_CACHE_SIZE - 1);
}
uint32_t flash_cache_write (flash_cache_t* fc, uint32_t dst, void const * src, uint32_t len)
int flash_cache_write (flash_cache_t* fc, uint32_t dst, void const * src, uint32_t len)
{
uint8_t const * src8 = (uint8_t const *) src;
uint32_t remain = len;
@@ -83,7 +83,7 @@ void flash_cache_flush (flash_cache_t* fc)
// skip erase & program if verify() exists, and memory matches
if ( !(fc->verify && fc->verify(fc->cache_addr, fc->cache_buf, FLASH_CACHE_SIZE)) )
{
// indicator
// indicator TODO allow to disable flash indicator
ledOn(LED_BUILTIN);
fc->erase(fc->cache_addr);
@@ -95,7 +95,7 @@ void flash_cache_flush (flash_cache_t* fc)
fc->cache_addr = FLASH_CACHE_INVALID_ADDR;
}
void flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t count)
int flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t count)
{
// there is no check for overflow / wraparound for dst + count, addr + count.
// this might be a useful thing to add for at least debug builds.
@@ -198,4 +198,6 @@ void flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t cou
// not using the cache, so just forward to read from flash
fc->read(dst, addr, count);
}
return (int) count;
}
@@ -46,9 +46,9 @@ typedef struct
extern "C" {
#endif
uint32_t flash_cache_write (flash_cache_t* fc, uint32_t dst, void const *src, uint32_t count);
int flash_cache_write (flash_cache_t* fc, uint32_t dst, void const *src, uint32_t count);
void flash_cache_flush (flash_cache_t* fc);
void flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t count);
int flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t count);
#ifdef __cplusplus
}
@@ -29,6 +29,17 @@
#include "delay.h"
#include "rtos.h"
#ifdef NRF52840_XXAA
#define BOOTLOADER_ADDR 0xF4000
#else
#define BOOTLOADER_ADDR 0x74000
#endif
// defined in linker script
extern uint32_t __flash_arduino_start[];
//extern uint32_t __flash_arduino_end[];
//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
//--------------------------------------------------------------------+
@@ -48,13 +59,15 @@ static bool fal_verify (uint32_t addr, void const * buf, uint32_t len);
static uint8_t _cache_buffer[FLASH_CACHE_SIZE] __attribute__((aligned(4)));
static flash_cache_t _cache = {
.erase = fal_erase,
.program = fal_program,
.read = fal_read,
.verify = fal_verify,
static flash_cache_t _cache =
{
.erase = fal_erase,
.program = fal_program,
.read = fal_read,
.verify = fal_verify,
.cache_addr = FLASH_CACHE_INVALID_ADDR,
.cache_buf = _cache_buffer
.cache_buf = _cache_buffer
};
//--------------------------------------------------------------------+
@@ -65,17 +78,20 @@ void flash_nrf5x_flush (void)
flash_cache_flush(&_cache);
}
uint32_t flash_nrf5x_write (uint32_t dst, void const * src, uint32_t len)
int flash_nrf5x_write (uint32_t dst, void const * src, uint32_t len)
{
// TODO prevent write SD + bootloader region
// Softdevice region
VERIFY(dst >= ((uint32_t) __flash_arduino_start), -1);
// Bootloader region
VERIFY(dst < BOOTLOADER_ADDR, -1);
return flash_cache_write(&_cache, dst, src, len);
}
uint32_t flash_nrf5x_read (void* dst, uint32_t src, uint32_t len)
int flash_nrf5x_read (void* dst, uint32_t src, uint32_t len)
{
// return cache value if available
flash_cache_read(&_cache, dst, src, len);
return len;
return flash_cache_read(&_cache, dst, src, len);
}
bool flash_nrf5x_erase(uint32_t addr)
@@ -86,7 +102,7 @@ bool flash_nrf5x_erase(uint32_t addr)
//--------------------------------------------------------------------+
// HAL for caching
//--------------------------------------------------------------------+
bool fal_erase (uint32_t addr)
static bool fal_erase (uint32_t addr)
{
// Init semaphore for first call
if ( _sem == NULL )
@@ -36,27 +36,24 @@
void flash_nrf5x_flush (void);
bool flash_nrf5x_erase(uint32_t addr);
uint32_t flash_nrf5x_write (uint32_t dst, void const * src, uint32_t len);
uint32_t flash_nrf5x_read (void* dst, uint32_t src, uint32_t len);
int flash_nrf5x_write (uint32_t dst, void const * src, uint32_t len);
int flash_nrf5x_read (void* dst, uint32_t src, uint32_t len);
//--------------------------------------------------------------------+
// Write helper
//--------------------------------------------------------------------+
static inline uint32_t flash_nrf5x_write8 (uint32_t dst, uint8_t num)
static inline int flash_nrf5x_write8 (uint32_t dst, uint8_t num)
{
flash_nrf5x_write(dst, &num, sizeof(num));
return sizeof(num);
return flash_nrf5x_write(dst, &num, sizeof(num));
}
static inline uint32_t flash_nrf5x_write16 (uint32_t dst, uint8_t num)
static inline int flash_nrf5x_write16 (uint32_t dst, uint8_t num)
{
flash_nrf5x_write(dst, &num, sizeof(num));
return sizeof(num);
return flash_nrf5x_write(dst, &num, sizeof(num));
}
static inline uint32_t flash_nrf5x_write32 (uint32_t dst, uint8_t num)
static inline int flash_nrf5x_write32 (uint32_t dst, uint8_t num)
{
flash_nrf5x_write(dst, &num, sizeof(num));
return sizeof(num);
return flash_nrf5x_write(dst, &num, sizeof(num));
}
//--------------------------------------------------------------------+
+128 -137
View File
@@ -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 <Arduino.h>
#include <wiring_private.h>
#include <assert.h>
#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
+49 -78
View File
@@ -23,6 +23,8 @@
#include <Arduino.h>
#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<uint8_t *>(buf);
for (size_t i=0; i<count; i++)
buffer[i] = transfer(buffer[i]);
}
#if SPI_INTERFACES_COUNT > 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
+222
View File
@@ -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 <Arduino.h>
#include <wiring_private.h>
#include <assert.h>
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<count; i++) {
uint8_t rxbyte = transfer(tx_buf8 ? tx_buf8[i] : 0xFF);
if (rx_buf8) rx_buf8[i] = rxbyte;
}
}
void SPIClass::transfer(void *buf, size_t count)
{
uint8_t *buffer = (uint8_t *) buf;
for (size_t i=0; i<count; i++) {
buffer[i] = transfer(buffer[i]);
}
}
void SPIClass::attachInterrupt() {
// Should be enableInterrupt()
}
void SPIClass::detachInterrupt() {
// Should be disableInterrupt()
}
// SPIM0, SPIM1 are configured as I2C
SPIClass SPI(NRF_SPIM2, PIN_SPI_MISO, PIN_SPI_SCK, PIN_SPI_MOSI);
#endif // NRF52832_XXAA
+2 -2
View File
@@ -392,12 +392,12 @@ void TwoWire::onService(void)
}
}
TwoWire Wire(NRF_TWIM1, NRF_TWIS1, SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQn, PIN_WIRE_SDA, PIN_WIRE_SCL);
TwoWire Wire(NRF_TWIM0, NRF_TWIS0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn, PIN_WIRE_SDA, PIN_WIRE_SCL);
#if WIRE_INTERFACES_COUNT > 0
extern "C"
{
void SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQHandler(void)
void SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler(void)
{
Wire.onService();
}