3 #include "test_VisCmd.h"
9 #define max_length_of_queue 5
12 static void _AssertCall(uint16_t num,
char const * name)
16 GString *message = g_string_new(NULL);
17 g_string_append_printf(message,
"`%s` is not call %d", name, num);
19 TEST_ASSERT_TRUE_MESSAGE(
20 AssertCall(mock, num, name),
24 g_string_free(message,
true);
26 static void _AssertArg(uint16_t call_n, uint8_t arg_n,
void *pval)
66 GString *msg = g_string_new(NULL);
67 g_string_printf(msg,
"Expect different value for call %d arg %d.", call_n, arg_n);
71 TEST_ASSERT_TRUE_MESSAGE(
72 AssertArg(mock, call_n, arg_n, pval),
75 g_string_free(msg,
true);
77 static void _AssertArgByteVal(uint16_t call_n, uint8_t arg_n, uint8_t byte_val) { _AssertArg(call_n, arg_n, &byte_val); }
78 static void _test_call_count_is(uint16_t num)
83 GString *msg = g_string_new(NULL);
85 "Expect call count is %d, but was %d.",
86 num, NumberOfActualCalls(mock)
89 if (num == NumberOfActualCalls(mock)) TEST_PASS();
90 else TEST_FAIL_MESSAGE(msg->str);
92 g_string_free(msg,
true);
97 void LisReadout_waits_for_Lis_Sync_to_go_HIGH_then_go_LOW(
void)
101 void LisReadout_reads_one_pixel_on_each_rising_edge_of_Lis_Clk(
void)
105 void LisReadout_LOOP_wait_for_the_rising_edge_of_Lis_Clk(
void)
109 void LisReadout_LOOP_start_the_ADC_conversion(
void)
113 void LisReadout_LOOP_wait_for_45_cycles_of_10MHz_clock(
void)
117 void LisReadout_LOOP_start_ADC_readout(
void)
121 void LisReadout_LOOP_wait_for_most_significant_byte_ADC_readout(
void)
125 void LisReadout_LOOP_save_MSB_to_frame_buffer(
void)
129 void LisReadout_LOOP_wait_for_least_significant_byte_ADC_readout(
void)
133 void LisReadout_LOOP_save_LSB_to_frame_buffer(
void)
140 void S13131Readout_must_be_called_immediately_after_S13131Expose(
void)
144 void S13131Readout_stores_pixel_values_in_a_global_array_named_frame(
void)
148 void S13131Readout_reads_one_pixel_on_each_falling_edge_of_CLK(
void)
152 void S13131Readout_LOOP_wait_for_a_CLK_falling_edge(
void)
156 SetBit(UartSpi_UCSR0A, UartSpi_RXC0);
160 _AssertCall(1,
"WaitForS13131ClkLow");
162 void S13131Readout_LOOP_start_the_ADC_conversion(
void)
166 SetBit(UartSpi_UCSR0A, UartSpi_RXC0);
170 _AssertCall(2,
"StartAdcConversion");
172 void S13131Readout_LOOP_wait_for_45_cycles_of_10MHz_clock(
void)
177 void S13131Readout_LOOP_start_ADC_readout(
void)
181 void S13131Readout_LOOP_wait_for_most_significant_byte_ADC_readout(
void)
185 void S13131Readout_LOOP_save_MSB_to_frame_buffer(
void)
189 void S13131Readout_LOOP_wait_for_least_significant_byte_ADC_readout(
void)
193 void S13131Readout_LOOP_save_LSB_to_frame_buffer(
void)
200 void GetSensorLED_waits_for_byte_led_num(
void)
204 void GetSensorLED_reads_byte_led_num(
void)
208 void GetSensorLED_sends_OK_and_LED_SETTING_if_led_num_is_valid(
void)
212 void GetSensorLED_sends_ERROR_and_pads_second_byte_if_led_num_is_invalid(
void)
218 void LedNumIsValid_returns_TRUE_if_led_num_is_0(
void)
223 void LedNumIsValid_returns_TRUE_if_led_num_is_1(
void)
228 void LedNumIsValid_returns_FALSE_if_led_num_is_not_0_or_1(
void)
235 void SetSensorLED_waits_for_byte_led_num(
void)
239 void SetSensorLED_reads_byte_led_num(
void)
243 void SetSensorLED_waits_for_byte_led_setting(
void)
247 void SetSensorLED_reads_byte_led_setting(
void)
251 void SetSensorLED_sends_ERROR_if_led_num_is_invalid(
void)
255 void SetSensorLED_sends_ERROR_if_led_setting_is_invalid(
void)
259 void SetSensorLED_applies_LED_setting_if_valid(
void)
263 void SetSensorLED_sends_OK_if_num_and_setting_are_valid(
void)
270 void GetSensorConfig_sends_OK(
void)
276 void GetSensorConfig_sends_binning(
void)
280 void GetSensorConfig_sends_gain(
void)
284 void GetSensorConfig_sends_row_bitmap(
void)
290 void SetSensorConfig_replies_OK_to_confirm_it_recognized_this_command(
void)
292 printf(
"\nTesting `SetSensorConfig_replies_OK_to_confirm_it_recognized_this_command...`\n");
312 SilentAssertCall(mock, call_n,
"SpiSlaveTxByte");
313 _AssertArgByteVal(call_n, 1, OK);
315 void SetSensorConfig_receives_three_bytes_of_config_from_Bridge(
void)
322 binning = BINNING_ON;
324 active_rows = ALL_ROWS_ACTIVE;
334 TEST_ASSERT_EQUAL_HEX8_MESSAGE(BINNING_OFF, binning,
"binning");
335 TEST_ASSERT_EQUAL_HEX8_MESSAGE(GAIN_5X, gain,
"gain");
336 TEST_ASSERT_EQUAL_HEX8_MESSAGE(ONLY_ROW_1_ACTIVE, active_rows,
"active_rows");
339 static bool _binning_is_valid(uint8_t binning)
342 (BINNING_ON == binning) ||
343 (BINNING_OFF == binning)
346 static bool _gain_is_valid(uint8_t gain)
350 (GAIN_2X5 == gain) ||
355 static bool _active_rows_is_valid(uint8_t active_rows)
357 return ( (active_rows & 0xE0) == 0x00 );
359 static bool _config_is_valid(
365 (_binning_is_valid(binning)) &&
366 (_gain_is_valid(gain)) &&
367 (_active_rows_is_valid(active_rows))
370 static void _print_list_of_invalid_config_values(
379 GString *msg = g_string_new(
"Invalid config value in this test: ");
380 if (!_binning_is_valid(binning)) g_string_append_printf(msg,
"binning ");
381 if (!_gain_is_valid(gain)) g_string_append_printf(msg,
"gain ");
382 if (!_active_rows_is_valid(active_rows)) g_string_append_printf(msg,
"active_rows");
383 g_string_append_printf(msg,
"\n");
385 if (!_config_is_valid(binning, gain, active_rows)) printf(
"%s", msg->str);
387 g_string_free(msg,
true);
389 static void _print_config_values(
394 printf(
"binning == %d, " , binning);
395 printf(
"gain == %d, " , gain);
396 printf(
"active_rows == %d\n", active_rows);
398 static void put_config_in_Queue(
403 printf(
"\n`put_config_in_Queue`...\n");
405 _print_list_of_invalid_config_values(binning, gain, active_rows);
407 _print_config_values(binning, gain, active_rows);
415 void SetSensorConfig_does_not_update_config_globals_if_config_is_invalid(
void)
419 binning = BINNING_ON;
421 active_rows = ALL_ROWS_ACTIVE;
432 TEST_ASSERT_EQUAL_HEX8_MESSAGE(BINNING_ON, binning,
"binning");
433 TEST_ASSERT_EQUAL_HEX8_MESSAGE(GAIN_1X, gain,
"gain");
434 TEST_ASSERT_EQUAL_HEX8_MESSAGE(ALL_ROWS_ACTIVE, active_rows,
"active_rows");
436 void SetSensorConfig_replies_ERROR_if_binning_is_invalid(
void)
438 printf(
"\nTesting `SetSensorConfig_replies_ERROR_if_binning_is_invalid...`\n");
450 SilentAssertCall(mock, call_n,
"SpiSlaveTxByte");
451 _AssertArgByteVal(call_n, 1, ERROR);
453 void SetSensorConfig_replies_ERROR_if_gain_is_invalid(
void)
456 printf(
"\nTesting `SetSensorConfig_replies_ERROR_if_gain_is_invalid`...\n");
467 _AssertCall(call_n,
"SpiSlaveTxByte");
468 _AssertArgByteVal(call_n, 1, ERROR);
470 void SetSensorConfig_replies_ERROR_if_active_rows_is_invalid(
void)
472 printf(
"\nTesting `SetSensorConfig_replies_ERROR_if_active_rows_is_invalid`...\n");
484 _AssertCall(call_n,
"SpiSlaveTxByte");
485 _AssertArgByteVal(call_n, 1, ERROR);
487 void SetSensorConfig_does_not_program_LIS_770i_if_config_is_invalid(
void)
501 _AssertCall(call_n,
"SpiSlaveTxByte");
502 _AssertArgByteVal(call_n, 1, ERROR);
503 _test_call_count_is(2);
505 static void _put_valid_config_in_Queue(
void)
517 void SetSensorConfig_writes_valid_config_to_LIS_770i_programmable_setup_register(
void)
520 _put_valid_config_in_Queue();
525 _AssertCall(2,
"LisWriteConfig");
527 void SetSensorConfig_replies_OK_if_all_config_values_are_valid(
void)
530 _put_valid_config_in_Queue();
535 _AssertCall(call_n,
"SpiSlaveTxByte");
536 _AssertArgByteVal(call_n, 1, OK);
538 void SetSensorConfig_the_OK_is_sent_after_LIS_is_programmed_with_new_config(
void)
541 _put_valid_config_in_Queue();
546 _AssertCall(call_n++,
"LisWriteConfig");
547 _AssertCall(call_n,
"SpiSlaveTxByte");
548 _AssertArgByteVal(call_n, 1, OK);
552 void GetExposure_sends_OK(
void)
558 void GetExposure_sends_MSB_of_exposure_ticks(
void)
562 void GetExposure_sends_LSB_of_exposure_ticks(
void)
568 void SetExposure_waits_for_byte_exposure_MSB(
void)
574 void SetExposure_reads_byte_exposure_MSB(
void)
578 void SetExposure_waits_for_byte_exposure_LSB(
void)
582 void SetExposure_reads_byte_exposure_LSB(
void)
586 void SetExposure_updates_global_exposure_ticks(
void)
590 void SetExposure_sends_OK(
void)
596 void CaptureFrame_sends_OK(
void)
611 void CaptureFrame_checks_binning_to_determine_number_of_pixels_in_frame(
void)
615 void CaptureFrame_sends_num_pixels_MSB(
void)
619 void CaptureFrame_sends_num_pixels_LSB(
void)
623 void CaptureFrame_exposes_the_pixels(
void)
627 void CaptureFrame_does_readout_of_num_pixels_into_the_frame_buffer(
void)
631 void CaptureFrame_sends_the_pixel_readings_stored_in_the_frame_buffer(
void)
659 pixel.MSB = (pixel_number-1)*2;
660 pixel.LSB = 1 + pixel.MSB;
665 void GetPeak_finds_the_peak_between_start_pixel_and_stop_pixel_inclusive(
void)
668 uint16_t _start_pixel = 1;
672 for (uint16_t byte_index=0; byte_index < sizeof_frame; byte_index++)
674 frame[byte_index] = 0;
677 TEST_ASSERT_EQUAL_HEX8_ARRAY_MESSAGE(
681 "Cannot run test: setup failed to empty the frame."
687 TEST_ASSERT_EQUAL_UINT16_MESSAGE(
689 "Cannot run test: byte_indices_of_pixel().MSB does not do what you think!"
691 TEST_ASSERT_EQUAL_UINT16_MESSAGE(
693 "Cannot run test: byte_indices_of_pixel().LSB does not do what you think!"
698 frame[pixel123.MSB] = 0xab;
699 frame[pixel123.LSB] = 0xcd;
700 TEST_ASSERT_EQUAL_UINT16(0xabcd,
GetPeak(_start_pixel,_stop_pixel));
703 frame[pixelstart.MSB] = 0xab;
704 frame[pixelstart.LSB] = 1 + 0xcd;
705 TEST_ASSERT_EQUAL_UINT16(1 + 0xabcd,
GetPeak(_start_pixel,_stop_pixel));
708 frame[pixelstop.MSB] = 0xab;
709 frame[pixelstop.LSB] = 2 + 0xcd;
710 TEST_ASSERT_EQUAL_UINT16(2 + 0xabcd,
GetPeak(_start_pixel,_stop_pixel));
712 void GetPeak_ignores_peaks_at_pixels_before_start_pixel_and_after_stop_pixel(
void)
716 uint16_t _start_pixel = 2;
720 for (uint16_t byte_index=0; byte_index < sizeof_frame; byte_index++)
722 frame[byte_index] = 0;
726 frame[pixel1.MSB] = 0x12;
727 frame[pixel1.LSB] = 0x34;
728 TEST_ASSERT_EQUAL_UINT16_MESSAGE(0,
GetPeak(_start_pixel,_stop_pixel),
729 "Peak detected at pixel before start pixel."
732 frame[pixel1.MSB] = 0;
733 frame[pixel1.LSB] = 0;
735 frame[pixelMAX.MSB] = 0x12;
736 frame[pixelMAX.LSB] = 0x35;
737 TEST_ASSERT_EQUAL_UINT16_MESSAGE(0,
GetPeak(_start_pixel,_stop_pixel),
738 "Peak detected at pixel after stop pixel."
742 void GetSensorHash_hash_sends_OK(
void)
747 uint8_t call_n = 1; uint8_t arg_n = 1; uint8_t
byte = OK;
748 _AssertCall(call_n,
"SpiSlaveTxByte");
749 _AssertArg(call_n, arg_n, &
byte);
751 void GetSensorHash_sends_first_three_bytes_of_SHA1_hash_of_sensor(
void)
754 printf(
"This is the LIS hash: 0x%x\n", LIS);
759 uint8_t first_byte = 0x35;
760 uint8_t second_byte = 0x1e;
761 uint8_t third_byte = 0xa9;
763 _AssertCall(call_n,
"SpiSlaveTxByte");
764 _AssertArg(call_n++, arg_n, &first_byte);
765 _AssertCall(call_n,
"SpiSlaveTxByte");
766 _AssertArg(call_n++, arg_n, &second_byte);
767 _AssertCall(call_n,
"SpiSlaveTxByte");
768 _AssertArg(call_n, arg_n, &third_byte);
771 printf(
"This is the S13131 hash: 0x%x\n", S13131);
776 uint8_t first_byte = 0x91;
777 uint8_t second_byte = 0xd3;
778 uint8_t third_byte = 0x18;
780 _AssertCall(call_n,
"SpiSlaveTxByte");
781 _AssertArg(call_n++, arg_n, &first_byte);
782 _AssertCall(call_n,
"SpiSlaveTxByte");
783 _AssertArg(call_n++, arg_n, &second_byte);
784 _AssertCall(call_n,
"SpiSlaveTxByte");
785 _AssertArg(call_n, arg_n, &third_byte);
#define MAX_NUM_PIXELS
LIS-770i maximum number of pixels.
void QueuePush(volatile Queue_s *SpiFifo, uint8_t data_to_push)
volatile Queue_s * QueueInit(volatile uint8_t *buffer, uint16_t const buffer_size_in_bytes)
void SetSensorConfig(void)
uint8_t frame[]
One frame of pixel data is, at most, 1568 bytes.
uint16_t GetPeak(uint16_t const, uint16_t const)
bool LedNumIsValid(bicolorled_num led_num)
Queue uses a byte array as a circular buffer.
#define max_length_of_queue
Maximum size of the Queue's FIFO buffer is 24 bytes.
volatile uint8_t spi_rx_buffer[max_length_of_queue]
Allocate static memory for the Queue's FIFO buffer.