This file is part of my co-routines framework for small microcontrollers
by Isaac Marino Bavaresco
This is file "C-R.h"
/*============================================================================*/
/*
Copyright (c) 2014, Isaac Marino Bavaresco
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Neither the name of the author nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*============================================================================*/
#if !defined __C_R_H__
#define __C_R_H__
/*============================================================================*/
/*----------------------------------------------------------------------------*/
#if defined __PICC__ /* HI-TECH PICC */
/*----------------------------------------------------------------------------*/
#include <pic.h>
#define DISABLEINTERRUPT() (GIE=0)
#define ENABLEINTERRUPT() (GIE=1)
#define CLEARWATCHDOG() CLRWDT()
typedef void mainreturn_t;
/*----------------------------------------------------------------------------*/
#elif defined __PICC18__ /* HI-TECH PICC18 */
/*----------------------------------------------------------------------------*/
#include <pic18.h>
#define DISABLEINTERRUPT() (GIE=0)
#define ENABLEINTERRUPT() (GIE=1)
#define CLEARWATCHDOG() CLRWDT()
typedef void mainreturn_t;
/*----------------------------------------------------------------------------*/
#elif defined __18CXX /* MPLAB-C18 or XC8 */
/*----------------------------------------------------------------------------*/
#include <p18cxxx.h>
#define DISABLEINTERRUPT() (PIE1bits.TMR1IE=0)
#define ENABLEINTERRUPT() (PIE1bits.TMR1IE=1)
#define CLEARWATCHDOG() ClrWdt()
typedef void mainreturn_t;
/*----------------------------------------------------------------------------*/
#elif defined __XC16__
/*----------------------------------------------------------------------------*/
#define DISABLEINTERRUPT() (IEC0bits.T1IE=0)
#define ENABLEINTERRUPT() (IEC0bits.T1IE=1)
#define CLEARWATCHDOG() ClrWdt()
typedef int mainreturn_t;
/*------------------------------------------------------------------------*/
#if defined __PIC24E__
/*------------------------------------------------------------------------*/
#include <p24exxxx.h>
#error Not working due to a suspected bug in XC16 compiler.
/*------------------------------------------------------------------------*/
#elif defined __dsPIC33E__
/*------------------------------------------------------------------------*/
#include <p33exxxx.h>
#error Not working due to a suspected bug in XC16 compiler.
/*------------------------------------------------------------------------*/
#elif defined __PIC24H__
/*------------------------------------------------------------------------*/
#include <p24hxxxx.h>
/*------------------------------------------------------------------------*/
#elif defined __PIC24F__ || defined __PIC24FK__
/*------------------------------------------------------------------------*/
#include <p24fxxxx.h>
/*------------------------------------------------------------------------*/
#elif defined __dsPIC30F__
/*------------------------------------------------------------------------*/
#include <p30fxxxx.h>
/*------------------------------------------------------------------------*/
#elif defined __dsPIC33F__
/*------------------------------------------------------------------------*/
#include <p33fxxxx.h>
/*------------------------------------------------------------------------*/
#else /* defined __dsPIC33F__ */
/*------------------------------------------------------------------------*/
#error Device not supported.
/*------------------------------------------------------------------------*/
#endif /* defined __PIC24E__ */
/*------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
#elif defined __PIC32MX__
/*----------------------------------------------------------------------------*/
#include <p32xxxx.h>
#define DISABLEINTERRUPT() (IEC0bits.T1IE=0)
#define ENABLEINTERRUPT() (IEC0bits.T1IE=1)
#define CLEARWATCHDOG()
typedef int mainreturn_t;
/*----------------------------------------------------------------------------*/
#endif /* defined __18CXX */
/*----------------------------------------------------------------------------*/
/*============================================================================*/
#include <string.h>
/*============================================================================*/
#define USE_POINTERS
/*============================================================================*/
#define BASIC_TICK_TYPE long
typedef unsigned BASIC_TICK_TYPE tickcount_t;
typedef signed BASIC_TICK_TYPE tickcompare_t;
typedef signed char taskreturn_t;
typedef taskreturn_t (*taskptr_t)( void );
extern volatile tickcount_t SystemTick;
#define INCREMENTTICK() (SystemTick++)
/*============================================================================*/
#define TaskEnd() }
#undef Sleep
#if 0
#define TaskStart() static int __State=0;switch(__State){default:case 0:
#define Yield(n) do{__State=__LINE__;return 0;case __LINE__:;}while(0)
#define Sleep(n,t) do{__State=__LINE__;_Sleep(t);return -1;case __LINE__:;}while(0)
#else
#define TaskStart() static unsigned char __State=0;switch(__State){default:case 0:
#define Yield(n) do{__State=(n);return 0;case (n):;}while(0)
#define Sleep(n,t) do{__State=(n);_Sleep(t);return -1;case (n):;}while(0)
#endif
#define Finish() do{__State=0;return -1;}while(0)
/*============================================================================*/
/*----------------------------------------------------------------------------*/
#if defined USE_POINTERS
/*----------------------------------------------------------------------------*/
typedef struct task_tag *ctxtptr_t;
typedef struct task_tag
{
taskptr_t TaskFunction;
ctxtptr_t Next;
ctxtptr_t Previous;
tickcount_t TimeToWake;
} task_t;
/*----------------------------------------------------------------------------*/
#else /* defined USE_POINTERS */
/*----------------------------------------------------------------------------*/
typedef unsigned char ctxtptr_t;
typedef struct
{
taskptr_t TaskFunction;
ctxtptr_t Next;
ctxtptr_t Previous;
unsigned char dummy; /* Just to make the structure length a power of two */
tickcount_t TimeToWake;
} task_t;
/*----------------------------------------------------------------------------*/
#endif /* defined USE_POINTERS */
/*----------------------------------------------------------------------------*/
/*============================================================================*/
extern task_t Tasks[];
extern const unsigned char __NumTasks;
#define DECLARETASKS(n) task_t Tasks[(n)];const unsigned char __NumTasks=(n)
/*============================================================================*/
ctxtptr_t CreateTask ( taskptr_t task );
void _Sleep ( tickcompare_t t );
taskreturn_t Task1 ( void );
void InitializeTimer ( void );
/*============================================================================*/
#endif /* !defined __C_R_H__ */
/*============================================================================*/