system.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, James Jackson
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "system.h"
33 
34 // current uptime for 32kHz systick timer. will rollover after 1.5 days. hopefully we won't care.
35 static volatile uint64_t sysTickUptime = 0;
36 
37 // SysTick
38 void SysTick_Handler(void)
39 {
40  sysTickUptime++;
41 }
42 
43 // Return system uptime in microseconds (rollover in 1.5 days)
44 volatile uint64_t micros(void)
45 {
46  return (sysTickUptime * 3125ul)/100ul; // The convsersion is 31.25, so doing fixed-point math to be exact
47 }
48 
49 // Return system uptime in milliseconds (rollover in 1.5 days)
50 volatile uint32_t millis(void)
51 {
52  return (volatile uint32_t)(sysTickUptime >> 5); // ( >> 5 is the same as divide by 32, but takes one operation)
53 }
54 
55 void systemInit(void)
56 {
57  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //2 bit preemption, 2 bit sub priority
58 
59  // Configure Systick
60  SysTick_Config(SystemCoreClock / 32000 + 28);
62 
63  //TODO: Should these be abstracted with the board-specific (ie revo_f4.h) file?
72 
77 
79 
81 
89 
92 }
93 
94 void delayMicroseconds(uint32_t us)
95 {
96  uint32_t now = micros();
97  volatile uint64_t m;
98  while ((m=micros()) - now < us);
99 }
100 
101 void delay(uint32_t ms)
102 {
103  while (ms--)
104  delayMicroseconds(1000);
105 }
106 
void delay(uint32_t ms)
Definition: system.c:101
static volatile uint64_t sysTickUptime
Definition: system.c:35
void delayMicroseconds(uint32_t us)
Definition: system.c:94
void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
Configures the priority grouping: pre-emption priority and subpriority.
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
Enables or disables the Low Speed APB (APB1) peripheral clock.
__STATIC_INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
Set Interrupt Priority.
Definition: core_cm0.h:611
volatile uint32_t millis(void)
Definition: system.c:50
uint32_t SystemCoreClock
void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
Enables or disables the AHB1 peripheral clock.
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
Enables or disables the High Speed APB (APB2) peripheral clock.
void RCC_AHB2PeriphClockCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState)
Enables or disables the AHB2 peripheral clock.
__STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
System Tick Configuration.
Definition: core_cm0.h:685
void systemInit(void)
Definition: system.c:55
void SysTick_Handler(void)
Definition: system.c:38
volatile uint64_t micros(void)
Definition: system.c:44


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Apr 15 2021 05:07:49