2222#define SPI_BUS 0
2323
2424/* SPI frequency in Hz */
25- #define SPI_FREQ 400000
25+ #define SPI_FREQ 100000
26+
27+ /* SPI bits per word*/
28+ #define SPI_BIT 16
2629
2730uint16_t pat [] = { 0xaa01 , 0x5502 , 0xaa03 , 0x5504 , 0xaa05 , 0x5506 , 0xaa07 , 0x5508 };
2831uint16_t pat_inv [] = { 0x5501 , 0xaa02 , 0x5503 , 0xaa04 , 0x5505 , 0xaa06 , 0x5507 , 0xaa08 };
@@ -45,69 +48,89 @@ main(int argc, char** argv)
4548 mraa_result_t status = MRAA_SUCCESS ;
4649 mraa_spi_context spi ;
4750 int i , j ;
51+ unsigned int tx_data [2 ];
52+ int spi_bus , spi_freq , spi_bit ;
53+ spi_bus = (argc >= 2 )?atoi (argv [1 ]):SPI_BUS ;
54+ spi_freq = (argc >= 3 )?atoi (argv [2 ]):SPI_FREQ ;
55+ printf ("Will set SPI BUS to %d, frequency to %d hz .\n" , spi_bus , spi_freq );
4856
4957 /* initialize mraa for the platform (not needed most of the times) */
5058 mraa_init ();
59+ usleep (50000 );
5160
5261 //! [Interesting]
5362 /* initialize SPI bus */
54- spi = mraa_spi_init (SPI_BUS );
63+ spi = mraa_spi_init (spi_bus );
64+ usleep (50000 );
5565 if (spi == NULL ) {
5666 fprintf (stderr , "Failed to initialize SPI\n" );
5767 mraa_deinit ();
5868 return EXIT_FAILURE ;
5969 }
6070
6171 /* set SPI frequency */
62- status = mraa_spi_frequency (spi , SPI_FREQ );
63- if (status != MRAA_SUCCESS )
72+ status = mraa_spi_frequency (spi , spi_freq );
73+ usleep (50000 );
74+ if (status != MRAA_SUCCESS ){
6475 goto err_exit ;
76+ }
77+
6578
6679 /* set big endian mode */
6780 status = mraa_spi_lsbmode (spi , 0 );
81+ usleep (50000 );
6882 if (status != MRAA_SUCCESS ) {
6983 goto err_exit ;
7084 }
7185
72- /* MAX7219/21 chip needs the data in word size */
73- status = mraa_spi_bit_per_word (spi , 16 );
86+ /* set the data in word size */
87+ status = mraa_spi_bit_per_word (spi , SPI_BIT );
88+ usleep (50000 );
7489 if (status != MRAA_SUCCESS ) {
75- fprintf (stdout , "Failed to set SPI Device to 16Bit mode\n" );
90+ fprintf (stdout , "Failed to set SPI Device to 16 Bit mode\n" );
7691 goto err_exit ;
7792 }
7893
7994 /* do not decode bits */
8095 mraa_spi_write_word (spi , 0x0009 ); //0x9 is for Decode mode, an 8x8 LEDs should be set to 0x00
81-
96+ usleep (50000 );
97+
8298 /* brightness of LEDs */
8399 mraa_spi_write_word (spi , 0x050a ); //0xA is for intersity, could be set from 0x0 to 0xF and 0xF is the brightest.
100+ usleep (50000 );
101+
84102
85103 /* show all scan lines */
86104 mraa_spi_write_word (spi , 0x070b ); //0xB is for Scan, means how many EEPROM you want to open.
105+ usleep (50000 );
87106
88107 /* set display on */
89108 mraa_spi_write_word (spi , 0x010c ); //0xC: set to 0x0 for shutdown, 0x1 for open.
109+ usleep (50000 );
90110
91111 /* testmode off */
92112 mraa_spi_write_word (spi , 0x000f ); //0xF is for test mode, if set to 0x0 every LED will set bright.
113+ usleep (50000 );
93114
94115 while (flag ) {
95116 /* set display pattern */
96- for (i = 0 ; i < sizeof (pat );i ++ )
117+ for (i = 0 ; i < sizeof (pat ); i ++ )
97118 {
98119 mraa_spi_write_word (spi , pat [i ]);
120+ usleep (50000 );
99121 }
100- sleep (2 );
122+ sleep (1 );
101123
102124 /* set inverted display pattern */
103- for (i = 0 ; i < sizeof (pat_inv );i ++ )
125+ for (i = 0 ; i < sizeof (pat_inv ); i ++ )
104126 {
105127 mraa_spi_write_word (spi , pat_inv [i ]);
128+ usleep (50000 );
106129 }
107- sleep (2 );
130+ sleep (1 );
108131
109132 /* clear the LED's */
110- for (i = 0 ; i < sizeof (pat_clear );i ++ )
133+ for (i = 0 ; i < sizeof (pat_clear ); i ++ )
111134 {
112135 mraa_spi_write_word (spi , pat_clear [i ]);
113136 }
@@ -116,18 +139,20 @@ main(int argc, char** argv)
116139 for (i = 1 ; i <= 8 ; i ++ ) {
117140 for (j = 0 ; j < 8 ; j ++ ) {
118141 mraa_spi_write_word (spi , i + (1 << (j + 8 )));
119- sleep ( 1 );
142+ usleep ( 50000 );
120143 }
121144 mraa_spi_write_word (spi , i << 8 );
122145 }
123146 }
124147
125148 /* stop spi */
126149 mraa_spi_stop (spi );
150+ usleep (50000 );
127151
128152 //! [Interesting]
129153 /* deinitialize mraa for the platform (not needed most of the times) */
130154 mraa_deinit ();
155+ usleep (50000 );
131156
132157 return EXIT_SUCCESS ;
133158
@@ -136,9 +161,10 @@ main(int argc, char** argv)
136161
137162 /* stop spi */
138163 mraa_spi_stop (spi );
164+ usleep (50000 );
139165
140166 /* deinitialize mraa for the platform (not needed most of the times) */
141167 mraa_deinit ();
142-
168+ usleep ( 50000 );
143169 return EXIT_FAILURE ;
144170}
0 commit comments