mirror of
https://github.com/ArteryTek/AT32F413_Firmware_Library.git
synced 2026-05-21 09:22:02 +00:00
upload version v2.0.0
This commit is contained in:
292
project/at32f413_board/at32f413_board.c
Normal file
292
project/at32f413_board/at32f413_board.c
Normal file
@@ -0,0 +1,292 @@
|
||||
/**
|
||||
**************************************************************************
|
||||
* @file at32f413_board.c
|
||||
* @version v2.0.0
|
||||
* @date 2021-11-26
|
||||
* @brief set of firmware functions to manage leds and push-button.
|
||||
* initialize delay function.
|
||||
**************************************************************************
|
||||
* 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 "at32f413_board.h"
|
||||
|
||||
/** @addtogroup AT32F413_board
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup BOARD
|
||||
* @brief onboard periph driver
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* delay macros */
|
||||
#define STEP_DELAY_MS 50
|
||||
|
||||
/* at-start led resouce array */
|
||||
gpio_type *led_gpio_port[LED_NUM] = {LED2_GPIO, LED3_GPIO, LED4_GPIO};
|
||||
uint16_t led_gpio_pin[LED_NUM] = {LED2_PIN, LED3_PIN, LED4_PIN};
|
||||
crm_periph_clock_type led_gpio_crm_clk[LED_NUM] = {LED2_GPIO_CRM_CLK, LED3_GPIO_CRM_CLK, LED4_GPIO_CRM_CLK};
|
||||
|
||||
/* delay variable */
|
||||
static __IO uint32_t fac_us;
|
||||
static __IO uint32_t fac_ms;
|
||||
|
||||
/**
|
||||
* @brief board initialize interface init led and button
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void at32_board_init()
|
||||
{
|
||||
/* initialize delay function */
|
||||
delay_init();
|
||||
|
||||
/* configure led in at_start_board */
|
||||
at32_led_init(LED2);
|
||||
at32_led_init(LED3);
|
||||
at32_led_init(LED4);
|
||||
at32_led_off(LED2);
|
||||
at32_led_off(LED3);
|
||||
at32_led_off(LED4);
|
||||
|
||||
/* configure button in at_start board */
|
||||
at32_button_init();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief configure button gpio
|
||||
* @param button: specifies the button to be configured.
|
||||
* @retval none
|
||||
*/
|
||||
void at32_button_init(void)
|
||||
{
|
||||
gpio_init_type gpio_init_struct;
|
||||
|
||||
/* enable the button clock */
|
||||
crm_periph_clock_enable(USER_BUTTON_CRM_CLK, TRUE);
|
||||
|
||||
/* set default parameter */
|
||||
gpio_default_para_init(&gpio_init_struct);
|
||||
|
||||
/* configure button pin as input with pull-up/pull-down */
|
||||
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 = USER_BUTTON_PIN;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_DOWN;
|
||||
gpio_init(USER_BUTTON_PORT, &gpio_init_struct);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief returns the selected button state
|
||||
* @param none
|
||||
* @retval the button gpio pin value
|
||||
*/
|
||||
uint8_t at32_button_state(void)
|
||||
{
|
||||
return gpio_input_data_bit_read(USER_BUTTON_PORT, USER_BUTTON_PIN);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief returns which button have press down
|
||||
* @param none
|
||||
* @retval the button have press down
|
||||
*/
|
||||
button_type at32_button_press()
|
||||
{
|
||||
static uint8_t pressed = 1;
|
||||
/* get button state in at_start board */
|
||||
if((pressed == 1) && (at32_button_state() != RESET))
|
||||
{
|
||||
/* debounce */
|
||||
pressed = 0;
|
||||
delay_ms(10);
|
||||
if(at32_button_state() != RESET)
|
||||
return USER_BUTTON;
|
||||
}
|
||||
else if(at32_button_state() == RESET)
|
||||
{
|
||||
pressed = 1;
|
||||
}
|
||||
return NO_BUTTON;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief configure led gpio
|
||||
* @param led: specifies the led to be configured.
|
||||
* @retval none
|
||||
*/
|
||||
void at32_led_init(led_type led)
|
||||
{
|
||||
gpio_init_type gpio_init_struct;
|
||||
|
||||
/* enable the led clock */
|
||||
crm_periph_clock_enable(led_gpio_crm_clk[led], TRUE);
|
||||
|
||||
/* set default parameter */
|
||||
gpio_default_para_init(&gpio_init_struct);
|
||||
|
||||
/* configure the led gpio */
|
||||
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_OUTPUT;
|
||||
gpio_init_struct.gpio_pins = led_gpio_pin[led];
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init(led_gpio_port[led], &gpio_init_struct);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief turns selected led on.
|
||||
* @param led: specifies the led to be set on.
|
||||
* this parameter can be one of following parameters:
|
||||
* @arg LED2
|
||||
* @arg LED3
|
||||
* @arg LED4
|
||||
* @retval none
|
||||
*/
|
||||
void at32_led_on(led_type led)
|
||||
{
|
||||
if(led > (LED_NUM - 1))
|
||||
return;
|
||||
if(led_gpio_pin[led])
|
||||
led_gpio_port[led]->clr = led_gpio_pin[led];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief turns selected led off.
|
||||
* @param led: specifies the led to be set off.
|
||||
* this parameter can be one of following parameters:
|
||||
* @arg LED2
|
||||
* @arg LED3
|
||||
* @arg LED4
|
||||
* @retval none
|
||||
*/
|
||||
void at32_led_off(led_type led)
|
||||
{
|
||||
if(led > (LED_NUM - 1))
|
||||
return;
|
||||
if(led_gpio_pin[led])
|
||||
led_gpio_port[led]->scr = led_gpio_pin[led];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief turns selected led tooggle.
|
||||
* @param led: specifies the led to be set off.
|
||||
* this parameter can be one of following parameters:
|
||||
* @arg LED2
|
||||
* @arg LED3
|
||||
* @arg LED4
|
||||
* @retval none
|
||||
*/
|
||||
void at32_led_toggle(led_type led)
|
||||
{
|
||||
if(led > (LED_NUM - 1))
|
||||
return;
|
||||
if(led_gpio_pin[led])
|
||||
led_gpio_port[led]->odt ^= led_gpio_pin[led];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief initialize delay function
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void delay_init()
|
||||
{
|
||||
/* configure systick */
|
||||
systick_clock_source_config(SYSTICK_CLOCK_SOURCE_AHBCLK_NODIV);
|
||||
fac_us = system_core_clock / (1000000U);
|
||||
fac_ms = fac_us * (1000U);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief inserts a delay time.
|
||||
* @param nus: specifies the delay time length, in microsecond.
|
||||
* @retval none
|
||||
*/
|
||||
void delay_us(uint32_t nus)
|
||||
{
|
||||
uint32_t temp = 0;
|
||||
SysTick->LOAD = (uint32_t)(nus * fac_us);
|
||||
SysTick->VAL = 0x00;
|
||||
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk ;
|
||||
do
|
||||
{
|
||||
temp = SysTick->CTRL;
|
||||
}while((temp & 0x01) && !(temp & (1 << 16)));
|
||||
|
||||
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
|
||||
SysTick->VAL = 0x00;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief inserts a delay time.
|
||||
* @param nms: specifies the delay time length, in milliseconds.
|
||||
* @retval none
|
||||
*/
|
||||
void delay_ms(uint16_t nms)
|
||||
{
|
||||
uint32_t temp = 0;
|
||||
while(nms)
|
||||
{
|
||||
if(nms > STEP_DELAY_MS)
|
||||
{
|
||||
SysTick->LOAD = (uint32_t)(STEP_DELAY_MS * fac_ms);
|
||||
nms -= STEP_DELAY_MS;
|
||||
}
|
||||
else
|
||||
{
|
||||
SysTick->LOAD = (uint32_t)(nms * fac_ms);
|
||||
nms = 0;
|
||||
}
|
||||
SysTick->VAL = 0x00;
|
||||
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
|
||||
do
|
||||
{
|
||||
temp = SysTick->CTRL;
|
||||
}while((temp & 0x01) && !(temp & (1 << 16)));
|
||||
|
||||
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
|
||||
SysTick->VAL = 0x00;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief inserts a delay time.
|
||||
* @param sec: specifies the delay time, in seconds.
|
||||
* @retval none
|
||||
*/
|
||||
void delay_sec(uint16_t sec)
|
||||
{
|
||||
uint16_t index;
|
||||
for(index = 0; index < sec; index++)
|
||||
{
|
||||
delay_ms(500);
|
||||
delay_ms(500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
139
project/at32f413_board/at32f413_board.h
Normal file
139
project/at32f413_board/at32f413_board.h
Normal file
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
**************************************************************************
|
||||
* @file at32f413_board.h
|
||||
* @version v2.0.0
|
||||
* @date 2021-11-26
|
||||
* @brief header file for at-start board. set of firmware functions to
|
||||
* manage leds and push-button. initialize delay function.
|
||||
**************************************************************************
|
||||
* 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.
|
||||
*
|
||||
**************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef __AT32F413_BOARD_H
|
||||
#define __AT32F413_BOARD_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "at32f413.h"
|
||||
|
||||
/** @addtogroup AT32F413_board
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @addtogroup BOARD
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup BOARD_pins_definition
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* this header include define support list:
|
||||
* 1. at-start-f413 v1.x board
|
||||
* if define AT_START_F413_V1, the header file support at-start-f413 v1.x board
|
||||
*/
|
||||
|
||||
#if !defined (AT_START_F413_V1)
|
||||
#error "please select first the board at-start device used in your application (in at32f413_board.h file)"
|
||||
#endif
|
||||
|
||||
/******************** define led ********************/
|
||||
typedef enum
|
||||
{
|
||||
LED2 = 0,
|
||||
LED3 = 1,
|
||||
LED4 = 2
|
||||
} led_type;
|
||||
|
||||
#define LED_NUM 3
|
||||
|
||||
#if defined (AT_START_F413_V1)
|
||||
#define LED2_PIN GPIO_PINS_2
|
||||
#define LED2_GPIO GPIOC
|
||||
#define LED2_GPIO_CRM_CLK CRM_GPIOC_PERIPH_CLOCK
|
||||
|
||||
#define LED3_PIN GPIO_PINS_3
|
||||
#define LED3_GPIO GPIOC
|
||||
#define LED3_GPIO_CRM_CLK CRM_GPIOC_PERIPH_CLOCK
|
||||
|
||||
#define LED4_PIN GPIO_PINS_5
|
||||
#define LED4_GPIO GPIOC
|
||||
#define LED4_GPIO_CRM_CLK CRM_GPIOC_PERIPH_CLOCK
|
||||
#endif
|
||||
|
||||
/******************* define button *******************/
|
||||
typedef enum
|
||||
{
|
||||
USER_BUTTON = 0,
|
||||
NO_BUTTON = 1
|
||||
} button_type;
|
||||
|
||||
#define USER_BUTTON_PIN GPIO_PINS_0
|
||||
#define USER_BUTTON_PORT GPIOA
|
||||
#define USER_BUTTON_CRM_CLK CRM_GPIOA_PERIPH_CLOCK
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup BOARD_exported_functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/******************** functions ********************/
|
||||
void at32_board_init(void);
|
||||
|
||||
/* led operation function */
|
||||
void at32_led_init(led_type led);
|
||||
void at32_led_on(led_type led);
|
||||
void at32_led_off(led_type led);
|
||||
void at32_led_toggle(led_type led);
|
||||
|
||||
/* button operation function */
|
||||
void at32_button_init(void);
|
||||
button_type at32_button_press(void);
|
||||
uint8_t at32_button_state(void);
|
||||
|
||||
/* delay function */
|
||||
void delay_init(void);
|
||||
void delay_us(uint32_t nus);
|
||||
void delay_ms(uint16_t nms);
|
||||
void delay_sec(uint16_t sec);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user