A141/Core/Src/flash.c

200 lines
3.3 KiB
C
Raw Normal View History

new file: .cproject new file: .gitignore new file: .mxproject new file: .project new file: .settings/com.atollic.truestudio.debug.hardware_device.prefs new file: .settings/language.settings.xml new file: .settings/org.eclipse.cdt.managedbuilder.core.prefs new file: .settings/org.eclipse.core.resources.prefs new file: A141 Amplifier.elf.launch new file: Amplifier.elf.launch new file: Amplifier.ioc new file: Core/Inc/flash.h new file: Core/Inc/gpio.h new file: Core/Inc/main.h new file: Core/Inc/my.h new file: Core/Inc/stm32l0xx_hal_conf.h new file: Core/Inc/stm32l0xx_it.h new file: Core/Inc/tim.h new file: Core/Inc/usart.h new file: Core/Src/amplif.c new file: Core/Src/flash.c new file: Core/Src/gpio.c new file: Core/Src/main.c new file: Core/Src/stm32l0xx_hal_msp.c new file: Core/Src/stm32l0xx_it.c new file: Core/Src/syscalls.c new file: Core/Src/system_stm32l0xx.c new file: Core/Src/tim.c new file: Core/Src/usart.c new file: Debug/A141 Amplifier.list new file: Debug/A141 Amplifier.map new file: Debug/Amplifier.list new file: Debug/Amplifier.map new file: Drivers/CMSIS/Device/ST/STM32L0xx/Include/stm32l072xx.h new file: Drivers/CMSIS/Device/ST/STM32L0xx/Include/stm32l0xx.h new file: Drivers/CMSIS/Device/ST/STM32L0xx/Include/system_stm32l0xx.h new file: Drivers/CMSIS/Include/cmsis_armcc.h new file: Drivers/CMSIS/Include/cmsis_armclang.h new file: Drivers/CMSIS/Include/cmsis_compiler.h new file: Drivers/CMSIS/Include/cmsis_gcc.h new file: Drivers/CMSIS/Include/cmsis_iccarm.h new file: Drivers/CMSIS/Include/cmsis_version.h new file: Drivers/CMSIS/Include/core_armv8mbl.h new file: Drivers/CMSIS/Include/core_armv8mml.h new file: Drivers/CMSIS/Include/core_cm0.h new file: Drivers/CMSIS/Include/core_cm0plus.h new file: Drivers/CMSIS/Include/core_cm1.h new file: Drivers/CMSIS/Include/core_cm23.h new file: Drivers/CMSIS/Include/core_cm3.h new file: Drivers/CMSIS/Include/core_cm33.h new file: Drivers/CMSIS/Include/core_cm4.h new file: Drivers/CMSIS/Include/core_cm7.h new file: Drivers/CMSIS/Include/core_sc000.h new file: Drivers/CMSIS/Include/core_sc300.h new file: Drivers/CMSIS/Include/mpu_armv7.h new file: Drivers/CMSIS/Include/mpu_armv8.h new file: Drivers/CMSIS/Include/tz_context.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_cortex.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_def.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_dma.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_exti.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_flash.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_flash_ex.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_flash_ramfunc.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_gpio.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_gpio_ex.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_i2c.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_i2c_ex.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_pwr.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_pwr_ex.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_rcc.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_rcc_ex.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_tim.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_tim_ex.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_uart.h new file: Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_hal_uart_ex.h new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_cortex.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_dma.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_exti.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_flash.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_flash_ex.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_flash_ramfunc.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_gpio.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_i2c.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_i2c_ex.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_pwr.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_pwr_ex.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_rcc.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_rcc_ex.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_tim.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_tim_ex.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_uart.c new file: Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_uart_ex.c new file: STM32L072CB_FLASH.ld new file: startup/startup_stm32l072xx.s
2023-12-19 15:23:01 +03:00
/*
* flash.c
*
* Created on: 18 <EFBFBD><EFBFBD><EFBFBD> 2021 <EFBFBD>.
* Author: Toporov
*/
#include "my.h"
#include "main.h"
#include "flash.h"
#include "stm32l0xx_hal_flash.h"
#include "stm32l0xx_hal_flash_ex.h"
extern void FLASH_PageErase(uint32_t PageAddress);
static const uint16_t FACTORY_CORR[2][20] = {
{
3814, 3726, 3656, 3811, 3727, 3657, 3637, 3570, 3504, 3485, 3630, 3563, 3560, 3750, 3750, 3750, 3750, 3750, 3750, 3750
},
{
3750, 3750, 3750, 3812, 3727, 3654, 3637, 3568, 3502, 3486, 3596, 3537, 3533, 3750, 3750, 3750, 3750, 3750, 3750, 3750
}
};
__IO UserData_TypeDef pardata;
__IO uint16_t CorrWord[2][20];
void MX_FLASH_Init(void)
{
__HAL_RCC_SYSCFG_CLK_ENABLE();
rdPar();
rdCorr();
}
void wrPar(void)
{
uint8_t i, len;
uint32_t *pData, Address;
len = sizeof(UserData_TypeDef);
len >>= 2;
HAL_FLASH_Unlock();
FLASH_PageErase(USERPAGE);
CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
FLASH_WaitForLastOperation(100);
Address = USERPAGE;
pData = (uint32_t *) &pardata;
for(i = 0; i < len; i++)
{
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, *pData++);
Address += 4;
}
CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
HAL_FLASH_Lock();
}
void rdPar(void)
{
uint8_t rewrite = 0;
memcpy((void *) &pardata, (void *) USERPAGE, sizeof(UserData_TypeDef));
//pardata.OWN = 0;
if((pardata.OWN != MY_ADDRESS) || (pardata.OWN == 0))
{
pardata.OWN = MY_ADDRESS;
pardata.BAUD = 7; //115200
pardata.INFB = 0;
pardata.IIN = CHARGE;
pardata.IFV = Hp0_2;
pardata.IFN = Lp100000;
pardata.IKU = Ku1;
pardata.IKE = 0;
pardata.IKD = 0;
pardata.IKS = 1;
pardata.IPZ = 0;
pardata.OPZ = 0;
pardata.VAL = Accel;
pardata.KCOND = 1.00001f;
pardata.SENS = 1.00001f;
pardata.ACCEL = 10.00001f;
rewrite = 1;
}
if((pardata.IKE == 0) && (pardata.IKD == 0) && (pardata.IKS == 0)) {
pardata.IKE = 0;
pardata.IKD = 0;
pardata.IKS = 1;
rewrite = true;
}
if(pardata.SENS == 0.0f) {
pardata.SENS = 1.0f;
rewrite = true;
}
if(pardata.IFN > Lp100000) {
pardata.IFN = Lp100000;
rewrite = true;
}
if(pardata.IFV > Hp10) {
pardata.IFN = Hp10;
rewrite = true;
}
if(pardata.IKU > Ku1000) {
pardata.IKU = Ku1000;
rewrite = true;
}
if(pardata.VAL > Nython) {
pardata.VAL = Accel;
rewrite = true;
}
/////////////////////////////
if(rewrite)
{
rewrite = 0;
wrPar();
}
/////////////////////////////
}
void wrCorr(void)
{
uint8_t i, len;
uint32_t *pData, Address;
len = sizeof(CorrWord);
len >>= 2;
HAL_FLASH_Unlock();
FLASH_PageErase(CORRPAGE);
CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
FLASH_WaitForLastOperation(100);
Address = CORRPAGE;
pData = (uint32_t *) &CorrWord;
for(i = 0; i < len; i++)
{
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, *pData++);
Address += 4;
}
CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
HAL_FLASH_Lock();
}
void rdCorr(void)
{
uint8_t i, j;
memcpy((void *) &CorrWord, (void *) CORRPAGE, sizeof(CorrWord));
if(((CorrWord[0][0] == 0xffff) || (CorrWord[0][1] == 0xffff)) || ((CorrWord[0][0] == 0) || (CorrWord[0][1] == 0)))
{
for(j = 0; j < 2; j++)
{
for(i = 0; i < 20; i++) {
CorrWord[j][i] = FACTORY_CORR[j][i];
}
}
wrCorr();
}
}