/** * @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 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(); }
/** * @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(); }
/** * @brief Configures LIS3DSH * @param InitTypeDef: Specifies the configuration. * @retval None */ void LIS3DSH_init(LIS3DSH_InitTypeDef li) { GPIO_InitTypeDef GPIO_InitStructure; /* Configure the LIS3DSH Control pins --------------------------------*/ /* Enable CS GPIO clock and configure GPIO pin for LIS3DSH Chip select */ LIS3DSH_SPI_CS_GPIO_CLK_ENABLE(); /* Configure GPIO PIN for LIS Chip select */ GPIO_InitStructure.Pin = LIS3DSH_SPI_CS_PIN; GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM; HAL_GPIO_Init(LIS3DSH_SPI_CS_GPIO_PORT, &GPIO_InitStructure); /* Deselect: Chip Select high */ /* SPI start when the chip select is low */ LIS3DSH_CS_HIGH(); /* Configure the SPI1 to communicate with the accelerometer */ if (HAL_SPI_GetState(&SpiHandle) == HAL_SPI_STATE_RESET) { /* SPI configuration -----------------------------------------------------*/ SpiHandle.Instance = LIS3DSH_SPI; SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE; SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW; SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; SpiHandle.Init.CRCPolynomial = 7; SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT; SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; SpiHandle.Init.NSS = SPI_NSS_SOFT; SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED; SpiHandle.Init.Mode = SPI_MODE_MASTER; /* SPI low level configuration */ /* Enable the SPI peripheral */ LIS3DSH_SPI_CLK_ENABLE(); /* Enable SCK, MOSI and MISO GPIO clocks */ LIS3DSH_SPI_GPIO_CLK_ENABLE(); /* SPI SCK, MOSI, MISO pin configuration */ GPIO_InitStructure.Pin = (LIS3DSH_SPI_SCK_PIN | LIS3DSH_SPI_MISO_PIN | LIS3DSH_SPI_MOSI_PIN); GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; GPIO_InitStructure.Pull = GPIO_PULLDOWN; GPIO_InitStructure.Speed = GPIO_SPEED_FAST; GPIO_InitStructure.Alternate = LIS3DSH_SPI_AF; HAL_GPIO_Init(LIS3DSH_SPI_GPIO_PORT, &GPIO_InitStructure); HAL_SPI_Init(&SpiHandle); } /* Configures the inner register of the accelerometer */ LIS3DSH_writeRegister(LIS3DSH_CTRL_REG4_ADDR, li.OutputDataRate | li.Axes_Enable | li.BlockDataUpdate); LIS3DSH_writeRegister(LIS3DSH_CTRL_REG5_ADDR, li.FullScale); }