int sif_anout_init(void) { vsn_sif.tim3 = stm32_tim_init(3); vsn_sif.tim8 = stm32_tim_init(8); if (!vsn_sif.tim3 || !vsn_sif.tim8) return ERROR; // Use the TIM3 as PWM modulated analogue output STM32_TIM_SETPERIOD(vsn_sif.tim3, 5); STM32_TIM_SETCOMPARE(vsn_sif.tim3, GPIO_OUT_PWM_TIM3_CH, 3); STM32_TIM_SETCLOCK(vsn_sif.tim3, 36e6); STM32_TIM_SETMODE(vsn_sif.tim3, STM32_TIM_MODE_UP); STM32_TIM_SETCHANNEL(vsn_sif.tim3, GPIO_OUT_PWM_TIM3_CH, STM32_TIM_CH_OUTPWM | STM32_TIM_CH_POLARITY_NEG); // Use the TIM8 to drive the upper power mosfet STM32_TIM_SETISR(vsn_sif.tim8, sif_anout_isr, 0); STM32_TIM_ENABLEINT(vsn_sif.tim8, 0); STM32_TIM_SETPERIOD(vsn_sif.tim8, 4096); STM32_TIM_SETCOMPARE(vsn_sif.tim8, GPIO_OUT_PWRPWM_TIM8_CH, 5000); STM32_TIM_SETCLOCK(vsn_sif.tim8, 36e6); STM32_TIM_SETMODE(vsn_sif.tim8, STM32_TIM_MODE_UP); //STM32_TIM_SETCHANNEL(vsn_sif.tim8, GPIO_OUT_PWRPWM_TIM8_CH, STM32_TIM_CH_OUTPWM | STM32_TIM_CH_POLARITY_NEG); vsn_sif.i2c1 = up_i2cinitialize(1); vsn_sif.i2c2 = up_i2cinitialize(2); vsn_sif.spi2 = up_spiinitialize(2); return OK; }
__EXPORT void up_wspeedinit() { // Initialize interrupts (GPIO 1-4) stm32_gpiosetevent(GPIO_GPIO0_INPUT, true, true, false, sensor_int1); stm32_gpiosetevent(GPIO_GPIO1_INPUT, true, true, false, sensor_int2); stm32_gpiosetevent(GPIO_GPIO2_INPUT, true, true, false, sensor_int3); stm32_gpiosetevent(GPIO_GPIO3_INPUT, true, true, false, sensor_int4); // Initialize timer 5 timer = stm32_tim_init(5); // Configure timer 5 for a frequency of 1000000 Hz (overflow after 4295 s) STM32_TIM_SETCLOCK(timer, 1000000); STM32_TIM_SETMODE(timer, STM32_TIM_MODE_UP | STM32_TIM_MODE_CK_INT); // Initialize timer for filter interrupt at a interval of 5ms timer2 = stm32_tim_init(4); STM32_TIM_SETISR(timer2, filter_interrupt, 0); STM32_TIM_ENABLEINT(timer2, 0); STM32_TIM_SETPERIOD(timer2, 4096); STM32_TIM_SETCOMPARE(timer2, 1, 5000); STM32_TIM_SETCLOCK(timer2, 1000000); // 1000000 Hz -> period: 1us STM32_TIM_SETMODE(timer2, STM32_TIM_MODE_UP | STM32_TIM_MODE_CK_INT); printf("[WSPEED] initialized\n"); }
/** * @brief Disable timers */ static void timesync_disable_timers(void) { STM32_TIM_DISABLEINT(timesync_strobe_timer, 0); STM32_TIM_SETMODE(timesync_strobe_timer, STM32_TIM_MODE_DISABLED); STM32_TIM_SETCLOCK(timesync_strobe_timer, 0); STM32_TIM_DISABLEINT(timesync_rollover_master_timer, 0); STM32_TIM_SETMODE(timesync_rollover_master_timer, STM32_TIM_MODE_DISABLED); STM32_TIM_SETCLOCK(timesync_rollover_master_timer, 0); STM32_TIM_DISABLEINT(timesync_rollover_slave_timer, 0); STM32_TIM_SETMODE(timesync_rollover_slave_timer, STM32_TIM_MODE_DISABLED); STM32_TIM_SETCLOCK(timesync_rollover_slave_timer, 0); }
FAR int up_lcdinitialize(void) { lcddbg("Initializing lcd\n"); lcddbg("init spi1\n"); spi = up_spiinitialize(1); DEBUGASSERT(spi); lcddbg("configure related io\n"); stm32_configgpio(GPIO_MEMLCD_EXTCOMIN); stm32_configgpio(GPIO_MEMLCD_DISP); lcddbg("configure EXTCOMIN timer\n"); if (tim == NULL) { tim = stm32_tim_init(2); DEBUGASSERT(tim); STM32_TIM_SETPERIOD(tim, TIMER_FREQ / EXTCOMIN_FREQ); STM32_TIM_SETCLOCK(tim, TIMER_FREQ); STM32_TIM_SETMODE(tim, STM32_TIM_MODE_UP); } lcddbg("init lcd\n"); l_lcddev = memlcd_initialize(spi, &memlcd_priv, 0); DEBUGASSERT(l_lcddev); return OK; }
/** * @brief Responsible for sending out the TIME_SYNC pulse * * Logs the time of the strobe and strobes. If the requested iteration * count is reached we transition to the TIMESYNC_STATE_ACTIVE state or * the TIMESYNC_STATE_DEBUT_INIT state depending on CONFIG_ARCH_TIMESYNC_DEBUG. * */ static int timesync_strobe_handler(int irq, void *context, void *priv) { irqstate_t flags; uint64_t strobe_time; flags = irqsave(); STM32_TIM_ACKINT(timesync_strobe_timer, irq); timesync_strobe(timesync_pin_strobe_mask, &strobe_time); timesync_log_frame_time(strobe_time); if (timesync_strobe_index == timesync_strobe_count) { STM32_TIM_DISABLEINT(timesync_strobe_timer, 0); STM32_TIM_SETMODE(timesync_strobe_timer, STM32_TIM_MODE_DISABLED); STM32_TIM_SETCLOCK(timesync_strobe_timer, 0); #ifdef CONFIG_ARCH_TIMESYNC_DEBUG timesync_set_state(TIMESYNC_STATE_DEBUG_INIT); sem_post(&dbg_thread_sem); #else timesync_set_state(TIMESYNC_STATE_ACTIVE); #endif } irqrestore(flags); return 0; }
void up_leds(int r, int g ,int b, int freqs) { long fosc = 72000000; long prescale = 2048; long p1s = fosc/prescale; long p0p5s = p1s/2; long p; static struct stm32_tim_dev_s *tim1 = 0; if (tim1 == 0) { tim1 = stm32_tim_init(1); STM32_TIM_SETMODE(tim1, STM32_TIM_MODE_UP); STM32_TIM_SETCLOCK(tim1, p1s-8); STM32_TIM_SETPERIOD(tim1, p1s); STM32_TIM_SETCOMPARE(tim1, 1, 0); STM32_TIM_SETCOMPARE(tim1, 2, 0); STM32_TIM_SETCOMPARE(tim1, 3, 0); STM32_TIM_SETCHANNEL(tim1, 1, STM32_TIM_CH_OUTPWM | STM32_TIM_CH_POLARITY_NEG); STM32_TIM_SETCHANNEL(tim1, 2, STM32_TIM_CH_OUTPWM | STM32_TIM_CH_POLARITY_NEG); STM32_TIM_SETCHANNEL(tim1, 3, STM32_TIM_CH_OUTPWM | STM32_TIM_CH_POLARITY_NEG); } p = freqs == 0 ? p1s : p1s / freqs; STM32_TIM_SETPERIOD(tim1, p); p = freqs == 0 ? p1s + 1 : p0p5s / freqs; STM32_TIM_SETCOMPARE(tim1, 2, (r * p) / 255); STM32_TIM_SETCOMPARE(tim1, 1, (b * p) / 255); STM32_TIM_SETCOMPARE(tim1, 3, (g * p) / 255); }
/** * @brief Enable TimeSync at the given reference frame_time * * Setup TIM8 as master clock cascading into TIM4 see DocID018909 Rev 10 page 612 * https://www.rapitasystems.com/blog/chaining-two-16-bit-timers-together-stm32f4 * 622/1728 DocID018909 Rev 10 - details which timers can be cascaded * TIM8 has been selected since we can clock that @ 96MHz and divide down by 5 to * get to 19.2MHz with just a little work @ the PLL configuration. */ int timesync_enable(uint8_t strobe_count, uint64_t frame_time, uint32_t strobe_delay, uint32_t refclk) { if (!strobe_count || strobe_count > GB_TIMESYNC_MAX_STROBES) { return -EINVAL; } if (!strobe_delay) { return -EINVAL; } if (!refclk || STM32_TIM18_FREQUENCY % refclk) { lldbg("Error Time-Sync clock %dHz doesn't divide APB clock %dHz evenly\n", refclk, STM32_TIM18_FREQUENCY); return -ENODEV; } timesync_refclk = refclk; timesync_strobe_count = strobe_count; timesync_strobe_index = 0; timesync_strobe_delay = strobe_delay; timesync_set_state(TIMESYNC_STATE_SYNCING); timesync_frame_time = frame_time; /* Disable timers */ timesync_disable_timers(); /******************************************************************* * Enable TIM8 against the hardware input clock output on TIM8_TRGO *******************************************************************/ /* Configure MMS=010 in TIM8_CR2 output TIM8_TRGO on rollover (master mode) */ STM32_TIM_SETMASTER_MODE(timesync_rollover_master_timer, STM32_TIM_MASTER_MODE_UPDATE); /* Configures TIM8_ARR - auto reload register */ STM32_TIM_SETPERIOD(timesync_rollover_master_timer, TIMESYNC_ROLLOVER_TIMER_PERIOD); /* Configures TIM8_PSC - prescaler value to get our desired master clock */ STM32_TIM_SETCLOCK(timesync_rollover_master_timer, timesync_refclk); /********************************************************************* * Enable TIM4 with clock input source ITR2 (TIM8_TRGO), no pre-scaler * interrupt on over-flow *********************************************************************/ /* Configure ITR0 as internal trigger TIM4_SMCR:TS=011(TIM8) external clock mode TIM4_SMCR:SMS=111 (slave mode) */ STM32_TIM_SETSLAVE_MODE(timesync_rollover_slave_timer, STM32_TIM_SLAVE_EXTERNAL_MODE, STM32_TIM_SLAVE_INTERNAL_TRIGGER3); /* Configures TIM4_ARR - auto reload register */ STM32_TIM_SETPERIOD(timesync_rollover_slave_timer, TIMESYNC_ROLLOVER_TIMER_PERIOD); /* Configures TIM4_PSC - set to STM32_TIM18_FREQUENCY to get a prescaler value of 0 */ STM32_TIM_SETCLOCK(timesync_rollover_slave_timer, STM32_TIM18_FREQUENCY); /**************************************************************************** * Enable TIM6 as a simple up-counter at the strobe_delay specified by the AP * strobe_delay is expressed in microseconds. ****************************************************************************/ /* Delay is expressed in microseconds so apply directly to TIM6_ARR */ STM32_TIM_SETPERIOD(timesync_strobe_timer, strobe_delay); /* Clock is simply the fundamental input Hz (clocks per second) / 1000 */ STM32_TIM_SETCLOCK(timesync_strobe_timer, 1000000UL); /*************** * Enable timers ***************/ /* Configures TIM8_CR1 - mode up-counter */ STM32_TIM_SETMODE(timesync_rollover_master_timer, STM32_TIM_MODE_UP); /* Configures TIM4_CR1 - mode up-counter */ STM32_TIM_SETMODE(timesync_rollover_slave_timer, STM32_TIM_MODE_UP); /* Configures TIM6_CR1 - mode up-counter */ STM32_TIM_SETMODE(timesync_strobe_timer, STM32_TIM_MODE_UP); /* Enable roll-over timer interrupt */ STM32_TIM_ACKINT(timesync_rollover_slave_timer, timesync_rollover_timer_irq); STM32_TIM_ENABLEINT(timesync_rollover_slave_timer, 0); /* Enable strobe timer interrupt */ STM32_TIM_ACKINT(timesync_strobe_timer, timesync_strobe_timer_irq); STM32_TIM_ENABLEINT(timesync_strobe_timer, 0); dbg_verbose("ref-clk-freq=%dHz timer-freq=%dHz period=%d\n", STM32_TIM18_FREQUENCY, timesync_refclk, TIMESYNC_ROLLOVER_TIMER_PERIOD); dbg_verbose("strobe-clk=%dHz strobe-pin-mask=0x%08lx period=%d\n", STM32_TIM18_FREQUENCY / 1000UL, timesync_pin_strobe_mask, strobe_delay); return 0; }