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
Diff: AlternativeDelay.cpp
- Revision:
- 0:2f819bf6cd99
- Child:
- 1:0d39ea4dee8b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AlternativeDelay.cpp Wed May 24 19:04:12 2017 +0000 @@ -0,0 +1,42 @@ +#include "AlternativeDelay.hpp" +#include "stm32f1xx_hal.h" +#include "mbed.h" + +#define DWT_CYCCNT *(volatile unsigned long *)0xE0001004 +#define DWT_CONTROL *(volatile unsigned long *)0xE0001000 +#define SCB_DEMCR *(volatile unsigned long *)0xE000EDFC + +static bool isInitializationDWT = false; + +void initializationDWT(void) { + // allow to use the counter + SCB_DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; + // zero the value of the register register + DWT_CYCCNT = 0; + // start the counter + DWT_CONTROL |= DWT_CTRL_CYCCNTENA_Msk; + isInitializationDWT = true; +} + +void initializeTimeDelays(void) { + initializationDWT(); +} + +static __inline uint32_t delta(uint32_t t0, uint32_t t1) { + return (t1 - t0); +} + +void delay_us(uint32_t us) { + uint32_t t0 = DWT->CYCCNT; + uint32_t us_count_tic = us * (SystemCoreClock/1000000); + while (delta(t0, DWT->CYCCNT) < us_count_tic) ; +} + +void delay_ms(uint32_t ms) { + if (isInitializationDWT == false) { + initializationDWT(); + } + uint32_t t0 = DWT->CYCCNT; + uint32_t us_count_tic = ms * (SystemCoreClock/1000000); + while (delta(t0, DWT->CYCCNT) < us_count_tic) ; +} \ No newline at end of file