firmware/comm.c
changeset 2 2f55e5dd591d
child 23 dc247e80ef26
equal deleted inserted replaced
1:f08135942074 2:2f55e5dd591d
       
     1 /*
       
     2  * Project Frontplatte
       
     3  *
       
     4  * comm.c  -  Handle incoming commands
       
     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 
       
    13 /** I N C L U D E S **********************************************************/
       
    14 #include <p18cxxx.h>
       
    15 #include <pwm.h>
       
    16 #include "typedefs.h"
       
    17 #include "usb.h"
       
    18 #include "cdc.h"
       
    19 #include "io_cfg.h"
       
    20 #include "comm.h"
       
    21 #include "user.h"
       
    22 
       
    23 #include "delay.h"
       
    24 #include "t6963.h"
       
    25 #include "myi2c.h"
       
    26 #include "rc5.h"
       
    27 
       
    28 
       
    29 /** P R I V A T E  P R O T O T Y P E S ***************************************/
       
    30 void comm_send_data(unsigned char *data, unsigned char numbytes); 
       
    31 void comm_send_ack(void);
       
    32 void comm_send_nack(void); 
       
    33 void comm_send_nimp(void);
       
    34 
       
    35 
       
    36 
       
    37 /** C O D E ********************************************************/
       
    38 void ProcessInput(void)
       
    39 {   
       
    40 	unsigned char len;			// command length
       
    41 	unsigned char input[64];	// input buffer
       
    42 	unsigned char i;			// default counter
       
    43 
       
    44 
       
    45 /* C H E C K   I N P U T */
       
    46 	//do we have some data ?
       
    47     len=getsUSBUSART((char*)&(input[0]), 64); 
       
    48 
       
    49 	if (len < CMD_HD_LENGTH) return;
       
    50 
       
    51 	//check if input is valid
       
    52 	if (input[CMD_HD_SYNC] == CMD_SYNC_SEND)
       
    53 
       
    54 		if ((input[CMD_HD_LENGTH+1] == (len-4)) && (input[CMD_HD_LENGTH] == 0) )// TODO!!
       
    55 		{
       
    56 			// looks like we recieved a valid command...
       
    57 			switch(input[CMD_HD_COMMAND])
       
    58 			{
       
    59 /* S Y S T E M    C O M M A N D S */
       
    60 				case CMD_GET_VERSION:
       
    61 					if (input[CMD_HD_LENGTH+1] != 0) { comm_send_nack(); break;}
       
    62 					input[0] = FW_VERSION;
       
    63 					comm_send_data(input, 0x01);
       
    64 					break;
       
    65 
       
    66 				case CMD_BOOT:
       
    67 					if (input[CMD_HD_LENGTH+1] != 0) { comm_send_nack(); break;}
       
    68 					comm_send_ack();
       
    69 					// stolen from microchip forum
       
    70 					INTCONbits.GIE = 0;              // shut down global interrupts 
       
    71 					USBSoftDetach();
       
    72 					Delay15ms();
       
    73  					_asm goto BOOTLOAD_START _endasm;
       
    74 					break;
       
    75 				
       
    76 /* D I S P L A Y    C O M M A N D S */
       
    77 				case CMD_DISP_CLEAR_SCREEN:
       
    78 					if (input[CMD_HD_LENGTH+1] != 0) { comm_send_nack(); break;}
       
    79 					lcd_ClearScreen();
       
    80 					// now send ack
       
    81 					comm_send_ack();
       
    82 					break;
       
    83 
       
    84 				case CMD_DISP_SET_ROW_DATA:
       
    85 					if (input[CMD_HD_LENGTH+1] < 3) { comm_send_nack(); break;}
       
    86 					// goto desired address
       
    87 					lcd_WriteData(input[CMD_DATA_START]);	// lower byte
       
    88 					lcd_WriteData(input[CMD_DATA_START+1]);	// upper byte
       
    89 					lcd_WriteCommand(0x24);
       
    90 
       
    91 					// now write data
       
    92 					//lcd_SetAutomode(MODE_AUTO);
       
    93 					for(i = (CMD_DATA_START+2); i<=(len-1); i++) 
       
    94 					{
       
    95 						//lcd_WriteData(input[i]);
       
    96 						lcd_WriteByte(input[i]);
       
    97 					}
       
    98 					//lcd_SetAutomode(MODE_NOAUTO);
       
    99 					comm_send_ack();							// now send ack
       
   100 					break;
       
   101 
       
   102 				case CMD_DISP_SET_ADDRESS:
       
   103 					if (input[CMD_HD_LENGTH+1] != 2) { comm_send_nack(); break;}
       
   104 					// goto desired position
       
   105 					lcd_WriteData(input[CMD_DATA_START]);	// lower byte
       
   106 					lcd_WriteData(input[CMD_DATA_START+1]);	// upper byte
       
   107 					lcd_WriteCommand(0x24);
       
   108 					// now send ack
       
   109 					comm_send_ack();
       
   110 					break; 
       
   111 
       
   112 /* C L O C K    C O M M A N D S */
       
   113 				case CMD_READ_CLOCK:
       
   114 					if (input[CMD_HD_LENGTH+1] != 0) { comm_send_nack(); break;}
       
   115 					LDPageReadI2C(pcf8583, 0x00, input, 0x10);
       
   116 					comm_send_data(input, 0x10);
       
   117 					break;
       
   118 
       
   119 				case CMD_WRITE_CLOCK:
       
   120 					if (input[CMD_HD_LENGTH+1] != 0x10) { comm_send_nack(); break;}
       
   121 					LDPageWriteI2C(pcf8583, 0x00, &input[CMD_DATA_START], 0x10);
       
   122 					comm_send_ack();;
       
   123 					break;
       
   124 
       
   125 /* P W M    C O M M A N D S */
       
   126 				case CMD_SET_PWM1:
       
   127 					if (input[CMD_HD_LENGTH+1] != 1) { comm_send_nack(); break;}
       
   128 					SetDCPWM1((int)input[CMD_DATA_START] << 2);
       
   129 					comm_send_ack();
       
   130 					break;
       
   131 
       
   132 				case CMD_SET_PWM2:
       
   133 					if (input[CMD_HD_LENGTH+1] != 1) { comm_send_nack(); break;}
       
   134 					SetDCPWM2((int)input[CMD_DATA_START] << 2);
       
   135 					comm_send_ack();
       
   136 					break;
       
   137 
       
   138 /* M O D E    C O M M A N D S */
       
   139 				case CMD_SET_MODE_MANAGED:
       
   140 					if (input[CMD_HD_LENGTH+1] != 0) { comm_send_nack(); break;}
       
   141 					gg_mode = MODE_MANAGED;
       
   142 					comm_send_ack();
       
   143 					break;
       
   144 
       
   145 				case CMD_SET_MODE_UNMANAGED:
       
   146 					if (input[CMD_HD_LENGTH+1] != 0) { comm_send_nack(); break;}
       
   147 					gg_mode = MODE_UNMANAGED;
       
   148 					comm_send_ack();
       
   149 					break;
       
   150 
       
   151 
       
   152 				default:
       
   153 					// not implemented...
       
   154 					comm_send_nimp();
       
   155 					break;
       
   156 		}
       
   157 	}
       
   158 	else
       
   159 	{
       
   160 		// input data is somehow invalid
       
   161 		comm_send_nack();
       
   162 	}
       
   163 
       
   164 
       
   165 }//end ProcessInput
       
   166 
       
   167 
       
   168 
       
   169 
       
   170 
       
   171 
       
   172 
       
   173 
       
   174 
       
   175 /* S E N D   D A T A  &  R E T U R N C O D E S */
       
   176 void comm_send_data(unsigned char *data, unsigned char numbytes) 
       
   177 {
       
   178     // send a data packet
       
   179 	unsigned char cmd[64];
       
   180 	unsigned char i;
       
   181 	cmd[CMD_HD_SYNC] = CMD_SYNC_RECV;
       
   182 	cmd[CMD_HD_COMMAND] = CMD_SYS_ACK;
       
   183 	cmd[CMD_HD_LENGTH] = 0;
       
   184 	cmd[CMD_HD_LENGTH+1] = numbytes;
       
   185 
       
   186 	for (i=0; i<numbytes; i++) {
       
   187 		cmd[CMD_DATA_START + i] = data[i];
       
   188 	}
       
   189 
       
   190 	if mUSBUSARTIsTxTrfReady()
       
   191 		mUSBUSARTTxRam((byte*)&cmd[0],(4+numbytes));
       
   192 }
       
   193 
       
   194 
       
   195 void comm_send_ir(unsigned char address, unsigned char command) 
       
   196 {
       
   197     // send a data packet
       
   198 	unsigned char ir_cmd[7];
       
   199 	ir_cmd[CMD_HD_SYNC] 		= CMD_SYNC_RECV;
       
   200 	ir_cmd[CMD_HD_COMMAND] 		= CMD_SYS_IR;
       
   201 	ir_cmd[CMD_HD_LENGTH]		= 0x00;
       
   202 	ir_cmd[CMD_HD_LENGTH+1]		= 0x02;
       
   203 	ir_cmd[CMD_DATA_START]		= address;
       
   204 	ir_cmd[CMD_DATA_START+1]	= command;
       
   205 
       
   206 	if mUSBUSARTIsTxTrfReady()
       
   207 		mUSBUSARTTxRam((byte*)&ir_cmd[0],6);
       
   208 }
       
   209 
       
   210 
       
   211 void comm_send_ack(void) 
       
   212 {
       
   213     // send a ack-packet
       
   214 	unsigned char cmd[5];
       
   215 	cmd[CMD_HD_SYNC] = CMD_SYNC_RECV;
       
   216 	cmd[CMD_HD_COMMAND] = CMD_SYS_ACK;
       
   217 	cmd[CMD_HD_LENGTH] = 0;
       
   218 	cmd[CMD_HD_LENGTH+1] = 0;
       
   219 
       
   220 	if mUSBUSARTIsTxTrfReady()
       
   221 		mUSBUSARTTxRam((byte*)&cmd[0],4);
       
   222 }
       
   223 
       
   224 void comm_send_nack(void) 
       
   225 {
       
   226     // send a NOTack-packet
       
   227 	unsigned char cmd[5];
       
   228 	cmd[CMD_HD_SYNC] = CMD_SYNC_RECV;
       
   229 	cmd[CMD_HD_COMMAND] = CMD_SYS_NACK;
       
   230 	cmd[CMD_HD_LENGTH] = 0;
       
   231 	cmd[CMD_HD_LENGTH+1] = 0;
       
   232 
       
   233 	while (!mUSBUSARTIsTxTrfReady()) Nop();
       
   234 	mUSBUSARTTxRam((byte*)&cmd[0],4);
       
   235 }
       
   236 
       
   237 void comm_send_nimp(void) 
       
   238 {
       
   239 	// send a NOTimplementet-packet
       
   240 	unsigned char cmd[3];
       
   241 	cmd[CMD_HD_SYNC] = CMD_SYNC_RECV;
       
   242 	cmd[CMD_HD_COMMAND] = CMD_SYS_NIMP;
       
   243 	cmd[CMD_HD_LENGTH] = 0;
       
   244 	cmd[CMD_HD_LENGTH+1] = 0;
       
   245 
       
   246 	if mUSBUSARTIsTxTrfReady()
       
   247 		mUSBUSARTTxRam((byte*)&cmd[0],4);
       
   248 }