mirror of
https://github.com/EFeru/hoverboard-sideboard-hack-GD.git
synced 2025-08-18 09:36:11 +00:00
Initial commit
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
/*!
|
||||
\file gd32f1x0_it.c
|
||||
\brief interrupt service routines
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright (C) 2017 GigaDevice
|
||||
|
||||
2014-12-26, V1.0.0, platform GD32F1x0(x=3,5)
|
||||
2016-01-15, V2.0.0, platform GD32F1x0(x=3,5,7,9)
|
||||
2016-04-30, V3.0.0, firmware update for GD32F1x0(x=3,5,7,9)
|
||||
2017-06-19, V3.1.0, firmware update for GD32F1x0(x=3,5,7,9)
|
||||
*/
|
||||
|
||||
#include "gd32f1x0_it.h"
|
||||
#include "gd32f1x0_eval.h"
|
||||
#include "main.h"
|
||||
|
||||
/*!
|
||||
\brief this function handles NMI exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void NMI_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles HardFault exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void HardFault_Handler(void)
|
||||
{
|
||||
/* if Hard Fault exception occurs, go to infinite loop */
|
||||
while (1);
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles MemManage exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void MemManage_Handler(void)
|
||||
{
|
||||
/* if Memory Manage exception occurs, go to infinite loop */
|
||||
while (1);
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles BusFault exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void BusFault_Handler(void)
|
||||
{
|
||||
/* if Bus Fault exception occurs, go to infinite loop */
|
||||
while (1);
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles UsageFault exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void UsageFault_Handler(void)
|
||||
{
|
||||
/* if Usage Fault exception occurs, go to infinite loop */
|
||||
while (1);
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles SVC exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void SVC_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles DebugMon exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void DebugMon_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles PendSV exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void PendSV_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles SysTick exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void SysTick_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles external lines 0 to 1 interrupt request
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void EXTI0_1_IRQHandler(void)
|
||||
{
|
||||
if(RESET != exti_interrupt_flag_get(WAKEUP_KEY_EXTI_LINE)){
|
||||
uint16_t wp_pages_bitmap = ((uint16_t)(WRITE_PROTECT_PAGES(WP_PAGES_BIT_1) | WRITE_PROTECT_PAGES(WP_PAGES_BIT_2) | WRITE_PROTECT_PAGES(WP_PAGES_BIT_3)));
|
||||
|
||||
/* enable target pages write protection */
|
||||
fmc_ob_write_protection_enable(wp_pages_bitmap);
|
||||
|
||||
/* clear the interrupt flag */
|
||||
exti_interrupt_flag_clear(WAKEUP_KEY_EXTI_LINE);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles external lines 4 to 15 interrupt request
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void EXTI4_15_IRQHandler(void)
|
||||
{
|
||||
if(RESET != exti_interrupt_flag_get(TAMPER_KEY_EXTI_LINE)){
|
||||
uint16_t wp_pages_bitmap = ((uint16_t)(WRITE_PROTECT_PAGES(WP_PAGES_BIT_1) | WRITE_PROTECT_PAGES(WP_PAGES_BIT_3)));
|
||||
|
||||
/* disable target pages write protection */
|
||||
fmc_ob_write_protection_disable(wp_pages_bitmap);
|
||||
|
||||
/* clear the interrupt flag */
|
||||
exti_interrupt_flag_clear(TAMPER_KEY_EXTI_LINE);
|
||||
}
|
||||
|
||||
if(RESET != exti_interrupt_flag_get(USER_KEY_EXTI_LINE)){
|
||||
/* disable target pages write protection */
|
||||
fmc_ob_write_protection_disable(WP_ALL_PAGES_BITMAP);
|
||||
|
||||
/* clear the interrupt flag */
|
||||
exti_interrupt_flag_clear(USER_KEY_EXTI_LINE);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,46 @@
|
||||
/*!
|
||||
\file gd32f1x0_it.h
|
||||
\brief the header file of the ISR
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright (C) 2017 GigaDevice
|
||||
|
||||
2014-12-26, V1.0.0, platform GD32F1x0(x=3,5)
|
||||
2016-01-15, V2.0.0, platform GD32F1x0(x=3,5,7,9)
|
||||
2016-04-30, V3.0.0, firmware update for GD32F1x0(x=3,5,7,9)
|
||||
2017-06-19, V3.1.0, firmware update for GD32F1x0(x=3,5,7,9)
|
||||
*/
|
||||
|
||||
#ifndef GD32F1X0_IT_H
|
||||
#define GD32F1X0_IT_H
|
||||
|
||||
#include "gd32f1x0.h"
|
||||
|
||||
/* function declarations */
|
||||
/* this function handles NMI exception */
|
||||
void NMI_Handler(void);
|
||||
/* this function handles HardFault exception */
|
||||
void HardFault_Handler(void);
|
||||
/* this function handles MemManage exception */
|
||||
void MemManage_Handler(void);
|
||||
/* this function handles BusFault exception */
|
||||
void BusFault_Handler(void);
|
||||
/* this function handles UsageFault exception */
|
||||
void UsageFault_Handler(void);
|
||||
/* this function handles SVC exception */
|
||||
void SVC_Handler(void);
|
||||
/* this function handles DebugMon exception */
|
||||
void DebugMon_Handler(void);
|
||||
/* this function handles PendSV exception */
|
||||
void PendSV_Handler(void);
|
||||
/* this function handles SysTick exception */
|
||||
void SysTick_Handler(void);
|
||||
/* this function handles external lines 0 to 1 interrupt request */
|
||||
void EXTI0_1_IRQHandler(void);
|
||||
/* this function handles external lines 4 to 15 interrupt request */
|
||||
void EXTI4_15_IRQHandler(void);
|
||||
|
||||
#endif /* GD32F1X0_IT_H */
|
||||
|
||||
|
@@ -0,0 +1,47 @@
|
||||
/*!
|
||||
\file gd32f1x0_libopt.h
|
||||
\brief library optional for gd32f1x0
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright (C) 2017 GigaDevice
|
||||
|
||||
2014-12-26, V1.0.0, platform GD32F1x0(x=3,5)
|
||||
2016-01-15, V2.0.0, platform GD32F1x0(x=3,5,7,9)
|
||||
2016-04-30, V3.0.0, firmware update for GD32F1x0(x=3,5,7,9)
|
||||
2017-06-19, V3.1.0, firmware update for GD32F1x0(x=3,5,7,9)
|
||||
*/
|
||||
|
||||
#ifndef GD32F1X0_LIBOPT_H
|
||||
#define GD32F1X0_LIBOPT_H
|
||||
|
||||
#include "gd32f1x0_adc.h"
|
||||
#include "gd32f1x0_cec.h"
|
||||
#include "gd32f1x0_crc.h"
|
||||
#include "gd32f1x0_cmp.h"
|
||||
#include "gd32f1x0_dac.h"
|
||||
#include "gd32f1x0_dbg.h"
|
||||
#include "gd32f1x0_dma.h"
|
||||
#include "gd32f1x0_exti.h"
|
||||
#include "gd32f1x0_fmc.h"
|
||||
#include "gd32f1x0_gpio.h"
|
||||
#include "gd32f1x0_syscfg.h"
|
||||
#include "gd32f1x0_i2c.h"
|
||||
#include "gd32f1x0_fwdgt.h"
|
||||
#include "gd32f1x0_pmu.h"
|
||||
#include "gd32f1x0_rcu.h"
|
||||
#include "gd32f1x0_rtc.h"
|
||||
#include "gd32f1x0_spi.h"
|
||||
#include "gd32f1x0_timer.h"
|
||||
#include "gd32f1x0_usart.h"
|
||||
#include "gd32f1x0_wwdgt.h"
|
||||
#include "gd32f1x0_misc.h"
|
||||
#include "gd32f1x0_tsi.h"
|
||||
#ifdef GD32F170_190
|
||||
#include "gd32f1x0_slcd.h"
|
||||
#include "gd32f1x0_opa.h"
|
||||
#include "gd32f1x0_ivref.h"
|
||||
#include "gd32f1x0_can.h"
|
||||
#endif /* GD32F170_190 */
|
||||
|
||||
#endif /* GD32F1X0_LIBOPT_H */
|
@@ -0,0 +1,194 @@
|
||||
/*!
|
||||
\file main.c
|
||||
\brief main flash pages write protection
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright (C) 2017 GigaDevice
|
||||
|
||||
2014-12-26, V1.0.0, platform GD32F1x0(x=3,5)
|
||||
2016-01-15, V2.0.0, platform GD32F1x0(x=3,5,7,9)
|
||||
2016-04-30, V3.0.0, firmware update for GD32F1x0(x=3,5,7,9)
|
||||
2017-06-19, V3.1.0, firmware update for GD32F1x0(x=3,5,7,9)
|
||||
*/
|
||||
|
||||
#include "gd32f1x0.h"
|
||||
#include "gd32f1x0_eval.h"
|
||||
#include "main.h"
|
||||
|
||||
/*!
|
||||
\brief enable some pages' write protection function by configuring option byte
|
||||
\param[in] wp_pages_bitmap: bitmap of pages which need to be enabled write protection function
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void fmc_ob_write_protection_enable(uint16_t wp_pages_bitmap)
|
||||
{
|
||||
uint8_t ob_user;
|
||||
uint16_t ob_data;
|
||||
uint16_t old_wp, new_wp;
|
||||
|
||||
/* unlock the main flash and option byte */
|
||||
fmc_unlock();
|
||||
ob_unlock();
|
||||
|
||||
/* clear all pending flags */
|
||||
fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
|
||||
|
||||
/* backup the old OB_USER, OB_DATA and OB_WP */
|
||||
ob_user = ob_user_get();
|
||||
ob_data = ob_data_get();
|
||||
old_wp = ob_write_protection_get();
|
||||
|
||||
/* it's need to do operation just when the pages indicated by wp_pages_bitmap have not been enabled */
|
||||
if(0 != (old_wp & wp_pages_bitmap)){
|
||||
/* caculate the new write protectiom bitmap */
|
||||
new_wp = ((~old_wp) | wp_pages_bitmap);
|
||||
|
||||
/* erase the option byte before modify the content */
|
||||
ob_erase();
|
||||
|
||||
/* restore the OB_USER and OB_DATA */
|
||||
ob_user_write(ob_user);
|
||||
ob_data_program(OB_DATA_ADDR0, (uint8_t)ob_data);
|
||||
ob_data_program(OB_DATA_ADDR1, (uint8_t)(ob_data >> 8));
|
||||
|
||||
/* enable the new write protection in option byte */
|
||||
ob_write_protection_enable(new_wp);
|
||||
|
||||
/* lock the option byte firstly and then lock the main flash after operation */
|
||||
ob_lock();
|
||||
fmc_lock();
|
||||
|
||||
/* reload the option byte and generate a system reset */
|
||||
ob_reset();
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief disable some pages' write protection function by configuring option byte
|
||||
\param[in] wp_pages_bitmap: bitmap of pages which need to be disabled write protection function
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void fmc_ob_write_protection_disable(uint16_t wp_pages_bitmap)
|
||||
{
|
||||
uint8_t ob_user;
|
||||
uint16_t ob_data;
|
||||
uint16_t old_wp, new_wp;
|
||||
|
||||
/* unlock the main flash and option byte */
|
||||
fmc_unlock();
|
||||
ob_unlock();
|
||||
|
||||
/* clear all pending flags */
|
||||
fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
|
||||
|
||||
/* backup the old OB_USER, OB_DATA and OB_WP */
|
||||
ob_user = ob_user_get();
|
||||
ob_data = ob_data_get();
|
||||
old_wp = ob_write_protection_get();
|
||||
|
||||
/* it's need to do operation just when the pages indicated by wp_pages_bitmap have been enabled */
|
||||
if((old_wp & wp_pages_bitmap) != wp_pages_bitmap){
|
||||
/* caculate the new write protectiom bitmap */
|
||||
new_wp = ~(old_wp | wp_pages_bitmap);
|
||||
|
||||
/* erase the option byte before modify the content */
|
||||
ob_erase();
|
||||
|
||||
/* restore the OB_USER and OB_DATA */
|
||||
ob_user_write(ob_user);
|
||||
ob_data_program(OB_DATA_ADDR0, (uint8_t)ob_data);
|
||||
ob_data_program(OB_DATA_ADDR1, (uint8_t)(ob_data >> 8));
|
||||
|
||||
/* enable the new write protection in option byte */
|
||||
ob_write_protection_enable(new_wp);
|
||||
|
||||
/* lock the option byte firstly and then lock the main flash after operation */
|
||||
ob_lock();
|
||||
fmc_lock();
|
||||
|
||||
/* reload the option byte and generate a system reset */
|
||||
ob_reset();
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief erase and program flash, meanwhile check the operation result
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void fmc_erase_and_program(void)
|
||||
{
|
||||
uint32_t *ptr = (uint32_t *)ERASE_PAGE_START_ADDR;
|
||||
|
||||
/* unlock the flash program/erase controller */
|
||||
fmc_unlock();
|
||||
|
||||
/* clear all pending flags */
|
||||
fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
|
||||
|
||||
/* erase target page */
|
||||
fmc_page_erase(ERASE_PAGE_START_ADDR);
|
||||
/* check the erase result, light the LED3 if the result is failed */
|
||||
if(0xFFFFFFFF != (*ptr)){
|
||||
gd_eval_led_on(LED3);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* program target address */
|
||||
fmc_word_program(PROGRAM_ADDRESS, PROGRAM_DATA);
|
||||
/* check the program result, light the LED3 if the result is failed */
|
||||
if(PROGRAM_DATA != (*ptr)){
|
||||
gd_eval_led_on(LED3);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* light the LED4 if the erase and program result are both successful */
|
||||
gd_eval_led_on(LED4);
|
||||
|
||||
/* clear all pending flags */
|
||||
fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
|
||||
|
||||
/* lock the main FMC after the operation */
|
||||
fmc_lock();
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief main function
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
/* initialize the LED1 and LED2 */
|
||||
gd_eval_led_init(LED1);
|
||||
gd_eval_led_init(LED2);
|
||||
gd_eval_led_init(LED3);
|
||||
gd_eval_led_init(LED4);
|
||||
|
||||
/* configure the keys */
|
||||
gd_eval_key_init(KEY_WAKEUP, KEY_MODE_EXTI);
|
||||
gd_eval_key_init(KEY_TAMPER, KEY_MODE_EXTI);
|
||||
gd_eval_key_init(KEY_USER, KEY_MODE_EXTI);
|
||||
|
||||
/* check the write protection result and light corresponding LEDs */
|
||||
if(WP_ALL_PAGES_BITMAP == ob_write_protection_get()){
|
||||
gd_eval_led_on(LED1);
|
||||
}else{
|
||||
gd_eval_led_on(LED2);
|
||||
}
|
||||
|
||||
/* erase and program flash,
|
||||
failure (light LED3) indicates the page is in write protection,
|
||||
success (light LED4) indicates the page is not in write protection */
|
||||
fmc_erase_and_program();
|
||||
|
||||
while(1);
|
||||
}
|
||||
|
@@ -0,0 +1,44 @@
|
||||
/*!
|
||||
\file main.h
|
||||
\brief the header file of main
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright (C) 2017 GigaDevice
|
||||
|
||||
2014-12-26, V1.0.0, platform GD32F1x0(x=3,5)
|
||||
2016-01-15, V2.0.0, platform GD32F1x0(x=3,5,7,9)
|
||||
2016-04-30, V3.0.0, firmware update for GD32F1x0(x=3,5,7,9)
|
||||
2017-06-19, V3.1.0, firmware update for GD32F1x0(x=3,5,7,9)
|
||||
*/
|
||||
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
/* bit num chosen for this example */
|
||||
#define WP_PAGES_BIT_1 (8U)
|
||||
#define WP_PAGES_BIT_2 (9U)
|
||||
#define WP_PAGES_BIT_3 (10U)
|
||||
#define WP_ALL_PAGES_BITMAP (0xFFFFU)
|
||||
|
||||
/* construct the bitmap which pages need to be configured the write protection */
|
||||
#define WRITE_PROTECT_PAGES(bit_num) (BIT(bit_num))
|
||||
|
||||
/* every bit in OB_WP indicates 4 pages if they are in write protection */
|
||||
#define PAGE_NUM_PER_WP_BIT (4U)
|
||||
|
||||
/* address and data for fmc operation */
|
||||
#define ERASE_PAGE (WP_PAGES_BIT_1 * PAGE_NUM_PER_WP_BIT)
|
||||
#define ERASE_PAGE_START_ADDR (0x08000000U + 0x400U * ERASE_PAGE)
|
||||
#define PROGRAM_ADDRESS ERASE_PAGE_START_ADDR
|
||||
#define PROGRAM_DATA (0x12345678U)
|
||||
|
||||
/* enable some pages' write protection function by configuring option byte */
|
||||
void fmc_ob_write_protection_enable(uint16_t wp_pages_bitmap);
|
||||
/* disable some pages' write protection function by configuring option byte */
|
||||
void fmc_ob_write_protection_disable(uint16_t wp_pages_bitmap);
|
||||
/* erase and program flash, meanwhile check the operation result */
|
||||
void fmc_erase_and_program(void);
|
||||
|
||||
#endif
|
||||
|
@@ -0,0 +1,41 @@
|
||||
/*!
|
||||
\file readme.txt
|
||||
\brief description of write_protection example
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright (C) 2017 GigaDevice
|
||||
|
||||
2014-12-26, V1.0.0, platform GD32F1x0(x=3,5)
|
||||
2016-01-15, V2.0.0, platform GD32F1x0(x=3,5,7,9)
|
||||
2016-04-30, V3.0.0, firmware update for GD32F1x0(x=3,5,7,9)
|
||||
2017-06-19, V3.1.0, firmware update for GD32F1x0(x=3,5,7,9)
|
||||
*/
|
||||
|
||||
This example is based on the GD32150R-EVAL board, it provides a description
|
||||
of how to enable and disable the write protection for the embedded flash.
|
||||
|
||||
After system start-up, LED1 and LED4 usually are lighted. Meanwhile, LED2 and LED3 usually
|
||||
are off. It indicates that no pages of flash are in write protection and all pages can be
|
||||
erased/programmed successfully. If not, press the key User to change the board to the normal
|
||||
condition.
|
||||
|
||||
STEP1: Press the key Wakeup. The LED1 and LED4 will be off and LED2 and LED3 will be on.
|
||||
It indicates that some pages are in write protection and erasing/programming on them is failed.
|
||||
|
||||
STEP2: Press the key User. The LED1 and LED4 will be on and LED2 and LED3 will be off.
|
||||
It indicates that those pages are out of write protection and erasing/programming on them is successful.
|
||||
|
||||
STEP3: Repeat the STEP1.
|
||||
|
||||
STEP4: Press the key Tamper. The LED2 and LED4 will be on and LED1 and LED3 will be off.
|
||||
It indicates that some pages are out of write protection and can be erased/programmed successfully.
|
||||
But some other pages are still in write protection.
|
||||
|
||||
STEP5: Press the key User. The LED1 and LED4 will be on and LED2 and LED3 will be off.
|
||||
It indicates that all pages are out of write protection and erasing/programming on them is successful.
|
||||
|
||||
Note: After testing the example, please ensure to press the key User to disable all pages'
|
||||
write protection.
|
||||
|
||||
|
Reference in New Issue
Block a user