improve SPI low level functions
- optimize code and run-time
This commit is contained in:
parent
4bc4b1d6bd
commit
5db2523284
|
@ -84,7 +84,7 @@ void spi_slave_enable(spi_dev *dev, spi_mode mode, uint32 flags) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Nonblocking SPI transmit.
|
* @brief Blocking SPI transmit.
|
||||||
* @param dev SPI port to use for transmission
|
* @param dev SPI port to use for transmission
|
||||||
* @param buf Buffer to transmit. The sizeof buf's elements are
|
* @param buf Buffer to transmit. The sizeof buf's elements are
|
||||||
* inferred from dev's data frame format (i.e., are
|
* inferred from dev's data frame format (i.e., are
|
||||||
|
@ -95,7 +95,8 @@ void spi_slave_enable(spi_dev *dev, spi_mode mode, uint32 flags) {
|
||||||
uint32 spi_tx(spi_dev *dev, const void *buf, uint32 len) {
|
uint32 spi_tx(spi_dev *dev, const void *buf, uint32 len) {
|
||||||
uint32 txed = 0;
|
uint32 txed = 0;
|
||||||
uint8 byte_frame = spi_dff(dev) == SPI_DFF_8_BIT;
|
uint8 byte_frame = spi_dff(dev) == SPI_DFF_8_BIT;
|
||||||
while (spi_is_tx_empty(dev) && (txed < len)) {
|
while ( txed < len ) {
|
||||||
|
while ( spi_is_tx_empty(dev)==0 ); // wait Tx to be empty
|
||||||
if (byte_frame) {
|
if (byte_frame) {
|
||||||
dev->regs->DR = ((const uint8*)buf)[txed++];
|
dev->regs->DR = ((const uint8*)buf)[txed++];
|
||||||
} else {
|
} else {
|
||||||
|
@ -162,5 +163,6 @@ static void spi_reconfigure(spi_dev *dev, uint32 cr1_config) {
|
||||||
spi_irq_disable(dev, SPI_INTERRUPTS_ALL);
|
spi_irq_disable(dev, SPI_INTERRUPTS_ALL);
|
||||||
if ( (dev->regs->CR1&MASK)!=(cr1_config&MASK) ) spi_peripheral_disable(dev);
|
if ( (dev->regs->CR1&MASK)!=(cr1_config&MASK) ) spi_peripheral_disable(dev);
|
||||||
dev->regs->CR1 = cr1_config;
|
dev->regs->CR1 = cr1_config;
|
||||||
|
//spi_rx_dma_enable(dev);
|
||||||
spi_peripheral_enable(dev);
|
spi_peripheral_enable(dev);
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,8 @@
|
||||||
#include "boards.h"
|
#include "boards.h"
|
||||||
|
|
||||||
//#include "HardwareSerial.h"
|
//#include "HardwareSerial.h"
|
||||||
|
/** Time in ms for DMA receive timeout */
|
||||||
|
#define DMA_TIMEOUT 100
|
||||||
|
|
||||||
#if CYCLES_PER_MICROSECOND != 72
|
#if CYCLES_PER_MICROSECOND != 72
|
||||||
/* TODO [0.2.0?] something smarter than this */
|
/* TODO [0.2.0?] something smarter than this */
|
||||||
|
@ -304,138 +306,110 @@ void SPIClass::endTransaction(void)
|
||||||
* I/O
|
* I/O
|
||||||
*/
|
*/
|
||||||
|
|
||||||
uint8 SPIClass::read(void) {
|
uint8 SPIClass::read(void)
|
||||||
uint8 buf[1];
|
{
|
||||||
this->read(buf, 1);
|
while ( spi_is_rx_nonempty(_currentSetting->spi_d)==0 ) ;
|
||||||
return buf[0];
|
return (uint8)spi_rx_reg(_currentSetting->spi_d);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SPIClass::read(uint8 *buf, uint32 len) {
|
void SPIClass::read(uint8 *buf, uint32 len)
|
||||||
|
{
|
||||||
|
spi_reg_map * regs = _currentSetting->spi_d->regs;
|
||||||
|
uint8 b = (regs->DR); // clear the RX buffer in case a byte is waiting on it.
|
||||||
uint32 rxed = 0;
|
uint32 rxed = 0;
|
||||||
while (rxed < len) {
|
// start sequence
|
||||||
while (!spi_is_rx_nonempty(_currentSetting->spi_d))
|
while ( rxed < len) {
|
||||||
;
|
regs->DR = 0x00FF; // " write the data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
|
||||||
buf[rxed++] = (uint8)spi_rx_reg(_currentSetting->spi_d);
|
while ( (regs->SR & SPI_SR_RXNE)==0 ) ; // wait till data is available in the Rx register
|
||||||
|
buf[rxed++] = (uint8)(regs->DR); // read and store the received byte
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SPIClass::write(uint16 data) {
|
void SPIClass::write(uint16 data)
|
||||||
// this->write(&data, 1);
|
{
|
||||||
|
|
||||||
/* Added for 16bit data Victor Perez. Roger Clark
|
/* Added for 16bit data Victor Perez. Roger Clark
|
||||||
* Improved speed by just directly writing the single byte to the SPI data reg and wait for completion, * by taking the Tx code from transfer(byte)
|
* Improved speed by just directly writing the single byte to the SPI data reg and wait for completion,
|
||||||
* The original method, of calling write(*data, length) .
|
* by taking the Tx code from transfer(byte)
|
||||||
* This almost doubles the speed of this function.
|
* This almost doubles the speed of this function.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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)."
|
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_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."
|
while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
||||||
}
|
}
|
||||||
|
|
||||||
//void SPIClass::write(uint8 byte) {
|
void SPIClass::write(const void *data, uint32 length)
|
||||||
// this->write(&byte, 1);
|
{
|
||||||
|
spi_tx(_currentSetting->spi_d, data, length); // data can be array of bytes or words
|
||||||
/* Roger Clark
|
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
||||||
* Improved speed by just directly writing the single byte to the SPI data reg and wait for completion, * by taking the Tx code from transfer(byte)
|
while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
||||||
* The original method, of calling write(*data, length) .
|
|
||||||
* This almost doubles the speed of this function.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// 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(_currentSetting->spi_d, data + txed, length - txed);
|
|
||||||
}
|
|
||||||
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."
|
|
||||||
// taken from SdSpiSTM32F1.cpp - Victor's lib, and adapted to support device selection
|
// taken from SdSpiSTM32F1.cpp - Victor's lib, and adapted to support device selection
|
||||||
if (spi_is_rx_nonempty(_currentSetting->spi_d)) {
|
uint16 b = spi_rx_reg(_currentSetting->spi_d); // dummy read, needed, don't remove!
|
||||||
uint8_t b = spi_rx_reg(_currentSetting->spi_d);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t SPIClass::transfer16(uint16_t wr_data) const {
|
uint16_t SPIClass::transfer16(uint16_t wr_data) const {
|
||||||
spi_tx_reg(_currentSetting->spi_d, wr_data); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
|
spi_tx_reg(_currentSetting->spi_d, wr_data); // "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 ..."
|
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
||||||
uint16_t rd_data = spi_rx_reg(_currentSetting->spi_d); // "... and read the last received data."
|
while (spi_is_busy(_currentSetting->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 ..."
|
return (uint16)spi_rx_reg(_currentSetting->spi_d); // "... and read the last received data."
|
||||||
// while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
|
||||||
return rd_data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8 SPIClass::transfer(uint8 byte) const {
|
uint8 SPIClass::transfer(uint8 byte) const {
|
||||||
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)."
|
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 ..."
|
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
||||||
uint8 b = spi_rx_reg(_currentSetting->spi_d); // "... and read the last received data."
|
while (spi_is_busy(_currentSetting->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 ..."
|
return (uint8)spi_rx_reg(_currentSetting->spi_d); // "... and read the last received data."
|
||||||
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
|
/* Roger Clark and Victor Perez, 2015
|
||||||
* Performs a DMA SPI transfer with at least a receive buffer.
|
* Performs a DMA SPI transfer with at least a receive buffer.
|
||||||
* If a TX buffer is not provided, FF is sent over and over for the lenght of the transfer.
|
* If a TX buffer is not provided, FF is sent over and over for the length of the transfer.
|
||||||
* On exit TX buffer is not modified, and RX buffer cotains the received data.
|
* On exit TX buffer is not modified, and RX buffer contains the received data.
|
||||||
* Still in progress.
|
* Still in progress.
|
||||||
*/
|
*/
|
||||||
uint8 SPIClass::dmaTransfer(uint8 *transmitBuf, uint8 *receiveBuf, uint16 length) {
|
uint8 SPIClass::dmaTransfer(void * transmitBuf, void * receiveBuf, uint16 length)
|
||||||
|
{
|
||||||
if (length == 0) return 0;
|
if (length == 0) return 0;
|
||||||
uint8 b = 0;
|
uint8 b = 0;
|
||||||
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.
|
spi_rx_reg(_currentSetting->spi_d); //Clear the RX buffer in case a byte is waiting on it.
|
||||||
// dma1_ch3_Active=true;
|
// dma1_ch3_Active=true;
|
||||||
dma_init(_currentSetting->spiDmaDev);
|
dma_init(_currentSetting->spiDmaDev);
|
||||||
// dma_attach_interrupt(DMA1, DMA_CH3, &SPIClass::DMA1_CH3_Event);
|
// dma_attach_interrupt(DMA1, DMA_CH3, &SPIClass::DMA1_CH3_Event);
|
||||||
|
|
||||||
// RX
|
// RX
|
||||||
spi_rx_dma_enable(_currentSetting->spi_d);
|
spi_rx_dma_enable(_currentSetting->spi_d);
|
||||||
dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel, &_currentSetting->spi_d->regs->DR, DMA_SIZE_8BITS,
|
dma_xfer_size dma_bit_size = (_currentSetting->dataSize==DATA_SIZE_8BIT) ? DMA_SIZE_8BITS : DMA_SIZE_16BITS;
|
||||||
receiveBuf, DMA_SIZE_8BITS, (DMA_MINC_MODE | DMA_TRNS_CMPLT));// receive buffer DMA
|
dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel, &_currentSetting->spi_d->regs->DR, dma_bit_size,
|
||||||
|
receiveBuf, dma_bit_size, (DMA_MINC_MODE | DMA_TRNS_CMPLT));// receive buffer DMA
|
||||||
dma_set_num_transfers(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel, length);
|
dma_set_num_transfers(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel, length);
|
||||||
|
|
||||||
// TX
|
// TX
|
||||||
|
uint32 flags = (DMA_MINC_MODE | DMA_FROM_MEM | DMA_TRNS_CMPLT);
|
||||||
spi_tx_dma_enable(_currentSetting->spi_d);
|
spi_tx_dma_enable(_currentSetting->spi_d);
|
||||||
if (!transmitBuf) {
|
if ( transmitBuf==0 ) {
|
||||||
static uint8_t ff = 0XFF;
|
static uint8_t ff = 0XFF;
|
||||||
transmitBuf = &ff;
|
transmitBuf = &ff;
|
||||||
dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &_currentSetting->spi_d->regs->DR, DMA_SIZE_8BITS,
|
flags ^= DMA_MINC_MODE; // remove increment mode
|
||||||
transmitBuf, DMA_SIZE_8BITS, (DMA_FROM_MEM | DMA_TRNS_CMPLT));// Transmit FF repeatedly
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &_currentSetting->spi_d->regs->DR, DMA_SIZE_8BITS,
|
|
||||||
transmitBuf, DMA_SIZE_8BITS, (DMA_MINC_MODE | DMA_FROM_MEM | DMA_TRNS_CMPLT));// Transmit buffer DMA
|
|
||||||
}
|
}
|
||||||
|
dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &_currentSetting->spi_d->regs->DR, dma_bit_size,
|
||||||
|
transmitBuf, dma_bit_size, flags);// Transmit buffer DMA
|
||||||
dma_set_num_transfers(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, length);
|
dma_set_num_transfers(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, length);
|
||||||
|
|
||||||
dma_enable(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel);// enable receive
|
dma_enable(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel);// enable receive
|
||||||
dma_enable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);// enable transmit
|
dma_enable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);// enable transmit
|
||||||
|
|
||||||
// while (dma1_ch3_Active);
|
|
||||||
// if (receiveBuf) {
|
|
||||||
uint32_t m = millis();
|
uint32_t m = millis();
|
||||||
while ((dma_get_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel) & 0x2)==0) {//Avoid interrupts and just loop waiting for the flag to be set.
|
while ((dma_get_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel) & 0x2)==0) {//Avoid interrupts and just loop waiting for the flag to be set.
|
||||||
if ((millis() - m) > 100) {
|
if ((millis() - m) > DMA_TIMEOUT) { b = 2; break; }
|
||||||
// dma1_ch3_Active = 0;
|
|
||||||
b = 2;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
dma_clear_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
|
dma_clear_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
|
||||||
|
|
||||||
// }
|
|
||||||
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
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."
|
while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
||||||
dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
|
dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
|
||||||
dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel);
|
dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiRxDmaChannel);
|
||||||
spi_rx_dma_disable(_currentSetting->spi_d); // And disable generation of DMA request from the SPI port so other peripherals can use the channels
|
spi_rx_dma_disable(_currentSetting->spi_d); // And disable generation of DMA request from the SPI port so other peripherals can use the channels
|
||||||
spi_tx_dma_disable(_currentSetting->spi_d);
|
spi_tx_dma_disable(_currentSetting->spi_d);
|
||||||
if (spi_is_rx_nonempty(_currentSetting->spi_d) != 0){; // "4. Wait until RXNE=1 ..."
|
uint16 x = spi_rx_reg(_currentSetting->spi_d); // dummy read, needed, don't remove!
|
||||||
uint8 x = spi_rx_reg(_currentSetting->spi_d); // "... and read the last received data."
|
|
||||||
}
|
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -443,10 +417,12 @@ uint8 SPIClass::dmaTransfer(uint8 *transmitBuf, uint8 *receiveBuf, uint16 length
|
||||||
* Performs a DMA SPI send using a TX buffer.
|
* Performs a DMA SPI send using a TX buffer.
|
||||||
* On exit TX buffer is not modified.
|
* On exit TX buffer is not modified.
|
||||||
* Still in progress.
|
* Still in progress.
|
||||||
|
* 2016 - stevstrong - reworked to automatically detect bit size from SPI setting
|
||||||
*/
|
*/
|
||||||
uint8 SPIClass::dmaSend(uint8 *transmitBuf, uint16 length, bool minc) {
|
uint8 SPIClass::dmaSend(void * transmitBuf, uint16 length)
|
||||||
|
{
|
||||||
if (length == 0) return 0;
|
if (length == 0) return 0;
|
||||||
uint32 flags = ((DMA_MINC_MODE * minc) | DMA_FROM_MEM | DMA_TRNS_CMPLT);
|
uint32 flags = (DMA_MINC_MODE | DMA_FROM_MEM | DMA_TRNS_CMPLT);
|
||||||
uint8 b = 0;
|
uint8 b = 0;
|
||||||
// dma1_ch3_Active=true;
|
// dma1_ch3_Active=true;
|
||||||
dma_init(_currentSetting->spiDmaDev);
|
dma_init(_currentSetting->spiDmaDev);
|
||||||
|
@ -454,55 +430,27 @@ uint8 SPIClass::dmaSend(uint8 *transmitBuf, uint16 length, bool minc) {
|
||||||
|
|
||||||
// TX
|
// TX
|
||||||
spi_tx_dma_enable(_currentSetting->spi_d);
|
spi_tx_dma_enable(_currentSetting->spi_d);
|
||||||
dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &_currentSetting->spi_d->regs->DR, DMA_SIZE_8BITS,
|
dma_xfer_size dma_bit_size = (_currentSetting->dataSize==DATA_SIZE_8BIT) ? DMA_SIZE_8BITS : DMA_SIZE_16BITS;
|
||||||
transmitBuf, DMA_SIZE_8BITS, flags);// Transmit buffer DMA
|
dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &_currentSetting->spi_d->regs->DR, dma_bit_size,
|
||||||
|
transmitBuf, dma_bit_size, flags);// Transmit buffer DMA
|
||||||
dma_set_num_transfers(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, length);
|
dma_set_num_transfers(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, length);
|
||||||
dma_enable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);// enable transmit
|
dma_enable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);// enable transmit
|
||||||
|
|
||||||
// while (dma1_ch3_Active);
|
// while (dma1_ch3_Active);
|
||||||
while ((dma_get_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel) & 0x2)==0); //Avoid interrupts and just loop waiting for the flag to be set.
|
uint32_t m = millis();
|
||||||
|
while ((dma_get_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel) & 0x2)==0) {//Avoid interrupts and just loop waiting for the flag to be set.
|
||||||
|
if ((millis() - m) > DMA_TIMEOUT) { b = 2; break; }
|
||||||
|
}
|
||||||
dma_clear_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
|
dma_clear_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
|
||||||
|
|
||||||
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
|
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."
|
while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
|
||||||
dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
|
dma_disable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
|
||||||
spi_tx_dma_disable(_currentSetting->spi_d);
|
spi_tx_dma_disable(_currentSetting->spi_d);
|
||||||
if (spi_is_rx_nonempty(_currentSetting->spi_d) != 0){; // "4. Wait until RXNE=1 ..."
|
uint16 x = spi_rx_reg(_currentSetting->spi_d); // dummy read, needed, don't remove!
|
||||||
uint8 x = spi_rx_reg(_currentSetting->spi_d); // "... and read the last received data."
|
|
||||||
}
|
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8 SPIClass::dmaSend(uint16 *transmitBuf, uint16 length, bool minc) {
|
|
||||||
if (length == 0) return 0;
|
|
||||||
uint32 flags = ((DMA_MINC_MODE * minc) | DMA_FROM_MEM | DMA_TRNS_CMPLT);
|
|
||||||
uint8 b;
|
|
||||||
dma1_ch3_Active=true;
|
|
||||||
dma_init(_currentSetting->spiDmaDev);
|
|
||||||
// dma_attach_interrupt(DMA1, DMA_CH3, &SPIClass::DMA1_CH3_Event);
|
|
||||||
|
|
||||||
// TX
|
|
||||||
spi_tx_dma_enable(_currentSetting->spi_d);
|
|
||||||
dma_setup_transfer(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, &_currentSetting->spi_d->regs->DR, DMA_SIZE_16BITS,
|
|
||||||
transmitBuf, DMA_SIZE_16BITS, flags);// Transmit buffer DMA
|
|
||||||
dma_set_num_transfers(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel, length);
|
|
||||||
dma_enable(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);// enable transmit
|
|
||||||
|
|
||||||
// while (dma1_ch3_Active);
|
|
||||||
while ((dma_get_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel) & 0x2)==0); //Avoid interrupts and just loop waiting for the flag to be set.
|
|
||||||
dma_clear_isr_bits(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
|
|
||||||
|
|
||||||
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(_currentSetting->spiDmaDev, _currentSetting->spiTxDmaChannel);
|
|
||||||
spi_tx_dma_disable(_currentSetting->spi_d);
|
|
||||||
if (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."
|
|
||||||
}
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void SPIClass::attachInterrupt(void) {
|
void SPIClass::attachInterrupt(void) {
|
||||||
// Should be enableInterrupt()
|
// Should be enableInterrupt()
|
||||||
}
|
}
|
||||||
|
|
|
@ -249,7 +249,7 @@ public:
|
||||||
* @param buffer Bytes to transmit.
|
* @param buffer Bytes to transmit.
|
||||||
* @param length Number of bytes in buffer to transmit.
|
* @param length Number of bytes in buffer to transmit.
|
||||||
*/
|
*/
|
||||||
void write(const uint8 *buffer, uint32 length);
|
void write(const void * buffer, uint32 length);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Transmit a byte, then return the next unread byte.
|
* @brief Transmit a byte, then return the next unread byte.
|
||||||
|
@ -264,6 +264,7 @@ public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Sets up a DMA Transfer for "length" bytes.
|
* @brief Sets up a DMA Transfer for "length" bytes.
|
||||||
|
* The transfer mode (8 or 16 bit mode) is evaluated from the SPI peripheral setting.
|
||||||
*
|
*
|
||||||
* This function transmits and receives to buffers.
|
* This function transmits and receives to buffers.
|
||||||
*
|
*
|
||||||
|
@ -271,30 +272,18 @@ public:
|
||||||
* @param receiveBuf buffer Bytes to save received data.
|
* @param receiveBuf buffer Bytes to save received data.
|
||||||
* @param length Number of bytes in buffer to transmit.
|
* @param length Number of bytes in buffer to transmit.
|
||||||
*/
|
*/
|
||||||
uint8 dmaTransfer(uint8 *transmitBuf, uint8 *receiveBuf, uint16 length);
|
uint8 dmaTransfer(void * transmitBuf, void * receiveBuf, uint16 length);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Sets up a DMA Transmit for bytes.
|
* @brief Sets up a DMA Transmit for SPI 8 or 16 bit transfer mode.
|
||||||
|
* The transfer mode (8 or 16 bit mode) is evaluated from the SPI peripheral setting.
|
||||||
*
|
*
|
||||||
* This function transmits and does not care about the RX fifo.
|
* This function only transmits and does not care about the RX fifo.
|
||||||
*
|
|
||||||
* @param transmitBuf buffer Bytes to transmit,
|
|
||||||
* @param length Number of bytes in buffer to transmit.
|
|
||||||
* @param minc Set to use Memory Increment mode, clear to use Circular mode.
|
|
||||||
*/
|
|
||||||
uint8 dmaSend(uint8 *transmitBuf, uint16 length, bool minc = 1);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Sets up a DMA Transmit for half words.
|
|
||||||
* SPI PERFIPHERAL MUST BE SET TO 16 BIT MODE BEFORE
|
|
||||||
*
|
|
||||||
* This function transmits and does not care about the RX fifo.
|
|
||||||
*
|
*
|
||||||
* @param data buffer half words to transmit,
|
* @param data buffer half words to transmit,
|
||||||
* @param length Number of bytes in buffer to transmit.
|
* @param length Number of bytes in buffer to transmit.
|
||||||
* @param minc Set to use Memory Increment mode (default if blank), clear to use Circular mode.
|
|
||||||
*/
|
*/
|
||||||
uint8 dmaSend(uint16 *transmitBuf, uint16 length, bool minc = 1);
|
uint8 dmaSend(void * transmitBuf, uint16 length);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Pin accessors
|
* Pin accessors
|
||||||
|
|
Loading…
Reference in New Issue