Skip to content

Commit

Permalink
Merge branch 'edge' into flex-stacker-tof-sensors-bringup
Browse files Browse the repository at this point in the history
  • Loading branch information
vegano1 committed Jan 3, 2025
2 parents 54577fe + ebea32f commit 91a2e8b
Show file tree
Hide file tree
Showing 54 changed files with 1,555 additions and 175 deletions.
3 changes: 3 additions & 0 deletions stm32-modules/flex-stacker/firmware/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ set(${TARGET_MODULE_NAME}_FW_LINTABLE_SRCS
${SYSTEM_DIR}/freertos_idle_timer_task.cpp
${SYSTEM_DIR}/freertos_system_task.cpp
${SYSTEM_DIR}/system_policy.cpp
${SYSTEM_DIR}/i2c_comms.cpp
${UI_DIR}/freertos_ui_task.cpp
${UI_DIR}/ui_policy.cpp
${MOTOR_CONTROL_DIR}/freertos_motor_task.cpp
${MOTOR_CONTROL_DIR}/freertos_motor_driver_task.cpp
${MOTOR_CONTROL_DIR}/motor_driver_policy.cpp
Expand All @@ -41,6 +43,7 @@ set(${TARGET_MODULE_NAME}_FW_NONLINTABLE_SRCS
${SYSTEM_DIR}/hal_tick.c
${SYSTEM_DIR}/system_serial_number.c
${SYSTEM_DIR}/system_hardware.c
${SYSTEM_DIR}/i2c_hardware.c
${UI_DIR}/ui_hardware.c
${COMMS_DIR}/usbd_conf.c
${COMMS_DIR}/usbd_desc.c
Expand Down
39 changes: 17 additions & 22 deletions stm32-modules/flex-stacker/firmware/host_comms_task/usbd_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,18 @@ uint8_t *USBD_Class_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *le
*/
static void Get_SerialNum(void)
{
uint32_t deviceserial0, deviceserial1, deviceserial2;
uint32_t sg_0, sg_1;
uint32_t pointer = USB_STRING_SERIAL_BASE;
// Serial number is stored in 6 32-bit words
for (uint8_t idx = 0; idx < 4; idx++) {
sg_0 = *(uint32_t *)(pointer + 4);
sg_1 = *(uint32_t *)(pointer);

deviceserial0 = *(uint32_t *)(UID_BASE+4);
deviceserial1 = *(uint32_t *)(UID_BASE+8);
deviceserial2 = *(uint32_t *)(UID_BASE+12);
if (sg_0 == 0U) break;

deviceserial0 += deviceserial2;

if (deviceserial0 != 0U)
{
IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8U);
IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4U);
IntToUnicode(sg_0, &USBD_StringSerial[2 + 16*idx], 4U);
IntToUnicode(sg_1, &USBD_StringSerial[10 + 16*idx], 4U);
pointer += 8;
}
}

Expand All @@ -259,20 +259,15 @@ static void IntToUnicode(uint32_t value, uint8_t *pbuf, uint8_t len)
{
uint8_t idx = 0U;

for (idx = 0U ; idx < len ; idx ++)
for (idx = 0U; idx < len; idx++)
{
if (((value >> 28)) < 0xAU)
{
pbuf[ 2U * idx] = (value >> 28) + '0';
}
else
{
pbuf[2U * idx] = (value >> 28) + 'A' - 10U;
}

value = value << 4;

// Extract the most significant byte
pbuf[2U * idx] = (value >> 24) & 0xFF;
// Add a null byte for Unicode (UTF-16)
pbuf[2U * idx + 1] = 0U;

// Shift the value to process the next byte
value = value << 8;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/

#define USB_SIZ_STRING_SERIAL 0x1A
#define USB_SIZ_STRING_SERIAL 0x32
#define USB_STRING_SERIAL_BASE 0x0807F800

/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
Expand Down
68 changes: 29 additions & 39 deletions stm32-modules/flex-stacker/firmware/main.cpp
Original file line number Diff line number Diff line change
@@ -1,69 +1,66 @@
#include "FreeRTOS.h"
#include "task.h"

#include "ot_utils/freertos/freertos_task.hpp"
#include "firmware/freertos_tasks.hpp"
#include "firmware/firmware_tasks.hpp"
#include "firmware/freertos_tasks.hpp"
#include "firmware/i2c_comms.hpp"
#include "firmware/i2c_hardware.h"
#include "firmware/motor_hardware.h"
#include "firmware/tof_sensor_hardware.h"
#include "firmware/system_stm32g4xx.h"
#include "flex-stacker/messages.hpp"
#include "ot_utils/freertos/freertos_task.hpp"
#include "systemwide.h"
#include "task.h"

#pragma GCC diagnostic push
// NOLINTNEXTLINE(clang-diagnostic-unknown-warning-option)
#pragma GCC diagnostic ignored "-Wvolatile"
#include "stm32g4xx_hal.h"
#pragma GCC diagnostic pop

// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)

// Task EntryPoint
using EntryPoint = std::function<void(tasks::FirmwareTasks::QueueAggregator *)>;
using EntryPointTOFDriver = std::function<void(
tasks::FirmwareTasks::QueueAggregator *, i2c::hardware::I2C *)>;
using EntryPointUI = std::function<void(tasks::FirmwareTasks::QueueAggregator *,
i2c::hardware::I2C *)>;

// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static auto motor_driver_task_entry = EntryPoint(motor_driver_task::run);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static auto motor_task_entry = EntryPoint(motor_control_task::run);
static auto ui_task_entry = EntryPoint(ui_control_task::run);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static auto ui_task_entry = EntryPointUI(ui_control_task::run);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static auto host_comms_entry = EntryPoint(host_comms_control_task::run);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static auto system_task_entry = EntryPoint(system_control_task::run);
static auto tof_driver_task_entry = EntryPointTOFDriver(tof_driver_task::run);
static auto tof_sensor_task_entry = EntryPoint(tof_sensor_task::run);

// Tasks
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static auto driver_task =
ot_utils::freertos_task::FreeRTOSTask<tasks::MOTOR_DRIVER_STACK_SIZE,
EntryPoint>(motor_driver_task_entry);

// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static auto motor_task =
ot_utils::freertos_task::FreeRTOSTask<tasks::MOTOR_STACK_SIZE, EntryPoint>(
motor_task_entry);

// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static auto host_comms_task =
ot_utils::freertos_task::FreeRTOSTask<tasks::COMMS_STACK_SIZE, EntryPoint>(
host_comms_entry);

// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static auto ui_task =
ot_utils::freertos_task::FreeRTOSTask<tasks::UI_STACK_SIZE, EntryPoint>(
ot_utils::freertos_task::FreeRTOSTask<tasks::UI_STACK_SIZE, EntryPointUI>(
ui_task_entry);

// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static auto system_task =
ot_utils::freertos_task::FreeRTOSTask<tasks::SYSTEM_STACK_SIZE, EntryPoint>(
system_task_entry);

static auto tof_d_task = ot_utils::freertos_task::FreeRTOSTask<
tasks::TOF_DRIVER_STACK_SIZE, EntryPointTOFDriver>(tof_driver_task_entry);

static auto tof_s_task =
ot_utils::freertos_task::FreeRTOSTask<tasks::TOF_SENSOR_STACK_SIZE,
EntryPoint>(tof_sensor_task_entry);

// Agregator
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static auto aggregator = tasks::FirmwareTasks::QueueAggregator();

// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static auto i2c2_comms = i2c::hardware::I2C();
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static auto i2c3_comms = i2c::hardware::I2C();
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static auto i2c_handles = I2CHandlerStruct{};

extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
switch (GPIO_Pin) {
Expand All @@ -77,27 +74,20 @@ extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
}
}

static auto i2c2_comms = i2c::hardware::I2C();
static auto i2c3_comms = i2c::hardware::I2C();
static auto i2c_handles = I2CHandlerStruct{};

auto main() -> int {
HardwareInit();

i2c_hardware_init(&i2c_handles);
i2c2_comms.set_handle(i2c_handles.i2c2);
i2c3_comms.set_handle(i2c_handles.i2c3);

i2c2_comms.set_handle(i2c_handles.i2c2, I2C_BUS_2);
i2c3_comms.set_handle(i2c_handles.i2c3, I2C_BUS_3);

system_task.start(tasks::SYSTEM_TASK_PRIORITY, "System", &aggregator);
driver_task.start(tasks::MOTOR_DRIVER_TASK_PRIORITY, "Motor Driver",
&aggregator);
motor_task.start(tasks::MOTOR_TASK_PRIORITY, "Motor", &aggregator);
tof_d_task.start(tasks::TOF_DRIVER_TASK_PRIORITY, "TOF Driver", &aggregator,
&i2c3_comms);
tof_s_task.start(tasks::TOF_SENSOR_TASK_PRIORITY, "TOF Sensor",
&aggregator);
host_comms_task.start(tasks::COMMS_TASK_PRIORITY, "Comms", &aggregator);
ui_task.start(tasks::UI_TASK_PRIORITY, "UI", &aggregator);
ui_task.start(tasks::UI_TASK_PRIORITY, "UI", &aggregator, &i2c2_comms);

vTaskStartScheduler();
return 0;
Expand Down
27 changes: 27 additions & 0 deletions stm32-modules/flex-stacker/firmware/system/i2c_comms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "firmware/i2c_comms.hpp"

#include <cstdint>

#include "firmware/i2c_hardware.h"
#include "systemwide.h"

using namespace i2c::hardware;

auto I2C::set_handle(HAL_I2C_HANDLE i2c_handle, I2C_BUS i2c_bus) -> void {
this->bus = i2c_bus;
i2c_register_handle(i2c_handle, i2c_bus);
}

auto I2C::i2c_write(uint16_t dev_addr, uint16_t reg, uint8_t* data,
uint16_t size) -> RxTxReturn {
MessageT resp{0};
auto ret = hal_i2c_write(bus, dev_addr, reg, data, size);
return RxTxReturn(ret, resp);
}

auto I2C::i2c_read(uint16_t dev_addr, uint16_t reg, uint16_t size)
-> RxTxReturn {
MessageT resp{0};
auto ret = hal_i2c_read(bus, dev_addr, reg, resp.data(), size);
return RxTxReturn(ret, resp);
}
Loading

0 comments on commit 91a2e8b

Please sign in to comment.