Skip to content

Commit 0396089

Browse files
committed
Fix #103, add option to configure base tick rate
SCH_LAB was fixed at 1Hz because it bound to the 1Hz tick from CFE_TIME. This creates an OSAL timer instead, which posts a semaphore, and this can serve as a much more flexible time source, with configurable rate. This still binds to 1Hz and the SCH does not start its first message until the first 1Hz is received, thereby keeping some form of 1Hz sync.
1 parent 8124041 commit 0396089

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed

fsw/platform_inc/sch_lab_table.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@
4848
typedef struct
4949
{
5050
CFE_SB_MsgId_t MessageID; /* Message ID for the table entry */
51-
uint32 PacketRate; /* Rate: Send packet every N seconds */
51+
uint32 PacketRate; /* Rate: Send packet every N ticks */
5252
CFE_MSG_FcnCode_t FcnCode; /* Command/Function code to set */
5353
} SCH_LAB_ScheduleTableEntry_t;
5454

5555
typedef struct
5656
{
57+
uint32 TickRate; /* Ticks per second to configure for timer (0=default) */
5758
SCH_LAB_ScheduleTableEntry_t Config[SCH_LAB_MAX_SCHEDULE_ENTRIES];
5859
} SCH_LAB_ScheduleTable_t;
5960

fsw/src/sch_lab_app.c

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ typedef struct
5959
typedef struct
6060
{
6161
SCH_LAB_StateEntry_t State[SCH_LAB_MAX_SCHEDULE_ENTRIES];
62+
osal_id_t TimerId;
63+
osal_id_t TimingSem;
6264
CFE_TBL_Handle_t TblHandle;
6365
CFE_SB_PipeId_t CmdPipe;
6466
} SCH_LAB_GlobalData_t;
@@ -80,8 +82,9 @@ void SCH_Lab_AppMain(void)
8082
{
8183
int i;
8284
uint32 SCH_OneHzPktsRcvd = 0;
83-
uint32 Status = CFE_SUCCESS;
84-
uint32 RunStatus = CFE_ES_RunStatus_APP_RUN;
85+
int32 OsStatus;
86+
CFE_Status_t Status;
87+
uint32 RunStatus = CFE_ES_RunStatus_APP_RUN;
8588
SCH_LAB_StateEntry_t *LocalStateEntry;
8689
CFE_SB_Buffer_t * SBBufPtr;
8790

@@ -99,16 +102,29 @@ void SCH_Lab_AppMain(void)
99102
{
100103
CFE_ES_PerfLogExit(SCH_MAIN_TASK_PERF_ID);
101104

102-
/* Pend on receipt of 1Hz packet */
103-
Status = CFE_SB_ReceiveBuffer(&SBBufPtr, SCH_LAB_Global.CmdPipe, CFE_SB_PEND_FOREVER);
105+
/* Pend on timing sem */
106+
OsStatus = OS_CountSemTake(SCH_LAB_Global.TimingSem);
107+
if (OsStatus == OS_SUCCESS)
108+
{
109+
/* check for arrival of the 1Hz - this should sync counts (TBD) */
110+
Status = CFE_SB_ReceiveBuffer(&SBBufPtr, SCH_LAB_Global.CmdPipe, CFE_SB_POLL);
111+
}
112+
else
113+
{
114+
Status = CFE_STATUS_EXTERNAL_RESOURCE_FAIL;
115+
}
104116

105117
CFE_ES_PerfLogEntry(SCH_MAIN_TASK_PERF_ID);
106118

107119
if (Status == CFE_SUCCESS)
108120
{
109121
SCH_OneHzPktsRcvd++;
122+
}
123+
124+
if (OsStatus == OS_SUCCESS && SCH_OneHzPktsRcvd > 0)
125+
{
110126
/*
111-
** Process table every second, sending packets that are ready
127+
** Process table every tick, sending packets that are ready
112128
*/
113129
LocalStateEntry = SCH_LAB_Global.State;
114130
for (i = 0; i < SCH_LAB_MAX_SCHEDULE_ENTRIES; i++)
@@ -132,6 +148,11 @@ void SCH_Lab_AppMain(void)
132148

133149
} /* end SCH_Lab_AppMain */
134150

151+
void SCH_LAB_LocalTimerCallback(osal_id_t object_id, void *arg)
152+
{
153+
OS_CountSemGive(SCH_LAB_Global.TimingSem);
154+
}
155+
135156
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
136157
/* */
137158
/* SCH_LAB_AppInit() -- initialization */
@@ -141,12 +162,38 @@ int32 SCH_LAB_AppInit(void)
141162
{
142163
int i;
143164
int32 Status;
165+
int32 OsStatus;
166+
uint32 TimerPeriod;
167+
osal_id_t TimeBaseId;
144168
SCH_LAB_ScheduleTable_t * ConfigTable;
145169
SCH_LAB_ScheduleTableEntry_t *ConfigEntry;
146170
SCH_LAB_StateEntry_t * LocalStateEntry;
147171

148172
memset(&SCH_LAB_Global, 0, sizeof(SCH_LAB_Global));
149173

174+
OsStatus = OS_CountSemCreate(&SCH_LAB_Global.TimingSem, "SCH_LAB", 0, 0);
175+
if (OsStatus != OS_SUCCESS)
176+
{
177+
CFE_ES_WriteToSysLog("%s: OS_CountSemCreate failed:RC=%ld\n", __func__, (long)OsStatus);
178+
return CFE_STATUS_EXTERNAL_RESOURCE_FAIL;
179+
}
180+
181+
/* The underlying timebase object should have been created by the PSP */
182+
OsStatus = OS_TimeBaseGetIdByName(&TimeBaseId, "cFS-Master");
183+
if (OsStatus != OS_SUCCESS)
184+
{
185+
CFE_ES_WriteToSysLog("%s: OS_TimeBaseGetIdByName failed:RC=%ld\n", __func__, (long)OsStatus);
186+
return CFE_STATUS_EXTERNAL_RESOURCE_FAIL;
187+
}
188+
189+
/* Create the timer callback (but not set yet, as that requires the config table) */
190+
OsStatus = OS_TimerAdd(&SCH_LAB_Global.TimerId, "SCH_LAB", TimeBaseId, SCH_LAB_LocalTimerCallback, NULL);
191+
if (OsStatus != OS_SUCCESS)
192+
{
193+
CFE_ES_WriteToSysLog("%s: OS_TimerAdd failed:RC=%ld\n", __func__, (long)OsStatus);
194+
return CFE_STATUS_EXTERNAL_RESOURCE_FAIL;
195+
}
196+
150197
/*
151198
** Register tables with cFE and load default data
152199
*/
@@ -203,6 +250,22 @@ int32 SCH_LAB_AppInit(void)
203250
++LocalStateEntry;
204251
}
205252

253+
if (ConfigTable->TickRate == 0)
254+
{
255+
/* use default of 1 second */
256+
CFE_ES_WriteToSysLog("%s: Using default tick rate of 1 second\n", __func__);
257+
TimerPeriod = 1000000;
258+
}
259+
else
260+
{
261+
TimerPeriod = 1000000 / ConfigTable->TickRate;
262+
if ((TimerPeriod * ConfigTable->TickRate) != 1000000)
263+
{
264+
CFE_ES_WriteToSysLog("%s: WARNING: tick rate of %lu is not an integer number of microseconds\n", __func__,
265+
(unsigned long)ConfigTable->TickRate);
266+
}
267+
}
268+
206269
/*
207270
** Release the table
208271
*/
@@ -225,6 +288,13 @@ int32 SCH_LAB_AppInit(void)
225288
OS_printf("SCH Error subscribing to 1hz!\n");
226289
}
227290

291+
/* Set timer period */
292+
OsStatus = OS_TimerSet(SCH_LAB_Global.TimerId, 1000000, TimerPeriod);
293+
if (OsStatus != OS_SUCCESS)
294+
{
295+
CFE_ES_WriteToSysLog("%s: OS_TimerSet failed:RC=%ld\n", __func__, (long)OsStatus);
296+
}
297+
228298
OS_printf("SCH Lab Initialized.%s\n", SCH_LAB_VERSION_STRING);
229299

230300
return (CFE_SUCCESS);

fsw/tables/sch_lab_table.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
** 3. If the table grows too big, increase SCH_LAB_MAX_SCHEDULE_ENTRIES
5050
*/
5151

52-
SCH_LAB_ScheduleTable_t SCH_TBL_Structure = {.Config = {
52+
SCH_LAB_ScheduleTable_t SCH_TBL_Structure = {.TickRate = 1,
53+
.Config = {
5354
{CFE_SB_MSGID_WRAP_VALUE(CFE_ES_SEND_HK_MID), 4, 0},
5455
{CFE_SB_MSGID_WRAP_VALUE(CFE_EVS_SEND_HK_MID), 4, 0},
5556
{CFE_SB_MSGID_WRAP_VALUE(CFE_TIME_SEND_HK_MID), 4, 0},

0 commit comments

Comments
 (0)