/****************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V1.0
* $Revision: 1 $
* $Date: 05/12/22 10:30p $
* @brief
*
*
* @note
* Copyright (C) 2022 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
#include <stdio.h>
#include "NM1530.h"
#include <string.h>
#include <math.h>
#include "system_initialize.h"
#define CAN_BAUD_RATE 500000
#define Master_ISP_ID 0xA3
#define Device0_ISP_ID 0xA2
#define Get_Model_Id 0x20
#define Get_FW_Version_Id 0x21
#define CLK_LED1 P33
#define C_Frame_Length 4
#define Frame_Start 0
#define Frame_LED_1 1
#define Frame_LED_2 2
#define Frame_End 3
void Test_BasicMode_Tx(CAN_T *tCAN);
uint32_t unItem, BaudRate = 0, RealBaudRate = 0;
uint8_t u8CAN_PackageFlag = 0, u8CAN_AckFlag = 0;
typedef struct{
uint32_t bit0:1;
uint32_t bit1:1;
uint32_t bit2:1;
uint32_t bit3:1;
uint32_t bit4:1;
uint32_t bit5:1;
uint32_t bit6:1;
uint32_t bit7:1;
uint32_t bit8:1;
uint32_t bit9:1;
uint32_t bit10:1;
uint32_t bit11:1;
uint32_t bit12:1;
uint32_t bit13:1;
uint32_t bit14:1;
uint32_t bit15:1;
uint32_t bit16:1;
uint32_t bit17:1;
uint32_t bit18:1;
uint32_t bit19:1;
uint32_t bit20:1;
uint32_t bit21:1;
uint32_t bit22:1;
uint32_t bit23:1;
uint32_t bit24:1;
uint32_t bit25:1;
uint32_t bit26:1;
uint32_t bit27:1;
uint32_t bit28:1;
uint32_t bit29:1;
uint32_t bit30:1;
uint32_t bit31:1;
}data_bit_t;
typedef union {
uint32_t data_32;
data_bit_t data_bit;
}serial_data_t;
serial_data_t serial_data;
typedef struct{
uint32_t mode;
uint32_t dim;
uint32_t blue;
uint32_t green;
uint32_t red;
}LED_Frame_t;
LED_Frame_t LED1_Frame[4];
LED_Frame_t LED1_Frame_reverse[4];
uint32_t record[3];
typedef struct{
uint32_t R;
uint32_t G;
uint32_t B;
uint32_t valid;
}Color_t;
Color_t Color;
typedef struct{
uint8_t Tag;
uint8_t CmdID;
uint8_t Length;
uint8_t Data[10];
uint8_t CRC;
}IX_t;
IX_t IX;
extern void CAN_Response_Get_Model_Id(CAN_T *tCAN,STR_CANMSG_T *msg);
extern void CAN_Response_Get_FW_Version(CAN_T *tCAN,STR_CANMSG_T *msg);
extern Color_t HSVtoRGB(float H, float S,float V);
extern void CAN_Set_LED_Steady(CAN_T *tCAN,uint16_t C_H,uint8_t Bitmap,uint8_t steady);
extern void CAN_Set_LED_Blink(CAN_T *tCAN,uint16_t C_H,uint8_t Bitmap,uint8_t Interval_T,uint8_t Fadein_T,uint8_t Keep_T,uint8_t Fadeout_T);
__align(4) STR_CANMSG_T rrMsg;
STR_CANMSG_T tMsg;
uint8_t rrMsg_record_Data[8];
uint16_t test_on,Led_steady_on,input_C_H,input_Bitmap,input_steady;
float C_H,C_S,C_V;
uint8_t input_Interval_T,input_Fadein_T,input_Keep_T,input_Fadeout_t;
uint16_t Led_Blink_on;
uint16_t read_record[8];
STR_CANMSG_T msg1;
void CAN_MsgInterrupt(CAN_T *tCAN, uint32_t u32IIDR)
{
if(u32IIDR == 1)
{
CAN_Receive(tCAN, MSG(0), &rrMsg);
if(rrMsg.DLC)
u8CAN_PackageFlag = 1;
}
}
uint16_t reset_can;
void CAN_IRQHandler(void)
{
uint32_t u8IIDRstatus;
u8IIDRstatus = CAN->IIDR;
if(u8IIDRstatus == 0x00008000) /* Check Status Interrupt Flag (Error status Int and Status change Int) */
{
/**************************/
/* Status Change interrupt*/
/**************************/
if(CAN->STATUS & CAN_STATUS_RXOK_Msk)
{
CAN->STATUS &= ~CAN_STATUS_RXOK_Msk; /* Clear RxOK status*/
}
if(CAN->STATUS & CAN_STATUS_TXOK_Msk)
{
CAN->STATUS &= ~CAN_STATUS_TXOK_Msk; /* Clear TxOK status*/
u8CAN_AckFlag = 0;
}
if( CAN->STATUS & CAN_STATUS_BOFF_Msk){
CAN->STATUS &= ~CAN_STATUS_BOFF_Msk;
reset_can = 1;
}
}
else if((u8IIDRstatus >= 0x1) || (u8IIDRstatus <= 0x20))
{
CAN_MsgInterrupt(CAN, u8IIDRstatus);
CAN_CLR_INT_PENDING_BIT(CAN, (u8IIDRstatus - 1)); /* Clear Interrupt Pending */
}
}
uint16_t Data_Cnt;
uint16_t Frame_Length,Frame_Cnt;
uint16_t Set_LED1;
void TMR0_IRQHandler(void){
TIMER_ClearIntFlag(TIMER0);
if(!test_on)
test_on = 1;
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
/* Init System, IP clock */
SYS_Init_Clock();
/* Init multi-function I/O */
GPIO->PWMPOEN = 0;
Initialize_GPIO();
/* Configure UART0 and set UART0 baud rate */
UART_Open(UART0, 115200);
/* Lock protected registers */
SYS_LockReg();
printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
printf("+------------------------------------------------+\n");
Initialize_Timer0();
/*---------------------------------------------------------------------------------------------------------*/
/* SAMPLE CODE */
/*---------------------------------------------------------------------------------------------------------*/
BaudRate = CAN_BAUD_RATE ;
RealBaudRate = CAN_Open(CAN, BaudRate, CAN_NORMAL_MODE);//Set target baud-rate and operation mode.
/* Enable CAN interrupt */
CAN_EnableInt(CAN, (CAN_CON_IE_Msk | CAN_CON_SIE_Msk | CAN_CON_EIE_Msk));
/* Set Interrupt Priority */
NVIC_SetPriority(CAN_IRQn, 0/*(1 << __NVIC_PRIO_BITS) - 2*/);
/* Enable External Interrupt */
NVIC_EnableIRQ(CAN_IRQn);
/* Set CAN reveive message */
CAN_SetRxMsg(CAN, MSG(0), CAN_STD_ID, Master_ISP_ID);
CLK_LED1 = 1;
test_on =1;
while(1){
if(reset_can){
reset_can = 0;
RealBaudRate = CAN_Open(CAN, BaudRate, CAN_NORMAL_MODE);//Set target baud-rate and operation mode.
/* Enable CAN interrupt */
CAN_EnableInt(CAN, (CAN_CON_IE_Msk | CAN_CON_SIE_Msk | CAN_CON_EIE_Msk));
/* Set CAN reveive message */
CAN_SetRxMsg(CAN, MSG(0), CAN_STD_ID, Master_ISP_ID);
}
CLK_LED1 ^=1;
if(u8CAN_PackageFlag){
u8CAN_PackageFlag = 0;
if(rrMsg.Data[0]){
memcpy(rrMsg_record_Data,rrMsg.Data,8);
memset(rrMsg.Data,NULL,8);
}
CAN_SetRxMsg(CAN, MSG(0), CAN_STD_ID, Master_ISP_ID);
}
if(test_on){
test_on = 0;
Test_BasicMode_Tx(CAN);
CLK_SysTickDelay(10000);
}
if(Led_steady_on){
Led_steady_on = 0;
CAN_Set_LED_Steady(CAN,input_C_H,input_Bitmap,input_steady);
}
if(Led_Blink_on){
Led_Blink_on = 0;
CAN_Set_LED_Blink(CAN,input_C_H,input_Bitmap,input_Interval_T,input_Fadein_T,input_Keep_T,input_Fadeout_t);
}
}
}
/*----------------------------------------------------------------------------*/
/* Send Tx Msg by Basic Mode Function (Without Message RAM) */
/*----------------------------------------------------------------------------*/
uint16_t CC_H;
uint8_t Bitmap;
uint8_t steady;
void Test_BasicMode_Tx(CAN_T *tCAN)
{
int32_t delaycount;
delaycount = 1000;
/* Send Message No.1 */
msg1.FrameType = CAN_DATA_FRAME;
msg1.IdType = CAN_STD_ID;
msg1.Id = Device0_ISP_ID;
msg1.DLC = 8;
msg1.Data[0] = 0xFE ;
msg1.Data[1] = 0x8;
msg1.Data[2] = 0x23;
msg1.Data[3] = CC_H & 0xFF;
msg1.Data[4] = (CC_H>>8) & 0xFF;
msg1.Data[5] = Bitmap;
msg1.Data[6] = steady;
msg1.Data[7] = 0x0;
CAN_Transmit(tCAN, MSG(0), &msg1);//Send CAN message
CLK_SysTickDelay(delaycount); /* Generate the Delay Time by Systick */
}
void CAN_Response_Get_Model_Id(CAN_T *tCAN,STR_CANMSG_T *msg){
uint8_t ModelId[3],Len;
ModelId[0] = 'M';
ModelId[1] = 'L';
ModelId[2] = 0x16;
Len = 4;
msg->FrameType = CAN_DATA_FRAME;
msg->IdType = CAN_STD_ID;
msg->Id = Device0_ISP_ID;
msg->DLC = 0x7;
msg->Data[0] = IX.Tag;
msg->Data[1] = Len;
msg->Data[2] = IX.CmdID;
msg->Data[3] = ModelId[0];
msg->Data[4] = ModelId[1];
msg->Data[5] = ModelId[2];
msg->Data[6] = IX.CRC;
CAN_Transmit(tCAN, MSG(0), msg);//Send CAN message
CLK_SysTickDelay(10); /* Generate the Delay Time by Systick */
}
void CAN_Response_Get_FW_Version(CAN_T *tCAN,STR_CANMSG_T *msg){
uint8_t FW_Version[3];
//develop version : 0.0.1
FW_Version[0] = 0;
FW_Version[1] = 0;
FW_Version[2] = 1;
msg->FrameType = CAN_DATA_FRAME;
msg->IdType = CAN_STD_ID;
msg->Id = Device0_ISP_ID;
msg->DLC = 0x4;
msg->Data[0] = Get_FW_Version_Id ;
msg->Data[1] = FW_Version[0];
msg->Data[2] = FW_Version[1];
msg->Data[3] = FW_Version[2];
CAN_Transmit(tCAN, MSG(0), msg);//Send CAN message
CLK_SysTickDelay(10); /* Generate the Delay Time by Systick */
}
void CAN_Set_LED_Steady(CAN_T *tCAN,uint16_t C_H,uint8_t Bitmap,uint8_t steady){
uint8_t CRC;
msg1.FrameType = CAN_DATA_FRAME;
msg1.IdType = CAN_STD_ID;
msg1.Id = Device0_ISP_ID;
msg1.DLC = 0x8;
msg1.Data[0] = 0xFE ;
msg1.Data[1] = 0x8;
msg1.Data[2] = 0x23;
msg1.Data[3] = C_H & 0xFF;
msg1.Data[4] = (C_H>>8) & 0xFF;
msg1.Data[5] = Bitmap;
msg1.Data[6] = steady;
msg1.Data[7] = CRC;
CLK_SysTickDelay(3000); /* Generate the Delay Time by Systick */
CAN_Transmit(tCAN, MSG(0), &msg1);//Send CAN message
}
void CAN_Set_LED_Blink(CAN_T *tCAN,uint16_t C_H,uint8_t Bitmap,uint8_t Interval_T,uint8_t Fadein_T,uint8_t Keep_T,uint8_t Fadeout_T){
uint8_t CRC;
/* Declare a CAN message structure */
msg1.FrameType = CAN_DATA_FRAME;
msg1.IdType = CAN_STD_ID;
msg1.Id = Device0_ISP_ID;
msg1.DLC = 0x8;
msg1.Data[0] = 0xFE ;
msg1.Data[1] = 0xB;
msg1.Data[2] = 0x22;
msg1.Data[3] = C_H & 0xFF;
msg1.Data[4] = (C_H>>8) & 0xFF;
msg1.Data[5] = Bitmap;
msg1.Data[6] = Interval_T;
msg1.Data[7] = Fadein_T;
CLK_SysTickDelay(3000); /* Generate the Delay Time by Systick */
CAN_Transmit(tCAN, MSG(0), &msg1);//Send CAN message
msg1.FrameType = CAN_DATA_FRAME;
msg1.IdType = CAN_STD_ID;
msg1.Id = Device0_ISP_ID;
msg1.DLC = 0x3;
msg1.Data[0] = Keep_T;
msg1.Data[1] = Fadeout_T;
msg1.Data[2] = CRC;
CLK_SysTickDelay(3000); /* Generate the Delay Time by Systick */
CAN_Transmit(tCAN, MSG(0), &msg1);//Send CAN message
}
uint32_t reverse(uint32_t x){
x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
return((x >> 16) | (x << 16));
}
Color_t HSVtoRGB(float H, float S,float V){
if(H>360 || H<0 || S>100 || S<0 || V>100 || V<0){
printf("The givem HSV values are not in valid range\n");
Color.valid = 0;
return Color;
}
float s = S/100;
float v = V/100;
float C = s*v;
float X = C*(1-fabs(fmod(H/60.0, 2)-1));
float m = v-C;
float r,g,b;
if(H >= 0 && H < 60){
r = C,g = X,b = 0;
}
else if(H >= 60 && H < 120){
r = X,g = C,b = 0;
}
else if(H >= 120 && H < 180){
r = 0,g = C,b = X;
}
else if(H >= 180 && H < 240){
r = 0,g = X,b = C;
}
else if(H >= 240 && H < 300){
r = X,g = 0,b = C;
}
else{
r = C,g = 0,b = X;
}
int R = (r+m)*255;
int G = (g+m)*255;
int B = (b+m)*255;
printf("R : %d",R);
printf("G : %d",G);
printf("B : %d",B);
Color.R = R;
Color.G = G;
Color.B = B;
Color.valid = 1;
return Color;
}