/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.1
 *
 * @date        2025-04-31
 *
 * @attention
 *
 *  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.
 */

/* Includes */
#include "Port.h"
#include "CDD_Usart.h"
#include "Mcu.h"
#include "Can.h"
#include "CanHelpers.h"
#include <string.h>

extern volatile uint8 g_receiveFlag;

/** @addtogroup Examples
  @{
  */
void Can_TxCompleteNotification(void)
{
    /* CAN transmission complete interrupt */
    printf("Can Tx Complete Notification\r\n");
}

/**
* @brief  a.Can tx/rx interrupt test:Enable DRXE、TCE、RF1NE and RF0NE and 
*         (STD_ON == CAN_RX_INTERRUPT_HANDLE_ENABLE) && (STD_ON == CAN_TX_INTERRUPT_HANDLE_ENABLE)
*
*         1. Configure CAN FD baudrate to 1000kbps:4000kbps or 500kbps:2000kbps.
*         2. Wait for CAN FD rx message and retransmit it.
*/
static void CAN_Interrupt(void)
{
    uint32 count = 0;
    Std_ReturnType ret = E_NOT_OK;
    uint16 baudrate = CAN_BAUDRATE_CONFIG_ID_0;       /* 500kbps:2000kbps */
    //uint16 baudrate = CAN_BAUDRATE_CONFIG_ID_1;     /* 1000kbps:4000kbps */
    uint8 controllerId1 = CanConf_CanController_CAN0;   /* CAN controller ID */
    Can_PduType g_pduTx;

    printf("\r\n----------------Can interrupt Example----------------\r\n\r\n");

    /* Initialize the CAN driver. */
    Can_Init(&g_canConfig);

    NVIC_SetPriority(CAN_IT0_IRQn, 0x0d);
    NVIC_EnableIRQ(CAN_IT0_IRQn);

    NVIC_SetPriority(CAN_IT1_IRQn, 0x0e);
    NVIC_EnableIRQ(CAN_IT1_IRQn);

    /* Set baudrate */
    ret = Can_SetBaudrate(controllerId1, baudrate);
    if (E_NOT_OK == ret)
    {
        printf("Set CAN0 FD baudrate fail\r\n");
        return;
    }
    /* Change the controller to START mode. */
    ret = Can_SetControllerMode(controllerId1, CAN_CS_STARTED);
    if (E_NOT_OK == ret)
    {
        printf("Set CAN0 controller mode fail\r\n");
        return;
    }

    while(count++ < 0xFFFFFFFF)
    {
       /* Wait for CAN RX FD message */
       if(1 == g_receiveFlag)
       {
           g_pduTx.id = g_rxBuffer[0].canId;
           g_pduTx.swPduHandle = 0xF1;
           g_pduTx.length = g_rxBuffer[0].sduLength;
           g_pduTx.sdu = g_rxBuffer[0].sduData;
           
           /* Transmit CAN FD message as received */
           ret = Can_Write(CAN0_TX_1, &g_pduTx);
           g_receiveFlag = 0;
           if (E_OK != ret)
           {
                printf("CAN0 TX1 FD write failed!\r\n");
            }
            else
            {
                /* CAN0 TX1 FD write success */
            }
           /* CAN0 FD -> CAN0 FD */
           memset(g_rxBuffer,0,sizeof(g_rxBuffer));
       }
    }
}

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    Mcu_Init(&g_mcuConfig);
    Mcu_InitClock(McuClockSettingConfig_0);
    
    Port_Init(&g_portConfig);
    Usart_Init(g_usartConfg); //TX PIN:PA9

    CAN_Interrupt();

    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
/**@} end of group Template_Functions */
/**@} end of group Template */
/**@} end of group Examples */
