Uses DMA to read/write SPI blocks, while putting the current thread in Wait state.

Dependencies:   SimpleDMA

Dependents:   SDFileSystem-RTOS

Committer:
Tomo2k
Date:
Sat Jun 28 06:55:08 2014 +0000
Revision:
4:0ab62cbad3aa
Parent:
2:d052724e2ad6
Switch to using Erik's version of the SimpleDMA library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Tomo2k 2:d052724e2ad6 1 #ifdef TARGET_KL46Z
Tomo2k 2:d052724e2ad6 2 #include "RTOS_SPI.h"
Tomo2k 2:d052724e2ad6 3
Tomo2k 2:d052724e2ad6 4 RTOS_SPI::RTOS_SPI(PinName mosi, PinName miso, PinName sclk, PinName _unused) : SPI(mosi, miso, sclk) {
Tomo2k 2:d052724e2ad6 5 if (_spi.spi == SPI0) {
Tomo2k 2:d052724e2ad6 6 read_dma.trigger(Trigger_SPI0_RX);
Tomo2k 2:d052724e2ad6 7 write_dma.trigger(Trigger_SPI0_TX);
Tomo2k 2:d052724e2ad6 8 } else {
Tomo2k 2:d052724e2ad6 9 read_dma.trigger(Trigger_SPI1_RX);
Tomo2k 2:d052724e2ad6 10 write_dma.trigger(Trigger_SPI1_TX);
Tomo2k 2:d052724e2ad6 11 }
Tomo2k 2:d052724e2ad6 12
Tomo2k 2:d052724e2ad6 13 read_dma.source(&_spi.spi->DL, false); // 8-bit SPI only uses the LSB
Tomo2k 2:d052724e2ad6 14 write_dma.destination(&_spi.spi->DL, false); // 8-bit SPI only uses the LSB
Tomo2k 2:d052724e2ad6 15 };
Tomo2k 2:d052724e2ad6 16
Tomo2k 2:d052724e2ad6 17 void RTOS_SPI::bulkInternal(uint8_t *read_data, const uint8_t *write_data, int length, bool read_inc, bool write_inc) {
Tomo2k 2:d052724e2ad6 18 aquire();
Tomo2k 2:d052724e2ad6 19 _spi.spi->C2 |= SPI_C2_TXDMAE_MASK | SPI_C2_RXDMAE_MASK;
Tomo2k 2:d052724e2ad6 20
Tomo2k 2:d052724e2ad6 21 read_dma.destination(read_data, read_inc);
Tomo2k 2:d052724e2ad6 22 if (write_inc)
Tomo2k 2:d052724e2ad6 23 write_dma.source(write_data+1, write_inc);
Tomo2k 2:d052724e2ad6 24 else
Tomo2k 2:d052724e2ad6 25 write_dma.source(write_data, write_inc);
Tomo2k 2:d052724e2ad6 26
Tomo2k 2:d052724e2ad6 27 //simply start the read_dma
Tomo2k 2:d052724e2ad6 28 read_dma.start(length);
Tomo2k 2:d052724e2ad6 29
Tomo2k 2:d052724e2ad6 30 //Write the first byte manually, since this is recommended method (and the normal method sends the first byte twice)
Tomo2k 2:d052724e2ad6 31 while((_spi.spi->S & SPI_S_SPTEF_MASK) == 0);
Tomo2k 2:d052724e2ad6 32 _spi.spi->DL = write_data[0]; // 8-bit SPI only uses the LSB
Tomo2k 2:d052724e2ad6 33
Tomo2k 2:d052724e2ad6 34 write_dma.wait(length-1);
Tomo2k 2:d052724e2ad6 35 while(read_dma.isBusy());
Tomo2k 2:d052724e2ad6 36
Tomo2k 2:d052724e2ad6 37 _spi.spi->C2 &= ~(SPI_C2_TXDMAE_MASK | SPI_C2_RXDMAE_MASK);
Tomo2k 2:d052724e2ad6 38 }
Tomo2k 2:d052724e2ad6 39 #endif