4 #include "test_SpiMaster.h"
6 #include "ReadWriteBits.h"
10 static void _AssertCall(uint16_t num,
char const * name)
14 GString *message = g_string_new(NULL);
15 g_string_append_printf(message,
"`%s` is not call %d", name, num);
17 TEST_ASSERT_TRUE_MESSAGE(
18 AssertCall(mock, num, name),
22 g_string_free(message,
true);
26 enum bit_val {LOW, HIGH};
typedef enum bit_val bit_val;
27 static void setup_bit_val( spi_reg reg, spi_bit bit, bit_val v )
40 GString *msg = g_string_new(NULL);
41 g_string_printf(msg,
"Bit must be %s when the test starts", v ?
"HIGH" :
"LOW");
45 TEST_ASSERT_BIT_HIGH_MESSAGE(bit, *reg, msg->str);
50 TEST_ASSERT_BIT_LOW_MESSAGE(bit, *reg, msg->str);
54 g_string_printf(msg,
"Test setup with invalid bit value: %d? ", v);
55 g_string_append_printf(msg,
"Bit value must be LOW or HIGH.");
56 TEST_FAIL_MESSAGE(msg->str);
58 g_string_free(msg,
true);
62 static void test_bit_val_msg( spi_reg reg, spi_bit bit, bit_val v,
char * bit_name )
66 GString *msg = g_string_new(NULL);
68 if ((HIGH != v) && (LOW != v))
72 msg,
"Invalid bit value `%s` = %d", bit_name, v
74 g_string_append_printf(msg,
"Bit value must be LOW or HIGH.");
75 TEST_FAIL_MESSAGE(msg->str);
80 g_string_printf(msg,
"Expect `%s` (bit %d) ", bit_name, bit);
81 g_string_append_printf(
86 if ((HIGH == v) && BitIsSet(reg, bit)) TEST_PASS();
87 else if ((LOW == v) && BitIsClear(reg, bit)) TEST_PASS();
88 else TEST_FAIL_MESSAGE(msg->str);
90 g_string_free(msg,
true);
94 void SpiMasterInit_idles_SlaveSelect_high(
void)
103 void SpiMasterInit_makes_SlaveSelect_an_output(
void)
112 void SpiMasterInit_makes_Miso_an_input(
void)
121 void SpiMasterInit_enables_pullup_on_Miso(
void)
130 void SpiMasterInit_makes_DataReady_an_input(
void)
139 void SpiMasterInit_enables_pullup_on_DataReady(
void)
146 test_bit_val_msg(
Spi_PortOutput, Spi_DataReady, HIGH,
"Spi_DataReady");
148 void SpiMasterInit_makes_Mosi_an_output(
void)
157 void SpiMasterInit_makes_Sck_an_output(
void)
166 void SpiMasterInit_makes_this_MCU_the_SPI_Master(
void)
169 setup_bit_val(
Spi_SPCR, Spi_MasterSlaveSelect, LOW);
173 test_bit_val_msg(
Spi_SPCR, Spi_MasterSlaveSelect, HIGH,
"Spi_MasterSlaveSelect");
175 void SpiMasterInit_sets_SPI_Clock_to_10MHz_ext_osc_divided_by_8(
void)
178 setup_bit_val(
Spi_SPCR, Spi_ClockBit0, LOW);
179 setup_bit_val(
Spi_SPCR, Spi_ClockBit1, HIGH);
180 setup_bit_val(
Spi_SPSR, Spi_DoubleClock, LOW);
184 TEST_ASSERT_TRUE(BitIsSet(
Spi_SPCR, Spi_ClockBit0));
185 TEST_ASSERT_TRUE(BitIsClear(
Spi_SPCR, Spi_ClockBit1));
186 TEST_ASSERT_TRUE(BitIsSet(
Spi_SPSR, Spi_DoubleClock));
188 void SpiMasterInit_enables_the_SPI_hardware_module(
void)
191 setup_bit_val(
Spi_SPCR, Spi_Enable, LOW);
195 test_bit_val_msg(
Spi_SPCR, Spi_Enable, HIGH,
"Spi_Enable");
197 void SpiMasterInit_clears_SPI_interrupt_flag(
void)
203 _AssertCall(call_n,
"ClearSpiInterruptFlag");
207 void SpiMasterXfrByte_selects_the_SPI_slave(
void)
211 void SpiMasterXfrByte_loads_SPI_data_reg_with_the_byte_to_send(
void)
215 TEST_ASSERT_EQUAL_HEX8_MESSAGE(0x00, *
Spi_SPDR,
"Initialize SPDR = 0 before running test.");
220 TEST_ASSERT_EQUAL_HEX8(cmd, *
Spi_SPDR);
222 void SpiMasterXfrByte_waits_until_the_transfer_is_done_by_reading_the_SPI_Interrupt_Flag(
void)
226 void SpiMasterXfrByte_unselects_the_SPI_slave(
void)
235 void SpiMasterXfrByte_clears_the_SPI_Interrupt_Flag_by_reading_the_SPI_data_reg(
void)
239 void SpiMasterXfrByte_returns_the_byte_in_the_SPI_data_reg(
void)
243 TEST_ASSERT_EQUAL_HEX8_MESSAGE(0x00, *
Spi_SPDR,
"Initialize SPDR = 0 before running test.");
254 TEST_ASSERT_EQUAL_HEX8(cmd, byte_in);
uint8_t SpiMasterXfrByte(uint8_t byte)
spi_ptr Spi_SPSR
SPI Status Register.
spi_ptr Spi_SPCR
SPI Control Register.
spi_ptr Spi_SPDR
SPI Data Register.
spi_reg Spi_PortOutput
Atmel PORT.
spi_reg Spi_PortDirection
Atmel DDR.
See cfg/microspec.json in the Python API repository.