summaryrefslogtreecommitdiffstats
path: root/positioning_hal/src/LineSensDrv/LineSensDrv_Sensor.cpp
blob: 78ae48816383ba6c7262d03e911f47ca5f26aff9 (plain)
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
* @file LineSensDrv_Snesor.cpp
*/

/*---------------------------------------------------------------------------*/
// Include files

#include "LineSensDrv_Sensor.h"
#include "LineSensDrv_Api.h"

/*---------------------------------------------------------------------------*/
// Value Define

#define LSDRV_MASK_WORD_L                   0x00FF
#define LSDRV_MASK_WORD_U                   0xFF00

#define LINE_SENS_DRV_SENSOR_DEBUG_FACTORY  0
#define LINE_SENS_DRV_SENSOR_DEBUG_DIAG     0

#define VEHICLE_SNS_INFO_PULSE_NUM          32

/*---------------------------------------------------------------------------*/
// Global variable

static LSDRV_SPEEDKMPH_DAT    g_speed_kmph_data;
static uint8_t                g_rcv_data_len;     /* Receive SYS data length */
extern uint8_t g_uc_vehicle_reverse;
/*---------------------------------------------------------------------------*/
// Functions

/*******************************************************************************
 * MODULE    : LineSensDrv_Sensor
 * ABSTRACT  : Sensor data reception processing
 * FUNCTION  : Convert sensor data to delivery format
 * ARGUMENT  : *ucRcvdata : Data pointer
 * NOTE      :
 * RETURN    : None
 ******************************************************************************/
void LineSensDrvSensor(u_int8* uc_rcvdata) {
  u_int8              uc_sens_cnt = 0;
  u_int16             us_sp_kmph = 0;         /* Vehicle speed(km/h)      */
  u_int16             us_sp_pls1 = 0;         /* Total vehicle speed pulse(Latest)  */
  u_int16             us_sp_pls2 = 0;         /* Total vehicle speed pulse(Last time)  */
  u_int8              us_sp_ret = 0;          /* Last vehicle speed information acquisition result  */
  u_int8              uc_size = 0;            /* Size of the data      */
  u_int16             us_cnt = 0;             /* Data counter    */
  u_int16             us_cnt2 = 0;            /* Data counter    */
  u_int8*             uc_data_pos = NULL;     /* Data storage location    */
  LSDRV_MSG_LSDATA_G* p_snd_buf = NULL;
  LSDRV_LSDATA_G*     p_snd_data_base = NULL;
  LSDRV_LSDATA_G*     p_snd_data = NULL;
  RET_API             ret = RET_NORMAL;

  /* Receive sensor data top address acquisition */
  uc_data_pos = (uc_rcvdata);

  /* Create send buffer/delivery all received data */
  ret = _pb_GetZcSndBuf(PNO_VEHICLE_SENSOR, reinterpret_cast<void**>(&p_snd_buf));
  if ((ret == RET_NORMAL) && (p_snd_buf != NULL)) {
    p_snd_data_base = p_snd_buf->st_para.st_data;
    (void)memset(p_snd_data_base, 0, LSDRV_KINDS_MAX * sizeof( LSDRV_LSDATA_G ));

    /* Sensor counter */
    p_snd_data              = p_snd_data_base + LSDRV_SNS_COUNTER;
    uc_sens_cnt             = (u_int8)*(uc_data_pos);
    p_snd_data->ul_did      = VEHICLE_DID_SNS_COUNTER;    /* Data ID       */
    p_snd_data->uc_size     = LSDRV_SNDMSG_DTSIZE_1;      /* Size of the data   */
    p_snd_data->uc_sns_cnt  = uc_sens_cnt;                /* Sensor counter */
    p_snd_data->uc_data[0]  = uc_sens_cnt;                /* Data content     */
    uc_data_pos             = (uc_data_pos + LSDRV_SNDMSG_DTSIZE_1);

    /* Gyro output */
    p_snd_data              = p_snd_data_base + LSDRV_GYRO_EXT;
    p_snd_data->ul_did      = VEHICLE_DID_GYRO_EXT;        /* Data ID       */
    p_snd_data->uc_size     = LSDRV_SNDMSG_DTSIZE_20;      /* Size of the data   */
    p_snd_data->uc_sns_cnt  = uc_sens_cnt;                 /* Sensor counter */
    for (us_cnt = 0; us_cnt < LSDRV_SNDMSG_DTSIZE_20; us_cnt++) {
      /* Since [0] is older and [9] is newer, the order of received data is switched. */
      /* Be in reverse order for endian conversion */
      p_snd_data->uc_data[LSDRV_SNDMSG_DTSIZE_20 - (us_cnt + 1)] = (u_int8)*(uc_data_pos + us_cnt);
    }
    uc_data_pos = (uc_data_pos + LSDRV_SNDMSG_DTSIZE_20);

    p_snd_data              = p_snd_data_base + LSDRV_GYRO_X;
    p_snd_data->ul_did      = VEHICLE_DID_GYRO;             /* Data ID       */
    p_snd_data->uc_size     = LSDRV_SNDMSG_DTSIZE_20;       /* Size of the data   */
    p_snd_data->uc_sns_cnt  = uc_sens_cnt;                  /* Sensor counter */
    for (us_cnt = 0; us_cnt < LSDRV_SNDMSG_DTSIZE_20; us_cnt++) {
      /* Since [0] is older and [9] is newer, the order of received data is switched. */
      /* Be in reverse order for endian conversion */
      p_snd_data->uc_data[LSDRV_SNDMSG_DTSIZE_20 - (us_cnt + 1)] = (u_int8)*(uc_data_pos + us_cnt);
    }
    uc_data_pos = (uc_data_pos + LSDRV_SNDMSG_DTSIZE_20);

    /* Reverse flag */
    p_snd_data              = p_snd_data_base + LSDRV_REV;
    p_snd_data->ul_did      = VEHICLE_DID_REV;              /* Data ID       */
    p_snd_data->uc_size     = LSDRV_SNDMSG_DTSIZE_1;        /* Size of the data   */
    p_snd_data->uc_sns_cnt  = uc_sens_cnt;                  /* Sensor Counter */
    p_snd_data->uc_data[0] = g_uc_vehicle_reverse;
    /* Gyro Temperature */
    p_snd_data              = p_snd_data_base + LSDRV_GYRO_TEMP;
    p_snd_data->ul_did      = VEHICLE_DID_GYRO_TEMP;        /* Data ID       */
    p_snd_data->uc_size     = LSDRV_SNDMSG_DTSIZE_2;        /* Size of the data   */
    p_snd_data->uc_sns_cnt  = uc_sens_cnt;                  /* Sensor counter */
    p_snd_data->uc_data[1]  = (u_int8)(*(uc_data_pos    ) & (u_int8)(LSDRV_TEMP_MASK >> 8));
    p_snd_data->uc_data[0]  = (u_int8)(*(uc_data_pos + 1) & (u_int8)(LSDRV_TEMP_MASK));
    uc_data_pos = (uc_data_pos + LSDRV_SNDMSG_DTSIZE_2);

    /* Vehicle speed pulse */
    p_snd_data              = p_snd_data_base + LSDRV_SPEED_PULSE;
    p_snd_data->ul_did      = VEHICLE_DID_SPEED_PULSE;      /* Data ID       */
    p_snd_data->uc_size     = LSDRV_SNDMSG_DTSIZE_20;       /* Size of the data   */
    p_snd_data->uc_sns_cnt  = uc_sens_cnt;                  /* Sensor counter */
    for (us_cnt = 0; us_cnt < LSDRV_SNDMSG_DTSIZE_20; us_cnt++) {
      /* Since [0] is older and [9] is newer, the order of received data is switched. */
      /* Be in reverse order for endian conversion */
      p_snd_data->uc_data[LSDRV_SNDMSG_DTSIZE_20 - (us_cnt + 1)] = (u_int8)*(uc_data_pos + us_cnt);
    }
    uc_data_pos = (uc_data_pos + LSDRV_SNDMSG_DTSIZE_20);

    /* Vehicle speed(km/h) */
    p_snd_data              = p_snd_data_base + LSDRV_SPEED_PULSE;
    us_sp_kmph              = 0;
    us_sp_pls1              = (u_int16)p_snd_data->uc_data[1];
    us_sp_pls1              = (u_int16)((us_sp_pls1 << 8) | p_snd_data->uc_data[0]);
    us_sp_ret               = LineSensDrvGetLastSpeedPulse(&us_sp_pls2, us_sp_pls1, uc_sens_cnt);

    LineSensDrvSetLastSpeedPulse(us_sp_pls1, uc_sens_cnt);

    p_snd_data            = p_snd_data_base + LSDRV_SPEED_KMPH;
    if (us_sp_ret != LSDRV_SPKMPH_INVALID) {
      /* Vehicle speed pulse before 100 ms is valid */
      LineSensDrvSpeedPulseSave(us_sp_pls1, us_sp_pls2, uc_sens_cnt);
      us_sp_kmph = LineSensDrvSpeedCalc(uc_sens_cnt);
      /* The size can be set only when the vehicle speed [km/h] is calculated. "0" is notified to the vehicle sensor when the size cannot be set. */
      p_snd_data->uc_size  = LSDRV_SNDMSG_DTSIZE_2;      /* Size of the data   */
    }
    p_snd_data->ul_did      = VEHICLE_DID_SPEED_KMPH;       /* Data ID       */
    p_snd_data->uc_sns_cnt  = uc_sens_cnt;                  /* Sensor counter */
    p_snd_data->uc_data[0]  = (u_int8)(us_sp_kmph & 0x00FF);
    p_snd_data->uc_data[1]  = (u_int8)(us_sp_kmph >> 8);

    POSITIONING_LOG("[LOG, %d(cnt), %d(km/h), %d(pls1), %d(pls2)]  \r\n",
      uc_sens_cnt,
      us_sp_kmph,
      us_sp_pls1,
      us_sp_pls2);

    /* G-Sensor X-axes */
    p_snd_data              = p_snd_data_base + LSDRV_GSENSOR_X;
    p_snd_data->ul_did      = VEHICLE_DID_GSNS_X;           /* Data ID       */
    p_snd_data->uc_size     = LSDRV_SNDMSG_DTSIZE_20;       /* Size of the data   */
    p_snd_data->uc_sns_cnt  = uc_sens_cnt;                  /* Sensor counter */
    us_cnt2                 = p_snd_data->uc_size - 1;
    for (us_cnt = 0; us_cnt < 10; us_cnt++) {
      /* Since [0] is older and [9] is newer, the order of received data is switched. */
      p_snd_data->uc_data[us_cnt2    ] = (u_int8)*( uc_data_pos + (sizeof(SENSORINPUT_INFO_DAT_GSENS) * us_cnt)    );
      p_snd_data->uc_data[us_cnt2 - 1] = (u_int8)*( uc_data_pos + (sizeof(SENSORINPUT_INFO_DAT_GSENS) * us_cnt) + 1);
      us_cnt2 = us_cnt2 - 2;
    }

    /* G-Sensor Y-axes */
    p_snd_data              = p_snd_data_base + LSDRV_GSENSOR_Y;
    p_snd_data->ul_did      = VEHICLE_DID_GSNS_Y;           /* Data ID       */
    p_snd_data->uc_size     = LSDRV_SNDMSG_DTSIZE_20;       /* Size of the data   */
    p_snd_data->uc_sns_cnt  = uc_sens_cnt;                  /* Sensor counter */
    us_cnt2                 = p_snd_data->uc_size - 1;
    for (us_cnt = 0; us_cnt < 10; us_cnt++) {
      /* Since [0] is older and [9] is newer, the order of received data is switched. */
      p_snd_data->uc_data[us_cnt2] = (u_int8)*( (uc_data_pos + sizeof(u_int16)) \
                                           + (sizeof(SENSORINPUT_INFO_DAT_GSENS) * us_cnt));

      p_snd_data->uc_data[us_cnt2-1] = (u_int8)*( (uc_data_pos + sizeof(u_int16)) \
                                           + (sizeof(SENSORINPUT_INFO_DAT_GSENS) * us_cnt) + 1);
      us_cnt2 = us_cnt2 - 2;
    }
    uc_data_pos = ( uc_data_pos + (sizeof(SENSORINPUT_INFO_DAT_GSENS) * 10) );

    /* Inter-Pulse time */
    p_snd_data              = p_snd_data_base + LSDRV_PULSE_TIME;
    p_snd_data->ul_did      = VEHICLE_DID_PULSE_TIME;       /* Data ID       */
    p_snd_data->uc_size     = LSDRV_SNDMSG_DTSIZE_132;      /* Size of the data   */
    p_snd_data->uc_sns_cnt  = uc_sens_cnt;                  /* Sensor counter */

    /* Clear the buffer for variable length */
    (void)memset(&p_snd_data->uc_data[0], 0x00, sizeof(p_snd_data->uc_data));

    if (g_rcv_data_len == LSDRV_PLSTIME_LEN) {
      /* Inter-Pulse time (number of items + time). The number of items is stored at the beginning. */
      uc_size = (u_int8)*uc_data_pos;
      if (uc_size > VEHICLE_SNS_INFO_PULSE_NUM) {
        uc_size = VEHICLE_SNS_INFO_PULSE_NUM;
      }
      p_snd_data->uc_data[0] = uc_size;
      p_snd_data->uc_data[1] = 0x00;
      p_snd_data->uc_data[2] = 0x00;
      p_snd_data->uc_data[3] = 0x00;
      uc_data_pos = ( uc_data_pos + sizeof(u_int8) );

      /* Since [0] is old and [31] is new in the received time, the order is changed. */
      /* Be in reverse order for endian conversion */
      for (us_cnt = 0; us_cnt < (uc_size * sizeof(u_int32)); us_cnt++) {
        p_snd_data->uc_data[(uc_size * sizeof(u_int32)) - (us_cnt + 1) + 4] = (u_int8)*(uc_data_pos + us_cnt);
      }
    }

    /* Messaging */
    DeliveryLineSensorDataPositioning(p_snd_buf, LSDRV_KINDS_MAX);
  }

  return;
}

/*******************************************************************************
 * MODULE    : LineSensDrv_SpeedCalc
 * ABSTRACT  : Vehicle speed calculation processing for sensor data
 * FUNCTION  : Calculate vehicle speed for sensor data
 * ARGUMENT  : uc_sens_cnt : Sensor counter
 * NOTE      :
 * RETURN    : Vehicle speed(0.01km/h)
 ******************************************************************************/
u_int16 LineSensDrvSpeedCalc(u_int8 uc_sens_cnt) {
  u_int32 ul_sp_caluc = 0;          /* Vehicle speed(2^-8km/h)  */
  u_int16 us_speed = 0;             /* Vehicle speed(km/h)    */
  u_int8  uc_ptr = 0;               /* Data storage pointer for sensor data  */  /* #010 */
  u_int8  uc_ptr2 = 0;              /* Data storage pointer for sensor data  */  /* #010 */
  u_int32 ul_work = 0;              /* Variables for unsigned 32-bit type calculations      */  /* #010 */
  double  d_work = 0.0;             /* Variables for calculating double types          */  /* #010 */  /* Ignore -> MISRA-C++:2008 Rule 3-9-2 */
  u_int16 us_sens_cnt_search = 0;   /* Sensor counter to be searched        */  /* #010 */
  u_int16 us_sens_cnt_ref = 0;      /* Sensor counters to be compared      */  /* #010 */
  int32   i = 0;                    /* Generic counters              */  /* #010 */
  int32   j = 0;                    /* Generic counters              */  /* #010 */
  u_int16 us_offset = 0;            /* Offset value              */  /* #010 */

  /* #016 start */
  /* Is the number of data that can be calculated at the vehicle speed already received? */
  if (LSDRV_PLSDATA_NRCV == g_speed_kmph_data.uc_calc_start) {
    /* Do not compute if there is not enough data. */
  } else {
    if (0 == g_speed_kmph_data.uc_ptr) {
      uc_ptr  = LSDRV_SPKMPH_TBL_NUM - 1;
      uc_ptr2 = LSDRV_SPKMPH_TBL_NUM - 1;
    } else {
      uc_ptr  = g_speed_kmph_data.uc_ptr - 1;  /* Ignore -> MISRA-C++:2008 Rule 5-0-5 */
      uc_ptr2 = g_speed_kmph_data.uc_ptr - 1;  /* Ignore -> MISRA-C++:2008 Rule 5-0-5 */
    }

    ul_work = 0;
    if (LSDRV_SPKMPH_AVE_TIME > uc_sens_cnt) {
      us_offset = LSDRV_SENSCNT_BRW_ADD;
    } else {
      us_offset = 0;
    }

    us_sens_cnt_search = (u_int16)uc_sens_cnt + us_offset - LSDRV_SPKMPH_AVE_TIME;

    for (i = 0; i < LSDRV_SPKMPH_TBL_NUM; i++) {
      /* Invalid data is detected, and the search is completed. */
      if (LSDRV_SPKMPH_DATA_DIS == g_speed_kmph_data.st_data[uc_ptr].uc_flag) {
        break;
        }

      /* When the sensor counter is 29 or less, the borrow is considered. */
      if (LSDRV_SPKMPH_AVE_TIME > g_speed_kmph_data.st_data[uc_ptr].uc_sens_cnt) {
        us_sens_cnt_ref = g_speed_kmph_data.st_data[uc_ptr].uc_sens_cnt + us_offset;
      } else {
        us_sens_cnt_ref = g_speed_kmph_data.st_data[uc_ptr].uc_sens_cnt;
      }

      /* Checking the sensor counter to finish search */
      if (us_sens_cnt_search >= us_sens_cnt_ref) {
        break;
      }

      /* Add to calculate average value */
      ul_work += (u_int32)g_speed_kmph_data.st_data[uc_ptr].us_speed_pulse;

      if (0 == uc_ptr) {
        uc_ptr = LSDRV_SPKMPH_TBL_NUM - 1;                /* To the end of the data table */
      } else {
        uc_ptr--;                            /* To the previous data */
      }
    }

    /* Averaging computation */
    if (0 == i) {
      d_work = 0;
    } else {
      if (ul_work == 1) {
        for (j = 0; j < LSDRV_SPKMPH_TBL_NUM; j++) {
          if ((g_speed_kmph_data.st_data[uc_ptr2].us_speed_pulse == 1)
            && (g_speed_kmph_data.st_data[uc_ptr2].uc_noise_flag == 1)) {
            ul_work = 0;
            break;
          } else if (g_speed_kmph_data.st_data[uc_ptr2].us_speed_pulse == 1 &&
            g_speed_kmph_data.st_data[uc_ptr2].uc_noise_flag == 0) {
            ul_work = 1;
            break;
          } else {
            /* nop */
          }

          /* Borrow actions for pointer values */
          if (0 == uc_ptr2) {
            uc_ptr2 = LSDRV_SPKMPH_TBL_NUM - 1;                /* To the end of the data table */
          } else {
            uc_ptr2--;                            /* To the previous data */
          }
        }
      }

      d_work = static_cast<double>(ul_work);
      d_work = d_work / static_cast<double>(i);
      d_work = d_work * static_cast<double>(LSDRV_SENS_COEFFICIENT);
      d_work = d_work * 100;               /* [1km/h] -> [0.01km/h] */
      d_work = d_work + 0.5;              /* Preparation for rounding */
    }

    ul_sp_caluc = static_cast<int32>(d_work);

    /* When the vehicle speed calculation result overflows, the upper limit value is used for clipping. */
    if (LSDRV_PLSMAX - 1 >= ul_sp_caluc) {
      us_speed = (u_int16)ul_sp_caluc;
    } else {
      us_speed = (u_int16)(LSDRV_PLSMAX - 1);
    }
  }

  return us_speed;
}

/*******************************************************************************
 * MODULE    : LineSensDrv_SpeedKmphDataInit
 * ABSTRACT  : Data table initialization process for vehicle speed calculation
 * FUNCTION  : Initialize the data table for calculating the vehicle speed
 * ARGUMENT  : None
 * NOTE      :
 * RETURN    : None
 ******************************************************************************/
void LineSensDrvSpeedKmphDataInit(void) {
  int32 i = 0;

  memset(reinterpret_cast<void*>(&g_speed_kmph_data), static_cast<int32>(0), (size_t)sizeof(g_speed_kmph_data));

  /* Disable all data storage flags */
  for (i = 0; i < LSDRV_SPKMPH_TBL_NUM; i++) {
    g_speed_kmph_data.st_data[i].uc_flag = LSDRV_SPKMPH_DATA_DIS;
  }

  return;
}

/*******************************************************************************
 * MODULE    : LineSensDrv_SpeedPulseSave
 * ABSTRACT  : Sensor data vehicle speed pulse save processing
 * FUNCTION  : Saving the vehicle speed pulse of the sensor data to the data table for calculating the vehicle speed 
 * ARGUMENT  : us_sp1     : Vehicle speed pulse of the latest information
 *           : us_sp2     : Vehicle speed pulse of the previous information
 *           : uc_sens_cnt : Sensor counter
 * NOTE      :
 * RETURN    : None
 ******************************************************************************/
void LineSensDrvSpeedPulseSave(u_int16 us_sp1, u_int16 us_sp2, u_int8 uc_sens_cnt) {
  u_int16 us_sp_diff = 0;     /* Vehicle speed pulse difference  */
  u_int8  uc_ptr = 0;         /* Data storage pointer for sensor data  */  /* #010 */
  u_int8  us_last_ptr = 0;    /* Last pointer              */  /* #010 */
  int32   i = 0;              /* Generic counters              */  /* #010 */
  u_int8  uc_fstsns_cnt = 0;  /* Initial sensor data sensor counter value  */  /* #016 */
  int32   l_data_num = 0;     /* Number of registered data              */  /* #016 */
  u_int8  uc_noise_flag = 0;  /* Noise flag              */  /* #017 */

  /* Calculate the vehicle speed pulse difference between the latest and last 100ms information */
  if (us_sp1 >= us_sp2) {
    /* The cumulative pulse number of the latest information is larger. */
    us_sp_diff = us_sp1 - us_sp2;  /* Ignore -> MISRA-C++:2008 Rule 5-0-5 */
  } else {
    /* The cumulative pulse number of the latest information is smaller (the accumulated pulse overflows) */
    us_sp_diff = (LSDRV_PLSMAX - us_sp2) + us_sp1;  /* Ignore -> MISRA-C++:2008 Rule 5-0-5 */
  }

  /* Call noise check only if us_sp_diff is 1 */
  if (us_sp_diff == 1) {
    uc_noise_flag = LineSensDrvCheckNoise(uc_sens_cnt);
  }

  /* Saving sensor data vehicle speed pulse in data table for vehicle speed calculation */
  if (LSDRV_PLSDATA_NRCV == g_speed_kmph_data.uc_sns_rcv) {
    if (LSDRV_PLSDATA_RCV == g_speed_kmph_data.uc_fstsns_rcv) {
      /* If the sensor data has already been received for the first time, set the temporary sensor counter value when the sensor data has been saved for the first time. */
      uc_fstsns_cnt = uc_sens_cnt;
      for (i = (LSDRV_SPKMPH_TBL_NUM - 1); i >= 0 ; i--) {
        if (LSDRV_SPKMPH_DATA_EN == g_speed_kmph_data.st_data[i].uc_flag) {
          /* Data storage flag is valid */
          if (0 != uc_fstsns_cnt) {
            uc_fstsns_cnt--;
          } else {
            uc_fstsns_cnt = LSDRV_SENSCNT_MAX;
          }

          g_speed_kmph_data.st_data[i].uc_sens_cnt = uc_fstsns_cnt;
        }
      }
    }

    /* Sensor data reception status  <-  "Received" */
    g_speed_kmph_data.uc_sns_rcv = LSDRV_PLSDATA_RCV;
  }

  uc_ptr = g_speed_kmph_data.uc_ptr;

  /* If the sensor counter is the same as the previous one, overwrite update */
  if (0 == uc_ptr) {
    us_last_ptr = LSDRV_SPKMPH_TBL_NUM - 1;
  } else {
    us_last_ptr = uc_ptr - 1;   /* Ignore -> MISRA-C++:2008 Rule 5-0-5 */
  }

  if (g_speed_kmph_data.st_data[us_last_ptr].uc_sens_cnt == uc_sens_cnt) {
    /* Next update of the data storage location */
    if (0 == g_speed_kmph_data.uc_ptr) {
      g_speed_kmph_data.uc_ptr = LSDRV_SPKMPH_TBL_NUM - 1;
    } else {
      g_speed_kmph_data.uc_ptr--;
    }

    uc_ptr = g_speed_kmph_data.uc_ptr;
  }

  /* Input into data table for calculation of vehicle speed */
  g_speed_kmph_data.st_data[uc_ptr].uc_flag         = LSDRV_SPKMPH_DATA_EN; /* Data validity      */
  g_speed_kmph_data.st_data[uc_ptr].uc_sens_cnt     = uc_sens_cnt;          /* Sensor counter input  */
  g_speed_kmph_data.st_data[uc_ptr].us_speed_pulse  = us_sp_diff;           /* Vehicle speed pulse difference    */
  g_speed_kmph_data.st_data[uc_ptr].uc_noise_flag   = uc_noise_flag;        /* Noise flag      */ /* #017 */

  /* Next update of the data storage location */
  if ((LSDRV_SPKMPH_TBL_NUM - 1) <= g_speed_kmph_data.uc_ptr) {
    g_speed_kmph_data.uc_ptr = 0;
  } else {
    g_speed_kmph_data.uc_ptr++;
  }

  /* Determine whether the vehicle speed can be calculated. */
  if (g_speed_kmph_data.uc_calc_start == LSDRV_PLSDATA_NRCV) {
    /* Data count detection */
    l_data_num = 0;
    for (i = 0; i < LSDRV_SPKMPH_TBL_NUM; i++) {
      if (LSDRV_SPKMPH_DATA_EN == g_speed_kmph_data.st_data[i].uc_flag) {
          l_data_num++;
      }
    }

    if (LSDRV_SPKMPH_MIN_DATA_N <= l_data_num) {
      /* Vehicle Speed Calculation Required Data Number Received */
      g_speed_kmph_data.uc_calc_start = LSDRV_PLSDATA_RCV;
    }
  }

  return;
}

/*******************************************************************************
 * MODULE    : LineSensDrv_CheckNoise
 * ABSTRACT  : Sensor data noise check processing
 * FUNCTION  : The vehicle speed pulse is saved in the data table for the vehicle speed calculation. 
 * ARGUMENT  : uc_sens_cnt     : Sensor counter of the latest data
 * NOTE      :
 * RETURN    : Noise flag
 ******************************************************************************/
u_int8 LineSensDrvCheckNoise(u_int8 uc_sens_cnt) {
  int32   i = 0;                  /* Generic counters */
  u_int16 us_sens_cnt_search = 0; /* Sensor counter to be searched*/
  u_int8  uc_ptr = 0;             /* Data storage pointer */
  u_int16 us_offset = 0;          /* Offset value  */
  u_int8  noise_flag = 0;         /* Noise flag  */
  u_int16 us_sens_cnt_ref = 0;    /* Sensor counters to be compared */

  /* If there is no point where the difference in vehicle speed pulse is 1 or more 
     between -1 and -14 of sensor counter of the latest data, 
     set the noise flag of the latest data to 1. */
  /* Set the noise flag to 1 */
  noise_flag = 1;

  /* The Target is the one before the storage location of the latest data. */
  if (0 == g_speed_kmph_data.uc_ptr) {
    uc_ptr = LSDRV_SPKMPH_TBL_NUM - 1;
  } else {
    uc_ptr = g_speed_kmph_data.uc_ptr - 1;  /* Ignore -> MISRA-C++:2008 Rule 5-0-5 */
  }

  if (LSDRV_SPKMPH_NOISE_TIME > uc_sens_cnt) {
    us_offset = LSDRV_SENSCNT_BRW_ADD;
  } else {
    us_offset = 0;
  }

  us_sens_cnt_search = (u_int16)uc_sens_cnt + us_offset - LSDRV_SPKMPH_NOISE_TIME;

  for (i = 0; i < LSDRV_SPKMPH_NOISE_TIME; i++) {
    /* When the sensor counter is 15 or less, the borrow is considered. */
    if (LSDRV_SPKMPH_NOISE_TIME > g_speed_kmph_data.st_data[uc_ptr].uc_sens_cnt) {
      us_sens_cnt_ref = g_speed_kmph_data.st_data[uc_ptr].uc_sens_cnt + us_offset;
    } else {
      us_sens_cnt_ref = g_speed_kmph_data.st_data[uc_ptr].uc_sens_cnt;  /* Ignore -> MISRA-C++:2008 Rule 5-0-5 */
    }

    /* Checking the sensor Counter to Finish Search */
    if (us_sens_cnt_ref <= us_sens_cnt_search) {
      noise_flag = 1;
      break;
    } else {
      if (g_speed_kmph_data.st_data[uc_ptr].us_speed_pulse >= 1) {
        noise_flag = 0;
        break;
      }
    }

    /* Borrow actions for pointer values */
    if (0 == uc_ptr) {
      uc_ptr = LSDRV_SPKMPH_TBL_NUM - 1;                /* To the end of the data table */
    } else {
      uc_ptr--;                            /* To the previous data */
    }
  }

  return noise_flag;
}

/**
 * @brief
 *  Return the last (100ms ago) vehicle speed pulse 
 *
 * @param[out]  us_sp2    Last vehicle speed pulse
 * @param[in]  us_sp1    The latest vehicle speed pulse
 * @param[in]  uc_sens_cnt  Latest sensor counter
 *
 * @return  LSDRV_SPKMPH_INVALID  Vehicle speed pulse information valid<br>
 *      LSDRV_SPKMPH_VALID    Vehicle speed pulse information invalid
 */
u_int8 LineSensDrvGetLastSpeedPulse(u_int16* us_sp2, u_int16 us_sp1, u_int8 uc_sens_cnt) {
  u_int8  ret = LSDRV_SPKMPH_INVALID;   /* Return value        */
  u_int16 sp_pls_diff = 0;              /* Vehicle speed pulse difference    */
  u_int16 sp_pls = 0;                   /* Vehicle speed pulse every 100 ms  */
  u_int8  cnt_diff = 0;                 /* Sensor counter difference  */

  /* Check if the last vehicle speed pulse has been set  */
  if (g_speed_kmph_data.st_last_data.uc_flag == LSDRV_SPKMPH_DATA_EN) {
    /* Differential calculation of sensor counter */
    if (uc_sens_cnt >= g_speed_kmph_data.st_last_data.uc_sens_cnt) {
      /* Latest sensor counter is larger */
      cnt_diff = uc_sens_cnt - g_speed_kmph_data.st_last_data.uc_sens_cnt;
    } else {
      /* Last sensor counter is larger(sensor counter overflows) */
      cnt_diff = (LSDRV_SENSCNT_MAX - g_speed_kmph_data.st_last_data.uc_sens_cnt) + uc_sens_cnt + 1;
  }

  /* Check if sensor counter is continuous */
  if (cnt_diff <= 1) {
    /* Continuous or same as the previous one, so the previous (100ms previous) vehicle speed pulse is set as it is */
    *us_sp2 = g_speed_kmph_data.st_last_data.us_speed_pulse;
  } else {
    /* Determine the vehicle speed pulse 100ms ago from the average considering the skipped portion because it is not consecutive. */
    if (us_sp1 >= g_speed_kmph_data.st_last_data.us_speed_pulse) {
      /* Larger latest cumulative vehicle speed pulse */
      sp_pls_diff = us_sp1 - g_speed_kmph_data.st_last_data.us_speed_pulse;
      } else {
        /* Last cumulative vehicle speed pulse is larger(Cumulative vehicle speed pulse overflows) */
        sp_pls_diff = (LSDRV_PLSMAX - g_speed_kmph_data.st_last_data.us_speed_pulse) + us_sp1;
      }

      /* Calculate average vehicle speed pulse including skip period */
      sp_pls = (u_int16)(sp_pls_diff / cnt_diff);

      /* Calculate the vehicle speed pulse 100ms ahead from the vehicle speed average */
      if (us_sp1 >= sp_pls) {
        /* Does not overflow even if the 100ms vehicle speed pulse is pulled from the latest one. */
        *us_sp2 = us_sp1 - sp_pls;
      } else {
        /* Subtracting a 100ms vehicle speed pulse from the latest one overflows */
        *us_sp2 = LSDRV_PLSMAX - (sp_pls - us_sp1);
      }
    }

    ret = LSDRV_SPKMPH_VALID;
  }

  return ret;
}

/**
 * @brief
 *  Return the last (100ms ago) vehicle speed pulse 
 *
 * @param[in]  us_sp      Vehicle speed pulse
 * @param[in]  uc_sens_cnt  Sensor counter
 */
void LineSensDrvSetLastSpeedPulse(u_int16 us_sp, u_int8 uc_sens_cnt) {
  /* Vehicle speed pulse information valid setting */
  g_speed_kmph_data.st_last_data.uc_flag = LSDRV_SPKMPH_DATA_EN;
  /* Sensor counter setting */
  g_speed_kmph_data.st_last_data.uc_sens_cnt = uc_sens_cnt;
  /* Vehicle speed pulse setting */
  g_speed_kmph_data.st_last_data.us_speed_pulse = us_sp;
}

/*---------------------------------------------------------------------------*/
/*EOF*/