initial commit
This commit is contained in:
209
project/src/at32f413_int.c
Normal file
209
project/src/at32f413_int.c
Normal file
@@ -0,0 +1,209 @@
|
||||
/* add user code begin Header */
|
||||
/**
|
||||
**************************************************************************
|
||||
* @file at32f413_int.c
|
||||
* @brief main interrupt service routines.
|
||||
**************************************************************************
|
||||
* Copyright notice & Disclaimer
|
||||
*
|
||||
* The software Board Support Package (BSP) that is made available to
|
||||
* download from Artery official website is the copyrighted work of Artery.
|
||||
* Artery authorizes customers to use, copy, and distribute the BSP
|
||||
* software and its related documentation for the purpose of design and
|
||||
* development in conjunction with Artery microcontrollers. Use of the
|
||||
* software is governed by this copyright notice and the following disclaimer.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
|
||||
* GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
|
||||
* TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
|
||||
* STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
|
||||
* INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
|
||||
*
|
||||
**************************************************************************
|
||||
*/
|
||||
/* add user code end Header */
|
||||
|
||||
/* includes ------------------------------------------------------------------*/
|
||||
#include "at32f413_int.h"
|
||||
|
||||
/* private includes ----------------------------------------------------------*/
|
||||
/* add user code begin private includes */
|
||||
|
||||
/* add user code end private includes */
|
||||
|
||||
/* private typedef -----------------------------------------------------------*/
|
||||
/* add user code begin private typedef */
|
||||
|
||||
/* add user code end private typedef */
|
||||
|
||||
/* private define ------------------------------------------------------------*/
|
||||
/* add user code begin private define */
|
||||
|
||||
/* add user code end private define */
|
||||
|
||||
/* private macro -------------------------------------------------------------*/
|
||||
/* add user code begin private macro */
|
||||
|
||||
/* add user code end private macro */
|
||||
|
||||
/* private variables ---------------------------------------------------------*/
|
||||
/* add user code begin private variables */
|
||||
|
||||
/* add user code end private variables */
|
||||
|
||||
/* private function prototypes --------------------------------------------*/
|
||||
/* add user code begin function prototypes */
|
||||
|
||||
/* add user code end function prototypes */
|
||||
|
||||
/* private user code ---------------------------------------------------------*/
|
||||
/* add user code begin 0 */
|
||||
|
||||
/* add user code end 0 */
|
||||
|
||||
/* external variables ---------------------------------------------------------*/
|
||||
/* add user code begin external variables */
|
||||
|
||||
/* add user code end external variables */
|
||||
|
||||
/**
|
||||
* @brief this function handles nmi exception.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void NMI_Handler(void)
|
||||
{
|
||||
/* add user code begin NonMaskableInt_IRQ 0 */
|
||||
|
||||
/* add user code end NonMaskableInt_IRQ 0 */
|
||||
|
||||
/* add user code begin NonMaskableInt_IRQ 1 */
|
||||
|
||||
/* add user code end NonMaskableInt_IRQ 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function handles hard fault exception.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void HardFault_Handler(void)
|
||||
{
|
||||
/* add user code begin HardFault_IRQ 0 */
|
||||
|
||||
/* add user code end HardFault_IRQ 0 */
|
||||
/* go to infinite loop when hard fault exception occurs */
|
||||
while (1)
|
||||
{
|
||||
/* add user code begin W1_HardFault_IRQ 0 */
|
||||
|
||||
/* add user code end W1_HardFault_IRQ 0 */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function handles memory manage exception.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void MemManage_Handler(void)
|
||||
{
|
||||
/* add user code begin MemoryManagement_IRQ 0 */
|
||||
|
||||
/* add user code end MemoryManagement_IRQ 0 */
|
||||
/* go to infinite loop when memory manage exception occurs */
|
||||
while (1)
|
||||
{
|
||||
/* add user code begin W1_MemoryManagement_IRQ 0 */
|
||||
|
||||
/* add user code end W1_MemoryManagement_IRQ 0 */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function handles bus fault exception.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void BusFault_Handler(void)
|
||||
{
|
||||
/* add user code begin BusFault_IRQ 0 */
|
||||
|
||||
/* add user code end BusFault_IRQ 0 */
|
||||
/* go to infinite loop when bus fault exception occurs */
|
||||
while (1)
|
||||
{
|
||||
/* add user code begin W1_BusFault_IRQ 0 */
|
||||
|
||||
/* add user code end W1_BusFault_IRQ 0 */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function handles usage fault exception.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void UsageFault_Handler(void)
|
||||
{
|
||||
/* add user code begin UsageFault_IRQ 0 */
|
||||
|
||||
/* add user code end UsageFault_IRQ 0 */
|
||||
/* go to infinite loop when usage fault exception occurs */
|
||||
while (1)
|
||||
{
|
||||
/* add user code begin W1_UsageFault_IRQ 0 */
|
||||
|
||||
/* add user code end W1_UsageFault_IRQ 0 */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function handles svcall exception.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void SVC_Handler(void)
|
||||
{
|
||||
/* add user code begin SVCall_IRQ 0 */
|
||||
|
||||
/* add user code end SVCall_IRQ 0 */
|
||||
/* add user code begin SVCall_IRQ 1 */
|
||||
|
||||
/* add user code end SVCall_IRQ 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function handles debug monitor exception.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void DebugMon_Handler(void)
|
||||
{
|
||||
/* add user code begin DebugMonitor_IRQ 0 */
|
||||
|
||||
/* add user code end DebugMonitor_IRQ 0 */
|
||||
/* add user code begin DebugMonitor_IRQ 1 */
|
||||
|
||||
/* add user code end DebugMonitor_IRQ 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function handles pendsv_handler exception.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void PendSV_Handler(void)
|
||||
{
|
||||
/* add user code begin PendSV_IRQ 0 */
|
||||
|
||||
/* add user code end PendSV_IRQ 0 */
|
||||
/* add user code begin PendSV_IRQ 1 */
|
||||
|
||||
/* add user code end PendSV_IRQ 1 */
|
||||
}
|
||||
|
||||
/* add user code begin 1 */
|
||||
|
||||
/* add user code end 1 */
|
||||
624
project/src/at32f413_wk_config.c
Normal file
624
project/src/at32f413_wk_config.c
Normal file
@@ -0,0 +1,624 @@
|
||||
/* add user code begin Header */
|
||||
/**
|
||||
**************************************************************************
|
||||
* @file at32f413_wk_config.c
|
||||
* @brief work bench config program
|
||||
**************************************************************************
|
||||
* Copyright notice & Disclaimer
|
||||
*
|
||||
* The software Board Support Package (BSP) that is made available to
|
||||
* download from Artery official website is the copyrighted work of Artery.
|
||||
* Artery authorizes customers to use, copy, and distribute the BSP
|
||||
* software and its related documentation for the purpose of design and
|
||||
* development in conjunction with Artery microcontrollers. Use of the
|
||||
* software is governed by this copyright notice and the following disclaimer.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
|
||||
* GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
|
||||
* TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
|
||||
* STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
|
||||
* INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
|
||||
*
|
||||
**************************************************************************
|
||||
*/
|
||||
/* add user code end Header */
|
||||
|
||||
#include "at32f413_wk_config.h"
|
||||
|
||||
/* private includes ----------------------------------------------------------*/
|
||||
/* add user code begin private includes */
|
||||
|
||||
/* add user code end private includes */
|
||||
|
||||
/* private typedef -----------------------------------------------------------*/
|
||||
/* add user code begin private typedef */
|
||||
|
||||
/* add user code end private typedef */
|
||||
|
||||
/* private define ------------------------------------------------------------*/
|
||||
/* add user code begin private define */
|
||||
|
||||
/* add user code end private define */
|
||||
|
||||
/* private macro -------------------------------------------------------------*/
|
||||
/* add user code begin private macro */
|
||||
|
||||
/* add user code end private macro */
|
||||
|
||||
/* private variables ---------------------------------------------------------*/
|
||||
/* add user code begin private variables */
|
||||
|
||||
/* add user code end private variables */
|
||||
|
||||
/* private function prototypes --------------------------------------------*/
|
||||
/* add user code begin function prototypes */
|
||||
|
||||
/* add user code end function prototypes */
|
||||
|
||||
/* private user code ---------------------------------------------------------*/
|
||||
/* add user code begin 0 */
|
||||
|
||||
/* add user code end 0 */
|
||||
|
||||
/**
|
||||
* @brief system clock config program
|
||||
* @note the system clock is configured as follow:
|
||||
* system clock (sclk) = hick / 12 * pll_mult
|
||||
* system clock source = HICK_VALUE
|
||||
* - sclk = 200000000
|
||||
* - ahbdiv = 1
|
||||
* - ahbclk = 200000000
|
||||
* - apb1div = 2
|
||||
* - apb1clk = 100000000
|
||||
* - apb2div = 2
|
||||
* - apb2clk = 100000000
|
||||
* - pll_mult = 50
|
||||
* - pll_range = GT72MHZ (greater than 72 mhz)
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void wk_system_clock_config(void)
|
||||
{
|
||||
/* reset crm */
|
||||
crm_reset();
|
||||
|
||||
/* enable lick */
|
||||
crm_clock_source_enable(CRM_CLOCK_SOURCE_LICK, TRUE);
|
||||
|
||||
/* wait till lick is ready */
|
||||
while(crm_flag_get(CRM_LICK_STABLE_FLAG) != SET)
|
||||
{
|
||||
}
|
||||
|
||||
/* enable hick */
|
||||
crm_clock_source_enable(CRM_CLOCK_SOURCE_HICK, TRUE);
|
||||
|
||||
/* wait till hick is ready */
|
||||
while(crm_flag_get(CRM_HICK_STABLE_FLAG) != SET)
|
||||
{
|
||||
}
|
||||
|
||||
/* config pll clock resource */
|
||||
crm_pll_config(CRM_PLL_SOURCE_HICK, CRM_PLL_MULT_50, CRM_PLL_OUTPUT_RANGE_GT72MHZ);
|
||||
|
||||
/* enable pll */
|
||||
crm_clock_source_enable(CRM_CLOCK_SOURCE_PLL, TRUE);
|
||||
|
||||
/* wait till pll is ready */
|
||||
while(crm_flag_get(CRM_PLL_STABLE_FLAG) != SET)
|
||||
{
|
||||
}
|
||||
|
||||
/* config ahbclk */
|
||||
crm_ahb_div_set(CRM_AHB_DIV_1);
|
||||
|
||||
/* config apb2clk, the maximum frequency of APB2 clock is 100 MHz */
|
||||
crm_apb2_div_set(CRM_APB2_DIV_2);
|
||||
|
||||
/* config apb1clk, the maximum frequency of APB1 clock is 100 MHz */
|
||||
crm_apb1_div_set(CRM_APB1_DIV_2);
|
||||
|
||||
/* enable auto step mode */
|
||||
crm_auto_step_mode_enable(TRUE);
|
||||
|
||||
/* select pll as system clock source */
|
||||
crm_sysclk_switch(CRM_SCLK_PLL);
|
||||
|
||||
/* wait till pll is used as system clock source */
|
||||
while(crm_sysclk_switch_status_get() != CRM_SCLK_PLL)
|
||||
{
|
||||
}
|
||||
|
||||
/* disable auto step mode */
|
||||
crm_auto_step_mode_enable(FALSE);
|
||||
|
||||
/* update system_core_clock global variable */
|
||||
system_core_clock_update();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief config periph clock
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void wk_periph_clock_config(void)
|
||||
{
|
||||
/* enable iomux periph clock */
|
||||
crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
|
||||
|
||||
/* enable gpioa periph clock */
|
||||
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
|
||||
|
||||
/* enable gpiob periph clock */
|
||||
crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
|
||||
|
||||
/* enable tmr1 periph clock */
|
||||
crm_periph_clock_enable(CRM_TMR1_PERIPH_CLOCK, TRUE);
|
||||
|
||||
/* enable tmr9 periph clock */
|
||||
crm_periph_clock_enable(CRM_TMR9_PERIPH_CLOCK, TRUE);
|
||||
|
||||
/* enable tmr10 periph clock */
|
||||
crm_periph_clock_enable(CRM_TMR10_PERIPH_CLOCK, TRUE);
|
||||
|
||||
/* enable tmr3 periph clock */
|
||||
crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, TRUE);
|
||||
|
||||
/* enable usart2 periph clock */
|
||||
crm_periph_clock_enable(CRM_USART2_PERIPH_CLOCK, TRUE);
|
||||
|
||||
/* enable can1 periph clock */
|
||||
crm_periph_clock_enable(CRM_CAN1_PERIPH_CLOCK, TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief init debug function.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void wk_debug_config(void)
|
||||
{
|
||||
/* jtag-dp disabled and sw-dp enabled */
|
||||
gpio_pin_remap_config(SWJTAG_GMUX_010, TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief nvic config
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void wk_nvic_config(void)
|
||||
{
|
||||
nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief init gpio_input/gpio_output/gpio_analog/eventout function.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void wk_gpio_config(void)
|
||||
{
|
||||
/* add user code begin gpio_config 0 */
|
||||
|
||||
/* add user code end gpio_config 0 */
|
||||
|
||||
gpio_init_type gpio_init_struct;
|
||||
gpio_default_para_init(&gpio_init_struct);
|
||||
|
||||
/* add user code begin gpio_config 1 */
|
||||
|
||||
/* add user code end gpio_config 1 */
|
||||
|
||||
/* gpio input config */
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
|
||||
gpio_init_struct.gpio_pins = SW_IN2_PIN | SW_IN1_PIN;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init(GPIOB, &gpio_init_struct);
|
||||
|
||||
/* gpio output config */
|
||||
gpio_bits_reset(GPIOB, MODE2_PIN | MODE1_PIN | MODE0_PIN);
|
||||
gpio_bits_reset(DIR_GPIO_PORT, DIR_PIN);
|
||||
|
||||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
|
||||
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
|
||||
gpio_init_struct.gpio_pins = MODE2_PIN | MODE1_PIN | MODE0_PIN;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init(GPIOB, &gpio_init_struct);
|
||||
|
||||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
|
||||
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
|
||||
gpio_init_struct.gpio_pins = DIR_PIN;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init(DIR_GPIO_PORT, &gpio_init_struct);
|
||||
|
||||
/* gpio analog config */
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
|
||||
gpio_init_struct.gpio_pins = EN_PIN;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init(EN_GPIO_PORT, &gpio_init_struct);
|
||||
|
||||
/* add user code begin gpio_config 2 */
|
||||
|
||||
/* add user code end gpio_config 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief init exint function.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void wk_exint_config(void)
|
||||
{
|
||||
/* add user code begin exint_config 0 */
|
||||
|
||||
/* add user code end exint_config 0 */
|
||||
|
||||
gpio_init_type gpio_init_struct;
|
||||
exint_init_type exint_init_struct;
|
||||
|
||||
/* add user code begin exint_config 1 */
|
||||
|
||||
/* add user code end exint_config 1 */
|
||||
|
||||
/* configure the EXINT5 */
|
||||
gpio_default_para_init(&gpio_init_struct);
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
|
||||
gpio_init_struct.gpio_pins = GPIO_PINS_5;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init(GPIOA, &gpio_init_struct);
|
||||
|
||||
gpio_exint_line_config(GPIO_PORT_SOURCE_GPIOA, GPIO_PINS_SOURCE5);
|
||||
|
||||
exint_default_para_init(&exint_init_struct);
|
||||
exint_init_struct.line_enable = TRUE;
|
||||
exint_init_struct.line_mode = EXINT_LINE_INTERRUPUT;
|
||||
exint_init_struct.line_select = EXINT_LINE_5;
|
||||
exint_init_struct.line_polarity = EXINT_TRIGGER_RISING_EDGE;
|
||||
exint_init(&exint_init_struct);
|
||||
|
||||
/* add user code begin exint_config 2 */
|
||||
|
||||
/* add user code end exint_config 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief init usart2 function
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void wk_usart2_init(void)
|
||||
{
|
||||
/* add user code begin usart2_init 0 */
|
||||
|
||||
/* add user code end usart2_init 0 */
|
||||
|
||||
gpio_init_type gpio_init_struct;
|
||||
gpio_default_para_init(&gpio_init_struct);
|
||||
|
||||
/* add user code begin usart2_init 1 */
|
||||
|
||||
/* add user code end usart2_init 1 */
|
||||
|
||||
/* configure the TX pin */
|
||||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
|
||||
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
|
||||
gpio_init_struct.gpio_pins = GPIO_PINS_2;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init(GPIOA, &gpio_init_struct);
|
||||
|
||||
/* configure the RX pin */
|
||||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
|
||||
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
|
||||
gpio_init_struct.gpio_pins = GPIO_PINS_3;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init(GPIOA, &gpio_init_struct);
|
||||
|
||||
/* configure param */
|
||||
usart_init(USART2, 115200, USART_DATA_8BITS, USART_STOP_1_BIT);
|
||||
usart_transmitter_enable(USART2, TRUE);
|
||||
usart_receiver_enable(USART2, TRUE);
|
||||
usart_parity_selection_config(USART2, USART_PARITY_NONE);
|
||||
|
||||
usart_hardware_flow_control_set(USART2, USART_HARDWARE_FLOW_NONE);
|
||||
|
||||
usart_enable(USART2, TRUE);
|
||||
|
||||
/* add user code begin usart2_init 2 */
|
||||
|
||||
/* add user code end usart2_init 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief init tmr1 function.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void wk_tmr1_init(void)
|
||||
{
|
||||
/* add user code begin tmr1_init 0 */
|
||||
|
||||
/* add user code end tmr1_init 0 */
|
||||
|
||||
|
||||
/* add user code begin tmr1_init 1 */
|
||||
|
||||
/* add user code end tmr1_init 1 */
|
||||
|
||||
/* configure counter settings */
|
||||
tmr_base_init(TMR1, 65535, 0);
|
||||
tmr_cnt_dir_set(TMR1, TMR_COUNT_UP);
|
||||
tmr_clock_source_div_set(TMR1, TMR_CLOCK_DIV1);
|
||||
tmr_repetition_counter_set(TMR1, 0);
|
||||
tmr_period_buffer_enable(TMR1, FALSE);
|
||||
|
||||
/* configure primary mode settings */
|
||||
tmr_sub_sync_mode_set(TMR1, FALSE);
|
||||
tmr_primary_mode_select(TMR1, TMR_PRIMARY_SEL_RESET);
|
||||
|
||||
tmr_counter_enable(TMR1, TRUE);
|
||||
|
||||
/* add user code begin tmr1_init 2 */
|
||||
|
||||
/* add user code end tmr1_init 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief init tmr3 function.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void wk_tmr3_init(void)
|
||||
{
|
||||
/* add user code begin tmr3_init 0 */
|
||||
|
||||
/* add user code end tmr3_init 0 */
|
||||
|
||||
gpio_init_type gpio_init_struct;
|
||||
tmr_input_config_type tmr_input_struct;
|
||||
|
||||
gpio_default_para_init(&gpio_init_struct);
|
||||
|
||||
/* add user code begin tmr3_init 1 */
|
||||
|
||||
/* add user code end tmr3_init 1 */
|
||||
|
||||
/* configure the CH1 pin */
|
||||
gpio_init_struct.gpio_pins = GPIO_PINS_6;
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
|
||||
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
|
||||
gpio_init(GPIOA, &gpio_init_struct);
|
||||
|
||||
/* configure the CH2 pin */
|
||||
gpio_init_struct.gpio_pins = GPIO_PINS_7;
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
|
||||
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
|
||||
gpio_init(GPIOA, &gpio_init_struct);
|
||||
|
||||
/* configure counter settings */
|
||||
tmr_base_init(TMR3, 65535, 0);
|
||||
tmr_cnt_dir_set(TMR3, TMR_COUNT_UP);
|
||||
tmr_clock_source_div_set(TMR3, TMR_CLOCK_DIV1);
|
||||
tmr_period_buffer_enable(TMR3, FALSE);
|
||||
|
||||
/* configure primary mode settings */
|
||||
tmr_sub_sync_mode_set(TMR3, FALSE);
|
||||
tmr_primary_mode_select(TMR3, TMR_PRIMARY_SEL_RESET);
|
||||
|
||||
/* configure encoder mode */
|
||||
tmr_input_struct.input_channel_select = TMR_SELECT_CHANNEL_1;
|
||||
tmr_input_struct.input_mapped_select = TMR_CC_CHANNEL_MAPPED_DIRECT;
|
||||
tmr_input_struct.input_polarity_select = TMR_INPUT_RISING_EDGE;
|
||||
tmr_input_struct.input_filter_value = 0;
|
||||
tmr_input_channel_init(TMR3, &tmr_input_struct, TMR_CHANNEL_INPUT_DIV_1);
|
||||
|
||||
tmr_input_struct.input_channel_select = TMR_SELECT_CHANNEL_2;
|
||||
tmr_input_struct.input_mapped_select = TMR_CC_CHANNEL_MAPPED_DIRECT;
|
||||
tmr_input_struct.input_polarity_select = TMR_INPUT_RISING_EDGE;
|
||||
tmr_input_struct.input_filter_value = 0;
|
||||
tmr_input_channel_init(TMR3, &tmr_input_struct, TMR_CHANNEL_INPUT_DIV_1);
|
||||
|
||||
tmr_encoder_mode_config(TMR3, TMR_ENCODER_MODE_A, TMR_INPUT_RISING_EDGE, TMR_INPUT_RISING_EDGE);
|
||||
|
||||
tmr_counter_enable(TMR3, TRUE);
|
||||
|
||||
/* add user code begin tmr3_init 2 */
|
||||
|
||||
/* add user code end tmr3_init 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief init tmr9 function.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void wk_tmr9_init(void)
|
||||
{
|
||||
/* add user code begin tmr9_init 0 */
|
||||
|
||||
/* add user code end tmr9_init 0 */
|
||||
|
||||
gpio_init_type gpio_init_struct;
|
||||
tmr_output_config_type tmr_output_struct;
|
||||
gpio_default_para_init(&gpio_init_struct);
|
||||
|
||||
/* add user code begin tmr9_init 1 */
|
||||
|
||||
/* add user code end tmr9_init 1 */
|
||||
|
||||
/* configure the CH2 pin */
|
||||
gpio_init_struct.gpio_pins = STEP_PIN;
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
|
||||
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
|
||||
gpio_init(STEP_GPIO_PORT, &gpio_init_struct);
|
||||
|
||||
/* GPIO PIN remap */
|
||||
gpio_pin_remap_config(TMR9_GMUX, TRUE);
|
||||
|
||||
/* configure counter settings */
|
||||
tmr_base_init(TMR9, 65535, 0);
|
||||
tmr_cnt_dir_set(TMR9, TMR_COUNT_UP);
|
||||
tmr_clock_source_div_set(TMR9, TMR_CLOCK_DIV1);
|
||||
tmr_period_buffer_enable(TMR9, FALSE);
|
||||
|
||||
/* configure sub-mode */
|
||||
tmr_sub_mode_select(TMR9, TMR_SUB_EXTERNAL_CLOCK_MODE_A);
|
||||
|
||||
/* configure sub-mode input */
|
||||
tmr_trigger_input_select(TMR9, TMR_SUB_INPUT_SEL_IS2);
|
||||
|
||||
/* configure channel 2 output settings */
|
||||
tmr_output_struct.oc_mode = TMR_OUTPUT_CONTROL_OFF;
|
||||
tmr_output_struct.oc_output_state = TRUE;
|
||||
tmr_output_struct.occ_output_state = FALSE;
|
||||
tmr_output_struct.oc_polarity = TMR_OUTPUT_ACTIVE_HIGH;
|
||||
tmr_output_struct.occ_polarity = TMR_OUTPUT_ACTIVE_HIGH;
|
||||
tmr_output_struct.oc_idle_state = FALSE;
|
||||
tmr_output_struct.occ_idle_state = FALSE;
|
||||
tmr_output_channel_config(TMR9, TMR_SELECT_CHANNEL_2, &tmr_output_struct);
|
||||
tmr_channel_value_set(TMR9, TMR_SELECT_CHANNEL_2, 0);
|
||||
tmr_output_channel_buffer_enable(TMR9, TMR_SELECT_CHANNEL_2, FALSE);
|
||||
|
||||
tmr_counter_enable(TMR9, TRUE);
|
||||
|
||||
/* add user code begin tmr9_init 2 */
|
||||
|
||||
/* add user code end tmr9_init 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief init tmr10 function.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void wk_tmr10_init(void)
|
||||
{
|
||||
/* add user code begin tmr10_init 0 */
|
||||
|
||||
/* add user code end tmr10_init 0 */
|
||||
|
||||
tmr_output_config_type tmr_output_struct;
|
||||
/* add user code begin tmr10_init 1 */
|
||||
|
||||
/* add user code end tmr10_init 1 */
|
||||
|
||||
/* configure counter settings */
|
||||
tmr_base_init(TMR10, 65535, 0);
|
||||
tmr_cnt_dir_set(TMR10, TMR_COUNT_UP);
|
||||
tmr_clock_source_div_set(TMR10, TMR_CLOCK_DIV1);
|
||||
tmr_period_buffer_enable(TMR10, FALSE);
|
||||
|
||||
/* configure channel 1 output settings */
|
||||
tmr_output_struct.oc_mode = TMR_OUTPUT_CONTROL_OFF;
|
||||
tmr_output_struct.oc_output_state = TRUE;
|
||||
tmr_output_struct.occ_output_state = FALSE;
|
||||
tmr_output_struct.oc_polarity = TMR_OUTPUT_ACTIVE_HIGH;
|
||||
tmr_output_struct.occ_polarity = TMR_OUTPUT_ACTIVE_HIGH;
|
||||
tmr_output_struct.oc_idle_state = FALSE;
|
||||
tmr_output_struct.occ_idle_state = FALSE;
|
||||
tmr_output_channel_config(TMR10, TMR_SELECT_CHANNEL_1, &tmr_output_struct);
|
||||
tmr_channel_value_set(TMR10, TMR_SELECT_CHANNEL_1, 0);
|
||||
tmr_output_channel_buffer_enable(TMR10, TMR_SELECT_CHANNEL_1, FALSE);
|
||||
|
||||
tmr_counter_enable(TMR10, TRUE);
|
||||
|
||||
/* add user code begin tmr10_init 2 */
|
||||
|
||||
/* add user code end tmr10_init 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief init can1 function.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void wk_can1_init(void)
|
||||
{
|
||||
/* add user code begin can1_init 0 */
|
||||
|
||||
/* add user code end can1_init 0 */
|
||||
|
||||
gpio_init_type gpio_init_struct;
|
||||
can_base_type can_base_struct;
|
||||
can_baudrate_type can_baudrate_struct;
|
||||
can_filter_init_type can_filter_init_struct;
|
||||
|
||||
/* add user code begin can1_init 1 */
|
||||
|
||||
/* add user code end can1_init 1 */
|
||||
|
||||
/*gpio-----------------------------------------------------------------------------*/
|
||||
gpio_default_para_init(&gpio_init_struct);
|
||||
|
||||
/* configure the CAN1 TX pin */
|
||||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
|
||||
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
|
||||
gpio_init_struct.gpio_pins = GPIO_PINS_9;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init(GPIOB, &gpio_init_struct);
|
||||
|
||||
/* configure the CAN1 RX pin */
|
||||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
|
||||
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
|
||||
gpio_init_struct.gpio_pins = GPIO_PINS_8;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init(GPIOB, &gpio_init_struct);
|
||||
|
||||
/* GPIO PIN remap */
|
||||
gpio_pin_remap_config(CAN1_GMUX_0010, TRUE);
|
||||
|
||||
/*can_base_init--------------------------------------------------------------------*/
|
||||
can_default_para_init(&can_base_struct);
|
||||
can_base_struct.mode_selection = CAN_MODE_COMMUNICATE;
|
||||
can_base_struct.ttc_enable = FALSE;
|
||||
can_base_struct.aebo_enable = TRUE;
|
||||
can_base_struct.aed_enable = TRUE;
|
||||
can_base_struct.prsf_enable = FALSE;
|
||||
can_base_struct.mdrsel_selection = CAN_DISCARDING_FIRST_RECEIVED;
|
||||
can_base_struct.mmssr_selection = CAN_SENDING_BY_ID;
|
||||
|
||||
can_base_init(CAN1, &can_base_struct);
|
||||
|
||||
/*can_baudrate_setting-------------------------------------------------------------*/
|
||||
/*set baudrate = pclk/(baudrate_div *(1 + bts1_size + bts2_size))------------------*/
|
||||
can_baudrate_struct.baudrate_div = 25; /*value: 1~0xFFF*/
|
||||
can_baudrate_struct.rsaw_size = CAN_RSAW_1TQ; /*value: 1~4*/
|
||||
can_baudrate_struct.bts1_size = CAN_BTS1_6TQ; /*value: 1~16*/
|
||||
can_baudrate_struct.bts2_size = CAN_BTS2_1TQ; /*value: 1~8*/
|
||||
can_baudrate_set(CAN1, &can_baudrate_struct);
|
||||
|
||||
/*can_filter_0_config--------------------------------------------------------------*/
|
||||
can_filter_init_struct.filter_activate_enable = TRUE;
|
||||
can_filter_init_struct.filter_number = 0;
|
||||
can_filter_init_struct.filter_fifo = CAN_FILTER_FIFO0;
|
||||
can_filter_init_struct.filter_bit = CAN_FILTER_16BIT;
|
||||
can_filter_init_struct.filter_mode = CAN_FILTER_MODE_ID_MASK;
|
||||
/*Standard identifier + Mask Mode + Data/Remote frame: id/mask 11bit --------------*/
|
||||
can_filter_init_struct.filter_id_high = 0x0 << 5;
|
||||
can_filter_init_struct.filter_id_low = 0x0 << 5;
|
||||
can_filter_init_struct.filter_mask_high = 0x0 << 5;
|
||||
can_filter_init_struct.filter_mask_low = 0x0 << 5;
|
||||
|
||||
can_filter_init(CAN1, &can_filter_init_struct);
|
||||
|
||||
/* add user code begin can1_init 2 */
|
||||
|
||||
/* add user code end can1_init 2 */
|
||||
}
|
||||
|
||||
/* add user code begin 1 */
|
||||
|
||||
/* add user code end 1 */
|
||||
23
project/src/by_debug.c
Normal file
23
project/src/by_debug.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "by_debug.h"
|
||||
|
||||
#include "at32f413.h"
|
||||
#include "lwprintf.h"
|
||||
|
||||
int lwprintf_out(int ch, lwprintf_t *lwp)
|
||||
{
|
||||
|
||||
/* May use printf to output it for test */
|
||||
|
||||
if (ch != '\0') {
|
||||
while (usart_flag_get(BY_DEBUG_USART_INDEX, USART_TDC_FLAG) == RESET)
|
||||
;
|
||||
usart_data_transmit(BY_DEBUG_USART_INDEX, (char)ch);
|
||||
}
|
||||
|
||||
return ch;
|
||||
}
|
||||
|
||||
void by_debug_init(void)
|
||||
{
|
||||
lwprintf_init(lwprintf_out);
|
||||
}
|
||||
632
project/src/eeprom.c
Normal file
632
project/src/eeprom.c
Normal file
@@ -0,0 +1,632 @@
|
||||
/**
|
||||
**************************************************************************
|
||||
* @file eeprom.c
|
||||
* @version v2.0.0
|
||||
* @date 2020-11-02
|
||||
* @brief the driver library of the flash eeprom
|
||||
**************************************************************************
|
||||
* Copyright notice & Disclaimer
|
||||
*
|
||||
* The software Board Support Package (BSP) that is made available to
|
||||
* download from Artery official website is the copyrighted work of Artery.
|
||||
* Artery authorizes customers to use, copy, and distribute the BSP
|
||||
* software and its related documentation for the purpose of design and
|
||||
* development in conjunction with Artery microcontrollers. Use of the
|
||||
* software is governed by this copyright notice and the following disclaimer.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
|
||||
* GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
|
||||
* TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
|
||||
* STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
|
||||
* INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
|
||||
*
|
||||
**************************************************************************
|
||||
*/
|
||||
|
||||
#include "eeprom.h"
|
||||
|
||||
#define EE_VALID_PAGE0 ((uint16_t)0x0000) /*!< the effective page is page 0 */
|
||||
#define EE_VALID_PAGE1 ((uint16_t)0x0001) /*!< the effective page is page 1 */
|
||||
#define EE_VALID_PAGE_NONE ((uint16_t)0x0002) /*!< no valid page found */
|
||||
|
||||
#define EE_PAGE_ERASED ((uint16_t)0xFFFF) /*!< page is in erased state */
|
||||
#define EE_PAGE_TRANSFER ((uint16_t)0xCCCC) /*!< page is in transfer state */
|
||||
#define EE_PAGE_VALID ((uint16_t)0x0000) /*!< page is in valid state */
|
||||
|
||||
/**
|
||||
* @brief flash eeprom valid page get mode
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
EE_VALID_PAGE_READ = 0x01, /*!< get valid page in read mode */
|
||||
EE_VALID_PAGE_WRITE = 0x02, /*!< get valid page in write mode */
|
||||
} ee_valid_page_type;
|
||||
|
||||
/**
|
||||
* @brief erase eeprom page, one page can contain one or more sectors.
|
||||
* @param page_address:
|
||||
* this parameter can be one of the following values:
|
||||
* - EE_PAGE0_ADDRESS: erase page 0
|
||||
* - EE_PAGE1_ADDRESS: erase page 1
|
||||
* @retval flash_status
|
||||
*/
|
||||
flash_status_type flash_ee_page_erase(uint32_t page_address)
|
||||
{
|
||||
uint16_t i;
|
||||
uint32_t erase_address;
|
||||
flash_status_type flash_status;
|
||||
|
||||
/* erase one or more sectors */
|
||||
for(i = 0; i < EE_SECTOR_NUM; i++)
|
||||
{
|
||||
/* calculate the erase address */
|
||||
erase_address = page_address + i * EE_SECTOR_SIZE;
|
||||
|
||||
/* erase sector */
|
||||
if ((flash_status = flash_sector_erase(erase_address)) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
return flash_status;
|
||||
}
|
||||
}
|
||||
|
||||
return FLASH_OPERATE_DONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief get the valid eeprom page.
|
||||
* @param mode
|
||||
* - EE_VALID_PAGE_READ: get valid page in read mode
|
||||
* - EE_VALID_PAGE_WRITE: get valid page in write mode
|
||||
* @retval valid page
|
||||
* - EE_VALID_PAGE0: page 0 is valid
|
||||
* - EE_VALID_PAGE1: page 1 is valid
|
||||
* - EE_VALID_PAGE_NONE: no valid page
|
||||
*/
|
||||
uint16_t flash_ee_valid_page_get(ee_valid_page_type mode)
|
||||
{
|
||||
uint16_t page0_status;
|
||||
uint16_t page1_status;
|
||||
|
||||
/* get page 0 status */
|
||||
page0_status = (*(__IO uint16_t*)EE_PAGE0_ADDRESS);
|
||||
|
||||
/* get page 1 status */
|
||||
page1_status = (*(__IO uint16_t*)EE_PAGE1_ADDRESS);
|
||||
|
||||
if (mode == EE_VALID_PAGE_READ)
|
||||
{
|
||||
if (page0_status == EE_PAGE_VALID)
|
||||
{
|
||||
/* page 0 is valid */
|
||||
return EE_VALID_PAGE0;
|
||||
}
|
||||
else if (page1_status == EE_PAGE_VALID)
|
||||
{
|
||||
/* page 1 is valid */
|
||||
return EE_VALID_PAGE1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (page0_status == EE_PAGE_TRANSFER)
|
||||
{
|
||||
/* page 0 is valid */
|
||||
return EE_VALID_PAGE0;
|
||||
}
|
||||
else if (page1_status == EE_PAGE_TRANSFER)
|
||||
{
|
||||
/* page 1 is valid */
|
||||
return EE_VALID_PAGE1;
|
||||
}
|
||||
else if (page0_status == EE_PAGE_VALID)
|
||||
{
|
||||
/* page 0 is valid */
|
||||
return EE_VALID_PAGE0;
|
||||
}
|
||||
else if (page1_status == EE_PAGE_VALID)
|
||||
{
|
||||
/* page 1 is valid */
|
||||
return EE_VALID_PAGE1;
|
||||
}
|
||||
}
|
||||
|
||||
/* no valid page found */
|
||||
return EE_VALID_PAGE_NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief write data to the eeprom.
|
||||
* @param address: variable address.
|
||||
* @param data: data.
|
||||
* @retval flash_status.
|
||||
*/
|
||||
flash_status_type flash_ee_write_no_check(uint16_t address, uint16_t data)
|
||||
{
|
||||
uint16_t valid_page;
|
||||
uint32_t find_address;
|
||||
uint32_t end_address;
|
||||
flash_status_type flash_status = FLASH_OPERATE_DONE;
|
||||
|
||||
/* get the valid page */
|
||||
valid_page = flash_ee_valid_page_get(EE_VALID_PAGE_WRITE);
|
||||
|
||||
if (valid_page == EE_VALID_PAGE_NONE)
|
||||
{
|
||||
return FLASH_PROGRAM_ERROR;
|
||||
}
|
||||
|
||||
/* find address calculation */
|
||||
find_address = EE_BASE_ADDRESS + valid_page * EE_PAGE_SIZE;
|
||||
|
||||
/* end address calculation */
|
||||
end_address = EE_BASE_ADDRESS + valid_page * EE_PAGE_SIZE + EE_PAGE_SIZE - 2;
|
||||
|
||||
while (find_address < end_address)
|
||||
{
|
||||
/* find addresses without data */
|
||||
if ((*(__IO uint32_t*)find_address) == 0xFFFFFFFF)
|
||||
{
|
||||
/* write data to flash */
|
||||
if ((flash_status = flash_halfword_program(find_address, data)) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
return flash_status;
|
||||
}
|
||||
|
||||
/* write variable address to flash */
|
||||
if ((flash_status = flash_halfword_program(find_address + 2, address)) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
return flash_status;
|
||||
}
|
||||
|
||||
return FLASH_OPERATE_DONE;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* find address + 4 */
|
||||
find_address += 4;
|
||||
}
|
||||
}
|
||||
|
||||
return FLASH_PROGRAM_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief transfer full page data to empty page.
|
||||
* @param none
|
||||
* @retval flash_status
|
||||
*/
|
||||
flash_status_type flash_ee_copy_to_new_page(void)
|
||||
{
|
||||
uint16_t data;
|
||||
uint16_t idx, valid_page;
|
||||
uint32_t full_page_address;
|
||||
uint32_t empty_page_address;
|
||||
flash_status_type flash_status = FLASH_OPERATE_DONE;
|
||||
|
||||
/* get valid page */
|
||||
valid_page = flash_ee_valid_page_get(EE_VALID_PAGE_READ);
|
||||
|
||||
if (valid_page == EE_VALID_PAGE0)
|
||||
{
|
||||
/* empty page is page 1 */
|
||||
empty_page_address = EE_PAGE1_ADDRESS;
|
||||
|
||||
/* full page is pgae 0 */
|
||||
full_page_address = EE_PAGE0_ADDRESS;
|
||||
}
|
||||
else if (valid_page == EE_VALID_PAGE1)
|
||||
{
|
||||
/* empty page is page 0 */
|
||||
empty_page_address = EE_PAGE0_ADDRESS;
|
||||
|
||||
/* full page is pgae 1 */
|
||||
full_page_address = EE_PAGE1_ADDRESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* page status flag error */
|
||||
return FLASH_PROGRAM_ERROR;
|
||||
}
|
||||
|
||||
/* change the status of the empty page to TRANSFER */
|
||||
if ((flash_status = flash_halfword_program(empty_page_address, EE_PAGE_TRANSFER)) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
return flash_status;
|
||||
}
|
||||
|
||||
for (idx = 0; idx < EE_PARA_MAX_NUMBER; idx++)
|
||||
{
|
||||
/* find valid variables */
|
||||
if (flash_ee_data_read(idx, &data) == 0)
|
||||
{
|
||||
/* store variable to new page */
|
||||
if ((flash_status = flash_ee_write_no_check(idx, data)) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
return flash_status;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* erase old page */
|
||||
if ((flash_status = flash_ee_page_erase(full_page_address)) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
return flash_status;
|
||||
}
|
||||
|
||||
/* change the status of the empty page to VALID */
|
||||
if ((flash_status = flash_halfword_program(empty_page_address, EE_PAGE_VALID)) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
return flash_status;
|
||||
}
|
||||
|
||||
return FLASH_OPERATE_DONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief erase all pages to format eeprom.
|
||||
* @param none
|
||||
* @retval flash_status
|
||||
*/
|
||||
flash_status_type flash_ee_format(void)
|
||||
{
|
||||
flash_status_type flash_status = FLASH_OPERATE_DONE;
|
||||
|
||||
/* erase page 0 */
|
||||
if ((flash_status = flash_ee_page_erase(EE_PAGE0_ADDRESS)) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
return flash_status;
|
||||
}
|
||||
|
||||
/* erase page 1 */
|
||||
if ((flash_status = flash_ee_page_erase(EE_PAGE1_ADDRESS)) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
return flash_status;
|
||||
}
|
||||
|
||||
/* mark the status of page 0 as VALID */
|
||||
return flash_halfword_program(EE_PAGE0_ADDRESS, EE_PAGE_VALID);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief eeprom format validity check.
|
||||
* @param page0_status: page0 status
|
||||
* @param page1_status: page1 status
|
||||
* @retval format status:
|
||||
* - 0: the format is correct
|
||||
* - 1: the format is incorrect
|
||||
*/
|
||||
uint16_t flash_ee_format_check(uint16_t page0_status, uint16_t page1_status)
|
||||
{
|
||||
/* the euqal status is invalid */
|
||||
if (page0_status == page1_status)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* status is not known status is invalid status */
|
||||
if ((page0_status != EE_PAGE_ERASED) && (page0_status != EE_PAGE_TRANSFER) && (page0_status != EE_PAGE_VALID))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* status is not known status is invalid status */
|
||||
if ((page1_status != EE_PAGE_ERASED) && (page1_status != EE_PAGE_TRANSFER) && (page1_status != EE_PAGE_VALID))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* the format is correct */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief check if eeprom page is full, when the page is full, transfer the data to erase page.
|
||||
* @param none
|
||||
* @retval flash_status
|
||||
*/
|
||||
flash_status_type flash_ee_full_check(void)
|
||||
{
|
||||
uint16_t valid_page;
|
||||
uint32_t end_address;
|
||||
flash_status_type flash_status = FLASH_OPERATE_DONE;
|
||||
|
||||
/* get the valid page */
|
||||
valid_page = flash_ee_valid_page_get(EE_VALID_PAGE_READ);
|
||||
|
||||
if (valid_page == EE_VALID_PAGE_NONE)
|
||||
{
|
||||
return FLASH_PROGRAM_ERROR;
|
||||
}
|
||||
|
||||
/* end address calculation */
|
||||
end_address = EE_BASE_ADDRESS + valid_page * EE_PAGE_SIZE + EE_PAGE_SIZE - 2;
|
||||
|
||||
/* check if the page is full */
|
||||
if ((*(__IO uint32_t*)(end_address - 2)) != 0xFFFFFFFF)
|
||||
{
|
||||
/* when the page is full, transfer the data to erase page */
|
||||
if ((flash_status = flash_ee_copy_to_new_page()) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
return flash_status;
|
||||
}
|
||||
}
|
||||
|
||||
/* page full check ok */
|
||||
return FLASH_OPERATE_DONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief transition state processing, a page state is ERASE, a page state is TRANSFER,
|
||||
* and the TRANSFER state is changed to VALID.
|
||||
* @param page0_status: page0 status
|
||||
* @param page1_status: page1 status
|
||||
* @retval format status:
|
||||
* - 0: the format is correct
|
||||
* - 1: the format is incorrect
|
||||
*/
|
||||
flash_status_type flash_ee_erase_transfer(uint16_t page0_status, uint16_t page1_status)
|
||||
{
|
||||
if (page0_status == EE_PAGE_TRANSFER)
|
||||
{
|
||||
/* mark the status of page 0 as VALID */
|
||||
return flash_halfword_program(EE_PAGE0_ADDRESS, EE_PAGE_VALID);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* mark the status of page 1 as VALID */
|
||||
return flash_halfword_program(EE_PAGE1_ADDRESS, EE_PAGE_VALID);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief transition state processing, one page state is VALID, one page state is TRANSFER,
|
||||
* and the data is transferred to the TRANSFER state page.
|
||||
* @param page0_status: page0 status
|
||||
* @param page1_status: page1 status
|
||||
* @retval format status:
|
||||
* - 0: the format is correct
|
||||
* - 1: the format is incorrect
|
||||
*/
|
||||
flash_status_type flash_ee_valid_transfer(uint16_t page0_status, uint16_t page1_status)
|
||||
{
|
||||
uint32_t erase_page_address;
|
||||
flash_status_type flash_status;
|
||||
|
||||
/* find the page in the transfer state, erase the page, and retransmit the data */
|
||||
if (page0_status == EE_PAGE_TRANSFER)
|
||||
{
|
||||
erase_page_address = EE_PAGE0_ADDRESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
erase_page_address = EE_PAGE1_ADDRESS;
|
||||
}
|
||||
|
||||
/* erase the transfer state page */
|
||||
if ((flash_status = flash_ee_page_erase(erase_page_address)) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
return flash_status;
|
||||
}
|
||||
|
||||
/* retransmit data */
|
||||
return flash_ee_copy_to_new_page();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief eeprom init.
|
||||
+-------------------+---------------------------------------------------------------------------------------------+
|
||||
| | PAGE 1 status |
|
||||
| +-----------------------+----------------------------------+----------------------------------+
|
||||
| | ERASE | TRANSFER | VALID |
|
||||
+--------+----------+-----------------------+----------------------------------+----------------------------------+
|
||||
| | | erase page 0 | erase page 0 | |
|
||||
| | ERASE | erase page 1 | mark page 1 VALID | erase page 0 |
|
||||
| | | mark page 0 VALID | | |
|
||||
| +----------+-----------------------+----------------------------------+----------------------------------+
|
||||
| | | erase page 1 | erase page 0 | erase page 0 |
|
||||
| PAGE 0 | TRANSFER | mark page 0 VALID | erase page 1 | copy data from page 1 to page 0 |
|
||||
| status | | | mark page 0 VALID | erase page 1 |
|
||||
| | | | | mark page 0 VALID |
|
||||
| +----------+-----------------------+----------------------------------+----------------------------------+
|
||||
| | | | erase page 1 | erase page 0 |
|
||||
| | VALID | erase page 1 | copy data from page 0 to page 1 | erase page 1 |
|
||||
| | | | erase page 0 | mark page 0 VALID |
|
||||
| | | | mark page 1 VALID | |
|
||||
+--------+----------+-----------------------+----------------------------------+----------------------------------+
|
||||
* @param none
|
||||
* @retval flash status.
|
||||
*/
|
||||
flash_status_type flash_ee_init(void)
|
||||
{
|
||||
uint16_t page0_status;
|
||||
uint16_t page1_status;
|
||||
flash_status_type flash_status = FLASH_OPERATE_DONE;
|
||||
|
||||
/* flash unlock */
|
||||
flash_unlock();
|
||||
|
||||
/* get page 0 status */
|
||||
page0_status = (*(__IO uint16_t*)EE_PAGE0_ADDRESS);
|
||||
|
||||
/* get page 2 status */
|
||||
page1_status = (*(__IO uint16_t*)EE_PAGE1_ADDRESS);
|
||||
|
||||
/* ensure that the sector data is completely erased */
|
||||
if (page0_status == EE_PAGE_ERASED)
|
||||
{
|
||||
/* erase page 0 */
|
||||
if ((flash_status = flash_ee_page_erase(EE_PAGE0_ADDRESS)) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
/* flash lock */
|
||||
flash_lock();
|
||||
|
||||
return flash_status;
|
||||
}
|
||||
}
|
||||
|
||||
/* ensure that the sector data is completely erased */
|
||||
if (page1_status == EE_PAGE_ERASED)
|
||||
{
|
||||
/* erase page 1 */
|
||||
if ((flash_status = flash_ee_page_erase(EE_PAGE1_ADDRESS)) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
/* flash lock */
|
||||
flash_lock();
|
||||
|
||||
return flash_status;
|
||||
}
|
||||
}
|
||||
|
||||
/* format validity check */
|
||||
if (flash_ee_format_check(page0_status, page1_status) != 0)
|
||||
{
|
||||
/* if the format is invalid, reformat */
|
||||
if ((flash_status = flash_ee_format()) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
/* flash lock */
|
||||
flash_lock();
|
||||
|
||||
return flash_status;
|
||||
}
|
||||
}
|
||||
|
||||
/* transition state processing, a page state is ERASE, a page state is TRANSFER, and the TRANSFER state is changed to VALID */
|
||||
if (((page0_status == EE_PAGE_ERASED) && (page1_status == EE_PAGE_TRANSFER)) ||
|
||||
((page0_status == EE_PAGE_TRANSFER) && (page1_status == EE_PAGE_ERASED)))
|
||||
{
|
||||
if ((flash_status = flash_ee_erase_transfer(page0_status, page1_status)) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
/* flash lock */
|
||||
flash_lock();
|
||||
|
||||
return flash_status;
|
||||
}
|
||||
}
|
||||
|
||||
/* transition state processing, one page state is VALID, one page state is TRANSFER, and the data is transferred to the TRANSFER state page */
|
||||
if (((page0_status == EE_PAGE_VALID) && (page1_status == EE_PAGE_TRANSFER)) ||
|
||||
((page0_status == EE_PAGE_TRANSFER) && (page1_status == EE_PAGE_VALID)))
|
||||
{
|
||||
if ((flash_status = flash_ee_valid_transfer(page0_status, page1_status)) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
/* flash lock */
|
||||
flash_lock();
|
||||
|
||||
return flash_status;
|
||||
}
|
||||
}
|
||||
|
||||
/* check if the page is full, when the page is full, transfer the data to erase page */
|
||||
if ((flash_status = flash_ee_full_check()) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
/* flash lock */
|
||||
flash_lock();
|
||||
|
||||
return flash_status;
|
||||
}
|
||||
|
||||
/* flash lock */
|
||||
flash_lock();
|
||||
|
||||
return FLASH_OPERATE_DONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief write data to the eeprom.
|
||||
* @param address: variable address.
|
||||
* @param data: data.
|
||||
* @retval flash_status.
|
||||
*/
|
||||
flash_status_type flash_ee_data_write(uint16_t address, uint16_t data)
|
||||
{
|
||||
flash_status_type flash_status = FLASH_OPERATE_DONE;
|
||||
|
||||
/* flash unlock */
|
||||
flash_unlock();
|
||||
|
||||
/* check if the page is full, when the page is full, transfer the data to erase page */
|
||||
if ((flash_status = flash_ee_full_check()) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
/* flash lock */
|
||||
flash_lock();
|
||||
|
||||
return flash_status;
|
||||
}
|
||||
|
||||
/* write data to flash */
|
||||
if ((flash_status = flash_ee_write_no_check(address, data)) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
/* flash lock */
|
||||
flash_lock();
|
||||
|
||||
return flash_status;
|
||||
}
|
||||
|
||||
/* check if the page is full, when the page is full, transfer the data to erase page */
|
||||
if ((flash_status = flash_ee_full_check()) != FLASH_OPERATE_DONE)
|
||||
{
|
||||
/* flash lock */
|
||||
flash_lock();
|
||||
|
||||
return flash_status;
|
||||
}
|
||||
|
||||
/* flash lock */
|
||||
flash_lock();
|
||||
|
||||
return FLASH_OPERATE_DONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief read data from the eeprom.
|
||||
* @param address: variable address.
|
||||
* @param pdata: data pointer.
|
||||
* @retval read status:
|
||||
* - 0: data successfully read
|
||||
* - 1: failed to read data
|
||||
*/
|
||||
uint16_t flash_ee_data_read(uint16_t address, uint16_t* pdata)
|
||||
{
|
||||
uint16_t valid_page;
|
||||
uint16_t data_address;
|
||||
uint32_t find_address;
|
||||
uint32_t start_address;
|
||||
|
||||
/* get the valid page */
|
||||
valid_page = flash_ee_valid_page_get(EE_VALID_PAGE_READ);
|
||||
|
||||
if (valid_page == EE_VALID_PAGE_NONE)
|
||||
{
|
||||
return EE_VALID_PAGE_NONE;
|
||||
}
|
||||
|
||||
/* start address calculation */
|
||||
start_address = EE_BASE_ADDRESS + valid_page * EE_PAGE_SIZE + 4;
|
||||
|
||||
/* find address calculation */
|
||||
find_address = EE_BASE_ADDRESS + valid_page * EE_PAGE_SIZE + EE_PAGE_SIZE - 2;
|
||||
|
||||
while (find_address > start_address)
|
||||
{
|
||||
/* read variable address */
|
||||
data_address = (*(__IO uint16_t*)find_address);
|
||||
|
||||
/* variable address matching */
|
||||
if (address == data_address)
|
||||
{
|
||||
/* read data */
|
||||
*pdata = (*(__IO uint16_t*)(find_address - 2));
|
||||
|
||||
/* data successfully read */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* find address - 4 */
|
||||
find_address -= 4;
|
||||
}
|
||||
|
||||
/* failed to read data */
|
||||
return 1;
|
||||
}
|
||||
|
||||
121
project/src/main.c
Normal file
121
project/src/main.c
Normal file
@@ -0,0 +1,121 @@
|
||||
/* add user code begin Header */
|
||||
/**
|
||||
**************************************************************************
|
||||
* @file main.c
|
||||
* @brief main program
|
||||
**************************************************************************
|
||||
* Copyright notice & Disclaimer
|
||||
*
|
||||
* The software Board Support Package (BSP) that is made available to
|
||||
* download from Artery official website is the copyrighted work of Artery.
|
||||
* Artery authorizes customers to use, copy, and distribute the BSP
|
||||
* software and its related documentation for the purpose of design and
|
||||
* development in conjunction with Artery microcontrollers. Use of the
|
||||
* software is governed by this copyright notice and the following disclaimer.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
|
||||
* GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
|
||||
* TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
|
||||
* STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
|
||||
* INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
|
||||
*
|
||||
**************************************************************************
|
||||
*/
|
||||
/* add user code end Header */
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "at32f413_wk_config.h"
|
||||
|
||||
/* private includes ----------------------------------------------------------*/
|
||||
/* add user code begin private includes */
|
||||
#include "by_debug.h"
|
||||
/* add user code end private includes */
|
||||
|
||||
/* private typedef -----------------------------------------------------------*/
|
||||
/* add user code begin private typedef */
|
||||
|
||||
/* add user code end private typedef */
|
||||
|
||||
/* private define ------------------------------------------------------------*/
|
||||
/* add user code begin private define */
|
||||
|
||||
/* add user code end private define */
|
||||
|
||||
/* private macro -------------------------------------------------------------*/
|
||||
/* add user code begin private macro */
|
||||
|
||||
/* add user code end private macro */
|
||||
|
||||
/* private variables ---------------------------------------------------------*/
|
||||
/* add user code begin private variables */
|
||||
|
||||
/* add user code end private variables */
|
||||
|
||||
/* private function prototypes --------------------------------------------*/
|
||||
/* add user code begin function prototypes */
|
||||
|
||||
/* add user code end function prototypes */
|
||||
|
||||
/* private user code ---------------------------------------------------------*/
|
||||
/* add user code begin 0 */
|
||||
|
||||
/* add user code end 0 */
|
||||
|
||||
/**
|
||||
* @brief main function.
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
/* add user code begin 1 */
|
||||
by_debug_init();
|
||||
/* add user code end 1 */
|
||||
|
||||
/* system clock config. */
|
||||
wk_system_clock_config();
|
||||
|
||||
/* config periph clock. */
|
||||
wk_periph_clock_config();
|
||||
|
||||
/* init debug function. */
|
||||
wk_debug_config();
|
||||
|
||||
/* nvic config. */
|
||||
wk_nvic_config();
|
||||
|
||||
/* init usart2 function. */
|
||||
wk_usart2_init();
|
||||
|
||||
/* init exint function. */
|
||||
wk_exint_config();
|
||||
|
||||
/* init tmr1 function. */
|
||||
wk_tmr1_init();
|
||||
|
||||
/* init tmr3 function. */
|
||||
wk_tmr3_init();
|
||||
|
||||
/* init tmr9 function. */
|
||||
wk_tmr9_init();
|
||||
|
||||
/* init tmr10 function. */
|
||||
wk_tmr10_init();
|
||||
|
||||
/* init can1 function. */
|
||||
wk_can1_init();
|
||||
|
||||
/* init gpio function. */
|
||||
wk_gpio_config();
|
||||
|
||||
/* add user code begin 2 */
|
||||
LOGD("hello world");
|
||||
/* add user code end 2 */
|
||||
|
||||
while (1) {
|
||||
/* add user code begin 3 */
|
||||
|
||||
/* add user code end 3 */
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user