diff --git a/.gitignore b/.gitignore index 3d5e94d..cfc0343 100644 --- a/.gitignore +++ b/.gitignore @@ -71,7 +71,6 @@ mdk_v5/ at32_ide_proj/ *.uvguix* *.cproject -project/ *.project *.template *.wvproj diff --git a/vd960Loop/project/at32f421_board/at32f421_board.c b/vd960Loop/project/at32f421_board/at32f421_board.c new file mode 100644 index 0000000..1f1ce29 --- /dev/null +++ b/vd960Loop/project/at32f421_board/at32f421_board.c @@ -0,0 +1,627 @@ +/** + ************************************************************************** + * @file at32f421_board.c + * @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 "at32f421_board.h" +#include "cmcng.h" + +/** @addtogroup AT32F421_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] = {LED1_GPIO,LED2_GPIO, LED3_GPIO, LED4_GPIO}; +uint16_t led_gpio_pin[LED_NUM] = {LED1_PIN,LED2_PIN, LED3_PIN, LED4_PIN}; +crm_periph_clock_type led_gpio_crm_clk[LED_NUM] = {LED1_GPIO_CRM_CLK,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; + +/* support printf function, usemicrolib is unnecessary */ +#if (__ARMCC_VERSION > 6000000) + __asm (".global __use_no_semihosting\n\t"); + void _sys_exit(int x) + { + x = x; + } + /* __use_no_semihosting was requested, but _ttywrch was */ + void _ttywrch(int ch) + { + ch = ch; + } + FILE __stdout; +#else + #ifdef __CC_ARM + #pragma import(__use_no_semihosting) + struct __FILE + { + int handle; + }; + FILE __stdout; + void _sys_exit(int x) + { + x = x; + } + /* __use_no_semihosting was requested, but _ttywrch was */ + void _ttywrch(int ch) + { + ch = ch; + } + #endif +#endif + +#if defined (__GNUC__) && !defined (__clang__) + #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) +#else + #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) +#endif + +/** + * @brief retargets the c library printf function to the usart. + * @param none + * @retval none + */ +PUTCHAR_PROTOTYPE +{ + while(usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET); + usart_data_transmit(PRINT_UART, (uint16_t)ch); + while(usart_flag_get(PRINT_UART, USART_TDC_FLAG) == RESET); + return ch; +} + +#if (defined (__GNUC__) && !defined (__clang__)) || (defined (__ICCARM__)) +#if defined (__GNUC__) && !defined (__clang__) +int _write(int fd, char *pbuffer, int size) +#elif defined ( __ICCARM__ ) +#pragma module_name = "?__write" +int __write(int fd, char *pbuffer, int size) +#endif +{ + for(int i = 0; i < size; i ++) + { + while(usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET); + usart_data_transmit(PRINT_UART, (uint16_t)(*pbuffer++)); + while(usart_flag_get(PRINT_UART, USART_TDC_FLAG) == RESET); + } + + return size; +} +#endif + +/** + * @brief initialize uart + * @param baudrate: uart baudrate + * @retval none + */ +void uart_print_init(uint32_t baudrate) +{ + gpio_init_type gpio_init_struct; + +#if defined (__GNUC__) && !defined (__clang__) + setvbuf(stdout, NULL, _IONBF, 0); +#endif + + /* enable the uart and gpio clock */ + crm_periph_clock_enable(PRINT_UART_CRM_CLK, TRUE); + crm_periph_clock_enable(PRINT_UART_TX_GPIO_CRM_CLK, TRUE); + + gpio_default_para_init(&gpio_init_struct); + + /* configure the uart tx 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_MUX; + gpio_init_struct.gpio_pins = PRINT_UART_TX_PIN; + gpio_init_struct.gpio_pull = GPIO_PULL_NONE; + gpio_init(PRINT_UART_TX_GPIO, &gpio_init_struct); + + gpio_pin_mux_config(PRINT_UART_TX_GPIO, PRINT_UART_TX_PIN_SOURCE, PRINT_UART_TX_PIN_MUX_NUM); + + /* configure uart param */ + usart_init(PRINT_UART, baudrate, USART_DATA_8BITS, USART_STOP_1_BIT); + usart_transmitter_enable(PRINT_UART, TRUE); + usart_enable(PRINT_UART, TRUE); +} + +/** + * @brief config usart1 + * @param none + * @retval none + */ +void usart_configuration(void) +{ + gpio_init_type gpio_init_struct; + uint32_t baud = 192000; //115200; //192000; // 115200; + + /* enable the usart1 and gpio clock */ + crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE); + crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE); + + gpio_default_para_init(&gpio_init_struct); + + /* configure the usart1 tx/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_MUX; + gpio_init_struct.gpio_pins = GPIO_PINS_9 | GPIO_PINS_10; + gpio_init_struct.gpio_pull = GPIO_PULL_NONE; + gpio_init(GPIOA, &gpio_init_struct); + + /* config usart1 iomux */ + gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE9, GPIO_MUX_1); + gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE10, GPIO_MUX_1); + + /* configure usart1 param */ + nvic_irq_enable(USART1_IRQn, 1, 0); // 优先级 中等:串口通信, 与上位机通信 + usart_init(USART1, baud, USART_DATA_8BITS, USART_STOP_1_BIT); +// usart_parity_selection_config(USART1, USART_PARITY_NONE); + usart_transmitter_enable(USART1, TRUE); + usart_receiver_enable(USART1, TRUE); + usart_interrupt_enable(USART1, USART_RDBF_INT, TRUE); + usart_enable(USART1, TRUE); + +// usart_init(USART1, 115200, USART_DATA_8BITS, USART_STOP_1_BIT); +// usart_parity_selection_config(USART1, USART_PARITY_NONE); +// usart_transmitter_enable(USART1, TRUE); +// usart_receiver_enable(USART1, TRUE); +//// usart_dma_transmitter_enable(USART1, TRUE); +// usart_dma_receiver_enable(USART1, TRUE); +// usart_enable(USART1, TRUE); + +// dma_configuration(); +} + +/** + * @brief config dma for usart2 and usart1 + * @param none + * @retval none + */ +void dma_configuration(void) +{ + dma_init_type dma_init_struct; + + /* enable dma1 clock */ + crm_periph_clock_enable(CRM_DMA1_PERIPH_CLOCK, TRUE); + + + /* dma1 channel3 for usart1 rx configuration */ + dma_reset(DMA1_CHANNEL5); + dma_default_para_init(&dma_init_struct); + dma_init_struct.buffer_size = BUFF_STACK_SIZE; + dma_init_struct.direction = DMA_DIR_PERIPHERAL_TO_MEMORY; + dma_init_struct.memory_base_addr = (uint32_t)usart1_rx_buffer; + dma_init_struct.memory_data_width = DMA_MEMORY_DATA_WIDTH_BYTE; + dma_init_struct.memory_inc_enable = TRUE; + dma_init_struct.peripheral_base_addr = (uint32_t)&USART1->dt; + dma_init_struct.peripheral_data_width = DMA_PERIPHERAL_DATA_WIDTH_BYTE; + dma_init_struct.peripheral_inc_enable = FALSE; + dma_init_struct.priority = DMA_PRIORITY_MEDIUM; + dma_init_struct.loop_mode_enable = FALSE; + dma_init(DMA1_CHANNEL5, &dma_init_struct); + + /* enable transfer full data interrupt */ + dma_interrupt_enable(DMA1_CHANNEL5, DMA_FDT_INT, TRUE); + + /* dma1 channel3 interrupt nvic init */ + nvic_irq_enable(DMA1_Channel5_4_IRQn, 0, 0); + + dma_channel_enable(DMA1_CHANNEL5, TRUE); /* usart1 rx begin dma receiving */ +} + + + +/** + * @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(LED_PWR); + at32_led_init(LED_LP1); + at32_led_init(LED_LP2); + at32_led_init(LED_LP3); + at32_led_init(LED_LP4); + +// at32_led_off(LED_PWR); + at32_led_off(LED_LP1); + at32_led_off(LED_LP2); + at32_led_off(LED_LP3); + at32_led_off(LED_LP4); + + /* configure button in at_start board */ +// at32_button_init(); + freq_level_gpio_init(); + +} + + +void loop_timer_io_init(void) +{ + uint8_t i, _div = TMR_CHANNEL_INPUT_DIV_4; + /* enable tmr3/gpioa clock */ + crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, TRUE); + crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE); + crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE); + + gpio_init_type gpio_init_struct; + + gpio_init_struct.gpio_pins = RLY1_PIN | RLY2_PIN | RLY3_PIN | RLY4_PIN; + + gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT; + 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(GPIOB, &gpio_init_struct); + + + +// LEDA_OFF; +// LEDC_OFF; + RLY1_OFF; + RLY2_OFF; + RLY3_OFF; + RLY4_OFF; + + /* Red LED light on tempporary , will be PWM */ + // gpio_init_struct.gpio_pins = GPIO_PINS_15; + // gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT; + // 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); + // gpio_bits_reset(GPIOA, GPIO_PINS_15); // red on + + + /* timer3 input pin Configuration */ + gpio_init_struct.gpio_pins = GPIO_PINS_6 | GPIO_PINS_7; + 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_STRONGER; + gpio_init(GPIOA, &gpio_init_struct); + + gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE6, GPIO_MUX_1); + gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE7, GPIO_MUX_1); + + gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1; + 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_STRONGER; + gpio_init(GPIOB, &gpio_init_struct); + + gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE0, GPIO_MUX_1); + gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE1, GPIO_MUX_1); + + /* tmr3 configuration: input capture mode + the external signal is connected to tmr3 ch2 pin (pa.07) + the rising edge is used as active edge, + the tmr3 c2dt is used to compute the frequency value */ + + /* tmr3 counter mode configuration */ + tmr_base_init(TMR3, 0xFFFF, 0); + tmr_cnt_dir_set(TMR3, TMR_COUNT_UP); + + /* configure tmr3 channel1 to get clock signal */ + g_tmr_input_config_struct.input_channel_select = TMR_SELECT_CHANNEL_1; + g_tmr_input_config_struct.input_mapped_select = TMR_CC_CHANNEL_MAPPED_DIRECT; + g_tmr_input_config_struct.input_polarity_select = TMR_INPUT_RISING_EDGE; + tmr_input_channel_init(TMR3, &g_tmr_input_config_struct, _div); + + tmr_interrupt_enable(TMR3, TMR_C1_INT, TRUE); + + /* configure tmr3 channel2 to get clock signal */ + g_tmr_input_config_struct.input_channel_select = TMR_SELECT_CHANNEL_2; + g_tmr_input_config_struct.input_mapped_select = TMR_CC_CHANNEL_MAPPED_DIRECT; + g_tmr_input_config_struct.input_polarity_select = TMR_INPUT_RISING_EDGE; + tmr_input_channel_init(TMR3, &g_tmr_input_config_struct, _div); + + tmr_interrupt_enable(TMR3, TMR_C2_INT, TRUE); + + /* configure tmr3 channel3 to get clock signal */ + g_tmr_input_config_struct.input_channel_select = TMR_SELECT_CHANNEL_3; + g_tmr_input_config_struct.input_mapped_select = TMR_CC_CHANNEL_MAPPED_DIRECT; + g_tmr_input_config_struct.input_polarity_select = TMR_INPUT_RISING_EDGE; + tmr_input_channel_init(TMR3, &g_tmr_input_config_struct, _div); + + tmr_interrupt_enable(TMR3, TMR_C3_INT, TRUE); + + /* configure tmr3 channel4 to get clock signal */ + g_tmr_input_config_struct.input_channel_select = TMR_SELECT_CHANNEL_4; + g_tmr_input_config_struct.input_mapped_select = TMR_CC_CHANNEL_MAPPED_DIRECT; + g_tmr_input_config_struct.input_polarity_select = TMR_INPUT_RISING_EDGE; + tmr_input_channel_init(TMR3, &g_tmr_input_config_struct, _div); + + tmr_interrupt_enable(TMR3, TMR_C4_INT, TRUE); + + for(i = 0; i < _div; i++) + { + g_input_div *= 2; + } + + /* tmr2 trigger interrupt nvic init */ +// nvic_priority_group_config(NVIC_PRIORITY_GROUP_4); + nvic_irq_enable(TMR3_GLOBAL_IRQn, 0, 0); + + /* enable tmr3 */ + tmr_counter_enable(TMR3, TRUE); + +} + + +/** + * @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 = SW1_BUTTON_PIN | SW2_BUTTON_PIN | SW3_BUTTON_PIN | SW4_BUTTON_PIN | SW5_BUTTON_PIN; +// gpio_init_struct.gpio_pull = GPIO_PULL_UP; +// gpio_init(USER_BUTTON_PORT, &gpio_init_struct); +} + +void freq_level_gpio_init(void) +{ + gpio_init_type gpio_init_struct; + + /* enable the button clock */ + crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE); + + /* set default parameter */ + gpio_default_para_init(&gpio_init_struct); + + gpio_init_struct.gpio_pins = FLPA1_PIN | FLPA2_PIN | FLPB1_PIN | FLPB2_PIN | FLPC1_PIN | FLPC2_PIN | FLPD1_PIN | FLPD2_PIN; + gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT; + 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); +} + +/** + * @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]; +// set_loops_relay_off(led); +} + +/** + * @brief turns selected led toggle. + * @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); + } +} + +/** + * @} + */ + +/** + * @} + */ + diff --git a/vd960Loop/project/at32f421_board/at32f421_board.h b/vd960Loop/project/at32f421_board/at32f421_board.h new file mode 100644 index 0000000..f63b6e6 --- /dev/null +++ b/vd960Loop/project/at32f421_board/at32f421_board.h @@ -0,0 +1,310 @@ +/** + ************************************************************************** + * @file at32f421_board.h + * @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 __AT32F421_BOARD_H +#define __AT32F421_BOARD_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "stdio.h" +#include "at32f421.h" +#include + +/** @addtogroup AT32F421_board + * @{ + */ + +/** @addtogroup BOARD + * @{ + */ + +/** @defgroup BOARD_pins_definition + * @{ + */ + +/** + * this header include define support list: + * 1. at-start-f421 v1.x boards + * if define AT_START_F421_V1, the header file support at-start-f421 v1.x board + */ + +#if !defined (AT_START_F421_V1) +#error "please select first the board at-start device used in your application (in at32f421_board.h file)" +#endif + +/******************** define led ********************/ +typedef enum +{ +// LED1 = 0, // PWR +// LED2 , // LP1 +// LED3 , // LP2 +// LED4 , // LP3 +// LED5 // LP4 +// LED_PWR = 0, // PWR + LED_LP1 = 0, // LP1 + LED_LP2 , // LP2 + LED_LP3 , // LP3 + LED_LP4 // LP4 +} led_type; + + +#define LED_NUM 4 + +//LED_PWR +//#define LED0_PIN GPIO_PINS_15 +//#define LED0_GPIO GPIOA +//#define LED0_GPIO_CRM_CLK CRM_GPIOA_PERIPH_CLOCK + +//LP1 +#define LED1_PIN GPIO_PINS_15 +#define LED1_GPIO GPIOA +#define LED1_GPIO_CRM_CLK CRM_GPIOA_PERIPH_CLOCK +#define LED_LP1_PIN LED1_PIN +//LP2 +#define LED2_PIN GPIO_PINS_3 +#define LED2_GPIO GPIOB +#define LED2_GPIO_CRM_CLK CRM_GPIOB_PERIPH_CLOCK +#define LED_LP2_PIN LED2_PIN +//LP3 +#define LED3_PIN GPIO_PINS_5 +#define LED3_GPIO GPIOA +#define LED3_GPIO_CRM_CLK CRM_GPIOA_PERIPH_CLOCK +#define LED_LP3_PIN LED3_PIN +//LP4 +#define LED4_PIN GPIO_PINS_4 +#define LED4_GPIO GPIOA +#define LED4_GPIO_CRM_CLK CRM_GPIOA_PERIPH_CLOCK +#define LED_LP4_PIN LED4_PIN + + +#define LED_RED_PIN LED0_PIN +#define LED_RED_GPIO LED0_GPIO +#define LED_GPIO_CRM_CLK LED0_GPIO_CRM_CLK + +#define LED1_ON gpio_bits_reset(LED1_GPIO, LED1_PIN) +#define LED1_OFF gpio_bits_set(LED1_GPIO, LED1_PIN) +#ifdef DEBUG +#define LED2_ON gpio_bits_reset(LED2_GPIO, LED2_PIN) +#define LED2_OFF gpio_bits_set(LED2_GPIO, LED2_PIN) +#else +#define LED2_ON gpio_bits_reset(LED2_GPIO, LED2_PIN) +#define LED2_OFF gpio_bits_set(LED2_GPIO, LED2_PIN) +#endif +#define LED3_ON gpio_bits_reset(LED3_GPIO, LED3_PIN) +#define LED3_OFF gpio_bits_set(LED3_GPIO, LED3_PIN) +#define LED4_ON gpio_bits_reset(LED4_GPIO, LED4_PIN) +#define LED4_OFF gpio_bits_set(LED4_GPIO, LED4_PIN) + +//#ifdef DEBUG +//#define LEDA_OFF +//#define LEDA_ON +//#else +//#define LEDA_OFF gpio_bits_set(LED_GREEN_GPIO, LED_GREEN_PIN) +//#define LEDA_ON gpio_bits_reset(LED_GREEN_GPIO, LED_GREEN_PIN) +//#endif + +//#define LEDC_OFF gpio_bits_set(LED_YELLOW_GPIO, LED_YELLOW_PIN) +//#define LEDC_ON gpio_bits_reset(LED_YELLOW_GPIO, LED_YELLOW_PIN) + +//#define LED_YELLOW_OFF LEDC_OFF +//#define LED_YELLOW_ON LEDC_ON + + + + +// RLY +//#define RLY1_PIN GPIO_PINS_6 +//#define RLY1_GPIO GPIOA +//#define RLY1_CRM_CLK CRM_GPIOA_PERIPH_CLOCK + +//#define RLY2_PIN GPIO_PINS_5 +//#define RLY2_GPIO GPIOA +//#define RLY2_CRM_CLK CRM_GPIOA_PERIPH_CLOCK + +//Functional state Relay and Exist state Relay +#define RLY1_PIN GPIO_PINS_4 +#define RLY1_GPIO GPIOB +#define RLY1_CRM_CLK CRM_GPIOB_PERIPH_CLOCK + +#define RLY2_PIN GPIO_PINS_5 +#define RLY2_GPIO GPIOB +#define RLY2_CRM_CLK CRM_GPIOB_PERIPH_CLOCK + +#define RLY3_PIN GPIO_PINS_6 +#define RLY3_GPIO GPIOB +#define RLY3_CRM_CLK CRM_GPIOB_PERIPH_CLOCK + +#define RLY4_PIN GPIO_PINS_7 +#define RLY4_GPIO GPIOB +#define RLY4_CRM_CLK CRM_GPIOB_PERIPH_CLOCK + +#define RLY1_ON gpio_bits_set(RLY1_GPIO, RLY1_PIN) +#define RLY1_OFF gpio_bits_reset(RLY1_GPIO, RLY1_PIN) +#define RLY2_ON gpio_bits_set(RLY2_GPIO, RLY2_PIN) +#define RLY2_OFF gpio_bits_reset(RLY2_GPIO, RLY2_PIN) +#define RLY3_ON gpio_bits_set(RLY3_GPIO, RLY3_PIN) +#define RLY3_OFF gpio_bits_reset(RLY3_GPIO, RLY3_PIN) +#define RLY4_ON gpio_bits_set(RLY4_GPIO, RLY4_PIN) +#define RLY4_OFF gpio_bits_reset(RLY4_GPIO, RLY4_PIN) + + + +//Freq level +#define FLPA1_PIN GPIO_PINS_2 // 33nF +#define FLPA1_GPIO GPIOA +#define FLPA1_CRM_CLK CRM_GPIOA_PERIPH_CLOCK + +#define FLPA2_PIN GPIO_PINS_3 // 10nF +#define FLPA2_GPIO GPIOA +#define FLPA2_CRM_CLK CRM_GPIOA_PERIPH_CLOCK + +#define FLPB1_PIN GPIO_PINS_0 // 33nF +#define FLPB1_GPIO GPIOA +#define FLPB1_CRM_CLK CRM_GPIOA_PERIPH_CLOCK + +#define FLPB2_PIN GPIO_PINS_1 // 10nF +#define FLPB2_GPIO GPIOA +#define FLPB2_CRM_CLK CRM_GPIOA_PERIPH_CLOCK + +#define FLPC1_PIN GPIO_PINS_11 // 33nF +#define FLPC1_GPIO GPIOA +#define FLPC1_CRM_CLK CRM_GPIOA_PERIPH_CLOCK + +#define FLPC2_PIN GPIO_PINS_12 // 10nF +#define FLPC2_GPIO GPIOA +#define FLPC2_CRM_CLK CRM_GPIOA_PERIPH_CLOCK + +#define FLPD1_PIN GPIO_PINS_13 // 33nF +#define FLPD1_GPIO GPIOA +#define FLPD1_CRM_CLK CRM_GPIOA_PERIPH_CLOCK + +#define FLPD2_PIN GPIO_PINS_14 // 10nF +#define FLPD2_GPIO GPIOA +#define FLPD2_CRM_CLK CRM_GPIOA_PERIPH_CLOCK + +// Set loop frequent level: high or low level +#define FLPA1_HIGH gpio_bits_reset(FLPA1_GPIO, FLPA1_PIN) +#define FLPA1_LOW gpio_bits_set(FLPA1_GPIO, FLPA1_PIN) +#define FLPA2_HIGH gpio_bits_reset(FLPA2_GPIO, FLPA2_PIN) +#define FLPA2_LOW gpio_bits_set(FLPA2_GPIO, FLPA2_PIN) + +#define FLPB1_HIGH gpio_bits_reset(FLPB1_GPIO, FLPB1_PIN) +#define FLPB1_LOW gpio_bits_set(FLPB1_GPIO, FLPB1_PIN) +#define FLPB2_HIGH gpio_bits_reset(FLPB2_GPIO, FLPB2_PIN) +#define FLPB2_LOW gpio_bits_set(FLPB2_GPIO, FLPB2_PIN) + +#define FLPC1_HIGH gpio_bits_reset(FLPC1_GPIO, FLPC1_PIN) +#define FLPC1_LOW gpio_bits_set(FLPC1_GPIO, FLPC1_PIN) +#define FLPC2_HIGH gpio_bits_reset(FLPC2_GPIO, FLPC2_PIN) +#define FLPC2_LOW gpio_bits_set(FLPC2_GPIO, FLPC2_PIN) + +#define FLPD1_HIGH gpio_bits_reset(FLPD1_GPIO, FLPD1_PIN) +#define FLPD1_LOW gpio_bits_set(FLPD1_GPIO, FLPD1_PIN) +#define FLPD2_HIGH gpio_bits_reset(FLPD2_GPIO, FLPD2_PIN) +#define FLPD2_LOW gpio_bits_set(FLPD2_GPIO, FLPD2_PIN) + + +/**************** define print uart ******************/ +#define PRINT_UART USART1 +#define PRINT_UART_CRM_CLK CRM_USART2_PERIPH_CLOCK +#define PRINT_UART_TX_PIN GPIO_PINS_8 +#define PRINT_UART_TX_GPIO GPIOA +#define PRINT_UART_TX_GPIO_CRM_CLK CRM_GPIOA_PERIPH_CLOCK +#define PRINT_UART_TX_PIN_SOURCE GPIO_PINS_SOURCE8 +#define PRINT_UART_TX_PIN_MUX_NUM GPIO_MUX_4 + +/******************* 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); +void loop_timer_io_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); +void freq_level_gpio_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); + +void poll_red_pwm(void); + +/* printf uart init function */ +void uart_print_init(uint32_t baudrate); +void dma_configuration(void); +void usart_configuration(void); +void usart1_sendstring(uint8_t *buf, uint16_t len); + + +/** + * @} + */ +extern crm_clocks_freq_type g_crm_clocks_freq_struct; +extern tmr_input_config_type g_tmr_input_config_struct; + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif +