2013-05-24 15:26:52 +00:00
/*
2013-06-15 11:37:22 +00:00
* This file is subject to the terms of the GFX License . If a copy of
* the license was not distributed with this file , you can obtain one at :
*
2018-10-01 15:32:39 +00:00
* http : //ugfx.io/license.html
2013-06-15 11:37:22 +00:00
*/
2013-05-24 15:26:52 +00:00
2015-11-21 09:27:08 +00:00
# include "../../gfx.h"
2013-05-24 15:26:52 +00:00
# if GFX_USE_OS_CHIBIOS
2013-07-20 11:57:01 +00:00
# include <string.h>
2021-08-12 10:47:08 +00:00
# if CH_KERNEL_MAJOR < 2 || CH_KERNEL_MAJOR > 6
2018-07-07 07:05:18 +00:00
# error "GOS: Unsupported version of ChibiOS"
# endif
2014-04-18 11:20:50 +00:00
2018-07-07 07:05:18 +00:00
# if CH_KERNEL_MAJOR <= 2
2014-04-18 11:20:50 +00:00
# if !CH_USE_MUTEXES
# error "GOS: CH_USE_MUTEXES must be defined in chconf.h"
# endif
# if !CH_USE_SEMAPHORES
# error "GOS: CH_USE_SEMAPHORES must be defined in chconf.h"
# endif
2018-07-07 07:05:18 +00:00
# else
2014-04-18 11:20:50 +00:00
# if !CH_CFG_USE_MUTEXES
2014-10-09 02:29:55 +00:00
# error "GOS: CH_CFG_USE_MUTEXES must be defined in chconf.h"
2014-04-18 11:20:50 +00:00
# endif
# if !CH_CFG_USE_SEMAPHORES
2014-10-09 02:29:55 +00:00
# error "GOS: CH_CFG_USE_SEMAPHORES must be defined in chconf.h"
2014-04-18 11:20:50 +00:00
# endif
2013-05-24 15:26:52 +00:00
# endif
2014-02-02 18:24:43 +00:00
void _gosInit ( void )
{
2015-08-16 15:21:01 +00:00
# if !GFX_OS_NO_INIT
2014-07-29 02:00:47 +00:00
/* Don't Initialize if the user already has */
2018-07-07 07:05:18 +00:00
# if CH_KERNEL_MAJOR <= 2
2014-07-29 02:00:47 +00:00
if ( ! chThdSelf ( ) ) {
halInit ( ) ;
chSysInit ( ) ;
}
2018-07-07 07:05:18 +00:00
# else
2014-07-29 02:00:47 +00:00
if ( ! chThdGetSelfX ( ) ) {
halInit ( ) ;
chSysInit ( ) ;
}
# endif
2015-07-16 09:02:59 +00:00
# elif !GFX_OS_INIT_NO_WARNING
2017-06-30 09:43:51 +00:00
# if GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_DIRECT
# warning "GOS: Operating System initialization has been turned off. Make sure you call halInit() and chSysInit() before gfxInit() in your application!"
# elif GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_MACRO
COMPILER_WARNING ( " GOS: Operating System initialization has been turned off. Make sure you call halInit() and chSysInit() before gfxInit() in your application! " )
# endif
2014-04-18 11:20:50 +00:00
# endif
2013-05-24 15:26:52 +00:00
}
2017-03-04 05:02:55 +00:00
void _gosPostInit ( void )
{
}
2014-02-02 18:24:43 +00:00
void _gosDeinit ( void )
{
/* ToDo */
}
2018-11-03 00:51:23 +00:00
void * gfxRealloc ( void * ptr , gMemSize oldsz , gMemSize newsz )
2014-04-18 11:20:50 +00:00
{
2013-07-20 11:57:01 +00:00
void * np ;
if ( newsz < = oldsz )
return ptr ;
np = gfxAlloc ( newsz ) ;
if ( ! np )
return 0 ;
if ( oldsz )
memcpy ( np , ptr , oldsz ) ;
return np ;
}
2018-07-08 04:30:31 +00:00
void gfxSleepMilliseconds ( gDelay ms )
2014-04-18 11:20:50 +00:00
{
2013-05-24 15:26:52 +00:00
switch ( ms ) {
2018-07-08 05:15:15 +00:00
case gDelayNone : chThdYield ( ) ; return ;
case gDelayForever : chThdSleep ( TIME_INFINITE ) ; return ;
2013-12-07 21:01:57 +00:00
default : chThdSleepMilliseconds ( ms ) ; return ;
2013-05-24 15:26:52 +00:00
}
}
2018-07-08 04:30:31 +00:00
void gfxSleepMicroseconds ( gDelay ms )
2014-04-18 11:20:50 +00:00
{
2013-05-24 15:26:52 +00:00
switch ( ms ) {
2018-07-08 05:15:15 +00:00
case gDelayNone : return ;
case gDelayForever : chThdSleep ( TIME_INFINITE ) ; return ;
2013-12-07 21:01:57 +00:00
default : chThdSleepMicroseconds ( ms ) ; return ;
2013-05-24 15:26:52 +00:00
}
}
2013-12-07 21:01:57 +00:00
2018-11-03 02:29:30 +00:00
void gfxSemInit ( gSem * psem , gSemcount val , gSemcount limit )
2014-04-18 11:20:50 +00:00
{
2013-12-07 21:01:57 +00:00
if ( val > limit )
val = limit ;
2013-05-24 15:26:52 +00:00
psem - > limit = limit ;
2014-04-18 11:20:50 +00:00
2018-07-07 07:05:18 +00:00
# if CH_KERNEL_MAJOR <= 2
2014-04-18 11:20:50 +00:00
chSemInit ( & psem - > sem , val ) ;
2018-07-07 07:05:18 +00:00
# else
2014-04-18 11:20:50 +00:00
chSemObjectInit ( & psem - > sem , val ) ;
# endif
2013-05-24 15:26:52 +00:00
}
2013-12-07 21:01:57 +00:00
2018-11-03 02:29:30 +00:00
void gfxSemDestroy ( gSem * psem )
2014-04-18 11:20:50 +00:00
{
2013-05-25 16:06:55 +00:00
chSemReset ( & psem - > sem , 1 ) ;
}
2013-12-07 21:01:57 +00:00
2018-11-03 02:29:30 +00:00
gBool gfxSemWait ( gSem * psem , gDelay ms )
2014-04-18 11:20:50 +00:00
{
2018-07-07 07:05:18 +00:00
# if CH_KERNEL_MAJOR <= 2
2014-04-18 11:20:50 +00:00
switch ( ms ) {
2018-07-08 05:15:15 +00:00
case gDelayNone : return chSemWaitTimeout ( & psem - > sem , TIME_IMMEDIATE ) ! = RDY_TIMEOUT ;
case gDelayForever : chSemWait ( & psem - > sem ) ; return gTrue ;
2018-07-07 07:05:18 +00:00
default : return chSemWaitTimeout ( & psem - > sem , gfxMillisecondsToTicks ( ms ) ) ! = RDY_TIMEOUT ;
2014-04-18 11:20:50 +00:00
}
2018-07-07 07:05:18 +00:00
# else
2014-04-18 11:20:50 +00:00
switch ( ms ) {
2018-07-08 05:15:15 +00:00
case gDelayNone : return chSemWaitTimeout ( & psem - > sem , TIME_IMMEDIATE ) ! = MSG_TIMEOUT ;
case gDelayForever : chSemWait ( & psem - > sem ) ; return gTrue ;
2018-07-07 07:05:18 +00:00
default : return chSemWaitTimeout ( & psem - > sem , gfxMillisecondsToTicks ( ms ) ) ! = MSG_TIMEOUT ;
2014-04-18 11:20:50 +00:00
}
# endif
2013-05-24 15:26:52 +00:00
}
2018-11-03 02:29:30 +00:00
gBool gfxSemWaitI ( gSem * psem )
2014-04-18 11:20:50 +00:00
{
2018-07-07 07:05:18 +00:00
# if CH_KERNEL_MAJOR <= 3
2017-08-16 07:59:53 +00:00
if ( psem - > sem . s_cnt < = 0 )
2018-06-23 03:02:07 +00:00
return gFalse ;
2018-07-07 07:05:18 +00:00
# else
2017-08-16 07:59:53 +00:00
if ( psem - > sem . cnt < = 0 )
2018-06-23 03:02:07 +00:00
return gFalse ;
2017-08-16 07:59:53 +00:00
# endif
2014-03-11 07:10:11 +00:00
chSemFastWaitI ( & psem - > sem ) ;
2018-06-23 03:02:07 +00:00
return gTrue ;
2014-03-11 07:10:11 +00:00
}
2018-11-03 02:29:30 +00:00
void gfxSemSignal ( gSem * psem )
2014-04-18 11:20:50 +00:00
{
2013-05-24 15:26:52 +00:00
chSysLock ( ) ;
2013-12-07 21:01:57 +00:00
2018-07-07 07:05:18 +00:00
# if CH_KERNEL_MAJOR <= 3
2017-08-16 07:59:53 +00:00
if ( psem - > sem . s_cnt < psem - > limit )
chSemSignalI ( & psem - > sem ) ;
2018-07-07 07:05:18 +00:00
# else
2017-08-16 07:59:53 +00:00
if ( psem - > sem . cnt < psem - > limit )
chSemSignalI ( & psem - > sem ) ;
# endif
2013-12-07 21:01:57 +00:00
2013-05-24 15:26:52 +00:00
chSchRescheduleS ( ) ;
chSysUnlock ( ) ;
}
2018-11-03 02:29:30 +00:00
void gfxSemSignalI ( gSem * psem )
2014-04-18 11:20:50 +00:00
{
2018-07-07 07:05:18 +00:00
# if CH_KERNEL_MAJOR <= 3
2017-08-16 07:59:53 +00:00
if ( psem - > sem . s_cnt < psem - > limit )
chSemSignalI ( & psem - > sem ) ;
2018-07-07 07:05:18 +00:00
# else
2017-08-16 07:59:53 +00:00
if ( psem - > sem . cnt < psem - > limit )
chSemSignalI ( & psem - > sem ) ;
# endif
2013-05-24 15:26:52 +00:00
}
2018-11-03 02:29:30 +00:00
gThread gfxThreadCreate ( void * stackarea , gMemSize stacksz , gThreadpriority prio , GFX_THREAD_FUNCTION ( ( * fn ) , p ) , void * param )
2014-04-18 11:20:50 +00:00
{
2013-05-24 15:26:52 +00:00
if ( ! stackarea ) {
if ( ! stacksz ) stacksz = 256 ;
2018-07-07 07:05:18 +00:00
# if CH_KERNEL_MAJOR <= 3
2017-08-16 07:59:53 +00:00
return chThdCreateFromHeap ( 0 , stacksz , prio , ( tfunc_t ) fn , param ) ;
2018-07-07 07:05:18 +00:00
# else
2017-08-16 07:59:53 +00:00
return chThdCreateFromHeap ( 0 , stacksz , " ugfx " , prio , ( tfunc_t ) fn , param ) ;
# endif
2013-05-24 15:26:52 +00:00
}
2013-12-07 21:01:57 +00:00
if ( ! stacksz )
2013-12-21 03:21:59 +00:00
return 0 ;
2013-12-07 21:01:57 +00:00
2015-10-05 15:13:11 +00:00
return chThdCreateStatic ( stackarea , stacksz , prio , ( tfunc_t ) fn , param ) ;
2013-05-24 15:26:52 +00:00
}
# endif /* GFX_USE_OS_CHIBIOS */