硬汉嵌入式论坛

 找回密码
 立即注册
查看: 152|回复: 1
收起左侧

[客户分享] 华大电子CIU32F003的读写保护设置

[复制链接]

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
116746
QQ
发表于 2025-6-20 13:06:22 | 显示全部楼层 |阅读模式
1.png

2.png

3.png

4.png

5.png

6.png
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
116746
QQ
 楼主| 发表于 2025-6-20 13:08:38 | 显示全部楼层
[C] 纯文本查看 复制代码
/************************************************************************************************/
/**
* @file               ciu32f003_std_flash.c
* @author             MCU Ecosystem Development Team
* @brief              FLASH STD库驱动。
*                     实现FLASH擦除、编程API。
*
*
**************************************************************************************************
* @attention
* Copyright (c) CEC Huada Electronic Design Co.,Ltd. All rights reserved.
*
**************************************************************************************************
*/

/************************************************************************************************/
/**
* @addtogroup CIU32F003_STD_Driver
* @{
*/

/**
* @addtogroup FLASH 
* @{
*
*/
/************************************************************************************************/


/*------------------------------------------includes--------------------------------------------*/
#include "ciu32f003_std.h"

#ifdef STD_FLASH_PERIPHERAL_USED

/*-------------------------------------------functions------------------------------------------*/

/************************************************************************************************/
/**
* @addtogroup FLASH_External_Functions 
* @{
*
*/
/************************************************************************************************/ 

/**
* @brief  Flash擦除与Option byte区擦除
* @param  mode 擦除模式
*             @arg FLASH_MODE_PAGE_ERASE
*             @arg FLASH_MODE_MASS_ERASE
* @param  address 擦除访问地址
* @note   user flash区擦除时,需先调std_flash_unlock(),解锁flash
* @note   Option Byte区擦除时,需先调用std_flash_opt_unlock(),解锁选项字节
* @retval std_status_t API执行结果
*/
std_status_t std_flash_erase(uint32_t mode, uint32_t address)
{
    std_status_t status = STD_OK;
    
    /* 设置擦除模式 */
    std_flash_set_operate_mode(mode);
    
    /* 执行擦除 */
    *(uint32_t *)address = 0xFFFFFFFF;
    
    /* 等待擦除完成,查询异常标志位 */
    while (std_flash_get_flag(FLASH_FLAG_BSY));
    if ((FLASH->SR & FLASH_FLAG_WRPERR) != 0x00000000U)
    {
        status = STD_ERR;
    }
    
    /* 清除所有标志 */
    std_flash_clear_flag(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR);
    
    /* 退出擦除模式 */
    std_flash_set_operate_mode(FLASH_MODE_IDLE);
    
    return (status);
}


/**
* @brief  User Flash区与Option Byte区字编程
* @param  address   编程地址
* @param  prog_data 编程数据(4字节)
* @note   user flash区编程时,需先调std_flash_unlock(),解锁flash
* @note   Option Byte区字编程时,需先调用std_flash_opt_unlock(),解锁选项字节
* @retval std_status_t API执行结果
*/
std_status_t std_flash_word_program(uint32_t address, uint32_t prog_data)
{
    std_status_t status = STD_OK;
    
    /* 进入编程模式 */
    std_flash_set_operate_mode(FLASH_MODE_PROGRAM);

    /* 向目标地址写入数据 */
    *(uint32_t *)address = prog_data;
    
    /* 等待编程完成,查询异常标志位 */
    while (std_flash_get_flag(FLASH_FLAG_BSY));
    if ((FLASH->SR & FLASH_FLAG_WRPERR) != 0x00000000U)
    {
        status = STD_ERR;
    }
    
    if (status == STD_OK)
    {
        /* 检查编程数据是否正确 */
        if(*((__IO uint32_t *)address) != prog_data)
        {
            status = STD_ERR;
        }
    }
    
    /* 清除所有标志 */
    std_flash_clear_flag(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR);
    
    /* 退出编程模式 */
    std_flash_set_operate_mode(FLASH_MODE_IDLE);
    
    return (status);
}


/** 
* @} 
*/

#endif /* STD_FLASH_PERIPHERAL_USED */

/** 
* @} 
*/

/** 
* @} 
*/

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-7-4 17:30 , Processed in 0.214966 second(s), 27 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表