polarimeter_software/User/board/bsp_temper_sampling.c

141 lines
3.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @Author: mypx
* @Date: 2025-10-15 15:32:36
* @LastEditTime: 2025-10-16 10:26:19
* @LastEditors: mypx mypx_coder@163.com
* @Description:
*/
#include "bsp_temper_sampling.h"
#include "et_log.h"
#include "spi.h"
#if (USING_SOFT_SPI == 1)
soft_spi_t soft_spi = {
.sck_port = GPIOB,
.sck_pin = GPIO_PIN_5,
.mosi_port = GPIOB,
.mosi_pin = GPIO_PIN_6,
.miso_port = GPIOB,
.miso_pin = GPIO_PIN_4,
.cs_port = GPIOB,
.cs_pin = GPIO_PIN_7,
.mode = SOFT_SPI_MODE0,
.bit_order = SOFT_SPI_MSB_FIRST,
.lock = RT_NULL, // 使用 RT_NULL初始化时会创建
.delay_us = 1, // 每半个 SPI 时钟周期延时 1 微秒
};
void bsp_ad7793_spi_transfer(uint8_t *tx_buf, uint8_t *rx_buf, uint16_t len)
{
soft_spi_transfer_buffer(&soft_spi, tx_buf, rx_buf, len);
}
void bsp_ad7793_spi_write(uint8_t cmd, uint8_t *data, uint16_t len)
{
soft_spi_write(&soft_spi, &cmd, 1, data, len);
}
void bsp_ad7793_spi_read(uint8_t cmd, uint8_t *rx_buf, uint16_t len)
{
soft_spi_read(&soft_spi, &cmd, 1, rx_buf, len);
}
void bsp_ad7793_gpio_set(uint8_t pin, bool state)
{
if (pin == soft_spi.cs_pin)
{
HAL_GPIO_WritePin(soft_spi.cs_port, soft_spi.cs_pin, state);
}
}
bool bsp_ad7793_gpio_get(uint8_t pin)
{
if (pin == soft_spi.miso_pin)
{
return HAL_GPIO_ReadPin(soft_spi.miso_port, soft_spi.miso_pin) == GPIO_PIN_SET;
}
return false;
}
#else
#define AD7793_CS_LOW() HAL_GPIO_WritePin(SPI3_AD7793_CS_GPIO_Port, SPI3_AD7793_CS_Pin, GPIO_PIN_RESET)
#define AD7793_CS_HIGH() HAL_GPIO_WritePin(SPI3_AD7793_CS_GPIO_Port, SPI3_AD7793_CS_Pin, GPIO_PIN_SET)
void bsp_ad7793_spi_transfer(uint8_t *tx_buf, uint8_t *rx_buf, uint16_t len)
{
HAL_StatusTypeDef status;
status = HAL_SPI_TransmitReceive(&AD7793_SPI, tx_buf, rx_buf, len, 1000);
if (status != HAL_OK)
{
ET_ERR("SPI transfer error:%d!", status);
}
}
void bsp_ad7793_spi_write(uint8_t cmd, uint8_t *data, uint16_t len)
{
uint8_t tx_buffer[256];
HAL_StatusTypeDef status;
tx_buffer[0] = cmd;
if (data && len > 0)
{
for (uint16_t i = 0; i < len; i++)
{
tx_buffer[i + 1] = data[i];
}
}
status = HAL_SPI_Transmit(&AD7793_SPI, tx_buffer, len + 1, 1000);
if (status != HAL_OK)
{
ET_ERR("SPI write error:%d!", status);
}
}
void bsp_ad7793_spi_read(uint8_t cmd, uint8_t *rx_buf, uint16_t len)
{
uint8_t tx_buffer[1] = {cmd};
HAL_StatusTypeDef status;
status = HAL_SPI_Transmit(&AD7793_SPI, tx_buffer, 1, 1000);
if (status != HAL_OK)
{
ET_ERR("SPI write error:%d!", status);
}
status = HAL_SPI_Receive(&AD7793_SPI, rx_buf, len, 1000);
if (status != HAL_OK)
{
ET_ERR("SPI read error:%d!", status);
}
}
void bsp_ad7793_gpio_set(uint8_t pin, bool state)
{
if (pin == SPI3_AD7793_CS_Pin)
{
HAL_GPIO_WritePin(SPI3_AD7793_CS_GPIO_Port, SPI3_AD7793_CS_Pin, state);
}
}
bool bsp_ad7793_gpio_get(uint8_t pin)
{
if (pin == SPI3_AD7793_MISO_Pin)
{
return HAL_GPIO_ReadPin(SPI3_AD7793_MISO_GPIO_Port, SPI3_AD7793_MISO_Pin) == GPIO_PIN_SET;
}
return false;
}
#endif
void bsp_ad7793_delay_ms(uint16_t ms)
{
rt_thread_mdelay(ms); // 使用 RT-Thread 的延时函数
}
void bsp_temper_sampling_init(void)
{
#if (USING_SOFT_SPI == 1)
soft_spi_init(&soft_spi);
soft_spi_set_mode(&soft_spi, SOFT_SPI_MODE3); // 设置 SPI 模式
soft_spi_set_bit_order(&soft_spi, SOFT_SPI_MSB_FIRST); // 设置位序
soft_spi_set_delay(&soft_spi, 1); // 设置延时为 1 微秒
soft_spi_select(&soft_spi); // 选择 SPI 设备
#endif
}