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"
// --------------------------------------------------------------------------
// Software SPI
// --------------------------------------------------------------------------
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);
}
inline void spiBegin() {
void spiBegin() {
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);
}
inline uint8_t spiRec() { return spiTransfer(0xFF); }
uint8_t spiRec() { return spiTransfer(0xFF); }
inline void spiRead(uint8_t * const buf, const uint16_t nbyte) {
if (nbyte == 0) return;
for (int i = 0; i < nbyte; i++)
buf[i] = spiTransfer(0xFF);
void spiRead(uint8_t*buf, uint16_t nbyte) {
if (nbyte)
for (int i = 0; i < nbyte; i++)
buf[i] = spiTransfer(0xFF);
}
inline void spiSend(const uint8_t b) {
uint8_t response = spiTransfer(b);
UNUSED(response);
void spiSend(uint8_t b) { (void)spiTransfer(b); }
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) {
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 spiSendBlock(uint8_t token, const uint8_t* buf) {
(void)spiTransfer(token);
for (uint16_t i = 0; i < 512; i++)
(void)spiTransfer(buf[i]);
@ -107,7 +102,7 @@
#else
// hardware SPI
// Hardware SPI
#include <lpc17xx_pinsel.h>
#include <lpc17xx_ssp.h>
@ -131,7 +126,7 @@
#define LPC_SSPn LPC_SSP1
#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
PinCfg.Funcnum = 2;
PinCfg.OpenDrain = 0;
@ -156,7 +151,7 @@
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
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
@ -174,49 +169,50 @@
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 */
SSP_SendData(LPC_SSPn, b & 0x00FF);
while (SSP_GetStatus(LPC_SSPn, SSP_STAT_BUSY)); // wait for it to finish
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) {
if (n == 0) return;
for (uint16_t i = 0; i < n; i++) doio(buf[i]);
void spiSend(const uint8_t* buf, size_t n) {
if (n)
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
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
inline void spiRead(uint8_t * const buf, const uint16_t nbyte) {
if (nbyte == 0) return;
for (int i = 0; i < nbyte; i++)
buf[i] = doio(0xFF);
void spiRead(uint8_t*buf, uint16_t nbyte) {
if (nbyte)
for (int i = 0; i < nbyte; i++) 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
inline void spiSendBlock(const uint8_t token, const uint8_t * const buf) {
(void)spiTransfer(token);
void spiSendBlock(uint8_t token, const uint8_t* buf) {
(void)spiTransfer(token);
for (uint16_t i = 0; i < 512; i++)
(void)spiTransfer(buf[i]);
}
/** 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
}
@ -225,7 +221,7 @@
void SPIClass::begin() { spiBegin(); }
void SPIClass::beginTransaction(const SPISettings cfg) {
void SPIClass::beginTransaction(SPISettings cfg) {
uint8_t spiRate;
switch (cfg.spiRate()) {
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); }
uint16_t SPIClass::transfer16(const uint16_t data) {
return (transfer((data >> 8) & 0xFF) << 8);
return (transfer((data >> 8) & 0xFF) << 8)
| (transfer(data & 0xFF) & 0xFF);
}