Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2345VOR
rt-thread
提交
d69c4111
R
rt-thread
项目概览
2345VOR
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
d69c4111
编写于
3月 11, 2021
作者:
B
Bernard Xiong
提交者:
GitHub
3月 11, 2021
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #4405 from zhuyf233/master
[bsp][stm32]fix stm32h7 flash driver error
上级
d7595d51
2d5bbbf0
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
88 addition
and
138 deletion
+88
-138
bsp/stm32/libraries/HAL_Drivers/drv_flash/drv_flash_h7.c
bsp/stm32/libraries/HAL_Drivers/drv_flash/drv_flash_h7.c
+88
-138
未找到文件。
bsp/stm32/libraries/HAL_Drivers/drv_flash/drv_flash_h7.c
浏览文件 @
d69c4111
...
...
@@ -9,6 +9,7 @@
* 2019-3-2 jinsheng add Macro judgment
* 2020-1-6 duminmin support single bank mode
* 2020-5-17 yufanyufan77 support support H7
* 2021-3-3 zhuyf233 fix some bugs
*/
#include "board.h"
...
...
@@ -24,110 +25,6 @@
//#define DRV_DEBUG
#define LOG_TAG "drv.flash"
#include <drv_log.h>
#define ADDR_FLASH_SECTOR_0 ((rt_uint32_t)0x08000000)
/* Base address of Sector 0, 128 Kbytes */
#define ADDR_FLASH_SECTOR_1 ((rt_uint32_t)0x08020000)
/* Base address of Sector 1, 128 Kbytes */
#define ADDR_FLASH_SECTOR_2 ((rt_uint32_t)0x08040000)
/* Base address of Sector 2, 128 Kbytes */
#define ADDR_FLASH_SECTOR_3 ((rt_uint32_t)0x08060000)
/* Base address of Sector 3, 128 Kbytes */
#define ADDR_FLASH_SECTOR_4 ((rt_uint32_t)0x08080000)
/* Base address of Sector 4, 128 Kbytes */
#define ADDR_FLASH_SECTOR_5 ((rt_uint32_t)0x080A0000)
/* Base address of Sector 5, 128 Kbytes */
#define ADDR_FLASH_SECTOR_6 ((rt_uint32_t)0x080C0000)
/* Base address of Sector 6, 128 Kbytes */
#define ADDR_FLASH_SECTOR_7 ((rt_uint32_t)0x080E0000)
/* Base address of Sector 7, 128 Kbytes */
#define ADDR_FLASH_SECTOR_8 ((rt_uint32_t)0x08100000)
/* Base address of Sector 8, 128 Kbytes */
#define FLASH_SECTOR_0 0U
/* Sector Number 0 */
#define FLASH_SECTOR_1 1U
/* Sector Number 1 */
#define FLASH_SECTOR_2 2U
/* Sector Number 2 */
#define FLASH_SECTOR_3 3U
/* Sector Number 3 */
#define FLASH_SECTOR_4 4U
/* Sector Number 4 */
#define FLASH_SECTOR_5 5U
/* Sector Number 5 */
#define FLASH_SECTOR_6 6U
/* Sector Number 6 */
#define FLASH_SECTOR_7 7U
/* Sector Number 7 */
/**
* @brief Gets the sector of a given address
* @param addr flash address
* @param flash bank
* @param flash sector
* @retval The sector of a given address
*/
static
void
GetSector
(
rt_uint32_t
Address
,
uint32_t
*
bank
,
uint32_t
*
sector
)
{
#if defined (FLASH_OPTCR_nDBANK)
FLASH_OBProgramInitTypeDef
OBInit
;
uint32_t
nbank
=
0
;
/* get duel bank ability:nDBANK(Bit29) */
HAL_FLASHEx_OBGetConfig
(
&
OBInit
);
nbank
=
((
OBInit
.
USERConfig
&
0x20000000U
)
>>
29
);
/* 1:single bank mode */
if
(
1
==
nbank
)
{
if
((
Address
<
ADDR_FLASH_SECTOR_1
)
&&
(
Address
>=
ADDR_FLASH_SECTOR_0
))
{
sector
=
FLASH_SECTOR_0
;
}
else
if
((
Address
<
ADDR_FLASH_SECTOR_2
)
&&
(
Address
>=
ADDR_FLASH_SECTOR_1
))
{
sector
=
FLASH_SECTOR_1
;
}
else
if
((
Address
<
ADDR_FLASH_SECTOR_3
)
&&
(
Address
>=
ADDR_FLASH_SECTOR_2
))
{
sector
=
FLASH_SECTOR_2
;
}
else
if
((
Address
<
ADDR_FLASH_SECTOR_4
)
&&
(
Address
>=
ADDR_FLASH_SECTOR_3
))
{
sector
=
FLASH_SECTOR_3
;
}
else
if
((
Address
<
ADDR_FLASH_SECTOR_5
)
&&
(
Address
>=
ADDR_FLASH_SECTOR_4
))
{
sector
=
FLASH_SECTOR_4
;
}
else
if
((
Address
<
ADDR_FLASH_SECTOR_6
)
&&
(
Address
>=
ADDR_FLASH_SECTOR_5
))
{
sector
=
FLASH_SECTOR_5
;
}
else
if
((
Address
<
ADDR_FLASH_SECTOR_7
)
&&
(
Address
>=
ADDR_FLASH_SECTOR_6
))
{
sector
=
FLASH_SECTOR_6
;
}
else
if
((
Address
<
ADDR_FLASH_SECTOR_8
)
&&
(
Address
>=
ADDR_FLASH_SECTOR_7
))
{
sector
=
FLASH_SECTOR_7
;
}
else
if
((
Address
<
ADDR_FLASH_SECTOR_9
)
&&
(
Address
>=
ADDR_FLASH_SECTOR_8
))
{
sector
=
FLASH_SECTOR_8
;
}
else
if
((
Address
<
ADDR_FLASH_SECTOR_10
)
&&
(
Address
>=
ADDR_FLASH_SECTOR_9
))
{
sector
=
FLASH_SECTOR_9
;
}
else
if
((
Address
<
ADDR_FLASH_SECTOR_11
)
&&
(
Address
>=
ADDR_FLASH_SECTOR_10
))
{
sector
=
FLASH_SECTOR_10
;
}
else
{
sector
=
FLASH_SECTOR_11
;
}
}
else
/* 0:dual bank mode */
{
LOG_E
(
"rtthread doesn't support duel bank mode yet!"
);
RT_ASSERT
(
0
);
}
#else
/* no dual bank ability */
*
sector
=
(
Address
&
0xffffff
)
/
FLASH_SIZE_GRANULARITY_128K
;
if
(
*
sector
>
7
)
{
*
bank
=
FLASH_BANK_1
;
*
sector
=
*
sector
/
2
;
}
else
{
*
bank
=
FLASH_BANK_2
;
}
#endif
}
/**
* Read data from flash.
...
...
@@ -137,16 +34,16 @@ static void GetSector(rt_uint32_t Address,uint32_t* bank,uint32_t* sector)
* @param buf buffer to store read data
* @param size read bytes size
*
* @ret
urn result
* @ret
val The length of bytes that have been read
*/
int
stm32_flash_read
(
rt_uint32_t
addr
,
rt_uint8_t
*
buf
,
size_t
size
)
{
size_t
i
;
if
((
addr
+
size
)
>
STM32_FLASH_END_ADDRESS
)
if
((
addr
+
size
-
1
)
>
FLASH_END
)
{
LOG_E
(
"read outrange flash size! addr is (0x%p)"
,
(
void
*
)(
addr
+
size
));
return
-
1
;
return
-
RT_ERROR
;
}
for
(
i
=
0
;
i
<
size
;
i
++
,
buf
++
,
addr
++
)
...
...
@@ -166,47 +63,68 @@ int stm32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
* @param buf the write data buffer
* @param size write bytes size
*
* @return
result
* @return
The length of bytes that have been written
*/
int
stm32_flash_write
(
rt_uint32_t
addr
,
const
rt_uint8_t
*
buf
,
size_t
size
)
{
rt_err_t
result
=
RT_EOK
;
rt_uint32_t
end_addr
=
addr
+
size
;
rt_uint32_t
bank
=
addr
/
ADDR_FLASH_SECTOR_8
;;
rt_uint32_t
end_addr
=
addr
+
size
-
1
,
write_addr
;
rt_uint32_t
write_granularity
=
FLASH_NB_32BITWORD_IN_FLASHWORD
*
4
;
rt_uint32_t
write_size
=
write_granularity
;
rt_uint8_t
write_buffer
[
32
]
=
{
0
};
if
((
end_addr
)
>
STM32_FLASH_END_ADDRESS
)
if
((
end_addr
)
>
FLASH_END
)
{
LOG_E
(
"write outrange flash size! addr is (0x%p)"
,
(
void
*
)(
addr
+
size
));
return
-
RT_EINVAL
;
}
if
(
addr
%
32
!=
0
)
{
LOG_E
(
"write addr must be 32-byte alignment"
);
return
-
RT_EINVAL
;
}
if
(
size
<
1
)
{
return
-
RT_EINVAL
;
}
/* Unlock the Flash to enable the flash control register access */
HAL_FLASH_Unlock
();
__HAL_FLASH_CLEAR_FLAG
(
FLASH_FLAG_EOP
|
FLASH_FLAG_OPERR
|
FLASH_FLAG_WRPERR
)
;
for
(
size_t
i
=
0
;
i
<
size
/
32
;
i
++
,
addr
+=
32
,
buf
+=
32
)
write_addr
=
(
uint32_t
)
buf
;
__HAL_FLASH_CLEAR_FLAG
(
FLASH_FLAG_EOP
|
FLASH_FLAG_OPERR
|
FLASH_FLAG_WRPERR
);
while
(
addr
<
end_addr
)
{
/* write data to flash */
if
(
HAL_FLASH_Program
(
FLASH_TYPEPROGRAM_FLASHWORD
,
addr
,
(
uint64_t
)((
uint32_t
)
buf
))
==
HAL_OK
)
if
(
end_addr
-
addr
+
1
<
write_granularity
)
{
if
(
*
(
rt_uint8_t
*
)
addr
!=
*
buf
)
write_size
=
end_addr
-
addr
+
1
;
for
(
size_t
i
=
0
;
i
<
write_size
;
i
++
)
{
result
=
-
RT_ERROR
;
break
;
write_buffer
[
i
]
=
*
((
uint8_t
*
)(
write_addr
+
i
));
}
write_addr
=
(
uint32_t
)((
rt_uint32_t
*
)
write_buffer
);
}
if
(
HAL_FLASH_Program
(
FLASH_TYPEPROGRAM_FLASHWORD
,
addr
,
write_addr
)
==
HAL_OK
)
{
for
(
rt_uint8_t
i
=
0
;
i
<
write_size
;
i
++
)
{
if
(
*
(
rt_uint8_t
*
)(
addr
+
i
)
!=
*
(
rt_uint8_t
*
)(
write_addr
+
i
))
{
result
=
-
RT_ERROR
;
goto
__exit
;
}
}
addr
+=
write_granularity
;
write_addr
+=
write_granularity
;
}
else
{
result
=
-
RT_ERROR
;
break
;
goto
__exit
;
}
}
__exit:
HAL_FLASH_Lock
();
if
(
result
!=
RT_EOK
)
...
...
@@ -230,42 +148,74 @@ int stm32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
int
stm32_flash_erase
(
rt_uint32_t
addr
,
size_t
size
)
{
rt_err_t
result
=
RT_EOK
;
rt_uint32_t
FirstSector
=
0
,
NbOfSectors
=
0
;
rt_uint32_t
SECTORError
=
0
;
rt_uint32_t
bank
=
0
;
if
((
addr
+
size
)
>
STM32_FLASH_END_ADDRESS
)
if
((
addr
+
size
-
1
)
>
FLASH_END
)
{
LOG_E
(
"ERROR: erase outrange flash size! addr is (0x%p)
\n
"
,
(
void
*
)(
addr
+
size
));
return
-
RT_EINVAL
;
}
rt_uint32_t
addr_bank1
=
0
;
rt_uint32_t
size_bank1
=
0
;
rt_uint32_t
addr_bank2
=
0
;
rt_uint32_t
size_bank2
=
0
;
if
((
addr
+
size
)
<
FLASH_BANK2_BASE
)
{
addr_bank1
=
addr
;
size_bank1
=
size
;
size_bank2
=
0
;
}
else
if
(
addr
>=
FLASH_BANK2_BASE
)
{
size_bank1
=
0
;
addr_bank2
=
addr
;
size_bank2
=
size
;
}
else
{
addr_bank1
=
addr
;
size_bank1
=
FLASH_BANK2_BASE
-
addr_bank1
;
addr_bank2
=
FLASH_BANK2_BASE
;
size_bank2
=
addr
+
size
-
FLASH_BANK2_BASE
;
}
/*Variable used for Erase procedure*/
FLASH_EraseInitTypeDef
EraseInitStruct
;
/* Unlock the Flash to enable the flash control register access */
HAL_FLASH_Unlock
();
/* Get the 1st sector to erase */
GetSector
(
addr
,
&
bank
,
&
FirstSector
);
/* Get the number of sector to erase from 1st sector */
GetSector
(
addr
+
size
,
0
,
&
NbOfSectors
);
NbOfSectors
=
NbOfSectors
-
FirstSector
+
1
;
/* Fill EraseInit structure */
EraseInitStruct
.
TypeErase
=
FLASH_TYPEERASE_SECTORS
;
EraseInitStruct
.
VoltageRange
=
FLASH_VOLTAGE_RANGE_3
;
EraseInitStruct
.
Sector
=
FirstSector
;
EraseInitStruct
.
NbSectors
=
NbOfSectors
;
EraseInitStruct
.
Banks
=
bank
;
SCB_DisableDCache
();
if
(
size_bank1
)
{
EraseInitStruct
.
Sector
=
(
addr_bank1
-
FLASH_BANK1_BASE
)
/
FLASH_SECTOR_SIZE
;
EraseInitStruct
.
NbSectors
=
(
addr_bank1
+
size_bank1
-
1
-
FLASH_BANK1_BASE
)
/
FLASH_SECTOR_SIZE
-
EraseInitStruct
.
Sector
+
1
;
EraseInitStruct
.
Banks
=
FLASH_BANK_1
;
if
(
HAL_FLASHEx_Erase
(
&
EraseInitStruct
,
&
SECTORError
)
!=
HAL_OK
)
{
result
=
-
RT_ERROR
;
goto
__exit
;
}
}
if
(
HAL_FLASHEx_Erase
(
&
EraseInitStruct
,
&
SECTORError
)
!=
HAL_OK
)
if
(
size_bank2
)
{
result
=
-
RT_ERROR
;
goto
__exit
;
EraseInitStruct
.
Sector
=
(
addr_bank2
-
FLASH_BANK2_BASE
)
/
FLASH_SECTOR_SIZE
;
EraseInitStruct
.
NbSectors
=
(
addr_bank2
+
size_bank2
-
1
-
FLASH_BANK2_BASE
)
/
FLASH_SECTOR_SIZE
-
EraseInitStruct
.
Sector
+
1
;
EraseInitStruct
.
Banks
=
FLASH_BANK_2
;
if
(
HAL_FLASHEx_Erase
(
&
EraseInitStruct
,
&
SECTORError
)
!=
HAL_OK
)
{
result
=
-
RT_ERROR
;
goto
__exit
;
}
}
__exit:
SCB_EnableDCache
();
HAL_FLASH_Lock
();
if
(
result
!=
RT_EOK
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录