/**
  **************************************************************************
  * @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 <stdint.h>

/** @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

