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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**************************************************************************
                        OBJECT DICTIONARY ACCESS
 **************************************************************************

 A set of functions for accessing information in the object dictionary.

 Authors and Contributors:
   Ethan Stump <estump@grasp.upenn.edu>
   GRASP Lab, University of Pennsylvania

   Daniel Gomez-Ibanez

   Ethan, I changed two lines here because I was getting a compile error,
   "identifier must be a pointer" See "char* source;" declaration.
 **************************************************************************/

// read and write to EEPROM

void   RBSetEEPROM(int *data, byte addr, int len)
{
   int i;
   for (i=0; i<len; i++)
   {
      write_eeprom(addr+i,data[i]);
   }
}

void   RBReadEEPROM(byte *data, byte addr, byte len)
{
   int i;

   for (i=0; i<len; i++)
   {
      data[i] = read_eeprom(addr+i);
   }
}

// a couple of support functions: sizeof and float translation

// returns the size in bytes of the specified type.
#inline // this #inline wastes ROM but prevents PM interrupts from interfering with main thread.
byte URB_Sizeof(byte type) {

   switch (type) {
   case RB_TYPE_BOOLEAN:     return 1;   break;
   case RB_TYPE_SINT8:       return 1;   break; // one byte long,
   case RB_TYPE_SINT16:      return 2;   break; // two bytes long,
   case RB_TYPE_SINT32:      return 4;   break; // etc.
   case RB_TYPE_UINT8:       return 1;   break;
   case RB_TYPE_UINT16:      return 2;   break;
   case RB_TYPE_UINT32:      return 4;   break;
   case RB_TYPE_FLOAT:       return 4;   break;
   case RB_TYPE_IDENTITY:    return 1;   break;
   case RB_TYPE_PM_CONFIG:   return 2;   break;
      // PM_MAPPING does not have a fixed length
default:   URBErrorCode = URB_ERROR_SDO_UNKNOWN_TYPE;
      return 0;
      break; // unknown type
   } // switch
} // URBsizeof

#inline // this #inline wastes ROM but prevents PM interrupts from interfering with main thread.
void URB_translate_microchip_to_ieee(int *Value)
{
   int   temp;

   // move the sign bit (thanks to plehman on CCS forum)
   // need to move the sign bit from (byte 1, bit 7) to (byte 0, bit 7).
   // (And shift everything over in a lossless manner.)

   temp = shift_left(&Value[1], 1, 0);
   temp = shift_right(&Value[0], 1, temp);
   shift_right(&Value[1], 1, temp);

   // and reverse the order of the bytes. Microchip is backward from IEEE.

   temp = Value[0];
   Value[0] = Value[3];
   Value[3] = temp;

   temp = Value[1];
   Value[1] = Value[2];
   Value[2] = temp;

   // for more info on floats, see:
   // http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html
}

void URB_translate_ieee_to_microchip(int *Value)
{
   int   temp;

   // invert the above transformation.

   temp = Value[0];
   Value[0] = Value[3];
   Value[3] = temp;

   temp = Value[1];
   Value[1] = Value[2];
   Value[2] = temp;

   temp = shift_left(&Value[1], 1, 0);
   temp = shift_left(&Value[0], 1, temp);
   shift_right(&Value[1], 1, temp);
}

#inline
int OD_read_data(char* dest, int num, uint8 type)
{
   char* source; // pointer for RAM copy
   int8 address; // address for EEPROM copy
   uint8 size;

   size = URB_sizeof(type);

   // is data in EEPROM?
   if ( (objectDictionary[num].permissions & RB_MEDIA_BIT) == RB_MEDIA_NONV )
   {
      // yes, data is in EEPROM
      address = objectDictionary[num].address;
      RBReadEEPROM(dest,(address & 0xFF),size);
   }
   else
   {
      // no, data is in RAM
      source = objectDictionary[num].address;
      memcpy(dest, source, size);
   }

   if (type == RB_TYPE_FLOAT)
      URB_translate_microchip_to_ieee(dest);

	return 0;
}

#inline
int OD_write_data(char* source, int num, int type)
{
   char* dest;
   uint8 size, address;

   size = URB_Sizeof(type);

    if (type == RB_TYPE_FLOAT)
      URB_translate_ieee_to_microchip(source);


    // is data in EEPROM?
   if ( (objectDictionary[num].permissions & RB_MEDIA_BIT) == RB_MEDIA_NONV )
   {
      // yes, data is in EEPROM
      address = objectDictionary[num].address;
      RBSetEEPROM(source,(address & 0xFF),size);
   }
   else
   {
      // no, data is in RAM
      dest = objectDictionary[num].address;
      memcpy(dest, source, size);
   }

	return 0;
}

uint8 RB_Get_Rx_Mapping_Length(uint8 myBuf)
{
   uint8 tmp;
   switch (myBuf) {
      case (0): tmp = RB_RXMAPPING_0_LENGTH;
         break;
      case (1): tmp = RB_RXMAPPING_1_LENGTH;
         break;
      case (2): tmp = RB_RXMAPPING_2_LENGTH;
         break;
      case (3): tmp = RB_RXMAPPING_3_LENGTH;
         break;
      default: tmp = 0;
         break;
   }
   return tmp;
}

uint8 RB_Get_Tx_Mapping_Length(uint8 myBuf)
{
   uint8 tmp;
   switch (myBuf) {
      case (0): tmp = RB_TXMAPPING_0_LENGTH;
         break;
      case (1): tmp = RB_TXMAPPING_1_LENGTH;
         break;
      case (2): tmp = RB_TXMAPPING_2_LENGTH;
         break;
      case (3): tmp = RB_TXMAPPING_3_LENGTH;
         break;
      default: tmp = 0;
         break;
   }
   return tmp;
}

uint16 RB_Get_Tx_Map_Index(uint8 bufNum, uint8 mapCount)
{
   uint16 tmp;
   switch (bufNum) {
      case (0): tmp = RB_TXMAPPING_0[mapCount].index;
         break;
      case (1): tmp = RB_TXMAPPING_1[mapCount].index;
         break;
      case (2): tmp = RB_TXMAPPING_2[mapCount].index;
         break;
      case (3): tmp = RB_TXMAPPING_3[mapCount].index;
         break;
   }
   return tmp;
}

uint16 RB_Get_Rx_Map_Index(uint8 bufNum, uint8 mapCount)
{
   uint16 tmp;
   switch (bufNum) {
      case (0): tmp = RB_RXMAPPING_0[mapCount].index;
         break;
      case (1): tmp = RB_RXMAPPING_1[mapCount].index;
         break;
      case (2): tmp = RB_RXMAPPING_2[mapCount].index;
         break;
      case (3): tmp = RB_RXMAPPING_3[mapCount].index;
         break;
   }
   return tmp;
}

uint8 RB_Get_Tx_Map_Type(uint8 bufNum, uint8 mapCount)
{
   uint8 tmp;
   switch (bufNum) {
      case (0): tmp = RB_TXMAPPING_0[mapCount].data_type;
         break;
      case (1): tmp = RB_TXMAPPING_1[mapCount].data_type;
         break;
      case (2): tmp = RB_TXMAPPING_2[mapCount].data_type;
         break;
      case (3): tmp = RB_TXMAPPING_3[mapCount].data_type;
         break;
   }
   return tmp;
}

uint8 RB_Get_Rx_Map_Type(uint8 bufNum, uint8 mapCount)
{
   uint8 tmp;
   switch (bufNum) {
      case (0): tmp = RB_RXMAPPING_0[mapCount].data_type;
         break;
      case (1): tmp = RB_RXMAPPING_1[mapCount].data_type;
         break;
      case (2): tmp = RB_RXMAPPING_2[mapCount].data_type;
         break;
      case (3): tmp = RB_RXMAPPING_3[mapCount].data_type;
         break;
   }
   return tmp;
}

char* RB_Get_Tx_Map_Address(uint8 bufNum, uint8 mapCount)
{
   char* tmp;
   switch (bufNum) {
      case (0): tmp = RB_TXMAPPING_0[mapCount].address;
         break;
      case (1): tmp = RB_TXMAPPING_1[mapCount].address;
         break;
      case (2): tmp = RB_TXMAPPING_2[mapCount].address;
         break;
      case (3): tmp = RB_TXMAPPING_3[mapCount].address;
         break;
   }
   return tmp;
}

char* RB_Get_Rx_Map_Address(uint8 bufNum, uint8 mapCount)
{
   char* tmp;
   switch (bufNum) {
      case (0): tmp = RB_RXMAPPING_0[mapCount].address;
         break;
      case (1): tmp = RB_RXMAPPING_1[mapCount].address;
         break;
      case (2): tmp = RB_RXMAPPING_2[mapCount].address;
         break;
      case (3): tmp = RB_RXMAPPING_3[mapCount].address;
         break;
   }
   return tmp;
}