Beispiel #1
0
/* adc8
 *
 * Returns a 8bit ADC value
 */
uint8_t adc8(void)
{
	/* Set the result to left adjust (LSB at ADC:2) */
	ADMUX |= (1<<ADLAR);
	adcStartConversion();	
	return ADCH;
}
Beispiel #2
0
void convStart( void )
{
    /*
    pwmStart( &PWMD2, &pwmCfg );
    palSetPadMode( GPIOA, 0, PAL_MODE_STM32_ALTERNATE_PUSHPULL );
    palSetPadMode( GPIOA, 1, PAL_MODE_STM32_ALTERNATE_PUSHPULL );
    //palSetPadMode( GPIOA, 2, PAL_MODE_STM32_ALTERNATE_PUSHPULL );
    pwmEnableChannel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH( &PWMD2, 9500 ) );
    pwmEnableChannel(&PWMD2, 1, PWM_PERCENTAGE_TO_WIDTH( &PWMD2, 3030 ) );
    //pwmEnableChannel(&PWMD2, 2, PWM_PERCENTAGE_TO_WIDTH( &PWMD2, 3030 ) );
    */

	// Start PWM peripherial.
    pwmStart( &CONV_PWM, &pwmCfg );
	// Init PWM pins.
    palSetPadMode( CONV_PORT, CONV_BUCK_PIN,  PAL_MODE_STM32_ALTERNATE_PUSHPULL );
    palSetPadMode( CONV_PORT, CONV_BOOST_PIN, PAL_MODE_STM32_ALTERNATE_PUSHPULL );
    // Set zero active period.
    pwmEnableChannel(&CONV_PWM, PWM_BOOST_CHAN, PWM_PERCENTAGE_TO_WIDTH( &CONV_PWM, 0000 ) );
    pwmEnableChannel(&CONV_PWM, PWM_BUCK_CHAN,  PWM_PERCENTAGE_TO_WIDTH( &CONV_PWM, 0000 ) );

    // Init ADC.
    palSetGroupMode(CONV_ADC_PORT, PAL_PORT_BIT( CONV_BUCK_FB_PIN ) |
    	                           PAL_PORT_BIT( CONV_BOOST_FB_PIN ) |
    	                           PAL_PORT_BIT( CONV_INPUT_FB_PIN ),
                                   0, PAL_MODE_INPUT_ANALOG);
    adcStart( &ADCD1, NULL );

    adcStartConversion( &ADCD1, &adcGroup, adcSamples, ADC_BUF_DEPTH );
}
Beispiel #3
0
/* adc10
 *
 * Returns a 10bit ADC value
 */
uint16_t adc10(void)
{
	/* Set the result to right adjust (LSB at ADC:0) */
	ADMUX &= ~(1<<ADLAR);
	adcStartConversion();	
	return ADC;
}
Beispiel #4
0
static void startFirstAudioADC()
{
#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO)  || defined(TEENSYDUINO) // teensy 3, 3.1
	adc->startSingleRead(AUDIO_INPUT_PIN); // ADC lib converts pin/channel in startSingleRead
#else
	adcStartConversion(adcPinToChannelNum(AUDIO_INPUT_PIN));
#endif
}
Beispiel #5
0
void adcInit(void) {
    DDRC &= ~(3);
    ADMUX = 0;
    ADMUX |= (1 << REFS0); // Ref. Voltage: Vcc
    ADCSRA |= (1 << ADPS2) | (1 << ADPS0); // Prescaler 64
    ADCSRA |= (1 << ADEN); // Enable adc
    adcStartConversion(0);
    adcGetResult();
}
Beispiel #6
0
void
vexAdcInit()
{
    // Init and start ADC1
    adcInit();
    adcStart(&ADCD1, &adccfg);

    // Start conversions
    adcStartConversion(&ADCD1, &adccg, &samples_buf[0], 1);
}
Beispiel #7
0
/*
 *******************************************************************************
 * EXPORTED FUNCTIONS
 *******************************************************************************
 */
void ADCInit_local(void){

  adcStart(&ADCD1, &adccfg);
  adcStartConversion(&ADCD1, &adccg, samples, ADC_BUF_DEPTH);

  chThdCreateStatic(PowerKeeperThreadWA,
          sizeof(PowerKeeperThreadWA),
          NORMALPRIO,
          PowerKeeperThread,
          NULL);
}
Beispiel #8
0
void dma_storm_adc_start(void){
  ints = 0;
  errors = 0;

  /* Activates the ADC1 driver and the temperature sensor.*/
  adcStart(&ADCD1, NULL);
  adcSTM32EnableTSVREFE();

  /* Starts an ADC continuous conversion.*/
  adcStartConversion(&ADCD1, &adccg, samples, ADC_BUF_DEPTH);
}
Beispiel #9
0
void adc_init() {
    // Following 3 functions use previously created configuration
    // to initialize ADC block, start ADC block and start conversion.
    // &ADCD1 is pointer to ADC driver structure, defined in the depths of HAL.
    // Other arguments defined ourself earlier.
    adcInit();
    adcStart(&ADCD1, &adccfg);
    adcStartConversion(&ADCD1, &adccg, &samples_buf[0], ADC_BUF_DEPTH);

    // Thats all. Now your conversion run in background without assistance.
}
Beispiel #10
0
/*
 * Application entry point.
 */
int main(void) {

    /*
     * System initializations.
     * - HAL initialization, this also initializes the configured device drivers
     *   and performs the board-specific initializations.
     * - Kernel initialization, the main() function becomes a thread and the
     *   RTOS is active.
     */
    halInit();
    chSysInit();

    /*
     * Activates the serial driver 1 using the driver default configuration.
     */
    sdStart(&SD1, NULL);

    /*
     * Starting GPT4 driver, it is used for triggering the ADC.
     */
    gptStart(&GPTD4, &gpt4cfg1);

    /*
     * Fixed an errata on the STM32F7xx, the DAC clock is required for ADC
     * triggering.
     */
    rccEnableDAC1(false);

    /*
     * Activates the ADC1 driver and the temperature sensor.
     */
    adcStart(&ADCD1, NULL);
    adcSTM32EnableTSVREFE();

    /*
     * Starts an ADC continuous conversion triggered with a period of
     * 1/10000 second.
     */
    adcStartConversion(&ADCD1, &adcgrpcfg1, samples1, ADC_GRP1_BUF_DEPTH);
    gptStartContinuous(&GPTD4, 100);

    /*
     * Creates the example thread.
     */
    chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

    /*
     * Normal main() thread activity, in this demo it does nothing.
     */
    while (true) {
        chThdSleepMilliseconds(500);
    }
}
Beispiel #11
0
static void startFirstAudioADC()
{
#if IS_TEENSY3()
	adc->startSingleRead(AUDIO_INPUT_PIN); // ADC lib converts pin/channel in startSingleRead
#elif IS_STM32()
	uint8_t dummy = AUDIO_INPUT_PIN;
	adc.setPins(&dummy, 1);
	adc.startConversion();
#else
	adcStartConversion(adcPinToChannelNum(AUDIO_INPUT_PIN));
#endif
}
Beispiel #12
0
/**
 * @brief  Starts the ADC and ICU input drivers.
 * @note   ICU drivers used in the firmware are modified ChibiOS
 *         drivers for extended input capture functionality.
 * @return none.
 */
void mixedInputStart(void) {
  /* Activates the ICU2 and ICU3 drivers. */
  icuStart(&ICUD2, &icucfg2);
  icuStart(&ICUD3, &icucfg3);
  /* Starts continuous pulse width measurements. */
  icuEnable(&ICUD2);
  icuEnable(&ICUD3);

  /* Activates the ADC1 driver. */
  adcStart(&ADCD1, NULL);
  /* Starts an ADC continuous conversion. */
  adcStartConversion(&ADCD1, &adcgrpcfg, adcBuf, ADC_GRP_BUF_DEPTH);
}
Beispiel #13
0
/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * Setting up analog inputs used by the demo.
   */
  palSetGroupMode(GPIOC, PAL_PORT_BIT(1) | PAL_PORT_BIT(2),
                  0, PAL_MODE_INPUT_ANALOG);

  /*
   * Creates the blinker thread.
   */
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

  /*
   * Activates the ADC1 driver and the temperature sensor.
   */
  adcStart(&ADCD1, NULL);
  adcSTM32EnableTS(&ADCD1);
  adcSTM32EnableVBAT(&ADCD1);

  /*
   * Linear conversion.
   */
  adcConvert(&ADCD1, &adcgrpcfg1, samples1, ADC_GRP1_BUF_DEPTH);
  chThdSleepMilliseconds(1000);

  /*
   * Starts an ADC continuous conversion.
   */
  adcStartConversion(&ADCD1, &adcgrpcfg2, samples2, ADC_GRP2_BUF_DEPTH);

  /*
   * Normal main() thread activity, in this demo it does nothing.
   */
  while (true) {
    if (palReadPad(GPIOA, GPIOA_BUTTON)) {
      adcStopConversion(&ADCD1);
    }
    chThdSleepMilliseconds(500);
  }
}
adc_values_t readADC(void) {
    adc_values_t values;
    adcStart(&ADCD1, &adccfg);
    adcSTM32SetCCR(ADC_CCR_TSEN | ADC_CCR_VREFEN);
    adcStartConversion(&ADCD1, &adcgrpcfg, samples, 1);
    chThdSleepMilliseconds(500);
    adcStop(&ADCD1);

    values.vdda_voltage = get_vdda_voltage(samples[4]);
    values.supply_voltage = ((float)samples[0] / ADC_RES * values.vdda_voltage) / VSUPPMON_DIVIDER;
    values.batt_voltage = ((float)samples[2] / ADC_RES * values.vdda_voltage) / VBATTMON_DIVIDER;
    values.charge_current = ((float)samples[1] / ADC_RES * values.vdda_voltage) / ICHARGE_DIVIDER;
    values.internal_temp = get_temp(samples[3]);
    return values;
}
Beispiel #15
0
void run_test(const char * test_msg, uint8_t num_channels, uint8_t depth,
    bool circular) {
  print(test_msg);
  
  cb_arg = 0;
  
  group.num_channels = num_channels;
  group.circular = circular;
  group.end_cb = adc_callback;
  
  if (depth > 1) cb_expect = 2;
  else cb_expect = 1;
  if (circular) cb_expect *= 3;
  
  adcStartConversion(&ADCD1, &group, buffer, depth);
  
  while (ADCD1.state == ADC_ACTIVE) ;
  
  
  int index = 0;
  for (int j = 0; j < depth; j++) {
    for (int i = 0; i < group.num_channels; i++) {
      index = i + (j * group.num_channels);
      sniprintf(out_string, 128, chn_fmt_string, group.channels[i]);
      print(out_string);
      
      sniprintf(out_string, 128, raw_fmt_string, buffer[index]); 
      print(out_string);
      
      if (group.channels[i] == 30) { /* internal temp sensor */
        buffer[index] = adcMSP430XAdjustTemp(&group, buffer[index]);
      }
      else {
        buffer[index] = adcMSP430XAdjustResult(&group, buffer[index]);
      }
      
      sniprintf(out_string, 128, cooked_fmt_string, buffer[index]); 
      print(out_string);
    }
  }
  
  if (cb_arg == cb_expect) {
    print(success_string);
  }
  else {
    print(fail_string);
  }
}
Beispiel #16
0
/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * Creates the blinker thread.
   */
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

  /*
   * Activates the ADC1 driver, the temperature sensor and the VBat
   * measurement.
   */
  adcStart(&ADCD1, NULL);
  adcSTM32Calibrate(&ADCD1);
  adcSTM32EnableTSVREFE();
  adcSTM32EnableVBATE();

  /*
   * Linear conversion.
   */
  adcConvert(&ADCD1, &adcgrpcfg1, samples1, ADC_GRP1_BUF_DEPTH);
  chThdSleepMilliseconds(1000);

  /*
   * Starts an ADC continuous conversion.
   */
  adcStartConversion(&ADCD1, &adcgrpcfg2, samples2, ADC_GRP2_BUF_DEPTH);

  /*
   * Normal main() thread activity, in this demo it does nothing.
   */
  while (TRUE) {
    if (palReadPad(GPIOA, GPIOA_WKUP_BUTTON)) {
      adcStopConversion(&ADCD1);
    }
    chThdSleepMilliseconds(500);
  }
}
Beispiel #17
0
static msg_t sensorRead (void*Arg)
{
	if (dip2 == 1)
	{
		while (true)
		{
    		adcStartConversion (&ADCD1, &adcGrpCfg, irSamples, ADC_BUF_DEPTH);
    		chprintf ((BaseChannel *)&SD2, "adc = \n\r", irSamples);
		}
	}

	int i = 255;
	while (i >= 0)
	{
		chprintf((BaseChannel *)&SD2, "%d  ", i);
		move (FWD, i);
		chThdSleepMilliseconds (100);
		--i;
	}

	i = 0;
	while (i >= 0)
	{
		chprintf((BaseChannel *)&SD2, "%d  ", i);
		move (LEFT, i);
		chThdSleepMilliseconds (100);
		--i;
	}

	i = 0;
	while (i >= 0)
	{
		chprintf((BaseChannel *)&SD2, "%d  ", i);
		move (RIGHT, i);
		chThdSleepMilliseconds (100);
		--i;
	}

	i = 0;
	while (i >= 0)
	{
		chprintf((BaseChannel *)&SD2, "%d  ", i);
		move (REV, i);
		chThdSleepMilliseconds (100);
		--i;
	}
}
Beispiel #18
0
void bus_voltage_adc_conversion(void)
{
    // read bus voltage
    adcStartConversion(&ADCD1, &adcgrpcfg, adc_samples, ADC_BUF_DEPTH);
    chBSemWait(&adc_wait);

    // set LED
    float voltage = bus_voltage_get();
    if (voltage > 4.5f && voltage < 5.5f) {
        led_set(CAN1_PWR_LED);
    } else if (voltage > 0.5f || voltage > 5.5f) {
        // voltage warning
        led_toggle(CAN1_PWR_LED);
    } else {
        led_clear(CAN1_PWR_LED);
    }
}
Beispiel #19
0
static void adctodac(void *arg)
{
    uint16_t temp = 0;
    adcStart(&ADCD1, &adccfg);
    dacStart(&DACD1, &dac1cfg1);

    gptStart(&GPTD6, &gpt6cfg1);

    while(!0)
    {
        adcStartConversion(&ADCD1, &adccg, samples_buf, ADC_BUF_DEPTH);
        temp = samples_buf[0];
        //dacPutChannelX(&DACD1, 1U, temp);
        dacStartConversion(&DACD1, &dacgrpcfg1, samples_buf, ADC_BUF_DEPTH);
        gptStartContinuous(&GPTD6, 2U);
        chThdSleepMilliseconds(1);
    }
}
/**
 * @brief   get the ADC sampled data of the Diagnostic Pin.
 *
 * @description todo
 *
 * @image html  todo
 * @image rtf   todo
 * @image latex todo
 *
 * @param [in]  *u16Data received data pointe.After the execution of the API this will hold the sampled data.

 *
 * @return      TRUE : no errors
 *              FALSE: Errors in execution
 *
 * Example Usage:
 * @code
 *      todo
 * @endcode
 *
 * @entrymode   todo
 * @exitmode    todo
 *
 * @seealso     todo
 *
 * **Note: todo**
 *
 * @destag      todo
 * @reqtag      todo
 */
boolean TPSDiagosticPinSampledData(uint16* u16Data)
{
    adcData_t adcdata;
    uint32 count;
    boolean blRetVal = TRUE;

    adcStartConversion(Adc_Param_Handle->ADC_TPS, Adc_Param_Handle->group);
    while(!adcIsConversionComplete(Adc_Param_Handle->ADC_TPS, Adc_Param_Handle->group));
    count = adcGetData(Adc_Param_Handle->ADC_TPS, Adc_Param_Handle->group, &adcdata);
    if((1U == count)&&(0U == adcdata.id))
    {
        *u16Data = adcdata.value;
        blRetVal = TRUE;
    }
    else
    {
        blRetVal = FALSE;
    }
    return blRetVal;;
}
Beispiel #21
0
/* Thread used for read data from sonar */
static msg_t irThread(void *arg) {
	(void) arg;
	char buf[40];

	/*
	 * Activates the ADC1 driver.
	 */
	adcStart(&ADCD1, NULL);
	adcStartConversion(&ADCD1, &adc_grpcfg, (adcsample_t *)&ir_data, ADC_BUF_DEPTH);

	while (TRUE) {
		chsprintf(buf, "[IR] N:%d,S:%d,W:%d,E:%d", ir_data.north, ir_data.south,
				ir_data.west, ir_data.east);

		bufferPutString(&outputBuffer, buf);
		chThdSleepMilliseconds(50);
	}

	return 0;
}
Beispiel #22
0
int main( void )
{
	//disable interrupts
	cli();	
	//INITS				
	hwInit();
//	uartInit();
	pwmInit();	
//	adcInit();
//	WdInit();
//	WdDisable();
	adcStartConversion();
	//enable interrupt
	Init_TWI();
	sei();
	//calibration des vitesses
//	adcCalibSeq();
	//attend que la sw Start soit appuyer 
	//pour lancer le reste du programme	
	SLWaitForTheStartSw();

//	WD_RESTART_WATCHDOG;
	
	
	while(1)
	{
		if(flag5ms)
		{
			count5ms++;
			if(count5ms>=10)
			{
				count5ms=0;
				Ping_sensor();
			}
		}
		SLCheckSwStatus(); //verifie la switch d'arret
		cPMainCmdParser(); //Machine à état communication
		CalculMoteur();	   //calculPWM et autre			
	}
}
void readVoltage(void)
{
	uint8_t thisTemp;
	uint32_t i;
	char Result2[6];

	adcStartConversion(&ADCD1, &adcgrpcfg2, samples2, ADC_GRP2_BUF_DEPTH);

	  sum=0;

	  for (i=0;i<=ADC_GRP2_BUF_DEPTH;i++){
	      sum += samples2[i];
	  }
	  sum /= ADC_GRP2_BUF_DEPTH;

	  thisTemp = (((int64_t)sum)*VREFINT*400/VREFMeasured-30400)+2500;
	  chprintf((BaseSequentialStream *)&SD2, "\r\nTemperature: %d.%2U°C", thisTemp/100,thisTemp%100);

	  sprintf(Result, "%d", (uint8_t)thisTemp/100);
	  sprintf(Result2, ".%iC", (uint8_t)thisTemp%100);
	  strncat(Result, Result2, 6);
	  gwinSetText(ADCvalue2, Result, TRUE);
}
Beispiel #24
0
/**
 * Adc init
 *
 * Initialize ADC drivers, buffers and start conversion in the background
 */
void adc_init(void)
{
  /* Init GPIO ports for ADC operation
   */
#if USE_ADC_1
  PRINT_CONFIG_MSG("Info: Using ADC_1");
  gpio_setup_pin_analog(ADC_1_GPIO_PORT, ADC_1_GPIO_PIN);
#endif
#if USE_ADC_2
  PRINT_CONFIG_MSG("Info: Using ADC_2");
  gpio_setup_pin_analog(ADC_2_GPIO_PORT, ADC_2_GPIO_PIN);
#endif
#if USE_ADC_3
  PRINT_CONFIG_MSG("Info: Using ADC_3");
  gpio_setup_pin_analog(ADC_3_GPIO_PORT, ADC_3_GPIO_PIN);
#endif
#if USE_ADC_4
  PRINT_CONFIG_MSG("Info: Using ADC_4");
  gpio_setup_pin_analog(ADC_4_GPIO_PORT, ADC_4_GPIO_PIN);
#endif
#if USE_ADC_5
  PRINT_CONFIG_MSG("Info: Using ADC_5");
  gpio_setup_pin_analog(ADC_5_GPIO_PORT, ADC_5_GPIO_PIN);
#endif
#if USE_ADC_6
  PRINT_CONFIG_MSG("Info: Using ADC_6");
  gpio_setup_pin_analog(ADC_6_GPIO_PORT, ADC_6_GPIO_PIN);
#endif
#if USE_ADC_7
  PRINT_CONFIG_MSG("Info: Using ADC_7");
  gpio_setup_pin_analog(ADC_7_GPIO_PORT, ADC_7_GPIO_PIN);
#endif
#if USE_ADC_8
  PRINT_CONFIG_MSG("Info: Using ADC_8");
  gpio_setup_pin_analog(ADC_8_GPIO_PORT, ADC_8_GPIO_PIN);
#endif
#if USE_ADC_9
  PRINT_CONFIG_MSG("Info: Using ADC_9");
  gpio_setup_pin_analog(ADC_9_GPIO_PORT, ADC_9_GPIO_PIN);
#endif

  // Configurtion register
  uint32_t sqr1, sqr2, sqr3;
  adc_regular_sequence(&sqr1, &sqr2, &sqr3, ADC_NUM_CHANNELS, adc_channel_map);

#ifdef __STM32F10x_H
  uint32_t smpr1, smpr2;
  adc_sample_time_on_all_channels(&smpr1, &smpr2, ADC_SAMPLE_41P5);

  adcgrpcfg.cr2 = ADC_CR2_TSVREFE;
#elif defined(__STM32F4xx_H)
  uint32_t smpr1, smpr2;
  adc_sample_time_on_all_channels(&smpr1, &smpr2, ADC_SAMPLE_480);

  adcgrpcfg.cr2 = ADC_CR2_SWSTART;
#endif

#if USE_ADC_WATCHDOG
  adc_watchdog.adc = NULL;
  adc_watchdog.cb = NULL;
  adc_watchdog.channel = 0;
  adc_watchdog.vmin = (1<<12)-1; // max adc
#endif

  adcgrpcfg.circular = TRUE;
  adcgrpcfg.num_channels = ADC_NUM_CHANNELS;
  adcgrpcfg.end_cb = adc1callback;
  adcgrpcfg.error_cb = adcerrorcallback;
  adcgrpcfg.cr1 = 0;
  adcgrpcfg.smpr1 = smpr1;
  adcgrpcfg.smpr2 = smpr2;
  adcgrpcfg.sqr1 = sqr1;
  adcgrpcfg.sqr2 = sqr2;
  adcgrpcfg.sqr3 = sqr3;

  // Start ADC in continious conversion mode
  adcStart(&ADCD1, NULL);
  adcStartConversion(&ADCD1, &adcgrpcfg, adc_samples, ADC_BUF_DEPTH);
}
Beispiel #25
0
static void pcbPwmAdcTrigger(PWMDriver *pwmp) {
  (void) pwmp;
  adcStartConversion(&ADCD1, &adcZeroSense, &zeroCrossing, 1);
}
Beispiel #26
0
void adc_convert(void) {
  adcStopConversion(&ADCD1);
  adcStartConversion(&ADCD1, &adcgrpcfg1, adcvalues, ADC_GRP1_BUF_DEPTH);
}
Beispiel #27
0
int main()
{
	DDRB |= _BV(5);
	PORTB &= ~_BV(5);

    /////////////////////////////
    // Set up the analogue input
    setupAdcOnPin(0);

    adcStartConversion(); // Start ADC measurement

    pulseSetFrameBuffer(frameBuffer, NUM_LEDS);

    const uint8_t pulseCount = 10;
    uint8_t pulseIdx = 0;
    struct cPulse pulses[pulseCount];
    memset(pulses, 0, sizeof(struct cPulse) * pulseCount);

    for (int i = 0; i < pulseCount; i += 2)
    {
        pulses[i].direction = 1;
    }

    uint8_t sampleHistory[HISTORY_COUNT];
    memset(sampleHistory, 0, HISTORY_COUNT);

    int8_t startingPosition = 0;

    while(1)
    {
    	++toggle;
    	toggle = toggle % 50;
        setIndicator(toggle == 0);

        // Hist = = = = =
        // Wind       - -
        // Copy   - -
        for (int i = 0; i < (HISTORY_COUNT - WINDOW_SIZE); ++i)
        {
            sampleHistory[i] = sampleHistory[i + WINDOW_SIZE];
        }

        for (int i = 0; i < WINDOW_SIZE; ++i)
        {
            adcStartConversion();
            _delay_us(500);
            adcWaitReady();
            sampleHistory[WINDOW_START + i] = ADCH;
        }

        uint8_t historicalPeak = findPeak(sampleHistory, 0, WINDOW_START);
        uint8_t currentPeak = findPeak(sampleHistory, WINDOW_START, WINDOW_SIZE);

        if (isPulseDetected(historicalPeak, currentPeak))
        {
            // Generate a new pulse
            if (++pulseIdx >= pulseCount) pulseIdx = 0;

            pulses[pulseIdx].colour.h = rand() % MAX_HUE;
            pulses[pulseIdx].colour.s = (MAX_SAT / 2) + (rand() % (MAX_SAT / 2));
            pulses[pulseIdx].colour.v = MAX_VAL;
            pulses[pulseIdx].position = startingPosition;
            startingPosition -= 3;
            if (startingPosition < 0) startingPosition += NUM_LEDS;
        }

        pulseClearFrameBuffer();

        for (int i = 0; i < pulseCount; ++i)
        {
            pulseUpdate(pulses + i);
            pulseRender(pulses + i);
        }

        ws2812_setleds(frameBuffer, 40); // Blocks for ~1.2ms
        _delay_ms(2);         // The sensor line will be noisy for a little while
    }
}
Beispiel #28
0
/*
 * Application entry point.
 */
int main(void) {
  unsigned i;
  static uint8_t patterns1[4096], patterns2[4096], buf1[4096], buf2[4096];

  /* System initializations.
     - HAL initialization, this also initializes the configured device drivers
       and performs the board-specific initializations.
     - Kernel initialization, the main() function becomes a thread and the
       RTOS is active.*/
  halInit();
  chSysInit();

  /* Creates the blinker thread.*/
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 10,
                    Thread1, NULL);

  /* Activates the ADC1 driver and the temperature sensor.*/
  adcStart(&ADCD1, NULL);
  adcSTM32EnableTSVREFE();

  /* Starts an ADC continuous conversion and its watchdog virtual timer.*/
  chVTSet(&adcvt, MS2ST(10), tmo, (void *)"ADC timeout");
  adcStartConversion(&ADCD1, &adcgrpcfg2, samples2, ADC_GRP2_BUF_DEPTH);

  /* Activating SPI drivers.*/
  spiStart(&SPID1, &hs_spicfg);
  spiStart(&SPID2, &hs_spicfg);
  spiStart(&SPID3, &hs_spicfg);

  /* Starting SPI threads instances.*/
  chThdCreateStatic(waSPI1, sizeof(waSPI1), NORMALPRIO + 1, spi_thread, &SPID1);
  chThdCreateStatic(waSPI2, sizeof(waSPI2), NORMALPRIO + 1, spi_thread, &SPID2);
  chThdCreateStatic(waSPI3, sizeof(waSPI3), NORMALPRIO + 1, spi_thread, &SPID3);

  /* Allocating two DMA2 streams for memory copy operations.*/
  if (dmaStreamAllocate(STM32_DMA2_STREAM6, 0, NULL, NULL))
    chSysHalt("DMA already in use");
  if (dmaStreamAllocate(STM32_DMA2_STREAM7, 0, NULL, NULL))
    chSysHalt("DMA already in use");
  for (i = 0; i < sizeof (patterns1); i++)
    patterns1[i] = (uint8_t)i;
  for (i = 0; i < sizeof (patterns2); i++)
    patterns2[i] = (uint8_t)(i ^ 0xAA);

  /* Normal main() thread activity, it does continues memory copy operations
     using 2 DMA streams at the lowest priority.*/
  while (true) {
    virtual_timer_t vt;

    chVTObjectInit(&vt);

    /* Starts a VT working as watchdog to catch a malfunction in the DMA
       driver.*/
    chVTSet(&vt, MS2ST(10), tmo, (void *)"copy timeout");

    /* Copy pattern 1.*/
    dmaStartMemCopy(STM32_DMA2_STREAM6,
                    STM32_DMA_CR_PL(0) | STM32_DMA_CR_PSIZE_BYTE |
                                         STM32_DMA_CR_MSIZE_BYTE,
                    patterns1, buf1, sizeof (patterns1));
    dmaStartMemCopy(STM32_DMA2_STREAM7,
                    STM32_DMA_CR_PL(0) | STM32_DMA_CR_PSIZE_BYTE |
                                         STM32_DMA_CR_MSIZE_BYTE,
                    patterns1, buf2, sizeof (patterns1));
    dmaWaitCompletion(STM32_DMA2_STREAM6);
    dmaWaitCompletion(STM32_DMA2_STREAM7);
    if (memcmp(patterns1, buf1, sizeof (patterns1)))
      chSysHalt("pattern error");
    if (memcmp(patterns1, buf2, sizeof (patterns1)))
      chSysHalt("pattern error");

    /* Copy pattern 2.*/
    dmaStartMemCopy(STM32_DMA2_STREAM6,
                    STM32_DMA_CR_PL(0) | STM32_DMA_CR_PSIZE_BYTE |
                                         STM32_DMA_CR_MSIZE_BYTE,
                    patterns2, buf1, sizeof (patterns2));
    dmaStartMemCopy(STM32_DMA2_STREAM7,
                    STM32_DMA_CR_PL(0) | STM32_DMA_CR_PSIZE_BYTE |
                                         STM32_DMA_CR_MSIZE_BYTE,
                    patterns2, buf2, sizeof (patterns2));
    dmaWaitCompletion(STM32_DMA2_STREAM6);
    dmaWaitCompletion(STM32_DMA2_STREAM7);
    if (memcmp(patterns2, buf1, sizeof (patterns2)))
      chSysHalt("pattern error");
    if (memcmp(patterns2, buf2, sizeof (patterns2)))
      chSysHalt("pattern error");

    /* Stops the watchdog.*/
    chVTReset(&vt);

    chThdSleepMilliseconds(2);
  }
  return 0;
}
Beispiel #29
0
Datei: cmdadc.c Projekt: J-L/BluC
tfunc_t adcConversionThread(void) 
{
	adcStart(&ADCD1, NULL);
  	adcStartConversion(&ADCD1, &adcSettings, &samples, 8);
	return (msg_t)0;  
}
Beispiel #30
0
void captureSamples()
{
		if(adcConfigured)
		{
			//if(!sampleRun)
			{
				adcGetConversion();
				switch (sampleBank)
				{
				case 0:

					samplesA[sampleIndex*2] = ((uhADCxConvertedValue - 2048.0f) / 4096.0f); // - 2048;
					samplesA[sampleIndex*2 + 1] = ((uhADCxConvertedValue2 - 2048.0f) / 4096.0f); // - 2048;//0.0;

					if(uhADCxConvertedValue > maxAmplitude) maxAmplitude = uhADCxConvertedValue;
					if(uhADCxConvertedValue2 > maxAmplitude) maxAmplitude = uhADCxConvertedValue2;

					if(samplesB[sampleIndex*2] > agcLevel) agcLevel = samplesB[sampleIndex*2];
					if(samplesB[sampleIndex*2+1] > agcLevel) agcLevel = samplesB[sampleIndex*2+1];
//					if(sampleIndex < filterKernelLength)
//					{
//						dac1SetValue(samplesB[sampleIndex*2] + samplesA[(FFT_SIZE - filterKernelLength)
//								+ sampleIndex * 2] /*/ (agcLevel * agcScale)*/ * 4096 * gain + 2048);
//						dac2SetValue(samplesB[sampleIndex*2+1] + samplesA[(FFT_SIZE - filterKernelLength)
//						        + sampleIndex * 2] /*/ (agcLevel * agcScale)*/ * 4096 * gain + 2048);
//					} else {
						dac1SetValue(samplesB[sampleIndex*2] /*/ (agcLevel * agcScale)*/ * 4096 * afGain + 2048);
						dac2SetValue(samplesB[sampleIndex*2+1] /*/ (agcLevel * agcScale)*/ * 4096 * afGain + 2048);
//					}

					if(sampleIndex > FFT_SIZE - filterKernelLength - 1)
					{
						samplesOverlap[(sampleIndex - (FFT_SIZE - filterKernelLength))*2] = samplesA[sampleIndex*2];
						samplesOverlap[(sampleIndex - (FFT_SIZE - filterKernelLength)) * 2 +1] = samplesA[sampleIndex*2+1];
					}

					break;

				case 1:

					samplesB[sampleIndex*2] = ((uhADCxConvertedValue - 2048.0f) / 4096.0f); // - 2048;
					samplesB[sampleIndex*2 + 1] = ((uhADCxConvertedValue2 - 2048.0f) / 4096.0f); // - 2048;//0.0;

					if(uhADCxConvertedValue > maxAmplitude) maxAmplitude = uhADCxConvertedValue;
					if(uhADCxConvertedValue2 > maxAmplitude) maxAmplitude = uhADCxConvertedValue2;

					if(samplesC[sampleIndex*2] > agcLevel) agcLevel =samplesC[sampleIndex*2];
					if(samplesC[sampleIndex*2+1] > agcLevel) agcLevel = samplesC[sampleIndex*2+1];
//					if(sampleIndex < filterKernelLength)
//					{
//						dac1SetValue(samplesC[sampleIndex*2] + samplesB[(FFT_SIZE - filterKernelLength)
//								+ sampleIndex * 2] /*/ (agcLevel * agcScale)*/ * 4096 * gain + 2048);
//						dac2SetValue(samplesC[sampleIndex*2+1] + samplesB[(FFT_SIZE - filterKernelLength)
//						        + sampleIndex * 2] /*/ (agcLevel * agcScale)*/ * 4096 * gain + 2048);
//					} else {
						dac1SetValue(samplesC[sampleIndex*2] /*/ (agcLevel * agcScale)*/ * 4096 * afGain + 2048);
						dac2SetValue(samplesC[sampleIndex*2+1] /*/ (agcLevel * agcScale)*/ * 4096 * afGain + 2048);
//					}

						if(sampleIndex > FFT_SIZE - filterKernelLength - 1)
						{
							samplesOverlap[(sampleIndex - (FFT_SIZE - filterKernelLength))*2] = samplesB[sampleIndex*2];
							samplesOverlap[(sampleIndex - (FFT_SIZE - filterKernelLength)) * 2 +1] = samplesB[sampleIndex*2+1];
						}

					break;

				case 2:

					samplesC[sampleIndex*2] = ((uhADCxConvertedValue - 2048.0f) / 4096.0f); // - 2048;
					samplesC[sampleIndex*2 + 1] = ((uhADCxConvertedValue2 - 2048.0f) / 4096.0f); // - 2048;//0.0;

					if(uhADCxConvertedValue > maxAmplitude) maxAmplitude = uhADCxConvertedValue;
					if(uhADCxConvertedValue2 > maxAmplitude) maxAmplitude = uhADCxConvertedValue2;

					if(samplesA[sampleIndex*2] > agcLevel) agcLevel = samplesA[sampleIndex*2];
					if(samplesA[sampleIndex*2+1] > agcLevel) agcLevel = samplesA[sampleIndex*2+1];
//					if(sampleIndex < filterKernelLength)
//					{
//						dac1SetValue(samplesA[sampleIndex*2] + samplesC[(FFT_SIZE - filterKernelLength)
//								+ sampleIndex * 2] /*/ (agcLevel * agcScale)*/ * 4096 * gain + 2048);
//						dac2SetValue(samplesA[sampleIndex*2+1] + samplesC[(FFT_SIZE - filterKernelLength)
//						        + sampleIndex * 2] /*/ (agcLevel * agcScale)*/ * 4096 * gain + 2048);
//					} else {
						dac1SetValue(samplesA[sampleIndex*2] /*/ (agcLevel * agcScale)*/ * 4096 * afGain + 2048);
						dac2SetValue(samplesA[sampleIndex*2+1] /*/ (agcLevel * agcScale)*/ * 4096 * afGain + 2048);
//					}

						if(sampleIndex > FFT_SIZE - filterKernelLength - 1)
						{
							samplesOverlap[(sampleIndex - (FFT_SIZE - filterKernelLength))*2] = samplesC[sampleIndex*2];
							samplesOverlap[(sampleIndex - (FFT_SIZE - filterKernelLength)) * 2 +1] = samplesC[sampleIndex*2+1];
						}

					break;
				}
				//dac1SetValue(outputSamplesA[sampleIndex*2]);

				agcLevel = agcLevel * (1.0f - 0.0001f);

				sampleIndex++;
				if(sampleIndex >= FFT_SIZE - (filterKernelLength/2))
				{
					sampleRun = 1;
					sampleIndex = filterKernelLength/2; //0;
					switch(sampleBank)
					{
					case 0:
						sampleBankAReady = 1;
						sampleBank = 1;
						zeroSampleBank(samplesB);
						break;
					case 1:
						sampleBankBReady = 1;
						sampleBank = 2;
						zeroSampleBank(samplesC);
						break;
					case 2:
						sampleBankCReady = 1;
						sampleBank = 0;
						zeroSampleBank(samplesA);
						break;
					}



					//sampleBank = sampleBank++ % 3;

//					if(sampleBank == 0)
//					{
//						sampleBankAReady = 1;
//						sampleBank = 1;
//
//					} else {
//						sampleBankBReady = 1;
//						sampleBank = 0;
//					}
				}

				adcStartConversion();
			}
		}
	}