@@ -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,13 +162,39 @@ 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 void * TableAddr ;
148172
149173 memset (& SCH_LAB_Global , 0 , sizeof (SCH_LAB_Global ));
150174
175+ OsStatus = OS_CountSemCreate (& SCH_LAB_Global .TimingSem , "SCH_LAB" , 0 , 0 );
176+ if (OsStatus != OS_SUCCESS )
177+ {
178+ CFE_ES_WriteToSysLog ("%s: OS_CountSemCreate failed:RC=%ld\n" , __func__ , (long )OsStatus );
179+ return CFE_STATUS_EXTERNAL_RESOURCE_FAIL ;
180+ }
181+
182+ /* The underlying timebase object should have been created by the PSP */
183+ OsStatus = OS_TimeBaseGetIdByName (& TimeBaseId , "cFS-Master" );
184+ if (OsStatus != OS_SUCCESS )
185+ {
186+ CFE_ES_WriteToSysLog ("%s: OS_TimeBaseGetIdByName failed:RC=%ld\n" , __func__ , (long )OsStatus );
187+ return CFE_STATUS_EXTERNAL_RESOURCE_FAIL ;
188+ }
189+
190+ /* Create the timer callback (but not set yet, as that requires the config table) */
191+ OsStatus = OS_TimerAdd (& SCH_LAB_Global .TimerId , "SCH_LAB" , TimeBaseId , SCH_LAB_LocalTimerCallback , NULL );
192+ if (OsStatus != OS_SUCCESS )
193+ {
194+ CFE_ES_WriteToSysLog ("%s: OS_TimerAdd failed:RC=%ld\n" , __func__ , (long )OsStatus );
195+ return CFE_STATUS_EXTERNAL_RESOURCE_FAIL ;
196+ }
197+
151198 /*
152199 ** Register tables with cFE and load default data
153200 */
@@ -206,6 +253,22 @@ int32 SCH_LAB_AppInit(void)
206253 ++ LocalStateEntry ;
207254 }
208255
256+ if (ConfigTable -> TickRate == 0 )
257+ {
258+ /* use default of 1 second */
259+ CFE_ES_WriteToSysLog ("%s: Using default tick rate of 1 second\n" , __func__ );
260+ TimerPeriod = 1000000 ;
261+ }
262+ else
263+ {
264+ TimerPeriod = 1000000 / ConfigTable -> TickRate ;
265+ if ((TimerPeriod * ConfigTable -> TickRate ) != 1000000 )
266+ {
267+ CFE_ES_WriteToSysLog ("%s: WARNING: tick rate of %lu is not an integer number of microseconds\n" , __func__ ,
268+ (unsigned long )ConfigTable -> TickRate );
269+ }
270+ }
271+
209272 /*
210273 ** Release the table
211274 */
@@ -228,6 +291,13 @@ int32 SCH_LAB_AppInit(void)
228291 OS_printf ("SCH Error subscribing to 1hz!\n" );
229292 }
230293
294+ /* Set timer period */
295+ OsStatus = OS_TimerSet (SCH_LAB_Global .TimerId , 1000000 , TimerPeriod );
296+ if (OsStatus != OS_SUCCESS )
297+ {
298+ CFE_ES_WriteToSysLog ("%s: OS_TimerSet failed:RC=%ld\n" , __func__ , (long )OsStatus );
299+ }
300+
231301 OS_printf ("SCH Lab Initialized.%s\n" , SCH_LAB_VERSION_STRING );
232302
233303 return (CFE_SUCCESS );
0 commit comments