Skip to content

Commit

Permalink
Fix Hz/kHz confusion.
Browse files Browse the repository at this point in the history
Don't close ports forcefully at exit, let the driver do that on cleanup.
Fix some text typos.
Don't add isopen to spix_spidev driver. It can be determined from the file descriptor.
  • Loading branch information
BsAtHome committed Dec 2, 2024
1 parent 782b891 commit 6d673c8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
3 changes: 1 addition & 2 deletions src/hal/drivers/mesa-hostmot2/hm2_spix.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ static int spix_setup(void)
if(!(spi_probe & (1 << i))) // Only probe if enabled
continue;

if(NULL == (port = hwdriver->open(i, spiclk_rate[j], spiclk_rate_rd[j]))) {
if(NULL == (port = hwdriver->open(i, spiclk_rate[j] * 1000, spiclk_rate_rd[j] * 1000))) {
LL_INFO("Failed to open hardware port index %d\n", i);
return i;
}
Expand Down Expand Up @@ -649,7 +649,6 @@ static void spix_cleanup(void)
buffer_free(&boards[i].wbuf);
buffer_free(&boards[i].rbuf);
buffer_free(&boards[i].rref);
hwdriver->close(boards[i].port);
}

if(hwdriver) {
Expand Down
4 changes: 2 additions & 2 deletions src/hal/drivers/mesa-hostmot2/spix.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ typedef struct __spix_driver_t {
* spix_port_t *open(int port, uint32_t clkw, uint32_t clkr)
*
* Open 'port' (number 0...N) with specified write clock 'clkw' and read
* clock 'clkr'. The driver will check the values.
* Return value is a reference to a the port to use or NULL on failure.
* clock 'clkr' (both in Hz). The driver will check the values.
* Return value is a reference to the port to use or NULL on failure.
*/
const spix_port_t *(*open)(int port, uint32_t clkw, uint32_t clkr);

Expand Down
4 changes: 2 additions & 2 deletions src/hal/drivers/mesa-hostmot2/spix_rpi3.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ spix_driver_t spix_rpi3 = {
.close = rpi3_close,
};

static int has_spi_module; // Set to non-zero when the kernel modules dw_spi and dw_spi_mmio are loaded
static int driver_enabled; // Set to non-zero ehen rpi3_setup() is successfully called
static int has_spi_module; // Set to non-zero when the kernel module spi_bcm2835 is loaded
static int driver_enabled; // Set to non-zero when rpi3_setup() is successfully called
static int port_probe_mask; // Which ports are requested

static void *peripheralmem = MAP_FAILED; // mmap'ed peripheral memory
Expand Down
27 changes: 11 additions & 16 deletions src/hal/drivers/mesa-hostmot2/spix_spidev.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@
*/
typedef struct __spi_port_t {
spix_port_t spix; // The upstream container
int isopen; // Non-zero if successfully opened
int fd; // Spidev file descriptor
uint32_t clkw; // Write clock divider setting
uint32_t clkr; // Read clock divider setting
uint32_t clkw; // Write clock setting
uint32_t clkr; // Read clock setting
} spidev_port_t;

/* Forward decls */
Expand Down Expand Up @@ -76,7 +75,7 @@ spix_driver_t spix_spidev = {
.close = spidev_close,
};

static int driver_enabled; // Set to non-zero ehen spidev_setup() is successfully called
static int driver_enabled; // Set to non-zero when spidev_setup() is successfully called
static int port_probe_mask; // Which ports are requested


Expand Down Expand Up @@ -107,10 +106,10 @@ static int spi_transfer(const spix_port_t *sp, uint32_t *wptr, size_t txlen, int
sit.len = txlen * sizeof(uint32_t);
sit.speed_hz = rw ? sdp->clkr : sdp->clkw;
sit.bits_per_word = 8;
sit.delay_usecs = 10; // Magic
if(ioctl(sdp->fd, SPI_IOC_MESSAGE(1), &sit) < 0) {
int e = errno;
LL_ERR("%s: SPI transfer failed: %s", sdp->spix.name, strerror(e));
return -e;
LL_ERR("%s: SPI transfer failed: %s", sdp->spix.name, strerror(errno));
return 0;
}

// Put the received words into host order
Expand Down Expand Up @@ -248,7 +247,7 @@ static int spidev_cleanup(void)

// Close any ports not closed already
for(i = 0; i < PORT_MAX; i++) {
if(spi_ports[i].isopen)
if(spi_ports[i].fd >= 0)
spix_spidev.close(&spi_ports[i].spix);
}

Expand Down Expand Up @@ -281,15 +280,14 @@ static const spix_port_t *spidev_open(int port, uint32_t clkw, uint32_t clkr)
return NULL;
}

if(sdp->isopen) {
if(sdp->fd >= 0) {
LL_ERR("%s: SPI port already open.\n", sdp->spix.name);
return NULL;
}

if(port_configure(sdp, clkw, clkr) < 0)
return NULL;

sdp->isopen = 1;
LL_INFO("%s: write clock rate calculated: %u Hz\n", sdp->spix.name, sdp->clkw);
LL_INFO("%s: read clock rate calculated: %u Hz\n", sdp->spix.name, sdp->clkr);

Expand All @@ -313,17 +311,14 @@ static int spidev_close(const spix_port_t *sp)
return -EINVAL;
}

if(!sdp->isopen) {
if(sdp->fd < 0) {
LL_ERR("%s: close(): SPI port not open.\n", sdp->spix.name);
return -ENODEV;
}

if(sdp->fd >= 0) {
close(sdp->fd);
sdp->fd = -1;
}
close(sdp->fd);
sdp->fd = -1;

sdp->isopen = 0;
return 0;
}

Expand Down

0 comments on commit 6d673c8

Please sign in to comment.