chiark / gitweb /
Added new SPI driver helpers.
[wiringPi.git] / examples / gertboard.c
1
2 /*
3  * gertboard.c:
4  *      Simple test for the SPI bus on the Gertboard
5  *
6  *      Hardware setup:
7  *              D/A port 0 jumpered to A/D port 0.
8  *
9  *      We output a sine wave on D/A port 0 and sample A/D port 0. We then
10  *      copy this value to D/A port 1 and use a 'scope on both D/A ports
11  *      to check all's well.
12  *
13  */
14
15 #include <stdio.h>
16 #include <stdint.h>
17 #include <math.h>
18
19 #define B_SIZE  200
20 #undef  DO_TIMING
21
22 #include <wiringPi.h>
23 #include <gertboard.h>
24
25 int main (void)
26 {
27   double angle ;
28   int i ;
29   uint32_t x1 ;
30   int  buffer [B_SIZE] ;
31
32 #ifdef  DO_TIMING
33   unsigned int now, then ;
34 #endif
35
36   printf ("Raspberry Pi Gertboard SPI test program\n") ;
37
38   if (wiringPiSetupSys () < 0)
39     return -1 ;
40
41   if (gertboardSPISetup () < 0)
42     return 1 ;
43
44 // Generate a Sine Wave
45
46   for (i = 0 ; i < B_SIZE ; ++i)
47   {
48     angle = ((double)i / (double)B_SIZE) * M_PI * 2.0 ;
49     buffer [i] = (int)rint ((sin (angle)) * 127.0 + 128.0) ;
50   }
51
52
53   for (;;)
54   {
55 #ifdef  DO_TIMING
56     then = millis () ;
57 #endif
58
59     for (i = 0 ; i < B_SIZE ; ++i)
60     {
61       gertboardAnalogWrite (0, buffer [i]) ;
62
63 #ifndef DO_TIMING
64       x1 = gertboardAnalogRead (0) ;
65       gertboardAnalogWrite (1, x1 >> 2) ;       // 10-bit A/D, 8-bit D/A
66 #endif
67     }
68
69 #ifdef  DO_TIMING
70     now = millis () ;
71     printf ("%4d mS, %9.7f S/sample", now - then, ((double)(now - then) / 1000.0) / (double)B_SIZE) ;
72     printf (" -> %9.4f samples/sec \n", 1 / (((double)(now - then) / 1000.0) / (double)B_SIZE)) ;
73 #endif
74   }
75
76   return 0 ;
77 }