Skip to content

Commit ee4367a

Browse files
committed
Modified to include Led Component, zephyr gpio needs work
1 parent 99cd1ab commit ee4367a

File tree

10 files changed

+298
-1
lines changed

10 files changed

+298
-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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
void Led ::parameterUpdated(FwPrmIdType id) {
21+
Fw::ParamValid isValid = Fw::ParamValid::INVALID;
22+
switch (id) {
23+
case PARAMID_BLINK_INTERVAL: {
24+
// Read back the parameter value
25+
const U32 interval = this->paramGet_BLINK_INTERVAL(isValid);
26+
// NOTE: isValid is always VALID in parameterUpdated as it was just properly set
27+
FW_ASSERT(isValid == Fw::ParamValid::VALID, static_cast<FwAssertArgType>(isValid));
28+
29+
// Emit the blink interval set event
30+
this->log_ACTIVITY_HI_BlinkIntervalSet(interval);
31+
break;
32+
}
33+
default:
34+
FW_ASSERT(0, static_cast<FwAssertArgType>(id));
35+
break;
36+
}
37+
}
38+
39+
// ----------------------------------------------------------------------
40+
// Handler implementations for user-defined typed input ports
41+
// ----------------------------------------------------------------------
42+
43+
void Led ::run_handler(FwIndexType portNum, U32 context) {
44+
// Read back the parameter value
45+
Fw::ParamValid isValid = Fw::ParamValid::INVALID;
46+
U32 interval = this->paramGet_BLINK_INTERVAL(isValid);
47+
FW_ASSERT((isValid != Fw::ParamValid::INVALID) && (isValid != Fw::ParamValid::UNINIT),
48+
static_cast<FwAssertArgType>(isValid));
49+
50+
// Only perform actions when set to blinking
51+
if (this->m_blinking && (interval != 0)) {
52+
// If toggling state
53+
if (this->m_toggleCounter == 0) {
54+
// Toggle state
55+
this->m_state = (this->m_state == Fw::On::ON) ? Fw::On::OFF : Fw::On::ON;
56+
this->m_transitions++;
57+
this->tlmWrite_LedTransitions(this->m_transitions);
58+
59+
// Port may not be connected, so check before sending output
60+
if (this->isConnected_gpioSet_OutputPort(0)) {
61+
this->gpioSet_out(0, (Fw::On::ON == this->m_state) ? Fw::Logic::HIGH : Fw::Logic::LOW);
62+
}
63+
64+
this->log_ACTIVITY_LO_LedState(this->m_state);
65+
}
66+
67+
this->m_toggleCounter = (this->m_toggleCounter + 1) % interval;
68+
}
69+
// We are not blinking
70+
else {
71+
if (this->m_state == Fw::On::ON) {
72+
// Port may not be connected, so check before sending output
73+
if (this->isConnected_gpioSet_OutputPort(0)) {
74+
this->gpioSet_out(0, Fw::Logic::LOW);
75+
}
76+
77+
this->m_state = Fw::On::OFF;
78+
this->log_ACTIVITY_LO_LedState(this->m_state);
79+
}
80+
}
81+
}
82+
83+
// ----------------------------------------------------------------------
84+
// Handler implementations for commands
85+
// ----------------------------------------------------------------------
86+
87+
void Led ::BLINKING_ON_OFF_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, Fw::On onOff) {
88+
this->m_toggleCounter = 0; // Reset count on any successful command
89+
this->m_blinking = Fw::On::ON == onOff; // Update blinking state
90+
91+
this->log_ACTIVITY_HI_SetBlinkingState(onOff);
92+
93+
this->tlmWrite_BlinkingState(onOff);
94+
95+
// Provide command response
96+
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
97+
}
98+
99+
} // namespace Components
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
@ Telemetry channel to report blinking state.
11+
telemetry BlinkingState: Fw.On
12+
13+
@ Telemetry channel counting LED transitions
14+
telemetry LedTransitions: U64
15+
16+
@ Reports the state we set to blinking.
17+
event SetBlinkingState($state: Fw.On) \
18+
severity activity high \
19+
format "Set blinking state to {}."
20+
21+
@ Event logged when the LED turns on or off
22+
event LedState(onOff: Fw.On) \
23+
severity activity low \
24+
format "LED is {}"
25+
26+
@ Event logged when the LED blink interval is updated
27+
event BlinkIntervalSet(interval: U32) \
28+
severity activity high \
29+
format "LED blink interval set to {}"
30+
31+
@ Blinking interval in rate group ticks
32+
param BLINK_INTERVAL: U32 default 1
33+
34+
@ Port receiving calls from the rate group
35+
async input port run: Svc.Sched
36+
37+
@ Port sending calls to the GPIO driver
38+
output port gpioSet: Drv.GpioWrite
39+
40+
###############################################################################
41+
# Standard AC Ports: Required for Channels, Events, Commands, and Parameters #
42+
###############################################################################
43+
@ Port for requesting the current time
44+
time get port timeCaller
45+
46+
@ Port for sending command registrations
47+
command reg port cmdRegOut
48+
49+
@ Port for receiving commands
50+
command recv port cmdIn
51+
52+
@ Port for sending command responses
53+
command resp port cmdResponseOut
54+
55+
@ Port for sending textual representation of events
56+
text event port logTextOut
57+
58+
@ Port for sending events to downlink
59+
event port logOut
60+
61+
@ Port for sending telemetry channels to downlink
62+
telemetry port tlmOut
63+
64+
@ Port to return the value of a parameter
65+
param get port prmGetOut
66+
67+
@Port to set the value of a parameter
68+
param set port prmSetOut
69+
70+
}
71+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
//! Emit parameter updated EVR
29+
//!
30+
void
31+
parameterUpdated(FwPrmIdType id //!< The parameter ID
32+
) override;
33+
34+
private :
35+
36+
// ----------------------------------------------------------------------
37+
// Handler implementations for user-defined typed input ports
38+
// ----------------------------------------------------------------------
39+
40+
//! Handler implementation for run
41+
//!
42+
//! Port receiving calls from the rate group
43+
void
44+
run_handler(FwIndexType portNum, //!< The port number
45+
U32 context //!< The call order
46+
) override;
47+
48+
private :
49+
// ----------------------------------------------------------------------
50+
// Handler implementations for commands
51+
// ----------------------------------------------------------------------
52+
53+
//! Handler implementation for command BLINKING_ON_OFF
54+
//!
55+
//! Command to turn on or off the blinking LED
56+
void
57+
BLINKING_ON_OFF_cmdHandler(FwOpcodeType opCode, //!< The opcode
58+
U32 cmdSeq, //!< The command sequence number
59+
Fw::On onOff //!< Indicates whether the blinking should be on or off
60+
) override;
61+
62+
Fw::On m_state = Fw::On::OFF; //! Keeps track if LED is on or off
63+
U64 m_transitions = 0; //! The number of on/off transitions that have occurred
64+
//! from FSW boot up
65+
U32 m_toggleCounter = 0; //! Keeps track of how many ticks the LED has been on for
66+
bool m_blinking = false; //! Flag: if true then LED blinking will occur else
67+
//! no blinking will happen
68+
};
69+
70+
} // namespace Components
71+
72+
#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::GpioDirection::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)