Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 1 | #include "motors/peripheral/adc.h" |
| 2 | |
| 3 | #include "motors/core/kinetis.h" |
| 4 | |
| 5 | namespace frc971 { |
| 6 | namespace salsa { |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 7 | namespace { |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 8 | |
| 9 | #define ADC_SC2_BASE (ADC_SC2_REFSEL(0) /* Use the external reference pins. */) |
| 10 | |
| 11 | #define ADC_FINISH_CALIBRATION(n, PM) \ |
| 12 | do { \ |
| 13 | uint16_t variable = 0; \ |
| 14 | variable += ADC##n##_CL##PM##0; \ |
| 15 | variable += ADC##n##_CL##PM##1; \ |
| 16 | variable += ADC##n##_CL##PM##2; \ |
| 17 | variable += ADC##n##_CL##PM##3; \ |
| 18 | variable += ADC##n##_CL##PM##4; \ |
| 19 | variable += ADC##n##_CL##PM##S; \ |
| 20 | variable /= 2; \ |
| 21 | variable |= 0x8000; \ |
| 22 | ADC##n##_##PM##G = variable; \ |
| 23 | } while (0); |
| 24 | |
| 25 | #define ADC_INIT_SINGLE(n) \ |
| 26 | do { \ |
| 27 | ADC##n##_CFG1 = ADC_CFG1_ADIV(2) /* Divide clock by 4 to get 15MHz. */ | \ |
| 28 | ADC_CFG1_MODE(1) /* 12 bit mode. */ | \ |
| 29 | ADC_CFG1_ADICLK(0) /* Use the bus clock (60MHz). */; \ |
| 30 | ADC##n##_CFG2 = ADC_CFG2_MUXSEL /* Use the b channels. */ | \ |
| 31 | ADC_CFG2_ADHSC /* Support higher ADC clock speeds. */; \ |
| 32 | ADC##n##_SC1A = 0; /* Clear SC1A's COCO flag. */ \ |
| 33 | ADC##n##_SC2 = ADC_SC2_BASE; \ |
| 34 | do { \ |
| 35 | ADC##n##_SC3 = ADC_SC3_CAL | ADC_SC3_AVGE | \ |
| 36 | ADC_SC3_AVGS(3) /* Average 32 samples (max). */; \ |
| 37 | /* Wait for calibration to complete. */ \ |
| 38 | while (!(ADC##n##_SC1A & ADC_SC1_COCO)) { \ |
| 39 | } \ |
| 40 | } while (ADC##n##_SC3 & ADC_SC3_CALF); \ |
| 41 | ADC_FINISH_CALIBRATION(n, P); \ |
| 42 | ADC_FINISH_CALIBRATION(n, M); \ |
| 43 | \ |
| 44 | ADC##n##_SC3 = 0 /* Disable hardware averaging. */; \ |
| 45 | } while (0) |
| 46 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 47 | void AdcInitCommon() { |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 48 | SIM_SCGC3 |= SIM_SCGC3_ADC1; |
| 49 | SIM_SCGC6 |= SIM_SCGC6_ADC0; |
| 50 | // TODO(Brian): Mess with SIM_SOPT7 to reconfigure ADC trigger input source? |
| 51 | ADC_INIT_SINGLE(0); |
| 52 | ADC_INIT_SINGLE(1); |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 53 | } |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 54 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 55 | } // namespace |
| 56 | |
| 57 | void AdcInitMedium() { |
| 58 | AdcInitCommon(); |
| 59 | |
| 60 | // M_CH2V ADC0_SE14 |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 61 | PORTC_PCR0 = PORT_PCR_MUX(0); |
| 62 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 63 | // M_CH0V ADC0_SE13 |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 64 | PORTB_PCR3 = PORT_PCR_MUX(0); |
| 65 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 66 | // M_CH1V ADC0_SE12 |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 67 | PORTB_PCR2 = PORT_PCR_MUX(0); |
| 68 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 69 | // M_CH0F ADC1_SE14 |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 70 | PORTB_PCR10 = PORT_PCR_MUX(0); |
| 71 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 72 | // M_CH1F ADC1_SE15 |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 73 | PORTB_PCR11 = PORT_PCR_MUX(0); |
| 74 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 75 | // M_VREF ADC0_SE18 |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 76 | PORTE_PCR25 = PORT_PCR_MUX(0); |
| 77 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 78 | // VIN ADC1_SE5B |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 79 | PORTC_PCR9 = PORT_PCR_MUX(0); |
| 80 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 81 | // M_CH2F ADC1_SE17 |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 82 | PORTA_PCR17 = PORT_PCR_MUX(0); |
| 83 | } |
| 84 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 85 | void AdcInitSmall() { |
| 86 | AdcInitCommon(); |
| 87 | |
| 88 | // M0_CH0F ADC1_SE17 |
| 89 | PORTA_PCR17 = PORT_PCR_MUX(0); |
| 90 | |
| 91 | // M0_CH1F ADC1_SE14 |
| 92 | PORTB_PCR10 = PORT_PCR_MUX(0); |
| 93 | |
| 94 | // M0_CH2F ADC1_SE15 |
| 95 | PORTB_PCR11 = PORT_PCR_MUX(0); |
| 96 | |
| 97 | // M0_ABS ADC0_SE5b |
| 98 | PORTD_PCR1 = PORT_PCR_MUX(0); |
| 99 | |
| 100 | // M1_CH0F ADC0_SE13 |
| 101 | PORTB_PCR3 = PORT_PCR_MUX(0); |
| 102 | |
| 103 | // M1_CH1F ADC0_SE12 |
| 104 | PORTB_PCR2 = PORT_PCR_MUX(0); |
| 105 | |
| 106 | // M1_CH2F ADC0_SE14 |
| 107 | PORTC_PCR0 = PORT_PCR_MUX(0); |
| 108 | |
| 109 | // M1_ABS ADC0_SE17 |
| 110 | PORTE_PCR24 = PORT_PCR_MUX(0); |
| 111 | |
| 112 | // WHEEL_ABS ADC0_SE18 |
| 113 | PORTE_PCR25 = PORT_PCR_MUX(0); |
| 114 | |
| 115 | // VIN ADC1_SE5B |
| 116 | PORTC_PCR9 = PORT_PCR_MUX(0); |
| 117 | } |
| 118 | |
| 119 | MediumAdcReadings AdcReadMedium(const DisableInterrupts &) { |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 120 | MediumAdcReadings r; |
| 121 | |
| 122 | ADC1_SC1A = 14; |
| 123 | while (!(ADC1_SC1A & ADC_SC1_COCO)) { |
| 124 | } |
| 125 | ADC1_SC1A = 15; |
| 126 | r.motor_currents[0][0] = ADC1_RA; |
| 127 | while (!(ADC1_SC1A & ADC_SC1_COCO)) { |
| 128 | } |
| 129 | ADC1_SC1A = 17; |
| 130 | ADC0_SC1A = 18; |
| 131 | r.motor_currents[1][0] = ADC1_RA; |
| 132 | while (!(ADC1_SC1A & ADC_SC1_COCO)) { |
| 133 | } |
| 134 | ADC1_SC1A = 5; |
| 135 | r.motor_currents[2][0] = ADC1_RA; |
| 136 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 137 | } |
| 138 | r.motor_current_ref = ADC0_RA; |
| 139 | while (!(ADC1_SC1A & ADC_SC1_COCO)) { |
| 140 | } |
| 141 | ADC1_SC1A = 14; |
| 142 | r.input_voltage = ADC1_RA; |
| 143 | while (!(ADC1_SC1A & ADC_SC1_COCO)) { |
| 144 | } |
| 145 | ADC1_SC1A = 15; |
| 146 | r.motor_currents[0][1] = ADC1_RA; |
| 147 | while (!(ADC1_SC1A & ADC_SC1_COCO)) { |
| 148 | } |
| 149 | ADC1_SC1A = 17; |
| 150 | r.motor_currents[1][1] = ADC1_RA; |
| 151 | while (!(ADC1_SC1A & ADC_SC1_COCO)) { |
| 152 | } |
| 153 | r.motor_currents[2][1] = ADC1_RA; |
| 154 | |
| 155 | return r; |
| 156 | } |
| 157 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 158 | SmallAdcReadings AdcReadSmall0(const DisableInterrupts &) { |
| 159 | SmallAdcReadings r; |
| 160 | |
| 161 | ADC1_SC1A = 17; |
| 162 | while (!(ADC1_SC1A & ADC_SC1_COCO)) { |
| 163 | } |
| 164 | ADC1_SC1A = 14; |
| 165 | r.currents[0] = ADC1_RA; |
| 166 | while (!(ADC1_SC1A & ADC_SC1_COCO)) { |
| 167 | } |
| 168 | ADC1_SC1A = 15; |
| 169 | r.currents[1] = ADC1_RA; |
| 170 | while (!(ADC1_SC1A & ADC_SC1_COCO)) { |
| 171 | } |
| 172 | r.currents[2] = ADC1_RA; |
| 173 | |
| 174 | return r; |
| 175 | } |
| 176 | |
| 177 | SmallAdcReadings AdcReadSmall1(const DisableInterrupts &) { |
| 178 | SmallAdcReadings r; |
| 179 | |
| 180 | ADC0_SC1A = 13; |
| 181 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 182 | } |
| 183 | ADC0_SC1A = 12; |
| 184 | r.currents[0] = ADC0_RA; |
| 185 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 186 | } |
| 187 | ADC0_SC1A = 14; |
| 188 | r.currents[1] = ADC0_RA; |
| 189 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 190 | } |
| 191 | r.currents[2] = ADC0_RA; |
| 192 | |
| 193 | return r; |
| 194 | } |
| 195 | |
| 196 | SmallInitReadings AdcReadSmallInit(const DisableInterrupts &) { |
| 197 | SmallInitReadings r; |
| 198 | |
| 199 | ADC0_SC1A = 5; |
| 200 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 201 | } |
| 202 | ADC0_SC1A = 17; |
| 203 | r.motor0_abs = ADC0_RA; |
| 204 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 205 | } |
| 206 | ADC0_SC1A = 18; |
| 207 | r.motor1_abs = ADC0_RA; |
| 208 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 209 | } |
| 210 | r.wheel_abs = ADC0_RA; |
| 211 | |
| 212 | return r; |
| 213 | } |
| 214 | |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 215 | } // namespace salsa |
| 216 | } // namespace frc971 |