From 664e1c88ac55cd0cde605deacf98878e86abd736 Mon Sep 17 00:00:00 2001 From: Roger Clark Date: Mon, 30 Mar 2015 06:47:26 +1100 Subject: [PATCH] Second attempt at SPI::DMATransfer. Now uses separate tx and rx buffers, and I also added code to wait for tx to complete and SPI to not be busy before exiting the function. Note. Operation of rx buffer has not been tested --- .../Adafruit_ILI9341/Adafruit_ILI9341.cpp | 10 +++--- STM32F1/libraries/SPI/src/SPI.cpp | 33 ++++++++++++------- STM32F1/libraries/SPI/src/SPI.h | 5 ++- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/STM32F1/libraries/Adafruit_ILI9341/Adafruit_ILI9341.cpp b/STM32F1/libraries/Adafruit_ILI9341/Adafruit_ILI9341.cpp index e8eba84..4233716 100644 --- a/STM32F1/libraries/Adafruit_ILI9341/Adafruit_ILI9341.cpp +++ b/STM32F1/libraries/Adafruit_ILI9341/Adafruit_ILI9341.cpp @@ -457,19 +457,21 @@ void Adafruit_ILI9341::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uin if (true) { // Use DMA - byte lineBuffer[h*2];// Buffer to be sent via DMA + byte txBuf[h*2];// Buffer to be sent via DMA + byte rxBuf[h*2];// Buffer to be sent via DMA // need to build a buffer of the required height (h) // Note I suspect there is a faster way to do this for(int i=0;iregs->DR, DMA_SIZE_8BITS, + receiveBuf, DMA_SIZE_8BITS, (DMA_MINC_MODE | DMA_TRNS_CMPLT | DMA_TRNS_ERR));// receive buffer DMA + dma_set_num_transfers(DMA1, DMA_CH2, length); + + // TX + spi_tx_dma_enable(SPI1); dma_setup_transfer(DMA1, DMA_CH3, &SPI1->regs->DR, DMA_SIZE_8BITS, - data, DMA_SIZE_8BITS, (DMA_MINC_MODE | DMA_FROM_MEM | DMA_TRNS_CMPLT)); + transmitBuf, DMA_SIZE_8BITS, (DMA_MINC_MODE | DMA_FROM_MEM | DMA_TRNS_CMPLT));// Transmit buffer DMA + dma_set_num_transfers(DMA1, DMA_CH3, length); + + dma_enable(DMA1, DMA_CH2);// enable receive + dma_enable(DMA1, DMA_CH3);// enable transmit - dma_set_num_transfers(DMA1, DMA_CH3, length); // 2 bytes per pixel - dma_enable(DMA1, DMA_CH3); - while (dma1_ch3_Active); + while (dma1_ch3_Active); + 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." return b; } diff --git a/STM32F1/libraries/SPI/src/SPI.h b/STM32F1/libraries/SPI/src/SPI.h index 3a32e39..7a8ea0d 100644 --- a/STM32F1/libraries/SPI/src/SPI.h +++ b/STM32F1/libraries/SPI/src/SPI.h @@ -228,7 +228,7 @@ public: */ uint8 transfer(uint8 data); - uint8 DMATransfer(uint8 *data, uint32 length); + uint8 DMATransfer(uint8 *transmitBuf, uint8 *receiveBuf, uint32 length); /* * Pin accessors @@ -303,6 +303,9 @@ private: static inline void DMA1_CH3_Event() { dma1_ch3_Active = 0; dma_disable(DMA1, DMA_CH3); + dma_disable(DMA1, DMA_CH2); + + // To Do. Need to wait for } spi_dev *spi_d; uint8_t _SSPin;