void can_init(can_t *obj, PinName rd, PinName td) { uint32_t can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); uint32_t can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); obj->can = (CANName)pinmap_merge(can_td, can_rd); MBED_ASSERT((int)obj->can != NC); const struct nu_modinit_s *modinit = get_modinit(obj->can, can_modinit_tab); MBED_ASSERT(modinit != NULL); MBED_ASSERT(modinit->modname == obj->can); // Reset this module SYS_ResetModule(modinit->rsetidx); // Enable IP clock CLK_EnableModuleClock(modinit->clkidx); obj->index = 0; pinmap_pinout(td, PinMap_CAN_TD); pinmap_pinout(rd, PinMap_CAN_RD); /* For M453 mbed Board Transmitter Setting (RS Pin) */ GPIO_SetMode(PA, BIT0| BIT1, GPIO_MODE_OUTPUT); PA0 = 0x00; PA1 = 0x00; CAN_Open((CAN_T *)NU_MODBASE(obj->can), 500000, CAN_NORMAL_MODE); can_filter(obj, 0, 0, CANStandard, 0); }
void can_init(can_t *obj, PinName rd, PinName td) { CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); obj->can = (CANName)pinmap_merge(can_rd, can_td); MBED_ASSERT((int)obj->can != NC); if (obj->can == CAN_1) { __HAL_RCC_CAN1_CLK_ENABLE(); obj->index = 0; } #if defined(CAN2_BASE) && (CAN_NUM == 2) else if (obj->can == CAN_2) { __HAL_RCC_CAN1_CLK_ENABLE(); // needed to set filters __HAL_RCC_CAN2_CLK_ENABLE(); obj->index = 1; } #endif else { return; } // Configure the CAN pins pinmap_pinout(rd, PinMap_CAN_RD); pinmap_pinout(td, PinMap_CAN_TD); if (rd != NC) { pin_mode(rd, PullUp); } if (td != NC) { pin_mode(td, PullUp); } CanHandle.Instance = (CAN_TypeDef *)(obj->can); CanHandle.Init.TTCM = DISABLE; CanHandle.Init.ABOM = DISABLE; CanHandle.Init.AWUM = DISABLE; CanHandle.Init.NART = DISABLE; CanHandle.Init.RFLM = DISABLE; CanHandle.Init.TXFP = DISABLE; CanHandle.Init.Mode = CAN_MODE_NORMAL; CanHandle.Init.SJW = CAN_SJW_1TQ; CanHandle.Init.BS1 = CAN_BS1_6TQ; CanHandle.Init.BS2 = CAN_BS2_8TQ; CanHandle.Init.Prescaler = 2; if (HAL_CAN_Init(&CanHandle) != HAL_OK) { error("Cannot initialize CAN"); } // Set initial CAN frequency to 100 kb/s can_frequency(obj, 100000); uint32_t filter_number = (obj->can == CAN_1) ? 0 : 14; can_filter(obj, 0, 0, CANStandard, filter_number); }
/* Calls the initialization routines */ static inline void init(void) { uint8_t cnf1=0x03, cnf2=0xb6, cnf3=0x04; /* Defaults to 125k */ uint8_t can_speed = 0; init_spi(); /* Set the direction for the status LEDs' */ DDRB |= (1<<PB0); DDRB |= (1<<PB1); /* Set the CAN speed. The values for 125k are the defaults so 0 is ignored and bad values also result in 125k */ can_speed = eeprom_read_byte(EE_CAN_SPEED); if(can_speed==BITRATE_250) cnf1=0x01; /* 250kbps */ else if(can_speed==BITRATE_500) cnf1=0x00; /* 500kbps */ else if(can_speed==BITRATE_1000) { cnf1=0x00; cnf2=0x92; cnf3=0x02; } /* 1Mbps */ node_id = eeprom_read_byte(EE_NODE_ID); /* Initialize the MCP2515 */ can_init(cnf1, cnf2, cnf3, 0x00); /* Set the masks and filters to listen for Node Specific Messages on RX 0. We put the node specific messages in RX0 and the two way communication channels in RX1 */ can_mode(CAN_MODE_CONFIG, 0); can_mask(0, 0x0700); /* This sets the filter to get a firmware update command to our node address. For the bootloader this is all we care about. */ can_filter(CAN_RXF0SIDH, 0x0700); can_mask(1, 0x07C0); can_filter(CAN_RXF2SIDH, 0x06E0); can_mode(CAN_MODE_NORMAL, 0); #ifdef UART_DEBUG init_serial(); #endif TCCR1B=0x05; /* Set Timer/Counter 1 to clk/1024 */ /* Move the Interrupt Vector table to the Bootloader section */ MCUCR = (1<<IVCE); MCUCR = (1<<IVSEL); EICRA = 0x02; /* Set INT0 to falling edge */ }
/** init the CAN frequency. * * @param rd receive pin. * @param td transmit pin. * @param hz The bus frequency in hertz. */ void can_init_freq(can_t *obj, PinName rd, PinName td, int hz) { CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); obj->can = (CANName)pinmap_merge(can_rd, can_td); MBED_ASSERT((int)obj->can != NC); if (obj->can == CAN_0) { rcu_periph_clock_enable(RCU_CAN0); can_deinit(obj->can); obj->index = 0; } else if (obj->can == CAN_1) { rcu_periph_clock_enable(RCU_CAN0); rcu_periph_clock_enable(RCU_CAN1); can_deinit(obj->can); obj->index = 1; } else { return; } /* Configure the CAN pins */ pinmap_pinout(rd, PinMap_CAN_RD); pinmap_pinout(td, PinMap_CAN_TD); if (rd != NC) { pin_mode(rd, PullUp); } if (td != NC) { pin_mode(td, PullUp); } dev_can_mode_config(obj->can, CAN_NORMAL_MODE); can_frequency(obj, hz); if (obj->can == CAN_0) { can_filter(obj, 0, 0, CANStandard, 0); } else { can_filter(obj, 0, 0, CANStandard, 14); } }
int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) { lock(); int ret = can_filter(&_can, id, mask, format, handle); unlock(); return ret; }
void can_init_freq (can_t *obj, PinName rd, PinName td, int hz) { CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); CANName can = (CANName)pinmap_merge(can_rd, can_td); MBED_ASSERT((int)can != NC); if (can == CAN_1) { __HAL_RCC_CAN1_CLK_ENABLE(); obj->index = 0; } #if defined(CAN2_BASE) && (CAN_NUM > 1) else if (can == CAN_2) { __HAL_RCC_CAN1_CLK_ENABLE(); // needed to set filters __HAL_RCC_CAN2_CLK_ENABLE(); obj->index = 1; } #endif #if defined(CAN3_BASE) && (CAN_NUM > 2) else if (can == CAN_3) { __HAL_RCC_CAN3_CLK_ENABLE(); obj->index = 2; } #endif else { return; } // Configure the CAN pins pinmap_pinout(rd, PinMap_CAN_RD); pinmap_pinout(td, PinMap_CAN_TD); if (rd != NC) { pin_mode(rd, PullUp); } if (td != NC) { pin_mode(td, PullUp); } /* Use default values for rist init */ obj->CanHandle.Instance = (CAN_TypeDef *)can; obj->CanHandle.Init.TTCM = DISABLE; obj->CanHandle.Init.ABOM = DISABLE; obj->CanHandle.Init.AWUM = DISABLE; obj->CanHandle.Init.NART = DISABLE; obj->CanHandle.Init.RFLM = DISABLE; obj->CanHandle.Init.TXFP = DISABLE; obj->CanHandle.Init.Mode = CAN_MODE_NORMAL; obj->CanHandle.Init.SJW = CAN_SJW_1TQ; obj->CanHandle.Init.BS1 = CAN_BS1_6TQ; obj->CanHandle.Init.BS2 = CAN_BS2_8TQ; obj->CanHandle.Init.Prescaler = 2; /* Store frequency to be restored in case of reset */ obj->hz = hz; can_registers_init(obj); /* Bits 27:14 are available for dual CAN configuration and are reserved for single CAN configuration: */ #if defined(CAN3_BASE) && (CAN_NUM > 2) uint32_t filter_number = (can == CAN_1 || can == CAN_3) ? 0 : 14; #else uint32_t filter_number = (can == CAN_1) ? 0 : 14; #endif can_filter(obj, 0, 0, CANStandard, filter_number); }
int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) { return can_filter(&_can, id, mask, format, handle); }