@@ -59,6 +59,8 @@ typedef struct
5959typedef 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 );
0 commit comments