Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
can.cpp
00001 #include "mbed.h" 00002 #include "can.h" 00003 00004 00005 00006 //--------------------------------------- 00007 // Hardware recources 00008 //--------------------------------------- 00009 CAN can1(p30, p29); 00010 00011 00012 00013 //--------------------------------------- 00014 // Prototypes 00015 //--------------------------------------- 00016 static void CAN_rxCallback( void ); 00017 00018 00019 00020 //--------------------------------------- 00021 // Internal variables 00022 //--------------------------------------- 00023 static CANMessage CAN_rxMessage; 00024 static bool CAN_newFrame = false; 00025 00026 00027 00028 //--------------------------------------- 00029 // External variables 00030 //--------------------------------------- 00031 00032 00033 00034 //--------------------------------------- 00035 // Global Functions 00036 //--------------------------------------- 00037 void CAN_init( void ) 00038 { 00039 CAN_newFrame = false; 00040 can1.reset(); 00041 can1.frequency( CAN_FREQUENCY ); 00042 can1.attach( &CAN_rxCallback ); 00043 } 00044 00045 00046 // Return: 00047 // 0 --> no new frame 00048 // 1 --> new frame was received 00049 int CAN_get( int *ptrID, char *ptrLen, char *ptrData ) 00050 { 00051 int i; 00052 00053 // No new frame 00054 if( CAN_newFrame == false ) 00055 { 00056 *ptrID = 0; 00057 *ptrLen = 0; 00058 return 0; 00059 } 00060 00061 // New frame was received 00062 *ptrID = CAN_rxMessage.id; 00063 *ptrLen = CAN_rxMessage.len; 00064 for( i = 0; i < CAN_rxMessage.len; i++) 00065 { 00066 *ptrData = CAN_rxMessage.data[i]; 00067 ptrData++; 00068 } 00069 00070 CAN_newFrame = false; 00071 00072 return 1; 00073 } 00074 00075 00076 // Return: 00077 // 0: ID, Len outside limit or CAN hardware problems 00078 // 1: Data have been send 00079 int CAN_send( int ID, char Len, char *ptrData ) 00080 { 00081 // Check limits 00082 if( Len > 8 ) 00083 { 00084 return 0; 00085 } 00086 00087 if( (ID < 0) || (ID > 0x7FF) ) 00088 { 00089 return 0; 00090 } 00091 00092 00093 if( can1.write( CANMessage(ID, ptrData, Len) ) ) 00094 { 00095 return 1; 00096 } 00097 00098 // Error sending data --> reset init CAN 00099 CAN_init(); 00100 return 0; 00101 } 00102 00103 00104 //--------------------------------------- 00105 // Internal Functions 00106 //--------------------------------------- 00107 00108 static void CAN_rxCallback( void ) 00109 { 00110 can1.read(CAN_rxMessage); 00111 CAN_newFrame = true; 00112 } 00113
Generated on Sat Jul 16 2022 03:23:51 by
