예제 #1
0
/**
  * @brief  Writes one byte to the LIS3DSH.
  * @param  pBuffer : pointer to the buffer  containing the data to be written to the LIS3DSH.
  * @param  WriteAddr : LIS3DSH's internal address to write to.
  * @param  NumByteToWrite: Number of bytes to write.
  * @retval None
  */
void LIS3DSH_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 */
  LIS3DSH_CS_LOW();

  /* Send the Address of the indexed register */
  LIS3DSH_SendByte(WriteAddr);
  /* Send the data that will be written into the device (MSB First) */
  while(NumByteToWrite >= 0x01)
  {
    LIS3DSH_SendByte(*pBuffer);
    NumByteToWrite--;
    pBuffer++;
  }

  /* Set chip select High at the end of the transmission */
  LIS3DSH_CS_HIGH();
}
예제 #2
0
/**
  * @brief  Reads a block of data from the LIS3DSH.
  * @param  pBuffer : pointer to the buffer that receives the data read from the LIS3DSH.
  * @param  ReadAddr : LIS3DSH's internal address to read from.
  * @param  NumByteToRead : number of bytes to read from the LIS3DSH.
  * @retval None
  */
void LIS3DSH_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 */
  LIS3DSH_CS_LOW();

  /* Send the Address of the indexed register */
  LIS3DSH_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 LIS3DSH (Slave device) */
    *pBuffer = LIS3DSH_SendByte(DUMMY_BYTE);
    NumByteToRead--;
    pBuffer++;
  }

  /* Set chip select High at the end of the transmission */
  LIS3DSH_CS_HIGH();
}
/**
 * @brief  Reads a register from the Accelerometer.
 * @param  addr : Address to read.
 * @param  value: The value read.
 * @retval None
 */
void LIS3DSH_readRegister(LIS3DSH_Register addr, uint8_t* value) {

	/* Set chip select Low at the start of the transmission */
	LIS3DSH_CS_LOW();

	addr |= (uint8_t)0x80;

	uint8_t dummyByte = 0;

	/* Send the Address of the indexed register */
	/* Send a Byte through the SPI peripheral */
	if (HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t*) &addr,
			(uint8_t*) &dummyByte, 1, LIS3DSH_FLAG_TIMEOUT) != HAL_OK) {
		//Error TODO
	}

	/* Send the value of the indexed register */
	/* Send a Byte through the SPI peripheral */
	if (HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t*) &dummyByte,
			(uint8_t*) value, 1, LIS3DSH_FLAG_TIMEOUT) != HAL_OK) {
		//Error TODO
	}

	/* Set chip select High at the end of the transmission */
	LIS3DSH_CS_HIGH();
}