/******************************************************************** FileName: helpers.c Dependencies: See INCLUDES section Processor: PIC18 USB Microcontrollers Hardware: Complier: Microchip C18 (for PIC18), C30 (for PIC24), C32 (for PIC32) Company: ******************************************************************** File Description: Change History: Rev Description ---- ----------------------------------------- 1.0 In developement ********************************************************************/ // http://www.robot-electronics.co.uk/htm/usb_gpio12_tech.htm /* USB Con END 1 - Pull low at power-up for boot-loader mode, RA3 - 1 2 - Red Led, CCP1/RC5 - 2 3 - Green Led, RC4 - 3 4 - RC3/AN7 - 4 5 - RC6/AN8 - 5 6 - RC7/AN9/SDO - 6 7 - RB7/Tx - 7 8 - 0v Ground - 8 */ /* USB Con END 16 - 5v Power from USB Cable 15 - RC0/AN4 14 - RC1/AN5 13 - RC2/AN6 12 - RB4/AN10/SDI/SDA 11 - RB5/AN11/Rx 10 - RB6/SCK/SCL 9 - 0v Ground */ /** INCLUDES *******************************************************/ /* #include "usb.h" #include "usb_function_cdc.h" #include "HardwareProfile.h" */ #include "usb.h" #include "usb_function_cdc.h" /* #include "HardwareProfile.h" */ #include #include "helpers.h" /* the code below is allowed */ extern int hlp_count; extern int hlp_count; int hlp_count; /********************************************************************/ void toHex( unsigned n, char * buff2 ){ /* convert a byte to two hex chars and put in buffer */ unsigned nibble; nibble = ( (n >> 4) & 0x0F ); if ( nibble >9 ){ buff2[0] = (char) ('A'-10) + nibble; } else { buff2[0] = (char) '0'+nibble ; } nibble = ( n & 0x0F ); if ( nibble > 9 ){ buff2[1] = (char) ('A'-10) + nibble; } else { buff2[1] = (char) '0'+nibble ; } } /* RSasciiToNibble MOVWF RSin ; if 0..1 then 0x30 to 0x39 ; if A..F then 0x41 to 0x46 ; if a..f then 0x61 to 0x66 ; test if bit 4 set and assume a letter or number BTFSS RSin,4 ADDLW 0x09 ; letter so add 9 ANDLW 0x0F ; mask of nibble return */ int toNum( char * buff ){ unsigned num; unsigned nibble; char chr; /* convert 2 ascii char to number */ chr = buff[0]; if ( isdigit( chr ) ){ nibble = chr - '0' ; } else { nibble = chr - 'A' +10 ; } nibble &= 0x0F; num = nibble << 4; /* convert 2 ascii char to number */ chr = buff[1]; if ( isdigit( chr ) ){ nibble = chr - '0' ; } else { nibble = chr - 'A' + 10 ; } nibble &= 0x0F; num += nibble; return num; } int tolook( int offset ){ unsigned num; BYTE * bP; bP = ( BYTE * )( 0x0F00 ); num = bP[offset]; return num ; } void put( int offset, unsigned num ){ BYTE * bP; bP = (BYTE * )( 0x0F00 ); bP[offset] = num & 255 ; } /*===================================================================*/ BYTE buildLine( char ipChar ){ BYTE cmdFound; if ( ( ipChar == 0x0A ) || ( ipChar == 0x0D ) ) { // don't save CRLF cmdFound = 1; } else { if ( buffIndex < MAXBUFF ) { buff[ buffIndex ] = ipChar ; buffIndex ++; } } if ( ipChar == ';' ) { cmdFound = 2; } if ( ipChar == '?' ) { putrsUSBUSART("\nHello World - ? pressed \n"); } return cmdFound ; } /*======================================================================*/ void processBuffer( BYTE cmdFound ){ BYTE regVal; BYTE numBytesRead; BYTE numBytesToSend; BYTE cmd_addr; BYTE cmd_data; BYTE tmp; if ( cmdFound ) { /* hlp_count = 0; */ cmdFound = 0; // we really need to padd out the buffer while( buffIndex < MAXBUFF ) { buff[ buffIndex ] = 0x20 ; buffIndex++ ; } buff[ MAXBUFF - 1 ] = 0x00 ; buffIndex = 0; /* run cmd */ USB_In_Buffer[0] = 0x0a ; USB_In_Buffer[1] = 0x0d ; USB_In_Buffer[2] = '|' & 127 ; USB_In_Buffer[3] = buff[ 0 ] & 127 ; // output received character to output port. // LATC = ( USB_In_Buffer[3] * 16 ) & 255; USB_In_Buffer[4] = buff[ 1 ] & 127 ; USB_In_Buffer[5] = buff[ 2 ] & 127 ; USB_In_Buffer[6] = buff[ 3 ] & 127 ; USB_In_Buffer[7] = buff[ 4 ] & 127 ; USB_In_Buffer[8] = buff[ 5 ] & 127 ; USB_In_Buffer[9] = buff[ 6 ] & 127 ; USB_In_Buffer[10] = buff[ 7 ] & 127 ; USB_In_Buffer[11] = '|' & 127 ; toHex(TMR1H,&USB_In_Buffer[12]); /* output TMR 1 registers, not a clean read */ toHex(TMR1L,&USB_In_Buffer[14]); USB_In_Buffer[16] = '|' & 127 ; toHex(ADRESH,&USB_In_Buffer[17]); /* output AtoD registers */ toHex(ADRESL,&USB_In_Buffer[19]); USB_In_Buffer[21] = '|' & 127 ; /* always do rAA */ regVal = toNum( &buff[1] ); /* CDD add DD to 0xF00 to get contents of SFR */ toHex(regVal,&USB_In_Buffer[22] ); USB_In_Buffer[24] = '|' & 127 ; regVal = tolook( regVal ); toHex(regVal,&USB_In_Buffer[25]); USB_In_Buffer[27] = '|' & 127 ; toHex(hlp_count,&USB_In_Buffer[28]); /* output AtoD registers */ USB_In_Buffer[30] = '|' & 127 ; toHex(buttonCount,&USB_In_Buffer[31]); /* output AtoD registers */ USB_In_Buffer[33] = '|' & 127 ; toHex(buttonCount2,&USB_In_Buffer[34]); /* output AtoD registers */ USB_In_Buffer[36] = '|' & 127 ; USB_In_Buffer[37] = ' ' & 127 ; if ( buff[0] == 'W' ){ /* write command wAADD */ cmd_addr = toNum( &buff[1] ); cmd_data = toNum( &buff[3] ); toHex( cmd_data ,&USB_In_Buffer[25] ); put( cmd_addr,cmd_data ); } if ( buff[0] == 'v' ){ /* write command wAADD */ cmd_addr = toNum( &buff[1] ); cmd_data = toNum( &buff[3] ); LATC = ( cmd_data ) & 255; } // btoa(ADRES,&USB_In_Buffer[15]); // if sprintf used, device is not found //if ( 1==0 ){ // sprintf(&USB_In_Buffer[11], ( const far rom char * ) "%02X|", TMR2 ); // sprintf(&USB_In_Buffer[11], ( const far rom char * ) "%04X|", TMR1H ); // sprintf(&USB_In_Buffer[11], ( const far rom char * ) "%04X|", ADRES ); //} // kick off an AtoD. This is the wrong place ADCON0 = ADCON0 | 2 ; numBytesToSend = 37; buffIndex = 0; // if( USBUSARTIsTxTrfReady() ){ putUSBUSART(USB_In_Buffer,numBytesToSend); // } cmdFound = 0; } }