Selaa lähdekoodia

Verbesserung: ADC Adressen für höhere slot ids gefixt

Heinrich Blatt 7 kuukautta sitten
vanhempi
commit
ae8e04a4bc
3 muutettua tiedostoa jossa 24 lisäystä ja 8 poistoa
  1. 4 0
      src/battery_data/battery.c
  2. 2 1
      src/battery_data/battery.h
  3. 18 7
      src/peripherals/adc/adc_hal.c

+ 4 - 0
src/battery_data/battery.c

@@ -35,10 +35,14 @@ static void batteryslots_init() {
 
     // initialize data structures
     battery_slots[0].timer = PWM_0_INST;
+    battery_slots[0].adc_addr = ADC_TARGET_BASE_ADDRESS;
 #if NUM_SLOTS == 4
     battery_slots[1].timer = PWM_1_INST;
+    battery_slots[1].adc_addr = ADC_TARGET_BASE_ADDRESS+4;
     battery_slots[2].timer = PWM_2_INST;
+    battery_slots[2].adc_addr = ADC_TARGET_BASE_ADDRESS+2;
     battery_slots[3].timer = PWM_3_INST;
+    battery_slots[3].adc_addr = ADC_TARGET_BASE_ADDRESS+6;
 #endif
 
     for(uint8_t i=0; i< NUM_SLOTS; i++){

+ 2 - 1
src/battery_data/battery.h

@@ -8,7 +8,7 @@
 #include "src/config.h"
 
 //Battery Discharge Safety Check
-typedef enum{
+typedef enum: uint8_t{
     SLOT_STATE_OK= 0x00,
     SLOT_STATE_SOV= 0x01,
     SLOT_ERR_HOV= (0x02 | 0x80),
@@ -48,6 +48,7 @@ typedef struct {
     GPTIMER_Regs *timer;
     SlotState *state;
     int16_t high_side_voltage;
+    uint8_t adc_addr;
 
 } BatterySlot;
 

+ 18 - 7
src/peripherals/adc/adc_hal.c

@@ -124,9 +124,13 @@ static bool adc_configure(uint8_t slot_id, ADC_Params *params) {
     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)
+    uint32_t n_cycles = 0;
+    while ((DL_I2C_getControllerStatus(I2C_controller_INST) &
+            DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && n_cycles++ < MAX_I2C_WAIT_RX)
         ;
+    if (n_cycles == MAX_I2C_WAIT_RX) {
+        printf("Error in reading from I2C Bus: Bus is not getting ready on config byte\n");
+    }
     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
@@ -140,9 +144,9 @@ static bool adc_configure(uint8_t slot_id, ADC_Params *params) {
     controllerTxPackage.len = 1;
     controllerTxPackage.count = 0;
     controllerTxPackage.complete = false;
-    i2c_hal.write(ADC_TARGET_BASE_ADDRESS);
+    i2c_hal.write(battery_slots[slot_id].adc_addr);
     
-    uint32_t n_cycles = 0;
+    n_cycles = 0;
     while(!controllerTxPackage.complete && n_cycles++ < MAX_I2C_WAIT_RX);
     if (n_cycles == MAX_I2C_WAIT_RX) {
 #ifdef DEBUG_ADC
@@ -166,7 +170,7 @@ Conversion mode, writing this bit to “1” initiates a new conversion.
 */
 
 static bool adc_is_ready(uint8_t slot_id, ADC_Params *params) {
-    uint8_t adc_address = ADC_TARGET_BASE_ADDRESS + slot_id;
+    uint8_t adc_address = battery_slots[slot_id].adc_addr;
     controllerRxPackage.len = 3;
     controllerRxPackage.count = 0;
     controllerRxPackage.complete = false;
@@ -194,8 +198,15 @@ static int16_t read_adc_raw_data(uint8_t slot_id, ADC_Params *params) {
     controllerRxPackage.count = 0;
     controllerRxPackage.complete = false;
     
-    i2c_hal.read(ADC_TARGET_BASE_ADDRESS + slot_id);
-    while(!controllerRxPackage.complete);
+    i2c_hal.read(battery_slots[slot_id].adc_addr);
+    uint32_t n_cycles = 0;
+    while(!controllerRxPackage.complete && n_cycles++ < MAX_I2C_WAIT_RX);
+    if (n_cycles == MAX_I2C_WAIT_RX) {
+#ifdef DEBUG_ADC
+        printf("[ADC] No Response to the ADC!\n");
+#endif
+        return 0xffff;
+    }
     uint8_t msb = controllerRxPackage.packet[0];
     uint8_t lsb = controllerRxPackage.packet[1];
     uint8_t config_adc_byte = controllerRxPackage.packet[2];