From 3adc899f81bf72eedce23693cfc9a22d52afdb43 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 16 Oct 2019 00:53:26 +0700 Subject: [PATCH] spi dma work but it actually draw image slower than before --- cores/nRF5/nordic/nrfx_config.h | 19 ++++++-- libraries/SPI/SPI.cpp | 77 +++++++++++++++++++++++++++------ libraries/SPI/SPI.h | 65 ++++++++++++---------------- 3 files changed, 106 insertions(+), 55 deletions(-) diff --git a/cores/nRF5/nordic/nrfx_config.h b/cores/nRF5/nordic/nrfx_config.h index 4405f442..4d44420b 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 1 +#define NRFX_SPIM1_ENABLED 1 +#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/SPI/SPI.cpp b/libraries/SPI/SPI.cpp index e0c4a2a5..907c1c4e 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 @@ -35,6 +36,9 @@ SPIClass::SPIClass(NRF_SPI_Type *p_spi, uint8_t uc_pinMISO, uint8_t uc_pinSCK, u assert(p_spi != NULL); _p_spi = p_spi; + _spim.p_reg = NRF_SPIM0; + _spim.drv_inst_idx = NRFX_SPIM0_INST_IDX; + // pins _uc_pinMiso = g_ADigitalPinMap[uc_pinMISO]; _uc_pinSCK = g_ADigitalPinMap[uc_pinSCK]; @@ -46,7 +50,25 @@ SPIClass::SPIClass(NRF_SPI_Type *p_spi, uint8_t uc_pinMISO, uint8_t uc_pinSCK, u void SPIClass::begin() { - init(); + if (initialized) return; + initialized = true; + + 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, + .frequency = NRF_SPIM_FREQ_8M, // NRF_SPIM_FREQ_4M, + .mode = NRF_SPIM_MODE_0, + .bit_order = NRF_SPIM_BIT_ORDER_MSB_FIRST, + }; + + // blocking + nrfx_spim_init(&_spim, &cfg, NULL, NULL); _p_spi->PSELSCK = _uc_pinSCK; _p_spi->PSELMOSI = _uc_pinMosi; @@ -55,18 +77,9 @@ void SPIClass::begin() config(DEFAULT_SPI_SETTINGS); } -void SPIClass::init() -{ - if (initialized) - return; - interruptMode = SPI_IMODE_NONE; - interruptSave = 0; - interruptMask = 0; - initialized = true; -} - void SPIClass::config(SPISettings settings) { +#if 0 _p_spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos); uint32_t config = settings.bitOrder; @@ -98,11 +111,14 @@ void SPIClass::config(SPISettings settings) _p_spi->FREQUENCY = settings.clockFreq; _p_spi->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos); +#endif } void SPIClass::end() { - _p_spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos); +// _p_spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos); + + nrfx_spim_uninit(&_spim); initialized = false; } @@ -113,16 +129,17 @@ void SPIClass::usingInterrupt(int /*interruptNumber*/) void SPIClass::beginTransaction(SPISettings settings) { - config(settings); +// config(settings); } void SPIClass::endTransaction(void) { - _p_spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos); +// _p_spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos); } void SPIClass::setBitOrder(BitOrder order) { +#if 0 this->_bitOrder = (order == MSBFIRST ? SPI_CONFIG_ORDER_MsbFirst : SPI_CONFIG_ORDER_LsbFirst); uint32_t config = this->_bitOrder; @@ -151,10 +168,12 @@ void SPIClass::setBitOrder(BitOrder order) } _p_spi->CONFIG = config; +#endif } void SPIClass::setDataMode(uint8_t mode) { +#if 0 this->_dataMode = mode; uint32_t config = this->_bitOrder; @@ -183,10 +202,12 @@ void SPIClass::setDataMode(uint8_t mode) } _p_spi->CONFIG = config; +#endif } void SPIClass::setClockDivider(uint8_t div) { +#if 0 uint32_t clockFreq; // Adafruit Note: nrf52 run at 64MHz @@ -207,10 +228,32 @@ void SPIClass::setClockDivider(uint8_t div) } _p_spi->FREQUENCY = clockFreq; +#endif +} + +void SPIClass::transfer(void *buf, size_t count) +{ +#if 0 + // TODO: Optimize for faster block-transfer + uint8_t *buffer = reinterpret_cast(buf); + for (size_t i=0; iTXD = data; while(!_p_spi->EVENTS_READY); @@ -220,9 +263,14 @@ byte SPIClass::transfer(uint8_t data) _p_spi->EVENTS_READY = 0x0UL; return data; +#else + transfer(&data, 1); + return data; +#endif } uint16_t SPIClass::transfer16(uint16_t data) { + union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } t; t.val = data; @@ -249,6 +297,7 @@ void SPIClass::detachInterrupt() { #if SPI_INTERFACES_COUNT > 0 SPIClass SPI (NRF_SPI0, 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); #endif diff --git a/libraries/SPI/SPI.h b/libraries/SPI/SPI.h index 778575bc..5af5d82a 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() @@ -84,55 +86,44 @@ class SPISettings { class SPIClass { public: - SPIClass(NRF_SPI_Type *p_spi, uint8_t uc_pinMISO, uint8_t uc_pinSCK, uint8_t uc_pinMOSI); + SPIClass(NRF_SPI_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); - 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(uint8_t uc_div); private: - void init(); - void config(SPISettings settings); + void config(SPISettings settings); - NRF_SPI_Type *_p_spi; - uint8_t _uc_pinMiso; - uint8_t _uc_pinMosi; - uint8_t _uc_pinSCK; + NRF_SPI_Type *_p_spi; + nrfx_spim_t _spim; - uint8_t _dataMode; - uint32_t _bitOrder; + uint8_t _uc_pinMiso; + uint8_t _uc_pinMosi; + uint8_t _uc_pinSCK; - bool initialized; - uint8_t interruptMode; - char interruptSave; - uint32_t interruptMask; + uint8_t _dataMode; + uint32_t _bitOrder; + + 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