Skip to content

Commit

Permalink
Merge pull request #547 from kaluma-project/develop/spi_pin_issue
Browse files Browse the repository at this point in the history
SPI/I2C/UART pin code change
  • Loading branch information
niklauslee authored Jan 4, 2023
2 parents 92df102 + a263ac5 commit 40043cf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
28 changes: 18 additions & 10 deletions targets/rp2/src/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,28 @@
#include "pico/stdlib.h"
#include "system.h"

static struct __i2c_status_s { km_i2c_mode_t mode; } __i2c_status[I2C_NUM];
static struct __i2c_status_s {
km_i2c_mode_t mode;
} __i2c_status[I2C_NUM];

static bool __check_i2c_pins(uint8_t bus, km_i2c_pins_t pins) {
if ((pins.sda < 0) || (pins.sda > 27) || (pins.scl < 0) || (pins.scl > 27)) {
if ((pins.sda > 27) || (pins.scl > 27)) {
return false;
}
if (bus == 0) {
if (((pins.sda % 4) != 0) || (pins.sda > 21)) {
if ((pins.sda >= 0) && ((pins.sda % 4) != 0) || (pins.sda > 21)) {
return false;
}
if (((pins.scl % 4) != 1) || (pins.scl > 21)) {
if ((pins.scl >= 0) && ((pins.scl % 4) != 1) || (pins.scl > 21)) {
return false;
}
} else if (bus == 1) {
if (((pins.sda % 4) != 2) || ((pins.sda > 21) && (pins.sda < 26))) {
if ((pins.sda >= 0) && ((pins.sda % 4) != 2) ||
((pins.sda > 21) && (pins.sda < 26))) {
return false;
}
if (((pins.scl % 4) != 3) || ((pins.scl > 21) && (pins.scl < 26))) {
if ((pins.scl >= 0) && ((pins.scl % 4) != 3) ||
((pins.scl > 21) && (pins.scl < 26))) {
return false;
}
} else {
Expand Down Expand Up @@ -109,10 +113,14 @@ int km_i2c_setup_master(uint8_t bus, uint32_t speed, km_i2c_pins_t pins) {
if (speed > I2C_MAX_CLOCK) {
speed = I2C_MAX_CLOCK;
}
gpio_set_function(pins.sda, GPIO_FUNC_I2C);
gpio_set_function(pins.scl, GPIO_FUNC_I2C);
gpio_pull_up(pins.sda);
gpio_pull_up(pins.scl);
if (pins.sda >= 0) {
gpio_set_function(pins.sda, GPIO_FUNC_I2C);
gpio_pull_up(pins.sda);
}
if (pins.scl >= 0) {
gpio_set_function(pins.scl, GPIO_FUNC_I2C);
gpio_pull_up(pins.scl);
}
i2c_init(i2c, speed);
return 0;
}
Expand Down
30 changes: 18 additions & 12 deletions targets/rp2/src/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@ struct __spi_status_s {
} __spi_status[SPI_NUM];

static bool __check_spi_pins(uint8_t bus, km_spi_pins_t pins) {
if ((pins.sck < 0) || (pins.miso < 0) || (pins.mosi < 0)) {
return false;
}
if (bus == 0) {
if ((pins.miso != 0) && (pins.miso != 4) && (pins.miso != 16)) {
if ((pins.miso >= 0) && (pins.miso != 0) && (pins.miso != 4) &&
(pins.miso != 16)) {
return false;
}
if ((pins.mosi != 3) && (pins.mosi != 7) && (pins.mosi != 19)) {
if ((pins.mosi >= 0) && (pins.mosi != 3) && (pins.mosi != 7) &&
(pins.mosi != 19)) {
return false;
}
if ((pins.sck != 2) && (pins.sck != 6) && (pins.sck != 18)) {
if ((pins.sck >= 0) && (pins.sck != 2) && (pins.sck != 6) &&
(pins.sck != 18)) {
return false;
}
} else if (bus == 1) {
if ((pins.miso != 8) && (pins.miso != 12)) {
if ((pins.miso >= 0) && (pins.miso != 8) && (pins.miso != 12)) {
return false;
}
if ((pins.mosi != 11) && (pins.mosi != 15)) {
if ((pins.mosi >= 0) && (pins.mosi != 11) && (pins.mosi != 15)) {
return false;
}
if ((pins.sck != 10) && (pins.sck != 14)) {
if ((pins.sck >= 0) && (pins.sck != 10) && (pins.sck != 14)) {
return false;
}
} else {
Expand Down Expand Up @@ -147,9 +147,15 @@ int km_spi_setup(uint8_t bus, km_spi_mode_t mode, uint32_t baudrate,
}
spi_init(spi, baudrate);
spi_set_format(spi, 8, pol, pha, order);
gpio_set_function(pins.miso, GPIO_FUNC_SPI);
gpio_set_function(pins.mosi, GPIO_FUNC_SPI);
gpio_set_function(pins.sck, GPIO_FUNC_SPI);
if (pins.miso >= 0) {
gpio_set_function(pins.miso, GPIO_FUNC_SPI);
}
if (pins.mosi >= 0) {
gpio_set_function(pins.mosi, GPIO_FUNC_SPI);
}
if (pins.sck >= 0) {
gpio_set_function(pins.sck, GPIO_FUNC_SPI);
}
if (miso_pullup) {
gpio_pull_up(pins.miso);
}
Expand Down
7 changes: 3 additions & 4 deletions targets/rp2/src/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@

static ringbuffer_t __uart_rx_ringbuffer[UART_NUM];
static uint8_t *__read_buffer[UART_NUM];
static struct __uart_status_s { bool enabled; } __uart_status[UART_NUM];
static struct __uart_status_s {
bool enabled;
} __uart_status[UART_NUM];

static uart_inst_t *__get_uart_no(uint8_t bus) {
if (bus == 0) {
Expand Down Expand Up @@ -59,9 +61,6 @@ void __uart_irq_handler_0(void) { __uart_fill_ringbuffer(uart0, 0); }
void __uart_irq_handler_1(void) { __uart_fill_ringbuffer(uart1, 1); }

static bool __check_uart_pins(uint8_t port, km_uart_pins_t pins) {
if ((pins.tx < 0) && (pins.rx < 0)) {
return false;
}
if (port == 0) {
if ((pins.tx >= 0) && (pins.tx != 0) && (pins.tx != 12) &&
(pins.tx != 16)) {
Expand Down

0 comments on commit 40043cf

Please sign in to comment.