在线时间84 小时
UID2051025
注册时间2015-7-1
NXP金币0
TA的每日心情 | 开心 2020-4-9 16:46 |
---|
签到天数: 260 天 连续签到: 1 天 [LV.8]以坛为家I
高级会员

- 积分
- 980
- 最后登录
- 2020-4-9
|

楼主 |
发表于 2018-9-11 10:27:33
|
显示全部楼层
初始化:
- MSCAN_ParametersConfig CAN_Module_Config;
- MSCAN_FilterConfigure CAN_Filter_Config;
- CAN_Module_Config.baudrate = MSCAN_Baudrate_250K; /* Baud rate 250Kbps */
- CAN_Module_Config.MSCAN_SignalPinsRemap = 0; /* MSCAN signal pins remap on PTC7(TX) and PTC6(RX) */
- CAN_Module_Config.MSCAN_StopInWaitMode = 1; /* MSCAN module is normal in wait mode */
- CAN_Module_Config.MSCAN_TimeStampEnable = 0; /* Disable internal MSCAN timer stamp */
- CAN_Module_Config.MSCAN_WakeUpEnable = 0; /* Wake-up disabled */
- CAN_Module_Config.MSCAN_LoopbackMode = 0; /* Loop back self test disabled */
- CAN_Module_Config.MSCAN_ListenOnlyMode = 0; /* Normal operation */
- CAN_Module_Config.MSCAN_BusoffRecoveryMode = 0; /* Automatic bus-off recovery */
- CAN_Module_Config.MSCAN_WakeUpMode = 0; /* Wakes up on any dominant level on CAN bus */
- CAN_Module_Config.MSCAN_WakeUpINTEnable = 0; /* Wake-up interrupt disable */
- CAN_Module_Config.MSCAN_OverrunINTEnable = 0; /* Overrun interrupt disable */
- CAN_Module_Config.MSCAN_ReceiveFullINTEnable = 1; /* Receive full interrupt enable */
- CAN_Module_Config.MSCAN_TransEmptyINTEnable = 0; /* Transmitter empty interrupt disable */
-
- CAN_Filter_Config.Filter_Enable = 1; /* Enable CAN module filters. If this value is cleared,the following parameters does not work */
- CAN_Filter_Config.Filter_Channel = 0; /* Select CAN module filter channel 0 */
- CAN_Filter_Config.frame_type = OnlyAcceptDataFrame; /* Enable CAN filter just receive data frame */
- CAN_Filter_Config.id_format = OnlyAcceptExtendedID; /* Enable CAN filter just receive extended ID */
- CAN_Filter_Config.id_mask = 0xFFFFFFFFu; /* When this value is set to 0xFFFFFFFFu,the id_accept value does not work */
- CAN_Filter_Config.id_accept = 0x18901212u; /* Because the id_mask value has been set to 0xFFFFFFFFu,id_accept does not work and can be set to any value */
-
- (void)MSCAN_Init(&CAN_Module_Config, &CAN_Filter_Config); /* Call MSCAN_Init function to initialize MSCAN module by specified properties */
-
- Set_Vector_Handler(MSCAN_RX_VECTORn, MSCAN_RX_Handler); /* Specify MSCAN receive data irq handler function name */
-
- NVIC_EnableIRQ(MSCAN_RX_IRQn); /* Enable MSCAN receive data NVIC interrupt */
-
- NVIC_SetPriority(MSCAN_RX_IRQn, 0); /* Set MSCAN_RX interrupt priority to 0(The highest). */
复制代码
发送:
- uint8_t reg_val, i;
- uint8_t temp1, temp2, temp3;
-
- if (W_Framebuff != NULL)
- {
- /* Ensure the length of the data not to be greater than 8 bytes. */
- if (W_Framebuff->data_length > 8)
- {
- return -1;
- }
- /* Ensure there is at least one Empty Transmission Buffer. */
- if ((reg_val = (MSCAN->CANTFLG & MSCAN_CANTFLG_TXE_MASK)) == 0)
- {
- return -1;
- }
- /*
- * According to the KEAZ128RM, select the Empty Transmission Buffer
- * by passing the value of CANx->CANTFLG to CANx->CANTBSEL. NOTICE,
- * after doing this, the value of CANx->CANTBSEL will be the index
- * of the Empty Transmission Buffer been selected.
- */
- MSCAN->CANTBSEL = reg_val;
- if ((W_Framebuff->frame_type == RemoteFrameWithStandardId)
- || (W_Framebuff->frame_type == DataFrameWithStandardId))
- {
- /* indicate the frame is standard format */
- MSCAN->TSIDR1 &= ~MSCAN_TSIDR1_TSIDE_MASK;
- /* If caller wants to send a packet with standard id, firstly
- * ensure the id not to be exceed 11 bits.
- */
- if (W_Framebuff->frame_id > 0x7FFu)
- {
- return -1;
- }
- /* Load the id into the respective registers. */
- MSCAN->TSIDR0 = (uint8_t)(W_Framebuff->frame_id >> 3);
- MSCAN->TSIDR1 = (uint8_t)(W_Framebuff->frame_id << 5);
- if (W_Framebuff->frame_type == RemoteFrameWithStandardId)
- {
- /*
- * If caller wants to send a remote packet, set the
- * respective bit in CANx->SIDR1, and set the TDLR
- * to be 0 (the remote packet has no data).
- */
- MSCAN->TSIDR1 |= MSCAN_TSIDR1_TSRTR_MASK;
- /* The data length should be 0 */
- MSCAN->TDLR = 0;
- }
- else
- {
- MSCAN->TSIDR1 &= ~MSCAN_TSIDR1_TSRTR_MASK;
- /*
- * If caller wants to send a data packet, set the
- * TDLR to be equal to the actual data length
- */
- MSCAN->TDLR = W_Framebuff->data_length;
- /* Load the actual data to the Transmit Extended Data Segment Register */
- for (i = 0; i < W_Framebuff->data_length; i++)
- {
- MSCAN->TEDSR[i] = W_Framebuff->data[i];
- }
- }
- }
- else if ((W_Framebuff->frame_type == RemoteFrameWithExtendedId)
- || (W_Framebuff->frame_type == DataFrameWithExtendedId))
- {
- /* When caller wants to send a frame with ExtendedID,
- * the TEIDE bit of MSCAN_TEIDR1 register should be set to 1 */
- MSCAN->TEIDR1 = MSCAN_TEIDR1_TEIDE_MASK;
- /* If caller wants to send a packet with standard id, firstly
- * ensure the id not to be exceed 29 bits.
- */
- if (W_Framebuff->frame_id > 0x1FFFFFFF)
- {
- return -1;
- }
- MSCAN->TEIDR0 = (uint8_t)((W_Framebuff->frame_id) >> 21);
- temp1 = (uint8_t)((W_Framebuff->frame_id) >> 18) & 0x07;
- temp2 = (uint8_t)((W_Framebuff->frame_id) >> 15) & 0x07;
- /* Attention:The TSRR bit is used only in extended format.
- * It must be set to 1 by the user for transmission buffers
- */
- MSCAN->TEIDR1 |= (temp1 << 5) | temp2 | MSCAN_TEIDR1_TSRR_MASK;
- MSCAN->TEIDR2 = (uint8_t)((W_Framebuff->frame_id) >> 7);
- temp3 = (uint8_t)W_Framebuff->frame_id & 0x7Fu;
- MSCAN->TEIDR3 = MSCAN_TEIDR3_TEID6_TEID0(temp3);
- if (W_Framebuff->frame_type == RemoteFrameWithExtendedId)
- {
- MSCAN->TEIDR3 |= MSCAN_TEIDR3_TERTR_MASK;
- MSCAN->TDLR = 0;
- }
- else
- {
- MSCAN->TEIDR3 &= ~MSCAN_TEIDR3_TERTR_MASK;
- MSCAN->TDLR = W_Framebuff->data_length;
- for (i = 0; i < W_Framebuff->data_length; i++)
- {
- MSCAN->TEDSR[i] = W_Framebuff->data[i];
- }
- }
- }
- else
- {
- return -1;
- }
- /*
- * The purpose is to clear the respective bit,
- * according to the KEAZ128RM, it should be done
- * by writing 1 but not 0.
- */
- MSCAN->CANTFLG = MSCAN->CANTBSEL;
- return 0;
- }
-
- return -1;
复制代码 |
|