Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
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;
    }
  }
}
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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;
  }
}