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