Skip to content

Commit 38b9ffd

Browse files
authored
Support SPI example. (#19)
* Support SPI example. * Minor update * Add ISR Usage * Update SPI example * Update ISR exmaple * Minor update * Minor update
1 parent 3975945 commit 38b9ffd

6 files changed

Lines changed: 193 additions & 47 deletions

File tree

examples/c/README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,33 @@ I2C_BUS means the supported I2C bus on ROScube platform, which starts from 0.
4545
```
4646

4747
# PWM
48-
`./pwm` can set the value to period of pwm.
48+
`./pwm <PWM_PIN> <PWM_FREQ>` can set the value to period of pwm.
4949

5050
```bash
5151
# For example, your pin of pwm is 22, and you want to set the period of pwm to 200us
5252
./pwm 22 200
5353
# To exit, using ctrl + c
54+
```
55+
56+
# SPI
57+
`./spi <SPI_BUS> <SPI_FREQ>` can set the value to spi.
58+
59+
Take MAX7219 chip with LED Matrix as example.
60+
Display set of patterns on MAX7219 repeately.
61+
62+
Note that we are using `/dev/spidev0.0`.
63+
64+
```bash
65+
# For example, your spi_device is 0, and you want to set the speed to 100000 hz.
66+
./spi 0 100000
67+
# Press Ctrl+C to exit
68+
```
69+
70+
# GPIO ISR
71+
`./gpio_advanced.c <PIN>` can configure GPIO pin for interruption.
72+
73+
```bash
74+
# For example, configure Pin 5 for interruption.
75+
./gpio_advanced.c
76+
# Press Ctrl+C to exit.
5477
```

examples/c/gpio_advanced.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
*
77
* SPDX-License-Identifier: MIT
88
*
9-
* Example usage: Configures GPIO pin for interrupt and waits 30 seconds for the isr to trigger
10-
*
9+
* Example usage: Configures GPIO pin for interrupt.
10+
* Press Ctrl+C to exit.
1111
*/
1212

1313
/* standard headers */
@@ -16,46 +16,57 @@
1616

1717
/* mraa header */
1818
#include "mraa/gpio.h"
19+
#include "mraa/led.h"
20+
21+
/* GPIO default */
22+
#define GPIO_PIN1 5
1923

20-
#define GPIO_PIN 6
24+
int count = 0;
2125

22-
void
23-
int_handler(void* args)
24-
{
25-
fprintf(stdout, "ISR triggered\n");
26+
void int_handler(void *arg) {
27+
mraa_gpio_context gpio = (mraa_gpio_context)arg;
28+
count += 1;
29+
printf("pin %d = %d\n",mraa_gpio_get_pin(gpio), mraa_gpio_read(gpio));
30+
printf("count = %d\n", count);
2631
}
2732

28-
int
29-
main()
30-
{
33+
int main(int argc, char *argv[]) {
3134
mraa_result_t status = MRAA_SUCCESS;
3235
mraa_gpio_context gpio;
36+
int gpio_pin1;
37+
gpio_pin1 = (argc >= 2)?atoi(argv[1]):GPIO_PIN1;
3338

3439
/* initialize mraa for the platform (not needed most of the times) */
3540
mraa_init();
3641

3742
//! [Interesting]
3843
/* initialize GPIO pin */
39-
gpio = mraa_gpio_init(GPIO_PIN);
44+
gpio = mraa_gpio_init(GPIO_PIN1);
45+
/* Wait for the GPIO to initialize */
46+
usleep(5000);
4047
if (gpio == NULL) {
41-
fprintf(stderr, "Failed to initialize GPIO %d\n", GPIO_PIN);
48+
fprintf(stderr, "Failed to initialize GPIO %d\n", GPIO_PIN1);
4249
mraa_deinit();
4350
return EXIT_FAILURE;
4451
}
4552

46-
/* set GPIO to input */
53+
/* set GPIO1 to input */
4754
status = mraa_gpio_dir(gpio, MRAA_GPIO_IN);
55+
usleep(5000);
4856
if (status != MRAA_SUCCESS) {
57+
fprintf(stderr, "Failed to set GPIO1 to input\n");
4958
goto err_exit;
5059
}
5160

5261
/* configure ISR for GPIO */
53-
status = mraa_gpio_isr(gpio, MRAA_GPIO_EDGE_BOTH, &int_handler, NULL);
62+
status = mraa_gpio_isr(gpio, MRAA_GPIO_EDGE_BOTH, &int_handler, (void *)gpio);
63+
/* Wait for the ISR to configure */
64+
usleep(5000);
5465
if (status != MRAA_SUCCESS) {
66+
fprintf(stderr, "Failed to configure ISR\n");
5567
goto err_exit;
5668
}
5769

58-
/* wait 30 seconds isr trigger */
5970
sleep(30);
6071

6172
/* close GPIO */

examples/c/spi.c

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
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

2730
uint16_t pat[] = { 0xaa01, 0x5502, 0xaa03, 0x5504, 0xaa05, 0x5506, 0xaa07, 0x5508 };
2831
uint16_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
}

examples/python/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,28 @@ python3 i2c.py 0 0x50
5252
# For example, your pin of pwm is 22, and you want to set the period of pwm to 200us
5353
python3 pwm.py 22 200
5454
# To exit, using ctrl + c
55+
```
56+
57+
# SPI
58+
59+
`python3 spi.py <SPI_Num> <SPI_Speed>` can set the value to the SPI.
60+
61+
Take MAX7219 chip with LED Matrix as example.
62+
Display set of patterns on MAX7219 repeately.
63+
64+
Note that we are using `/dev/spidev0.0`.
65+
66+
```bash
67+
# For example, your spi_device is 0, and you want to set the speed to 100000 hz.
68+
python3 spi.py 0 100000
69+
# Press Ctrl+C to exit
70+
```
71+
72+
# GPIO ISR
73+
`python3 spi.py <GPIO_PIN>` can triggers ISR upon GPIO state change.
74+
75+
```bash
76+
# For example, configure Pin 5 for interruption.
77+
python3 gpio_advanced.py 5.
78+
# Press ENTER to stop
5579
```

examples/python/gpio_advanced.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ class Counter:
1919
# inside a python interrupt you cannot use 'basic' types so you'll need to use
2020
# objects
2121
def isr_routine(gpio):
22-
print("pin " + repr(gpio.getPin(True)) + " = " + repr(gpio.read()))
2322
c.count += 1
23+
print("pin " + repr(gpio.getPin(True)) + " = " + repr(gpio.read()))
24+
print("count = ", c.count)
2425

2526
# GPIO
26-
pin = 6;
27+
pin = 5 if len(sys.argv) < 2 else int(sys.argv[1])
2728

2829
try:
2930
# initialise GPIO
@@ -36,7 +37,7 @@ def isr_routine(gpio):
3637
x.isr(mraa.EDGE_BOTH, isr_routine, x)
3738

3839
# wait until ENTER is pressed
39-
var = raw_input("Press ENTER to stop")
40+
var = input("Press ENTER to stop\n")
4041
x.isrExit()
4142
except ValueError as e:
42-
print(e)
43+
print(e)

0 commit comments

Comments
 (0)