From a39d1bf8f98d918ea61983224574b6155ac02b85 Mon Sep 17 00:00:00 2001 From: Kiel Oleson Date: Wed, 17 Jan 2024 21:28:20 -0800 Subject: [PATCH 1/2] start RP2040 SPI HAL cmd/resp implementation --- src/hal/si4707_hal.h | 3 ++- src/hal/si4707_hal_rp2040_spi.c | 39 +++++++++++++++++++++++++++++++-- src/si4707/si4707.c | 3 --- src/si4707/si4707_const.h | 4 +++- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/hal/si4707_hal.h b/src/hal/si4707_hal.h index fe16d9c..59dd89e 100644 --- a/src/hal/si4707_hal.h +++ b/src/hal/si4707_hal.h @@ -15,7 +15,8 @@ struct Si4707_HAL_FPs { void (*reset)(); void (*power_up)(); void (*send_command)(const uint8_t cmd, const struct Si4707_Command_Args* args); - + void (*send_command_and_read_response)(const bool wait_for_cts_command, const uint8_t cmd, const struct Si4707_Command_Args* args, + const bool wait_for_cts_response, const uint8_t resp_length, uint8_t* resp_buf); }; #endif // SI4707_HAL_H \ No newline at end of file diff --git a/src/hal/si4707_hal_rp2040_spi.c b/src/hal/si4707_hal_rp2040_spi.c index d20c270..7f6fb93 100644 --- a/src/hal/si4707_hal_rp2040_spi.c +++ b/src/hal/si4707_hal_rp2040_spi.c @@ -6,7 +6,9 @@ #include "pico/stdlib.h" #include "hardware/spi.h" +#include "si4707_const.h" #include "si4707_hal.h" +#include "si4707.h" spi_inst_t* g_hal_rp2040_spi = NULL; uint g_hal_rp2040_mosi_pin = 0; @@ -35,7 +37,8 @@ void hal_rp2040_set_si4707_pinmap(spi_inst_t *spi, uint mosi_pin, uint miso_pin, g_hal_rp2040_pinmap_set = true; } -void hal_rp2040_assert_pinmap_set() { +void hal_rp2040_assert_pinmap_set() +{ if (!g_hal_rp2040_pinmap_set) { printf("RP2040 HAL: pinmap not set for Si4707 but you're trying to use it\n"); abort(); @@ -117,6 +120,38 @@ void hal_rp2040_si4707_reset() sleep_ms(2); } +void si4707_hal_rpi2040_spi_send_command_and_read_response(const bool wait_for_cts_command, const uint8_t cmd, const struct Si4707_Command_Args* args, + const bool wait_for_cts_response, const uint8_t resp_length, uint8_t* resp_buf) +{ + // note to self: unclear whether i should use the + // HAL version or the driver version. kmo 17 jan 2024 + // si4707_txn_start(); + hal_rp2040_si4707_txn_start(); + + if (wait_for_cts_command) { + si4707_await_cts(CTS_WAIT); + } + + uint8_t cmd_buf[9] = { 0x00 }; + cmd_buf[0] = cmd; + cmd_buf[1] = args->ARG1; cmd_buf[2] = args->ARG2; cmd_buf[3] = args->ARG3; + cmd_buf[4] = args->ARG4; cmd_buf[5] = args->ARG5; cmd_buf[6] = args->ARG6; + cmd_buf[7] = args->ARG7; + + // TODO: i cannot remember why this is 9 instead of 8 length + // kmo 17 jan 2024 20h50 + spi_write_blocking(g_hal_rp2040_spi, cmd_buf, 9); + + if (wait_for_cts_response) { + si4707_await_cts(CTS_WAIT); + } + + spi_read_blocking(g_hal_rp2040_spi, 0, resp_buf, resp_length); + + hal_rp2040_si4707_txn_end(); + +} + struct Si4707_HAL_FPs* hal_rp2040_FPs() { struct Si4707_HAL_FPs* function_pointers = (struct Si4707_HAL_FPs*)malloc(sizeof(struct Si4707_HAL_FPs)); @@ -127,4 +162,4 @@ struct Si4707_HAL_FPs* hal_rp2040_FPs() function_pointers->reset = hal_rp2040_si4707_reset; return function_pointers; -} \ No newline at end of file +} diff --git a/src/si4707/si4707.c b/src/si4707/si4707.c index 71b7c91..b83cc34 100644 --- a/src/si4707/si4707.c +++ b/src/si4707/si4707.c @@ -17,9 +17,6 @@ #define MIN(a, b) ((b)>(a)?(a):(b)) #endif -#define CTS_WAIT 250 - - struct Si4707_HAL_FPs* current_hal = NULL; void si4707_set_hal(struct Si4707_HAL_FPs* hal) { current_hal = hal; diff --git a/src/si4707/si4707_const.h b/src/si4707/si4707_const.h index 245647f..c875691 100644 --- a/src/si4707/si4707_const.h +++ b/src/si4707/si4707_const.h @@ -89,4 +89,6 @@ // see also HDRRDY (header ready) #define SI4707_SAME_STATE_HEADER_COMPLETE 3 -// TODO: this list of available properties is probably not complete. check AN332. kmo 9 oct 2023 \ No newline at end of file +// TODO: this list of available properties is probably not complete. check AN332. kmo 9 oct 2023 + +#define CTS_WAIT 250 From 65a2ac27baf7532f0a98869812f8669062e67965 Mon Sep 17 00:00:00 2001 From: Kiel Oleson Date: Wed, 17 Jan 2024 21:41:18 -0800 Subject: [PATCH 2/2] tweaks --- src/hal/si4707_hal_rp2040_spi.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/hal/si4707_hal_rp2040_spi.c b/src/hal/si4707_hal_rp2040_spi.c index 7f6fb93..bea4abd 100644 --- a/src/hal/si4707_hal_rp2040_spi.c +++ b/src/hal/si4707_hal_rp2040_spi.c @@ -45,7 +45,8 @@ void hal_rp2040_assert_pinmap_set() } } -void hal_rp2040_setup_si4707_spi() { +void hal_rp2040_setup_si4707_spi() +{ hal_rp2040_assert_pinmap_set(); // SPI initialization. 400kHz. @@ -60,7 +61,8 @@ void hal_rp2040_setup_si4707_spi() { gpio_put(g_hal_rp2040_cs_pin, 1); } -void hal_rp2040_prepare_interface() { +void hal_rp2040_prepare_interface() +{ hal_rp2040_setup_si4707_spi(); }