Ejemplo n.º 1
0
void cmd_readadc(char *line) {
  adc_init(PIN_MAP[12].adc_device); // all on ADC1

  adc_set_extsel(PIN_MAP[12].adc_device, ADC_SWSTART);
  adc_set_exttrig(PIN_MAP[12].adc_device, true);

  adc_enable(PIN_MAP[12].adc_device);
  adc_calibrate(PIN_MAP[12].adc_device);
  adc_set_sample_rate(PIN_MAP[12].adc_device, ADC_SMPR_55_5);

  gpio_set_mode (PIN_MAP[12].gpio_device,PIN_MAP[12].gpio_bit, GPIO_INPUT_ANALOG);
  gpio_set_mode (PIN_MAP[19].gpio_device,PIN_MAP[19].gpio_bit, GPIO_INPUT_ANALOG);
  gpio_set_mode (PIN_MAP[20].gpio_device,PIN_MAP[20].gpio_bit, GPIO_INPUT_ANALOG);

  uint16 value1 = adc_read(PIN_MAP[12].adc_device,PIN_MAP[12].adc_channel);
  uint16 value2 = adc_read(PIN_MAP[19].adc_device,PIN_MAP[19].adc_channel);
  uint16 value3 = adc_read(PIN_MAP[20].adc_device,PIN_MAP[20].adc_channel);
  char values[50];
  sprintf(values,"PA6 ADC Read: %u\r\n",value1);
  serial_write_string(values);
  sprintf(values,"PC4 ADC Read: %u\r\n",value2);
  serial_write_string(values);
  sprintf(values,"PC5 ADC Read: %u\r\n",value3);
  serial_write_string(values);
}
Ejemplo n.º 2
0
void setupUltrasonicPins(){
    pinMode(ULTRASONIC_ONE_VOLTAGE_PIN, INPUT_ANALOG);
    pinMode(ULTRASONIC_TWO_VOLTAGE_PIN, INPUT_ANALOG);
    pinMode(ULTRASONIC_ONE_ENABLE_PIN, OUTPUT);
    pinMode(ULTRASONIC_TWO_ENABLE_PIN, OUTPUT);
    adc_set_sample_rate(ADC2, ADC_SMPR_13_5); //sets ultrasonic ADC to sample much faster
    digitalWrite(ULTRASONIC_ONE_VOLTAGE_PIN, LOW);
    digitalWrite(ULTRASONIC_TWO_VOLTAGE_PIN, LOW);
}
Ejemplo n.º 3
0
static void adcDefaultConfig(const adc_dev* dev)
{
    adc_init(dev);
    adc_set_extsel(dev, ADC_SWSTART);
    adc_set_exttrig(dev, 1);
    adc_enable(dev);
    adc_calibrate(dev);
    adc_set_sample_rate(dev, ADC_SMPR_55_5);
}
Ejemplo n.º 4
0
/*
 * Initialize the ADC for taking meshurement on the BLDC driver.
 */
void me_adc_init(void)
{
    adc_load_calibration();

    adc_ch_set_input(&ADC_CH_U, ADC_U_MUXPOS, ADC_U_MUXNEG);
    adc_ch_set_input(&ADC_CH_V, ADC_V_MUXPOS, ADC_V_MUXNEG);
    adc_ch_set_input(&ADC_CH_VOLT, ADC_VOLT_MUXPOS, ADC_VOLT_MUXNEG);

    adc_ch_set_input_mode(&ADC_CH_U, ADC_U_INP_MODE);
    adc_ch_set_input_mode(&ADC_CH_V, ADC_V_INP_MODE);
    adc_ch_set_input_mode(&ADC_CH_VOLT, ADC_VOLT_INP_MODE);

    adc_set_freerunning(&ADCA, true, ADCA_SWEEP);
    adc_set_freerunning(&ADCB, true, ADCB_SWEEP);

    adc_set_resolution(&ADCA, ADC_RESOLUTION);
    adc_set_resolution(&ADCB, ADC_RESOLUTION);

    adc_set_conversion_mode(&ADCA, true);
    adc_set_conversion_mode(&ADCB, true);

    adc_set_reference(&ADCA, ADC_REFERENCE);
    adc_set_reference(&ADCB, ADC_REFERENCE);

    adc_set_sample_rate(&ADCA, ADC_PRESCALER);
    adc_set_sample_rate(&ADCB, ADC_PRESCALER);

    adc_enable(&ADCA, true);
    adc_enable(&ADCB, true);

    _delay_ms(5);

    me_adc_set_channel(&ADC_Res_U, &ADC_CH_U);
    me_adc_set_channel(&ADC_Res_V, &ADC_CH_V);
    me_adc_set_channel(&ADC_Res_Volt, &ADC_CH_VOLT);

    me_adc_set_zero(&ADC_Res_U);
    me_adc_set_zero(&ADC_Res_V);
    //adc_set_zero(&ADC_Res_Volt);
}
Ejemplo n.º 5
0
static void adcDefaultConfig(const adc_dev *dev) {
    adc_init(dev);
    // ok

    adc_set_extsel(dev, ADC_SWSTART);
    // ok
    adc_set_exttrig(dev, true);
    // ok

    adc_enable(dev);
    // up to 1mA
    adc_calibrate(dev);
    // stick at 1mA
    adc_set_sample_rate(dev, ADC_SMPR_55_5);
    // stick at 1mA
}
Ejemplo n.º 6
0
void adc_init(void)
{
    adc_load_calibration();

    adc_ch_set_input(&ADC_CH, ADC_CH_MUXPOS_PIN2_gc, ADC_CH_MUXNEG_PIN1_gc);
    adc_ch_set_input_mode(&ADC_CH, ADC_CH_INPUTMODE_DIFF_gc);

    adc_set_freerunning(&ADCA, true, ADC_SWEEP_0_gc);
    adc_set_resolution(&ADCA, ADC_RESOLUTION_12BIT_gc);
    adc_set_conversion_mode(&ADCA, true);
    adc_set_reference(&ADCA, ADC_REFSEL_AREFA_gc);
    adc_set_sample_rate(&ADCA, ADC_PRESCALER_DIV128_gc);

    adc_enable(&ADCA, true);

    _delay_ms(10);
    ADC_ch_u.channel = &ADC_CH;
    adc_set_zero(&ADC_ch_u);
}
Ejemplo n.º 7
0
static void adc_default_config(adc_dev *dev) {
    adc_enable_single_swstart(dev);
    adc_set_sample_rate(dev, wirish::priv::w_adc_smp);
}
Ejemplo n.º 8
0
void serial_process_command(char *line) {

  if(in_displayparams) {
    serial_displayparams_run(line);
    in_displayparams = false;
  }

  if(in_setdevicetag) {
    serial_setdevicetag_run(line);
    in_setdevicetag = false;
  }

  if(in_setrtc) {
    serial_setrtc_run(line);
    in_setrtc = false;
  }

  if(in_setkeyval) {
    serial_setkeyval_run(line);
    in_setkeyval = false;
  }

  serial_write_string("\r\n");
  if(strcmp(line,"HELLO") == 0) {
   serial_write_string("GREETINGS PROFESSOR FALKEN.\r\n");
  } else 
  if(strcmp(line,"LIST GAMES") == 0) {
    serial_write_string("I'M KIND OF BORED OF GAMES, TURNS OUT THE ONLY WAY TO WIN IS NOT TO PLAY...\r\n");
  } else 
  if(strcmp(line,"LOGXFER") == 0) {
    serial_sendlog();
  } else 
  if(strcmp(line,"DISPLAYPARAMS") == 0) {
    serial_displayparams();
  } else
  if(strcmp(line,"HELP") == 0) {
    serial_write_string("Available commands: HELP, LOGXFER, DISPLAYTEST, HELLO");
  } else 
  if(strcmp(line,"DISPLAYTEST") == 0) {
    display_test();
  } else
  if(strcmp(line,"LOGTEST") == 0) {
    char stemp[100];

    sprintf(stemp,"Raw log data\r\n");
    serial_write_string(stemp);

    uint8_t *flash_log = flashstorage_log_get();
    for(int n=0;n<1024;n++) {
      sprintf(stemp,"%u ",flash_log[n]);
      serial_write_string(stemp);
      if(n%64 == 0) serial_write_string("\r\n");
    }
    serial_write_string("\r\n");

    log_data_t data;
    data.time = 0;
    data.cpm  = 1;
    data.accel_x_start = 2;
    data.accel_y_start = 3;
    data.accel_z_start = 4;
    data.accel_x_end   = 5;
    data.accel_y_end   = 6;
    data.accel_z_end   = 7;
    data.log_type      = UINT_MAX;
    sprintf(stemp,"Log size: %u\r\n",flashstorage_log_size());
    serial_write_string(stemp);

    sprintf(stemp,"Writing test log entry of size: %u\r\n",sizeof(log_data_t));
    serial_write_string(stemp);

    flashstorage_log_pushback((uint8 *) &data,sizeof(log_data_t));

    sprintf(stemp,"Log size: %u\r\n",flashstorage_log_size());
    serial_write_string(stemp);
  } else 
  if(strcmp(line,"VERSION") == 0) {
    char stemp[50];
    sprintf(stemp,"Version: %s\r\n",OS100VERSION);
    serial_write_string(stemp);
  } else 
  if(strcmp(line,"GETDEVICETAG") == 0) {
    const char *devicetag = flashstorage_keyval_get("DEVICETAG");
    if(devicetag != 0) {
      char stemp[100];
      sprintf(stemp,"Devicetag: %s\r\n",devicetag);
      serial_write_string(stemp);
    } else {
      serial_write_string("No device tag set");
    }
  } else
  if(strcmp(line,"SETDEVICETAG") == 0) {
    serial_setdevicetag();
  } else 
  if(strcmp(line,"READPRIVATEKEY") == 0) {
   // serial_readprivatekey();  // removed for production
  } else
  if(strcmp(line,"WRITEPRIVATEKEY") == 0) {
   // serial_writeprivatekey(); // maybe this should be removed for production?
  } else 
  if(strcmp(line,"MAGREAD") == 0) {
    gpio_set_mode (PIN_MAP[41].gpio_device,PIN_MAP[41].gpio_bit, GPIO_OUTPUT_PP); // MAGPOWER
    gpio_set_mode (PIN_MAP[29].gpio_device,PIN_MAP[29].gpio_bit, GPIO_INPUT_PU);  // MAGSENSE

    // Power up magsense
    gpio_write_bit(PIN_MAP[41].gpio_device,PIN_MAP[41].gpio_bit,1);

    // wait...
    delay_us(1000);
    
    // Read magsense
    int magsense = gpio_read_bit(PIN_MAP[29].gpio_device,PIN_MAP[29].gpio_bit);

    char magsenses[50];
    sprintf(magsenses,"%u\r\n",magsense);
    serial_write_string(magsenses);
  } else
  if(strcmp(line,"WRITEDAC") == 0) {
    dac_init(DAC,DAC_CH2);

    int8_t idelta=1;
    uint8_t i=0;
    for(int n=0;n<1000000;n++) {
      
      if(i == 254) idelta = -1;
      if(i == 0  ) idelta =  1;

      i += idelta;

      dac_write_channel(DAC,2,i);
    }
    serial_write_string("WRITEDACFIN");
  } else
  if(strcmp(line,"TESTHP") == 0) {
    gpio_set_mode (PIN_MAP[12].gpio_device,PIN_MAP[12].gpio_bit, GPIO_OUTPUT_PP);  // HP_COMBINED
    for(int n=0;n<100000;n++) {
      gpio_write_bit(PIN_MAP[12].gpio_device,PIN_MAP[12].gpio_bit,1);
      delay_us(100);
      gpio_write_bit(PIN_MAP[12].gpio_device,PIN_MAP[12].gpio_bit,0);
      delay_us(100);
    }
  } else 
  if(strcmp(line,"READADC") == 0) {

    adc_init(PIN_MAP[12].adc_device); // all on ADC1

    adc_set_extsel(PIN_MAP[12].adc_device, ADC_SWSTART);
    adc_set_exttrig(PIN_MAP[12].adc_device, true);

    adc_enable(PIN_MAP[12].adc_device);
    adc_calibrate(PIN_MAP[12].adc_device);
    adc_set_sample_rate(PIN_MAP[12].adc_device, ADC_SMPR_55_5);

    gpio_set_mode (PIN_MAP[12].gpio_device,PIN_MAP[12].gpio_bit, GPIO_INPUT_ANALOG);
    gpio_set_mode (PIN_MAP[19].gpio_device,PIN_MAP[19].gpio_bit, GPIO_INPUT_ANALOG);
    gpio_set_mode (PIN_MAP[20].gpio_device,PIN_MAP[20].gpio_bit, GPIO_INPUT_ANALOG);

    int n=0;
    uint16 value1 = adc_read(PIN_MAP[12].adc_device,PIN_MAP[12].adc_channel);
    uint16 value2 = adc_read(PIN_MAP[19].adc_device,PIN_MAP[19].adc_channel);
    uint16 value3 = adc_read(PIN_MAP[20].adc_device,PIN_MAP[20].adc_channel);
    char values[50];
    sprintf(values,"PA6 ADC Read: %u\r\n",value1);
    serial_write_string(values);
    sprintf(values,"PC4 ADC Read: %u\r\n",value2);
    serial_write_string(values);
    sprintf(values,"PC5 ADC Read: %u\r\n",value3);
    serial_write_string(values);
  } else
  if(strcmp(line,"SETMICREVERSE") == 0) {
    gpio_set_mode (PIN_MAP[36].gpio_device,PIN_MAP[36].gpio_bit, GPIO_OUTPUT_PP);  // MICREVERSE
    gpio_set_mode (PIN_MAP[35].gpio_device,PIN_MAP[35].gpio_bit, GPIO_OUTPUT_PP);  // MICIPHONE
    gpio_write_bit(PIN_MAP[36].gpio_device,PIN_MAP[36].gpio_bit,1); // MICREVERSE
    gpio_write_bit(PIN_MAP[35].gpio_device,PIN_MAP[35].gpio_bit,0); // MICIPHONE
    serial_write_string("Set MICREVERSE to 1, MICIPHONE to 0\r\n");
  } else 
  if(strcmp(line,"SETMICIPHONE") == 0) {
    gpio_set_mode (PIN_MAP[36].gpio_device,PIN_MAP[36].gpio_bit, GPIO_OUTPUT_PP);  // MICREVERSE
    gpio_set_mode (PIN_MAP[35].gpio_device,PIN_MAP[35].gpio_bit, GPIO_OUTPUT_PP);  // MICIPHONE
    gpio_write_bit(PIN_MAP[36].gpio_device,PIN_MAP[36].gpio_bit,0); // MICREVERSE
    gpio_write_bit(PIN_MAP[35].gpio_device,PIN_MAP[35].gpio_bit,1); // MICIPHONE
    serial_write_string("Set MICREVERSE to 0, MICIPHONE to 1\r\n");
  } else
  if(strcmp(line,"TESTSIGN") == 0) {
    serial_signing_test();
  } else 
  if(strcmp(line,"PUBKEY") == 0) {
    signing_printPubKey();
    serial_write_string("\n\r");
  } else 
  if(strcmp(line,"GUID") == 0) {
    signing_printGUID();
    serial_write_string("\n\r");
  } else 
  if(strcmp(line,"KEYVALID") == 0) {
    if( signing_isKeyValid() == 1 )
      serial_write_string("uu_valid VALID KEY\r\n");
    else
      serial_write_string("uu_valid IMPROPER OR UNINITIALIZED KEY\r\n");
  } else 
  if(strcmp(line,"LOGSIG") == 0) {
    signing_hashLog();
    serial_write_string("\n\r");
  } else
  if(strcmp(line,"LOGPAUSE") == 0) {
    flashstorage_log_pause();
  } else 
  if(strcmp(line,"LOGRESUME") == 0) {
    flashstorage_log_resume();
  } else
  if(strcmp(line,"LOGCLEAR") == 0) {
    serial_write_string("Clearing flash log\r\n");
    flashstorage_log_clear();
    serial_write_string("Cleared\r\n");
  } else
  if(strcmp(line,"KEYVALDUMP") == 0) {
    serial_keyvaldump();
  } else
  if(strcmp(line,"KEYVALSET") == 0) {
    serial_setkeyval();
  } else
  if(strcmp(line,"SETRTC") == 0) {
    serial_setrtc();
  } else
  if(strcmp(line,"RTCALARM") == 0) {
    serial_write_string("Alarm triggered for 10s\r\n");
    rtc_set_alarm(RTC,rtc_get_time(RTC)+10);
  }

  serial_write_string("\r\n>");
}
Ejemplo n.º 9
0
/*
    Set the ADC Sampling Rate.
    ADC_SMPR_1_5,               < 1.5 ADC cycles
    ADC_SMPR_7_5,               < 7.5 ADC cycles
    ADC_SMPR_13_5,              < 13.5 ADC cycles
    ADC_SMPR_28_5,              < 28.5 ADC cycles
    ADC_SMPR_41_5,              < 41.5 ADC cycles
    ADC_SMPR_55_5,              < 55.5 ADC cycles
    ADC_SMPR_71_5,              < 71.5 ADC cycles
    ADC_SMPR_239_5,             < 239.5 ADC cycles
*/
    void STM32ADC::setSampleRate(adc_smp_rate SampleRate){
        adc_set_sample_rate(_dev, SampleRate);
    }