#include #include // Test macros #include // Assertion macros #include // Diagnostic output #include // CYGNUM_HAL_STACK_SIZE_TYPICAL #include #include // Common SPI API #include // STM32 data structures #include //--------------------------------------------------------------------------- // Thread data structures. cyg_uint8 stack [CYGNUM_HAL_STACK_SIZE_TYPICAL]; cyg_thread thread_data; cyg_handle_t thread_handle; externC cyg_spi_cortexm_stm32_bus_t cyg_spi_stm32_bus1; //--------------------------------------------------------------------------- // SPI loopback device driver data structures. cyg_spi_cortexm_stm32_device_t loopback_device = { .spi_device.spi_bus = &cyg_spi_stm32_bus1.spi_bus, .dev_num = 0 , // Only 1 device. .cl_pol = 0, .cl_pha = 0, .cl_brate = 8000000, // Nominal 8Mhz. .cs_up_udly = 1, .cs_dw_udly = 1, .tr_bt_udly = 1, .bus_16bit = false, }; //--------------------------------------------------------------------------- //const char tx_data[] = {0,0,0,0,0,0,0,0,0,0}; //const char tx_data[] = {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1}; const char tx_data[] = {0,0xff}; //const char tx_data[] = "Testing, testing, 12, 123."; const char tx_data1[] = "Testing extended API..."; const char tx_data2[] = "Testing extended API for a second transaction."; char rx_data [sizeof(tx_data)]; char rx_data1 [sizeof(tx_data1)]; char rx_data2 [sizeof(tx_data2)]; //--------------------------------------------------------------------------- // Run single loopback transaction using simple transfer API call. void run_test_1 (cyg_bool polled) { diag_printf ("Test 1 : Simple transfer test (polled = %d).\n", polled ? 1 : 0); //cyg_spi_transfer (&loopback_device.spi_device, polled, sizeof (tx_data), // (const cyg_uint8*) &tx_data[0], (cyg_uint8*) NULL); cyg_spi_transfer (&loopback_device.spi_device, polled, sizeof (tx_data), (const cyg_uint8*) &tx_data[0], (cyg_uint8*) NULL); diag_printf (" Tx data : %s\n", tx_data); diag_printf (" Rx data : %s\n", rx_data); CYG_ASSERT (memcmp (tx_data, rx_data, sizeof (tx_data)) == 0, "Simple transfer loopback failed - mismatched data.\n"); } //--------------------------------------------------------------------------- // Run two loopback transactions using extended transfer API. void run_test_2 (cyg_bool polled) { diag_printf ("Test 2 : Extended API test (polled = %d).\n", polled ? 1 : 0); cyg_spi_transaction_begin (&loopback_device.spi_device); cyg_spi_transaction_transfer (&loopback_device.spi_device, polled, sizeof (tx_data1), (const cyg_uint8*) &tx_data1[0], (cyg_uint8*) &rx_data1[0], false); cyg_spi_transaction_transfer (&loopback_device.spi_device, polled, sizeof (tx_data2), (const cyg_uint8*) &tx_data2[0], (cyg_uint8*) &rx_data2[0], false); cyg_spi_transaction_end (&loopback_device.spi_device); diag_printf (" Tx data 1 : %s\n", tx_data1); diag_printf (" Rx data 1 : %s\n", rx_data1); diag_printf (" Tx data 2 : %s\n", tx_data2); diag_printf (" Rx data 2 : %s\n", rx_data2); CYG_ASSERT (memcmp (tx_data1, rx_data1, sizeof (tx_data1)) == 0, "Simple transfer loopback failed - mismatched data (transfer 1).\n"); CYG_ASSERT (memcmp (tx_data2, rx_data2, sizeof (tx_data2)) == 0, "Simple transfer loopback failed - mismatched data (transfer 2).\n"); } //--------------------------------------------------------------------------- // Run all PL022 SPI interface loopback tests. void run_tests (void) { diag_printf ("Running STM32 SPI driver loopback tests.\n"); run_test_1 (true); /* run_test_1 (false); run_test_2 (true); run_test_2 (false); */ CYG_TEST_PASS_FINISH ("Loopback tests ran OK"); } //--------------------------------------------------------------------------- // User startup - tests are run in their own thread. void cyg_user_start(void) { CYG_TEST_INIT(); cyg_thread_create( 10, // Arbitrary priority (cyg_thread_entry_t*) run_tests, // Thread entry point 0, // "test_thread", // Thread name &stack[0], // Stack CYGNUM_HAL_STACK_SIZE_TYPICAL, // Stack size &thread_handle, // Thread handle &thread_data // Thread data structure ); cyg_thread_resume(thread_handle); cyg_scheduler_start(); } //=============================================================================