/** * @brief Reads a block of data from the LIS302DL. * @param pBuffer : pointer to the buffer that receives the data read from the LIS302DL. * @param ReadAddr : LIS302DL's internal address to read from. * @param NumByteToRead : number of bytes to read from the LIS302DL. * @retval None */ void LIS302DL_Read(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead) { if(NumByteToRead > 0x01) { ReadAddr |= (uint8_t)(READWRITE_CMD | MULTIPLEBYTE_CMD); } else { ReadAddr |= (uint8_t)READWRITE_CMD; } /* Set chip select Low at the start of the transmission */ LIS302DL_CS_LOW(); /* Send the Address of the indexed register */ LIS302DL_SendByte(ReadAddr); /* Receive the data that will be read from the device (MSB First) */ while(NumByteToRead > 0x00) { /* Send dummy byte (0x00) to generate the SPI clock to LIS302DL (Slave device) */ *pBuffer = LIS302DL_SendByte(DUMMY_BYTE); NumByteToRead--; pBuffer++; } /* Set chip select High at the end of the transmission */ LIS302DL_CS_HIGH(); }
/** * @brief Writes one byte to the LIS302DL. * @param pBuffer : pointer to the buffer containing the data to be written to the LIS302DL. * @param WriteAddr : LIS302DL's internal address to write to. * @param NumByteToWrite: Number of bytes to write. * @retval None */ void LIS302DL_Write(uint8_t* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite) { /* Configure the MS bit: - When 0, the address will remain unchanged in multiple read/write commands. - When 1, the address will be auto incremented in multiple read/write commands. */ if(NumByteToWrite > 0x01) { WriteAddr |= (uint8_t)MULTIPLEBYTE_CMD; } /* Set chip select Low at the start of the transmission */ LIS302DL_CS_LOW(); /* Send the Address of the indexed register */ LIS302DL_SendByte(WriteAddr); /* Send the data that will be written into the device (MSB First) */ while(NumByteToWrite >= 0x01) { LIS302DL_SendByte(*pBuffer); NumByteToWrite--; pBuffer++; } /* Set chip select High at the end of the transmission */ LIS302DL_CS_HIGH(); }
void LIS302DL_Read(SPI_TypeDef* spi, uint8_t* pBuffer, uint8_t readAddr, uint16_t numByteToRead) { // For reading multiple bytes we need to set bit 0 (RW) and 1 (MS) if(numByteToRead > 1) { readAddr |= (uint8_t)(LIS302DL_BIT0 | LIS302DL_BIT1); } else { readAddr |= (uint8_t)LIS302DL_BIT0; } // Set chip select Low at the start of the transmission LIS302DL_CS_LOW(); // Send the address of the indexed register _LIS302DL_SendByte(spi, readAddr); // Receive the data that will be read from the device (MSB First) // Send a dummy byte (0x00) to generate the SPI clock to LIS302DL (Slave device) and receive data. while (numByteToRead > 0) { *pBuffer = _LIS302DL_SendByte(spi, (uint8_t)LIS302DL_DUMMY_BYTE); numByteToRead--; pBuffer++; } // Set chip select High at the end of the transmission LIS302DL_CS_HIGH(); }
void LIS302DL_Write(SPI_TypeDef* spi, uint8_t* pBuffer, uint8_t writeAddr, uint16_t numByteToWrite) { // For writing multiple bytes we need to set bit 1 (MS) if (numByteToWrite > 1) { writeAddr |= (uint8_t)LIS302DL_BIT1; } // Set chip select Low at the start of the transmission LIS302DL_CS_LOW(); LIS302DL_Read(spi); // Send the Address of the indexed register _LIS302DL_SendByte(spi, writeAddr); // Send the data that will be written into the device (MSB First) while (numByteToWrite >= 1) { _LIS302DL_SendByte(spi, *pBuffer); numByteToWrite--; pBuffer++; } // Set chip select High at the end of the transmission LIS302DL_CS_HIGH(); }