Skip to content
This repository was archived by the owner on Oct 26, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions projects/timer_hw/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--
General guidelines
These are just guidelines, not strict rules - document however seems best.
A README for a firmware-only project (e.g. Babydriver, MU, bootloader, CAN explorer) should answer the following questions:
- What is it?
- What problem does it solve?
- How do I use it? (with usage examples / example commands, etc)
- How does it work? (architectural overview)
A README for a board project (powering a hardware board, e.g. power distribution, centre console, charger, BMS carrier) should answer the following questions:
- What is the purpose of the board?
- What are all the things that the firmware needs to do?
- How does it fit into the overall system?
- How does it work? (architectural overview, e.g. what each module's purpose is or how data flows through the firmware)
-->
# timer_hw

9 changes: 9 additions & 0 deletions projects/timer_hw/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Defines $(T)_SRC, $(T)_INC, $(T)_DEPS, and $(T)_CFLAGS for the build makefile.
# Tests can be excluded by defining $(T)_EXCLUDE_TESTS.
# Pre-defined:
# $(T)_SRC_ROOT: $(T)_DIR/src
# $(T)_INC_DIRS: $(T)_DIR/inc{/$(PLATFORM)}
# $(T)_SRC: $(T)_DIR/src{/$(PLATFORM)}/*.{c,s}

# Specify the libraries you want to include
$(T)_DEPS := ms-common
51 changes: 51 additions & 0 deletions projects/timer_hw/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <stdint.h>
#include <stdlib.h>

#include "interrupt.h"
#include "log.h"
#include "soft_timer.h"
#include "wait.h"

#define COUNTER_PERIOD_MS 500

typedef struct Counters {
uint8_t counter_a;
uint8_t counter_b;
} Counters;

void prv_timer_callback_2(SoftTimerId timer_id, void *context);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry about forward declaring your functions, just declare them where you write the function bodies.
This line can go


void prv_timer_callback(SoftTimerId timer_id, void *context) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can (and should) do all the soft-timer functionality in one timer callback function instead of 2, see if you can figure it out, but if not feel free to ask for help 🙂

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, all private functions in a file should be declared as static, ie static void prv_*

Counters *storage = context;
storage->counter_a++;

LOG_DEBUG("Counter A: %i\n", storage->counter_a);

soft_timer_start_millis(COUNTER_PERIOD_MS, prv_timer_callback_2, storage, NULL);
}

void prv_timer_callback_2(SoftTimerId timer_id, void *context) {
Counters *storage = context;
storage->counter_a++;
storage->counter_b++;

LOG_DEBUG("Counter A: %i\n", storage->counter_a);
LOG_DEBUG("Counter B: %i\n", storage->counter_b);

soft_timer_start_millis(COUNTER_PERIOD_MS, prv_timer_callback, storage, NULL);
}

int main() {
interrupt_init();
soft_timer_init();

Counters storage = { 0 };

soft_timer_start_millis(COUNTER_PERIOD_MS, prv_timer_callback, &storage, NULL);

while (true) {
wait();
}

return 0;
}