Here, alternative functions and classes for STM32. The program contains a class for the I2C software bus, a class for working with a watchdog timer and time delay functions based on DWT. All functions and classes use the HAL library. Functions and classes were written for the microcontroller stm32f103.
Dependents: STM32_F1XX_Alternative
AlternativeDelay.cpp@1:0d39ea4dee8b, 2017-05-24 (annotated)
- Committer:
- Yar
- Date:
- Wed May 24 20:35:23 2017 +0000
- Revision:
- 1:0d39ea4dee8b
- Parent:
- 0:2f819bf6cd99
Fixed a bug with a delay of milliseconds
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yar | 0:2f819bf6cd99 | 1 | #include "AlternativeDelay.hpp" |
Yar | 0:2f819bf6cd99 | 2 | #include "stm32f1xx_hal.h" |
Yar | 0:2f819bf6cd99 | 3 | #include "mbed.h" |
Yar | 0:2f819bf6cd99 | 4 | |
Yar | 0:2f819bf6cd99 | 5 | #define DWT_CYCCNT *(volatile unsigned long *)0xE0001004 |
Yar | 0:2f819bf6cd99 | 6 | #define DWT_CONTROL *(volatile unsigned long *)0xE0001000 |
Yar | 0:2f819bf6cd99 | 7 | #define SCB_DEMCR *(volatile unsigned long *)0xE000EDFC |
Yar | 0:2f819bf6cd99 | 8 | |
Yar | 1:0d39ea4dee8b | 9 | void initializeTimeDelays(void) { |
Yar | 0:2f819bf6cd99 | 10 | // allow to use the counter |
Yar | 0:2f819bf6cd99 | 11 | SCB_DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; |
Yar | 0:2f819bf6cd99 | 12 | // zero the value of the register register |
Yar | 0:2f819bf6cd99 | 13 | DWT_CYCCNT = 0; |
Yar | 0:2f819bf6cd99 | 14 | // start the counter |
Yar | 0:2f819bf6cd99 | 15 | DWT_CONTROL |= DWT_CTRL_CYCCNTENA_Msk; |
Yar | 0:2f819bf6cd99 | 16 | } |
Yar | 0:2f819bf6cd99 | 17 | |
Yar | 0:2f819bf6cd99 | 18 | static __inline uint32_t delta(uint32_t t0, uint32_t t1) { |
Yar | 0:2f819bf6cd99 | 19 | return (t1 - t0); |
Yar | 0:2f819bf6cd99 | 20 | } |
Yar | 0:2f819bf6cd99 | 21 | |
Yar | 0:2f819bf6cd99 | 22 | void delay_us(uint32_t us) { |
Yar | 1:0d39ea4dee8b | 23 | if (!(SCB_DEMCR & CoreDebug_DEMCR_TRCENA_Msk)) { |
Yar | 1:0d39ea4dee8b | 24 | // allow to use the counter |
Yar | 1:0d39ea4dee8b | 25 | SCB_DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; |
Yar | 1:0d39ea4dee8b | 26 | // zero the value of the register register |
Yar | 1:0d39ea4dee8b | 27 | DWT_CYCCNT = 0; |
Yar | 1:0d39ea4dee8b | 28 | // start the counter |
Yar | 1:0d39ea4dee8b | 29 | DWT_CONTROL |= DWT_CTRL_CYCCNTENA_Msk; |
Yar | 1:0d39ea4dee8b | 30 | //isInitializationDWT = true; |
Yar | 1:0d39ea4dee8b | 31 | } |
Yar | 0:2f819bf6cd99 | 32 | uint32_t t0 = DWT->CYCCNT; |
Yar | 0:2f819bf6cd99 | 33 | uint32_t us_count_tic = us * (SystemCoreClock/1000000); |
Yar | 0:2f819bf6cd99 | 34 | while (delta(t0, DWT->CYCCNT) < us_count_tic) ; |
Yar | 0:2f819bf6cd99 | 35 | } |
Yar | 0:2f819bf6cd99 | 36 | |
Yar | 0:2f819bf6cd99 | 37 | void delay_ms(uint32_t ms) { |
Yar | 1:0d39ea4dee8b | 38 | if (!(SCB_DEMCR & CoreDebug_DEMCR_TRCENA_Msk)) { |
Yar | 1:0d39ea4dee8b | 39 | // allow to use the counter |
Yar | 1:0d39ea4dee8b | 40 | SCB_DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; |
Yar | 1:0d39ea4dee8b | 41 | // zero the value of the register register |
Yar | 1:0d39ea4dee8b | 42 | DWT_CYCCNT = 0; |
Yar | 1:0d39ea4dee8b | 43 | // start the counter |
Yar | 1:0d39ea4dee8b | 44 | DWT_CONTROL |= DWT_CTRL_CYCCNTENA_Msk; |
Yar | 1:0d39ea4dee8b | 45 | //isInitializationDWT = true; |
Yar | 0:2f819bf6cd99 | 46 | } |
Yar | 0:2f819bf6cd99 | 47 | uint32_t t0 = DWT->CYCCNT; |
Yar | 1:0d39ea4dee8b | 48 | uint32_t us_count_tic = ms * (SystemCoreClock/1000); |
Yar | 0:2f819bf6cd99 | 49 | while (delta(t0, DWT->CYCCNT) < us_count_tic) ; |
Yar | 1:0d39ea4dee8b | 50 | } |
Yar | 1:0d39ea4dee8b | 51 |