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

Committer:
Yar
Date:
Wed May 24 19:04:12 2017 +0000
Revision:
0:2f819bf6cd99
Child:
1:0d39ea4dee8b
Implemented class for the I2C software bus. The class for working with I2C only supports writing data. Implemented alternative functions for time delays based on DWT. Implemented a class for working with a watchdog timer.

Who changed what in which revision?

UserRevisionLine numberNew 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 0:2f819bf6cd99 9 static bool isInitializationDWT = false;
Yar 0:2f819bf6cd99 10
Yar 0:2f819bf6cd99 11 void initializationDWT(void) {
Yar 0:2f819bf6cd99 12 // allow to use the counter
Yar 0:2f819bf6cd99 13 SCB_DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
Yar 0:2f819bf6cd99 14 // zero the value of the register register
Yar 0:2f819bf6cd99 15 DWT_CYCCNT = 0;
Yar 0:2f819bf6cd99 16 // start the counter
Yar 0:2f819bf6cd99 17 DWT_CONTROL |= DWT_CTRL_CYCCNTENA_Msk;
Yar 0:2f819bf6cd99 18 isInitializationDWT = true;
Yar 0:2f819bf6cd99 19 }
Yar 0:2f819bf6cd99 20
Yar 0:2f819bf6cd99 21 void initializeTimeDelays(void) {
Yar 0:2f819bf6cd99 22 initializationDWT();
Yar 0:2f819bf6cd99 23 }
Yar 0:2f819bf6cd99 24
Yar 0:2f819bf6cd99 25 static __inline uint32_t delta(uint32_t t0, uint32_t t1) {
Yar 0:2f819bf6cd99 26 return (t1 - t0);
Yar 0:2f819bf6cd99 27 }
Yar 0:2f819bf6cd99 28
Yar 0:2f819bf6cd99 29 void delay_us(uint32_t us) {
Yar 0:2f819bf6cd99 30 uint32_t t0 = DWT->CYCCNT;
Yar 0:2f819bf6cd99 31 uint32_t us_count_tic = us * (SystemCoreClock/1000000);
Yar 0:2f819bf6cd99 32 while (delta(t0, DWT->CYCCNT) < us_count_tic) ;
Yar 0:2f819bf6cd99 33 }
Yar 0:2f819bf6cd99 34
Yar 0:2f819bf6cd99 35 void delay_ms(uint32_t ms) {
Yar 0:2f819bf6cd99 36 if (isInitializationDWT == false) {
Yar 0:2f819bf6cd99 37 initializationDWT();
Yar 0:2f819bf6cd99 38 }
Yar 0:2f819bf6cd99 39 uint32_t t0 = DWT->CYCCNT;
Yar 0:2f819bf6cd99 40 uint32_t us_count_tic = ms * (SystemCoreClock/1000000);
Yar 0:2f819bf6cd99 41 while (delta(t0, DWT->CYCCNT) < us_count_tic) ;
Yar 0:2f819bf6cd99 42 }