Skip to content

Commit

Permalink
Add a unified HM2 SPI driver supporting RPI3, RPi4, RPi5 and spidev.
Browse files Browse the repository at this point in the history
  • Loading branch information
BsAtHome committed Dec 2, 2024
1 parent 5aaf39d commit 3873e30
Show file tree
Hide file tree
Showing 14 changed files with 3,675 additions and 34 deletions.
82 changes: 82 additions & 0 deletions src/hal/drivers/mesa-hostmot2/dtcboards.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* This is a component for RaspberryPi and other boards for linuxcnc.
* Copyright (c) 2024 B.Stultiens <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, see <https://www.gnu.org/licenses/>.
*/
#ifndef HAL_DTCBOARDS_H
#define HAL_DTCBOARDS_H

/*
* Info about the hardware platform, see:
* https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#best-practices-for-revision-code-usage
* https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#check-raspberry-pi-model-and-cpu-across-distributions
*
* Reading /proc/device-tree/compatible should contain the relevant
* information. We should get a buffer containing a string-list like:
* "raspberrypi,5-model-b\0brcm,bcm2712\0"
* And yes, it has embedded NULs.
*
* The idea is to match one of the strings to assign the correct driver for the
* specific board.
*/
#define DTC_BOARD_MAKE_RPI "raspberrypi"

#define DTC_BOARD_MODEL_5CM "5-compute-module"
#define DTC_BOARD_MODEL_5B "5-model-b"
#define DTC_BOARD_MODEL_4CM "4-compute-module"
#define DTC_BOARD_MODEL_4B "4-model-b"
#define DTC_BOARD_MODEL_3CM "3-compute-module"
#define DTC_BOARD_MODEL_3BP "3-model-b-plus"
#define DTC_BOARD_MODEL_3AP "3-model-a-plus"
#define DTC_BOARD_MODEL_3B "3-model-b"
#define DTC_BOARD_MODEL_2B "2-model-b"
#define DTC_BOARD_MODEL_CM "compute-module"
#define DTC_BOARD_MODEL_BP "model-b-plus"
#define DTC_BOARD_MODEL_AP "model-a-plus"
#define DTC_BOARD_MODEL_BR2 "model-b-rev2"
#define DTC_BOARD_MODEL_B "model-b"
#define DTC_BOARD_MODEL_A "model-a"
#define DTC_BOARD_MODEL_ZERO_2W "model-zero-2-w"
#define DTC_BOARD_MODEL_ZERO_W "model-zero-w"
#define DTC_BOARD_MODEL_ZERO "model-zero"

#define DTC_SOC_MAKE_BRCM "brcm"

#define DTC_SOC_MODEL_BCM2712 "bcm2712"
#define DTC_SOC_MODEL_BCM2711 "bcm2711"
#define DTC_SOC_MODEL_BCM2837 "bcm2837"
#define DTC_SOC_MODEL_BCM2836 "bcm2836"
#define DTC_SOC_MODEL_BCM2835 "bcm2835"

/* The device-tree compatible strings for the boards */
#define DTC_RPI_SOC_BCM2712 DTC_SOC_MAKE_RPI "," DTC_SOC_MODEL_BCM2712
#define DTC_RPI_MODEL_5CM DTC_BOARD_MAKE_RPI "," DTC_BOARD_MODEL_5CM
#define DTC_RPI_MODEL_5B DTC_BOARD_MAKE_RPI "," DTC_BOARD_MODEL_5B

#define DTC_RPI_SOC_BCM2711 DTC_SOC_MAKE_RPI "," DTC_SOC_MODEL_BCM2711
#define DTC_RPI_MODEL_4CM DTC_BOARD_MAKE_RPI "," DTC_BOARD_MODEL_4CM
#define DTC_RPI_MODEL_4B DTC_BOARD_MAKE_RPI "," DTC_BOARD_MODEL_4B

#define DTC_RPI_SOC_BCM2837 DTC_SOC_MAKE_BRCM "," DTC_SOC_MODEL_BCM2837
#define DTC_RPI_MODEL_3CM DTC_BOARD_MAKE_RPI "," DTC_BOARD_MODEL_3CM
#define DTC_RPI_MODEL_3BP DTC_BOARD_MAKE_RPI "," DTC_BOARD_MODEL_3BP
#define DTC_RPI_MODEL_3AP DTC_BOARD_MAKE_RPI "," DTC_BOARD_MODEL_3AP
#define DTC_RPI_MODEL_3B DTC_BOARD_MAKE_RPI "," DTC_BOARD_MODEL_3B
#define DTC_RPI_MODEL_ZERO_2W DTC_BOARD_MAKE_RPI "," DTC_BOARD_MODEL_ZERO_2W

/* Older than a RPi3 (bcm2836 and bcm2835) is probably not a good idea to use. */

#endif
// vim: ts=4
62 changes: 62 additions & 0 deletions src/hal/drivers/mesa-hostmot2/eshellf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* This is a component for hostmot2 board drivers
* Copyright (c) 2013,2014,2020,2024 Michael Geszkiewicz <[email protected]>,
* Jeff Epler <[email protected]>
* B.Stultiens <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, see <https://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <sys/wait.h>

#include <rtapi.h>

int shell(char *command)
{
char *const argv[] = {"sh", "-c", command, NULL};
pid_t pid;
int res = rtapi_spawn_as_root(&pid, "/bin/sh", NULL, NULL, argv, environ);
if(res < 0)
perror("rtapi_spawn_as_root");
int status;
waitpid(pid, &status, 0);
if(WIFEXITED(status))
return WEXITSTATUS(status);
else if(WIFSTOPPED(status))
return WTERMSIG(status)+128;
return status;
}

int eshellf(const char *errpfx, const char *fmt, ...)
{
char commandbuf[1024];
va_list ap;
va_start(ap, fmt);
vsnprintf(commandbuf, sizeof(commandbuf), fmt, ap);
va_end(ap);

int res = shell(commandbuf);
if(res == EXIT_SUCCESS)
return 0;

rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: Failed to execute '%s'\n", errpfx ? errpfx : "eshellf()", commandbuf);
return -EINVAL;
}

/* vim: ts=4
*/
28 changes: 28 additions & 0 deletions src/hal/drivers/mesa-hostmot2/eshellf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* This is a component for hostmot2 board drivers
* Copyright (c) 2013,2014,2020,2024 Michael Geszkiewicz <[email protected]>,
* Jeff Epler <[email protected]>
* B.Stultiens <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, see <https://www.gnu.org/licenses/>.
*/
#ifndef HAL_HM2_ESHELLF_H
#define HAL_HM2_ESHELLF_H

int shell(char *command);
int eshellf(const char *errpfx, const char *fmt, ...);

#endif
/* vim: ts=4
*/
22 changes: 0 additions & 22 deletions src/hal/drivers/mesa-hostmot2/hm2_rpspi.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,28 +343,6 @@ RTAPI_MP_INT(spi_probe, "Bit-field to select which SPI/CE combinations to probe
static int spi_debug = -1;
RTAPI_MP_INT(spi_debug, "Set message level for debugging purpose [0...5] where 0=none and 5=all (default: -1; upstream defined)")

/*********************************************************************/
/*
* Synchronized read and write to peripheral memory.
* Ensures coherency between cores, cache and peripherals
*/
#define rmb() __sync_synchronize() // Read sync (finish all reads before continuing)
#define wmb() __sync_synchronize() // Write sync (finish all write before continuing)

RPSPI_ALWAYS_INLINE static inline uint32_t reg_rd(const volatile void *addr)
{
uint32_t val;
val = *(volatile uint32_t *)addr;
rmb();
return val;
}

RPSPI_ALWAYS_INLINE static inline void reg_wr(const volatile void *addr, uint32_t val)
{
wmb();
*(volatile uint32_t *)addr = val;
}

/*********************************************************************/
#if defined(RPSPI_DEBUG_PIN)
/*
Expand Down
Loading

0 comments on commit 3873e30

Please sign in to comment.