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"); }
static void up_lcddispcontrol(bool on) { lcddbg("set: %s\n", on ? "on" : "off"); if (on) { stm32_gpiowrite(GPIO_MEMLCD_DISP, 1); STM32_TIM_ENABLEINT(tim, 0); } else { stm32_gpiowrite(GPIO_MEMLCD_DISP, 0); STM32_TIM_DISABLEINT(tim, 0); } }
/** * @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; }