AnsweredAssumed Answered

positively lost regarding systick

Question asked by Solaris on Jul 10, 2014
Latest reply on Jul 11, 2014 by Solaris
Hi,

I'm quite lost regarding systick setup/operation.. If anyone can help that'd be great :)

my setup :



system_stm32f4xx.c  :

  *=============================================================================
  *=============================================================================
  *        Supported STM32F40xx/41xx/427x/437x devices
  *-----------------------------------------------------------------------------
  *        System Clock source                    | PLL (HSE)
  *-----------------------------------------------------------------------------
  *        SYSCLK(Hz)                             | 168000000
  *-----------------------------------------------------------------------------
  *        HCLK(Hz)                               | 168000000
  *-----------------------------------------------------------------------------
  *        AHB Prescaler                          | 1
  *-----------------------------------------------------------------------------
  *        APB1 Prescaler                         | 4
  *-----------------------------------------------------------------------------
  *        APB2 Prescaler                         | 2
  *-----------------------------------------------------------------------------
  *        HSE Frequency(Hz)                      | 8000000
  *-----------------------------------------------------------------------------
  *        PLL_M                                  | 8
  *-----------------------------------------------------------------------------
  *        PLL_N                                  | 336
  *-----------------------------------------------------------------------------
  *        PLL_P                                  | 2
  *-----------------------------------------------------------------------------
  *        PLL_Q                                  | 7
  *-----------------------------------------------------------------------------
  *        PLLI2S_N                               | 192
  *-----------------------------------------------------------------------------
  *        PLLI2S_R                               | 2
  *-----------------------------------------------------------------------------
  *        I2S input clock(Hz)                    | 96000000
  *                                               |
  *        To achieve the following I2S config:   |
  *         - Master clock output (MCKO): ON      |
  *         - Frame wide                : 16bit   |
  *         - Audio sampling freq (KHz) : 32      |
  *         - Error %                   : 2.3438  |
  *         - Prescaler Odd factor (ODD): 0       |
  *         - Linear prescaler (DIV)    : 2       |
  *-----------------------------------------------------------------------------
  *        VDD(V)                                 | 3.3
  *-----------------------------------------------------------------------------
  *        Main regulator output voltage          | Scale1 mode
  *-----------------------------------------------------------------------------
  *        Flash Latency(WS)                      | 5
  *-----------------------------------------------------------------------------
  *        Prefetch Buffer                        | OFF
  *-----------------------------------------------------------------------------
  *        Instruction cache                      | ON
  *-----------------------------------------------------------------------------
  *        Data cache                             | ON
  *-----------------------------------------------------------------------------
  *=============================================================================


stm32f4xx.h:
//  #define HSE_VALUE    ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */ 
#define HSE_VALUE    ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */



misc.h:
#define SysTick_CLKSource_HCLK_Div8    ((uint32_t)0xFFFFFFFB)
#define SysTick_CLKSource_HCLK         ((uint32_t)0x00000004)
#define IS_SYSTICK_CLK_SOURCE(SOURCE) (((SOURCE) == SysTick_CLKSource_HCLK) || \
                                       ((SOURCE) == SysTick_CLKSource_HCLK_Div8))
void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource);


misc.c: 
* @param  SysTick_CLKSource: specifies the SysTick clock source.
*     @arg SysTick_CLKSource_HCLK_Div8: AHB clock divided by 8 selected as SysTick clock source.
*     @arg SysTick_CLKSource_HCLK: AHB clock selected as SysTick clock source.
void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)
  assert_param(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource));
  if (SysTick_CLKSource == SysTick_CLKSource_HCLK)
    SysTick->CTRL |= SysTick_CLKSource_HCLK;
    SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;


If I  undestand the theory correctly then systick is working at 1/8th of the system clock : 168000000/8 = 21M ticks / second (counts from 21M to 0 every second). I need a timer with a resolution of 1sec, 1msec and 1usec, i wrote the following program (i'm printing to usb->serial emulation) but the timings seem to be all over the place (i'm trying  to get an accurate 1sec timer at the moment):


 
while (1)             //my main()
{
    delay(1);
    //GPIO_ToggleBits(GPIOD, GPIO_Pin_14);
    yolo_count++;
    printf("%i\n\r", yolo_count);
    //printf("systemcoreclock : %i \n", SystemCoreClock);
    //printf("hse_value: %i\n", HSE_VALUE);
    //printf("
}



void timing_handler ()    //this is called from the systick interrupt
{
}
 
 
void delay(uint32_t ms)              //the 'delay' function
{
    uint32_t i=0;
    for (i=0;i<ms;i++)
    {
        while (SysTick_Config(21000000) != 0)       //(SystemCoreClock / 110) != 0)
        {
            __NOP();
        }
    }

ultimately  i want to be able to call this specifying the unit of time and amount. When I'm running the program above instead of getting a 1sec delay nothing happens, as if i'm overflowing a buffer or something :S

the definition of SysTick_Config specifies a uint32_t 'no. of ticks' value, i'm positively lost :S


core_cm0.h:static __INLINE uint32_t SysTick_Config(uint32_t ticks)
core_cm4.h:static __INLINE uint32_t SysTick_Config(uint32_t ticks)
core_cm3.h:static __INLINE uint32_t SysTick_Config(uint32_t ticks)

when i change the value of ticks to 21 i get roughly 2-4 tics/sec, which again doesn't make sense

Can you please let me know what am i doing wrong and if the way i think systick works has nothing to do with reality ?


Many thanks

Outcomes