// kernel.h


#ifndef _KERNEL_H_
#define _KERNEL_H_




// SYSTEM CONFIGURATION CONSTANTS
/* these must be set correctly for the code 
   to operate properly, ...or at all.  */
#define KERNEL_ERROR_MSGS     	 	   1
//#define KDB_TRACE_LEVEL_1	 	 	   1		
//#define KDB_TRACE_LEVEL_2    	 	   1		
#define KDB_CYCLES			 	 	   4
#define RAM_SIZE    		 	 	   1536
#define GLOBAL_VAR_SPACE			   134
#define KERNEL_STACK_SIZE			   128
#define DEFAULT_STACK_SIZE	 	 	   64
#define DEFAULT_SHELL_STACK_SIZE 	   350
#define USED_RAM    		 	 	   (KERNEL_STACK_SIZE + GLOBAL_VAR_SPACE)
#define INITIAL_HEAP_SIZE    	 	   (RAM_SIZE - USED_RAM)
#define MAXTASKS     		 	 	   8
#define MAXMUTEXES					   4

#define uS_PER_SYSTEM_TICK			   65535
#define uS_PER_TIME_TICK			   16384
#define ms_PER_TIME_TICK			   (16)
#define uS_PER_SECOND				   1000000
#define mS_PER_SECOND				   1000
#define mS_PER_MINUTE				   (60 * mS_PER_SECOND)
#define mS_PER_HOUR					   (60 * mS_PER_MINUTE)
#define mS_PER_DAY					   (24 * mS_PER_HOUR)
#define TIME_TICKS_PER_SECOND		   (uS_PER_SECOND / uS_PER_TIME_TICK)
#define TIME_TICKS_PER_HOUR			   (TIME_TICKS_PER_SECOND * 3600)
#define TIME_TICKS_PER_DAY			   (TIME_TICKS_PER_SECOND * 86400)



// RESOURCE TYPE DEFINITIONS
#ifndef ADC0
#define ADC0   	   0
#endif
#ifndef ADC1
#define ADC1 	   1
#endif

#ifndef ECTP
#define ECTP 	   2
#endif

#ifndef COM1
#define COM1 	   3
#endif
#ifndef COM2
#define COM2 	   4
#endif
#ifndef SPI1
#define SPI1 	   5
#endif
#ifndef CAN0
#define CAN0 	   6
#endif

#ifndef PWM0
#define PWM0 	   7
#endif
#ifndef PWM1
#define PWM1 	   8
#endif
#ifndef PWM2
#define PWM2 	   9
#endif
#ifndef PWM3
#define PWM3 	   10
#endif




// MACROS
#ifndef COP_OPS
// set COP to time out at 1 second
#define COP_ON()  	   COPCTL = 0xC6
#define COP_OFF()	   COPCTL = 0x00
#define COP_PET(x)     COPRST = (x)
#endif




// ENUMERATIONS
enum task_state { 	 	IDLE	 = 0, // capitalize all these
	 			  		PENDING	 = 1, 
				  		RUNNING	 = 2, 
				  		WAITING	 = 3, 
				  		STOPPED	 = 4
				};
enum mutex_state { 		NOTBUSY	 = 0, 
	 				  	BUSY	 = 1
					};
enum message_box { 	  	STATE_FLAG 	  = 0x01, // 0000 0001
	 			   		PRIO_FLAG 	  = 0x02  // 0000 0010
				 };  	// =4, =8, =16, etc.
				 
enum message_box_data { STATE_BOX  	  = 0, 
	 				  	PRIO_BOX 	  = 1 
					  }; // =2, =3, =4, etc.




// FUNCTION PROTOTYPES
void kernel_init(void);
int if_task_exists(unsigned char id);
int get_task_address(unsigned char id);
char get_task_id(void);
char *get_task_name(unsigned char id);
int get_task_state(unsigned char id);
int get_task_priority(unsigned char id);
int get_task_messages(unsigned char id);
int set_task_state(unsigned char id, unsigned char state);
int set_task_priority(unsigned char id, unsigned char priority);
int create_task(char *name, int (*addr)(), unsigned char priority, int state, int stack);
int remove_task(unsigned char id);

char *get_mutex_name(unsigned char id);
int get_mutex_state(unsigned char id);
int get_mutex_owner(unsigned char id);
int get_mutex_queuelen(unsigned char id);
int create_mutex(unsigned char type, char *name);
int get_mutex(unsigned char type);
int give_mutex(unsigned char type);

int get_free_memory(void);

unsigned long int get_sysTime(void);
void set_sysTime(unsigned int hours, unsigned int minutes, unsigned int seconds);


/*
unsigned long get_time(void);
void set_time(unsigned char hour, unsigned char min, unsigned char sec);
char* get_os_version();
*/



#endif

