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