5 #include "ReadWriteBits.h"
8 static void _AssertCall(uint16_t num,
char const * name)
12 GString *message = g_string_new(NULL);
13 g_string_append_printf(message,
"`%s` is not call %d", name, num);
15 TEST_ASSERT_TRUE_MESSAGE(
16 AssertCall(mock, num, name),
20 g_string_free(message,
true);
22 static void _AssertArg(uint16_t call_n, uint8_t arg_n,
void *pval)
62 GString *msg = g_string_new(NULL);
63 g_string_printf(msg,
"Expect different value for call %d arg %d.", call_n, arg_n);
67 TEST_ASSERT_TRUE_MESSAGE(
68 AssertArg(mock, call_n, arg_n, pval),
71 g_string_free(msg,
true);
74 enum bit_val {LOW, HIGH};
typedef enum bit_val bit_val;
75 static void setup_bit_val( usb_reg reg, usb_pin bit, bit_val v )
88 GString *msg = g_string_new(NULL);
89 g_string_printf(msg,
"Bit must be %s when the test starts", v ?
"HIGH" :
"LOW");
93 TEST_ASSERT_BIT_HIGH_MESSAGE(bit, *reg, msg->str);
98 TEST_ASSERT_BIT_LOW_MESSAGE(bit, *reg, msg->str);
102 g_string_printf(msg,
"Test setup with invalid bit value: %d? ", v);
103 g_string_append_printf(msg,
"Bit value must be LOW or HIGH.");
104 TEST_FAIL_MESSAGE(msg->str);
106 g_string_free(msg,
true);
109 static void test_bit_val_msg(
117 GString *msg = g_string_new(NULL);
119 if ((HIGH != v) && (LOW != v))
123 msg,
"Invalid bit value `%s` = %d", bit_name, v
125 g_string_append_printf(msg,
"Bit value must be LOW or HIGH.");
126 TEST_FAIL_MESSAGE(msg->str);
131 g_string_printf(msg,
"Expect `%s` (bit %d) ", bit_name, bit);
132 g_string_append_printf(
137 if ((HIGH == v) && BitIsSet(reg, bit)) TEST_PASS();
138 else if ((LOW == v) && BitIsClear(reg, bit)) TEST_PASS();
139 else TEST_FAIL_MESSAGE(msg->str);
141 g_string_free(msg,
true);
147 void FtClockDatabus_drives_FtClock_HIGH_if_direction_is_FtDrive(
void)
150 setup_bit_val(FtCtrl_port, FtClock, LOW);
154 test_bit_val_msg(FtCtrl_port, FtClock, HIGH,
"FtClock");
156 void FtClockDatabus_drives_FtClock_LOW_if_direction_is_FtSample(
void)
159 setup_bit_val(FtCtrl_port, FtClock, HIGH);
163 test_bit_val_msg(FtCtrl_port, FtClock, LOW,
"FtClock");
167 void FtReadDatabus_copies_databus_pin_values_to_address_pbyte(
void)
172 uint8_t
byte = 0; uint8_t * pbyte = &byte;
175 TEST_ASSERT_EQUAL_HEX8(*FtData_pin,
byte);
179 void FtWriteDatabus_outputs_byte_on_databus_pins(
void)
187 TEST_ASSERT_EQUAL_HEX8(
byte, *FtData_port);
191 void FtDatabusPinDirection_makes_databus_pins_outputs_if_direction_is_FtOut(
void)
198 TEST_ASSERT_EQUAL_HEX8(0xFF, *FtData_ddr);
200 void FtDatabusPinDirection_makes_databus_pins_inputs_if_direction_is_FtIn(
void)
207 TEST_ASSERT_EQUAL_HEX8(0x00, *FtData_ddr);
213 void FtSelectFT221X_drives_FtChipSelect_LOW(
void)
216 setup_bit_val(FtCtrl_port, FtChipSelect, HIGH);
220 test_bit_val_msg(FtCtrl_port, FtChipSelect, LOW,
"FtChipSelect");
224 void FtUnselectFT221X_drives_FtChipSelect_HIGH(
void)
227 setup_bit_val(FtCtrl_port, FtChipSelect, LOW);
231 test_bit_val_msg(FtCtrl_port, FtChipSelect, HIGH,
"FtChipSelect");
235 void FtBusTurnaround_clocks_one_cycle_to_signal_data_drive_then_data_sample(
void)
237 puts(
"Drive clock to signal Data-Drive then Data-Sample:\n");
238 puts(
"- FtDrive: rising edge");
239 puts(
"- FtSample: falling edge");
244 uint16_t call_n; uint8_t direction;
247 SilentAssertCall(mock, call_n,
"_FtClockDatabus");
250 _AssertArg(call_n, 1, &direction);
253 SilentAssertCall(mock, call_n,
"_FtClockDatabus");
255 direction = FtSample;
256 _AssertArg(call_n, 1, &direction);
260 void FtIsOk_returns_true_if_FtMiso_is_LOW(
void)
262 setup_bit_val(FtCtrl_pin, FtMiso, LOW);
263 TEST_ASSERT_TRUE(
FtIsOk());
265 void FtIsOk_returns_false_if_FtMiso_is_HIGH(
void)
267 setup_bit_val(FtCtrl_pin, FtMiso, HIGH);
268 TEST_ASSERT_FALSE(
FtIsOk());
272 void FtRead_clocks_one_byte_out_of_the_FT221X(
void)
275 uint8_t
byte = 0; uint8_t *pbyte = &byte;
279 uint16_t call_n; uint8_t direction;
282 SilentAssertCall(mock, call_n,
"_FtClockDatabus");
285 _AssertArg(call_n, 1, &direction);
288 SilentAssertCall(mock, call_n,
"_FtClockDatabus");
290 direction = FtSample;
291 _AssertArg(call_n, 1, &direction);
294 void FtRead_stores_the_byte_at_address_pbyte(
void)
297 uint8_t
byte = 0xcd; uint8_t *pbyte = &byte;
301 SilentAssertCall(mock, call_n,
"_FtReadDatabus");
303 _AssertArg(call_n, 1, &pbyte);
309 void FtWrite_signals_to_drive_data_onto_the_databus(
void)
314 uint16_t call_n; uint8_t direction;
317 SilentAssertCall(mock, call_n,
"_FtClockDatabus");
320 _AssertArg(call_n, 1, &direction);
322 void FtWrite_sets_microcontroller_databus_pins_as_outputs(
void)
328 SilentAssertCall(mock, call_n,
"_FtDatabusPinDirection");
329 uint8_t pin_direction = FtOut;
330 _AssertArg(call_n, 1, &pin_direction);
332 void FtWrite_outputs_byte_on_databus_pins(
void)
339 SilentAssertCall(mock, call_n,
"_FtWriteDatabus");
340 _AssertArg(call_n, 1, &
byte);
342 void FtWrite_signals_FT221X_to_sample_the_databus(
void)
347 uint16_t call_n; uint8_t direction;
350 SilentAssertCall(mock, call_n,
"_FtClockDatabus");
352 direction = FtSample;
353 _AssertArg(call_n, 1, &direction);
355 void FtWrite_sets_microcontroller_databus_pins_as_inputs(
void)
361 SilentAssertCall(mock, call_n,
"_FtDatabusPinDirection");
362 uint8_t pin_direction = FtIn;
363 _AssertArg(call_n, 1, &pin_direction);
369 void UsbRxbufferIsEmpty_returns_true_if_pin_FT1248_MISO_is_HIGH(
void)
372 SetBit(FtCtrl_pin, FtMiso);
376 void UsbRxbufferIsEmpty_returns_false_if_pin_FT1248_MISO_is_LOW(
void)
379 ClearBit(FtCtrl_pin, FtMiso);
385 void UsbTxbufferIsFull_returns_true_if_pin_MIOSIO0_is_HIGH(
void)
388 SetBit(FtData_pin, FtMiosio0);
392 void UsbTxbufferIsFull_returns_false_if_pin_MIOSIO0_is_LOW(
void)
395 ClearBit(FtData_pin, FtMiosio0);
401 void UsbReadByte_selects_the_FT221X(
void)
404 uint8_t
byte = 0; uint8_t * pbyte = &byte;
408 _AssertCall(++call_n,
"FtSelectFT221X");
410 void UsbReadByte_drives_databus_with_read_command(
void)
412 puts(
"\n*Read command is* `0xc6`");
414 uint8_t
byte = 0; uint8_t * pbyte = &byte;
418 SilentAssertCall(mock, ++call_n,
"FtSelectFT221X");
419 SilentAssertCall(mock, ++call_n,
"FtWrite");
420 uint8_t read_cmd = FtReadCmd;
421 _AssertArg(call_n, 1, &read_cmd);
423 void UsbReadByte_signals_FT221X_to_sample_the_databus(
void)
426 uint8_t
byte = 0; uint8_t * pbyte = &byte;
430 SilentAssertCall(mock, ++call_n,
"FtSelectFT221X");
431 SilentAssertCall(mock, ++call_n,
"FtWrite");
432 _AssertCall(++call_n,
"FtBusTurnaround");
434 void UsbReadByte_reads_the_byte_if_data_transfer_status_is_OK(
void)
437 uint8_t
byte = 0; uint8_t * pbyte = &byte;
441 SilentAssertCall(mock, ++call_n,
"FtSelectFT221X");
442 SilentAssertCall(mock, ++call_n,
"FtWrite");
443 SilentAssertCall(mock, ++call_n,
"FtBusTurnaround");
444 _AssertCall(++call_n,
"FtIsOk");
445 TEST_FAIL_MESSAGE(
"Implement Test");
448 void UsbReadByte_unselects_the_FT221X(
void)
451 uint8_t
byte = 0; uint8_t * pbyte = &byte;
455 SilentAssertCall(mock, ++call_n,
"FtSelectFT221X");
456 SilentAssertCall(mock, ++call_n,
"FtWrite");
457 SilentAssertCall(mock, ++call_n,
"FtBusTurnaround");
458 SilentAssertCall(mock, ++call_n,
"FtIsOk");
459 _AssertCall(++call_n,
"FtUnselectFT221X");
461 void UsbReadByte_returns_either_FtOK_if_pbyte_has_the_read_data_or_FtError_if_Usb_receive_buffer_was_empty(
void)
463 TEST_FAIL_MESSAGE(
"Implement Test");
usb-bridge communicates over USB via FTDI USB Bridge IC FT221X
void _FtClockDatabus(uint8_t direction)
bool UsbRxbufferIsEmpty(void)
bool UsbTxbufferIsFull(void)
void FtSelectFT221X(void)
uint8_t UsbReadByte(uint8_t *pbyte)
void _FtWriteDatabus(uint8_t byte)
void FtUnselectFT221X(void)
void _FtReadDatabus(uint8_t *pbyte)
void FtWrite(uint8_t byte)
void FtRead(uint8_t *pbyte)
void FtBusTurnaround(void)
void _FtDatabusPinDirection(uint8_t pin_direction)