/**
 *******************************************************************************
 * @file    main.c
 *
 * @brief   Main program body.
 *
 * @author  DPM
 *
 * @version V1.0.0
 *
 * @date    2024-5-6
 *
 *******************************************************************************/

/*
    Copyright (c) 2024, DP Microelectronics Inc.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

/* Files include */
#include "main.h"
#include "dpm32m0xx.h"
#include "dpm32m0xx_eval.h"
#include "systick.h"
#include <stdio.h>


/**
  * @addtogroup DPM32M08X_LibSamples
  * @{
  */

/**
  * @addtogroup CAN
  * @{
  */

/**
  * @addtogroup CAN_BasicBlock
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
CAN_InitTypeStruct CAN_InitStruct;

/* Private define ------------------------------------------------------------*/
#define NUM 5

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
CanBasicMsg BasicTxMessage;
CanBasicMsg BasicRxMessage;
uint32_t CAN_Identifier = 0x555;

/* Private function prototypes -----------------------------------------------*/

/* Private functions ------------------------------------------------------------*/

/**
  * @brief  The program entry function.
  * @retval int
  */
int main(void)
{
  /* Configure the system clock */
  SystemClockInit();

  /* Configure the system tick */
  SystickInit();

  /* LED Init */
  EVAL_LedInit(LED1);

  /* Print Uart Init */
  EVAL_ComInit();

  /********************* Operation  *********************/
  RCC_Unlock();
  RCC_AHBPeriphClockCmd(RCC_AHB_PERIPH_GPIO, ENABLE);
  RCC_APBPeriphClockCmd(RCC_APB_PERIPH_CAN, ENABLE);
  RCC_Lock();
  /* Cofig CAN GPIO */
  GPIO_InitTypeStruct GPIO_InitStruct;
  GPIO_InitStruct.GPIO_Mode = GPIO_MODE_ALT_FUNC;
  GPIO_InitStruct.GPIO_Speed = GPIO_SPEED_HIGH;
  GPIO_InitStruct.GPIO_OType = GPIO_OTYPE_PP;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PUPD_NOPULL;
  GPIO_InitStruct.GPIO_Pin = GPIO_PIN_8 | GPIO_PIN_9;
  GPIO_Init(GPIOB, &GPIO_InitStruct);
  GPIO_AltFuncConfig(GPIOB, GPIO_PIN_8 | GPIO_PIN_9, GPIO_ALT_FUNC_4);
  /* Config CAN */
  CAN_StructInit(&CAN_InitStruct);
  CAN_InitStruct.CAN_Prescaler = 3;
  CAN_InitStruct.CAN_TimeSegment1 = CAN_TSEG1_3TQ;
  CAN_InitStruct.CAN_TimeSegment2 = CAN_TSEG2_2TQ;
  CAN_InitStruct.CAN_SyncJumpWidth = CAN_SJW_1TQ;
  CAN_InitStruct.CAN_Sampling = CAN_SAM_SINGLE;
  CAN_Init(&CAN_InitStruct);
  /* Enable CAN */
  CAN_Cmd(ENABLE);
  /* Initialize can transmit data */
  BasicTxMessage.Data[0] = 0x11;
  BasicTxMessage.Data[1] = 0x22;
  BasicTxMessage.Data[2] = 0x33;
  BasicTxMessage.Data[3] = 0x44;
  BasicTxMessage.Data[4] = 0x55;
  BasicTxMessage.Data[5] = 0x66;
  BasicTxMessage.Data[6] = 0x77;
  BasicTxMessage.Data[7] = 0x88;
  BasicTxMessage.DLC = 8;
  BasicTxMessage.RTR = CAN_RTR_TYPE_DATA;
  BasicTxMessage.ID = CAN_Identifier;

  uint32_t tick = GetTick();

  /* Infinite loop */
  while(1)
  {
    /* CAN receive */
    if(CAN_GetFlagStatus(CAN_FLAG_RBS) && CAN_GetFlagStatus(CAN_FLAG_RS))
    {
      BasicRxMessage.Data[0] = 0;
      BasicRxMessage.Data[1] = 0;
      BasicRxMessage.Data[2] = 0;
      BasicRxMessage.Data[3] = 0;
      BasicRxMessage.Data[4] = 0;
      BasicRxMessage.Data[5] = 0;
      BasicRxMessage.Data[6] = 0;
      BasicRxMessage.Data[7] = 0;
      BasicRxMessage.ID = 0;
      BasicRxMessage.RTR = CAN_RTR_TYPE_DATA;
      BasicRxMessage.DLC = 0;
      CAN_Receive(&BasicRxMessage);

      printf("********************* CAN BASIC Receive  *********************\n");
      printf("Data: 0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X\r\n", \
             BasicRxMessage.Data[0], \
             BasicRxMessage.Data[1], \
             BasicRxMessage.Data[2], \
             BasicRxMessage.Data[3], \
             BasicRxMessage.Data[4], \
             BasicRxMessage.Data[5], \
             BasicRxMessage.Data[6], \
             BasicRxMessage.Data[7]);
      printf("DLC: %d\r\n", BasicRxMessage.DLC);
      printf("RTR: %d\r\n", BasicRxMessage.RTR);
      printf("ID: 0x%X\r\n", BasicRxMessage.DLC);
    }

    /* CAN Transmit */
    if(tick % 1000 == 0)
    {
      if(CAN_GetFlagStatus(CAN_FLAG_TBS)/*&& CAN_GetFlagStatus(CAN_FLAG_TS)*/)
      {
        /* Switch LED status */
        EVAL_LedToggle(LED1);

        CAN_Transmit(&BasicTxMessage);
        printf("********************* CAN BASIC Transmit  *********************\n");
        printf("Data: 0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X\r\n", \
               BasicTxMessage.Data[0], \
               BasicTxMessage.Data[1], \
               BasicTxMessage.Data[2], \
               BasicTxMessage.Data[3], \
               BasicTxMessage.Data[4], \
               BasicTxMessage.Data[5], \
               BasicTxMessage.Data[6], \
               BasicTxMessage.Data[7]);
        printf("DLC: %d\r\n", BasicTxMessage.DLC);
        printf("RTR: %d\r\n", BasicTxMessage.RTR);
        printf("ID: 0x%X\r\n", BasicTxMessage.DLC);
      }
    }

    tick = GetTick();
  }
}


/* retarget the C library printf function to the usart */
int fputc(int ch, FILE *f)
{
  UART_SendData(EVAL_COM0, (uint8_t)ch);

  while(RESET == UART_GetIntFlagStatus(EVAL_COM0, UART_INT_FLAG_TXE));

  return ch;
}



/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT DP *****END OF FILE ***************************/
