/** * @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_Write(u8 REG, u8 *DATA, u8 count) { GPIO_WriteBit(GPIOE, GPIO_Pin_3, RESET); LIS302DL_SendByte(REG); for (int i=0; i < count; i++) { LIS302DL_SendByte(*DATA); DATA++; } GPIO_WriteBit(GPIOE, GPIO_Pin_3, SET); }
void LIS302DL_Read(u8 REG, u8 *DATA, u8 count) { GPIO_WriteBit(GPIOE, GPIO_Pin_3, RESET); REG |= 0x80; if (count > 1) { REG |= 0x40; } LIS302DL_SendByte(REG); for (u8 i=0; i < count; i++) { *DATA = LIS302DL_SendByte((u8)0x00); DATA++; } GPIO_WriteBit(GPIOE, GPIO_Pin_3, SET); }