static void prvSetupTimerInterrupt( void ) { unsigned char a; unsigned short b; unsigned long n = configCPU_PERIPH_HZ / configTICK_RATE_HZ; TIM_InitTypeDef timer; SCU_APBPeriphClockConfig( __TIM23, ENABLE ); TIM_DeInit(TIM2); TIM_StructInit(&timer); prvFindFactors( n, &a, &b ); timer.TIM_Mode = TIM_OCM_CHANNEL_1; timer.TIM_OC1_Modes = TIM_TIMING; timer.TIM_Clock_Source = TIM_CLK_APB; timer.TIM_Clock_Edge = TIM_CLK_EDGE_RISING; timer.TIM_Prescaler = a-1; timer.TIM_Pulse_Level_1 = TIM_HIGH; timer.TIM_Pulse_Length_1 = s_nPulseLength = b-1; TIM_Init (TIM2, &timer); TIM_ITConfig(TIM2, TIM_IT_OC1, ENABLE); /* Configure the VIC for the WDG interrupt. */ VIC_Config( TIM2_ITLine, VIC_IRQ, 10 ); VIC_ITCmd( TIM2_ITLine, ENABLE ); /* Install the default handlers for both VIC's. */ VIC0->DVAR = ( unsigned long ) prvDefaultHandler; VIC1->DVAR = ( unsigned long ) prvDefaultHandler; TIM_CounterCmd(TIM2, TIM_CLEAR); TIM_CounterCmd(TIM2, TIM_START); }
int platform_init() { unsigned i; TIM_InitTypeDef tim; TIM_TypeDef* base; // System configuration platform_config_scu(); // PIO setup for( i = 0; i < 10; i ++ ) GPIO_DeInit( ( GPIO_TypeDef* )port_data[ i ] ); // UART setup (only STR9_UART is used in this example) platform_uart_setup( 0, 115200, 8, PLATFORM_UART_PARITY_NONE, PLATFORM_UART_STOPBITS_1 ); // Initialize timers for( i = 0; i < 4; i ++ ) { base = ( TIM_TypeDef* )timer_data[ i ]; TIM_DeInit( base ); TIM_StructInit( &tim ); tim.TIM_Clock_Source = TIM_CLK_APB; tim.TIM_Prescaler = 255; TIM_Init( base, &tim ); TIM_CounterCmd( base, TIM_START ); } // Set the send/recv functions std_set_send_func( uart_send ); std_set_get_func( uart_recv ); return PLATFORM_OK; }
int platform_init() { unsigned i; TIM_InitTypeDef tim; TIM_TypeDef* base; // System configuration platform_config_scu(); // PIO setup for( i = 0; i < 10; i ++ ) GPIO_DeInit( ( GPIO_TypeDef* )port_data[ i ] ); // Initialize VIC VIC_DeInit(); // UART setup (only STR9_UART is used in this example) platform_uart_setup( CON_UART_ID, CON_UART_SPEED, 8, PLATFORM_UART_PARITY_NONE, PLATFORM_UART_STOPBITS_1 ); // Initialize timers for( i = 0; i < 4; i ++ ) { base = ( TIM_TypeDef* )timer_data[ i ]; TIM_DeInit( base ); TIM_StructInit( &tim ); tim.TIM_Clock_Source = TIM_CLK_APB; tim.TIM_Prescaler = 255; TIM_Init( base, &tim ); TIM_CounterCmd( base, TIM_START ); } cmn_platform_init(); return PLATFORM_OK; }
/*..........................................................................*/ QState AlarmMgr_silent(AlarmMgr *me, QEvt const *e) { switch (e->sig) { case Q_ENTRY_SIG: { TIM_CounterCmd(TIM3, TIM_STOP); /* silence */ me->curr_alarm = 0; return Q_HANDLED(); } } return Q_SUPER(&AlarmMgr_on); }
int platform_init() { unsigned i; TIM_InitTypeDef tim; TIM_TypeDef* base; // System configuration platform_config_scu(); // PIO setup for( i = 0; i < 10; i ++ ) GPIO_DeInit( ( GPIO_TypeDef* )port_data[ i ] ); // UART setup platform_gpio_uart_setup(); // Initialize timers for( i = 0; i < NUM_PHYS_TIMER; i ++ ) { base = ( TIM_TypeDef* )str9_timer_data[ i ]; TIM_DeInit( base ); TIM_StructInit( &tim ); tim.TIM_Clock_Source = TIM_CLK_APB; tim.TIM_Prescaler = 255; TIM_Init( base, &tim ); TIM_CounterCmd( base, TIM_START ); } cmn_platform_init(); #ifdef BUILD_ADC // Setup ADCs platform_setup_adcs(); #endif #ifdef BUILD_CAN // Setup CANs cans_init(); #endif // Initialize system timer cmn_systimer_set_base_freq( 1000000 ); cmn_systimer_set_interrupt_freq( VTMR_FREQ_HZ ); platform_s_timer_set_match_int( VTMR_TIMER_ID, 1000000 / VTMR_FREQ_HZ, PLATFORM_TIMER_INT_CYCLIC ); return PLATFORM_OK; }
//---------------------------------------------------------------------------------------------------- // 1ms Timer //---------------------------------------------------------------------------------------------------- void TIMER1_Init(void) { TIM_InitTypeDef TIM_InitStructure; UART1_PutString("\r\n Timer1 init..."); #define TIM1_FREQ 200000 // 200kHz // TimerOCR set in IntHandler SCU_APBPeriphClockConfig(__TIM01, ENABLE); TIM_DeInit(TIM1); TIM_StructInit(&TIM_InitStructure); TIM_InitStructure.TIM_Mode = TIM_OCM_CHANNEL_1; TIM_InitStructure.TIM_OC1_Modes = TIM_TIMING; TIM_InitStructure.TIM_Clock_Source = TIM_CLK_APB; TIM_InitStructure.TIM_Prescaler = (SCU_GetPCLKFreqValue() * 1000) / TIM1_FREQ; // is only valid up to 48 MHz ! TIM_Init (TIM1, &TIM_InitStructure); TIM_ITConfig(TIM1, TIM_IT_OC1, ENABLE); TIM_CounterCmd(TIM1, TIM_START); VIC_Config(TIM1_ITLine, VIC_IRQ, PRIORITY_TIMER1); VIC_ITCmd(TIM1_ITLine, ENABLE); SystemTime.Year = 0; SystemTime.Month = 0; SystemTime.Day = 0; SystemTime.Hour = 0; SystemTime.Min = 0; SystemTime.Sec = 0; SystemTime.mSec = 0; SystemTime.Valid = 0; CountMilliseconds = 0; UART1_PutString("ok"); }
/*..........................................................................*/ QState AlarmMgr_playing(AlarmMgr *me, QEvt const *e) { switch (e->sig) { case Q_ENTRY_SIG: { QTimeEvt_postIn(&me->timer, (QActive *)me, l_alarms[me->curr_alarm].note[me->curr_note].dur); TIM_PrescalerConfig(TIM3, l_alarms[me->curr_alarm].note[me->curr_note].freq); TIM_CounterCmd(TIM3, TIM_START); /* start noise */ return Q_HANDLED(); } case Q_EXIT_SIG: { QTimeEvt_disarm(&me->timer); return Q_HANDLED(); } case TIMEOUT_SIG: { ++me->curr_note; if (me->curr_note == l_alarms[me->curr_alarm].length) { me->curr_note = 0; /* start over again */ } return Q_TRAN(&AlarmMgr_playing); /* transition to self */ } } return Q_SUPER(&AlarmMgr_on); }
int main(void) { u16 Conversion_Value = 0; #ifdef DEBUG debug(); #endif SCU_MCLKSourceConfig(SCU_MCLK_OSC); /*Use OSC as the default clock source*/ SCU_PCLKDivisorConfig(SCU_PCLK_Div1); /* ARM Peripheral bus clokdivisor = 1*/ SCU_APBPeriphClockConfig(__ADC, ENABLE); /* Enable the clock for the ADC */ ADC_DeInit(); /* ADC Deinitialization */ SCU_APBPeriphClockConfig(__TIM01, ENABLE); /* Enable the clock for TIM0 and TIM1 */ TIM_DeInit(TIM0); /* TIM0 Deinitialization */ SCU_APBPeriphClockConfig(__GPIO4, ENABLE); /* Enable the clock for the GPIO4 */ GPIO_DeInit(GPIO4); /* GPIO4 Deinitialization */ SCU_APBPeriphClockConfig(__GPIO3, ENABLE); /* Enable the clock for the GPIO3 */ GPIO_DeInit(GPIO3); /* GPIO3 Deinitialization */ /* Configure the GPIO4 pin 5 as analog input */ GPIO_ANAPinConfig(GPIO_ANAChannel5, ENABLE); /* GPIO6 configuration (PWM on P3.0, pin 55) */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt3; GPIO_Init(GPIO3,&GPIO_InitStructure); /* TIM0 Structure Initialization */ TIM_StructInit(&TIM_InitStructure); /* TIM0 Configuration in PWM Mode */ TIM_InitStructure.TIM_Mode = TIM_PWM; TIM_InitStructure.TIM_Clock_Source = TIM_CLK_APB; TIM_InitStructure.TIM_Prescaler = 0x0; TIM_InitStructure.TIM_Pulse_Level_1 = TIM_HIGH; TIM_InitStructure.TIM_Period_Level = TIM_LOW; TIM_InitStructure.TIM_Pulse_Length_1 = 0x200; TIM_InitStructure.TIM_Full_Period = 0x404; TIM_Init (TIM0, &TIM_InitStructure); /* Start the counter of TIM0 */ TIM_CounterCmd(TIM0, TIM_START); /* ADC Structure Initialization */ ADC_StructInit(&ADC_InitStructure); /* Configure the ADC in continuous mode conversion */ ADC_InitStructure.ADC_Channel_5_Mode = ADC_NoThreshold_Conversion; ADC_InitStructure.ADC_Select_Channel = ADC_Channel_5; ADC_InitStructure.ADC_Scan_Mode = DISABLE; ADC_InitStructure.ADC_Conversion_Mode = ADC_Continuous_Mode; /* Enable the ADC */ ADC_Cmd(ENABLE); /* Prescaler config */ ADC_PrescalerConfig(0x0); /* Configure the ADC */ ADC_Init(&ADC_InitStructure); /* Start the conversion */ ADC_ConversionCmd(ADC_Conversion_Start); while(1) { /* Wait until conversion completion */ while(ADC_GetFlagStatus(ADC_FLAG_ECV) == RESET); /* Get the conversion value */ Conversion_Value = ADC_GetConversionValue(ADC_Channel_5); /* Clear the end of conversion flag */ ADC_ClearFlag(ADC_FLAG_ECV); /* Set the new pulse of the TIM0 */ TIM_SetPulse(TIM0, TIM_PWM_OC1_Channel, Conversion_Value); } }