ADC cleanup (#1401)

* cleanup function names

* cleanup voltage reading

* define constant

* fix misra

* remove comments
This commit is contained in:
Robbe Derks 2023-05-04 13:22:01 +02:00 committed by GitHub
parent cedb5fd1a6
commit fea1feb598
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 33 additions and 46 deletions

View File

@ -191,7 +191,7 @@ const board board_black = {
.has_canfd = false,
.has_rtc_battery = false,
.fan_max_rpm = 0U,
.adc_scale = 8862U,
.avdd_mV = 3300U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
.init = black_init,

View File

@ -25,7 +25,7 @@ struct board {
const bool has_canfd;
const bool has_rtc_battery;
const uint16_t fan_max_rpm;
const uint16_t adc_scale;
const uint16_t avdd_mV;
const bool fan_stall_recovery;
const uint8_t fan_enable_cooldown_time;
board_init init;

View File

@ -215,7 +215,7 @@ const board board_dos = {
.has_canfd = false,
.has_rtc_battery = true,
.fan_max_rpm = 6500U,
.adc_scale = 8862U,
.avdd_mV = 3300U,
.fan_stall_recovery = true,
.fan_enable_cooldown_time = 3U,
.init = dos_init,

View File

@ -45,7 +45,7 @@ const board board_grey = {
.has_canfd = false,
.has_rtc_battery = false,
.fan_max_rpm = 0U,
.adc_scale = 8862U,
.avdd_mV = 3300U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
.init = grey_init,

View File

@ -83,7 +83,7 @@ const board board_pedal = {
.has_canfd = false,
.has_rtc_battery = false,
.fan_max_rpm = 0U,
.adc_scale = 8862U,
.avdd_mV = 3300U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
.init = pedal_init,

View File

@ -178,7 +178,7 @@ const board board_red = {
.has_canfd = true,
.has_rtc_battery = false,
.fan_max_rpm = 0U,
.adc_scale = 5539U,
.avdd_mV = 3300U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
.init = red_init,

View File

@ -22,7 +22,7 @@ const board board_red_v2 = {
.has_canfd = true,
.has_rtc_battery = true,
.fan_max_rpm = 0U,
.adc_scale = 5539U,
.avdd_mV = 3300U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
.init = red_panda_v2_init,

View File

@ -94,7 +94,7 @@ const board board_tres = {
.has_canfd = true,
.has_rtc_battery = true,
.fan_max_rpm = 6600U,
.adc_scale = 3021U,
.avdd_mV = 1800U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 3U,
.init = tres_init,

View File

@ -215,7 +215,7 @@ void uno_init(void) {
}
// Switch to phone usb mode if harness connection is powered by less than 7V
if(adc_get_voltage(current_board->adc_scale) < 7000U){
if((adc_get_mV(ADCCHAN_VIN) * VIN_READOUT_DIVIDER) < 7000U){
uno_set_usb_switch(true);
} else {
uno_set_usb_switch(false);
@ -251,7 +251,7 @@ const board board_uno = {
.has_canfd = false,
.has_rtc_battery = true,
.fan_max_rpm = 5100U,
.adc_scale = 8862U,
.avdd_mV = 3300U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
.init = uno_init,

View File

@ -142,7 +142,7 @@ void white_set_can_mode(uint8_t mode){
}
uint32_t white_read_current(void){
return adc_get(ADCCHAN_CURRENT);
return adc_get_raw(ADCCHAN_CURRENT);
}
bool white_check_ignition(void){
@ -214,7 +214,7 @@ void white_grey_common_init(void) {
white_set_can_mode(CAN_MODE_NORMAL);
// Init usb power mode
uint32_t voltage = adc_get_voltage(current_board->adc_scale);
uint32_t voltage = adc_get_mV(ADCCHAN_VIN) * VIN_READOUT_DIVIDER;
// Init in CDP mode only if panda is powered by 12V.
// Otherwise a PC would not be able to flash a standalone panda
if (voltage > 8000U) { // 8V threshold
@ -247,7 +247,7 @@ const board board_white = {
.has_canfd = false,
.has_rtc_battery = false,
.fan_max_rpm = 0U,
.adc_scale = 8862U,
.avdd_mV = 3300U,
.fan_stall_recovery = false,
.fan_enable_cooldown_time = 0U,
.init = white_init,

View File

@ -15,6 +15,8 @@
#define USBPACKET_MAX_SIZE 0x40U
#define MAX_CAN_MSGS_PER_BULK_TRANSFER 51U
#define VIN_READOUT_DIVIDER 11U
// USB definitions
#define USB_VID 0xBBAAU

View File

@ -53,8 +53,8 @@ uint8_t harness_detect_orientation(void) {
uint8_t ret = HARNESS_STATUS_NC;
#ifndef BOOTSTUB
uint32_t sbu1_voltage = adc_get(current_board->harness_config->adc_channel_SBU1);
uint32_t sbu2_voltage = adc_get(current_board->harness_config->adc_channel_SBU2);
uint32_t sbu1_voltage = adc_get_raw(current_board->harness_config->adc_channel_SBU1);
uint32_t sbu2_voltage = adc_get_raw(current_board->harness_config->adc_channel_SBU2);
// Detect connection and orientation
if((sbu1_voltage < HARNESS_CONNECTED_THRESHOLD) || (sbu2_voltage < HARNESS_CONNECTED_THRESHOLD)){

View File

@ -9,10 +9,10 @@ int get_health_pkt(void *dat) {
struct health_t * health = (struct health_t*)dat;
health->uptime_pkt = uptime_cnt;
health->voltage_pkt = adc_get_voltage(current_board->adc_scale);
health->voltage_pkt = adc_get_mV(ADCCHAN_VIN) * VIN_READOUT_DIVIDER;
health->current_pkt = current_board->read_current();
//Use the GPIO pin to determine ignition or use a CAN based logic
// Use the GPIO pin to determine ignition or use a CAN based logic
health->ignition_line_pkt = (uint8_t)(current_board->check_ignition());
health->ignition_can_pkt = (uint8_t)(ignition_can);

View File

@ -244,8 +244,8 @@ void TIM3_IRQ_Handler(void) {
void pedal(void) {
// read/write
pdl0 = adc_get(ADCCHAN_ACCEL0);
pdl1 = adc_get(ADCCHAN_ACCEL1);
pdl0 = adc_get_raw(ADCCHAN_ACCEL0);
pdl1 = adc_get_raw(ADCCHAN_ACCEL1);
// write the pedal to the DAC
if (state == NO_FAULT) {

View File

@ -5,6 +5,7 @@
#include "boards/unused_funcs.h"
// ///// Board definition and detection ///// //
#include "stm32fx/lladc.h"
#include "drivers/harness.h"
#ifdef PANDA
#include "drivers/fan.h"

View File

@ -5,7 +5,7 @@
#define ADCCHAN_ACCEL0 10
#define ADCCHAN_ACCEL1 11
#define ADCCHAN_VOLTAGE 12
#define ADCCHAN_VIN 12
#define ADCCHAN_CURRENT 13
void register_set(volatile uint32_t *addr, uint32_t val, uint32_t mask);
@ -16,9 +16,9 @@ void adc_init(void) {
register_set(&(ADC1->SMPR1), ADC_SMPR1_SMP12 | ADC_SMPR1_SMP13, 0x7FFFFFFU);
}
uint32_t adc_get(unsigned int channel) {
uint16_t adc_get_raw(uint8_t channel) {
// Select channel
register_set(&(ADC1->JSQR), (channel << 15U), 0x3FFFFFU);
register_set(&(ADC1->JSQR), ((uint32_t) channel << 15U), 0x3FFFFFU);
// Start conversion
ADC1->SR &= ~(ADC_SR_JEOC);
@ -28,13 +28,6 @@ uint32_t adc_get(unsigned int channel) {
return ADC1->JDR1;
}
uint32_t adc_get_voltage(uint16_t scale) {
// 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) * scale) / 1000U;
uint16_t adc_get_mV(uint8_t channel) {
return (adc_get_raw(channel) * current_board->avdd_mV) / 4095U;
}

View File

@ -63,7 +63,6 @@
#include "stm32fx/peripherals.h"
#include "stm32fx/interrupt_handlers.h"
#include "drivers/timers.h"
#include "stm32fx/lladc.h"
#include "stm32fx/board.h"
#include "stm32fx/clock.h"

View File

@ -5,6 +5,7 @@
#include "boards/unused_funcs.h"
// ///// Board definition and detection ///// //
#include "stm32h7/lladc.h"
#include "drivers/harness.h"
#include "drivers/fan.h"
#include "stm32h7/llfan.h"

View File

@ -1,6 +1,6 @@
// 5VOUT_S = ADC12_INP5
// VOLT_S = ADC1_INP2
#define ADCCHAN_VOLTAGE 2
#define ADCCHAN_VIN 2
void adc_init(void) {
ADC1->CR &= ~(ADC_CR_DEEPPWD); //Reset deep-power-down mode
@ -17,10 +17,10 @@ void adc_init(void) {
while(!(ADC1->ISR & ADC_ISR_ADRDY));
}
uint32_t adc_get(unsigned int channel) {
uint16_t adc_get_raw(uint8_t channel) {
ADC1->SQR1 &= ~(ADC_SQR1_L);
ADC1->SQR1 = (channel << 6U);
ADC1->SQR1 = ((uint32_t) channel << 6U);
ADC1->SMPR1 = (0x7U << (channel * 3U) );
ADC1->PCSEL_RES0 = (0x1U << channel);
@ -36,14 +36,6 @@ uint32_t adc_get(unsigned int channel) {
return res;
}
uint32_t adc_get_voltage(uint16_t scale) {
// REVC has a 10, 1 (1/11) voltage divider
// Here is the calculation for the scale (s)
// ADCV = VIN_S * (1/11) * (65535/3.3)
// RETVAL = ADCV * s = VIN_S*1000
// s = 1000/((65535/3.3)*(1/11)) = 0.553902494
// s = 1000/((65535/1.8)*(1/11)) = 0.3021
// Avoid needing floating point math, so output in mV
return (adc_get(ADCCHAN_VOLTAGE) * scale) / 10000U;
uint16_t adc_get_mV(uint8_t channel) {
return (adc_get_raw(channel) * current_board->avdd_mV) / 65535U;
}

View File

@ -67,7 +67,6 @@ separate IRQs for RX and TX.
#include "stm32h7/peripherals.h"
#include "stm32h7/interrupt_handlers.h"
#include "drivers/timers.h"
#include "stm32h7/lladc.h"
#if !defined(BOOTSTUB) && defined(PANDA)
#include "drivers/uart.h"