7 static void _AssertCall(uint16_t num,
char const * name)
11 GString *message = g_string_new(NULL);
12 g_string_append_printf(message,
"`%s` is not call %d", name, num);
14 TEST_ASSERT_TRUE_MESSAGE(
15 AssertCall(mock, num, name),
19 g_string_free(message,
true);
21 static void _AssertArg(uint16_t call_n, uint8_t arg_n,
void *pval)
61 GString *msg = g_string_new(NULL);
62 g_string_printf(msg,
"Expect different value for call %d arg %d.", call_n, arg_n);
66 TEST_ASSERT_TRUE_MESSAGE(
67 AssertArg(mock, call_n, arg_n, pval),
70 g_string_free(msg,
true);
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)
87 GString *msg = g_string_new(NULL);
88 g_string_printf(msg,
"Bit must be %s when the test starts", v ?
"HIGH" :
"LOW");
92 TEST_ASSERT_BIT_HIGH_MESSAGE(bit, *reg, msg->str);
97 TEST_ASSERT_BIT_LOW_MESSAGE(bit, *reg, msg->str);
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);
105 g_string_free(msg,
true);
107 static void _setup_bit_val_msg(lis_ptr reg, lis_bit bit, bit_val v,
122 GString *message = g_string_new(NULL);
123 g_string_append_printf(
124 message,
"`%s` must be ", bit_name
126 g_string_append_printf(
127 message,
"%s when the test starts.",
133 TEST_ASSERT_BIT_HIGH_MESSAGE(bit, *reg, message->str);
138 TEST_ASSERT_BIT_LOW_MESSAGE(bit, *reg, message->str);
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);
147 g_string_free(message,
true);
151 static void _test_bit_val( lis_ptr reg, lis_bit bit, bit_val v )
154 GString *msg = g_string_new(NULL);
155 g_string_printf(msg,
"Expect bit is %s", v ?
"HIGH" :
"LOW");
157 TEST_ASSERT_BIT_HIGH_MESSAGE(bit, *reg, msg->str)
159 TEST_ASSERT_BIT_LOW_MESSAGE(bit, *reg, msg->str);
160 g_string_free(msg,
true);
164 void MSB_returns_most_significant_bit_of_16bit_input(
void)
170 void LSB_returns_least_significant_bit_of_16bit_input(
void)
178 void LisInit_sets_PixSelect_as_an_output(
void)
182 Lis_ddr2, Lis_PixSelect, LOW,
183 "Expect test starts with PixSelect LOW (input pin), but PixSelect"
188 TEST_ASSERT_BIT_HIGH(Lis_PixSelect, *Lis_ddr2);
190 void LisInit_idles_PixSelect_low(
void)
194 Lis_port2, Lis_PixSelect, HIGH,
195 "Expect test starts with PixSelect HIGH, but PixSelect"
200 TEST_ASSERT_BIT_LOW(Lis_PixSelect, *Lis_port2);
202 void LisInit_sets_Clk_as_an_output(
void)
205 _setup_bit_val_msg(Lis_ddr1, Lis_Clk, LOW,
206 "Expect Lis_Clk is an input (LOW) when test starts. Lis_Clk"
211 TEST_ASSERT_BIT_HIGH(Lis_Clk, *Lis_ddr1);
213 void LisInit_sets_Rst_as_an_output(
void)
216 _setup_bit_val_msg(Lis_ddr1, Lis_Rst, LOW,
217 "Expect Lis_Rst is an input (LOW) when test starts. Lis_Rst"
222 TEST_ASSERT_BIT_HIGH(Lis_Rst, *Lis_ddr1);
224 void LisInit_idles_Rst_low(
void)
227 _setup_bit_val_msg(Lis_port1, Lis_Rst, HIGH,
228 "Expect test starts with Lis_Rst HIGH, but Lis_Rst"
233 TEST_ASSERT_BIT_LOW(Lis_Rst, *Lis_port1);
235 void LisInit_sets_Sync_as_an_input(
void)
237 _setup_bit_val_msg(Lis_ddr1, Lis_Sync, HIGH,
238 "Sync must be an output when the test starts, but Lis_Sync"
243 TEST_ASSERT_BIT_LOW(Lis_Sync, *Lis_ddr1);
245 void LisInit_resets_PWM_timer_at_top(
void)
249 TEST_ASSERT_BIT_LOW_MESSAGE(
252 "WGM00 must be clear when the test starts."
254 TEST_ASSERT_BIT_LOW_MESSAGE(
257 "WGM01 must be clear when the test starts."
262 TEST_ASSERT_BIT_HIGH(Lis_WGM00, *Lis_TCCR0A);
263 TEST_ASSERT_BIT_HIGH(Lis_WGM01, *Lis_TCCR0A);
265 void LisInit_PWM_timer_top_is_OCR0A(
void)
269 TEST_ASSERT_BIT_LOW_MESSAGE(
272 "WGM02 must be clear before test starts."
277 TEST_ASSERT_BIT_HIGH(Lis_WGM02, *Lis_TCCR0B);
279 void LisInit_PWM_timer_is_clocked_by_CPU_with_no_prescaling(
void)
283 TEST_ASSERT_BIT_LOW_MESSAGE(
286 "CS00 must be clear before test starts."
288 TEST_ASSERT_BIT_HIGH_MESSAGE(
291 "CS01 must be set before test starts."
293 TEST_ASSERT_BIT_HIGH_MESSAGE(
296 "CS02 must be set before test starts."
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);
305 void LisInit_sets_PWM_frequency_at_50kHz(
void)
312 int fcpu = 10e6;
int fclk = 50e3;
314 TEST_ASSERT_EQUAL_UINT8(fcpu/fclk, *Lis_OCR0A);
316 void LisInit_sets_PWM_duty_cycle_to_50_percent(
void)
324 float duty_cycle = (float)(*Lis_OCR0B)/(float)(*Lis_OCR0A);
325 TEST_ASSERT_EQUAL_FLOAT(0.5, duty_cycle);
327 void LisInit_outputs_the_PWM_clock_on_pin_Clk(
void)
331 TEST_ASSERT_BIT_HIGH_MESSAGE(
332 Lis_COM0B0, *Lis_TCCR0A,
333 "COM0B0 must be HIGH before the test starts."
335 TEST_ASSERT_BIT_LOW_MESSAGE(
336 Lis_COM0B1, *Lis_TCCR0A,
337 "COM0B1 must be LOW before the test starts."
342 TEST_ASSERT_BIT_LOW(Lis_COM0B0, *Lis_TCCR0A);
343 TEST_ASSERT_BIT_HIGH(Lis_COM0B1, *Lis_TCCR0A);
347 void LisConfigIsValid_returns_false_if_binning_is_invalid(
void)
353 active_rows = ALL_ROWS_ACTIVE;
355 TEST_ASSERT_TRUE_MESSAGE(
357 "Binning is invalid: expect `LisConfigIsValid` returns false."
360 void LisConfigIsValid_returns_false_if_gain_is_invalid(
void)
364 binning = BINNING_ON;
366 active_rows = ALL_ROWS_ACTIVE;
368 TEST_ASSERT_TRUE_MESSAGE(
370 "Gain is invalid: expect `LisConfigIsValid` returns false."
373 void LisConfigIsValid_returns_false_if_active_rows_is_invalid(
void)
377 binning = BINNING_OFF;
381 TEST_ASSERT_TRUE_MESSAGE(
383 "Rows is invalid: expect `LisConfigIsValid` returns false."
386 void LisConfigIsValid_returns_true_if_config_is_valid(
void)
390 binning = BINNING_OFF;
392 active_rows = ALL_ROWS_ACTIVE;
394 TEST_ASSERT_TRUE_MESSAGE(
396 "Config is valid: expect `LisConfigIsValid` returns true."
401 void ConfigAs28bits_writes_config_as_little_endian_ie_binning_is_config_byte0_bit0(
void)
406 binning = BINNING_ON;
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.");
418 void ConfigAs28bits_sets_config_byte0_bit0_if_BINNING_ON(
void)
421 binning = BINNING_ON;
423 ClearBit(&config[0], 0);
424 TEST_ASSERT_BIT_LOW_MESSAGE(
427 "Test must start with bit clear, but bit is set!"
432 TEST_ASSERT_BIT_HIGH_MESSAGE(
435 "Expect byte0, bit0 set for BINNING_ON."
438 void ConfigAs28bits_clears_config_byte0_bit0_if_BINNING_OFF(
void)
441 binning = BINNING_OFF;
443 SetBit(&config[0], 0);
444 TEST_ASSERT_BIT_HIGH_MESSAGE(
447 "Test must start with bit set, but bit is clear!"
452 TEST_ASSERT_BIT_LOW_MESSAGE(
455 "Expect byte0, bit0 clear for BINNING_OFF."
458 void ConfigAs28bits_byte0_bit1_clear_and_bit2_clear_if_GAIN_1X(
void)
462 SetBit(&config[0], 1);
463 SetBit(&config[0], 2);
464 TEST_ASSERT_BIT_HIGH_MESSAGE(
467 "Test must start with bit 1 set, but bit is clear!"
469 TEST_ASSERT_BIT_HIGH_MESSAGE(
472 "Test must start with bit 2 set, but bit is clear!"
478 TEST_ASSERT_BIT_LOW_MESSAGE(
481 "Expect byte0, bit1 clear for GAIN_1X."
483 TEST_ASSERT_BIT_LOW_MESSAGE(
486 "Expect byte0, bit2 clear for GAIN_1X."
489 void ConfigAs28bits_byte0_bit1_clear_and_bit2_set_if_GAIN_2X5(
void)
493 SetBit(&config[0], 1);
494 ClearBit(&config[0], 2);
495 TEST_ASSERT_BIT_HIGH_MESSAGE(
498 "Test must start with bit 1 set, but bit is clear!"
500 TEST_ASSERT_BIT_LOW_MESSAGE(
503 "Test must start with bit 2 clear, but bit is set!"
509 TEST_ASSERT_BIT_LOW_MESSAGE(
512 "Expect byte0, bit1 clear for GAIN_2X5."
514 TEST_ASSERT_BIT_HIGH_MESSAGE(
517 "Expect byte0, bit2 set for GAIN_2X5."
520 void ConfigAs28bits_byte0_bit1_set_and_bit2_clear_if_GAIN_4X(
void)
524 ClearBit(&config[0], 1);
525 SetBit(&config[0], 2);
526 TEST_ASSERT_BIT_LOW_MESSAGE(
529 "Test must start with bit 1 clear, but bit is set!"
531 TEST_ASSERT_BIT_HIGH_MESSAGE(
534 "Test must start with bit 2 set, but bit is clear!"
540 TEST_ASSERT_BIT_HIGH_MESSAGE(
543 "Expect byte0, bit1 set for GAIN_4X."
545 TEST_ASSERT_BIT_LOW_MESSAGE(
548 "Expect byte0, bit2 clear for GAIN_4X."
551 void ConfigAs28bits_byte0_bit1_set_and_bit2_set_if_GAIN_5X(
void)
555 ClearBit(&config[0], 1);
556 ClearBit(&config[0], 2);
557 TEST_ASSERT_BIT_LOW_MESSAGE(
560 "Test must start with bit 1 clear, but bit is set!"
562 TEST_ASSERT_BIT_LOW_MESSAGE(
565 "Test must start with bit 2 clear, but bit is set!"
571 TEST_ASSERT_BIT_HIGH_MESSAGE(
574 "Expect byte0, bit1 set for GAIN_5X."
576 TEST_ASSERT_BIT_HIGH_MESSAGE(
579 "Expect byte0, bit2 set for GAIN_5X."
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)
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]);
597 void ConfigAs28bits_bit3_to_bit27_set_if_ALL_ROWS_ACTIVE(
void)
607 uint8_t config[4]; FakePreviousConfig(config, desired);
609 binning = BINNING_OFF; gain = GAIN_1X;
610 active_rows = ALL_ROWS_ACTIVE;
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].");
618 void ConfigAs28bits_b3b8b13b18b23_set_if_ROW_1_ACTIVE(
void)
628 uint8_t config[4]; FakePreviousConfig(config, desired);
630 binning = BINNING_OFF; gain = GAIN_1X;
631 active_rows = ONLY_ROW_1_ACTIVE;
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].");
639 void ConfigAs28bits_b4b9b14b19b24_set_if_ROW_2_ACTIVE(
void)
649 uint8_t config[4]; FakePreviousConfig(config, desired);
651 binning = BINNING_OFF; gain = GAIN_1X;
652 active_rows = ONLY_ROW_2_ACTIVE;
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].");
660 void ConfigAs28bits_b5b10b15b20b25_set_if_ROW_3_ACTIVE(
void)
670 uint8_t config[4]; FakePreviousConfig(config, desired);
672 binning = BINNING_OFF; gain = GAIN_1X;
673 active_rows = ONLY_ROW_3_ACTIVE;
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].");
681 void ConfigAs28bits_b6b11b16b21b26_set_if_ROW_4_ACTIVE(
void)
691 uint8_t config[4]; FakePreviousConfig(config, desired);
693 binning = BINNING_OFF; gain = GAIN_1X;
694 active_rows = ONLY_ROW_4_ACTIVE;
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].");
702 void ConfigAs28bits_b7b12b17b22b27_set_if_ROW_5_ACTIVE(
void)
712 uint8_t config[4]; FakePreviousConfig(config, desired);
714 binning = BINNING_OFF; gain = GAIN_1X;
715 active_rows = ONLY_ROW_5_ACTIVE;
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].");
725 void LisWriteConfig_converts_config_to_28bit_sequence(
void)
730 _AssertCall(1,
"_ConfigAs28bits");
732 void LisWriteConfig_enters_LIS_programming_mode(
void)
737 _AssertCall(2,
"_EnterLisProgrammingMode");
739 void LisWriteConfig_writes_28bits_to_LIS_setup_register(
void)
744 _AssertCall(3,
"_Write28bitLisConfig");
746 void LisWriteConfig_exits_LIS_programming_mode(
void)
751 _AssertCall(4,
"_ExitLisProgrammingMode");
755 void WaitForLisClkLow_clears_flag_PwmTimerMatchesOCF0B(
void)
763 _setup_bit_val_msg(Lis_TIFR0, Lis_OCF0B, LOW,
"TIFR0 bit Lis_OCF0B");
769 TEST_ASSERT_BIT_HIGH_MESSAGE(
772 "Expect `_WaitForLisClkLow` clears flag by setting bit."
775 void WaitForLisClkLow_waits_until_flag_PwmTimerMatchesOCF0B_is_set(
void)
782 void WaitForLisClkHigh_clears_flag_PwmTimerMatchesOCF0A(
void)
790 _setup_bit_val(Lis_TIFR0, Lis_OCF0A, LOW);
794 _test_bit_val(Lis_TIFR0, Lis_OCF0A, HIGH);
796 void WaitForLisClkHigh_waits_until_flag_PwmTimerMatchesOCF0A_is_set(
void)
803 void EnterLisProgrammingMode_waits_for_LisClk_LOW(
void)
808 _AssertCall(1,
"_WaitForLisClkLow");
810 void EnterLisProgrammingMode_asserts_LisPixSelect_to_program_Lis(
void)
813 ClearBit(Lis_port2, Lis_PixSelect);
814 TEST_ASSERT_BIT_LOW_MESSAGE(
817 "PixSelect must be LOW when the test starts."
822 TEST_ASSERT_BIT_HIGH_MESSAGE(
825 "Expect `EnterLisProgrammingMode` drives LisPixSelect HIGH."
830 void WriteLisConfigBit_outputs_bit_on_LisRst(
void)
833 uint8_t config_bytes[4];
834 config_bytes[0] = 0x01;
835 config_bytes[1] = 0x00;
836 TEST_ASSERT_BIT_HIGH_MESSAGE(
837 0, config_bytes[0],
"Test starts with byte0 bit0 = 1"
839 TEST_ASSERT_BIT_LOW_MESSAGE(
840 0, config_bytes[1],
"Test starts with byte1 bit0 = 0"
843 uint8_t * config = config_bytes; uint8_t bit_index = 0;
845 TEST_ASSERT_BIT_HIGH(Lis_Rst, *Lis_port1);
847 TEST_ASSERT_BIT_LOW(Lis_Rst, *Lis_port1);
849 void WriteLisConfigBit_waits_for_LisClk_HIGH(
void)
852 uint8_t config[4]; uint8_t bit_index = 0;
855 _AssertCall(1,
"_WaitForLisClkHigh");
857 void WriteLisConfigBit_waits_for_LisClk_LOW(
void)
860 uint8_t config[4]; uint8_t bit_index = 0;
863 _AssertCall(2,
"_WaitForLisClkLow");
867 void Write28bitLisConfig_writes_28bits_starting_at_byte0_bit0_and_ending_at_byte3_bit3(
void)
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;
875 uint8_t * config = config_bytes;
878 char const * call_name =
"_WriteLisConfigBit";
881 for (uint8_t byte_i = 0; byte_i<3; byte_i++)
883 for (uint8_t bit_i = 0; bit_i<8; bit_i++)
885 _AssertCall(call_n, call_name);
886 _AssertArg(call_n, 1, &config);
887 _AssertArg(call_n, 2, &bit_i);
893 for (uint8_t bit_i = 0; bit_i<4; bit_i++)
895 _AssertCall(call_n, call_name);
896 _AssertArg(call_n, 1, &config);
897 _AssertArg(call_n, 2, &bit_i);
903 void ExitLisProgrammingMode_outputs_LOW_on_pin_LisRst(
void)
906 SetBit(Lis_port1, Lis_Rst);
907 TEST_ASSERT_BIT_HIGH_MESSAGE(
910 "Rst must be HIGH when the test starts."
915 TEST_ASSERT_BIT_LOW_MESSAGE(
918 "Expect `Lis_Rst` LOW."
921 void ExitLisProgrammingMode_outputs_LOW_on_pin_LisPixSelect(
void)
924 SetBit(Lis_port2, Lis_PixSelect);
925 TEST_ASSERT_BIT_HIGH_MESSAGE(
928 "PixSelect must be HIGH when the test starts."
933 TEST_ASSERT_BIT_LOW_MESSAGE(
936 "Expect `Lis_PixSelect` LOW."
941 void LisExpose_waits_for_the_falling_edge_of_Lis_Clk(
void)
947 void LisExpose_starts_exposure_by_driving_Lis_Rst_HIGH(
void)
951 void LisExpose_counts_falling_edges_of_Lis_Clk_until_count_equals_exposure_ticks(
void)
955 void LisExpose_stops_exposure_by_driving_Lis_Rst_LOW(
void)
bool LisConfigIsValid(uint8_t binning, uint8_t gain, uint8_t active_rows)
void _ExitLisProgrammingMode(void)
uint8_t MSB(uint16_t msb_lsb)
void _ConfigAs28bits(uint8_t *config)
void _EnterLisProgrammingMode(void)
void _WaitForLisClkHigh(void)
void _WriteLisConfigBit(uint8_t const *config, uint8_t bit_index)
void LisWriteConfig(void)
uint16_t exposure_ticks
LIS-770i exposure time.
void _WaitForLisClkLow(void)
void _Write28bitLisConfig(uint8_t const *config)
uint8_t LSB(uint16_t msb_lsb)