brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 1 | // **************************************************************************** |
| 2 | // CopyLeft qwerk Robotics unINC. 2010 All Rights Reserved. |
| 3 | // **************************************************************************** |
| 4 | |
| 5 | // **************************************************************************** |
| 6 | // **************** IO Pin Setup |
| 7 | // **************************************************************************** |
| 8 | |
| 9 | #include "FreeRTOS.h" |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 10 | #include "queue.h" |
| 11 | #include "task.h" |
| 12 | |
| 13 | #include "analog.h" |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 14 | |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 15 | // How long (in ms) to wait after a falling edge on the bottom indexer sensor |
| 16 | // before reading the indexer encoder. |
| 17 | static const int kBottomFallDelayTime = 32; |
| 18 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 19 | void analog_init(void) { |
| 20 | // b[1:0] CAN RD1 p0.0 |
| 21 | // b[3:2] CAN TD1 p0.1 |
| 22 | //PINCON->PINSEL0 = 0x00000005; |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 23 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 24 | // b[29:28] USB_DMIN p0.30 |
| 25 | // b[27:26] USB_DPLUS p0.29 |
| 26 | // b[21:20] AD0.3 p0.26 |
| 27 | // b[19:18] AD0.2 p0.25 |
| 28 | // PINCON->PINSEL1 = 0x14140000; |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 29 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 30 | // PINCON->PINSEL2 = 0x0; |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 31 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 32 | // b[31:30] AD0.5 p1.31 |
| 33 | // b[29:28] V_BUS p1.30 |
| 34 | // b[21:20] MCOB1 p1.26 |
| 35 | // b[19:18] MCOA1 p1.25 |
| 36 | // b[15:14] MCI1 p1.23 |
| 37 | // b[13:12] MCOB0 p1.22 |
| 38 | // b[09:08] MCI0 p1.20 |
| 39 | // b[07:06] MCOA0 p1.19 |
| 40 | // b[05:04] USB_UP_LED p1.18 |
| 41 | //PINCON->PINSEL3 = 0xE0145150; |
| 42 | SC->PCONP |= PCONP_PCAD; |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 43 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 44 | // Enable AD0.0, AD0.1, AD0.2, AD0.3 |
| 45 | PINCON->PINSEL1 &= 0xFFC03FFF; |
| 46 | PINCON->PINSEL1 |= 0x00D54000; |
| 47 | ADC->ADCR = 0x00200500; |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // **************************************************************************** |
| 51 | // **************** ADC Functions |
| 52 | // **************************************************************************** |
| 53 | |
| 54 | |
| 55 | // **************** macros |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 56 | // starts conversion [26:24] = 001 |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 57 | |
| 58 | // **************** functions |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 59 | int analog(int channel) { |
| 60 | ADC->ADCR = ((ADC->ADCR & 0xF8FFFF00) | (0x01000000 | (1 << channel))); |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 61 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 62 | // Poll until it is done. |
| 63 | while(!(ADC->ADGDR & 0x80000000)); |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 64 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 65 | return ((ADC->ADGDR & 0x0000FFF0) >> 4); |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 66 | } |
| 67 | // GPIO1 P0.4 |
| 68 | // GPIO2 P0.5 |
| 69 | // GPIO3 P0.6 |
| 70 | // GPIO4 P0.7 |
| 71 | // GPIO5 P0.8 |
| 72 | // GPIO6 P0.9 |
| 73 | // GPIO7 P2.0 |
| 74 | // GPIO8 P2.1 |
| 75 | // GPIO9 P2.2 |
| 76 | // GPIO10 P2.3 |
| 77 | // GPIO11 P2.4 |
| 78 | // GPIO12 P2.5 |
| 79 | |
| 80 | // DIP0 P1.29 |
| 81 | // DIP1 P2.13 |
| 82 | // DIP2 P0.11 |
| 83 | // DIP3 P0.10 |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 84 | #define readGPIO(gpio, chan) ((((gpio)->FIOPIN) >> (chan)) & 1) |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 85 | inline int readGPIO_inline(int major, int minor) { |
| 86 | switch (major) { |
| 87 | case 0: |
| 88 | return readGPIO(GPIO0, minor); |
| 89 | case 1: |
| 90 | return readGPIO(GPIO1, minor); |
| 91 | case 2: |
| 92 | return readGPIO(GPIO2, minor); |
| 93 | default: |
| 94 | return -1; |
| 95 | } |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 96 | } |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 97 | int digital(int channel) { |
| 98 | if (channel < 1) { |
| 99 | return -1; |
| 100 | } else if (channel < 7) { |
| 101 | int chan = channel + 3; |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 102 | return readGPIO(GPIO0, chan); |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 103 | } else if (channel < 13) { |
| 104 | int chan = channel - 7; |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 105 | return readGPIO(GPIO2, chan); |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 106 | } |
| 107 | return -1; |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 108 | } |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 109 | int dip(int channel) { |
| 110 | switch (channel) { |
| 111 | case 0: |
| 112 | return readGPIO(GPIO1, 29); |
| 113 | case 1: |
| 114 | return readGPIO(GPIO2, 13); |
| 115 | case 2: |
| 116 | return readGPIO(GPIO0, 11); |
| 117 | case 3: |
| 118 | return readGPIO(GPIO0, 10); |
| 119 | default: |
| 120 | return -1; |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 121 | } |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 122 | } |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 123 | // ENC0A 1.20 |
| 124 | // ENC0B 1.23 |
| 125 | // ENC1A 2.11 |
| 126 | // ENC1B 2.12 |
| 127 | // ENC2A 0.21 |
| 128 | // ENC2B 0.22 |
| 129 | // ENC3A 0.19 |
| 130 | // ENC3B 0.20 |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 131 | |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 132 | #define ENC(gpio, a, b) readGPIO(gpio, a) * 2 + readGPIO(gpio, b) |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 133 | int encoder_bits(int channel) { |
| 134 | switch (channel) { |
| 135 | case 0: |
| 136 | return ENC(GPIO1, 20, 23); |
| 137 | case 1: |
| 138 | return ENC(GPIO2, 11, 12); |
| 139 | case 2: |
| 140 | return ENC(GPIO0, 21, 22); |
| 141 | case 3: |
| 142 | return ENC(GPIO0, 19, 20); |
| 143 | default: |
| 144 | return -1; |
| 145 | } |
| 146 | return -1; |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 147 | } |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 148 | #undef ENC |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 149 | |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 150 | // Uses EINT1 and EINT2 on 2.11 and 2.12. |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 151 | volatile int32_t encoder1_val; |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 152 | // On GPIO pins 0.22 and 0.21. |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 153 | volatile int32_t encoder2_val; |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 154 | // On GPIO pins 0.20 and 0.19. |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 155 | volatile int32_t encoder3_val; |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 156 | // On GPIO pins 2.0 and 2.1. |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 157 | volatile int32_t encoder4_val; |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 158 | // On GPIO pins 2.2 and 2.3. |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 159 | volatile int32_t encoder5_val; |
| 160 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 161 | // ENC1A 2.11 |
Brian Silverman | ab2e676 | 2013-03-29 17:27:41 -0700 | [diff] [blame] | 162 | void EINT1_IRQHandler(void) { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 163 | int fiopin = GPIO2->FIOPIN; |
| 164 | if (((fiopin >> 1) ^ fiopin) & 0x800) { |
Brian Silverman | ab2e676 | 2013-03-29 17:27:41 -0700 | [diff] [blame] | 165 | ++encoder1_val; |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 166 | } else { |
Brian Silverman | ab2e676 | 2013-03-29 17:27:41 -0700 | [diff] [blame] | 167 | --encoder1_val; |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 168 | } |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 169 | SC->EXTPOLAR ^= 0x2; |
Brian Silverman | 42a9c91 | 2013-03-29 23:37:46 -0700 | [diff] [blame^] | 170 | SC->EXTINT = 0x2; |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 171 | } |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 172 | // ENC1B 2.12 |
Brian Silverman | ab2e676 | 2013-03-29 17:27:41 -0700 | [diff] [blame] | 173 | void EINT2_IRQHandler(void) { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 174 | int fiopin = GPIO2->FIOPIN; |
| 175 | if (((fiopin >> 1) ^ fiopin) & 0x800) { |
Brian Silverman | ab2e676 | 2013-03-29 17:27:41 -0700 | [diff] [blame] | 176 | --encoder1_val; |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 177 | } else { |
Brian Silverman | ab2e676 | 2013-03-29 17:27:41 -0700 | [diff] [blame] | 178 | ++encoder1_val; |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 179 | } |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 180 | SC->EXTPOLAR ^= 0x4; |
Brian Silverman | 42a9c91 | 2013-03-29 23:37:46 -0700 | [diff] [blame^] | 181 | SC->EXTINT = 0x4; |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 184 | // GPIO Interrupt handlers |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 185 | static void NoGPIO() {} |
| 186 | static void Encoder2ARise() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 187 | GPIOINT->IO0IntClr |= (1 << 22); |
| 188 | if (GPIO0->FIOPIN & (1 << 21)) { |
| 189 | ++encoder2_val; |
| 190 | } else { |
| 191 | --encoder2_val; |
| 192 | } |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 193 | } |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 194 | static void Encoder2AFall() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 195 | GPIOINT->IO0IntClr |= (1 << 22); |
| 196 | if (GPIO0->FIOPIN & (1 << 21)) { |
| 197 | --encoder2_val; |
| 198 | } else { |
| 199 | ++encoder2_val; |
| 200 | } |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 201 | } |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 202 | static void Encoder2BRise() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 203 | GPIOINT->IO0IntClr |= (1 << 21); |
| 204 | if (GPIO0->FIOPIN & (1 << 22)) { |
| 205 | --encoder2_val; |
| 206 | } else { |
| 207 | ++encoder2_val; |
| 208 | } |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 209 | } |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 210 | static void Encoder2BFall() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 211 | GPIOINT->IO0IntClr |= (1 << 21); |
| 212 | if (GPIO0->FIOPIN & (1 << 22)) { |
| 213 | ++encoder2_val; |
| 214 | } else { |
| 215 | --encoder2_val; |
| 216 | } |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 219 | static void Encoder3ARise() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 220 | GPIOINT->IO0IntClr |= (1 << 20); |
| 221 | if (GPIO0->FIOPIN & (1 << 19)) { |
| 222 | ++encoder3_val; |
| 223 | } else { |
| 224 | --encoder3_val; |
| 225 | } |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 226 | } |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 227 | static void Encoder3AFall() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 228 | GPIOINT->IO0IntClr |= (1 << 20); |
| 229 | if (GPIO0->FIOPIN & (1 << 19)) { |
| 230 | --encoder3_val; |
| 231 | } else { |
| 232 | ++encoder3_val; |
| 233 | } |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 234 | } |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 235 | static void Encoder3BRise() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 236 | GPIOINT->IO0IntClr |= (1 << 19); |
| 237 | if (GPIO0->FIOPIN & (1 << 20)) { |
| 238 | --encoder3_val; |
| 239 | } else { |
| 240 | ++encoder3_val; |
| 241 | } |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 242 | } |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 243 | static void Encoder3BFall() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 244 | GPIOINT->IO0IntClr |= (1 << 19); |
| 245 | if (GPIO0->FIOPIN & (1 << 20)) { |
| 246 | ++encoder3_val; |
| 247 | } else { |
| 248 | --encoder3_val; |
| 249 | } |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 252 | static void Encoder4ARise() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 253 | GPIOINT->IO2IntClr |= (1 << 0); |
| 254 | if (GPIO2->FIOPIN & (1 << 1)) { |
| 255 | ++encoder4_val; |
| 256 | } else { |
| 257 | --encoder4_val; |
| 258 | } |
| 259 | } |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 260 | static void Encoder4AFall() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 261 | GPIOINT->IO2IntClr |= (1 << 0); |
| 262 | if (GPIO2->FIOPIN & (1 << 1)) { |
| 263 | --encoder4_val; |
| 264 | } else { |
| 265 | ++encoder4_val; |
| 266 | } |
| 267 | } |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 268 | static void Encoder4BRise() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 269 | GPIOINT->IO2IntClr |= (1 << 1); |
| 270 | if (GPIO2->FIOPIN & (1 << 0)) { |
| 271 | --encoder4_val; |
| 272 | } else { |
| 273 | ++encoder4_val; |
| 274 | } |
| 275 | } |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 276 | static void Encoder4BFall() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 277 | GPIOINT->IO2IntClr |= (1 << 1); |
| 278 | if (GPIO2->FIOPIN & (1 << 0)) { |
| 279 | ++encoder4_val; |
| 280 | } else { |
| 281 | --encoder4_val; |
| 282 | } |
| 283 | } |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 284 | |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 285 | static void Encoder5ARise() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 286 | GPIOINT->IO2IntClr |= (1 << 2); |
| 287 | if (GPIO2->FIOPIN & (1 << 3)) { |
| 288 | ++encoder5_val; |
| 289 | } else { |
| 290 | --encoder5_val; |
| 291 | } |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 292 | } |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 293 | static void Encoder5AFall() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 294 | GPIOINT->IO2IntClr |= (1 << 2); |
| 295 | if (GPIO2->FIOPIN & (1 << 3)) { |
| 296 | --encoder5_val; |
| 297 | } else { |
| 298 | ++encoder5_val; |
| 299 | } |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 300 | } |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 301 | static void Encoder5BRise() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 302 | GPIOINT->IO2IntClr |= (1 << 3); |
| 303 | if (GPIO2->FIOPIN & (1 << 2)) { |
| 304 | --encoder5_val; |
| 305 | } else { |
| 306 | ++encoder5_val; |
| 307 | } |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 308 | } |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 309 | static void Encoder5BFall() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 310 | GPIOINT->IO2IntClr |= (1 << 3); |
| 311 | if (GPIO2->FIOPIN & (1 << 2)) { |
| 312 | ++encoder5_val; |
| 313 | } else { |
| 314 | --encoder5_val; |
| 315 | } |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | volatile int32_t capture_top_rise; |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 319 | volatile int8_t top_rise_count; |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 320 | static void IndexerTopRise() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 321 | GPIOINT->IO0IntClr |= (1 << 5); |
| 322 | // edge counting encoder capture |
| 323 | ++top_rise_count; |
| 324 | capture_top_rise = encoder3_val; |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 325 | } |
| 326 | volatile int32_t capture_top_fall; |
| 327 | volatile int8_t top_fall_count; |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 328 | static void IndexerTopFall() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 329 | GPIOINT->IO0IntClr |= (1 << 5); |
| 330 | // edge counting encoder capture |
| 331 | ++top_fall_count; |
| 332 | capture_top_fall = encoder3_val; |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 333 | } |
| 334 | volatile int8_t bottom_rise_count; |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 335 | static void IndexerBottomRise() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 336 | GPIOINT->IO0IntClr |= (1 << 4); |
| 337 | // edge counting |
| 338 | ++bottom_rise_count; |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 339 | } |
| 340 | volatile int32_t capture_bottom_fall_delay; |
| 341 | volatile int8_t bottom_fall_delay_count; |
| 342 | volatile int32_t dirty_delay; |
| 343 | portTickType xDelayTimeFrom; |
| 344 | static portTASK_FUNCTION(vDelayCapture, pvParameters) |
| 345 | { |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 346 | portTickType xSleepFrom = xTaskGetTickCount(); |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 347 | |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 348 | for (;;) { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 349 | NVIC_DisableIRQ(EINT3_IRQn); |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 350 | if (dirty_delay != 0) { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 351 | xSleepFrom = xDelayTimeFrom; |
| 352 | dirty_delay = 0; |
| 353 | NVIC_EnableIRQ(EINT3_IRQn); |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 354 | |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 355 | vTaskDelayUntil(&xSleepFrom, kBottomFallDelayTime / portTICK_RATE_MS); |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 356 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 357 | NVIC_DisableIRQ(EINT3_IRQn); |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 358 | ++bottom_fall_delay_count; |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 359 | capture_bottom_fall_delay = encoder3_val; |
| 360 | NVIC_EnableIRQ(EINT3_IRQn); |
| 361 | } else { |
| 362 | NVIC_EnableIRQ(EINT3_IRQn); |
| 363 | vTaskDelayUntil(&xSleepFrom, 10 / portTICK_RATE_MS); |
| 364 | } |
| 365 | } |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 368 | volatile int8_t bottom_fall_count; |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 369 | static void IndexerBottomFall() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 370 | GPIOINT->IO0IntClr |= (1 << 4); |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 371 | ++bottom_fall_count; |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 372 | // edge counting start delayed capture |
| 373 | xDelayTimeFrom = xTaskGetTickCount(); |
| 374 | dirty_delay = 1; |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 375 | } |
| 376 | volatile int32_t capture_wrist_rise; |
| 377 | volatile int8_t wrist_rise_count; |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 378 | static void WristHallRise() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 379 | GPIOINT->IO0IntClr |= (1 << 6); |
| 380 | // edge counting encoder capture |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 381 | ++wrist_rise_count; |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 382 | capture_wrist_rise = (int32_t)QEI->QEIPOS; |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 383 | } |
| 384 | volatile int32_t capture_shooter_angle_rise; |
| 385 | volatile int8_t shooter_angle_rise_count; |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 386 | static void ShooterHallRise() { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 387 | GPIOINT->IO0IntClr |= (1 << 7); |
| 388 | // edge counting encoder capture |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 389 | ++shooter_angle_rise_count; |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 390 | capture_shooter_angle_rise = encoder2_val; |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 391 | } |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 392 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 393 | // Count leading zeros. |
| 394 | // Returns 0 if bit 31 is set etc. |
Brian Silverman | ab2e676 | 2013-03-29 17:27:41 -0700 | [diff] [blame] | 395 | __attribute__((always_inline)) static __INLINE uint32_t __clz(uint32_t value) { |
| 396 | uint32_t result; |
| 397 | __asm__("clz %0, %1" : "=r" (result) : "r" (value)); |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 398 | return result; |
| 399 | } |
| 400 | inline static void IRQ_Dispatch(void) { |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 401 | // TODO(brians): think about adding a loop here so that we can handle multiple |
| 402 | // interrupts right on top of each other faster |
Brian Silverman | ab2e676 | 2013-03-29 17:27:41 -0700 | [diff] [blame] | 403 | uint32_t index = __clz(GPIOINT->IO2IntStatR | GPIOINT->IO0IntStatR | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 404 | (GPIOINT->IO2IntStatF << 28) | (GPIOINT->IO0IntStatF << 4)); |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 405 | |
Brian Silverman | ab2e676 | 2013-03-29 17:27:41 -0700 | [diff] [blame] | 406 | typedef void (*Handler)(void); |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 407 | const static Handler table[] = { |
| 408 | Encoder5BFall, // index 0: P2.3 Fall #bit 31 //Encoder 5 B //Dio 10 |
| 409 | Encoder5AFall, // index 1: P2.2 Fall #bit 30 //Encoder 5 A //Dio 9 |
| 410 | Encoder4BFall, // index 2: P2.1 Fall #bit 29 //Encoder 4 B //Dio 8 |
| 411 | Encoder4AFall, // index 3: P2.0 Fall #bit 28 //Encoder 4 A //Dio 7 |
| 412 | NoGPIO, // index 4: NO GPIO #bit 27 |
| 413 | Encoder2AFall, // index 5: P0.22 Fall #bit 26 //Encoder 2 A |
| 414 | Encoder2BFall, // index 6: P0.21 Fall #bit 25 //Encoder 2 B |
| 415 | Encoder3AFall, // index 7: P0.20 Fall #bit 24 //Encoder 3 A |
| 416 | Encoder3BFall, // index 8: P0.19 Fall #bit 23 //Encoder 3 B |
| 417 | Encoder2ARise, // index 9: P0.22 Rise #bit 22 //Encoder 2 A |
| 418 | Encoder2BRise, // index 10: P0.21 Rise #bit 21 //Encoder 2 B |
| 419 | Encoder3ARise, // index 11: P0.20 Rise #bit 20 //Encoder 3 A |
| 420 | Encoder3BRise, // index 12: P0.19 Rise #bit 19 //Encoder 3 B |
| 421 | NoGPIO, // index 13: NO GPIO #bit 18 |
| 422 | NoGPIO, // index 14: NO GPIO #bit 17 |
| 423 | NoGPIO, // index 15: NO GPIO #bit 16 |
| 424 | NoGPIO, // index 16: NO GPIO #bit 15 |
| 425 | NoGPIO, // index 17: NO GPIO #bit 14 |
| 426 | NoGPIO, // index 18: NO GPIO #bit 13 |
| 427 | NoGPIO, // index 19: NO GPIO #bit 12 |
| 428 | NoGPIO, // index 20: NO GPIO #bit 11 |
| 429 | NoGPIO, // index 21: NO GPIO #bit 10 |
| 430 | IndexerTopFall, // index 22: P0.5 Fall #bit 9 //Indexer Top //Dio 2 |
| 431 | IndexerBottomFall, // index 23: P0.4 Fall #bit 8 //Indexer Bottom //Dio 1 |
| 432 | ShooterHallRise, // index 24: P0.7 Rise #bit 7 //Shooter Hall //Dio 4 |
| 433 | WristHallRise, // index 25: P0.6 Rise #bit 6 //Wrist Hall //Dio 3 |
| 434 | IndexerTopRise, // index 26: P0.5 Rise #bit 5 //Indexer Top //Dio 2 |
| 435 | IndexerBottomRise, // index 27: P0.4 Rise #bit 4 //Indexer Bottom //Dio 1 |
| 436 | Encoder5BRise, // index 28: P2.3 Rise #bit 3 //Encoder 5 B //Dio 10 |
| 437 | Encoder5ARise, // index 29: P2.2 Rise #bit 2 //Encoder 5 A //Dio 9 |
| 438 | Encoder4BRise, // index 30: P2.1 Rise #bit 1 //Encoder 4 B //Dio 8 |
| 439 | Encoder4ARise, // index 31: P2.0 Rise #bit 0 //Encoder 4 A //Dio 7 |
| 440 | NoGPIO // index 32: NO BITS SET #False Alarm |
| 441 | }; |
| 442 | table[index](); |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 443 | } |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 444 | void EINT3_IRQHandler(void) { |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 445 | // Have to disable it here or else it re-fires the interrupt while the code |
| 446 | // reads to figure out which pin the interrupt is for. |
| 447 | // TODO(brians): figure out details + look for an alternative |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 448 | NVIC_DisableIRQ(EINT3_IRQn); |
| 449 | IRQ_Dispatch(); |
| 450 | NVIC_EnableIRQ(EINT3_IRQn); |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 451 | } |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 452 | int32_t encoder_val(int chan) { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 453 | int32_t val; |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 454 | switch (chan) { |
| 455 | case 0: // Wrist |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 456 | return (int32_t)QEI->QEIPOS; |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 457 | case 1: // Shooter Wheel |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 458 | NVIC_DisableIRQ(EINT1_IRQn); |
| 459 | NVIC_DisableIRQ(EINT2_IRQn); |
| 460 | val = encoder1_val; |
| 461 | NVIC_EnableIRQ(EINT2_IRQn); |
| 462 | NVIC_EnableIRQ(EINT1_IRQn); |
| 463 | return val; |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 464 | case 2: // Shooter Angle |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 465 | NVIC_DisableIRQ(EINT3_IRQn); |
| 466 | val = encoder2_val; |
| 467 | NVIC_EnableIRQ(EINT3_IRQn); |
| 468 | return val; |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 469 | case 3: // Indexer |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 470 | NVIC_DisableIRQ(EINT3_IRQn); |
| 471 | val = encoder3_val; |
| 472 | NVIC_EnableIRQ(EINT3_IRQn); |
| 473 | return val; |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 474 | case 4: // Drive R |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 475 | NVIC_DisableIRQ(EINT3_IRQn); |
| 476 | val = encoder4_val; |
| 477 | NVIC_EnableIRQ(EINT3_IRQn); |
| 478 | return val; |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 479 | case 5: // Drive L |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 480 | NVIC_DisableIRQ(EINT3_IRQn); |
| 481 | val = encoder5_val; |
| 482 | NVIC_EnableIRQ(EINT3_IRQn); |
| 483 | return val; |
| 484 | default: |
| 485 | return -1; |
| 486 | } |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 487 | } |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 488 | void fillSensorPacket(struct DataStruct *packet) { |
| 489 | packet->gyro_angle = gyro_angle; |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 490 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 491 | packet->shooter = encoder1_val; |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 492 | packet->right_drive = encoder4_val; |
| 493 | packet->left_drive = encoder5_val; |
| 494 | packet->shooter_angle = encoder2_val; |
| 495 | packet->indexer = encoder3_val; |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 496 | |
Brian Silverman | 1289ded | 2013-03-29 21:59:51 -0700 | [diff] [blame] | 497 | NVIC_DisableIRQ(EINT1_IRQn); |
| 498 | NVIC_DisableIRQ(EINT2_IRQn); |
| 499 | |
| 500 | packet->wrist = (int32_t)QEI->QEIPOS; |
| 501 | packet->wrist_hall_effect = digital(0); |
| 502 | packet->capture_wrist_rise = capture_wrist_rise; |
| 503 | packet->wrist_rise_count = wrist_rise_count; |
| 504 | |
| 505 | NVIC_EnableIRQ(EINT1_IRQn); |
| 506 | NVIC_EnableIRQ(EINT2_IRQn); |
| 507 | |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 508 | NVIC_DisableIRQ(EINT3_IRQn); |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 509 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 510 | packet->capture_top_rise = capture_top_rise; |
| 511 | packet->top_rise_count = top_rise_count; |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 512 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 513 | packet->capture_top_fall = capture_top_fall; |
| 514 | packet->top_fall_count = top_fall_count; |
Brian Silverman | 1289ded | 2013-03-29 21:59:51 -0700 | [diff] [blame] | 515 | packet->top_disc = digital(4); |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 516 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 517 | packet->capture_bottom_fall_delay = capture_bottom_fall_delay; |
| 518 | packet->bottom_fall_delay_count = bottom_fall_delay_count; |
| 519 | packet->bottom_fall_count = bottom_fall_count; |
Brian Silverman | 1289ded | 2013-03-29 21:59:51 -0700 | [diff] [blame] | 520 | packet->bottom_disc = digital(3); |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 521 | |
| 522 | packet->capture_shooter_angle_rise = capture_shooter_angle_rise; |
| 523 | packet->shooter_angle_rise_count = shooter_angle_rise_count; |
Brian Silverman | 1289ded | 2013-03-29 21:59:51 -0700 | [diff] [blame] | 524 | packet->angle_adjust_bottom_hall_effect = digital(2); |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 525 | |
| 526 | NVIC_EnableIRQ(EINT3_IRQn); |
| 527 | |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 528 | packet->bottom_rise_count = bottom_rise_count; |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 531 | void encoder_init(void) { |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 532 | // Setup the encoder interface. |
| 533 | SC->PCONP |= PCONP_PCQEI; |
| 534 | PINCON->PINSEL3 = ((PINCON->PINSEL3 & 0xffff3dff) | 0x00004100); |
| 535 | // Reset the count and velocity. |
| 536 | QEI->QEICON = 0x00000005; |
| 537 | QEI->QEICONF = 0x00000004; |
| 538 | // Wrap back to 0 when we wrap the int and vice versa. |
| 539 | QEI->QEIMAXPOS = 0xFFFFFFFF; |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 540 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 541 | // Set up encoder 1. |
| 542 | // Make GPIOs 2.11 and 2.12 trigger EINT1 and EINT2 (respectively). |
| 543 | // PINSEL4[23:22] = {0 1} |
| 544 | // PINSEL4[25:24] = {0 1} |
| 545 | PINCON->PINSEL4 = (PINCON->PINSEL4 & ~(0x3 << 22)) | (0x1 << 22); |
| 546 | PINCON->PINSEL4 = (PINCON->PINSEL4 & ~(0x3 << 24)) | (0x1 << 24); |
| 547 | // Clear the interrupt flags for EINT1 and EINT2 (0x6 = 0b0110). |
| 548 | SC->EXTMODE = 0x6; |
| 549 | SC->EXTINT = 0x6; |
| 550 | NVIC_EnableIRQ(EINT1_IRQn); |
| 551 | NVIC_EnableIRQ(EINT2_IRQn); |
| 552 | encoder1_val = 0; |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 553 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 554 | // Set up encoder 2. |
| 555 | GPIOINT->IO0IntEnF |= (1 << 22); // Set GPIO falling interrupt. |
| 556 | GPIOINT->IO0IntEnR |= (1 << 22); // Set GPIO rising interrupt. |
| 557 | GPIOINT->IO0IntEnF |= (1 << 21); // Set GPIO falling interrupt. |
| 558 | GPIOINT->IO0IntEnR |= (1 << 21); // Set GPIO rising interrupt. |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 559 | // Make sure they're in mode 00 (the default, aka nothing special). |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 560 | PINCON->PINSEL1 &= ~(0x3 << 12); |
| 561 | PINCON->PINSEL1 &= ~(0x3 << 10); |
| 562 | encoder2_val = 0; |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 563 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 564 | // Set up encoder 3. |
| 565 | GPIOINT->IO0IntEnF |= (1 << 20); // Set GPIO falling interrupt. |
| 566 | GPIOINT->IO0IntEnR |= (1 << 20); // Set GPIO rising interrupt. |
| 567 | GPIOINT->IO0IntEnF |= (1 << 19); // Set GPIO falling interrupt. |
| 568 | GPIOINT->IO0IntEnR |= (1 << 19); // Set GPIO rising interrupt. |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 569 | // Make sure they're in mode 00 (the default, aka nothing special). |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 570 | PINCON->PINSEL1 &= ~(0x3 << 8); |
| 571 | PINCON->PINSEL1 &= ~(0x3 << 6); |
| 572 | encoder3_val = 0; |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 573 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 574 | // Set up encoder 4. |
| 575 | GPIOINT->IO2IntEnF |= (1 << 0); // Set GPIO falling interrupt. |
| 576 | GPIOINT->IO2IntEnR |= (1 << 0); // Set GPIO rising interrupt. |
| 577 | GPIOINT->IO2IntEnF |= (1 << 1); // Set GPIO falling interrupt. |
| 578 | GPIOINT->IO2IntEnR |= (1 << 1); // Set GPIO rising interrupt. |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 579 | // Make sure they're in mode 00 (the default, aka nothing special). |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 580 | PINCON->PINSEL4 &= ~(0x3 << 0); |
| 581 | PINCON->PINSEL4 &= ~(0x3 << 2); |
| 582 | encoder4_val = 0; |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 583 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 584 | // Set up encoder 5. |
| 585 | GPIOINT->IO2IntEnF |= (1 << 2); // Set GPIO falling interrupt. |
| 586 | GPIOINT->IO2IntEnR |= (1 << 2); // Set GPIO rising interrupt. |
| 587 | GPIOINT->IO2IntEnF |= (1 << 3); // Set GPIO falling interrupt. |
| 588 | GPIOINT->IO2IntEnR |= (1 << 3); // Set GPIO rising interrupt. |
Brian Silverman | d241148 | 2013-03-28 21:36:32 -0700 | [diff] [blame] | 589 | // Make sure they're in mode 00 (the default, aka nothing special). |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 590 | PINCON->PINSEL4 &= ~(0x3 << 4); |
| 591 | PINCON->PINSEL4 &= ~(0x3 << 6); |
| 592 | encoder5_val = 0; |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 593 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 594 | // Enable interrupts from the GPIO pins. |
| 595 | NVIC_EnableIRQ(EINT3_IRQn); |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 596 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 597 | xTaskCreate(vDelayCapture, |
| 598 | (signed char *) "SENSORs", |
Brian Silverman | ab2e676 | 2013-03-29 17:27:41 -0700 | [diff] [blame] | 599 | configMINIMAL_STACK_SIZE + 100, |
| 600 | NULL /*parameters*/, |
| 601 | tskIDLE_PRIORITY + 5, |
| 602 | NULL /*return task handle*/); |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 603 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 604 | GPIOINT->IO0IntEnF |= (1 << 4); // Set GPIO falling interrupt |
| 605 | GPIOINT->IO0IntEnR |= (1 << 4); // Set GPIO rising interrupt |
| 606 | PINCON->PINSEL0 &= ~(0x3 << 8); |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 607 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 608 | GPIOINT->IO0IntEnF |= (1 << 5); // Set GPIO falling interrupt |
| 609 | GPIOINT->IO0IntEnR |= (1 << 5); // Set GPIO rising interrupt |
| 610 | PINCON->PINSEL0 &= ~(0x3 << 10); |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 611 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 612 | GPIOINT->IO0IntEnR |= (1 << 6); // Set GPIO rising interrupt |
| 613 | PINCON->PINSEL0 &= ~(0x3 << 12); |
Austin Schuh | 63d0e9b | 2013-03-27 04:43:14 +0000 | [diff] [blame] | 614 | |
Brian Silverman | 6ad00b8 | 2013-03-27 19:02:38 -0700 | [diff] [blame] | 615 | GPIOINT->IO0IntEnR |= (1 << 7); // Set GPIO rising interrupt |
| 616 | PINCON->PINSEL0 &= ~(0x3 << 14); |
brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame] | 617 | } |