void CAN1Init(void) { CAN_BIT_CONFIG canBitConfig; UINT baudPrescalar; CANEnableModule(CAN1, TRUE); CANSetOperatingMode(CAN1, CAN_CONFIGURATION); while(CANGetOperatingMode(CAN1) != CAN_CONFIGURATION); // Standard Values canBitConfig.phaseSeg2Tq = CAN_BIT_3TQ; canBitConfig.phaseSeg1Tq = CAN_BIT_5TQ; canBitConfig.propagationSegTq = CAN_BIT_1TQ; canBitConfig.phaseSeg2TimeSelect = TRUE; canBitConfig.sample3Time = TRUE; canBitConfig.syncJumpWidth = CAN_BIT_1TQ; // Set speed to 1Mbit/s CANSetSpeed(CAN1, &canBitConfig, SYSTEM_FREQ, CAN_BUS_SPEED); // 256 bytes of storage space CANAssignMemoryBuffer(CAN1, CAN1MessageFifoArea, (2*8*16)); CANConfigureChannelForTx(CAN1, CAN_CHANNEL0, 8, CAN_TX_RTR_DISABLED, CAN_LOW_MEDIUM_PRIORITY); CANConfigureChannelForRx(CAN1, CAN_CHANNEL1, 8, CAN_RX_FULL_RECEIVE); CANConfigureFilter(CAN1, CAN_FILTER0, 0x5F1, CAN_SID); CANConfigureFilterMask(CAN1, CAN_FILTER_MASK0, 0x7FF, CAN_SID, CAN_FILTER_MASK_IDE_TYPE); CANLinkFilterToChannel(CAN1, CAN_FILTER0, CAN_FILTER_MASK0, CAN_CHANNEL1); CANEnableFilter(CAN1, CAN_FILTER0, TRUE); CANEnableChannelEvent(CAN1, CAN_CHANNEL1, CAN_RX_CHANNEL_NOT_EMPTY, TRUE); CANEnableModuleEvent(CAN1, CAN_RX_EVENT, TRUE); INTSetVectorPriority(INT_CAN_1_VECTOR, INT_PRIORITY_LEVEL_4); INTSetVectorSubPriority(INT_CAN_1_VECTOR, INT_SUB_PRIORITY_LEVEL_0); INTEnable(INT_CAN1, INT_ENABLED); CANSetOperatingMode(CAN1, CAN_NORMAL_OPERATION); while(CANGetOperatingMode(CAN1) != CAN_NORMAL_OPERATION); }
MUST_CHECK s32 nu__Can__add_filter(const struct nu__Can *c, CAN_CHANNEL chn, CAN_FILTER filter, CAN_ID_TYPE filter_type, u32 id, CAN_FILTER_MASK mask, CAN_FILTER_MASK_TYPE mide, u32 mask_bits) { s32 err; if ((err = go_config_mode(c)) < 0) { go_normal_mode(c); return err; } CANConfigureFilter(c->module, filter, id, filter_type); CANConfigureFilterMask(c->module, mask, mask_bits, filter_type, mide); CANLinkFilterToChannel(c->module, filter, mask, chn); CANEnableFilter(c->module, filter, TRUE); if ((err = go_normal_mode(c)) < 0) return err; return 0; }
s32 nu__Can__add_channel_rx(const struct nu__Can *c, CAN_CHANNEL chn, u32 channel_msg_size, CAN_RX_DATA_MODE data_only, CAN_CHANNEL_EVENT interrupt_events) { s32 err; if ((err = go_config_mode(c)) < 0) { go_normal_mode(c); return err; } CANConfigureChannelForRx(c->module, chn, channel_msg_size, data_only); CANConfigureFilter(c->module, CAN_FILTER0, 0, CAN_SID); CANConfigureFilterMask(c->module, CAN_FILTER_MASK0, 0, CAN_SID, CAN_FILTER_MASK_ANY_TYPE); CANLinkFilterToChannel(c->module, CAN_FILTER0, CAN_FILTER_MASK0, CAN_CHANNEL1); CANEnableFilter(c->module, CAN_FILTER0, TRUE); CANEnableChannelEvent(c->module, chn, interrupt_events, TRUE); if ((err = go_normal_mode(c)) < 0) return err; return 0; }
void CAN1Init(void) { CAN_BIT_CONFIG canBitConfig; /* This function will intialize * CAN1 module. */ /* Step 1: Switch the CAN module * ON and switch it to Configuration * mode. Wait till the switch is * complete */ CANEnableModule(CAN1,TRUE); CANSetOperatingMode(CAN1, CAN_CONFIGURATION); while(CANGetOperatingMode(CAN1) != CAN_CONFIGURATION); /* Step 2: Configure the CAN Module Clock. The * CAN_BIT_CONFIG data structure is used * for this purpose. The propagation segment, * phase segment 1 and phase segment 2 * are configured to have 3TQ. The * CANSetSpeed function sets the baud.*/ canBitConfig.phaseSeg2Tq = CAN_BIT_3TQ; canBitConfig.phaseSeg1Tq = CAN_BIT_3TQ; canBitConfig.propagationSegTq = CAN_BIT_3TQ; canBitConfig.phaseSeg2TimeSelect = TRUE; canBitConfig.sample3Time = TRUE; canBitConfig.syncJumpWidth = CAN_BIT_2TQ; CANSetSpeed(CAN1,&canBitConfig,SYSTEM_FREQ,CAN_BUS_SPEED); /* Step 3: Assign the buffer area to the * CAN module. */ CANAssignMemoryBuffer(CAN1,CAN1MessageFifoArea,(2 * 8 * 16)); /* Step 4: Configure channel 0 for TX and size of * 8 message buffers with RTR disabled and low medium * priority. Configure channel 1 for RX and size * of 8 message buffers and receive the full message. */ CANConfigureChannelForTx(CAN1, CAN_CHANNEL0, 8, CAN_TX_RTR_DISABLED, CAN_LOW_MEDIUM_PRIORITY); CANConfigureChannelForRx(CAN1, CAN_CHANNEL1, 8, CAN_RX_FULL_RECEIVE); /* Step 5: Configure filters and mask. Configure * filter 0 to accept EID messages with ID 0x0. * Configure filter mask 0 to compare none of the ID * bits and to filter by the ID type specified in * the filter configuration. Messages accepted by * filter 0 should be stored in channel 1. */ //CANConfigureFilter (CAN1, CAN_FILTER0, 0x201, CAN_SID); CANConfigureFilter(CAN1, CAN_FILTER0, 0x0, CAN_EID); //The following Filter mask allows the program to listen for all CAN_EIDs change 0x0 //(the third parameter in CANConfigureFilterMask) to compare bits to mask CANConfigureFilterMask (CAN1, CAN_FILTER_MASK0, 0x0, CAN_EID, CAN_FILTER_MASK_IDE_TYPE); CANLinkFilterToChannel (CAN1, CAN_FILTER0, CAN_FILTER_MASK0, CAN_CHANNEL1); CANEnableFilter (CAN1, CAN_FILTER0, TRUE); /* Step 6: Enable interrupt and events. Enable the receive * channel not empty event (channel event) and the receive * channel event (module event). * The interrrupt peripheral library is used to enable * the CAN interrupt to the CPU. */ CANEnableChannelEvent(CAN1, CAN_CHANNEL1, CAN_RX_CHANNEL_NOT_EMPTY, TRUE); CANEnableModuleEvent (CAN1, CAN_RX_EVENT, TRUE); /* These functions are from interrupt peripheral * library. */ INTSetVectorPriority(INT_CAN_1_VECTOR, INT_PRIORITY_LEVEL_4); INTSetVectorSubPriority(INT_CAN_1_VECTOR, INT_SUB_PRIORITY_LEVEL_0); INTEnable(INT_CAN1, INT_ENABLED); /* Step 7: Switch the CAN mode * to normal mode. */ CANSetOperatingMode(CAN1, CAN_NORMAL_OPERATION); while(CANGetOperatingMode(CAN1) != CAN_NORMAL_OPERATION); }