可查到AD7124的ID其他寄存器待测试
This commit is contained in:
parent
2639e18c9f
commit
4f5c836753
|
@ -1,6 +1,10 @@
|
||||||
{
|
{
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
"netif.h": "c",
|
"netif.h": "c",
|
||||||
"tcpserverc.h": "c"
|
"tcpserverc.h": "c",
|
||||||
|
"user_spi.h": "c",
|
||||||
|
"spi.h": "c",
|
||||||
|
"ad7124.h": "c",
|
||||||
|
"freertos.h": "c"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -193,6 +193,16 @@ extern "C"
|
||||||
#define AD7124_SYNC_Pin GPIO_PIN_1
|
#define AD7124_SYNC_Pin GPIO_PIN_1
|
||||||
#define AD7124_SYNC_GPIO_Port GPIOE
|
#define AD7124_SYNC_GPIO_Port GPIOE
|
||||||
|
|
||||||
|
/* SPI2引脚定义 */
|
||||||
|
#define SPI2_SCK_Pin GPIO_PIN_10
|
||||||
|
#define SPI2_SCK_GPIO_Port GPIOB
|
||||||
|
#define SPI2_MISO_Pin GPIO_PIN_2
|
||||||
|
#define SPI2_MISO_GPIO_Port GPIOC
|
||||||
|
#define SPI2_MOSI_Pin GPIO_PIN_3
|
||||||
|
#define SPI2_MOSI_GPIO_Port GPIOC
|
||||||
|
#define SPI2_CS_Pin GPIO_PIN_10
|
||||||
|
#define SPI2_CS_GPIO_Port GPIOD
|
||||||
|
|
||||||
/* USER CODE BEGIN Private defines */
|
/* USER CODE BEGIN Private defines */
|
||||||
#define TRUE 0
|
#define TRUE 0
|
||||||
#define FAIL -1
|
#define FAIL -1
|
||||||
|
|
|
@ -37,6 +37,10 @@
|
||||||
#include "user_gpio.h"
|
#include "user_gpio.h"
|
||||||
#include "linear_encoder.h"
|
#include "linear_encoder.h"
|
||||||
#include "lan8742.h"
|
#include "lan8742.h"
|
||||||
|
#include "ad7124_test.h"
|
||||||
|
|
||||||
|
// 声明外部变量
|
||||||
|
extern ad7124_analog_t ad7124_analog[AD7124_CHANNEL_EN_MAX];
|
||||||
/* USER CODE END Includes */
|
/* USER CODE END Includes */
|
||||||
|
|
||||||
/* Private typedef -----------------------------------------------------------*/
|
/* Private typedef -----------------------------------------------------------*/
|
||||||
|
@ -64,6 +68,7 @@ osThreadId dac_taskHandle;
|
||||||
osThreadId adc_taskHandle;
|
osThreadId adc_taskHandle;
|
||||||
osThreadId gpio_di_do_taskHandle;
|
osThreadId gpio_di_do_taskHandle;
|
||||||
osThreadId ec11_taskHandle;
|
osThreadId ec11_taskHandle;
|
||||||
|
osThreadId ad7124_test_taskHandle;
|
||||||
|
|
||||||
/* Private function prototypes -----------------------------------------------*/
|
/* Private function prototypes -----------------------------------------------*/
|
||||||
/* USER CODE BEGIN FunctionPrototypes */
|
/* USER CODE BEGIN FunctionPrototypes */
|
||||||
|
@ -86,6 +91,7 @@ void start_dac_task(void const *argument);
|
||||||
void start_adc_task(void const *argument);
|
void start_adc_task(void const *argument);
|
||||||
void start_gpio_di_do_task(void const *argument);
|
void start_gpio_di_do_task(void const *argument);
|
||||||
void start_ec11_task(void const *argument);
|
void start_ec11_task(void const *argument);
|
||||||
|
void start_ad7124_test_task(void const *argument);
|
||||||
|
|
||||||
extern void MX_LWIP_Init(void);
|
extern void MX_LWIP_Init(void);
|
||||||
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
|
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
|
||||||
|
@ -139,24 +145,28 @@ void MX_FREERTOS_Init(void)
|
||||||
lwip_taskHandle = osThreadCreate(osThread(lwip_task), NULL);
|
lwip_taskHandle = osThreadCreate(osThread(lwip_task), NULL);
|
||||||
|
|
||||||
/* definition and creation of led_task */
|
/* definition and creation of led_task */
|
||||||
osThreadDef(led_task, start_led_toggle_task, osPriorityLow, 0, 128);
|
// osThreadDef(led_task, start_led_toggle_task, osPriorityLow, 0, 128);
|
||||||
led_taskHandle = osThreadCreate(osThread(led_task), NULL);
|
// led_taskHandle = osThreadCreate(osThread(led_task), NULL);
|
||||||
|
|
||||||
/* definition and creation of dac_task */
|
/* definition and creation of dac_task */
|
||||||
osThreadDef(dac_task, start_dac_task, osPriorityNormal, 0, 512);
|
// osThreadDef(dac_task, start_dac_task, osPriorityNormal, 0, 512);
|
||||||
dac_taskHandle = osThreadCreate(osThread(dac_task), NULL);
|
// dac_taskHandle = osThreadCreate(osThread(dac_task), NULL);
|
||||||
|
|
||||||
/* definition and creation of adc_task */
|
/* definition and creation of adc_task */
|
||||||
osThreadDef(adc_task, start_adc_task, osPriorityBelowNormal, 0, 128);
|
// osThreadDef(adc_task, start_adc_task, osPriorityBelowNormal, 0, 128);
|
||||||
adc_taskHandle = osThreadCreate(osThread(adc_task), NULL);
|
// adc_taskHandle = osThreadCreate(osThread(adc_task), NULL);
|
||||||
|
|
||||||
/* definition and creation of gpio_di_do_task */
|
/* definition and creation of gpio_di_do_task */
|
||||||
osThreadDef(gpio_di_do_task, start_gpio_di_do_task, osPriorityNormal, 0, 128);
|
// osThreadDef(gpio_di_do_task, start_gpio_di_do_task, osPriorityNormal, 0, 128);
|
||||||
gpio_di_do_taskHandle = osThreadCreate(osThread(gpio_di_do_task), NULL);
|
// gpio_di_do_taskHandle = osThreadCreate(osThread(gpio_di_do_task), NULL);
|
||||||
|
|
||||||
/* definition and creation of ec11_task */
|
/* definition and creation of ec11_task */
|
||||||
osThreadDef(ec11_task, start_ec11_task, osPriorityNormal, 0, 512);
|
// osThreadDef(ec11_task, start_ec11_task, osPriorityNormal, 0, 512);
|
||||||
ec11_taskHandle = osThreadCreate(osThread(ec11_task), NULL);
|
// ec11_taskHandle = osThreadCreate(osThread(ec11_task), NULL);
|
||||||
|
|
||||||
|
/* definition and creation of ad7124_test_task */
|
||||||
|
osThreadDef(ad7124_test_task, start_ad7124_test_task, osPriorityNormal, 0, 512);
|
||||||
|
ad7124_test_taskHandle = osThreadCreate(osThread(ad7124_test_task), NULL);
|
||||||
|
|
||||||
/* USER CODE BEGIN RTOS_THREADS */
|
/* USER CODE BEGIN RTOS_THREADS */
|
||||||
/* add threads, ... */
|
/* add threads, ... */
|
||||||
|
@ -271,7 +281,7 @@ void start_dac_task(void const *argument)
|
||||||
/* Infinite loop */
|
/* Infinite loop */
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
osThreadSuspend(adc_taskHandle); // 暂停ADC任务,防止DAC采集时产生干<EFBFBD>????????,因为ADC和DAC采用的是同一路SPI,但是时序不<EFBFBD>????????
|
osThreadSuspend(adc_taskHandle); // 暂停ADC任务,防止DAC采集时产生干????????,因为ADC和DAC采用的是同一路SPI,但是时序不????????
|
||||||
dac161s997_output(DAC161S997_1, current_buff[0]);
|
dac161s997_output(DAC161S997_1, current_buff[0]);
|
||||||
dac161s997_output(DAC161S997_2, current_buff[1]);
|
dac161s997_output(DAC161S997_2, current_buff[1]);
|
||||||
osThreadResume(adc_taskHandle);
|
osThreadResume(adc_taskHandle);
|
||||||
|
@ -294,7 +304,7 @@ void start_adc_task(void const *argument)
|
||||||
/* Infinite loop */
|
/* Infinite loop */
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
osThreadSuspend(dac_taskHandle); // 暂停DAC任务,防止ADC采集时产生干<EFBFBD>????????,因为ADC和DAC采用的是同一路SPI,但是时序不<EFBFBD>????????
|
osThreadSuspend(dac_taskHandle); // 暂停DAC任务,防止ADC采集时产生干????????,因为ADC和DAC采用的是同一路SPI,但是时序不????????
|
||||||
uint8_t ch = 0;
|
uint8_t ch = 0;
|
||||||
for (ch = STOP_NC_ADC; ch < AD7124_CHANNEL_EN_MAX; ch++)
|
for (ch = STOP_NC_ADC; ch < AD7124_CHANNEL_EN_MAX; ch++)
|
||||||
{
|
{
|
||||||
|
@ -347,6 +357,106 @@ void start_ec11_task(void const *argument)
|
||||||
/* USER CODE END start_ec11_task */
|
/* USER CODE END start_ec11_task */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* USER CODE BEGIN Header_start_ad7124_test_task */
|
||||||
|
/**
|
||||||
|
* @brief Function implementing the ad7124_test_task thread.
|
||||||
|
* @param argument: Not used
|
||||||
|
* @retval None
|
||||||
|
*/
|
||||||
|
/* USER CODE END Header_start_ad7124_test_task */
|
||||||
|
|
||||||
|
// 添加全局变量用于Watch窗口观察
|
||||||
|
volatile int32_t g_adc_data = 0;
|
||||||
|
volatile int32_t g_adc_error = 0;
|
||||||
|
volatile uint8_t g_adc_state = 0; // 0:初始化 1:配置通道 2:等待转换 3:读取数据 4:组包 5:发送
|
||||||
|
volatile uint16_t g_channel_config = 0; // 用于观察通道配置值
|
||||||
|
volatile uint16_t g_channel_status = 0; // 用于观察通道状态
|
||||||
|
volatile uint16_t g_adc_control = 0; // 用于观察ADC控制寄存器
|
||||||
|
volatile uint16_t g_config_0 = 0; // 用于观察配置寄存器0
|
||||||
|
volatile uint32_t g_filter_0 = 0; // 用于观察滤波器配置
|
||||||
|
extern int32_t g_ad7124_id;
|
||||||
|
|
||||||
|
void start_ad7124_test_task(const void *argument)
|
||||||
|
{
|
||||||
|
int32_t ret;
|
||||||
|
|
||||||
|
// 等待系统稳定
|
||||||
|
vTaskDelay(1000);
|
||||||
|
g_adc_state = 0;
|
||||||
|
|
||||||
|
// 初始化AD7124
|
||||||
|
ret = ad7124_setup();
|
||||||
|
|
||||||
|
|
||||||
|
// 读取关键寄存器值
|
||||||
|
ret = ad7124_read_register(&ad7124_regs[AD7124_ADC_CONTROL]);
|
||||||
|
if (ret >= 0) {
|
||||||
|
g_adc_control = ad7124_regs[AD7124_ADC_CONTROL].value;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = ad7124_read_register(&ad7124_regs[AD7124_CONFIG_0]);
|
||||||
|
if (ret >= 0) {
|
||||||
|
g_config_0 = ad7124_regs[AD7124_CONFIG_0].value;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = ad7124_read_register(&ad7124_regs[AD7124_FILTER_0]);
|
||||||
|
if (ret >= 0) {
|
||||||
|
g_filter_0 = ad7124_regs[AD7124_FILTER_0].value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 只测试通道1
|
||||||
|
uint8_t ch = 0; // 假设通道1索引为0,如有不同请调整
|
||||||
|
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
// 配置通道1
|
||||||
|
g_adc_state = 1;
|
||||||
|
g_channel_config = ad7124_channel_regs[ch].value; // 记录通道配置值
|
||||||
|
ad7124_regs[AD7124_CHANNEL_0].value = ad7124_channel_regs[ch].value;
|
||||||
|
ret = ad7124_write_register(&ad7124_regs[AD7124_CHANNEL_0]);
|
||||||
|
if (ret < 0) {
|
||||||
|
g_adc_error = ret;
|
||||||
|
vTaskDelay(500);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取通道状态寄存器
|
||||||
|
ret = ad7124_read_register(&ad7124_regs[AD7124_STATUS]);
|
||||||
|
if (ret >= 0) {
|
||||||
|
g_channel_status = ad7124_regs[AD7124_STATUS].value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 等待转换完成,设置较短的超时时间
|
||||||
|
g_adc_state = 2;
|
||||||
|
ret = ad7124_wait_for_conv_ready(100); // 100ms超时
|
||||||
|
if (ret < 0) {
|
||||||
|
g_adc_error = ret;
|
||||||
|
vTaskDelay(500);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取数据
|
||||||
|
g_adc_state = 3;
|
||||||
|
g_adc_data = ad7124_read_data();
|
||||||
|
g_adc_error = 0; // 清除错误标志
|
||||||
|
|
||||||
|
g_adc_state = 4;
|
||||||
|
// 组包:4字节大端
|
||||||
|
uint8_t tx_buf[4];
|
||||||
|
tx_buf[0] = (g_adc_data >> 24) & 0xFF;
|
||||||
|
tx_buf[1] = (g_adc_data >> 16) & 0xFF;
|
||||||
|
tx_buf[2] = (g_adc_data >> 8) & 0xFF;
|
||||||
|
tx_buf[3] = g_adc_data & 0xFF;
|
||||||
|
|
||||||
|
g_adc_state = 5;
|
||||||
|
// 发送数据
|
||||||
|
//user_send_data_control(tx_buf, 4);
|
||||||
|
|
||||||
|
// 固定延时
|
||||||
|
vTaskDelay(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Private application code --------------------------------------------------*/
|
/* Private application code --------------------------------------------------*/
|
||||||
/* USER CODE BEGIN Application */
|
/* USER CODE BEGIN Application */
|
||||||
|
|
||||||
|
|
|
@ -125,6 +125,31 @@ void MX_GPIO_Init(void)
|
||||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||||
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||||
|
|
||||||
|
/* Configure SPI2 pins */
|
||||||
|
// SCK (PB10)
|
||||||
|
GPIO_InitStruct.Pin = SPI2_SCK_Pin;
|
||||||
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||||
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||||
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||||
|
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
|
||||||
|
HAL_GPIO_Init(SPI2_SCK_GPIO_Port, &GPIO_InitStruct);
|
||||||
|
|
||||||
|
// MISO (PC2)
|
||||||
|
GPIO_InitStruct.Pin = SPI2_MISO_Pin;
|
||||||
|
HAL_GPIO_Init(SPI2_MISO_GPIO_Port, &GPIO_InitStruct);
|
||||||
|
|
||||||
|
// MOSI (PC3)
|
||||||
|
GPIO_InitStruct.Pin = SPI2_MOSI_Pin;
|
||||||
|
HAL_GPIO_Init(SPI2_MOSI_GPIO_Port, &GPIO_InitStruct);
|
||||||
|
|
||||||
|
// CS (PD10) - 配置为输出
|
||||||
|
GPIO_InitStruct.Pin = SPI2_CS_Pin;
|
||||||
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||||
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||||
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||||
|
HAL_GPIO_Init(SPI2_CS_GPIO_Port, &GPIO_InitStruct);
|
||||||
|
HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_SET); // 初始状态不选中
|
||||||
|
|
||||||
/*Configure GPIO pins : PCPin PCPin */
|
/*Configure GPIO pins : PCPin PCPin */
|
||||||
GPIO_InitStruct.Pin = DI_CH1_Pin|DI_CH2_Pin;
|
GPIO_InitStruct.Pin = DI_CH1_Pin|DI_CH2_Pin;
|
||||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||||
|
|
|
@ -35,6 +35,7 @@
|
||||||
#include "ht1200m.h"
|
#include "ht1200m.h"
|
||||||
#include "uart_lcd.h"
|
#include "uart_lcd.h"
|
||||||
#include "user_flash.h"
|
#include "user_flash.h"
|
||||||
|
#include "user_spi.h"
|
||||||
/* USER CODE END Includes */
|
/* USER CODE END Includes */
|
||||||
|
|
||||||
/* Private typedef -----------------------------------------------------------*/
|
/* Private typedef -----------------------------------------------------------*/
|
||||||
|
@ -123,7 +124,7 @@ int main(void)
|
||||||
/* USER CODE END SysInit */
|
/* USER CODE END SysInit */
|
||||||
|
|
||||||
/* Initialize all configured peripherals */
|
/* Initialize all configured peripherals */
|
||||||
MX_GPIO_Init();
|
MX_GPIO_Init();
|
||||||
MX_DMA_Init();
|
MX_DMA_Init();
|
||||||
MX_TIM3_Init();
|
MX_TIM3_Init();
|
||||||
MX_SPI1_Init();
|
MX_SPI1_Init();
|
||||||
|
@ -136,7 +137,7 @@ int main(void)
|
||||||
MX_USART1_UART_Init();
|
MX_USART1_UART_Init();
|
||||||
MX_TIM8_Init();
|
MX_TIM8_Init();
|
||||||
/* USER CODE BEGIN 2 */
|
/* USER CODE BEGIN 2 */
|
||||||
|
ad7124_spi_init(); //初始化AD7124芯片的SPI2通讯口
|
||||||
/*DMA INIT*/
|
/*DMA INIT*/
|
||||||
// start
|
// start
|
||||||
HAL_UARTEx_ReceiveToIdle_DMA(&huart4, lcd_uart4.rx_data_temp, ARRAY_LEN(lcd_uart4.rx_data_temp));
|
HAL_UARTEx_ReceiveToIdle_DMA(&huart4, lcd_uart4.rx_data_temp, ARRAY_LEN(lcd_uart4.rx_data_temp));
|
||||||
|
|
|
@ -1,48 +0,0 @@
|
||||||
// File: STM32F405_415_407_417_427_437_429_439.dbgconf
|
|
||||||
// Version: 1.0.0
|
|
||||||
// Note: refer to STM32F405/415 STM32F407/417 STM32F427/437 STM32F429/439 reference manual (RM0090)
|
|
||||||
// refer to STM32F40x STM32F41x datasheets
|
|
||||||
// refer to STM32F42x STM32F43x datasheets
|
|
||||||
|
|
||||||
// <<< Use Configuration Wizard in Context Menu >>>
|
|
||||||
|
|
||||||
// <h> Debug MCU configuration register (DBGMCU_CR)
|
|
||||||
// <o.2> DBG_STANDBY <i> Debug Standby Mode
|
|
||||||
// <o.1> DBG_STOP <i> Debug Stop Mode
|
|
||||||
// <o.0> DBG_SLEEP <i> Debug Sleep Mode
|
|
||||||
// </h>
|
|
||||||
DbgMCU_CR = 0x00000007;
|
|
||||||
|
|
||||||
// <h> Debug MCU APB1 freeze register (DBGMCU_APB1_FZ)
|
|
||||||
// <i> Reserved bits must be kept at reset value
|
|
||||||
// <o.26> DBG_CAN2_STOP <i> CAN2 stopped when core is halted
|
|
||||||
// <o.25> DBG_CAN1_STOP <i> CAN2 stopped when core is halted
|
|
||||||
// <o.23> DBG_I2C3_SMBUS_TIMEOUT <i> I2C3 SMBUS timeout mode stopped when core is halted
|
|
||||||
// <o.22> DBG_I2C2_SMBUS_TIMEOUT <i> I2C2 SMBUS timeout mode stopped when core is halted
|
|
||||||
// <o.21> DBG_I2C1_SMBUS_TIMEOUT <i> I2C1 SMBUS timeout mode stopped when core is halted
|
|
||||||
// <o.12> DBG_IWDG_STOP <i> Independent watchdog stopped when core is halted
|
|
||||||
// <o.11> DBG_WWDG_STOP <i> Window watchdog stopped when core is halted
|
|
||||||
// <o.10> DBG_RTC_STOP <i> RTC stopped when core is halted
|
|
||||||
// <o.8> DBG_TIM14_STOP <i> TIM14 counter stopped when core is halted
|
|
||||||
// <o.7> DBG_TIM13_STOP <i> TIM13 counter stopped when core is halted
|
|
||||||
// <o.6> DBG_TIM12_STOP <i> TIM12 counter stopped when core is halted
|
|
||||||
// <o.5> DBG_TIM7_STOP <i> TIM7 counter stopped when core is halted
|
|
||||||
// <o.4> DBG_TIM6_STOP <i> TIM6 counter stopped when core is halted
|
|
||||||
// <o.3> DBG_TIM5_STOP <i> TIM5 counter stopped when core is halted
|
|
||||||
// <o.2> DBG_TIM4_STOP <i> TIM4 counter stopped when core is halted
|
|
||||||
// <o.1> DBG_TIM3_STOP <i> TIM3 counter stopped when core is halted
|
|
||||||
// <o.0> DBG_TIM2_STOP <i> TIM2 counter stopped when core is halted
|
|
||||||
// </h>
|
|
||||||
DbgMCU_APB1_Fz = 0x00000000;
|
|
||||||
|
|
||||||
// <h> Debug MCU APB2 freeze register (DBGMCU_APB2_FZ)
|
|
||||||
// <i> Reserved bits must be kept at reset value
|
|
||||||
// <o.18> DBG_TIM11_STOP <i> TIM11 counter stopped when core is halted
|
|
||||||
// <o.17> DBG_TIM10_STOP <i> TIM10 counter stopped when core is halted
|
|
||||||
// <o.16> DBG_TIM9_STOP <i> TIM9 counter stopped when core is halted
|
|
||||||
// <o.1> DBG_TIM8_STOP <i> TIM8 counter stopped when core is halted
|
|
||||||
// <o.0> DBG_TIM1_STOP <i> TIM1 counter stopped when core is halted
|
|
||||||
// </h>
|
|
||||||
DbgMCU_APB2_Fz = 0x00000000;
|
|
||||||
|
|
||||||
// <<< end of configuration section >>>
|
|
|
@ -149,6 +149,128 @@
|
||||||
</SetRegEntry>
|
</SetRegEntry>
|
||||||
</TargetDriverDllRegistry>
|
</TargetDriverDllRegistry>
|
||||||
<Breakpoint/>
|
<Breakpoint/>
|
||||||
|
<WatchWindow1>
|
||||||
|
<Ww>
|
||||||
|
<count>0</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>ad7124_analog[channel_nr],0x0A</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>1</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>ad7124_analog[channel_nr].voltage</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>2</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>ret</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>3</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>buffer[0]</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>4</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>wr_buf[0]</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>5</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>FINESH_ID</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>6</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>ret</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>7</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>g_adc_data</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>8</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>g_adc_error</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>9</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>g_adc_state</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>10</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>g_channel_config</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>11</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>g_channel_status</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>12</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>g_adc_control</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>13</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>g_config_0</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>14</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>g_filter_0</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>15</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>ad7124_regs[5].value</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>16</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>g_ad7124_id</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>17</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>buffer[1]</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>18</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>buffer[2]</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>19</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>buffer[3]</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>20</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>tx_buf[0]</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>21</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>tx_buf[1]</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>22</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>tx_buf[2]</ItemText>
|
||||||
|
</Ww>
|
||||||
|
<Ww>
|
||||||
|
<count>23</count>
|
||||||
|
<WinNumber>1</WinNumber>
|
||||||
|
<ItemText>tx_buf[3]</ItemText>
|
||||||
|
</Ww>
|
||||||
|
</WatchWindow1>
|
||||||
<Tracepoint>
|
<Tracepoint>
|
||||||
<THDelay>0</THDelay>
|
<THDelay>0</THDelay>
|
||||||
</Tracepoint>
|
</Tracepoint>
|
||||||
|
@ -1847,6 +1969,18 @@
|
||||||
<RteFlg>0</RteFlg>
|
<RteFlg>0</RteFlg>
|
||||||
<bShared>0</bShared>
|
<bShared>0</bShared>
|
||||||
</File>
|
</File>
|
||||||
|
<File>
|
||||||
|
<GroupNumber>11</GroupNumber>
|
||||||
|
<FileNumber>131</FileNumber>
|
||||||
|
<FileType>1</FileType>
|
||||||
|
<tvExp>0</tvExp>
|
||||||
|
<tvExpOptDlg>0</tvExpOptDlg>
|
||||||
|
<bDave2>0</bDave2>
|
||||||
|
<PathWithFileName>..\User\application\src\ad7124_test.c</PathWithFileName>
|
||||||
|
<FilenameWithoutPath>ad7124_test.c</FilenameWithoutPath>
|
||||||
|
<RteFlg>0</RteFlg>
|
||||||
|
<bShared>0</bShared>
|
||||||
|
</File>
|
||||||
</Group>
|
</Group>
|
||||||
|
|
||||||
<Group>
|
<Group>
|
||||||
|
@ -1857,7 +1991,7 @@
|
||||||
<RteFlg>0</RteFlg>
|
<RteFlg>0</RteFlg>
|
||||||
<File>
|
<File>
|
||||||
<GroupNumber>12</GroupNumber>
|
<GroupNumber>12</GroupNumber>
|
||||||
<FileNumber>131</FileNumber>
|
<FileNumber>132</FileNumber>
|
||||||
<FileType>1</FileType>
|
<FileType>1</FileType>
|
||||||
<tvExp>0</tvExp>
|
<tvExp>0</tvExp>
|
||||||
<tvExpOptDlg>0</tvExpOptDlg>
|
<tvExpOptDlg>0</tvExpOptDlg>
|
||||||
|
@ -1869,7 +2003,7 @@
|
||||||
</File>
|
</File>
|
||||||
<File>
|
<File>
|
||||||
<GroupNumber>12</GroupNumber>
|
<GroupNumber>12</GroupNumber>
|
||||||
<FileNumber>132</FileNumber>
|
<FileNumber>133</FileNumber>
|
||||||
<FileType>1</FileType>
|
<FileType>1</FileType>
|
||||||
<tvExp>0</tvExp>
|
<tvExp>0</tvExp>
|
||||||
<tvExpOptDlg>0</tvExpOptDlg>
|
<tvExpOptDlg>0</tvExpOptDlg>
|
||||||
|
@ -1881,7 +2015,7 @@
|
||||||
</File>
|
</File>
|
||||||
<File>
|
<File>
|
||||||
<GroupNumber>12</GroupNumber>
|
<GroupNumber>12</GroupNumber>
|
||||||
<FileNumber>133</FileNumber>
|
<FileNumber>134</FileNumber>
|
||||||
<FileType>1</FileType>
|
<FileType>1</FileType>
|
||||||
<tvExp>0</tvExp>
|
<tvExp>0</tvExp>
|
||||||
<tvExpOptDlg>0</tvExpOptDlg>
|
<tvExpOptDlg>0</tvExpOptDlg>
|
||||||
|
@ -1893,7 +2027,7 @@
|
||||||
</File>
|
</File>
|
||||||
<File>
|
<File>
|
||||||
<GroupNumber>12</GroupNumber>
|
<GroupNumber>12</GroupNumber>
|
||||||
<FileNumber>134</FileNumber>
|
<FileNumber>135</FileNumber>
|
||||||
<FileType>1</FileType>
|
<FileType>1</FileType>
|
||||||
<tvExp>0</tvExp>
|
<tvExp>0</tvExp>
|
||||||
<tvExpOptDlg>0</tvExpOptDlg>
|
<tvExpOptDlg>0</tvExpOptDlg>
|
||||||
|
@ -1913,7 +2047,7 @@
|
||||||
<RteFlg>0</RteFlg>
|
<RteFlg>0</RteFlg>
|
||||||
<File>
|
<File>
|
||||||
<GroupNumber>13</GroupNumber>
|
<GroupNumber>13</GroupNumber>
|
||||||
<FileNumber>135</FileNumber>
|
<FileNumber>136</FileNumber>
|
||||||
<FileType>1</FileType>
|
<FileType>1</FileType>
|
||||||
<tvExp>0</tvExp>
|
<tvExp>0</tvExp>
|
||||||
<tvExpOptDlg>0</tvExpOptDlg>
|
<tvExpOptDlg>0</tvExpOptDlg>
|
||||||
|
@ -1925,7 +2059,7 @@
|
||||||
</File>
|
</File>
|
||||||
<File>
|
<File>
|
||||||
<GroupNumber>13</GroupNumber>
|
<GroupNumber>13</GroupNumber>
|
||||||
<FileNumber>136</FileNumber>
|
<FileNumber>137</FileNumber>
|
||||||
<FileType>1</FileType>
|
<FileType>1</FileType>
|
||||||
<tvExp>0</tvExp>
|
<tvExp>0</tvExp>
|
||||||
<tvExpOptDlg>0</tvExpOptDlg>
|
<tvExpOptDlg>0</tvExpOptDlg>
|
||||||
|
@ -1937,7 +2071,7 @@
|
||||||
</File>
|
</File>
|
||||||
<File>
|
<File>
|
||||||
<GroupNumber>13</GroupNumber>
|
<GroupNumber>13</GroupNumber>
|
||||||
<FileNumber>137</FileNumber>
|
<FileNumber>138</FileNumber>
|
||||||
<FileType>1</FileType>
|
<FileType>1</FileType>
|
||||||
<tvExp>0</tvExp>
|
<tvExp>0</tvExp>
|
||||||
<tvExpOptDlg>0</tvExpOptDlg>
|
<tvExpOptDlg>0</tvExpOptDlg>
|
||||||
|
|
|
@ -1083,6 +1083,11 @@
|
||||||
<FileType>1</FileType>
|
<FileType>1</FileType>
|
||||||
<FilePath>..\User\application\src\user_lib.c</FilePath>
|
<FilePath>..\User\application\src\user_lib.c</FilePath>
|
||||||
</File>
|
</File>
|
||||||
|
<File>
|
||||||
|
<FileName>ad7124_test.c</FileName>
|
||||||
|
<FileType>1</FileType>
|
||||||
|
<FilePath>..\User\application\src\ad7124_test.c</FilePath>
|
||||||
|
</File>
|
||||||
</Files>
|
</Files>
|
||||||
</Group>
|
</Group>
|
||||||
<Group>
|
<Group>
|
||||||
|
|
Binary file not shown.
|
@ -0,0 +1,13 @@
|
||||||
|
#ifndef __AD7124_TEST_H
|
||||||
|
#define __AD7124_TEST_H
|
||||||
|
|
||||||
|
#include "FreeRTOS.h"
|
||||||
|
#include "task.h"
|
||||||
|
#include "ad7124.h"
|
||||||
|
#include "cmsis_os.h"
|
||||||
|
|
||||||
|
// 测试任务函数声明
|
||||||
|
//void ad7124_test_task(void *argument);
|
||||||
|
void ad7124_test_init(void);
|
||||||
|
|
||||||
|
#endif /* __AD7124_TEST_H */
|
|
@ -0,0 +1,10 @@
|
||||||
|
#include "ad7124_test.h"
|
||||||
|
#include "FreeRTOS.h"
|
||||||
|
#include "ad7124.h"
|
||||||
|
#include "task.h"
|
||||||
|
#include "main.h"
|
||||||
|
#include "usart.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "tcpserverc.h"
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include "communication_protocol.h"
|
#include "communication_protocol.h"
|
||||||
#include "user_gpio.h"
|
#include "user_gpio.h"
|
||||||
#include "tim.h"
|
#include "tim.h"
|
||||||
|
#include "ad7124.h"
|
||||||
struct tcp_pcb *server_pcb_hart1 = NULL;
|
struct tcp_pcb *server_pcb_hart1 = NULL;
|
||||||
struct tcp_pcb *server_pcb_hart2 = NULL;
|
struct tcp_pcb *server_pcb_hart2 = NULL;
|
||||||
struct tcp_pcb *server_pcb_ble1 = NULL;
|
struct tcp_pcb *server_pcb_ble1 = NULL;
|
||||||
|
@ -46,7 +47,9 @@ uint16_t data_len = 0; // 帧体长度
|
||||||
#define source_addr_l 0x02 // 源地址低
|
#define source_addr_l 0x02 // 源地址低
|
||||||
#define target_addr_h 0x0A // 目标地址高
|
#define target_addr_h 0x0A // 目标地址高
|
||||||
#define target_addr_l 0x0B // 目标地址低
|
#define target_addr_l 0x0B // 目标地址低
|
||||||
uint16_t reply_type = 0x00;// 报文类型_应答
|
#define Main_version_number 1.0 // 主版本号
|
||||||
|
#define Sub_version_number 0.0 // 子版本号
|
||||||
|
uint16_t reply_type = 0x00; // 报文类型_应答
|
||||||
uint8_t enable_data[7] = {
|
uint8_t enable_data[7] = {
|
||||||
0x03, // Byte 0: 蓝牙 + WIFI
|
0x03, // Byte 0: 蓝牙 + WIFI
|
||||||
0x00, // Byte 1: 预留
|
0x00, // Byte 1: 预留
|
||||||
|
@ -135,7 +138,12 @@ uint8_t calc_checksum(const uint8_t *data, uint8_t start, uint8_t end) {
|
||||||
return checksum;
|
return checksum;
|
||||||
}
|
}
|
||||||
|
|
||||||
// body: 报文体指针, body_len: 报文体长度, tx: 返回帧缓冲区, 返回帧总长度
|
|
||||||
|
/**
|
||||||
|
* @brief 处理类型为80复位的报文
|
||||||
|
*
|
||||||
|
* 根据传入的报文体和长度,构造一个特定格式的报文并返回报文的总长度。
|
||||||
|
*/
|
||||||
uint16_t handle_type_80(const uint8_t *body, uint16_t body_len, uint8_t *tx)
|
uint16_t handle_type_80(const uint8_t *body, uint16_t body_len, uint8_t *tx)
|
||||||
{
|
{
|
||||||
uint16_t total_len = 2 + 2 + 2 + 2 + 1 + body_len + 2; // 帧头+帧长+源+目标+类型+体+校验
|
uint16_t total_len = 2 + 2 + 2 + 2 + 1 + body_len + 2; // 帧头+帧长+源+目标+类型+体+校验
|
||||||
|
@ -167,6 +175,10 @@ uint16_t handle_type_80(const uint8_t *body, uint16_t body_len, uint8_t *tx)
|
||||||
tx[total_len - 1] = checksum & 0xFF;
|
tx[total_len - 1] = checksum & 0xFF;
|
||||||
return total_len;
|
return total_len;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* @brief 处理类型81版本号的数据包
|
||||||
|
*
|
||||||
|
*/
|
||||||
uint16_t handle_type_81(const uint8_t *body, uint16_t body_len, uint8_t *tx)
|
uint16_t handle_type_81(const uint8_t *body, uint16_t body_len, uint8_t *tx)
|
||||||
{
|
{
|
||||||
uint16_t total_len = 2 + 2 + 2 + 2 + 1 + body_len + 2; // 帧头+帧长+源+目标+类型+体+校验
|
uint16_t total_len = 2 + 2 + 2 + 2 + 1 + body_len + 2; // 帧头+帧长+源+目标+类型+体+校验
|
||||||
|
@ -185,8 +197,8 @@ uint16_t total_len = 2 + 2 + 2 + 2 + 1 + body_len + 2; // 帧头+帧长+源+目
|
||||||
// 报文类型
|
// 报文类型
|
||||||
tx[8] = reply_type;
|
tx[8] = reply_type;
|
||||||
// 报文体
|
// 报文体
|
||||||
tx[9] = 0x01;//主版本号1.0
|
tx[9] = Main_version_number;//主版本号1.0
|
||||||
tx[10] = 0x00;//次版本号0.0
|
tx[10] = Sub_version_number;//次版本号0.0
|
||||||
//memcpy(&tx[9], body, body_len);
|
//memcpy(&tx[9], body, body_len);
|
||||||
// 校验和(累加源地址到报文体)
|
// 校验和(累加源地址到报文体)
|
||||||
uint16_t checksum = 0;
|
uint16_t checksum = 0;
|
||||||
|
@ -199,6 +211,12 @@ uint16_t total_len = 2 + 2 + 2 + 2 + 1 + body_len + 2; // 帧头+帧长+源+目
|
||||||
tx[total_len - 1] = checksum & 0xFF;
|
tx[total_len - 1] = checksum & 0xFF;
|
||||||
return total_len;
|
return total_len;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* @brief 处理类型为82心跳的报文
|
||||||
|
*
|
||||||
|
* 根据传入的报文体和数据长度,构造出对应的响应报文,并计算校验和。
|
||||||
|
*
|
||||||
|
*/
|
||||||
uint16_t handle_type_82(const uint8_t *body, uint16_t body_len, uint8_t *tx)
|
uint16_t handle_type_82(const uint8_t *body, uint16_t body_len, uint8_t *tx)
|
||||||
{
|
{
|
||||||
uint16_t total_len = 2 + 2 + 2 + 2 + 1 + body_len + 2; // 帧头+帧长+源+目标+类型+体+校验
|
uint16_t total_len = 2 + 2 + 2 + 2 + 1 + body_len + 2; // 帧头+帧长+源+目标+类型+体+校验
|
||||||
|
@ -231,6 +249,12 @@ uint16_t handle_type_82(const uint8_t *body, uint16_t body_len, uint8_t *tx)
|
||||||
tx[total_len - 1] = checksum & 0xFF;
|
tx[total_len - 1] = checksum & 0xFF;
|
||||||
return total_len;
|
return total_len;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* @brief 处理类型为83模块使能状态的报文
|
||||||
|
*
|
||||||
|
* 根据传入的报文体和数据长度,构造出对应的响应报文,并计算校验和。
|
||||||
|
*
|
||||||
|
*/
|
||||||
uint16_t handle_type_83(const uint8_t *body, uint16_t body_len, uint8_t *tx)
|
uint16_t handle_type_83(const uint8_t *body, uint16_t body_len, uint8_t *tx)
|
||||||
{
|
{
|
||||||
// 检查 body_len 是否为7,如果不是则返回错误(可选)
|
// 检查 body_len 是否为7,如果不是则返回错误(可选)
|
||||||
|
@ -388,7 +412,9 @@ uint16_t total_len = 2 + 2 + 2 + 2 + 1 + 22 + 2; // 帧头+帧长+源+目标+类
|
||||||
uint16_t handle_type_87(const uint8_t *body, uint16_t body_len, uint8_t *tx)
|
uint16_t handle_type_87(const uint8_t *body, uint16_t body_len, uint8_t *tx)
|
||||||
{
|
{
|
||||||
uint16_t total_len = 2 + 2 + 2 + 2 + 1 + 74 + 2; // 帧头+帧长+源+目标+类型+74字节输出读取+校验
|
uint16_t total_len = 2 + 2 + 2 + 2 + 1 + 74 + 2; // 帧头+帧长+源+目标+类型+74字节输出读取+校验
|
||||||
|
// 1. 读取ID寄存器
|
||||||
|
ad7124_read_register(&ad7124_regs[AD7124_ID]);
|
||||||
|
uint8_t id = (uint8_t)(ad7124_regs[AD7124_ID].value & 0xFF);
|
||||||
// 帧头
|
// 帧头
|
||||||
tx[0] = head_00;
|
tx[0] = head_00;
|
||||||
tx[1] = head_01;
|
tx[1] = head_01;
|
||||||
|
@ -407,9 +433,9 @@ uint16_t total_len = 2 + 2 + 2 + 2 + 1 + 74 + 2; // 帧头+帧长+源+目标+类
|
||||||
|
|
||||||
// 报文类型
|
// 报文类型
|
||||||
tx[8] = reply_type;
|
tx[8] = reply_type;
|
||||||
|
tx[9] = id;
|
||||||
// 7字节使能状态(直接复制 body 到 tx[9]~tx[15])
|
// 7字节使能状态(直接复制 body 到 tx[9]~tx[15])
|
||||||
memcpy(&tx[9], body, 74); // 确保 body 是74字节
|
//memcpy(&tx[9], body, 74); // 确保 body 是74字节
|
||||||
|
|
||||||
// 校验和
|
// 校验和
|
||||||
uint16_t checksum = 0;
|
uint16_t checksum = 0;
|
||||||
|
|
|
@ -1,11 +1,17 @@
|
||||||
#include "ad7124.h"
|
#include "ad7124.h"
|
||||||
|
#include "user_spi.h"
|
||||||
|
#include "usart.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
/* Error codes */
|
/* Error codes */
|
||||||
#define INVALID_VAL -1 /* Invalid argument */
|
#define INVALID_VAL -1 /* Invalid argument */
|
||||||
#define COMM_ERR -2 /* Communication error on receive */
|
#define COMM_ERR -2 /* Communication error on receive */
|
||||||
#define AD7124_TIMEOUT -3 /* A timeout has occured */
|
#define AD7124_TIMEOUT -3 /* A timeout has occured */
|
||||||
|
int32_t FINESH_ID;
|
||||||
|
int32_t g_ad7124_id;
|
||||||
// 配置ad7124寄存器的值,根据实际项目需求配置
|
// 配置ad7124寄存器的值,根据实际项目需求配置
|
||||||
static ad7124_st_reg_t ad7124_regs[AD7124_REG_NO] = {
|
ad7124_st_reg_t ad7124_regs[AD7124_REG_NO] = {
|
||||||
{AD7124_STATUS, 0x00, AD7124_SIZE_1, AD7124_R}, /* AD7124_Status */
|
{AD7124_STATUS, 0x00, AD7124_SIZE_1, AD7124_R}, /* AD7124_Status */
|
||||||
{AD7124_ADC_CONTROL, 0x0280, AD7124_SIZE_2, AD7124_RW}, /* AD7124_ADC_Control */
|
{AD7124_ADC_CONTROL, 0x0280, AD7124_SIZE_2, AD7124_RW}, /* AD7124_ADC_Control */
|
||||||
{AD7124_DATA, 0x000000, AD7124_SIZE_3, AD7124_R}, /* AD7124_Data */
|
{AD7124_DATA, 0x000000, AD7124_SIZE_3, AD7124_R}, /* AD7124_Data */
|
||||||
|
@ -65,7 +71,7 @@ static ad7124_st_reg_t ad7124_regs[AD7124_REG_NO] = {
|
||||||
{AD7124_GAIN_7, 0x500000, AD7124_SIZE_3, AD7124_RW}, /* AD7124_Gain_7 */
|
{AD7124_GAIN_7, 0x500000, AD7124_SIZE_3, AD7124_RW}, /* AD7124_Gain_7 */
|
||||||
};
|
};
|
||||||
|
|
||||||
static ad7124_st_reg_t ad7124_channel_regs[AD7124_CHANNEL_EN_MAX] = {
|
ad7124_st_reg_t ad7124_channel_regs[AD7124_CHANNEL_EN_MAX] = {
|
||||||
{AD7124_CHANNEL_0, 0x8051, AD7124_SIZE_2, AD7124_RW}, /* AD7124_Channel_0 */
|
{AD7124_CHANNEL_0, 0x8051, AD7124_SIZE_2, AD7124_RW}, /* AD7124_Channel_0 */
|
||||||
{AD7124_CHANNEL_1, 0x8071, AD7124_SIZE_2, AD7124_RW}, /* AD7124_Channel_1 */
|
{AD7124_CHANNEL_1, 0x8071, AD7124_SIZE_2, AD7124_RW}, /* AD7124_Channel_1 */
|
||||||
{AD7124_CHANNEL_2, 0x8091, AD7124_SIZE_2, AD7124_RW}, /* AD7124_Channel_2 */
|
{AD7124_CHANNEL_2, 0x8091, AD7124_SIZE_2, AD7124_RW}, /* AD7124_Channel_2 */
|
||||||
|
@ -92,45 +98,47 @@ int32_t ad7124_no_check_read_register(ad7124_st_reg_t *p_reg)
|
||||||
{
|
{
|
||||||
int32_t ret = 0;
|
int32_t ret = 0;
|
||||||
uint8_t buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
uint8_t buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||||
uint8_t i = 0;
|
|
||||||
uint8_t add_status_length = 0;
|
uint8_t add_status_length = 0;
|
||||||
|
|
||||||
/* Build the Command word */
|
// 构建命令字
|
||||||
buffer[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_RD |
|
buffer[0] = AD7124_COMM_REG_RD | AD7124_COMM_REG_RA(p_reg->addr);
|
||||||
AD7124_COMM_REG_RA(p_reg->addr);
|
|
||||||
|
// 打印调试信息
|
||||||
|
// char uart_buf[100];
|
||||||
|
// sprintf(uart_buf, "Reading register 0x%02X, command: 0x%02X\r\n", p_reg->addr, buffer[0]);
|
||||||
|
// HAL_UART_Transmit(&huart1, (uint8_t*)uart_buf, strlen(uart_buf), 100);
|
||||||
|
|
||||||
/*
|
|
||||||
* If this is an AD7124_DATA register read, and the DATA_STATUS bit is set
|
|
||||||
* in ADC_CONTROL, need to read 4, not 3 bytes for DATA with STATUS
|
|
||||||
*/
|
|
||||||
if ((p_reg->addr == AD7124_DATA) &&
|
if ((p_reg->addr == AD7124_DATA) &&
|
||||||
(ad7124_regs[AD7124_ADC_CONTROL].value & AD7124_ADC_CTRL_REG_DATA_STATUS))
|
(ad7124_regs[AD7124_ADC_CONTROL].value & AD7124_ADC_CTRL_REG_DATA_STATUS))
|
||||||
{
|
{
|
||||||
add_status_length = 1; // 此处是根据寄存器配置而决定,根据AD7124_ADC_CONTROL第10位决定
|
add_status_length = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read data from the device */
|
// 从设备读取数据
|
||||||
ret = ad7124_read_write_spi(buffer, p_reg->size + 1 + add_status_length);
|
ret = ad7124_read_write_spi(buffer, p_reg->size + 1 + add_status_length);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
/*
|
// 解析数据(buffer[1]开始为有效数据)
|
||||||
* if reading Data with 4 bytes, need to copy the status byte to the STATUS
|
if (p_reg->size == 1) {
|
||||||
* register struct value member
|
p_reg->value = buffer[1];
|
||||||
*/
|
} else if (p_reg->size == 2) {
|
||||||
|
p_reg->value = ((uint16_t)buffer[1] << 8) | buffer[2];
|
||||||
|
} else if (p_reg->size == 3) {
|
||||||
|
p_reg->value = ((uint32_t)buffer[1] << 16) | ((uint32_t)buffer[2] << 8) | buffer[3];
|
||||||
|
} else {
|
||||||
|
p_reg->value = 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (add_status_length)
|
if (add_status_length)
|
||||||
{
|
{
|
||||||
ad7124_regs[AD7124_STATUS].value = buffer[p_reg->size + 1];
|
ad7124_regs[AD7124_STATUS].value = buffer[p_reg->size + 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Build the result */
|
// 仅用于调试:如果是ID寄存器,赋值到全局变量
|
||||||
p_reg->value = 0;
|
if (p_reg->addr == AD7124_ID) {
|
||||||
for (i = 1; i < p_reg->size + 1; i++)
|
g_ad7124_id = p_reg->value;
|
||||||
{
|
|
||||||
p_reg->value <<= 8;
|
|
||||||
p_reg->value += buffer[i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,7 +171,7 @@ int32_t ad7124_no_check_write_register(ad7124_st_reg_t *reg)
|
||||||
|
|
||||||
/* Write data to the device */
|
/* Write data to the device */
|
||||||
ret = ad7124_read_write_spi(wr_buf, reg->size + 1);
|
ret = ad7124_read_write_spi(wr_buf, reg->size + 1);
|
||||||
|
FINESH_ID = ret;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,13 +190,17 @@ int32_t ad7124_read_register(ad7124_st_reg_t *p_reg)
|
||||||
{
|
{
|
||||||
int32_t ret;
|
int32_t ret;
|
||||||
|
|
||||||
|
// 检查寄存器地址是否不是错误地址,并且检查ERROR寄存器的SPI_IGNORE_ERR位是否被设置
|
||||||
if (p_reg->addr != AD7124_ERROR && (ad7124_regs[AD7124_ERROR_EN].value & AD7124_ERREN_REG_SPI_IGNORE_ERR_EN))
|
if (p_reg->addr != AD7124_ERROR && (ad7124_regs[AD7124_ERROR_EN].value & AD7124_ERREN_REG_SPI_IGNORE_ERR_EN))
|
||||||
{
|
{
|
||||||
ret = ad7124_wait_for_spi_ready(AD7124_RDY); // 读寄存器之前检查ERROR寄存器的SPI_IGNORE_ERR位
|
// 读寄存器之前检查ERROR寄存器的SPI_IGNORE_ERR位
|
||||||
|
ret = ad7124_wait_for_spi_ready(AD7124_RDY);
|
||||||
|
|
||||||
|
// 如果等待SPI就绪失败,则返回错误码
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
// 不检查ERROR寄存器的SPI_IGNORE_ERR位,直接读取寄存器
|
||||||
ret = ad7124_no_check_read_register(p_reg);
|
ret = ad7124_no_check_read_register(p_reg);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -333,7 +345,7 @@ int32_t ad7124_read_data(void)
|
||||||
{
|
{
|
||||||
int32_t read_data;
|
int32_t read_data;
|
||||||
/* Read the value of the Status Register */
|
/* Read the value of the Status Register */
|
||||||
ad7124_read_register(&ad7124_regs[AD7124_DATA]);
|
ad7124_read_register(&ad7124_regs[AD7124_DATA]);//读取数据寄存器
|
||||||
/* Get the read result */
|
/* Get the read result */
|
||||||
read_data = ad7124_regs[AD7124_DATA].value;
|
read_data = ad7124_regs[AD7124_DATA].value;
|
||||||
|
|
||||||
|
@ -383,19 +395,30 @@ int32_t ad7124_read_data(void)
|
||||||
void ad7124_get_analog(uint8_t channel_nr)
|
void ad7124_get_analog(uint8_t channel_nr)
|
||||||
{
|
{
|
||||||
int32_t read_data;
|
int32_t read_data;
|
||||||
|
|
||||||
|
// 设置AD7124寄存器,选择指定通道
|
||||||
// for (i = STOP_NC_ADC; i < AD7124_CHANNEL_EN_MAX; i++)
|
// for (i = STOP_NC_ADC; i < AD7124_CHANNEL_EN_MAX; i++)
|
||||||
// {
|
// {
|
||||||
ad7124_regs[AD7124_CHANNEL_0].value = ad7124_channel_regs[channel_nr].value;
|
ad7124_regs[AD7124_CHANNEL_0].value = ad7124_channel_regs[channel_nr].value;
|
||||||
ad7124_write_register(&ad7124_regs[AD7124_CHANNEL_0]);
|
ad7124_write_register(&ad7124_regs[AD7124_CHANNEL_0]);
|
||||||
|
|
||||||
|
// 等待转换完成
|
||||||
while (ad7124_wait_for_conv_ready(AD7124_RDY))
|
while (ad7124_wait_for_conv_ready(AD7124_RDY))
|
||||||
; // 等待转换完成
|
; // 等待转换完成
|
||||||
|
|
||||||
|
// 存储读取的模拟数据
|
||||||
ad7124_analog[channel_nr].channel = channel_nr;
|
ad7124_analog[channel_nr].channel = channel_nr;
|
||||||
read_data = ad7124_read_data();
|
read_data = ad7124_read_data();
|
||||||
ad7124_analog[channel_nr].data = read_data;
|
ad7124_analog[channel_nr].data = read_data;
|
||||||
ad7124_analog[channel_nr].voltage = (float)(read_data * VREF / GAIN / AD_CODE); // AD7124单极性计算公式:Code = (0xFFFFFF × AIN × Gain)/VREF
|
|
||||||
ad7124_analog[channel_nr].current = (float)(ad7124_analog[channel_nr].voltage / AD7124_RES * 1000); // 乘1000是为了将单位转换为mA
|
|
||||||
|
|
||||||
|
// 计算电压值
|
||||||
|
// AD7124单极性计算公式:Code = (0xFFFFFF × AIN × Gain)/VREF
|
||||||
|
ad7124_analog[channel_nr].voltage = (float)(read_data * VREF / GAIN / AD_CODE);
|
||||||
|
|
||||||
|
// 计算电流值,乘1000是为了将单位转换为mA
|
||||||
|
ad7124_analog[channel_nr].current = (float)(ad7124_analog[channel_nr].voltage / AD7124_RES * 1000);
|
||||||
|
|
||||||
|
// 恢复通道0寄存器为默认值
|
||||||
ad7124_regs[AD7124_CHANNEL_0].value = 0;
|
ad7124_regs[AD7124_CHANNEL_0].value = 0;
|
||||||
ad7124_write_register(&ad7124_regs[AD7124_CHANNEL_0]);
|
ad7124_write_register(&ad7124_regs[AD7124_CHANNEL_0]);
|
||||||
// }
|
// }
|
||||||
|
@ -411,27 +434,81 @@ int32_t ad7124_setup(void)
|
||||||
{
|
{
|
||||||
int32_t ret;
|
int32_t ret;
|
||||||
uint8_t reg_nr;
|
uint8_t reg_nr;
|
||||||
// board_spi_init(AD7124); // 初始化SPI,因为DAC161的SPI要和AD7124共用,并且时序不一样,所以要先初始化SPI接口。
|
char uart_buf[100];
|
||||||
/* Reset the device interface.*/
|
|
||||||
|
// 初始化SPI
|
||||||
|
board_spi_init(AD7124);
|
||||||
|
HAL_Delay(10); // 等待SPI稳定
|
||||||
|
|
||||||
|
// 复位设备
|
||||||
ret = ad7124_reset();
|
ret = ad7124_reset();
|
||||||
if (ret < 0)
|
if (ret < 0) {
|
||||||
|
sprintf(uart_buf, "AD7124 reset failed: %ld\r\n", ret);
|
||||||
|
HAL_UART_Transmit(&huart1, (uint8_t*)uart_buf, strlen(uart_buf), 100);
|
||||||
return ret;
|
return ret;
|
||||||
HAL_Delay(10);
|
}
|
||||||
ad7124_read_register(&ad7124_regs[AD7124_ID]);
|
|
||||||
/* Initialize registers AD7124_ADC_Control through AD7124_Filter_7. */
|
// 等待复位完成
|
||||||
for (reg_nr = AD7124_STATUS; reg_nr < AD7124_OFFSET_0; reg_nr++) // 对ad7124的可写寄存器进行配置,不包括只读寄存器
|
HAL_Delay(100); // 增加复位后的等待时间
|
||||||
{
|
|
||||||
// ret = ad7124_read_register(&ad7124_regs[reg_nr]);
|
// 等待电源稳定
|
||||||
if (ad7124_regs[reg_nr].rw == AD7124_RW)
|
ret = ad7124_wait_to_power_on(AD7124_RDY);
|
||||||
{
|
if (ret < 0) {
|
||||||
|
sprintf(uart_buf, "AD7124 power on timeout: %ld\r\n", ret);
|
||||||
|
HAL_UART_Transmit(&huart1, (uint8_t*)uart_buf, strlen(uart_buf), 100);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取ID寄存器
|
||||||
|
ret = ad7124_read_register(&ad7124_regs[AD7124_ID]);
|
||||||
|
if (ret < 0) {
|
||||||
|
sprintf(uart_buf, "Failed to read ID register: %ld\r\n", ret);
|
||||||
|
HAL_UART_Transmit(&huart1, (uint8_t*)uart_buf, strlen(uart_buf), 100);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查ID值
|
||||||
|
// if (ad7124_regs[AD7124_ID].value != 0x14) {
|
||||||
|
// sprintf(uart_buf, "Invalid ID: 0x%02X, expected 0x14\r\n", ad7124_regs[AD7124_ID].value);
|
||||||
|
// HAL_UART_Transmit(&huart1, (uint8_t*)uart_buf, strlen(uart_buf), 100);
|
||||||
|
// return COMM_ERR;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// sprintf(uart_buf, "AD7124 ID verified: 0x%02X\r\n", ad7124_regs[AD7124_ID].value);
|
||||||
|
// HAL_UART_Transmit(&huart1, (uint8_t*)uart_buf, strlen(uart_buf), 100);
|
||||||
|
|
||||||
|
// 初始化其他寄存器
|
||||||
|
for (reg_nr = AD7124_STATUS; reg_nr < AD7124_OFFSET_0; reg_nr++) {
|
||||||
|
if (ad7124_regs[reg_nr].rw == AD7124_RW) {
|
||||||
ret = ad7124_write_register(&ad7124_regs[reg_nr]);
|
ret = ad7124_write_register(&ad7124_regs[reg_nr]);
|
||||||
|
if (ret < 0) {
|
||||||
|
sprintf(uart_buf, "Failed to write register 0x%02X: %ld\r\n", reg_nr, ret);
|
||||||
|
HAL_UART_Transmit(&huart1, (uint8_t*)uart_buf, strlen(uart_buf), 100);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证写入
|
||||||
ret = ad7124_read_register(&ad7124_regs[reg_nr]);
|
ret = ad7124_read_register(&ad7124_regs[reg_nr]);
|
||||||
if (ret < 0)
|
if (ret < 0) {
|
||||||
break;
|
sprintf(uart_buf, "Failed to verify register 0x%02X: %ld\r\n", reg_nr, ret);
|
||||||
|
HAL_UART_Transmit(&huart1, (uint8_t*)uart_buf, strlen(uart_buf), 100);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果读取的值与写入的值不匹配,返回错误
|
||||||
|
if (ad7124_regs[reg_nr].value != ad7124_regs[reg_nr].value) {
|
||||||
|
sprintf(uart_buf, "Register 0x%02X mismatch: wrote 0x%04X, read 0x%04X\r\n",
|
||||||
|
reg_nr, ad7124_regs[reg_nr].value, ad7124_regs[reg_nr].value);
|
||||||
|
HAL_UART_Transmit(&huart1, (uint8_t*)uart_buf, strlen(uart_buf), 100);
|
||||||
|
return COMM_ERR;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
HAL_GPIO_WritePin(AD7124_SYNC_GPIO_Port, AD7124_SYNC_Pin, GPIO_PIN_SET); // AD7124同步信号使能
|
|
||||||
return ret;
|
// 使能同步信号
|
||||||
|
HAL_GPIO_WritePin(AD7124_SYNC_GPIO_Port, AD7124_SYNC_Pin, GPIO_PIN_SET);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -447,9 +524,18 @@ int32_t ad7124_setup(void)
|
||||||
int32_t ad7124_read_write_spi(uint8_t *buff, uint8_t length)
|
int32_t ad7124_read_write_spi(uint8_t *buff, uint8_t length)
|
||||||
{
|
{
|
||||||
int32_t ret;
|
int32_t ret;
|
||||||
board_spi_init(AD7124); // 初始化SPI,因为DAC161的SPI要和AD7124共用,并且时序不一样,所以要先初始化SPI接口。
|
|
||||||
board_spi_cs_on(AD7124);
|
// 确保CS信号在传输前是高的
|
||||||
ret = spi_transmit_receive(&hspi1, buff, length);
|
|
||||||
board_spi_cs_off(AD7124);
|
board_spi_cs_off(AD7124);
|
||||||
|
HAL_Delay(1); // 添加短暂延时
|
||||||
|
//board_spi_init(AD7124);
|
||||||
|
board_spi_cs_on(AD7124);
|
||||||
|
HAL_Delay(1); // 添加短暂延时
|
||||||
|
|
||||||
|
ret = spi_transmit_receive(&hspi2, buff, length);
|
||||||
|
|
||||||
|
HAL_Delay(1); // 添加短暂延时
|
||||||
|
board_spi_cs_off(AD7124);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,14 +6,25 @@
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "user_spi.h"
|
#include "user_spi.h"
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/******************* Register map and register definitions ********************/
|
/******************* Register map and register definitions ********************/
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
// 常量定义
|
||||||
|
#define AD7124_CRC8_POLYNOMIAL_REPRESENTATION 0x07 /* x8 + x2 + x + 1 */
|
||||||
|
#define AD7124_DISABLE_CRC 0
|
||||||
|
#define AD7124_USE_CRC 1
|
||||||
|
#define AD7124_RDY 10000
|
||||||
|
#define AD7124_RES 100
|
||||||
|
#define VREF 2.5f
|
||||||
|
#define GAIN 1
|
||||||
|
#define AD_CODE 0XFFFFFF
|
||||||
|
|
||||||
/* Communication Register bits */
|
/* Communication Register bits */
|
||||||
#define AD7124_COMM_REG_WEN (0 << 7)
|
#define AD7124_COMM_REG_WEN (0 << 7)
|
||||||
#define AD7124_COMM_REG_WR (0 << 6)
|
#define AD7124_COMM_REG_WR (0 << 6)
|
||||||
#define AD7124_COMM_REG_RD (1 << 6)
|
#define AD7124_COMM_REG_RD (1 << 6) //0x40
|
||||||
#define AD7124_COMM_REG_RA(x) ((x) & 0x3F)
|
#define AD7124_COMM_REG_RA(x) ((x) & 0x3F)
|
||||||
|
|
||||||
/* Status Register bits */
|
/* Status Register bits */
|
||||||
|
@ -159,151 +170,127 @@
|
||||||
/*! AD7124 registers list*/
|
/*! AD7124 registers list*/
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
AD7124_STATUS = 0X00, // 只读的状态寄存器,读取芯片状态
|
AD7124_STATUS = 0X00,//状态寄存器
|
||||||
AD7124_ADC_CONTROL, // 控制寄存器,设置采样速率等
|
AD7124_ADC_CONTROL,//ADC控制寄存器
|
||||||
AD7124_DATA, // 数据寄存器,读取采样数据
|
AD7124_DATA,//数据寄存器
|
||||||
AD7124_IOCON1, // 设置激励电流等参数,用作RTD测试时可用
|
AD7124_IOCON1,//IO控制寄存器1
|
||||||
AD7124_IOCON2, // 使能偏置电压
|
AD7124_IOCON2,//IO控制寄存器2
|
||||||
AD7124_ID, // ID寄存器,部分芯片的ID和手册上有所区别,可用此寄存器测试SPI通讯
|
AD7124_ID,//ID寄存器
|
||||||
AD7124_ERROR, // 错误寄存器,读取错误信息,读写寄存器之前先读下该寄存器,检测芯片状态是否支持读写
|
AD7124_ERROR,//错误寄存器
|
||||||
AD7124_ERROR_EN, // 通过使能该寄存器的相应位来使能或者禁用诊断功能
|
AD7124_ERROR_EN,//错误使能寄存器
|
||||||
AD7124_MCLK_COUNT, // 监控主时钟频率
|
AD7124_MCLK_COUNT,//MCLK计数寄存器
|
||||||
AD7124_CHANNEL_0, // 设置AD采样通道和所需要的配置,其中的Setup位决定了采用哪种Config、Filter、Offset、Gain寄存器的配置;共有八种配置
|
AD7124_CHANNEL_0,//通道0
|
||||||
AD7124_CHANNEL_1, // 通道寄存器的顺序并不是从AI0引脚读到最后一个引脚,而是通过自己的设置来决定顺序
|
AD7124_CHANNEL_1,//通道1
|
||||||
AD7124_CHANNEL_2,
|
AD7124_CHANNEL_2,//通道2
|
||||||
AD7124_CHANNEL_3,
|
AD7124_CHANNEL_3,//通道3
|
||||||
AD7124_CHANNEL_4,
|
AD7124_CHANNEL_4,//通道4
|
||||||
AD7124_CHANNEL_5,
|
AD7124_CHANNEL_5,//通道5
|
||||||
AD7124_CHANNEL_6,
|
AD7124_CHANNEL_6,//通道6
|
||||||
AD7124_CHANNEL_7,
|
AD7124_CHANNEL_7,//通道7
|
||||||
AD7124_CHANNEL_8,
|
AD7124_CHANNEL_8,//通道8
|
||||||
AD7124_CHANNEL_9,
|
AD7124_CHANNEL_9,//通道9
|
||||||
AD7124_CHANNEL_10,
|
AD7124_CHANNEL_10,//通道10
|
||||||
AD7124_CHANNEL_11,
|
AD7124_CHANNEL_11,//通道11
|
||||||
AD7124_CHANNEL_12,
|
AD7124_CHANNEL_12,//通道12
|
||||||
AD7124_CHANNEL_13,
|
AD7124_CHANNEL_13,//通道13
|
||||||
AD7124_CHANNEL_14,
|
AD7124_CHANNEL_14,//通道14
|
||||||
AD7124_CHANNEL_15,
|
AD7124_CHANNEL_15,//通道15
|
||||||
AD7124_CONFIG_0, // 配置极性,增益,基准选择等参数
|
AD7124_CONFIG_0,//配置寄存器0
|
||||||
AD7124_CONFIG_1,
|
AD7124_CONFIG_1,//配置寄存器1
|
||||||
AD7124_CONFIG_2,
|
AD7124_CONFIG_2,//配置寄存器2
|
||||||
AD7124_CONFIG_3,
|
AD7124_CONFIG_3,//配置寄存器3
|
||||||
AD7124_CONFIG_4,
|
AD7124_CONFIG_4,//配置寄存器4
|
||||||
AD7124_CONFIG_5,
|
AD7124_CONFIG_5,//配置寄存器5
|
||||||
AD7124_CONFIG_6,
|
AD7124_CONFIG_6,//配置寄存器6
|
||||||
AD7124_CONFIG_7,
|
AD7124_CONFIG_7,//配置寄存器7
|
||||||
AD7124_FILTER_0,
|
AD7124_FILTER_0,//滤波寄存器0
|
||||||
AD7124_FILTER_1, // 配置滤波器
|
AD7124_FILTER_1,//滤波寄存器1
|
||||||
AD7124_FILTER_2,
|
AD7124_FILTER_2,//滤波寄存器2
|
||||||
AD7124_FILTER_3,
|
AD7124_FILTER_3,//滤波寄存器3
|
||||||
AD7124_FILTER_4,
|
AD7124_FILTER_4,//滤波寄存器4
|
||||||
AD7124_FILTER_5,
|
AD7124_FILTER_5,//滤波寄存器5
|
||||||
AD7124_FILTER_6,
|
AD7124_FILTER_6,//滤波寄存器6
|
||||||
AD7124_FILTER_7,
|
AD7124_FILTER_7,//滤波寄存器7
|
||||||
AD7124_OFFSET_0,
|
AD7124_OFFSET_0,//偏移寄存器0
|
||||||
AD7124_OFFSET_1,
|
AD7124_OFFSET_1,//偏移寄存器1
|
||||||
AD7124_OFFSET_2,
|
AD7124_OFFSET_2,//偏移寄存器2
|
||||||
AD7124_OFFSET_3,
|
AD7124_OFFSET_3,//偏移寄存器3
|
||||||
AD7124_OFFSET_4,
|
AD7124_OFFSET_4,//偏移寄存器4
|
||||||
AD7124_OFFSET_5,
|
AD7124_OFFSET_5,//偏移寄存器5
|
||||||
AD7124_OFFSET_6,
|
AD7124_OFFSET_6,//偏移寄存器6
|
||||||
AD7124_OFFSET_7,
|
AD7124_OFFSET_7,//偏移寄存器7
|
||||||
AD7124_GAIN_0,
|
AD7124_GAIN_0,//增益寄存器0
|
||||||
AD7124_GAIN_1,
|
AD7124_GAIN_1,//增益寄存器1
|
||||||
AD7124_GAIN_2,
|
AD7124_GAIN_2,//增益寄存器2
|
||||||
AD7124_GAIN_3,
|
AD7124_GAIN_3,//增益寄存器3
|
||||||
AD7124_GAIN_4,
|
AD7124_GAIN_4,//增益寄存器4
|
||||||
AD7124_GAIN_5,
|
AD7124_GAIN_5,//增益寄存器5
|
||||||
AD7124_GAIN_6,
|
AD7124_GAIN_6,//增益寄存器6
|
||||||
AD7124_GAIN_7,
|
AD7124_GAIN_7,//增益寄存器7
|
||||||
AD7124_REG_NO
|
AD7124_REG_NO//寄存器数量
|
||||||
} ad7124_registers_addr_e; // 寄存器地址
|
} ad7124_registers_addr_e;
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
|
|
||||||
AD7124_SIZE_1 = 1,
|
AD7124_SIZE_1 = 1,
|
||||||
AD7124_SIZE_2 = 2,
|
AD7124_SIZE_2 = 2,
|
||||||
AD7124_SIZE_3 = 3,
|
AD7124_SIZE_3 = 3,
|
||||||
} ad7124_registers_size_e; // 寄存器字节大小
|
} ad7124_registers_size_e;
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
AD7124_RW = 1,
|
AD7124_RW = 1,//读写
|
||||||
AD7124_R = 2,
|
AD7124_R = 2,//读
|
||||||
AD7124_W = 3,
|
AD7124_W = 3,//写
|
||||||
} ad7124_registers_rw_e; // 寄存器读写操作
|
} ad7124_registers_rw_e;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
int addr; // 寄存器地址,ad7124_registers_e
|
int addr;
|
||||||
int value; // 寄存器值
|
int32_t value;
|
||||||
int size; // 寄存器字节大小
|
int size;
|
||||||
int rw; // 寄存器可执行的操作,ad7124_registers_rw_e
|
int rw;
|
||||||
} ad7124_st_reg_t; // AD7124寄存器结构体
|
} ad7124_st_reg_t;
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
STOP_NC_ADC = 0, // AD7124_CHANNEL_EN_2
|
STOP_NC_ADC = 0,
|
||||||
STOP_NO_ADC, // AD7124_CHANNEL_EN_3
|
STOP_NO_ADC,
|
||||||
AI_IN1_ADC, // AD7124_CHANNEL_EN_4
|
AI_IN1_ADC,
|
||||||
AI_IN2_ADC, // AD7124_CHANNEL_EN_5
|
AI_IN2_ADC,
|
||||||
P1_DI2_ADC, // AD7124_CHANNEL_EN_6
|
P1_DI2_ADC,
|
||||||
P1_DI1_ADC, // AD7124_CHANNEL_EN_7
|
P1_DI1_ADC,
|
||||||
P1_AI_ADC, // AD7124_CHANNEL_EN_8
|
P1_AI_ADC,
|
||||||
P2_DI2_ADC, // AD7124_CHANNEL_EN_9
|
P2_DI2_ADC,
|
||||||
P2_DI1_ADC, // AD7124_CHANNEL_EN_10
|
P2_DI1_ADC,
|
||||||
P2_AI_ADC, // AD7124_CHANNEL_EN_11
|
P2_AI_ADC,
|
||||||
AD7124_CHANNEL_EN_MAX,
|
AD7124_CHANNEL_EN_MAX,
|
||||||
} ad7124_channel_e; // 该项目所使用的通道
|
} ad7124_channel_e;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
uint8_t channel;
|
uint8_t channel;
|
||||||
int32_t data; // 采样数据
|
int32_t data;
|
||||||
float voltage; // 电压值
|
float voltage;
|
||||||
float current; // 电流值
|
float current;
|
||||||
} ad7124_analog_t; // 采样数据结构体
|
} ad7124_analog_t;
|
||||||
|
|
||||||
#define AD7124_CRC8_POLYNOMIAL_REPRESENTATION 0x07 /* x8 + x2 + x + 1 */
|
// 声明外部变量
|
||||||
#define AD7124_DISABLE_CRC 0 // 默认关闭校验
|
extern ad7124_st_reg_t ad7124_regs[AD7124_REG_NO];
|
||||||
#define AD7124_USE_CRC 1
|
extern ad7124_st_reg_t ad7124_channel_regs[AD7124_CHANNEL_EN_MAX];
|
||||||
|
extern int32_t g_ad7124_id;
|
||||||
#define AD7124_RDY 10000 // 等待时间
|
// 函数声明
|
||||||
|
int32_t ad7124_read_register(ad7124_st_reg_t *p_reg);
|
||||||
#define AD7124_RES 100 // 采样基准电阻
|
int32_t ad7124_write_register(ad7124_st_reg_t *reg);
|
||||||
#define VREF 2.5f // 基准电压
|
|
||||||
#define GAIN 1 // 增益,该值和配置寄存器有关
|
|
||||||
#define AD_CODE 0XFFFFFF // 24位ADC
|
|
||||||
|
|
||||||
/*! Reads the value of the specified register. */
|
|
||||||
int32_t ad7124_read_register(ad7124_st_reg_t *p_reg); // 读寄存器
|
|
||||||
|
|
||||||
/*! Writes the value of the specified register. */
|
|
||||||
int32_t ad7124_write_register(ad7124_st_reg_t *reg); // 写寄存器
|
|
||||||
|
|
||||||
/*! Reads the value of the specified register without a device state check. */
|
|
||||||
int32_t ad7124_no_check_read_register(ad7124_st_reg_t *p_reg);
|
int32_t ad7124_no_check_read_register(ad7124_st_reg_t *p_reg);
|
||||||
|
|
||||||
/*! Writes the value of the specified register without a device state check. */
|
|
||||||
int32_t ad7124_no_check_write_register(ad7124_st_reg_t *reg);
|
int32_t ad7124_no_check_write_register(ad7124_st_reg_t *reg);
|
||||||
|
int32_t ad7124_reset(void);
|
||||||
/*! Resets the device. */
|
int32_t ad7124_wait_for_spi_ready(uint32_t timeout);
|
||||||
int32_t ad7124_reset(void); // 复位ad7124芯片
|
|
||||||
|
|
||||||
/*! Waits until the device can accept read and write user actions. */
|
|
||||||
int32_t ad7124_wait_for_spi_ready(uint32_t timeout); // 读取ad7124芯片状态,直到可以执行读写操作
|
|
||||||
|
|
||||||
/*! Waits until the device finishes the power-on reset operation. */
|
|
||||||
int32_t ad7124_wait_to_power_on(uint32_t timeout);
|
int32_t ad7124_wait_to_power_on(uint32_t timeout);
|
||||||
|
int32_t ad7124_wait_for_conv_ready(uint32_t timeout);
|
||||||
/*! Waits until a new conversion result is available. */
|
|
||||||
int32_t ad7124_wait_for_conv_ready(uint32_t timeout); // 等待转换完成
|
|
||||||
|
|
||||||
/*! Reads the conversion result from the device. */
|
|
||||||
int32_t ad7124_read_data(void);
|
int32_t ad7124_read_data(void);
|
||||||
void ad7124_get_analog(uint8_t channel_nr);
|
void ad7124_get_analog(uint8_t channel_nr);
|
||||||
/*! Initializes the AD7124. */
|
|
||||||
int32_t ad7124_setup(void);
|
int32_t ad7124_setup(void);
|
||||||
|
|
||||||
int32_t ad7124_read_write_spi(uint8_t *buff, uint8_t length);
|
int32_t ad7124_read_write_spi(uint8_t *buff, uint8_t length);
|
||||||
|
|
||||||
#endif /* __AD7124_H__ */
|
#endif /* __AD7124_H */
|
||||||
|
|
|
@ -1,33 +1,42 @@
|
||||||
#include "user_spi.h"
|
#include "user_spi.h"
|
||||||
|
|
||||||
|
/* SPI2 Handle */
|
||||||
|
SPI_HandleTypeDef hspi2;
|
||||||
|
|
||||||
#define SPI_BUFFER_SIZE 255
|
#define SPI_BUFFER_SIZE 255
|
||||||
static uint8_t spi_rx_buffer[SPI_BUFFER_SIZE] = {0};
|
static uint8_t spi_rx_buffer[SPI_BUFFER_SIZE] = {0};
|
||||||
int32_t spi_transmit_receive(SPI_HandleTypeDef *hspi, uint8_t *data_write, uint8_t bytes_number)
|
int32_t spi_transmit_receive(SPI_HandleTypeDef *hspi, uint8_t *data_write, uint8_t bytes_number)
|
||||||
{
|
{
|
||||||
if (HAL_SPI_TransmitReceive(hspi, data_write, (uint8_t *)spi_rx_buffer, bytes_number, 1000) != HAL_OK)
|
// 使用 HAL_SPI_TransmitReceive 函数进行 SPI 通信
|
||||||
|
if (HAL_SPI_TransmitReceive(&hspi2, data_write, (uint8_t *)spi_rx_buffer, bytes_number, 1000) != HAL_OK)
|
||||||
{
|
{
|
||||||
|
// 如果通信失败,返回 FAIL
|
||||||
return FAIL;
|
return FAIL;
|
||||||
}
|
}
|
||||||
/* Copy the SPI receive buffer to the supplied data buffer to return to caller*/
|
/* 将 SPI 接收缓冲区复制到提供的数据缓冲区,以便返回给调用者 */
|
||||||
memcpy(data_write, spi_rx_buffer, bytes_number);
|
memcpy(data_write, spi_rx_buffer, bytes_number);
|
||||||
|
// 返回 TRUE 表示通信成功
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ad7124_spi_init(void)
|
void ad7124_spi_init(void)
|
||||||
{
|
{
|
||||||
hspi1.Instance = SPI1;
|
/* Enable SPI2 clock */
|
||||||
hspi1.Init.Mode = SPI_MODE_MASTER;
|
__HAL_RCC_SPI2_CLK_ENABLE();
|
||||||
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
|
|
||||||
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
|
hspi2.Instance = SPI2;
|
||||||
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
|
hspi2.Init.Mode = SPI_MODE_MASTER;
|
||||||
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
|
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
|
||||||
hspi1.Init.NSS = SPI_NSS_SOFT;
|
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||||
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
|
hspi2.Init.CLKPolarity = SPI_POLARITY_HIGH;
|
||||||
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
hspi2.Init.CLKPhase = SPI_PHASE_2EDGE;
|
||||||
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
|
hspi2.Init.NSS = SPI_NSS_SOFT;
|
||||||
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
|
||||||
hspi1.Init.CRCPolynomial = 10;
|
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||||
if (HAL_SPI_Init(&hspi1) != HAL_OK)
|
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||||
|
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||||
|
hspi2.Init.CRCPolynomial = 10;
|
||||||
|
if (HAL_SPI_Init(&hspi2) != HAL_OK)
|
||||||
{
|
{
|
||||||
Error_Handler();
|
Error_Handler();
|
||||||
}
|
}
|
||||||
|
@ -35,19 +44,22 @@ void ad7124_spi_init(void)
|
||||||
|
|
||||||
void dac161s997_spi_init(void)
|
void dac161s997_spi_init(void)
|
||||||
{
|
{
|
||||||
hspi1.Instance = SPI1;
|
/* Enable SPI2 clock */
|
||||||
hspi1.Init.Mode = SPI_MODE_MASTER;
|
__HAL_RCC_SPI2_CLK_ENABLE();
|
||||||
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
|
|
||||||
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
|
hspi2.Instance = SPI2;
|
||||||
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
|
hspi2.Init.Mode = SPI_MODE_MASTER;
|
||||||
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
|
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
|
||||||
hspi1.Init.NSS = SPI_NSS_SOFT;
|
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||||
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
|
hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||||
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||||
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
|
hspi2.Init.NSS = SPI_NSS_SOFT;
|
||||||
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
|
||||||
hspi1.Init.CRCPolynomial = 10;
|
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||||
if (HAL_SPI_Init(&hspi1) != HAL_OK)
|
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||||
|
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||||
|
hspi2.Init.CRCPolynomial = 10;
|
||||||
|
if (HAL_SPI_Init(&hspi2) != HAL_OK)
|
||||||
{
|
{
|
||||||
Error_Handler();
|
Error_Handler();
|
||||||
}
|
}
|
||||||
|
@ -55,16 +67,14 @@ void dac161s997_spi_init(void)
|
||||||
|
|
||||||
void ad7124_cs_on(void)
|
void ad7124_cs_on(void)
|
||||||
{
|
{
|
||||||
HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_SET);
|
// 激活AD7124 (CS低电平有效)
|
||||||
HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC2_CS_Pin, GPIO_PIN_SET);
|
HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_RESET);
|
||||||
HAL_GPIO_WritePin(ADC_CS_GPIO_Port, ADC_CS_Pin, GPIO_PIN_RESET);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ad7124_cs_off(void)
|
void ad7124_cs_off(void)
|
||||||
{
|
{
|
||||||
HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_SET);
|
// 禁用AD7124 (CS高电平)
|
||||||
HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC2_CS_Pin, GPIO_PIN_SET);
|
HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_SET);
|
||||||
HAL_GPIO_WritePin(ADC_CS_GPIO_Port, ADC_CS_Pin, GPIO_PIN_SET);
|
|
||||||
}
|
}
|
||||||
void dac161s997_cs_on(chip_type_e dac_num)
|
void dac161s997_cs_on(chip_type_e dac_num)
|
||||||
{
|
{
|
||||||
|
|
|
@ -13,7 +13,7 @@ typedef enum
|
||||||
} chip_type_e; // 芯片类型
|
} chip_type_e; // 芯片类型
|
||||||
|
|
||||||
extern int32_t spi_transmit_receive(SPI_HandleTypeDef *hspi, uint8_t *data_write, uint8_t bytes_number);
|
extern int32_t spi_transmit_receive(SPI_HandleTypeDef *hspi, uint8_t *data_write, uint8_t bytes_number);
|
||||||
|
extern SPI_HandleTypeDef hspi2;
|
||||||
extern void board_spi_init(chip_type_e chip_type);
|
extern void board_spi_init(chip_type_e chip_type);
|
||||||
extern void board_spi_cs_on(chip_type_e chip_type);
|
extern void board_spi_cs_on(chip_type_e chip_type);
|
||||||
extern void board_spi_cs_off(chip_type_e chip_type);
|
extern void board_spi_cs_off(chip_type_e chip_type);
|
||||||
|
|
Loading…
Reference in New Issue