Dagozilla to RoboCup / EncoderDAGOZ

Nucleo_Encoder_16_bits.cpp

Committer:
irfantitok
Date:
2019-01-17
Revision:
4:9ce0e451aeff
Parent:
3:d43c60d01569
Child:
5:a3349f37ef99

File content as of revision 4:9ce0e451aeff:

#include "Nucleo_Encoder_16_bits.h"

namespace mbed
{   

    Nucleo_Encoder_16_bits::Nucleo_Encoder_16_bits(TIM_TypeDef * _TIM)
    {
        TIM = _TIM;
        // Initialisation of the TIM module as an encoder counter
        EncoderInit(&encoder, &timer, _TIM, 0xffff, TIM_ENCODERMODE_TI12);

        // Update (aka over- and underflow) interrupt enabled
        TIM->DIER |= 0x0001;
        // The initialisation process generates an update interrupt, so we'll have to clear the update flag before anything else
        TIM->SR &= 0xfffe;
    }
    
    Nucleo_Encoder_16_bits::Nucleo_Encoder_16_bits(TIM_TypeDef * _TIM, uint32_t _maxcount, uint32_t _encmode)
    {
        TIM = _TIM;
        // Initialisation of the TIM module as an encoder counter
        EncoderInit(&encoder, &timer, _TIM, _maxcount, _encmode);

        // Update (aka over- and underflow) interrupt enabled
        TIM->DIER |= 0x0001;
        // The initialisation process generates an update interrupt, so we'll have to clear the update flag before anything else
        TIM->SR &= 0xfffe;
    }
    
    Nucleo_Encoder_16_bits::Nucleo_Encoder_16_bits(TIM_Encoder_InitTypeDef * _encoder, TIM_HandleTypeDef * _timer, TIM_TypeDef * _TIM, uint32_t _maxcount, uint32_t _encmode)
    {
        timer = *_timer;
        encoder = *_encoder;
        TIM = _TIM;
        // Initialisation of the TIM module as an encoder counter
        EncoderInit(&encoder, &timer, _TIM, _maxcount, _encmode);

        // Update (aka over- and underflow) interrupt enabled
        TIM->DIER |= 0x0001;
        // The initialisation process generates an update interrupt, so we'll have to clear the update flag before anything else
        TIM->SR &= 0xfffe;
    }

    
    int32_t Nucleo_Encoder_16_bits::GetCounter(bool reset)
    {
        int16_t count = TIM->CNT;
        if(reset){
            switch((uint32_t)TIM){
                case TIM1_BASE :
                    TIM1->CNT = 0;
                break;
                
                case TIM2_BASE :
                    TIM2->CNT = 0;
                break;
                
                case TIM3_BASE :
                    TIM3->CNT = 0;
                break;
                
                case TIM4_BASE :
                    TIM4->CNT = 0;
                break;
                
                case TIM5_BASE :
                    TIM5->CNT = 0;
                break;
                
                case TIM8_BASE :
                    TIM8->CNT = 0;
                break;
                /*
                case TIM9_BASE :
                    TIM9->CNT = 0;
                break;
                
                case TIM12_BASE :
                    TIM12->CNT = 0;
                break;
                */
            }
        }
        else{
            switch((uint32_t)TIM)
            {
                case TIM1_BASE :
                    return (int32_t)count;
                
                case TIM2_BASE :
                    return (int32_t)count;
                
                case TIM3_BASE :
                    return (int32_t)count;
                
                case TIM4_BASE :
                    return (int32_t)count;
                
                case TIM5_BASE :
                    return (int32_t)count;
                    
                case TIM8_BASE :
                    return (int32_t)count;
                /*  
                case TIM9_BASE :
                    return (int32_t)count;
                    
                case TIM12_BASE :
                    return (int32_t)count;
                */
            }
        }
        
        return (int32_t)count;
    }
    
    
    TIM_HandleTypeDef* Nucleo_Encoder_16_bits::GetTimer()
    {
        return &timer;    
    }
    
}