|
@@ -54,6 +54,7 @@ D15.
|
|
|
#include "ti_msp_dl_config.h"
|
|
#include "ti_msp_dl_config.h"
|
|
|
#include <stdio.h>
|
|
#include <stdio.h>
|
|
|
#include "src/battery_data/battery.h"
|
|
#include "src/battery_data/battery.h"
|
|
|
|
|
+#include "src/config.h"
|
|
|
|
|
|
|
|
/*
|
|
/*
|
|
|
* Creating Configuartion Register as mentioned in the datasheet: https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/22226a.pdf
|
|
* Creating Configuartion Register as mentioned in the datasheet: https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/22226a.pdf
|
|
@@ -110,27 +111,32 @@ static uint8_t construct_config_byte(ADC_Params *params) {
|
|
|
* I2C*/
|
|
* I2C*/
|
|
|
|
|
|
|
|
static bool adc_configure(uint8_t slot_id, ADC_Params *params) {
|
|
static bool adc_configure(uint8_t slot_id, ADC_Params *params) {
|
|
|
- controllerTxPackage.packet[0] = construct_config_byte(params);
|
|
|
|
|
- printf("Config Byte: 0x%02X\n", controllerTxPackage.packet[0]);
|
|
|
|
|
- // Wait for I2C Bus to be Free**
|
|
|
|
|
- while (DL_I2C_getControllerStatus(I2C_controller_INST) &
|
|
|
|
|
- DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
|
|
|
|
|
- ;
|
|
|
|
|
- if(controllerTxPackage.packet[0] == 0xFF){
|
|
|
|
|
- // this clause can only happen if the internal memory management is messed up?!
|
|
|
|
|
- // the config function should take care that this is never the case
|
|
|
|
|
- printf("[ADC] Unable to send config bytes\n");
|
|
|
|
|
- *battery_slots[slot_id].state = SLOT_ERR_CONFIGBYTE;
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
- // Prepare TX Buffer
|
|
|
|
|
- controllerTxPackage.len = 1;
|
|
|
|
|
- controllerTxPackage.count = 0;
|
|
|
|
|
- controllerTxPackage.complete = false;
|
|
|
|
|
- i2c_hal.write(ADC_TARGET_BASE_ADDRESS);
|
|
|
|
|
-
|
|
|
|
|
- while(!controllerTxPackage.complete);
|
|
|
|
|
- return true;
|
|
|
|
|
|
|
+ controllerTxPackage.packet[0] = construct_config_byte(params);
|
|
|
|
|
+
|
|
|
|
|
+#ifdef DEBUG_ADC
|
|
|
|
|
+ printf("Config Byte: 0x%02X\n", controllerTxPackage.packet[0]);
|
|
|
|
|
+#endif
|
|
|
|
|
+ // Wait for I2C Bus to be Free**
|
|
|
|
|
+ while (DL_I2C_getControllerStatus(I2C_controller_INST) &
|
|
|
|
|
+ DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
|
|
|
|
|
+ ;
|
|
|
|
|
+ if(controllerTxPackage.packet[0] == 0xFF){
|
|
|
|
|
+ // this clause can only happen if the internal memory management is messed up?!
|
|
|
|
|
+ // the config function should take care that this is never the case
|
|
|
|
|
+#ifdef DEBUG_ADC
|
|
|
|
|
+ printf("[ADC] Unable to send config bytes\n");
|
|
|
|
|
+#endif
|
|
|
|
|
+ *battery_slots[slot_id].state = SLOT_ERR_CONFIGBYTE;
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ // Prepare TX Buffer
|
|
|
|
|
+ controllerTxPackage.len = 1;
|
|
|
|
|
+ controllerTxPackage.count = 0;
|
|
|
|
|
+ controllerTxPackage.complete = false;
|
|
|
|
|
+ i2c_hal.write(ADC_TARGET_BASE_ADDRESS);
|
|
|
|
|
+
|
|
|
|
|
+ while(!controllerTxPackage.complete);
|
|
|
|
|
+ return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -146,86 +152,92 @@ Conversion mode, writing this bit to “1” initiates a new conversion.
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
static bool adc_is_ready(uint8_t slot_id, ADC_Params *params) {
|
|
static bool adc_is_ready(uint8_t slot_id, ADC_Params *params) {
|
|
|
- uint8_t adc_address = ADC_TARGET_BASE_ADDRESS + slot_id;
|
|
|
|
|
- controllerRxPackage.len = 3;
|
|
|
|
|
- controllerRxPackage.count = 0;
|
|
|
|
|
- controllerRxPackage.complete = false;
|
|
|
|
|
-
|
|
|
|
|
- i2c_hal.read(adc_address);
|
|
|
|
|
- //i2c_hal.read(ADC_TARGET_BASE_ADDRESS + slot_id, 3);
|
|
|
|
|
- // Ready bit is bit 7
|
|
|
|
|
- while(!controllerRxPackage.complete);
|
|
|
|
|
- uint8_t config_adc_byte = controllerRxPackage.packet[2];
|
|
|
|
|
- bool ready = (config_adc_byte & 0x80) == 1;
|
|
|
|
|
- printf("Bytes: 0x%02X 0x%02X 0x%02X (%d %d %d)\n", controllerRxPackage.packet[0], controllerRxPackage.packet[1], controllerRxPackage.packet[2], controllerRxPackage.packet[0], controllerRxPackage.packet[1], controllerRxPackage.packet[2]);
|
|
|
|
|
- printf("ADC Ready:: gRxADClen: %d, gRxADCcount: %d ready? %d\n", controllerRxPackage.len, controllerRxPackage.count, ready);
|
|
|
|
|
- return ready;
|
|
|
|
|
|
|
+ uint8_t adc_address = ADC_TARGET_BASE_ADDRESS + slot_id;
|
|
|
|
|
+ controllerRxPackage.len = 3;
|
|
|
|
|
+ controllerRxPackage.count = 0;
|
|
|
|
|
+ controllerRxPackage.complete = false;
|
|
|
|
|
+
|
|
|
|
|
+ i2c_hal.read(adc_address);
|
|
|
|
|
+ //i2c_hal.read(ADC_TARGET_BASE_ADDRESS + slot_id, 3);
|
|
|
|
|
+ // Ready bit is bit 7
|
|
|
|
|
+ while(!controllerRxPackage.complete);
|
|
|
|
|
+ uint8_t config_adc_byte = controllerRxPackage.packet[2];
|
|
|
|
|
+ bool ready = (config_adc_byte & 0x80) == 1;
|
|
|
|
|
+#ifdef DEBUG_ADC
|
|
|
|
|
+ printf("Bytes: 0x%02X 0x%02X 0x%02X (%d %d %d)\n", controllerRxPackage.packet[0], controllerRxPackage.packet[1], controllerRxPackage.packet[2], controllerRxPackage.packet[0], controllerRxPackage.packet[1], controllerRxPackage.packet[2]);
|
|
|
|
|
+ printf("ADC Ready:: gRxADClen: %d, gRxADCcount: %d ready? %d\n", controllerRxPackage.len, controllerRxPackage.count, ready);
|
|
|
|
|
+#endif
|
|
|
|
|
+ return ready;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
static int16_t read_adc_raw_data(uint8_t slot_id, ADC_Params *params) {
|
|
static int16_t read_adc_raw_data(uint8_t slot_id, ADC_Params *params) {
|
|
|
|
|
|
|
|
- // Buffer for ADC data (MSB, LSB, Config Byte)
|
|
|
|
|
- int16_t raw_adc = 0;
|
|
|
|
|
|
|
+ // Buffer for ADC data (MSB, LSB, Config Byte)
|
|
|
|
|
+ int16_t raw_adc = 0;
|
|
|
|
|
|
|
|
- controllerRxPackage.len = 3;
|
|
|
|
|
- controllerRxPackage.count = 0;
|
|
|
|
|
- controllerRxPackage.complete = false;
|
|
|
|
|
-
|
|
|
|
|
- i2c_hal.read(ADC_TARGET_BASE_ADDRESS + slot_id);
|
|
|
|
|
- while(!controllerRxPackage.complete);
|
|
|
|
|
- printf("ADC Read:: gRxADClen: %d, gRxADCcount: %d\n", controllerRxPackage.len, controllerRxPackage.count);
|
|
|
|
|
- printf("Bytes: 0x%02X 0x%02X 0x%02X (%d %d %d)\n", controllerRxPackage.packet[0], controllerRxPackage.packet[1], controllerRxPackage.packet[2], controllerRxPackage.packet[0], controllerRxPackage.packet[1], controllerRxPackage.packet[2]);
|
|
|
|
|
- uint8_t msb = controllerRxPackage.packet[0];
|
|
|
|
|
- uint8_t lsb = controllerRxPackage.packet[1];
|
|
|
|
|
- uint8_t config_adc_byte = controllerRxPackage.packet[2];
|
|
|
|
|
-
|
|
|
|
|
- if (params->resolution == 12) {
|
|
|
|
|
- raw_adc = ((msb & 0b00001111) << 8) | lsb;
|
|
|
|
|
- if (raw_adc > 2047)
|
|
|
|
|
- raw_adc -= 4096;
|
|
|
|
|
- }
|
|
|
|
|
- printf("MSB: 0x%02X (%d)\n", msb, msb);
|
|
|
|
|
- printf("LSB: 0x%02X (%d)\n", lsb, lsb);
|
|
|
|
|
- printf("Config Byte response: 0x%02X \n", config_adc_byte);
|
|
|
|
|
- return raw_adc;
|
|
|
|
|
|
|
+ controllerRxPackage.len = 3;
|
|
|
|
|
+ controllerRxPackage.count = 0;
|
|
|
|
|
+ controllerRxPackage.complete = false;
|
|
|
|
|
+
|
|
|
|
|
+ i2c_hal.read(ADC_TARGET_BASE_ADDRESS + slot_id);
|
|
|
|
|
+ while(!controllerRxPackage.complete);
|
|
|
|
|
+#ifdef DEBUG_ADC
|
|
|
|
|
+ printf("ADC Read:: gRxADClen: %d, gRxADCcount: %d\n", controllerRxPackage.len, controllerRxPackage.count);
|
|
|
|
|
+ printf("Bytes: 0x%02X 0x%02X 0x%02X (%d %d %d)\n", controllerRxPackage.packet[0], controllerRxPackage.packet[1], controllerRxPackage.packet[2], controllerRxPackage.packet[0], controllerRxPackage.packet[1], controllerRxPackage.packet[2]);
|
|
|
|
|
+#endif
|
|
|
|
|
+ uint8_t msb = controllerRxPackage.packet[0];
|
|
|
|
|
+ uint8_t lsb = controllerRxPackage.packet[1];
|
|
|
|
|
+ uint8_t config_adc_byte = controllerRxPackage.packet[2];
|
|
|
|
|
+
|
|
|
|
|
+ if (params->resolution == 12) {
|
|
|
|
|
+ raw_adc = ((msb & 0b00001111) << 8) | lsb;
|
|
|
|
|
+ if (raw_adc > 2047)
|
|
|
|
|
+ raw_adc -= 4096;
|
|
|
|
|
+ }
|
|
|
|
|
+#ifdef DEBUG_ADC
|
|
|
|
|
+ printf("MSB: 0x%02X (%d)\n", msb, msb);
|
|
|
|
|
+ printf("LSB: 0x%02X (%d)\n", lsb, lsb);
|
|
|
|
|
+ printf("Config Byte response: 0x%02X \n", config_adc_byte);
|
|
|
|
|
+#endif
|
|
|
|
|
+ return raw_adc;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* Function to Convert ADC Reading to Voltage */
|
|
/* Function to Convert ADC Reading to Voltage */
|
|
|
static uint16_t adc_voltage(int16_t adc_value, ADC_Params *params) {
|
|
static uint16_t adc_voltage(int16_t adc_value, ADC_Params *params) {
|
|
|
- uint16_t measured_voltage = 0;
|
|
|
|
|
- uint16_t LSB = 0;
|
|
|
|
|
- uint32_t max_adc_value = 1;
|
|
|
|
|
|
|
+ uint16_t measured_voltage = 0;
|
|
|
|
|
+ uint16_t LSB = 0;
|
|
|
|
|
+ uint32_t max_adc_value = 1;
|
|
|
|
|
|
|
|
- switch (params->resolution) {
|
|
|
|
|
- case 12: // 12-bit
|
|
|
|
|
- max_adc_value = 4095;
|
|
|
|
|
- break;
|
|
|
|
|
- case 14: // 14-bit
|
|
|
|
|
- max_adc_value = 16383;
|
|
|
|
|
- break;
|
|
|
|
|
- case 16: // 16-bit
|
|
|
|
|
- max_adc_value = 65535;
|
|
|
|
|
- break;
|
|
|
|
|
- default:
|
|
|
|
|
- //printf("Error: Unknown ADC Resolution!\n");
|
|
|
|
|
- return 0;
|
|
|
|
|
- }
|
|
|
|
|
- measured_voltage = (((uint32_t)adc_value) * 2.7);
|
|
|
|
|
- //printf("Measured ADC voltage: %d\n", measured_voltage);
|
|
|
|
|
- return (uint16_t)measured_voltage;
|
|
|
|
|
|
|
+ switch (params->resolution) {
|
|
|
|
|
+ case 12: // 12-bit
|
|
|
|
|
+ max_adc_value = 4095;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 14: // 14-bit
|
|
|
|
|
+ max_adc_value = 16383;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 16: // 16-bit
|
|
|
|
|
+ max_adc_value = 65535;
|
|
|
|
|
+ break;
|
|
|
|
|
+ default:
|
|
|
|
|
+ //printf("Error: Unknown ADC Resolution!\n");
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ measured_voltage = (((uint32_t)adc_value) * 2.7);
|
|
|
|
|
+ //printf("Measured ADC voltage: %d\n", measured_voltage);
|
|
|
|
|
+ return (uint16_t)measured_voltage;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* Function to Convert ADC Reading to Voltage */
|
|
/* Function to Convert ADC Reading to Voltage */
|
|
|
uint16_t adc_current(int16_t adc_value, ADC_Params *params) {
|
|
uint16_t adc_current(int16_t adc_value, ADC_Params *params) {
|
|
|
- int16_t current_mA = 0;
|
|
|
|
|
- // Convert ADC value to voltage across shunt resistor:
|
|
|
|
|
- uint16_t voltage_mV = adc_voltage(adc_value, params);
|
|
|
|
|
- uint8_t gain_multiplier = (1 << (params->gain - 1));
|
|
|
|
|
- // Convert voltage drop across shunt resistor to current
|
|
|
|
|
- current_mA = (adc_value) * (10 * gain_multiplier);
|
|
|
|
|
- //printf("[ADC] Battery current is %u mA.\n", current_mA);
|
|
|
|
|
- return (int16_t)current_mA;
|
|
|
|
|
|
|
+ int16_t current_mA = 0;
|
|
|
|
|
+ // Convert ADC value to voltage across shunt resistor:
|
|
|
|
|
+ uint16_t voltage_mV = adc_voltage(adc_value, params);
|
|
|
|
|
+ uint8_t gain_multiplier = (1 << (params->gain - 1));
|
|
|
|
|
+ // Convert voltage drop across shunt resistor to current
|
|
|
|
|
+ current_mA = (adc_value) * (10 * gain_multiplier);
|
|
|
|
|
+ //printf("[ADC] Battery current is %u mA.\n", current_mA);
|
|
|
|
|
+ return (int16_t)current_mA;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|