-
Notifications
You must be signed in to change notification settings - Fork 3k
[FL-3348] Software SPI #3861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
portasynthinca3
wants to merge
15
commits into
dev
Choose a base branch
from
portasynthinca3/3348-softspi
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[FL-3348] Software SPI #3861
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2503f1f
feat: softspi
portasynthinca3 829c80a
build: fix errors
portasynthinca3 8e9885b
Merge branch 'dev' into portasynthinca3/3348-softspi
portasynthinca3 698b49b
Merge branch 'dev' into portasynthinca3/3348-softspi
skotopes 1df33df
refactor: rename to softio
portasynthinca3 85a1d42
docs: fix file name
portasynthinca3 65bc290
Merge branch 'dev' into portasynthinca3/3348-softspi
skotopes 4073a1b
Merge branch 'dev' into portasynthinca3/3348-softspi
portasynthinca3 4118ca6
refactor: softspi
portasynthinca3 752b708
refactor: remove slave support
portasynthinca3 8350227
Merge branch 'dev' into portasynthinca3/3348-softspi
portasynthinca3 1421156
fix: stop timer after transfer
portasynthinca3 85be5db
Merge branch 'dev' into portasynthinca3/3348-softspi
portasynthinca3 8c60cc5
Merge branch 'dev' into portasynthinca3/3348-softspi
portasynthinca3 b0573ef
Merge branch 'dev' into portasynthinca3/3348-softspi
skotopes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
App( | ||
appid="spi_test", | ||
name="SPI Test", | ||
apptype=FlipperAppType.DEBUG, | ||
entry_point="spi_test_app", | ||
requires=["gui"], | ||
stack_size=1 * 1024, | ||
fap_category="Debug", | ||
fap_libs=["softio"], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
#include <furi_hal.h> | ||
#include <furi_hal_spi.h> | ||
#include <furi_hal_resources.h> | ||
#include <furi.h> | ||
|
||
#include <gui/gui.h> | ||
#include <gui/view_dispatcher.h> | ||
#include <gui/modules/submenu.h> | ||
|
||
#include <lib/softio/softio_spi.h> | ||
|
||
#define TAG "SpiTest" | ||
|
||
typedef struct { | ||
Gui* gui; | ||
ViewDispatcher* view_dispatcher; | ||
Submenu* submenu; | ||
} SpiTest; | ||
|
||
typedef enum { | ||
SpiTestViewSubmenu, | ||
} SpiTestView; | ||
|
||
typedef enum { | ||
SpiTestSubmenuHardwareTx, | ||
SpiTestSubmenuSoftwareTx, | ||
} SpiTestSubmenu; | ||
|
||
static void spi_test_submenu_callback(void* context, uint32_t index) { | ||
SpiTest* instance = (SpiTest*)context; | ||
UNUSED(instance); | ||
|
||
uint8_t tx_buffer[] = {0x55, 0xAA}; | ||
uint8_t rx_buffer[sizeof(tx_buffer)] = {0}; | ||
|
||
if(index == SpiTestSubmenuHardwareTx) { | ||
FuriHalSpiBusHandle* handle = &furi_hal_spi_bus_handle_external; | ||
furi_hal_spi_bus_handle_init(handle); | ||
furi_hal_spi_acquire(handle); | ||
furi_hal_spi_bus_trx(handle, tx_buffer, rx_buffer, sizeof(tx_buffer), FuriWaitForever); | ||
furi_hal_spi_release(handle); | ||
furi_hal_spi_bus_handle_deinit(handle); | ||
} else { | ||
// initialize | ||
SoftIoSpiBusConfig bus_cfg = { | ||
.miso = &gpio_ext_pa6, | ||
.mosi = &gpio_ext_pa7, | ||
.sck = &gpio_ext_pb3, | ||
.clk_polarity = 0, | ||
.clk_fq_khz = 200, | ||
.clk_phase = 0, | ||
}; | ||
SoftIoSpiBus* bus = softio_spi_alloc(&bus_cfg); | ||
softio_spi_init(bus); | ||
furi_hal_gpio_write(&gpio_ext_pa4, true); | ||
furi_hal_gpio_init(&gpio_ext_pa4, GpioModeOutputOpenDrain, GpioPullUp, GpioSpeedVeryHigh); | ||
|
||
// transmit | ||
furi_hal_gpio_write(&gpio_ext_pa4, false); | ||
softio_spi_trx(bus, tx_buffer, rx_buffer, sizeof(tx_buffer), FuriWaitForever); | ||
furi_hal_gpio_write(&gpio_ext_pa4, true); | ||
|
||
// deinitialize | ||
furi_hal_gpio_init(&gpio_ext_pa4, GpioModeAnalog, GpioPullNo, GpioSpeedLow); | ||
softio_spi_deinit(bus); | ||
softio_spi_free(bus); | ||
} | ||
|
||
FURI_LOG_I( | ||
TAG, | ||
"sent %02hhx %02hhX, received %02hhx %02hhX", | ||
tx_buffer[0], | ||
tx_buffer[1], | ||
rx_buffer[0], | ||
rx_buffer[1]); | ||
} | ||
|
||
static uint32_t spi_test_exit_callback(void* context) { | ||
UNUSED(context); | ||
return VIEW_NONE; | ||
} | ||
|
||
SpiTest* spi_test_alloc(void) { | ||
SpiTest* instance = malloc(sizeof(SpiTest)); | ||
|
||
View* view = NULL; | ||
|
||
instance->gui = furi_record_open(RECORD_GUI); | ||
instance->view_dispatcher = view_dispatcher_alloc(); | ||
view_dispatcher_attach_to_gui( | ||
instance->view_dispatcher, instance->gui, ViewDispatcherTypeFullscreen); | ||
|
||
// Menu | ||
instance->submenu = submenu_alloc(); | ||
view = submenu_get_view(instance->submenu); | ||
view_set_previous_callback(view, spi_test_exit_callback); | ||
view_dispatcher_add_view(instance->view_dispatcher, SpiTestViewSubmenu, view); | ||
submenu_add_item( | ||
instance->submenu, | ||
"Hardware TRX", | ||
SpiTestSubmenuHardwareTx, | ||
spi_test_submenu_callback, | ||
instance); | ||
submenu_add_item( | ||
instance->submenu, | ||
"Software TRX", | ||
SpiTestSubmenuSoftwareTx, | ||
spi_test_submenu_callback, | ||
instance); | ||
|
||
return instance; | ||
} | ||
|
||
void spi_test_free(SpiTest* instance) { | ||
view_dispatcher_remove_view(instance->view_dispatcher, SpiTestViewSubmenu); | ||
submenu_free(instance->submenu); | ||
|
||
view_dispatcher_free(instance->view_dispatcher); | ||
furi_record_close(RECORD_GUI); | ||
|
||
free(instance); | ||
} | ||
|
||
int32_t spi_test_run(SpiTest* instance) { | ||
view_dispatcher_switch_to_view(instance->view_dispatcher, SpiTestViewSubmenu); | ||
view_dispatcher_run(instance->view_dispatcher); | ||
return 0; | ||
} | ||
|
||
int32_t spi_test_app(void* p) { | ||
UNUSED(p); | ||
|
||
SpiTest* instance = spi_test_alloc(); | ||
|
||
int32_t ret = spi_test_run(instance); | ||
|
||
spi_test_free(instance); | ||
|
||
return ret; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ libs = env.BuildModules( | |
"ble_profile", | ||
"bit_lib", | ||
"datetime", | ||
"softio", | ||
], | ||
) | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Import("env") | ||
|
||
env.Append( | ||
LINT_SOURCES=[ | ||
Dir("."), | ||
], | ||
CPPPATH=[ | ||
"#/lib/softio", | ||
], | ||
SDK_HEADERS=[], | ||
) | ||
|
||
libenv = env.Clone(FW_LIB_NAME="softio") | ||
libenv.ApplyLibFlags() | ||
|
||
sources = libenv.GlobRecursive("*.c*") | ||
|
||
lib = libenv.StaticLibrary("${FW_LIB_NAME}", sources) | ||
libenv.Install("${LIB_DIST_DIR}", lib) | ||
Return("lib") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
#include "softio_spi.h" | ||
#include <stm32wbxx_ll_tim.h> | ||
#include <furi_hal_bus.h> | ||
#include <furi_hal_gpio.h> | ||
#include <furi_hal_resources.h> | ||
#include <furi_hal_interrupt.h> | ||
|
||
#define SOFTSPI_TIM TIM17 | ||
#define SOFTSPI_TIM_BUS FuriHalBusTIM17 | ||
#define SOFTSPI_TIM_IRQ FuriHalInterruptIdTim1TrgComTim17 | ||
#define SOFTSPI_TIM_FQ_KHZ 64000UL | ||
|
||
struct SoftIoSpiBus { | ||
SoftIoSpiBusConfig config; | ||
uint8_t is_currently_initd; | ||
}; | ||
|
||
typedef struct { | ||
SoftIoSpiBusConfig config; | ||
const uint8_t* tx_buffer; | ||
uint8_t* rx_buffer; | ||
size_t size; | ||
FuriSemaphore* done_semaphore; | ||
uint8_t bit : 3; | ||
uint8_t sck_level : 1; | ||
uint8_t done : 1; | ||
uint8_t | ||
out_level : 1; // <! which edge (1=rising, 0=falling) to transmit on; reception is done on the opposite edge | ||
} SoftspiTimerIsrContext; | ||
|
||
SoftIoSpiBus* softio_spi_alloc(SoftIoSpiBusConfig* config) { | ||
furi_check(config); | ||
furi_check(config->miso); | ||
furi_check(config->mosi); | ||
furi_check(config->sck); | ||
|
||
SoftIoSpiBus* bus = malloc(sizeof(SoftIoSpiBus)); | ||
bus->config = *config; | ||
|
||
return bus; | ||
} | ||
|
||
void softio_spi_free(SoftIoSpiBus* bus) { | ||
furi_check(bus); | ||
furi_check(!bus->is_currently_initd); | ||
free(bus); | ||
} | ||
|
||
void softio_spi_init(SoftIoSpiBus* bus) { | ||
furi_check(bus); | ||
furi_check(!bus->is_currently_initd); | ||
|
||
furi_hal_gpio_write(bus->config.mosi, false); | ||
furi_hal_gpio_write(bus->config.sck, bus->config.clk_polarity); | ||
furi_hal_gpio_init(bus->config.mosi, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh); | ||
furi_hal_gpio_init(bus->config.sck, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh); | ||
furi_hal_gpio_init(bus->config.miso, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh); | ||
|
||
bus->is_currently_initd = true; | ||
} | ||
|
||
void softio_spi_deinit(SoftIoSpiBus* bus) { | ||
furi_check(bus); | ||
furi_check(bus->is_currently_initd); | ||
|
||
furi_hal_gpio_init(bus->config.mosi, GpioModeAnalog, GpioPullNo, GpioSpeedLow); | ||
furi_hal_gpio_init(bus->config.sck, GpioModeAnalog, GpioPullNo, GpioSpeedLow); | ||
furi_hal_gpio_init(bus->config.miso, GpioModeAnalog, GpioPullNo, GpioSpeedLow); | ||
|
||
bus->is_currently_initd = false; | ||
} | ||
|
||
static void softio_spi_timer_isr(void* param) { | ||
furi_check(LL_TIM_IsActiveFlag_UPDATE(SOFTSPI_TIM)); | ||
LL_TIM_ClearFlag_UPDATE(SOFTSPI_TIM); | ||
|
||
SoftspiTimerIsrContext* context = param; | ||
|
||
if(context->done) { | ||
furi_hal_gpio_write(context->config.sck, context->config.clk_polarity); | ||
LL_TIM_DisableCounter(SOFTSPI_TIM); | ||
return; | ||
} else { | ||
furi_hal_gpio_write(context->config.sck, context->sck_level); | ||
} | ||
|
||
if(context->sck_level == context->out_level) { | ||
// TX edge | ||
if(context->tx_buffer) | ||
furi_hal_gpio_write(context->config.mosi, (*context->tx_buffer >> context->bit) & 1); | ||
} else { | ||
// RX edge | ||
if(context->rx_buffer) | ||
*context->rx_buffer |= furi_hal_gpio_read(context->config.miso) << context->bit; | ||
|
||
if(context->bit == 0) { | ||
// entire byte transmitted | ||
if(context->tx_buffer) context->tx_buffer++; | ||
if(context->rx_buffer) context->rx_buffer++; | ||
if(!--context->size) { | ||
furi_semaphore_release(context->done_semaphore); | ||
context->done = 1; | ||
} else { | ||
context->bit = 7; | ||
} | ||
} else { | ||
context->bit--; | ||
} | ||
} | ||
|
||
context->sck_level = !context->sck_level; | ||
} | ||
|
||
void softio_spi_trx( | ||
SoftIoSpiBus* bus, | ||
const uint8_t* tx_buffer, | ||
uint8_t* rx_buffer, | ||
size_t size, | ||
uint32_t timeout) { | ||
furi_check(bus); | ||
furi_check(bus->is_currently_initd); | ||
furi_assert(size); | ||
|
||
SoftspiTimerIsrContext context = { | ||
.config = bus->config, | ||
.tx_buffer = tx_buffer, | ||
.rx_buffer = rx_buffer, | ||
.size = size, | ||
.bit = 7, | ||
.done_semaphore = furi_semaphore_alloc(1, 0), | ||
.sck_level = bus->config.clk_polarity, | ||
.done = 0, | ||
.out_level = bus->config.clk_polarity ^ bus->config.clk_phase, | ||
}; | ||
|
||
furi_hal_bus_enable(SOFTSPI_TIM_BUS); | ||
furi_hal_interrupt_set_isr_ex( | ||
SOFTSPI_TIM_IRQ, FuriHalInterruptPriorityHighest, softio_spi_timer_isr, &context); | ||
LL_TIM_SetPrescaler(SOFTSPI_TIM, 0); | ||
LL_TIM_SetCounterMode(SOFTSPI_TIM, LL_TIM_COUNTERMODE_UP); | ||
LL_TIM_SetAutoReload( | ||
SOFTSPI_TIM, SOFTSPI_TIM_FQ_KHZ / bus->config.clk_fq_khz / 2); // f_ISR = 2 f_CLK | ||
LL_TIM_DisableARRPreload(SOFTSPI_TIM); | ||
LL_TIM_SetRepetitionCounter(SOFTSPI_TIM, 0); | ||
LL_TIM_SetClockDivision(SOFTSPI_TIM, LL_TIM_CLOCKDIVISION_DIV1); | ||
LL_TIM_SetClockSource(SOFTSPI_TIM, LL_TIM_CLOCKSOURCE_INTERNAL); | ||
LL_TIM_GenerateEvent_UPDATE(SOFTSPI_TIM); | ||
LL_TIM_EnableIT_UPDATE(SOFTSPI_TIM); | ||
LL_TIM_EnableCounter(SOFTSPI_TIM); | ||
|
||
furi_semaphore_acquire(context.done_semaphore, timeout); | ||
|
||
furi_hal_interrupt_set_isr(SOFTSPI_TIM_IRQ, NULL, NULL); | ||
furi_hal_bus_disable(SOFTSPI_TIM_BUS); | ||
furi_semaphore_free(context.done_semaphore); | ||
} | ||
|
||
void softio_spi_tx(SoftIoSpiBus* bus, const uint8_t* buffer, size_t size, uint32_t timeout) { | ||
furi_check(buffer); | ||
softio_spi_trx(bus, buffer, NULL, size, timeout); | ||
} | ||
|
||
void softio_spi_rx(SoftIoSpiBus* bus, uint8_t* buffer, size_t size, uint32_t timeout) { | ||
furi_check(buffer); | ||
softio_spi_trx(bus, NULL, buffer, size, timeout); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.