添加flash

This commit is contained in:
2025-03-06 16:33:34 +08:00
parent 8648025432
commit 8603e73b45
94 changed files with 800 additions and 40308 deletions

View File

@ -4,9 +4,11 @@
#define USART2_RX_BUFFER_SIZE 516
#define Baudnum 512
#define USART_REC_LEN 200
#define USART_EN_RX 1
#define RXBUFFERSIZE 1
#define FLASH_SAVE_ADDR 0x080E0000 // BANK1 Sector 7
uint16_t g_usart_rx_sta = 0;
uint8_t g_usart_rx_buf[USART_REC_LEN];
uint8_t g_rx_buffer[RXBUFFERSIZE];
@ -22,7 +24,7 @@ uint16_t Moving_Average_Buffer[USART2_RX_BUFFER_SIZE - 1];
uint32_t Receive_Data_Count = 0;
uint32_t Last_Receive_Data_Count = 0;
uint32_t shutter_time = 20;
uint32_t shutter_time = 15;
uint32_t mode = 1;
float Temperature = 0;
/* Send_Data_Type
@ -33,7 +35,11 @@ float Temperature = 0;
* 4 发送滑动平均数据
*/
uint8_t Send_Data_Type = 0;
uint32_t sn = 1699;
uint32_t sn = 1;
double bochangxishu[4] = {0,0,0,0};
void swap_buf(uint8_t *p , uint32_t size);
void Communication_Init() {
__HAL_UART_DISABLE(&huart2);
@ -64,37 +70,29 @@ void mymain()
setvbuf(stdout, NULL, _IONBF, 0);
dma_init(DMA2_Stream7, DMA_REQUEST_USART1_TX);
/////
// uint8_t p[4] = {0x00,0x01,0x5f,0x90};
// HAL_UART_Transmit(&huart2,p,4,100);
stm32h7_read_flash(FLASH_SAVE_ADDR, (uint8_t *)bochangxishu, 32); // Flash读取波长系数
DS18B20_Init();
// led_init();
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传输
Send_Shutter_Time(15);
// LED_OFF;
// // HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,0);
// LED_ON;
Send_Shutter_Time(shutter_time);
uint32_t Average_Times = 0;
uint32_t Moving_Average_Times = 0;
uint32_t a=0;
// uint32_t a=0;
while (1) {
while (mode == 0) {
commander_run();
if (Receive_Data_Count != Last_Receive_Data_Count) {
uint32_t a = Opt_Snenser(90, Receive_Data_Buffer ,USART2_RX_BUFFER_SIZE,shutter_time );
if (a !=0) shutter_time = a;
else mode = 1;
if (a != 0) shutter_time = a;
else {
mode = 1;
}
Last_Receive_Data_Count = Receive_Data_Count;
memset(g_usart_rx_buf,0,200);
g_usart_rx_sta=0;
}
@ -104,7 +102,7 @@ void mymain()
//处理指令
commander_run();
//处理数据
if (Receive_Data_Count != Last_Receive_Data_Count)
if (Receive_Data_Count > Last_Receive_Data_Count)
{
// Send_Data((uint8_t *)Receive_Data_Buffer,USART2_RX_BUFFER_SIZE*2);
//2多次平均
@ -125,41 +123,38 @@ void mymain()
Moving_Average_Buffer[i] += Moving_Buffer[j][i] / 10;
}
}
}
//按命令发送数据
if((Send_Data_Type & 0x80) !=0)
{
Send_Data_Type = Send_Data_Type & 0x7F;
uint16_t sgi_result[USART2_RX_BUFFER_SIZE];
// printf("AAA");
switch (Send_Data_Type)
Last_Receive_Data_Count = Receive_Data_Count;
//按命令发送数据
if((Send_Data_Type & 0x80) !=0)
{
case 0:
Send_Data(0x61,(uint8_t *)Receive_Data_Buffer,515*2);
Send_Data_Type = Send_Data_Type & 0x7F;
uint16_t sgi_result[USART2_RX_BUFFER_SIZE];
// printf("AAA");
switch (Send_Data_Type)
{
case 0:
Send_Data(0x61,(uint8_t *)Receive_Data_Buffer,515*2);
break;
case 1:
Send_Data(0x61,(uint8_t *)Average_Buffer,515*2);
case 1:
Send_Data(0x61,(uint8_t *)Average_Buffer,515*2);
break;
case 2:
sgi(Receive_Data_Buffer,sgi_result);
case 2:
sgi(Receive_Data_Buffer,sgi_result);
Send_Data(0x61,(uint8_t *)sgi_result,515*2);
break;
case 3:
sgi(Average_Buffer,sgi_result);
case 3:
sgi(Average_Buffer,sgi_result);
Send_Data(0x61,(uint8_t *)sgi_result,515*2);
break;
case 4:
Send_Data(0x61,(uint8_t *)Moving_Average_Buffer,515*2);
case 4:
Send_Data(0x61,(uint8_t *)Moving_Average_Buffer,515*2);
break;
}
Send_Data_Type = Send_Data_Type & 0x7F;
}
Send_Data_Type = Send_Data_Type & 0x7F;
}
// if ( a == 1300 * 15) {
// Temperature = DS18B20_GetTemperature();
// printf("Temperatureqqq: %f\n", Temperature);
// a = 0;
// }
a++;
}
}
}
@ -200,12 +195,15 @@ void commander_run(void)
{
if(g_usart_rx_sta > 7)
{
uint16_t data_length = g_usart_rx_sta & 0x3fff;
HAL_Delay(1);
uint16_t data_length = g_usart_rx_sta;
// printf("data_length %d \r\n",data_length);
uint8_t data_type = 0;
uint8_t command_data[USART_REC_LEN] = {0};
// data_length = IRIS_Cut_Befor_Header(g_usart_rx_buf,data_length);
int ret = IRIS_STM32_Protocol_Unpack(g_usart_rx_buf,data_length,&data_type,command_data);
// printf( "ret %d \r\n",ret);
if (ret > 0) {
switch(data_type)
{
@ -232,27 +230,23 @@ void commander_run(void)
shutter_time = command_data[0]<<24 | command_data[1]<<16 | command_data[2]<<8 | command_data[3];
Send_Shutter_Time(shutter_time);
// memset()
// Receive_Data_Count = 0;
Last_Receive_Data_Count ++;
Send_Data(0x51,command_data,4);
break;
}
//自动曝光
case 0x52:{
mode = 0;
Send_Data(0x52,command_data,1);
break;
}
//获取曝光时间
case 0x53:{
uint8_t st[4];
// memcpy(st,&shutter_time,4);
st[3] = shutter_time & 0xff;
st[2] = (shutter_time >> 8) & 0xff;
st[1] = (shutter_time >> 16) & 0xff;
st[0] = (shutter_time >> 24) & 0xff;
memcpy(st,&shutter_time,4);
swap_buf(st,4);
// printf("shutter_time %d\n",shutter_time);
Send_Data(0x53,st,4);
break;
}
@ -261,10 +255,8 @@ void commander_run(void)
// 将Temperature的值转换为uint8_t数组并将其作为参数传递给Send_Data函数。
uint8_t temp[4];
Temperature = DS18B20_GetTemperature();
memcpy(&temp[0],(uint8_t *)&Temperature + 3,1);
memcpy(&temp[1],(uint8_t *)&Temperature + 2,1);
memcpy(&temp[2],(uint8_t *)&Temperature + 1,1);
memcpy(&temp[3],(uint8_t *)&Temperature + 0,1);
memcpy(temp,&Temperature,4);
swap_buf(temp,4);
Send_Data(0x54,temp,4);
break;
@ -281,6 +273,42 @@ void commander_run(void)
Send_Data(0x56,command_data,1);
break;
}
//获取波长数
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;
}
//设置数据处理方式
case 0x60: {
@ -292,6 +320,7 @@ void commander_run(void)
//获取数据
case 0x61: {
Send_Data_Type = Send_Data_Type | 0x80;
// Send_Data(0x61,command_data,5);
break;
}
case 0x62: {
@ -305,4 +334,13 @@ void commander_run(void)
memset(g_usart_rx_buf,0,200);
g_usart_rx_sta=0;
}
}
}
//实现大小端交换
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];
}
}