#include "Mcu.h"
#include "Port.h"
#include "Pwm.h"
#include "Det.h"
#include "CDD_Usart.h"

void Pwm_Notification_Channel_0(void)
{
    printf("Pwm Notification Channel 0 occurred!\r\n");
}

void Pwm_Tmr1Fault_Notification(uint8 ModuleId)
{
    printf("Pwm Tmr1 Fault occurred, ModuleId:%d\r\n", ModuleId);
}

int main(void)
{
    /* Initialize MCU module */
    Mcu_Init(&g_mcuConfig);
    /* Initialize clock */
    Mcu_InitClock(McuClockSettingConfig_0);
    /* Initialize PORT module */
    Port_Init(&g_portConfig);
    /* Initialize LPUART1 */
    Usart_Init(g_usartConfg);//TX PIN:PA9

    printf("\r\n---------------- Pwm_FaultControl Start ----------------\r\n");

    Pwm_Init(&Pwm_Config);

#if (STD_ON == PWM_NOTIFICATION_SUPPORTED)
    NVIC_SetPriority(TMR1_CC_IRQn, 0x0f);
    NVIC_EnableIRQ(TMR1_CC_IRQn);
    NVIC_SetPriority(TMR1_BRK_UP_TRG_COM_IRQn, 0x0f);
    NVIC_EnableIRQ(TMR1_BRK_UP_TRG_COM_IRQn);
    Pwm_EnableNotification(PwmChannel_0, PWM_BOTH_EDGES);
#endif

    printf("\r\n---------------- Pwm_FaultControl End ------------------\r\n");

    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(USART1_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(USART1_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(USART1_ID,&tempCh);
    }
    return len;
}

#endif

