[Teensy 3.1] spi4teensy3 : Send and Receive at the same time

p.lerolland

New member
Hi,

I recently faced a problem to interface an ADC from Texas Instruments (ADS8328).
This ADC needs to send and receive the data at the same time and the spi4teensy3 library could not do it so I modified it a little to do so.

Here is the function I added in the spi3teensy4.cpp :

Code:
        void send_receive(void *bufr, void *data_in, size_t n) {
                int i;
                int nf;
                uint8_t *buf = (uint8_t *)bufr;
                uint16_t *data = (uint16_t *)data_in;

                // clear any data in RX/TX FIFOs, and be certain we are in master mode.
                SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_CLR_RXF | SPI_MCR_CLR_TXF | SPI_MCR_PCSIS(0x1F);
                // initial number of words to push into TX FIFO
                nf = n / 2 < 3 ? n / 2 : 3;
                // limit for pushing data into TX fifo
                uint8_t* limit = buf + n;
                for(i = 0; i < nf; i++) {
                        uint16_t w = (*buf++) << 8;
                        w |= *buf++;
                        SPI0_PUSHR = SPI_PUSHR_CONT | SPI_PUSHR_CTAS(1) | w;
                }
                // write data to TX FIFO
                while(buf < limit) {
                        uint16_t w = *buf++ << 8;
                        w |= *buf++;
                        while(!(SPI0_SR & SPI_SR_RXCTR));
                        SPI0_PUSHR = SPI_PUSHR_CONT | SPI_PUSHR_CTAS(1) | w;
                        *data++ = SPI0_POPR;
                }
                // wait for data to be sent
                while(nf) {
                        while(!(SPI0_SR & SPI_SR_RXCTR));
                        *data++ = SPI0_POPR;
                        nf--;
                }
        }

Also don't forget to add the prototype of the function in the .h.

I know that this is not very clean but it worked for me. Please don't hesitate to modify and improve this piece of code!

Best,

Paul
 
Back
Top