Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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