Skip to content

Commit

Permalink
spi: centralize pin init
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Jul 22, 2024
1 parent bb0f7e6 commit a9496e6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 96 deletions.
49 changes: 1 addition & 48 deletions src/driver/at32/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,6 @@ static uint32_t spi_find_divder(uint32_t clk_hz) {
return spi_divider_to_ll(divider);
}

static void spi_init_pins(spi_ports_t port) {
const target_spi_port_t *dev = &target.spi_ports[port];

gpio_config_t gpio_init;

gpio_init.mode = GPIO_ALTERNATE;
gpio_init.drive = GPIO_DRIVE_HIGH;
gpio_init.output = GPIO_PUSHPULL;
gpio_init.pull = GPIO_UP_PULL;
gpio_pin_init_tag(dev->sck, gpio_init, SPI_TAG(port, RES_SPI_SCK));

gpio_init.mode = GPIO_ALTERNATE;
gpio_init.drive = GPIO_DRIVE_HIGH;
gpio_init.output = GPIO_PUSHPULL;
gpio_init.pull = GPIO_NO_PULL;
gpio_pin_init_tag(dev->miso, gpio_init, SPI_TAG(port, RES_SPI_MISO));

gpio_init.mode = GPIO_ALTERNATE;
gpio_init.drive = GPIO_DRIVE_HIGH;
gpio_init.output = GPIO_PUSHPULL;
gpio_init.pull = GPIO_NO_PULL;
gpio_pin_init_tag(dev->mosi, gpio_init, SPI_TAG(port, RES_SPI_MOSI));
}

static void spi_dma_init_rx(spi_ports_t port) {
const dma_stream_def_t *dma = &dma_stream_defs[PORT.dma_rx];

Expand Down Expand Up @@ -231,13 +207,7 @@ void spi_dma_transfer_begin(spi_ports_t port, uint8_t *buffer, uint32_t length)
spi_enable(PORT.channel, TRUE);
}

static void spi_device_init(spi_ports_t port) {
if (spi_dev[port].is_init) {
return;
}

spi_init_pins(port);

void spi_device_init(spi_ports_t port) {
const spi_port_def_t *def = &spi_port_defs[port];
rcc_enable(def->rcc);
dma_enable_rcc(def->dma_rx);
Expand All @@ -256,7 +226,6 @@ static void spi_device_init(spi_ports_t port) {
default_init.cs_mode_selection = SPI_CS_SOFTWARE_MODE;
spi_init(def->channel, &default_init);

spi_dev[port].is_init = true;
spi_dev[port].dma_done = true;
spi_dev[port].mode = SPI_MODE_TRAILING_EDGE;
spi_dev[port].hz = 0;
Expand All @@ -268,22 +237,6 @@ static void spi_device_init(spi_ports_t port) {
interrupt_enable(dma_rx->irq, DMA_PRIORITY);
}

void spi_bus_device_init(const spi_bus_device_t *bus) {
if (!target_spi_port_valid(&target.spi_ports[bus->port])) {
return;
}

gpio_config_t gpio_init;
gpio_init.mode = GPIO_OUTPUT;
gpio_init.drive = GPIO_DRIVE_HIGH;
gpio_init.output = GPIO_PUSHPULL;
gpio_init.pull = GPIO_UP_PULL;
gpio_pin_init(bus->nss, gpio_init);
gpio_pin_set(bus->nss);

spi_device_init(bus->port);
}

void spi_seg_submit_wait_ex(spi_bus_device_t *bus, const spi_txn_segment_t *segs, const uint32_t count) {
spi_txn_wait(bus);

Expand Down
47 changes: 47 additions & 0 deletions src/driver/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ FAST_RAM spi_device_t spi_dev[SPI_PORT_MAX] = {
FAST_RAM spi_txn_t txn_pool[SPI_TXN_MAX];
DMA_RAM uint8_t txn_buffers[SPI_TXN_MAX][DMA_ALIGN(512)];

extern void spi_device_init(spi_ports_t port);
extern void spi_reconfigure(spi_bus_device_t *bus);
extern void spi_dma_transfer_begin(spi_ports_t port, uint8_t *buffer, uint32_t length);

Expand Down Expand Up @@ -187,4 +188,50 @@ void spi_txn_finish(spi_ports_t port) {

spi_dev[port].dma_done = true;
spi_txn_continue_port(port);
}

static void spi_init_pins(spi_ports_t port) {
const target_spi_port_t *dev = &target.spi_ports[port];

gpio_config_t gpio_init;

gpio_init.mode = GPIO_ALTERNATE;
gpio_init.drive = GPIO_DRIVE_HIGH;
gpio_init.output = GPIO_PUSHPULL;
gpio_init.pull = GPIO_UP_PULL;
gpio_pin_init_tag(dev->sck, gpio_init, SPI_TAG(port, RES_SPI_SCK));

gpio_init.mode = GPIO_ALTERNATE;
gpio_init.drive = GPIO_DRIVE_HIGH;
gpio_init.output = GPIO_PUSHPULL;
gpio_init.pull = GPIO_NO_PULL;
gpio_pin_init_tag(dev->miso, gpio_init, SPI_TAG(port, RES_SPI_MISO));

gpio_init.mode = GPIO_ALTERNATE;
gpio_init.drive = GPIO_DRIVE_HIGH;
gpio_init.output = GPIO_PUSHPULL;
gpio_init.pull = GPIO_NO_PULL;
gpio_pin_init_tag(dev->mosi, gpio_init, SPI_TAG(port, RES_SPI_MOSI));
}

void spi_bus_device_init(const spi_bus_device_t *bus) {
if (!target_spi_port_valid(&target.spi_ports[bus->port])) {
return;
}

gpio_config_t gpio_init;
gpio_init.mode = GPIO_OUTPUT;
gpio_init.drive = GPIO_DRIVE_HIGH;
gpio_init.output = GPIO_PUSHPULL;
gpio_init.pull = GPIO_UP_PULL;
gpio_pin_init(bus->nss, gpio_init);
gpio_pin_set(bus->nss);

if (spi_dev[bus->port].is_init) {
return;
}

spi_init_pins(bus->port);
spi_device_init(bus->port);
spi_dev[bus->port].is_init = true;
}
49 changes: 1 addition & 48 deletions src/driver/stm32/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,6 @@ static uint32_t spi_find_divder(uint32_t clk_hz) {
return spi_divider_to_ll(divider);
}

static void spi_init_pins(spi_ports_t port) {
const target_spi_port_t *dev = &target.spi_ports[port];

gpio_config_t gpio_init;

gpio_init.mode = GPIO_ALTERNATE;
gpio_init.drive = GPIO_DRIVE_HIGH;
gpio_init.output = GPIO_PUSHPULL;
gpio_init.pull = GPIO_UP_PULL;
gpio_pin_init_tag(dev->sck, gpio_init, SPI_TAG(port, RES_SPI_SCK));

gpio_init.mode = GPIO_ALTERNATE;
gpio_init.drive = GPIO_DRIVE_HIGH;
gpio_init.output = GPIO_PUSHPULL;
gpio_init.pull = GPIO_NO_PULL;
gpio_pin_init_tag(dev->miso, gpio_init, SPI_TAG(port, RES_SPI_MISO));

gpio_init.mode = GPIO_ALTERNATE;
gpio_init.drive = GPIO_DRIVE_HIGH;
gpio_init.output = GPIO_PUSHPULL;
gpio_init.pull = GPIO_NO_PULL;
gpio_pin_init_tag(dev->mosi, gpio_init, SPI_TAG(port, RES_SPI_MOSI));
}

static void spi_dma_init_rx(spi_ports_t port) {
const dma_stream_def_t *dma = &dma_stream_defs[PORT.dma_rx];

Expand Down Expand Up @@ -244,13 +220,7 @@ void spi_dma_transfer_begin(spi_ports_t port, uint8_t *buffer, uint32_t length)
#endif
}

static void spi_device_init(spi_ports_t port) {
if (spi_dev[port].is_init) {
return;
}

spi_init_pins(port);

void spi_device_init(spi_ports_t port) {
const spi_port_def_t *def = &spi_port_defs[port];
rcc_enable(def->rcc);
dma_enable_rcc(def->dma_rx);
Expand Down Expand Up @@ -281,7 +251,6 @@ static void spi_device_init(spi_ports_t port) {
LL_SPI_DisableNSSPulseMgt(def->channel);
#endif

spi_dev[port].is_init = true;
spi_dev[port].dma_done = true;
spi_dev[port].mode = SPI_MODE_TRAILING_EDGE;
spi_dev[port].hz = 0;
Expand All @@ -296,22 +265,6 @@ static void spi_device_init(spi_ports_t port) {
LL_DMA_EnableIT_TE(dma_rx->port, dma_rx->stream_index);
}

void spi_bus_device_init(const spi_bus_device_t *bus) {
if (!target_spi_port_valid(&target.spi_ports[bus->port])) {
return;
}

gpio_config_t gpio_init;
gpio_init.mode = GPIO_OUTPUT;
gpio_init.drive = GPIO_DRIVE_HIGH;
gpio_init.output = GPIO_PUSHPULL;
gpio_init.pull = GPIO_UP_PULL;
gpio_pin_init(bus->nss, gpio_init);
gpio_pin_set(bus->nss);

spi_device_init(bus->port);
}

void spi_seg_submit_wait_ex(spi_bus_device_t *bus, const spi_txn_segment_t *segs, const uint32_t count) {
spi_txn_wait(bus);

Expand Down

0 comments on commit a9496e6

Please sign in to comment.