IMU_DUAL/ASM330LHH/user_ams330lhh.c

55 lines
1.3 KiB
C
Raw Normal View History

2022-12-06 17:34:02 +08:00
#include "user_asm330lhh.h"
stmdev_ctx_t spi1_dev ;
extern SPI_HandleTypeDef hspi1;
static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,uint16_t len)
{
GPIO_TypeDef *GPIOx;
uint16_t GPIO_Pin;
if(handle == &hspi1)
{
GPIOx = SPI1_CS_PORT;
GPIO_Pin= SPI1_CS_PIN;
}
HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(handle, &reg, 1, 1000);
HAL_SPI_Transmit(handle, (uint8_t*) bufp, len, 1000);
HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_SET);
return 0;
}
static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,uint16_t len)
{
uint8_t temp=reg;
temp |= 0x80;
// GPIO_TypeDef *GPIOx;
// uint16_t GPIO_Pin;
// if (handle == &hspi1) {
// GPIOx = SPI1_CS_PORT;
// GPIO_Pin = SPI1_CS_PIN;
// }
// HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_RESET);
// HAL_SPI_Transmit(handle, &temp, 1, 1000);
// HAL_SPI_Receive(handle, bufp, len, 1000);
// HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
HAL_SPI_Transmit(handle, &temp, 1, 1000);
HAL_SPI_Receive(handle, bufp, len, 1000);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
return 0;
}
void user_dev_init()
{
spi1_dev.handle=&hspi1;
spi1_dev.mdelay=HAL_Delay;
spi1_dev.write_reg=platform_write;
spi1_dev.read_reg=platform_read;
}