void getBytes(unsigned char bytes, unsigned char start){ unsigned char i; for(i=0;i<bytes; i++){ input_buffer[start]=UART1RX(); start++; } }
int main(void){ //main function, execution starts here char c; AD1PCFGL = 0xFFFF; //digital pins //setup internal clock for 80MHz/40MIPS //7.37/2=3.685*43=158.455/2=79.2275 CLKDIVbits.PLLPRE=0; // PLLPRE (N2) 0=/2 PLLFBD=41; //pll multiplier (M) = +2 CLKDIVbits.PLLPOST=0;// PLLPOST (N1) 0=/2 while(!OSCCONbits.LOCK);//wait for PLL ready //uart //UART can be placed on any RPx pin //we need to configure it for RP14/RP15 to use the FTDI usb->serial converter //you could also output one (or both) of the two available UARTS to the I/O header //assign pin 14 to the UART1 RX input register //RX PR14 (input) U1RXR_I = 14; //assign UART1 TX function to the pin 15 output register //TX RP15 (output) RP15_O=U1TX_O; //InitializeUART1(); //setup UART U1BRG = 85;//86@80mhz, [email protected]=115200 U1MODE = 0; //clear mode register U1MODEbits.BRGH = 1; //use high percison baud generator U1STA = 0; //clear status register U1MODEbits.UARTEN = 1; //enable the UART RX IFS0bits.U1RXIF = 0; //clear the receive flag //setup LEDs SD_TRIS = 0; //set pin direction to output IO1_TRIS = 0; LD1_TRIS = 0; SD_O = 1; //set all pins high (LED on) LD1_O = 1; IO1_O=1; while(1){//never ending loop if(UART1RXRdy()==1){//check for data waiting c=UART1RX();//get the character if(c=='0'){ //LATA &=(~0b11100000000); LD1_O = 0; IO1_O=0; SD_O = 0; }else if(c=='1'){ LD1_O = 1; }else if(c=='2'){ IO1_O=1; }else if(c=='3'){ SD_O = 1; } UART1TX(c);//echo the character back } } }
//this loop services user input and passes it to be processed on <enter> int main(void){ CLKDIVbits.RCDIV0=0; //clock divider to 0 AD1PCFG = 0xFFFF; // Default all pins to digital OSCCONbits.SOSCEN=0; //Oscilator setup, registar-OSCON bits-SOSCEN MODE_LED_DIR = 0; //Mode LED set as output VREG_DIR =0; //VREG is set as output VREG_EN = 1; //Sets the VREG pin to 1, turns on the Voltage regulators. MODE_LED = 1; //Turns the MOD LED 0N InitializeUART1(); //Initialize UART1 initADC(); //Initialise ADC unsigned int voltage; /////FOREVER///LOOP////////// while(1) { if(UART1RX() == 'a') //Checks if the recived byte is 'a' if so it sends the two RAW bytes form ADC { voltage = getADC(); //reads the ADC pin UART1TX(voltage>>8); //seds the top byte of ADC to UART UART1TX(voltage); //sends the bottom byte of ADC to UART } }
// measure constantly void bpADCCprobe(void) { unsigned int temp; //bpWline(OUMSG_PS_ADCC); BPMSG1042; //bpWline(OUMSG_PS_ANY_KEY); BPMSG1250; //BPMSG1043; //bpWstring(OUMSG_PS_ADC_VOLT_PROBE); BPMSG1044; bpWvolts(0); // print dummy (0v) //bpWstring(OUMSG_PS_ADC_VOLTS); BPMSG1045; while(!UART1RXRdy()) // wait for keypress { AD1CON1bits.ADON = 1; // turn ADC ON temp=bpADC(BP_ADC_PROBE); AD1CON1bits.ADON = 0; // turn ADC OFF bpWstring("\x08\x08\x08\x08\x08"); // 5x backspace ( e.g. 5.00V ) //BPMSG1046; bpWvolts(temp); // print measurement //bpWstring(OUMSG_PS_ADC_VOLTS); BPMSG1045; // CvD: wait xx ms?? } UART1RX(); bpWline(""); // need a linefeed :D }
int16_t main(void) { char c = 'g'; /* Configure the oscillator for the device */ ConfigureOscillator(); /* Initialize IO ports and peripherals */ InitApp(); hd44780_init(N_2LINE,FONT_8); //hd44780_setCursorPosition(0, 0); hd44780_write_char(' '); hd44780_write_char('G'); hd44780_write_char('K'); hd44780_write_string("hat DE Lieb"); while(1) { if(UART1RXRdy()==1){//check for data waiting c=UART1RX();//get the character if(c=='0') { T1CONbits.TON = ~T1CONbits.TON; /* enable Timer 1 and start the count */ if(!T1CONbits.TON) //if timer is off, turn off LED { UART1TX('T'); UART1TX('1'); UART1TX('-'); UART1TX('O'); UART1TX('F'); UART1TX('F'); UART1TX('\n'); UART1TX('\r'); MSLED_O = 0; } else { UART1TX('T'); UART1TX('1'); UART1TX('-'); UART1TX('O'); UART1TX('N'); UART1TX('\n'); UART1TX('\r'); MSLED_O=1; } } else { UART1TX(c);//echo the character back hd44780_write_char(c); } } } }
//self test, showProgress=1 displays the test results in the terminal, set to 0 for silent mode //errors are counted in the global errors variable //returns number of errors unsigned char selfTest(unsigned char showProgress, unsigned char jumperTest){ //toggle display of test results with show Progress variable errors=0; if(!showProgress) bpConfig.quiet=1; //instructions (skip pause if no display output) if(showProgress && jumperTest){ //bpPOSTWline("Disconnect any devices"); //bpPOSTWline("Connect (Vpu to +5V) and (ADC to +3.3V)"); BPMSG1163; BPMSG1251; // //bpPOSTWline("Press a key to start"); //JTR Not required while(!UART1RXRdy()); //wait for key UART1RX();//discard byte } //bpPOSTWline("Ctrl"); BPMSG1164; BP_AUX0=1; BP_AUX0_DIR=0; //bpPOSTWstring("AUX"); BPMSG1165; bpTest(BP_AUX0,1); BP_AUX0=0; BP_AUX0_DIR=1; BP_LEDMODE=1; BP_LEDMODE_DIR=0; //bpPOSTWstring("MODE LED"); BPMSG1166; bpTest(BP_LEDMODE,1); BP_LEDMODE=0; BP_PULLUP_ON(); //bpPOSTWstring("PULLUP H"); BPMSG1167; bpTest(BP_PULLUP,1); BP_PULLUP_OFF(); //bpPOSTWstring("PULLUP L"); BPMSG1168; bpTest(BP_PULLUP,0); BP_VREG_ON(); bpDelayMS(2);//in silent mode there's not enought delay for the power supplied to come on //bpPOSTWstring("VREG"); BPMSG1169; bpTest(BP_VREGEN,1); #if defined (BUSPIRATEV4) BPMSG1265; //bpWline("EEPROM"); BPMSG1266; //bpWstring("SCL"); bpTest(BP_EE_SCL, 1); BPMSG1267; //bpWstring("SDA"); bpTest(BP_EE_SDA, 1); BPMSG1268; //bpWstring("WP"); bpTest(BP_EE_WP, 1); BPMSG1269; //bpWstring("ACK"); bpTest(eetest(), 0); #endif //ADC check //bpPOSTWline("ADC and supply"); BPMSG1170; ADCON(); // turn ADC ON #if defined (BUSPIRATEV4) BPMSG1270; //bpWstring("Vusb"); bpADCPinTest(BP_ADC_USB,V5L, V5H); //bpPOSTWstring("5V"); BPMSG1171; bpADCPinTest(BP_ADC_5V0,V5L, V5H); //enable 5v0 pullup and test BP_5VPU_ON(); BPMSG1171; //bpWstring("5V0 VPU"); bpWstring(" "); BPMSG1172; //VPU bpDelayMS(2); bpADCPinTest(BP_ADC_VPU,V5L, V5H); BP_5VPU_OFF(); //ADC test (3.3 volts) if(jumperTest){ //ADC is connected to 3.3 volts //bpPOSTWstring("ADC"); BPMSG1174; bpADCPinTest(BP_ADC_PROBE,V33L, V33H); } //bpPOSTWstring("3.3V"); BPMSG1173; bpADCPinTest(BP_ADC_3V3,V33L, V33H); //enable 3v3 pullup and test BP_3V3PU_ON(); BPMSG1173; //bpWstring("3V3 VPU"); bpWstring(" "); BPMSG1172; //VPU bpDelayMS(2); bpADCPinTest(BP_ADC_VPU,V33L, V33H); BP_3V3PU_OFF(); #elif defined (BUSPIRATEV3) //v3 test //0x030F is 5volts //bpPOSTWstring("5V"); BPMSG1171; bpADCPinTest(BP_ADC_5V0,V5L, V5H); if(jumperTest){ //Vpullup is connected to 5volts //bpPOSTWstring("VPU"); BPMSG1172; bpADCPinTest(BP_ADC_VPU,V5L, V5H); } //0x0208 is 3.3volts //bpPOSTWstring("3.3V"); BPMSG1173; bpADCPinTest(BP_ADC_3V3,V33L, V33H); if(jumperTest){ //ADC is connected to 3.3volts //bpPOSTWstring("ADC"); BPMSG1174; bpADCPinTest(BP_ADC_PROBE,V33L, V33H); } #endif ADCOFF(); // turn ADC OFF //************* // // Test bus pins three ways, also tests on-board pullup resistors: // 1. normal/high, 2. open collector ground, 3. open collector high. // //*************** //pullup off, pins=output & high, read input, high? //bpPOSTWline("Bus high"); BPMSG1175; IODIR&= ~(ALLIO);//output IOLAT|=ALLIO; //high bpDelayMS(100); bpBusPinsTest(1); //pullup on, pins=output & low, read input, low? //bpPOSTWline("Bus Hi-Z 0"); BPMSG1176; IOLAT&= ~(ALLIO); //low if(jumperTest){ #if defined (BUSPIRATEV4) BP_3V3PU_ON(); #endif BP_PULLUP_ON(); } bpDelayMS(100); bpBusPinsTest(0); if(jumperTest){ //pullup on, pins=input & low, read input, high? //bpPOSTWline("Bus Hi-Z 1"); BPMSG1177; IODIR|=ALLIO;//output bpDelayMS(100); bpBusPinsTest(1); #if defined (BUSPIRATEV4) BP_3V3PU_OFF(); #endif } //instructions (skip pause if no display output) if(showProgress && jumperTest){ BP_VREG_ON(); BP_MODELED_ON(); //bpPOSTWline("MODE and VREG LEDs should be on! Any key exits."); #if defined (BUSPIRATEV4) BP_USBLED_ON(); #endif BPMSG1178; BPMSG1250; //JTR Not required while(!UART1RXRdy()); UART1RX(); #ifdef BUSPIRATEV4 BP_USBLED_OFF(); #endif BP_MODELED_OFF(); BP_VREG_OFF(); } bpInit();//clean up BPMSG1179; bpWdec(errors); BPMSG1180; bpConfig.quiet=0; return errors; }
void binpic(void) { unsigned char cmd; int ok; unsigned int temp; bpWstring("PIC1"); modeConfig.HiZ=1; // to allow different Vcc bbL(MOSI|CLK, PICSPEED); // pull both pins to 0 before applying Vcc and Vpp picmode=PICMODE6; piccmddelay=2; while(1) { cmd=UART1RX(); switch(cmd&0xC0) { case 0x00: ok=1; switch(cmd&0xF0) { case 0x00: switch(cmd) { case 0x00: return; case 0x01: bpWstring("PIC1"); break; case 0x02: picmode=PICMODE6; break; case 0x03: picmode=PICMODE4; break; case 0x04: case 0x05: case 0x06: case 0x07: piccmddelay=(cmd-0x04); break; default: ok=0; } break; case 0x10: if(cmd&0x08) { if(cmd&0x04) { bbH(AUX ,5); } else { bbL(AUX ,5); } if(cmd&0x02) { bbH(MISO ,5); } else { bbL(MISO ,5); } if(cmd&0x01) { bbH(CS ,5); } else { bbL(CS ,5); } } else { if(cmd&0x04) // pwm? { PWMfreq=100; PWMduty=50; updatePWM(); } else { PWMfreq=0; updatePWM(); } if(cmd&0x02) // vreg on { BP_VREG_ON(); //modeConfig.vregEN=1; } else { BP_VREG_OFF(); //modeConfig.vregEN=0; } if(cmd&0x01) // pullup on #ifndef BUSPIRATEV1A { BP_PULLUP_ON(); // modeConfig.pullupEN=1; } else { BP_PULLUP_OFF(); // modeConfig.pullupEN=0; } #endif } break; default: ok=0; } if(ok) { UART1TX(1); } else { UART1TX(0); } break; case 0x40: picmode|=PICCMD; picwrite(cmd&0x3F); picmode&=PICMODEMSK; UART1TX(1); break; case 0x80: picmode|=PICCMD; picwrite(cmd&0x3F); picmode&=PICMODEMSK; temp=UART1RX(); temp<<=8; temp|=UART1RX(); picwrite(temp); UART1TX(1); break; case 0xC0: picmode|=PICCMD; picwrite(cmd&0x3F); picmode&=PICMODEMSK; UART1TX(1); temp=picread(); UART1TX(temp>>8); UART1TX(temp&0x0FF); break; } } }
void binBB(void) { static unsigned char inByte; unsigned int i; BP_LEDMODE = 1; //light MODE LED binReset(); binBBversion(); //send mode name and version while (1) { inByte = getRXbyte(); if ((inByte & 0b10000000) == 0) {//if command bit cleared, process command if (inByte == 0) {//reset, send BB version binBBversion(); } else if (inByte == 1) {//goto SPI mode binReset(); #ifdef BP_USE_HWSPI binSPI(); //go into rawSPI loop #endif binReset(); binBBversion(); //say name on return } else if (inByte == 2) {//goto I2C mode binReset(); #ifdef BP_USE_I2C binI2C(); #endif binReset(); binBBversion(); //say name on return } else if (inByte == 3) {//goto UART mode binReset(); #ifdef BP_USE_HWUART binUART(); #endif binReset(); binBBversion(); //say name on return } else if (inByte == 4) {//goto 1WIRE mode binReset(); #ifdef BP_USE_1WIRE bin1WIRE(); #endif binReset(); binBBversion(); //say name on return } else if (inByte == 5) {//goto RAW WIRE mode binReset(); binwire(); binReset(); binBBversion(); //say name on return } else if (inByte == 6) {//goto OpenOCD mode binReset(); #ifndef BUSPIRATEV4 binOpenOCD(); #endif binReset(); binBBversion(); //say name on return } else if (inByte == 7) {//goto pic mode binReset(); #ifdef BP_USE_PIC binpic(); #endif binReset(); binBBversion(); //say name on return } else if (inByte == 0b1111) {//return to terminal UART1TX(1); BP_LEDMODE = 0; //light MODE LED WAITTXEmpty(); //wait untill TX finishes #ifndef BUSPIRATEV4 asm("RESET"); #endif #ifdef BUSPIRATEV4 //cannot use ASM reset on BPv4 binReset(); return; #endif //self test is only for v2go and v3 #ifndef BUSPIRATEV1A } else if (inByte == 0b10000) {//short self test binSelfTest(0); } else if (inByte == 0b10001) {//full self test with jumpers binSelfTest(1); #endif } else if (inByte == 0b10010) {//setup PWM //cleanup timers from FREQ measure T2CON = 0; //16 bit mode T4CON = 0; OC5CON = 0; //clear PWM settings BP_AUX_RPOUT = OC5_IO; //setup pin //get one byte i = getRXbyte(); if (i & 0b10) T2CONbits.TCKPS1 = 1; //set prescalers if (i & 0b1) T2CONbits.TCKPS0 = 1; //get two bytes i = (getRXbyte() << 8); i |= getRXbyte(); OC5R = i; //Write duty cycle to both registers OC5RS = i; OC5CON = 0x6; // PWM mode on OC, Fault pin disabled //get two bytes i = (getRXbyte() << 8); i |= getRXbyte(); PR2 = i; // write period T2CONbits.TON = 1; // Start Timer2 UART1TX(1); } else if (inByte == 0b10011) {//clear PWM T2CON = 0; // stop Timer2 OC5CON = 0; BP_AUX_RPOUT = 0; //remove output from AUX pin UART1TX(1); //ADC only for v1, v2, v3 } else if (inByte == 0b10100) {//ADC reading (x/1024)*6.6volts AD1CON1bits.ADON = 1; // turn ADC ON i = bpADC(BP_ADC_PROBE); //take measurement AD1CON1bits.ADON = 0; // turn ADC OFF UART1TX((i >> 8)); //send upper 8 bits UART1TX(i); //send lower 8 bits } else if (inByte == 0b10101) {//ADC reading (x/1024)*6.6volts AD1CON1bits.ADON = 1; // turn ADC ON while (1) { i = bpADC(BP_ADC_PROBE); //take measurement WAITTXEmpty(); UART1TX((i >> 8)); //send upper 8 bits //while(UART1TXRdy==0); UART1TX(i); //send lower 8 bits if (UART1RXRdy() == 1) {//any key pressed, exit i = UART1RX(); // /* JTR usb port; */; break; } } AD1CON1bits.ADON = 0; // turn ADC OFF }else if (inByte==0b10110){ //binary frequency count access
void ProcessIO(void) { char oldPGDtris; char PIN; static byte counter=0; int nBytes; unsigned long address; unsigned char i; input_buffer[0]=UART1RX(); //USBGenRead((byte*)input_buffer,64); // if(nBytes>0) // { switch(input_buffer[0]) { case CMD_ERASE: setLeds(LEDS_ON | LEDS_WR); getBytes(1,1);//get more data, #bytes, where to insert in input buffer array output_buffer[0]=bulk_erase(picfamily,pictype,input_buffer[1]); counter=1; setLeds(LEDS_ON); break; case CMD_READ_ID: setLeds(LEDS_ON | LEDS_RD); switch(picfamily) { case DSPIC30: read_code(picfamily,pictype,0xFF0000,(unsigned char*)output_buffer,2,3); break; case PIC18: read_code(picfamily,pictype,0x3FFFFE,(unsigned char*)output_buffer,2,3); //devid is at location 0x3ffffe for PIC18 devices break; case PIC16: set_vdd_vpp(picfamily, pictype, 0); read_code(picfamily,pictype,0x2006,(unsigned char*)output_buffer,2,3); //devid is at location 0x2006 for PIC16 devices break; } counter=2; setLeds(LEDS_ON); break; case CMD_WRITE_CODE: setLeds(LEDS_ON | LEDS_WR); address=((unsigned long)input_buffer[2])<<16| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4]); output_buffer[0]=write_code(picfamily,pictype,address, (unsigned char*)(input_buffer+6),input_buffer[1],input_buffer[5]); counter=1; setLeds(LEDS_ON); break; case CMD_READ_CODE: setLeds(LEDS_ON | LEDS_RD); address=((unsigned long)input_buffer[2])<<16| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4]); read_code(picfamily,pictype,address,(unsigned char*)output_buffer,input_buffer[1],input_buffer[5]); counter=input_buffer[1]; setLeds(LEDS_ON); break; case CMD_WRITE_DATA: setLeds(LEDS_ON | LEDS_WR); address=((unsigned long)input_buffer[2])<<16| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4]); output_buffer[0]=write_data(picfamily,pictype,address, (unsigned char*)(input_buffer+6),input_buffer[1],input_buffer[5]); counter=1; setLeds(LEDS_ON); break; case CMD_READ_DATA: setLeds(LEDS_ON | LEDS_RD); address=((unsigned long)input_buffer[2])<<16| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4]); read_data(picfamily,pictype,address,(unsigned char*)output_buffer,input_buffer[1],input_buffer[5]); counter=input_buffer[1]; setLeds(LEDS_ON); break; case CMD_WRITE_CONFIG: setLeds(LEDS_ON | LEDS_WR); address=((unsigned long)input_buffer[2])<<16| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4]); output_buffer[0]=write_config_bits(picfamily, pictype, address, (unsigned char*)(input_buffer+6),input_buffer[1],input_buffer[5]); counter=1; setLeds(LEDS_ON); break; case CMD_SET_PICTYPE: output_buffer[0]=set_pictype(input_buffer+1); //output_buffer[0]=1; //Ok counter=1; setLeds(LEDS_ON); break; case CMD_FIRMWARE_VERSION: for(counter=0; counter<18; counter++)output_buffer[counter]=upp_version[counter]; counter=18; setLeds(LEDS_ON); break; case CMD_DEBUG: setLeds(LEDS_ON | LEDS_WR | LEDS_RD); switch(input_buffer[1]) { case 0: set_vdd_vpp(dsP30F, DSPIC30, 1); output_buffer[0]=1; counter=1; break; case 1: set_vdd_vpp(dsP30F, DSPIC30, 0); output_buffer[0]=1; counter=1; break; case 2: dspic_send_24_bits(((unsigned long)input_buffer[2])| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4])<<16); output_buffer[0]=1; counter=1; break; case 3: nBytes = dspic_read_16_bits(); output_buffer[0]=(unsigned char)nBytes; output_buffer[1]=(unsigned char)(nBytes>>8); counter=2; break; } break; case CMD_GET_PIN_STATUS: switch(input_buffer[1]) { case SUBCMD_PIN_PGC: if((!TRISPGC_LOW)&&(!PGC_LOW)) //3.3V levels { if(PGC) output_buffer[0] = PIN_STATE_3_3V; else output_buffer[0] = PIN_STATE_0V; } else //5V levels { if(PGC) output_buffer[0] = PIN_STATE_5V; else output_buffer[0] = PIN_STATE_0V; } counter=1; break; case SUBCMD_PIN_PGD: if(TRISPGD)//PGD is input { if(PGD_READ) output_buffer[0] = PIN_STATE_5V; else output_buffer[0] = PIN_STATE_0V; } else { if((!TRISPGD_LOW)&&(!PGD_LOW)) //3.3V levels { if(PGD) output_buffer[0] = PIN_STATE_3_3V; else output_buffer[0] = PIN_STATE_0V; } else //5V levels { if(PGD) output_buffer[0] = PIN_STATE_5V; else output_buffer[0] = PIN_STATE_0V; } } counter=1; break; case SUBCMD_PIN_VDD: //if(VDD) output_buffer[0] = PIN_STATE_FLOAT; //else output_buffer[0] = PIN_STATE_5V; output_buffer[0] = PIN_STATE_5V; counter = 1; break; case SUBCMD_PIN_VPP: counter=1; if(!VPP){output_buffer[0] = PIN_STATE_12V;break;} if(VPP_RST){output_buffer[0] = PIN_STATE_0V;break;} if(VPP_RUN){output_buffer[0] = PIN_STATE_5V;break;} output_buffer[0] = PIN_STATE_FLOAT; break; case SUBCMD_PIN_VPP_VOLTAGE: ReadAdc(output_buffer); counter=2; break; default: output_buffer[0]=3; counter=1; break; } break; case CMD_SET_PIN_STATUS: switch(input_buffer[1]) { case SUBCMD_PIN_PGC: switch(input_buffer[2]) { case PIN_STATE_0V: TRISPGC = 0; PGC = 0; TRISPGC_LOW = 1; PGC_LOW = 0; output_buffer[0]=1;//ok break; case PIN_STATE_3_3V: TRISPGC = 0; PGC = 1; TRISPGC_LOW = 0; PGC_LOW = 0; output_buffer[0]=1;//ok break; case PIN_STATE_5V: TRISPGC = 0; PGC = 1; TRISPGC_LOW = 1; PGC_LOW = 0; output_buffer[0]=1;//ok break; default: output_buffer[0]=3; break; } break; case SUBCMD_PIN_PGD: switch(input_buffer[2]) { case PIN_STATE_0V: TRISPGD = 0; PGD = 0; TRISPGD_LOW = 1; PGD_LOW = 0; output_buffer[0]=1;//ok break; case PIN_STATE_3_3V: TRISPGD = 0; PGD = 1; TRISPGD_LOW = 0; PGD_LOW = 0; output_buffer[0]=1;//ok break; case PIN_STATE_5V: TRISPGD = 0; PGD = 1; TRISPGD_LOW = 1; PGD_LOW = 0; output_buffer[0]=1;//ok break; case PIN_STATE_INPUT: TRISPGD_LOW = 1; TRISPGD = 1; output_buffer[0]=1;//ok break; default: output_buffer[0]=3; break; } break; case SUBCMD_PIN_VDD: switch(input_buffer[2]) { case PIN_STATE_5V: //VDD = 0; output_buffer[0]=1; break; case PIN_STATE_FLOAT: //VDD = 1; output_buffer[0]=1; break; default: output_buffer[0]=3; break; } break; case SUBCMD_PIN_VPP: switch(input_buffer[2]) { case PIN_STATE_0V: VPP = 1; VPP_RST = 1; VPP_RUN = 0; output_buffer[0]=1;//ok break; case PIN_STATE_5V: VPP = 1; VPP_RST = 0; VPP_RUN = 1; output_buffer[0]=1;//ok break; case PIN_STATE_12V: VPP = 0; VPP_RST = 0; VPP_RUN = 0; output_buffer[0]=1;//ok break; case PIN_STATE_FLOAT: VPP = 1; VPP_RST = 0; VPP_RUN = 0; output_buffer[0]=1;//ok break; default: output_buffer[0]=3; break; } break; default: output_buffer[0]=3; } counter=1; break; } //} //if nBytes>0 if(counter != 0) { //if(!mUSBGenTxIsBusy()) //USBGenWrite((byte*)&output_buffer,counter); for(i=0; i<counter; i++) UART1TX(output_buffer[i]); counter=0; } }//end ProcessIO
void jtag(void) { static unsigned char c, cmd; static unsigned int i; jtagSetup(); jtagSettings.HiZ=1; while(1) { cmd=UART1RX(); switch(cmd) { //bpWline("JTAG READY"); case 1://jtag reset jtagReset();//reset //bpWline("JTAGSM: RESET"); jtagLeaveState();//move chain to idle (gives own message) break; case 2://read ID, chain length, # devices //bpWline("JTAG INIT CHAIN"); jtagReset();//reset //bpWline("JTAGSM: RESET"); //data high jtagDataHigh(); //how many devices? //[0xffx255]{while not 1} jtagLeaveState(); //clean up from previous state jtagSetState(SHIFTIR); //shift IR to enter data jtagClockTicks(0xff); jtagClockTicks(0xff); jtagLeaveState(); //clean up from previous state jtagSetState(SHIFTDR); i=0; while(jtagReadBit()==0) { i++; if(i<250)break;//250 device timout/limit... } jtagLeaveState(); //clean up from previous state //reset jtagReset(); //bpWline("JTAGSM: RESET"); //read ID#s (32 bits * devices) {r: (4*devices)} jtagLeaveState(); //clean up from previous state jtagSetState(SHIFTDR); //bpWline("JTAG CHAIN REPORT:"); UART1TX(i*4); //how many bytes are we returning for(c=0; c<i; c++) { UART1TX(jtagReadByte()); UART1TX(jtagReadByte()); UART1TX(jtagReadByte()); UART1TX(jtagReadByte()); } jtagLeaveState(); //clean up from previous state break; case 3://XSFV player //data MUST be low when we start or we get error 3! jtagDataLow(); jtagClockLow(); jtagTMSLow(); xsvf_setup(); /* while(1){ readByte(i); } */ // just return the code #define XSVF_ERROR_NONE 0 #define XSVF_ERROR_UNKNOWN 1 #define XSVF_ERROR_TDOMISMATCH 2 #define XSVF_ERROR_MAXRETRIES 3 /* TDO mismatch after max retries */ #define XSVF_ERROR_ILLEGALCMD 4 #define XSVF_ERROR_ILLEGALSTATE 5 #define XSVF_ERROR_DATAOVERFLOW 6 /* Data > lenVal MAX_LEN buffer size*/ /* Insert new errors here */ #define XSVF_ERROR_LAST 7 i=xsvfExecute(); UART1TX(i); break; default: break;//bpWmessage(MSG_ERROR_MACRO); }//switch }//while }//function
int main(void){ //main function, execution starts here char c = 'g'; /* * PIC pins start as analog inputs to protect any sensitive * externally connected components from unwanted pin output. * To use a pin for digital input and output, we need to * disable the analog functions on a pin by writing 1 to * the corresponding bits of the AD1PCFGL register. */ AD1PCFGL = 0xFFFF; //digital pins /* Initialize ports */ LATA = 0x0000; // set latch levels TRISA = 0x0000; // set IO as outputs LATB = 0x0000; // set latch levels TRISB = 0x0000; // set IO as outputs TRISBbits.TRISB6 = 1; //setup internal clock for 80MHz/40MIPS //7.37/2=3.685*43=158.455/2=79.2275 CLKDIVbits.PLLPRE=0; // PLLPRE (N2) 0=/2 PLLFBD=41; //pll multiplier (M) = +2 CLKDIVbits.PLLPOST=0;// PLLPOST (N1) 0=/2 while(!OSCCONbits.LOCK);//wait for PLL ready InitTimer1(); //uart //UART can be placed on any RPx pin //we need to configure it for RP14/RP15 to use the FTDI usb->serial converter //you could also output one (or both) of the two available UARTS to the I/O header //assign pin 14 to the UART1 RX input register //RX PR14 (input) U1RXR_I = 6; //assign UART1 TX function to the pin 15 output register //TX RP15 (output) RP7_O=U1TX_O; //InitializeUART1(); //setup UART U1BRG = 85;//86@80mhz, [email protected]=115200 U1MODE = 0; //clear mode register U1MODEbits.BRGH = 1; //use high percison baud generator U1STA = 0; //clear status register U1MODEbits.UARTEN = 1; //enable the UART RX IFS0bits.U1RXIF = 0; //clear the receive flag /* //setup LEDs // SD_TRIS = 0; //set pin direction to output // IO1_TRIS = 0; LD1_TRIS = 0; // SD_O = 1; //set all pins high (LED on) LD1_O = 0; // IO1_O=1; */ UART1TX(' ');//first character is discarded UART1TX('h'); UART1TX('e'); UART1TX('l'); UART1TX('l'); UART1TX('o'); while(FOREVER){//never ending loop /*Microchip code*/ if (timer_expired && Counter == FLASH_RATE ) { LATA = ~LATA; LATB = ~LATB; Counter = 0; timer_expired = 0; } // or do something else Nop(); Nop(); Nop(); //-----------end Microchip code-------------// // for(counterNum1 = 0;counterNum1<10000;counterNum1++) // { // counterNum1=counterNum1; // } //random delay) // LD1_O = 1 - LD1_O; if(UART1RXRdy()==1){//check for data waiting c=UART1RX();//get the character /*if(c=='0'){ //LATA &=(~0b11100000000); LD1_O = 0; IO1_O=0; SD_O = 0; }else if(c=='1'){ LD1_O = 1; }else if(c=='2'){ IO1_O=1; }else if(c=='3'){ SD_O = 1; }*/ UART1TX(c);//echo the character back } } }