int can_init(can_speed_t can_speed, can_send_callback_t send_callback, can_recv_callback_t recv_callback, uint32_t send_timeout_real_us, can_mask_t masked_id, can_mask_t mask) { if((send_callback == NULL) || (recv_callback == NULL)) { return 1; } cli(); can_send_callback = send_callback; can_recv_callback = recv_callback; can_send_timeout_ms = send_timeout_real_us / 1000; can_masked_id = masked_id; can_mask = mask; if(can_set_speed(can_speed) == -1) return -1; list_init(can_cmd_list); can_clear_all_mob(); CAN_ENABLE(); CAN_SET_TCON(199); // FIXME for F_CPU = 16 Mhz -> CAN timestamp will be 100uS resolution CAN_INT_MOB_ENABLE(); CAN_INT_ENABLE(); sei(); if(init_recv_buffers() == -1) { return -2; } return 0; }
void USB2CAN_open(u32 speed,u32 flags,CAN_TIMINGS *CanTimings) { CAN_ENABLE(); USB2CAN_set_speed(speed,CanTimings); switch( flags & 0x0003 ) { case CAN_MODE_NORMAL: // normal mode CAN_LeaveTestMode(); break; case CAN_MODE_SILENT: // silent mode CAN_EnterTestMode( CAN_TESTR_SILENT ); break; case CAN_MODE_LOOPBACK: // loopback mode CAN_EnterTestMode( CAN_TESTR_LBACK ); break; case CAN_MODE_SILENT_LOOPBACK: CAN_EnterTestMode( CAN_TESTR_LBACK | CAN_TESTR_SILENT ); break; default: CAN_LeaveTestMode(); break; } // DAR if( flags & 0x0004 ) CAN->CR |= CAN_CR_DAR; // disable auto retransmitions else CAN->CR &= ~CAN_CR_DAR; // enable auto retransmitions }
int can_reset(unsigned long int afcpu, uint32_t bps) { /* Configure CAN module */ CAN_DISABLE(); CAN_RESET(); /* Enables mob 0-14 interrupts */ CANIE1 = 0x7F; CANIE2 = 0xFF; /* Configure bitrate */ can_configure_bitrate(afcpu, bps); /* Configure MOBS */ can_configure_mobs(); /* Enable and return */ CAN_ENABLE(); CAN_SET_INTERRUPT(); return 0; }