Set blackbox file handler to NULL after closing file
[inav.git] / src / main / config / config_streamer_stm32h7.c
blobbaa41ea92bb5f1c37b1e0a85add282659893b66e
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <string.h>
19 #include "platform.h"
20 #include "drivers/system.h"
21 #include "config/config_streamer.h"
23 #if defined(STM32H7) && !defined(CONFIG_IN_RAM) && !defined(CONFIG_IN_EXTERNAL_FLASH)
25 #if defined(STM32H743xx)
26 /* Sectors 0-7 of 128K each */
27 #define FLASH_PAGE_SIZE ((uint32_t)0x20000) // 128K sectors
28 static uint32_t getFLASHSectorForEEPROM(uint32_t address)
30 if (address <= 0x0801FFFF)
31 return FLASH_SECTOR_0;
32 if (address <= 0x0803FFFF)
33 return FLASH_SECTOR_1;
34 if (address <= 0x0805FFFF)
35 return FLASH_SECTOR_2;
36 if (address <= 0x0807FFFF)
37 return FLASH_SECTOR_3;
38 if (address <= 0x0809FFFF)
39 return FLASH_SECTOR_4;
40 if (address <= 0x080BFFFF)
41 return FLASH_SECTOR_5;
42 if (address <= 0x080DFFFF)
43 return FLASH_SECTOR_6;
44 if (address <= 0x080FFFFF)
45 return FLASH_SECTOR_7;
47 while (1) {
48 failureMode(FAILURE_FLASH_WRITE_FAILED);
51 #elif defined(STM32H750xx)
52 # error "STM32750xx only has one flash page which contains the bootloader, no spare flash pages available, use external storage for persistent config or ram for target testing"
53 #else
54 # error "Unsupported CPU!"
55 #endif
57 void config_streamer_impl_unlock(void)
59 HAL_FLASH_Unlock();
62 void config_streamer_impl_lock(void)
64 HAL_FLASH_Lock();
67 int config_streamer_impl_write_word(config_streamer_t *c, config_streamer_buffer_align_type_t *buffer)
69 if (c->err != 0) {
70 return c->err;
73 if (c->address % FLASH_PAGE_SIZE == 0) {
74 FLASH_EraseInitTypeDef EraseInitStruct = {
75 .TypeErase = FLASH_TYPEERASE_SECTORS,
76 .VoltageRange = FLASH_VOLTAGE_RANGE_3, // 2.7-3.6V
77 .NbSectors = 1,
78 .Banks = FLASH_BANK_1
80 EraseInitStruct.Sector = getFLASHSectorForEEPROM(c->address);
82 uint32_t SECTORError;
83 const HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
84 if (status != HAL_OK) {
85 return -1;
89 // On H7 HAL_FLASH_Program takes data address, not the raw word value
90 const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, c->address, (uint32_t)buffer);
91 if (status != HAL_OK) {
92 return -2;
95 c->address += CONFIG_STREAMER_BUFFER_SIZE;
96 return 0;
99 #endif