Fork of X_NUCLEO_COMMON lib adding 16-bit SPI support
Fork of X_NUCLEO_COMMON by
Diff: DevSPI/DevSPI.h
- Revision:
- 14:655c1d3a5d59
- Parent:
- 13:9ea386e57527
- Child:
- 15:923ab55e8d5d
--- a/DevSPI/DevSPI.h Tue Feb 16 09:44:32 2016 +0000 +++ b/DevSPI/DevSPI.h Tue Feb 16 09:50:42 2016 +0000 @@ -42,7 +42,7 @@ /* Includes ------------------------------------------------------------------*/ #include "mbed.h" - +extern DigitalOut led; /* Classes -------------------------------------------------------------------*/ /** Helper class DevSPI providing functions for SPI communication common for a * series of SPI devices. @@ -105,12 +105,19 @@ ssel = 0; /* Write data. */ - if (_bits == 16) - for (int i = 0; i < NumBytesToWrite; i += 2) - write(((uint16_t *) pBuffer)[i]); - else if(_bits == 8) - for (int i = 0; i < NumBytesToWrite; i++) + if (_bits == 16) { + uint16_t dataToWrite; + for (int i = 0; i < NumBytesToWrite; i += 2) { + dataToWrite = convertFrom8To16(pBuffer); + write(dataToWrite); + pBuffer += sizeof(uint16_t); + } + + } else if(_bits == 8) { + for (int i = 0; i < NumBytesToWrite; i++) { write(pBuffer[i]); + } + } /* Unselect the chip. */ ssel = 1; @@ -133,12 +140,19 @@ ssel = 0; /* Read data. */ - if (_bits == 16) - for (int i = 0; i < NumBytesToRead; i += 2) - ((uint16_t *) pBuffer)[i] = write(0x00); - else if(_bits == 8) - for (int i = 0; i < NumBytesToRead; i++) + if (_bits == 16) { + uint16_t dataToRead; + for (int i = 0; i < NumBytesToRead; i += 2) { + dataToRead = write(0x00); + convertFrom16To8(dataToRead, pBuffer); + pBuffer += sizeof(uint16_t); + } + + } else if(_bits == 8) { + for (int i = 0; i < NumBytesToRead; i++) { pBuffer[i] = write(0x00); + } + } /* Unselect the chip. */ ssel = 1; @@ -162,18 +176,62 @@ ssel = 0; /* Read and write data at the same time. */ - if (_bits == 16) - for (int i = 0; i < NumBytes; i += 2) - ((uint16_t *) pBufferToRead)[i] = write(((uint16_t *) pBufferToWrite)[i]); - else if(_bits == 8) - for (int i = 0; i < NumBytes; i++) + if (_bits == 16) { + uint16_t dataToRead; + uint16_t dataToWrite; + + for (int i = 0; i < NumBytes; i += 2) { + dataToWrite = convertFrom8To16(pBufferToWrite); + + dataToRead = write(dataToWrite); + + convertFrom16To8(dataToRead, pBufferToRead); + + pBufferToWrite += sizeof(uint16_t); + pBufferToRead += sizeof(uint16_t); + } + + } else if(_bits == 8) { + for (int i = 0; i < NumBytes; i++) { pBufferToRead[i] = write(pBufferToWrite[i]); + } + } /* Unselect the chip. */ ssel = 1; return 0; } + +private: + + /** + * @brief Converts two uint8_t words into one of uint16_t + * @param[in] ptr pointer to the buffer of data to be converted. + * @retval 16-bit data. + */ + uint16_t convertFrom8To16(uint8_t *ptr) + { + uint16_t data16 = 0x0000; + + data16 = *ptr; + data16 |= *(++ptr) << 8; + + return data16; + } + + /** + * @brief Converts one uint16_t word into two uint8_t + * @param[in] ptr pointer to the buffer of uint8_t words. + * @param[in] 16-bit data. + * @retval none. + */ + void convertFrom16To8(uint16_t data16, uint8_t *ptr) + { + *(ptr) = data16 & 0x00FF; + *(++ptr) = (data16 >> 8) & 0x00FF; + } + }; #endif /* __DEV_SPI_H */