/**
 * \internal
 * \brief Test sending and receiving 9-bit data by polling.
 *
 * This test sends (writes) one 9-bit data to the slave and
 * receives (reads) the data back and compares.
 *
 * Writing and reading are carried out by polling.
 *
 * \param test Current test case.
 */
static void run_transfer_9bit_test(const struct test_case *test)
{
	uint16_t txd_data = 0x155;
	uint16_t rxd_data = 0;

	/* Skip test if initialization failed */
	test_assert_true(test, spi_init_success,
			"Skipping test due to failed initialization");

	/* Send data to slave */
	spi_select_slave(&master, &slave_inst, true);
	while (!spi_is_ready_to_write(&master)) {
	}
	spi_write(&master, txd_data);
	while (!spi_is_write_complete(&master)) {
	}

	/* Dummy read SPI master data register */
	while (!spi_is_ready_to_read(&master)) {
	}
	spi_read(&master, &rxd_data);

	/* Read SPI slave data register */
	while (!spi_is_ready_to_read(&slave)) {
	}
	spi_read(&slave, &rxd_data);
	spi_select_slave(&master, &slave_inst, false);

	/* Output test result */
	test_assert_true(test, rxd_data == txd_data,
			"Failed transmitting/receiving byte. TX='%d', RX='%d'",
			txd_data, rxd_data);
}
Esempio n. 2
0
static sint8 spi_rw(uint8* pu8Mosi, uint8* pu8Miso, uint16 u16Sz)
{
	uint8 u8Dummy = 0;
	uint8 u8SkipMosi = 0, u8SkipMiso = 0;
	uint16_t txd_data = 0;
	uint16_t rxd_data = 0;

	if (!pu8Mosi) {
		pu8Mosi = &u8Dummy;
		u8SkipMosi = 1;
	}
	else if(!pu8Miso) {
		pu8Miso = &u8Dummy;
		u8SkipMiso = 1;
	}
	else {
		return M2M_ERR_BUS_FAIL;
	}

	spi_select_slave(&master, &slave_inst, true);

	while (u16Sz) {
		txd_data = *pu8Mosi;
		while (!spi_is_ready_to_write(&master))
			;
		while(spi_write(&master, txd_data) != STATUS_OK)
			;

		/* Read SPI master data register. */
		while (!spi_is_ready_to_read(&master))
			;
		while (spi_read(&master, &rxd_data) != STATUS_OK)
			;
		*pu8Miso = rxd_data;

		u16Sz--;
		if (!u8SkipMiso)
			pu8Miso++;
		if (!u8SkipMosi)
			pu8Mosi++;
	}

	while (!spi_is_write_complete(&master))
		;

	spi_select_slave(&master, &slave_inst, false);

	return M2M_SUCCESS;
}