 /*******************************************
 * NAME       : Main.c
 *
 * DESCRIPTION : Main controlling routine
 *               for the preset controller.
 *******************************************/

#include "pcon.h"
#include "que.h"
#include "serial.h"

extern void key_msg();
extern void message_out();
extern void message_in();
extern void display();
extern void diag();

	/* at 08   Timer Control and Status Register:   */
extern unsigned char TCSR;
	/* at 09:0A  Counter: */
extern unsigned char COUNTER;
	/* at 0B:0C  Output Compare Register: */
extern unsigned char OCR;
	/* at 0D:0E  Input Capture Register: */
extern unsigned char ICR;
	/* at 0F   Port 3 Control and Status Register: */
extern unsigned char P3CSR;
	/* rate and mode control register: */
extern unsigned char RMCR;
	/* transmit/receive control register: */
extern unsigned char TRCSR;
	/* receive data register: */
extern unsigned char RDR;
	/* transmit data register: */
extern unsigned char TDR;

extern unsigned char P1DDR;
extern unsigned char P1DR;
extern unsigned char P2DDR;
extern unsigned char P2DR;

	/* for debugging */
extern unsigned char over, received;

struct sale_status key_buf;
struct rev_status  rev_stat;
struct config_table config;

struct g_queue  key_que;
struct g_queue  in_que;
struct g_queue  out_que;
struct g_queue  xmit_que;

int globaltime;
int flashtime;
unsigned char disable_on;
unsigned char test_stat;
unsigned char nozzle_up;
extern unsigned char enable_timer;

	/* date of release */
const char date[] = "022290";
	/* major revision number */
#define revis0 0
	/* minor revision number */
#define revis1 6

 /**************************************************
  * NAME       : main
  *
  * DESCRIPTION: call all the initialization of
  *              hardware and task setup then
  *              loops forever and calls each task
  *              in a round-robin fashion.  Each
  *              task will run until it suspends
  *              itself then return to main for
  *              the next task in the list.
  **************************************************/

void main()
{
   initsystem();
   initstructures();
   init_task(0,&display);
   init_task(1,&message_in);
   init_task(2,&message_out);
   init_task(3,&key_msg);
   clear_irq();
   while(true)
   {
      continue_task(0);
      continue_task(1);
      continue_task(2);
      continue_task(3);
   }
}


 /**************************************************
  * NAME       : initstructures
  *
  * DESCRIPTION: init the data structures for the
  *              firmware sets up all pointers and
  *              what ever else need doing
  **************************************************/

initstructures ()
{
        int i;

         /* init the key queue */
         key_que.insert = 0;
         key_que.remove = 0;
         key_que.empty  = true;

         /* init the in message que */
         in_que.insert = 0;
         in_que.remove = 0;
         in_que.empty  = true;

         /* init the out message que */
         out_que.insert = 0;
         out_que.remove = 0;
         out_que.empty  = true;

         /* init the Transmit que */
         xmit_que.insert = 0;
         xmit_que.remove = 0;
         xmit_que.empty  = true;

         /* init the all other data as needed */
}


 /***************************************************
  * NAME: initsystem
  *
  * DESCRIPTION:
  *  Init the counter timer chip for continous
  *  operation and to interupt at overflow.
  *  Init the serial port for receive and send and
  *  to generate an interrupt whenever the transmit
  *  buffer is empty and the receive buffer is full.
  ***************************************************/

#define ETOI 0x04

initsystem()
{
   /* set up the rate and mode control
      for internal clock and 9600     */
   RMCR = CC0 | SS0;

   /* set up the serial port to transmit and receive */
   TRCSR = normal_mode;

   /* set up the free running counter
      to interupt at overflow         */
   TCSR = ETOI;

   /* set up port 1 bits 0-3 as write only
      and bits 4-7 as read only            */
   P1DDR = 0x0f;

   /* set up port 2 bit 0 as a write bit
      and turn off the serial transmit buffer */
   P2DDR = 0x01;
   P2DR = 0xFF;
}

 /**************************************************
  * NAME       : set_irq, clear_irq
  *
  * DESCRIPTION: two routines that set the
  *              interupt mask and clear the
  *              interrupt mask in the condition
  *              code register of the 6801
  *************************************************/

/* routine to set the interupt mask */

set_irq()
{
   ;
   #asm
      sei
   #endasm
}

 /* routine to clear the interupt mask */

clear_irq()
{
   ;
   #asm
      cli
   #endasm
}
