Files
IS3/APP/mymain.c

359 lines
12 KiB
C
Raw Normal View History

2025-02-19 14:46:14 +08:00
////////// Created by lijie on 2025/2/6.
#include "mymain.h"
#define USART2_RX_BUFFER_SIZE 516
2025-03-06 16:33:34 +08:00
#define Baudnum 512
2025-02-19 14:46:14 +08:00
#define USART_REC_LEN 200
#define USART_EN_RX 1
#define RXBUFFERSIZE 1
2025-03-06 16:33:34 +08:00
#define FLASH_SAVE_ADDR 0x080E0000 // BANK1 Sector 7
2025-02-19 14:46:14 +08:00
uint16_t g_usart_rx_sta = 0;
uint8_t g_usart_rx_buf[USART_REC_LEN];
uint8_t g_rx_buffer[RXBUFFERSIZE];
//平均次数
uint8_t Target_Average_Times = 1;
uint16_t USART2_RX_Buffer[USART2_RX_BUFFER_SIZE];
2025-03-12 15:03:24 +08:00
uint16_t Receive_Data_Buffer[USART2_RX_BUFFER_SIZE - 1];
2025-02-19 14:46:14 +08:00
uint16_t Average_Buffer[USART2_RX_BUFFER_SIZE - 1];
uint16_t Moving_Buffer[10][USART2_RX_BUFFER_SIZE - 1];
uint16_t Moving_Average_Buffer[USART2_RX_BUFFER_SIZE - 1];
uint32_t Receive_Data_Count = 0;
uint32_t Last_Receive_Data_Count = 0;
2025-03-06 16:33:34 +08:00
uint32_t shutter_time = 15;
2025-03-05 15:07:16 +08:00
uint32_t mode = 1;
2025-02-19 14:46:14 +08:00
float Temperature = 0;
/* Send_Data_Type
* 0
* 1
* 2 sgi数据
* 3
* 4
*/
2025-03-12 15:03:24 +08:00
uint8_t Send_Data_Type = 0;
2025-03-06 16:33:34 +08:00
uint32_t sn = 1;
2025-03-12 15:03:24 +08:00
uint8_t busy_sta = 0;
2025-03-06 16:33:34 +08:00
double bochangxishu[4] = {0,0,0,0};
void swap_buf(uint8_t *p , uint32_t size);
2025-02-19 14:46:14 +08:00
void Communication_Init() {
__HAL_UART_DISABLE(&huart2);
HAL_UART_Receive_DMA(&huart2, (uint8_t *)USART2_RX_Buffer, USART2_RX_BUFFER_SIZE*2);
USART2->CR3 |= USART_CR3_DMAT; // 启用DMA传输
}
void Send_Data(uint8_t command,uint8_t *pData, uint16_t Size) {
if (__HAL_DMA_GET_FLAG(&g_dma_handle, DMA_FLAG_TCIF3_7))
{
__HAL_DMA_CLEAR_FLAG(&g_dma_handle, DMA_FLAG_TCIF3_7);
HAL_UART_DMAStop(&huart1);
}
uint8_t send_buff[1024*2];
uint32_t send_lenth;
send_lenth = IRIS_Protocol_Pack(command,Size,pData,send_buff);
// printf("Send_Data: %s\n", send_buff);
HAL_UART_Transmit_DMA(&huart1,(uint8_t *)send_buff,send_lenth);
}
void mymain()
{
HAL_UART_Receive_IT(&huart1, (uint8_t *)g_rx_buffer, RXBUFFERSIZE);
2025-03-12 15:03:24 +08:00
2025-02-19 14:46:14 +08:00
setvbuf(stdout, NULL, _IONBF, 0);
dma_init(DMA2_Stream7, DMA_REQUEST_USART1_TX);
/////
2025-03-06 16:33:34 +08:00
stm32h7_read_flash(FLASH_SAVE_ADDR, (uint8_t *)bochangxishu, 32); // Flash读取波长系数
2025-02-19 14:46:14 +08:00
DS18B20_Init();
2025-03-06 16:33:34 +08:00
Communication_Init();
2025-02-19 14:46:14 +08:00
2025-03-06 16:33:34 +08:00
Send_Shutter_Time(shutter_time);
2025-02-19 14:46:14 +08:00
uint32_t Average_Times = 0;
uint32_t Moving_Average_Times = 0;
2025-03-06 16:33:34 +08:00
// uint32_t a=0;
2025-02-19 14:46:14 +08:00
while (1) {
while (mode == 0) {
2025-03-06 17:37:06 +08:00
shutter_time = 0x0000;
2025-02-19 14:46:14 +08:00
commander_run();
if (Receive_Data_Count != Last_Receive_Data_Count) {
2025-03-06 17:37:06 +08:00
uint32_t a = Opt_Snenser(90, Receive_Data_Buffer ,USART2_RX_BUFFER_SIZE);
if (a != 0) {
shutter_time = a;
2025-03-06 16:33:34 +08:00
mode = 1;
2025-03-12 15:03:24 +08:00
busy_sta = 0;
2025-03-06 16:33:34 +08:00
}
Last_Receive_Data_Count = Receive_Data_Count;
2025-02-19 14:46:14 +08:00
memset(g_usart_rx_buf,0,200);
g_usart_rx_sta=0;
}
}
while(mode == 1) {
//处理指令
commander_run();
//处理数据
2025-03-06 16:33:34 +08:00
if (Receive_Data_Count > Last_Receive_Data_Count)
2025-02-19 14:46:14 +08:00
{
//2多次平均
Average_Times ++;
if(Average_Times <= Target_Average_Times ) {
for (uint32_t i = 0; i < USART2_RX_BUFFER_SIZE; i++) {
Average_Buffer[i] = (Average_Buffer[i] * (Average_Times-1) + Receive_Data_Buffer[i]) / Average_Times;
}
}else {
Average_Times = 0;
}
//滑动平均
memcpy(&Moving_Average_Buffer[Moving_Average_Times % 10], Receive_Data_Buffer, USART2_RX_BUFFER_SIZE*2);
Moving_Average_Times ++;
for (uint32_t i = 0; i < USART2_RX_BUFFER_SIZE; i++) {
for (uint32_t j = 0; j < 10; j++) {
Moving_Average_Buffer[i] += Moving_Buffer[j][i] / 10;
}
}
2025-03-06 16:33:34 +08:00
Last_Receive_Data_Count = Receive_Data_Count;
//按命令发送数据
if((Send_Data_Type & 0x80) !=0)
2025-02-19 14:46:14 +08:00
{
2025-03-06 16:33:34 +08:00
Send_Data_Type = Send_Data_Type & 0x7F;
2025-03-12 15:03:24 +08:00
uint16_t sgi_result[USART2_RX_BUFFER_SIZE - 1];
2025-03-06 16:33:34 +08:00
// printf("AAA");
switch (Send_Data_Type)
{
case 0:
Send_Data(0x61,(uint8_t *)Receive_Data_Buffer,515*2);
2025-02-19 14:46:14 +08:00
break;
2025-03-06 16:33:34 +08:00
case 1:
Send_Data(0x61,(uint8_t *)Average_Buffer,515*2);
2025-02-19 14:46:14 +08:00
break;
2025-03-06 16:33:34 +08:00
case 2:
sgi(Receive_Data_Buffer,sgi_result);
2025-03-06 17:37:06 +08:00
Send_Data(0x61,(uint8_t *)sgi_result,515*2);
2025-02-19 14:46:14 +08:00
break;
2025-03-06 16:33:34 +08:00
case 3:
sgi(Average_Buffer,sgi_result);
2025-03-06 17:37:06 +08:00
Send_Data(0x61,(uint8_t *)sgi_result,515*2);
2025-02-19 14:46:14 +08:00
break;
2025-03-06 16:33:34 +08:00
case 4:
Send_Data(0x61,(uint8_t *)Moving_Average_Buffer,515*2);
2025-02-19 14:46:14 +08:00
break;
2025-03-06 16:33:34 +08:00
}
Send_Data_Type = Send_Data_Type & 0x7F;
2025-03-12 15:03:24 +08:00
busy_sta = 0;
2025-02-19 14:46:14 +08:00
}
}
}
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
// 当DMA接收完成时这个回调函数会被触发
if (huart->Instance == USART2)
{
__HAL_UART_DISABLE(&huart2);
2025-03-12 15:03:24 +08:00
memcpy((uint8_t *)Receive_Data_Buffer,&USART2_RX_Buffer[2], (USART2_RX_BUFFER_SIZE-2)*2);
2025-02-19 14:46:14 +08:00
Receive_Data_Count ++;
2025-03-12 15:03:24 +08:00
// if (__HAL_DMA_GET_FLAG(&g_dma_handle, DMA_FLAG_TCIF3_7))
// {
// __HAL_DMA_CLEAR_FLAG(&g_dma_handle, DMA_FLAG_TCIF3_7);
// HAL_UART_DMAStop(&huart1);
// }
// HAL_UART_Transmit_DMA(&huart1,(uint8_t *)Receive_Data_Buffer,USART2_RX_BUFFER_SIZE*2);
2025-02-19 14:46:14 +08:00
HAL_UART_Receive_DMA(&huart2, (uint8_t *)USART2_RX_Buffer, USART2_RX_BUFFER_SIZE*2);
}
if(huart->Instance == USART1)
{
g_usart_rx_buf[g_usart_rx_sta] = g_rx_buffer[0];
g_usart_rx_sta++;
HAL_UART_Receive_IT(&huart1, (uint8_t *)g_rx_buffer, RXBUFFERSIZE);
}
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == GPIO_PIN_0)
{
__HAL_UART_ENABLE(&huart2);
}
}
void commander_run(void)
{
if(g_usart_rx_sta > 7)
{
2025-03-06 16:33:34 +08:00
HAL_Delay(1);
uint16_t data_length = g_usart_rx_sta;
// printf("data_length %d \r\n",data_length);
2025-02-19 14:46:14 +08:00
uint8_t data_type = 0;
uint8_t command_data[USART_REC_LEN] = {0};
2025-03-12 15:03:24 +08:00
data_length = IRIS_Cut_Befor_Header(g_usart_rx_buf,data_length);
2025-02-19 14:46:14 +08:00
int ret = IRIS_STM32_Protocol_Unpack(g_usart_rx_buf,data_length,&data_type,command_data);
2025-03-06 16:33:34 +08:00
// printf( "ret %d \r\n",ret);
2025-02-19 14:46:14 +08:00
if (ret > 0) {
2025-03-12 15:03:24 +08:00
if (busy_sta == 1) {
uint8_t str = 0xff;
Send_Data(0x40,&str,1);
memset(g_usart_rx_buf,0,200);
g_usart_rx_sta=0;
return;
}
2025-02-19 14:46:14 +08:00
switch(data_type)
{
// 获取设备信息
case 0x50: {
// printf("hello \n");
uint8_t str[8];
str[0] = 'I';
str[1] = 'S';
str[2] = '3';
str[3] = '-';
str[4] = '0'+ sn/1000%10;
str[5] = '0'+ sn/100%10;
str[6] = '0'+ sn/10%10;
str[7] = '0'+ sn%10;
// printf("shutter_time %d\n",shutter_time);
Send_Data(0x50,str,8);
break;
}
//设置曝光时间
case 0x51: {
shutter_time = command_data[0]<<24 | command_data[1]<<16 | command_data[2]<<8 | command_data[3];
Send_Shutter_Time(shutter_time);
// memset()
2025-03-06 16:33:34 +08:00
// Receive_Data_Count = 0;
Last_Receive_Data_Count ++;
2025-02-19 14:46:14 +08:00
Send_Data(0x51,command_data,4);
break;
}
//自动曝光
case 0x52:{
mode = 0;
2025-03-12 15:03:24 +08:00
busy_sta = 1;
2025-02-19 14:46:14 +08:00
Send_Data(0x52,command_data,1);
break;
}
//获取曝光时间
case 0x53:{
2025-03-05 15:07:16 +08:00
uint8_t st[4];
2025-03-06 16:33:34 +08:00
memcpy(st,&shutter_time,4);
swap_buf(st,4);
2025-03-05 15:07:16 +08:00
Send_Data(0x53,st,4);
2025-02-19 14:46:14 +08:00
break;
}
//获取温度
case 0x54: {
// 将Temperature的值转换为uint8_t数组并将其作为参数传递给Send_Data函数。
uint8_t temp[4];
Temperature = DS18B20_GetTemperature();
2025-03-06 16:33:34 +08:00
memcpy(temp,&Temperature,4);
swap_buf(temp,4);
2025-02-19 14:46:14 +08:00
Send_Data(0x54,temp,4);
break;
}
//开启快门
case 0x55: {
Shutter_Open(command_data[0]);
Send_Data(0x55,command_data,1);
break;
}
//关闭快门
case 0x56: {
Shutter_Close(command_data[0]);
Send_Data(0x56,command_data,1);
break;
}
2025-03-06 16:33:34 +08:00
//获取波长数
case 0x57: {
uint32_t d = Baudnum;
uint8_t st[4];
memcpy(st,&d,4);
swap_buf(st,4);
Send_Data(0x57,st,4);
break;
}
//设置波长系数
case 0x58: {
swap_buf(command_data,8);
swap_buf(command_data+8,8);
swap_buf(command_data+16,8);
swap_buf(command_data+24,8);
memcpy(bochangxishu,command_data,32);
uint8_t d = 0;
Send_Data(0x58,&d,1);
stm32h7_write_flash(FLASH_SAVE_ADDR,(uint8_t *)bochangxishu,32);
break;
}
//获取波长系数
case 0x59: {
double temp[4];
memcpy(temp,bochangxishu,32);
swap_buf((uint8_t *)temp,8);
swap_buf((uint8_t *)temp+8,8);
swap_buf((uint8_t *)temp+16,8);
swap_buf((uint8_t *)temp+24,8);
Send_Data(0x59,temp,32);
break;
}
2025-02-19 14:46:14 +08:00
//设置数据处理方式
case 0x60: {
Send_Data_Type = command_data[0];
Target_Average_Times = command_data[1]<<24 | command_data[2]<<16 | command_data[3]<<8 | command_data[4];
Send_Data(0x60,command_data,5);
break;
}
//获取数据
case 0x61: {
2025-03-12 15:03:24 +08:00
busy_sta = 1;
2025-03-05 15:07:16 +08:00
Send_Data_Type = Send_Data_Type | 0x80;
2025-03-06 16:33:34 +08:00
// Send_Data(0x61,command_data,5);
2025-03-05 15:07:16 +08:00
break;
}
case 0x62: {
uint8_t d = Send_Data_Type & 0x7F;
Send_Data(0x62,&d,1);
2025-02-19 14:46:14 +08:00
break;
}
}
}
memset(g_usart_rx_buf,0,200);
g_usart_rx_sta=0;
}
2025-03-06 16:33:34 +08:00
}
//实现大小端交换
void swap_buf(uint8_t *p , uint32_t size) {
uint8_t temp_buf[size];
memcpy(temp_buf,p,size);
for (int i = 0; i < size; i++) {
p[i] = temp_buf[size-i-1];
}
}