-
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.
rpipico: fix unintended collateral damage
Split the GPIO stuff off as picogpio, restore picoctl and fix a bug in both with the open check.
- Loading branch information
1 parent
07b98cb
commit 47ee7b6
Showing
3 changed files
with
89 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,31 @@ | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <stdio.h> | ||
#include <stdint.h> | ||
|
||
#include <sys/gpio.h> | ||
#include <fcntl.h> | ||
#include <sys/ioctl.h> | ||
#include "../pico_ioctl.h" | ||
|
||
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 main(int argc, char **argv) | ||
{ | ||
if (argc == 1 || strcmp(argv[1], "--help") == 0) | ||
{ | ||
puts("usage: picoctl [ --help ] <commmand>"); | ||
puts("Command list:"); | ||
puts("\tflash\tReset into flash mode."); | ||
return 0; | ||
} | ||
int fd = open("/dev/sys", O_RDWR, 0); | ||
if (fd == -1) | ||
{ | ||
perror("Failed to open /dev/sys"); | ||
exit(1); | ||
} | ||
if (ioctl(fd, PICOIOC_FLASH) != 0) | ||
{ | ||
perror("Failed to perform operation"); | ||
close(fd); | ||
exit(1); | ||
} | ||
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; | ||
return 0; | ||
} |
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,66 @@ | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <stdio.h> | ||
#include <stdint.h> | ||
|
||
#include <sys/gpio.h> | ||
#include <sys/ioctl.h> | ||
#include "../pico_ioctl.h" | ||
|
||
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; | ||
} | ||
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: picogpio <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 == -1) { | ||
perror("Failed to open /dev/gpio"); | ||
exit(1); | ||
} | ||
|
||
struct gpioreq gr; | ||
gr.pin = pin; | ||
gr.val = value; | ||
|
||
if (ioctl(fd, GPIOC_SET, &gr) != 0) { | ||
perror("Failed to perform operation"); | ||
close(fd); | ||
exit(1); | ||
} | ||
|
||
close(fd); | ||
return 0; | ||
} |