Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wwsmg1
rt-thread
提交
a80b2720
R
rt-thread
项目概览
wwsmg1
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
1
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,发现更多精彩内容 >>
提交
a80b2720
编写于
12月 26, 2018
作者:
S
SummerGift
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[bsp][stm32] add f0 flash driver
上级
663baf89
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
185 addition
and
0 deletion
+185
-0
bsp/stm32/libraries/HAL_Drivers/drv_flash/drv_flash_f0.c
bsp/stm32/libraries/HAL_Drivers/drv_flash/drv_flash_f0.c
+185
-0
未找到文件。
bsp/stm32/libraries/HAL_Drivers/drv_flash/drv_flash_f0.c
0 → 100644
浏览文件 @
a80b2720
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-12-22 zylx first version
*/
#include "board.h"
#ifdef BSP_USING_ON_CHIP_FLASH
#include "drv_config.h"
#include "drv_flash.h"
#if defined(PKG_USING_FAL)
#include "fal.h"
#endif
//#define DRV_DEBUG
#define LOG_TAG "drv.flash"
#include <drv_log.h>
/**
* @brief Gets the page of a given address
* @param Addr: Address of the FLASH Memory
* @retval The page of a given address
*/
static
uint32_t
GetPage
(
uint32_t
addr
)
{
uint32_t
page
=
0
;
page
=
RT_ALIGN_DOWN
(
addr
,
FLASH_PAGE_SIZE
);
return
page
;
}
/**
* Read data from flash.
* @note This operation's units is word.
*
* @param addr flash address
* @param buf buffer to store read data
* @param size read bytes size
*
* @return result
*/
int
stm32_flash_read
(
long
offset
,
rt_uint8_t
*
buf
,
size_t
size
)
{
size_t
i
;
rt_uint32_t
addr
=
STM32_FLASH_START_ADRESS
+
offset
;
if
((
addr
+
size
)
>
STM32_FLASH_END_ADDRESS
)
{
LOG_E
(
"read outrange flash size! addr is (0x%p)"
,
(
void
*
)(
addr
+
size
));
return
-
RT_EINVAL
;
}
for
(
i
=
0
;
i
<
size
;
i
++
,
buf
++
,
addr
++
)
{
*
buf
=
*
(
rt_uint8_t
*
)
addr
;
}
return
size
;
}
/**
* Write data to flash.
* @note This operation's units is word.
* @note This operation must after erase. @see flash_erase.
*
* @param addr flash address
* @param buf the write data buffer
* @param size write bytes size
*
* @return result
*/
int
stm32_flash_write
(
long
offset
,
const
rt_uint8_t
*
buf
,
size_t
size
)
{
rt_err_t
result
=
RT_EOK
;
rt_uint32_t
addr
=
STM32_FLASH_START_ADRESS
+
offset
;
rt_uint32_t
end_addr
=
addr
+
size
;
if
(
addr
%
4
!=
0
)
{
LOG_E
(
"write addr must be 4-byte alignment"
);
return
-
RT_EINVAL
;
}
if
(
size
%
4
!=
0
)
{
LOG_E
(
"write size must be 4-byte alignment"
);
return
-
RT_EINVAL
;
}
if
((
end_addr
)
>
STM32_FLASH_END_ADDRESS
)
{
LOG_E
(
"write outrange flash size! addr is (0x%p)"
,
(
void
*
)(
addr
+
size
));
return
-
RT_EINVAL
;
}
HAL_FLASH_Unlock
();
while
(
addr
<
end_addr
)
{
if
(
HAL_FLASH_Program
(
FLASH_TYPEPROGRAM_WORD
,
addr
,
*
((
rt_uint32_t
*
)
buf
))
==
HAL_OK
)
{
if
(
*
(
rt_uint32_t
*
)
addr
!=
*
(
rt_uint32_t
*
)
buf
)
{
result
=
-
RT_ERROR
;
break
;
}
addr
+=
4
;
buf
+=
4
;
}
else
{
result
=
-
RT_ERROR
;
break
;
}
}
HAL_FLASH_Lock
();
if
(
result
!=
RT_EOK
)
{
return
result
;
}
return
size
;
}
/**
* Erase data on flash.
* @note This operation is irreversible.
* @note This operation's units is different which on many chips.
*
* @param addr flash address
* @param size erase bytes size
*
* @return result
*/
int
stm32_flash_erase
(
long
offset
,
size_t
size
)
{
rt_err_t
result
=
RT_EOK
;
rt_uint32_t
addr
=
STM32_FLASH_START_ADRESS
+
offset
;
uint32_t
PAGEError
=
0
;
/*Variable used for Erase procedure*/
FLASH_EraseInitTypeDef
EraseInitStruct
;
if
((
addr
+
size
)
>
STM32_FLASH_END_ADDRESS
)
{
LOG_E
(
"ERROR: erase outrange flash size! addr is (0x%p)
\n
"
,
(
void
*
)(
addr
+
size
));
return
-
RT_EINVAL
;
}
HAL_FLASH_Unlock
();
/* Fill EraseInit structure*/
EraseInitStruct
.
TypeErase
=
FLASH_TYPEERASE_PAGES
;
EraseInitStruct
.
PageAddress
=
GetPage
(
addr
);
EraseInitStruct
.
NbPages
=
(
size
+
FLASH_PAGE_SIZE
-
1
)
/
FLASH_PAGE_SIZE
;
if
(
HAL_FLASHEx_Erase
(
&
EraseInitStruct
,
&
PAGEError
)
!=
HAL_OK
)
{
result
=
-
RT_ERROR
;
goto
__exit
;
}
__exit:
HAL_FLASH_Lock
();
if
(
result
!=
RT_EOK
)
{
return
result
;
}
LOG_D
(
"erase done: addr (0x%p), size %d"
,
(
void
*
)
addr
,
size
);
return
result
;
}
#if defined(PKG_USING_FAL)
const
struct
fal_flash_dev
stm32_onchip_flash
=
{
"onchip_flash"
,
STM32_FLASH_START_ADRESS
,
STM32_FLASH_SIZE
,
FLASH_PAGE_SIZE
,
{
NULL
,
stm32_flash_read
,
stm32_flash_write
,
stm32_flash_erase
}
};
#endif
#endif
/* BSP_USING_ON_CHIP_FLASH */
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录