Skip to content

Commit ae0b215

Browse files
committed
ok i think this works
1 parent 8f93fd2 commit ae0b215

File tree

12 files changed

+306
-285
lines changed

12 files changed

+306
-285
lines changed

stm32-modules/flex-stacker/firmware/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ set(GDBINIT_PATH "${CMAKE_CURRENT_BINARY_DIR}/../../common/STM32G491/gdbinit")
1717
set(${TARGET_MODULE_NAME}_FW_LINTABLE_SRCS
1818
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
1919
${SYSTEM_DIR}/freertos_idle_timer_task.cpp
20-
${MOTOR_CONTROL_DIR}/freertos_motor_driver_task.cpp
2120
${MOTOR_CONTROL_DIR}/freertos_motor_task.cpp
22-
${MOTOR_CONTROL_DIR}/motor_policy.cpp
21+
${MOTOR_CONTROL_DIR}/freertos_motor_driver_task.cpp
22+
${MOTOR_CONTROL_DIR}/motor_driver_policy.cpp
2323
)
2424

2525
# Add source files that should NOT be checked by clang-tidy here

stm32-modules/flex-stacker/firmware/main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ static auto aggregator = tasks::FirmwareTasks::QueueAggregator();
1717

1818
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
1919
static auto driver_task =
20-
ot_utils::freertos_task::FreeRTOSTask<tasks::MOTOR_DRIVER_STACK_SIZE, EntryPoint>(
21-
motor_driver_task_entry);
20+
ot_utils::freertos_task::FreeRTOSTask<tasks::MOTOR_DRIVER_STACK_SIZE,
21+
EntryPoint>(motor_driver_task_entry);
2222
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
2323
static auto motor_task =
2424
ot_utils::freertos_task::FreeRTOSTask<tasks::MOTOR_STACK_SIZE, EntryPoint>(
2525
motor_task_entry);
2626

2727
auto main() -> int {
2828
HardwareInit();
29-
driver_task.start(tasks::MOTOR_DRIVER_TASK_PRIORITY, "Motor Driver", &aggregator);
29+
driver_task.start(tasks::MOTOR_DRIVER_TASK_PRIORITY, "Motor Driver",
30+
&aggregator);
3031
motor_task.start(tasks::MOTOR_TASK_PRIORITY, "Motor", &aggregator);
3132

3233
vTaskStartScheduler();

stm32-modules/flex-stacker/firmware/motor_control/freertos_motor_driver_task.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
#include "firmware/freertos_tasks.hpp"
2-
31
#include "FreeRTOS.h"
4-
#include "firmware/motor_hardware.h"
2+
#include "firmware/freertos_tasks.hpp"
53
#include "firmware/motor_driver_policy.hpp"
4+
#include "firmware/motor_hardware.h"
65
#include "flex-stacker/motor_driver_task.hpp"
76
#include "ot_utils/freertos/freertos_timer.hpp"
87

stm32-modules/flex-stacker/firmware/motor_control/freertos_motor_task.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#include "firmware/freertos_tasks.hpp"
2-
31
#include "FreeRTOS.h"
2+
#include "firmware/freertos_tasks.hpp"
43
#include "firmware/motor_hardware.h"
54
#include "firmware/motor_policy.hpp"
65
#include "flex-stacker/motor_task.hpp"
@@ -27,7 +26,7 @@ auto run(tasks::FirmwareTasks::QueueAggregator* aggregator) -> void {
2726
aggregator->register_queue(_queue);
2827
_top_task.provide_aggregator(aggregator);
2928

30-
// motor_hardware_init();
29+
// motor_hardware_init();
3130

3231
// auto policy = motor_policy::MotorPolicy();
3332
while (true) {

stm32-modules/flex-stacker/firmware/motor_control/motor_driver_policy.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
#include "firmware/motor_hardware.h"
44

5+
using namespace motor_driver_policy;
56

67
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
7-
auto MotorDriverPolicy::tmc2160_transmit_receive(MotorID motor_id, tmc2160::MessageT& data) -> RxTxReturn {
8+
auto MotorDriverPolicy::tmc2160_transmit_receive(MotorID motor_id,
9+
tmc2160::MessageT& data)
10+
-> RxTxReturn {
811
tmc2160::MessageT retBuf = {0};
9-
if (spi_dma_transmit_receive(motor_id, data.data(), retBuf.data(), data.size())) {
12+
if (spi_dma_transmit_receive(motor_id, data.data(), retBuf.data(),
13+
data.size())) {
1014
return RxTxReturn(retBuf);
1115
}
1216
return RxTxReturn();

stm32-modules/flex-stacker/firmware/motor_control/motor_spi_hardware.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ struct motor_spi_hardware {
3939
};
4040

4141
static void spi_interrupt_service(void);
42-
static void spi_set_nss(bool selected);
42+
static void enable_spi_nss(MotorID motor);
43+
static void disable_spi_nss(void);
4344

4445

4546
DMA_HandleTypeDef hdma_spi2_rx;
@@ -284,11 +285,11 @@ void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
284285
spi_interrupt_service();
285286
}
286287

287-
bool tmc2160_transmit_receive(
288+
bool spi_dma_transmit_receive(
288289
MotorID motor_id, uint8_t *txData, uint8_t *rxData, uint16_t size
289290
) {
290291
const TickType_t max_block_time = pdMS_TO_TICKS(100);
291-
HAL_StatusTypeDef ret;
292+
// HAL_StatusTypeDef ret;
292293
uint32_t notification_val = 0;
293294

294295
if (!_spi.initialized || (_spi.task_to_notify != NULL) || (size > MOTOR_MAX_SPI_LEN)) {

stm32-modules/include/flex-stacker/firmware/motor_driver_policy.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
#include <optional>
44

5+
#include "flex-stacker/tmc2160_interface.hpp"
56
#include "systemwide.h"
6-
#include "flex-stacker/tmc2160.hpp"
77

88
namespace motor_driver_policy {
9+
910
class MotorDriverPolicy {
1011
public:
1112
using RxTxReturn = std::optional<tmc2160::MessageT>;
12-
auto tmc2160_transmit_receive(MotorID motor_id, tmc2160::MessageT& data) -> RxTxReturn;
13+
auto tmc2160_transmit_receive(MotorID motor_id, tmc2160::MessageT& data)
14+
-> RxTxReturn;
1315
};
1416

1517
} // namespace motor_driver_policy

stm32-modules/include/flex-stacker/flex-stacker/messages.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ using SystemMessage =
127127
::std::variant<std::monostate, AcknowledgePrevious, GetSystemInfoMessage,
128128
SetSerialNumberMessage, EnterBootloaderMessage>;
129129

130-
using MotorDriverMessage = ::std::variant<std::monostate, SetMotorDriverRegister,
131-
GetMotorDriverRegister,
132-
PollMotorDriverRegister,
133-
StopPollingMotorDriverRegister>;
130+
using MotorDriverMessage =
131+
::std::variant<std::monostate, SetMotorDriverRegister,
132+
GetMotorDriverRegister, PollMotorDriverRegister,
133+
StopPollingMotorDriverRegister>;
134134
using MotorMessage = ::std::variant<std::monostate>;
135135

136136
}; // namespace messages

stm32-modules/include/flex-stacker/flex-stacker/motor_driver_task.hpp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*/
66
#pragma once
77

8+
#include <optional>
89

9-
#include "systemwide.h"
1010
#include "core/ack_cache.hpp"
1111
#include "core/queue_aggregator.hpp"
1212
#include "core/version.hpp"
@@ -17,13 +17,12 @@
1717
#include "flex-stacker/tmc2160_interface.hpp"
1818
#include "flex-stacker/tmc2160_registers.hpp"
1919
#include "hal/message_queue.hpp"
20+
#include "systemwide.h"
2021

2122
namespace motor_driver_task {
2223

23-
2424
using Message = messages::MotorDriverMessage;
2525

26-
2726
static constexpr tmc2160::TMC2160RegisterMap motor_z_config{
2827
.gconfig = {.diag0_error = 1, .diag1_stall = 1},
2928
.glob_scale = {.global_scaler = 0x8B},
@@ -70,7 +69,7 @@ static constexpr tmc2160::TMC2160RegisterMap motor_x_config{
7069
.pwm_lim = 0xC},
7170
};
7271

73-
constexpr tmc2160::TMC2160RegisterMap motor_l_config{
72+
static constexpr tmc2160::TMC2160RegisterMap motor_l_config{
7473
.gconfig = {.diag0_error = 1, .diag1_stall = 1},
7574
.glob_scale = {.global_scaler = 0x8B},
7675
.ihold_irun = {.hold_current = 1,
@@ -107,7 +106,8 @@ class MotorDriverTask {
107106
MotorDriverTask(const MotorDriverTask& other) = delete;
108107
auto operator=(const MotorDriverTask& other) -> MotorDriverTask& = delete;
109108
MotorDriverTask(MotorDriverTask&& other) noexcept = delete;
110-
auto operator=(MotorDriverTask&& other) noexcept -> MotorDriverTask& = delete;
109+
auto operator=(MotorDriverTask&& other) noexcept
110+
-> MotorDriverTask& = delete;
111111
~MotorDriverTask() = default;
112112

113113
auto provide_aggregator(Aggregator* aggregator) {
@@ -120,16 +120,22 @@ class MotorDriverTask {
120120
return;
121121
}
122122
if (!_initialized) {
123-
124-
if (!_tmc2160.initialize_config(motor_z_config, tmc2160::TMC2160Interface(policy), MotorID::MOTOR_Z)) {
123+
if (!_tmc2160.hello()) {
125124
return;
126125
}
127-
if (!_tmc2160.initialize_config(motor_x_config, tmc2160::TMC2160Interface(policy), MotorID::MOTOR_X)) {
128-
return;
129-
}
130-
if (!_tmc2160.initialize_config(motor_l_config, tmc2160::TMC2160Interface(policy), MotorID::MOTOR_L)) {
126+
static tmc2160::TMC2160Interface<Policy> tmc2160_interface(policy);
127+
if (!_tmc2160.initialize_config(motor_z_config, tmc2160_interface,
128+
MotorID::MOTOR_Z)) {
131129
return;
132130
}
131+
// if (!_tmc2160.initialize_config(motor_x_config,
132+
// _tmc2160_interface, MotorID::MOTOR_X)) {
133+
// return;
134+
// }
135+
// if (!_tmc2160.initialize_config(motor_l_config,
136+
// _tmc2160_interface, MotorID::MOTOR_L)) {
137+
// return;
138+
// }
133139
_initialized = true;
134140
}
135141

@@ -147,31 +153,30 @@ class MotorDriverTask {
147153
static_cast<void>(message);
148154
}
149155

150-
151-
auto visit_message(const messages::SetMotorDriverRegister& message) -> void {
156+
auto visit_message(const messages::SetMotorDriverRegister& message)
157+
-> void {
152158
static_cast<void>(message);
153159
}
154160

155-
auto visit_message(const messages::GetMotorDriverRegister& message) -> void {
161+
auto visit_message(const messages::GetMotorDriverRegister& message)
162+
-> void {
156163
static_cast<void>(message);
157164
}
158165

159-
auto visit_message(const messages::PollMotorDriverRegister& message) -> void {
166+
auto visit_message(const messages::PollMotorDriverRegister& message)
167+
-> void {
160168
static_cast<void>(message);
161169
}
162170

163-
auto visit_message(const messages::StopPollingMotorDriverRegister& message) -> void {
171+
auto visit_message(const messages::StopPollingMotorDriverRegister& message)
172+
-> void {
164173
static_cast<void>(message);
165174
}
166175

167-
168-
169176
Queue& _message_queue;
170177
Aggregator* _task_registry;
171178
bool _initialized;
172179

173180
tmc2160::TMC2160 _tmc2160{};
174-
tmc2160::TMC2160Interface<Policy>* _tmc2160_interface;
175-
176181
};
177-
}; // namespace motor_task
182+
}; // namespace motor_driver_task

stm32-modules/include/flex-stacker/flex-stacker/tasks.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ struct Tasks {
1616
// Message queue for motor control task
1717
using MotorQueue = QueueImpl<messages::MotorMessage>;
1818

19-
2019
// Central aggregator
21-
using QueueAggregator = queue_aggregator::QueueAggregator<MotorDriverQueue, MotorQueue>;
20+
using QueueAggregator =
21+
queue_aggregator::QueueAggregator<MotorDriverQueue, MotorQueue>;
2222

2323
// Addresses
2424
static constexpr size_t MotorDriverAddress =

0 commit comments

Comments
 (0)