记录自己学习GD32F103R 使用fmc的过程

先来重点可直接食用

/*!
    \file  main.c
    \brief main flash program, erase

    \version 2014-12-26, V1.0.0, firmware for GD32F10x
    \version 2017-06-20, V2.0.0, firmware for GD32F10x
    \version 2018-07-31, V2.1.0, firmware for GD32F10x
*/

/*
    Copyright (c) 2018, GigaDevice Semiconductor Inc.

    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/

#include "gd32f10x.h" 
#include "gd32f10x_eval.h"
#include "main.h" 
#include "board_usart.h""

#define FMC_PAGE_SIZE           ((uint16_t)0x800U)
//#define FMC_WRITE_START_ADDR    ((uint32_t)0x0807F800U)
//#define FMC_WRITE_END_ADDR      ((uint32_t)0x0807FFFFU)
#define FMC_WRITE_START_ADDR    ((uint32_t)0x08004000U)
#define FMC_WRITE_END_ADDR      ((uint32_t)0x08004800U)

uint32_t *ptrd;
uint32_t address = 0x00;
uint32_t data0   = 1980;


/* calculate the num of page to be programmed/erased */
uint32_t page_num = (FMC_WRITE_END_ADDR - FMC_WRITE_START_ADDR) / FMC_PAGE_SIZE;
/* calculate the num of page to be programmed/erased */
uint32_t word_num = ((FMC_WRITE_END_ADDR - FMC_WRITE_START_ADDR) >> 2);

/*!
    \brief      erase fmc pages from FMC_WRITE_START_ADDR to FMC_WRITE_END_ADDR
    \param[in]  none
    \param[out] none
    \retval     none
*/
void fmc_erase_pages(void)
{
    uint32_t erase_counter;

    /* unlock the flash program/erase controller */
    fmc_unlock();
		ob_unlock();
    /* clear all pending flags */
    fmc_flag_clear(FMC_FLAG_BANK0_END);
    fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
    fmc_flag_clear(FMC_FLAG_BANK0_PGERR);
    
    /* erase the flash pages */
    for(erase_counter = 0; erase_counter < page_num; erase_counter++){
        fmc_page_erase(FMC_WRITE_START_ADDR + (FMC_PAGE_SIZE * erase_counter));
        fmc_flag_clear(FMC_FLAG_BANK0_END);
        fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
        fmc_flag_clear(FMC_FLAG_BANK0_PGERR);
    }

    /* lock the main FMC after the erase operation */
    fmc_lock();
}

/*!
    \brief      program fmc word by word from FMC_WRITE_START_ADDR to FMC_WRITE_END_ADDR
    \param[in]  none
    \param[out] none
    \retval     none
*/
void fmc_program(void)
{
    /* unlock the flash program/erase controller */
    fmc_unlock();

    address = FMC_WRITE_START_ADDR;

    /* program flash */
    while(address < FMC_WRITE_END_ADDR){
        fmc_word_program(address, data0);
        address += 4;
        fmc_flag_clear(FMC_FLAG_BANK0_END);
        fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
        fmc_flag_clear(FMC_FLAG_BANK0_PGERR); 
    }

    /* lock the main FMC after the program operation */
    fmc_lock();
}

/*!
    \brief      check fmc erase result
    \param[in]  none
    \param[out] none
    \retval     none
*/
void fmc_erase_pages_check(void)
{
    uint32_t i;

    ptrd = (uint32_t *)FMC_WRITE_START_ADDR;

    /* check flash whether has been erased */
    for(i = 0; i < word_num; i++){
        if(0xFFFFFFFF != (*ptrd)){

            break;
        }else{
            ptrd++;
        }
    }
}

/*!
    \brief      check fmc program result
    \param[in]  none
    \param[out] none
    \retval     none
*/
void fmc_program_check(void)
{
    uint32_t i;

    ptrd = (uint32_t *)FMC_WRITE_START_ADDR;

    /* check flash whether has been programmed */
    for(i = 0; i < word_num; i++){
        if((*ptrd) != data0){

            break;
        }else{
            ptrd++;
						//gpio_bit_set(GPIOA,GPIO_PIN_8);
        }
    }
}

/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
uint32_t GDFLASH_ReadWord(uint32_t faddr)   

{   

    return *(volatile uint32_t*)faddr;    

}
int main(void)
{
		SystemInit();
    /* initialize led on the board */
		USART_Init();

    /* step1: erase pages and check if it is successful. if not, light the LED2 */
    fmc_erase_pages();
    fmc_erase_pages_check();

    /* step2: program and check if it is successful. if not, light the LED3 */
    fmc_program();
    fmc_program_check();
		
		gpio_bit_set(GPIOB,GPIO_PIN_3);
		printf("%d",GDFLASH_ReadWord(0x08004000));

    while(1);
}

送64k 地址表
页0 0x0800 0000 - 0x0800 03FF 1K
页1 0x0800 0400 - 0x0800 07FF 1K
页2 0x0800 0800 - 0x0800 0BFF 1K
页3 0x0800 0C00 - 0x0800 0FFF 1K
页4 0x0800 1000 - 0x0800 13FF 1K
页5 0x0800 1400 - 0x0800 17FF 1K
页6 0x0800 1800 - 0x0800 1BFF 1K
页7 0x0800 1C00 - 0x0800 1FFF 1K
页8 0x0800 2000 - 0x0800 23FF 1K
页9 0x0800 2400 - 0x0800 27FF 1K
页10 0x0800 2800 - 0x0800 2BFF 1K
页11 0x0800 2C00 - 0x0800 2FFF 1K
页12 0x0800 3000 - 0x0800 33FF 1K
页13 0x0800 3400 - 0x0800 37FF 1K
页14 0x0800 3800 - 0x0800 3BFF 1K
页15 0x0800 3C00 - 0x0800 3FFF 1K
页16 0x0800 4000 - 0x0800 43FF 1K
页17 0x0800 4400 - 0x0800 47FF 1K
页18 0x0800 4800 - 0x0800 4BFF 1K
页19 0x0800 4C00 - 0x0800 4FFF 1K
页20 0x0800 5000 - 0x0800 53FF 1K
页21 0x0800 5400 - 0x0800 57FF 1K
页22 0x0800 5800 - 0x0800 5BFF 1K
页23 0x0800 5C00 - 0x0800 5FFF 1K
页24 0x0800 6000 - 0x0800 63FF 1K
页25 0x0800 6400 - 0x0800 67FF 1K
页26 0x0800 6800 - 0x0800 6BFF 1K
页27 0x0800 6C00 - 0x0800 6FFF 1K
页28 0x0800 7000 - 0x0800 73FF 1K
页29 0x0800 7400 - 0x0800 77FF 1K
页30 0x0800 7800 - 0x0800 7BFF 1K
页31 0x0800 7C00 - 0x0800 7FFF 1K
页32 0x0800 8000 - 0x0800 83FF 1K
页33 0x0800 8400 - 0x0800 87FF 1K
页34 0x0800 8800 - 0x0800 8BFF 1K
页35 0x0800 8C00 - 0x0800 8FFF 1K
页36 0x0800 9000 - 0x0800 93FF 1K
页37 0x0800 9400 - 0x0800 97FF 1K
页38 0x0800 9800 - 0x0800 9BFF 1K
页39 0x0800 9C00 - 0x0800 9FFF 1K
页40   0x0800 A000 - 0x0800 A3FF    1K
页41   0x0800 A400 - 0x0800 A7FF    1K
页42   0x0800 A800 - 0x0800 ABFF    1K
页43   0x0800 AC00 - 0x0800 AFFF    1K
页44   0x0800 B000 - 0x0800 B3FF    1K
页45   0x0800 B400 - 0x0800 B7FF    1K
页46   0x0800 B800 - 0x0800 BBFF    1K
页47   0x0800 BC00 - 0x0800 BFFF   1K
页48   0x0800 C000 - 0x0800 C3FF    1K
页49   0x0800 C400 - 0x0800 C7FF    1K
页50   0x0800 C800 - 0x0800 CBFF    1K
页51   0x0800 CC00 - 0x0800 CFFF    1K
页52   0x0800 D000 - 0x0800 D3FF    1K
页53   0x0800 D400 - 0x0800 D7FF    1K
页54   0x0800 D800 - 0x0800 DBFF    1K
页55   0x0800 DC00 - 0x0800 DFFF   1K
页56   0x0800 E000 - 0x0800 E3FF    1K
页57   0x0800 E400 - 0x0800 E7FF   1K
页58   0x0800 E800 - 0x0800 EBFF   1K
页59   0x0800 EC00 - 0x0800 EFFF    1K
页60   0x0800 F000 - 0x0800 F3FF    1K
页61   0x0800 F400 - 0x0800 F7FF    1K
页62   0x0800 F800 - 0x0800 FBFF    1K
页63   0x0800 FC00 - 0x0800 FFFF    1K

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Quieeeet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值