-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1079 from napicella/master
Gpio access on the Raspberry pi Pico
- Loading branch information
Showing
7 changed files
with
113 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <kernel.h> | ||
#include <kdata.h> | ||
#include <gpio.h> | ||
#include "picosdk.h" | ||
|
||
// Access raspberry pi pico GPIO | ||
int gpio_ioctl(uarg_t request, char *data) { | ||
const uint8_t num_pins = 28; | ||
static struct gpioreq gr; | ||
|
||
if (request == GPIOC_COUNT) { | ||
return num_pins; | ||
} | ||
|
||
if (uget(data, &gr, sizeof(struct gpioreq)) == -1) { | ||
return -1; | ||
} | ||
|
||
if (gr.pin >= num_pins) { | ||
udata.u_error = ENODEV; | ||
return -1; | ||
} | ||
|
||
switch (request) { | ||
case GPIOC_SETBYTE: | ||
break; | ||
case GPIOC_SET: | ||
gpio_init(gr.pin); | ||
gpio_set_dir(gr.pin, GPIO_OUT); | ||
gpio_put(gr.pin, gr.val != 0); | ||
return 0; | ||
break; | ||
default: | ||
udata.u_error = ENODEV; | ||
return -1; | ||
} | ||
|
||
return -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,66 @@ | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <stdint.h> | ||
|
||
#include <sys/gpio.h> | ||
#include <sys/ioctl.h> | ||
#include "../pico_ioctl.h" | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc == 1 || strcmp(argv[1], "--help") == 0) | ||
{ | ||
puts("usage: picoioctl [ --help ] <commmand>"); | ||
puts("Command list:"); | ||
puts("\tflash\tReset into flash mode."); | ||
return 0; | ||
int is_valid_uint8(const char *str) { | ||
char *endptr; | ||
long val = strtol(str, &endptr, 10); | ||
|
||
// check string was a number and within the range of uint8_t | ||
if (*endptr == '\0' && val >= 0 && val <= UINT8_MAX) { | ||
return 1; | ||
} | ||
int fd = open("/dev/sys", O_RDWR, 0); | ||
if (!fd) | ||
{ | ||
puts("Failed to open /dev/sys."); | ||
exit(1); | ||
} | ||
if (ioctl(fd, PICOIOC_FLASH) != 0) | ||
{ | ||
puts("Failed to perform operation."); | ||
close(fd); | ||
exit(1); | ||
} | ||
close(fd); | ||
return 0; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
if (argc == 1 || strcmp(argv[1], "--help") == 0) | ||
{ | ||
puts("Turn on/off GPIO pins on the pico"); | ||
puts("Usage: picoctl <PIN> <VALUE>"); | ||
return 0; | ||
} | ||
|
||
if (argc != 3) { | ||
fprintf(stderr, "Usage: %s <pin> <value>\n", argv[0]); | ||
return 1; | ||
} | ||
|
||
if (!is_valid_uint8(argv[1])) { | ||
fprintf(stderr, "Error: <pin> must be a valid uint8_t value\n"); | ||
return 1; | ||
} | ||
uint8_t pin = (uint8_t)strtoul(argv[1], NULL, 10); | ||
|
||
if (!is_valid_uint8(argv[2])) { | ||
fprintf(stderr, "Error: <value> must be a valid uint8_t value\n"); | ||
return 1; | ||
} | ||
uint8_t value = (uint8_t)strtoul(argv[2], NULL, 10); | ||
|
||
int fd = open("/dev/gpio", O_RDWR, 0); | ||
if (!fd) { | ||
puts("Failed to open /dev/gpio."); | ||
exit(1); | ||
} | ||
|
||
struct gpioreq gr; | ||
gr.pin = pin; | ||
gr.val = value; | ||
|
||
if (ioctl(fd, GPIOC_SET, &gr) != 0) { | ||
puts("Failed to perform operation."); | ||
close(fd); | ||
exit(1); | ||
} | ||
|
||
close(fd); | ||
return 0; | ||
} |