/*******************************************************************************
* @file             main.c
*
* @version          1.0.0
*
* @brief            Integration Framework -  main function
*
* Copyright (C) 2026 Geehy Semiconductor
*
* You may not use this file except in compliance with the GEEHY COPYRIGHT NOTICE
* (Geehy Semiconductor Software License Agreement).
*
* The program is only for reference, which is distributed in the hope that it
* will be useful and instructional for customers to develop their software.
* Unless required by applicable law or agreed to in writing, the program is
* distributed on an "AS IS" BASIS, WITHOUT ANY WARRANTY OR CONDITIONS OF ANY
* KIND, either express or implied. See the Geehy Semiconductor Software License Agreement for
* the governing permissions and limitations under the License.
*
*******************************************************************************/

#ifdef __cplusplus
extern "C"{
#endif

#include "CDD_Usart.h"
#include "Mcu.h"
#include "Ocu.h"
#include "Port.h"

/*******************************************************************************
**                        LOCAL VARIABLES                                    **
*******************************************************************************/  
static uint8_t g_flag_0_notification_print = 0;
static uint8_t g_flag_1_notification_print = 0;
static uint8_t g_flag_2_notification_print = 0;
static uint8_t g_flag_3_notification_print = 0;
static uint8_t g_flag_4_notification_print = 0;
static uint8_t g_flag_5_notification_print = 0;
static uint8_t g_flag_6_notification_print = 0;
static uint8_t g_flag_7_notification_print = 0;
static uint8_t g_flag_8_notification_print = 0;
static uint8_t g_flag_9_notification_print = 0;
static uint8_t g_flag_10_notification_print = 0;
static uint8_t g_flag_11_notification_print = 0;
static uint8_t g_flag_12_notification_print = 0;
static uint8_t g_flag_13_notification_print = 0;
static uint8_t g_flag_14_notification_print = 0;
static uint8_t g_flag_15_notification_print = 0;

/*******************************************************************************
**                     GLOBAL FUNCTION                                        **
*******************************************************************************/

extern ISR(TMR1_CC_IRQHandler);
extern ISR(TMR2_IRQHandler);
extern ISR(TMR3_IRQHandler);
extern ISR(TMR4_IRQHandler);

 /*!
 * @brief       Ocu channel match notification
 *
 * @param       None
 *
 * @retval      None
 *
 * @note        None
 */
void Ocu_Channel0_Notificaiton(void)
{
  if(1 == g_flag_0_notification_print)
  {
    g_flag_0_notification_print = 0;
    printf("OcuChannel_0 notification has been called\r\n");
    printf("\r\nOCU_ITA OK\r\n");
  }
}

void Ocu_Channel1_Notificaiton(void)
{
  if(1 == g_flag_1_notification_print)
  {
    g_flag_1_notification_print = 0;
    printf("OcuChannel_1 notification has been called\r\n");
    printf("\r\nOCU_ITA OK\r\n");
  }
}

void Ocu_Channel2_Notificaiton(void)
{
  if(1 == g_flag_2_notification_print)
  {
    g_flag_2_notification_print = 0;
    printf("OcuChannel_2 notification has been called\r\n");
    printf("\r\nOCU_ITA OK\r\n");
  }
}

void Ocu_Channel3_Notificaiton(void)
{
  if(1 == g_flag_3_notification_print)
  {
    g_flag_3_notification_print = 0;
    printf("OcuChannel_3 notification has been called\r\n");
    printf("\r\nOCU_ITA OK\r\n");
  }
}

void Ocu_Channel4_Notificaiton(void)
{
  if(1 == g_flag_4_notification_print)
  {
    g_flag_4_notification_print = 0;
    printf("OcuChannel_4 notification has been called\r\n");
    printf("\r\nOCU_ITA OK\r\n");
  }
}

void Ocu_Channel5_Notificaiton(void)
{
  if(1 == g_flag_5_notification_print)
  {
    g_flag_5_notification_print = 0;
    printf("OcuChannel_5 notification has been called\r\n");
    printf("\r\nOCU_ITA OK\r\n");
  }
}

void Ocu_Channel6_Notificaiton(void)
{
  if(1 == g_flag_6_notification_print)
  {
    g_flag_6_notification_print = 0;
    printf("OcuChannel_6 notification has been called\r\n");
    printf("\r\nOCU_ITA OK\r\n");
  }
}

void Ocu_Channel7_Notificaiton(void)
{
  if(1 == g_flag_7_notification_print)
  {
    g_flag_7_notification_print = 0;
    printf("OcuChannel_7 notification has been called\r\n");
    printf("\r\nOCU_ITA OK\r\n");
  }
}

void Ocu_Channel8_Notificaiton(void)
{
  if(1 == g_flag_8_notification_print)
  {
    g_flag_8_notification_print = 0;
    printf("OcuChannel_8 notification has been called\r\n");
    printf("\r\nOCU_ITA OK\r\n");
  }
}

void Ocu_Channel9_Notificaiton(void)
{
  if(1 == g_flag_9_notification_print)
  {
    g_flag_9_notification_print = 0;
    printf("OcuChannel_9 notification has been called\r\n");
    printf("\r\nOCU_ITA OK\r\n");
  }
}

void Ocu_Channel10_Notificaiton(void)
{
  if(1 == g_flag_10_notification_print)
  {
    g_flag_10_notification_print = 0;
    printf("OcuChannel_10 notification has been called\r\n");
    printf("\r\nOCU_ITA OK\r\n");
  }
}

void Ocu_Channel11_Notificaiton(void)
{
  if(1 == g_flag_11_notification_print)
  {
    g_flag_11_notification_print = 0;
    printf("OcuChannel_11 notification has been called\r\n");
    printf("\r\nOCU_ITA OK\r\n");
  }
}

void Ocu_Channel12_Notificaiton(void)
{
  if(1 == g_flag_12_notification_print)
  {
    g_flag_12_notification_print = 0;
    printf("OcuChannel_12 notification has been called\r\n");
    printf("\r\nOCU_ITA OK\r\n");
  }
}

void Ocu_Channel13_Notificaiton(void)
{
  if(1 == g_flag_13_notification_print)
  {
    g_flag_13_notification_print = 0;
    printf("OcuChannel_13 notification has been called\r\n");
    printf("\r\nOCU_ITA OK\r\n");
  }
}

void Ocu_Channel14_Notificaiton(void)
{
  if(1 == g_flag_14_notification_print)
  {
    g_flag_14_notification_print = 0;
    printf("OcuChannel_14 notification has been called\r\n");
    printf("\r\nOCU_ITA OK\r\n");
  }
}

void Ocu_Channel15_Notificaiton(void)
{
  if(1 == g_flag_15_notification_print)
  {
    g_flag_15_notification_print = 0;
    printf("OcuChannel_15 notification has been called\r\n");
    printf("\r\nOCU_ITA OK\r\n");
  }
}

 /*!
 * @brief       Delay function
 *
 * @param       DelayTime：The delay time
 *
 * @retval      None
 *
 * @note        None
 */
/*  */
static void sys_delay(uint32_t DelayTime)
{
  while(DelayTime--);
}

int main()
{
   /* Init the mcu,port,uart and ocu modules */
    Mcu_Init(&g_mcuConfig);
    Mcu_InitClock(McuClockSettingConfig_0);
    Port_Init(&g_portConfig);
    Usart_Init(g_usartConfg);
    Ocu_Init(&g_ocuConfig);
    
    NVIC_EnableIRQ(TMR1_CC_IRQn);
    NVIC_EnableIRQ(TMR2_IRQn);
    NVIC_EnableIRQ(TMR3_IRQn);
    NVIC_EnableIRQ(TMR4_IRQn);
    
    printf("\r\n---------------- OCU_ITA Start ----------------\r\n\r\n");
    
    /* Enable the notification of the ocu logic channel */ 
    for (int i = 0; i < 4; i++)
    {
        Ocu_EnableNotification(i);
    }
    /* Start the ocu channel */
    g_flag_0_notification_print = 1;
    g_flag_1_notification_print = 1;
    g_flag_2_notification_print = 1;
    g_flag_3_notification_print = 1;
    g_flag_4_notification_print = 1;
    g_flag_5_notification_print = 1;
    g_flag_6_notification_print = 1;
    g_flag_7_notification_print = 1;
    g_flag_8_notification_print = 1;
    g_flag_9_notification_print = 1;
    g_flag_10_notification_print = 1;
    g_flag_11_notification_print = 1;
    g_flag_12_notification_print = 1;
    g_flag_13_notification_print = 1;
    g_flag_14_notification_print = 1;
    g_flag_15_notification_print = 1;

    for (int i = 0; i < 4; i++)
    {
        Ocu_StartChannel(i);
    }

    sys_delay(80000);
    
    /* Stop the ocu channel */ 
    for (int i = 0; i < 4; i++)
    {
        Ocu_StopChannel(i);
    }
    
    /* DeInit the ocu module */
    Ocu_DeInit();

    while(1)
    {
    }
}

#if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @param       *f:  pointer to a FILE that can recording all information
*              needed to control a stream
*
* @retval      The characters that need to be send.
*
* @note
*/
int fputc(int ch, FILE* f)
{
    int reVal = 0;
    reVal = Usart_PutRedefinition(USART2_ID,&ch);
    
    return reVal;
}

/* If configuring the full library using versions after IAR 9.30,
   it is necessary to implement the _write function for printf redirection.*/
size_t __write(int handle,const unsigned char *buf,size_t bufSize)
{
    int32 tempCh = 0;
    for(int i=0; i<bufSize; i++)
    {
        Usart_PutRedefinition(USART2_ID,&tempCh);
    }
    return bufSize;
}

#elif defined (__GNUC__)

/* For Arm Cross GCC: Eclipse */
int _write(int fd,const unsigned char *buf,int len)
{
    int32 tempCh = 0;
    for(int i=0; i<len; i++)
    {
        int32 tempCh = (int32)buf[i];
        Usart_PutRedefinition(USART2_ID,&tempCh);
    }
    return len;
}

#endif

#ifdef __cplusplus
}
#endif
