#ifndef PLATFORM_H
#define PLATFORM_H

// Include build configuration
#include "build.h"

// ============================================================================
//                           CONSTANTES
// ============================================================================

#if BUILD_PLATFORM == BUILD_PLATFORM_MINIPIX_V330
    // ========================================================================
    //                              MICRO WATCHDOG
    // ========================================================================

    // Include ST HAL for platform
    #include "stm32g0xx.h"

    // BTN
    #define BTN_PORT                                GPIOA
    #define BTN_PIN                                 GPIO_PIN_0
    #define BTN_IRQ                                 EXTI0_1_IRQn
    #define BTN_IRQ_HANDLER                         EXTI0_1_IRQHandler
    #define BTN_WAKEUP_PIN                          PWR_WAKEUP_PIN1_LOW
    #define BTN_WAKEUP_FLAG                         PWR_FLAG_WUF1


    // LED
    #define LED_DATA_PORT                           GPIOA
    #define LED_DATA_PIN                            GPIO_PIN_7
    #define LED_EN_PORT                             GPIOA
    #define LED_EN_PIN                              GPIO_PIN_6
    #define LED_TIM                                 TIM3
    #define LED_TIM_CLK_ENABLE()                    __HAL_RCC_TIM3_CLK_ENABLE()
    #define LED_TIM_AF                              GPIO_AF1_TIM3
    #define LED_TIM_CHANNEL                         TIM_CHANNEL_2
    #define LED_DMA_CLK_ENABLE()                    __HAL_RCC_DMA1_CLK_ENABLE()
    #define LED_DMA_IRQ                             DMA1_Channel1_IRQn
    #define LED_DMA_IRQ_HANDLER                     DMA1_Channel1_IRQHandler
    #define LED_DMA_CHANNEL                         DMA1_Channel1
    #define LED_DMA_REQUEST                         DMA_REQUEST_TIM3_CH2

    // RF
    #define RF_RX_DATA_PORT                         GPIOB
    #define RF_RX_DATA_PIN                          GPIO_PIN_3
    #define RF_RX_GET_DATA()                        ((RF_RX_DATA_PORT->IDR & RF_RX_DATA_PIN) != 0x00u)
    #define RF_RX_EN_PORT                           GPIOA
    #define RF_RX_EN_PIN                            GPIO_PIN_12
    #define RF_RX_TIM                               TIM1
    #define RF_RX_TIM_CLK_ENABLE()                  __HAL_RCC_TIM1_CLK_ENABLE()
    #define RF_RX_TIM_AF                            GPIO_AF1_TIM1
    #define RF_RX_TIM_ACTIVE_CHANNEL                HAL_TIM_ACTIVE_CHANNEL_2
    #define RF_RX_TIM_CHANNEL                       TIM_CHANNEL_2
    #define RF_RX_TIM_GET_CCR(_tim)                 (_tim)->Instance->CCR2
    #define RF_RX_TIM_IRQ                           TIM1_CC_IRQn
    #define RF_RX_TIM_IRQ_HANDLER                   TIM1_CC_IRQHandler

    #define RF_RX_DMA                               DMA1_Channel2
    #define RF_RX_DMA_CLK_ENABLE()                  __HAL_RCC_DMA1_CLK_ENABLE()
    #define RF_RX_DMA_CHANNEL                       TIM_CHANNEL_2
    #define RF_RX_DMA_REQUEST                       DMA_REQUEST_TIM1_CH2
    #define RF_RX_DMA_LINK                          TIM_DMA_ID_CC2
    #define RF_RX_DMA_IRQ                           DMA1_Channel2_3_IRQn
    #define RF_RX_DMA_IRQ_HANDLER                   DMA1_Channel2_3_IRQHandler

    // TERM
    #define TERM_UART                               USART2
    #define TERM_UART_CLK_ENABLE()                  __HAL_RCC_USART2_CLK_ENABLE()
    #define TERM_UART_AF                            GPIO_AF1_USART2
    #define TERM_TX_PORT                            GPIOA
    #define TERM_TX_PIN                             GPIO_PIN_2
    #define TERM_RX_PORT                            GPIOA
    #define TERM_RX_PIN                             GPIO_PIN_3

    // EEPROM
    #define EEPROM_SCL_PORT                         GPIOB
    #define EEPROM_SCL_PIN                          GPIO_PIN_8
    #define EEPROM_SDA_PORT                         GPIOB
    #define EEPROM_SDA_PIN                          GPIO_PIN_9
    #define EEPROM_I2C                              I2C1
    #define EEPROM_I2C_CLK_INIT()                   do{                                                                 \
                                                        RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};                   \
                                                        PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;        \
                                                        PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;     \
                                                        HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);                      \
                                                    }while(0)
    #define EEPROM_SPEED_100KHZ                     0x10707DBC
    #define EEPROM_SPEED_400KHZ                     0x00602173
    #define EEPROM_SPEED_1MHZ                       0x00300B29
    #define EEPROM_I2C_CLK_ENABLE()                 __HAL_RCC_I2C1_CLK_ENABLE();
    #define EEPROM_I2C_AF                           GPIO_AF6_I2C1


#else
    #error Unsupported platform (BUILD_PLATFORM) !
#endif

// ============================================================================
//                              FUNCTION
// ============================================================================




#endif
