firmware  v0.1.2
Chromation Spectrometer Dev-Kit
test_Lis.c
1 #include "unity.h"
2 #include "Mock.h"
3 #include "test_Lis.h"
4 #include "Lis.h"
5 
6 /* =====[ Test Helpers ]===== */
7 static void _AssertCall(uint16_t num, char const * name)
8 {
10  // Put num and name in the message displayed if test fails
11  GString *message = g_string_new(NULL);
12  g_string_append_printf(message, "`%s` is not call %d", name, num);
13  // Perform the test
14  TEST_ASSERT_TRUE_MESSAGE(
15  AssertCall(mock, num, name),
16  message->str
17  );
18  // Free memory used by GString
19  g_string_free(message, true);
20 }
21 static void _AssertArg(uint16_t call_n, uint8_t arg_n, void *pval)
22 {
24 
61  GString *msg = g_string_new(NULL);
62  g_string_printf(msg, "Expect different value for call %d arg %d.", call_n, arg_n);
63  // I cannot print the expected value without asking the
64  // caller to include the type as a string. Better to keep the
65  // arg list short. Call and arg number are good enough.
66  TEST_ASSERT_TRUE_MESSAGE(
67  AssertArg(mock, call_n, arg_n, pval),
68  msg->str
69  );
70  g_string_free(msg, true);
71 }
72 /* =====[ _setup_bit_val ]===== */
73 enum bit_val {LOW, HIGH}; typedef enum bit_val bit_val;
74 static void _setup_bit_val(lis_ptr reg, lis_bit bit, bit_val v)
75 {
77 
87  GString *msg = g_string_new(NULL);
88  g_string_printf(msg, "Bit must be %s when the test starts", v ? "HIGH" : "LOW");
89  if (HIGH == v)
90  {
91  SetBit(reg, bit);
92  TEST_ASSERT_BIT_HIGH_MESSAGE(bit, *reg, msg->str);
93  }
94  else if (LOW == v)
95  {
96  ClearBit(reg, bit);
97  TEST_ASSERT_BIT_LOW_MESSAGE(bit, *reg, msg->str);
98  }
99  else
100  {
101  g_string_printf(msg, "Test setup with invalid bit value: %d? ", v);
102  g_string_append_printf(msg, "Bit value must be LOW or HIGH.");
103  TEST_FAIL_MESSAGE(msg->str);
104  }
105  g_string_free(msg, true);
106 }
107 static void _setup_bit_val_msg(lis_ptr reg, lis_bit bit, bit_val v,
108  char * bit_name)
109 {
111 
121  // Put bit_name in the message displayed if test fails
122  GString *message = g_string_new(NULL);
123  g_string_append_printf(
124  message, "`%s` must be ", bit_name
125  );
126  g_string_append_printf(
127  message, "%s when the test starts.",
128  v ? "HIGH" : "LOW"
129  );
130  if (HIGH == v)
131  {
132  SetBit(reg, bit);
133  TEST_ASSERT_BIT_HIGH_MESSAGE(bit, *reg, message->str);
134  }
135  else if (LOW == v)
136  {
137  ClearBit(reg, bit);
138  TEST_ASSERT_BIT_LOW_MESSAGE(bit, *reg, message->str);
139  }
140  else
141  {
142  g_string_printf(message, "Test setup is invalid. `%s`", bit_name);
143  g_string_append_printf(message, " = %d? ", v);
144  g_string_append_printf(message, "Bit value must be LOW or HIGH.");
145  TEST_FAIL_MESSAGE(message->str);
146  }
147  g_string_free(message, true);
148 }
149 
150 /* =====[ _test_bit_val ]===== */
151 static void _test_bit_val( lis_ptr reg, lis_bit bit, bit_val v )
152 {
154  GString *msg = g_string_new(NULL);
155  g_string_printf(msg, "Expect bit is %s", v ? "HIGH" : "LOW");
156  v ?
157  TEST_ASSERT_BIT_HIGH_MESSAGE(bit, *reg, msg->str)
158  :
159  TEST_ASSERT_BIT_LOW_MESSAGE(bit, *reg, msg->str);
160  g_string_free(msg, true);
161 }
162 
163 /* =====[ Exposure ]===== */
164 void MSB_returns_most_significant_bit_of_16bit_input(void)
165 {
166  /* =====[ Operate and Test ]===== */
167  exposure_ticks = 0xABCD;
168  TEST_ASSERT_EQUAL_HEX8( 0xAB, MSB(exposure_ticks) );
169 }
170 void LSB_returns_least_significant_bit_of_16bit_input(void)
171 {
172  /* =====[ Operate and Test ]===== */
173  exposure_ticks = 0xABCD;
174  TEST_ASSERT_EQUAL_HEX8( 0xCD, LSB(exposure_ticks) );
175 }
176 
177 /* =====[ LisInit ]===== */
178 void LisInit_sets_PixSelect_as_an_output(void)
179 {
180  /* =====[ Setup ]===== */
181  _setup_bit_val_msg(
182  Lis_ddr2, Lis_PixSelect, LOW,
183  "Expect test starts with PixSelect LOW (input pin), but PixSelect" // = bit_val
184  );
185  /* =====[ Operate ]===== */
186  LisInit();
187  /* =====[ Test ]===== */
188  TEST_ASSERT_BIT_HIGH(Lis_PixSelect, *Lis_ddr2);
189 }
190 void LisInit_idles_PixSelect_low(void)
191 {
192  /* =====[ Setup ]===== */
193  _setup_bit_val_msg(
194  Lis_port2, Lis_PixSelect, HIGH,
195  "Expect test starts with PixSelect HIGH, but PixSelect" // = bit_val
196  );
197  /* =====[ Operate ]===== */
198  LisInit();
199  /* =====[ Test ]===== */
200  TEST_ASSERT_BIT_LOW(Lis_PixSelect, *Lis_port2);
201 }
202 void LisInit_sets_Clk_as_an_output(void)
203 {
204  /* =====[ Setup ]===== */
205  _setup_bit_val_msg(Lis_ddr1, Lis_Clk, LOW,
206  "Expect Lis_Clk is an input (LOW) when test starts. Lis_Clk"
207  );
208  /* =====[ Operate ]===== */
209  LisInit();
210  /* =====[ Test ]===== */
211  TEST_ASSERT_BIT_HIGH(Lis_Clk, *Lis_ddr1);
212 }
213 void LisInit_sets_Rst_as_an_output(void)
214 {
215  /* =====[ Setup ]===== */
216  _setup_bit_val_msg(Lis_ddr1, Lis_Rst, LOW,
217  "Expect Lis_Rst is an input (LOW) when test starts. Lis_Rst"
218  );
219  /* =====[ Operate ]===== */
220  LisInit();
221  /* =====[ Test ]===== */
222  TEST_ASSERT_BIT_HIGH(Lis_Rst, *Lis_ddr1);
223 }
224 void LisInit_idles_Rst_low(void)
225 {
226  /* =====[ Setup ]===== */
227  _setup_bit_val_msg(Lis_port1, Lis_Rst, HIGH,
228  "Expect test starts with Lis_Rst HIGH, but Lis_Rst"
229  );
230  /* =====[ Operate ]===== */
231  LisInit();
232  /* =====[ Test ]===== */
233  TEST_ASSERT_BIT_LOW(Lis_Rst, *Lis_port1);
234 }
235 void LisInit_sets_Sync_as_an_input(void)
236 {
237  _setup_bit_val_msg(Lis_ddr1, Lis_Sync, HIGH,
238  "Sync must be an output when the test starts, but Lis_Sync"
239  );
240  /* =====[ Operate ]===== */
241  LisInit();
242  /* =====[ Test ]===== */
243  TEST_ASSERT_BIT_LOW(Lis_Sync, *Lis_ddr1);
244 }
245 void LisInit_resets_PWM_timer_at_top(void)
246 {
247  /* =====[ Setup ]===== */
248  *Lis_TCCR0A = 0x00;
249  TEST_ASSERT_BIT_LOW_MESSAGE(
250  Lis_WGM00,
251  *Lis_TCCR0A,
252  "WGM00 must be clear when the test starts."
253  );
254  TEST_ASSERT_BIT_LOW_MESSAGE(
255  Lis_WGM01,
256  *Lis_TCCR0A,
257  "WGM01 must be clear when the test starts."
258  );
259  /* =====[ Operate ]===== */
260  LisInit();
261  /* =====[ Test ]===== */
262  TEST_ASSERT_BIT_HIGH(Lis_WGM00, *Lis_TCCR0A);
263  TEST_ASSERT_BIT_HIGH(Lis_WGM01, *Lis_TCCR0A);
264 }
265 void LisInit_PWM_timer_top_is_OCR0A(void)
266 {
267  /* =====[ Setup ]===== */
268  *Lis_TCCR0B = 0x00;
269  TEST_ASSERT_BIT_LOW_MESSAGE(
270  Lis_WGM02,
271  *Lis_TCCR0B,
272  "WGM02 must be clear before test starts."
273  );
274  /* =====[ Operate ]===== */
275  LisInit();
276  /* =====[ Test ]===== */
277  TEST_ASSERT_BIT_HIGH(Lis_WGM02, *Lis_TCCR0B);
278 }
279 void LisInit_PWM_timer_is_clocked_by_CPU_with_no_prescaling(void)
280 {
281  /* =====[ Setup ]===== */
282  *Lis_TCCR0B = 0x06;
283  TEST_ASSERT_BIT_LOW_MESSAGE(
284  Lis_CS00,
285  *Lis_TCCR0B,
286  "CS00 must be clear before test starts."
287  );
288  TEST_ASSERT_BIT_HIGH_MESSAGE(
289  Lis_CS01,
290  *Lis_TCCR0B,
291  "CS01 must be set before test starts."
292  );
293  TEST_ASSERT_BIT_HIGH_MESSAGE(
294  Lis_CS02,
295  *Lis_TCCR0B,
296  "CS02 must be set before test starts."
297  );
298  /* =====[ Operate ]===== */
299  LisInit();
300  /* =====[ Test ]===== */
301  TEST_ASSERT_BIT_HIGH(Lis_CS00, *Lis_TCCR0B);
302  TEST_ASSERT_BIT_LOW(Lis_CS01, *Lis_TCCR0B);
303  TEST_ASSERT_BIT_LOW(Lis_CS02, *Lis_TCCR0B);
304 }
305 void LisInit_sets_PWM_frequency_at_50kHz(void)
306 {
307  /* =====[ Setup ]===== */
308  *Lis_OCR0A = 0x00;
309  /* =====[ Operate ]===== */
310  LisInit();
311  /* =====[ Test ]===== */
312  int fcpu = 10e6; int fclk = 50e3;
313  // 10.0e6/50.0e3 = 200.0 tics
314  TEST_ASSERT_EQUAL_UINT8(fcpu/fclk, *Lis_OCR0A);
315 }
316 void LisInit_sets_PWM_duty_cycle_to_50_percent(void)
317 {
318  /* =====[ Setup ]===== */
319  *Lis_OCR0B = 0;
320  *Lis_OCR0A = 0;
321  /* =====[ Operate ]===== */
322  LisInit();
323  /* =====[ Test ]===== */
324  float duty_cycle = (float)(*Lis_OCR0B)/(float)(*Lis_OCR0A);
325  TEST_ASSERT_EQUAL_FLOAT(0.5, duty_cycle);
326 }
327 void LisInit_outputs_the_PWM_clock_on_pin_Clk(void)
328 {
329  /* =====[ Setup ]===== */
330  *Lis_TCCR0A = 0xDF;
331  TEST_ASSERT_BIT_HIGH_MESSAGE(
332  Lis_COM0B0, *Lis_TCCR0A,
333  "COM0B0 must be HIGH before the test starts."
334  );
335  TEST_ASSERT_BIT_LOW_MESSAGE(
336  Lis_COM0B1, *Lis_TCCR0A,
337  "COM0B1 must be LOW before the test starts."
338  );
339  /* =====[ Operate ]===== */
340  LisInit();
341  /* =====[ Test ]===== */
342  TEST_ASSERT_BIT_LOW(Lis_COM0B0, *Lis_TCCR0A);
343  TEST_ASSERT_BIT_HIGH(Lis_COM0B1, *Lis_TCCR0A);
344 }
345 
346 /* =====[ LisConfigIsValid ]===== */
347 void LisConfigIsValid_returns_false_if_binning_is_invalid(void)
348 {
349  /* =====[ Setup ]===== */
350  // Fake some config values.
351  binning = 0xFF;
352  gain = GAIN_1X;
353  active_rows = ALL_ROWS_ACTIVE;
354  /* =====[ Operate and Test ]===== */
355  TEST_ASSERT_TRUE_MESSAGE(
356  !LisConfigIsValid(binning, gain, active_rows),
357  "Binning is invalid: expect `LisConfigIsValid` returns false."
358  );
359 }
360 void LisConfigIsValid_returns_false_if_gain_is_invalid(void)
361 {
362  /* =====[ Setup ]===== */
363  // Fake some config values.
364  binning = BINNING_ON;
365  gain = 0xFF;
366  active_rows = ALL_ROWS_ACTIVE;
367  /* =====[ Operate and Test ]===== */
368  TEST_ASSERT_TRUE_MESSAGE(
369  !LisConfigIsValid(binning, gain, active_rows),
370  "Gain is invalid: expect `LisConfigIsValid` returns false."
371  );
372 }
373 void LisConfigIsValid_returns_false_if_active_rows_is_invalid(void)
374 {
375  /* =====[ Setup ]===== */
376  // Fake some config values.
377  binning = BINNING_OFF;
378  gain = GAIN_2X5;
379  active_rows = 0xFF;
380  /* =====[ Operate and Test ]===== */
381  TEST_ASSERT_TRUE_MESSAGE(
382  !LisConfigIsValid(binning, gain, active_rows),
383  "Rows is invalid: expect `LisConfigIsValid` returns false."
384  );
385 }
386 void LisConfigIsValid_returns_true_if_config_is_valid(void)
387 {
388  /* =====[ Setup ]===== */
389  // Fake some config values.
390  binning = BINNING_OFF;
391  gain = GAIN_2X5;
392  active_rows = ALL_ROWS_ACTIVE;
393  /* =====[ Operate and Test ]===== */
394  TEST_ASSERT_TRUE_MESSAGE(
395  LisConfigIsValid(binning, gain, active_rows),
396  "Config is valid: expect `LisConfigIsValid` returns true."
397  );
398 }
399 
400 /* =====[ _ConfigAs28bits ]===== */
401 void ConfigAs28bits_writes_config_as_little_endian_ie_binning_is_config_byte0_bit0(void)
402 {
403  /* =====[ Setup ]===== */
404  uint8_t config[4];
405  /* =====[ Operate ]===== */
406  binning = BINNING_ON; // byte0 bit 0 set
407  gain = GAIN_1X; // byt0 bit 1 clear, bit 2 clear
408  active_rows = 0x00; // no rows active
409  _ConfigAs28bits(config); // Write config
410  /* =====[ Test ]===== */
411  // Expect all bits in config are 0 except binning.
412  // Expect binning bit is byte 0, bit 0.
413  TEST_ASSERT_EQUAL_HEX8_MESSAGE(0x00, config[3], "Expect config[3]==0x00.");
414  TEST_ASSERT_EQUAL_HEX8_MESSAGE(0x00, config[2], "Expect config[2]==0x00.");
415  TEST_ASSERT_EQUAL_HEX8_MESSAGE(0x00, config[1], "Expect config[1]==0x00.");
416  TEST_ASSERT_EQUAL_HEX8_MESSAGE(0x01, config[0], "Expect config[0]==0x01.");
417 }
418 void ConfigAs28bits_sets_config_byte0_bit0_if_BINNING_ON(void)
419 {
420  /* =====[ Setup ]===== */
421  binning = BINNING_ON;
422  uint8_t config[4];
423  ClearBit(&config[0], 0);
424  TEST_ASSERT_BIT_LOW_MESSAGE(
425  0, // bit 0
426  config[0], // config byte 0
427  "Test must start with bit clear, but bit is set!"
428  );
429  /* =====[ Operate ]===== */
430  _ConfigAs28bits(config);
431  /* =====[ Test ]===== */
432  TEST_ASSERT_BIT_HIGH_MESSAGE(
433  0, // bit 0
434  config[0], // config byte 0
435  "Expect byte0, bit0 set for BINNING_ON."
436  );
437 }
438 void ConfigAs28bits_clears_config_byte0_bit0_if_BINNING_OFF(void)
439 {
440  /* =====[ Setup ]===== */
441  binning = BINNING_OFF;
442  uint8_t config[4];
443  SetBit(&config[0], 0);
444  TEST_ASSERT_BIT_HIGH_MESSAGE(
445  0, // bit 0
446  config[0], // config byte 0
447  "Test must start with bit set, but bit is clear!"
448  );
449  /* =====[ Operate ]===== */
450  _ConfigAs28bits(config);
451  /* =====[ Test ]===== */
452  TEST_ASSERT_BIT_LOW_MESSAGE(
453  0, // bit 0
454  config[0], // config byte 0
455  "Expect byte0, bit0 clear for BINNING_OFF."
456  );
457 }
458 void ConfigAs28bits_byte0_bit1_clear_and_bit2_clear_if_GAIN_1X(void)
459 {
460  /* =====[ Setup ]===== */
461  uint8_t config[4];
462  SetBit(&config[0], 1);
463  SetBit(&config[0], 2);
464  TEST_ASSERT_BIT_HIGH_MESSAGE(
465  1, // bit 1
466  config[0], // config byte 0
467  "Test must start with bit 1 set, but bit is clear!"
468  );
469  TEST_ASSERT_BIT_HIGH_MESSAGE(
470  2, // bit 2
471  config[0], // config byte 0
472  "Test must start with bit 2 set, but bit is clear!"
473  );
474  /* =====[ Operate ]===== */
475  gain = GAIN_1X;
476  _ConfigAs28bits(config);
477  /* =====[ Test ]===== */
478  TEST_ASSERT_BIT_LOW_MESSAGE(
479  1, // bit 1
480  config[0], // config byte 0
481  "Expect byte0, bit1 clear for GAIN_1X."
482  );
483  TEST_ASSERT_BIT_LOW_MESSAGE(
484  2, // bit 2
485  config[0], // config byte 0
486  "Expect byte0, bit2 clear for GAIN_1X."
487  );
488 }
489 void ConfigAs28bits_byte0_bit1_clear_and_bit2_set_if_GAIN_2X5(void)
490 {
491  /* =====[ Setup ]===== */
492  uint8_t config[4];
493  SetBit(&config[0], 1);
494  ClearBit(&config[0], 2);
495  TEST_ASSERT_BIT_HIGH_MESSAGE(
496  1, // bit 1
497  config[0], // config byte 0
498  "Test must start with bit 1 set, but bit is clear!"
499  );
500  TEST_ASSERT_BIT_LOW_MESSAGE(
501  2, // bit 2
502  config[0], // config byte 0
503  "Test must start with bit 2 clear, but bit is set!"
504  );
505  /* =====[ Operate ]===== */
506  gain = GAIN_2X5;
507  _ConfigAs28bits(config);
508  /* =====[ Test ]===== */
509  TEST_ASSERT_BIT_LOW_MESSAGE(
510  1, // bit 1
511  config[0], // config byte 0
512  "Expect byte0, bit1 clear for GAIN_2X5."
513  );
514  TEST_ASSERT_BIT_HIGH_MESSAGE(
515  2, // bit 2
516  config[0], // config byte 0
517  "Expect byte0, bit2 set for GAIN_2X5."
518  );
519 }
520 void ConfigAs28bits_byte0_bit1_set_and_bit2_clear_if_GAIN_4X(void)
521 {
522  /* =====[ Setup ]===== */
523  uint8_t config[4];
524  ClearBit(&config[0], 1);
525  SetBit(&config[0], 2);
526  TEST_ASSERT_BIT_LOW_MESSAGE(
527  1, // bit 1
528  config[0], // config byte 0
529  "Test must start with bit 1 clear, but bit is set!"
530  );
531  TEST_ASSERT_BIT_HIGH_MESSAGE(
532  2, // bit 2
533  config[0], // config byte 0
534  "Test must start with bit 2 set, but bit is clear!"
535  );
536  /* =====[ Operate ]===== */
537  gain = GAIN_4X;
538  _ConfigAs28bits(config);
539  /* =====[ Test ]===== */
540  TEST_ASSERT_BIT_HIGH_MESSAGE(
541  1, // bit 1
542  config[0], // config byte 0
543  "Expect byte0, bit1 set for GAIN_4X."
544  );
545  TEST_ASSERT_BIT_LOW_MESSAGE(
546  2, // bit 2
547  config[0], // config byte 0
548  "Expect byte0, bit2 clear for GAIN_4X."
549  );
550 }
551 void ConfigAs28bits_byte0_bit1_set_and_bit2_set_if_GAIN_5X(void)
552 {
553  /* =====[ Setup ]===== */
554  uint8_t config[4];
555  ClearBit(&config[0], 1);
556  ClearBit(&config[0], 2);
557  TEST_ASSERT_BIT_LOW_MESSAGE(
558  1, // bit 1
559  config[0], // config byte 0
560  "Test must start with bit 1 clear, but bit is set!"
561  );
562  TEST_ASSERT_BIT_LOW_MESSAGE(
563  2, // bit 2
564  config[0], // config byte 0
565  "Test must start with bit 2 clear, but bit is set!"
566  );
567  /* =====[ Operate ]===== */
568  gain = GAIN_5X;
569  _ConfigAs28bits(config);
570  /* =====[ Test ]===== */
571  TEST_ASSERT_BIT_HIGH_MESSAGE(
572  1, // bit 1
573  config[0], // config byte 0
574  "Expect byte0, bit1 set for GAIN_5X."
575  );
576  TEST_ASSERT_BIT_HIGH_MESSAGE(
577  2, // bit 2
578  config[0], // config byte 0
579  "Expect byte0, bit2 set for GAIN_5X."
580  );
581 }
582 static uint8_t Clear_3_LSB(uint8_t byte) { return byte & (!0x07); }
583 static uint8_t Clear_4_MSB(uint8_t byte) { return byte & (!0xF0); }
584 static void FakePreviousConfig(uint8_t *prev_config, uint8_t *desired_config)
585 {
592  prev_config[0] = Clear_3_LSB(!desired_config[0]);
593  prev_config[1] = (!desired_config[1]);
594  prev_config[2] = (!desired_config[2]);
595  prev_config[3] = Clear_4_MSB(!desired_config[3]);
596 }
597 void ConfigAs28bits_bit3_to_bit27_set_if_ALL_ROWS_ACTIVE(void)
598 {
599  /* =====[ Setup ]===== */
600  // Define the desired 28-bit sequence.
601  uint8_t desired[4];
602  desired[0]=0xF8; // desired values for bits 7: 0 - 0b11111000
603  desired[1]=0xFF; // desired values for bits 15: 8 - 0b11111111
604  desired[2]=0xFF; // desired values for bits 23:16 - 0b11111111
605  desired[3]=0x0F; // desired values for bits 31:24 - 0b11111000
606  // Start test with previous config value faked to make test fail.
607  uint8_t config[4]; FakePreviousConfig(config, desired);
608  /* =====[ Operate ]===== */
609  binning = BINNING_OFF; gain = GAIN_1X; // clear bits
610  active_rows = ALL_ROWS_ACTIVE; // new active_rows value
611  _ConfigAs28bits(config); // Write config with new active_rows
612  /* =====[ Test ]===== */
613  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[0], config[0], "Bad config[0].");
614  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[1], config[1], "Bad config[1].");
615  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[2], config[2], "Bad config[2].");
616  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[3], config[3], "Bad config[3].");
617 }
618 void ConfigAs28bits_b3b8b13b18b23_set_if_ROW_1_ACTIVE(void)
619 {
620  /* =====[ Setup ]===== */
621  // Define the desired 28-bit sequence.
622  uint8_t desired[4];
623  desired[0]=0x08; // desired values for bits 7: 0 - 0b00001000
624  desired[1]=0x21; // desired values for bits 15: 8 - 0b00100001
625  desired[2]=0x84; // desired values for bits 23:16 - 0b10000100
626  desired[3]=0x00; // desired values for bits 31:24 - 0b00000000
627  // Start test with previous config value faked to make test fail.
628  uint8_t config[4]; FakePreviousConfig(config, desired);
629  /* =====[ Operate ]===== */
630  binning = BINNING_OFF; gain = GAIN_1X; // clear bits
631  active_rows = ONLY_ROW_1_ACTIVE; // new active_rows value
632  _ConfigAs28bits(config); // Write config with new active_rows
633  /* =====[ Test ]===== */
634  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[0], config[0], "Bad config[0].");
635  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[1], config[1], "Bad config[1].");
636  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[2], config[2], "Bad config[2].");
637  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[3], config[3], "Bad config[3].");
638 }
639 void ConfigAs28bits_b4b9b14b19b24_set_if_ROW_2_ACTIVE(void)
640 {
641  /* =====[ Setup ]===== */
642  // Define the desired 28-bit sequence.
643  uint8_t desired[4];
644  desired[0]=0x10; // desired values for bits 7: 0 - 0b00010000
645  desired[1]=0x42; // desired values for bits 15: 8 - 0b01000010
646  desired[2]=0x08; // desired values for bits 23:16 - 0b00001000
647  desired[3]=0x01; // desired values for bits 31:24 - 0b00000001
648  // Start test with previous config value faked to make test fail.
649  uint8_t config[4]; FakePreviousConfig(config, desired);
650  /* =====[ Operate ]===== */
651  binning = BINNING_OFF; gain = GAIN_1X; // clear bits
652  active_rows = ONLY_ROW_2_ACTIVE; // new active_rows value
653  _ConfigAs28bits(config); // Write config with new active_rows
654  /* =====[ Test ]===== */
655  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[0], config[0], "Bad config[0].");
656  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[1], config[1], "Bad config[1].");
657  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[2], config[2], "Bad config[2].");
658  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[3], config[3], "Bad config[3].");
659 }
660 void ConfigAs28bits_b5b10b15b20b25_set_if_ROW_3_ACTIVE(void)
661 {
662  /* =====[ Setup ]===== */
663  // Define the desired 28-bit sequence.
664  uint8_t desired[4];
665  desired[0]=0x20; // desired values for bits 7: 0 - 0b00100000
666  desired[1]=0x84; // desired values for bits 15: 8 - 0b10000100
667  desired[2]=0x10; // desired values for bits 23:16 - 0b00010000
668  desired[3]=0x02; // desired values for bits 31:24 - 0b00000010
669  // Start test with previous config value faked to make test fail.
670  uint8_t config[4]; FakePreviousConfig(config, desired);
671  /* =====[ Operate ]===== */
672  binning = BINNING_OFF; gain = GAIN_1X; // clear bits
673  active_rows = ONLY_ROW_3_ACTIVE; // new active_rows value
674  _ConfigAs28bits(config); // Write config with new active_rows
675  /* =====[ Test ]===== */
676  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[0], config[0], "Bad config[0].");
677  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[1], config[1], "Bad config[1].");
678  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[2], config[2], "Bad config[2].");
679  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[3], config[3], "Bad config[3].");
680 }
681 void ConfigAs28bits_b6b11b16b21b26_set_if_ROW_4_ACTIVE(void)
682 {
683  /* =====[ Setup ]===== */
684  // Define the desired 28-bit sequence.
685  uint8_t desired[4];
686  desired[0]=0x40; // desired values for bits 7: 0 - 0b01000000
687  desired[1]=0x08; // desired values for bits 15: 8 - 0b00001000
688  desired[2]=0x21; // desired values for bits 23:16 - 0b00100001
689  desired[3]=0x04; // desired values for bits 31:24 - 0b00000100
690  // Start test with previous config value faked to make test fail.
691  uint8_t config[4]; FakePreviousConfig(config, desired);
692  /* =====[ Operate ]===== */
693  binning = BINNING_OFF; gain = GAIN_1X; // clear bits
694  active_rows = ONLY_ROW_4_ACTIVE; // new active_rows value
695  _ConfigAs28bits(config); // Write config with new active_rows
696  /* =====[ Test ]===== */
697  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[0], config[0], "Bad config[0].");
698  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[1], config[1], "Bad config[1].");
699  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[2], config[2], "Bad config[2].");
700  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[3], config[3], "Bad config[3].");
701 }
702 void ConfigAs28bits_b7b12b17b22b27_set_if_ROW_5_ACTIVE(void)
703 {
704  /* =====[ Setup ]===== */
705  // Define the desired 28-bit sequence.
706  uint8_t desired[4];
707  desired[0]=0x80; // desired values for bits 7: 0 - 0b10000000
708  desired[1]=0x10; // desired values for bits 15: 8 - 0b00010000
709  desired[2]=0x42; // desired values for bits 23:16 - 0b01000010
710  desired[3]=0x08; // desired values for bits 31:24 - 0b00001000
711  // Start test with previous config value faked to make test fail.
712  uint8_t config[4]; FakePreviousConfig(config, desired);
713  /* =====[ Operate ]===== */
714  binning = BINNING_OFF; gain = GAIN_1X; // clear bits
715  active_rows = ONLY_ROW_5_ACTIVE; // new active_rows value
716  _ConfigAs28bits(config); // Write config with new active_rows
717  /* =====[ Test ]===== */
718  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[0], config[0], "Bad config[0].");
719  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[1], config[1], "Bad config[1].");
720  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[2], config[2], "Bad config[2].");
721  TEST_ASSERT_EQUAL_HEX8_MESSAGE(desired[3], config[3], "Bad config[3].");
722 }
723 
724 /* =====[ LisWriteConfig ]===== */
725 void LisWriteConfig_converts_config_to_28bit_sequence(void)
726 {
727  /* =====[ Operate ]===== */
728  LisWriteConfig();
729  /* =====[ Test ]===== */
730  _AssertCall(1, "_ConfigAs28bits");
731 }
732 void LisWriteConfig_enters_LIS_programming_mode(void)
733 {
734  /* =====[ Operate ]===== */
735  LisWriteConfig();
736  /* =====[ Test ]===== */
737  _AssertCall(2, "_EnterLisProgrammingMode");
738 }
739 void LisWriteConfig_writes_28bits_to_LIS_setup_register(void)
740 {
741  /* =====[ Operate ]===== */
742  LisWriteConfig();
743  /* =====[ Test ]===== */
744  _AssertCall(3, "_Write28bitLisConfig");
745 }
746 void LisWriteConfig_exits_LIS_programming_mode(void)
747 {
748  /* =====[ Operate ]===== */
749  LisWriteConfig();
750  /* =====[ Test ]===== */
751  _AssertCall(4, "_ExitLisProgrammingMode");
752 }
753 
754 /* =====[ _WaitForLisClkLow ]===== */
755 void WaitForLisClkLow_clears_flag_PwmTimerMatchesOCF0B(void)
756 {
757  /* =====[ Setup ]===== */
763  _setup_bit_val_msg(Lis_TIFR0, Lis_OCF0B, LOW, "TIFR0 bit Lis_OCF0B");
764  /* =====[ Operate ]===== */
766  /* =====[ Test ]===== */
767  // Clear flag by setting flag bit
768  // reg: TIFR0, bit: OCF0B
769  TEST_ASSERT_BIT_HIGH_MESSAGE(
770  Lis_OCF0B,
771  *Lis_TIFR0,
772  "Expect `_WaitForLisClkLow` clears flag by setting bit."
773  );
774 }
775 void WaitForLisClkLow_waits_until_flag_PwmTimerMatchesOCF0B_is_set(void)
776 {
777  // Cannot fake setting flag after calling function under test
778  TEST_PASS();
779 }
780 
781 /* =====[ _WaitForLisClkHigh ]===== */
782 void WaitForLisClkHigh_clears_flag_PwmTimerMatchesOCF0A(void)
783 {
784  /* =====[ Setup ]===== */
790  _setup_bit_val(Lis_TIFR0, Lis_OCF0A, LOW);
791  /* =====[ Operate ]===== */
793  /* =====[ Test ]===== */
794  _test_bit_val(Lis_TIFR0, Lis_OCF0A, HIGH);
795 }
796 void WaitForLisClkHigh_waits_until_flag_PwmTimerMatchesOCF0A_is_set(void)
797 {
798  // Cannot fake setting flag after calling function under test
799  TEST_PASS();
800 }
801 
802 /* =====[ _EnterLisProgrammingMode ]===== */
803 void EnterLisProgrammingMode_waits_for_LisClk_LOW(void)
804 {
805  /* =====[ Operate ]===== */
807  /* =====[ Test ]===== */
808  _AssertCall(1, "_WaitForLisClkLow");
809 }
810 void EnterLisProgrammingMode_asserts_LisPixSelect_to_program_Lis(void)
811 {
812  /* =====[ Setup ]===== */
813  ClearBit(Lis_port2, Lis_PixSelect);
814  TEST_ASSERT_BIT_LOW_MESSAGE(
815  Lis_PixSelect,
816  *Lis_port2,
817  "PixSelect must be LOW when the test starts."
818  );
819  /* =====[ Operate ]===== */
821  /* =====[ Test ]===== */
822  TEST_ASSERT_BIT_HIGH_MESSAGE(
823  Lis_PixSelect,
824  *Lis_port2,
825  "Expect `EnterLisProgrammingMode` drives LisPixSelect HIGH."
826  );
827 }
828 
829 /* =====[ _WriteLisConfigBit ]===== */
830 void WriteLisConfigBit_outputs_bit_on_LisRst(void)
831 {
832  /* =====[ Setup ]===== */
833  uint8_t config_bytes[4];
834  config_bytes[0] = 0x01; // byte 0 bit 0 = 1
835  config_bytes[1] = 0x00; // byte 1 bit 0 = 0
836  TEST_ASSERT_BIT_HIGH_MESSAGE(
837  0, config_bytes[0], "Test starts with byte0 bit0 = 1"
838  );
839  TEST_ASSERT_BIT_LOW_MESSAGE(
840  0, config_bytes[1], "Test starts with byte1 bit0 = 0"
841  );
842  /* =====[ Operate and Test ]===== */
843  uint8_t * config = config_bytes; uint8_t bit_index = 0;
844  _WriteLisConfigBit(config++, bit_index); // byte 0 bit 0
845  TEST_ASSERT_BIT_HIGH(Lis_Rst, *Lis_port1);
846  _WriteLisConfigBit(config, bit_index); // byte 1 bit 0
847  TEST_ASSERT_BIT_LOW(Lis_Rst, *Lis_port1);
848 }
849 void WriteLisConfigBit_waits_for_LisClk_HIGH(void)
850 {
851  /* =====[ Operate ]===== */
852  uint8_t config[4]; uint8_t bit_index = 0;
853  _WriteLisConfigBit(config, bit_index);
854  /* =====[ Test ]===== */
855  _AssertCall(1, "_WaitForLisClkHigh");
856 }
857 void WriteLisConfigBit_waits_for_LisClk_LOW(void)
858 {
859  /* =====[ Operate ]===== */
860  uint8_t config[4]; uint8_t bit_index = 0;
861  _WriteLisConfigBit(config, bit_index);
862  /* =====[ Test ]===== */
863  _AssertCall(2, "_WaitForLisClkLow");
864 }
865 
866 /* =====[ _Write28bitLisConfig ]===== */
867 void Write28bitLisConfig_writes_28bits_starting_at_byte0_bit0_and_ending_at_byte3_bit3(void)
868 {
869  uint8_t config_bytes[4];
870  config_bytes[0] = 0xac;
871  config_bytes[1] = 0xbd;
872  config_bytes[2] = 0xce;
873  config_bytes[3] = 0xdf;
874  /* =====[ Operate ]===== */
875  uint8_t * config = config_bytes;
876  _Write28bitLisConfig(config);
877  /* =====[ Test ]===== */
878  char const * call_name = "_WriteLisConfigBit";
879  uint16_t call_n = 1;
880  // Check first 24 bits
881  for (uint8_t byte_i = 0; byte_i<3; byte_i++)
882  {
883  for (uint8_t bit_i = 0; bit_i<8; bit_i++)
884  {
885  _AssertCall(call_n, call_name);
886  _AssertArg(call_n, 1, &config);
887  _AssertArg(call_n, 2, &bit_i);
888  call_n++;
889  }
890  config++;
891  }
892  // Check last four bits
893  for (uint8_t bit_i = 0; bit_i<4; bit_i++)
894  {
895  _AssertCall(call_n, call_name);
896  _AssertArg(call_n, 1, &config);
897  _AssertArg(call_n, 2, &bit_i);
898  call_n++;
899  }
900 }
901 
902 /* =====[ _ExitLisProgrammingMode ]===== */
903 void ExitLisProgrammingMode_outputs_LOW_on_pin_LisRst(void)
904 {
905  /* =====[ Setup ]===== */
906  SetBit(Lis_port1, Lis_Rst);
907  TEST_ASSERT_BIT_HIGH_MESSAGE(
908  Lis_Rst,
909  *Lis_port1,
910  "Rst must be HIGH when the test starts."
911  );
912  /* =====[ Operate ]===== */
914  /* =====[ Test ]===== */
915  TEST_ASSERT_BIT_LOW_MESSAGE(
916  Lis_Rst,
917  *Lis_port1,
918  "Expect `Lis_Rst` LOW."
919  );
920 }
921 void ExitLisProgrammingMode_outputs_LOW_on_pin_LisPixSelect(void)
922 {
923  /* =====[ Setup ]===== */
924  SetBit(Lis_port2, Lis_PixSelect);
925  TEST_ASSERT_BIT_HIGH_MESSAGE(
926  Lis_PixSelect,
927  *Lis_port2,
928  "PixSelect must be HIGH when the test starts."
929  );
930  /* =====[ Operate ]===== */
932  /* =====[ Test ]===== */
933  TEST_ASSERT_BIT_LOW_MESSAGE(
934  Lis_PixSelect,
935  *Lis_port2,
936  "Expect `Lis_PixSelect` LOW."
937  );
938 }
939 
940 /* =====[ LisExpose ]===== */
941 void LisExpose_waits_for_the_falling_edge_of_Lis_Clk(void)
942 {
943  /* =====[ Operate ]===== */
944  /* LisExpose(); */
945  TEST_PASS();
946 }
947 void LisExpose_starts_exposure_by_driving_Lis_Rst_HIGH(void)
948 {
949  TEST_PASS();
950 }
951 void LisExpose_counts_falling_edges_of_Lis_Clk_until_count_equals_exposure_ticks(void)
952 {
953  TEST_PASS();
954 }
955 void LisExpose_stops_exposure_by_driving_Lis_Rst_LOW(void)
956 {
957  TEST_PASS();
958 }
bool LisConfigIsValid(uint8_t binning, uint8_t gain, uint8_t active_rows)
Definition: Lis.h:441
void _ExitLisProgrammingMode(void)
Definition: Lis.h:304
uint8_t MSB(uint16_t msb_lsb)
Definition: Lis.h:401
void _ConfigAs28bits(uint8_t *config)
Definition: Lis.h:155
void _EnterLisProgrammingMode(void)
Definition: Lis.h:283
void _WaitForLisClkHigh(void)
Definition: Lis.h:268
void _WriteLisConfigBit(uint8_t const *config, uint8_t bit_index)
Definition: Lis.h:324
void LisWriteConfig(void)
Definition: Lis.h:475
uint16_t exposure_ticks
LIS-770i exposure time.
Definition: Lis.c:32
void LisInit(void)
Definition: Lis.h:416
void _WaitForLisClkLow(void)
Definition: Lis.h:257
void _Write28bitLisConfig(uint8_t const *config)
Definition: Lis.h:360
uint8_t LSB(uint16_t msb_lsb)
Definition: Lis.h:408