// Initialize CAN port void CAN0_Open(void){ uint32_t volatile delay; int32_t sr; sr = StartCritical(); MailFlag = false; SYSCTL_RCGCCAN_R |= 0x00000001; // CAN0 enable bit 0 SYSCTL_RCGCGPIO_R |= 0x00000010; // RCGC2 portE bit 4 for(delay=0; delay<10; delay++){}; GPIO_PORTE_AFSEL_R |= 0x30; //PORTE AFSEL bits 5,4 // PORTE PCTL 88 into fields for pins 5,4 GPIO_PORTE_PCTL_R = (GPIO_PORTE_PCTL_R&0xFF00FFFF)|0x00880000; GPIO_PORTE_DEN_R |= 0x30; GPIO_PORTE_DIR_R |= 0x20; CANInit(CAN0_BASE); CANBitRateSet(CAN0_BASE, 80000000, CAN_BITRATE); CANEnable(CAN0_BASE); // make sure to enable STATUS interrupts CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS); // Set up filter to receive these IDs // in this case there is just one type, but you could accept multiple ID types CAN0_Setup_Message_Object(Ping1_ID, MSG_OBJ_RX_INT_ENABLE, 4, NULL, Ping1_ID, MSG_OBJ_TYPE_RX); CAN0_Setup_Message_Object(Ping2_ID, MSG_OBJ_RX_INT_ENABLE, 4, NULL, Ping2_ID, MSG_OBJ_TYPE_RX); CAN0_Setup_Message_Object(Ping3_ID, MSG_OBJ_RX_INT_ENABLE, 4, NULL, Ping3_ID, MSG_OBJ_TYPE_RX); CAN0_Setup_Message_Object(Ping4_ID, MSG_OBJ_RX_INT_ENABLE, 4, NULL, Ping4_ID, MSG_OBJ_TYPE_RX); CAN0_Setup_Message_Object(Button1_ID, MSG_OBJ_RX_INT_ENABLE, 4, NULL, Button1_ID, MSG_OBJ_TYPE_RX); CAN0_Setup_Message_Object(Button2_ID, MSG_OBJ_RX_INT_ENABLE, 4, NULL, Button2_ID, MSG_OBJ_TYPE_RX); CAN0_Setup_Message_Object(IR_ID, MSG_OBJ_RX_INT_ENABLE, 4, NULL, IR_ID, MSG_OBJ_TYPE_RX); NVIC_EN1_R = (1 << (INT_CAN0 - 48)); //IntEnable(INT_CAN0); EndCritical(sr); return; }
// Initialize CAN port void CAN0_Open(void){uint32_t volatile delay; CAN0_Fifo_Init(16); OS_InitSemaphore(&CAN0ReceiveFree, 1); PacketLost = 0; MailFlag = false; SYSCTL_RCGCCAN_R |= 0x00000001; // CAN0 enable bit 0 SYSCTL_RCGCGPIO_R |= 0x00000010; // RCGC2 portE bit 4 for(delay=0; delay<10; delay++){}; GPIO_PORTE_AFSEL_R |= 0x30; //PORTE AFSEL bits 5,4 // PORTE PCTL 88 into fields for pins 5,4 GPIO_PORTE_PCTL_R = (GPIO_PORTE_PCTL_R&0xFF00FFFF)|0x00880000; GPIO_PORTE_DEN_R |= 0x30; GPIO_PORTE_DIR_R |= 0x20; CANInit(CAN0_BASE); CANBitRateSet(CAN0_BASE, 80000000, CAN_BITRATE); CANEnable(CAN0_BASE); // make sure to enable STATUS interrupts CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS); // Set up filter to receive these IDs // in this case there is just one type, but you could accept multiple ID types CAN0_Setup_Message_Object(RCV_ID, MSG_OBJ_RX_INT_ENABLE, 8, NULL, RCV_ID, MSG_OBJ_TYPE_RX); NVIC_EN1_R = (1 << (INT_CAN0 - 48)); //IntEnable(INT_CAN0); return; }
void BCAN_enable(void) { CAN_Data_lost = 0; CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS); IntEnable(INT_CAN0); CANEnable(CAN0_BASE); }
void NetworkInit(void) { // configure GPIO pins for CAN SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1); // set up the CAN controller SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); CANInit(CAN0_BASE); CANBitRateSet(CAN0_BASE, 8000000, CAN_BIT_RATE); CANEnable(CAN0_BASE); CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS); NetworkMsgInit(); // volatile int i; // CAN0_CTL_R; // for (i=0;i<8;i++); // CAN0_CTL_R |= CAN_CTL_TEST; // enable CAN test mode // for (i=0;i<8;i++); // CAN0_TST_R; // for (i=0;i<8;i++); // CAN0_TST_R |= CAN_TST_LBACK; // enable loopback mode // removed because SYS/BIOS configures this // IntPrioritySet(INT_CAN0, 96); // IntEnable(INT_CAN0); }
void setup(){ Serial.begin(9600); Serial.println("DEBUG1"); SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); SysCtlPeripheralEnable(GPIO_PORTE_BASE); GPIOPinTypeCAN(GPIO_PORTE_BASE, GPIO_PIN_4 | GPIO_PIN_5); GPIOPinConfigure(GPIO_PE4_CAN0RX); GPIOPinConfigure(GPIO_PE5_CAN0TX); tCANMsgObject sCANMessage; uint8_t ucMsgData[8]; Serial.println("DEBUG2"); uint8_t pui8BufferIn[8]; uint8_t pui8BufferOut[8]; Serial.println("DEBUG3"); CANInit(CAN0_BASE); CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000); CANEnable(CAN0_BASE); sCANMessage.ui32MsgID = 0; // CAN msg ID - 0 for any sCANMessage.ui32MsgIDMask = 0; // mask is 0 for any ID sCANMessage.ui32Flags = MSG_OBJ_RX_INT_ENABLE | MSG_OBJ_USE_ID_FILTER; sCANMessage.ui32MsgLen = 8; // allow up to 8 bytes // // Now load the message object into the CAN peripheral. Once loaded the // CAN will receive any message on the bus, and an interrupt will occur. // Use message object 1 for receiving messages (this is not the same as // the CAN ID which can be any value in this example). // CANMessageSet(CAN0_BASE, 1, &sCANMessage, MSG_OBJ_TYPE_RX); Serial.println("DEBUG5"); while((CANStatusGet(CAN0_BASE, CAN_STS_NEWDAT) & 1) == 0) { // // Read the message out of the message object. // CANMessageGet(CAN0_BASE, 1, &sCANMessage, true); Serial.println(sCANMessage.ui32MsgLen); } Serial.println(sCANMessage.ui32MsgLen); Serial.println(sCANMessage.ui32MsgID); }
void CAN::init() { // Initialize the CAN controller CANInit(_CAN_BASE); CANBitRateSet(_CAN_BASE, SysCtlClockGet(), 125000); CANIntEnable(_CAN_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS); IntEnable(_CAN_INT); HWREG(CAN0_BASE + CAN_O_CTL) |= CAN_CTL_TEST; HWREG(CAN0_BASE + CAN_O_TST) = CAN_TST_LBACK | CAN_TST_SILENT; CANEnable(_CAN_BASE); // Configure LoopBack mode }
//***************************************************************************** // // This function configures the CAN hardware and the message objects so that // they are ready to use once the application returns from this function. // //***************************************************************************** void CANConfigure(void) { // // Configure CAN Pins // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1); // // Enable the CAN controllers. // SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); // // Reset the state of all the message object and the state of the CAN // module to a known state. // CANInit(CAN0_BASE); // // Configure the bit rate for the CAN device, the clock rate to the CAN // controller is fixed at 8MHz for this class of device and the bit rate is // set to 250000. // CANBitRateSet(CAN0_BASE, 8000000, 250000); // // Take the CAN1 device out of INIT state. // CANEnable(CAN0_BASE); // // Enable interrups from CAN controller. // CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR); // // Set up the message object that will receive all messages on the CAN // bus. // CANConfigureNetwork(); // // Enable interrupts for the CAN in the NVIC. // IntEnable(INT_CAN0); }
void init(){ // Init OLED RIT128x96x4Init(1000000); // Set the clock to 50 MHz SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ); // Select SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_1); GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); // Navigation Switches SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3); GPIOPadConfigSet(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); // CAN Connection SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1); CANInit(CAN0_BASE); CANBitRateSet(CAN0_BASE, 8000000, 250000); CANIntEnable(CAN0_BASE, CAN_INT_MASTER); IntEnable(INT_CAN0); CANEnable(CAN0_BASE); // CAN Objects transmit.ulMsgID= 0x200; transmit.ulMsgIDMask= 0; transmit.ulMsgLen= 2*sizeof(unsigned long); // IR Receiver SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); GPIOPinTypeTimer(GPIO_PORTD_BASE, GPIO_PIN_4); // Timer SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); TimerConfigure(TIMER0_BASE, TIMER_CFG_A_CAP_TIME); TimerControlEvent(TIMER0_BASE, TIMER_A, TIMER_EVENT_NEG_EDGE); TimerLoadSet(TIMER0_BASE, TIMER_A, TIME_WIDTH); TimerIntEnable(TIMER0_BASE, TIMER_CAPA_EVENT | TIMER_TIMA_TIMEOUT); TimerEnable(TIMER0_BASE, TIMER_A); IntEnable(INT_TIMER0A); }
static int can_init(const can_cfg_t *cfg) { tCANMsgObject MsgObjectRx; SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1); CANInit(CAN0_BASE); //PLLclock is 400M, and can module clock is 8M(400/50) if (CANBitRateSet(CAN0_BASE, 8000000, cfg->baud) == 0) return 1; //can receive setting,set MsgIDMask value to zero, receive all! MsgObjectRx.ulFlags = MSG_OBJ_NO_FLAGS | MSG_OBJ_USE_ID_FILTER; MsgObjectRx.ulMsgIDMask = 0; CANMessageSet(CAN0_BASE, LM3S_RxMsgObjNr, &MsgObjectRx, MSG_OBJ_TYPE_RX); CANEnable(CAN0_BASE); return 0; }
void vComTaskInitImpl(void) { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); GPIOPinTypeCAN(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7); SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); CANInit(CAN0_BASE); CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 1000000); CANEnable(CAN0_BASE); CANIntEnable(CAN0_BASE, CAN_INT_MASTER); IntEnable(INT_CAN0); IntMasterEnable(); }
int main(void) { char txtBuffer[32]; SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); GPIOPinConfigure(GPIO_PE4_CAN0RX); GPIOPinConfigure(GPIO_PE5_CAN0TX); GPIOPinTypeCAN(GPIO_PORTE_BASE, GPIO_PIN_4 | GPIO_PIN_5); CANInit(CAN0_BASE); CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000); CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS); IntEnable(INT_CAN0); CANEnable(CAN0_BASE); // // Initialize a message object to be used for receiving CAN messages with // any CAN ID. In order to receive any CAN ID, the ID and mask must both // be set to 0, and the ID filter enabled. // sCANMessage.ui32MsgIDMask = 0x7ff; // mask is 0 for any ID sCANMessage.ui32Flags = MSG_OBJ_RX_INT_ENABLE | MSG_OBJ_USE_ID_FILTER; sCANMessage.ui32MsgLen = 16; // allow up to 8 bytes // // Now load the message object into the CAN peripheral. Once loaded the // CAN will receive any message on the bus, and an interrupt will occur. // Use message object 1 for receiving messages (this is not the same as // the CAN ID which can be any value in this example). // sCANMessage.ui32MsgID = 0x0b0; // wheels A sCANMessage.pui8MsgData = (uint8_t *)&wheel_a_data; CANMessageSet(CAN0_BASE, 1, &sCANMessage, MSG_OBJ_TYPE_RX); sCANMessage.ui32MsgID = 0x0b2; // wheels B sCANMessage.pui8MsgData = (uint8_t *)&wheel_b_data; CANMessageSet(CAN0_BASE, 2, &sCANMessage, MSG_OBJ_TYPE_RX); sCANMessage.ui32MsgID = 0x2c4; // engine sCANMessage.pui8MsgData = (uint8_t *)&engine_data; CANMessageSet(CAN0_BASE, 3, &sCANMessage, MSG_OBJ_TYPE_RX); sCANMessage.ui32MsgID = 0x398; // fuel sCANMessage.pui8MsgData = (uint8_t *)&fuel_data; CANMessageSet(CAN0_BASE, 4, &sCANMessage, MSG_OBJ_TYPE_RX); lcd_port_setup(); lcd_init(); for (;;) { unsigned int li, lf, ri, rf, fi, ff; // integral and fractional parts for left, right wheels' speed, and fuel consumption unsigned long int v = 0; // Average speed float vf, ef; // Float versions of speed and economy li = ntohs(wheel_a_data.wheel2); v += li; lf = li%100; li /= 100; ri = ntohs(wheel_a_data.wheel1); v += ri; rf = ri%100; ri /= 100; snprintf(txtBuffer, 21, "L %3d.%02d -- R %3d.%02d", li, lf, ri, rf); lcd_goto(0,0); lcd_puts(txtBuffer); li = ntohs(wheel_b_data.wheel2); v += li; lf = li%100; li /= 100; ri = ntohs(wheel_b_data.wheel1); v += ri; rf = ri%100; ri /= 100; snprintf(txtBuffer, 21, "L %3d.%02d -- R %3d.%02d", li, lf, ri, rf); lcd_goto(1,0); lcd_puts(txtBuffer); vf = v / 400.0; snprintf(txtBuffer, 21, "%4d RPM %3d.%02d KM/H", ntohs(engine_data.rpm), (int)vf, ((int)(vf*100))%100); lcd_goto(2,0); lcd_puts(txtBuffer); fi = ntohs(fuel_data.fuel); ff = fi % 100; fi /= 100; if (vf >= .01) { ef = vf/(fi/100.0); snprintf(txtBuffer, 21, "%2d.%02d L/H %2d.%02d KM/L", fi, ff, (int)ef, ((int)(ef*100))%100); } else { snprintf(txtBuffer, 21, "%2d.%02d L/H --.-- KM/L", fi, ff); } lcd_goto(3,0); lcd_puts(txtBuffer); } return (0); }
//***************************************************************************** // // Configure the CAN and enter a loop to transmit periodic CAN messages. // //***************************************************************************** int main(void) { // // Set the clocking to run directly from the external crystal/oscillator. // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the // crystal on your board. // SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for CAN operation. // InitConsole(); // // For this example CAN0 is used with RX and TX pins on port B4 and B5. // The actual port and pins used may be different on your part, consult // the data sheet for more information. // GPIO port B needs to be enabled so these pins can be used. // TODO: change this to whichever GPIO port you are using // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // Configure the GPIO pin muxing to select CAN0 functions for these pins. // This step selects which alternate function is available for these pins. // This is necessary if your part supports GPIO pin function muxing. // Consult the data sheet to see which functions are allocated per pin. // TODO: change this to select the port/pin you are using // GPIOPinConfigure(GPIO_PB4_CAN0RX); GPIOPinConfigure(GPIO_PB5_CAN0TX); // // Enable the alternate function on the GPIO pins. The above step selects // which alternate function is available. This step actually enables the // alternate function instead of GPIO for these pins. // TODO: change this to match the port/pin you are using // GPIOPinTypeCAN(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_5); // // The GPIO port and pins have been set up for CAN. The CAN peripheral // must be enabled. // SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); // // Initialize the CAN controller // CANInit(CAN0_BASE); // // Set up the bit rate for the CAN bus. This function sets up the CAN // bus timing for a nominal configuration. You can achieve more control // over the CAN bus timing by using the function CANBitTimingSet() instead // of this one, if needed. // In this example, the CAN bus is set to 500 kHz. In the function below, // the call to SysCtlClockGet() is used to determine the clock rate that // is used for clocking the CAN peripheral. This can be replaced with a // fixed value if you know the value of the system clock, saving the extra // function call. For some parts, the CAN peripheral is clocked by a fixed // 8 MHz regardless of the system clock in which case the call to // SysCtlClockGet() should be replaced with 8000000. Consult the data // sheet for more information about CAN peripheral clocking. // CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000); // // Enable interrupts on the CAN peripheral. This example uses static // allocation of interrupt handlers which means the name of the handler // is in the vector table of startup code. If you want to use dynamic // allocation of the vector table, then you must also call CANIntRegister() // here. // // CANIntRegister(CAN0_BASE, CANIntHandler); // if using dynamic vectors // CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS); // // Enable the CAN interrupt on the processor (NVIC). // IntEnable(INT_CAN0); // // Enable the CAN for operation. // CANEnable(CAN0_BASE); // // Initialize the message object that will be used for sending CAN // messages. The message will be 4 bytes that will contain an incrementing // value. Initially it will be set to 0. // // // Initialize message object 1 to be able to send CAN message 1. This // message object is not shared so it only needs to be initialized one // time, and can be used for repeatedly sending the same message ID. // g_sCANMsgObject1.ui32MsgID = 0x1001; g_sCANMsgObject1.ui32MsgIDMask = 0; g_sCANMsgObject1.ui32Flags = MSG_OBJ_TX_INT_ENABLE; g_sCANMsgObject1.ui32MsgLen = sizeof(g_pui8Msg1); g_sCANMsgObject1.pui8MsgData = g_pui8Msg1; // // Initialize message object 2 to be able to send CAN message 2. This // message object is not shared so it only needs to be initialized one // time, and can be used for repeatedly sending the same message ID. // g_sCANMsgObject2.ui32MsgID = 0x2001; g_sCANMsgObject2.ui32MsgIDMask = 0; g_sCANMsgObject2.ui32Flags = MSG_OBJ_TX_INT_ENABLE; g_sCANMsgObject2.ui32MsgLen = sizeof(g_pui8Msg2); g_sCANMsgObject2.pui8MsgData = g_pui8Msg2; // // Enter loop to send messages. Four messages will be sent once per // second. The contents of each message will be changed each time. // for(;;) { // // Send message 1 using CAN controller message object 1. This is // the only message sent using this message object. The // CANMessageSet() function will cause the message to be sent right // away. // PrintCANMessageInfo(&g_sCANMsgObject1, 1); CANMessageSet(CAN0_BASE, 1, &g_sCANMsgObject1, MSG_OBJ_TYPE_TX); // // Send message 2 using CAN controller message object 2. This is // the only message sent using this message object. The // CANMessageSet() function will cause the message to be sent right // away. // PrintCANMessageInfo(&g_sCANMsgObject2, 2); CANMessageSet(CAN0_BASE, 2, &g_sCANMsgObject2, MSG_OBJ_TYPE_TX); // // Load message object 3 with message 3. This is needs to be done each // time because message object 3 is being shared for two different // messages. // g_sCANMsgObject3.ui32MsgID = 0x3001; g_sCANMsgObject3.ui32MsgIDMask = 0; g_sCANMsgObject3.ui32Flags = MSG_OBJ_TX_INT_ENABLE; g_sCANMsgObject3.ui32MsgLen = sizeof(g_pui8Msg3); g_sCANMsgObject3.pui8MsgData = g_pui8Msg3; // // Clear the flag that indicates that message 3 has been sent. This // flag will be set in the interrupt handler when a message has been // sent using message object 3. // g_bMsgObj3Sent = 0; // // Now send message 3 using CAN controller message object 3. This is // the first message sent using this message object. The // CANMessageSet() function will cause the message to be sent right // away. // PrintCANMessageInfo(&g_sCANMsgObject3, 3); CANMessageSet(CAN0_BASE, 3, &g_sCANMsgObject3, MSG_OBJ_TYPE_TX); // // Wait for the indication from the interrupt handler that message // object 3 is done, because we are re-using it for another message. // while(!g_bMsgObj3Sent) { } // // Load message object 3 with message 4. This is needed because // message object 3 is being shared for two different messages. // g_sCANMsgObject3.ui32MsgID = 0x3002; g_sCANMsgObject3.ui32MsgIDMask = 0; g_sCANMsgObject3.ui32Flags = MSG_OBJ_TX_INT_ENABLE; g_sCANMsgObject3.ui32MsgLen = sizeof(g_pui8Msg4); g_sCANMsgObject3.pui8MsgData = g_pui8Msg4; // // Now send message 4 using CAN controller message object 3. This is // the second message sent using this message object. The // CANMessageSet() function will cause the message to be sent right // away. // PrintCANMessageInfo(&g_sCANMsgObject3, 3); CANMessageSet(CAN0_BASE, 3, &g_sCANMsgObject3, MSG_OBJ_TYPE_TX); // // Wait 1 second before continuing // SimpleDelay(); // // Check the error flag to see if errors occurred // if(g_bErrFlag) { UARTprintf(" error - cable connected?\n"); } else { // // If no errors then print the count of message sent // UARTprintf(" total count = %u\n", g_ui32Msg1Count + g_ui32Msg2Count + g_ui32Msg3Count); } // // Change the value in the message data for each of the messages. // (*(uint32_t *)g_pui8Msg1)++; (*(uint32_t *)g_pui8Msg2)++; (*(uint32_t *)g_pui8Msg3)++; (*(uint32_t *)&g_pui8Msg4[0])++; (*(uint32_t *)&g_pui8Msg4[4])--; } // // Return no errors // return(0); }
void app_can_init() { /* //27.2.2.34 SysCtlPeripheralPowerOn //Powers on CAN peripheral. //Not used in CAN example. SysCtlPeripheralPowerOn(SYSCTL_PERIPH_CAN0); */ //27.2.2.32 SysCtlPeripheralEnable // GPIO port B and E needs to be enabled so these pins can be used SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); //enable GPIO port B SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); //enable GPIO port E //15.2.3.17 GPIOPinConfigure // Configures pins for use as a CAN device. // Configure the GPIO pin muxing to select CAN0 functions for these pins. // This step selects which alternate function is available for these pins. GPIOPinConfigure(GPIO_PE4_CAN0RX); GPIOPinConfigure(GPIO_PE5_CAN0TX); //15.2.3.20 GPIOPinTypeCAN // Enable the alternate function on the GPIO pins. The above step selects // which alternate function is available. This step actually enables the // alternate function instead of GPIO for these pins. // Configure GPIO Port E pins 4 and 5 to be used as CAN0. GPIOPinTypeCAN(GPIO_PORTE_BASE,GPIO_PIN_4 | GPIO_PIN_5); //15.2.3.27 GPIOPinTypeGPIOOutput // Set GPIO PORTB pins 6 and 7 as output, SW controlled. // CAN_RS=PB6, CAN_Enable=PB7 GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE,GPIO_PIN_6 | GPIO_PIN_7); //15.2.3.48 GPIOPinWrite // Writes a "0" to pin PB6 (CAN_RS pin of CAN transceiver TI SN65HVD64). GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_6,0); //enable CAN driver slope control through value of RS resistor. // If a high-level input (> 0.75 VCC) is applied to CAN_RS pin (TI SN65HVD64, pin 8), the circuit enters // a low-current, listen only standby mode during which the driver is switched off and the receiver // remains active. // Uncomment following line (and comment previous line) if a listen-only standby-mode is desired. // // GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_6,GPIO_PIN_6); //CAN driver switched off and CAN receiver active // // // Writes a "1" to pin PB7 CAN_Enable pin of CAN transceiver TI SN65HVD64, enabling the external HW. GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_7,GPIO_PIN_7); //enable CAN transceiver //27.2.2.32 SysCtlPeripheralEnable // The GPIO port and pins have been set up for CAN. The CAN peripheral // must be enabled. SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); //enable CAN 0 // It takes five clock cycles after the write to enable a peripheral before // the the peripheral is actually enabled. Uncomment following line if needed. // SysCtlDelay(2); //it generates a delay by executing (two times) a 3 instruction cycle loop // 6.2.5.7 CANInit // Initialize the CAN controller CANInit(CAN0_BASE); // 6.2.5.1 CANBitRateSet // Sets the CAN bit timing values to a nominal setting based on a desired bit rate. // If tighter timing requirements or longer network lengths are needed, then the // CANBitTimingSet() function is available for full customization of all of the CAN b // it timing values. CANBitRateSet(CAN0_BASE, SysCtlClockGet(), CAN_BITRATE); // 6.2.5.10 CANIntEnable & 6.2.5.11 CANIntRegister // - Registers an interrupt handler for the CAN controller and // - enable interrupts on the CAN peripheral. // This code uses dynamic allocation of the vector table. If you want static allocation // of interrupt handlers which means the name of the handler is in the vector table of // startup code you must also comment "CANIntRegister(CAN0_BASE, CANIntHandler)". CANIntRegister(CAN0_BASE, CANIntHandler); // needed only using dynamic vectors CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS); //18.2.3.2 IntEnable //Enables CAN0 interrupts. IntEnable(INT_CAN0); //6.2.5.5 CANEnable //Enables the CAN controller. CANEnable(CAN0_BASE); }
//***************************************************************************** // // Configure the CAN and enter a loop to receive CAN messages. // //***************************************************************************** int main(void) { #if defined(TARGET_IS_TM4C129_RA0) || \ defined(TARGET_IS_TM4C129_RA1) || \ defined(TARGET_IS_TM4C129_RA2) uint32_t ui32SysClock; #endif tCANMsgObject sCANMessage; uint8_t pui8MsgData[8]; // // Set the clocking to run directly from the external crystal/oscillator. // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the // crystal used on your board. // #if defined(TARGET_IS_TM4C129_RA0) || \ defined(TARGET_IS_TM4C129_RA1) || \ defined(TARGET_IS_TM4C129_RA2) ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_OSC) 25000000); #else SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); #endif // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for CAN operation. // InitConsole(); // // For this example CAN0 is used with RX and TX pins on port B4 and B5. // The actual port and pins used may be different on your part, consult // the data sheet for more information. // GPIO port B needs to be enabled so these pins can be used. // TODO: change this to whichever GPIO port you are using // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // Configure the GPIO pin muxing to select CAN0 functions for these pins. // This step selects which alternate function is available for these pins. // This is necessary if your part supports GPIO pin function muxing. // Consult the data sheet to see which functions are allocated per pin. // TODO: change this to select the port/pin you are using // GPIOPinConfigure(GPIO_PB4_CAN0RX); GPIOPinConfigure(GPIO_PB5_CAN0TX); // // Enable the alternate function on the GPIO pins. The above step selects // which alternate function is available. This step actually enables the // alternate function instead of GPIO for these pins. // TODO: change this to match the port/pin you are using // GPIOPinTypeCAN(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_5); // // The GPIO port and pins have been set up for CAN. The CAN peripheral // must be enabled. // SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); // // Initialize the CAN controller // CANInit(CAN0_BASE); // // Set up the bit rate for the CAN bus. This function sets up the CAN // bus timing for a nominal configuration. You can achieve more control // over the CAN bus timing by using the function CANBitTimingSet() instead // of this one, if needed. // In this example, the CAN bus is set to 500 kHz. In the function below, // the call to SysCtlClockGet() or ui32SysClock is used to determine the // clock rate that is used for clocking the CAN peripheral. This can be // replaced with a fixed value if you know the value of the system clock, // saving the extra function call. For some parts, the CAN peripheral is // clocked by a fixed 8 MHz regardless of the system clock in which case // the call to SysCtlClockGet() or ui32SysClock should be replaced with // 8000000. Consult the data sheet for more information about CAN // peripheral clocking. // #if defined(TARGET_IS_TM4C129_RA0) || \ defined(TARGET_IS_TM4C129_RA1) || \ defined(TARGET_IS_TM4C129_RA2) CANBitRateSet(CAN0_BASE, ui32SysClock, 500000); #else CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000); #endif // // Enable interrupts on the CAN peripheral. This example uses static // allocation of interrupt handlers which means the name of the handler // is in the vector table of startup code. If you want to use dynamic // allocation of the vector table, then you must also call CANIntRegister() // here. // // CANIntRegister(CAN0_BASE, CANIntHandler); // if using dynamic vectors // CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS); // // Enable the CAN interrupt on the processor (NVIC). // IntEnable(INT_CAN0); // // Enable the CAN for operation. // CANEnable(CAN0_BASE); // // Initialize a message object to be used for receiving CAN messages with // any CAN ID. In order to receive any CAN ID, the ID and mask must both // be set to 0, and the ID filter enabled. // sCANMessage.ui32MsgID = 0; sCANMessage.ui32MsgIDMask = 0; sCANMessage.ui32Flags = MSG_OBJ_RX_INT_ENABLE | MSG_OBJ_USE_ID_FILTER; sCANMessage.ui32MsgLen = 8; // // Now load the message object into the CAN peripheral. Once loaded the // CAN will receive any message on the bus, and an interrupt will occur. // Use message object 1 for receiving messages (this is not the same as // the CAN ID which can be any value in this example). // CANMessageSet(CAN0_BASE, 1, &sCANMessage, MSG_OBJ_TYPE_RX); // // Enter loop to process received messages. This loop just checks a flag // that is set by the interrupt handler, and if set it reads out the // message and displays the contents. This is not a robust method for // processing incoming CAN data and can only handle one messages at a time. // If many messages are being received close together, then some messages // may be dropped. In a real application, some other method should be used // for queuing received messages in a way to ensure they are not lost. You // can also make use of CAN FIFO mode which will allow messages to be // buffered before they are processed. // for(;;) { unsigned int uIdx; // // If the flag is set, that means that the RX interrupt occurred and // there is a message ready to be read from the CAN // if(g_bRXFlag) { // // Reuse the same message object that was used earlier to configure // the CAN for receiving messages. A buffer for storing the // received data must also be provided, so set the buffer pointer // within the message object. // sCANMessage.pui8MsgData = pui8MsgData; // // Read the message from the CAN. Message object number 1 is used // (which is not the same thing as CAN ID). The interrupt clearing // flag is not set because this interrupt was already cleared in // the interrupt handler. // CANMessageGet(CAN0_BASE, 1, &sCANMessage, 0); // // Clear the pending message flag so that the interrupt handler can // set it again when the next message arrives. // g_bRXFlag = 0; // // Check to see if there is an indication that some messages were // lost. // if(sCANMessage.ui32Flags & MSG_OBJ_DATA_LOST) { UARTprintf("CAN message loss detected\n"); } // // Print out the contents of the message that was received. // UARTprintf("Msg ID=0x%08X len=%u data=0x", sCANMessage.ui32MsgID, sCANMessage.ui32MsgLen); for(uIdx = 0; uIdx < sCANMessage.ui32MsgLen; uIdx++) { UARTprintf("%02X ", pui8MsgData[uIdx]); } UARTprintf("total count=%u\n", g_ui32MsgCount); } } // // Return no errors // return(0); }
int main(void) { // // If running on Rev A2 silicon, turn the LDO voltage up to 2.75V. This is // a workaround to allow the PLL to operate reliably. // if(REVISION_IS_A2) { SysCtlLDOSet(SYSCTL_LDO_2_75V); } // Set the clocking to run directly from the PLL at 25MHz. SysCtlClockSet(SYSCTL_SYSDIV_8 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ); // Configure CAN 0 Pins. SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1); // Enable the CAN controller. SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); // // Reset the state of all the message object and the state of the CAN // module to a known state. // CANInit(CAN0_BASE); // // Configure the bit rate for the CAN device, the clock rate to the CAN // controller is fixed at 8MHz for this class of device and the bit rate is // set to 250000. // CANBitRateSet(CAN0_BASE, 8000000, 1000000); // // Take the CAN0 device out of INIT state. // CANEnable(CAN0_BASE); // // Enable interrups from CAN controller. // CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR); // Enable interrupts for the CAN in the NVIC. IntEnable(INT_CAN0); // // Enable processor interrupts. // IntMasterEnable(); // // Configure SysTick for a 10ms interrupt. // // 1 = 1s - // 10 = 100ms // 100 = 10ms // 200 = 5ms // 500 = 2ms // 1000 = 1ms //10000 = 100us //20000 = 50us - this is about the time a max CAN packet (50bytes) needs to be sent at max bit rate of 1Mbps SysTickPeriodSet(SysCtlClockGet() / 10); SysTickEnable(); SysTickIntEnable(); // // Loop forever. // while(1) { } }
//***************************************************************************** // // This is the main loop for the application. // //***************************************************************************** int main(void) { // // If running on Rev A2 silicon, turn the LDO voltage up to 2.75V. This is // a workaround to allow the PLL to operate reliably. // if(REVISION_IS_A2) { SysCtlLDOSet(SYSCTL_LDO_2_75V); } // // Set the clocking to run directly from the PLL at 50MHz. // SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ); // // Configure CAN 0 Pins. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1); // // Configure LED pin. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // // Configure GPIO Pin used for the LED. // GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2); // // Turn off the LED. // GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0); // // Enable the CAN controller. // SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); // // Reset the state of all the message object and the state of the CAN // module to a known state. // CANInit(CAN0_BASE); // // Configure the bit rate for the CAN device, the clock rate to the CAN // controller is fixed at 8MHz for this class of device and the bit rate is // set to CAN_BITRATE. // CANBitRateSet(CAN0_BASE, 8000000, CAN_BITRATE); // // Take the CAN0 device out of INIT state. // CANEnable(CAN0_BASE); // // Enable interrupts from CAN controller. // CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR); // // Enable interrupts for the CAN in the NVIC. // IntEnable(INT_CAN0); // // Enable processor interrupts. // IntMasterEnable(); // // Set the initial state to wait for data. // g_sCAN.eState = CAN_WAIT_RX; // // Reset the buffer pointer. // g_sCAN.MsgObjectRx.pucMsgData = g_sCAN.pucBuffer; // // Set the total number of bytes expected. // g_sCAN.ulBytesRemaining = CAN_FIFO_SIZE; // // Configure the receive message FIFO. // CANReceiveFIFO(g_sCAN.pucBuffer, CAN_FIFO_SIZE); // // Initialized the LED toggle count. // g_ulLEDCount = 0; // // Loop forever. // while(1) { switch(g_sCAN.eState) { case CAN_IDLE: { // // Switch to sending state. // g_sCAN.eState = CAN_SENDING; // // Initialize the transmit count to zero. // g_sCAN.ulBytesTransmitted = 0; // // Schedule all of the CAN transmissions. // CANTransmitFIFO(g_sCAN.pucBuffer, CAN_FIFO_SIZE); break; } case CAN_SENDING: { // // Wait for all bytes to go out. // if(g_sCAN.ulBytesTransmitted == CAN_FIFO_SIZE) { // // Switch to wait for RX state. // g_sCAN.eState = CAN_WAIT_RX; } break; } case CAN_WAIT_RX: { // // Wait for all new data to be received. // if(g_sCAN.ulBytesRemaining == 0) { // // Switch to wait for Process data state. // g_sCAN.eState = CAN_PROCESS; // // Reset the buffer pointer. // g_sCAN.MsgObjectRx.pucMsgData = g_sCAN.pucBuffer; // // Reset the number of bytes expected. // g_sCAN.ulBytesRemaining = CAN_FIFO_SIZE; } break; } case CAN_PROCESS: { // // Handle the LED toggle. // ToggleLED(); // // Return to the idle state. // g_sCAN.eState = CAN_IDLE; break; } default: { break; } } } }
//***************************************************************************** // // Configure the CAN and enter a loop to transmit periodic CAN messages. // //***************************************************************************** int main(void) { tCANMsgObject sCANMessage; uint32_t ui32MsgData; uint8_t *pui8MsgData; pui8MsgData = (uint8_t *)&ui32MsgData; // // Set the clocking to run directly from the external crystal/oscillator. // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the // crystal on your board. // SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for CAN operation. // InitConsole(); // // For this example CAN0 is used with RX and TX pins on port B4 and B5. // The actual port and pins used may be different on your part, consult // the data sheet for more information. // GPIO port B needs to be enabled so these pins can be used. // TODO: change this to whichever GPIO port you are using // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // Configure the GPIO pin muxing to select CAN0 functions for these pins. // This step selects which alternate function is available for these pins. // This is necessary if your part supports GPIO pin function muxing. // Consult the data sheet to see which functions are allocated per pin. // TODO: change this to select the port/pin you are using // GPIOPinConfigure(GPIO_PB4_CAN0RX); GPIOPinConfigure(GPIO_PB5_CAN0TX); // // Enable the alternate function on the GPIO pins. The above step selects // which alternate function is available. This step actually enables the // alternate function instead of GPIO for these pins. // TODO: change this to match the port/pin you are using // GPIOPinTypeCAN(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_5); // // The GPIO port and pins have been set up for CAN. The CAN peripheral // must be enabled. // SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); // // Initialize the CAN controller // CANInit(CAN0_BASE); // // Set up the bit rate for the CAN bus. This function sets up the CAN // bus timing for a nominal configuration. You can achieve more control // over the CAN bus timing by using the function CANBitTimingSet() instead // of this one, if needed. // In this example, the CAN bus is set to 500 kHz. In the function below, // the call to SysCtlClockGet() is used to determine the clock rate that // is used for clocking the CAN peripheral. This can be replaced with a // fixed value if you know the value of the system clock, saving the extra // function call. For some parts, the CAN peripheral is clocked by a fixed // 8 MHz regardless of the system clock in which case the call to // SysCtlClockGet() should be replaced with 8000000. Consult the data // sheet for more information about CAN peripheral clocking. // CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000); // // Enable interrupts on the CAN peripheral. This example uses static // allocation of interrupt handlers which means the name of the handler // is in the vector table of startup code. If you want to use dynamic // allocation of the vector table, then you must also call CANIntRegister() // here. // // CANIntRegister(CAN0_BASE, CAN0_IRQHandler); // if using dynamic vectors // CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS); // // Enable the CAN interrupt on the processor (NVIC). // IntEnable(INT_CAN0); // // Enable the CAN for operation. // CANEnable(CAN0_BASE); // // Initialize the message object that will be used for sending CAN // messages. The message will be 4 bytes that will contain an incrementing // value. Initially it will be set to 0. // ui32MsgData = 0; sCANMessage.ui32MsgID = 1; sCANMessage.ui32MsgIDMask = 0; sCANMessage.ui32Flags = MSG_OBJ_TX_INT_ENABLE; sCANMessage.ui32MsgLen = sizeof(pui8MsgData); sCANMessage.pui8MsgData = pui8MsgData; // // Enter loop to send messages. A new message will be sent once per // second. The 4 bytes of message content will be treated as an uint32_t // and incremented by one each time. // while(1) { // // Print a message to the console showing the message count and the // contents of the message being sent. // UARTprintf("Sending msg: 0x%02X %02X %02X %02X", pui8MsgData[0], pui8MsgData[1], pui8MsgData[2], pui8MsgData[3]); // // Send the CAN message using object number 1 (not the same thing as // CAN ID, which is also 1 in this example). This function will cause // the message to be transmitted right away. // CANMessageSet(CAN0_BASE, 1, &sCANMessage, MSG_OBJ_TYPE_TX); // // Now wait 1 second before continuing // SimpleDelay(); // // Check the error flag to see if errors occurred // if(g_bErrFlag) { UARTprintf(" error - cable connected?\n"); } else { // // If no errors then print the count of message sent // UARTprintf(" total count = %u\n", g_ui32MsgCount); } // // Increment the value in the message data. // ui32MsgData++; } // // Return no errors // return(0); }
//***************************************************************************** // // This is the main loop for the application. // //***************************************************************************** int main(void) { // // If running on Rev A2 silicon, turn the LDO voltage up to 2.75V. This is // a workaround to allow the PLL to operate reliably. // if(REVISION_IS_A2) { SysCtlLDOSet(SYSCTL_LDO_2_75V); } // // Set the clocking to run directly from the PLL at 25MHz. // SysCtlClockSet(SYSCTL_SYSDIV_8 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ); // // Enable the pull-ups on the JTAG signals. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); GPIOPadConfigSet(GPIO_PORTC_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); // // Configure CAN 0 Pins. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1); // // Configure GPIO Pins used for the Buttons. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1); GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); // // Configure GPIO Pin used for the LED. // GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2); // // Turn off the LED. // GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0); // // Enable the CAN controller. // SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); // // Reset the state of all the message object and the state of the CAN // module to a known state. // CANInit(CAN0_BASE); // // Configure the bit rate for the CAN device, the clock rate to the CAN // controller is fixed at 8MHz for this class of device and the bit rate is // set to 250000. // CANBitRateSet(CAN0_BASE, 8000000, 250000); // // Take the CAN0 device out of INIT state. // CANEnable(CAN0_BASE); // // Enable interrups from CAN controller. // CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR); // // Set up the message object that will receive all messages on the CAN // bus. // CANConfigureNetwork(); // // Enable interrupts for the CAN in the NVIC. // IntEnable(INT_CAN0); // // Enable processor interrupts. // IntMasterEnable(); // // Configure SysTick for a 10ms interrupt. // SysTickPeriodSet(SysCtlClockGet() / 100); SysTickEnable(); SysTickIntEnable(); // // Initialize the button status. // g_ucButtonStatus = GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1); // // Loop forever. // while(1) { // // Forground handling of interrupts. // ProcessInterrupts(); // // Handle any incoming commands. // ProcessCmd(); } }
//***************************************************************************** // // Configure the CAN and enter a loop to receive CAN messages. // //***************************************************************************** int main(void) { tCANMsgObject sCANMessage; uint8_t pui8MsgData[8]; // // Set the clocking to run directly from the external crystal/oscillator. // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the // crystal used on your board. // SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for CAN operation. // InitConsole(); // // For this example CAN0 is used with RX and TX pins on port B4 and B5. // The actual port and pins used may be different on your part, consult // the data sheet for more information. // GPIO port B needs to be enabled so these pins can be used. // TODO: change this to whichever GPIO port you are using // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // Configure the GPIO pin muxing to select CAN0 functions for these pins. // This step selects which alternate function is available for these pins. // This is necessary if your part supports GPIO pin function muxing. // Consult the data sheet to see which functions are allocated per pin. // TODO: change this to select the port/pin you are using // GPIOPinConfigure(GPIO_PB4_CAN0RX); GPIOPinConfigure(GPIO_PB5_CAN0TX); // // Enable the alternate function on the GPIO pins. The above step selects // which alternate function is available. This step actually enables the // alternate function instead of GPIO for these pins. // TODO: change this to match the port/pin you are using // GPIOPinTypeCAN(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_5); // // The GPIO port and pins have been set up for CAN. The CAN peripheral // must be enabled. // SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); // // Initialize the CAN controller // CANInit(CAN0_BASE); // // Set up the bit rate for the CAN bus. This function sets up the CAN // bus timing for a nominal configuration. You can achieve more control // over the CAN bus timing by using the function CANBitTimingSet() instead // of this one, if needed. // In this example, the CAN bus is set to 500 kHz. In the function below, // the call to SysCtlClockGet() is used to determine the clock rate that // is used for clocking the CAN peripheral. This can be replaced with a // fixed value if you know the value of the system clock, saving the extra // function call. For some parts, the CAN peripheral is clocked by a fixed // 8 MHz regardless of the system clock in which case the call to // SysCtlClockGet() should be replaced with 8000000. Consult the data // sheet for more information about CAN peripheral clocking. // CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000); // // Enable interrupts on the CAN peripheral. This example uses static // allocation of interrupt handlers which means the name of the handler // is in the vector table of startup code. If you want to use dynamic // allocation of the vector table, then you must also call CANIntRegister() // here. // // CANIntRegister(CAN0_BASE, CAN0_IRQHandler); // if using dynamic vectors // CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS); // // Enable the CAN interrupt on the processor (NVIC). // IntEnable(INT_CAN0); // // Enable the CAN for operation. // CANEnable(CAN0_BASE); // // Initialize a message object to receive CAN messages with ID 0x1001. // The expected ID must be set along with the mask to indicate that all // bits in the ID must match. // sCANMessage.ui32MsgID = 0x1001; sCANMessage.ui32MsgIDMask = 0xfffff; sCANMessage.ui32Flags = (MSG_OBJ_RX_INT_ENABLE | MSG_OBJ_USE_ID_FILTER | MSG_OBJ_EXTENDED_ID); sCANMessage.ui32MsgLen = 8; // // Now load the message object into the CAN peripheral message object 1. // Once loaded the CAN will receive any messages with this CAN ID into // this message object, and an interrupt will occur. // CANMessageSet(CAN0_BASE, 1, &sCANMessage, MSG_OBJ_TYPE_RX); // // Change the ID to 0x2001, and load into message object 2 which will be // used for receiving any CAN messages with this ID. Since only the CAN // ID field changes, we don't need to reload all the other fields. // sCANMessage.ui32MsgID = 0x2001; CANMessageSet(CAN0_BASE, 2, &sCANMessage, MSG_OBJ_TYPE_RX); // // Change the ID to 0x3001, and load into message object 3 which will be // used for receiving any CAN messages with this ID. Since only the CAN // ID field changes, we don't need to reload all the other fields. // sCANMessage.ui32MsgID = 0x3001; CANMessageSet(CAN0_BASE, 3, &sCANMessage, MSG_OBJ_TYPE_RX); // // Enter loop to process received messages. This loop just checks flags // for each of the 3 expected messages. The flags are set by the interrupt // handler, and if set this loop reads out the message and displays the // contents to the console. This is not a robust method for processing // incoming CAN data and can only handle one messages at a time per message // object. If many messages are being received close together using the // same message object, then some messages may be dropped. In a real // application, some other method should be used for queuing received // messages in a way to ensure they are not lost. You can also make use // of CAN FIFO mode which will allow messages to be buffered before they // are processed. // for(;;) { // // If the flag for message object 1 is set, that means that the RX // interrupt occurred and there is a message ready to be read from // this CAN message object. // if(g_bRXFlag1) { // // Reuse the same message object that was used earlier to configure // the CAN for receiving messages. A buffer for storing the // received data must also be provided, so set the buffer pointer // within the message object. This same buffer is used for all // messages in this example, but your application could set a // different buffer each time a message is read in order to store // different messages in different buffers. // sCANMessage.pui8MsgData = pui8MsgData; // // Read the message from the CAN. Message object number 1 is used // (which is not the same thing as CAN ID). The interrupt clearing // flag is not set because this interrupt was already cleared in // the interrupt handler. // CANMessageGet(CAN0_BASE, 1, &sCANMessage, 0); // // Clear the pending message flag so that the interrupt handler can // set it again when the next message arrives. // g_bRXFlag1 = 0; // // Print information about the message just received. // PrintCANMessageInfo(&sCANMessage, 1); } // // Check for message received on message object 2. If so then // read message and print information. // if(g_bRXFlag2) { sCANMessage.pui8MsgData = pui8MsgData; CANMessageGet(CAN0_BASE, 2, &sCANMessage, 0); g_bRXFlag2 = 0; PrintCANMessageInfo(&sCANMessage, 2); } // // Check for message received on message object 3. If so then // read message and print information. // if(g_bRXFlag3) { sCANMessage.pui8MsgData = pui8MsgData; CANMessageGet(CAN0_BASE, 3, &sCANMessage, 0); g_bRXFlag3 = 0; PrintCANMessageInfo(&sCANMessage, 3); } } // // Return no errors // return(0); }
//***************************************************************************** // // Setup CAN0 to both send and receive at 500KHz. // Interrupts on // Use PE4 / PE5 // //***************************************************************************** void InitCAN0(void) { // // For this example CAN0 is used with RX and TX pins on port E4 and E5. // GPIO port E needs to be enabled so these pins can be used. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); // // Configure the GPIO pin muxing to select CAN0 functions for these pins. // This step selects which alternate function is available for these pins. // GPIOPinConfigure(GPIO_PE4_CAN0RX); GPIOPinConfigure(GPIO_PE5_CAN0TX); // // Enable the alternate function on the GPIO pins. The above step selects // which alternate function is available. This step actually enables the // alternate function instead of GPIO for these pins. // GPIOPinTypeCAN(GPIO_PORTE_BASE, GPIO_PIN_4 | GPIO_PIN_5); // // The GPIO port and pins have been set up for CAN. The CAN peripheral // must be enabled. // SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); // // Initialize the CAN controller // CANInit(CAN0_BASE); // // Set up the bit rate for the CAN bus. This function sets up the CAN // bus timing for a nominal configuration. You can achieve more control // over the CAN bus timing by using the function CANBitTimingSet() instead // of this one, if needed. // In this example, the CAN bus is set to 500 kHz. // CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000); // // Enable interrupts on the CAN peripheral. This example uses static // allocation of interrupt handlers which means the name of the handler // is in the vector table of startup code. // CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS); // // Enable the CAN interrupt on the processor (NVIC). // IntEnable(INT_CAN0); // // Enable the CAN for operation. // CANEnable(CAN0_BASE); // // Initialize a message object to be used for receiving CAN messages with // any CAN ID. In order to receive any CAN ID, the ID and mask must both // be set to 0, and the ID filter enabled. // g_sCAN0RxMessage.ui32MsgID = CAN0RXID; g_sCAN0RxMessage.ui32MsgIDMask = 0; g_sCAN0RxMessage.ui32Flags = MSG_OBJ_RX_INT_ENABLE | MSG_OBJ_USE_ID_FILTER; g_sCAN0RxMessage.ui32MsgLen = sizeof(g_ui8RXMsgData); // // Now load the message object into the CAN peripheral. Once loaded the // CAN will receive any message on the bus, and an interrupt will occur. // Use message object RXOBJECT for receiving messages (this is not the //same as the CAN ID which can be any value in this example). // CANMessageSet(CAN0_BASE, RXOBJECT, &g_sCAN0RxMessage, MSG_OBJ_TYPE_RX); // // Initialize the message object that will be used for sending CAN // messages. The message will be 1 bytes that will contain the character // received from the other controller. Initially it will be set to 0. // g_ui8TXMsgData = 0; g_sCAN0TxMessage.ui32MsgID = CAN0TXID; g_sCAN0TxMessage.ui32MsgIDMask = 0; g_sCAN0TxMessage.ui32Flags = MSG_OBJ_TX_INT_ENABLE; g_sCAN0TxMessage.ui32MsgLen = sizeof(g_ui8TXMsgData); g_sCAN0TxMessage.pui8MsgData = (uint8_t *)&g_ui8TXMsgData; }