void bp_adc_continuous_probe(void) { uint16_t measurement; MSG_ADC_VOLTMETER_MODE; MSG_ANY_KEY_TO_EXIT_PROMPT; MSG_ADC_VOLTAGE_PROBE_HEADER; bp_write_voltage(0); MSG_VOLTAGE_UNIT; /* Perform ADC probes until a character is sent to the serial port. */ while (!user_serial_ready_to_read()) { /* Turn the ADC on. */ AD1CON1bits.ADON = ON; /* Perform the measurement. */ measurement = bp_read_adc(BP_ADC_PROBE); /* Turn the ADC off. */ AD1CON1bits.ADON = OFF; /* Erase previous measurement. */ bp_write_string("\x08\x08\x08\x08\x08"); /* Print new measurement. */ bp_write_voltage(measurement); MSG_VOLTAGE_UNIT; } /* Flush the incoming serial buffer. */ user_serial_read_byte(); bpBR; }
void readByte(unsigned char *data){ unsigned int i; unsigned char bh, bl; if(bufBytes==0){ user_serial_transmit_character(0xff); //--- while(U1STAbits.URXDA == 0); //--- dont use it as already in UART1RX => dead lock bh=user_serial_read_byte();//get up to 255 bytes each time bl=user_serial_read_byte();//get up to 255 bytes each time bufBytes=(bh<<8)|bl; for(i=0; i<bufBytes; i++){ //--- while(U1STAbits.URXDA == 0); //--- dont use it otherwise dead lock buf[i]=user_serial_read_byte(); } bufPointer=0; } (*data)=buf[bufPointer]; bufPointer++; bufBytes--; }
void binBB(void) { static unsigned char inByte; unsigned int i; BP_LEDMODE = 1; // light MODE LED binReset(); send_binary_io_mode_identifier(); while (1) { inByte = getRXbyte(); if ((inByte & 0b10000000) == 0) { // if command bit cleared, process command if (inByte == 0) { // reset, send BB version send_binary_io_mode_identifier(); } else if (inByte == 1) { // goto SPI mode binReset(); #ifdef BP_ENABLE_SPI_SUPPORT spi_enter_binary_io(); // go into rawSPI loop #endif /* BP_ENABLE_SPI_SUPPORT */ binReset(); send_binary_io_mode_identifier(); } else if (inByte == 2) { // goto I2C mode binReset(); #ifdef BP_ENABLE_I2C_SUPPORT binary_io_enter_i2c_mode(); #endif /* BP_ENABLE_I2C_SUPPORT */ binReset(); send_binary_io_mode_identifier(); } else if (inByte == 3) { // goto UART mode binReset(); #ifdef BP_ENABLE_UART_SUPPORT binUART(); #endif binReset(); send_binary_io_mode_identifier(); } else if (inByte == 4) { // goto 1WIRE mode binReset(); #ifdef BP_ENABLE_1WIRE_SUPPORT binary_io_enter_1wire_mode(); #endif /* BP_ENABLE_1WIRE_SUPPORT */ binReset(); send_binary_io_mode_identifier(); } else if (inByte == 5) { // goto RAW WIRE mode binReset(); binwire(); binReset(); send_binary_io_mode_identifier(); } else if (inByte == 6) { // goto OpenOCD mode binReset(); #ifdef BP_JTAG_OPENOCD_SUPPORT binOpenOCD(); #endif /* BP_JTAG_OPENOCD_SUPPORT */ binReset(); send_binary_io_mode_identifier(); } else if (inByte == 7) { // goto pic mode binReset(); #ifdef BP_ENABLE_PIC_SUPPORT binpic(); #endif /* BP_ENABLE_PIC_SUPPORT */ binReset(); send_binary_io_mode_identifier(); } else if (inByte == 0b1111) { // return to terminal user_serial_transmit_character(1); BP_LEDMODE = 0; // light MODE LED user_serial_wait_transmission_done(); // 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 } else if (inByte == 0b10000) { // short self test binSelfTest(0); } else if (inByte == 0b10001) { // full self test with jumpers binSelfTest(1); } 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 user_serial_transmit_character(1); } else if (inByte == 0b10011) { // clear PWM T2CON = 0; // stop Timer2 OC5CON = 0; BP_AUX_RPOUT = 0; // remove output from AUX pin user_serial_transmit_character(1); // ADC only for v1, v2, v3 } else if (inByte == 0b10100) { // ADC reading (x/1024)*6.6volts AD1CON1bits.ADON = 1; // turn ADC ON i = bp_read_adc(BP_ADC_PROBE); // take measurement AD1CON1bits.ADON = 0; // turn ADC OFF user_serial_transmit_character((i >> 8)); // send upper 8 bits user_serial_transmit_character(i); // send lower 8 bits } else if (inByte == 0b10101) { // ADC reading (x/1024)*6.6volts AD1CON1bits.ADON = 1; // turn ADC ON while (1) { i = bp_read_adc(BP_ADC_PROBE); // take measurement user_serial_wait_transmission_done(); user_serial_transmit_character((i >> 8)); // send upper 8 bits // while(UART1TXRdy==0); user_serial_transmit_character(i); // send lower 8 bits if (user_serial_ready_to_read() == 1) { // any key pressed, exit i = user_serial_read_byte(); // /* JTR usb port; */; break; } } AD1CON1bits.ADON = 0; // turn ADC OFF } else if (inByte == 0b10110) { // binary frequency count access
void binUART(void) { uint16_t brg_value; uart_settings.databits_parity = 0; uart_settings.stop_bits = 0; uart_settings.receive_polarity = 0; mode_configuration.high_impedance = ON; brg_value = UART_BRG_SPEED[0]; // start at 300bps uart_settings.echo_uart = OFF; uart2_setup(brg_value, mode_configuration.high_impedance, uart_settings.receive_polarity, uart_settings.databits_parity, uart_settings.stop_bits); uart2_enable(); MSG_UART_MODE_IDENTIFIER; for (;;) { uint8_t input_byte; if (uart2_rx_ready()) { if (uart_settings.echo_uart) { user_serial_transmit_character(uart2_rx()); } else { uart2_rx(); } } U2STAbits.OERR = OFF; if (!user_serial_ready_to_read()) { continue; } input_byte = user_serial_read_byte(); switch (input_byte & 0xF0) { case 0: switch (input_byte) { case 0: uart2_disable(); return; case 1: MSG_UART_MODE_IDENTIFIER; break; case 2: REPORT_IO_SUCCESS(); U2STAbits.OERR = OFF; uart_settings.echo_uart = ON; break; case 3: uart_settings.echo_uart = OFF; REPORT_IO_SUCCESS(); break; case 7: REPORT_IO_SUCCESS(); uart2_disable(); brg_value = (uint16_t)user_serial_read_byte() << 8; REPORT_IO_SUCCESS(); brg_value |= user_serial_read_byte(); uart2_setup(brg_value, mode_configuration.high_impedance, uart_settings.receive_polarity, uart_settings.databits_parity, uart_settings.stop_bits); uart2_enable(); REPORT_IO_SUCCESS(); break; case 15: REPORT_IO_SUCCESS(); U2STAbits.OERR = OFF; for (;;) { #ifdef BUSPIRATEV4 if (U2STAbits.URXDA == ON) { user_serial_transmit_character(U2RXREG); } #else if ((U2STAbits.URXDA == 1) && (U1STAbits.UTXBF == 0)) { U1TXREG = U2RXREG; } #endif /* BUSPIRATEV4 */ if (user_serial_ready_to_read() && (U2STAbits.UTXBF == 0)) { U2TXREG = user_serial_read_byte(); } } break; default: REPORT_IO_FAILURE(); break; } break; case 0b00010000: { size_t counter; input_byte &= 0b00001111; input_byte++; REPORT_IO_SUCCESS(); for (counter = 0; counter < input_byte; counter++) { uart2_tx(user_serial_read_byte()); REPORT_IO_SUCCESS(); } break; } case 0b01000000: bp_binary_io_peripherals_set(input_byte); REPORT_IO_SUCCESS(); break; #ifdef BUSPIRATEV4 case 0b01010000: user_serial_transmit_character(bp_binary_io_pullup_control(input_byte)); break; #endif /* BUSPIRATEV4 */ case 0b01100000: input_byte &= 0b00001111; if (input_byte > 0b1010) { input_byte = 0b1010; } brg_value = UART_BRG_SPEED[input_byte]; uart2_disable(); uart2_setup(brg_value, mode_configuration.high_impedance, uart_settings.receive_polarity, uart_settings.databits_parity, uart_settings.stop_bits); uart2_enable(); REPORT_IO_SUCCESS(); break; case 0b10000000: case 0b10010000: uart_settings.databits_parity = 0; uart_settings.stop_bits = 0; uart_settings.receive_polarity = 0; mode_configuration.high_impedance = OFF; if (input_byte & 0b1000) { uart_settings.databits_parity |= 0b10; } if (input_byte & 0b100) { uart_settings.databits_parity |= 0b1; } if (input_byte & 0b10) { uart_settings.stop_bits = 1; } if (input_byte & 0b1) { uart_settings.receive_polarity = 1; } if ((input_byte & 0b10000) == 0) { mode_configuration.high_impedance = ON; } uart2_disable(); uart2_setup(brg_value, mode_configuration.high_impedance, uart_settings.receive_polarity, uart_settings.databits_parity, uart_settings.stop_bits); uart2_enable(); REPORT_IO_SUCCESS(); break; default: REPORT_IO_FAILURE(); break; } } }
void uart_run_macro(const uint16_t macro) { switch (macro) { case UART_MACRO_MENU: MSG_UART_MACRO_MENU; break; case UART_MACRO_BRIDGE_WITH_FLOW_CONTROL: #ifdef BUSPIRATEV3 /* FTDI CTS and RTS setup. */ FTDI_CTS_DIR = OUTPUT; FTDI_RTS_DIR = INPUT; /* External CTS and RTS setup. */ BP_CS_DIR = INPUT; BP_CLK_DIR = OUTPUT; #else /* Do nothing for v4? */ #endif /* BUSPIRATEV3 */ /* Intentional fall-through. */ case UART_MACRO_TRANSPARENT_BRIDGE: MSG_UART_BRIDGE; MSG_UART_BRIDGE_EXIT; if (!agree()) { break; } /* Make terminal speed match target speed to avoid overruns? */ /* Clear overrun flag. */ U2STAbits.OERR = OFF; for (;;) { #ifdef BUSPIRATEV4 if (BP_BUTTON_ISDOWN()) { break; } if (U2STAbits.URXDA == ON) { user_serial_transmit_character(U2RXREG); } #else if ((U2STAbits.URXDA == ON) && (U1STAbits.UTXBF == OFF)) { U1TXREG = U2RXREG; } #endif /* BUSPIRATEV4 */ if (user_serial_ready_to_read() && (U2STAbits.UTXBF == OFF)) { U2TXREG = user_serial_read_byte(); } #ifdef BUSPIRATEV3 if (U2STAbits.OERR || U1STAbits.OERR) { U2STAbits.OERR = OFF; U1STAbits.OERR = OFF; BP_LEDMODE = LOW; } #else if (U2STAbits.OERR) { U2STAbits.OERR = OFF; BP_LEDMODE = LOW; } #endif /* BUSPIRATEV3 */ #ifdef BUSPIRATEV3 if (macro == UART_MACRO_BRIDGE_WITH_FLOW_CONTROL) { /* Relay flow control bits. */ BP_CLK = FTDI_RTS; FTDI_CTS = BP_CS; } #endif /* BUSPIRATEV3 */ } break; case UART_MACRO_RAW_UART: MSG_UART_RAW_UART_INPUT; MSG_ANY_KEY_TO_EXIT_PROMPT; U2STAbits.OERR = OFF; for (;;) { #ifdef BUSPIRATEV4 if (BP_BUTTON_ISDOWN()) { break; } if (uart2_rx_ready()) { user_serial_transmit_character(uart2_rx()); } #else if ((U2STAbits.URXDA == ON) && (U1STAbits.UTXBF == OFF)) { U1TXREG = U2RXREG; } #endif /* BUSPIRATEV4 */ if (user_serial_ready_to_read()) { volatile uint16_t dummy; dummy = user_serial_read_byte(); break; } } break; case UART_MACRO_AUTO_BAUD_RATE_DETECTION: uart2_disable(); uart_get_baud_rate(false); uart2_enable(); #ifdef BUSPIRATEV3 if (U2BRG < U1BRG) { MSG_UART_POSSIBLE_OVERFLOW; } #endif /* BUSPIRATEV3 */ break; default: MSG_UNKNOWN_MACRO_ERROR; break; } }
uint8_t perform_selftest(bool show_progress, bool jumper_test) { errors = 0; if (!show_progress) { bus_pirate_configuration.quiet = true; } /* Alert the user to perform the required manual operations if needed. */ if (show_progress && jumper_test) { /* Print alert. */ BPMSG1163; BPMSG1251; /* Wait for a character to come in. */ user_serial_read_byte(); } /* Start the test procedure. */ BPMSG1164; /* Check whether AUX0 goes HIGH when requested. */ BP_AUX0 = HIGH; BP_AUX0_DIR = OUTPUT; BPMSG1165; check_result(BP_AUX0, HIGH); BP_AUX0 = LOW; BP_AUX0_DIR = INPUT; /* Check whether the LED line goes HIGH when requested. */ BP_LEDMODE = HIGH; BP_LEDMODE_DIR = OUTPUT; BPMSG1166; check_result(BP_LEDMODE, HIGH); BP_LEDMODE = LOW; /* Check whether the pull-up line goes HIGH when requested. */ BP_PULLUP_ON(); BPMSG1167; check_result(BP_PULLUP, HIGH); /* Check whether the pull-up line goes LOW when requested. */ BP_PULLUP_OFF(); BPMSG1168; check_result(BP_PULLUP, LOW); /* Check whether the regulated voltage line goes HIGH when requested. */ BP_VREG_ON(); bp_delay_ms(PWR_STATE_TEST_DELAY); BPMSG1169; check_result(BP_VREGEN, HIGH); #ifdef BUSPIRATEV4 /* Test the internal EEPROM. */ BPMSG1265; /* Check the I2C flash clock line. */ BPMSG1266; check_result(BP_EE_SCL, HIGH); /* Check the I2C flash data line. */ BPMSG1267; check_result(BP_EE_SDA, HIGH); /* Check the I2C flash WRITE PROTECT line. */ BPMSG1268; check_result(BP_EE_WP, HIGH); /* Performs a more complete EEPROM test. */ BPMSG1269; check_result(eeprom_test(), true); #endif /* BUSPIRATEV4 */ /* ADC check. */ BPMSG1170; /* Turn ADC on. */ ADCON(); #ifdef BUSPIRATEV4 /* Check whether the voltage coming in from the USB port is within range. */ BPMSG1270; perform_adc_test(BP_ADC_USB, V5L, V5H); #endif /* BUSPIRATEV4 */ /* Check whether the +5v rail output is within range. */ BPMSG1171; perform_adc_test(BP_ADC_5V0, V5L, V5H); #ifdef BUSPIRATEV4 /* Test the +5v pull-up line. */ BP_5VPU_ON(); BPMSG1171; bpSP; BPMSG1172; bp_delay_ms(PWR_STATE_TEST_DELAY); perform_adc_test(BP_ADC_VPU, V5L, V5H); BP_5VPU_OFF(); if (jumper_test) { /* * Check whether the +3.3v rail output is within range when measured from * outside the board circuitry, once a jumper wire is manually placed * between the +3.3v rail pin and the ADC input pin. */ BPMSG1174; perform_adc_test(BP_ADC_PROBE, V33L, V33H); } /* * Check whether the +3.3v rail output is within range when measured from * inside the board circuitry. */ BPMSG1173; perform_adc_test(BP_ADC_3V3, V33L, V33H); /* Test the +3.3v pull-up line. */ BP_3V3PU_ON(); BPMSG1173; bpSP; BPMSG1172; bp_delay_ms(PWR_STATE_TEST_DELAY); perform_adc_test(BP_ADC_VPU, V33L, V33H); BP_3V3PU_OFF(); #elif defined(BUSPIRATEV3) if (jumper_test) { /* * Check whether the +5v pull-up output is within range when measured from * outside the board circuitry, once a jumper wire is manually placed * between the +5v pull-up pin and the ADC input pin. */ BPMSG1172; perform_adc_test(BP_ADC_VPU, V5L, V5H); } /* Check whether the +3.3v rail output is within range. */ BPMSG1173; perform_adc_test(BP_ADC_3V3, V33L, V33H); if (jumper_test) { /* * Check whether the +3.3v rail output is within range when measured from * outside the board circuitry, once a jumper wire is manually placed * between the +3.3v rail pin and the ADC input pin. */ BPMSG1174; perform_adc_test(BP_ADC_PROBE, V33L, V33H); } #endif /* BUSPIRATEV4 || BUSPIRATEV3 */ /* Turn ADC off. */ ADCOFF(); /* * Pull all I/O pins HIGH with pull-ups deactivated, and check the pins * state afterwards. */ BPMSG1175; IODIR &= ~ALLIO; IOLAT |= ALLIO; bp_delay_ms(PIN_STATE_TEST_DELAY); perform_pins_state_test(HIGH); /* * Pull all I/O pins LOW with pull-ups active and check the pins state * afterwards. */ BPMSG1176; IOLAT &= ~ALLIO; if (jumper_test) { #ifdef BUSPIRATEV4 BP_3V3PU_ON(); #endif /* BUSPIRATEV4 */ BP_PULLUP_ON(); } bp_delay_ms(PIN_STATE_TEST_DELAY); perform_pins_state_test(LOW); if (jumper_test) { /* * Pull all I/O pins HIGH with pull-ups active and check the pins state * afterwards. */ /* TODO: should this be done without jumpers too? */ BPMSG1177; IODIR |= ALLIO; bp_delay_ms(PIN_STATE_TEST_DELAY); perform_pins_state_test(HIGH); #ifdef BUSPIRATEV4 BP_3V3PU_OFF(); #endif /* BUSPIRATEV4 */ } if (show_progress && jumper_test) { /* * Alert the user to check the LED on/off states, and prompt for a key * to be pressed to continue. */ BP_VREG_ON(); BP_MODELED_ON(); #ifdef BUSPIRATEV4 BP_USBLED_ON(); #endif /* BUSPIRATEV4 */ BPMSG1178; MSG_ANY_KEY_TO_EXIT_PROMPT; user_serial_read_byte(); #ifdef BUSPIRATEV4 BP_USBLED_OFF(); #endif /* BUSPIRATEV4 */ BP_MODELED_OFF(); BP_VREG_OFF(); } bp_reset_board_state(); BPMSG1179; bp_write_dec_byte(errors); BPMSG1180; bus_pirate_configuration.quiet = false; return errors; }