foxBMS  1.1.0
The foxBMS Battery Management System API Documentation
adc.c File Reference

Driver for the ADC module. More...

#include "adc.h"
#include "beta.h"
#include "database.h"
#include "epcos_b57251v5103j060.h"
#include "io.h"
#include "spi.h"
Include dependency graph for adc.c:

Go to the source code of this file.

Macros

#define ADC_TEMPERATURE_FACTOR   (10.0f)
 

Typedefs

typedef enum ADC_TEMPERATURE_SENSOR_TYPE ADC_TEMPERATURE_SENSOR_TYPE_e
 

Enumerations

enum  ADC_TEMPERATURE_SENSOR_TYPE { ADC0_TEMPERATURE_SENSOR , ADC1_TEMPERATURE_SENSOR }
 

Functions

static STD_RETURN_TYPE_e ADC_Transmit (uint32 blocksize, uint16 *pTxBuffer, uint16 *pRxBuffer, SPI_INTERFACE_CONFIG_s *pSpiInterface)
 
static float ADC_ConvertVoltagesToTemperatures (float v_adc_V, ADC_TEMPERATURE_SENSOR_TYPE_e TsensorType)
 converts a raw voltage from ADC to a temperature value in Celsius. More...
 
void ADC_Initialize (void)
 initializes the ADC devices. It is called during startup. More...
 
void ADC_Control (void)
 determines which ADC is measured and stores result in database. More...
 

Variables

static uint8_t adc_conversionState = ADC_INIT
 describes the current state of the conversion More...
 
static uint16_t adc_txNull [SINGLE_MESSAGE_LENGTH] = {0x0000u, 0x0000u}
 
static uint16_t adc_txUnlockCommand [SINGLE_MESSAGE_LENGTH] = {0x0655u, 0x0000u}
 
static uint16_t adc_txLockCommand [SINGLE_MESSAGE_LENGTH] = {0x0555u, 0x0000u}
 
static uint16_t adc_txWakeupCommand [SINGLE_MESSAGE_LENGTH] = {0x0033u, 0x0000u}
 
static uint16_t adc_txReadRegisterCommand [SINGLE_MESSAGE_LENGTH] = {0x2F00u, 0x0000u}
 
static uint16_t adc_txWriteRegisterCommand [SINGLE_MESSAGE_LENGTH] = {0x4F0Fu, 0x0000u}
 
static uint16_t adc_rxReadSingleMessage [SINGLE_MESSAGE_LENGTH] = {0x0000u, 0x0000u}
 
static uint16_t adc_txConvert [CONVERT_LENGTH]
 
static uint16_t adc_rxConvert [CONVERT_LENGTH]
 
static float adc_adc0Voltage [4] = {0.0f, 0.0f, 0.0f, 0.0f}
 
static float adc_adc1Voltage [4] = {0.0f, 0.0f, 0.0f, 0.0f}
 
static float adc_lsb1 = 0.0f
 
static float adc_lsb2 = 0.0f
 
static DATA_BLOCK_ADC_TEMPERATURE_s adc_tableTemperature = {.header.uniqueId = DATA_BLOCK_ID_ADC_TEMPERATURE}
 

Detailed Description

Driver for the ADC module.

SPDX-License-Identifier: BSD-3-Clause

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

We kindly request you to use one or more of the following phrases to refer to foxBMS in your hardware, software, documentation or advertising materials:

  • ″This product uses parts of foxBMS®″
  • ″This product includes parts of foxBMS®″
  • ″This product is derived from foxBMS®″
Author
foxBMS Team
Date
2019-01-07 (date of creation)
Updated
2021-07-14 (date of last update)
Prefix
ADC

Definition in file adc.c.

Macro Definition Documentation

◆ ADC_TEMPERATURE_FACTOR

#define ADC_TEMPERATURE_FACTOR   (10.0f)

scaling factor for the conversion of a measurement value to a temperature

Definition at line 66 of file adc.c.

Typedef Documentation

◆ ADC_TEMPERATURE_SENSOR_TYPE_e

Type of temperature sensors

Enumeration Type Documentation

◆ ADC_TEMPERATURE_SENSOR_TYPE

Type of temperature sensors

Enumerator
ADC0_TEMPERATURE_SENSOR 

First ADC (ADC0)

ADC1_TEMPERATURE_SENSOR 

Second ADC (ADC1)

Definition at line 71 of file adc.c.

Function Documentation

◆ ADC_Control()

void ADC_Control ( void  )

determines which ADC is measured and stores result in database.

It alternates between measurement on ADC1 and ADC2. Measurement read by SPI.

Definition at line 211 of file adc.c.

Here is the call graph for this function:

◆ ADC_ConvertVoltagesToTemperatures()

static float ADC_ConvertVoltagesToTemperatures ( float  v_adc_V,
ADC_TEMPERATURE_SENSOR_TYPE_e  TsensorType 
)
static

converts a raw voltage from ADC to a temperature value in Celsius.

The temperatures are read from NTC elements via voltage dividers. This function implements the look-up table between voltage and temperature, taking into account the NTC characteristics and the voltage divider.

Parameters
v_adc_Vvoltage read from ADC in V
TsensorTypesensor type, dependent on ADC used (ADC0 or ADC1)
Returns
temperature value in deci °C

Definition at line 179 of file adc.c.

Here is the call graph for this function:

◆ ADC_Initialize()

void ADC_Initialize ( void  )

initializes the ADC devices. It is called during startup.

Definition at line 194 of file adc.c.

Here is the call graph for this function:

◆ ADC_Transmit()

static STD_RETURN_TYPE_e ADC_Transmit ( uint32  blocksize,
uint16 *  pTxBuffer,
uint16 *  pRxBuffer,
SPI_INTERFACE_CONFIG_s pSpiInterface 
)
static

transmission wrapper for the SPI communication to the ADC

Definition at line 167 of file adc.c.

Here is the call graph for this function:

Variable Documentation

◆ adc_adc0Voltage

float adc_adc0Voltage[4] = {0.0f, 0.0f, 0.0f, 0.0f}
static

Voltages measured by the 4 channels of ADC0

Definition at line 129 of file adc.c.

◆ adc_adc1Voltage

float adc_adc1Voltage[4] = {0.0f, 0.0f, 0.0f, 0.0f}
static

Voltages measured by the 4 channels of ADC1

Definition at line 131 of file adc.c.

◆ adc_conversionState

uint8_t adc_conversionState = ADC_INIT
static

describes the current state of the conversion

This variable is used as a state-variable for switching through the steps of a conversion.

Definition at line 83 of file adc.c.

◆ adc_lsb1

float adc_lsb1 = 0.0f
static

LSB of ADC0, used to convert raw measurement to voltage in V

Definition at line 133 of file adc.c.

◆ adc_lsb2

float adc_lsb2 = 0.0f
static

LSB of ADC1, used to convert raw measurement to voltage in V

Definition at line 135 of file adc.c.

◆ adc_rxConvert

uint16_t adc_rxConvert[CONVERT_LENGTH]
static
Initial value:
=
{0x0000u, 0x0000u, 0x0000u, 0x0000u, 0x0000u, 0x0000u, 0x0000u, 0x0000u, 0x0000u, 0x0000u}

This message is used to get the result of conversions

Definition at line 125 of file adc.c.

◆ adc_rxReadSingleMessage

uint16_t adc_rxReadSingleMessage[SINGLE_MESSAGE_LENGTH] = {0x0000u, 0x0000u}
static

This is a generic purpose receive buffer. It is used to read the answer of the ADC to the commands defined above.

Definition at line 114 of file adc.c.

◆ adc_tableTemperature

DATA_BLOCK_ADC_TEMPERATURE_s adc_tableTemperature = {.header.uniqueId = DATA_BLOCK_ID_ADC_TEMPERATURE}
static

local copy of the adc temperature table

Definition at line 138 of file adc.c.

◆ adc_txConvert

uint16_t adc_txConvert[CONVERT_LENGTH]
static
Initial value:
=
{0x0000u, 0x0000u, 0x0000u, 0x0000u, 0x0000u, 0x0000u, 0x0000u, 0x0000u, 0x0000u, 0x0000u}

This message is used to read conversion results It consists of NULL commands. When receiving NULL commands, the ADC outputs conversion data on the MasterInSlaveOut line.

Definition at line 121 of file adc.c.

◆ adc_txLockCommand

uint16_t adc_txLockCommand[SINGLE_MESSAGE_LENGTH] = {0x0555u, 0x0000u}
static

Lock command the ADC When ADC is locked, regsiters cannot be written

Definition at line 98 of file adc.c.

◆ adc_txNull

uint16_t adc_txNull[SINGLE_MESSAGE_LENGTH] = {0x0000u, 0x0000u}
static

NULL command sent to initialize the ADC

Definition at line 86 of file adc.c.

◆ adc_txReadRegisterCommand

uint16_t adc_txReadRegisterCommand[SINGLE_MESSAGE_LENGTH] = {0x2F00u, 0x0000u}
static

This command is used to read registers

Definition at line 104 of file adc.c.

◆ adc_txUnlockCommand

uint16_t adc_txUnlockCommand[SINGLE_MESSAGE_LENGTH] = {0x0655u, 0x0000u}
static

Unlock command for the ADC When ADC is locked, regsiters cannot be written

Definition at line 92 of file adc.c.

◆ adc_txWakeupCommand

uint16_t adc_txWakeupCommand[SINGLE_MESSAGE_LENGTH] = {0x0033u, 0x0000u}
static

The wake-up command starts the conversions by the ADC

Definition at line 101 of file adc.c.

◆ adc_txWriteRegisterCommand

uint16_t adc_txWriteRegisterCommand[SINGLE_MESSAGE_LENGTH] = {0x4F0Fu, 0x0000u}
static

This command is used to write registers

Definition at line 107 of file adc.c.