1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Stm32f4 systick

Discussion in 'ARM' started by AtomSoft, Mar 8, 2012.

  1. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Why am i always going crazy here?

    Here is a snippet from the main.c file for a example of SysTick from st. How do they get a 1ms time base from simply dividing the clock by 1000 ?

    The time base should be 168Khz or ~6uS

    Shouldnt they divide it by 168000 for the time base to be 1ms ? (1khz)

    Code (text):

    //.....
    /* Setup SysTick Timer for 1 msec interrupts.
         ------------------------------------------
        1. The SysTick_Config() function is a CMSIS function which configure:
           - The SysTick Reload register with value passed as function parameter.
           - Configure the SysTick IRQ priority to the lowest value (0x0F).
           - Reset the SysTick Counter register.
           - Configure the SysTick Counter clock source to be Core Clock Source (HCLK).
           - Enable the SysTick Interrupt.
           - Start the SysTick Counter.
       
        2. You can change the SysTick Clock source to be HCLK_Div8 by calling the
           SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8) just after the
           SysTick_Config() function call. The SysTick_CLKSourceConfig() is defined
           inside the misc.c file.

        3. You can change the SysTick IRQ priority by calling the
           NVIC_SetPriority(SysTick_IRQn,...) just after the SysTick_Config() function
           call. The NVIC_SetPriority() is defined inside the core_cm3.h file.

        4. To adjust the SysTick time base, use the following formula:
                               
             Reload Value = SysTick Counter Clock (Hz) x  Desired Time base (s)
       
           - Reload Value is the parameter to be passed for SysTick_Config() function
           - Reload Value should not exceed 0xFFFFFF
       */

      if (SysTick_Config(SystemCoreClock / 1000))
      {
        /* Capture error */
        while (1);
      }
    //.....
     
    From the release notes:
    from the included system_stm32f4xx.c file

    Forgive me if i missed something obvious :)
     
    Last edited: Mar 8, 2012
  2. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    There is not much info on the SysTick but:

     
  3. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Ah forget this lol ill use TIM2
     
    Last edited: Mar 8, 2012
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. DirtyLude

    DirtyLude Well-Known Member

    Joined:
    Aug 5, 2003
    Messages:
    1,904
    Likes:
    56
    Location:
    Toronto, Canada

    There are 168,000,000 clock cycle in a second. If you want 1000th of a second (1ms) you would divide that by 1000. Seems pretty straight forward. So it takes 168,000 clock cycles to reach 1000th of a second.
     
  6. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    hah yeah i figure that... i dont get enough sleep sometimes :)i was complaining about the 1000 but didnt notice its not sending 1000 to the function its sending 168mhz/1000 which is the 168000 :)
     
  7. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    I think what made me hate this is that a ton of the stuff isnt documented good like:

    I wanted to know how the Compiler knew where the interrupt was for the SysTick and All other interrupts and it took me 3 hours to find the answer in the Startup file :(

    Code (text):
    ; Vector Table Mapped to Address 0 at Reset
                    AREA    RESET, DATA, READONLY
                    EXPORT  __Vectors
                    EXPORT  __Vectors_End
                    EXPORT  __Vectors_Size

    __Vectors       DCD     __initial_sp               ; Top of Stack
                    DCD     Reset_Handler              ; Reset Handler
                    DCD     NMI_Handler                ; NMI Handler
                    DCD     HardFault_Handler          ; Hard Fault Handler
                    DCD     MemManage_Handler          ; MPU Fault Handler
                    DCD     BusFault_Handler           ; Bus Fault Handler
                    DCD     UsageFault_Handler         ; Usage Fault Handler
                    DCD     0                          ; Reserved
                    DCD     0                          ; Reserved
                    DCD     0                          ; Reserved
                    DCD     0                          ; Reserved
                    DCD     SVC_Handler                ; SVCall Handler
                    DCD     DebugMon_Handler           ; Debug Monitor Handler
                    DCD     0                          ; Reserved
                    DCD     PendSV_Handler             ; PendSV Handler
                    DCD     SysTick_Handler            ; SysTick Handler

                    ; External Interrupts
                    DCD     WWDG_IRQHandler                   ; Window WatchDog                                        
                    DCD     PVD_IRQHandler                    ; PVD through EXTI Line detection                        
                    DCD     TAMP_STAMP_IRQHandler             ; Tamper and TimeStamps through the EXTI line            
                    DCD     RTC_WKUP_IRQHandler               ; RTC Wakeup through the EXTI line                      
                    DCD     FLASH_IRQHandler                  ; FLASH                                          
                    DCD     RCC_IRQHandler                    ; RCC                                            
                    DCD     EXTI0_IRQHandler                  ; EXTI Line0                                            
                    DCD     EXTI1_IRQHandler                  ; EXTI Line1                                            
                    DCD     EXTI2_IRQHandler                  ; EXTI Line2                                            
                    DCD     EXTI3_IRQHandler                  ; EXTI Line3                                            
                    DCD     EXTI4_IRQHandler                  ; EXTI Line4                                            
                    DCD     DMA1_Stream0_IRQHandler           ; DMA1 Stream 0                                  
                    DCD     DMA1_Stream1_IRQHandler           ; DMA1 Stream 1                                  
                    DCD     DMA1_Stream2_IRQHandler           ; DMA1 Stream 2                                  
                    DCD     DMA1_Stream3_IRQHandler           ; DMA1 Stream 3                                  
                    DCD     DMA1_Stream4_IRQHandler           ; DMA1 Stream 4                                  
                    DCD     DMA1_Stream5_IRQHandler           ; DMA1 Stream 5                                  
                    DCD     DMA1_Stream6_IRQHandler           ; DMA1 Stream 6                                  
                    DCD     ADC_IRQHandler                    ; ADC1, ADC2 and ADC3s                            
                    DCD     CAN1_TX_IRQHandler                ; CAN1 TX                                                
                    DCD     CAN1_RX0_IRQHandler               ; CAN1 RX0                                              
                    DCD     CAN1_RX1_IRQHandler               ; CAN1 RX1                                              
                    DCD     CAN1_SCE_IRQHandler               ; CAN1 SCE                                              
                    DCD     EXTI9_5_IRQHandler                ; External Line[9:5]s                                    
                    DCD     TIM1_BRK_TIM9_IRQHandler          ; TIM1 Break and TIM9                  
                    DCD     TIM1_UP_TIM10_IRQHandler          ; TIM1 Update and TIM10                
                    DCD     TIM1_TRG_COM_TIM11_IRQHandler     ; TIM1 Trigger and Commutation and TIM11
                    DCD     TIM1_CC_IRQHandler                ; TIM1 Capture Compare                                  
                    DCD     TIM2_IRQHandler                   ; TIM2                                            
                    DCD     TIM3_IRQHandler                   ; TIM3                                            
                    DCD     TIM4_IRQHandler                   ; TIM4                                            
                    DCD     I2C1_EV_IRQHandler                ; I2C1 Event                                            
                    DCD     I2C1_ER_IRQHandler                ; I2C1 Error                                            
                    DCD     I2C2_EV_IRQHandler                ; I2C2 Event                                            
                    DCD     I2C2_ER_IRQHandler                ; I2C2 Error                                              
                    DCD     SPI1_IRQHandler                   ; SPI1                                            
                    DCD     SPI2_IRQHandler                   ; SPI2                                            
                    DCD     USART1_IRQHandler                 ; USART1                                          
                    DCD     USART2_IRQHandler                 ; USART2                                          
                    DCD     USART3_IRQHandler                 ; USART3                                          
                    DCD     EXTI15_10_IRQHandler              ; External Line[15:10]s                                  
                    DCD     RTC_Alarm_IRQHandler              ; RTC Alarm (A and B) through EXTI Line                  
                    DCD     OTG_FS_WKUP_IRQHandler            ; USB OTG FS Wakeup through EXTI line                        
                    DCD     TIM8_BRK_TIM12_IRQHandler         ; TIM8 Break and TIM12                  
                    DCD     TIM8_UP_TIM13_IRQHandler          ; TIM8 Update and TIM13                
                    DCD     TIM8_TRG_COM_TIM14_IRQHandler     ; TIM8 Trigger and Commutation and TIM14
                    DCD     TIM8_CC_IRQHandler                ; TIM8 Capture Compare                                  
                    DCD     DMA1_Stream7_IRQHandler           ; DMA1 Stream7                                          
                    DCD     FSMC_IRQHandler                   ; FSMC                                            
                    DCD     SDIO_IRQHandler                   ; SDIO                                            
                    DCD     TIM5_IRQHandler                   ; TIM5                                            
                    DCD     SPI3_IRQHandler                   ; SPI3                                            
                    DCD     UART4_IRQHandler                  ; UART4                                          
                    DCD     UART5_IRQHandler                  ; UART5                                          
                    DCD     TIM6_DAC_IRQHandler               ; TIM6 and DAC1&2 underrun errors                  
                    DCD     TIM7_IRQHandler                   ; TIM7                  
                    DCD     DMA2_Stream0_IRQHandler           ; DMA2 Stream 0                                  
                    DCD     DMA2_Stream1_IRQHandler           ; DMA2 Stream 1                                  
                    DCD     DMA2_Stream2_IRQHandler           ; DMA2 Stream 2                                  
                    DCD     DMA2_Stream3_IRQHandler           ; DMA2 Stream 3                                  
                    DCD     DMA2_Stream4_IRQHandler           ; DMA2 Stream 4                                  
                    DCD     ETH_IRQHandler                    ; Ethernet                                        
                    DCD     ETH_WKUP_IRQHandler               ; Ethernet Wakeup through EXTI line                      
                    DCD     CAN2_TX_IRQHandler                ; CAN2 TX                                                
                    DCD     CAN2_RX0_IRQHandler               ; CAN2 RX0                                              
                    DCD     CAN2_RX1_IRQHandler               ; CAN2 RX1                                              
                    DCD     CAN2_SCE_IRQHandler               ; CAN2 SCE                                              
                    DCD     OTG_FS_IRQHandler                 ; USB OTG FS                                      
                    DCD     DMA2_Stream5_IRQHandler           ; DMA2 Stream 5                                  
                    DCD     DMA2_Stream6_IRQHandler           ; DMA2 Stream 6                                  
                    DCD     DMA2_Stream7_IRQHandler           ; DMA2 Stream 7                                  
                    DCD     USART6_IRQHandler                 ; USART6                                          
                    DCD     I2C3_EV_IRQHandler                ; I2C3 event                                            
                    DCD     I2C3_ER_IRQHandler                ; I2C3 error                                            
                    DCD     OTG_HS_EP1_OUT_IRQHandler         ; USB OTG HS End Point 1 Out                      
                    DCD     OTG_HS_EP1_IN_IRQHandler          ; USB OTG HS End Point 1 In                      
                    DCD     OTG_HS_WKUP_IRQHandler            ; USB OTG HS Wakeup through EXTI                        
                    DCD     OTG_HS_IRQHandler                 ; USB OTG HS                                      
                    DCD     DCMI_IRQHandler                   ; DCMI                                            
                    DCD     CRYP_IRQHandler                   ; CRYP crypto                                    
                    DCD     HASH_RNG_IRQHandler               ; Hash and Rng
                    DCD     FPU_IRQHandler                    ; FPU
                             
    __Vectors_End

    __Vectors_Size  EQU  __Vectors_End - __Vectors
     
  8. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Ok fun time... i havent actually checked my led pins with a analyzer to see if its doing time correctly but
    Code (text):
    /* Includes ------------------------------------------------------------------*/
    #include "main.h"

    static __IO uint32_t TimingDelay;

    int main(void)
    {

      /*!< At this stage the microcontroller clock setting is already configured,
           this is done through SystemInit() function which is called from startup
           file (startup_stm32f4xx.s) before to branch to application main.
           To reconfigure the default setting of SystemInit() function, refer to
           system_stm32f4xx.c file
         */  

      /* SysTick end of count event each 1us */
      if (SysTick_Config(168))
      {
        /* Capture error */
        while (1);
      }

      /* All GPIO Clock Enabled... Must Enable this before you can change other GPIO Registers ******/
      RCC->AHB1ENR = 0xFF;

      /* Initialize LEDs *****************/
        GPIOD->MODER = 0x55000000;     //Set the mode to OUTPUT for PD12:15
        GPIOD->OTYPER = 0x00;      //Set the TYPE to PUSH-PULL for all
        GPIOD->OSPEEDR = 0xFF000000;   //100/80 MHz Speed for leds
        GPIOD->PUPDR = 0x00;       //PULL-UP & PULL-DOWN DISABLED for all
      /* Turn on LEDs available on STM324xG-EVAL **********************************/
      GPIOD->BSRRL = 0xF000;
      while (1)
      {
        /* Toggle Leds off */
        GPIOD->BSRRH = 0xF000;
        /* Insert 100 ms delay */
        Delay(100,ms);

        /* Toggle Leds on */
        GPIOD->BSRRL = 0xF000;
        /* Insert 100 ms delay */
        Delay(100,ms);
      }
    }

    /**
      * @brief  Inserts a delay time.
      * @param  nTime: specifies the delay time length, in 10 ms.
      * @retval None
      */
    void Delay(__IO uint32_t nTime, uint32_t base)
    {
      TimingDelay = (nTime * base);

      while(TimingDelay != 0);
    }

    /**
      * @brief  Decrements the TimingDelay variable.
      * @param  None
      * @retval None
      */
    void TimingDelay_Decrement(void)
    {
      if (TimingDelay != 0x00)
      {
        TimingDelay--;
      }
    }

    #ifdef  USE_FULL_ASSERT

    /**
      * @brief  Reports the name of the source file and the source line number
      *   where the assert_param error has occurred.
      * @param  file: pointer to the source file name
      * @param  line: assert_param error line source number
      * @retval None
      */
    void assert_failed(uint8_t* file, uint32_t line)
    {
      /* User can add his own implementation to report the file name and line number,
         ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

      /* Infinite loop */
      while (1)
      {
      }
    }
    #endif

    /**
      * @}
      */


    /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
     
    Also i wasnt wrong then technically you could simply do: SysTick_Config(168000) for a 1ms time base then correct?
     
    Last edited: Mar 8, 2012
  9. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    with the 168 i get 300ms instead of 100ms... If i use 54 as my value i get 100ms close enough to perfect for me to keep it this way heh..

    [​IMG]
     

Share This Page