1. SDK下打开system.mss文件,如下图所示,import examples导入polled相关例程。
因为我们的设备上有两个can,一个ps端一个pl端的,原理图在can收发器侧一样。
2. ps 端参调试
确定逻辑对ps can的clock配置值,为100M,参考《ug585-Zynq-7000-TRM.pdf》手册,计算can波特率。
波特率=100M/ ((49 +1)*(3 +15 +2)) = 100k
int CanPsPolledExample(u16 DeviceId) { int Status; XCanPs *CanInstPtr = &Can; XCanPs_Config *ConfigPtr; /* * Initialize the Can device. */ ConfigPtr = XCanPs_LookupConfig(DeviceId); if (CanInstPtr == NULL) { return XST_FAILURE; } Status = XCanPs_CfgInitialize(CanInstPtr, ConfigPtr, ConfigPtr->BaseAddr); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Run self-test on the device, which verifies basic sanity of the * device and the driver. */ Status = XCanPs_SelfTest(CanInstPtr); if (Status != XST_SUCCESS) { return XST_FAILURE; } xil_printf("SelfTest successfuly \r\n\r\n"); /* * Enter Configuration Mode so we can setup Baud Rate Prescaler * Register (BRPR) and Bit Timing Register (BTR). */ XCanPs_EnterMode(CanInstPtr, XCANPS_MODE_CONFIG); while(XCanPs_GetMode(CanInstPtr) != XCANPS_MODE_CONFIG); /* * Setup Baud Rate Prescaler Register (BRPR) and * Bit Timing Register (BTR). ps_can_clock = 100M . */ XCanPs_SetBaudRatePrescaler(CanInstPtr, /*TEST_BRPR_BAUD_PRESCALAR*/ 49); XCanPs_SetBitTiming(CanInstPtr, TEST_BTR_SYNCJUMPWIDTH, TEST_BTR_SECOND_TIMESEGMENT, TEST_BTR_FIRST_TIMESEGMENT); /* * Enter Loop Back Mode. */ #define ps_can_clock 100000000 //100M XCanPs_EnterMode(CanInstPtr, XCANPS_MODE_SLEEP); while(XCanPs_GetMode(CanInstPtr) != XCANPS_MODE_SLEEP); xil_printf("ps can bitRate: %lu bps\r\n", ps_can_clock/((49+1)* (3+15+2))); /* * Send a frame, receive the frame via the loop back and verify its * contents. */ Status = SendFrame(CanInstPtr); if (Status != XST_SUCCESS) { return Status; } xil_printf("recving ....... \r\n"); while(1){ Status = RecvFrame(CanInstPtr); } return Status; }
can消息格式:有效负载为DWR, DW2R
确定发送id
2. 使用宇泰的 8251 usb can设备,按照说明书设置好can卡,波特率为100k.
sdk启动debug,上来就会发送一帧数据个给外部can设备,使用宇泰can上位机给板子发数据,板子侧添加收数据打印信息。
3. pl 侧can和ps测试过程一样,需要注意can clock是多少,去计算can波特率