Skip to content

Commit

Permalink
Refactoring into modular structure with hierarchical CMakeLists.txt t…
Browse files Browse the repository at this point in the history
…o enable building of unit tests.
  • Loading branch information
devYaoYH committed Jan 28, 2025
1 parent ee53ace commit abdbf77
Show file tree
Hide file tree
Showing 48 changed files with 359 additions and 229 deletions.
50 changes: 13 additions & 37 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,27 @@ set(toolchainVersion 13_2_Rel1)
set(picotoolVersion 2.0.0)
include(${USERHOME}/.pico-sdk/cmake/pico-vscode.cmake)
# ====================================================================================
set(PICO_BOARD pico CACHE STRING "Board type")
set(TEST OFF)

if(DEFINED TEST)
add_definitions(-DTEST=1)
else()
add_definitions(-DTEST=0)
set(PICO_BOARD pico CACHE STRING "Board type")
endif()

# Pull in Raspberry Pi Pico SDK (must be before project)
include(pico-sdk/pico_sdk_init.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/pico-sdk/pico_sdk_init.cmake)

if (PICO_SDK_VERSION_STRING VERSION_LESS "1.4.0")
message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.4.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()

project(samwise C CXX ASM)

# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()

# Include libraries
add_subdirectory(src/drivers)
add_subdirectory(src/scheduler)
add_subdirectory(src/state_machine/states)
add_subdirectory(src/state_machine/tasks)

# Add executable. Default name is the project name, version 0.1
file(GLOB top CONFIGURE_DEPENDS "src/*.c")

add_executable(samwise ${top})

target_link_libraries(samwise PRIVATE
scheduler
state_machine_states
state_machine_tasks
drivers
pico_stdlib
hardware_spi
)

pico_set_program_name(samwise "samwise")
pico_set_program_version(samwise "0.1")

# Modify the below lines to enable/disable output over UART/USB
pico_enable_stdio_uart(samwise 0)
pico_enable_stdio_usb(samwise 1)

# Add the standard include files to the build
target_include_directories(samwise PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/common # common headers
)

pico_add_extra_outputs(samwise)
project(samwise C CXX ASM)

# Include src
# Executable `samwise` built under src/
add_subdirectory(src)
File renamed without changes.
29 changes: 29 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
add_subdirectory(common)
add_subdirectory(drivers)
add_subdirectory(scheduler)
add_subdirectory(states)
add_subdirectory(tasks)

add_executable(samwise main.c init.c)
target_link_libraries(samwise PUBLIC scheduler error rfm9x)

# Global include directory common for all targets
target_include_directories(samwise PUBLIC
"${PROJECT_SOURCE_DIR}/src/common"
"${PROJECT_SOURCE_DIR}/src/drivers/rfm9x"
"${PROJECT_SOURCE_DIR}/src/scheduler"
)

pico_set_program_name(samwise "samwise")
pico_set_program_version(samwise "0.1")

# Modify the below lines to enable/disable output over UART/USB
pico_enable_stdio_uart(samwise 0)
pico_enable_stdio_usb(samwise 1)

# Add the standard include files to the build
target_include_directories(samwise PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/common # common headers
)

pico_add_extra_outputs(samwise)
1 change: 1 addition & 0 deletions src/common/CMAkeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(error)
6 changes: 6 additions & 0 deletions src/common/error/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_library(error error.c)
target_link_libraries(error PUBLIC pico_stdlib)

target_include_directories(error PUBLIC
"${PROJECT_SOURCE_DIR}/src/common"
)
2 changes: 0 additions & 2 deletions src/common/error.c → src/common/error/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
*/

#include "error.h"
#include "macros.h"
#include "pico/stdlib.h"

/**
* This function should be called if we encounter an unrecoverable error. In
Expand Down
2 changes: 2 additions & 0 deletions src/common/error.h → src/common/error/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@

#pragma once

#include "pico/stdlib.h"

void fatal_error();
6 changes: 5 additions & 1 deletion src/common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
#pragma once

#include "error.h"
#include "pico/printf.h"
#if TEST == 1
#include <stdio.h>
#else
#include "pico/printf.h"
#endif

/**
* If this symbol is defined, we are configured to run a flight build.
Expand Down
7 changes: 5 additions & 2 deletions src/slate.h → src/common/slate.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

#pragma once

#include "drivers/rfm9x.h"
#include "pico/types.h"
#include "pico/util/queue.h"
#include "scheduler/scheduler.h"

#include "typedefs.h"
#include "state_machine.h"

#include "rfm9x.h"

typedef struct samwise_slate
{
Expand Down
64 changes: 64 additions & 0 deletions src/common/state_machine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once

Check notice on line 1 in src/common/state_machine.h

View workflow job for this annotation

GitHub Actions / Lint-Check

Run clang-format on src/common/state_machine.h

File src/common/state_machine.h does not conform to Custom style guidelines. (lines 5)

#include <stdlib.h>

#include "typedefs.h"
#include "slate.h"

#define MAX_TASKS_PER_STATE 10

/**
* Holds the info for a single task. A single task can belong to multiple
* states.
*/
typedef struct sched_task
{
/**
* Friendly name for the task.
*/
const char *name;

/**
* Minimum number of milliseconds between dispatches of this task.
*/
const uint32_t dispatch_period_ms;

/**
* Earliest time this task can be dispatched.
*/
absolute_time_t next_dispatch;

/**
* Called once when the task initializes.
* @param slate Pointer to the current satellite slate
*/
void (*task_init)(slate_t *slate);

/**
* Called each time the task dispatches.
* @param slate Pointer to the current satellite slate
*/
void (*task_dispatch)(slate_t *slate);

} sched_task_t;

/**
* Holds the info for defining a state.
*/
typedef struct sched_state
{
/**
* Friendly name for the state.
*/
const char *name;

size_t num_tasks;
sched_task_t *task_list[MAX_TASKS_PER_STATE];

/**
* Called each time the state dispatches.
* @param slate Pointer to the current satellite slate
* @return The next state to transition to
*/
struct sched_state *(*get_next_state)(slate_t *slate);
} sched_state_t;
4 changes: 4 additions & 0 deletions src/common/typedefs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
typedef struct samwise_slate slate_t;
typedef struct sched_state sched_state_t;
typedef struct sched_task sched_task_t;
typedef struct _rfm9x rfm9x_t;
1 change: 1 addition & 0 deletions src/drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(rfm9x)
8 changes: 8 additions & 0 deletions src/drivers/rfm9x/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_library(rfm9x rfm9x.c)

target_link_libraries(rfm9x PRIVATE error pico_stdlib hardware_spi)

target_include_directories(rfm9x PUBLIC
"${PROJECT_SOURCE_DIR}/src/common"
"${PROJECT_SOURCE_DIR}/src/common/error"
)
6 changes: 0 additions & 6 deletions src/drivers/rfm9x/rfm9x.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
#include "rfm9x.h"
#include "bit-support.h"
#include "hardware/resets.h"
#include "hardware/spi.h"
#include "pico/time.h"
#include "src/macros.h"
#include <string.h>

rfm9x_t rfm9x_mk(spi_inst_t *spi, uint reset_pin, uint cs_pin, uint spi_tx_pin,
uint spi_rx_pin, uint spi_clk_pin, uint d0_pin,
Expand Down
6 changes: 6 additions & 0 deletions src/drivers/rfm9x/rfm9x.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#pragma once

#include "hardware/resets.h"
#include "hardware/spi.h"

#include "pico/stdlib.h"
#include "pico/time.h"

#include "bit-support.h"
#include "macros.h"

#define PACKET_SIZE 256
#define PAYLOAD_SIZE 251
Expand Down
3 changes: 0 additions & 3 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
*/

#include "init.h"
#include "macros.h"
#include "pico/stdlib.h"
#include "scheduler/scheduler.h"

/**
* Initialize all gpio pins to their default states.
Expand Down
10 changes: 9 additions & 1 deletion src/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
* @date 2024-08-27
*/

#pragma once

#include "pico/stdlib.h"

#include "macros.h"
#include "slate.h"
#include "state_machine.h"

#include "scheduler.h"

bool init(slate_t *slate);
bool init(slate_t *slate);
7 changes: 1 addition & 6 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
* This file contains the main entry point for the SAMWISE flight code.
*/

#include "drivers/rfm9x.h"
#include "init.h"
#include "macros.h"
#include "pico/stdlib.h"
#include "scheduler/scheduler.h"
#include "slate.h"
#include "main.h"

/**
* Statically allocate the slate.
Expand Down
12 changes: 12 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "pico/stdlib.h"

#include "macros.h"
#include "slate.h"
#include "state_machine.h"

#include "init.h"

#include "rfm9x.h"
#include "scheduler.h"
12 changes: 12 additions & 0 deletions src/scheduler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
add_library(scheduler scheduler.c)
target_link_libraries(scheduler PUBLIC pico_stdlib hardware_spi init_state running_state error)

target_include_directories(scheduler PUBLIC
"${PROJECT_SOURCE_DIR}/src/common"
"${PROJECT_SOURCE_DIR}/src/states/init"
"${PROJECT_SOURCE_DIR}/src/states/running"
)

# Debug message to print the list of include directories for state_machine
get_target_property(INCLUDE_DIRS scheduler INCLUDE_DIRECTORIES)
message("[state_machine] Include directories: ${INCLUDE_DIRS}")
16 changes: 8 additions & 8 deletions src/scheduler/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
*/

#include "scheduler.h"
#include "macros.h"
#include "pico/time.h"
#include "slate.h"

/*
* Include the actual state machine
/**
* List of all states. We need this because we cannot enumerate all states at
* runtime.
*
* Note: For each state, the order of the task list determines priority. Tasks
* nearer the top have higher priority.
*/
#include "state_machine/states/states.h"
#include "state_machine/tasks/tasks.h"

static const sched_state_t *all_states[] = {&init_state, &running_state};
static sched_state_t *const initial_state = &init_state;
static size_t n_tasks = 0;
static sched_task_t *all_tasks[num_states * MAX_TASKS_PER_STATE];

Expand Down
Loading

0 comments on commit abdbf77

Please sign in to comment.