LPC1549でQEI(ハードウェアでエンコーダ読み取り)を実現します。 (.cppでピン指定しています(基本的にはどこでも使用可))
Dependents: Tourobo2022_TBCMotorDriver
qeihw.cpp@4:9385032f963b, 2019-03-19 (annotated)
- Committer:
- YutaTogashi
- Date:
- Tue Mar 19 08:51:32 2019 +0000
- Revision:
- 4:9385032f963b
- Parent:
- 3:68844cd35e64
- Child:
- 5:9923b9d751f1
- Child:
- 6:cd0694454594
QEI_hw
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hexley | 0:20a27391f6dc | 1 | /* mbed Library - QEI driver for LP1768 hardware |
hexley | 0:20a27391f6dc | 2 | * Copyright (c) 2010, hball |
hexley | 0:20a27391f6dc | 3 | * released under MIT license http://mbed.org/licence/mit |
hexley | 0:20a27391f6dc | 4 | */ |
hexley | 0:20a27391f6dc | 5 | |
hexley | 0:20a27391f6dc | 6 | /***********************************************************************//** |
hexley | 0:20a27391f6dc | 7 | * @file qeihw.cpp |
hexley | 0:20a27391f6dc | 8 | * @brief Driver file for the QEI hardware. Requires connection to |
hexley | 0:20a27391f6dc | 9 | * internal mbed circuit nodes. Adapted from the CMSIS |
hexley | 0:20a27391f6dc | 10 | * driver, lpc17xx_qei.c, v 2.0 |
hexley | 2:53f8ae2cf502 | 11 | * @version 0.1 |
hexley | 2:53f8ae2cf502 | 12 | * @date 28 Dec 2010 |
hexley | 0:20a27391f6dc | 13 | * @author hb |
hexley | 0:20a27391f6dc | 14 | **************************************************************************/ |
hexley | 0:20a27391f6dc | 15 | #include "mbed.h" |
hexley | 0:20a27391f6dc | 16 | #include "qeihw.h" |
hexley | 0:20a27391f6dc | 17 | |
hexley | 0:20a27391f6dc | 18 | QEIHW *QEIHW::instance; |
hexley | 0:20a27391f6dc | 19 | |
hexley | 0:20a27391f6dc | 20 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 21 | * @brief Create a QEI object and configure it. |
hexley | 0:20a27391f6dc | 22 | * @param _dirinv Direction invert. When = 1, complements the QEICONF register DIR bit |
hexley | 0:20a27391f6dc | 23 | * @param _sigmode Signal mode. When = 0, PhA and PhB are quadrature inputs. When = 1, PhA is direction and PhB is clock |
hexley | 0:20a27391f6dc | 24 | * @param _capmode Capture mode. When = 0, count PhA edges only (2X mode). Whe = 1, count PhB edges also (4X mode). |
hexley | 0:20a27391f6dc | 25 | * @param _invinx Invert index. When = 1, inverts the sense of the index signal |
hexley | 0:20a27391f6dc | 26 | * @return None |
hexley | 0:20a27391f6dc | 27 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 28 | QEIHW::QEIHW(uint32_t _dirinv, uint32_t _sigmode, uint32_t _capmode, uint32_t _invinx) |
hexley | 0:20a27391f6dc | 29 | { |
wt8008 | 3:68844cd35e64 | 30 | #ifdef TARGET_LPC1768 |
hexley | 0:20a27391f6dc | 31 | /* Set up clock and power for QEI module */ |
hexley | 0:20a27391f6dc | 32 | LPC_SC->PCONP |= PCONP_QEI_ENABLE; |
hexley | 0:20a27391f6dc | 33 | |
hexley | 0:20a27391f6dc | 34 | /* The clock for theQEI module is set to FCCLK */ |
hexley | 0:20a27391f6dc | 35 | LPC_SC->PCLKSEL1 = LPC_SC->PCLKSEL1 & ~(3UL<<0) | ((PCLKSEL_CCLK_DIV_1 & 3)<<0); |
wt8008 | 3:68844cd35e64 | 36 | #elif defined(TARGET_LPC1549) |
wt8008 | 3:68844cd35e64 | 37 | /* Enable clock for the QEI module */ |
wt8008 | 3:68844cd35e64 | 38 | LPC_SYSCON->SYSAHBCLKCTRL1 = LPC_SYSCON->SYSAHBCLKCTRL1 | (1<<21); |
wt8008 | 3:68844cd35e64 | 39 | /* Clear peripheral reset for entire comparator block */ |
wt8008 | 3:68844cd35e64 | 40 | LPC_SYSCON->PRESETCTRL1 = 0; |
wt8008 | 3:68844cd35e64 | 41 | #endif |
hexley | 0:20a27391f6dc | 42 | |
wt8008 | 3:68844cd35e64 | 43 | #ifdef TARGET_LPC1768 |
hexley | 0:20a27391f6dc | 44 | /* Assign the pins. They are hard-coded, not user-selected. The index |
hexley | 0:20a27391f6dc | 45 | * pin is assigned, even though it is not easily accessed on the mbed. |
hexley | 0:20a27391f6dc | 46 | * As it may be unconnected, it is given a pull-up resistor to minimize |
hexley | 0:20a27391f6dc | 47 | * power drain. |
hexley | 0:20a27391f6dc | 48 | */ |
hexley | 0:20a27391f6dc | 49 | // MCI0 (PhA) |
hexley | 0:20a27391f6dc | 50 | LPC_PINCON->PINSEL3 = (LPC_PINCON->PINSEL3 & PINSEL3_MCI0_MASK) | PINSEL3_MCI0 ; |
hexley | 0:20a27391f6dc | 51 | LPC_PINCON->PINMODE3 = (LPC_PINCON->PINMODE3 & PINMODE3_MCI0_MASK) | PINMODE3_MCI0; |
hexley | 0:20a27391f6dc | 52 | |
hexley | 0:20a27391f6dc | 53 | // MCI1 (PhB) |
hexley | 0:20a27391f6dc | 54 | LPC_PINCON->PINSEL3 = (LPC_PINCON->PINSEL3 & PINSEL3_MCI1_MASK) | PINSEL3_MCI1 ; |
hexley | 0:20a27391f6dc | 55 | LPC_PINCON->PINMODE3 = (LPC_PINCON->PINMODE3 & PINMODE3_MCI1_MASK) | PINMODE3_MCI1; |
hexley | 0:20a27391f6dc | 56 | |
hexley | 0:20a27391f6dc | 57 | // MCI2 (Index) |
hexley | 0:20a27391f6dc | 58 | LPC_PINCON->PINSEL3 = (LPC_PINCON->PINSEL3 & PINSEL3_MCI2_MASK) | PINSEL3_MCI2 ; |
hexley | 0:20a27391f6dc | 59 | LPC_PINCON->PINMODE3 = (LPC_PINCON->PINMODE3 & PINMODE3_MCI2_MASK) | PINMODE3_MCI2; |
wt8008 | 3:68844cd35e64 | 60 | #elif defined(TARGET_LPC1549) // Assign pins |
wt8008 | 3:68844cd35e64 | 61 | LPC_SWM->PINASSIGN14 = (LPC_SWM->PINASSIGN14 & 0x000000FF) |
YutaTogashi | 4:9385032f963b | 62 | |(6<<QEIO_PHA_I) | (5<<QEIO_PHB_I) | (34<<QEIO_IDX_I); |
wt8008 | 3:68844cd35e64 | 63 | // P0_24 P0_0 P1_3 |
wt8008 | 3:68844cd35e64 | 64 | #endif |
hexley | 0:20a27391f6dc | 65 | |
hexley | 0:20a27391f6dc | 66 | // Initialize all remaining values in QEI peripheral |
hexley | 0:20a27391f6dc | 67 | LPC_QEI->QEICON = QEI_CON_RESP | QEI_CON_RESV | QEI_CON_RESI; |
hexley | 0:20a27391f6dc | 68 | LPC_QEI->QEIMAXPOS = 0xFFFFFFFF; // Default value |
hexley | 0:20a27391f6dc | 69 | LPC_QEI->CMPOS0 = 0x00; |
hexley | 0:20a27391f6dc | 70 | LPC_QEI->CMPOS1 = 0x00; |
hexley | 0:20a27391f6dc | 71 | LPC_QEI->CMPOS2 = 0x00; |
hexley | 0:20a27391f6dc | 72 | LPC_QEI->INXCMP = 0x00; |
hexley | 0:20a27391f6dc | 73 | LPC_QEI->QEILOAD = 0x00; |
hexley | 0:20a27391f6dc | 74 | LPC_QEI->VELCOMP = 0x00; |
wt8008 | 3:68844cd35e64 | 75 | #ifdef TARGET_LPC1768 |
hexley | 0:20a27391f6dc | 76 | LPC_QEI->FILTER = 200000; // Default for mechanical switches. |
wt8008 | 3:68844cd35e64 | 77 | #elif defined(TARGET_LPC1549) |
wt8008 | 3:68844cd35e64 | 78 | LPC_QEI->FILTERPHA = 200000; |
wt8008 | 3:68844cd35e64 | 79 | LPC_QEI->FILTERPHB = 200000; |
wt8008 | 3:68844cd35e64 | 80 | LPC_QEI->FILTERINX = 200000; |
wt8008 | 3:68844cd35e64 | 81 | #endif |
hexley | 0:20a27391f6dc | 82 | |
hexley | 0:20a27391f6dc | 83 | // Set QEI configuration value corresponding to the call parameters |
hexley | 0:20a27391f6dc | 84 | LPC_QEI->QEICONF = ( |
hexley | 0:20a27391f6dc | 85 | ((_dirinv << 0) & 1) | \ |
hexley | 0:20a27391f6dc | 86 | ((_sigmode << 1) & 2) | \ |
hexley | 0:20a27391f6dc | 87 | ((_capmode << 2) & 4) | \ |
hexley | 0:20a27391f6dc | 88 | ((_invinx <<3) & 8) ); |
hexley | 0:20a27391f6dc | 89 | |
hexley | 0:20a27391f6dc | 90 | // Mask all int sources |
hexley | 0:20a27391f6dc | 91 | LPC_QEI->QEIIEC = QEI_IECLR_BITMASK; // Set the "clear" bits for all sources in the IE clear register |
hexley | 0:20a27391f6dc | 92 | |
hexley | 0:20a27391f6dc | 93 | // Clear any pending ints |
hexley | 0:20a27391f6dc | 94 | LPC_QEI->QEICLR = QEI_INTCLR_BITMASK; // Set the "clear" bits for for all sources in the Interrupt clear register |
hexley | 0:20a27391f6dc | 95 | |
hexley | 0:20a27391f6dc | 96 | /* preemption = 1, sub-priority = 1 */ |
hexley | 0:20a27391f6dc | 97 | NVIC_SetPriority(QEI_IRQn, ((0x01<<3)|0x01)); |
hexley | 0:20a27391f6dc | 98 | |
hexley | 0:20a27391f6dc | 99 | //* Attach IRQ |
hexley | 0:20a27391f6dc | 100 | instance = this; |
hexley | 0:20a27391f6dc | 101 | NVIC_SetVector(QEI_IRQn, (uint32_t)&_Qeiisr); |
hexley | 0:20a27391f6dc | 102 | |
hexley | 0:20a27391f6dc | 103 | /* Enable interrupt for QEI */ |
hexley | 0:20a27391f6dc | 104 | NVIC_EnableIRQ(QEI_IRQn); |
hexley | 0:20a27391f6dc | 105 | |
hexley | 0:20a27391f6dc | 106 | } |
hexley | 0:20a27391f6dc | 107 | |
hexley | 0:20a27391f6dc | 108 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 109 | * @brief Resets value for each type of QEI value, such as velocity, |
hexley | 0:20a27391f6dc | 110 | * counter, position, etc.. |
hexley | 0:20a27391f6dc | 111 | * @param[in] ulResetType QEI Reset Type, should be one of the following: |
hexley | 0:20a27391f6dc | 112 | * - QEI_RESET_POS: Reset Position Counter |
hexley | 0:20a27391f6dc | 113 | * - QEI_RESET_POSOnIDX: Reset Position Counter on Index signal |
hexley | 0:20a27391f6dc | 114 | * - QEI_RESET_VEL: Reset Velocity |
hexley | 0:20a27391f6dc | 115 | * - QEI_RESET_IDX: Reset Index Counter |
hexley | 0:20a27391f6dc | 116 | * @return None |
hexley | 0:20a27391f6dc | 117 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 118 | void QEIHW::Reset(uint32_t ulResetType) |
hexley | 0:20a27391f6dc | 119 | { |
hexley | 0:20a27391f6dc | 120 | LPC_QEI->QEICON = ulResetType; |
hexley | 0:20a27391f6dc | 121 | } |
hexley | 0:20a27391f6dc | 122 | |
hexley | 0:20a27391f6dc | 123 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 124 | * @brief De-initializes the QEI peripheral registers to their |
hexley | 0:20a27391f6dc | 125 | * default reset values. |
hexley | 0:20a27391f6dc | 126 | * |
hexley | 0:20a27391f6dc | 127 | * @return None |
hexley | 0:20a27391f6dc | 128 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 129 | void QEIHW::DeInit() |
hexley | 0:20a27391f6dc | 130 | { |
wt8008 | 3:68844cd35e64 | 131 | #ifdef TARGET_LPC1768 |
hexley | 0:20a27391f6dc | 132 | /* Turn off clock and power for QEI module */ |
hexley | 0:20a27391f6dc | 133 | LPC_SC->PCONP &= PCONP_QEI_DISABLE; |
hexley | 0:20a27391f6dc | 134 | |
wt8008 | 3:68844cd35e64 | 135 | |
hexley | 0:20a27391f6dc | 136 | /* Return pins to their default assignment (PINSEL = 0, PINMODE = PULLDOWN) */ |
hexley | 0:20a27391f6dc | 137 | // MCI0 (PhA) -> LED-2 (p1.20) |
hexley | 0:20a27391f6dc | 138 | LPC_PINCON->PINSEL3 &= PINSEL3_MCI0_MASK; |
hexley | 0:20a27391f6dc | 139 | LPC_PINCON->PINMODE3 = (LPC_PINCON->PINMODE3 & PINMODE3_MCI0_MASK) | PINMODE3_GPIO1p20; |
hexley | 0:20a27391f6dc | 140 | |
hexley | 0:20a27391f6dc | 141 | // MCI1 (PhB) -> LED-4 (p1.23) |
hexley | 0:20a27391f6dc | 142 | LPC_PINCON->PINSEL3 &= PINSEL3_MCI1_MASK; |
hexley | 0:20a27391f6dc | 143 | LPC_PINCON->PINMODE3 = (LPC_PINCON->PINMODE3 & PINMODE3_MCI1_MASK) | PINMODE3_GPIO1p23; |
hexley | 0:20a27391f6dc | 144 | |
hexley | 0:20a27391f6dc | 145 | // MCI2 (Index) -> p1.24 |
hexley | 0:20a27391f6dc | 146 | LPC_PINCON->PINSEL3 &= PINSEL3_MCI2_MASK; |
hexley | 0:20a27391f6dc | 147 | LPC_PINCON->PINMODE3 = (LPC_PINCON->PINMODE3 & PINMODE3_MCI2_MASK) | PINMODE3_GPIO1p24; |
wt8008 | 3:68844cd35e64 | 148 | #elif defined(TARGET_LPC1549) |
wt8008 | 3:68844cd35e64 | 149 | LPC_SWM->PINASSIGN14 = LPC_SWM ->PINASSIGN14 & 0x000000FF; |
wt8008 | 3:68844cd35e64 | 150 | #endif |
hexley | 0:20a27391f6dc | 151 | } |
hexley | 0:20a27391f6dc | 152 | |
hexley | 0:20a27391f6dc | 153 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 154 | * @brief Report direction (QEISTAT bit DIR) |
hexley | 0:20a27391f6dc | 155 | * |
hexley | 0:20a27391f6dc | 156 | * @return State of the DIR bit (SET or RESET) |
hexley | 0:20a27391f6dc | 157 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 158 | FlagStatus QEIHW::Direction() |
hexley | 0:20a27391f6dc | 159 | { |
hexley | 0:20a27391f6dc | 160 | return ((LPC_QEI->QEISTAT & QEI_STATUS_DIR) ? SET : RESET); |
hexley | 0:20a27391f6dc | 161 | } |
hexley | 0:20a27391f6dc | 162 | |
hexley | 0:20a27391f6dc | 163 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 164 | * @brief Get current position value in QEI peripheral |
hexley | 0:20a27391f6dc | 165 | * |
hexley | 0:20a27391f6dc | 166 | * @return Current position value of QEI peripheral |
hexley | 0:20a27391f6dc | 167 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 168 | uint32_t QEIHW::GetPosition() |
hexley | 0:20a27391f6dc | 169 | { |
hexley | 0:20a27391f6dc | 170 | return (LPC_QEI->QEIPOS); |
hexley | 0:20a27391f6dc | 171 | } |
hexley | 0:20a27391f6dc | 172 | |
hexley | 0:20a27391f6dc | 173 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 174 | * @brief Set max position value for QEI peripheral |
hexley | 0:20a27391f6dc | 175 | * |
hexley | 0:20a27391f6dc | 176 | * @param[in] ulMaxPos Max position value to set |
hexley | 0:20a27391f6dc | 177 | * @return None |
hexley | 0:20a27391f6dc | 178 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 179 | void QEIHW::SetMaxPosition(uint32_t ulMaxPos) |
hexley | 0:20a27391f6dc | 180 | { |
hexley | 0:20a27391f6dc | 181 | LPC_QEI->QEIMAXPOS = ulMaxPos; |
hexley | 0:20a27391f6dc | 182 | } |
hexley | 0:20a27391f6dc | 183 | |
hexley | 0:20a27391f6dc | 184 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 185 | * @brief Set position compare value for QEI peripheral |
hexley | 0:20a27391f6dc | 186 | * @param[in] QEIx QEI peripheral, should be LPC_QEI |
hexley | 0:20a27391f6dc | 187 | * @param[in] bPosCompCh Compare Position channel, should be: |
hexley | 0:20a27391f6dc | 188 | * - QEI_COMPPOS_CH_0: QEI compare position channel 0 |
hexley | 0:20a27391f6dc | 189 | * - QEI_COMPPOS_CH_1: QEI compare position channel 1 |
hexley | 0:20a27391f6dc | 190 | * - QEI_COMPPOS_CH_2: QEI compare position channel 2 |
hexley | 0:20a27391f6dc | 191 | * @param[in] ulPosComp Compare Position value to set |
hexley | 0:20a27391f6dc | 192 | * @return None |
hexley | 0:20a27391f6dc | 193 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 194 | void QEIHW::SetPositionComp( uint8_t bPosCompCh, uint32_t ulPosComp) |
hexley | 0:20a27391f6dc | 195 | { |
hexley | 0:20a27391f6dc | 196 | uint32_t *tmp; |
hexley | 0:20a27391f6dc | 197 | |
hexley | 0:20a27391f6dc | 198 | tmp = (uint32_t *) (&(LPC_QEI->CMPOS0) + bPosCompCh * 4); |
hexley | 0:20a27391f6dc | 199 | *tmp = ulPosComp; |
hexley | 0:20a27391f6dc | 200 | } |
hexley | 0:20a27391f6dc | 201 | |
hexley | 0:20a27391f6dc | 202 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 203 | * @brief Get current index counter of QEI peripheral |
hexley | 0:20a27391f6dc | 204 | * |
hexley | 0:20a27391f6dc | 205 | * @return Current value of QEI index counter |
hexley | 0:20a27391f6dc | 206 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 207 | uint32_t QEIHW::GetIndex() |
hexley | 0:20a27391f6dc | 208 | { |
hexley | 0:20a27391f6dc | 209 | return (LPC_QEI->INXCNT); |
hexley | 0:20a27391f6dc | 210 | } |
hexley | 0:20a27391f6dc | 211 | |
hexley | 0:20a27391f6dc | 212 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 213 | * @brief Set value for index compare in QEI peripheral |
hexley | 0:20a27391f6dc | 214 | * @param[in] ulIndexComp Compare Index Value to set |
hexley | 0:20a27391f6dc | 215 | * @return None |
hexley | 0:20a27391f6dc | 216 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 217 | void QEIHW::SetIndexComp( uint32_t ulIndexComp) |
hexley | 0:20a27391f6dc | 218 | { |
hexley | 0:20a27391f6dc | 219 | LPC_QEI->INXCMP = ulIndexComp; |
hexley | 0:20a27391f6dc | 220 | } |
hexley | 0:20a27391f6dc | 221 | |
hexley | 0:20a27391f6dc | 222 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 223 | * @brief Set Velocity timer reload value |
hexley | 0:20a27391f6dc | 224 | * |
hexley | 0:20a27391f6dc | 225 | * @param[in] ulReloadValue Velocity timer reload count |
hexley | 0:20a27391f6dc | 226 | * @return None |
hexley | 0:20a27391f6dc | 227 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 228 | void QEIHW::SetVelocityTimerReload( uint32_t ulReloadValue) |
hexley | 0:20a27391f6dc | 229 | { |
hexley | 0:20a27391f6dc | 230 | LPC_QEI->QEILOAD = ulReloadValue; |
hexley | 0:20a27391f6dc | 231 | } |
hexley | 0:20a27391f6dc | 232 | |
hexley | 0:20a27391f6dc | 233 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 234 | * @brief Set Velocity timer reload value in microseconds |
hexley | 0:20a27391f6dc | 235 | * |
hexley | 0:20a27391f6dc | 236 | * @param[in] ulReloadValue Velocity timer reload count |
hexley | 0:20a27391f6dc | 237 | * @return None |
hexley | 0:20a27391f6dc | 238 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 239 | void QEIHW::SetVelocityTimerReload_us( uint32_t ulReloadValue) |
hexley | 0:20a27391f6dc | 240 | { |
wt8008 | 3:68844cd35e64 | 241 | #ifdef TARGET_LPC1768 |
hexley | 0:20a27391f6dc | 242 | int div; |
hexley | 0:20a27391f6dc | 243 | |
hexley | 0:20a27391f6dc | 244 | //Work out CCLK |
hexley | 0:20a27391f6dc | 245 | uint32_t m = (LPC_SC->PLL0CFG & 0xFFFF) + 1; |
hexley | 0:20a27391f6dc | 246 | uint32_t n = (LPC_SC->PLL0CFG >> 16) + 1; |
hexley | 0:20a27391f6dc | 247 | uint32_t cclkdiv = LPC_SC->CCLKCFG + 1; |
hexley | 0:20a27391f6dc | 248 | uint32_t Fcco = (2 * m * XTAL_FREQ) / n; |
hexley | 0:20a27391f6dc | 249 | uint32_t cclk = Fcco / cclkdiv; |
hexley | 0:20a27391f6dc | 250 | |
hexley | 0:20a27391f6dc | 251 | |
hexley | 0:20a27391f6dc | 252 | |
hexley | 0:20a27391f6dc | 253 | // div = CLKPWR_GetPCLKSEL(ClkType); |
hexley | 0:20a27391f6dc | 254 | div = LPC_SC->PCLKSEL1 & PCLKSEL1_PCLK_QEI_MASK; |
hexley | 0:20a27391f6dc | 255 | switch (div) |
hexley | 0:20a27391f6dc | 256 | { |
hexley | 0:20a27391f6dc | 257 | case 0: |
hexley | 0:20a27391f6dc | 258 | div = 4; |
hexley | 0:20a27391f6dc | 259 | break; |
hexley | 0:20a27391f6dc | 260 | |
hexley | 0:20a27391f6dc | 261 | case 1: |
hexley | 0:20a27391f6dc | 262 | div = 1; |
hexley | 0:20a27391f6dc | 263 | break; |
hexley | 0:20a27391f6dc | 264 | |
hexley | 0:20a27391f6dc | 265 | case 2: |
hexley | 0:20a27391f6dc | 266 | div = 2; |
hexley | 0:20a27391f6dc | 267 | break; |
hexley | 0:20a27391f6dc | 268 | |
hexley | 0:20a27391f6dc | 269 | case 3: |
hexley | 0:20a27391f6dc | 270 | div = 8; |
hexley | 0:20a27391f6dc | 271 | break; |
hexley | 0:20a27391f6dc | 272 | } |
hexley | 2:53f8ae2cf502 | 273 | cclk /=div; |
hexley | 2:53f8ae2cf502 | 274 | cclk =((uint64_t)cclk / (1000000/ulReloadValue)) - 1; |
hexley | 2:53f8ae2cf502 | 275 | LPC_QEI->QEILOAD = (uint32_t) cclk; |
wt8008 | 3:68844cd35e64 | 276 | #elif defined(TARGET_LPC1549) |
wt8008 | 3:68844cd35e64 | 277 | uint32_t p; |
wt8008 | 3:68844cd35e64 | 278 | uint32_t sysclk; |
wt8008 | 3:68844cd35e64 | 279 | uint32_t qeicount; |
wt8008 | 3:68844cd35e64 | 280 | |
wt8008 | 3:68844cd35e64 | 281 | // Assuming using crystal as clock source |
wt8008 | 3:68844cd35e64 | 282 | // Workout system clock (pg 63 LPC15xx user guide) |
wt8008 | 3:68844cd35e64 | 283 | uint32_t psel = ((LPC_SYSCON->SYSPLLCTRL & 0x000000C0) >> 6); // p value |
wt8008 | 3:68844cd35e64 | 284 | uint32_t msel = ((LPC_SYSCON->SYSPLLCTRL & 0x0000001F) + 1); // m description = msel value + 1 |
wt8008 | 3:68844cd35e64 | 285 | |
wt8008 | 3:68844cd35e64 | 286 | switch(psel) // p description |
wt8008 | 3:68844cd35e64 | 287 | { |
wt8008 | 3:68844cd35e64 | 288 | case 0: p = 1; break; |
wt8008 | 3:68844cd35e64 | 289 | case 1: p = 2; break; |
wt8008 | 3:68844cd35e64 | 290 | case 2: p = 4; break; |
wt8008 | 3:68844cd35e64 | 291 | case 3: p = 8; break; |
wt8008 | 3:68844cd35e64 | 292 | } |
wt8008 | 3:68844cd35e64 | 293 | |
wt8008 | 3:68844cd35e64 | 294 | sysclk = msel * XTAL_FREQ /(p * 2); |
wt8008 | 3:68844cd35e64 | 295 | |
wt8008 | 3:68844cd35e64 | 296 | ulReloadValue = LPC_QEI->LOAD; |
wt8008 | 3:68844cd35e64 | 297 | |
wt8008 | 3:68844cd35e64 | 298 | qeicount = ((uint64_t)sysclk / (1000000/ulReloadValue)) - 1; |
wt8008 | 3:68844cd35e64 | 299 | |
wt8008 | 3:68844cd35e64 | 300 | //LPC_QEI->LOAD = (uint32_t)qeicount; |
wt8008 | 3:68844cd35e64 | 301 | LPC_QEI->LOAD = (uint32_t)(ulReloadValue); |
wt8008 | 3:68844cd35e64 | 302 | #endif |
hexley | 0:20a27391f6dc | 303 | } |
hexley | 0:20a27391f6dc | 304 | |
hexley | 0:20a27391f6dc | 305 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 306 | * @brief Get current timer counter in QEI peripheral |
hexley | 0:20a27391f6dc | 307 | * |
hexley | 0:20a27391f6dc | 308 | * @return Current timer counter in QEI peripheral |
hexley | 0:20a27391f6dc | 309 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 310 | uint32_t QEIHW::GetTimer() |
hexley | 0:20a27391f6dc | 311 | { |
hexley | 0:20a27391f6dc | 312 | return (LPC_QEI->QEITIME); |
hexley | 0:20a27391f6dc | 313 | } |
hexley | 0:20a27391f6dc | 314 | |
hexley | 0:20a27391f6dc | 315 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 316 | * @brief Get current velocity pulse counter in current time period |
hexley | 0:20a27391f6dc | 317 | * |
hexley | 0:20a27391f6dc | 318 | * @return Current velocity pulse counter value |
hexley | 0:20a27391f6dc | 319 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 320 | uint32_t QEIHW::GetVelocity() |
hexley | 0:20a27391f6dc | 321 | { |
hexley | 0:20a27391f6dc | 322 | return (LPC_QEI->QEIVEL); |
hexley | 0:20a27391f6dc | 323 | } |
hexley | 0:20a27391f6dc | 324 | |
hexley | 0:20a27391f6dc | 325 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 326 | * @brief Get the most recently captured velocity of the QEI. When |
hexley | 0:20a27391f6dc | 327 | * the Velocity timer in QEI is over-flow, the current velocity |
hexley | 0:20a27391f6dc | 328 | * value will be loaded into Velocity Capture register. |
hexley | 0:20a27391f6dc | 329 | * |
hexley | 0:20a27391f6dc | 330 | * @return The most recently measured velocity value |
hexley | 0:20a27391f6dc | 331 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 332 | uint32_t QEIHW::GetVelocityCap() |
hexley | 0:20a27391f6dc | 333 | { |
wt8008 | 3:68844cd35e64 | 334 | //return (LPC_QEI->QEICAP); |
wt8008 | 3:68844cd35e64 | 335 | return (LPC_QEI->QEILOAD); |
hexley | 0:20a27391f6dc | 336 | } |
hexley | 0:20a27391f6dc | 337 | |
hexley | 0:20a27391f6dc | 338 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 339 | * @brief Set Velocity Compare value for QEI peripheral |
hexley | 0:20a27391f6dc | 340 | * |
hexley | 0:20a27391f6dc | 341 | * @param[in] ulVelComp Compare Velocity value to set |
hexley | 0:20a27391f6dc | 342 | * @return None |
hexley | 0:20a27391f6dc | 343 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 344 | void QEIHW::SetVelocityComp( uint32_t ulVelComp) |
hexley | 0:20a27391f6dc | 345 | { |
hexley | 0:20a27391f6dc | 346 | LPC_QEI->VELCOMP = ulVelComp; |
hexley | 0:20a27391f6dc | 347 | } |
hexley | 0:20a27391f6dc | 348 | |
hexley | 0:20a27391f6dc | 349 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 350 | * @brief Set value of sampling count for the digital filter in |
hexley | 0:20a27391f6dc | 351 | * QEI peripheral |
hexley | 0:20a27391f6dc | 352 | * |
hexley | 0:20a27391f6dc | 353 | * @param[in] ulSamplingPulse Value of sampling count to set |
hexley | 0:20a27391f6dc | 354 | * @return None |
hexley | 0:20a27391f6dc | 355 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 356 | void QEIHW::SetDigiFilter( uint32_t ulSamplingPulse) |
hexley | 0:20a27391f6dc | 357 | { |
wt8008 | 3:68844cd35e64 | 358 | #ifdef TARGET_LPC1768 |
hexley | 0:20a27391f6dc | 359 | LPC_QEI->FILTER = ulSamplingPulse; |
wt8008 | 3:68844cd35e64 | 360 | #elif defined(TARGET_LPC1549) |
wt8008 | 3:68844cd35e64 | 361 | LPC_QEI->FILTERPHA = ulSamplingPulse; |
wt8008 | 3:68844cd35e64 | 362 | LPC_QEI->FILTERPHB = ulSamplingPulse; |
wt8008 | 3:68844cd35e64 | 363 | LPC_QEI->FILTERINX = ulSamplingPulse; |
wt8008 | 3:68844cd35e64 | 364 | #endif |
wt8008 | 3:68844cd35e64 | 365 | |
hexley | 0:20a27391f6dc | 366 | } |
hexley | 0:20a27391f6dc | 367 | |
hexley | 0:20a27391f6dc | 368 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 369 | * @brief Check whether if specified interrupt flag status in QEI |
hexley | 0:20a27391f6dc | 370 | * peripheral is set or not |
hexley | 0:20a27391f6dc | 371 | * |
hexley | 0:20a27391f6dc | 372 | * @param[in] ulIntType Interrupt Flag Status type, should be: |
hexley | 0:20a27391f6dc | 373 | - QEI_INTFLAG_INX_Int: index pulse was detected interrupt |
hexley | 0:20a27391f6dc | 374 | - QEI_INTFLAG_TIM_Int: Velocity timer over flow interrupt |
hexley | 0:20a27391f6dc | 375 | - QEI_INTFLAG_VELC_Int: Capture velocity is less than compare interrupt |
hexley | 0:20a27391f6dc | 376 | - QEI_INTFLAG_DIR_Int: Change of direction interrupt |
hexley | 0:20a27391f6dc | 377 | - QEI_INTFLAG_ERR_Int: An encoder phase error interrupt |
hexley | 0:20a27391f6dc | 378 | - QEI_INTFLAG_ENCLK_Int: An encoder clock pulse was detected interrupt |
hexley | 0:20a27391f6dc | 379 | - QEI_INTFLAG_POS0_Int: position 0 compare value is equal to the |
hexley | 0:20a27391f6dc | 380 | current position interrupt |
hexley | 0:20a27391f6dc | 381 | - QEI_INTFLAG_POS1_Int: position 1 compare value is equal to the |
hexley | 0:20a27391f6dc | 382 | current position interrupt |
hexley | 0:20a27391f6dc | 383 | - QEI_INTFLAG_POS2_Int: position 2 compare value is equal to the |
hexley | 0:20a27391f6dc | 384 | current position interrupt |
hexley | 0:20a27391f6dc | 385 | - QEI_INTFLAG_REV_Int: Index compare value is equal to the current |
hexley | 0:20a27391f6dc | 386 | index count interrupt |
hexley | 0:20a27391f6dc | 387 | - QEI_INTFLAG_POS0REV_Int: Combined position 0 and revolution count interrupt |
hexley | 0:20a27391f6dc | 388 | - QEI_INTFLAG_POS1REV_Int: Combined position 1 and revolution count interrupt |
hexley | 0:20a27391f6dc | 389 | - QEI_INTFLAG_POS2REV_Int: Combined position 2 and revolution count interrupt |
hexley | 0:20a27391f6dc | 390 | * @return New State of specified interrupt flag status (SET or RESET) |
hexley | 0:20a27391f6dc | 391 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 392 | FlagStatus QEIHW::GetIntStatus( uint32_t ulIntType) |
hexley | 0:20a27391f6dc | 393 | { |
hexley | 0:20a27391f6dc | 394 | return((LPC_QEI->QEIINTSTAT & ulIntType) ? SET : RESET); |
hexley | 0:20a27391f6dc | 395 | } |
hexley | 0:20a27391f6dc | 396 | |
hexley | 0:20a27391f6dc | 397 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 398 | * @brief Enable/Disable specified interrupt in QEI peripheral |
hexley | 0:20a27391f6dc | 399 | * |
hexley | 0:20a27391f6dc | 400 | * @param[in] ulIntType Interrupt Flag Status type, should be: |
hexley | 0:20a27391f6dc | 401 | * - QEI_INTFLAG_INX_Int: index pulse was detected interrupt |
hexley | 0:20a27391f6dc | 402 | * - QEI_INTFLAG_TIM_Int: Velocity timer over flow interrupt |
hexley | 0:20a27391f6dc | 403 | * - QEI_INTFLAG_VELC_Int: Capture velocity is less than compare interrupt |
hexley | 0:20a27391f6dc | 404 | * - QEI_INTFLAG_DIR_Int: Change of direction interrupt |
hexley | 0:20a27391f6dc | 405 | * - QEI_INTFLAG_ERR_Int: An encoder phase error interrupt |
hexley | 0:20a27391f6dc | 406 | * - QEI_INTFLAG_ENCLK_Int: An encoder clock pulse was detected interrupt |
hexley | 0:20a27391f6dc | 407 | * - QEI_INTFLAG_POS0_Int: position 0 compare value is equal to the |
hexley | 0:20a27391f6dc | 408 | * current position interrupt |
hexley | 0:20a27391f6dc | 409 | * - QEI_INTFLAG_POS1_Int: position 1 compare value is equal to the |
hexley | 0:20a27391f6dc | 410 | * current position interrupt |
hexley | 0:20a27391f6dc | 411 | * - QEI_INTFLAG_POS2_Int: position 2 compare value is equal to the |
hexley | 0:20a27391f6dc | 412 | * current position interrupt |
hexley | 0:20a27391f6dc | 413 | * - QEI_INTFLAG_REV_Int: Index compare value is equal to the current |
hexley | 0:20a27391f6dc | 414 | * index count interrupt |
hexley | 0:20a27391f6dc | 415 | * - QEI_INTFLAG_POS0REV_Int: Combined position 0 and revolution count interrupt |
hexley | 0:20a27391f6dc | 416 | * - QEI_INTFLAG_POS1REV_Int: Combined position 1 and revolution count interrupt |
hexley | 0:20a27391f6dc | 417 | * - QEI_INTFLAG_POS2REV_Int: Combined position 2 and revolution count interrupt |
hexley | 0:20a27391f6dc | 418 | * @param[in] NewState New function state, should be: |
hexley | 0:20a27391f6dc | 419 | * - DISABLE |
hexley | 0:20a27391f6dc | 420 | * - ENABLE |
hexley | 0:20a27391f6dc | 421 | * @return None |
hexley | 0:20a27391f6dc | 422 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 423 | void QEIHW::IntCmd( uint32_t ulIntType, FunctionalState NewState) |
hexley | 0:20a27391f6dc | 424 | { |
hexley | 0:20a27391f6dc | 425 | if (NewState == ENABLE) { |
hexley | 0:20a27391f6dc | 426 | LPC_QEI->QEIIES = ulIntType; |
hexley | 0:20a27391f6dc | 427 | } else { |
hexley | 0:20a27391f6dc | 428 | LPC_QEI->QEIIEC = ulIntType; |
hexley | 0:20a27391f6dc | 429 | } |
hexley | 0:20a27391f6dc | 430 | } |
hexley | 0:20a27391f6dc | 431 | |
hexley | 0:20a27391f6dc | 432 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 433 | * @brief Assert specified interrupt in QEI peripheral |
hexley | 0:20a27391f6dc | 434 | * |
hexley | 0:20a27391f6dc | 435 | * @param[in] ulIntType Interrupt Flag Status type, should be: |
hexley | 0:20a27391f6dc | 436 | - QEI_INTFLAG_INX_Int: index pulse was detected interrupt |
hexley | 0:20a27391f6dc | 437 | - QEI_INTFLAG_TIM_Int: Velocity timer over flow interrupt |
hexley | 0:20a27391f6dc | 438 | - QEI_INTFLAG_VELC_Int: Capture velocity is less than compare interrupt |
hexley | 0:20a27391f6dc | 439 | - QEI_INTFLAG_DIR_Int: Change of direction interrupt |
hexley | 0:20a27391f6dc | 440 | - QEI_INTFLAG_ERR_Int: An encoder phase error interrupt |
hexley | 0:20a27391f6dc | 441 | - QEI_INTFLAG_ENCLK_Int: An encoder clock pulse was detected interrupt |
hexley | 0:20a27391f6dc | 442 | - QEI_INTFLAG_POS0_Int: position 0 compare value is equal to the |
hexley | 0:20a27391f6dc | 443 | current position interrupt |
hexley | 0:20a27391f6dc | 444 | - QEI_INTFLAG_POS1_Int: position 1 compare value is equal to the |
hexley | 0:20a27391f6dc | 445 | current position interrupt |
hexley | 0:20a27391f6dc | 446 | - QEI_INTFLAG_POS2_Int: position 2 compare value is equal to the |
hexley | 0:20a27391f6dc | 447 | current position interrupt |
hexley | 0:20a27391f6dc | 448 | - QEI_INTFLAG_REV_Int: Index compare value is equal to the current |
hexley | 0:20a27391f6dc | 449 | index count interrupt |
hexley | 0:20a27391f6dc | 450 | - QEI_INTFLAG_POS0REV_Int: Combined position 0 and revolution count interrupt |
hexley | 0:20a27391f6dc | 451 | - QEI_INTFLAG_POS1REV_Int: Combined position 1 and revolution count interrupt |
hexley | 0:20a27391f6dc | 452 | - QEI_INTFLAG_POS2REV_Int: Combined position 2 and revolution count interrupt |
hexley | 0:20a27391f6dc | 453 | * @return None |
hexley | 0:20a27391f6dc | 454 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 455 | void QEIHW::IntSet( uint32_t ulIntType) |
hexley | 0:20a27391f6dc | 456 | { |
hexley | 0:20a27391f6dc | 457 | LPC_QEI->QEISET = ulIntType; |
hexley | 0:20a27391f6dc | 458 | } |
hexley | 0:20a27391f6dc | 459 | |
hexley | 0:20a27391f6dc | 460 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 461 | * @brief De-assert specified interrupt (pending) in QEI peripheral |
hexley | 0:20a27391f6dc | 462 | * |
hexley | 0:20a27391f6dc | 463 | * @param[in] ulIntType Interrupt Flag Status type, should be: |
hexley | 0:20a27391f6dc | 464 | - QEI_INTFLAG_INX_Int: index pulse was detected interrupt |
hexley | 0:20a27391f6dc | 465 | - QEI_INTFLAG_TIM_Int: Velocity timer over flow interrupt |
hexley | 0:20a27391f6dc | 466 | - QEI_INTFLAG_VELC_Int: Capture velocity is less than compare interrupt |
hexley | 0:20a27391f6dc | 467 | - QEI_INTFLAG_DIR_Int: Change of direction interrupt |
hexley | 0:20a27391f6dc | 468 | - QEI_INTFLAG_ERR_Int: An encoder phase error interrupt |
hexley | 0:20a27391f6dc | 469 | - QEI_INTFLAG_ENCLK_Int: An encoder clock pulse was detected interrupt |
hexley | 0:20a27391f6dc | 470 | - QEI_INTFLAG_POS0_Int: position 0 compare value is equal to the |
hexley | 0:20a27391f6dc | 471 | current position interrupt |
hexley | 0:20a27391f6dc | 472 | - QEI_INTFLAG_POS1_Int: position 1 compare value is equal to the |
hexley | 0:20a27391f6dc | 473 | current position interrupt |
hexley | 0:20a27391f6dc | 474 | - QEI_INTFLAG_POS2_Int: position 2 compare value is equal to the |
hexley | 0:20a27391f6dc | 475 | current position interrupt |
hexley | 0:20a27391f6dc | 476 | - QEI_INTFLAG_REV_Int: Index compare value is equal to the current |
hexley | 0:20a27391f6dc | 477 | index count interrupt |
hexley | 0:20a27391f6dc | 478 | - QEI_INTFLAG_POS0REV_Int: Combined position 0 and revolution count interrupt |
hexley | 0:20a27391f6dc | 479 | - QEI_INTFLAG_POS1REV_Int: Combined position 1 and revolution count interrupt |
hexley | 0:20a27391f6dc | 480 | - QEI_INTFLAG_POS2REV_Int: Combined position 2 and revolution count interrupt |
hexley | 0:20a27391f6dc | 481 | * @return None |
hexley | 0:20a27391f6dc | 482 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 483 | void QEIHW::IntClear( uint32_t ulIntType) |
hexley | 0:20a27391f6dc | 484 | { |
hexley | 0:20a27391f6dc | 485 | LPC_QEI->QEICLR = ulIntType; |
hexley | 0:20a27391f6dc | 486 | } |
hexley | 0:20a27391f6dc | 487 | |
hexley | 0:20a27391f6dc | 488 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 489 | * @brief Calculates the actual velocity in RPM passed via velocity |
hexley | 0:20a27391f6dc | 490 | * capture value and Pulse Per Revolution (of the encoder) value |
hexley | 0:20a27391f6dc | 491 | * parameter input. |
hexley | 0:20a27391f6dc | 492 | * |
hexley | 0:20a27391f6dc | 493 | * @param[in] ulVelCapValue Velocity capture input value that can |
hexley | 0:20a27391f6dc | 494 | * be got from QEI_GetVelocityCap() function |
hexley | 0:20a27391f6dc | 495 | * @param[in] ulPPR Pulse per round of encoder |
hexley | 0:20a27391f6dc | 496 | * @return The actual value of velocity in RPM (Revolutions per minute) |
hexley | 0:20a27391f6dc | 497 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 498 | uint32_t QEIHW::CalculateRPM( uint32_t ulVelCapValue, uint32_t ulPPR) |
hexley | 0:20a27391f6dc | 499 | { |
hexley | 0:20a27391f6dc | 500 | uint64_t rpm, Load, edges; |
hexley | 0:20a27391f6dc | 501 | int div; |
hexley | 0:20a27391f6dc | 502 | |
wt8008 | 3:68844cd35e64 | 503 | #ifdef TARGET_LPC1768 |
hexley | 0:20a27391f6dc | 504 | // Get current Clock rate for timer input |
hexley | 0:20a27391f6dc | 505 | //Work out CCLK |
hexley | 0:20a27391f6dc | 506 | uint32_t m = (LPC_SC->PLL0CFG & 0xFFFF) + 1; |
hexley | 0:20a27391f6dc | 507 | uint32_t n = (LPC_SC->PLL0CFG >> 16) + 1; |
hexley | 0:20a27391f6dc | 508 | uint32_t cclkdiv = LPC_SC->CCLKCFG + 1; |
hexley | 0:20a27391f6dc | 509 | uint32_t Fcco = (2 * m * XTAL_FREQ) / n; |
hexley | 0:20a27391f6dc | 510 | uint32_t cclk = Fcco / cclkdiv; |
hexley | 0:20a27391f6dc | 511 | |
hexley | 0:20a27391f6dc | 512 | // div = CLKPWR_GetPCLKSEL(ClkType); |
hexley | 0:20a27391f6dc | 513 | div = LPC_SC->PCLKSEL1 & PCLKSEL1_PCLK_QEI_MASK; |
hexley | 0:20a27391f6dc | 514 | switch (div) |
hexley | 0:20a27391f6dc | 515 | { |
hexley | 0:20a27391f6dc | 516 | case 0: |
hexley | 0:20a27391f6dc | 517 | div = 4; |
hexley | 0:20a27391f6dc | 518 | break; |
hexley | 0:20a27391f6dc | 519 | |
hexley | 0:20a27391f6dc | 520 | case 1: |
hexley | 0:20a27391f6dc | 521 | div = 1; |
hexley | 0:20a27391f6dc | 522 | break; |
hexley | 0:20a27391f6dc | 523 | |
hexley | 0:20a27391f6dc | 524 | case 2: |
hexley | 0:20a27391f6dc | 525 | div = 2; |
hexley | 0:20a27391f6dc | 526 | break; |
hexley | 0:20a27391f6dc | 527 | |
hexley | 0:20a27391f6dc | 528 | case 3: |
hexley | 0:20a27391f6dc | 529 | div = 8; |
hexley | 0:20a27391f6dc | 530 | break; |
hexley | 0:20a27391f6dc | 531 | } |
hexley | 0:20a27391f6dc | 532 | cclk /= div; |
hexley | 0:20a27391f6dc | 533 | |
hexley | 0:20a27391f6dc | 534 | // Get Timer load value (velocity capture period) |
hexley | 0:20a27391f6dc | 535 | Load = (uint64_t)(LPC_QEI->QEILOAD + 1); |
hexley | 0:20a27391f6dc | 536 | // Get Edge |
hexley | 0:20a27391f6dc | 537 | edges = (uint64_t)((LPC_QEI->QEICONF & QEI_CONF_CAPMODE) ? 4 : 2); |
hexley | 0:20a27391f6dc | 538 | // Calculate RPM |
hexley | 0:20a27391f6dc | 539 | rpm = ((( uint64_t)cclk * ulVelCapValue * 60) / (Load * ulPPR * edges)); |
wt8008 | 3:68844cd35e64 | 540 | #elif defined(TARGET_LPC1549) |
wt8008 | 3:68844cd35e64 | 541 | // Assuming using crystal as clock source |
wt8008 | 3:68844cd35e64 | 542 | // Workout system clock (pg 63 LPC15xx user guide) |
wt8008 | 3:68844cd35e64 | 543 | uint32_t psel = ((LPC_SYSCON->SYSPLLCTRL & 0x000000C0) >> 6); // p value |
wt8008 | 3:68844cd35e64 | 544 | uint32_t msel = ((LPC_SYSCON->SYSPLLCTRL & 0x0000001F) + 1); // m description = msel value + 1 |
wt8008 | 3:68844cd35e64 | 545 | |
wt8008 | 3:68844cd35e64 | 546 | uint32_t p; |
wt8008 | 3:68844cd35e64 | 547 | uint32_t sysclk; |
wt8008 | 3:68844cd35e64 | 548 | uint32_t qeicount; |
wt8008 | 3:68844cd35e64 | 549 | uint32_t ulReloadValue; |
wt8008 | 3:68844cd35e64 | 550 | |
wt8008 | 3:68844cd35e64 | 551 | switch(psel) // p description |
wt8008 | 3:68844cd35e64 | 552 | { |
wt8008 | 3:68844cd35e64 | 553 | case 0: p = 1; break; |
wt8008 | 3:68844cd35e64 | 554 | case 1: p = 2; break; |
wt8008 | 3:68844cd35e64 | 555 | case 2: p = 4; break; |
wt8008 | 3:68844cd35e64 | 556 | case 3: p = 8; break; |
wt8008 | 3:68844cd35e64 | 557 | } |
wt8008 | 3:68844cd35e64 | 558 | |
wt8008 | 3:68844cd35e64 | 559 | sysclk = msel * XTAL_FREQ /(p * 2); |
wt8008 | 3:68844cd35e64 | 560 | |
wt8008 | 3:68844cd35e64 | 561 | ulReloadValue = LPC_QEI->LOAD; |
wt8008 | 3:68844cd35e64 | 562 | |
wt8008 | 3:68844cd35e64 | 563 | qeicount = ((uint64_t)sysclk / (1000000/ulReloadValue)) - 1; |
wt8008 | 3:68844cd35e64 | 564 | |
wt8008 | 3:68844cd35e64 | 565 | // Get Timer load value (velocity capture period) |
wt8008 | 3:68844cd35e64 | 566 | Load = (uint64_t)(LPC_QEI->LOAD + 1); |
wt8008 | 3:68844cd35e64 | 567 | |
wt8008 | 3:68844cd35e64 | 568 | // Get Edge |
wt8008 | 3:68844cd35e64 | 569 | edges = (uint64_t)((LPC_QEI->CONF & QEI_CONF_CAPMODE) ? 4 : 2); |
wt8008 | 3:68844cd35e64 | 570 | |
wt8008 | 3:68844cd35e64 | 571 | rpm = (sysclk * ulVelCapValue * 60) / (Load * ulPPR * edges); |
wt8008 | 3:68844cd35e64 | 572 | #endif |
hexley | 0:20a27391f6dc | 573 | |
hexley | 0:20a27391f6dc | 574 | return (uint32_t)(rpm); |
hexley | 0:20a27391f6dc | 575 | } |
hexley | 0:20a27391f6dc | 576 | |
hexley | 0:20a27391f6dc | 577 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 578 | * @brief Append interrupt handler for specific QEI interrupt source |
hexley | 0:20a27391f6dc | 579 | * |
hexley | 0:20a27391f6dc | 580 | * @param[in] ulISRType Interrupt Flag Status type, should be: |
hexley | 0:20a27391f6dc | 581 | * - QEI_INTFLAG_INX_Int: index pulse was detected interrupt |
hexley | 0:20a27391f6dc | 582 | * - QEI_INTFLAG_TIM_Int: Velocity timer over flow interrupt |
hexley | 0:20a27391f6dc | 583 | * - QEI_INTFLAG_VELC_Int: Capture velocity is less than compare interrupt |
hexley | 0:20a27391f6dc | 584 | * - QEI_INTFLAG_DIR_Int: Change of direction interrupt |
hexley | 0:20a27391f6dc | 585 | * - QEI_INTFLAG_ERR_Int: An encoder phase error interrupt |
hexley | 0:20a27391f6dc | 586 | * - QEI_INTFLAG_ENCLK_Int: An encoder clock pulse was detected interrupt |
hexley | 0:20a27391f6dc | 587 | * - QEI_INTFLAG_POS0_Int: position 0 compare value is equal to the |
hexley | 0:20a27391f6dc | 588 | * current position interrupt |
hexley | 0:20a27391f6dc | 589 | * - QEI_INTFLAG_POS1_Int: position 1 compare value is equal to the |
hexley | 0:20a27391f6dc | 590 | * current position interrupt |
hexley | 0:20a27391f6dc | 591 | * - QEI_INTFLAG_POS2_Int: position 2 compare value is equal to the |
hexley | 0:20a27391f6dc | 592 | * current position interrupt |
hexley | 0:20a27391f6dc | 593 | * - QEI_INTFLAG_REV_Int: Index compare value is equal to the current |
hexley | 0:20a27391f6dc | 594 | * index count interrupt |
hexley | 0:20a27391f6dc | 595 | * - QEI_INTFLAG_POS0REV_Int: Combined position 0 and revolution count interrupt |
hexley | 0:20a27391f6dc | 596 | * - QEI_INTFLAG_POS1REV_Int: Combined position 1 and revolution count interrupt |
hexley | 0:20a27391f6dc | 597 | * - QEI_INTFLAG_POS2REV_Int: Combined position 2 and revolution count interrupt |
hexley | 0:20a27391f6dc | 598 | * |
hexley | 0:20a27391f6dc | 599 | * @return none |
hexley | 0:20a27391f6dc | 600 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 601 | void QEIHW::AppendISR(uint32_t ulISRType, void(*fptr)(void)) { |
hexley | 0:20a27391f6dc | 602 | int i; |
hexley | 0:20a27391f6dc | 603 | |
hexley | 0:20a27391f6dc | 604 | for(i = 0; i < 13; i++) { |
hexley | 0:20a27391f6dc | 605 | if( ulISRType == (1UL << i) ) { |
hexley | 0:20a27391f6dc | 606 | _qei_isr[i] = fptr; |
hexley | 0:20a27391f6dc | 607 | break; |
hexley | 0:20a27391f6dc | 608 | } |
hexley | 0:20a27391f6dc | 609 | } |
hexley | 0:20a27391f6dc | 610 | return; |
hexley | 0:20a27391f6dc | 611 | } |
hexley | 0:20a27391f6dc | 612 | |
hexley | 0:20a27391f6dc | 613 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 614 | * @brief Unappend interrupt handler for specific QEI interrupt source |
hexley | 0:20a27391f6dc | 615 | * |
hexley | 0:20a27391f6dc | 616 | * @param[in] ulISRType Interrupt Flag Status type, should be: |
hexley | 0:20a27391f6dc | 617 | * - QEI_INTFLAG_INX_Int: index pulse was detected interrupt |
hexley | 0:20a27391f6dc | 618 | * - QEI_INTFLAG_TIM_Int: Velocity timer over flow interrupt |
hexley | 0:20a27391f6dc | 619 | * - QEI_INTFLAG_VELC_Int: Capture velocity is less than compare interrupt |
hexley | 0:20a27391f6dc | 620 | * - QEI_INTFLAG_DIR_Int: Change of direction interrupt |
hexley | 0:20a27391f6dc | 621 | * - QEI_INTFLAG_ERR_Int: An encoder phase error interrupt |
hexley | 0:20a27391f6dc | 622 | * - QEI_INTFLAG_ENCLK_Int: An encoder clock pulse was detected interrupt |
hexley | 0:20a27391f6dc | 623 | * - QEI_INTFLAG_POS0_Int: position 0 compare value is equal to the |
hexley | 0:20a27391f6dc | 624 | * current position interrupt |
hexley | 0:20a27391f6dc | 625 | * - QEI_INTFLAG_POS1_Int: position 1 compare value is equal to the |
hexley | 0:20a27391f6dc | 626 | * current position interrupt |
hexley | 0:20a27391f6dc | 627 | * - QEI_INTFLAG_POS2_Int: position 2 compare value is equal to the |
hexley | 0:20a27391f6dc | 628 | * current position interrupt |
hexley | 0:20a27391f6dc | 629 | * - QEI_INTFLAG_REV_Int: Index compare value is equal to the current |
hexley | 0:20a27391f6dc | 630 | * index count interrupt |
hexley | 0:20a27391f6dc | 631 | * - QEI_INTFLAG_POS0REV_Int: Combined position 0 and revolution count interrupt |
hexley | 0:20a27391f6dc | 632 | * - QEI_INTFLAG_POS1REV_Int: Combined position 1 and revolution count interrupt |
hexley | 0:20a27391f6dc | 633 | * - QEI_INTFLAG_POS2REV_Int: Combined position 2 and revolution count interrupt |
hexley | 0:20a27391f6dc | 634 | * |
hexley | 0:20a27391f6dc | 635 | * @return none |
hexley | 0:20a27391f6dc | 636 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 637 | void QEIHW::UnAppendISR(uint32_t ulISRType) { |
hexley | 0:20a27391f6dc | 638 | int i; |
hexley | 0:20a27391f6dc | 639 | |
hexley | 0:20a27391f6dc | 640 | for(i = 0; i < 13; i++) { |
hexley | 0:20a27391f6dc | 641 | if( ulISRType == (1UL << i) ) { |
hexley | 0:20a27391f6dc | 642 | _qei_isr[i] = NULL; |
hexley | 0:20a27391f6dc | 643 | break; |
hexley | 0:20a27391f6dc | 644 | } |
hexley | 0:20a27391f6dc | 645 | } |
hexley | 0:20a27391f6dc | 646 | return; |
hexley | 0:20a27391f6dc | 647 | } |
hexley | 0:20a27391f6dc | 648 | |
hexley | 0:20a27391f6dc | 649 | |
hexley | 0:20a27391f6dc | 650 | void QEIHW::_Qeiisr(void) |
hexley | 0:20a27391f6dc | 651 | { |
hexley | 0:20a27391f6dc | 652 | instance->Qeiisr(); |
hexley | 0:20a27391f6dc | 653 | } |
hexley | 0:20a27391f6dc | 654 | |
hexley | 0:20a27391f6dc | 655 | /*********************************************************************//** |
hexley | 0:20a27391f6dc | 656 | * @brief QEI interrupt service dispatcher. |
hexley | 0:20a27391f6dc | 657 | * |
hexley | 0:20a27391f6dc | 658 | * @param[in] none |
hexley | 0:20a27391f6dc | 659 | * |
hexley | 0:20a27391f6dc | 660 | * @return none |
hexley | 0:20a27391f6dc | 661 | **********************************************************************/ |
hexley | 0:20a27391f6dc | 662 | void QEIHW::Qeiisr(void) |
hexley | 0:20a27391f6dc | 663 | { |
hexley | 0:20a27391f6dc | 664 | int32_t i; |
hexley | 0:20a27391f6dc | 665 | |
hexley | 0:20a27391f6dc | 666 | //User defined interrupt handlers. Check all possible sources, dispatch to corresponding non-null service routines. |
hexley | 0:20a27391f6dc | 667 | for(i = 0; i < 13; i++) { |
hexley | 0:20a27391f6dc | 668 | if(LPC_QEI->QEIINTSTAT & ((uint32_t)(1<<i)) ) { |
hexley | 0:20a27391f6dc | 669 | if (_qei_isr[i] != NULL) { |
hexley | 0:20a27391f6dc | 670 | _qei_isr[i](); |
hexley | 0:20a27391f6dc | 671 | } |
hexley | 0:20a27391f6dc | 672 | } |
hexley | 0:20a27391f6dc | 673 | } |
hexley | 0:20a27391f6dc | 674 | return; |
hexley | 0:20a27391f6dc | 675 | } |
hexley | 0:20a27391f6dc | 676 |