Skip to content

Commit

Permalink
Remove unused includes and defines.
Browse files Browse the repository at this point in the history
Add spi_pull_* arguments for compatibility and warn if they are used.
Use rtapi_open_as_root instead of plain open.
  • Loading branch information
BsAtHome committed Dec 4, 2024
1 parent 9be6cd5 commit ca68c1b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
30 changes: 18 additions & 12 deletions src/hal/drivers/mesa-hostmot2/hm2_spix.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,12 @@
* this program; if not, see <https://www.gnu.org/licenses/>.
*/

/* Without Source Tree */
#undef WOST

#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <errno.h>

#include <hal.h>
#include <rtapi.h>
#include <rtapi_app.h>
#include <rtapi_slab.h>

#define HM2_LLIO_NAME "hm2_spix"
Expand Down Expand Up @@ -154,6 +143,18 @@ RTAPI_MP_INT(spi_noqueue, "Disable queued SPI requests, use for debugging only (
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)")

/*
* We have these for compatibility with the hm2_rpspi driver. You can simply
* change the driver name in the ini/hal files without having to switch
* arguments.
*/
static int spi_pull_miso = -1;
static int spi_pull_mosi = -1;
static int spi_pull_sclk = -1;
RTAPI_MP_INT(spi_pull_miso, "Obsolete parameter")
RTAPI_MP_INT(spi_pull_mosi, "Obsolete parameter")
RTAPI_MP_INT(spi_pull_sclk, "Obsolete parameter")

/*********************************************************************/
/*
* Buffer management for queued transfers.
Expand Down Expand Up @@ -493,7 +494,7 @@ ssize_t spix_read_file(const char *fname, void *buffer, size_t bufsize)

memset(buffer, 0, bufsize);

if(!(fd = open(fname, O_RDONLY))) {
if((fd = rtapi_open_as_root(fname, O_RDONLY)) < 0) {
LL_ERR("Cannot open '%s' for read (errno=%d: %s)\n", fname, errno, strerror(errno));
return -errno;
}
Expand Down Expand Up @@ -536,6 +537,11 @@ static int spix_setup(void)

}

// Check if spi_pull_{miso,mosi,sclk} were set and warn if they were
if(spi_pull_miso != -1 || spi_pull_mosi != -1 || spi_pull_sclk != -1) {
LL_WARN("Setting spi_pull_{miso,mosi,sclk} has no effect in the hm2_spix driver.\n");
}

// Set process-level message level if requested
if(spi_debug >= RTAPI_MSG_NONE && spi_debug <= RTAPI_MSG_ALL)
rtapi_set_msg_level(spi_debug);
Expand Down
7 changes: 4 additions & 3 deletions src/hal/drivers/mesa-hostmot2/spix_rpi3.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
// The min/max allowed frequencies of the SPI clock
#define SCLK_FREQ_MIN 30000
#define SCLK_FREQ_MAX 50000000
#define SCLK_FREQ_DEF 25000000 // Default

#define SPI_MAX_SPI 2 // SPI0 and SPI1

Expand Down Expand Up @@ -132,7 +131,7 @@ static bcm2835_spi_t *spi; // SPI peripheral structure in mmap'ed address space
static bcm2835_aux_t *aux; // AUX peripheral structure in mmap'ed address space
static uint32_t aux_enables; // Previous state of SPI1 enable

#define F_PERI 400000000UL
#define F_PERI 500000000UL
static uint32_t spiclk_base = F_PERI; // The base clock (sys_clk) for the SPI port dividers

/*********************************************************************/
Expand Down Expand Up @@ -498,8 +497,10 @@ static void peripheral_setup(void)

// Check if SPI1 needs to be enabled
aux_enables = reg_rd(&aux->enables);
if(!(aux_enables & AUX_ENABLES_SPI1))
if(!(aux_enables & AUX_ENABLES_SPI1)) {
reg_wr(&aux->enables, aux_enables | AUX_ENABLES_SPI1); // Enable SPI1
LL_WARN("SPI1 needed to be enabled.\n");
}

spi1_reset();
}
Expand Down
1 change: 0 additions & 1 deletion src/hal/drivers/mesa-hostmot2/spix_rpi5.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
// The min/max allowed frequencies of the SPI clock
#define SCLK_FREQ_MIN 4000
#define SCLK_FREQ_MAX 50000000
#define SCLK_FREQ_DEF 25000000 // Default

#define SPI_MAX_SPI 2 // SPI0 and SPI1

Expand Down
3 changes: 0 additions & 3 deletions src/hal/drivers/mesa-hostmot2/spix_spidev.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>

Expand Down Expand Up @@ -51,8 +50,6 @@ static int spidev_close(const spix_port_t *sp);
static int spi_transfer(const spix_port_t *sp, uint32_t *wptr, size_t txlen, int rw);

#define PORT_MAX 5
#define PORT_SPI0 0 // port index for hardware SPI0
#define PORT_SPI1 2 // port index for hardware SPI1
static spidev_port_t spi_ports[PORT_MAX] = {
{ .spix = { .width = 8, .name = "/dev/spidev0.0", .transfer = spi_transfer }, .fd = -1 },
{ .spix = { .width = 8, .name = "/dev/spidev0.1", .transfer = spi_transfer }, .fd = -1 },
Expand Down

0 comments on commit ca68c1b

Please sign in to comment.