Skip to content

Commit 3b84911

Browse files
Feature/stepper overlay update (#4)
* Update device tree overlay and driver enable functions for stepper control * Update workflow triggers to only run on main branch for push events * Update device tree overlay for stepper control and adjust project configurations * Update device tree overlays and configurations for stepper control and UART handling * Refactor thread management and update device configuration structure for stepper and UART handling * Refactor focuser initialization to include firmware version and remove unused thread stack size configurations * Add setup action for Zephyr project and update build workflow to use it * Fix setup action path for Zephyr project in build workflow * Remove unnecessary path specification in checkout step of build workflow * Update build workflow to use correct paths for Zephyr project and firmware build * Fix toolchain specification format in Zephyr setup action * Update build workflow to use official Zephyr setup action and restrict OS matrix to Windows * Update app/boards/esp32s3_devkitc_procpu.overlay Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update tests/app/focuser/boards/qemu_cortex_m0.overlay Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update app/boards/esp32s3_devkitc_procpu.overlay Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 9c5cb93 commit 3b84911

22 files changed

Lines changed: 449 additions & 168 deletions

.github/workflows/build.yml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ on:
99
permissions:
1010
contents: read
1111

12+
env:
13+
ZEPHYR_SDK_VERSION: 0.17.4
14+
ZEPHYR_TOOLCHAINS: |
15+
arm-zephyr-eabi
16+
xtensa-espressif_esp32s3_zephyr-elf
17+
1218
jobs:
1319
build:
1420
name: Build app and tests
@@ -25,9 +31,10 @@ jobs:
2531
persist-credentials: false
2632

2733
- name: Set up Python
28-
uses: actions/setup-python@v5
34+
uses: actions/setup-python@v6
2935
with:
3036
python-version: 3.12
37+
cache: "pip"
3138

3239
- name: Install system dependencies
3340
shell: bash
@@ -47,21 +54,34 @@ jobs:
4754
fi
4855
4956
- name: Install west
57+
shell: bash
5058
run: |
5159
pip install west
5260
5361
- name: Initialize Zephyr workspace
62+
shell: bash
5463
run: |
5564
west init -l OpenAstroFocuser
5665
west update
5766
5867
- name: Install pip dependencies
68+
shell: bash
5969
run: |
6070
west packages pip --install --ignore-venv-check || pip3 install -r zephyr/scripts/requirements.txt
6171
72+
- name: Zephyr SDK cache
73+
id: cache
74+
uses: actions/cache@v5
75+
with:
76+
path: ~/zephyr-sdk-${{ env.ZEPHYR_SDK_VERSION }}
77+
key: zephyr-sdk-${{ env.ZEPHYR_SDK_VERSION }}-${{ matrix.os }}
78+
6279
- name: Install Zephyr SDK
80+
if: steps.cache.outputs.cache-hit != 'true'
81+
shell: bash
6382
run: |
64-
west sdk install -t arm-zephyr-eabi xtensa-espressif_esp32s3_zephyr-elf
83+
TOOLCHAINS="$(echo "${{ env.ZEPHYR_TOOLCHAINS }}" | xargs)"
84+
west sdk install --version ${{ env.ZEPHYR_SDK_VERSION }} -t ${TOOLCHAINS}
6585
6686
- name: Build firmware
6787
working-directory: OpenAstroFocuser

app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ target_sources(app PRIVATE
1515
src/FocuserThread.cpp
1616
src/UartHandler.cpp
1717
src/UartThread.cpp
18+
src/Thread.cpp
1819
src/ZephyrStepper.cpp)

app/Kconfig

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@ endmenu
1212

1313
menu "OpenAstroFocuser options"
1414

15-
config FOCUSER_THREAD_STACK_SIZE
16-
int "Focuser thread stack size"
17-
default 2048
18-
help
19-
Stack allocation used by the focuser control thread.
20-
21-
config SERIAL_THREAD_STACK_SIZE
22-
int "Serial thread stack size"
23-
default 2048
24-
help
25-
Stack allocation used by the Moonlite UART handler thread.
26-
2715
endmenu
2816

2917
module = APP

app/boards/esp32s3_devkitc_procpu.overlay

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@
44
*/
55

66
/* Devicetree overlay enabling the Moonlite focuser firmware on ESP32-S3 DevKitC
7-
* (PROCPU). The zephyr,gpio-step-dir-controller drives the step/dir pins
8-
* referenced by the "stepper" alias, while the dedicated TMC2209 device is
9-
* referenced by the "stepper-drv" alias so the application can toggle the
10-
* external driver rails. The zephyr,user node supplies the UART phandle
11-
* consumed by the application.
7+
* (PROCPU). Device configuration is performed using chosen nodes:
8+
* - focuser,uart: selects the UART device for the focuser console
9+
* - focuser,stepper: selects the step/dir controller for the stepper motor
10+
* - focuser,stepper-drv: selects the TMC2209 stepper driver device
11+
* These chosen nodes are used by the application to access the appropriate hardware.
1212
*/
1313

1414
/ {
15-
aliases {
16-
stepper = &focuser_stepper;
17-
stepper-drv = &focuser_stepper_drv;
15+
chosen {
16+
zephyr,console = &uart1;
17+
zephyr,log-uart = &focuser_log_uarts;
18+
focuser,uart = &uart0;
19+
focuser,stepper = &focuser_stepper;
20+
focuser,stepper-drv = &focuser_stepper_drv;
21+
};
22+
23+
focuser_log_uarts: focuser_log_uarts {
24+
compatible = "zephyr,log-uart";
25+
uarts = <&uart1>;
1826
};
1927

2028
focuser_stepper_drv: focuser_stepper_drv {
@@ -31,23 +39,30 @@
3139
step-width-ns = <10000>;
3240
status = "okay";
3341
};
34-
35-
zephyr,user {
36-
uart_handler = <&uart1>;
37-
};
3842
};
3943

44+
/**
45+
* Moonlite focuser console UART configuration
46+
*/
4047
&uart0 {
4148
current-speed = <9600>;
49+
status = "okay";
4250
};
4351

52+
/**
53+
* Moonlite focuser control UART configuration.
54+
* Used for logging and console output.
55+
*/
4456
&uart1 {
4557
status = "okay";
4658
current-speed = <9600>;
4759
pinctrl-0 = <&moonlite_uart1_default>;
4860
pinctrl-names = "default";
4961
};
5062

63+
/**
64+
* Pin control settings
65+
*/
5166
&pinctrl {
5267
moonlite_uart1_default: moonlite_uart1_default {
5368
group1 {

app/prj.conf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ CONFIG_MOONLITE=y
1010
# Provide heap storage for std::string and other dynamic allocations.
1111
CONFIG_HEAP_MEM_POOL_SIZE=4096
1212

13-
# Route logs and console I/O over UART for the focuser serial protocol.
13+
# Enable Serial/UART driver
1414
CONFIG_SERIAL=y
15+
CONFIG_UART_INTERRUPT_DRIVEN=y
16+
17+
# Enable Console (printk)
1518
CONFIG_CONSOLE=y
16-
CONFIG_STDOUT_CONSOLE=y
17-
CONFIG_PRINTK=y
1819
CONFIG_UART_CONSOLE=y
19-
CONFIG_UART_INTERRUPT_DRIVEN=y
20+
21+
# Enable Logging (LOG_INF, LOG_ERR)
2022
CONFIG_LOG=y
21-
CONFIG_LOG_MODE_IMMEDIATE=y
22-
CONFIG_LOG_BACKEND_UART=y
2323

2424
# Set the application log level.
2525
CONFIG_APP_LOG_LEVEL_INF=y

app/src/Configuration.hpp

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,47 @@
22

33
#include <zephyr/device.h>
44
#include <zephyr/kernel.h>
5+
#include <zephyr/sys/util.h>
6+
7+
#include <app_version.h>
58

69
#include <cstddef>
710

8-
#define FOCUSER_NODE DT_PATH(zephyr_user)
9-
#define STEPPER_ALIAS DT_ALIAS(stepper)
10-
#define STEPPER_DRV_ALIAS DT_ALIAS(stepper_drv)
11+
namespace config
12+
{
13+
constexpr const char version[] = STRINGIFY(APP_VERSION_MAJOR) STRINGIFY(APP_VERSION_MINOR);
1114

12-
#if !DT_NODE_EXISTS(FOCUSER_NODE)
13-
#error "zephyr,user node must provide focuser pin assignments"
14-
#endif
15+
namespace devices
16+
{
1517

16-
#if !DT_NODE_HAS_PROP(FOCUSER_NODE, uart_handler)
17-
#error "zephyr,user node requires uart_handler phandle"
18+
#if !DT_HAS_CHOSEN(focuser_uart)
19+
#error "UART device is required for Moonlite serial protocol"
20+
#else
21+
constexpr auto uart = DEVICE_DT_GET(DT_CHOSEN(focuser_uart));
1822
#endif
1923

20-
#if !DT_NODE_HAS_STATUS(STEPPER_ALIAS, okay)
21-
#error "stepper alias must reference an enabled stepper controller"
24+
#if !DT_HAS_CHOSEN(focuser_stepper)
25+
#error "Stepper device is required for Moonlite serial protocol"
26+
#else
27+
constexpr auto stepper = DEVICE_DT_GET(DT_CHOSEN(focuser_stepper));
2228
#endif
2329

24-
#if !DT_NODE_HAS_STATUS(STEPPER_DRV_ALIAS, okay)
25-
#error "stepper-drv alias must reference an enabled stepper driver"
30+
#if !DT_HAS_CHOSEN(focuser_stepper_drv)
31+
#error "Stepper driver device is required for Moonlite serial protocol"
32+
#else
33+
constexpr auto stepper_drv = DEVICE_DT_GET(DT_CHOSEN(focuser_stepper_drv));
2634
#endif
2735

28-
#if !DT_HAS_CHOSEN(zephyr_console)
29-
#error "Console device is required for Moonlite serial protocol"
30-
#endif
31-
32-
namespace config
33-
{
34-
35-
struct ThreadConfig
36-
{
37-
std::size_t stack_size;
38-
int priority;
39-
};
40-
41-
inline constexpr ThreadConfig kFocuserThread{CONFIG_FOCUSER_THREAD_STACK_SIZE, K_PRIO_PREEMPT(4)};
42-
inline constexpr ThreadConfig kSerialThread{CONFIG_SERIAL_THREAD_STACK_SIZE, K_PRIO_PREEMPT(5)};
36+
} // namespace devices
4337

44-
inline const struct device *console_device()
45-
{
46-
return DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
47-
}
48-
49-
inline const struct device *uart_handler_device()
50-
{
51-
return DEVICE_DT_GET(DT_PHANDLE(FOCUSER_NODE, uart_handler));
52-
}
53-
54-
inline const struct device *stepper_device()
55-
{
56-
return DEVICE_DT_GET(STEPPER_ALIAS);
57-
}
58-
59-
inline const struct device *stepper_driver_device()
60-
{
61-
return DEVICE_DT_GET(STEPPER_DRV_ALIAS);
62-
}
38+
namespace threads
39+
{
40+
constexpr auto focuser_priority = K_PRIO_PREEMPT(4);
41+
constexpr auto focuser_stack_size = K_THREAD_STACK_LEN(2048);
42+
inline k_thread_stack_t focuser_stack[focuser_stack_size];
6343

44+
constexpr auto serial_priority = K_PRIO_PREEMPT(5);
45+
constexpr auto serial_stack_size = K_THREAD_STACK_LEN(2048);
46+
inline k_thread_stack_t serial_stack[serial_stack_size];
47+
} // namespace threads
6448
} // namespace config

app/src/Focuser.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22

33
#include <zephyr/logging/log.h>
44

5-
#include <app_version.h>
6-
75
#include <errno.h>
86

97
LOG_MODULE_DECLARE(focuser, CONFIG_APP_LOG_LEVEL);
108

119
namespace
1210
{
13-
14-
constexpr const char kFirmwareVersion[] = STRINGIFY(APP_VERSION_MAJOR) STRINGIFY(APP_VERSION_MINOR);
15-
1611
uint32_t compute_step_period_us(uint8_t multiplier)
1712
{
1813
uint32_t m = (multiplier == 0U) ? 1U : static_cast<uint32_t>(multiplier);
@@ -27,8 +22,8 @@ namespace
2722

2823
} // namespace
2924

30-
Focuser::Focuser(FocuserStepper &stepper)
31-
: m_stepper(stepper)
25+
Focuser::Focuser(FocuserStepper &stepper, const char *firmware_version)
26+
: m_firmware_version(firmware_version), m_stepper(stepper)
3227
{
3328
(void)set_stepper_driver_enabled(true);
3429
}
@@ -242,8 +237,8 @@ bool Focuser::isMoving()
242237
std::string Focuser::getFirmwareVersion()
243238
{
244239
LOG_DBG("getFirmwareVersion()");
245-
LOG_DBG("getFirmwareVersion -> %s", kFirmwareVersion);
246-
return std::string(kFirmwareVersion);
240+
LOG_DBG("getFirmwareVersion -> %s", m_firmware_version);
241+
return std::string(m_firmware_version);
247242
}
248243

249244
uint8_t Focuser::getSpeed()

app/src/Focuser.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class Focuser final : public moonlite::Handler
1313
{
1414
public:
15-
explicit Focuser(FocuserStepper &stepper);
15+
explicit Focuser(FocuserStepper &stepper, const char *firmware_version);
1616

1717
int initialise();
1818
void loop();
@@ -71,6 +71,8 @@ class Focuser final : public moonlite::Handler
7171
int32_t read_actual_position();
7272
int set_stepper_driver_enabled(bool enable);
7373

74+
const char *m_firmware_version;
75+
7476
FocuserState m_state{};
7577
FocuserStepper &m_stepper;
7678
};

app/src/FocuserThread.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,25 @@
22

33
#include <zephyr/logging/log.h>
44

5+
#include "Configuration.hpp"
56
#include "Focuser.hpp"
67

78
LOG_MODULE_DECLARE(focuser);
89

910
FocuserThread::FocuserThread(Focuser &focuser)
10-
: m_focuser(focuser)
11+
: Thread(config::threads::focuser_stack,
12+
K_THREAD_STACK_SIZEOF(config::threads::focuser_stack),
13+
config::threads::focuser_priority, "focuser"),
14+
m_focuser(focuser)
1115
{
1216
}
1317

1418
void FocuserThread::start()
1519
{
16-
k_thread_create(&m_thread, m_stack, K_THREAD_STACK_SIZEOF(m_stack),
17-
thread_entry, this, nullptr, nullptr, config::kFocuserThread.priority,
18-
0, K_NO_WAIT);
19-
k_thread_name_set(&m_thread, "focuser");
20-
}
21-
22-
void FocuserThread::thread_entry(void *arg1, void *, void *)
23-
{
24-
auto *self = static_cast<FocuserThread *>(arg1);
25-
if (self == nullptr)
20+
if (!start_thread())
2621
{
27-
return;
22+
LOG_ERR("Failed to start focuser thread");
2823
}
29-
30-
self->run();
3124
}
3225

3326
void FocuserThread::run()

app/src/FocuserThread.hpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
#pragma once
22

3-
#include <zephyr/kernel.h>
4-
5-
#include <cstddef>
6-
7-
#include "Configuration.hpp"
3+
#include "Thread.hpp"
84

95
class Focuser;
106

11-
class FocuserThread {
7+
class FocuserThread : public Thread {
128
public:
139
explicit FocuserThread(Focuser &focuser);
1410

1511
void start();
1612

1713
private:
18-
static void thread_entry(void *, void *, void *);
19-
void run();
14+
void run() override;
2015

2116
Focuser &m_focuser;
22-
k_thread_stack_t m_stack[K_THREAD_STACK_LEN(config::kFocuserThread.stack_size)];
23-
struct k_thread m_thread;
2417
};

0 commit comments

Comments
 (0)