sensor_epsonCommon.c
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // sensor_epsonCommon.c - Epson IMU sensor protocol specific code common
4 // for all IMU models
5 //
6 //
7 // THE SOFTWARE IS RELEASED INTO THE PUBLIC DOMAIN.
8 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
9 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10 // NONINFRINGEMENT, SECURITY, SATISFACTORY QUALITY, AND FITNESS FOR A
11 // PARTICULAR PURPOSE. IN NO EVENT SHALL EPSON BE LIABLE FOR ANY LOSS, DAMAGE
12 // OR CLAIM, ARISING FROM OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF THE
13 // SOFTWARE.
14 //
15 //==============================================================================
16 #include "sensor_epsonCommon.h"
17 
18 #include "hcl.h"
19 #include "hcl_gpio.h"
20 
21 /*****************************************************************************
22 ** Function name: sensorHWReset
23 ** Description: Toggle the RESET pin, delay, wait for NOT_READY bit=0
24 ** This is only applicable on embedded platforms with
25 ** GPIO pin connected to IMU RESET#
26 ** Parameters: None
27 ** Return value: OK or NG
28 *****************************************************************************/
29 int sensorHWReset(void) {
30  unsigned int debug = FALSE;
31  unsigned short rxData;
32  unsigned short retryCount = 3000;
33 
34  gpioSet(EPSON_RESET); // RESET pin HIGH
36  gpioClr(EPSON_RESET); // Assert RESET (LOW)
38  gpioSet(EPSON_RESET); // Deassert RESET (HIGH)
40 
41  // Poll NOT_READY bit every 1msec until returns 0
42  // Exit after specified retries
43  do {
45  seDelayMicroSecs(1000);
46  retryCount--;
47  } while ((rxData & 0x0400) == 0x0400 && (retryCount != 0));
48 
49  if (retryCount == 0) {
50  printf("\r\n...Error: NOT_READY stuck HIGH.");
51  return NG;
52  }
53  return OK;
54 }
55 
56 /*****************************************************************************
57 ** Function name: sensorPowerOn
58 ** Description: Initial startup, Goto Config Mode (sanity),
59 ** Check for Hardware Error Flags
60 ** Parameters: None
61 ** Return value: OK or NG
62 *****************************************************************************/
63 int sensorPowerOn(void) {
64  unsigned short rxData = 0xFFFF;
65  unsigned int debug = FALSE;
66  unsigned short retryCount = 3000;
67 
68  // Safety Measure, Force Exit of Sampling Mode
69  do {
72  seDelayMicroSecs(1000);
73  retryCount--;
74  } while ((rxData & 0x0400) == 0x0000 && (retryCount != 0));
75 
76  if (retryCount == 0) {
77  printf("\r\n...Error: Stuck in Sampling Mode.");
78  return NG;
79  }
80 
81  // Hardware Reset if connected, and check for NOT_READY flag
82  if (!sensorHWReset()) {
83  return NG;
84  }
85 
86  // Check for error flags
87  rxData = registerRead16(CMD_WINDOW0, ADDR_DIAG_STAT, debug);
88  if (rxData == 0x0000)
89  return OK;
90  else
91  return NG;
92 }
93 
94 /*****************************************************************************
95 ** Function name: sensorStart
96 ** Description: Start sensor sampling (goto Sampling Mode)
97 ** Parameters: None
98 ** Return value: None
99 *****************************************************************************/
100 void sensorStart(void) {
101  unsigned int debug = FALSE;
102 
104  printf("\r\n...Sensor start.");
105 }
106 
107 /*****************************************************************************
108 ** Function name: sensorStop
109 ** Description: Stop sensor sampling (goto Config Mode)
110 ** Parameters: None
111 ** Return value: None
112 *****************************************************************************/
113 void sensorStop(void) {
114  unsigned int debug = FALSE;
115 
118  200000); // Provide 200msec for sensor to finish sending sample
119  printf("\r\n...Sensor stop.");
120 }
121 
122 /*****************************************************************************
123 ** Function name: sensorReset
124 ** Description: Send Software Reset to Sensor + Delay 800 msec
125 ** Parameters: None
126 ** Return value: None
127 *****************************************************************************/
128 void sensorReset(void) {
129  unsigned int debug = FALSE;
130 
131  printf("\r\n...Software Reset begin.");
134  printf("\r\n...Software Reset complete.");
135 }
136 
137 /*****************************************************************************
138 ** Function name: sensorFlashTest
139 ** Description: Send Flashtest command to Sensor and check status
140 ** NOTE: Not supported for V340PDD0
141 ** Parameters: None
142 ** Return value: OK or NG
143 *****************************************************************************/
144 int sensorFlashTest(void) {
145 #if defined V340PDD0
146  // always return OK
147 #else
148  unsigned int debug = FALSE;
149  unsigned short rxData;
150  unsigned short retryCount = 3000;
151 
152  printf("\r\n...Flash test begin.");
155  do {
156  rxData = registerRead16(CMD_WINDOW1, ADDR_MSC_CTRL_LO, debug);
157  retryCount--;
158  } while ((rxData & 0x0800) == 0x0800 && (retryCount != 0));
159  if (retryCount == 0) {
160  printf("\r\n...Error: Flashtest bit did not return to 0b.");
161  return NG;
162  }
163 
164  rxData = registerRead16(CMD_WINDOW0, ADDR_DIAG_STAT, debug);
165  printf("\r\n...Flash test complete.");
166 
167  if ((rxData & 0x0004) != 0x0000) return NG;
168 
169 #endif
170  return OK;
171 }
172 
173 /*****************************************************************************
174 ** Function name: sensorSelfTest
175 ** Description: Send SelfTest command to Sensor and check status
176 ** Parameters: None
177 ** Return value: OK or NG
178 *****************************************************************************/
179 int sensorSelfTest(void) {
180  unsigned int debug = FALSE;
181  unsigned short rxData;
182  unsigned short retryCount = 3000;
183 
184  printf("\r\n...Self test begin.");
187  do {
188  rxData = registerRead16(CMD_WINDOW1, ADDR_MSC_CTRL_LO, debug);
189  retryCount--;
190  } while ((rxData & 0x0400) == 0x0400 && (retryCount != 0));
191  if (retryCount == 0) {
192  printf("\r\n...Error: Self test bit did not return to 0b.");
193  return NG;
194  }
195 
196  rxData = registerRead16(CMD_WINDOW0, ADDR_DIAG_STAT, debug);
197  printf("\r\n...Self test complete.");
198 
199  if ((rxData & 0x0002) == 0x0000)
200  return OK;
201  else
202  return NG;
203 }
204 
205 /*****************************************************************************
206 ** Function name: sensorInitialBackup
207 ** Description: Send InitialBackup command (restore defaults) to Sensor
208 ** and check status
209 ** NOTE: Only supported for G365/G370
210 ** Parameters: None
211 ** Return value: OK or NG
212 *****************************************************************************/
214 #if defined G354PDH0 || defined G364PDCA || defined G364PDC0 || \
215  defined V340PDD0 || defined G320PDG0
216  // always return OK
217 #else
218  unsigned int debug = FALSE;
219  unsigned short rxData;
220  unsigned short retryCount = 3000;
221 
222  printf("\r\n...InitialBackup begin.");
225 
226  do {
227  rxData = registerRead16(CMD_WINDOW1, ADDR_GLOB_CMD_LO, debug);
228  retryCount--;
229  } while ((rxData & 0x0010) == 0x0010 && (retryCount != 0));
230  if (retryCount == 0) {
231  printf("\r\n...Error: InitialBackup bit did not return to 0b.");
232  return NG;
233  }
234 
235  printf("\r\n...Initial Backup complete.");
236 #endif
237  return OK;
238 }
239 
240 /*****************************************************************************
241 ** Function name: sensorDataByteLength
242 ** Description: Determines the sensor burst read packet data length
243 ** based on the parameters in the EpsonOptions struct.
244 ** Parameters: options - struct describing IMU configuration.
245 ** Return value: data byte length
246 *****************************************************************************/
247 unsigned int sensorDataByteLength(struct EpsonOptions options) {
248  unsigned int length = 0;
249 
250 #ifdef V340PDD0
251  // V340 has fixed packet format with optional 16-bit count value
252  length = 18;
253 
254 #else
255  // 16 bit ND_EA FLAG
256  if (options.flag_out) length += 2;
257 
258  // 16 or 32 bit Temperature Output
259  if (options.temp_out) {
260  if (options.temp_bit)
261  length += 4;
262  else
263  length += 2;
264  }
265 
266  // 16 or 32 bit Gyro X, Y, Z Output
267  if (options.gyro_out) {
268  if (options.gyro_bit)
269  length += 12;
270  else
271  length += 6;
272  }
273 
274  // 16 or 32 bit Accl X, Y, Z Output
275  if (options.accel_out) {
276  if (options.accel_bit)
277  length += 12;
278  else
279  length += 6;
280  }
281 
282  // 16 or 32 bit Delta Angle X, Y, Z Output
283  if (options.gyro_delta_out) {
284  if (options.gyro_delta_bit)
285  length += 12;
286  else
287  length += 6;
288  }
289 
290  // 16 or 32 bit Delta Velocity X, Y, Z Output
291  if (options.accel_delta_out) {
292  if (options.accel_delta_bit)
293  length += 12;
294  else
295  length += 6;
296  }
297 
298 #if defined G325PDF1 || defined G365PDC1 || defined G365PDF1 || \
299  defined G330PDG0 || defined G366PDG0
300  // 16 or 32 bit Quaternion1,2,3,4 Output
301  // NOTE: Only supported by G330/G366/G365PDCx/G365PDFx/G325PDFx
302  if (options.qtn_out) {
303  if (options.qtn_bit)
304  length += 16;
305  else
306  length += 8;
307  }
308  // 16 or 32 bit Attitude X, Y, Z Output
309  // NOTE: Only supported by G330/G366/G365PDCx/G365PDFx/G325PDFx
310  if (options.atti_out) {
311  if (options.atti_bit)
312  length += 12;
313  else
314  length += 6;
315  }
316 #endif
317 
318  // 16 bit GPIO status output
319  if (options.gpio_out) {
320  length += 2;
321  }
322  if (options.checksum_out) {
323  length += 2;
324  }
325 #endif
326 
327  // 16 bit Count output
328  if (options.count_out) {
329  length += 2;
330  }
331 
332 #if !defined SPI
333  // For Start and End byte when using UART interface
334  length += 2;
335 #endif
336 
337  return length;
338 }
339 
340 /*****************************************************************************
341 ** Function name: sensorDummyWrite
342 ** Description: Sets the WINDOW_ID of IMU to 0
343 ** This is a workaround to flush the UART port on embedded
344 ** Linux platform to prevent hanging if the first register
345 ** access is register read
346 ** Parameters: None
347 ** Return value: None
348 *****************************************************************************/
349 void sensorDummyWrite(void) {
350  unsigned int debug = FALSE;
351 
352  seDelayMicroSecs(100000);
353  registerWriteByteNoId(ADDR_WIN_CTRL, 0x00, debug);
354  seDelayMicroSecs(100000);
355  printf("\r\n...sensorDummyWrite.");
356 }
357 
358 /*****************************************************************************
359 ** Function name: getProductId
360 ** Description: Updates char array with Product ID ASCII
361 **
362 ** Parameters: pointer to String
363 ** Return value: pointer to String
364 *****************************************************************************/
365 char* getProductId(char* pcharArr) {
366  unsigned short prod_id1 = registerRead16(CMD_WINDOW1, ADDR_PROD_ID1, FALSE);
367  unsigned short prod_id2 = registerRead16(CMD_WINDOW1, ADDR_PROD_ID2, FALSE);
368  unsigned short prod_id3 = registerRead16(CMD_WINDOW1, ADDR_PROD_ID3, FALSE);
369  unsigned short prod_id4 = registerRead16(CMD_WINDOW1, ADDR_PROD_ID4, FALSE);
370 
371  pcharArr[0] = (char)prod_id1;
372  pcharArr[1] = (char)(prod_id1 >> 8);
373  pcharArr[2] = (char)prod_id2;
374  pcharArr[3] = (char)(prod_id2 >> 8);
375  pcharArr[4] = (char)prod_id3;
376  pcharArr[5] = (char)(prod_id3 >> 8);
377  pcharArr[6] = (char)prod_id4;
378  pcharArr[7] = (char)(prod_id4 >> 8);
379  pcharArr[8] = '\0';
380 
381  return (pcharArr);
382 }
383 
384 /*****************************************************************************
385 ** Function name: getSerialId
386 ** Description: Updates char array with Serial ID ASCII
387 **
388 ** Parameters: pointer to String
389 ** Return value: pointer to String
390 *****************************************************************************/
391 char* getSerialId(char* pcharArr) {
392  unsigned short ser_num1 =
394  unsigned short ser_num2 =
396  unsigned short ser_num3 =
398  unsigned short ser_num4 =
400 
401  pcharArr[0] = (char)ser_num1;
402  pcharArr[1] = (char)(ser_num1 >> 8);
403  pcharArr[2] = (char)ser_num2;
404  pcharArr[3] = (char)(ser_num2 >> 8);
405  pcharArr[4] = (char)ser_num3;
406  pcharArr[5] = (char)(ser_num3 >> 8);
407  pcharArr[6] = (char)ser_num4;
408  pcharArr[7] = (char)(ser_num4 >> 8);
409  pcharArr[8] = '\0';
410 
411  return pcharArr;
412 }
#define DELAY_EPSON_RESET
#define EPSON_POWER_ON_DELAY
unsigned int sensorDataByteLength(struct EpsonOptions options)
#define ADDR_MSC_CTRL_LO
#define ADDR_SERIAL_NUM1
int sensorFlashTest(void)
#define ADDR_SERIAL_NUM4
#define CMD_WINDOW1
#define CMD_SOFTRESET
char * getSerialId(char *pcharArr)
#define OK
Definition: hcl.h:29
#define ADDR_SERIAL_NUM3
#define ADDR_SERIAL_NUM2
#define NG
Definition: hcl.h:33
void gpioClr(__attribute__((unused)) uint8_t pin)
Definition: hcl_gpio.c:57
#define CMD_SELFTEST
#define ADDR_WIN_CTRL
#define CMD_WINDOW0
TF2SIMD_FORCE_INLINE tf2Scalar length(const Quaternion &q)
void sensorStop(void)
#define EPSON_SELF_TEST_DELAY
int sensorInitialBackup(void)
#define ADDR_MODE_CTRL_HI
#define EPSON_FLASH_TEST_DELAY
#define ADDR_MODE_CTRL_LO
void seDelayMicroSecs(uint32_t micros)
Definition: hcl_linux.c:57
#define FALSE
#define EPSON_RESET
Definition: hcl_gpio.h:28
unsigned short registerRead16(unsigned char, unsigned char, unsigned int)
#define ADDR_PROD_ID2
void registerWriteByte(unsigned char, unsigned char, unsigned char, unsigned int)
#define ADDR_GLOB_CMD_LO
int sensorHWReset(void)
#define CMD_END_SAMPLING
void registerWriteByteNoId(unsigned char, unsigned char, unsigned int)
void sensorStart(void)
#define ADDR_DIAG_STAT
#define CMD_FLASHTEST
#define ADDR_PROD_ID4
char * getProductId(char *pcharArr)
void seDelayMS(uint32_t millis)
Definition: hcl_linux.c:46
void gpioSet(__attribute__((unused)) uint8_t pin)
Definition: hcl_gpio.c:49
int sensorSelfTest(void)
int sensorPowerOn(void)
#define CMD_INITIAL_BACKUP
#define ADDR_MSC_CTRL_HI
#define ADDR_PROD_ID3
void sensorReset(void)
void sensorDummyWrite(void)
#define CMD_BEGIN_SAMPLING
#define ADDR_PROD_ID1


ess_imu_ros1_uart_driver
Author(s):
autogenerated on Sun Jun 4 2023 02:59:29