.

Dependents:  

Committer:
altb
Date:
Mon Feb 25 09:47:04 2019 +0000
Revision:
11:78e723ede0c6
Parent:
0:d784b08f51ff
2018

Who changed what in which revision?

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