Skip to content

Commit

Permalink
Merge pull request #15902 from maribu/spi-api-change-1
Browse files Browse the repository at this point in the history
drivers/periph_spi: let spi_acquire return void
  • Loading branch information
fjmolinas authored Sep 2, 2021
2 parents 550565f + 2efc50b commit a1cbcc9
Show file tree
Hide file tree
Showing 39 changed files with 148 additions and 264 deletions.
7 changes: 3 additions & 4 deletions cpu/atmega_common/periph/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
*
* @}
*/
#include <assert.h>

#include "cpu.h"
#include "mutex.h"
#include "assert.h"
#include "periph/spi.h"

/**
Expand Down Expand Up @@ -78,10 +78,11 @@ void spi_init_pins(spi_t bus)
#endif
}

int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
{
(void)bus;
(void)cs;
assert(bus == SPI_DEV(0));

/* lock the bus and power on the SPI peripheral */
mutex_lock(&lock);
Expand All @@ -94,8 +95,6 @@ int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
/* clear interrupt flag by reading SPSR and data register by reading SPDR */
(void)SPSR;
(void)SPDR;

return SPI_OK;
}

void spi_release(spi_t bus)
Expand Down
4 changes: 1 addition & 3 deletions cpu/atxmega/periph/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void spi_init_pins(spi_t bus)
gpio_init(spi_config[bus].mosi_pin, GPIO_OUT);
}

int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
{
(void)cs;
(void)clk;
Expand All @@ -106,8 +106,6 @@ int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)

(void)dev(bus)->STATUS;
(void)dev(bus)->DATA;

return SPI_OK;
}

void spi_release(spi_t bus)
Expand Down
10 changes: 5 additions & 5 deletions cpu/cc2538/periph/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
* @}
*/

#include <assert.h>

#include "vendor/hw_memmap.h"
#include "vendor/hw_ssi.h"

#include "cpu.h"
#include "mutex.h"
#include "assert.h"
#include "periph/spi.h"

#define ENABLE_DEBUG 0
Expand Down Expand Up @@ -93,10 +94,10 @@ void spi_init_pins(spi_t bus)
gpio_init_mux(spi_config[bus].miso_pin, OVERRIDE_DISABLE, GPIO_MUX_NONE, rxd);
}

int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
{
DEBUG("%s: bus=%u\n", __FUNCTION__, bus);
(void) cs;
(void)cs;
/* lock the bus */
mutex_lock(&locks[bus]);
/* power on device */
Expand All @@ -107,8 +108,6 @@ int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
dev(bus)->CR0 = ((spi_clk_config[clk].scr << 8) | mode | SSI_CR0_DSS(8));
/* enable SSI device */
dev(bus)->CR1 = SSI_CR1_SSE;

return SPI_OK;
}

void spi_release(spi_t bus)
Expand All @@ -132,6 +131,7 @@ static uint8_t _trx(cc2538_ssi_t *dev, uint8_t in)
void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont,
const void *out, void *in, size_t len)
{
assert((unsigned)bus < SPI_NUMOF);
DEBUG("%s: bus=%u, len=%u\n", __FUNCTION__, bus, (unsigned)len);

const uint8_t *out_buf = out;
Expand Down
9 changes: 4 additions & 5 deletions cpu/efm32/periph/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ void spi_init_pins(spi_t bus)
gpio_init(spi_config[bus].miso_pin, GPIO_IN_PD);
}

int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
{
(void)cs;
assert((unsigned)bus < SPI_NUMOF);

mutex_lock(&spi_lock[bus]);

Expand All @@ -68,8 +69,8 @@ int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)

USART_InitSync_TypeDef init = USART_INITSYNC_DEFAULT;

init.baudrate = (uint32_t) clk;
init.clockMode = (USART_ClockMode_TypeDef) mode;
init.baudrate = (uint32_t)clk;
init.clockMode = (USART_ClockMode_TypeDef)mode;
init.msbf = true;

USART_InitSync(spi_config[bus].dev, &init);
Expand All @@ -86,8 +87,6 @@ int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
USART_ROUTEPEN_TXPEN |
USART_ROUTEPEN_CLKPEN);
#endif

return SPI_OK;
}

void spi_release(spi_t bus)
Expand Down
6 changes: 2 additions & 4 deletions cpu/esp_common/periph/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ int spi_init_cs(spi_t bus, spi_cs_t cs)
return SPI_OK;
}

int IRAM_ATTR spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
void IRAM_ATTR spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
{
DEBUG("%s bus=%u cs=%u mode=%u clk=%u\n", __func__, bus, cs, mode, clk);

Expand All @@ -308,7 +308,7 @@ int IRAM_ATTR spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk
LOG_TAG_ERROR("spi",
"SPI_DEV(%d) CS signal could not be initialized\n",
bus);
return SPI_NOCS;
assert(0);
}

/* lock the bus */
Expand Down Expand Up @@ -375,8 +375,6 @@ int IRAM_ATTR spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk

DEBUG("%s bus %d: SPI_CLOCK_REG=%08x\n",
__func__, bus, _spi[bus].regs->clock.val);

return SPI_OK;
}

void IRAM_ATTR spi_release(spi_t bus)
Expand Down
7 changes: 3 additions & 4 deletions cpu/fe310/periph/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
* @}
*/

#include <assert.h>

#include "cpu.h"
#include "mutex.h"
#include "assert.h"
#include "periph/spi.h"

#include "vendor/spi.h"
Expand Down Expand Up @@ -102,7 +103,7 @@ int spi_init_cs(spi_t dev, spi_cs_t cs)
return SPI_OK;
}

int spi_acquire(spi_t dev, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
void spi_acquire(spi_t dev, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
{
(void)cs;
assert(dev < SPI_NUMOF);
Expand All @@ -111,8 +112,6 @@ int spi_acquire(spi_t dev, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)

_REG32(spi_config[dev].addr, SPI_REG_SCKDIV) = _spi_clks_config[clk];
_REG32(spi_config[dev].addr, SPI_REG_SCKMODE) = mode;

return SPI_OK;
}

void spi_release(spi_t dev)
Expand Down
10 changes: 5 additions & 5 deletions cpu/kinetis/periph/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
* @}
*/

#include <assert.h>

#include "cpu.h"
#include "mutex.h"
#include "assert.h"
#include "periph/spi.h"

#define ENABLE_DEBUG 0
Expand Down Expand Up @@ -144,9 +145,10 @@ int spi_init_cs(spi_t bus, spi_cs_t cs)
return SPI_OK;
}

int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
{
(void) cs;
(void)cs;
assert((unsigned)bus < SPI_NUMOF);
/* lock and power on the bus */
mutex_lock(&locks[bus]);
poweron(bus);
Expand All @@ -156,8 +158,6 @@ int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)

/* configure clock and mode */
dev(bus)->CTAR[0] = (mode | SPI_CTAR_FMSZ(7) | spi_clk_config[clk]);

return SPI_OK;
}

void spi_release(spi_t bus)
Expand Down
7 changes: 3 additions & 4 deletions cpu/lm4f120/periph/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ void spi_init_pins(spi_t bus)
ROM_GPIOPinTypeSSI(spi_confs[bus].gpio_port, spi_confs[bus].pins.mask);
}

int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
{
(void) cs;
(void)cs;
assert((unsigned)bus < SPI_NUMOF);
/* lock bus */
mutex_lock(&locks[bus]);
/* enable clock for SSI */
Expand All @@ -83,8 +84,6 @@ int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
8);

ROM_SSIEnable(spi_confs[bus].ssi_base);

return SPI_OK;
}

void spi_release(spi_t bus)
Expand Down
13 changes: 4 additions & 9 deletions cpu/lpc23xx/periph/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,17 @@ void spi_init_pins(spi_t bus)
*(&PINSEL0 + cfg->pinsel_clk) |= cfg->pinsel_msk_clk;
}

int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
{
(void) cs;
(void)cs; (void)mode;
assert((unsigned)bus < SPI_NUMOF);
assert(mode == SPI_MODE_0);

uint32_t pclksel;
uint32_t cpsr;

lpc23xx_spi_t *dev = get_dev(bus);

/* only support for mode 0 at the moment */
if (mode != SPI_MODE_0) {
return SPI_NOMODE;
}

/* lock bus */
mutex_lock(&lock[bus]);

Expand Down Expand Up @@ -141,8 +138,6 @@ int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
while (dev->SR & SSPSR_RNE) { /* while RNE (Receive FIFO Not Empty)...*/
dev->DR; /* read data*/
}

return SPI_OK;
}

void spi_release(spi_t bus)
Expand Down
15 changes: 6 additions & 9 deletions cpu/msp430fxyz/periph/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
* @}
*/

#include <assert.h>

#include "cpu.h"
#include "mutex.h"
#include "assert.h"
#include "periph/spi.h"

/**
Expand All @@ -35,7 +36,7 @@ static mutex_t spi_lock = MUTEX_INIT;

void spi_init(spi_t bus)
{
assert(bus <= SPI_NUMOF);
assert((unsigned)bus < SPI_NUMOF);

/* we need to differentiate between the legacy SPI device and USCI */
#ifndef SPI_USE_USCI
Expand Down Expand Up @@ -65,14 +66,12 @@ void spi_init_pins(spi_t bus)
gpio_periph_mode(SPI_PIN_CLK, true);
}

int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
{
(void)bus;
(void)cs;

if (clk == SPI_CLK_10MHZ) {
return SPI_NOCLK;
}
assert((unsigned)bus < SPI_NUMOF);
assert(clk != SPI_CLK_10MHZ);

/* lock the bus */
mutex_lock(&spi_lock);
Expand All @@ -99,8 +98,6 @@ int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
/* release from software reset */
SPI_BASE->CTL1 &= ~(USCI_SPI_CTL1_SWRST);
#endif

return SPI_OK;
}

void spi_release(spi_t bus)
Expand Down
20 changes: 8 additions & 12 deletions cpu/native/periph/spidev_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#ifdef MODULE_PERIPH_SPIDEV_LINUX

#include <assert.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
Expand Down Expand Up @@ -145,12 +146,10 @@ void spidev_linux_teardown(void)
}
}

int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
{
DEBUG("spi_acquire(%u, %u, 0x%02x, %d)\n", bus, cs, mode, clk);
if (bus >= SPI_NUMOF) {
return SPI_NODEV;
}
assert((unsigned)bus < SPI_NUMOF);

mutex_lock(&(device_state[bus].lock));

Expand All @@ -166,34 +165,31 @@ int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
unsigned csid = CS_TO_CSID(cs);
if (device_state[bus].fd[csid] < 0) {
DEBUG("spi_acquire: No fd for %u:%u\n", bus, csid);
mutex_unlock(&(device_state[bus].lock));
return SPI_NOCS;
assert(0);
}
fd = device_state[bus].fd[csid];
DEBUG("spi_acquire: Using %u:%u with HWCS (-> fd 0x%x)\n", bus, csid, fd);
}
else if (IS_GPIO_CS(cs) || cs == SPI_CS_UNDEF) {
fd = spidev_get_first_fd(&(device_state[bus]));
if (fd < 0) {
mutex_unlock(&(device_state[bus].lock));
return SPI_NOCS;
DEBUG("spi_acquire: Invalid CS parameter\n");
assert(0);
}
DEBUG("spi_acquire: Using SPI_CS_UNDEF (-> fd 0x%x)\n", fd);
}
else {
DEBUG("spi_acquire: Invalid CS parameter\n");
mutex_unlock(&(device_state[bus].lock));
return SPI_NOCS;
assert(0);
}

int res = spi_set_params(fd, use_hwcs, mode, clk);
if (res < 0) {
DEBUG("spi_acquire: set_params failed\n");
mutex_unlock(&(device_state[bus].lock));
assert(0);
}

DEBUG("spi_acquire: bus %u acquired\n", bus);
return SPI_OK;
}

void spi_init(spi_t bus)
Expand Down
Loading

0 comments on commit a1cbcc9

Please sign in to comment.