/* * 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); } }
int main (void) { halInit(); chSysInit(); sdStart (&SD2, NULL); pwmStart (&PWMD3, &pwmCfg); adcStart (&ADCD1, &adcGrpCfg); palSetPadMode (GPIOA, 6, PAL_MODE_STM32_ALTERNATE_PUSHPULL); palSetPadMode (GPIOA, 7, PAL_MODE_STM32_ALTERNATE_PUSHPULL); palSetPadMode (GPIOB, 0, PAL_MODE_STM32_ALTERNATE_PUSHPULL); palSetPadMode (GPIOB, 1, PAL_MODE_STM32_ALTERNATE_PUSHPULL); palSetPadMode (GPIOC, 2, PAL_MODE_INPUT_ANALOG); chThdCreateStatic (heartWrkArea, sizeof (heartWrkArea), NORMALPRIO, heartBeat, NULL); chThdCreateStatic (sensorWrkArea, sizeof (sensorWrkArea), NORMALPRIO, sensorRead, NULL); }
/* * Board-specific initialization code. */ void boardInit(void) { rccEnableAHB3(RCC_AHB3ENR_FSMCEN, FALSE); // Bank 1 Control Register FSMC_Bank1->BTCR[0] = FSMC_BCR1_MBKEN | FSMC_BCR1_MWID_0 | FSMC_BCR1_WREN | FSMC_BCR1_EXTMOD; // Bank 1 Read Timing Register FSMC_Bank1->BTCR[1] = FSMC_BTR1_CLKDIV_0 | FSMC_BTR1_ADDSET_0 | FSMC_BTR1_DATAST_0 | FSMC_BTR1_DATAST_2; // Bank 1 Write Timing Register FSMC_Bank1E->BWTR[0] = FSMC_BWTR1_CLKDIV_0 | FSMC_BWTR1_ADDSET_0 | FSMC_BWTR1_DATAST_1; adcStart(&ADCD1, NULL); adcSTM32EnableTSVREFE(); }
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); } }
void adc_configpads(void) { #if ((BOARD_AXOLOTI_V03)||(BOARD_AXOLOTI_V05)) palSetPadMode(GPIOA, 0, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, 1, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, 2, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, 3, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, 7, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOB, 0, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOB, 1, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOC, 0, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOC, 1, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOC, 2, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOC, 3, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOC, 4, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOC, 5, PAL_MODE_INPUT_ANALOG); #elif (BOARD_STM32F4DISCOVERY) palSetPadMode(GPIOA, 0, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, 1, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, 2, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, 3, PAL_MODE_INPUT_ANALOG); // skip GPIOA4: LRCLK // skip GPIOA5,GPIOA6,GPIOA7: accelerometer palSetPadMode(GPIOB, 0, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOB, 1, PAL_MODE_INPUT_ANALOG); //skip GPIOPC0: USB PowerOn palSetPadMode(GPIOC, 1, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOC, 2, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOC, 3, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOC, 4, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOC, 5, PAL_MODE_INPUT_ANALOG); adcStart(&ADCD1, NULL); #else #error "ADC: No board defined?" #endif }
/* 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; }
/* * 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(0) | PAL_PORT_BIT(1), PAL_MODE_INPUT_ANALOG); /* * Creates the blinker thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); /* * Starts an ADC continuous conversion. */ adcStart(&ADCD1, NULL); adcStartConversion(&ADCD1, &adcgrpcfg, samples, ADC_GRP1_BUF_DEPTH); /* * Normal main() thread activity, in this demo it does nothing. */ while (TRUE) { if (palReadPad(GPIOA, GPIOA_BUTTON)) adcStopConversion(&ADCD1); chThdSleepMilliseconds(500); } return 0; }
/* * Application entry point. */ int main(void) { static const evhandler_t evhndl[] = { WKUP_button_handler }; struct EventListener el0; /* * 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(); /* * Initialize event structures BEFORE using them */ chEvtInit(&wkup_event); /* * Shell manager initialization. */ usbSerialShellStart(commands); // Enable Continuous GPT for 1ms Interval gptStart(&GPTD1, &gpt1cfg); gptStartContinuous(&GPTD1,10000); // Configure pins for Feedback ADC's palSetPadMode(GPIOA, GPIOA_PIN4, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOA, GPIOA_PIN5, PAL_MODE_INPUT_ANALOG); // Configure pins for Input ADC's palSetPadMode(GPIOF, GPIOF_PIN6, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOF, GPIOF_PIN7, PAL_MODE_INPUT_ANALOG); // Configure pins for LED's // PD3: Vertical Axis LED palSetPadMode(GPIOD, GPIOD_PIN3, PAL_MODE_OUTPUT_PUSHPULL); // PD4: Lateral Axis LED palSetPadMode(GPIOD, GPIOD_PIN4, PAL_MODE_OUTPUT_PUSHPULL); // Configure pins for switches // PD8: Drive Enable Switch palSetPadMode(GPIOD, GPIOD_PIN8, PAL_MODE_INPUT); // PD9: Mode Select Switch palSetPadMode(GPIOD, GPIOD_PIN9, PAL_MODE_INPUT); // Configure pins for long lead GMD Enable/Watchdog // PD5: Enable out to GMD palSetPadMode(GPIOD, GPIOD_PIN5, PAL_MODE_OUTPUT_PUSHPULL); // PD6: Watchdog out to GMD palSetPadMode(GPIOD, GPIOD_PIN6, PAL_MODE_OUTPUT_PUSHPULL); // Configure pins for short lead GMD Enable/Watchdog // PD10: Enable out to GMD palSetPadMode(GPIOD, GPIOD_PIN10, PAL_MODE_OUTPUT_PUSHPULL); // PD11: Watchdog out to GMD palSetPadMode(GPIOD, GPIOD_PIN11, PAL_MODE_OUTPUT_PUSHPULL); // Configure pins for PWM output (D12-D15: TIM4, channel 1-4) palSetPadMode(GPIOD, GPIOD_PIN12, PAL_MODE_ALTERNATE(2)); //U-pole, short lead palSetPadMode(GPIOD, GPIOD_PIN13, PAL_MODE_ALTERNATE(2)); //V-pole, short lead palSetPadMode(GPIOD, GPIOD_PIN14, PAL_MODE_ALTERNATE(2)); //U-pole, long lead palSetPadMode(GPIOD, GPIOD_PIN15, PAL_MODE_ALTERNATE(2)); //V-pole, long lead adcStart(&ADCD1, NULL); adcStart(&ADCD2, NULL); adcStart(&ADCD3, NULL); pwmStart(&PWMD4, &pwmcfg); // Enable TIM4 PWM channel 1-4 with initial DC=0% /* @param[in] pwmp pointer to a @p PWMDriver object * @param[in] channel PWM channel identifier (0...PWM_CHANNELS-1) * @param[in] width PWM pulse width as clock pulses number */ pwmEnableChannel(&PWMD4, 0, 0); pwmEnableChannel(&PWMD4, 1, 0); pwmEnableChannel(&PWMD4, 2, 0); pwmEnableChannel(&PWMD4, 3, 0); // Set axis control gain and limit values // Set Vertical Axis Gains vertAxisStruct.U16PositionPGain = 4; vertAxisStruct.U16PositionIGain = 1; vertAxisStruct.U16PositionDGain = 0; // Set vertical axis limits vertAxisStruct.U16CommandLimit = VERTICAL_COMMAND_LIMIT; vertAxisStruct.U16HighPosnLimit = 5100; vertAxisStruct.U16LowPosnLimit = 2480; // Set Lateral Axis Gains latAxisStruct.U16PositionPGain = 2; latAxisStruct.U16PositionIGain = 0; latAxisStruct.U16PositionDGain = 0; // Set lateral axis limits latAxisStruct.U16CommandLimit = LATERAL_COMMAND_LIMIT; latAxisStruct.U16HighPosnLimit = 5300; latAxisStruct.U16LowPosnLimit = 3100; /* * Activates the serial driver 6 and SDC driver 1 using default * configuration. */ sdStart(&SD6, NULL); /* * Activates the EXT driver 1. * This is for the external interrupt */ extStart(&EXTD1, &extcfg); /* * Normal main() thread activity, in this demo it does nothing except * sleeping in a loop and listen for events. */ chEvtRegister(&wkup_event, &el0, 0); while (TRUE) { //Cycle motordrive if timer fails if(U32DelayCount++ > 2500){ motordrive(&GPTD1); } chEvtDispatch(evhndl, chEvtWaitOneTimeout(ALL_EVENTS, MS2ST(500))); } }
int main(void) { /* * Start OS and HAL */ halInit(); chSysInit(); setupIPC(); DEBUGEN(printf("App Mode\n")); usbDisconnectBus(&USBD1); /* * Initialize extra driver objects. */ sduObjectInit(&SDU1); sduObjectInit(&SDU2); /* * Start peripherals */ sdStart(&SD1, &uart1Cfg); sdStart(&SD2, &uart2Cfg); sdStart(&SD3, &uart3Cfg); usbStart(&USBD1, &usbcfg); sduStart(&SDU1, &serusbcfg1); sduStart(&SDU2, &serusbcfg2); canStart(&CAND1, &cancfg); dacStart(&DACD1, &dac1cfg1); adcStart(&ADCD1, NULL); adcStart(&ADCD3, NULL); timcapStart(&TIMCAPD3, &tc_conf); pwmStart(&PWMD_LED2, &pwmcfg); eeInit(); // Compare and update versions in EEprom if needed. version_t v; if (readVersionFromEE(VERSION_IDX_APP, &v) == 0 && memcmp(&versions, &v, sizeof(version_t)) != 0) { writeVersionToEE(VERSION_IDX_APP, &versions[VERSION_IDX_APP]); } if (readVersionFromEE(VERSION_IDX_BL, &v) == 0 ) { memcpy(&versions[VERSION_IDX_BL], &v, sizeof(version_t)); } /* * Creates the threads. */ chThdCreateStatic(waThreadBDU, sizeof(waThreadBDU), NORMALPRIO+5, ThreadBDU, NULL); chThdCreateStatic(waThreadSDU, sizeof(waThreadSDU), NORMALPRIO+2, ThreadSDU, NULL); chThdCreateStatic(waThreadADC, sizeof(waThreadADC), NORMALPRIO, ThreadADC, NULL); chThdCreateStatic(waThreadKnock, sizeof(waThreadKnock), NORMALPRIO, ThreadKnock, NULL); chThdCreateStatic(waThreadCAN, sizeof(waThreadCAN), NORMALPRIO, ThreadCAN, NULL); chThdCreateStatic(waThreadSER1, sizeof(waThreadSER1), NORMALPRIO, ThreadSER1, NULL); chThdCreateStatic(waThreadRecord, sizeof(waThreadRecord), NORMALPRIO+1, ThreadRecord, NULL); chThdCreateStatic(waThreadWdg, sizeof(waThreadWdg), HIGHPRIO, ThreadWdg, NULL); /* Create last as it uses pointers from above */ chThdCreateStatic(waThreadMonitor, sizeof(waThreadMonitor), NORMALPRIO+10, ThreadMonitor, NULL); while (TRUE) { while(USBD1.state != USB_READY) chThdSleepMilliseconds(10); chThdSleepMilliseconds(100); if (usbConnected()) { usbConnectBus(&USBD1); } else { usbDisconnectBus(&USBD1); } } }
void initAdcInputs(bool boardTestMode) { printMsg(&logger, "initAdcInputs()"); if (ADC_BUF_DEPTH_FAST > MAX_ADC_GRP_BUF_DEPTH) firmwareError(CUSTOM_ERR_ADC_DEPTH_FAST, "ADC_BUF_DEPTH_FAST too high"); if (ADC_BUF_DEPTH_SLOW > MAX_ADC_GRP_BUF_DEPTH) firmwareError(CUSTOM_ERR_ADC_DEPTH_SLOW, "ADC_BUF_DEPTH_SLOW too high"); configureInputs(); // migrate to 'enable adcdebug' addConsoleActionI("adcdebug", &setAdcDebugReporting); #if EFI_INTERNAL_ADC /* * Initializes the ADC driver. */ adcStart(&ADC_SLOW_DEVICE, NULL); adcStart(&ADC_FAST_DEVICE, NULL); adcSTM32EnableTSVREFE(); // Internal temperature sensor for (int adc = 0; adc < HW_MAX_ADC_INDEX; adc++) { adc_channel_mode_e mode = adcHwChannelEnabled[adc]; /** * in board test mode all currently enabled ADC channels are running in slow mode */ if (mode == ADC_SLOW || (boardTestMode && mode == ADC_FAST)) { slowAdc.enableChannelAndPin((adc_channel_e) (ADC_CHANNEL_IN0 + adc)); } else if (mode == ADC_FAST) { fastAdc.enableChannelAndPin((adc_channel_e) (ADC_CHANNEL_IN0 + adc)); } } // Internal temperature sensor, Available on ADC1 only slowAdc.enableChannel((adc_channel_e)ADC_CHANNEL_SENSOR); slowAdc.init(); #if HAL_USE_PWM || defined(__DOXYGEN__) pwmStart(EFI_INTERNAL_SLOW_ADC_PWM, &pwmcfg_slow); pwmEnablePeriodicNotification(EFI_INTERNAL_SLOW_ADC_PWM); #endif /* HAL_USE_PWM */ if (CONFIGB(isFastAdcEnabled)) { fastAdc.init(); /* * Initializes the PWM driver. */ #if HAL_USE_PWM || defined(__DOXYGEN__) pwmStart(EFI_INTERNAL_FAST_ADC_PWM, &pwmcfg_fast); pwmEnablePeriodicNotification(EFI_INTERNAL_FAST_ADC_PWM); #endif /* HAL_USE_PWM */ } // ADC_CHANNEL_IN0 // PA0 // ADC_CHANNEL_IN1 // PA1 // ADC_CHANNEL_IN2 // PA2 // ADC_CHANNEL_IN3 // PA3 // ADC_CHANNEL_IN4 // PA4 // ADC_CHANNEL_IN5 // PA5 - this is also TIM2_CH1 // ADC_CHANNEL_IN6 // PA6 // ADC_CHANNEL_IN7 // PA7 // ADC_CHANNEL_IN8 // PB0 // ADC_CHANNEL_IN9 // PB1 // ADC_CHANNEL_IN10 // PC0 // ADC_CHANNEL_IN11 // PC1 // ADC_CHANNEL_IN12 // PC2 // ADC_CHANNEL_IN13 // PC3 // ADC_CHANNEL_IN14 // PC4 // ADC_CHANNEL_IN15 // PC5 //if(slowAdcChannelCount > ADC_MAX_SLOW_CHANNELS_COUNT) // todo: do we need this logic? do we need this check addConsoleActionI("adc", (VoidInt) printAdcValue); #else printMsg(&logger, "ADC disabled"); #endif }
void myADCinit(void){ palSetGroupMode(GPIOC, PAL_PORT_BIT(1), 0, PAL_MODE_INPUT_ANALOG); adcStart(&ADCD1, NULL); //enable temperature sensor and Vref adcSTM32EnableTSVREFE(); }
void sensorsStart(void) { adcStart(); }
void bus_power_init(void) { chBSemObjectInit(&adc_wait, true); adcStart(&ADCD1, NULL); }
void sonarInit(){ adcStart(&ADCD1,0); palSetPadMode(GPIOC, 4, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOD, GPIOD_LED3, PAL_MODE_OUTPUT_PUSHPULL); }
void gadc_lld_init(void) { adcStart(&ADCD1, 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; }
/* * 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); /* * If the user button is pressed after the reset then the test suite is * executed immediately before activating the various device drivers in * order to not alter the benchmark scores. */ if (palReadPad(GPIOA, GPIOA_BUTTON)) TestThread(&SD1); /* * Initializes the SPI driver 1. */ spiStart(&SPID1, &spicfg); /* * Initializes the ADC driver 1. * The pin PC0 on the port GPIOC is programmed as analog input. */ adcStart(&ADCD1, NULL); palSetGroupMode(GPIOC, PAL_PORT_BIT(0), 0, PAL_MODE_INPUT_ANALOG); /* * Initializes the PWM driver 3, re-routes the TIM3 outputs, programs the * pins as alternate functions. * Note, the AFIO access routes the TIM3 output pins on the PC6...PC9 * where the LEDs are connected. */ pwmStart(&PWMD3, &pwmcfg); AFIO->MAPR |= AFIO_MAPR_TIM3_REMAP_0 | AFIO_MAPR_TIM3_REMAP_1; palSetGroupMode(GPIOC, PAL_PORT_BIT(GPIOC_LED3) | PAL_PORT_BIT(GPIOC_LED4), 0, PAL_MODE_STM32_ALTERNATE_PUSHPULL); /* * Creates the example thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); /* * Normal main() thread activity, in this demo it does nothing except * sleeping in a loop and check the button state, when the button is * pressed the test procedure is launched with output on the serial * driver 1. */ while (TRUE) { if (palReadPad(GPIOA, GPIOA_BUTTON)) TestThread(&SD1); chThdSleepMilliseconds(500); } }
/****************** Thread main loop ***********************************/ msg_t analogue_thread(void *args) { (void)args; chRegSetThreadName("Analogue"); chBSemObjectInit(&bsAnalogueInst, true); chBSemObjectInit(&bsAnalogueFX, true); adcInit(); adcStart(&ADCD1, NULL); adcStart(&ADCD2, NULL); adcStartConversion(&ADCD1, &adc_con_group_1, (adcsample_t*)buffer1, INST_BUF_DEPTH); adcStartConversion(&ADCD2, &adc_con_group_2, (adcsample_t*)fx_samples, FX_BUF_DEPTH); dacInit(); dacStart(&DACD1, &dac_cfg); dacStartConversion(&DACD1, &dac_conv_grp, (dacsample_t*)buffer3, INST_BUF_DEPTH); // Enable DAC output buffer: DACD1.params->dac->CR |= DAC_CR_BOFF1; /* Start the GPT timers. They reload at after reaching 1 such that * TRGO frequency equals timer frequency. */ gptStart(&GPTD3, &gpt_inst_config); GPTD3.tim->CR2 |= STM32_TIM_CR2_MMS(2); gptStartContinuous(&GPTD3, 2); GPTD3.tim->DIER &= ~STM32_TIM_DIER_UIE; gptStart(&GPTD8, &gpt_fx_config); GPTD8.tim->CR2 |= STM32_TIM_CR2_MMS(2); gptStartContinuous(&GPTD8, 2); GPTD8.tim->DIER &= ~STM32_TIM_DIER_UIE; state = 1; // States: // 1 - ADC:buf1, DSP:buf2, DAC:buf3 // 2 - DSP:buf1, DAC:buf2, ADC:buf3 // 3 - DAC:buf1, ADC:buf2, DSP:buf3 /* Wait until the ADC callback boops the semaphore. */ volatile uint16_t *dsp_buf; while(true) { chSysLock(); chBSemWaitS(&bsAnalogueInst); chSysUnlock(); state += 1; switch(state) { case 1: dmaSetOtherMemory(ADCD1.dmastp, buffer1); dsp_buf = buffer2; dmaSetOtherMemory(DACD1.params->dma, buffer3); break; case 2: dmaSetOtherMemory(ADCD1.dmastp, buffer3); dsp_buf = buffer1; dmaSetOtherMemory(DACD1.params->dma, buffer2); break; case 3: dmaSetOtherMemory(ADCD1.dmastp, buffer2); dsp_buf = buffer3; dmaSetOtherMemory(DACD1.params->dma, buffer1); break; default: state = 1; dmaSetOtherMemory(ADCD1.dmastp, buffer1); dsp_buf = buffer2; dmaSetOtherMemory(DACD1.params->dma, buffer3); } dsp_stuff(dsp_buf); } }
/* * 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. * PA9 and PA10 are routed to USART1. */ sdStart(&SD1, NULL); palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(7)); palSetPadMode(GPIOA, 10, PAL_MODE_ALTERNATE(7)); /* * If the user button is pressed after the reset then the test suite is * executed immediately before activating the various device drivers in * order to not alter the benchmark scores. */ if (palReadPad(GPIOA, GPIOA_BUTTON)) TestThread(&SD1); /* * Initializes the SPI driver 2. The SPI2 signals are routed as follow: * PB12 - NSS. * PB13 - SCK. * PB14 - MISO. * PB15 - MOSI. */ spiStart(&SPID2, &spicfg); palSetPad(GPIOB, 12); palSetPadMode(GPIOB, 12, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); /* NSS. */ palSetPadMode(GPIOB, 13, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); /* SCK. */ palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(5)); /* MISO. */ palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); /* MOSI. */ /* * Initializes the ADC driver 1 and enable the thermal sensor. * The pin PC0 on the port GPIOC is programmed as analog input. */ adcStart(&ADCD1, NULL); adcSTM32EnableTSVREFE(); palSetPadMode(GPIOC, 0, PAL_MODE_INPUT_ANALOG); /* * Initializes the PWM driver 4, routes the TIM4 outputs to the board LEDs. */ pwmStart(&PWMD4, &pwmcfg); palSetPadMode(GPIOB, GPIOB_LED4, PAL_MODE_ALTERNATE(2)); palSetPadMode(GPIOB, GPIOB_LED3, PAL_MODE_ALTERNATE(2)); /* * Creates the example thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); /* * Normal main() thread activity, in this demo it does nothing except * sleeping in a loop and check the button state, when the button is * pressed the test procedure is launched with output on the serial * driver 1. */ while (TRUE) { if (palReadPad(GPIOA, GPIOA_BUTTON)) TestThread(&SD1); chThdSleepMilliseconds(500); } }
/* * Application entry point. */ int main(void) { enum led_status lstat = LST_INIT; EventListener el0; alert_status_t proto_st = ALST_INIT; alert_status_t bmp085_st = ALST_INIT; alert_status_t mpu6050_st = ALST_INIT; alert_status_t hmc5883_st = ALST_INIT; /* * 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(); #ifdef BOARD_IMU_AHRF /* Clear DRDY pad */ palClearPad(GPIOA, GPIOA_DRDY); /* Activates serial */ sdStart(&SD1, NULL); sdStart(&SD2, NULL); /* Activate pwm */ pwmStart(&PWMD1, &pwm1cfg); /* Activate i2c */ i2cStart(&I2CD1, &i2c1cfg); /* Activate exti */ extStart(&EXTD1, &extcfg); #endif /* BOARD_IMU_AHRF */ #ifdef BOARD_CAPTAIN_PRO2 /* Activates serial */ sdStart(&SD3, NULL); sdStart(&SD4, NULL); /* Activate pwm */ pwmStart(&PWMD3, &pwm3cfg); pwmStart(&PWMD4, &pwm4cfg); pwmStart(&PWMD5, &pwm5cfg); /* Activate i2c */ i2cStart(&I2CD1, &i2c1cfg); /* Activate exti */ extStart(&EXTD1, &extcfg); /* Activate adc */ adcStart(&ADCD1, NULL); #endif /* BOARD_CAPTAIN_PRO2 */ /* alert subsys */ chEvtInit(&alert_event_source); chEvtRegister(&alert_event_source, &el0, 0); /* init devices */ pt_init(); chThdSleepMilliseconds(10); /* power on delay */ #ifdef HAS_DEV_BMP085 bmp085_init(); chThdSleepMilliseconds(50); /* init delay */ #endif #ifdef HAS_DEV_MS5611 ms5611_init(&ms5611cfg); chThdSleepMilliseconds(50); /* init delay */ #endif #ifdef HAS_DEV_MPU6050 mpu6050_init(&mpu6050cfg); chThdSleepMilliseconds(250); /* give some time for mpu6050 configuration */ #endif #ifdef HAS_DEV_HMC5883 hmc5883_init(&hmc5883cfg); #endif #ifdef HAS_DEV_SERVOPWM servopwm_init(&servopwmcfg); #endif #ifdef HAS_DEV_NTC10K ntc10k_init(); #endif #ifdef HAS_DEV_RPM rpm_init(); #endif #ifdef BOARD_IMU_AHRF /* Set DRDY pad */ palSetPad(GPIOA, GPIOA_DRDY); #endif while (TRUE) { eventmask_t msk = chEvtWaitOneTimeout(ALL_EVENTS, MS2ST(100)); if (msk & EVENT_MASK(0)) { flagsmask_t fl = chEvtGetAndClearFlags(&el0); if (fl & ALERT_FLAG_PROTO) proto_st = pt_get_status(); #ifdef HAS_DEV_MPU6050 if (fl & ALERT_FLAG_MPU6050) mpu6050_st = mpu6050_get_status(); #endif #ifdef HAS_DEV_HMC5883 if (fl & ALERT_FLAG_HMC5883) hmc5883_st = hmc5883_get_status(); #endif #ifdef HAS_DEV_BMP085 if (fl & ALERT_FLAG_BMP085) bmp085_st = bmp085_get_status(); #endif #ifdef HAS_DEV_MS5611 if (fl & ALERT_FLAG_BMP085) bmp085_st = ms5611_get_status(); #endif pt_set_sens_state(mpu6050_st, hmc5883_st, bmp085_st); } if (proto_st == ALST_FAIL || mpu6050_st == ALST_FAIL || hmc5883_st == ALST_FAIL || bmp085_st == ALST_FAIL) lstat = LST_FAIL; else if (proto_st == ALST_INIT || mpu6050_st == ALST_INIT || hmc5883_st == ALST_INIT || bmp085_st == ALST_INIT) lstat = LST_INIT; else if (proto_st == ALST_NORMAL && mpu6050_st == ALST_NORMAL && hmc5883_st == ALST_NORMAL && bmp085_st == ALST_NORMAL) lstat = LST_NORMAL; led_update(lstat); } }
/* * 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 2 using the driver default configuration. * PA2(TX) and PA3(RX) are routed to USART2. */ sdStart(&SD2, NULL); palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7)); palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7)); /* * If the user button is pressed after the reset then the test suite is * executed immediately before activating the various device drivers in * order to not alter the benchmark scores. */ if (palReadPad(GPIOA, GPIOA_BUTTON)) TestThread(&SD2); /* * Initializes the SPI driver 2. The SPI2 signals are routed as follow: * PB12 - NSS. * PB13 - SCK. * PB14 - MISO. * PB15 - MOSI. */ spiStart(&SPID2, &spi2cfg); palSetPad(GPIOB, 12); palSetPadMode(GPIOB, 12, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); /* NSS. */ palSetPadMode(GPIOB, 13, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); /* SCK. */ palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(5)); /* MISO. */ palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); /* MOSI. */ /* * Initializes the ADC driver 1 and enable the thermal sensor. * The pin PC0 on the port GPIOC is programmed as analog input. */ adcStart(&ADCD1, NULL); adcSTM32EnableTSVREFE(); palSetPadMode(GPIOC, 1, PAL_MODE_INPUT_ANALOG); /* * Initializes the PWM driver 4, routes the TIM4 outputs to the board LEDs. */ pwmStart(&PWMD4, &pwmcfg); palSetPadMode(GPIOD, GPIOD_LED4, PAL_MODE_ALTERNATE(2)); /* Green. */ palSetPadMode(GPIOD, GPIOD_LED6, PAL_MODE_ALTERNATE(2)); /* Blue. */ /* * Creates the example thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); /* * Initializes the SPI driver 1 in order to access the MEMS. The signals * are initialized in the board file. * Several LIS302DL registers are then initialized. */ spiStart(&SPID1, &spi1cfg); lis302dlWriteRegister(&SPID1, LIS302DL_CTRL_REG1, 0x43); lis302dlWriteRegister(&SPID1, LIS302DL_CTRL_REG2, 0x00); lis302dlWriteRegister(&SPID1, LIS302DL_CTRL_REG3, 0x00); /* * Normal main() thread activity, in this demo it does nothing except * sleeping in a loop and check the button state, when the button is * pressed the test procedure is launched with output on the serial * driver 2. */ while (TRUE) { int8_t x, y, z; if (palReadPad(GPIOA, GPIOA_BUTTON)) TestThread(&SD2); x = (int8_t)lis302dlReadRegister(&SPID1, LIS302DL_OUTX); y = (int8_t)lis302dlReadRegister(&SPID1, LIS302DL_OUTY); z = (int8_t)lis302dlReadRegister(&SPID1, LIS302DL_OUTZ); chprintf((BaseChannel *)&SD2, "%d, %d, %d\r\n", x, y, z); chThdSleepMilliseconds(500); } }
THD_FUNCTION(Thread1, arg) { (void)arg; /* * Activate the serial driver 0 using the driver default configuration. */ sdStart(&SD1, NULL); /* Activate the ADC driver 1 using its config */ adcStart(&ADCD1, &config); while (chnGetTimeout(&SD1, TIME_INFINITE)) { print(start_msg); chThdSleepMilliseconds(2000); /* Test 1 - 1ch1d, no circular */ run_test(test_1_msg, 1, 1, false); /* Test 2 - 1ch8d, no circular */ run_test(test_2_msg, 1, 8, false); /* Test 3 - 4chd1, no circular */ run_test(test_3_msg, 4, 1, false); /* Test 4 - 4ch8d, no circular */ run_test(test_4_msg, 4, 8, false); /* Test 5 - 1ch1d, circular */ run_test(test_5_msg, 1, 1, true); /* Test 6 - 1ch8d, circular */ run_test(test_6_msg, 1, 8, true); /* Test 7 - 4ch1d, circular */ run_test(test_7_msg, 4, 1, true); /* Test 8 - 4ch8d, circular */ run_test(test_8_msg, 4, 8, true); /* Test 9 - 1ch1d, synchronous */ print(test_9_msg); cb_arg = 0; group.num_channels = 1; group.circular = false; group.end_cb = adc_callback; cb_expect = 1; adcConvert(&ADCD1, &group, buffer, 1); while (ADCD1.state == ADC_ACTIVE) ; sniprintf(out_string, 128, chn_fmt_string, group.channels[0]); print(out_string); sniprintf(out_string, 128, raw_fmt_string, buffer[0]); print(out_string); buffer[0] = adcMSP430XAdjustTemp(&group, buffer[0]); sniprintf(out_string, 128, cooked_fmt_string, buffer[0]); print(out_string); if (cb_arg == cb_expect) { print(success_string); } else { print(fail_string); } } }
void initAdcInputs(bool boardTestMode) { printMsg(&logger, "initAdcInputs()"); configureInputs(); printStatus(); addConsoleActionI("adcDebug", &setAdcDebugReporting); #if EFI_INTERNAL_ADC /* * Initializes the ADC driver. */ adcStart(&ADC_SLOW_DEVICE, NULL); adcStart(&ADC_FAST_DEVICE, NULL); for (int adc = 0; adc < HW_MAX_ADC_INDEX; adc++) { adc_channel_mode_e mode = adcHwChannelEnabled[adc]; /** * in board test mode all currently enabled ADC channels are running in slow mode */ if (mode == ADC_SLOW || (boardTestMode && mode == ADC_FAST)) { slowAdc.addChannel((adc_channel_e) (ADC_CHANNEL_IN0 + adc)); } else if (mode == ADC_FAST) { fastAdc.addChannel((adc_channel_e) (ADC_CHANNEL_IN0 + adc)); } } slowAdc.init(); pwmStart(EFI_INTERNAL_SLOW_ADC_PWM, &pwmcfg_slow); if (boardConfiguration->isFastAdcEnabled) { fastAdc.init(); /* * Initializes the PWM driver. */ pwmStart(EFI_INTERNAL_FAST_ADC_PWM, &pwmcfg_fast); } // ADC_CHANNEL_IN0 // PA0 // ADC_CHANNEL_IN1 // PA1 // ADC_CHANNEL_IN2 // PA2 // ADC_CHANNEL_IN3 // PA3 // ADC_CHANNEL_IN4 // PA4 // ADC_CHANNEL_IN5 // PA5 - this is also TIM2_CH1 // ADC_CHANNEL_IN6 // PA6 // ADC_CHANNEL_IN7 // PA7 // ADC_CHANNEL_IN8 // PB0 // ADC_CHANNEL_IN9 // PB1 // ADC_CHANNEL_IN10 // PC0 // ADC_CHANNEL_IN11 // PC1 // ADC_CHANNEL_IN12 // PC2 // ADC_CHANNEL_IN13 // PC3 // ADC_CHANNEL_IN14 // PC4 // ADC_CHANNEL_IN15 // PC5 //if(slowAdcChannelCount > ADC_MAX_SLOW_CHANNELS_COUNT) // todo: do we need this logic? do we need this check addConsoleActionI("adc", (VoidInt) printAdcValue); addConsoleAction("fadc", printFullAdcReport); #else printMsg(&logger, "ADC disabled"); #endif }
/** * 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); }
tfunc_t adcConversionThread(void) { adcStart(&ADCD1, NULL); adcStartConversion(&ADCD1, &adcSettings, &samples, 8); return (msg_t)0; }
/** * @brief This is the pressure control thread * @param void* to a PID Loops configuration * @retval msg_t status */ msg_t Pressure_Thread(void *This_Config) { /* This thread is passed a pointer to a PID loop configuration */ PID_State Pressure_PID_Controllers[((Pressure_Config_Type*)This_Config)->Number_Setpoints]; memset(Pressure_PID_Controllers,0,((Pressure_Config_Type*)This_Config)->Number_Setpoints*sizeof(PID_State));/* Initialise as zeros */ float* Last_PID_Out=(float*)chHeapAlloc(NULL,sizeof(float)*((Pressure_Config_Type*)This_Config)->Number_Setpoints);/* PID output for interpol */ adcsample_t Pressure_Samples[PRESSURE_SAMPLES],Pressure_Sample;/* Use multiple pressure samples to drive down the noise */ float PID_Out,Pressure;//,step=0.01,sawtooth=0.7; uint32_t Setpoint=0; uint8_t Old_Setpoint=0, Previous_Setpoint; chRegSetThreadName("PID_Pressure"); //palSetGroupMode(GPIOC, PAL_PORT_BIT(5) | PAL_PORT_BIT(4), 0, PAL_MODE_INPUT_ANALOG); palSetPadMode(GPIOE, 9, PAL_MODE_ALTERNATE(1)); /* Only set the pin as AF output here, so as to avoid solenoid getting driven earlier*/ palSetPadMode(GPIOE, 11, PAL_MODE_ALTERNATE(1)); /* Experimental servo output here */ #ifndef USE_SERVO /* * Activates the PWM driver */ pwmStart(&PWM_Driver_Solenoid, &PWM_Config_Solenoid); /* Have to define the timer to use for PWM_Driver in hardware config */ /* * Set the solenoid PWM to off */ pwmEnableChannel(&PWM_Driver_Solenoid, (pwmchannel_t)PWM_CHANNEL_SOLENOID, (pwmcnt_t)0); #else /* * Activates the experimental servo driver */ pwmStart(&PWM_Driver_Servo, &PWM_Config_Servo); /* Have to define the timer to use for PWM_Driver in hardware config */ #endif /* * Activates the ADC2 driver *and the thermal sensor*. */ adcStart(&ADCD2, NULL); //adcSTM32EnableTSVREFE(); /* / Now we run the sensor offset calibration loop */ do { adcConvert(&ADCD2, &adcgrpcfg1, &Pressure_Sample, 1);/* This function blocks until it has one sample*/ } while(Calibrate_Sensor((uint16_t)Pressure_Sample)); systime_t time = chTimeNow(); /* T0 */ systime_t Interpolation_Timeout = time; /* Set to T0 to show there is no current interpolation */ /* Loop for the pressure control thread */ while(TRUE) { /* * Linear conversion. */ adcConvert(&ADCD2, &adcgrpcfg1, Pressure_Samples, PRESSURE_SAMPLES);/* This function blocks until it has the samples via DMA*/ /* / Now we process the data and apply the PID controller - we use a median filter to take out the non guassian noise */ Pressure_Sample = quick_select(Pressure_Samples, PRESSURE_SAMPLES); Pressure = Convert_Pressure((uint16_t)Pressure_Sample);/* Converts to PSI as a float */ /* Retrieve a new setpoint from the setpoint mailbox, only continue if we get it*/ if(chMBFetch(&Pressures_Setpoint, (msg_t*)&Setpoint, TIME_IMMEDIATE) == RDY_OK) { //Pressure=Run_Pressure_Filter(Pressure);/* Square root raised cosine filter for low pass with minimal lag */ Pressure = Pressure<0?0.0:Pressure; /* A negative pressure is impossible with current hardware setup - disregard*/ Setpoint &= 0x000000FF; /* The controller is built around an interpolated array of independant PID controllers with seperate setpoints */ if(Setpoint != Old_Setpoint) { /* The setpoint has changed */ Previous_Setpoint = Old_Setpoint;/* This is for use by the interpolator */ Old_Setpoint = Setpoint; /* Store the setpoint */ /* Store the time at which the interpolation to new setpoint completes*/ Interpolation_Timeout = time + (systime_t)( 4.0 / ((Pressure_Config_Type*)This_Config)->Interpolation_Base ); } if(Interpolation_Timeout > time) { /* If we have an ongoing interpolation - note, operates in tick units */ /* Value goes from 1 to -1 */ float interpol = erff( (float)(Interpolation_Timeout - time) *\ ((Pressure_Config_Type*)This_Config)->Interpolation_Base - 2.0 );/* erf function interpolator */ interpol = ( (-interpol + 1.0) / 2.0);/* Interpolation value goes from 0 to 1 */ PID_Out = ( Last_PID_Out[Previous_Setpoint] * (1.0 - interpol) ) + ( Last_PID_Out[Setpoint] * interpol ); Pressure_PID_Controllers[Setpoint].Last_Input = Pressure;/* Make sure the input to next PID controller is continuous */ } else { PID_Out = Run_PID_Loop( ((Pressure_Config_Type*)This_Config)->PID_Loop_Config, &Pressure_PID_Controllers[Setpoint],\ (((Pressure_Config_Type*)This_Config)->Setpoints)[Setpoint], \ Pressure, (float)PRESSURE_TIME_INTERVAL/1000.0);/* Run PID */ Last_PID_Out[Setpoint] = PID_Out;/* Store for use by the interpolator */ } } else PID_Out=0; /* So we can turn off the solenoid simply by failing to send Setpoints */ PID_Out=PID_Out>1.0?1.0:PID_Out; PID_Out=PID_Out<0.0?0.0:PID_Out; /* Enforce range limits on the PID output */ //sawtooth+=step; /* Test code for debugging mechanics with a sawtooth */ //if(sawtooth>=1 || sawtooth<=0.65) // step=-step; //PID_Out=sawtooth; #ifndef USE_SERVO /* / Now we apply the PID output to the PWM based solenoid controller, and feed data into the mailbox output - Note fractional input */ pwmEnableChannel(&PWM_Driver_Solenoid, (pwmchannel_t)PWM_CHANNEL_SOLENOID, (pwmcnt_t)PWM_FRACTION_TO_WIDTH(&PWM_Driver_Solenoid, 1000\ , (uint32_t)(1000.0*PID_Out))); #else pwmEnableChannel(&PWM_Driver_Servo, (pwmchannel_t)PWM_CHANNEL_SERVO, (pwmcnt_t)PWM_FRACTION_TO_WIDTH(&PWM_Driver_Servo, 10000\ , (uint32_t)(1000.0*(PID_Out+1.0)))); #endif chMBPost(&Pressures_Reported, *((msg_t*)&Pressure), TIME_IMMEDIATE);/* Non blocking write attempt to the Reported Pressure mailbox FIFO */ /* / The Thread is syncronised to system time */ time += MS2ST(PRESSURE_TIME_INTERVAL); /* Next deadline */ chThdSleepUntil(time); /* Gives us a thread with regular timing */ } }
/** * @brief Low level Touchscreen driver initialization. * * @param[in] ts The touchscreen driver struct * * @notapi */ void ts_lld_init(const TouchscreenDriver *ts_init) { ts = ts_init; adcStart(ts->adc_driver, NULL); }
void initAdcInputs() { initLoggingExt(&logger, "ADC", LOGGING_BUFFER, sizeof(LOGGING_BUFFER)); printStatus(); addConsoleActionI(ADC_DEBUG_KEY, &setAdcDebugReporting); #ifdef EFI_INTERNAL_ADC /* * Initializes the ADC driver. */ adcStart(&ADC_SLOW, NULL); adcStart(&ADC_FAST, NULL); adcgrpcfg_slow.sqr2 = 0; adcgrpcfg_slow.sqr3 = 0; int index = 0; #if EFI_USE_ADC_CHANNEL_IN0 initSlowChannel(index++, ADC_CHANNEL_IN0); // PA0 #endif #if EFI_USE_ADC_CHANNEL_IN1 initSlowChannel(index++, ADC_CHANNEL_IN1); // PA1 #endif #if EFI_USE_ADC_CHANNEL_IN2 initSlowChannel(index++, ADC_CHANNEL_IN2); // PA2 #endif #if EFI_USE_ADC_CHANNEL_IN3 initSlowChannel(index++, ADC_CHANNEL_IN3); // PA3 #endif #if EFI_USE_ADC_CHANNEL_IN4 initSlowChannel(index++, ADC_CHANNEL_IN4); // PA4 #endif #if EFI_USE_ADC_CHANNEL_IN5 initSlowChannel(index++, ADC_CHANNEL_IN5); // PA5 - this is also TIM2_CH1 #endif #if EFI_USE_ADC_CHANNEL_IN6 initSlowChannel(index++, ADC_CHANNEL_IN6); // PA6 #endif #if EFI_USE_ADC_CHANNEL_IN7 initSlowChannel(index++, ADC_CHANNEL_IN7); // PA7 #endif #if EFI_USE_ADC_CHANNEL_IN8 initSlowChannel(index++, ADC_CHANNEL_IN8); // PB0 #endif #if EFI_USE_ADC_CHANNEL_IN9 initSlowChannel(index++, ADC_CHANNEL_IN9); // PB1 #endif #if EFI_USE_ADC_CHANNEL_IN10 initSlowChannel(index++, ADC_CHANNEL_IN10); // PC0 #endif #if EFI_USE_ADC_CHANNEL_IN11 initSlowChannel(index++, ADC_CHANNEL_IN11); // PC1 #endif #if EFI_USE_ADC_CHANNEL_IN12 initSlowChannel(index++, ADC_CHANNEL_IN12); // PC2 #endif #if EFI_USE_ADC_CHANNEL_IN13 initSlowChannel(index++, ADC_CHANNEL_IN13); // PC3 #endif #if EFI_USE_ADC_CHANNEL_IN14 initSlowChannel(index++, ADC_CHANNEL_IN14); // PC4 #endif #if EFI_USE_ADC_CHANNEL_IN15 initSlowChannel(index++, ADC_CHANNEL_IN15); // PC5 #endif if (index != EFI_ADC_SLOW_CHANNELS_COUNT) fatal("Invalud internal ADC config"); /* * Initializes the PWM driver. */ pwmStart(EFI_INTERNAL_SLOW_ADC_PWM, &pwmcfg_slow); pwmStart(EFI_INTERNAL_FAST_ADC_PWM, &pwmcfg_fast); addConsoleActionI("adc", printAdcValue); addConsoleAction("fadc", printFullAdcReport); #else printSimpleMsg(&logger, "ADC disabled", 0); #endif }
void adc_init(void) { adc_configpads(); adcStart(&ADCD1, NULL); }
msg_t motor_calibration_node(void * arg) { //Configure current node calibration_pub_node_conf* conf; if (arg != NULL) conf = (calibration_pub_node_conf *) arg; else conf = &defaultPubConf; Node node(conf->name); Publisher<FloatMsg> current_pub; FloatMsg * msgp; chRegSetThreadName(conf->name); node.advertise(current_pub, conf->topic); // Start the ADC driver and conversion adcStart(&ADC_DRIVER, NULL); adcStartConversion(&ADC_DRIVER, &adcgrpcfg, adc_samples, ADC_BUF_DEPTH); // Init motor driver palSetPad(DRIVER_GPIO, DRIVER_RESET); chThdSleepMilliseconds(500); pwmStart(&PWM_DRIVER, &pwmcfg); // wait some time chThdSleepMilliseconds(500); // start pwm float voltage = 24.0; const float pwm_res = 4096.0f / 24.0f; pwm = static_cast<int>(voltage * pwm_res); if (pwm > 0) { pwm_lld_enable_channel(&PWM_DRIVER, 1, pwm); pwm_lld_enable_channel(&PWM_DRIVER, 0, 0); pwm_lld_enable_channel(&PWM_DRIVER, 2, pwm/2); } else { pwm_lld_enable_channel(&PWM_DRIVER, 1, 0); pwm_lld_enable_channel(&PWM_DRIVER, 0, -pwm); pwm_lld_enable_channel(&PWM_DRIVER, 2, -pwm/2); } // Start publishing current measures for (;;) { // Wait for interrupt chSysLock() ; tp_motor = chThdSelf(); chSchGoSleepS(THD_STATE_SUSPENDED); chSysUnlock(); // publish current if (current_pub.alloc(msgp)) { msgp->value = meanLevel; current_pub.publish(*msgp); } } return CH_SUCCESS; }