2424int main ()
2525{
2626 char * test_str = "Hello World" ;
27- int bytes = strlen (test_str ); // get number of bytes in test string
28- uint8_t buf [32 ]; // read buffer
29- int ret ; // return value
27+
28+ // get number of bytes in test string, add 1 for the terminating null
29+ // character which strlen omits
30+ int bytes = strlen (test_str )+ 1 ;
31+
32+ char buf [32 ]; // read buffer
33+ int ret ; // return value
3034
3135 printf ("Make sure the MISO and MOSI lines are connected with\n" );
3236 printf ("a loopback jumper which is necessary for this test.\n" );
@@ -35,28 +39,17 @@ int main()
3539 return -1 ;
3640 }
3741
38- // attempt a string send/receive test
39- printf ("Sending %d bytes: %s\n" , bytes , test_str );
40-
41- while (1 ){
42- //ret = rc_spi_transfer(BUS, SLAVE, (uint8_t*)test_str, bytes, buf);
43- ret = rc_spi_write (BUS , SLAVE , (uint8_t * )test_str , bytes );
44- if (ret < 0 ){
45- printf ("send failed\n" );
46- rc_spi_close (SLAVE );
47- return -1 ;
48- }
49- else printf ("sent %d bytes: %s\n" ,bytes , test_str );
50- // read back
51- ret = rc_spi_read (BUS , SLAVE , buf , bytes );
52- if (ret < 0 ){
53- printf ("read failed\n" );
54- rc_spi_close (SLAVE );
55- return -1 ;
56- }
57- else printf ("Received %d bytes: %s\n" ,ret , (char * )buf );
58- rc_usleep (10000 );
42+ // attempt a string send/receive transfer
43+ printf ("transfer test:\n" );
44+ printf ("Sending %d bytes: %s\n" , bytes , test_str );
45+ ret = rc_spi_transfer (BUS , SLAVE , (uint8_t * )test_str , bytes , (uint8_t * )buf );
46+ if (ret < 0 ){
47+ printf ("send failed\n" );
48+ rc_spi_close (SLAVE );
49+ return -1 ;
5950 }
51+ else printf ("Received %d bytes: %s\n" ,ret , buf );\
52+
6053
6154 rc_spi_close (BUS );
6255 return 0 ;
0 commit comments