GlobLib
HAL and API libraries for MCUs and hardware.
stm32f103cb_systick.c
Go to the documentation of this file.
1 /*!**************************************************************************
2  @file stm32f103cb_systick.c
3  @brief Source file for stm32f103cb systick
4  @author Stuart Ianna
5  @version 0.1
6  @date June 2018
7  @copyright GNU GPLv3
8  @warning None
9  @bug
10 
11  @details
12 
13  @par Compilers
14  - arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
15 ******************************************************************************/
16 
17 #include "stm32f103cb_systick.h"
18 
19 //The systick handler function pointer
20 void (*systick_handler)(void);
21 
22 //Setup systick counter and set handler
23 mcu_error SYSTICK_setup(uint32_t timeout, void (*handler)(void)){
24 
25  mcu_error error;
26  nvic_set_priority(NVIC_SYSTICK_IRQ,16);
27  error = SYSTICK_timeout(timeout);
28  SYSTICK_handler(handler);
29  return error;
30 }
31 
32 //Setup the timout
33 mcu_error SYSTICK_timeout(uint32_t timeout){
34 
35  uint32_t reload;
37 
38  //Disable systick before changing values
39  systick_counter_disable();
40  systick_clear();
41 
42  if((CLOCK_getSpeed() < 24000000) && (timeout < 1000)){
43 
44  systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
45  systick_set_reload((uint32_t)(timeout * (float)((float)(CLOCK_getSpeed())/1000000.0f)));
46 
47  }
48  else{
49 
50  systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8);
51  reload = (uint32_t)(timeout * (float)((float)(CLOCK_getSpeed()>>3)/1000000.0f));
52 
53  //Check if reload value is greater than max value (24-bit)
54  if(reload > 0xFFFFFF){
55 
56  systick_set_reload(0xFFFFFF);
57  error = E_SYSTICK_TOOLONG;
58 
59  //Print error if needed
60  if(MCU_debugIsEnabled()){
61 
63  }
64 
65  }
66  else{
67 
68  systick_set_reload(reload);
69  }
70  }
71 
72  //Enable systick again
73  systick_counter_enable();
74 
75  return error;
76 }
77 
78 void SYSTICK_handler(void (*handler)(void)){
79 
80  //Turn interrupts off first
81  systick_interrupt_disable();
82 
83  //Set the handler
84  systick_handler = handler;
85 
86  //Enable interrupts again
87  systick_interrupt_enable();
88 }
89 
90 //Return current clock value
91 uint32_t SYSTICK_count(void){
92 
93  return systick_get_value();
94 }
95 
96 void sys_tick_handler(void){
97 
98  systick_handler();
99 
100 }
101 
102 void SYSTICK_start(void){
103 
104  systick_counter_enable();
105 
106 }
107 
108 void SYSTICK_stop(void){
109 
110  systick_counter_disable();
111 }
void SYSTICK_start(void)
Start the systick counter.
void MCU_printError(mcu_error errorNum)
Print a given error number as a character stream.
uint8_t MCU_debugIsEnabled(void)
Checks if debug is enabled.
void SYSTICK_handler(void(*handler)(void))
Sets the handler called on interrupt event.
mcu_error SYSTICK_timeout(uint32_t timeout)
Change the systick timeout value.
void SYSTICK_stop(void)
Stop the systick counter.
uint32_t CLOCK_getSpeed(void)
Get the current clock speed of the device.
uint32_t SYSTICK_count(void)
Get the current clock value.
Error systick: No error.
Header file for stm32f103cb SYSTICK.
Error systick: Timeout value is too short.
mcu_error SYSTICK_setup(uint32_t timeout, void(*handler)(void))
Sets up systick and handler.
mcu_error
Error enumerators for the Debug peripheral.