1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// RB.h

// Constants and Global Variables used by Universal Robotics Bus.

/* ----- revision history:

 2004-04-03, DGI: Today I started this revision history.
 The things that need work are: the ERROR constants. These are not very
 well thought out. Also, perhaps some more guidance for user on how to set up
 the object dictionary. The master version of this file should be
 downloaded from: www.grasp.upenn.edu/destudio/CAN/code

 2004-09-10, EAS: Added object_dictionary_entry type, added an object type
 and changed the numbering of the data types to bring them more in line with
 the CANopen specification.

 2005-01-10 DGI: changed the od_entry type to explicitly include a permissions
 byte. Eliminated the "object" type because the word "object" is too vague.
 Added "MEDIA" const and uint8 RB_NODEID variable, and changed many of the
 names from URB_* to RB_*. Robotics Bus web site is now:
 www.grasp.upenn.edu/destudio/RB

*/

/**************************************************************************
ROBOTICS BUS TYPE DEFINITIONS
***************************************************************************/

typedef int8 bool;
typedef signed int8 sint8;
typedef signed int16 sint16;
typedef signed int32 sint32;
typedef unsigned int8 uint8;
typedef unsigned int16 uint16;
typedef unsigned int32 uint32;

/**************************************************************************
CONSTANTS
***************************************************************************/

// this is what they do in the microcanopen2.00
#define URB_HEARTBEAT_NORMAL     0x05
#define URB_HEARTBEAT_STOPPED    0x04

// heartbeat interval of 1 Hz
// this const changes with clock/crystal
#define URB_HEARTBEAT_INTERVAL   0x4C47

#define URB_ERROR_OK                  0x00
#define URB_ERROR_RX_INVALID_MSG      0x10
#define URB_ERROR_RX_XTD_FRAME        0x20
#define URB_ERROR_RX_RTR_FRAME        0x40

#define URB_RX_IN_PROCESS             0x34

#define URB_ERROR_SDO_REPLY_SENT      0x22
#define URB_ERROR_SDO_IN_PROCESS      0x33
#define URB_ERROR_SDO_MALFORMED       0xA9
#define URB_ERROR_SDO_UNKNOWN_TYPE    0xAA
#define URB_ERROR_SDO_NOT_FOUND       0xAB
#define URB_ERROR_SDO_TYPE_MISMATCH   0xAC
#define URB_ERROR_WAITING_FOR_TXBUF   0xAD
#define URB_ERROR_SDO_UNKNOWN_COMMAND 0xAE
#define URB_ERROR_SDO_PERMISSIONS     0xAF

#define URB_ERROR_NMT_UNKNOWN_COMMAND 0xCC
#define URB_ERROR_NMT_NOT_ADDRESSED   0xCD
#define URB_ERROR_NMT_INVALID_LENGTH  0xCE

#define URB_ERROR_PM_NO_MAP           0xD0
#define URB_ERROR_MAPPING_IN_PROCESS  0xD1
#define URB_ERROR_READ_IN_PROCESS     0xD2

// CAN identifiers
#define RB_MSG_DICT_READ	       	0x40
#define RB_MSG_DICT_WRITE      		0x20

/**************************************************************************
TYPE SPECIFIERS
***************************************************************************/

// The MEDIA bit specifies whether the variable
// is stored in RAM or EEPROM
#define RB_MEDIA_BIT     0b10000000
#define RB_MEDIA_NONV    0b10000000
#define RB_MEDIA_RAM     0b00000000
#define RB_MEDIA_ROM     0b00000000 // this one isn't used

// access definitions -- permissions
// occupy the first byte of subindex zero.
#define RB_PERM_BITS      0b00000011
#define RB_PERM_READ_BIT  0b00000001
#define RB_PERM_READONLY  0b00000001
#define RB_PERM_READWRITE 0b00000011
#define RB_PERM_WRITE_BIT 0b00000010
#define RB_PERM_WRITEONLY 0b00000010

// data type definitions
#define RB_TYPE_BOOLEAN   0x41
#define RB_TYPE_SINT8     0x42
#define RB_TYPE_SINT16    0x43
#define RB_TYPE_SINT32    0x44
#define RB_TYPE_UINT8     0x45
#define RB_TYPE_UINT16    0x46
#define RB_TYPE_UINT32    0x47
#define RB_TYPE_FLOAT     0x48
#define RB_TYPE_PM_CONFIG 0x60
#define RB_TYPE_PM_MAPPING 0x61
#define RB_TYPE_IDENTITY  0x63

// end-of-dictionary marker is a zero-index object
#define RB_END_OF_DICTIONARY {0x0000,0,0,0,0}

#define RB_NULL 0
#define RB_UNUSED_MAP {{0,0,0}}

/**************************************************************************
DATA STRUCTURES
***************************************************************************/

typedef struct
{
	int16 ID;
	byte len;
	byte buf[8];
} CAN_MSG;

// the main dictionary
typedef struct
{
	uint16		index;
	uint8		permissions;
	uint8		data_type;
	char*		address;
	char		name[33];
	byte		padding;
} od_entry;

// used in a process message mapping - RAM only
typedef struct
{
   int16  index;
   uint8  data_type;
   char*  address;
} pm_map;

/**************************************************************************
GLOBAL RAM VARIABLES
***************************************************************************/

CAN_MSG RBtPM[4]; // caches the TPDOs before they are sent.
bool   RBtPMready[4]; // flag to indicate ready to transmit.
CAN_MSG RBrPM[4]; // caches the RPDOs when they are received. is this necessary?
CAN_MSG URBhb; // heartbeat message buffer
CAN_MSG URBStackRcv; // buffer used for incoming messages.
CAN_MSG URBTxSDO; // buffer used for responding to an SDO request
byte URBErrorCode; // returns the exit code when something unexpected happens.
byte URBRecvOverflowCounter;
byte URBRecvCounter;
uint8 RB_NODEID=0;

/**************************************************************************
EEPROM MAP
***************************************************************************/

// addresses below 0x7F may be used by application
// addresses above 0x80 are allocated here.

#define RB_EEPROM_NODEID 0x80 // single byte stores the Node ID (can be 1-127)

/**************************************************************************
FUNCTION PROTOTYPES
***************************************************************************/

// prototypes are necessary to make things compile
void RB_PM_SetDefaults(void);
void RB_Handle_Mapping_Request(int16 index, int8 subindex);
bool OD_lookup(int16 index, int8& num);
void RB_Set_tPM_Identifiers(void);

// these two functions are "callback functions" which must be implemented by the application in "main.c"

void RB_App_ResetApp(void);
void RB_App_SetFactoryDefaults(void);


#define CAN_MASK_ELEVEN_BITS 0x07FF
#define CAN_MASK_RB_MESG     0x0700

#define RB_NMT        0x0000
#define RB_COMMAND    0x0100
#define RB_SENSOR     0x0200
#define RB_USART      0x0300
#define RB_NEIGHBOR   0x0400
#define RB_DICT_RESP  0x0500
#define RB_DICT_REQ   0x0600
#define RB_HEARTBEAT  0x0700

#define RB_ALL        0x0700
#define RB_NODE_RESET 129
#define NUM_MASK      2
#define NUM_FILTER    6

uint16 RB_RXCONFIG_0;
uint16 RB_RXCONFIG_1;
uint16 RB_RXCONFIG_2;    
uint16 RB_RXCONFIG_3;    
uint16 RB_TXCONFIG_0;    
uint16 RB_TXCONFIG_1;    
uint16 RB_TXCONFIG_2;    
uint16 RB_TXCONFIG_3;

void RB_reset_from_eeprom(void);
void RB_setup_CAN(void);