c

Dependencies:   Servo ServoArm mbed

Fork of PES_PIXY_Official by zhaw_st16b_pes2_10

Committer:
beacon
Date:
Mon May 22 11:24:46 2017 +0000
Revision:
0:15a8480061e8
o

Who changed what in which revision?

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