SPI HAL implemented and more (#207)
- Cleanup unused code and move ISR/IRQ checks to `Kernel.h` - Improve clang-format - Fix for LVGL lock transfer: ensure lock isn't activate when changing the lock - Implement SPI HAL - Remove `initHardware` HAL configuration entry - Fix `I2cScanner`: don't scan when port isn't started
This commit is contained in:
parent
88b3bfbe3e
commit
c1f55429b6
@ -41,11 +41,11 @@ ContinuationIndentWidth: 4
|
||||
EmptyLineBeforeAccessModifier: Always
|
||||
EmptyLineAfterAccessModifier: Always
|
||||
IndentCaseLabels: true
|
||||
IndentPPDirectives: None
|
||||
IndentPPDirectives: BeforeHash
|
||||
IndentWidth: 4
|
||||
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||
MaxEmptyLinesToKeep: 2
|
||||
NamespaceIndentation: All
|
||||
NamespaceIndentation: None
|
||||
ObjCSpaceAfterProperty: false
|
||||
ObjCSpaceBeforeProtocolList: true
|
||||
PointerAlignment: Left
|
||||
|
||||
@ -1,84 +0,0 @@
|
||||
#include "YellowConfig.h"
|
||||
#include "hal/YellowTouchConstants.h"
|
||||
#include <Tactility/TactilityCore.h>
|
||||
#include <driver/spi_common.h>
|
||||
|
||||
#define TAG "twodotfour_bootstrap"
|
||||
|
||||
static bool init_i2c() {
|
||||
TT_LOG_I(TAG, LOG_MESSAGE_I2C_INIT_START);
|
||||
|
||||
const i2c_config_t i2c_conf = {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = GPIO_NUM_33,
|
||||
.scl_io_num = GPIO_NUM_32,
|
||||
.sda_pullup_en = false,
|
||||
.scl_pullup_en = false,
|
||||
.master = {
|
||||
.clk_speed = 400000
|
||||
}
|
||||
};
|
||||
|
||||
if (i2c_param_config(TWODOTFOUR_TOUCH_I2C_PORT, &i2c_conf) != ESP_OK) {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_I2C_INIT_CONFIG_FAILED );
|
||||
return false;
|
||||
}
|
||||
|
||||
if (i2c_driver_install(TWODOTFOUR_TOUCH_I2C_PORT, i2c_conf.mode, 0, 0, 0) != ESP_OK) {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_I2C_INIT_DRIVER_INSTALL_FAILED);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool init_spi2() {
|
||||
TT_LOG_I(TAG, LOG_MESSAGE_SPI_INIT_START_FMT, SPI2_HOST);
|
||||
|
||||
const spi_bus_config_t bus_config = {
|
||||
.mosi_io_num = TWODOTFOUR_SPI2_PIN_MOSI,
|
||||
.miso_io_num = GPIO_NUM_NC,
|
||||
.sclk_io_num = TWODOTFOUR_SPI2_PIN_SCLK,
|
||||
.quadwp_io_num = GPIO_NUM_NC,
|
||||
.quadhd_io_num = GPIO_NUM_NC,
|
||||
.max_transfer_sz = TWODOTFOUR_SPI2_TRANSACTION_LIMIT
|
||||
};
|
||||
|
||||
if (spi_bus_initialize(SPI2_HOST, &bus_config, SPI_DMA_CH_AUTO) != ESP_OK) {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_SPI_INIT_FAILED_FMT, SPI2_HOST);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool init_spi3() {
|
||||
TT_LOG_I(TAG, LOG_MESSAGE_SPI_INIT_START_FMT, SPI3_HOST);
|
||||
|
||||
const spi_bus_config_t bus_config = {
|
||||
.mosi_io_num = TWODOTFOUR_SPI3_PIN_MOSI,
|
||||
.miso_io_num = TWODOTFOUR_SPI3_PIN_MISO,
|
||||
.sclk_io_num = TWODOTFOUR_SPI3_PIN_SCLK,
|
||||
.quadwp_io_num = GPIO_NUM_NC,
|
||||
.quadhd_io_num = GPIO_NUM_NC,
|
||||
.data4_io_num = 0,
|
||||
.data5_io_num = 0,
|
||||
.data6_io_num = 0,
|
||||
.data7_io_num = 0,
|
||||
.max_transfer_sz = TWODOTFOUR_SPI3_TRANSACTION_LIMIT,
|
||||
.flags = 0,
|
||||
.isr_cpu_id = ESP_INTR_CPU_AFFINITY_AUTO,
|
||||
.intr_flags = 0
|
||||
};
|
||||
|
||||
if (spi_bus_initialize(SPI3_HOST, &bus_config, SPI_DMA_CH_AUTO) != ESP_OK) {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_SPI_INIT_FAILED_FMT, SPI3_HOST);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool twodotfour_boot() {
|
||||
return init_i2c() && init_spi2() && init_spi3();
|
||||
}
|
||||
@ -1,12 +1,14 @@
|
||||
#include "CYD2432S024C.h"
|
||||
#include "Tactility/lvgl/LvglSync.h"
|
||||
#include "hal/YellowDisplay.h"
|
||||
#include "hal/YellowDisplayConstants.h"
|
||||
#include "hal/YellowSdCard.h"
|
||||
|
||||
#define CYD_SPI_TRANSFER_SIZE_LIMIT (TWODOTFOUR_LCD_DRAW_BUFFER_SIZE * LV_COLOR_DEPTH / 8)
|
||||
|
||||
bool twodotfour_lvgl_init();
|
||||
bool twodotfour_boot();
|
||||
|
||||
const tt::hal::Configuration cyd_2432S024c_config = {
|
||||
.initBoot = &twodotfour_boot,
|
||||
.initLvgl = &twodotfour_lvgl_init,
|
||||
.createDisplay = createDisplay,
|
||||
.sdcard = createYellowSdCard(),
|
||||
@ -15,13 +17,13 @@ const tt::hal::Configuration cyd_2432S024c_config = {
|
||||
tt::hal::i2c::Configuration {
|
||||
.name = "First",
|
||||
.port = I2C_NUM_0,
|
||||
.initMode = tt::hal::i2c::InitMode::Disabled,
|
||||
.initMode = tt::hal::i2c::InitMode::ByTactility,
|
||||
.canReinit = true,
|
||||
.hasMutableConfiguration = true,
|
||||
.config = (i2c_config_t) {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = GPIO_NUM_NC,
|
||||
.scl_io_num = GPIO_NUM_NC,
|
||||
.sda_io_num = GPIO_NUM_33,
|
||||
.scl_io_num = GPIO_NUM_32,
|
||||
.sda_pullup_en = false,
|
||||
.scl_pullup_en = false,
|
||||
.master = {
|
||||
@ -48,5 +50,55 @@ const tt::hal::Configuration cyd_2432S024c_config = {
|
||||
.clk_flags = 0
|
||||
}
|
||||
}
|
||||
},
|
||||
.spi {
|
||||
tt::hal::spi::Configuration {
|
||||
.device = SPI2_HOST,
|
||||
.dma = SPI_DMA_CH_AUTO,
|
||||
.config = {
|
||||
.mosi_io_num = GPIO_NUM_13,
|
||||
.miso_io_num = GPIO_NUM_NC,
|
||||
.sclk_io_num = GPIO_NUM_14,
|
||||
.quadwp_io_num = -1, // Quad SPI LCD driver is not yet supported
|
||||
.quadhd_io_num = -1, // Quad SPI LCD driver is not yet supported
|
||||
.data4_io_num = 0,
|
||||
.data5_io_num = 0,
|
||||
.data6_io_num = 0,
|
||||
.data7_io_num = 0,
|
||||
.data_io_default_level = false,
|
||||
.max_transfer_sz = CYD_SPI_TRANSFER_SIZE_LIMIT,
|
||||
.flags = 0,
|
||||
.isr_cpu_id = ESP_INTR_CPU_AFFINITY_AUTO,
|
||||
.intr_flags = 0
|
||||
},
|
||||
.initMode = tt::hal::spi::InitMode::ByTactility,
|
||||
.canReinit = false,
|
||||
.hasMutableConfiguration = false,
|
||||
.lock = tt::lvgl::getLvglSyncLockable() // esp_lvgl_port owns the lock for the display
|
||||
},
|
||||
tt::hal::spi::Configuration {
|
||||
.device = SPI3_HOST,
|
||||
.dma = SPI_DMA_CH_AUTO,
|
||||
.config = {
|
||||
.mosi_io_num = GPIO_NUM_23,
|
||||
.miso_io_num = GPIO_NUM_19,
|
||||
.sclk_io_num = GPIO_NUM_18,
|
||||
.quadwp_io_num = -1, // Quad SPI LCD driver is not yet supported
|
||||
.quadhd_io_num = -1, // Quad SPI LCD driver is not yet supported
|
||||
.data4_io_num = 0,
|
||||
.data5_io_num = 0,
|
||||
.data6_io_num = 0,
|
||||
.data7_io_num = 0,
|
||||
.data_io_default_level = false,
|
||||
.max_transfer_sz = 8192,
|
||||
.flags = 0,
|
||||
.isr_cpu_id = ESP_INTR_CPU_AFFINITY_AUTO,
|
||||
.intr_flags = 0
|
||||
},
|
||||
.initMode = tt::hal::spi::InitMode::ByTactility,
|
||||
.canReinit = false,
|
||||
.hasMutableConfiguration = false,
|
||||
.lock = nullptr
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "driver/spi_common.h"
|
||||
#include "driver/i2c.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "hal/YellowDisplayConstants.h"
|
||||
|
||||
// SPI 2 - display
|
||||
#define TWODOTFOUR_SPI2_PIN_SCLK GPIO_NUM_14
|
||||
#define TWODOTFOUR_SPI2_PIN_MOSI GPIO_NUM_13
|
||||
#define TWODOTFOUR_SPI2_TRANSACTION_LIMIT TWODOTFOUR_LCD_DRAW_BUFFER_SIZE
|
||||
|
||||
// SPI 3 - sdcard
|
||||
#define TWODOTFOUR_SPI3_PIN_SCLK GPIO_NUM_18
|
||||
#define TWODOTFOUR_SPI3_PIN_MOSI GPIO_NUM_23
|
||||
#define TWODOTFOUR_SPI3_PIN_MISO GPIO_NUM_19
|
||||
#define TWODOTFOUR_SPI3_TRANSACTION_LIMIT 8192 // TODO: Determine proper limit
|
||||
@ -1,51 +0,0 @@
|
||||
#include "hal/TdeckDisplayConstants.h"
|
||||
#include <Tactility/TactilityCore.h>
|
||||
#include <driver/spi_common.h>
|
||||
#include <soc/gpio_num.h>
|
||||
|
||||
#define TAG "tdeck"
|
||||
|
||||
// SPI
|
||||
#define TDECK_SPI_HOST SPI2_HOST
|
||||
#define TDECK_SPI_PIN_SCLK GPIO_NUM_40
|
||||
#define TDECK_SPI_PIN_MOSI GPIO_NUM_41
|
||||
#define TDECK_SPI_PIN_MISO GPIO_NUM_38
|
||||
#define TDECK_SPI_TRANSFER_SIZE_LIMIT (TDECK_LCD_HORIZONTAL_RESOLUTION * TDECK_LCD_SPI_TRANSFER_HEIGHT * (TDECK_LCD_BITS_PER_PIXEL / 8))
|
||||
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_TIMER LEDC_TIMER_0
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_MODE LEDC_LOW_SPEED_MODE
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_CHANNEL LEDC_CHANNEL_0
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_DUTY_RES LEDC_TIMER_8_BIT
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_FREQUENCY (4000)
|
||||
|
||||
static bool init_spi() {
|
||||
TT_LOG_I(TAG, LOG_MESSAGE_SPI_INIT_START_FMT, TDECK_SPI_HOST);
|
||||
|
||||
spi_bus_config_t bus_config = {
|
||||
.mosi_io_num = TDECK_SPI_PIN_MOSI,
|
||||
.miso_io_num = TDECK_SPI_PIN_MISO,
|
||||
.sclk_io_num = TDECK_SPI_PIN_SCLK,
|
||||
.quadwp_io_num = -1, // Quad SPI LCD driver is not yet supported
|
||||
.quadhd_io_num = -1, // Quad SPI LCD driver is not yet supported
|
||||
.data4_io_num = 0,
|
||||
.data5_io_num = 0,
|
||||
.data6_io_num = 0,
|
||||
.data7_io_num = 0,
|
||||
.data_io_default_level = false,
|
||||
.max_transfer_sz = TDECK_SPI_TRANSFER_SIZE_LIMIT,
|
||||
.flags = 0,
|
||||
.isr_cpu_id = ESP_INTR_CPU_AFFINITY_AUTO,
|
||||
.intr_flags = 0
|
||||
};
|
||||
|
||||
if (spi_bus_initialize(TDECK_SPI_HOST, &bus_config, SPI_DMA_CH_AUTO) != ESP_OK) {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_SPI_INIT_FAILED_FMT, TDECK_SPI_HOST);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tdeck_init_hardware() {
|
||||
return init_spi();
|
||||
}
|
||||
@ -1,17 +1,19 @@
|
||||
#include "Tactility/lvgl/LvglSync.h"
|
||||
#include "hal/TdeckDisplay.h"
|
||||
#include "hal/TdeckDisplayConstants.h"
|
||||
#include "hal/TdeckKeyboard.h"
|
||||
#include "hal/TdeckPower.h"
|
||||
#include "hal/TdeckSdCard.h"
|
||||
|
||||
#include <Tactility/hal/Configuration.h>
|
||||
|
||||
#define TDECK_SPI_TRANSFER_SIZE_LIMIT (TDECK_LCD_HORIZONTAL_RESOLUTION * TDECK_LCD_SPI_TRANSFER_HEIGHT * (TDECK_LCD_BITS_PER_PIXEL / 8))
|
||||
|
||||
bool tdeck_init_power();
|
||||
bool tdeck_init_hardware();
|
||||
bool tdeck_init_lvgl();
|
||||
|
||||
extern const tt::hal::Configuration lilygo_tdeck = {
|
||||
.initBoot = tdeck_init_power,
|
||||
.initHardware = tdeck_init_hardware,
|
||||
.initLvgl = tdeck_init_lvgl,
|
||||
.createDisplay = createDisplay,
|
||||
.createKeyboard = createKeyboard,
|
||||
@ -54,5 +56,31 @@ extern const tt::hal::Configuration lilygo_tdeck = {
|
||||
.clk_flags = 0
|
||||
}
|
||||
}
|
||||
},
|
||||
.spi {
|
||||
tt::hal::spi::Configuration {
|
||||
.device = SPI2_HOST,
|
||||
.dma = SPI_DMA_CH_AUTO,
|
||||
.config = {
|
||||
.mosi_io_num = GPIO_NUM_41,
|
||||
.miso_io_num = GPIO_NUM_38,
|
||||
.sclk_io_num = GPIO_NUM_40,
|
||||
.quadwp_io_num = -1, // Quad SPI LCD driver is not yet supported
|
||||
.quadhd_io_num = -1, // Quad SPI LCD driver is not yet supported
|
||||
.data4_io_num = 0,
|
||||
.data5_io_num = 0,
|
||||
.data6_io_num = 0,
|
||||
.data7_io_num = 0,
|
||||
.data_io_default_level = false,
|
||||
.max_transfer_sz = TDECK_SPI_TRANSFER_SIZE_LIMIT,
|
||||
.flags = 0,
|
||||
.isr_cpu_id = ESP_INTR_CPU_AFFINITY_AUTO,
|
||||
.intr_flags = 0
|
||||
},
|
||||
.initMode = tt::hal::spi::InitMode::ByTactility,
|
||||
.canReinit = false,
|
||||
.hasMutableConfiguration = false,
|
||||
.lock = tt::lvgl::getLvglSyncLockable() // esp_lvgl_port owns the lock for the display
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
#include "hal/Core2DisplayConstants.h"
|
||||
#include "axp192/axp192.h"
|
||||
|
||||
#include <Tactility/Log.h>
|
||||
@ -7,14 +6,9 @@
|
||||
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/spi_master.h>
|
||||
#include <esp_intr_types.h>
|
||||
|
||||
#define TAG "core2"
|
||||
|
||||
#define CORE2_SPI2_PIN_SCLK GPIO_NUM_18
|
||||
#define CORE2_SPI2_PIN_MOSI GPIO_NUM_23
|
||||
#define CORE2_SPI2_PIN_MISO GPIO_NUM_38
|
||||
|
||||
axp192_t axpDevice;
|
||||
|
||||
static int32_t axpI2cRead(TT_UNUSED void* handle, uint8_t address, uint8_t reg, uint8_t* buffer, uint16_t size) {
|
||||
@ -33,33 +27,7 @@ static int32_t axpI2cWrite(TT_UNUSED void* handle, uint8_t address, uint8_t reg,
|
||||
}
|
||||
}
|
||||
|
||||
static bool initSpi2() {
|
||||
TT_LOG_I(TAG, LOG_MESSAGE_SPI_INIT_START_FMT, SPI2_HOST);
|
||||
const spi_bus_config_t bus_config = {
|
||||
.mosi_io_num = CORE2_SPI2_PIN_MOSI,
|
||||
.miso_io_num = CORE2_SPI2_PIN_MISO,
|
||||
.sclk_io_num = CORE2_SPI2_PIN_SCLK,
|
||||
.data2_io_num = GPIO_NUM_NC,
|
||||
.data3_io_num = GPIO_NUM_NC,
|
||||
.data4_io_num = GPIO_NUM_NC,
|
||||
.data5_io_num = GPIO_NUM_NC,
|
||||
.data6_io_num = GPIO_NUM_NC,
|
||||
.data7_io_num = GPIO_NUM_NC,
|
||||
.max_transfer_sz = CORE2_LCD_DRAW_BUFFER_SIZE,
|
||||
.flags = 0,
|
||||
.isr_cpu_id = ESP_INTR_CPU_AFFINITY_AUTO,
|
||||
.intr_flags = 0
|
||||
};
|
||||
|
||||
if (spi_bus_initialize(SPI2_HOST, &bus_config, SPI_DMA_CH_AUTO) != ESP_OK) {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_SPI_INIT_FAILED_FMT, SPI2_HOST);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool initAxp() {
|
||||
void initAxp() {
|
||||
axpDevice.read = axpI2cRead;
|
||||
axpDevice.write = axpI2cWrite;
|
||||
|
||||
@ -74,11 +42,10 @@ bool initAxp() {
|
||||
axp192_write(&axpDevice, AXP192_PWM1_DUTY_CYCLE_2, 255); // PWM 255 (LED OFF)
|
||||
axp192_write(&axpDevice, AXP192_GPIO1_CONTROL, 0x02); // GPIO1 PWM
|
||||
// TODO: We could charge at 390mA according to the M5Unified code, but the AXP driver in M5Unified limits to 132mA, so it's unclear what the AXP supports.
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool initBoot() {
|
||||
TT_LOG_I(TAG, "initBoot");
|
||||
return initAxp() && initSpi2();
|
||||
initAxp();
|
||||
return true;
|
||||
}
|
||||
@ -1,9 +1,13 @@
|
||||
#include "M5stackCore2.h"
|
||||
#include "InitBoot.h"
|
||||
#include "InitLvgl.h"
|
||||
#include "Tactility/lvgl/LvglSync.h"
|
||||
#include "hal/Core2Display.h"
|
||||
#include "hal/Core2SdCard.h"
|
||||
#include "hal/Core2DisplayConstants.h"
|
||||
#include "hal/Core2Power.h"
|
||||
#include "hal/Core2SdCard.h"
|
||||
|
||||
#define CORE2_SPI_TRANSFER_SIZE_LIMIT (CORE2_LCD_DRAW_BUFFER_SIZE * LV_COLOR_DEPTH / 8)
|
||||
|
||||
extern const tt::hal::Configuration m5stack_core2 = {
|
||||
.initBoot = initBoot,
|
||||
@ -48,5 +52,31 @@ extern const tt::hal::Configuration m5stack_core2 = {
|
||||
.clk_flags = 0
|
||||
}
|
||||
}
|
||||
},
|
||||
.spi {
|
||||
tt::hal::spi::Configuration {
|
||||
.device = SPI2_HOST,
|
||||
.dma = SPI_DMA_CH_AUTO,
|
||||
.config = {
|
||||
.mosi_io_num = GPIO_NUM_23,
|
||||
.miso_io_num = GPIO_NUM_38,
|
||||
.sclk_io_num = GPIO_NUM_18,
|
||||
.quadwp_io_num = -1, // Quad SPI LCD driver is not yet supported
|
||||
.quadhd_io_num = -1, // Quad SPI LCD driver is not yet supported
|
||||
.data4_io_num = 0,
|
||||
.data5_io_num = 0,
|
||||
.data6_io_num = 0,
|
||||
.data7_io_num = 0,
|
||||
.data_io_default_level = false,
|
||||
.max_transfer_sz = CORE2_SPI_TRANSFER_SIZE_LIMIT,
|
||||
.flags = 0,
|
||||
.isr_cpu_id = ESP_INTR_CPU_AFFINITY_AUTO,
|
||||
.intr_flags = 0
|
||||
},
|
||||
.initMode = tt::hal::spi::InitMode::ByTactility,
|
||||
.canReinit = false,
|
||||
.hasMutableConfiguration = false,
|
||||
.lock = tt::lvgl::getLvglSyncLockable() // esp_lvgl_port owns the lock for the display
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -5,48 +5,12 @@
|
||||
#include <Tactility/Log.h>
|
||||
#include <Tactility/kernel/Kernel.h>
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/spi_master.h>
|
||||
#include <esp_intr_types.h>
|
||||
|
||||
#define TAG "core2"
|
||||
|
||||
#define CORES3_SPI2_PIN_SCLK GPIO_NUM_36
|
||||
#define CORES3_SPI2_PIN_MOSI GPIO_NUM_37
|
||||
#define CORES3_SPI2_PIN_MISO GPIO_NUM_35
|
||||
|
||||
std::shared_ptr<Axp2101> axp2101;
|
||||
std::shared_ptr<Aw9523> aw9523;
|
||||
|
||||
/**
|
||||
* For details see https://github.com/espressif/esp-bsp/blob/master/bsp/m5stack_core_s3/m5stack_core_s3.c
|
||||
*/
|
||||
static bool initSpi3() {
|
||||
TT_LOG_I(TAG, LOG_MESSAGE_SPI_INIT_START_FMT, SPI3_HOST);
|
||||
const spi_bus_config_t bus_config = {
|
||||
.mosi_io_num = CORES3_SPI2_PIN_MOSI,
|
||||
.miso_io_num = CORES3_SPI2_PIN_MISO,
|
||||
.sclk_io_num = CORES3_SPI2_PIN_SCLK,
|
||||
.data2_io_num = GPIO_NUM_NC,
|
||||
.data3_io_num = GPIO_NUM_NC,
|
||||
.data4_io_num = GPIO_NUM_NC,
|
||||
.data5_io_num = GPIO_NUM_NC,
|
||||
.data6_io_num = GPIO_NUM_NC,
|
||||
.data7_io_num = GPIO_NUM_NC,
|
||||
.data_io_default_level = false,
|
||||
.max_transfer_sz = CORES3_LCD_DRAW_BUFFER_SIZE,
|
||||
.flags = 0,
|
||||
.isr_cpu_id = ESP_INTR_CPU_AFFINITY_AUTO,
|
||||
.intr_flags = 0
|
||||
};
|
||||
|
||||
if (spi_bus_initialize(SPI3_HOST, &bus_config, SPI_DMA_CH_AUTO) != ESP_OK) {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_SPI_INIT_FAILED_FMT, SPI3_HOST);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* For details see https://github.com/espressif/esp-bsp/blob/master/bsp/m5stack_core_s3/m5stack_core_s3.c
|
||||
* and schematic: https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/docs/datasheet/core/K128%20CoreS3/Sch_M5_CoreS3_v1.0.pdf
|
||||
@ -184,7 +148,5 @@ bool initBoot() {
|
||||
aw9523 = std::make_shared<Aw9523>(I2C_NUM_0);
|
||||
tt::hal::registerDevice(aw9523);
|
||||
|
||||
return initPowerControl() &&
|
||||
initGpioExpander() &&
|
||||
initSpi3();
|
||||
return initPowerControl() && initGpioExpander();
|
||||
}
|
||||
@ -1,9 +1,13 @@
|
||||
#include "M5stackCoreS3.h"
|
||||
#include "InitBoot.h"
|
||||
#include "InitLvgl.h"
|
||||
#include "Tactility/lvgl/LvglSync.h"
|
||||
#include "hal/CoreS3Display.h"
|
||||
#include "hal/CoreS3SdCard.h"
|
||||
#include "hal/CoreS3DisplayConstants.h"
|
||||
#include "hal/CoreS3Power.h"
|
||||
#include "hal/CoreS3SdCard.h"
|
||||
|
||||
#define CORES3_TRANSACTION_SIZE (CORES3_LCD_DRAW_BUFFER_SIZE * LV_COLOR_DEPTH / 8)
|
||||
|
||||
const tt::hal::Configuration m5stack_cores3 = {
|
||||
.initBoot = initBoot,
|
||||
@ -48,5 +52,31 @@ const tt::hal::Configuration m5stack_cores3 = {
|
||||
.clk_flags = 0
|
||||
}
|
||||
}
|
||||
},
|
||||
.spi {
|
||||
tt::hal::spi::Configuration {
|
||||
.device = SPI3_HOST,
|
||||
.dma = SPI_DMA_CH_AUTO,
|
||||
.config = {
|
||||
.mosi_io_num = GPIO_NUM_37,
|
||||
.miso_io_num = GPIO_NUM_35,
|
||||
.sclk_io_num = GPIO_NUM_36,
|
||||
.data2_io_num = GPIO_NUM_NC,
|
||||
.data3_io_num = GPIO_NUM_NC,
|
||||
.data4_io_num = GPIO_NUM_NC,
|
||||
.data5_io_num = GPIO_NUM_NC,
|
||||
.data6_io_num = GPIO_NUM_NC,
|
||||
.data7_io_num = GPIO_NUM_NC,
|
||||
.data_io_default_level = false,
|
||||
.max_transfer_sz = CORES3_TRANSACTION_SIZE,
|
||||
.flags = 0,
|
||||
.isr_cpu_id = ESP_INTR_CPU_AFFINITY_AUTO,
|
||||
.intr_flags = 0
|
||||
},
|
||||
.initMode = tt::hal::spi::InitMode::ByTactility,
|
||||
.canReinit = false,
|
||||
.hasMutableConfiguration = false,
|
||||
.lock = tt::lvgl::getLvglSyncLockable() // esp_lvgl_port owns the lock for the display
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,47 +0,0 @@
|
||||
#include "hal/UnPhoneDisplayConstants.h"
|
||||
#include "hx8357/disp_spi.h"
|
||||
#include <Tactility/TactilityCore.h>
|
||||
#include <driver/spi_common.h>
|
||||
#include <soc/gpio_num.h>
|
||||
#include <lvgl.h>
|
||||
|
||||
#define TAG "unphone"
|
||||
|
||||
// SPI
|
||||
#define UNPHONE_SPI_HOST SPI2_HOST
|
||||
#define UNPHONE_SPI_PIN_SCLK GPIO_NUM_39
|
||||
#define UNPHONE_SPI_PIN_MOSI GPIO_NUM_40
|
||||
#define UNPHONE_SPI_PIN_MISO GPIO_NUM_41
|
||||
#define UNPHONE_SPI_TRANSFER_SIZE_LIMIT (UNPHONE_LCD_HORIZONTAL_RESOLUTION * UNPHONE_LCD_SPI_TRANSFER_HEIGHT * LV_COLOR_DEPTH / 8)
|
||||
|
||||
static bool initSpi() {
|
||||
TT_LOG_I(TAG, LOG_MESSAGE_SPI_INIT_START_FMT, UNPHONE_SPI_HOST);
|
||||
|
||||
spi_bus_config_t bus_config = {
|
||||
.mosi_io_num = UNPHONE_SPI_PIN_MOSI,
|
||||
.miso_io_num = UNPHONE_SPI_PIN_MISO,
|
||||
.sclk_io_num = UNPHONE_SPI_PIN_SCLK,
|
||||
.quadwp_io_num = -1, // Quad SPI LCD driver is not yet supported
|
||||
.quadhd_io_num = -1, // Quad SPI LCD driver is not yet supported
|
||||
.data4_io_num = 0,
|
||||
.data5_io_num = 0,
|
||||
.data6_io_num = 0,
|
||||
.data7_io_num = 0,
|
||||
.data_io_default_level = false,
|
||||
.max_transfer_sz = UNPHONE_SPI_TRANSFER_SIZE_LIMIT,
|
||||
.flags = 0,
|
||||
.isr_cpu_id = ESP_INTR_CPU_AFFINITY_AUTO,
|
||||
.intr_flags = 0
|
||||
};
|
||||
|
||||
if (spi_bus_initialize(UNPHONE_SPI_HOST, &bus_config, SPI_DMA_CH_AUTO) != ESP_OK) {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_SPI_INIT_FAILED_FMT, UNPHONE_SPI_HOST);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool unPhoneInitHardware() {
|
||||
return initSpi();
|
||||
}
|
||||
@ -1,5 +1,3 @@
|
||||
#include "hal/UnPhoneDisplay.h"
|
||||
|
||||
#include <Tactility/Log.h>
|
||||
#include <Tactility/Thread.h>
|
||||
#include <Tactility/lvgl/LvglSync.h>
|
||||
|
||||
@ -1,16 +1,18 @@
|
||||
#include "Tactility/lvgl/LvglSync.h"
|
||||
#include "UnPhoneFeatures.h"
|
||||
#include "hal/UnPhoneDisplayConstants.h"
|
||||
#include "hal/UnPhoneDisplay.h"
|
||||
#include "hal/UnPhonePower.h"
|
||||
#include "hal/UnPhoneSdCard.h"
|
||||
#include <Tactility/hal/Configuration.h>
|
||||
|
||||
#define UNPHONE_SPI_TRANSFER_SIZE_LIMIT (UNPHONE_LCD_HORIZONTAL_RESOLUTION * UNPHONE_LCD_SPI_TRANSFER_HEIGHT * LV_COLOR_DEPTH / 8)
|
||||
|
||||
bool unPhoneInitPower();
|
||||
bool unPhoneInitHardware();
|
||||
bool unPhoneInitLvgl();
|
||||
|
||||
extern const tt::hal::Configuration unPhone = {
|
||||
.initBoot = unPhoneInitPower,
|
||||
.initHardware = unPhoneInitHardware,
|
||||
.initLvgl = unPhoneInitLvgl,
|
||||
.createDisplay = createDisplay,
|
||||
.sdcard = createUnPhoneSdCard(),
|
||||
@ -52,5 +54,31 @@ extern const tt::hal::Configuration unPhone = {
|
||||
.clk_flags = 0
|
||||
}
|
||||
}
|
||||
},
|
||||
.spi {
|
||||
tt::hal::spi::Configuration {
|
||||
.device = SPI2_HOST,
|
||||
.dma = SPI_DMA_CH_AUTO,
|
||||
.config = {
|
||||
.mosi_io_num = GPIO_NUM_40,
|
||||
.miso_io_num = GPIO_NUM_41,
|
||||
.sclk_io_num = GPIO_NUM_39,
|
||||
.quadwp_io_num = -1, // Quad SPI LCD driver is not yet supported
|
||||
.quadhd_io_num = -1, // Quad SPI LCD driver is not yet supported
|
||||
.data4_io_num = 0,
|
||||
.data5_io_num = 0,
|
||||
.data6_io_num = 0,
|
||||
.data7_io_num = 0,
|
||||
.data_io_default_level = false,
|
||||
.max_transfer_sz = UNPHONE_SPI_TRANSFER_SIZE_LIMIT,
|
||||
.flags = 0,
|
||||
.isr_cpu_id = ESP_INTR_CPU_AFFINITY_AUTO,
|
||||
.intr_flags = 0
|
||||
},
|
||||
.initMode = tt::hal::spi::InitMode::ByTactility,
|
||||
.canReinit = false,
|
||||
.hasMutableConfiguration = false,
|
||||
.lock = tt::lvgl::getLvglSyncLockable() // esp_lvgl_port owns the lock for the display
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -14,7 +14,6 @@
|
||||
#include <Tactility/Timer.h>
|
||||
|
||||
#include <format>
|
||||
#include <ranges>
|
||||
|
||||
#define START_SCAN_TEXT "Scan"
|
||||
#define STOP_SCAN_TEXT "Stop scan"
|
||||
@ -225,20 +224,27 @@ bool I2cScannerApp::shouldStopScanTimer() {
|
||||
void I2cScannerApp::onScanTimer() {
|
||||
TT_LOG_I(TAG, "Scan thread started");
|
||||
|
||||
i2c_port_t safe_port;
|
||||
if (!getPort(&safe_port)) {
|
||||
TT_LOG_E(TAG, "Failed to get I2C port");
|
||||
onScanTimerFinished();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!hal::i2c::isStarted(safe_port)) {
|
||||
TT_LOG_E(TAG, "I2C port not started");
|
||||
onScanTimerFinished();
|
||||
return;
|
||||
}
|
||||
|
||||
for (uint8_t address = 0; address < 128; ++address) {
|
||||
i2c_port_t safe_port;
|
||||
if (getPort(&safe_port)) {
|
||||
if (hal::i2c::masterHasDeviceAtAddress(port, address, 10 / portTICK_PERIOD_MS)) {
|
||||
TT_LOG_I(TAG, "Found device at address %d", address);
|
||||
if (!shouldStopScanTimer()) {
|
||||
addAddressToList(address);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
if (hal::i2c::masterHasDeviceAtAddress(port, address, 10 / portTICK_PERIOD_MS)) {
|
||||
TT_LOG_I(TAG, "Found device at address %d", address);
|
||||
if (!shouldStopScanTimer()) {
|
||||
addAddressToList(address);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "onScanTimer");
|
||||
break;
|
||||
}
|
||||
|
||||
if (shouldStopScanTimer()) {
|
||||
|
||||
@ -18,8 +18,14 @@ static LvglLock lock_singleton = defaultLock;
|
||||
static LvglUnlock unlock_singleton = defaultUnlock;
|
||||
|
||||
void syncSet(LvglLock lock, LvglUnlock unlock) {
|
||||
auto old_lock = lock_singleton;
|
||||
auto old_unlock = unlock_singleton;
|
||||
|
||||
// Ensure the old lock is not engaged when changing locks
|
||||
old_lock(portMAX_DELAY);
|
||||
lock_singleton = lock;
|
||||
unlock_singleton = unlock;
|
||||
old_unlock();
|
||||
}
|
||||
|
||||
bool lock(TickType_t timeout) {
|
||||
|
||||
@ -2,36 +2,8 @@
|
||||
|
||||
#include "CoreExtraDefines.h"
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#else
|
||||
#include "portmacro.h"
|
||||
#endif
|
||||
|
||||
#define TT_RETURNS_NONNULL __attribute__((returns_nonnull))
|
||||
|
||||
#define TT_WARN_UNUSED __attribute__((warn_unused_result))
|
||||
|
||||
#define TT_UNUSED __attribute__((unused))
|
||||
|
||||
#define TT_WEAK __attribute__((weak))
|
||||
|
||||
#define TT_PACKED __attribute__((packed))
|
||||
|
||||
#define TT_PLACE_IN_SECTION(x) __attribute__((section(x)))
|
||||
|
||||
#define TT_ALIGN(n) __attribute__((aligned(n)))
|
||||
|
||||
// Used by portENABLE_INTERRUPTS and portDISABLE_INTERRUPTS?
|
||||
#ifdef ESP_PLATFORM
|
||||
#define TT_IS_IRQ_MODE() (xPortInIsrContext() == pdTRUE)
|
||||
#else
|
||||
#define TT_IS_IRQ_MODE() false
|
||||
#endif
|
||||
|
||||
#define TT_IS_ISR() (TT_IS_IRQ_MODE())
|
||||
|
||||
#define TT_CHECK_RETURN __attribute__((__warn_unused_result__))
|
||||
|
||||
// region Variable arguments support
|
||||
|
||||
// Adapted from https://stackoverflow.com/a/78848701/3848666
|
||||
|
||||
@ -37,13 +37,24 @@ public:
|
||||
ErrorISR = 0xFFFFFFFAU, ///< TtStatusErrorISR (-6).
|
||||
};
|
||||
|
||||
/** Set the bitmask for 1 or more flags that we might be waiting for */
|
||||
uint32_t set(uint32_t flags) const;
|
||||
|
||||
/** Clear the specified flags */
|
||||
uint32_t clear(uint32_t flags) const;
|
||||
|
||||
/** Get the currently set flags */
|
||||
uint32_t get() const;
|
||||
|
||||
/** Await for flags to be set
|
||||
* @param[in] flags the bitmask of the flags that we want to wait for
|
||||
* @param[in] options the trigger behaviour: WaitAny, WaitAll, NoClear (NoClear can be combined with either WaitAny or WaitAll)
|
||||
* @param[in] timeoutTicks the maximum amount of ticks to wait
|
||||
*/
|
||||
uint32_t wait(
|
||||
uint32_t flags,
|
||||
uint32_t options = WaitAny,
|
||||
uint32_t timeout = (uint32_t)portMAX_DELAY
|
||||
uint32_t timeoutTicks = (uint32_t)portMAX_DELAY
|
||||
) const;
|
||||
};
|
||||
|
||||
|
||||
@ -22,6 +22,6 @@ void log(LogLevel level, const char* tag, const char* format, ...);
|
||||
#define TT_LOG_D(tag, format, ...) \
|
||||
tt::log(tt::LogLevel::Debug, tag, format, ##__VA_ARGS__)
|
||||
#define TT_LOG_V(tag, format, ...) \
|
||||
tt::log(tt::LogLevel::Trace, tag, format, ##__VA_ARGS__)
|
||||
tt::log(tt::LogLevel::Verbose, tag, format, ##__VA_ARGS__)
|
||||
|
||||
#endif // ESP_PLATFORM
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
#include "RtosCompatSemaphore.h"
|
||||
#include "Check.h"
|
||||
#include "Lockable.h"
|
||||
#include "kernel/Kernel.h"
|
||||
#include <memory>
|
||||
|
||||
namespace tt {
|
||||
@ -29,7 +30,7 @@ private:
|
||||
|
||||
struct SemaphoreHandleDeleter {
|
||||
void operator()(QueueHandle_t handleToDelete) {
|
||||
assert(!TT_IS_IRQ_MODE());
|
||||
assert(!kernel::isIsr());
|
||||
vSemaphoreDelete(handleToDelete);
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "kernel/Kernel.h"
|
||||
#include "Lockable.h"
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
@ -24,7 +25,7 @@ private:
|
||||
|
||||
struct SemaphoreHandleDeleter {
|
||||
void operator()(QueueHandle_t handleToDelete) {
|
||||
assert(!TT_IS_IRQ_MODE());
|
||||
assert(!kernel::isIsr());
|
||||
vSemaphoreDelete(handleToDelete);
|
||||
}
|
||||
};
|
||||
|
||||
@ -42,6 +42,7 @@ std::string join(const std::vector<std::string>& input, const std::string& delim
|
||||
|
||||
/**
|
||||
* Returns the lowercase value of a string.
|
||||
* @warning This only works for strings with 1 byte per character
|
||||
* @param[in] the string with lower and/or uppercase characters
|
||||
* @return a string with only lowercase characters
|
||||
*/
|
||||
|
||||
@ -14,6 +14,13 @@ typedef enum {
|
||||
PlatformSimulator
|
||||
} Platform;
|
||||
|
||||
/** Return true when called from an Interrupt Service Routine (~IRQ mode) */
|
||||
#ifdef ESP_PLATFORM
|
||||
inline constexpr bool isIsr() { return (xPortInIsrContext() == pdTRUE); }
|
||||
#else
|
||||
inline constexpr bool isIsr() { return false; }
|
||||
#endif
|
||||
|
||||
/** Check if kernel is running
|
||||
* @return true if the FreeRTOS kernel is running, false otherwise
|
||||
*/
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#include "Tactility/EventFlag.h"
|
||||
|
||||
#include "Tactility/Check.h"
|
||||
#include "Tactility/CoreDefines.h"
|
||||
#include "Tactility/kernel/Kernel.h"
|
||||
|
||||
#define TT_EVENT_FLAG_MAX_BITS_EVENT_GROUPS 24U
|
||||
#define TT_EVENT_FLAG_INVALID_BITS (~((1UL << TT_EVENT_FLAG_MAX_BITS_EVENT_GROUPS) - 1U))
|
||||
@ -11,12 +11,12 @@ namespace tt {
|
||||
EventFlag::EventFlag() :
|
||||
handle(xEventGroupCreate())
|
||||
{
|
||||
assert(!TT_IS_IRQ_MODE());
|
||||
assert(!kernel::isIsr());
|
||||
tt_check(handle);
|
||||
}
|
||||
|
||||
EventFlag::~EventFlag() {
|
||||
assert(!TT_IS_IRQ_MODE());
|
||||
assert(!kernel::isIsr());
|
||||
}
|
||||
|
||||
uint32_t EventFlag::set(uint32_t flags) const {
|
||||
@ -26,7 +26,7 @@ uint32_t EventFlag::set(uint32_t flags) const {
|
||||
uint32_t rflags;
|
||||
BaseType_t yield;
|
||||
|
||||
if (TT_IS_IRQ_MODE()) {
|
||||
if (kernel::isIsr()) {
|
||||
yield = pdFALSE;
|
||||
if (xEventGroupSetBitsFromISR(handle.get(), (EventBits_t)flags, &yield) == pdFAIL) {
|
||||
rflags = (uint32_t)ErrorResource;
|
||||
@ -47,7 +47,7 @@ uint32_t EventFlag::clear(uint32_t flags) const {
|
||||
|
||||
uint32_t rflags;
|
||||
|
||||
if (TT_IS_IRQ_MODE()) {
|
||||
if (kernel::isIsr()) {
|
||||
rflags = xEventGroupGetBitsFromISR(handle.get());
|
||||
|
||||
if (xEventGroupClearBitsFromISR(handle.get(), (EventBits_t)flags) == pdFAIL) {
|
||||
@ -69,7 +69,7 @@ uint32_t EventFlag::clear(uint32_t flags) const {
|
||||
uint32_t EventFlag::get() const {
|
||||
uint32_t rflags;
|
||||
|
||||
if (TT_IS_IRQ_MODE()) {
|
||||
if (kernel::isIsr()) {
|
||||
rflags = xEventGroupGetBitsFromISR(handle.get());
|
||||
} else {
|
||||
rflags = xEventGroupGetBits(handle.get());
|
||||
@ -82,9 +82,9 @@ uint32_t EventFlag::get() const {
|
||||
uint32_t EventFlag::wait(
|
||||
uint32_t flags,
|
||||
uint32_t options,
|
||||
uint32_t timeout
|
||||
uint32_t timeoutTicksw
|
||||
) const {
|
||||
assert(!TT_IS_IRQ_MODE());
|
||||
assert(!kernel::isIsr());
|
||||
assert((flags & TT_EVENT_FLAG_INVALID_BITS) == 0U);
|
||||
|
||||
BaseType_t wait_all;
|
||||
@ -94,11 +94,11 @@ uint32_t EventFlag::wait(
|
||||
if (options & WaitAll) {
|
||||
wait_all = pdTRUE;
|
||||
} else {
|
||||
wait_all = pdFAIL;
|
||||
wait_all = pdFALSE;
|
||||
}
|
||||
|
||||
if (options & NoClear) {
|
||||
exit_clear = pdFAIL;
|
||||
exit_clear = pdFALSE;
|
||||
} else {
|
||||
exit_clear = pdTRUE;
|
||||
}
|
||||
@ -108,12 +108,12 @@ uint32_t EventFlag::wait(
|
||||
(EventBits_t)flags,
|
||||
exit_clear,
|
||||
wait_all,
|
||||
(TickType_t)timeout
|
||||
(TickType_t)timeoutTicksw
|
||||
);
|
||||
|
||||
if (options & WaitAll) {
|
||||
if ((flags & rflags) != flags) {
|
||||
if (timeout > 0U) {
|
||||
if (timeoutTicksw > 0U) {
|
||||
rflags = (uint32_t)ErrorTimeout;
|
||||
} else {
|
||||
rflags = (uint32_t)ErrorResource;
|
||||
@ -121,7 +121,7 @@ uint32_t EventFlag::wait(
|
||||
}
|
||||
} else {
|
||||
if ((flags & rflags) == 0U) {
|
||||
if (timeout > 0U) {
|
||||
if (timeoutTicksw > 0U) {
|
||||
rflags = (uint32_t)ErrorTimeout;
|
||||
} else {
|
||||
rflags = (uint32_t)ErrorResource;
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
namespace tt {
|
||||
|
||||
static inline QueueHandle_t createQueue(uint32_t capacity, uint32_t messageSize) {
|
||||
assert(!TT_IS_ISR() && (capacity > 0U) && (messageSize > 0U));
|
||||
assert(!kernel::isIsr() && (capacity > 0U) && (messageSize > 0U));
|
||||
return xQueueCreate(capacity, messageSize);
|
||||
}
|
||||
|
||||
@ -14,14 +14,14 @@ MessageQueue::MessageQueue(uint32_t capacity, uint32_t messageSize) : handle(cre
|
||||
}
|
||||
|
||||
MessageQueue::~MessageQueue() {
|
||||
assert(!TT_IS_ISR());
|
||||
assert(!kernel::isIsr());
|
||||
}
|
||||
|
||||
bool MessageQueue::put(const void* message, TickType_t timeout) {
|
||||
bool result = true;
|
||||
BaseType_t yield;
|
||||
|
||||
if (TT_IS_ISR()) {
|
||||
if (kernel::isIsr()) {
|
||||
if ((handle == nullptr) || (message == nullptr) || (timeout != 0U)) {
|
||||
result = false;
|
||||
} else {
|
||||
@ -46,7 +46,7 @@ bool MessageQueue::get(void* msg_ptr, TickType_t timeout) {
|
||||
bool result = true;
|
||||
BaseType_t yield;
|
||||
|
||||
if (TT_IS_ISR()) {
|
||||
if (kernel::isIsr()) {
|
||||
if ((handle == nullptr) || (msg_ptr == nullptr) || (timeout != 0U)) {
|
||||
result = false;
|
||||
} else {
|
||||
@ -92,7 +92,7 @@ uint32_t MessageQueue::getCount() const {
|
||||
|
||||
if (handle == nullptr) {
|
||||
count = 0U;
|
||||
} else if (TT_IS_ISR()) {
|
||||
} else if (kernel::isIsr()) {
|
||||
count = uxQueueMessagesWaitingFromISR(handle.get());
|
||||
} else {
|
||||
count = uxQueueMessagesWaiting(handle.get());
|
||||
@ -109,7 +109,7 @@ uint32_t MessageQueue::getSpace() const {
|
||||
|
||||
if (mq == nullptr) {
|
||||
space = 0U;
|
||||
} else if (TT_IS_ISR()) {
|
||||
} else if (kernel::isIsr()) {
|
||||
isrm = taskENTER_CRITICAL_FROM_ISR();
|
||||
|
||||
/* space = pxQueue->uxLength - pxQueue->uxMessagesWaiting; */
|
||||
@ -124,7 +124,7 @@ uint32_t MessageQueue::getSpace() const {
|
||||
}
|
||||
|
||||
bool MessageQueue::reset() {
|
||||
tt_check(!TT_IS_ISR());
|
||||
tt_check(!kernel::isIsr());
|
||||
if (handle == nullptr) {
|
||||
return false;
|
||||
} else {
|
||||
|
||||
@ -39,7 +39,7 @@ Mutex::Mutex(Type type) : handle(createSemaphoreHandle(type)), type(type) {
|
||||
}
|
||||
|
||||
bool Mutex::lock(TickType_t timeout) const {
|
||||
assert(!TT_IS_IRQ_MODE());
|
||||
assert(!kernel::isIsr());
|
||||
assert(handle != nullptr);
|
||||
tt_mutex_info(mutex, "acquire");
|
||||
|
||||
@ -54,7 +54,7 @@ bool Mutex::lock(TickType_t timeout) const {
|
||||
}
|
||||
|
||||
bool Mutex::unlock() const {
|
||||
assert(!TT_IS_IRQ_MODE());
|
||||
assert(!kernel::isIsr());
|
||||
assert(handle != nullptr);
|
||||
tt_mutex_info(mutex, "release");
|
||||
|
||||
@ -69,7 +69,7 @@ bool Mutex::unlock() const {
|
||||
}
|
||||
|
||||
ThreadId Mutex::getOwner() const {
|
||||
assert(!TT_IS_IRQ_MODE());
|
||||
assert(!kernel::isIsr());
|
||||
assert(handle != nullptr);
|
||||
return (ThreadId)xSemaphoreGetMutexHolder(handle.get());
|
||||
}
|
||||
|
||||
@ -22,16 +22,16 @@ static inline QueueHandle_t createHandle(uint32_t maxCount, uint32_t initialCoun
|
||||
}
|
||||
|
||||
Semaphore::Semaphore(uint32_t maxAvailable, uint32_t initialAvailable) : handle(createHandle(maxAvailable, initialAvailable)) {
|
||||
assert(!TT_IS_IRQ_MODE());
|
||||
assert(!kernel::isIsr());
|
||||
tt_check(handle != nullptr);
|
||||
}
|
||||
|
||||
Semaphore::~Semaphore() {
|
||||
assert(!TT_IS_IRQ_MODE());
|
||||
assert(!kernel::isIsr());
|
||||
}
|
||||
|
||||
bool Semaphore::acquire(TickType_t timeout) const {
|
||||
if (TT_IS_IRQ_MODE()) {
|
||||
if (kernel::isIsr()) {
|
||||
if (timeout != 0U) {
|
||||
return false;
|
||||
} else {
|
||||
@ -50,7 +50,7 @@ bool Semaphore::acquire(TickType_t timeout) const {
|
||||
}
|
||||
|
||||
bool Semaphore::release() const {
|
||||
if (TT_IS_IRQ_MODE()) {
|
||||
if (kernel::isIsr()) {
|
||||
BaseType_t yield = pdFALSE;
|
||||
if (xSemaphoreGiveFromISR(handle.get(), &yield) != pdTRUE) {
|
||||
return false;
|
||||
@ -64,7 +64,7 @@ bool Semaphore::release() const {
|
||||
}
|
||||
|
||||
uint32_t Semaphore::getAvailable() const {
|
||||
if (TT_IS_IRQ_MODE()) {
|
||||
if (kernel::isIsr()) {
|
||||
// TODO: uxSemaphoreGetCountFromISR is not supported on esp-idf 5.1.2 - perhaps later on?
|
||||
#ifdef uxSemaphoreGetCountFromISR
|
||||
return uxSemaphoreGetCountFromISR(handle.get());
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#include "Tactility/StreamBuffer.h"
|
||||
|
||||
#include "Tactility/Check.h"
|
||||
#include "Tactility/CoreDefines.h"
|
||||
#include "Tactility/kernel/Kernel.h"
|
||||
|
||||
namespace tt {
|
||||
|
||||
@ -23,7 +23,7 @@ size_t StreamBuffer::send(
|
||||
size_t length,
|
||||
uint32_t timeout
|
||||
) const {
|
||||
if (TT_IS_IRQ_MODE()) {
|
||||
if (kernel::isIsr()) {
|
||||
BaseType_t yield;
|
||||
size_t result = xStreamBufferSendFromISR(handle.get(), data, length, &yield);
|
||||
portYIELD_FROM_ISR(yield);
|
||||
@ -38,7 +38,7 @@ size_t StreamBuffer::receive(
|
||||
size_t length,
|
||||
uint32_t timeout
|
||||
) const {
|
||||
if (TT_IS_IRQ_MODE()) {
|
||||
if (kernel::isIsr()) {
|
||||
BaseType_t yield;
|
||||
size_t result = xStreamBufferReceiveFromISR(handle.get(), data, length, &yield);
|
||||
portYIELD_FROM_ISR(yield);
|
||||
|
||||
@ -206,7 +206,7 @@ uint32_t Thread::setFlags(ThreadId threadId, uint32_t flags) {
|
||||
} else {
|
||||
rflags = (uint32_t)EventFlag::Error;
|
||||
|
||||
if (TT_IS_IRQ_MODE()) {
|
||||
if (kernel::isIsr()) {
|
||||
yield = pdFALSE;
|
||||
|
||||
(void)xTaskNotifyIndexedFromISR(hTask, THREAD_NOTIFY_INDEX, flags, eSetBits, &yield);
|
||||
@ -228,7 +228,7 @@ uint32_t Thread::clearFlags(uint32_t flags) {
|
||||
TaskHandle_t hTask;
|
||||
uint32_t rflags, cflags;
|
||||
|
||||
if (TT_IS_IRQ_MODE()) {
|
||||
if (kernel::isIsr()) {
|
||||
rflags = (uint32_t)EventFlag::ErrorISR;
|
||||
} else if ((flags & THREAD_FLAGS_INVALID_BITS) != 0U) {
|
||||
rflags = (uint32_t)EventFlag::ErrorParameter;
|
||||
@ -257,7 +257,7 @@ uint32_t Thread::getFlags() {
|
||||
TaskHandle_t hTask;
|
||||
uint32_t rflags;
|
||||
|
||||
if (TT_IS_IRQ_MODE()) {
|
||||
if (kernel::isIsr()) {
|
||||
rflags = (uint32_t)EventFlag::ErrorISR;
|
||||
} else {
|
||||
hTask = xTaskGetCurrentTaskHandle();
|
||||
@ -277,7 +277,7 @@ uint32_t Thread::awaitFlags(uint32_t flags, uint32_t options, uint32_t timeout)
|
||||
TickType_t t0, td, tout;
|
||||
BaseType_t rval;
|
||||
|
||||
if (TT_IS_IRQ_MODE()) {
|
||||
if (kernel::isIsr()) {
|
||||
rflags = (uint32_t)EventFlag::ErrorISR;
|
||||
} else if ((flags & THREAD_FLAGS_INVALID_BITS) != 0U) {
|
||||
rflags = (uint32_t)EventFlag::ErrorParameter;
|
||||
@ -345,7 +345,7 @@ uint32_t Thread::getStackSpace(ThreadId threadId) {
|
||||
auto hTask = (TaskHandle_t)threadId;
|
||||
uint32_t sz;
|
||||
|
||||
if (TT_IS_IRQ_MODE() || (hTask == nullptr)) {
|
||||
if (kernel::isIsr() || (hTask == nullptr)) {
|
||||
sz = 0U;
|
||||
} else {
|
||||
sz = (uint32_t)(uxTaskGetStackHighWaterMark(hTask) * sizeof(StackType_t));
|
||||
@ -361,7 +361,7 @@ void Thread::suspend(ThreadId threadId) {
|
||||
|
||||
void Thread::resume(ThreadId threadId) {
|
||||
auto hTask = (TaskHandle_t)threadId;
|
||||
if (TT_IS_IRQ_MODE()) {
|
||||
if (kernel::isIsr()) {
|
||||
xTaskResumeFromISR(hTask);
|
||||
} else {
|
||||
vTaskResume(hTask);
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
#include "Tactility/Check.h"
|
||||
#include "Tactility/RtosCompat.h"
|
||||
#include "Tactility/kernel/Kernel.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
@ -34,44 +35,44 @@ Timer::Timer(Type type, Callback callback, std::shared_ptr<void> callbackContext
|
||||
callbackContext(std::move(callbackContext)),
|
||||
handle(createTimer(type, this, onCallback))
|
||||
{
|
||||
assert(!TT_IS_ISR());
|
||||
assert(!kernel::isIsr());
|
||||
assert(handle != nullptr);
|
||||
}
|
||||
|
||||
Timer::~Timer() {
|
||||
assert(!TT_IS_ISR());
|
||||
assert(!kernel::isIsr());
|
||||
}
|
||||
|
||||
bool Timer::start(TickType_t interval) {
|
||||
assert(!TT_IS_ISR());
|
||||
assert(!kernel::isIsr());
|
||||
assert(interval < portMAX_DELAY);
|
||||
return xTimerChangePeriod(handle.get(), interval, portMAX_DELAY) == pdPASS;
|
||||
}
|
||||
|
||||
bool Timer::restart(TickType_t interval) {
|
||||
assert(!TT_IS_ISR());
|
||||
assert(!kernel::isIsr());
|
||||
assert(interval < portMAX_DELAY);
|
||||
return xTimerChangePeriod(handle.get(), interval, portMAX_DELAY) == pdPASS &&
|
||||
xTimerReset(handle.get(), portMAX_DELAY) == pdPASS;
|
||||
}
|
||||
|
||||
bool Timer::stop() {
|
||||
assert(!TT_IS_ISR());
|
||||
assert(!kernel::isIsr());
|
||||
return xTimerStop(handle.get(), portMAX_DELAY) == pdPASS;
|
||||
}
|
||||
|
||||
bool Timer::isRunning() {
|
||||
assert(!TT_IS_ISR());
|
||||
assert(!kernel::isIsr());
|
||||
return xTimerIsTimerActive(handle.get()) == pdTRUE;
|
||||
}
|
||||
|
||||
TickType_t Timer::getExpireTime() {
|
||||
assert(!TT_IS_ISR());
|
||||
assert(!kernel::isIsr());
|
||||
return xTimerGetExpiryTime(handle.get());
|
||||
}
|
||||
|
||||
bool Timer::setPendingCallback(PendingCallback callback, void* callbackContext, uint32_t callbackArg, TickType_t timeout) {
|
||||
if (TT_IS_ISR()) {
|
||||
if (kernel::isIsr()) {
|
||||
assert(timeout == 0);
|
||||
return xTimerPendFunctionCallFromISR(callback, callbackContext, callbackArg, nullptr) == pdPASS;
|
||||
} else {
|
||||
@ -80,7 +81,7 @@ bool Timer::setPendingCallback(PendingCallback callback, void* callbackContext,
|
||||
}
|
||||
|
||||
void Timer::setThreadPriority(Thread::Priority priority) {
|
||||
assert(!TT_IS_ISR());
|
||||
assert(!kernel::isIsr());
|
||||
|
||||
TaskHandle_t task_handle = xTimerGetTimerDaemonTaskHandle();
|
||||
assert(task_handle); // Don't call this method before timer task start
|
||||
|
||||
@ -16,7 +16,7 @@ bool isRunning() {
|
||||
}
|
||||
|
||||
bool lock() {
|
||||
assert(!TT_IS_ISR());
|
||||
assert(!kernel::isIsr());
|
||||
|
||||
int32_t lock;
|
||||
|
||||
@ -39,7 +39,7 @@ bool lock() {
|
||||
}
|
||||
|
||||
bool unlock() {
|
||||
assert(!TT_IS_ISR());
|
||||
assert(!kernel::isIsr());
|
||||
|
||||
switch (xTaskGetSchedulerState()) {
|
||||
case taskSCHEDULER_SUSPENDED:
|
||||
@ -60,7 +60,7 @@ bool unlock() {
|
||||
}
|
||||
|
||||
bool restoreLock(bool lock) {
|
||||
assert(!TT_IS_ISR());
|
||||
assert(!kernel::isIsr());
|
||||
|
||||
switch (xTaskGetSchedulerState()) {
|
||||
case taskSCHEDULER_SUSPENDED:
|
||||
@ -89,7 +89,7 @@ uint32_t getTickFrequency() {
|
||||
}
|
||||
|
||||
void delayTicks(TickType_t ticks) {
|
||||
assert(!TT_IS_ISR());
|
||||
assert(!kernel::isIsr());
|
||||
if (ticks == 0U) {
|
||||
taskYIELD();
|
||||
} else {
|
||||
@ -98,7 +98,7 @@ void delayTicks(TickType_t ticks) {
|
||||
}
|
||||
|
||||
bool delayUntilTick(TickType_t tick) {
|
||||
assert(!TT_IS_ISR());
|
||||
assert(!kernel::isIsr());
|
||||
|
||||
TickType_t tcnt, delay;
|
||||
|
||||
@ -118,7 +118,7 @@ bool delayUntilTick(TickType_t tick) {
|
||||
}
|
||||
|
||||
TickType_t getTicks() {
|
||||
if (TT_IS_ISR() != 0U) {
|
||||
if (kernel::isIsr() != 0U) {
|
||||
return xTaskGetTickCountFromISR();
|
||||
} else {
|
||||
return xTaskGetTickCount();
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#include "Tactility/kernel/critical/Critical.h"
|
||||
|
||||
#include "Tactility/CoreDefines.h"
|
||||
#include "Tactility/RtosCompatTask.h"
|
||||
#include "Tactility/kernel/Kernel.h"
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
static portMUX_TYPE critical_mutex;
|
||||
@ -15,7 +15,7 @@ namespace tt::kernel::critical {
|
||||
CriticalInfo enter() {
|
||||
CriticalInfo info = {
|
||||
.isrm = 0,
|
||||
.fromIsr = TT_IS_ISR(),
|
||||
.fromIsr = kernel::isIsr(),
|
||||
.kernelRunning = (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING)
|
||||
};
|
||||
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
#include "./Power.h"
|
||||
#include "./SdCard.h"
|
||||
#include "./i2c/I2c.h"
|
||||
#include "Tactility/hal/spi/Spi.h"
|
||||
|
||||
namespace tt::hal {
|
||||
|
||||
@ -23,41 +24,26 @@ struct Configuration {
|
||||
*/
|
||||
const InitBoot _Nullable initBoot = nullptr;
|
||||
|
||||
/**
|
||||
* Called after I2C/SPI/etc is initialized.
|
||||
* This can be used to communicate with built-in peripherals such as an I2C keyboard.
|
||||
*/
|
||||
const InitHardware _Nullable initHardware = nullptr;
|
||||
|
||||
/**
|
||||
* Create and initialize all LVGL devices. (e.g. display, touch, keyboard)
|
||||
*/
|
||||
/** Create and initialize all LVGL devices. (e.g. display, touch, keyboard) */
|
||||
const InitLvgl _Nullable initLvgl = nullptr;
|
||||
|
||||
/**
|
||||
* Display HAL functionality.
|
||||
*/
|
||||
/** Display HAL functionality. */
|
||||
const CreateDisplay _Nullable createDisplay = nullptr;
|
||||
|
||||
/**
|
||||
* Display HAL functionality.
|
||||
*/
|
||||
/** Display HAL functionality. */
|
||||
const CreateKeyboard _Nullable createKeyboard = nullptr;
|
||||
|
||||
/**
|
||||
* An optional SD card interface.
|
||||
*/
|
||||
/** An optional SD card interface. */
|
||||
const std::shared_ptr<SdCard> _Nullable sdcard = nullptr;
|
||||
|
||||
/**
|
||||
* An optional power interface for battery or other power delivery.
|
||||
*/
|
||||
/** An optional power interface for battery or other power delivery. */
|
||||
const CreatePower _Nullable power = nullptr;
|
||||
|
||||
/**
|
||||
* A list of i2c interfaces
|
||||
*/
|
||||
/** A list of I2C interfaces */
|
||||
const std::vector<i2c::Configuration> i2c = {};
|
||||
|
||||
/** A list of SPI interfaces */
|
||||
const std::vector<spi::Configuration> spi = {};
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
@ -38,6 +38,7 @@ enum class Status {
|
||||
|
||||
bool init(const std::vector<i2c::Configuration>& configurations);
|
||||
|
||||
bool configure(i2c_port_t port, const i2c_config_t& configuration);
|
||||
bool start(i2c_port_t port);
|
||||
bool stop(i2c_port_t port);
|
||||
bool isStarted(i2c_port_t port);
|
||||
|
||||
48
TactilityHeadless/Include/Tactility/hal/spi/Spi.h
Normal file
48
TactilityHeadless/Include/Tactility/hal/spi/Spi.h
Normal file
@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include "SpiCompat.h"
|
||||
|
||||
#include <Tactility/Lockable.h>
|
||||
#include <Tactility/RtosCompat.h>
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace tt::hal::spi {
|
||||
|
||||
enum class InitMode {
|
||||
ByTactility, // Tactility will initialize it in the correct bootup phase
|
||||
ByExternal, // The device is already initialized and Tactility should assume it works
|
||||
Disabled // Not initialized by default
|
||||
};
|
||||
|
||||
struct Configuration {
|
||||
spi_host_device_t device;
|
||||
spi_common_dma_t dma;
|
||||
spi_bus_config_t config;
|
||||
/** Whether this bus should be initialized when device starts up */
|
||||
InitMode initMode;
|
||||
/** Whether this bus can stopped and re-started. */
|
||||
bool canReinit;
|
||||
/** Whether configuration can be changed. */
|
||||
bool hasMutableConfiguration;
|
||||
/** Optional custom lock */
|
||||
std::shared_ptr<Lockable> _Nullable lock;
|
||||
};
|
||||
|
||||
enum class Status {
|
||||
Started,
|
||||
Stopped,
|
||||
Unknown
|
||||
};
|
||||
|
||||
bool init(const std::vector<spi::Configuration>& configurations);
|
||||
|
||||
bool start(spi_host_device_t device);
|
||||
bool stop(spi_host_device_t device);
|
||||
bool isStarted(spi_host_device_t device);
|
||||
|
||||
bool lock(spi_host_device_t device, TickType_t timeout = 10 / portTICK_PERIOD_MS);
|
||||
bool unlock(spi_host_device_t device);
|
||||
|
||||
} // namespace tt::hal::spi
|
||||
58
TactilityHeadless/Include/Tactility/hal/spi/SpiCompat.h
Normal file
58
TactilityHeadless/Include/Tactility/hal/spi/SpiCompat.h
Normal file
@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include <driver/spi_common.h>
|
||||
#else
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
enum spi_host_device_t {
|
||||
SPI1_HOST = 0,
|
||||
SPI2_HOST = 1,
|
||||
SPI3_HOST = 2,
|
||||
SPI_HOST_MAX,
|
||||
};
|
||||
|
||||
enum spi_common_dma_t {
|
||||
SPI_DMA_DISABLED = 0, ///< No DMA
|
||||
SPI_DMA_CH1 = 1, ///< DMA, select DMA Channel 1
|
||||
SPI_DMA_CH2 = 2, ///< DMA, select DMA Channel 2
|
||||
SPI_DMA_CH_AUTO = 3, ///< DMA, channel selected by driver
|
||||
};
|
||||
|
||||
enum esp_intr_cpu_affinity_t {
|
||||
ESP_INTR_CPU_AFFINITY_AUTO,
|
||||
ESP_INTR_CPU_AFFINITY_0,
|
||||
ESP_INTR_CPU_AFFINITY_1,
|
||||
};
|
||||
|
||||
struct spi_bus_config_t {
|
||||
union {
|
||||
int mosi_io_num;
|
||||
int data0_io_num;
|
||||
};
|
||||
union {
|
||||
int miso_io_num;
|
||||
int data1_io_num;
|
||||
};
|
||||
int sclk_io_num;
|
||||
union {
|
||||
int quadwp_io_num;
|
||||
int data2_io_num;
|
||||
};
|
||||
union {
|
||||
int quadhd_io_num;
|
||||
int data3_io_num;
|
||||
};
|
||||
int data4_io_num;
|
||||
int data5_io_num;
|
||||
int data6_io_num;
|
||||
int data7_io_num;
|
||||
bool data_io_default_level;
|
||||
int max_transfer_sz;
|
||||
uint32_t flags;
|
||||
esp_intr_cpu_affinity_t isr_cpu_id;
|
||||
int intr_flags;
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -9,6 +9,8 @@ enum class SystemEvent {
|
||||
BootInitHalEnd,
|
||||
BootInitI2cBegin,
|
||||
BootInitI2cEnd,
|
||||
BootInitSpiBegin,
|
||||
BootInitSpiEnd,
|
||||
BootInitLvglBegin,
|
||||
BootInitLvglEnd,
|
||||
BootSplash,
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#include "Tactility/hal/Device.h"
|
||||
#include "Tactility/hal/Hal_i.h"
|
||||
#include "Tactility/hal/i2c/I2c.h"
|
||||
#include "Tactility/hal/spi/Spi.h"
|
||||
|
||||
#include <Tactility/kernel/SystemEvents.h>
|
||||
|
||||
@ -13,12 +14,12 @@ void init(const Configuration& configuration) {
|
||||
|
||||
kernel::systemEventPublish(kernel::SystemEvent::BootInitI2cBegin);
|
||||
tt_check(i2c::init(configuration.i2c), "I2C init failed");
|
||||
if (configuration.initHardware != nullptr) {
|
||||
TT_LOG_I(TAG, "Init hardware");
|
||||
tt_check(configuration.initHardware(), "Hardware init failed");
|
||||
}
|
||||
kernel::systemEventPublish(kernel::SystemEvent::BootInitI2cEnd);
|
||||
|
||||
kernel::systemEventPublish(kernel::SystemEvent::BootInitSpiBegin);
|
||||
tt_check(spi::init(configuration.spi), "SPI init failed");
|
||||
kernel::systemEventPublish(kernel::SystemEvent::BootInitSpiEnd);
|
||||
|
||||
if (configuration.initBoot != nullptr) {
|
||||
TT_LOG_I(TAG, "Init power");
|
||||
tt_check(configuration.initBoot(), "Init power failed");
|
||||
|
||||
@ -7,22 +7,21 @@
|
||||
|
||||
#include <esp_check.h>
|
||||
|
||||
#define TAG "i2c"
|
||||
|
||||
namespace tt::hal::i2c {
|
||||
|
||||
static const uint8_t ACK_CHECK_EN = 1;
|
||||
|
||||
typedef struct Data {
|
||||
struct Data {
|
||||
Mutex mutex;
|
||||
bool isConfigured = false;
|
||||
bool isStarted = false;
|
||||
Configuration configuration;
|
||||
} Data;
|
||||
};
|
||||
|
||||
static const uint8_t ACK_CHECK_EN = 1;
|
||||
static Data dataArray[I2C_NUM_MAX];
|
||||
|
||||
#define TAG "i2c"
|
||||
|
||||
const char* initModeToString(InitMode mode) {
|
||||
static const char* initModeToString(InitMode mode) {
|
||||
switch (mode) {
|
||||
using enum InitMode;
|
||||
case ByTactility:
|
||||
@ -35,7 +34,7 @@ const char* initModeToString(InitMode mode) {
|
||||
tt_crash("not implemented");
|
||||
}
|
||||
|
||||
void printInfo(const Data& data) {
|
||||
static void printInfo(const Data& data) {
|
||||
TT_LOG_V(TAG, "I2C info for port %d", data.configuration.port);
|
||||
TT_LOG_V(TAG, " isStarted: %d", data.isStarted);
|
||||
TT_LOG_V(TAG, " isConfigured: %d", data.isConfigured);
|
||||
@ -76,13 +75,13 @@ static bool configureLocked(i2c_port_t port, const i2c_config_t& configuration)
|
||||
Data& data = dataArray[port];
|
||||
if (data.isStarted) {
|
||||
TT_LOG_E(TAG, "(%d) Cannot reconfigure while interface is started", port);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
return false;
|
||||
} else if (!data.configuration.hasMutableConfiguration) {
|
||||
TT_LOG_E(TAG, "(%d) Mutation not allowed by original configuration", port);
|
||||
return ESP_ERR_NOT_ALLOWED;
|
||||
return false;
|
||||
} else {
|
||||
data.configuration.config = configuration;
|
||||
return ESP_OK;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -52,7 +52,7 @@ static bool configureLocked(i2c_port_t port, const i2c_config_t& configuration)
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t configure(i2c_port_t port, const i2c_config_t& configuration) {
|
||||
bool configure(i2c_port_t port, const i2c_config_t& configuration) {
|
||||
lock(port);
|
||||
bool result = configureLocked(port, configuration);
|
||||
unlock(port);
|
||||
|
||||
205
TactilityHeadless/Source/hal/spi/Spi.cpp
Normal file
205
TactilityHeadless/Source/hal/spi/Spi.cpp
Normal file
@ -0,0 +1,205 @@
|
||||
#include "Tactility/hal/spi/Spi.h"
|
||||
|
||||
#include <Tactility/Mutex.h>
|
||||
|
||||
#define TAG "spi"
|
||||
|
||||
namespace tt::hal::spi {
|
||||
|
||||
struct Data {
|
||||
std::shared_ptr<Lockable> lock;
|
||||
bool isConfigured = false;
|
||||
bool isStarted = false;
|
||||
Configuration configuration;
|
||||
};
|
||||
|
||||
static Data dataArray[SPI_HOST_MAX];
|
||||
|
||||
static const char* initModeToString(InitMode mode) {
|
||||
switch (mode) {
|
||||
using enum InitMode;
|
||||
case ByTactility:
|
||||
return TT_STRINGIFY(InitMode::ByTactility);
|
||||
case ByExternal:
|
||||
return TT_STRINGIFY(InitMode::ByExternal);
|
||||
case Disabled:
|
||||
return TT_STRINGIFY(InitMode::Disabled);
|
||||
}
|
||||
tt_crash("not implemented");
|
||||
}
|
||||
|
||||
static void printInfo(const Data& data) {
|
||||
TT_LOG_V(TAG, "SPI info for device %d", data.configuration.device);
|
||||
TT_LOG_V(TAG, " isStarted: %d", data.isStarted);
|
||||
TT_LOG_V(TAG, " isConfigured: %d", data.isConfigured);
|
||||
TT_LOG_V(TAG, " initMode: %s", initModeToString(data.configuration.initMode));
|
||||
TT_LOG_V(TAG, " canReinit: %d", data.configuration.canReinit);
|
||||
TT_LOG_V(TAG, " hasMutableConfiguration: %d", data.configuration.hasMutableConfiguration);
|
||||
TT_LOG_V(TAG, " MISO pin: %d", data.configuration.config.miso_io_num);
|
||||
TT_LOG_V(TAG, " MOSI pin: %d", data.configuration.config.mosi_io_num);
|
||||
TT_LOG_V(TAG, " SCLK pin: %d", data.configuration.config.sclk_io_num);
|
||||
}
|
||||
|
||||
bool init(const std::vector<spi::Configuration>& configurations) {
|
||||
TT_LOG_I(TAG, "Init");
|
||||
for (const auto& configuration: configurations) {
|
||||
Data& data = dataArray[configuration.device];
|
||||
data.configuration = configuration;
|
||||
data.isConfigured = true;
|
||||
if (configuration.lock != nullptr) {
|
||||
data.lock = configuration.lock;
|
||||
} else {
|
||||
data.lock = std::make_shared<Mutex>();
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& config: configurations) {
|
||||
printInfo(dataArray[config.device]);
|
||||
if (config.initMode == InitMode::ByTactility) {
|
||||
if (!start(config.device)) {
|
||||
return false;
|
||||
}
|
||||
} else if (config.initMode == InitMode::ByExternal) {
|
||||
dataArray[config.device].isStarted = true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool configureLocked(spi_host_device_t device, const spi_bus_config_t& configuration) {
|
||||
Data& data = dataArray[device];
|
||||
if (data.isStarted) {
|
||||
TT_LOG_E(TAG, "(%d) Cannot reconfigure while interface is started", device);
|
||||
return false;
|
||||
} else if (!data.configuration.hasMutableConfiguration) {
|
||||
TT_LOG_E(TAG, "(%d) Mutation not allowed by original configuration", device);
|
||||
return false;
|
||||
} else {
|
||||
data.configuration.config = configuration;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool configure(spi_host_device_t device, const spi_bus_config_t& configuration) {
|
||||
if (lock(device)) {
|
||||
bool result = configureLocked(device, configuration);
|
||||
unlock(device);
|
||||
return result;
|
||||
} else {
|
||||
TT_LOG_E(TAG, "(%d) Mutex timeout", device);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool startLocked(spi_host_device_t device) {
|
||||
Data& data = dataArray[device];
|
||||
printInfo(data);
|
||||
|
||||
if (data.isStarted) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Already started", device);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!data.isConfigured) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Not configured", device);
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
Configuration& config = data.configuration;
|
||||
auto result = spi_bus_initialize(device, &data.configuration.config, data.configuration.dma);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Failed to initialize: %s", device, esp_err_to_name(result));
|
||||
return false;
|
||||
} else {
|
||||
data.isStarted = true;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
data.isStarted = true;
|
||||
|
||||
#endif
|
||||
|
||||
TT_LOG_I(TAG, "(%d) Started", device);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool start(spi_host_device_t device) {
|
||||
if (lock(device)) {
|
||||
bool result = startLocked(device);
|
||||
unlock(device);
|
||||
return result;
|
||||
} else {
|
||||
TT_LOG_E(TAG, "(%d) Mutex timeout", device);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool stopLocked(spi_host_device_t device) {
|
||||
Data& data = dataArray[device];
|
||||
Configuration& config = data.configuration;
|
||||
|
||||
if (!config.canReinit) {
|
||||
TT_LOG_E(TAG, "(%d) Stopping: Not allowed to re-init", device);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!data.isStarted) {
|
||||
TT_LOG_E(TAG, "(%d) Stopping: Not started", device);
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
auto result = spi_bus_free(device);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Stopping: Failed to free device: %s", device, esp_err_to_name(result));
|
||||
return false;
|
||||
} else {
|
||||
data.isStarted = false;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
data.isStarted = false;
|
||||
|
||||
#endif
|
||||
|
||||
TT_LOG_I(TAG, "(%d) Stopped", device);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool stop(spi_host_device_t device) {
|
||||
if (lock(device)) {
|
||||
bool result = stopLocked(device);
|
||||
unlock(device);
|
||||
return result;
|
||||
} else {
|
||||
TT_LOG_E(TAG, "(%d) Mutex timeout", device);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool isStarted(spi_host_device_t device) {
|
||||
if (lock(device, 50 / portTICK_PERIOD_MS)) {
|
||||
bool started = dataArray[device].isStarted;
|
||||
unlock(device);
|
||||
return started;
|
||||
} else {
|
||||
// If we can't get a lock, we assume the device is busy and thus has started
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool lock(spi_host_device_t device, TickType_t timeout) {
|
||||
return dataArray[device].lock->lock(timeout);
|
||||
}
|
||||
|
||||
bool unlock(spi_host_device_t device) {
|
||||
return dataArray[device].lock->unlock();
|
||||
}
|
||||
|
||||
}
|
||||
@ -30,6 +30,10 @@ static const char* getEventName(SystemEvent event) {
|
||||
return TT_STRINGIFY(BootInitI2cBegin);
|
||||
case BootInitI2cEnd:
|
||||
return TT_STRINGIFY(BootInitI2cEnd);
|
||||
case BootInitSpiBegin:
|
||||
return TT_STRINGIFY(BootInitSpiBegin);
|
||||
case BootInitSpiEnd:
|
||||
return TT_STRINGIFY(BootInitSpiEnd);
|
||||
case BootInitLvglBegin:
|
||||
return TT_STRINGIFY(BootInitLvglBegin);
|
||||
case BootInitLvglEnd:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user