/********************************************************************************************************************* ** Function name: can1_setup ** Descriptions: 安装can1设备 ** Input parameters: Baudrate ** Output parameters: ** Returned value: ==OS_OK : 操作成功 ** !=OS_OK : 操作失败(包含出错信息) **-------------------------------------------------------------------------------------------------------------------- ** Created by: Feng Liang ** Created Date: 2011-12-13 17:13:54 ** Test recorde: **-------------------------------------------------------------------------------------------------------------------- ** Modified by: ** Modified date: ** Test recorde: *********************************************************************************************************************/ static int can1_setup(void) { /* 1) * 私有构建过程 */ dword_set_bits(PCONP, 1ul << 14); /* 使能can1外设备供电 */ #if 1 /* * CAN控制器收发引脚配置 * 引脚p0.4: RD2; * 引脚p0.5: TD2 */ dword_modify(PINSEL0, (1ul<<10) | (1ul<<8), /* 设置P0.4功能: RD2 */ (1ul<<11) | (1ul<<9)); /* 设置P0.5功能: TD2 */ #else /* * CAN控制器收发引脚配置 * 引脚p2.7: RD2; * 引脚p2.8: TD2 */ dword_modify(PINSEL4, (1ul<<17) | (1ul<<15), /* 设置P2.7功能: RD2 */ (1ul<<16) | (1ul<<14)); /* 设置P2.8功能: TD2 */ #endif /* 2) * 类构建过程 */ can_setup(&can1_feature); return OK; }
/** * CAN test main function * * @return Nothing really... */ int main(void) { int timer; uint8_t data[1]; mcu_init(); led_init(); sys_tick_init(); can_setup(); timer = sys_tick_get_timer(); data[0] = 10; while (true) { if (sys_tick_check_timer(timer, 10000)) { data[0]++; timer = sys_tick_get_timer(); #ifdef CAN__SEND #ifdef CAN_ADDR can_trans(CAN_ADDR, data, 1); #else can_trans(CAN__DEFAULT_ADDR, data, 1); #endif #endif } } }
int main(void) { rcc_clock_setup_in_hse_12mhz_out_72mhz(); gpio_setup(); can_setup(); systick_setup(); while (1); /* Halt. */ return 0; }
int main(void) { status = 0; rcc_clock_setup_in_hse_8mhz_out_72mhz(); gpio_setup(); can_setup(); systick_setup(); /* endless loop */ while (1) ; return 0; }
int main(void) { set_canit_callback(CANIT_RX_COMPLETED, rx_complete); set_canit_callback(CANIT_TX_COMPLETED, tx_complete); set_canit_callback(CANIT_DEFAULT, can_default); usart1_init(); CAN_INIT_ALL(); //Can setup sei(); //Enable interrupt usart1_printf("\n\n\nSTARTING\n"); // Set MOB 8 to listen for messeges with id 4 and length 7 can_msg_t rx_msg = { .mob = 8, .id = 4, .dlc = 7, .mode = MOB_RECIEVE }; can_setup(&rx_msg); usart1_printf("Listning for id %d on mob %d with a msg length %d\n", rx_msg.id, rx_msg.mob, rx_msg.dlc ); while(1){ // Main work loop _delay_ms(250); // send a message with id 4 on MOB 10 can_msg_t tx_msg = { .mob = 10, .id = 4, .data = {'H', 'E', 'L', 'L', 'O', '!', '!'}, .dlc = 7, .mode = MOB_TRANSMIT }; can_send(&tx_msg); usart1_printf("CAN Tx\t id %d, mob %d, :: ", tx_msg.id, tx_msg.mob); // As tx_msg.data is a byte array we cant treat it as a string usart1_putn(sizeof(tx_msg.data)/sizeof(tx_msg.data[0]), tx_msg.data); usart1_putc('\n'); } return 0; } // Callback to be run when rx comletes on the CAN static void rx_complete(uint8_t mob) { can_msg_t msg = { .mob = mob // mob is the MOB that fired the interrupt }; can_receive(&msg); // Fetch the message and fill out the msg struct // Print out the received data. Please dont print inside can callbacks // in real code as these are run inside the can ISR usart1_printf("CAN Rx\t id: %d on mob %d :: ", msg.id, msg.mob); usart1_putn(msg.dlc, msg.data); usart1_putc('\n'); } static void tx_complete(uint8_t mob) { // We clear the mob so it can be used again MOB_ABORT(); // Freed the MOB MOB_CLEAR_INT_STATUS(); // and reset MOB status CAN_DISABLE_MOB_INTERRUPT(mob); // Unset interrupt }