QuakeGod
2024-07-27 842bb64195f958b050867c50db66fc0aa413dafb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "stm32f0xx_ll_flash.h"
 
 
void static LL_FLASH_Program_TwoBtye(uint32_t flash_addr,uint16_t data)
{
    LL_FLASH_EnableProgram(FLASH);
    *(__IO uint16_t*)(flash_addr) = data;
}
 
ErrorStatus LL_Flash_Unlock(void)
{
    while (LL_FLASH_IsActiveFlag_BSY(FLASH))  
    {
    } 
    if (LL_FLASH_LockState(FLASH)) 
    { 
        LL_FLASh_SetKey(FLASH,FLASH_KEY1);
        LL_FLASh_SetKey(FLASH,FLASH_KEY2);
    }
    return SUCCESS;
}
 
ErrorStatus LL_Flash_PageErase(void * page_addr,uint16_t Nb)
{
    uint32_t End_addr =  Nb* FLASH_PAGE_SIZE + (uint32_t)page_addr;
    uint32_t Start_addr = (uint32_t)page_addr;
    for(;Start_addr < End_addr;(Start_addr += FLASH_PAGE_SIZE))
    {
        LL_FLASH_SetTypeErase(FLASH,LL_FLASH_TYPEERASE_PAGES);
        LL_FLASH_SetEraseADDR(FLASH,Start_addr);
        LL_FLASH_StartErase(FLASH); 
        while (LL_FLASH_IsActiveFlag_BSY(FLASH)) 
        { 
        } 
        if (LL_FLASH_IsActiveFlag_EOP(FLASH)) 
        { 
            LL_FLASH_ClearFlag_EOP(FLASH);; 
        } 
        else
        { 
            return ERROR;
        }
        LL_FLASH_DisenableErase(FLASH,LL_FLASH_TYPEERASE_PAGES);
    }
    return SUCCESS;
}
 
ErrorStatus LL_FLASH_Program(ProgaramDataType ProgramType,uint32_t flash_addr,uint64_t data)
{
    
    uint8_t index = 0U;
    uint8_t nbiterations = 0U;
    
    if(ProgramType == ProgaraType_DATA16)
        nbiterations = 1U;
    else if(ProgramType == ProgaraType_DATA32)
        nbiterations = 2U;
    else
        nbiterations = 4U;
    for(index = 0U; index < nbiterations; index++)
    {
         LL_FLASH_Program_TwoBtye((flash_addr + (2U*index)), (uint16_t)(data >> (16U*index)));
    }
    
    while (LL_FLASH_IsActiveFlag_BSY(FLASH)) 
    {
    }
    if (LL_FLASH_IsActiveFlag_EOP(FLASH))    
    {
        LL_FLASH_ClearFlag_EOP(FLASH);
    }
    else
    {
        return ERROR;
    }
    LL_FLASH_DisenableProgram(FLASH);
    return SUCCESS;
}