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 #define PWR_CR oC_Register(PWR,PWR_CR)
57 #define IsRam(Address) (oC_LSF_IsRamAddress(Address) || oC_LSF_IsExternalAddress(Address))
58 #define IsRom(Address) oC_LSF_IsRomAddress(Address)
59 
60 #undef _________________________________________LOCAL_MACROS_SECTION_______________________________________________________________________
61 
67 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________
68 
69 static oC_SYS_LLD_EventInterrupt_t EventInterruptHandler = NULL;
70 static oC_SYS_LLD_EventFlags_t EnabledEventsFlags = 0;
71 static oC_Frequency_t SystemTimerFrequency = 0;
72 
73 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________
74 
80 #define _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________
81 
82 static void EnableEventsInterrupts ( oC_SYS_LLD_EventFlags_t EventFlags );
83 static void CallEventHandler ( oC_SYS_LLD_EventFlags_t EventFlags , void * MemoryAddress );
84 
85 #undef _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________
86 
92 #define _________________________________________INTERFACE_FUNCITONS_SECTION________________________________________________________________
93 
96 
97 //==========================================================================================================================================
102 //==========================================================================================================================================
103 oC_ErrorCode_t oC_SYS_LLD_TurnOnDriver( void )
104 {
105  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
106 
107  if(oC_Module_TurnOffVerification(&errorCode , oC_Module_SYS_LLD))
108  {
111  {
112  oC_Module_TurnOn(oC_Module_SYS_LLD);
113 
114  /* The setting oC_UnexpectedInterruptHandler is very important - without it, the oc_interrupt.c file is not used, and then
115  * the interrupt vector is not included to the output file. DO NOT REMOVE IT!! */
116  EventInterruptHandler = NULL;
117  EnabledEventsFlags = 0;
118  SystemTimerFrequency = 0;
120  errorCode = oC_ErrorCode_None;
121  }
122  else
123  {
124  errorCode = oC_ErrorCode_CannotInitializeModule;
125  }
127  }
128 
129  return errorCode;
130 }
131 
132 //==========================================================================================================================================
137 //==========================================================================================================================================
138 oC_ErrorCode_t oC_SYS_LLD_TurnOffDriver( void )
139 {
140  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
141 
142  if(oC_Module_TurnOnVerification(&errorCode , oC_Module_SYS_LLD))
143  {
145 
146  EventInterruptHandler = NULL;
147  EnabledEventsFlags = 0;
148  SystemTimerFrequency = 0;
149  oC_Module_TurnOff(oC_Module_SYS_LLD);
150 
152  errorCode = oC_ErrorCode_None;
153  }
154 
155  return errorCode;
156 }
157 
158 //==========================================================================================================================================
163 //==========================================================================================================================================
165 {
166  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
167 
168  if(oC_Module_TurnOnVerification(&errorCode , oC_Module_SYS_LLD))
169  {
171  if(
172  oC_AssignErrorCodeIfFalse(&errorCode , IsRam(EventHandler) || IsRom(EventHandler) , oC_ErrorCode_WrongEventHandlerAddress) &&
173  oC_AssignErrorCodeIfFalse(&errorCode , EventInterruptHandler == NULL , oC_ErrorCode_InterruptHandlerAlreadySet)
174  )
175  {
176  EventInterruptHandler = EventHandler;
177  EnabledEventsFlags = EventsFlags;
178 
179  EnableEventsInterrupts(EventsFlags);
180 
181  errorCode = oC_ErrorCode_None;
182  }
184  }
185 
186  return errorCode;
187 }
188 
189 //==========================================================================================================================================
194 //==========================================================================================================================================
196 {
197  return true;
198 }
199 
200 //==========================================================================================================================================
205 //==========================================================================================================================================
207 {
209 }
210 
211 //==========================================================================================================================================
216 //==========================================================================================================================================
218 {
220 }
221 
222 //==========================================================================================================================================
227 //==========================================================================================================================================
229 {
231 }
232 
233 //==========================================================================================================================================
238 //==========================================================================================================================================
240 {
242 }
243 
244 //==========================================================================================================================================
249 //==========================================================================================================================================
251 {
253 }
254 
255 //==========================================================================================================================================
260 //==========================================================================================================================================
262 {
263  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
265 
266  if(
267  oC_Module_TurnOnVerification(&errorCode , oC_Module_SYS_LLD) &&
268  oC_Module_TurnOnVerification(&errorCode , oC_Module_CLOCK_LLD) &&
269  oC_AssignErrorCodeIfFalse(&errorCode , IsRom(Interrupt) || IsRam(Interrupt) , oC_ErrorCode_WrongEventHandlerAddress) &&
270  oC_AssignErrorCodeIfFalse(&errorCode , Frequency > 0 && (Frequency <= clockFrequency) , oC_ErrorCode_WrongFrequency)
271  )
272  {
273  uint32_t prescaler = clockFrequency / Frequency;
274 
276 
277  if(oC_MCS_ConfigureSystemTimer(prescaler,Interrupt))
278  {
279  SystemTimerFrequency = clockFrequency / prescaler;
280  errorCode = oC_ErrorCode_None;
281  }
282  else
283  {
284  errorCode = oC_ErrorCode_FrequencyNotPossible;
285  }
286 
288  }
289 
290  return errorCode;
291 }
292 
293 //==========================================================================================================================================
298 //==========================================================================================================================================
299 oC_ErrorCode_t oC_SYS_LLD_ReadSystemTimerFrequency( oC_Frequency_t * outFrequency )
300 {
301  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
302 
303  if(oC_Module_TurnOnVerification(&errorCode , oC_Module_SYS_LLD))
304  {
305  if(oC_AssignErrorCodeIfFalse(&errorCode , SystemTimerFrequency != 0 , oC_ErrorCode_SystemClockNotConfigured))
306  {
307  *outFrequency = SystemTimerFrequency;
308  errorCode = oC_ErrorCode_None;
309  }
310  }
311 
312  return errorCode;
313 }
314 
315 //==========================================================================================================================================
320 //==========================================================================================================================================
322 {
323  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
324 
325  if(oC_Module_TurnOnVerification(&errorCode , oC_Module_SYS_LLD))
326  {
328 
329  if(oC_MCS_SetNextStack(Context))
330  {
331  errorCode = oC_ErrorCode_None;
332  }
333  else
334  {
335  errorCode = oC_ErrorCode_ContextNotCorrect;
336  }
337 
339  }
340 
341  return errorCode;
342 }
343 
344 //==========================================================================================================================================
349 //==========================================================================================================================================
351 {
352  oC_Int_t stackSize = 0;
353 
354  if(oC_Module_IsTurnedOn(oC_Module_SYS_LLD))
355  {
356  stackSize = oC_MCS_GetStackSize(Context);
357  }
358 
359  return stackSize;
360 }
361 
362 //==========================================================================================================================================
367 //==========================================================================================================================================
369 {
370  oC_Int_t freeStackSize = 0;
371 
372  if(oC_Module_IsTurnedOn(oC_Module_SYS_LLD))
373  {
374  freeStackSize = oC_MCS_GetFreeStackSize(Context);
375  }
376 
377  return freeStackSize;
378 }
379 
380 //==========================================================================================================================================
385 //==========================================================================================================================================
386 oC_ErrorCode_t oC_SYS_LLD_ReturnToSystemContext( void )
387 {
389 }
390 
391 //==========================================================================================================================================
396 //==========================================================================================================================================
398 {
399  return oC_MCS_GetCurrentStack();
400 }
401 
402 //==========================================================================================================================================
407 //==========================================================================================================================================
409 {
410  return oC_MCS_GetSystemStack();
411 }
412 
413 //==========================================================================================================================================
418 //==========================================================================================================================================
419 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 )
420 {
421  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
422 
423  if(oC_Module_TurnOnVerification(&errorCode , oC_Module_SYS_LLD))
424  {
425  if(
426  oC_AssignErrorCodeIfFalse(&errorCode , IsRam(Context) , oC_ErrorCode_OutputAddressNotInRAM ) &&
427  oC_AssignErrorCodeIfFalse(&errorCode , IsRam(Context) || IsRom(ContextHandler) , oC_ErrorCode_WrongEventHandlerAddress ) &&
428  oC_AssignErrorCodeIfFalse(&errorCode , IsRam(ExitHandler) || IsRom(ExitHandler) , oC_ErrorCode_WrongEventHandlerAddress ) &&
429  oC_AssignErrorCodeIfFalse(&errorCode , StackSize > 0 , oC_ErrorCode_WrongStackSize)
430  )
431  {
432  if(oC_MCS_InitializeStack((oC_Stack_t*)Context,*Context,StackSize,ContextHandler,Parameters,ExitHandler))
433  {
434  errorCode = oC_ErrorCode_None;
435  }
436  else
437  {
438  errorCode = oC_ErrorCode_CannotInitializeStack;
439  }
440  }
441  }
442 
443  return errorCode;
444 }
445 
446 //==========================================================================================================================================
451 //==========================================================================================================================================
452 oC_Int_t oC_SYS_LLD_GetMinimumContextSize( oC_Int_t StackSize )
453 {
454  return oC_MCS_GetMinimumStackBufferSize(StackSize);
455 }
456 
457 //==========================================================================================================================================
462 //==========================================================================================================================================
463 const char * oC_SYS_LLD_GetMachineName( void )
464 {
465  return oC_TO_STRING(oC_MACHINE);
466 }
467 
468 //==========================================================================================================================================
473 //==========================================================================================================================================
475 {
476  return oC_TO_STRING(oC_MACHINE_FAMILY);
477 }
478 
479 //==========================================================================================================================================
484 //==========================================================================================================================================
486 {
487  return "ST";
488 }
489 
490 //==========================================================================================================================================
495 //==========================================================================================================================================
496 void oC_SYS_LLD_Reset( void )
497 {
498  oC_MCS_Reboot();
499 }
500 
501 //==========================================================================================================================================
506 //==========================================================================================================================================
508 {
510 }
511 
512 //==========================================================================================================================================
517 //==========================================================================================================================================
518 oC_ErrorCode_t oC_SYS_LLD_ReadPowerState( float * outVcc )
519 {
520  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
521 
522  if(oC_AssignErrorCodeIfFalse(&errorCode , oC_LSF_IsRamAddress(outVcc) , oC_ErrorCode_OutputAddressNotInRAM))
523  {
524  *outVcc = 3.3;
525  errorCode = oC_ErrorCode_None;
526  }
527 
528  return errorCode;
529 }
530 
531 //==========================================================================================================================================
536 //==========================================================================================================================================
538 {
540 }
541 #undef _________________________________________INTERFACE_FUNCITONS_SECTION________________________________________________________________
542 
549 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
550 
551 //==========================================================================================================================================
555 //==========================================================================================================================================
556 static void EnableEventsInterrupts( oC_SYS_LLD_EventFlags_t EventFlags )
557 {
558  if(EventFlags & oC_SYS_LLD_EventFlags_HardFault)
559  {
560  /* Always it is turned on */
561  }
562 }
563 
564 //==========================================================================================================================================
568 //==========================================================================================================================================
569 static void CallEventHandler( oC_SYS_LLD_EventFlags_t EventFlags , void * MemoryAddress )
570 {
571  if(IsRom(EventInterruptHandler) || IsRam(EventInterruptHandler))
572  {
573  if(EnabledEventsFlags & EventFlags)
574  {
575  EventInterruptHandler(EventFlags,oC_MCS_GetCurrentStack(), MemoryAddress);
576  }
577  }
578 }
579 
580 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
581 
587 #define _________________________________________INTERRUPTS_SECTION_________________________________________________________________________
588 
589 //==========================================================================================================================================
593 //==========================================================================================================================================
594 oC_InterruptHandler(System,HardFault)
595 {
597 
598  /* Interrupt handler is not set, but an hard fault occurs */
599  oC_ASSERT( EventInterruptHandler != NULL );
600 }
601 
602 #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