firmware/main.c
changeset 2 2f55e5dd591d
child 25 96f051df5d60
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/firmware/main.c	Tue Jan 29 22:31:52 2008 +0100
     1.3 @@ -0,0 +1,160 @@
     1.4 +/*
     1.5 + * Project Frontplatte
     1.6 + *
     1.7 + * main.c  -  main programm
     1.8 + *
     1.9 + * This file is released under the GNU General Public License. Refer
    1.10 + * to the COPYING file distributed with this package.
    1.11 + *
    1.12 + * (c) 2007 Carsten Presser cpresser AT fsing.uni-sb.de
    1.13 + */
    1.14 +
    1.15 +/** I N C L U D E S **********************************************************/
    1.16 +#include <p18cxxx.h>
    1.17 +#include "usb.h"
    1.18 +#include "io_cfg.h"
    1.19 +#include "usb_compile_time_validation.h"
    1.20 +
    1.21 +#include "delay.h"
    1.22 +#include <pwm.h>
    1.23 +#include "user.h"
    1.24 +#include "rc5.h"
    1.25 +#include "comm.h"
    1.26 +#include "myi2c.h"
    1.27 +#include "t6963.h"
    1.28 +
    1.29 +/** P R I V A T E  P R O T O T Y P E S ***************************************/
    1.30 +void USBTasks(void);
    1.31 +
    1.32 +/** V E C T O R  R E M A P P I N G *******************************************/
    1.33 +extern void _startup (void);        // See c018i.c in your C18 compiler dir
    1.34 +#pragma code _RESET_INTERRUPT_VECTOR = 0x000800
    1.35 +void _reset (void)
    1.36 +{
    1.37 +    _asm goto _startup _endasm
    1.38 +}
    1.39 +
    1.40 +
    1.41 +/** C O D E ********************************************************/
    1.42 +#pragma code
    1.43 +
    1.44 +void main(void)
    1.45 +{
    1.46 +	unsigned char i = 0;
    1.47 +	// init system
    1.48 +    mInitializeUSBDriver();
    1.49 +    UserInit();
    1.50 +
    1.51 +	// main loop
    1.52 +    while(1)
    1.53 +    {
    1.54 +// USB-Tasks first...
    1.55 +        USBTasks();
    1.56 +
    1.57 +		if (gg_ioflags & FLAG_I2C)
    1.58 +		{
    1.59 +			ReadKeys();
    1.60 +			if (! (gg_ioflags & FLAG_KEY)) ReadClock();
    1.61 +
    1.62 +			gg_ioflags &= (FLAG_I2C ^ 255);
    1.63 +		}
    1.64 +
    1.65 +
    1.66 +// perhaps we need to power up?
    1.67 +		if (((gg_ioflags & FLAG_IR) && (gg_ir == RC5_KEY_POWER)) || (gg_ioflags & FLAG_ALARM))
    1.68 +		{
    1.69 +			PIN_POWER	= 1;
    1.70 +			Delay15ms();
    1.71 +			PIN_POWER	= 0;
    1.72 +			gg_ioflags |= FLAG_POWER;
    1.73 +			// TODO: echo starting command??
    1.74 +			gg_ioflags &= (FLAG_IR ^ 255);
    1.75 +		}
    1.76 +
    1.77 +
    1.78 +		// check for input, process it and send our answers...
    1.79 +		if((usb_device_state >= CONFIGURED_STATE)&&(UCONbits.SUSPND!=1))
    1.80 +		{
    1.81 +        	ProcessInput();
    1.82 +		}
    1.83 +
    1.84 +// if in managed mode...
    1.85 +		if (gg_mode == MODE_MANAGED)
    1.86 +		{
    1.87 +			if((usb_device_state >= CONFIGURED_STATE)&&(UCONbits.SUSPND!=1))
    1.88 +			{
    1.89 +				// wooo wooo woooo. now zoidberg is the popular one!
    1.90 +				if (gg_ioflags & FLAG_KEY)
    1.91 +				{
    1.92 +					comm_send_ir(RC5_FRONT_ADDRESS, gg_keys);
    1.93 +					gg_ioflags &= (FLAG_KEY ^ 255);
    1.94 +				}
    1.95 +				if (gg_ioflags & FLAG_IR)
    1.96 +				{
    1.97 +					comm_send_ir(gg_ir_address, gg_ir);
    1.98 +					gg_ioflags &= (FLAG_IR ^ 255);
    1.99 +				}
   1.100 +			}
   1.101 +			else
   1.102 +			{
   1.103 +				gg_mode = MODE_UNMANAGED;
   1.104 +			}
   1.105 +		} 
   1.106 +		else
   1.107 +		{
   1.108 +			// we are NOT in usb-mode...
   1.109 +			if (gg_ioflags & FLAG_KEY)
   1.110 +			{
   1.111 +				switch (gg_keys)
   1.112 +				{
   1.113 +					case RC5_KEY_UP:	
   1.114 +						if (gg_pwm1dc <= 250) gg_pwm1dc += 5;
   1.115 +						SetDCPWM1((int)gg_pwm1dc << 2);
   1.116 +						break;
   1.117 +
   1.118 +					case RC5_KEY_DOWN:	
   1.119 +						if (gg_pwm1dc >= 5) gg_pwm1dc -= 5;
   1.120 +						SetDCPWM1((int)gg_pwm1dc << 2);
   1.121 +						break;
   1.122 +
   1.123 +					default:
   1.124 +						break;
   1.125 +				}
   1.126 +				gg_ioflags &= (FLAG_KEY ^ 255);
   1.127 +			}
   1.128 +
   1.129 +			// always display some stuff...
   1.130 +			if (gg_ioflags & FLAG_COUNTER)
   1.131 +			{
   1.132 +
   1.133 +				DrawTime();
   1.134 +/*				lcd_WriteData(0x00);		// byte1
   1.135 +				lcd_WriteData(0x08);		// byte2
   1.136 +				lcd_WriteCommand(0x24);		//set!
   1.137 +				Wdebug(gg_pwm1dc);*/
   1.138 +
   1.139 +				gg_ioflags &= (FLAG_COUNTER ^ 255);
   1.140 +			}
   1.141 +
   1.142 +		}
   1.143 +
   1.144 +    } // end of main loop
   1.145 +} 
   1.146 +
   1.147 +
   1.148 +
   1.149 +
   1.150 +void USBTasks(void)
   1.151 +{
   1.152 +    /*
   1.153 +     * Servicing Hardware
   1.154 +     */
   1.155 +    USBCheckBusStatus();                    // Must use polling method
   1.156 +    if(UCFGbits.UTEYE!=1)
   1.157 +        USBDriverService();                 // Interrupt or polling method
   1.158 +    
   1.159 +    #if defined(USB_USE_CDC)
   1.160 +    CDCTxService();
   1.161 +    #endif
   1.162 +
   1.163 +}