-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* micros.h | ||
* | ||
* Created on: Nov 18, 2019 | ||
* Author: Bulanov Konstantin | ||
* | ||
* Contact information | ||
* ------------------- | ||
* | ||
* e-mail : [email protected] | ||
*/ | ||
|
||
/* | ||
* |--------------------------------------------------------------------------------- | ||
* | Copyright (C) Bulanov Konstantin,2019 | ||
* | | ||
* | This program is free software: you can redistribute it and/or modify | ||
* | it under the terms of the GNU General Public License as published by | ||
* | the Free Software Foundation, either version 3 of the License, or | ||
* | any later version. | ||
* | | ||
* | This program is distributed in the hope that it will be useful, | ||
* | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* | GNU General Public License for more details. | ||
* | | ||
* | You should have received a copy of the GNU General Public License | ||
* | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | | ||
* | DWT and delay_us() algorithm used from https://istarik.ru/blog/stm32/131.html and https://github.com/stDstm/Example_STM32F103/tree/master/delay_micros_one_file | ||
* |--------------------------------------------------------------------------------- | ||
*/ | ||
|
||
#ifndef INC_MICROS_H_ | ||
#define INC_MICROS_H_ | ||
|
||
#include "main.h" | ||
|
||
__STATIC_INLINE void DWT_Init(void) | ||
{ | ||
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; // разрешаем использовать счётчик | ||
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; // запускаем счётчик | ||
} | ||
|
||
__STATIC_INLINE void delay_us(uint32_t us) | ||
{ | ||
uint32_t us_count_tic = us * (SystemCoreClock / 1000000U); | ||
DWT->CYCCNT = 0U; | ||
while(DWT->CYCCNT < us_count_tic); | ||
} | ||
|
||
__STATIC_INLINE uint32_t micros(void){ | ||
return DWT->CYCCNT / (SystemCoreClock / 1000000U); | ||
} | ||
|
||
#endif /* INC_MICROS_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,47 @@ | ||
# micros | ||
STM32 HAL library for release function micros() and delay_us(). | ||
# STM32 HAL library for release function micros() and delay_us(). | ||
|
||
## English note | ||
A simple C library (STM32 HAL) for working with microsecond function. | ||
|
||
Copy the library header to the appropriate project directories (Inc). | ||
|
||
In the head file of your project (main.c), include the header file | ||
``` | ||
/ * USER CODE BEGIN Includes * / | ||
#include "micros.h" | ||
/ * USER CODE END Includes * / | ||
``` | ||
add in main function section for initial initialization of the DWT (Data Watchpoint and Trace unit) | ||
``` | ||
/* USER CODE BEGIN 2 */ | ||
DWT_Init(); | ||
/* USER CODE END 2 */ | ||
``` | ||
Use function on program | ||
``` | ||
uint32_t micros(); | ||
void delay_us(...); | ||
``` | ||
|
||
## Russian note | ||
Простая библиотека на С (STM32 HAL) для работы с микросекундами. | ||
|
||
Скопируйте заголовочный файл библиотеки в соотвесвтующую директорию проекта (Inc). | ||
|
||
В головном файл вашего проекта (main.c) подключите заголовочный файл | ||
``` | ||
/ * USER CODE BEGIN Includes * / | ||
#include "micros.h" | ||
/ * USER CODE END Includes * / | ||
``` | ||
добавьте в секцию функции main(void) код инициализации модуля DWT (Data Watchpoint and Trace unit) | ||
``` | ||
/* USER CODE BEGIN 2 */ | ||
DWT_Init(); | ||
/* USER CODE END 2 */ | ||
``` | ||
Можете использовать функции в вашем проекте | ||
``` | ||
uint32_t micros(); | ||
void delay_us(...); | ||
``` |