123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- /*!
- \file main.c
- \brief main flash program, erase
- \version 2017-02-10, V1.0.0, firmware for GD32F30x
- \version 2018-10-10, V1.1.0, firmware for GD32F30x
- \version 2018-12-25, V2.0.0, firmware for GD32F30x
- \version 2020-09-30, V2.1.0, firmware for GD32F30x
- */
- /*
- Copyright (c) 2020, GigaDevice Semiconductor Inc.
- 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 "gd32f30x.h"
- #include "gd32f307c_eval.h"
- #include "main.h"
- #define FMC_PAGE_SIZE ((uint16_t)0x800U)
- #define FMC_WRITE_START_ADDR ((uint32_t)0x08004000U)
- #define FMC_WRITE_END_ADDR ((uint32_t)0x08004800U)
- uint32_t *ptrd;
- uint32_t address = 0x00000000U;
- uint32_t data0 = 0x01234567U;
- led_typedef_enum lednum = LED4;
- /* calculate the number of page to be programmed/erased */
- uint32_t PageNum = (FMC_WRITE_END_ADDR - FMC_WRITE_START_ADDR) / FMC_PAGE_SIZE;
- /* calculate the number of page to be programmed/erased */
- uint32_t WordNum = ((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 EraseCounter;
- /* unlock the flash program/erase controller */
- fmc_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(EraseCounter = 0; EraseCounter < PageNum; EraseCounter++){
- fmc_page_erase(FMC_WRITE_START_ADDR + (FMC_PAGE_SIZE * EraseCounter));
- 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 < WordNum; i++){
- if(0xFFFFFFFF != (*ptrd)){
- lednum = LED2;
- gd_eval_led_on(lednum);
- 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 < WordNum; i++){
- if((*ptrd) != data0){
- lednum = LED3;
- gd_eval_led_on(lednum);
- break;
- }else{
- ptrd++;
- }
- }
- }
- /*!
- \brief main function
- \param[in] none
- \param[out] none
- \retval none
- */
- int main(void)
- {
- /* initialize led on the board */
- gd_eval_led_init(LED2);
- gd_eval_led_init(LED3);
- gd_eval_led_init(LED4);
- /* 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();
- /* if all the operations are successful, light the LED4. */
- if(LED4 == lednum){
- gd_eval_led_on(LED4);
- }
- while(1);
- }
|