altb_pmic / Mbed 2 deprecated Test_optical_flow_PX4

Dependencies:   mbed

Committer:
pmic
Date:
Mon Apr 06 05:57:56 2020 +0000
Revision:
13:89b65bfe6dda
Parent:
4:77914e52baf3
Change receiving driver and firmware. Data latency now 2.4 ms faster.

Who changed what in which revision?

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