Including SPI 3-wires class
Fork of X_NUCLEO_COMMON_SPI3W by
Diff: DevSPI3W/SPI3W.cpp
- Revision:
- 23:a42a4217ff73
- Parent:
- 22:62b662a23496
- Child:
- 24:95fd537e9725
--- a/DevSPI3W/SPI3W.cpp Fri May 12 13:12:59 2017 +0000 +++ b/DevSPI3W/SPI3W.cpp Wed May 24 13:54:26 2017 +0200 @@ -120,7 +120,6 @@ So to guarantee the clock generation for only one data, the clock must be disabled after the first bit and before the latest bit */ /* Interrupts should be disabled during this operation */ - __disable_irq(); __HAL_SPI_ENABLE(xSpiHandle); @@ -130,7 +129,7 @@ __enable_irq(); - *val = spi_slave_read(&_spi); + *val = spi_slave_read(&_spi); } /** @@ -142,27 +141,8 @@ */ void SPI3W::SPI_Read_nBytes(SPI_HandleTypeDef* xSpiHandle, uint8_t *val, uint16_t nBytesToRead) { - int tmp=0; - /* Interrupts should be disabled during this operation */ - __disable_irq(); - __HAL_SPI_ENABLE(xSpiHandle); - - /* In master RX mode the clock is automaticaly generated on the SPI enable. - So to guarantee the clock generation for only one data, the clock must be - disabled after the first bit and before the latest bit of the last Byte received */ - /* __DSB instruction are inserted to garantee that clock is Disabled in the right timeframe */ - __DSB(); - __DSB(); - - /* Transfer loop */ - for (int i=0; i<nBytesToRead; i++) *(val+i) = spi_slave_read(&_spi); - - __HAL_SPI_DISABLE(xSpiHandle); - - __enable_irq(); - - tmp = spi_slave_read(&_spi); -} + for (int i=0; i<nBytesToRead; i++) *(val+i) = spi_slave_read(&_spi); +} /** @@ -175,35 +155,18 @@ * @retval 1 in case of failure */ uint8_t SPI3W::Sensor_IO_SPI_Read( DigitalOut * _cs_pin, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ) -{ - +{ /* Select the correct device */ Sensor_IO_SPI_CS_Enable(_cs_pin); - - /* Write Reg Address */ - write(ReadAddr | 0x80); + + /* Write Reg Address with RD bit*/ + write(ReadAddr | 0x80); - /* Disable the SPI and change the data line to input */ - __HAL_SPI_DISABLE(&_spi.spi.handle); - - SPI_1LINE_RX(&_spi.spi.handle); - - /* Check if we need to read one byte or more */ - if(nBytesToRead > 1U) - { - SPI_Read_nBytes(&_spi.spi.handle, pBuffer, nBytesToRead); - } - else - { - SPI_Read(&_spi.spi.handle, pBuffer); - } + SPI_Read_nBytes(&_spi.spi.handle, pBuffer, nBytesToRead); /* Deselect the device */ Sensor_IO_SPI_CS_Disable(_cs_pin); - /* Change the data line to output and enable the SPI */ - SPI_1LINE_TX(&_spi.spi.handle); - return 0; }