I03 motor HAL shared across boards #14

Merged
gronod merged 2 commits from feature/integration-I03 into develop 2026-09-16 22:13:27 +01:00
4 changed files with 36 additions and 16 deletions
+4 -1
View File
@@ -3,8 +3,11 @@ set(priv_inc)
set(priv_req driver)
if(IDF_TARGET STREQUAL "esp32")
list(APPEND srcs wroom/hal_motor.c)
list(APPEND srcs hal_motor.c)
list(APPEND priv_req board_wroom)
elseif(IDF_TARGET STREQUAL "esp32s3")
list(APPEND srcs hal_motor.c)
list(APPEND priv_req board_jc4827w543)
else()
list(APPEND srcs stub/hal_motor.c)
endif()
@@ -10,9 +10,9 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define PIN_STEP 12
#define PIN_DIR 14
#define PIN_EN 27
#include "board.h"
#include "hal_motor.h"
#define STEPS_PER_REV 4800
#define DEFAULT_RPM 60
#define ACCEL 9600
@@ -23,6 +23,10 @@
static const char *TAG = "motor";
static gpio_num_t s_pin_en = GPIO_NUM_NC;
static gpio_num_t s_pin_dir = GPIO_NUM_NC;
static gpio_num_t s_pin_step = GPIO_NUM_NC;
static int s_en_disable_level = 1;
static TaskHandle_t s_task;
static atomic_uint s_stop_req;
static float s_cw;
@@ -35,13 +39,17 @@ static rmt_encoder_handle_t s_enc;
static void en_disable(void)
{
gpio_set_level((gpio_num_t)PIN_EN, 1);
if (s_pin_en != GPIO_NUM_NC) {
gpio_set_level(s_pin_en, s_en_disable_level);
}
s_enabled = false;
}
static void en_enable(void)
{
gpio_set_level((gpio_num_t)PIN_EN, 0);
if (s_pin_en != GPIO_NUM_NC) {
gpio_set_level(s_pin_en, !s_en_disable_level);
}
s_enabled = true;
}
@@ -111,7 +119,7 @@ static bool run_move(long steps)
if (steps < 0) {
steps = -steps;
}
gpio_set_level((gpio_num_t)PIN_DIR, dir);
gpio_set_level(s_pin_dir, dir);
uint32_t cruise = (s_rpm * STEPS_PER_REV) / 60;
if (cruise == 0) {
@@ -189,22 +197,31 @@ void hal_motor_init(void)
if (s_inited) {
return;
}
s_pin_en = board_pin_motor_en();
s_pin_dir = board_pin_motor_dir();
s_pin_step = board_pin_motor_step();
s_en_disable_level = board_motor_en_disable_level();
if (s_pin_en == GPIO_NUM_NC || s_pin_dir == GPIO_NUM_NC || s_pin_step == GPIO_NUM_NC) {
ESP_LOGW(TAG, "no motor pins on this board");
s_inited = true;
return;
}
gpio_config_t io = {
.pin_bit_mask = (1ULL << PIN_EN) | (1ULL << PIN_DIR),
.pin_bit_mask = (1ULL << s_pin_en) | (1ULL << s_pin_dir),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};
gpio_config(&io);
gpio_set_level((gpio_num_t)PIN_EN, 1);
gpio_set_level((gpio_num_t)PIN_DIR, 0);
gpio_set_level(s_pin_en, s_en_disable_level);
gpio_set_level(s_pin_dir, 0);
s_enabled = false;
atomic_store(&s_stop_req, 0);
rmt_tx_channel_config_t txcfg = {
.clk_src = RMT_CLK_SRC_DEFAULT,
.gpio_num = PIN_STEP,
.gpio_num = s_pin_step,
.mem_block_symbols = 64,
.resolution_hz = RMT_RES_HZ,
.trans_queue_depth = 4,
@@ -219,7 +236,8 @@ void hal_motor_init(void)
configMAX_PRIORITIES - 2, &s_task, 0);
}
s_inited = true;
ESP_LOGI(TAG, "init EN=HIGH RMT step=%d dir=%d", PIN_STEP, PIN_DIR);
ESP_LOGI(TAG, "init en=%d step=%d dir=%d dis_lvl=%d",
s_pin_en, s_pin_step, s_pin_dir, s_en_disable_level);
}
void hal_motor_enable(bool on)
@@ -234,8 +252,7 @@ void hal_motor_enable(bool on)
void hal_motor_request_stop(void)
{
atomic_store(&s_stop_req, 1);
gpio_set_level((gpio_num_t)PIN_EN, 1);
s_enabled = false;
en_disable();
rmt_abort();
if (s_task != NULL) {
xTaskNotifyGive(s_task);
+1 -1
View File
@@ -37,5 +37,5 @@ If blocked: stop, commit nothing broken, write `BLOCKED:` at top of the phase fi
| --- | --- | --- | --- |
| I01 | `integration/I01-board-configs.md` | Board pin allocations & accessors | DONE |
| I02 | `integration/I02-temp-fix.md` | Temp HAL shared & starvation fix | DONE |
| I03 | `integration/I03-motor-hal.md` | Motor HAL shared across boards | TODO |
| I03 | `integration/I03-motor-hal.md` | Motor HAL shared across boards | DONE |
| I04 | `integration/I04-audio-i2s.md` | I2S audio implementation for S3 | TODO |
+1 -1
View File
@@ -1,4 +1,4 @@
STATUS: TODO
STATUS: DONE
DEPENDS: I01
**READ:** `docs/megaplans/INTEGRATION-MEGAPLAN.md`, `components/hal_motor/*`