/**
 * @file    main.c
 * @brief   该文件是程序的入口点，包含主函数`main`以及一些辅助函数的实现
 * @author
 * @date
 * @version
 *
 * 代码许可和免责信息
 * 武汉芯源半导体有限公司授予您使用所有编程代码示例的非专属的版权许可，您可以由
 * 此生成根据您的特定需要而定制的相似功能。根据不能被排除的任何法定保证，武汉芯
 * 源半导体有限公司及其程序开发商和供应商对程序或技术支持（如果有）不提供任何明
 * 示或暗含的保证或条件，包括但不限于暗含的有关适销性、适用于某种特定用途和非侵
 * 权的保证或条件。
 * 无论何种情形，武汉芯源半导体有限公司及其程序开发商或供应商均不对下列各项负责，
 * 即使被告知其发生的可能性时，也是如此：数据的丢失或损坏；直接的、特别的、附带
 * 的或间接的损害，或任何后果性经济损害；或利润、业务、收入、商誉或预期可节省金
 * 额的损失。
 * 某些司法辖区不允许对直接的、附带的或后果性的损害有任何的排除或限制，因此某些
 * 或全部上述排除或限制可能并不适用于您。
 **/
/******************************************************************************
 * Include files
 ******************************************************************************/
#include "../inc/main.h"

/******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
#define BAUDRATE                       115200
#define BIT_PERIOD_ns                  (1000000000/BAUDRATE)
#define LOGIC_0_PERIOD_ns              (BIT_PERIOD_ns * 3 / 16)    // 3/16 ENDEC编码波形

/******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/
void SYSCTRL_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);

static void GTIM1_Config(void);
static void UART1_Config(void);

void IrDA_Send(const uint8_t *pData, uint16_t len);

/******************************************************************************
 * Local variable definitions ('static')                                      *
 ******************************************************************************/
uint32_t DelayTime;
static uint8_t DataBuffer[10] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39};


/*****************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/

/**
 * @brief
 * @return int32_t
 */
int32_t main(void)
{
    /* System Clocks Configuration */
    SYSCTRL_Configuration();

    /* GPIO Configuration */
    GPIO_Configuration();

    GTIM1_Config();
    UART1_Config();

    IRMOD_Config(IRMOD_UART1_TXD_AND_GTIM1_CH2, IRMOD_POLARITY_POSITIVE);

    /* NVIC Configuration */
    NVIC_Configuration();

    while (1)
    {
        IrDA_Send(DataBuffer, 10);
    }
}

/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void SYSCTRL_Configuration(void)
{
    SYSCTRL_HSI_Enable(HSIOSC_TO_HSI96MHZ);  
    SYSCTRL_HCLKPRS_Config(SYSCTRL_HCLK_DIV1);
    SYSCTRL_PCLKPRS_Config(SYSCTRL_PCLK_DIV1);
    SYSCTRL_SystemCoreClockUpdate(96000000);
}

/**
  * @brief  Configure the GPIO Pins.  *
  * @param  None
  * @retval None
  */
void GPIO_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;

    __SYSCTRL_GPIOB_CLK_ENABLE();
    PB09_AFx_IROUT();

    GPIO_InitStruct.IT = GPIO_IT_NONE;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pins = GPIO_PIN_9;
    GPIO_Init(CW_GPIOB, &GPIO_InitStruct);
}


/**
  * @brief  Configure the nested vectored interrupt controller.
  * @param  None
  * @retval None
  */
void NVIC_Configuration(void)
{
   
}

/**
 * @brief GTIM1生成载波
 *
 */
static void GTIM1_Config(void)
{
    GTIM_InitTypeDef GTIM_InitStruct;
    GTIM_OCModeCfgTypeDef GTIM_OCModeCfgStruct;

    __SYSCTRL_GTIM1_CLK_ENABLE();
    __SYSCTRL_GPIOA_CLK_ENABLE();
    
    PA07_AFx_GTIM1CH2();    
    PA07_DIGTAL_ENABLE();
    PA07_DIR_OUTPUT();
    PA07_PUSHPULL_ENABLE();
    
    GTIM_InitStruct.AlignMode = GTIM_ALIGN_MODE_EDGE;
    GTIM_InitStruct.ARRBuffState = GTIM_ARR_BUFF_EN;
    GTIM_InitStruct.Direction = GTIM_DIRECTION_UP;
    GTIM_InitStruct.EventOption = GTIM_EVENT_NORMAL;
    GTIM_InitStruct.Prescaler = 0;     // 1分频
    GTIM_InitStruct.PulseMode = GTIM_PULSE_MODE_DIS;
    GTIM_InitStruct.ReloadValue = BIT_PERIOD_ns * 96 / 1000;
    GTIM_InitStruct.UpdateOption = GTIM_UPDATE_DIS;    
    GTIM_TimeBaseInit(CW_GTIM1, &GTIM_InitStruct);
    
    GTIM_OCModeCfgStruct.FastMode = ENABLE;
    GTIM_OCModeCfgStruct.OCMode = GTIM_OC_MODE_PWM1;
    GTIM_OCModeCfgStruct.OCPolarity = GTIM_OC_POLAR_NONINVERT;
    GTIM_OCModeCfgStruct.PreloadState = ENABLE;

    GTIM_OC2ModeCfg(CW_GTIM1, &GTIM_OCModeCfgStruct);   
    GTIM_SetCompare2(CW_GTIM1, LOGIC_0_PERIOD_ns * 96 /1000);
    GTIM_OC2Cmd(CW_GTIM1, ENABLE);

    GTIM_Cmd(CW_GTIM1, ENABLE);
}

static void UART1_Config(void)
{
    UART_InitTypeDef UART_InitStruct = {0};
    __SYSCTRL_UART1_CLK_ENABLE();
    __SYSCTRL_GPIOA_CLK_ENABLE();
    
    PA02_AFx_UART1TXD();
    PA02_DIGTAL_ENABLE();
    PA02_DIR_OUTPUT();
    PA02_PUSHPULL_ENABLE();

    UART_InitStruct.UART_BaudRate = 115200;
    UART_InitStruct.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
    UART_InitStruct.UART_Mode = UART_Mode_Tx;    
    UART_InitStruct.UART_Over = UART_Over_16;
    UART_InitStruct.UART_Parity = UART_Parity_No;
    UART_InitStruct.UART_Source = UART_Source_PCLK;
    UART_InitStruct.UART_StartBit = UART_StartBit_LL;
    UART_InitStruct.UART_StopBits = UART_StopBits_1;
    UART_InitStruct.UART_UclkFreq = SystemCoreClock;

    UART_Init(CW_UART1, &UART_InitStruct);
}


void IrDA_Send(const uint8_t *pData, uint16_t len)
{    
    while (len--)
    {
        UART_SendData(CW_UART1, *pData);
        while (CW_UART1->ISR_f.TXBUSY)
            ;
        pData++;
    }    
}
/******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number */
    printf("Wrong parameters value: file %s on line %d\r\n", file, line);
    /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

