Skip to content

Commit eb3a8e6

Browse files
authored
Merge pull request #2 from Open-Source-Space-Foundation/add-led-blinker
Simple `led-blinker` in Proves Reference
2 parents df71548 + ed82463 commit eb3a8e6

File tree

12 files changed

+293
-1
lines changed

12 files changed

+293
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Include project-wide components here
22

33
add_fprime_subdirectory("${CMAKE_CURRENT_LIST_DIR}/FatalHandler")
4+
add_fprime_subdirectory("${CMAKE_CURRENT_LIST_DIR}/Led")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
####
2+
# F Prime CMakeLists.txt:
3+
#
4+
# SOURCES: list of source files (to be compiled)
5+
# AUTOCODER_INPUTS: list of files to be passed to the autocoders
6+
# DEPENDS: list of libraries that this module depends on
7+
#
8+
# More information in the F´ CMake API documentation:
9+
# https://fprime.jpl.nasa.gov/latest/docs/reference/api/cmake/API/
10+
#
11+
####
12+
13+
# Module names are derived from the path from the nearest project/library/framework
14+
# root when not specifically overridden by the developer. i.e. The module defined by
15+
# `Ref/SignalGen/CMakeLists.txt` will be named `Ref_SignalGen`.
16+
17+
register_fprime_library(
18+
AUTOCODER_INPUTS
19+
"${CMAKE_CURRENT_LIST_DIR}/Led.fpp"
20+
SOURCES
21+
"${CMAKE_CURRENT_LIST_DIR}/Led.cpp"
22+
# DEPENDS
23+
# MyPackage_MyOtherModule
24+
)
25+
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// ======================================================================
2+
// \title Led.cpp
3+
// \author ortega
4+
// \brief cpp file for Led component implementation class
5+
// ======================================================================
6+
7+
#include "FprimeZephyrReference/Components/Led/Led.hpp"
8+
#include "config/FpConfig.hpp"
9+
10+
namespace Components {
11+
12+
// ----------------------------------------------------------------------
13+
// Component construction and destruction
14+
// ----------------------------------------------------------------------
15+
16+
Led ::Led(const char* const compName) : LedComponentBase(compName) {}
17+
18+
Led ::~Led() {}
19+
20+
// ----------------------------------------------------------------------
21+
// Handler implementations for user-defined typed input ports
22+
// ----------------------------------------------------------------------
23+
24+
void Led ::run_handler(FwIndexType portNum, U32 context) {
25+
26+
U32 interval = this->m_blinkInterval; // Get the blink interval from the member variable
27+
28+
// Only perform actions when set to blinking
29+
if (this->m_blinking && (interval != 0)) {
30+
// If toggling state
31+
if (this->m_toggleCounter == 0) {
32+
// Toggle state
33+
this->m_state = (this->m_state == Fw::On::ON) ? Fw::On::OFF : Fw::On::ON;
34+
this->m_transitions++;
35+
this->tlmWrite_LedTransitions(this->m_transitions);
36+
37+
// Port may not be connected, so check before sending output
38+
if (this->isConnected_gpioSet_OutputPort(0)) {
39+
this->gpioSet_out(0, (Fw::On::ON == this->m_state) ? Fw::Logic::HIGH : Fw::Logic::LOW);
40+
}
41+
42+
this->log_ACTIVITY_LO_LedState(this->m_state);
43+
}
44+
45+
this->m_toggleCounter = (this->m_toggleCounter + 1) % interval;
46+
}
47+
// We are not blinking
48+
else {
49+
if (this->m_state == Fw::On::ON) {
50+
// Port may not be connected, so check before sending output
51+
if (this->isConnected_gpioSet_OutputPort(0)) {
52+
this->gpioSet_out(0, Fw::Logic::LOW);
53+
}
54+
55+
this->m_state = Fw::On::OFF;
56+
this->log_ACTIVITY_LO_LedState(this->m_state);
57+
}
58+
}
59+
}
60+
61+
// ----------------------------------------------------------------------
62+
// Handler implementations for commands
63+
// ----------------------------------------------------------------------
64+
65+
void Led ::BLINKING_ON_OFF_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, Fw::On onOff) {
66+
this->m_toggleCounter = 0; // Reset count on any successful command
67+
this->m_blinking = Fw::On::ON == onOff; // Update blinking state
68+
69+
this->log_ACTIVITY_HI_SetBlinkingState(onOff);
70+
71+
this->tlmWrite_BlinkingState(onOff);
72+
73+
// Provide command response
74+
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
75+
}
76+
77+
void Led ::SET_BLINK_INTERVAL_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, U32 interval) {
78+
// Update the member variable
79+
this->m_blinkInterval = interval;
80+
81+
// Log the event
82+
this->log_ACTIVITY_HI_BlinkIntervalSet(interval);
83+
84+
// Provide command response
85+
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
86+
}
87+
88+
} // namespace Components
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module Components {
2+
@ Component to blink an LED driven by a rate group
3+
active component Led {
4+
5+
@ Command to turn on or off the blinking LED
6+
async command BLINKING_ON_OFF(
7+
onOff: Fw.On @< Indicates whether the blinking should be on or off
8+
)
9+
10+
@ Command to set the LED blink interval
11+
async command SET_BLINK_INTERVAL(
12+
interval: U32 @< Blink interval in rate group ticks
13+
)
14+
15+
@ Telemetry channel to report blinking state.
16+
telemetry BlinkingState: Fw.On
17+
18+
@ Telemetry channel counting LED transitions
19+
telemetry LedTransitions: U64
20+
21+
@ Reports the state we set to blinking.
22+
event SetBlinkingState($state: Fw.On) \
23+
severity activity high \
24+
format "Set blinking state to {}."
25+
26+
@ Event logged when the LED turns on or off
27+
event LedState(onOff: Fw.On) \
28+
severity activity low \
29+
format "LED is {}"
30+
31+
@ Event logged when the LED blink interval is updated
32+
event BlinkIntervalSet(interval: U32) \
33+
severity activity high \
34+
format "LED blink interval set to {}"
35+
36+
@ Port receiving calls from the rate group
37+
async input port run: Svc.Sched
38+
39+
@ Port sending calls to the GPIO driver
40+
output port gpioSet: Drv.GpioWrite
41+
42+
###############################################################################
43+
# Standard AC Ports: Required for Channels, Events, Commands, and Parameters #
44+
###############################################################################
45+
@ Port for requesting the current time
46+
time get port timeCaller
47+
48+
@ Port for sending command registrations
49+
command reg port cmdRegOut
50+
51+
@ Port for receiving commands
52+
command recv port cmdIn
53+
54+
@ Port for sending command responses
55+
command resp port cmdResponseOut
56+
57+
@ Port for sending textual representation of events
58+
text event port logTextOut
59+
60+
@ Port for sending events to downlink
61+
event port logOut
62+
63+
@ Port for sending telemetry channels to downlink
64+
telemetry port tlmOut
65+
66+
}
67+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// ======================================================================
2+
// \title Led.hpp
3+
// \author ortega
4+
// \brief hpp file for Led component implementation class
5+
// ======================================================================
6+
7+
#ifndef Components_Led_HPP
8+
#define Components_Led_HPP
9+
10+
#include "FprimeZephyrReference/Components/Led/LedComponentAc.hpp"
11+
12+
namespace Components {
13+
14+
class Led : public LedComponentBase {
15+
public:
16+
// ----------------------------------------------------------------------
17+
// Component construction and destruction
18+
// ----------------------------------------------------------------------
19+
20+
//! Construct Led object
21+
Led(const char* const compName //!< The component name
22+
);
23+
24+
//! Destroy Led object
25+
~Led();
26+
27+
private :
28+
29+
// ----------------------------------------------------------------------
30+
// Handler implementations for user-defined typed input ports
31+
// ----------------------------------------------------------------------
32+
33+
//! Handler implementation for run
34+
//!
35+
//! Port receiving calls from the rate group
36+
void
37+
run_handler(FwIndexType portNum, //!< The port number
38+
U32 context //!< The call order
39+
) override;
40+
41+
private :
42+
// ----------------------------------------------------------------------
43+
// Handler implementations for commands
44+
// ----------------------------------------------------------------------
45+
46+
//! Handler implementation for command BLINKING_ON_OFF
47+
//!
48+
//! Command to turn on or off the blinking LED
49+
void
50+
BLINKING_ON_OFF_cmdHandler(FwOpcodeType opCode, //!< The opcode
51+
U32 cmdSeq, //!< The command sequence number
52+
Fw::On onOff //!< Indicates whether the blinking should be on or off
53+
) override;
54+
55+
//! Handler implementation for command SET_BLINK_INTERVAL
56+
//!
57+
//! Command to set the LED blink interval
58+
void
59+
SET_BLINK_INTERVAL_cmdHandler(FwOpcodeType opCode, //!< The opcode
60+
U32 cmdSeq, //!< The command sequence number
61+
U32 interval //!< Blink interval in rate group ticks
62+
) override;
63+
64+
Fw::On m_state = Fw::On::OFF; //! Keeps track if LED is on or off
65+
U64 m_transitions = 0; //! The number of on/off transitions that have occurred
66+
//! from FSW boot up
67+
U32 m_toggleCounter = 0; //! Keeps track of how many ticks the LED has been on for
68+
bool m_blinking = false; //! Flag: if true then LED blinking will occur else
69+
//! no blinking will happen
70+
U32 m_blinkInterval = 10; //! Blink interval in rate group ticks (FIX FOR ZEPHYR)
71+
};
72+
73+
} // namespace Components
74+
75+
#endif

FprimeZephyrReference/ReferenceDeployment/Top/ReferenceDeploymentPackets.fppi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ telemetry packets ReferenceDeploymentPackets {
3030
CdhCore.version.LibraryVersion01
3131
}
3232

33+
packet Led id 5 group 4 {
34+
ReferenceDeployment.led.LedTransitions
35+
ReferenceDeployment.led.BlinkingState
36+
}
37+
3338

3439
} omit {
3540
CdhCore.cmdDisp.CommandErrors

FprimeZephyrReference/ReferenceDeployment/Top/ReferenceDeploymentTopology.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
// Necessary project-specified types
1212
#include <Fw/Types/MallocAllocator.hpp>
1313

14+
#include <zephyr/drivers/gpio.h>
15+
16+
static const struct gpio_dt_spec ledGpio = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
17+
1418
// Allows easy reference to objects in FPP/autocoder required namespaces
1519
using namespace ReferenceDeployment;
1620

@@ -50,6 +54,8 @@ void configureTopology() {
5054
// Rate groups require context arrays.
5155
rateGroup10Hz.configure(rateGroup10HzContext, FW_NUM_ARRAY_ELEMENTS(rateGroup10HzContext));
5256
rateGroup1Hz.configure(rateGroup1HzContext, FW_NUM_ARRAY_ELEMENTS(rateGroup1HzContext));
57+
58+
gpioDriver.open(ledGpio, Zephyr::ZephyrGpioDriver::GpioConfiguration::OUT);
5359
}
5460

5561
// Public functions for use in main program are namespaced with deployment name ReferenceDeployment

FprimeZephyrReference/ReferenceDeployment/Top/ReferenceDeploymentTopologyDefs.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
// Include autocoded FPP constants
2424
#include "FprimeZephyrReference/ReferenceDeployment/Top/FppConstantsAc.hpp"
25+
26+
#include <zephyr/kernel.h>
27+
#include <zephyr/device.h>
2528
#include <zephyr/drivers/uart.h>
2629

2730

FprimeZephyrReference/ReferenceDeployment/Top/instances.fpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ module ReferenceDeployment {
3535
queue size Default.QUEUE_SIZE \
3636
stack size Default.STACK_SIZE \
3737
priority 4
38-
38+
39+
instance led: Components.Led base id 0x10003000 \
40+
queue size Default.QUEUE_SIZE \
41+
stack size Default.STACK_SIZE \
42+
priority 5
3943

4044
# ----------------------------------------------------------------------
4145
# Queued component instances
@@ -56,4 +60,6 @@ module ReferenceDeployment {
5660

5761
instance comDriver: Zephyr.ZephyrUartDriver base id 0x10014000
5862

63+
instance gpioDriver: Zephyr.ZephyrGpioDriver base id 0x10015000
64+
5965
}

FprimeZephyrReference/ReferenceDeployment/Top/topology.fpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ module ReferenceDeployment {
2626
instance rateGroupDriver
2727
instance timer
2828
instance comDriver
29+
instance gpioDriver
30+
instance led
2931

3032
# ----------------------------------------------------------------------
3133
# Pattern graph specifiers
@@ -89,6 +91,13 @@ module ReferenceDeployment {
8991
rateGroup1Hz.RateGroupMemberOut[3] -> CdhCore.tlmSend.Run
9092
}
9193

94+
connections LedBlinker {
95+
# Rate Group 1 (1Hz cycle) ouput is connected to led's run input
96+
rateGroup1Hz.RateGroupMemberOut[4] -> led.run
97+
# led's gpioSet output is connected to gpioDriver's gpioWrite input
98+
led.gpioSet -> gpioDriver.gpioWrite
99+
}
100+
92101
connections ReferenceDeployment {
93102
94103
}

0 commit comments

Comments
 (0)