xx

Dependencies:   Servo mbed pixy

Fork of PES1 by Gruppe 3

Committer:
itslinear
Date:
Tue May 09 13:21:49 2017 +0000
Revision:
18:6547e54ac803
f?r holdi;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
itslinear 18:6547e54ac803 1 /*
itslinear 18:6547e54ac803 2 * EncoderCounter.cpp
itslinear 18:6547e54ac803 3 * Copyright (c) 2016, ZHAW
itslinear 18:6547e54ac803 4 * All rights reserved.
itslinear 18:6547e54ac803 5 */
itslinear 18:6547e54ac803 6
itslinear 18:6547e54ac803 7 #include "EncoderCounter.h"
itslinear 18:6547e54ac803 8
itslinear 18:6547e54ac803 9 using namespace std;
itslinear 18:6547e54ac803 10
itslinear 18:6547e54ac803 11 /**
itslinear 18:6547e54ac803 12 * Creates and initializes the driver to read the quadrature
itslinear 18:6547e54ac803 13 * encoder counter of the STM32 microcontroller.
itslinear 18:6547e54ac803 14 * @param a the input pin for the channel A.
itslinear 18:6547e54ac803 15 * @param b the input pin for the channel B.
itslinear 18:6547e54ac803 16 */
itslinear 18:6547e54ac803 17 EncoderCounter::EncoderCounter(PinName a, PinName b) {
itslinear 18:6547e54ac803 18
itslinear 18:6547e54ac803 19 // check pins
itslinear 18:6547e54ac803 20
itslinear 18:6547e54ac803 21 if ((a == PA_6) && (b == PC_7)) {
itslinear 18:6547e54ac803 22
itslinear 18:6547e54ac803 23 // pinmap OK for TIM3 CH1 and CH2
itslinear 18:6547e54ac803 24
itslinear 18:6547e54ac803 25 TIM = TIM3;
itslinear 18:6547e54ac803 26
itslinear 18:6547e54ac803 27 // configure reset and clock control registers
itslinear 18:6547e54ac803 28
itslinear 18:6547e54ac803 29 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // manually enable port C (port A enabled by mbed library)
itslinear 18:6547e54ac803 30
itslinear 18:6547e54ac803 31 // configure general purpose I/O registers
itslinear 18:6547e54ac803 32
itslinear 18:6547e54ac803 33 GPIOA->MODER &= ~GPIO_MODER_MODER6; // reset port A6
itslinear 18:6547e54ac803 34 GPIOA->MODER |= GPIO_MODER_MODER6_1; // set alternate mode of port A6
itslinear 18:6547e54ac803 35 GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR6; // reset pull-up/pull-down on port A6
itslinear 18:6547e54ac803 36 GPIOA->PUPDR |= GPIO_PUPDR_PUPDR6_1; // set input as pull-down
itslinear 18:6547e54ac803 37 GPIOA->AFR[0] &= ~(0xF << 4*6); // reset alternate function of port A6
itslinear 18:6547e54ac803 38 GPIOA->AFR[0] |= 2 << 4*6; // set alternate funtion 2 of port A6
itslinear 18:6547e54ac803 39
itslinear 18:6547e54ac803 40 GPIOC->MODER &= ~GPIO_MODER_MODER7; // reset port C7
itslinear 18:6547e54ac803 41 GPIOC->MODER |= GPIO_MODER_MODER7_1; // set alternate mode of port C7
itslinear 18:6547e54ac803 42 GPIOC->PUPDR &= ~GPIO_PUPDR_PUPDR7; // reset pull-up/pull-down on port C7
itslinear 18:6547e54ac803 43 GPIOC->PUPDR |= GPIO_PUPDR_PUPDR7_1; // set input as pull-down
itslinear 18:6547e54ac803 44 GPIOC->AFR[0] &= ~0xF0000000; // reset alternate function of port C7
itslinear 18:6547e54ac803 45 GPIOC->AFR[0] |= 2 << 4*7; // set alternate funtion 2 of port C7
itslinear 18:6547e54ac803 46
itslinear 18:6547e54ac803 47 // configure reset and clock control registers
itslinear 18:6547e54ac803 48
itslinear 18:6547e54ac803 49 RCC->APB1RSTR |= RCC_APB1RSTR_TIM3RST; //reset TIM3 controller
itslinear 18:6547e54ac803 50 RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM3RST;
itslinear 18:6547e54ac803 51
itslinear 18:6547e54ac803 52 RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; // TIM3 clock enable
itslinear 18:6547e54ac803 53
itslinear 18:6547e54ac803 54 } else if ((a == PB_6) && (b == PB_7)) {
itslinear 18:6547e54ac803 55
itslinear 18:6547e54ac803 56 // pinmap OK for TIM4 CH1 and CH2
itslinear 18:6547e54ac803 57
itslinear 18:6547e54ac803 58 TIM = TIM4;
itslinear 18:6547e54ac803 59
itslinear 18:6547e54ac803 60 // configure reset and clock control registers
itslinear 18:6547e54ac803 61
itslinear 18:6547e54ac803 62 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // manually enable port B (port A enabled by mbed library)
itslinear 18:6547e54ac803 63
itslinear 18:6547e54ac803 64 // configure general purpose I/O registers
itslinear 18:6547e54ac803 65
itslinear 18:6547e54ac803 66 GPIOB->MODER &= ~GPIO_MODER_MODER6; // reset port B6
itslinear 18:6547e54ac803 67 GPIOB->MODER |= GPIO_MODER_MODER6_1; // set alternate mode of port B6
itslinear 18:6547e54ac803 68 GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR6; // reset pull-up/pull-down on port B6
itslinear 18:6547e54ac803 69 GPIOB->PUPDR |= GPIO_PUPDR_PUPDR6_1; // set input as pull-down
itslinear 18:6547e54ac803 70 GPIOB->AFR[0] &= ~(0xF << 4*6); // reset alternate function of port B6
itslinear 18:6547e54ac803 71 GPIOB->AFR[0] |= 2 << 4*6; // set alternate funtion 2 of port B6
itslinear 18:6547e54ac803 72
itslinear 18:6547e54ac803 73 GPIOB->MODER &= ~GPIO_MODER_MODER7; // reset port B7
itslinear 18:6547e54ac803 74 GPIOB->MODER |= GPIO_MODER_MODER7_1; // set alternate mode of port B7
itslinear 18:6547e54ac803 75 GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR7; // reset pull-up/pull-down on port B7
itslinear 18:6547e54ac803 76 GPIOB->PUPDR |= GPIO_PUPDR_PUPDR7_1; // set input as pull-down
itslinear 18:6547e54ac803 77 GPIOB->AFR[0] &= ~0xF0000000; // reset alternate function of port B7
itslinear 18:6547e54ac803 78 GPIOB->AFR[0] |= 2 << 4*7; // set alternate funtion 2 of port B7
itslinear 18:6547e54ac803 79
itslinear 18:6547e54ac803 80 // configure reset and clock control registers
itslinear 18:6547e54ac803 81
itslinear 18:6547e54ac803 82 RCC->APB1RSTR |= RCC_APB1RSTR_TIM4RST; //reset TIM4 controller
itslinear 18:6547e54ac803 83 RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM4RST;
itslinear 18:6547e54ac803 84
itslinear 18:6547e54ac803 85 RCC->APB1ENR |= RCC_APB1ENR_TIM4EN; // TIM4 clock enable
itslinear 18:6547e54ac803 86
itslinear 18:6547e54ac803 87 } else {
itslinear 18:6547e54ac803 88
itslinear 18:6547e54ac803 89 printf("pinmap not found for peripheral\n");
itslinear 18:6547e54ac803 90 }
itslinear 18:6547e54ac803 91
itslinear 18:6547e54ac803 92 // configure general purpose timer 3 or 4
itslinear 18:6547e54ac803 93
itslinear 18:6547e54ac803 94 TIM->CR1 = 0x0000; // counter disable
itslinear 18:6547e54ac803 95 TIM->CR2 = 0x0000; // reset master mode selection
itslinear 18:6547e54ac803 96 TIM->SMCR = TIM_SMCR_SMS_1 | TIM_SMCR_SMS_0; // counting on both TI1 & TI2 edges
itslinear 18:6547e54ac803 97 TIM->CCMR1 = TIM_CCMR1_CC2S_0 | TIM_CCMR1_CC1S_0;
itslinear 18:6547e54ac803 98 TIM->CCMR2 = 0x0000; // reset capture mode register 2
itslinear 18:6547e54ac803 99 TIM->CCER = TIM_CCER_CC2E | TIM_CCER_CC1E;
itslinear 18:6547e54ac803 100 TIM->CNT = 0x0000; // reset counter value
itslinear 18:6547e54ac803 101 TIM->ARR = 0xFFFF; // auto reload register
itslinear 18:6547e54ac803 102 TIM->CR1 = TIM_CR1_CEN; // counter enable
itslinear 18:6547e54ac803 103 }
itslinear 18:6547e54ac803 104
itslinear 18:6547e54ac803 105 EncoderCounter::~EncoderCounter() {}
itslinear 18:6547e54ac803 106
itslinear 18:6547e54ac803 107 /**
itslinear 18:6547e54ac803 108 * Resets the counter value to zero.
itslinear 18:6547e54ac803 109 */
itslinear 18:6547e54ac803 110 void EncoderCounter::reset() {
itslinear 18:6547e54ac803 111
itslinear 18:6547e54ac803 112 TIM->CNT = 0x0000;
itslinear 18:6547e54ac803 113 }
itslinear 18:6547e54ac803 114
itslinear 18:6547e54ac803 115 /**
itslinear 18:6547e54ac803 116 * Resets the counter value to a given offset value.
itslinear 18:6547e54ac803 117 * @param offset the offset value to reset the counter to.
itslinear 18:6547e54ac803 118 */
itslinear 18:6547e54ac803 119 void EncoderCounter::reset(short offset) {
itslinear 18:6547e54ac803 120
itslinear 18:6547e54ac803 121 TIM->CNT = -offset;
itslinear 18:6547e54ac803 122 }
itslinear 18:6547e54ac803 123
itslinear 18:6547e54ac803 124 /**
itslinear 18:6547e54ac803 125 * Reads the quadrature encoder counter value.
itslinear 18:6547e54ac803 126 * @return the quadrature encoder counter as a signed 16-bit integer value.
itslinear 18:6547e54ac803 127 */
itslinear 18:6547e54ac803 128 short EncoderCounter::read() {
itslinear 18:6547e54ac803 129
itslinear 18:6547e54ac803 130 return (short)(-TIM->CNT);
itslinear 18:6547e54ac803 131 }
itslinear 18:6547e54ac803 132
itslinear 18:6547e54ac803 133 /**
itslinear 18:6547e54ac803 134 * The empty operator is a shorthand notation of the <code>read()</code> method.
itslinear 18:6547e54ac803 135 */
itslinear 18:6547e54ac803 136 EncoderCounter::operator short() {
itslinear 18:6547e54ac803 137
itslinear 18:6547e54ac803 138 return read();
itslinear 18:6547e54ac803 139 }