1 Star 0 Fork 0

NEO/ADIS16505

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
platform_specific_impl.c 7.34 KB
一键复制 编辑 原始数据 按行查看 历史
/**
* @file platform_specific_impl.c
* @brief Source file for platform-specific implementations of the ADIS16505 driver.
*
* This source file contains the implementations of functions and types declared in
* platform_specific_impl.h. It includes platform-specific code such as SPI communication,
* delay functions, and any other hardware-dependent operations required by the ADIS16505 driver.
*
* @note When porting the driver to a new platform, ensure to provide the appropriate
* platform-specific implementations in this file.
*
* @author Neo
* @date March, 2024
* @version V1.0
*
* @see platform_specific_impl.h for function declarations and type definitions.
*/
#include "platform_specific_impl.h"
#ifdef PLATFORM_STM32_USE_HAL
#define SPI_ADIS16505_TIMEOUT 0x00FF // flexible
extern SPI_HandleTypeDef SPI_ADIS16505_HANDLE; // hspi2
extern TIM_HandleTypeDef DLY_US_TIM_HANDLE; // htim11
/*** delay functions ***/
// no precision requirements
void delay_ms(uint32_t nms)
{
HAL_Delay(nms-1);
}
void delay_us(uint16_t nus)
{
__HAL_TIM_SET_COUNTER(&DLY_US_TIM_HANDLE, 0);
__HAL_TIM_ENABLE(&DLY_US_TIM_HANDLE);
while (__HAL_TIM_GET_COUNTER(&DLY_US_TIM_HANDLE) < nus)
{
}
__HAL_TIM_DISABLE(&DLY_US_TIM_HANDLE);
}
/*** basic spi operation ***/
/*
* @brief write word (16bits) through SPI and read reply
* @param word: word to write
* @param pRxData: pointer to reception data buffer
* @return 0 for success, 1 for fail
* */
uint8_t SPI_ADIS16505_WR_Word(const uint16_t word, uint16_t* pRxData)
{
SPI_HandleTypeDef *hspi = &SPI_ADIS16505_HANDLE;
// wait SPI serial free
while (HAL_SPI_GetState(hspi) == HAL_SPI_STATE_BUSY_TX_RX)
;
#ifndef SPI_ADIS16505_DATASIZE_8BIT // SPI_ADIS16505_DATASIZE_16BIT as default
if (HAL_SPI_TransmitReceive(hspi, (uint8_t*)&word, (uint8_t*)pRxData, 1, SPI_ADIS16505_TIMEOUT) != HAL_OK)
return 1;
#else
uint8_t txBuf[2] = {word >> 8, word & 0xFF};
uint8_t rxBuf[2] = {0, };
if (HAL_SPI_TransmitReceive(hspi, txBuf, rxBuf, 2, SPI_ADIS16505_TIMEOUT) != HAL_OK)
return 1;
pRxData[0] = (rxBuf[0] << 8) | rxBuf[1];
#endif /* SPI_ADIS16505_DATASIZE_8BIT */
return 0;
}
/*
* @brief write word (16bits) through SPI and read reply
* @param pTxData: pointer to words to be written
* @param pRxData: pointer to reception data buffer
* @param num: amount of data to be sent and received
* @return 0 for success, 1 for fail
* */
uint8_t SPI_ADIS16505_WR_Words(const uint16_t* pTxData, uint16_t* pRxData, uint16_t num)
{
SPI_HandleTypeDef *hspi = &SPI_ADIS16505_HANDLE;
// wait SPI serial free
while (HAL_SPI_GetState(hspi) == HAL_SPI_STATE_BUSY_TX_RX)
;
#ifndef SPI_ADIS16505_DATASIZE_8BIT // SPI_ADIS16505_DATASIZE_16BIT as default
if (HAL_SPI_TransmitReceive(hspi, (uint8_t*)pTxData, (uint8_t*)pRxData, num, SPI_ADIS16505_TIMEOUT) != HAL_OK)
return 1;
#else
uint8_t* txBuf = (uint8_t *)malloc(num * sizeof(uint16_t));
uint8_t* rxBuf = (uint8_t *)malloc(num * sizeof(uint16_t));
if (txBuf == NULL || rxBuf == NULL)
return 1; // memory allocation failed
for(uint8_t i = 0; i < num; i++)
{
txBuf[2*i] = pTxData[i] >> 8;
txBuf[2*i+1] = pTxData[i] & 0xFF;
}
if (HAL_SPI_TransmitReceive(hspi, txBuf, rxBuf, 2*num, SPI_ADIS16505_TIMEOUT) != HAL_OK)
return 1;
for(uint8_t i = 0; i < num; i++)
{
pRxData[i] = (rxBuf[2*i] << 8) | rxBuf[2*i+1];
}
free(txBuf);
free(rxBuf);
#endif /* SPI_ADIS16505_DATASIZE_8BIT */
return 0;
}
#ifdef ADIS16505_USE_HARDWARE_RST
/**
* @brief Performs a hardware reset on the ADIS16505 sensor.
*
* This function triggers a hardware reset sequence for the ADIS16505 sensor,
* ensuring that the sensor is reinitialized and returns to its default state.
* It is typically used to recover from error conditions or to reinitialize
* the sensor during startup.
*/
void ADIS16505_HardwareReset()
{
HAL_GPIO_WritePin(RSTn_ADIS16505_GPIO_PORT, RSTn_ADIS16505_PIN, GPIO_PIN_RESET);
delay_us(20); // be in a low stage for at least 10us to ensure a proper reset initiation and recovery
HAL_GPIO_WritePin(RSTn_ADIS16505_GPIO_PORT, RSTn_ADIS16505_PIN, GPIO_PIN_SET);
delay_ms(300); // reset revocery time: 255ms
}
#endif /* RSTn_ADIS16505_GPIO_PORT && RSTn_ADIS16505_PIN */
#elif defined PLATFORM_LINUX_USE_MRAA
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
/* mraa header */
#include "mraa/spi.h"
#include "mraa/gpio.h"
extern mraa_spi_context SPI_ADIS16505;
/*** delay functions ***/
// no precision requirements
void delay_ms(uint32_t nms)
{
for(int i=0; i < 1000; i++)
{
usleep(nms);
}
}
void delay_us(uint16_t nus)
{
usleep(nus);
}
/*** basic spi operation ***/
/*
* @brief write word (16bits) through SPI and read reply
* @param word: word to write
* @param pRxData: pointer to reception data buffer
* @return 0 for success, 1 for fail
* */
uint8_t SPI_ADIS16505_WR_Word(const uint16_t word, uint16_t* pRxData)
{
#ifndef SPI_ADIS16505_DATASIZE_8BIT // SPI_ADIS16505_DATASIZE_16BIT as default
if (mraa_spi_transfer_buf_word(SPI_ADIS16505, &word, pRxData, 2) != MRAA_SUCCESS)
return 1;
#else
uint8_t txBuf[2] = {word >> 8, word & 0xFF};
uint8_t rxBuf[2] = {0, };
if (mraa_spi_transfer_buf(SPI_ADIS16505, txBuf, rxBuf, 2) != MRAA_SUCCESS)
return 1;
pRxData[0] = (rxBuf[0] << 8) | rxBuf[1];
#endif /* SPI_ADIS16505_DATASIZE_8BIT */
return 0;
}
/*
* @brief write word (16bits) through SPI and read reply
* @param pTxData: pointer to words to be written
* @param pRxData: pointer to reception data buffer
* @param num: amount of data to be sent and received
* @return 0 for success, 1 for fail
* */
uint8_t SPI_ADIS16505_WR_Words(const uint16_t* pTxData, uint16_t* pRxData, uint16_t num)
{
#ifndef SPI_ADIS16505_DATASIZE_8BIT // SPI_ADIS16505_DATASIZE_16BIT as default
if (mraa_spi_transfer_buf_word(SPI_ADIS16505, pTxData, pRxData, 2*num) != MRAA_SUCCESS)
return 1;
#else
uint8_t* txBuf = (uint8_t *)malloc(num * sizeof(uint16_t));
uint8_t* rxBuf = (uint8_t *)malloc(num * sizeof(uint16_t));
if (txBuf == NULL || rxBuf == NULL)
return 1; // memory allocation failed
for(uint8_t i = 0; i < num; i++)
{
txBuf[2*i] = pTxData[i] >> 8;
txBuf[2*i+1] = pTxData[i] & 0xFF;
}
if (mraa_spi_transfer_buf(SPI_ADIS16505, txBuf, rxBuf, 2*num) != MRAA_SUCCESS)
return 1;
for(uint8_t i = 0; i < num; i++)
{
pRxData[i] = (rxBuf[2*i] << 8) | rxBuf[2*i+1];
}
free(txBuf);
free(rxBuf);
#endif /* SPI_ADIS16505_DATASIZE_8BIT */
return 0;
}
#ifdef ADIS16505_USE_HARDWARE_RST
/**
* @brief Performs a hardware reset on the ADIS16505 sensor.
*
* This function triggers a hardware reset sequence for the ADIS16505 sensor,
* ensuring that the sensor is reinitialized and returns to its default state.
* It is typically used to recover from error conditions or to reinitialize
* the sensor during startup.
*/
void ADIS16505_HardwareReset()
{
mraa_gpio_write(RSTn_ADIS16505, 0);
delay_us(20); // be in a low stage for at least 10us to ensure a proper reset initiation and recovery
mraa_gpio_write(RSTn_ADIS16505, 1);
delay_ms(300); // reset revocery time: 255ms
}
#endif /* RSTn_ADIS16505 */
#endif /* PLATFORM MACRO */
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qCwCp/ADIS16505.git
git@gitee.com:qCwCp/ADIS16505.git
qCwCp
ADIS16505
ADIS16505
main

搜索帮助