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

Files at this revision

API Documentation at this revision

Comitter:
Yar
Date:
Wed May 24 20:35:23 2017 +0000
Parent:
0:2f819bf6cd99
Commit message:
Fixed a bug with a delay of milliseconds

Changed in this revision

AlternativeDelay.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/AlternativeDelay.cpp	Wed May 24 19:04:12 2017 +0000
+++ b/AlternativeDelay.cpp	Wed May 24 20:35:23 2017 +0000
@@ -6,20 +6,13 @@
 #define    DWT_CONTROL   *(volatile unsigned long *)0xE0001000
 #define    SCB_DEMCR     *(volatile unsigned long *)0xE000EDFC
 
-static bool isInitializationDWT = false;
-
-void initializationDWT(void) {
+void initializeTimeDelays(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) {
@@ -27,16 +20,32 @@
 }
 
 void delay_us(uint32_t us) {
+    if (!(SCB_DEMCR & CoreDebug_DEMCR_TRCENA_Msk)) {
+        // 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;
+    }
     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();
+    if (!(SCB_DEMCR & CoreDebug_DEMCR_TRCENA_Msk)) {
+        // 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;
     }
     uint32_t t0 =  DWT->CYCCNT;
-    uint32_t us_count_tic =  ms * (SystemCoreClock/1000000);
+    uint32_t us_count_tic =  ms * (SystemCoreClock/1000);
     while (delta(t0, DWT->CYCCNT) < us_count_tic) ;
-}
\ No newline at end of file
+}
+