Skip to content

Commit

Permalink
Configurable SPI & UART Channels
Browse files Browse the repository at this point in the history
  • Loading branch information
RogueMaster committed Jul 28, 2023
1 parent 71dc8ff commit fc4c6bd
Show file tree
Hide file tree
Showing 36 changed files with 458 additions and 103 deletions.
5 changes: 3 additions & 2 deletions ReadMe.md
Expand Up @@ -31,9 +31,10 @@ This software is for experimental purposes only and is not meant for any illegal

## Latest Updates - [PATREON: Latest Release RM0727-0355-0.87.1-1670f3c](https://www.patreon.com/RogueMaster?filters[tag]=Latest%20Release)

- Last Synced/Checked Unleashed, changes in [changelog](https://github.com/RogueMaster/flipperzero-firmware-wPlugins/blob/420/CHANGELOG.md) and in [commits](https://github.com/DarkFlippers/unleashed-firmware/commits/dev): `2023-07-28 01:00 EST`
- Last Synced/Checked OFW, changes in [commits](https://github.com/flipperdevices/flipperzero-firmware/commits/dev): `2023-07-28 01:00 EST`
- Last Synced/Checked Unleashed, changes in [changelog](https://github.com/RogueMaster/flipperzero-firmware-wPlugins/blob/420/CHANGELOG.md) and in [commits](https://github.com/DarkFlippers/unleashed-firmware/commits/dev): `2023-07-28 02:20 EST`
- Last Synced/Checked OFW, changes in [commits](https://github.com/flipperdevices/flipperzero-firmware/commits/dev): `2023-07-28 02:20 EST`
- [Fixed Memory leak/Pointer issue with CFW Settings app (By ESurge)](https://github.com/RogueMaster/flipperzero-firmware-wPlugins/pull/724)
- [Configurable SPI & UART Channels (By Sil333033)]

<a name="release">

Expand Down
35 changes: 28 additions & 7 deletions applications/drivers/subghz/cc1101_ext/cc1101_ext.c
Expand Up @@ -18,7 +18,7 @@
#define TAG "SubGhz_Device_CC1101_Ext"

#define SUBGHZ_DEVICE_CC1101_EXT_TX_GPIO &gpio_ext_pb2
#define SUBGHZ_DEVICE_CC1101_EXT_DANGEROUS_RANGE false
#define SUBGHZ_DEVICE_CC1101_EXT_EXTENDED_RANGE false

/* DMA Channels definition */
#define SUBGHZ_DEVICE_CC1101_EXT_DMA DMA2
Expand Down Expand Up @@ -193,11 +193,25 @@ bool subghz_device_cc1101_ext_alloc() {
subghz_device_cc1101_ext->state = SubGhzDeviceCC1101ExtStateInit;
subghz_device_cc1101_ext->regulation = SubGhzDeviceCC1101ExtRegulationTxRx;
subghz_device_cc1101_ext->async_mirror_pin = NULL;
subghz_device_cc1101_ext->spi_bus_handle = &furi_hal_spi_bus_handle_external;

subghz_device_cc1101_ext->g0_pin = SUBGHZ_DEVICE_CC1101_EXT_TX_GPIO;

subghz_device_cc1101_ext->async_rx.capture_delta_duration = 0;

subghz_device_cc1101_ext->spi_bus_handle =
(CFW_SETTINGS()->spi_cc1101_handle == SpiDefault ?
&furi_hal_spi_bus_handle_external :
&furi_hal_spi_bus_handle_external_extra);

// this is needed if multiple SPI devices are connected to the same bus but with different CS pins
if(CFW_SETTINGS()->spi_cc1101_handle == SpiDefault) {
furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeOutputPushPull);
furi_hal_gpio_write(&gpio_ext_pc3, true);
} else if(CFW_SETTINGS()->spi_cc1101_handle == SpiExtra) {
furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeOutputPushPull);
furi_hal_gpio_write(&gpio_ext_pa4, true);
}

furi_hal_spi_bus_handle_init(subghz_device_cc1101_ext->spi_bus_handle);
return subghz_device_cc1101_ext_check_init();
}
Expand All @@ -207,6 +221,13 @@ void subghz_device_cc1101_ext_free() {
furi_hal_spi_bus_handle_deinit(subghz_device_cc1101_ext->spi_bus_handle);
free(subghz_device_cc1101_ext);
subghz_device_cc1101_ext = NULL;

// resetting the CS pins to floating
if(CFW_SETTINGS()->spi_nrf24_handle == SpiDefault) {
furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeAnalog);
} else if(CFW_SETTINGS()->spi_nrf24_handle == SpiExtra) {
furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeAnalog);
}
}

void subghz_device_cc1101_ext_set_async_mirror_pin(const GpioPin* pin) {
Expand Down Expand Up @@ -432,16 +453,16 @@ bool subghz_device_cc1101_ext_is_frequency_valid(uint32_t value) {
}

bool subghz_device_cc1101_ext_is_tx_allowed(uint32_t value) {
if(!(SUBGHZ_DEVICE_CC1101_EXT_DANGEROUS_RANGE) &&
if(!(SUBGHZ_DEVICE_CC1101_EXT_EXTENDED_RANGE) &&
!(value >= 299999755 && value <= 350000335) && // was increased from 348 to 350
!(value >= 386999938 && value <= 467750000) && // was increased from 464 to 467.75
!(value >= 778999847 && value <= 928000000)) {
FURI_LOG_I(TAG, "Frequency blocked - outside default range");
return false;
} else if(
(SUBGHZ_DEVICE_CC1101_EXT_DANGEROUS_RANGE) &&
(SUBGHZ_DEVICE_CC1101_EXT_EXTENDED_RANGE) &&
!subghz_device_cc1101_ext_is_frequency_valid(value)) {
FURI_LOG_I(TAG, "Frequency blocked - outside dangerous range");
FURI_LOG_I(TAG, "Frequency blocked - outside extended range");
return false;
}

Expand Down Expand Up @@ -529,7 +550,7 @@ void subghz_device_cc1101_ext_start_async_rx(
furi_hal_bus_enable(FuriHalBusTIM17);

// Configure TIM
//Set the timer resolution to 2 µs
//Set the timer resolution to 2 �s
LL_TIM_SetPrescaler(TIM17, (64 << 1) - 1);
LL_TIM_SetCounterMode(TIM17, LL_TIM_COUNTERMODE_UP);
LL_TIM_SetAutoReload(TIM17, 0xFFFF);
Expand Down Expand Up @@ -710,7 +731,7 @@ bool subghz_device_cc1101_ext_start_async_tx(SubGhzDeviceCC1101ExtCallback callb
furi_hal_bus_enable(FuriHalBusTIM17);

// Configure TIM
// Set the timer resolution to 2 µs
// Set the timer resolution to 2 �s
LL_TIM_SetPrescaler(TIM17, (64 << 1) - 1);
LL_TIM_SetCounterMode(TIM17, LL_TIM_COUNTERMODE_UP);
LL_TIM_SetAutoReload(TIM17, 0xFFFF);
Expand Down
1 change: 1 addition & 0 deletions applications/drivers/subghz/cc1101_ext/cc1101_ext.h
Expand Up @@ -10,6 +10,7 @@
#include <stddef.h>
#include <toolbox/level_duration.h>
#include <furi_hal_gpio.h>
#include <cfw.h>

#ifdef __cplusplus
extern "C" {
Expand Down
Expand Up @@ -202,7 +202,7 @@ static bool camera_suite_view_camera_input(InputEvent* event, void* context) {
break;
}
// Send `data` to the ESP32-CAM
furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
furi_hal_uart_tx(UART_CH, data, 1);
}
return true;
}
Expand All @@ -224,7 +224,7 @@ static void camera_suite_view_camera_enter(void* context) {
uint8_t data[1];
data[0] = 'S'; // Uppercase `S` to start the camera
// Send `data` to the ESP32-CAM
furi_hal_uart_tx(FuriHalUartIdUSART1, data, 1);
furi_hal_uart_tx(UART_CH, data, 1);

with_view_model(
instance->view,
Expand Down Expand Up @@ -352,9 +352,13 @@ CameraSuiteViewCamera* camera_suite_view_camera_alloc() {
furi_thread_start(instance->worker_thread);

// Enable uart listener
furi_hal_console_disable();
furi_hal_uart_set_br(FuriHalUartIdUSART1, 230400);
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, camera_on_irq_cb, instance);
if(UART_CH == FuriHalUartIdUSART1) {
furi_hal_console_disable();
} else if(UART_CH == FuriHalUartIdLPUART1) {
furi_hal_uart_init(UART_CH, 230400);
}
furi_hal_uart_set_br(UART_CH, 230400);
furi_hal_uart_set_irq_cb(UART_CH, camera_on_irq_cb, instance);

return instance;
}
Expand All @@ -366,6 +370,14 @@ void camera_suite_view_camera_free(CameraSuiteViewCamera* instance) {
instance->view, UartDumpModel * model, { UNUSED(model); }, true);
view_free(instance->view);
free(instance);

furi_hal_uart_set_irq_cb(UART_CH, NULL, NULL);

if(UART_CH == FuriHalUartIdLPUART1) {
furi_hal_uart_deinit(UART_CH);
} else {
furi_hal_console_enable();
}
}

View* camera_suite_view_camera_get_view(CameraSuiteViewCamera* instance) {
Expand Down
Expand Up @@ -14,6 +14,11 @@
#include <storage/filesystem_api_defines.h>
#include <storage/storage.h>

#include <cfw.h>

#define UART_CH \
(CFW_SETTINGS()->uart_esp_channel == UARTDefault ? FuriHalUartIdUSART1 : FuriHalUartIdLPUART1)

#pragma once

#define FRAME_WIDTH 128
Expand Down
4 changes: 3 additions & 1 deletion applications/external/esp_flasher/esp_flasher_uart.c
@@ -1,7 +1,9 @@
#include "esp_flasher_app_i.h"
#include "esp_flasher_uart.h"
#include <cfw.h>

#define UART_CH (FuriHalUartIdUSART1)
#define UART_CH \
(CFW_SETTINGS()->uart_esp_channel == UARTDefault ? FuriHalUartIdUSART1 : FuriHalUartIdLPUART1)
#define BAUDRATE (115200)

struct EspFlasherUart {
Expand Down
6 changes: 5 additions & 1 deletion applications/external/evil_portal/evil_portal_app_i.h
Expand Up @@ -12,10 +12,14 @@
#include <gui/scene_manager.h>
#include <gui/view_dispatcher.h>

#include <dialogs/dialogs.h>
#include <cfw.h>

#define NUM_MENU_ITEMS (5)

#define EVIL_PORTAL_TEXT_BOX_STORE_SIZE (4096)
#define UART_CH (FuriHalUartIdUSART1)
#define UART_CH \
(CFW_SETTINGS()->uart_esp_channel == UARTDefault ? FuriHalUartIdUSART1 : FuriHalUartIdLPUART1)

#define SET_HTML_CMD "sethtml"
#define SET_AP_CMD "setap"
Expand Down
15 changes: 13 additions & 2 deletions applications/external/evil_portal/evil_portal_uart.c
Expand Up @@ -121,10 +121,16 @@ Evil_PortalUart* evil_portal_uart_init(Evil_PortalApp* app) {

furi_thread_start(uart->rx_thread);

furi_hal_console_disable();
if(UART_CH == FuriHalUartIdUSART1) {
furi_hal_console_disable();
} else if(UART_CH == FuriHalUartIdLPUART1) {
furi_hal_uart_init(UART_CH, app->BAUDRATE);
}

if(app->BAUDRATE == 0) {
app->BAUDRATE = 115200;
}

furi_hal_uart_set_br(UART_CH, app->BAUDRATE);
furi_hal_uart_set_irq_cb(UART_CH, evil_portal_uart_on_irq_cb, uart);

Expand All @@ -139,7 +145,12 @@ void evil_portal_uart_free(Evil_PortalUart* uart) {
furi_thread_free(uart->rx_thread);

furi_hal_uart_set_irq_cb(UART_CH, NULL, NULL);
furi_hal_console_enable();

if(UART_CH == FuriHalUartIdLPUART1) {
furi_hal_uart_deinit(UART_CH);
} else {
furi_hal_console_enable();
}

free(uart);
}
19 changes: 14 additions & 5 deletions applications/external/gps_nmea_uart/gps_uart.c
Expand Up @@ -20,15 +20,24 @@ static void gps_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
}

static void gps_uart_serial_init(GpsUart* gps_uart) {
furi_hal_console_disable();
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, gps_uart_on_irq_cb, gps_uart);
furi_hal_uart_set_br(FuriHalUartIdUSART1, gps_uart->baudrate);
if(UART_CH == FuriHalUartIdUSART1) {
furi_hal_console_disable();
} else if(UART_CH == FuriHalUartIdLPUART1) {
furi_hal_uart_init(UART_CH, gps_uart->baudrate);
}

furi_hal_uart_set_irq_cb(UART_CH, gps_uart_on_irq_cb, gps_uart);
furi_hal_uart_set_br(UART_CH, gps_uart->baudrate);
}

static void gps_uart_serial_deinit(GpsUart* gps_uart) {
UNUSED(gps_uart);
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);
furi_hal_console_enable();
furi_hal_uart_set_irq_cb(UART_CH, NULL, NULL);
if(UART_CH == FuriHalUartIdLPUART1) {
furi_hal_uart_deinit(UART_CH);
} else {
furi_hal_console_enable();
}
}

static void gps_uart_parse_nmea(GpsUart* gps_uart, char* line) {
Expand Down
4 changes: 4 additions & 0 deletions applications/external/gps_nmea_uart/gps_uart.h
Expand Up @@ -2,6 +2,10 @@

#include <furi_hal.h>
#include <notification/notification_messages.h>
#include <cfw.h>

#define UART_CH \
(CFW_SETTINGS()->uart_nmea_channel == UARTDefault ? FuriHalUartIdUSART1 : FuriHalUartIdLPUART1)

#define RX_BUF_SIZE 1024

Expand Down
16 changes: 16 additions & 0 deletions applications/external/mousejacker/lib/nrf24/nrf24.c
Expand Up @@ -6,6 +6,15 @@
#include <string.h>

void nrf24_init() {
// this is needed if multiple SPI devices are connected to the same bus but with different CS pins
if(CFW_SETTINGS()->spi_nrf24_handle == SpiDefault) {
furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeOutputPushPull);
furi_hal_gpio_write(&gpio_ext_pc3, true);
} else if(CFW_SETTINGS()->spi_nrf24_handle == SpiExtra) {
furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeOutputPushPull);
furi_hal_gpio_write(&gpio_ext_pa4, true);
}

furi_hal_spi_bus_handle_init(nrf24_HANDLE);
furi_hal_spi_acquire(nrf24_HANDLE);
furi_hal_gpio_init(nrf24_CE_PIN, GpioModeOutputPushPull, GpioPullUp, GpioSpeedVeryHigh);
Expand All @@ -17,6 +26,13 @@ void nrf24_deinit() {
furi_hal_spi_bus_handle_deinit(nrf24_HANDLE);
furi_hal_gpio_write(nrf24_CE_PIN, false);
furi_hal_gpio_init(nrf24_CE_PIN, GpioModeAnalog, GpioPullNo, GpioSpeedLow);

// resetting the CS pins to floating
if(CFW_SETTINGS()->spi_nrf24_handle == SpiDefault) {
furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeAnalog);
} else if(CFW_SETTINGS()->spi_nrf24_handle == SpiExtra) {
furi_hal_gpio_init_simple(&gpio_ext_pa4, GpioModeAnalog);
}
}

void nrf24_spi_trx(
Expand Down
5 changes: 4 additions & 1 deletion applications/external/mousejacker/lib/nrf24/nrf24.h
Expand Up @@ -2,6 +2,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <furi_hal_spi.h>
#include <cfw.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -40,7 +41,9 @@ extern "C" {

#define nrf24_TIMEOUT 500
#define nrf24_CE_PIN &gpio_ext_pb2
#define nrf24_HANDLE &furi_hal_spi_bus_handle_external
#define nrf24_HANDLE \
(CFW_SETTINGS()->spi_nrf24_handle == SpiDefault ? &furi_hal_spi_bus_handle_external : \
&furi_hal_spi_bus_handle_external_extra)

/* Low level API */

Expand Down

0 comments on commit fc4c6bd

Please sign in to comment.