mirror of
https://github.com/IcedRooibos/py32f0-template.git
synced 2025-10-28 16:32:05 -07:00
feat: ll flash read&write example
This commit is contained in:
parent
545789ba11
commit
a00b738a90
124
Examples/LL/Flash/SectorEraseAndWrite/main.c
Normal file
124
Examples/LL/Flash/SectorEraseAndWrite/main.c
Normal file
@ -0,0 +1,124 @@
|
||||
#include "main.h"
|
||||
#include "py32f0xx_bsp_clock.h"
|
||||
#include "py32f0xx_bsp_printf.h"
|
||||
|
||||
#define FLASH_USER_START_ADDR 0x0800F800
|
||||
|
||||
const uint32_t DATA[64] =
|
||||
{
|
||||
0x01010101, 0x23456789, 0x3456789A, 0x456789AB, 0x56789ABC, 0x6789ABCD, 0x789ABCDE, 0x89ABCDEF,
|
||||
0x9ABCDEF0, 0xABCDEF01, 0xBCDEF012, 0xCDEF0123, 0xDEF01234, 0xEF012345, 0xF0123456, 0x01234567,
|
||||
0x01010101, 0x23456789, 0x3456789A, 0x456789AB, 0x56789ABC, 0x6789ABCD, 0x789ABCDE, 0x89ABCDEF,
|
||||
0x9ABCDEF0, 0xABCDEF01, 0xBCDEF012, 0xCDEF0123, 0xDEF01234, 0xEF012345, 0xF0123456, 0x01234567,
|
||||
0x23456789, 0xAAAAAAAA, 0x55555555, 0x23456789, 0xAAAAAAAA, 0x55555555, 0x23456789, 0xAAAAAAAA,
|
||||
0x23456789, 0xAAAAAAAA, 0x55555555, 0x23456789, 0xAAAAAAAA, 0x55555555, 0x23456789, 0xAAAAAAAA,
|
||||
0x23456789, 0xAAAAAAAA, 0x55555555, 0x23456789, 0xAAAAAAAA, 0x55555555, 0x23456789, 0xAAAAAAAA,
|
||||
0x23456789, 0xAAAAAAAA, 0x55555555, 0x23456789, 0xAAAAAAAA, 0x55555555, 0x23456789, 0xAAAAAAAA,
|
||||
};
|
||||
|
||||
static void APP_FlashErase(void);
|
||||
static void APP_FlashProgram(void);
|
||||
static void APP_FlashEraseVerify(void);
|
||||
static void APP_FlashWriteVerify(void);
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
BSP_HSI_PLL_48MConfig();
|
||||
|
||||
BSP_USART_Config(115200);
|
||||
printf("Flash Read&Write Demo\r\nClock: %ld\r\n", SystemCoreClock);
|
||||
|
||||
// Unlock flash
|
||||
LL_FLASH_Unlock();
|
||||
// Erase flash
|
||||
APP_FlashErase();
|
||||
APP_FlashEraseVerify();
|
||||
printf("Erase verif: succ\r\n");
|
||||
|
||||
// Write to flash
|
||||
APP_FlashProgram();
|
||||
// Lock
|
||||
LL_FLASH_Lock();
|
||||
// Write verification
|
||||
APP_FlashWriteVerify();
|
||||
printf("Write verif: succ\r\n");
|
||||
|
||||
while (1);
|
||||
}
|
||||
|
||||
static void APP_FlashErase(void)
|
||||
{
|
||||
uint32_t SECTORError = 0;
|
||||
FLASH_EraseInitTypeDef EraseInitStruct;
|
||||
// Erase type = sector
|
||||
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORERASE;
|
||||
// Erase address start
|
||||
EraseInitStruct.SectorAddress = FLASH_USER_START_ADDR;
|
||||
// Number of sectors
|
||||
EraseInitStruct.NbSectors = 1;
|
||||
// Erase
|
||||
if (LL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != SUCCESS)
|
||||
{
|
||||
APP_ErrorHandler();
|
||||
}
|
||||
}
|
||||
|
||||
static void APP_FlashProgram(void)
|
||||
{
|
||||
uint32_t flash_program_start = FLASH_USER_START_ADDR ;
|
||||
uint32_t flash_program_end = (FLASH_USER_START_ADDR + sizeof(DATA));
|
||||
uint32_t *src = (uint32_t *)DATA;
|
||||
|
||||
while (flash_program_start < flash_program_end)
|
||||
{
|
||||
// Write to flash
|
||||
if (LL_FLASH_Program(FLASH_TYPEPROGRAM_PAGE, flash_program_start, src) == SUCCESS)
|
||||
{
|
||||
// Move flash point to next page
|
||||
flash_program_start += FLASH_PAGE_SIZE;
|
||||
// Move data point
|
||||
src += FLASH_PAGE_SIZE / 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void APP_FlashEraseVerify(void)
|
||||
{
|
||||
uint32_t addr = 0;
|
||||
|
||||
while (addr < sizeof(DATA))
|
||||
{
|
||||
if (0xFFFFFFFF != HW32_REG(FLASH_USER_START_ADDR + addr))
|
||||
{
|
||||
printf("erase verification error\r\n");
|
||||
}
|
||||
addr += 4;
|
||||
}
|
||||
}
|
||||
|
||||
static void APP_FlashWriteVerify(void)
|
||||
{
|
||||
uint32_t addr = 0;
|
||||
|
||||
while (addr < sizeof(DATA))
|
||||
{
|
||||
if (DATA[addr / 4] != HW32_REG(FLASH_USER_START_ADDR + addr))
|
||||
{
|
||||
printf("write verification error\r\n");
|
||||
}
|
||||
addr += 4;
|
||||
}
|
||||
}
|
||||
|
||||
void APP_ErrorHandler(void)
|
||||
{
|
||||
while (1);
|
||||
}
|
||||
|
||||
#ifdef USE_FULL_ASSERT
|
||||
void assert_failed(uint8_t *file, uint32_t line)
|
||||
{
|
||||
while (1);
|
||||
}
|
||||
#endif /* USE_FULL_ASSERT */
|
||||
27
Examples/LL/Flash/SectorEraseAndWrite/main.h
Normal file
27
Examples/LL/Flash/SectorEraseAndWrite/main.h
Normal file
@ -0,0 +1,27 @@
|
||||
#ifndef __MAIN_H
|
||||
#define __MAIN_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "py32f0xx_ll_flash.h"
|
||||
#include "py32f0xx_ll_bus.h"
|
||||
#include "py32f0xx_ll_cortex.h"
|
||||
#include "py32f0xx_ll_dma.h"
|
||||
#include "py32f0xx_ll_exti.h"
|
||||
#include "py32f0xx_ll_gpio.h"
|
||||
#include "py32f0xx_ll_pwr.h"
|
||||
#include "py32f0xx_ll_rcc.h"
|
||||
#include "py32f0xx_ll_system.h"
|
||||
#include "py32f0xx_ll_tim.h"
|
||||
#include "py32f0xx_ll_utils.h"
|
||||
|
||||
|
||||
void APP_ErrorHandler(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __MAIN_H */
|
||||
40
Examples/LL/Flash/SectorEraseAndWrite/py32f0xx_it.c
Normal file
40
Examples/LL/Flash/SectorEraseAndWrite/py32f0xx_it.c
Normal file
@ -0,0 +1,40 @@
|
||||
#include "main.h"
|
||||
#include "py32f0xx_it.h"
|
||||
|
||||
/**
|
||||
* @brief This function handles Non maskable interrupt.
|
||||
*/
|
||||
void NMI_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles Hard fault interrupt.
|
||||
*/
|
||||
void HardFault_Handler(void)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles System service call via SWI instruction.
|
||||
*/
|
||||
void SVC_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles Pendable request for system service.
|
||||
*/
|
||||
void PendSV_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles System tick timer.
|
||||
*/
|
||||
void SysTick_Handler(void)
|
||||
{
|
||||
}
|
||||
19
Examples/LL/Flash/SectorEraseAndWrite/py32f0xx_it.h
Normal file
19
Examples/LL/Flash/SectorEraseAndWrite/py32f0xx_it.h
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef __PY32F0XX_IT_H
|
||||
#define __PY32F0XX_IT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void NMI_Handler(void);
|
||||
void HardFault_Handler(void);
|
||||
void SVC_Handler(void);
|
||||
void PendSV_Handler(void);
|
||||
void SysTick_Handler(void);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __PY32F0XX_IT_H */
|
||||
Loading…
x
Reference in New Issue
Block a user