Skip to content

Commit c279154

Browse files
bcostmadbridge
authored andcommitted
Add cmsis, hal_tick, system files
1 parent b64f087 commit c279154

File tree

10 files changed

+9527
-0
lines changed

10 files changed

+9527
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* mbed Microcontroller Library
2+
* A generic CMSIS include header
3+
*******************************************************************************
4+
* Copyright (c) 2014, STMicroelectronics
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. 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+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
16+
* may be used to endorse or promote products derived from this software
17+
* without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*******************************************************************************
30+
*/
31+
32+
#ifndef MBED_CMSIS_H
33+
#define MBED_CMSIS_H
34+
35+
#include "stm32f4xx.h"
36+
#include "cmsis_nvic.h"
37+
38+
#endif
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* mbed Microcontroller Library
2+
* CMSIS-style functionality to support dynamic vectors
3+
*******************************************************************************
4+
* Copyright (c) 2014, STMicroelectronics
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. 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+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
16+
* may be used to endorse or promote products derived from this software
17+
* without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*******************************************************************************
30+
*/
31+
#include "cmsis_nvic.h"
32+
33+
#define NVIC_RAM_VECTOR_ADDRESS (0x20000000) // Vectors positioned at start of RAM
34+
#define NVIC_FLASH_VECTOR_ADDRESS (0x08000000) // Initial vector position in flash
35+
36+
void NVIC_SetVector(IRQn_Type IRQn, uint32_t vector) {
37+
uint32_t *vectors = (uint32_t *)SCB->VTOR;
38+
uint32_t i;
39+
40+
// Copy and switch to dynamic vectors if the first time called
41+
if (SCB->VTOR == NVIC_FLASH_VECTOR_ADDRESS) {
42+
uint32_t *old_vectors = vectors;
43+
vectors = (uint32_t*)NVIC_RAM_VECTOR_ADDRESS;
44+
for (i=0; i<NVIC_NUM_VECTORS; i++) {
45+
vectors[i] = old_vectors[i];
46+
}
47+
SCB->VTOR = (uint32_t)NVIC_RAM_VECTOR_ADDRESS;
48+
}
49+
vectors[IRQn + NVIC_USER_IRQ_OFFSET] = vector;
50+
}
51+
52+
uint32_t NVIC_GetVector(IRQn_Type IRQn) {
53+
uint32_t *vectors = (uint32_t*)SCB->VTOR;
54+
return vectors[IRQn + NVIC_USER_IRQ_OFFSET];
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* mbed Microcontroller Library
2+
* CMSIS-style functionality to support dynamic vectors
3+
*******************************************************************************
4+
* Copyright (c) 2014, STMicroelectronics
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. 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+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
16+
* may be used to endorse or promote products derived from this software
17+
* without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*******************************************************************************
30+
*/
31+
32+
#ifndef MBED_CMSIS_NVIC_H
33+
#define MBED_CMSIS_NVIC_H
34+
35+
// STM32F412ZG
36+
// CORE: 16 vectors = 64 bytes from 0x00 to 0x3F
37+
// MCU Peripherals: 97 vectors = 388 bytes from 0x40 to 0x1C3
38+
// Total: 113 vectors = 452 bytes (0x1C4) to be reserved in RAM
39+
#define NVIC_NUM_VECTORS 113
40+
#define NVIC_USER_IRQ_OFFSET 16
41+
42+
#include "cmsis.h"
43+
44+
#ifdef __cplusplus
45+
extern "C" {
46+
#endif
47+
48+
void NVIC_SetVector(IRQn_Type IRQn, uint32_t vector);
49+
uint32_t NVIC_GetVector(IRQn_Type IRQn);
50+
51+
#ifdef __cplusplus
52+
}
53+
#endif
54+
55+
#endif
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/**
2+
******************************************************************************
3+
* @file hal_tick.c
4+
* @author MCD Application Team
5+
* @brief Initialization of HAL tick
6+
******************************************************************************
7+
* @attention
8+
*
9+
* <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
10+
*
11+
* Redistribution and use in source and binary forms, with or without modification,
12+
* are permitted provided that the following conditions are met:
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
19+
* may be used to endorse or promote products derived from this software
20+
* without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
*
33+
******************************************************************************
34+
*/
35+
#include "hal_tick.h"
36+
37+
TIM_HandleTypeDef TimMasterHandle;
38+
uint32_t PreviousVal = 0;
39+
40+
void us_ticker_irq_handler(void);
41+
42+
void timer_irq_handler(void) {
43+
// Channel 1 for mbed timeout
44+
if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
45+
if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) {
46+
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
47+
us_ticker_irq_handler();
48+
}
49+
}
50+
51+
// Channel 2 for HAL tick
52+
if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) {
53+
if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC2) == SET) {
54+
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2);
55+
uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle);
56+
if ((val - PreviousVal) >= HAL_TICK_DELAY) {
57+
// Increment HAL variable
58+
HAL_IncTick();
59+
// Prepare next interrupt
60+
__HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY);
61+
PreviousVal = val;
62+
#if 0 // For DEBUG only
63+
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
64+
#endif
65+
}
66+
}
67+
}
68+
}
69+
70+
// Reconfigure the HAL tick using a standard timer instead of systick.
71+
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) {
72+
// Enable timer clock
73+
TIM_MST_RCC;
74+
75+
// Reset timer
76+
TIM_MST_RESET_ON;
77+
TIM_MST_RESET_OFF;
78+
79+
// Update the SystemCoreClock variable
80+
SystemCoreClockUpdate();
81+
82+
// Configure time base
83+
TimMasterHandle.Instance = TIM_MST;
84+
TimMasterHandle.Init.Period = 0xFFFFFFFF;
85+
TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 us tick
86+
TimMasterHandle.Init.ClockDivision = 0;
87+
TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
88+
TimMasterHandle.Init.RepetitionCounter = 0;
89+
HAL_TIM_OC_Init(&TimMasterHandle);
90+
91+
NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler);
92+
NVIC_EnableIRQ(TIM_MST_IRQ);
93+
94+
// Channel 1 for mbed timeout
95+
HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
96+
97+
// Channel 2 for HAL tick
98+
HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2);
99+
PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle);
100+
__HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY);
101+
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
102+
103+
#if 0 // For DEBUG only
104+
__GPIOB_CLK_ENABLE();
105+
GPIO_InitTypeDef GPIO_InitStruct;
106+
GPIO_InitStruct.Pin = GPIO_PIN_6;
107+
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
108+
GPIO_InitStruct.Pull = GPIO_PULLUP;
109+
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
110+
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
111+
#endif
112+
113+
return HAL_OK;
114+
}
115+
116+
void HAL_SuspendTick(void)
117+
{
118+
TimMasterHandle.Instance = TIM_MST;
119+
120+
// Disable HAL tick and us_ticker update interrupts (used for 32 bit counter)
121+
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2);
122+
}
123+
124+
void HAL_ResumeTick(void)
125+
{
126+
TimMasterHandle.Instance = TIM_MST;
127+
128+
// Enable HAL tick and us_ticker update interrupts (used for 32 bit counter)
129+
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
130+
}
131+
/**
132+
* @}
133+
*/
134+
135+
/**
136+
* @}
137+
*/
138+
139+
/**
140+
* @}
141+
*/
142+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
******************************************************************************
3+
* @file hal_tick.h
4+
* @author MCD Application Team
5+
* @brief Initialization of HAL tick
6+
******************************************************************************
7+
* @attention
8+
*
9+
* <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
10+
*
11+
* Redistribution and use in source and binary forms, with or without modification,
12+
* are permitted provided that the following conditions are met:
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
19+
* may be used to endorse or promote products derived from this software
20+
* without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
*
33+
******************************************************************************
34+
*/
35+
#ifndef __HAL_TICK_H
36+
#define __HAL_TICK_H
37+
38+
#ifdef __cplusplus
39+
extern "C" {
40+
#endif
41+
42+
#include "stm32f4xx.h"
43+
#include "cmsis_nvic.h"
44+
45+
#define TIM_MST TIM5
46+
#define TIM_MST_IRQ TIM5_IRQn
47+
#define TIM_MST_RCC __TIM5_CLK_ENABLE()
48+
49+
#define TIM_MST_RESET_ON __TIM5_FORCE_RESET()
50+
#define TIM_MST_RESET_OFF __TIM5_RELEASE_RESET()
51+
52+
#define HAL_TICK_DELAY (1000) // 1 ms
53+
54+
#ifdef __cplusplus
55+
}
56+
#endif
57+
58+
#endif // __HAL_TICK_H
59+
60+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

0 commit comments

Comments
 (0)