Initial attempt at SPI setModule - but it doesn't seem to work for SPI2 etc, even when not using setModule to select the SPI channel, when I tested with a VS1053 board attached to SPI2 and SD on SPI1
This commit is contained in:
parent
cd9e17deea
commit
2e13a1d57e
|
@ -91,30 +91,29 @@ static const spi_pins board_spi_pins[] __FLASH__ = {
|
|||
*/
|
||||
|
||||
SPIClass::SPIClass(uint32 spi_num) {
|
||||
|
||||
_currentSetting=&_settings[spi_num];
|
||||
switch (spi_num) {
|
||||
#if BOARD_NR_SPI >= 1
|
||||
case 1:
|
||||
this->spi_d = SPI1;
|
||||
_currentSetting->spi_d = SPI1;
|
||||
break;
|
||||
#endif
|
||||
#if BOARD_NR_SPI >= 2
|
||||
case 2:
|
||||
this->spi_d = SPI2;
|
||||
_currentSetting->spi_d = SPI2;
|
||||
break;
|
||||
#endif
|
||||
#if BOARD_NR_SPI >= 3
|
||||
case 3:
|
||||
this->spi_d = SPI3;
|
||||
_currentSetting->spi_d = SPI3;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
ASSERT(0);
|
||||
}
|
||||
|
||||
bitOrder=MSBFIRST;
|
||||
//pinMode(BOARD_SPI_DEFAULT_SS,OUTPUT);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -123,45 +122,45 @@ SPIClass::SPIClass(uint32 spi_num) {
|
|||
|
||||
void SPIClass::begin(void) {
|
||||
|
||||
uint32 flags = ((bitOrder == MSBFIRST ? SPI_FRAME_MSB : SPI_FRAME_LSB) | SPI_DFF_8_BIT | SPI_SW_SLAVE | SPI_SOFT_SS);
|
||||
spi_init(spi_d);
|
||||
configure_gpios(spi_d, 1);
|
||||
uint32 flags = ((_currentSetting->bitOrder == MSBFIRST ? SPI_FRAME_MSB : SPI_FRAME_LSB) | SPI_DFF_8_BIT | SPI_SW_SLAVE | SPI_SOFT_SS);
|
||||
spi_init(_currentSetting->spi_d);
|
||||
configure_gpios(_currentSetting->spi_d, 1);
|
||||
#ifdef SPI_DEBUG
|
||||
Serial.print("spi_master_enable("); Serial.print(clockDivider); Serial.print(","); Serial.print(dataMode); Serial.print(","); Serial.print(flags); Serial.println(")");
|
||||
Serial.print("spi_master_enable("); Serial.print(_currentSetting->clock); Serial.print(","); Serial.print(_currentSetting->dataMode); Serial.print(","); Serial.print(flags); Serial.println(")");
|
||||
#endif
|
||||
spi_master_enable(spi_d, (spi_baud_rate)clockDivider, (spi_mode)dataMode, flags);
|
||||
spi_master_enable(_currentSetting->spi_d, (spi_baud_rate)_currentSetting->clock, (spi_mode)_currentSetting->dataMode, flags);
|
||||
}
|
||||
|
||||
void SPIClass::beginSlave(void) {
|
||||
if (dataMode >= 4) {
|
||||
if (_currentSetting->dataMode >= 4) {
|
||||
ASSERT(0);
|
||||
return;
|
||||
}
|
||||
uint32 flags = ((bitOrder == MSBFIRST ? SPI_FRAME_MSB : SPI_FRAME_LSB) | SPI_DFF_8_BIT | SPI_SW_SLAVE);
|
||||
spi_init(spi_d);
|
||||
configure_gpios(spi_d, 0);
|
||||
uint32 flags = ((_currentSetting->bitOrder == MSBFIRST ? SPI_FRAME_MSB : SPI_FRAME_LSB) | SPI_DFF_8_BIT | SPI_SW_SLAVE);
|
||||
spi_init(_currentSetting->spi_d);
|
||||
configure_gpios(_currentSetting->spi_d, 0);
|
||||
#ifdef SPI_DEBUG
|
||||
Serial.print("spi_slave_enable("); Serial.print(dataMode); Serial.print(","); Serial.print(flags); Serial.println(")");
|
||||
Serial.print("spi_slave_enable("); Serial.print(_currentSetting->dataMode); Serial.print(","); Serial.print(flags); Serial.println(")");
|
||||
#endif
|
||||
spi_slave_enable(spi_d, (spi_mode)dataMode, flags);
|
||||
spi_slave_enable(_currentSetting->spi_d, (spi_mode)_currentSetting->dataMode, flags);
|
||||
}
|
||||
|
||||
void SPIClass::end(void) {
|
||||
if (!spi_is_enabled(this->spi_d)) {
|
||||
if (!spi_is_enabled(_currentSetting->spi_d)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Follows RM0008's sequence for disabling a SPI in master/slave
|
||||
// full duplex mode.
|
||||
while (spi_is_rx_nonempty(this->spi_d)) {
|
||||
while (spi_is_rx_nonempty(_currentSetting->spi_d)) {
|
||||
// FIXME [0.1.0] remove this once you have an interrupt based driver
|
||||
volatile uint16 rx __attribute__((unused)) = spi_rx_reg(this->spi_d);
|
||||
volatile uint16 rx __attribute__((unused)) = spi_rx_reg(_currentSetting->spi_d);
|
||||
}
|
||||
while (!spi_is_tx_empty(this->spi_d))
|
||||
while (!spi_is_tx_empty(_currentSetting->spi_d))
|
||||
;
|
||||
while (spi_is_busy(this->spi_d))
|
||||
while (spi_is_busy(_currentSetting->spi_d))
|
||||
;
|
||||
spi_peripheral_disable(this->spi_d);
|
||||
spi_peripheral_disable(_currentSetting->spi_d);
|
||||
}
|
||||
|
||||
/* Roger Clark added 3 functions */
|
||||
|
@ -170,7 +169,7 @@ void SPIClass::setClockDivider(uint32_t clockDivider)
|
|||
#ifdef SPI_DEBUG
|
||||
Serial.print("Clock divider set to "); Serial.println(clockDivider);
|
||||
#endif
|
||||
this->clockDivider = clockDivider;
|
||||
_currentSetting->clock = clockDivider;
|
||||
this->begin();
|
||||
}
|
||||
|
||||
|
@ -179,7 +178,7 @@ void SPIClass::setBitOrder(BitOrder bitOrder)
|
|||
#ifdef SPI_DEBUG
|
||||
Serial.print("Bit order set to "); Serial.println(bitOrder);
|
||||
#endif
|
||||
this->bitOrder = bitOrder;
|
||||
_currentSetting->bitOrder = bitOrder;
|
||||
this->begin();
|
||||
}
|
||||
|
||||
|
@ -189,11 +188,11 @@ void SPIClass::setBitOrder(BitOrder bitOrder)
|
|||
*/
|
||||
void SPIClass::setDataSize(uint32 datasize)
|
||||
{
|
||||
uint32 cr1 = this->spi_d->regs->CR1;
|
||||
uint32 cr1 = _currentSetting->spi_d->regs->CR1;
|
||||
datasize &= SPI_CR1_DFF;
|
||||
cr1 &= ~(SPI_CR1_DFF);
|
||||
cr1 |= datasize;
|
||||
this->spi_d->regs->CR1 = cr1;
|
||||
_currentSetting->spi_d->regs->CR1 = cr1;
|
||||
}
|
||||
|
||||
void SPIClass::setDataMode(uint8_t dataMode)
|
||||
|
@ -228,7 +227,7 @@ If someone finds this is not the case or sees a logic error with this let me kno
|
|||
#ifdef SPI_DEBUG
|
||||
Serial.print("Data mode set to "); Serial.println(dataMode);
|
||||
#endif
|
||||
this->dataMode = dataMode;
|
||||
_currentSetting->dataMode = dataMode;
|
||||
this->begin();
|
||||
}
|
||||
|
||||
|
@ -243,29 +242,9 @@ void SPIClass::beginTransaction(uint8_t pin, SPISettings settings)
|
|||
//digitalWrite(_SSPin,LOW);
|
||||
setBitOrder(settings.bitOrder);
|
||||
setDataMode(settings.dataMode);
|
||||
setClockDivider(determine_baud_rate(spi_d, settings.clock));
|
||||
setClockDivider(determine_baud_rate(_currentSetting->spi_d, settings.clock));
|
||||
begin();
|
||||
#if 0
|
||||
// code from SAM core
|
||||
uint8_t mode = interruptMode;
|
||||
if (mode > 0) {
|
||||
if (mode < 16) {
|
||||
if (mode & 1) PIOA->PIO_IDR = interruptMask[0];
|
||||
if (mode & 2) PIOB->PIO_IDR = interruptMask[1];
|
||||
if (mode & 4) PIOC->PIO_IDR = interruptMask[2];
|
||||
if (mode & 8) PIOD->PIO_IDR = interruptMask[3];
|
||||
} else {
|
||||
interruptSave = interruptsStatus();
|
||||
noInterrupts();
|
||||
}
|
||||
}
|
||||
uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(pin);
|
||||
bitOrder[ch] = settings.border;
|
||||
SPI_ConfigureNPCS(spi, ch, settings.config);
|
||||
//setBitOrder(pin, settings.border);
|
||||
//setDataMode(pin, settings.datamode);
|
||||
//setClockDivider(pin, settings.clockdiv);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
void SPIClass::endTransaction(void)
|
||||
|
@ -304,9 +283,9 @@ uint8 SPIClass::read(void) {
|
|||
void SPIClass::read(uint8 *buf, uint32 len) {
|
||||
uint32 rxed = 0;
|
||||
while (rxed < len) {
|
||||
while (!spi_is_rx_nonempty(this->spi_d))
|
||||
while (!spi_is_rx_nonempty(_currentSetting->spi_d))
|
||||
;
|
||||
buf[rxed++] = (uint8)spi_rx_reg(this->spi_d);
|
||||
buf[rxed++] = (uint8)spi_rx_reg(_currentSetting->spi_d);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -319,9 +298,9 @@ void SPIClass::write(uint16 data) {
|
|||
* This almost doubles the speed of this function.
|
||||
*/
|
||||
|
||||
spi_tx_reg(this->spi_d, data); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
|
||||
while (spi_is_tx_empty(this->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
||||
while (spi_is_busy(this->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
||||
spi_tx_reg(_currentSetting->spi_d, data); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
|
||||
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
||||
while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
||||
}
|
||||
|
||||
//void SPIClass::write(uint8 byte) {
|
||||
|
@ -333,27 +312,27 @@ void SPIClass::write(uint16 data) {
|
|||
* This almost doubles the speed of this function.
|
||||
*/
|
||||
|
||||
// spi_tx_reg(this->spi_d, byte); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
|
||||
// while (spi_is_tx_empty(this->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
||||
// while (spi_is_busy(this->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
||||
// spi_tx_reg(_currentSetting->spi_d, byte); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
|
||||
// while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
||||
// while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
||||
//}
|
||||
|
||||
void SPIClass::write(const uint8 *data, uint32 length) {
|
||||
uint32 txed = 0;
|
||||
while (txed < length) {
|
||||
txed += spi_tx(this->spi_d, data + txed, length - txed);
|
||||
txed += spi_tx(_currentSetting->spi_d, data + txed, length - txed);
|
||||
}
|
||||
while (spi_is_tx_empty(this->spi_d) == 0); // "4. After writing the last data item into the SPI_DR register, wait until TXE=1 ..."
|
||||
while (spi_is_busy(this->spi_d) != 0); // "... then wait until BSY=0, this indicates that the transmission of the last data is complete."
|
||||
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "4. After writing the last data item into the SPI_DR register, wait until TXE=1 ..."
|
||||
while (spi_is_busy(_currentSetting->spi_d) != 0); // "... then wait until BSY=0, this indicates that the transmission of the last data is complete."
|
||||
}
|
||||
|
||||
uint8 SPIClass::transfer(uint8 byte) const {
|
||||
uint8 b;
|
||||
spi_tx_reg(this->spi_d, byte); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
|
||||
while (spi_is_rx_nonempty(this->spi_d) == 0); // "4. Wait until RXNE=1 ..."
|
||||
b = spi_rx_reg(this->spi_d); // "... and read the last received data."
|
||||
while (spi_is_tx_empty(this->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
||||
while (spi_is_busy(this->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
||||
spi_tx_reg(_currentSetting->spi_d, byte); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
|
||||
while (spi_is_rx_nonempty(_currentSetting->spi_d) == 0); // "4. Wait until RXNE=1 ..."
|
||||
b = spi_rx_reg(_currentSetting->spi_d); // "... and read the last received data."
|
||||
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
||||
while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
||||
return b;
|
||||
}
|
||||
/* Roger Clark and Victor Perez, 2015
|
||||
|
@ -365,7 +344,7 @@ uint8 SPIClass::transfer(uint8 byte) const {
|
|||
uint8 SPIClass::dmaTransfer(uint8 *transmitBuf, uint8 *receiveBuf, uint16 length) {
|
||||
if (length == 0) return 0;
|
||||
uint8 b;
|
||||
if (spi_is_rx_nonempty(this->spi_d) == 1) b = spi_rx_reg(this->spi_d); //Clear the RX buffer in case a byte is waiting on it.
|
||||
if (spi_is_rx_nonempty(_currentSetting->spi_d) == 1) b = spi_rx_reg(_currentSetting->spi_d); //Clear the RX buffer in case a byte is waiting on it.
|
||||
dma1_ch3_Active=true;
|
||||
dma_init(DMA1);
|
||||
dma_attach_interrupt(DMA1, DMA_CH3, &SPIClass::DMA1_CH3_Event);
|
||||
|
@ -405,8 +384,8 @@ uint8 SPIClass::dmaTransfer(uint8 *transmitBuf, uint8 *receiveBuf, uint16 length
|
|||
}
|
||||
|
||||
// }
|
||||
while (spi_is_tx_empty(this->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
||||
while (spi_is_busy(this->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
||||
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
||||
while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
||||
dma_disable(DMA1, DMA_CH3);
|
||||
dma_disable(DMA1, DMA_CH2);
|
||||
spi_rx_dma_disable(SPI1);
|
||||
|
@ -435,10 +414,10 @@ uint8 SPIClass::dmaSend(uint8 *transmitBuf, uint16 length, bool minc) {
|
|||
dma_enable(DMA1, DMA_CH3);// enable transmit
|
||||
|
||||
while (dma1_ch3_Active);
|
||||
while (spi_is_rx_nonempty(this->spi_d) == 0); // "4. Wait until RXNE=1 ..."
|
||||
b = spi_rx_reg(this->spi_d); // "... and read the last received data."
|
||||
while (spi_is_tx_empty(this->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
||||
while (spi_is_busy(this->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
||||
while (spi_is_rx_nonempty(_currentSetting->spi_d) == 0); // "4. Wait until RXNE=1 ..."
|
||||
b = spi_rx_reg(_currentSetting->spi_d); // "... and read the last received data."
|
||||
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
||||
while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
||||
dma_disable(DMA1, DMA_CH3);
|
||||
spi_tx_dma_disable(SPI1);
|
||||
return b;
|
||||
|
@ -460,10 +439,10 @@ uint8 SPIClass::dmaSend(uint16 *transmitBuf, uint16 length, bool minc) {
|
|||
dma_enable(DMA1, DMA_CH3);// enable transmit
|
||||
|
||||
while (dma1_ch3_Active);
|
||||
while (spi_is_rx_nonempty(this->spi_d) == 0); // "4. Wait until RXNE=1 ..."
|
||||
b = spi_rx_reg(this->spi_d); // "... and read the last received data."
|
||||
while (spi_is_tx_empty(this->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
||||
while (spi_is_busy(this->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
||||
while (spi_is_rx_nonempty(_currentSetting->spi_d) == 0); // "4. Wait until RXNE=1 ..."
|
||||
b = spi_rx_reg(_currentSetting->spi_d); // "... and read the last received data."
|
||||
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
||||
while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
||||
dma_disable(DMA1, DMA_CH3);
|
||||
spi_tx_dma_disable(SPI1);
|
||||
return b;
|
||||
|
@ -483,19 +462,19 @@ void SPIClass::detachInterrupt(void) {
|
|||
*/
|
||||
|
||||
uint8 SPIClass::misoPin(void) {
|
||||
return dev_to_spi_pins(this->spi_d)->miso;
|
||||
return dev_to_spi_pins(_currentSetting->spi_d)->miso;
|
||||
}
|
||||
|
||||
uint8 SPIClass::mosiPin(void) {
|
||||
return dev_to_spi_pins(this->spi_d)->mosi;
|
||||
return dev_to_spi_pins(_currentSetting->spi_d)->mosi;
|
||||
}
|
||||
|
||||
uint8 SPIClass::sckPin(void) {
|
||||
return dev_to_spi_pins(this->spi_d)->sck;
|
||||
return dev_to_spi_pins(_currentSetting->spi_d)->sck;
|
||||
}
|
||||
|
||||
uint8 SPIClass::nssPin(void) {
|
||||
return dev_to_spi_pins(this->spi_d)->nss;
|
||||
return dev_to_spi_pins(_currentSetting->spi_d)->nss;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -118,6 +118,11 @@ private:
|
|||
uint32_t clock;
|
||||
BitOrder bitOrder;
|
||||
uint8_t dataMode;
|
||||
|
||||
spi_dev *spi_d;
|
||||
uint8_t _SSPin;
|
||||
//uint32_t clockDivider;
|
||||
|
||||
friend class SPIClass;
|
||||
};
|
||||
|
||||
|
@ -304,7 +309,7 @@ public:
|
|||
* @brief Get a pointer to the underlying libmaple spi_dev for
|
||||
* this HardwareSPI instance.
|
||||
*/
|
||||
spi_dev* c_dev(void) { return this->spi_d; }
|
||||
spi_dev* c_dev(void) { return _currentSetting->spi_d; }
|
||||
|
||||
/* -- The following methods are deprecated --------------------------- */
|
||||
|
||||
|
@ -338,7 +343,7 @@ public:
|
|||
*/
|
||||
uint8 recv(void);
|
||||
|
||||
spi_dev *dev(){ return spi_d;}
|
||||
spi_dev *dev(){ return _currentSetting->spi_d;}
|
||||
|
||||
|
||||
|
||||
|
@ -351,11 +356,17 @@ private:
|
|||
|
||||
// To Do. Need to wait for
|
||||
}
|
||||
|
||||
SPISettings _settings[BOARD_NR_SPI];
|
||||
SPISettings *_currentSetting;
|
||||
|
||||
/*
|
||||
spi_dev *spi_d;
|
||||
uint8_t _SSPin;
|
||||
uint32_t clockDivider;
|
||||
uint8_t dataMode;
|
||||
BitOrder bitOrder;
|
||||
*/
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue