2025-08-13 16:43:29 +08:00
|
|
|
|
/*
|
|
|
|
|
|
*Copyright ,2023 , NANOCHAP
|
|
|
|
|
|
*File name: MIAN.C
|
|
|
|
|
|
*Author:
|
|
|
|
|
|
*Version: V1.0
|
2025-08-20 11:03:53 +08:00
|
|
|
|
*Date: 2023-11-
|
2025-08-29 11:30:52 +08:00
|
|
|
|
*Description: 定时器电刺激综合测试(TIMER0 + 电刺激)
|
2025-08-13 16:43:29 +08:00
|
|
|
|
*Function List:
|
|
|
|
|
|
|
|
|
|
|
|
History:
|
|
|
|
|
|
1.V1.0
|
|
|
|
|
|
Date:
|
|
|
|
|
|
Author:
|
2025-08-29 11:30:52 +08:00
|
|
|
|
Modification: 初版
|
2025-08-13 16:43:29 +08:00
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
#include "my_header.h"
|
|
|
|
|
|
#include "ENS1_MTP.h"
|
|
|
|
|
|
#include "ENS1_CLOCK.h"
|
|
|
|
|
|
#include "ENS1_UART.h"
|
|
|
|
|
|
#include "ENS1_TIMER.h"
|
|
|
|
|
|
#include "ENS1_GPIO.h"
|
2025-08-20 11:03:53 +08:00
|
|
|
|
#include "ENS1_WAVEGEN.h"
|
|
|
|
|
|
|
2025-09-30 14:07:39 +08:00
|
|
|
|
// CRC-16-CCITT-FALSE计算函数
|
|
|
|
|
|
uint16_t CalculateCRC16_CCITT_FALSE(uint8_t *data, uint16_t length)
|
|
|
|
|
|
{
|
|
|
|
|
|
uint16_t crc = 0xFFFF; // 初始值为0xFFFF
|
|
|
|
|
|
for(uint16_t i = 0; i < length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
crc ^= (uint16_t)(data[i] << 8); // 高字节先处理
|
|
|
|
|
|
for(uint8_t j = 0; j < 8; j++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(crc & 0x8000)
|
|
|
|
|
|
{
|
|
|
|
|
|
crc = (crc << 1) ^ 0x1021; // 多项式0x1021
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
crc = crc << 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return crc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// UART数据接收函数 - 轮询方式
|
|
|
|
|
|
void UART_ReceiveData(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 检查接收FIFO并读取所有可用的数据
|
|
|
|
|
|
while(!UART_RX_FIFO_EMPTY(CMSDK_UART1))
|
|
|
|
|
|
{
|
|
|
|
|
|
uint8_t received_data = READ_UART_RCVBuff(CMSDK_UART1); // 读取接收数据
|
|
|
|
|
|
if(uart_rx_count < sizeof(uart_rx_buffer)) // 防止缓冲区溢出
|
|
|
|
|
|
{
|
|
|
|
|
|
uart_rx_buffer[uart_rx_count] = received_data;
|
|
|
|
|
|
uart_rx_count++;
|
|
|
|
|
|
uart_data_ready = 1; // 设置数据就绪标志
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 缓冲区溢出处理
|
|
|
|
|
|
uart_rx_count = 0; // 重置计数器
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 增强的中断接收函数
|
|
|
|
|
|
void UART_ReceiveDataFromISR(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!UART_RX_FIFO_EMPTY(CMSDK_UART1))
|
|
|
|
|
|
{
|
|
|
|
|
|
uint8_t received_data = READ_UART_RCVBuff(CMSDK_UART1);
|
|
|
|
|
|
if(uart_rx_count < sizeof(uart_rx_buffer))
|
|
|
|
|
|
{
|
|
|
|
|
|
uart_rx_buffer[uart_rx_count] = received_data;
|
|
|
|
|
|
uart_rx_count++;
|
|
|
|
|
|
// 注意:在中断中不建议使用printf,只设置标志
|
|
|
|
|
|
uart_data_ready = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UART数据发送函数(可选的)
|
|
|
|
|
|
void UART_SendData(uint8_t data)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!UART_TX_FIFO_FULL(CMSDK_UART1)) // 如果发送FIFO未满
|
|
|
|
|
|
{
|
|
|
|
|
|
WRITE_UART_THRBuff(CMSDK_UART1, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UART数据包解析函数
|
|
|
|
|
|
uint8_t ParseUART_EMS_Packet(uint8_t *data, uint16_t length, UART_EMS_Packet_t *packet)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 解析数据包(注意字节序)
|
|
|
|
|
|
packet->function_code = (data[1] << 8) | data[0]; // 小端序
|
|
|
|
|
|
packet->data_length = (data[3] << 8) | data[2];
|
|
|
|
|
|
packet->switch_type = data[4];
|
|
|
|
|
|
packet->intensity = data[5];
|
|
|
|
|
|
packet->frequency = (data[7] << 8) | data[6];
|
|
|
|
|
|
packet->duration = (data[9] << 8) | data[8];
|
|
|
|
|
|
packet->rest_time = (data[11] << 8) | data[10];
|
|
|
|
|
|
packet->silent_time = (data[13] << 8) | data[12];
|
|
|
|
|
|
packet->ramp_up_time = data[14];
|
|
|
|
|
|
packet->hold_time = data[15];
|
|
|
|
|
|
packet->ramp_down_time = data[16];
|
|
|
|
|
|
packet->crc16 = (data[18] << 8) | data[17];
|
|
|
|
|
|
|
|
|
|
|
|
// CRC-16-CCITT-FALSE校验(对前17字节进行校验,不包括CRC本身)
|
|
|
|
|
|
uint16_t calculated_crc = CalculateCRC16_CCITT_FALSE(data, 17);
|
|
|
|
|
|
/*if(calculated_crc != packet->crc16)
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
// 功能码检查
|
|
|
|
|
|
|
|
|
|
|
|
return 1; // 解析成功
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 根据UART数据包更新电刺激配置
|
|
|
|
|
|
void UpdateEMS_ConfigFromUART(UART_EMS_Packet_t *packet)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 检查开关状态
|
|
|
|
|
|
if(packet->switch_type == 0x00)
|
|
|
|
|
|
{
|
|
|
|
|
|
EMS_Stop();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查电刺激类型 (0x10~0x1F)
|
|
|
|
|
|
if(packet->switch_type < 0x10 || packet->switch_type > 0x1F)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建新的电刺激配置
|
|
|
|
|
|
EMS_Config_TypeDef new_config = {
|
|
|
|
|
|
.frequency = packet->frequency,
|
|
|
|
|
|
.duration = packet->duration,
|
|
|
|
|
|
.intensity = packet->intensity,
|
|
|
|
|
|
.rest_time = packet->rest_time,
|
|
|
|
|
|
.silent_time = packet->silent_time,
|
|
|
|
|
|
.ramp_up_time = packet->ramp_up_time,
|
|
|
|
|
|
.hold_time = packet->hold_time,
|
|
|
|
|
|
.ramp_down_time = packet->ramp_down_time,
|
|
|
|
|
|
.enable_ramp = 1 // 启用渐进控制
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-10-09 11:29:11 +08:00
|
|
|
|
|
2025-09-30 14:07:39 +08:00
|
|
|
|
printf("0x%02X\n", packet->switch_type);
|
|
|
|
|
|
printf("%d\n", packet->intensity);
|
|
|
|
|
|
printf("%d\n", packet->frequency);
|
|
|
|
|
|
printf("%d\n", packet->duration);
|
|
|
|
|
|
printf("%d\n", packet->rest_time);
|
|
|
|
|
|
printf("%d\n", packet->silent_time);
|
|
|
|
|
|
printf("%d\n", packet->ramp_up_time);
|
|
|
|
|
|
printf("%d\n", packet->hold_time);
|
|
|
|
|
|
printf("%d\n", packet->ramp_down_time);
|
|
|
|
|
|
|
|
|
|
|
|
// 应用新配置
|
|
|
|
|
|
EMS_Configure(&new_config);
|
|
|
|
|
|
|
|
|
|
|
|
// 启动电刺激
|
|
|
|
|
|
EMS_Start();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-09-10 18:07:54 +08:00
|
|
|
|
|
2025-08-13 16:43:29 +08:00
|
|
|
|
int main(){
|
2025-08-29 11:30:52 +08:00
|
|
|
|
// 初始化系统
|
2025-08-13 16:43:29 +08:00
|
|
|
|
MTP_init();
|
|
|
|
|
|
ClockInit();
|
2025-08-20 11:03:53 +08:00
|
|
|
|
|
2025-08-29 11:30:52 +08:00
|
|
|
|
// 初始化GPIO19用于定时器指示
|
|
|
|
|
|
GPIO_IO_Init(GPIO_19, OUTPUT, GPIO_OType_PP, GPIO_NOPULL, OUTPUT_FAST, PDRV_4mA, ENABLE);
|
2025-08-13 16:43:29 +08:00
|
|
|
|
GPIO_Output(GPIO_19, LOW_LEVEL);
|
2025-08-20 11:03:53 +08:00
|
|
|
|
|
2025-08-29 11:30:52 +08:00
|
|
|
|
// 初始化UART
|
2025-08-13 16:43:29 +08:00
|
|
|
|
UART_Init(CMSDK_UART1, &UART1_Init);
|
|
|
|
|
|
UART_ITConfig(CMSDK_UART1, &UART1_ITSet);
|
2025-09-30 14:07:39 +08:00
|
|
|
|
|
2025-09-11 10:41:05 +08:00
|
|
|
|
// 配置电刺激参数
|
|
|
|
|
|
EMS_Configure(&ems_config);
|
2025-09-10 18:07:54 +08:00
|
|
|
|
// 初始化时间管理器
|
|
|
|
|
|
Time_Manager_Init();
|
2025-08-29 11:30:52 +08:00
|
|
|
|
// 初始化定时器(在wavegen_Init之前)
|
2025-08-20 11:03:53 +08:00
|
|
|
|
TIMER0_Init(1);
|
|
|
|
|
|
|
2025-09-11 10:41:05 +08:00
|
|
|
|
// 初始化波形生成器(电刺激)- 必须先初始化
|
2025-08-20 11:03:53 +08:00
|
|
|
|
wavegen_Init();
|
2025-08-29 11:30:52 +08:00
|
|
|
|
// 启动电刺激
|
2025-08-20 11:03:53 +08:00
|
|
|
|
EMS_Start();
|
2025-08-29 11:30:52 +08:00
|
|
|
|
|
2025-08-13 16:43:29 +08:00
|
|
|
|
while(1)
|
2025-08-29 11:30:52 +08:00
|
|
|
|
{
|
2025-08-13 16:43:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-30 14:07:39 +08:00
|
|
|
|
}
|