This commit is contained in:
Scott Lahteine 2019-02-04 08:27:47 -06:00
parent b35a7c2c87
commit 9e394139e5

View File

@ -62,44 +62,39 @@
#include "SoftwareSPI.h" #include "SoftwareSPI.h"
// --------------------------------------------------------------------------
// Software SPI // Software SPI
// --------------------------------------------------------------------------
static uint8_t SPI_speed = 0; static uint8_t SPI_speed = 0;
static inline uint8_t spiTransfer(const uint8_t b) { static uint8_t spiTransfer(uint8_t b) {
return swSpiTransfer(b, SPI_speed, SCK_PIN, MISO_PIN, MOSI_PIN); return swSpiTransfer(b, SPI_speed, SCK_PIN, MISO_PIN, MOSI_PIN);
} }
inline void spiBegin() { void spiBegin() {
swSpiBegin(SCK_PIN, MISO_PIN, MOSI_PIN); swSpiBegin(SCK_PIN, MISO_PIN, MOSI_PIN);
} }
inline void spiInit(const uint8_t spiRate) { void spiInit(uint8_t spiRate) {
SPI_speed = swSpiInit(spiRate, SCK_PIN, MOSI_PIN); SPI_speed = swSpiInit(spiRate, SCK_PIN, MOSI_PIN);
} }
inline uint8_t spiRec() { return spiTransfer(0xFF); } uint8_t spiRec() { return spiTransfer(0xFF); }
inline void spiRead(uint8_t * const buf, const uint16_t nbyte) { void spiRead(uint8_t*buf, uint16_t nbyte) {
if (nbyte == 0) return; if (nbyte)
for (int i = 0; i < nbyte; i++) for (int i = 0; i < nbyte; i++)
buf[i] = spiTransfer(0xFF); buf[i] = spiTransfer(0xFF);
} }
inline void spiSend(const uint8_t b) { void spiSend(uint8_t b) { (void)spiTransfer(b); }
uint8_t response = spiTransfer(b);
UNUSED(response); void spiSend(const uint8_t* buf, size_t n) {
if (n)
for (uint16_t i = 0; i < n; i++)
(void)spiTransfer(buf[i]);
} }
inline void spiSend(const uint8_t* buf, const size_t n) { void spiSendBlock(uint8_t token, const uint8_t* buf) {
if (n == 0) return;
for (uint16_t i = 0; i < n; i++)
(void)spiTransfer(buf[i]);
}
inline void spiSendBlock(const uint8_t token, const uint8_t * const buf) {
(void)spiTransfer(token); (void)spiTransfer(token);
for (uint16_t i = 0; i < 512; i++) for (uint16_t i = 0; i < 512; i++)
(void)spiTransfer(buf[i]); (void)spiTransfer(buf[i]);
@ -107,7 +102,7 @@
#else #else
// hardware SPI // Hardware SPI
#include <lpc17xx_pinsel.h> #include <lpc17xx_pinsel.h>
#include <lpc17xx_ssp.h> #include <lpc17xx_ssp.h>
@ -131,7 +126,7 @@
#define LPC_SSPn LPC_SSP1 #define LPC_SSPn LPC_SSP1
#endif #endif
inline void spiBegin() { // setup SCK, MOSI & MISO pins for SSP0 void spiBegin() { // setup SCK, MOSI & MISO pins for SSP0
PINSEL_CFG_Type PinCfg; // data structure to hold init values PINSEL_CFG_Type PinCfg; // data structure to hold init values
PinCfg.Funcnum = 2; PinCfg.Funcnum = 2;
PinCfg.OpenDrain = 0; PinCfg.OpenDrain = 0;
@ -156,7 +151,7 @@
SSP_Cmd(LPC_SSPn, ENABLE); // start SSP running SSP_Cmd(LPC_SSPn, ENABLE); // start SSP running
} }
inline void spiInit(const uint8_t spiRate) { void spiInit(uint8_t spiRate) {
// table to convert Marlin spiRates (0-5 plus default) into bit rates // table to convert Marlin spiRates (0-5 plus default) into bit rates
uint32_t Marlin_speed[7]; // CPSR is always 2 uint32_t Marlin_speed[7]; // CPSR is always 2
Marlin_speed[0] = 8333333; //(SCR: 2) desired: 8,000,000 actual: 8,333,333 +4.2% SPI_FULL_SPEED Marlin_speed[0] = 8333333; //(SCR: 2) desired: 8,000,000 actual: 8,333,333 +4.2% SPI_FULL_SPEED
@ -174,49 +169,50 @@
SSP_Init(LPC_SSPn, &HW_SPI_init); // puts the values into the proper bits in the SSP0 registers SSP_Init(LPC_SSPn, &HW_SPI_init); // puts the values into the proper bits in the SSP0 registers
} }
static inline uint8_t doio(const uint8_t b) { static uint8_t doio(uint8_t b) {
/* send and receive a single byte */ /* send and receive a single byte */
SSP_SendData(LPC_SSPn, b & 0x00FF); SSP_SendData(LPC_SSPn, b & 0x00FF);
while (SSP_GetStatus(LPC_SSPn, SSP_STAT_BUSY)); // wait for it to finish while (SSP_GetStatus(LPC_SSPn, SSP_STAT_BUSY)); // wait for it to finish
return SSP_ReceiveData(LPC_SSPn) & 0x00FF; return SSP_ReceiveData(LPC_SSPn) & 0x00FF;
} }
inline void spiSend(const uint8_t b) { doio(b); } void spiSend(uint8_t b) { doio(b); }
inline void spiSend(const uint8_t* buf, const size_t n) { void spiSend(const uint8_t* buf, size_t n) {
if (n == 0) return; if (n)
for (uint16_t i = 0; i < n; i++) doio(buf[i]); for (uint16_t i = 0; i < n; i++) doio(buf[i]);
} }
inline void spiSend(const uint32_t chan, const byte b) { void spiSend(uint32_t chan, byte b) {
} }
inline void spiSend(const uint32_t chan, const uint8_t * const buf, const size_t n) { void spiSend(uint32_t chan, const uint8_t* buf, size_t n) {
} }
// Read single byte from SPI // Read single byte from SPI
inline uint8_t spiRec() { return doio(0xFF); } uint8_t spiRec() { return doio(0xFF); }
inline uint8_t spiRec(const uint32_t chan) { return 0; } uint8_t spiRec(uint32_t chan) { return 0; }
// Read from SPI into buffer // Read from SPI into buffer
inline void spiRead(uint8_t * const buf, const uint16_t nbyte) { void spiRead(uint8_t*buf, uint16_t nbyte) {
if (nbyte == 0) return; if (nbyte)
for (int i = 0; i < nbyte; i++) for (int i = 0; i < nbyte; i++) buf[i] = doio(0xff);
buf[i] = doio(0xFF);
} }
static uint8_t spiTransfer(const uint8_t b) { return doio(b); } static uint8_t spiTransfer(uint8_t b) {
return doio(b);
}
// Write from buffer to SPI // Write from buffer to SPI
inline void spiSendBlock(const uint8_t token, const uint8_t * const buf) { void spiSendBlock(uint8_t token, const uint8_t* buf) {
(void)spiTransfer(token); (void)spiTransfer(token);
for (uint16_t i = 0; i < 512; i++) for (uint16_t i = 0; i < 512; i++)
(void)spiTransfer(buf[i]); (void)spiTransfer(buf[i]);
} }
/** Begin SPI transaction, set clock, bit order, data mode */ /** Begin SPI transaction, set clock, bit order, data mode */
inline void spiBeginTransaction(const uint32_t spiClock, const uint8_t bitOrder, const uint8_t dataMode) { void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
// TODO: to be implemented // TODO: to be implemented
} }
@ -225,7 +221,7 @@
void SPIClass::begin() { spiBegin(); } void SPIClass::begin() { spiBegin(); }
void SPIClass::beginTransaction(const SPISettings cfg) { void SPIClass::beginTransaction(SPISettings cfg) {
uint8_t spiRate; uint8_t spiRate;
switch (cfg.spiRate()) { switch (cfg.spiRate()) {
case 8000000: spiRate = 0 ;break; case 8000000: spiRate = 0 ;break;
@ -243,7 +239,7 @@ void SPIClass::beginTransaction(const SPISettings cfg) {
uint8_t SPIClass::transfer(const uint8_t B) { return spiTransfer(B); } uint8_t SPIClass::transfer(const uint8_t B) { return spiTransfer(B); }
uint16_t SPIClass::transfer16(const uint16_t data) { uint16_t SPIClass::transfer16(const uint16_t data) {
return (transfer((data >> 8) & 0xFF) << 8); return (transfer((data >> 8) & 0xFF) << 8)
| (transfer(data & 0xFF) & 0xFF); | (transfer(data & 0xFF) & 0xFF);
} }