Official support library for using mbed as a programmable coprocessor in the Innomatix DAP-III+ telematics platform
InnomatixCanAPI/can_example.txt@12:3d37c8a7be2b, 2017-09-06 (annotated)
- Committer:
- root@developer-sjc-cyan-compiler.local.mbed.org
- Date:
- Wed Sep 06 19:19:37 2017 +0000
- Revision:
- 12:3d37c8a7be2b
- Parent:
- 10:5fbe72ffb725
Added tag v1.0.7 for changeset b28c0d6773bf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Innomatix | 10:5fbe72ffb725 | 1 | /******************************************************************* |
Innomatix | 10:5fbe72ffb725 | 2 | * |
Innomatix | 10:5fbe72ffb725 | 3 | * File: can_example.cpp |
Innomatix | 10:5fbe72ffb725 | 4 | * |
Innomatix | 10:5fbe72ffb725 | 5 | * Copyright 2017 Innomatix, LLC, All Rights Reserved |
Innomatix | 10:5fbe72ffb725 | 6 | * |
Innomatix | 10:5fbe72ffb725 | 7 | * Example of using the Innomatix CAN API for mbed |
Innomatix | 10:5fbe72ffb725 | 8 | * |
Innomatix | 10:5fbe72ffb725 | 9 | *******************************************************************/ |
Innomatix | 10:5fbe72ffb725 | 10 | #include "InnomatixCanAPI.h" |
Innomatix | 10:5fbe72ffb725 | 11 | |
Innomatix | 10:5fbe72ffb725 | 12 | // Create the CAN objects per "the mbed way" |
Innomatix | 10:5fbe72ffb725 | 13 | CAN can0( p9, p10 ); |
Innomatix | 10:5fbe72ffb725 | 14 | CAN can1( p30, p29 ); |
Innomatix | 10:5fbe72ffb725 | 15 | |
Innomatix | 10:5fbe72ffb725 | 16 | |
Innomatix | 10:5fbe72ffb725 | 17 | /******************************************************************************/ |
Innomatix | 10:5fbe72ffb725 | 18 | void CanExampleOpen() |
Innomatix | 10:5fbe72ffb725 | 19 | { |
Innomatix | 10:5fbe72ffb725 | 20 | CanResults_e canresult; |
Innomatix | 10:5fbe72ffb725 | 21 | unsigned long can_baud = 500000; |
Innomatix | 10:5fbe72ffb725 | 22 | |
Innomatix | 10:5fbe72ffb725 | 23 | /*-----------------------------------------------*/ |
Innomatix | 10:5fbe72ffb725 | 24 | // Initialize the CAN library |
Innomatix | 10:5fbe72ffb725 | 25 | CanInit( &can0, &can1 ); |
Innomatix | 10:5fbe72ffb725 | 26 | |
Innomatix | 10:5fbe72ffb725 | 27 | /*-----------------------------------------------*/ |
Innomatix | 10:5fbe72ffb725 | 28 | // Open the CAN channels at the specified baud rate |
Innomatix | 10:5fbe72ffb725 | 29 | canresult = CanOpen( canChDAP3, can_baud ); |
Innomatix | 10:5fbe72ffb725 | 30 | // Have to manually attache the Rx event handler |
Innomatix | 10:5fbe72ffb725 | 31 | can0.attach( OnReceiveCanZero ); |
Innomatix | 10:5fbe72ffb725 | 32 | printf( "Innomatix CAN API initialization for Ch3 @ %dbps: %s\r\n", can_baud, (canSuccess == canresult ? "INITIALIZED" : "FAILED") ); |
Innomatix | 10:5fbe72ffb725 | 33 | |
Innomatix | 10:5fbe72ffb725 | 34 | /*-----------------------------------------------*/ |
Innomatix | 10:5fbe72ffb725 | 35 | canresult = CanOpen( canChDAP4, can_baud ); |
Innomatix | 10:5fbe72ffb725 | 36 | can1.attach( OnReceiveCanOne ); |
Innomatix | 10:5fbe72ffb725 | 37 | printf( "Innomatix CAN API initialization for Ch4 @ %dbps: %s\r\n", can_baud, (canSuccess == canresult ? "INITIALIZED" : "FAILED") ); |
Innomatix | 10:5fbe72ffb725 | 38 | |
Innomatix | 10:5fbe72ffb725 | 39 | /*-----------------------------------------------*/ |
Innomatix | 10:5fbe72ffb725 | 40 | // Pause a moment to let all the CAN init stuff take effect, else |
Innomatix | 10:5fbe72ffb725 | 41 | // sometimes we dont receive the first message on the bus |
Innomatix | 10:5fbe72ffb725 | 42 | wait_ms( 100 ); |
Innomatix | 10:5fbe72ffb725 | 43 | } |
Innomatix | 10:5fbe72ffb725 | 44 | |
Innomatix | 10:5fbe72ffb725 | 45 | |
Innomatix | 10:5fbe72ffb725 | 46 | |
Innomatix | 10:5fbe72ffb725 | 47 | |
Innomatix | 10:5fbe72ffb725 | 48 | /******************************************************************************/ |
Innomatix | 10:5fbe72ffb725 | 49 | void CanExampleClose() |
Innomatix | 10:5fbe72ffb725 | 50 | { |
Innomatix | 10:5fbe72ffb725 | 51 | // Close the channels and clean up the library |
Innomatix | 10:5fbe72ffb725 | 52 | CanClose( canChDAP3 ); |
Innomatix | 10:5fbe72ffb725 | 53 | CanClose( canChDAP4 ); |
Innomatix | 10:5fbe72ffb725 | 54 | } |
Innomatix | 10:5fbe72ffb725 | 55 | |
Innomatix | 10:5fbe72ffb725 | 56 | |
Innomatix | 10:5fbe72ffb725 | 57 | |
Innomatix | 10:5fbe72ffb725 | 58 | /******************************************************************************/ |
Innomatix | 10:5fbe72ffb725 | 59 | void CanExampleMain() |
Innomatix | 10:5fbe72ffb725 | 60 | { |
Innomatix | 10:5fbe72ffb725 | 61 | CanResults_e canresult; |
Innomatix | 10:5fbe72ffb725 | 62 | unsigned long msgid; |
Innomatix | 10:5fbe72ffb725 | 63 | char msglen; |
Innomatix | 10:5fbe72ffb725 | 64 | unsigned char msgbuff[ 8 ] = {0}; |
Innomatix | 10:5fbe72ffb725 | 65 | CanFormat_e msgformat; |
Innomatix | 10:5fbe72ffb725 | 66 | unsigned long timestamp; |
Innomatix | 10:5fbe72ffb725 | 67 | |
Innomatix | 10:5fbe72ffb725 | 68 | |
Innomatix | 10:5fbe72ffb725 | 69 | /*-----------------------------------------------*/ |
Innomatix | 10:5fbe72ffb725 | 70 | // Get a message from CAN3 |
Innomatix | 10:5fbe72ffb725 | 71 | canresult = CanReceive( canChDAP3, ×tamp, &msgid, (CanFormat_e *)&msgformat, msgbuff, &msglen ); |
Innomatix | 10:5fbe72ffb725 | 72 | if( canSuccess == canresult ) |
Innomatix | 10:5fbe72ffb725 | 73 | { |
Innomatix | 10:5fbe72ffb725 | 74 | // Check msgid to see if we're interested in this message |
Innomatix | 10:5fbe72ffb725 | 75 | if( 0x100 == msgid ) |
Innomatix | 10:5fbe72ffb725 | 76 | { |
Innomatix | 10:5fbe72ffb725 | 77 | // get some data from the message |
Innomatix | 10:5fbe72ffb725 | 78 | unsigned short val = 0; |
Innomatix | 10:5fbe72ffb725 | 79 | val += msgbuff[0] << 8; |
Innomatix | 10:5fbe72ffb725 | 80 | val += msgbuff[1]; |
Innomatix | 10:5fbe72ffb725 | 81 | |
Innomatix | 10:5fbe72ffb725 | 82 | //TODO do something with the value.... |
Innomatix | 10:5fbe72ffb725 | 83 | |
Innomatix | 10:5fbe72ffb725 | 84 | }else if( canNoData != canresult ) |
Innomatix | 10:5fbe72ffb725 | 85 | { |
Innomatix | 10:5fbe72ffb725 | 86 | // No CAN message available |
Innomatix | 10:5fbe72ffb725 | 87 | }else |
Innomatix | 10:5fbe72ffb725 | 88 | { |
Innomatix | 10:5fbe72ffb725 | 89 | // Something went wrong, check InnomatixCanAPI.h for result codes |
Innomatix | 10:5fbe72ffb725 | 90 | } |
Innomatix | 10:5fbe72ffb725 | 91 | |
Innomatix | 10:5fbe72ffb725 | 92 | |
Innomatix | 10:5fbe72ffb725 | 93 | |
Innomatix | 10:5fbe72ffb725 | 94 | /*-----------------------------------------------*/ |
Innomatix | 10:5fbe72ffb725 | 95 | // detect receive overruns - this means CAN messages are arrving faster |
Innomatix | 10:5fbe72ffb725 | 96 | // than the application is reading them via CanReceive() |
Innomatix | 10:5fbe72ffb725 | 97 | { |
Innomatix | 10:5fbe72ffb725 | 98 | static unsigned long overflow = 0; |
Innomatix | 10:5fbe72ffb725 | 99 | StatisticsStruct_t Stats; |
Innomatix | 10:5fbe72ffb725 | 100 | |
Innomatix | 10:5fbe72ffb725 | 101 | // Check if there are more overrun errors this time than last time |
Innomatix | 10:5fbe72ffb725 | 102 | CanStatistics( canChDAP3, &Stats ); |
Innomatix | 10:5fbe72ffb725 | 103 | if( Stats.OverflowCount > 0 && Stats.OverflowCount != overflow ) |
Innomatix | 10:5fbe72ffb725 | 104 | { |
Innomatix | 10:5fbe72ffb725 | 105 | // save the new overflow count so we can tell if it changes next time |
Innomatix | 10:5fbe72ffb725 | 106 | overflow = Stats.OverflowCount; |
Innomatix | 10:5fbe72ffb725 | 107 | |
Innomatix | 10:5fbe72ffb725 | 108 | // publish the overflow count to a data bin |
Innomatix | 10:5fbe72ffb725 | 109 | sprintf( str, "CAN3 OVF: %d", overflow ); |
Innomatix | 10:5fbe72ffb725 | 110 | PutString( StatusBin, str ); |
Innomatix | 10:5fbe72ffb725 | 111 | } |
Innomatix | 10:5fbe72ffb725 | 112 | } |
Innomatix | 10:5fbe72ffb725 | 113 | |
Innomatix | 10:5fbe72ffb725 | 114 | |
Innomatix | 10:5fbe72ffb725 | 115 | |
Innomatix | 10:5fbe72ffb725 | 116 | /*-----------------------------------------------*/ |
Innomatix | 10:5fbe72ffb725 | 117 | // Send a message on CAN4 |
Innomatix | 10:5fbe72ffb725 | 118 | memset( msgbuff, 0x00, sizeof(txbuff) ); |
Innomatix | 10:5fbe72ffb725 | 119 | msgbuff[ 0 ] = 0xF0; |
Innomatix | 10:5fbe72ffb725 | 120 | msgbuff[ 1 ] = 0x0D; |
Innomatix | 10:5fbe72ffb725 | 121 | canresult = CanSend( canChDAP4, 0x200, canFormatStandard, msgbuff, 8 ); |
Innomatix | 10:5fbe72ffb725 | 122 | if( canSuccess == canresult ) |
Innomatix | 10:5fbe72ffb725 | 123 | { |
Innomatix | 10:5fbe72ffb725 | 124 | // success |
Innomatix | 10:5fbe72ffb725 | 125 | }else |
Innomatix | 10:5fbe72ffb725 | 126 | { |
Innomatix | 10:5fbe72ffb725 | 127 | // fail - check InnomatixCanAPI.h for result codes |
Innomatix | 10:5fbe72ffb725 | 128 | } |
Innomatix | 10:5fbe72ffb725 | 129 | } |
Innomatix | 10:5fbe72ffb725 | 130 | |
Innomatix | 10:5fbe72ffb725 | 131 |