59 lines
1.5 KiB
C
59 lines
1.5 KiB
C
/*
|
|
* CS43L22.c
|
|
*
|
|
* Created on: Apr 3, 2021
|
|
* Author: Think
|
|
*/
|
|
#include "CS43L22.h"
|
|
|
|
//CS43L22 I2C address
|
|
static const uint8_t CS43L22_ADDR = 0x4A << 1;
|
|
|
|
//CS43L22 register definition
|
|
static const uint8_t _ID = 0x01;
|
|
|
|
//Driver function definition
|
|
bool CS43L22_Init(CS43L22 *device, I2C_HandleTypeDef *i2cHandler)
|
|
{
|
|
device->i2cHandler = i2cHandler;
|
|
|
|
//We check that the reset pin is not low...
|
|
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_SET);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool CS43L22_Reset(CS43L22 *device)
|
|
{
|
|
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_RESET);
|
|
HAL_Delay(50);
|
|
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_SET);
|
|
return true;
|
|
}
|
|
|
|
bool CS43L22_GetDeviceID(CS43L22 *device, uint8_t *chipID, uint8_t *chipREVID)
|
|
{
|
|
uint8_t data;
|
|
if(!CS43L22_ReadRegister(device, ID, &data))
|
|
return false;
|
|
|
|
*chipID = data >> 3;
|
|
*chipREVID = data & 0x07;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool CS43L22_ReadRegister(CS43L22 *device, uint8_t registerAddr, uint8_t *data)
|
|
{
|
|
HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(device->i2cHandler, CS43L22_ADDR, ®isterAddr, 1, HAL_MAX_DELAY);
|
|
if(status != HAL_OK)
|
|
return false;
|
|
return HAL_I2C_Master_Receive(device->i2cHandler, CS43L22_ADDR, data, 1, HAL_MAX_DELAY) == HAL_OK ? true : false;
|
|
}
|
|
|
|
bool CS43L22_WriteRegister(CS43L22 *device, uint8_t registerAddr, uint8_t data)
|
|
{
|
|
uint8_t regAndData[] = {registerAddr, data};
|
|
return HAL_I2C_Master_Transmit(device->i2cHandler, CS43L22_ADDR, regAndData, 2, HAL_MAX_DELAY) == HAL_OK ? true : false;
|
|
}
|