Including SPI 3-wires class
Fork of X_NUCLEO_COMMON_SPI3W by
Diff: DevSPI/DevSPI.h
- Revision:
- 13:9a2c9b0bbe61
- Parent:
- 12:fb6321e37154
- Parent:
- 11:165bd6bc00ea
- Child:
- 16:216930edb6b7
diff -r fb6321e37154 -r 9a2c9b0bbe61 DevSPI/DevSPI.h --- a/DevSPI/DevSPI.h Mon Oct 19 10:24:47 2015 +0200 +++ b/DevSPI/DevSPI.h Fri Feb 19 09:21:54 2016 +0100 @@ -2,8 +2,8 @@ ****************************************************************************** * @file DevSPI.h * @author AST / Software Platforms and Cloud - * @version V0.0.1 - * @date September 24th, 2015 + * @version V1.0.1 + * @date February 11th, 2016 * @brief Header file for a special SPI class DevSPI which provides some * helper function for on-board communication. ****************************************************************************** @@ -58,10 +58,8 @@ */ DevSPI(PinName mosi, PinName miso, PinName sclk) : SPI(mosi, miso, sclk) { - bits = 9; - mode = 0; - frequency_hz = 1E6; - setup_done = false; + /* Set default configuration. */ + setup(8, 3, 1E6); } /* @@ -87,9 +85,9 @@ */ void setup(int bits, int mode = 0, int frequency_hz = 1E6) { + /* Set given configuration. */ format(bits, mode); frequency(frequency_hz); - setup_done = true; } /** @@ -103,20 +101,16 @@ */ int spi_write(uint8_t* pBuffer, DigitalOut ssel, uint16_t NumBytesToWrite) { - /* Setup. */ - if (!setup_done) - { - ssel = 1; - setup(8, 3, 1E6); - setup_done = true; - } - /* Select the chip. */ ssel = 0; /* Write data. */ - for (int i = 0; i < NumBytesToWrite; i++) - write(pBuffer[i]); + 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++) + write(pBuffer[i]); /* Unselect the chip. */ ssel = 1; @@ -135,20 +129,16 @@ */ int spi_read(uint8_t* pBuffer, DigitalOut ssel, uint16_t NumBytesToRead) { - /* Setup. */ - if (!setup_done) - { - ssel = 1; - setup(8, 3, 1E6); - setup_done = true; - } - /* Select the chip. */ ssel = 0; /* Read data. */ - for (int i = 0; i < NumBytesToRead; i++) - pBuffer[i] = write(0x00); + 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++) + pBuffer[i] = write(0x00); /* Unselect the chip. */ ssel = 1; @@ -168,32 +158,22 @@ */ int spi_read_write(uint8_t* pBufferToRead, uint8_t* pBufferToWrite, DigitalOut ssel, uint16_t NumBytes) { - /* Setup. */ - if (!setup_done) - { - ssel = 1; - setup(8, 3, 1E6); - setup_done = true; - } - /* Select the chip. */ ssel = 0; /* Read and write data at the same time. */ - for (int i = 0; i < NumBytes; i++) - pBufferToRead[i] = write(pBufferToWrite[i]); + 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++) + pBufferToRead[i] = write(pBufferToWrite[i]); /* Unselect the chip. */ ssel = 1; return 0; } - - protected: - bool setup_done; - int bits; - int mode; - int frequency_hz; }; #endif /* __DEV_SPI_H */