Skip to content

Commit

Permalink
Merge pull request #1079 from napicella/master
Browse files Browse the repository at this point in the history
Gpio access on the Raspberry pi Pico
  • Loading branch information
EtchedPixels authored Jul 13, 2024
2 parents 8f22643 + 3f6d0e5 commit 9f9d5cb
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 25 deletions.
1 change: 1 addition & 0 deletions Kernel/platform/platform-rpipico/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ add_executable(fuzix
swapper.c
tricks.S
core1.c
devgpio.c
usbdescriptors.c
../../dev/blkdev.c
../../dev/mbr.c
Expand Down
4 changes: 2 additions & 2 deletions Kernel/platform/platform-rpipico/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Set one of these depending.
export PICO_SDK_FETCH_FROM_GIT = yes
#export PICO_SDK_PATH = /home/dg/src/pico/pico-sdk
#export PICO_SDK_FETCH_FROM_GIT = yes
export PICO_SDK_PATH = ${HOME}/.pico-sdk/sdk/1.5.1

include ../../../version.mk

Expand Down
4 changes: 3 additions & 1 deletion Kernel/platform/platform-rpipico/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
* CS GPIO 5
*/

#define CONFIG_RC2040
#define CONFIG_MAKER_PI

/* We have a GPIO interface */
#define CONFIG_DEV_GPIO
/* Enable to make ^Z dump the inode table for debug */
#undef CONFIG_IDUMP
/* Enable to make ^A drop back into the monitor */
Expand Down
39 changes: 39 additions & 0 deletions Kernel/platform/platform-rpipico/devgpio.c
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;
}
10 changes: 10 additions & 0 deletions Kernel/platform/platform-rpipico/devsdspi.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
//Pico spi0 or spi1 must match GPIO pins used above.
#define Pico_SD_SPI_MOD spi1

#elif defined(CONFIG_MAKER_PI)
// Maker pico board
// https://www.adafruit.com/product/5160
#define Pico_SD_SCK 10
#define Pico_SD_TX 11
#define Pico_SD_RX 12
#define Pico_SD_CS 15

#define Pico_SD_SPI_MOD spi1

#else

/* Pico SPI GPIO connected to SD SPIO - David Given's Arrangement */
Expand Down
1 change: 1 addition & 0 deletions Kernel/platform/platform-rpipico/picosdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <pico/platform.h>
#include <hardware/gpio.h>
#include <hardware/regs/addressmap.h>
#include "pico/stdlib.h"

#define MANGLED 1
#include "mangle.h"
Expand Down
79 changes: 57 additions & 22 deletions Kernel/platform/platform-rpipico/utils/picoctl.c
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;
}

0 comments on commit 9f9d5cb

Please sign in to comment.