firmware/main.c
author cpresser@slime-ws
Sat, 26 Jul 2008 14:22:38 +0200
changeset 25 96f051df5d60
parent 2 2f55e5dd591d
permissions -rw-r--r--
added eeprom code
     1 /*
     2  * Project Frontplatte
     3  *
     4  * main.c  -  main programm
     5  *
     6  * This file is released under the GNU General Public License. Refer
     7  * to the COPYING file distributed with this package.
     8  *
     9  * (c) 2007 Carsten Presser cpresser AT fsing.uni-sb.de
    10  */
    11 
    12 /** I N C L U D E S **********************************************************/
    13 #include <p18cxxx.h>
    14 #include "usb.h"
    15 #include "io_cfg.h"
    16 #include "usb_compile_time_validation.h"
    17 
    18 #include "delay.h"
    19 #include <pwm.h>
    20 #include "user.h"
    21 #include "rc5.h"
    22 #include "comm.h"
    23 #include "myi2c.h"
    24 #include "t6963.h"
    25 
    26 /** P R I V A T E  P R O T O T Y P E S ***************************************/
    27 void USBTasks(void);
    28 
    29 /** V E C T O R  R E M A P P I N G *******************************************/
    30 extern void _startup (void);        // See c018i.c in your C18 compiler dir
    31 #pragma code _RESET_INTERRUPT_VECTOR = 0x000800
    32 void _reset (void)
    33 {
    34     _asm goto _startup _endasm
    35 }
    36 
    37 
    38 /** C O D E ********************************************************/
    39 #pragma code
    40 
    41 void main(void)
    42 {
    43 	unsigned char i = 0;
    44 	// init system
    45     mInitializeUSBDriver();
    46     UserInit();
    47 
    48 	// main loop
    49     while(1)
    50     {
    51 // USB-Tasks first...
    52         USBTasks();
    53 
    54 		if (gg_ioflags & FLAG_I2C)
    55 		{
    56 			ReadKeys();
    57 			if (! (gg_ioflags & FLAG_KEY)) ReadClock();
    58 
    59 			gg_ioflags &= (FLAG_I2C ^ 255);					// delete i2c-flag
    60 		}
    61 
    62 
    63 // perhaps we need to power up?
    64 		if (((gg_ioflags & FLAG_IR) && (gg_ir == RC5_KEY_POWER)) || (gg_ioflags & FLAG_ALARM))
    65 		{
    66 			PIN_POWER	= 1;
    67 			Delay15ms();
    68 			PIN_POWER	= 0;
    69 			gg_ioflags |= FLAG_POWER;
    70 			// TODO: echo starting command??
    71 			gg_ioflags &= (FLAG_IR ^ 255);
    72 		}
    73 
    74 
    75 		// check for input, process it and send our answers...
    76 		if((usb_device_state >= CONFIGURED_STATE)&&(UCONbits.SUSPND!=1))
    77 		{
    78         	ProcessInput();
    79 		}
    80 
    81 // if in managed mode...
    82 		if (gg_mode == MODE_MANAGED)
    83 		{
    84 			if((usb_device_state >= CONFIGURED_STATE)&&(UCONbits.SUSPND!=1))
    85 			{
    86 				// wooo wooo woooo. now zoidberg is the popular one!
    87 				if (gg_ioflags & FLAG_KEY)
    88 				{
    89 					comm_send_ir(RC5_FRONT_ADDRESS, gg_keys);
    90 					gg_ioflags &= (FLAG_KEY ^ 255);
    91 				}
    92 				if (gg_ioflags & FLAG_IR)
    93 				{
    94 					comm_send_ir(gg_ir_address, gg_ir);
    95 					gg_ioflags &= (FLAG_IR ^ 255);
    96 				}
    97 			}
    98 			else
    99 			{
   100 				gg_mode = MODE_UNMANAGED;
   101 			}
   102 		} 
   103 		else
   104 		{
   105 			// we are NOT in usb-mode...
   106 			if (gg_ioflags & FLAG_KEY)
   107 			{
   108 				switch (gg_keys)
   109 				{
   110 					case RC5_KEY_UP:	
   111 						if (gg_pwm1dc <= 250) gg_pwm1dc += 5;
   112 						SetDCPWM1((int)gg_pwm1dc << 2);
   113 						break;
   114 
   115 					case RC5_KEY_DOWN:	
   116 						if (gg_pwm1dc >= 5) gg_pwm1dc -= 5;
   117 						SetDCPWM1((int)gg_pwm1dc << 2);
   118 						break;
   119 
   120 					default:
   121 						break;
   122 				}
   123 				gg_ioflags &= (FLAG_KEY ^ 255);
   124 			}
   125 
   126 			// always display some stuff...
   127 			if (gg_ioflags & FLAG_COUNTER)
   128 			{
   129 
   130 				DrawTime();
   131 /*				lcd_WriteData(0x00);		// byte1
   132 				lcd_WriteData(0x08);		// byte2
   133 				lcd_WriteCommand(0x24);		//set!
   134 				Wdebug(gg_pwm1dc);*/
   135 
   136 				gg_ioflags &= (FLAG_COUNTER ^ 255);
   137 			}
   138 
   139 		}
   140 
   141     } // end of main loop
   142 } 
   143 
   144 
   145 
   146 
   147 void USBTasks(void)
   148 {
   149     /*
   150      * Servicing Hardware
   151      */
   152     USBCheckBusStatus();                    // Must use polling method
   153     if(UCFGbits.UTEYE!=1)
   154         USBDriverService();                 // Interrupt or polling method
   155     
   156     #if defined(USB_USE_CDC)
   157     CDCTxService();
   158     #endif
   159 
   160 }