mirror of
https://github.com/infiniteCable2/panda.git
synced 2026-02-26 05:23:57 +08:00
* Let refactoring begin! * Fix pedal build * Fix pedal safety tests * Forgot few TIM2 instances * Try this way with misra * More misras... * More misras... * Still fighting with misra blindfolded * Almost got it! * Last misra error.. * Last misra error.. * Misra works locally.. * Maybe this? * Looks like it was cppcheck bug, revert changes * Suggested changes and reverts * File structure change * revert includes * remove spaces * remove timer delay * endings * more typing * rename early to early_initialization * Remove delay_us * Revert RTC default values * Revert initialization sequence * Fix quotes * Revert * Return TIM6EN * Alias slow timer to TICK_TIMER * Refactor files structure * Remove definition of PANDA * Abstract timers * Fix include * tick_timer_init * Split usb driver * Move LL stuff: adc * Move LL stuff: usb * Fix include again... * Will check pedal builds also locally.. * Move LL stuff: CAN * Move LL stuff: clock * Rename common to peripherals and move * Move board HAL * Change include, not needed for pedal * llgpio to gpio and new lines fix * remove board_has_relay, not used * Remove board_functions.h and add to board struct * Move include * Fk MISRA... * has_onboard_gmlan to has_hw_gmlan * Typos * Move board_declarations include * Shuffle * More abstraction * fix paths, fix cppcheck test * Fix for pedal build with USB
41 lines
1.1 KiB
C
41 lines
1.1 KiB
C
// ACCEL1 = ADC10
|
|
// ACCEL2 = ADC11
|
|
// VOLT_S = ADC12
|
|
// CURR_S = ADC13
|
|
|
|
#define ADCCHAN_ACCEL0 10
|
|
#define ADCCHAN_ACCEL1 11
|
|
#define ADCCHAN_VOLTAGE 12
|
|
#define ADCCHAN_CURRENT 13
|
|
|
|
void register_set(volatile uint32_t *addr, uint32_t val, uint32_t mask);
|
|
|
|
void adc_init(void) {
|
|
register_set(&(ADC->CCR), ADC_CCR_TSVREFE | ADC_CCR_VBATE, 0xC30000U);
|
|
register_set(&(ADC1->CR2), ADC_CR2_ADON, 0xFF7F0F03U);
|
|
register_set(&(ADC1->SMPR1), ADC_SMPR1_SMP12 | ADC_SMPR1_SMP13, 0x7FFFFFFU);
|
|
}
|
|
|
|
uint32_t adc_get(unsigned int channel) {
|
|
// Select channel
|
|
register_set(&(ADC1->JSQR), (channel << 15U), 0x3FFFFFU);
|
|
|
|
// Start conversion
|
|
ADC1->SR &= ~(ADC_SR_JEOC);
|
|
ADC1->CR2 |= ADC_CR2_JSWSTART;
|
|
while (!(ADC1->SR & ADC_SR_JEOC));
|
|
|
|
return ADC1->JDR1;
|
|
}
|
|
|
|
uint32_t adc_get_voltage(void) {
|
|
// REVC has a 10, 1 (1/11) voltage divider
|
|
// Here is the calculation for the scale (s)
|
|
// ADCV = VIN_S * (1/11) * (4095/3.3)
|
|
// RETVAL = ADCV * s = VIN_S*1000
|
|
// s = 1000/((4095/3.3)*(1/11)) = 8.8623046875
|
|
|
|
// Avoid needing floating point math, so output in mV
|
|
return (adc_get(ADCCHAN_VOLTAGE) * 8862U) / 1000U;
|
|
}
|