Platform drivers for Mbed.
Dependents: EVAL-CN0535-FMCZ EVAL-CN0535-FMCZ EVAL-AD568x-AD569x EVAL-AD7606 ... more
Diff: src/spi.cpp
- Revision:
- 14:46aad38346a6
- Parent:
- 13:c446482b0360
- Child:
- 15:fd2c3c3038bf
--- a/src/spi.cpp Fri Aug 14 14:26:41 2020 +0530 +++ b/src/spi.cpp Fri Oct 16 21:23:58 2020 +0530 @@ -25,11 +25,14 @@ /********************** Macros and Constants Definitions **********************/ /******************************************************************************/ +#define SPI_16_BIT_FRAME 16 // SPI 16-bit frame size +#define SPI_8_BIT_FRAME 8 // SPI 8-bit frame size + /******************************************************************************/ /********************** Variables and User defined data types *****************/ /******************************************************************************/ -static uint8_t spi_format_bytes = 16; // SPI format +static uint8_t spi_format_bytes = SPI_16_BIT_FRAME; // SPI format /******************************************************************************/ /************************ Functions Declarations ******************************/ @@ -97,10 +100,10 @@ SPI frequency is required, consult your device documentation. **/ spi->frequency(param->max_speed_hz); - spi->format(16, param->mode); // 16-bit data write/read format - spi_format_bytes = 16; - spi->set_default_write_value(0x00); // code to write when reading back - ss->write(GPIO_HIGH); // set SS high + spi->format(SPI_16_BIT_FRAME, param->mode); // data write/read format + spi_format_bytes = SPI_16_BIT_FRAME; + spi->set_default_write_value(0x00); // code to write when reading back + ss->write(GPIO_HIGH); // set SS high return SUCCESS; } @@ -155,9 +158,10 @@ { mbed::SPI *spi; // pointer to new spi instance mbed::DigitalOut *ss; // pointer to new SS instance - uint8_t num_of_words; // Number of words in SPI frame - uint16_t rw_data; // SPI read data - uint8_t data_index = 0; // Data index + uint16_t num_of_words; // Number of words in SPI frame + uint16_t rw_data; // SPI read data (16-bit) + uint16_t data_index = 0; // Data index + size_t byte; // Byte read/write index if (desc) { spi = (SPI *)(((mbed_spi_desc *)(desc->extra))->spi_port); @@ -168,14 +172,14 @@ /* Determine the data transmit/receive format based on parity of data */ if (!(bytes_number % 2)) { - if (spi_format_bytes != 16) { - spi->format(16, desc->mode); - spi_format_bytes = 16; + if (spi_format_bytes != SPI_16_BIT_FRAME) { + spi->format(SPI_16_BIT_FRAME, desc->mode); + spi_format_bytes = SPI_16_BIT_FRAME; } } else { - if (spi_format_bytes != 8) { - spi->format(8, desc->mode); - spi_format_bytes = 8; + if (spi_format_bytes != SPI_8_BIT_FRAME) { + spi->format(SPI_8_BIT_FRAME, desc->mode); + spi_format_bytes = SPI_8_BIT_FRAME; } } @@ -204,7 +208,7 @@ num_of_words--; } } else { - for (size_t byte = 0; byte < bytes_number; byte++) { + for (byte = 0; byte < bytes_number; byte++) { data[byte] = spi->write(data[byte]); } }