Choco OS  V.0.16.9.0
Join to the chocolate world
oc_sys_lld.c
1 
27 #include <oc_sys_lld.h>
28 #include <oc_mem_lld.h>
29 #include <oc_clock_lld.h>
30 #include <oc_compiler.h>
31 #include <oc_mcs.h>
32 #include <oc_lsf.h>
33 #include <oc_machine.h>
34 #include <oc_interrupts.h>
35 #include <oc_module.h>
36 
42 #define _________________________________________LOCAL_TYPES_SECTION________________________________________________________________________
43 
44 
45 
46 #undef _________________________________________LOCAL_TYPES_SECTION________________________________________________________________________
47 
48 
54 #define _________________________________________LOCAL_MACROS_SECTION_______________________________________________________________________
55 
56 
57 #define IsRam(Address) oC_LSF_IsRamAddress(Address)
58 #define IsRom(Address) oC_LSF_IsRomAddress(Address)
59 //==========================================================================================================================================
63 //==========================================================================================================================================
64 #define ALIGN_SIZE(SIZE,ALIGNMENT) ((((oC_UInt_t)SIZE) + ALIGNMENT - 1) & ~(ALIGNMENT-1))
65 
66 #undef _________________________________________LOCAL_MACROS_SECTION_______________________________________________________________________
67 
68 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________
69 
70 static oC_SYS_LLD_EventInterrupt_t EventInterruptHandler = NULL;
71 static oC_SYS_LLD_EventFlags_t EnabledEventsFlags = 0;
72 static oC_Frequency_t SystemTimerFrequency = 0;
73 
74 
75 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________
76 
82 #define _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________
83 
84 //static void CallEventInterrupt ( oC_SYS_LLD_EventFlags_t EventsFlags , oC_SYS_LLD_Context_t * Context );
85 static void EnableEventsInterrupts ( oC_SYS_LLD_EventFlags_t EventsFlags );
86 
87 //static void DisableEventInterrupts ( oC_SYS_LLD_EventFlags_t EventsFlags );
88 static void CallEventHandler ( oC_SYS_LLD_EventFlags_t EventFlags , void * MemoryAddress );
89 //static inline bool IsCorrectHandlerPointer ( void * Address );
90 //static inline bool IsCorrectContext ( void * Address );
91 
92 
93 
94 #undef _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________
95 
101 #define _________________________________________INTERFACE_FUNCITONS_SECTION________________________________________________________________
102 
105 
106 //==========================================================================================================================================
111 //==========================================================================================================================================
112 oC_ErrorCode_t oC_SYS_LLD_TurnOnDriver( void )
113 {
114  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
115 
116  if(oC_Module_TurnOffVerification(&errorCode , oC_Module_SYS_LLD))
117  {
120  {
121  oC_Module_TurnOn(oC_Module_SYS_LLD);
122 
123  /* The setting oC_UnexpectedInterruptHandler is very important - without it, the oc_interrupt.c file is not used, and then
124  * the interrupt vector is not included to the output file. DO NOT REMOVE IT!! */
125  EventInterruptHandler = NULL;
126  EnabledEventsFlags = 0;
127  SystemTimerFrequency = 0;
129  errorCode = oC_ErrorCode_None;
130  }
131  else
132  {
133  errorCode = oC_ErrorCode_CannotInitializeModule;
134  }
136  }
137 
138  return errorCode;
139 }
140 
141 //==========================================================================================================================================
146 //==========================================================================================================================================
147 oC_ErrorCode_t oC_SYS_LLD_TurnOffDriver( void )
148 {
149  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
150 
151  if(oC_Module_TurnOnVerification(&errorCode , oC_Module_SYS_LLD))
152  {
154 
155  EventInterruptHandler = NULL;
156  EnabledEventsFlags = 0;
157  SystemTimerFrequency = 0;
158  oC_Module_TurnOff(oC_Module_SYS_LLD);
159 
161  errorCode = oC_ErrorCode_None;
162  }
163 
164  return errorCode;
165 }
166 
167 //==========================================================================================================================================
172 //==========================================================================================================================================
174 {
175  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
176 
177  if(oC_Module_TurnOnVerification(&errorCode , oC_Module_SYS_LLD))
178  {
180  if(
181  oC_AssignErrorCodeIfFalse(&errorCode , IsRam(EventHandler) || IsRom(EventHandler) , oC_ErrorCode_WrongEventHandlerAddress) &&
182  oC_AssignErrorCodeIfFalse(&errorCode , EventInterruptHandler == NULL , oC_ErrorCode_InterruptHandlerAlreadySet)
183  )
184  {
185  EventInterruptHandler = EventHandler;
186  EnabledEventsFlags = EventsFlags;
187 
188  EnableEventsInterrupts(EventsFlags);
189 
190  errorCode = oC_ErrorCode_None;
191  }
193  }
194 
195  return errorCode;
196 }
197 
198 //==========================================================================================================================================
203 //==========================================================================================================================================
205 {
206  return true;
207 }
208 
209 //==========================================================================================================================================
214 //==========================================================================================================================================
216 {
218 }
219 
220 //==========================================================================================================================================
225 //==========================================================================================================================================
227 {
229 }
230 
231 //==========================================================================================================================================
236 //==========================================================================================================================================
238 {
240 }
241 //==========================================================================================================================================
246 //==========================================================================================================================================
248 {
250 }
251 
252 //==========================================================================================================================================
257 //==========================================================================================================================================
259 {
261 }
262 //==========================================================================================================================================
268 //==========================================================================================================================================
270 {
271  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
273 
274  if(
275  oC_Module_TurnOnVerification(&errorCode , oC_Module_SYS_LLD) &&
276  oC_Module_TurnOnVerification(&errorCode , oC_Module_CLOCK_LLD) &&
277  oC_AssignErrorCodeIfFalse(&errorCode , IsRom(Interrupt) || IsRam(Interrupt) , oC_ErrorCode_WrongEventHandlerAddress) &&
278  oC_AssignErrorCodeIfFalse(&errorCode , Frequency > 0 && (Frequency <= clockFrequency) , oC_ErrorCode_WrongFrequency)
279  )
280  {
281  uint32_t prescaler = clockFrequency / Frequency;
282 
284 
285  if(oC_MCS_ConfigureSystemTimer(prescaler,Interrupt))
286  {
287  SystemTimerFrequency = clockFrequency / prescaler;
288  errorCode = oC_ErrorCode_None;
289  }
290  else
291  {
292  errorCode = oC_ErrorCode_FrequencyNotPossible;
293  }
294 
296  }
297 
298  return errorCode;
299 }
300 
301 //==========================================================================================================================================
306 //==========================================================================================================================================
307 oC_ErrorCode_t oC_SYS_LLD_ReadSystemTimerFrequency( oC_Frequency_t * outFrequency )
308 {
309  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
310 
311  if(oC_Module_TurnOnVerification(&errorCode , oC_Module_SYS_LLD))
312  {
313  if(oC_AssignErrorCodeIfFalse(&errorCode , SystemTimerFrequency != 0 , oC_ErrorCode_SystemClockNotConfigured))
314  {
315  *outFrequency = SystemTimerFrequency;
316  errorCode = oC_ErrorCode_None;
317  }
318  }
319 
320  return errorCode;
321 }
322 
323 //==========================================================================================================================================
328 //==========================================================================================================================================
329 oC_ErrorCode_t oC_SYS_LLD_SetNextContext( oC_SYS_LLD_Context_t * Context )
330 {
331  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
332 
333  if(oC_Module_TurnOnVerification(&errorCode , oC_Module_SYS_LLD))
334  {
336 
337  if(oC_MCS_SetNextStack(Context))
338  {
339  errorCode = oC_ErrorCode_None;
340  }
341  else
342  {
343  errorCode = oC_ErrorCode_ContextNotCorrect;
344  }
345 
347  }
348 
349  return errorCode;
350 }
351 
352 
353 //==========================================================================================================================================
358 //==========================================================================================================================================
360 {
361  oC_Int_t stackSize = 0;
362 
363  if(oC_Module_IsTurnedOn(oC_Module_SYS_LLD))
364  {
365  stackSize = oC_MCS_GetStackSize(Context);
366  }
367 
368  return stackSize;
369 }
370 
371 //==========================================================================================================================================
376 //==========================================================================================================================================
378 {
379  oC_Int_t freeStackSize = 0;
380 
381  if(oC_Module_IsTurnedOn(oC_Module_SYS_LLD))
382  {
383  freeStackSize = oC_MCS_GetFreeStackSize(Context);
384  }
385 
386  return freeStackSize;
387 }
388 
389 //==========================================================================================================================================
394 //==========================================================================================================================================
395 oC_ErrorCode_t oC_SYS_LLD_ReturnToSystemContext( void )
396 {
398 }
399 
400 //==========================================================================================================================================
405 //==========================================================================================================================================
407 {
408  return oC_MCS_GetCurrentStack();
409 }
410 
411 //==========================================================================================================================================
416 //==========================================================================================================================================
418 {
419  return oC_MCS_GetSystemStack();
420 }
421 
422 //==========================================================================================================================================
427 //==========================================================================================================================================
428 oC_ErrorCode_t oC_SYS_LLD_InitializeContext( oC_SYS_LLD_Context_t ** Context , oC_Int_t StackSize , oC_SYS_LLD_ContextHandler_t ContextHandler , void * Parameters , oC_SYS_LLD_ContextExitHandler_t ExitHandler )
429 {
430  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
431 
432  if(oC_Module_TurnOnVerification(&errorCode , oC_Module_SYS_LLD))
433  {
434  if(
435  oC_AssignErrorCodeIfFalse(&errorCode , IsRam(Context) , oC_ErrorCode_OutputAddressNotInRAM ) &&
436  oC_AssignErrorCodeIfFalse(&errorCode , IsRam(Context) || IsRom(ContextHandler) , oC_ErrorCode_WrongEventHandlerAddress ) &&
437  oC_AssignErrorCodeIfFalse(&errorCode , IsRam(ExitHandler) || IsRom(ExitHandler) , oC_ErrorCode_WrongEventHandlerAddress ) &&
438  oC_AssignErrorCodeIfFalse(&errorCode , StackSize > 0 , oC_ErrorCode_WrongStackSize)
439  )
440  {
441  if(oC_MCS_InitializeStack((oC_Stack_t*)Context,*Context,StackSize,ContextHandler,Parameters,ExitHandler))
442  {
443  errorCode = oC_ErrorCode_None;
444  }
445  else
446  {
447  errorCode = oC_ErrorCode_CannotInitializeStack;
448  }
449  }
450  }
451 
452  return errorCode;
453 }
454 
455 //==========================================================================================================================================
460 //==========================================================================================================================================
461 oC_Int_t oC_SYS_LLD_GetMinimumContextSize( oC_Int_t StackSize )
462 {
463  return oC_MCS_GetMinimumStackBufferSize(StackSize);
464 }
465 
466 //==========================================================================================================================================
471 //==========================================================================================================================================
472 const char * oC_SYS_LLD_GetMachineName( void )
473 {
474  return oC_TO_STRING(oC_MACHINE);
475 }
476 
477 //==========================================================================================================================================
482 //==========================================================================================================================================
483 const char * oC_SYS_LLD_GetMachineFamilyName( void )
484 {
485  return oC_TO_STRING(oC_MACHINE_FAMILY);
486 }
487 
488 //==========================================================================================================================================
493 //==========================================================================================================================================
494 const char * oC_SYS_LLD_GetMachineProducentName( void )
495 {
496  return "TI";
497 }
498 
499 //==========================================================================================================================================
504 //==========================================================================================================================================
505 void oC_SYS_LLD_Reset( void )
506 {
507  oC_MCS_Reboot();
508 }
509 
510 //==========================================================================================================================================
515 //==========================================================================================================================================
517 {
519 }
520 
521 //==========================================================================================================================================
526 //==========================================================================================================================================
527 oC_ErrorCode_t oC_SYS_LLD_ReadPowerState( float * outVcc )
528 {
529  return oC_ErrorCode_NotSupportedOnTargetMachine;
530 }
531 
532 //==========================================================================================================================================
537 //==========================================================================================================================================
539 {
541 }
542 
543 #undef _________________________________________INTERFACE_FUNCITONS_SECTION________________________________________________________________
544 
551 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
552 
553 //==========================================================================================================================================
557 //==========================================================================================================================================
558 static void EnableEventsInterrupts( oC_SYS_LLD_EventFlags_t EventFlags )
559 {
560  if(EventFlags & oC_SYS_LLD_EventFlags_HardFault)
561  {
562  /* Always it is turned on */
563  }
564 }
565 //==========================================================================================================================================
569 //==========================================================================================================================================
570 static void CallEventHandler( oC_SYS_LLD_EventFlags_t EventFlags , void * MemoryAddress )
571 {
572  if(IsRom(EventInterruptHandler) || IsRam(EventInterruptHandler))
573  {
574  if(EnabledEventsFlags & EventFlags)
575  {
576  EventInterruptHandler(EventFlags,oC_MCS_GetCurrentStack(),MemoryAddress);
577  }
578  }
579 }
580 
581 //==========================================================================================================================================
585 //==========================================================================================================================================
586 
587 
588 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
589 
595 #define _________________________________________INTERRUPTS_SECTION_________________________________________________________________________
596 
597 //==========================================================================================================================================
601 //==========================================================================================================================================
602 oC_InterruptHandler(System,HardFault)
603 {
605 
606  /* Interrupt handler is not set, but an hard fault occurs */
607  oC_ASSERT( EventInterruptHandler != NULL );
608 }
609 
610 
611 #undef _________________________________________INTERRUPTS_SECTION_________________________________________________________________________
#define oC_InterruptHandler(BASE_NAME, TYPE_NAME)
Define handler for interrupt.
stores stack data
Definition: oc_mcs.h:208
oC_SYS_LLD_Context_t * oC_SYS_LLD_GetSystemContext(void)
returns pointer to the system context
Definition: oc_sys_lld.c:408
oC_SYS_LLD_EventFlags_t
event flags handled by the module.
Definition: oc_sys_lld.h:153
oC_Int_t oC_MCS_GetMinimumStackBufferSize(oC_Int_t StackSize)
returns minimum stack buffer size
Definition: oc_mcs.c:601
void * oC_MCS_GetCurrentProcessStackPointer(void)
returns current value of PSP
Definition: oc_mcs.c:510
oC_InterruptHandler_t oC_UnexpectedInterruptHandler
Handler for unexpected interrupts.
Definition: oc_interrupts.c:43
void(* oC_SYS_LLD_ContextHandler_t)(void *Parameter)
stores pointer to context handler function
Definition: oc_sys_lld.h:207
oC_Int_t oC_SYS_LLD_GetMinimumContextSize(oC_Int_t StackSize)
returns minimum size to allocate for context
Definition: oc_sys_lld.c:452
#define oC_MACHINE_FAMILY
double oC_Frequency_t
type to store frequency
Definition: oc_frequency.h:76
void(* oC_SYS_LLD_EventInterrupt_t)(oC_SYS_LLD_EventFlags_t EventFlags, oC_SYS_LLD_Context_t *Context, void *BusAddress)
stores event handler function
Definition: oc_sys_lld.h:196
oC_ErrorCode_t oC_SYS_LLD_InitializeContext(oC_SYS_LLD_Context_t **Context, oC_Int_t StackSize, oC_SYS_LLD_ContextHandler_t ContextHandler, void *Parameters, oC_SYS_LLD_ContextExitHandler_t ExitHandler)
initializes stack of process
Definition: oc_sys_lld.c:419
oC_SYS_LLD_Context_t * oC_SYS_LLD_GetCurrentContext(void)
returns pointer to the current context
Definition: oc_sys_lld.c:397
const char * oC_SYS_LLD_GetMachineFamilyName(void)
returns printable name of the machine family
Definition: oc_sys_lld.c:474
void * oC_MCS_GetHardFaultReason(void)
returns address that cause a hard fault
Definition: oc_mcs.c:321
The file contains definitions for the compiler, that helps to manage errors, etc. ...
oC_Stack_t oC_MCS_GetSystemStack(void)
returns pointer to the system stack
Definition: oc_mcs.c:659
oC_ErrorCode_t oC_SYS_LLD_TurnOnDriver(void)
initializes the driver to work
Definition: oc_sys_lld.c:103
const char * oC_SYS_LLD_GetMachineProducentName(void)
returns printable name of the machine producent
Definition: oc_sys_lld.c:485
The file with interface for LSF module.
bool oC_MCS_ReturnToSystemStack(void)
sets next stack as system stack
Definition: oc_mcs.c:669
oC_ErrorCode_t oC_SYS_LLD_ReturnToSystemContext(void)
switches to the system context
Definition: oc_sys_lld.c:386
void oC_MCS_Reboot(void)
Software reboots of machine.
Definition: oc_mcs.c:421
void oC_SYS_LLD_ExitCriticalSection(void)
exits from critical section
Definition: oc_sys_lld.c:239
The file with LLD interface for the MEM driver.
bool oC_MCS_InitializeStack(oC_Stack_t *outStack, void *Buffer, oC_Int_t BufferSize, oC_ContextHandler_t ContextHandler, void *HandlerParameter, oC_ContextExitHandler_t ExitHandler)
initializes stack for the system
Definition: oc_mcs.c:432
oC_ErrorCode_t oC_SYS_LLD_ConfigureSystemTimer(oC_Frequency_t Frequency, oC_SYS_LLD_SysTickIncrementHandler_t Interrupt)
configures system timer
Definition: oc_sys_lld.c:261
oC_ErrorCode_t oC_SYS_LLD_ReadSystemTimerFrequency(oC_Frequency_t *outFrequency)
reads configured system frequency
Definition: oc_sys_lld.c:299
void oC_SYS_LLD_Context_t
type for storing context of the machine
Definition: oc_sys_lld.h:143
void oC_SYS_LLD_TurnOnInterrupts(void)
turns on interrupt
Definition: oc_sys_lld.c:206
void(* oC_SYS_LLD_SysTickIncrementHandler_t)(void)
stores SysTick interrupt handler
Definition: oc_sys_lld.h:182
oC_Int_t oC_MCS_GetFreeStackSize(oC_Stack_t Stack)
returns number of free stack
Definition: oc_mcs.c:551
oC_ErrorCode_t oC_SYS_LLD_TurnOffDriver(void)
release the driver
Definition: oc_sys_lld.c:138
bool oC_MCS_SetNextStack(oC_Stack_t Stack)
sets next stack for context switching
Definition: oc_mcs.c:613
void(* oC_SYS_LLD_ContextExitHandler_t)(void)
pointer to the context exit handler
Definition: oc_sys_lld.h:216
#define oC_MACHINE
The file with interface for the module library.
const char * oC_SYS_LLD_GetMachineName(void)
returns printable name of the machine
Definition: oc_sys_lld.c:463
oC_Frequency_t oC_CLOCK_LLD_GetClockFrequency(void)
returns frequency of the system clock
Definition: oc_clock_lld.c:237
oC_Int_t oC_SYS_LLD_GetContextStackSize(oC_SYS_LLD_Context_t *Context)
returns size of the stack for context
Definition: oc_sys_lld.c:350
static bool oC_Module_IsTurnedOn(oC_Module_t Module)
checks if the module is turned on
Definition: oc_module.h:121
The file with LLD interface for the CLOCK driver.
oC_ErrorCode_t oC_SYS_LLD_ReadPowerState(float *outVcc)
reads power state
Definition: oc_sys_lld.c:518
oC_ErrorCode_t oC_SYS_LLD_SetNextContext(oC_SYS_LLD_Context_t *Context)
configures next machine context
Definition: oc_sys_lld.c:321
void oC_SYS_LLD_EnterCriticalSection(void)
enters to critical section
Definition: oc_sys_lld.c:228
bool oC_MCS_ConfigureSystemTimer(oC_UInt_t Prescaler, oC_FindNextStackHandler_t FindNextStackHandler)
configures system timer
Definition: oc_mcs.c:634
bool oC_MCS_AreInterruptsEnabled(void)
Checks if interrupts are enabled in HW.
Definition: oc_mcs.c:356
oC_Int_t oC_MCS_GetStackSize(oC_Stack_t Stack)
returns size of stack
Definition: oc_mcs.c:529
static void oC_Module_TurnOn(oC_Module_t Module)
sets module as turned on
Definition: oc_module.h:170
Contains machine core specific functions.
bool oC_MCS_InitializeModule(void)
initializes module to work
Definition: oc_mcs.c:299
oC_ErrorCode_t oC_SYS_LLD_SetEventInterruptHandler(oC_SYS_LLD_EventInterrupt_t EventHandler, oC_SYS_LLD_EventFlags_t EventsFlags)
sets event interrupt handler
Definition: oc_sys_lld.c:164
static void oC_MCS_EnterCriticalSection(void)
Enters to critical section.
Definition: oc_mcs.h:755
oC_Stack_t oC_MCS_GetCurrentStack(void)
returns current stack
Definition: oc_mcs.c:591
The file with interface for the machine module.
The file with interface interrupt module.
void oC_MCS_DisableInterrupts(void)
Globally disables interrupts (always)
Definition: oc_mcs.c:346
#define oC_MCS_IsStackPushDecrementPointer()
returns true if stack push decrements SP
Definition: oc_mcs.h:77
void oC_SYS_LLD_Reset(void)
software reset of the machine
Definition: oc_sys_lld.c:496
static bool oC_Module_TurnOffVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned off
Definition: oc_module.h:155
static bool oC_MCS_ExitCriticalSection(void)
Exits from critical section.
Definition: oc_mcs.h:784
static bool oC_Module_TurnOnVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned on
Definition: oc_module.h:138
void oC_MCS_EnableInterrupts(void)
Globally enables interrupts (always)
Definition: oc_mcs.c:331
The file with LLD interface for the SYS driver.
bool oC_SYS_LLD_AreInterruptsEnabled(void)
checks if interrupts are turned on
Definition: oc_sys_lld.c:250
void * oC_SYS_LLD_GetLastProcessStackPointer(void)
returns stack pointer of last executed process
Definition: oc_sys_lld.c:537
bool oC_SYS_LLD_IsStackPushDecrementPointer(void)
flag if stack push is decrement pointer
Definition: oc_sys_lld.c:507
void oC_SYS_LLD_TurnOffInterrupts(void)
turns off interrupt
Definition: oc_sys_lld.c:217
bool oC_SYS_LLD_IsMachineSupportMultithreadMode(void)
checks if the machine supports multi-thread mode
Definition: oc_sys_lld.c:195
Hard fault, reason is unknown.
Definition: oc_sys_lld.h:155
#define NULL
pointer to a zero
Definition: oc_null.h:37
static void oC_Module_TurnOff(oC_Module_t Module)
sets module as turned off
Definition: oc_module.h:185
oC_Int_t oC_SYS_LLD_GetContextFreeStackSize(oC_SYS_LLD_Context_t *Context)
returns size of free stack in the context
Definition: oc_sys_lld.c:368