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

Committer:
Yar
Date:
2017-05-24
Revision:
1:0d39ea4dee8b
Parent:
0:2f819bf6cd99

File content as of revision 1:0d39ea4dee8b:

#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

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; 
}

static __inline uint32_t delta(uint32_t t0, uint32_t t1) {
    return (t1 - t0); 
}

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 (!(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/1000);
    while (delta(t0, DWT->CYCCNT) < us_count_tic) ;
}