添加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];
}
}

View File

@ -24,6 +24,7 @@
#include "delay.h"
#include "tim.h"
#include "data_analyse.h"
#include "stm32h7_flash.h"
void mymain();
void commander_run(void);
#endif //FOC_N_MYMAIN_H

285
APP/stm32h7_flash.c Normal file
View File

@ -0,0 +1,285 @@
//
// Created by hu123456 on 2024/5/22.
//
#include "stm32h7_flash.h"
uint32_t GetSector(uint32_t Address)
{
uint32_t sector = 0;
if (((Address < ADDR_FLASH_SECTOR_1_BANK1) && (Address >= ADDR_FLASH_SECTOR_0_BANK1)) || \
((Address < ADDR_FLASH_SECTOR_1_BANK2) && (Address >= ADDR_FLASH_SECTOR_0_BANK2)))
{
sector = FLASH_SECTOR_0;
}
else if (((Address < ADDR_FLASH_SECTOR_2_BANK1) && (Address >= ADDR_FLASH_SECTOR_1_BANK1)) || \
((Address < ADDR_FLASH_SECTOR_2_BANK2) && (Address >= ADDR_FLASH_SECTOR_1_BANK2)))
{
sector = FLASH_SECTOR_1;
}
else if (((Address < ADDR_FLASH_SECTOR_3_BANK1) && (Address >= ADDR_FLASH_SECTOR_2_BANK1)) || \
((Address < ADDR_FLASH_SECTOR_3_BANK2) && (Address >= ADDR_FLASH_SECTOR_2_BANK2)))
{
sector = FLASH_SECTOR_2;
}
else if (((Address < ADDR_FLASH_SECTOR_4_BANK1) && (Address >= ADDR_FLASH_SECTOR_3_BANK1)) || \
((Address < ADDR_FLASH_SECTOR_4_BANK2) && (Address >= ADDR_FLASH_SECTOR_3_BANK2)))
{
sector = FLASH_SECTOR_3;
}
else if (((Address < ADDR_FLASH_SECTOR_5_BANK1) && (Address >= ADDR_FLASH_SECTOR_4_BANK1)) || \
((Address < ADDR_FLASH_SECTOR_5_BANK2) && (Address >= ADDR_FLASH_SECTOR_4_BANK2)))
{
sector = FLASH_SECTOR_4;
}
else if (((Address < ADDR_FLASH_SECTOR_6_BANK1) && (Address >= ADDR_FLASH_SECTOR_5_BANK1)) || \
((Address < ADDR_FLASH_SECTOR_6_BANK2) && (Address >= ADDR_FLASH_SECTOR_5_BANK2)))
{
sector = FLASH_SECTOR_5;
}
else if (((Address < ADDR_FLASH_SECTOR_7_BANK1) && (Address >= ADDR_FLASH_SECTOR_6_BANK1)) || \
((Address < ADDR_FLASH_SECTOR_7_BANK2) && (Address >= ADDR_FLASH_SECTOR_6_BANK2)))
{
sector = FLASH_SECTOR_6;
}
else if (((Address < ADDR_FLASH_SECTOR_0_BANK2) && (Address >= ADDR_FLASH_SECTOR_7_BANK1)) || \
((Address < CPU_FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_7_BANK2)))
{
sector = FLASH_SECTOR_7;
}
else
{
sector = FLASH_SECTOR_7;
}
return sector;
}
/*
***************************************************************************************************
*?? ??????flash????
*?? ?<3F><>?_ulFlashAddr????????flash???
* _ucpDst ????????
* _ulSize ???????<3F><>,??<3F><>????
* ?? ?? ???0??? 1?????????? 2<><32>?????
***************************************************************************************************
*/
uint8_t stm32h7_read_flash(uint32_t _ulFlashAddr, uint8_t *_ucpDst, uint32_t _ulSize)
{
uint32_t i;
if (_ulFlashAddr + _ulSize > CPU_FLASH_BASE_ADDR + CPU_FLASH_SIZE)
{
return 1;
}
if (_ulSize == 0)
{
return 1;
}
for (i = 0; i < _ulSize; i++)
{
*_ucpDst++ = *(uint8_t *)_ulFlashAddr++;
}
return 0;
}
/*
//???flash???????
?????: 0 <20><>???????flash??????? ???<3F><>???
1 ????????????<3F><>
2 ??????????<3F><>
3 ????????
*/
uint8_t cmp_flash(uint32_t _ulFlashAddr, uint8_t *_ucpBuf, uint32_t _ulSize)
{
uint32_t i;
uint8_t ucIsEqu;
uint8_t ucByte;
if (_ulFlashAddr + _ulSize > CPU_FLASH_BASE_ADDR + CPU_FLASH_SIZE)
{
return FLASH_PARAM_ERR;
}
if (_ulSize == 0)
{
return FLASH_IS_EQU;
}
ucIsEqu = 1;
for (i = 0; i < _ulSize; i++)
{
ucByte = *(uint8_t *)_ulFlashAddr;
if (ucByte != *_ucpBuf)
{
if (ucByte != 0xFF)
{
return FLASH_REQ_ERASE;
}
else
{
ucIsEqu = 0;
}
}
_ulFlashAddr++;
_ucpBuf++;
}
if (ucIsEqu == 1)
{
return FLASH_IS_EQU;
}
else
{
return FLASH_REQ_WRITE;
}
}
uint8_t erase_flash(uint32_t _ulFlashAddr)
{
uint32_t FirstSector = 0, NbOfSectors = 0;
FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t SECTORError = 0;
uint8_t re;
HAL_FLASH_Unlock();
FirstSector = GetSector(_ulFlashAddr);
NbOfSectors = 1;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
if (_ulFlashAddr >= ADDR_FLASH_SECTOR_0_BANK2)
{
EraseInitStruct.Banks = FLASH_BANK_2;
}
else
{
EraseInitStruct.Banks = FLASH_BANK_1;
}
EraseInitStruct.Sector = FirstSector;
EraseInitStruct.NbSectors = NbOfSectors;
re = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
HAL_FLASH_Lock();
return re;
}
/*
***************************************************************************************************
*?? ?????????<3F><>??flash??????32?????????<3F><>?????????????
*?? ?<3F><>?_ulFlashAddr??flash ???
* _ucpSrc ??<3F><>????????
* _ulSize ???????<3F><>,??<3F><>??????????32???????????
* ?? ?? ???0??? 1?????????? 2<><32>?????
***************************************************************************************************
*/
uint8_t stm32h7_write_flash(uint32_t _ulFlashAddr, uint8_t *_ucpSrc, uint32_t _ulSize)
{
uint32_t i;
uint8_t ucRet;
if (_ulFlashAddr + _ulSize > CPU_FLASH_BASE_ADDR + CPU_FLASH_SIZE)
{
return 1;
}
if (_ulSize == 0)
{
return 0;
}
ucRet = cmp_flash(_ulFlashAddr, _ucpSrc, _ulSize);
if (ucRet == FLASH_IS_EQU)
{
return 0;
}
__set_PRIMASK(1);//???<3F><>?
erase_flash(_ulFlashAddr);
HAL_FLASH_Unlock();
for (i = 0; i < _ulSize / 32; i++)
{
uint64_t FlashWord[4];
memcpy((char *)FlashWord, _ucpSrc, 32);
_ucpSrc += 32;
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, _ulFlashAddr, (uint64_t)((uint32_t)FlashWord)) == HAL_OK)
{
_ulFlashAddr = _ulFlashAddr + 32;
}
else
{
goto err;
}
}
if (_ulSize % 32)
{
uint64_t FlashWord[4];
FlashWord[0] = 0;
FlashWord[1] = 0;
FlashWord[2] = 0;
FlashWord[3] = 0;
memcpy((char *)FlashWord, _ucpSrc, _ulSize % 32);
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, _ulFlashAddr, (uint64_t)((uint32_t)FlashWord)) == HAL_OK)
{
; // _ulFlashAddr = _ulFlashAddr + 32;
}
else
{
goto err;
}
}
HAL_FLASH_Lock();
__set_PRIMASK(0);
return 0;
err:
HAL_FLASH_Lock();
__set_PRIMASK(0);
return 1;
}

58
APP/stm32h7_flash.h Normal file
View File

@ -0,0 +1,58 @@
//
// Created by hu123456 on 2024/5/22.
//
#ifndef FOC_N_STM32H7_FLASH_H
#define FOC_N_STM32H7_FLASH_H
#include "stm32h7xx_hal.h"
#include "main.h"
#include "mymain.h"
#include "string.h"
#define CPU_FLASH_BASE_ADDR (uint32_t)(FLASH_BASE) /* 0x08000000 */
#define CPU_FLASH_END_ADDR (uint32_t)(0x081FFFFF)
#define CPU_FLASH_SIZE (2 * 1024 * 1024) /* FLASH<53><48>????? */
#define CPU_FLASH_SECTOR_SIZE (128 * 1024) /* ?????<3F><>????<3F><>??? */
/* Base address of the Flash sectors Bank 1 */
#define ADDR_FLASH_SECTOR_0_BANK1 ((uint32_t)0x08000000) /* Base @ of Sector 0, 128 Kbytes */
#define ADDR_FLASH_SECTOR_1_BANK1 ((uint32_t)0x08020000) /* Base @ of Sector 1, 128 Kbytes */
#define ADDR_FLASH_SECTOR_2_BANK1 ((uint32_t)0x08040000) /* Base @ of Sector 2, 128 Kbytes */
#define ADDR_FLASH_SECTOR_3_BANK1 ((uint32_t)0x08060000) /* Base @ of Sector 3, 128 Kbytes */
#define ADDR_FLASH_SECTOR_4_BANK1 ((uint32_t)0x08080000) /* Base @ of Sector 4, 128 Kbytes */
#define ADDR_FLASH_SECTOR_5_BANK1 ((uint32_t)0x080A0000) /* Base @ of Sector 5, 128 Kbytes */
#define ADDR_FLASH_SECTOR_6_BANK1 ((uint32_t)0x080C0000) /* Base @ of Sector 6, 128 Kbytes */
#define ADDR_FLASH_SECTOR_7_BANK1 ((uint32_t)0x080E0000) /* Base @ of Sector 7, 128 Kbytes */
/* Base address of the Flash sectors Bank 2 */
#define ADDR_FLASH_SECTOR_0_BANK2 ((uint32_t)0x08100000) /* Base @ of Sector 0, 128 Kbytes */
#define ADDR_FLASH_SECTOR_1_BANK2 ((uint32_t)0x08120000) /* Base @ of Sector 1, 128 Kbytes */
#define ADDR_FLASH_SECTOR_2_BANK2 ((uint32_t)0x08140000) /* Base @ of Sector 2, 128 Kbytes */
#define ADDR_FLASH_SECTOR_3_BANK2 ((uint32_t)0x08160000) /* Base @ of Sector 3, 128 Kbytes */
#define ADDR_FLASH_SECTOR_4_BANK2 ((uint32_t)0x08180000) /* Base @ of Sector 4, 128 Kbytes */
#define ADDR_FLASH_SECTOR_5_BANK2 ((uint32_t)0x081A0000) /* Base @ of Sector 5, 128 Kbytes */
#define ADDR_FLASH_SECTOR_6_BANK2 ((uint32_t)0x081C0000) /* Base @ of Sector 6, 128 Kbytes */
#define ADDR_FLASH_SECTOR_7_BANK2 ((uint32_t)0x081E0000) /* Base @ of Sector 7, 128 Kbytes */
#define FLASH_IS_EQU 0
#define FLASH_REQ_WRITE 1
#define FLASH_REQ_ERASE 2
#define FLASH_PARAM_ERR 3
uint8_t stm32h7_read_flash(uint32_t _ulFlashAddr, uint8_t *_ucpDst, uint32_t _ulSize);
uint8_t stm32h7_write_flash(uint32_t _ulFlashAddr, uint8_t *_ucpSrc, uint32_t _ulSize);
uint8_t cmp_flash(uint32_t _ulFlashAddr, uint8_t *_ucpBuf, uint32_t _ulSize);
#endif //FOC_N_STM32H7_FLASH_H

View File

@ -1,7 +1,6 @@
//
// Created by hu123456 on 2024/8/19.
//
#include "usart1_tx_dma.h"
DMA_HandleTypeDef g_dma_handle;