Skip to content

Commit

Permalink
Reverted to fastgpio for the SPI functions
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulZC committed Apr 10, 2020
1 parent 68b9bfb commit 8338011
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 79 deletions.
12 changes: 6 additions & 6 deletions fastspi.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ template<uint8_t _DATA_PIN, uint8_t _CLOCK_PIN, uint32_t _SPI_CLOCK_DIVIDER>
class SPIOutput : public NRF52SPIOutput<_DATA_PIN, _CLOCK_PIN, _SPI_CLOCK_DIVIDER> {};
#endif

#if defined(SPI_DATA) && defined(SPI_CLOCK)

#if defined(FASTLED_APOLLO3)
#if defined(FASTLED_APOLLO3) && defined(FASTLED_ALL_PINS_HARDWARE_SPI)
template<uint8_t _DATA_PIN, uint8_t _CLOCK_PIN, uint32_t _SPI_CLOCK_DIVIDER>
class SPIOutput : public APOLLO3HardwareSPIOutput<_DATA_PIN, _CLOCK_PIN, _SPI_CLOCK_DIVIDER> {};
#endif

template<uint32_t SPI_SPEED>
class SPIOutput<SPI_DATA, SPI_CLOCK, SPI_SPEED> : public APOLLO3HardwareSPIOutput<SPI_DATA, SPI_CLOCK, SPI_SPEED> {};
#if defined(SPI_DATA) && defined(SPI_CLOCK)

#elif defined(FASTLED_TEENSY3) && defined(ARM_HARDWARE_SPI)
#if defined(FASTLED_TEENSY3) && defined(ARM_HARDWARE_SPI)

template<uint32_t SPI_SPEED>
class SPIOutput<SPI_DATA, SPI_CLOCK, SPI_SPEED> : public ARMHardwareSPIOutput<SPI_DATA, SPI_CLOCK, SPI_SPEED, 0x4002C000> {};
Expand Down
2 changes: 1 addition & 1 deletion platforms/apollo3/clockless_apollo3.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ClocklessController : public CPixelLEDController<RGB_ORDER> {
SysTick_Config(0xFFFFFFUL); // The LOAD value needs to be 24-bit
}

virtual uint16_t getMaxRefreshRate() const { return 400; } // This can probably be increased?
virtual uint16_t getMaxRefreshRate() const { return 400; }

protected:

Expand Down
42 changes: 1 addition & 41 deletions platforms/apollo3/fastpin_apollo3.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ _FL_DEFPIN(20); _FL_DEFPIN(21); _FL_DEFPIN(22); _FL_DEFPIN(23); _FL_DEFPIN(24);
_FL_DEFPIN(25); _FL_DEFPIN(26); _FL_DEFPIN(27); _FL_DEFPIN(28); _FL_DEFPIN(29);
_FL_DEFPIN(30); _FL_DEFPIN(31);

//The Artemis RedBoard has 4 SPI ports defined by default
//TO DO: implement multiple SPI functionality
//#define AP3_FASTLED_SPI_IOM 0
#define SPI_DATA MOSI
#define SPI_CLOCK SCK

#define HAS_HARDWARE_PIN_SUPPORT 1

#elif defined(ARDUINO_AM_AP3_SFE_BB_ARTEMIS_NANO)
Expand All @@ -100,10 +94,6 @@ _FL_DEFPIN(10); _FL_DEFPIN(11); _FL_DEFPIN(12); _FL_DEFPIN(13); _FL_DEFPIN(14);
_FL_DEFPIN(15); _FL_DEFPIN(16); _FL_DEFPIN(17); _FL_DEFPIN(18); _FL_DEFPIN(19);
_FL_DEFPIN(20); _FL_DEFPIN(21); _FL_DEFPIN(22); _FL_DEFPIN(23);

//#define AP3_FASTLED_SPI_IOM 0
#define SPI_DATA MOSI
#define SPI_CLOCK SCK

#define HAS_HARDWARE_PIN_SUPPORT 1

#elif defined(ARDUINO_AM_AP3_SFE_THING_PLUS)
Expand All @@ -116,37 +106,11 @@ _FL_DEFPIN(15); _FL_DEFPIN(16); _FL_DEFPIN(17); _FL_DEFPIN(18); _FL_DEFPIN(19);
_FL_DEFPIN(20); _FL_DEFPIN(21); _FL_DEFPIN(22); _FL_DEFPIN(23); _FL_DEFPIN(24);
_FL_DEFPIN(25); _FL_DEFPIN(26); _FL_DEFPIN(27); _FL_DEFPIN(28);

//#define AP3_FASTLED_SPI_IOM 0
#define SPI_DATA MOSI
#define SPI_CLOCK SCK

#define HAS_HARDWARE_PIN_SUPPORT 1

#elif defined(ARDUINO_AM_AP3_SFE_BB_ARTEMIS_ATP)

#define MAX_PIN 50 // AP3_VARIANT_NUM_PINS
_FL_DEFPIN(0); _FL_DEFPIN(1); _FL_DEFPIN(2); _FL_DEFPIN(3); _FL_DEFPIN(4);
_FL_DEFPIN(5); _FL_DEFPIN(6); _FL_DEFPIN(7); _FL_DEFPIN(8); _FL_DEFPIN(9);
_FL_DEFPIN(10); _FL_DEFPIN(11); _FL_DEFPIN(12); _FL_DEFPIN(13); _FL_DEFPIN(14);
_FL_DEFPIN(15); _FL_DEFPIN(16); _FL_DEFPIN(17); _FL_DEFPIN(18); _FL_DEFPIN(19);
_FL_DEFPIN(20); _FL_DEFPIN(21); _FL_DEFPIN(22); _FL_DEFPIN(23); _FL_DEFPIN(24);
_FL_DEFPIN(25); _FL_DEFPIN(26); _FL_DEFPIN(27); _FL_DEFPIN(28); _FL_DEFPIN(29);
_FL_DEFPIN(31); _FL_DEFPIN(32); _FL_DEFPIN(33); _FL_DEFPIN(34);
_FL_DEFPIN(35); _FL_DEFPIN(36); _FL_DEFPIN(37); _FL_DEFPIN(38); _FL_DEFPIN(39);
_FL_DEFPIN(40); _FL_DEFPIN(41); _FL_DEFPIN(42); _FL_DEFPIN(43); _FL_DEFPIN(44);
_FL_DEFPIN(45); _FL_DEFPIN(47); _FL_DEFPIN(48); _FL_DEFPIN(49);

//#define AP3_FASTLED_SPI_IOM 0
#define SPI_DATA MOSI
#define SPI_CLOCK SCK

#define HAS_HARDWARE_PIN_SUPPORT 1

#elif defined(ARDUINO_SFE_ARTEMIS)
#elif defined(ARDUINO_AM_AP3_SFE_BB_ARTEMIS_ATP) || defined(ARDUINO_SFE_ARTEMIS)

#define MAX_PIN 50 // AP3_VARIANT_NUM_PINS
// Pin definitions taken from (e.g.) C:\Users\...\AppData\Local\Arduino15\packages\SparkFun\hardware\apollo3\1.0.30\variants\redboard_artemis_atp\config\variant.cpp
// ap3_variant_pinmap maps pins to pads. FastLED expects pin numbers so we'll use those.
_FL_DEFPIN(0); _FL_DEFPIN(1); _FL_DEFPIN(2); _FL_DEFPIN(3); _FL_DEFPIN(4);
_FL_DEFPIN(5); _FL_DEFPIN(6); _FL_DEFPIN(7); _FL_DEFPIN(8); _FL_DEFPIN(9);
_FL_DEFPIN(10); _FL_DEFPIN(11); _FL_DEFPIN(12); _FL_DEFPIN(13); _FL_DEFPIN(14);
Expand All @@ -158,10 +122,6 @@ _FL_DEFPIN(35); _FL_DEFPIN(36); _FL_DEFPIN(37); _FL_DEFPIN(38); _FL_DEFPIN(39);
_FL_DEFPIN(40); _FL_DEFPIN(41); _FL_DEFPIN(42); _FL_DEFPIN(43); _FL_DEFPIN(44);
_FL_DEFPIN(45); _FL_DEFPIN(47); _FL_DEFPIN(48); _FL_DEFPIN(49);

//The Artemis module has all six SPI ports defined by default _but_
//does not assign pins for them.
//TO DO: implement multiple SPI functionality with correct pin mapping

#define HAS_HARDWARE_PIN_SUPPORT 1

#else
Expand Down
54 changes: 23 additions & 31 deletions platforms/apollo3/fastspi_apollo3.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#ifndef __INC_FASTSPI_APOLLO3_H
#define __INC_FASTSPI_APOLLO3_H

// This is the implementation of fastspi for the Apollo3.
// It uses fastgpio instead of actual SPI, which means you can use it on all pins.
// It can run slightly faster than the default fastpin (bit banging).

#include "FastLED.h"

FASTLED_NAMESPACE_BEGIN

#if defined(FASTLED_APOLLO3)

#include <SPI.h>
#define FASTLED_ALL_PINS_HARDWARE_SPI

template <uint8_t _DATA_PIN, uint8_t _CLOCK_PIN, uint32_t SPI_CLOCK_SPEED>
template <uint8_t _DATA_PIN, uint8_t _CLOCK_PIN, uint32_t _SPI_CLOCK_DIVIDER>
class APOLLO3HardwareSPIOutput {
Selectable *m_pSelect;

Expand All @@ -20,37 +24,33 @@ class APOLLO3HardwareSPIOutput {
// set the object representing the selectable
void setSelect(Selectable *pSelect) { m_pSelect = pSelect; }

// initialize the SPI subssytem
// initialize the pins for fastgpio
void init() {
//enableBurstMode(); //Optional. Go to 96MHz. Roughly doubles the speed of shiftOut and fastShiftOut
pinMode(_DATA_PIN, OUTPUT);
pinMode(_CLOCK_PIN, OUTPUT);
am_hal_gpio_fastgpio_enable(_DATA_PIN);
am_hal_gpio_fastgpio_enable(_CLOCK_PIN);
SPI.begin();
}

// latch the CS select
void inline select() __attribute__((always_inline)) {
// Begin the SPI transaction
// We want CPOL/CKP to be 0 and CPHA to be 0 so we need SPI Mode 0
SPI.beginTransaction(SPISettings((F_CPU/SPI_CLOCK_SPEED), MSBFIRST, AM_HAL_IOM_SPI_MODE_0));
if(m_pSelect != NULL) { m_pSelect->select(); }
}
void inline select() { /* TODO */ }

// release the CS select
void inline release() {
if(m_pSelect != NULL) { m_pSelect->release(); }
SPI.endTransaction();
}
void inline release() { /* TODO */ }

// wait until all queued up data has been written
static void waitFully() { /* TODO */ }

// write a byte out via SPI (returns immediately on writing register)
// write a byte as bits
static void writeByte(uint8_t b) {
//fastShiftOut(_DATA_PIN, _CLOCK_PIN, MSBFIRST, b);
SPI.transferOut(&b,1);
writeBit<7>(b);
writeBit<6>(b);
writeBit<5>(b);
writeBit<4>(b);
writeBit<3>(b);
writeBit<2>(b);
writeBit<1>(b);
writeBit<0>(b);
}

// write a word out via SPI (returns immediately on writing register)
Expand Down Expand Up @@ -91,18 +91,15 @@ class APOLLO3HardwareSPIOutput {
template <uint8_t BIT> inline static void writeBit(uint8_t b) {
//waitFully();
if(b & (1 << BIT)) {
//digitalWrite(_DATA_PIN, HIGH); //FastPin<_DATA_PIN>::hi();
am_hal_gpio_fastgpio_set(_DATA_PIN);
} else {
//digitalWrite(_DATA_PIN, LOW); //FastPin<_DATA_PIN>::lo();
am_hal_gpio_fastgpio_clr(_DATA_PIN);
}

//digitalWrite(_CLOCK_PIN, HIGH);
//digitalWrite(_CLOCK_PIN, LOW);
am_hal_gpio_fastgpio_set(_CLOCK_PIN);
__NOP();
for (int16_t d = (_SPI_CLOCK_DIVIDER >> 1); d > 0; d--) { __NOP(); }
am_hal_gpio_fastgpio_clr(_CLOCK_PIN);
for (int16_t d = ((_SPI_CLOCK_DIVIDER >> 1) - 1); d > 0; d--) { __NOP(); }
}

// write a block of uint8_ts out in groups of three. len is the total number of uint8_ts to write out. The template
Expand All @@ -112,18 +109,13 @@ class APOLLO3HardwareSPIOutput {

int len = pixels.mLen;

//select();
while(pixels.has(1)) {
if(FLAGS & FLAG_START_BIT) {
writeBit<0>(1);
writeByte(D::adjust(pixels.loadAndScale0()));
writeByte(D::adjust(pixels.loadAndScale1()));
writeByte(D::adjust(pixels.loadAndScale2()));
} else {
writeByte(D::adjust(pixels.loadAndScale0()));
writeByte(D::adjust(pixels.loadAndScale1()));
writeByte(D::adjust(pixels.loadAndScale2()));
}
writeByte(D::adjust(pixels.loadAndScale0()));
writeByte(D::adjust(pixels.loadAndScale1()));
writeByte(D::adjust(pixels.loadAndScale2()));

pixels.advanceData();
pixels.stepDithering();
Expand Down

0 comments on commit 8338011

Please sign in to comment.