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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
/**
******************************************************************************
* @file hw_flash.c
* @author MCD Application Team
******************************************************************************
* @attention
*
* Copyright (c) 2020-2021 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
//#include "common.h"
#include "stm32_seq.h"
//#include "dbg_gpio.h"
#include "hw_flash.h"
#include "stm32wbxx_hal.h"
static void HW_FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
static void HW_FLASH_PageErase(uint32_t Page);
//static void HW_FLASH_WaitEndOfOperation(void);
/*****************************************************************************/
int HW_FLASH_Write(uint32_t address, uint64_t data)
{
/* Enable EOP interrupt */
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR |FLASH_FLAG_PGSERR | FLASH_FLAG_OPTVERR);
/*Enable EOP interupt */
__HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
HW_FLASH_Program_DoubleWord(address, data);
/* Disable EOP interrupt */
__HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);
/* Clear the PG bit once data has been written */
CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
HAL_FLASH_Lock();
return (HW_OK);
}
/*****************************************************************************/
int HW_FLASH_Erase(uint32_t page, uint16_t n, int interrupt)
{
UNUSED(interrupt);
uint32_t loop;
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP |FLASH_FLAG_ALL_ERRORS);
/* Enable EOP interrupt */
__HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
for( loop = 0; loop < n ; loop++)
{
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
HW_FLASH_PageErase(page+loop);
}
/* Disable EOP interrupt */
__HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);
/* Clear the page erase bit */
CLEAR_BIT(FLASH->CR, (FLASH_CR_PER | FLASH_CR_PNB));
HAL_FLASH_Lock();
return (HW_OK);
}
/*************************************************************
*
* LOCAL FUNCTIONS
*
*************************************************************/
/**
* This is a copy of FLASH_Program_DoubleWord() from the HAL
*/
static void HW_FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
{
// DBG_GPIO_Gr2Set(DBG_GPIO_GR2_FLASH_WRITE);
/* Set PG bit */
SET_BIT(FLASH->CR, FLASH_CR_PG);
/* Program first word */
*(uint32_t *)Address = (uint32_t)Data;
/* Barrier to ensure programming is performed in 2 steps, in right order
(independently of compiler optimization behavior) */
__ISB();
/* Program second word */
*(uint32_t *)(Address + 4U) = (uint32_t)(Data >> 32U);
// DBG_GPIO_Gr2Reset(DBG_GPIO_GR2_FLASH_WRITE);
}
/**
* This is a copy of LASH_PageErase() from the HAL
*/
static void HW_FLASH_PageErase(uint32_t Page)
{
// DBG_GPIO_Gr2Set(DBG_GPIO_GR2_FLASH_ERASE);
/* Check the parameters */
assert_param(IS_FLASH_PAGE(Page));
/* Proceed to erase the page */
MODIFY_REG(FLASH->CR, FLASH_CR_PNB, ((Page << FLASH_CR_PNB_Pos) | FLASH_CR_PER | FLASH_CR_STRT));
// DBG_GPIO_Gr2Set(DBG_GPIO_GR2_FLASH_ERASE);
}
|