Guest User

491_AXP2021_pl23+_Addons.diff

a guest
Dec 4th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.52 KB | Source Code | 0 0
  1. 491_AXP2021_pl23+_Addons.diff
  2.  
  3. - Modified Battery Status Logic
  4. - CV_Volage selection at Compile Time (only 4.0, 4.1, 4.2 Volt)
  5. - ADC Voltage Readings >0x1fff make no sense, Display as 'Floating' in Debug Menu
  6. - Found a Formula for ADC TDie calculation, implemented Display in Debug Menu
  7.   (https://github.com/lewisxhe/XPowersLib src/REG/AXP2101Constants.h, modified to integer only)
  8. - Show the AXP2101_REG_BATT_PERCENTAGE Value for Reference in the Battery Status Debug Menu
  9.  
  10. Mini-Credit ZappBranigan2972 on forums / F. Jacobsen on the Theme Site
  11.  
  12. ---
  13.  
  14. --- a/firmware/drivers/axp-2101.c.orig  2024-11-12 10:10:52.680404703 +0100
  15. +++ b/firmware/drivers/axp-2101.c   2024-11-12 10:22:13.304513117 +0100
  16. @@ -266,7 +266,7 @@
  17.          .en_bit = 0,
  18.          .min_mV = 500,
  19.          .max_mV = 1400,
  20. -        .step_mV = 560,
  21. +        .step_mV = 50,  // 560 must be a typo...
  22.          .step2_min_mV = 0,
  23.          .step2_mV = 0,
  24.          .step2_max_mV = 0,
  25. --- a/firmware/drivers/axp-2101.c.orig  2024-11-12 10:10:52.680404703 +0100
  26. +++ b/firmware/drivers/axp-2101.c   2024-11-12 10:22:13.304513117 +0100
  27. @@ -398,10 +398,10 @@
  28.  /* TODO: can we trust the battery current direction? */
  29.  int axp2101_battery_status(void)
  30.  {
  31. -    int r = i2c_reg_read1(AXP_PMU_BUS, AXP_PMU_ADDR, AXP2101_REG_PMU_STATUS2);
  32. -    if((r >> 5) & 0x03 == 0) {
  33. +    int r = i2c_reg_read1(AXP_PMU_BUS, AXP_PMU_ADDR, AXP2101_REG_PMU_STATUS2) & 0x07;
  34. +    if(r == 4) {
  35.          return AXP2101_BATT_FULL;
  36. -    } else if((r >> 5) & 0x03 == 01) {
  37. +    } else if(r < 4) {
  38.          return AXP2101_BATT_CHARGING;
  39.      } else {
  40.          return AXP2101_BATT_DISCHARGING;
  41. @@ -499,6 +499,12 @@
  42.      300,  400, 500,  600,  700, 800,  900, 1000,
  43.  };
  44.  
  45. +enum CV_Voltage {
  46. +  CV4_00V = 1,
  47. +  CV4_10V = 2,
  48. +  CV4_20V = 3,
  49. +};
  50. +
  51.  // constant current charge current limits
  52.  void axp2101_set_charge_current(int current_mA)
  53.  {
  54. @@ -508,6 +514,9 @@
  55.            chargecurrent_tbl[index+1] <= current_mA)
  56.          ++index;
  57.  
  58. +    // Limit Charger Voltage
  59. +    i2c_reg_modify1(AXP_PMU_BUS, AXP_PMU_ADDR,
  60. +                    AXP2101_REG_CV_SETTING, 0x07, CV4_20V, NULL);
  61.      i2c_reg_modify1(AXP_PMU_BUS, AXP_PMU_ADDR,
  62.                      AXP2101_REG_ICC_SETTING, 0x0f, index, NULL);
  63.  }
  64. @@ -555,11 +564,11 @@
  65.      (void)data;
  66.  
  67.      static const char* const adc_names[] = {
  68. -        "V_bat", "V_bus", "V_sys", "V_ts", "V_die",
  69. +        "V_bat", "V_bus", "V_sys", "V_ts", "T_die",
  70.      };
  71.  
  72.      static const char* const adc_units[] = {
  73. -        "mV", "mV", "mV", "mV", "C*100",
  74. +        "mV", "mV", "mV", "mV", "°C",
  75.      };
  76.  
  77.      static const char* const supply_names[] = {
  78. @@ -574,12 +583,24 @@
  79.          if(raw_value == INT_MIN) {
  80.              snprintf(buf, buflen, "%s: [Disabled]", adc_names[adc]);
  81.              return buf;
  82. +        } else if(raw_value > 0x1fff) { // Values greater makes no sense
  83. +            snprintf(buf, buflen, "%s: [Floating]", adc_names[adc]);
  84. +            return buf;
  85.          }
  86.  
  87.          int value = axp2101_adc_conv_raw(adc, raw_value);
  88. -        snprintf(buf, buflen, "%s: %d %s", adc_names[adc],
  89. +        if(adc == AXP2101_ADC_DIE_TEMPERATURE) {
  90. +    /* From https://github.com/lewisxhe/XPowersLib src/REG/AXP2101Constants.h, integer only ver. */
  91. +    #define XPOWERS_AXP2101_TEMP_CONVERSION(raw)  (220 + ( (7274 - raw) / 2) )
  92. +            int die_temp = XPOWERS_AXP2101_TEMP_CONVERSION(value);
  93. +            int die_temp_abs = die_temp<0 ? -die_temp : die_temp;
  94. +            snprintf(buf, buflen, "%s: %s%d.%1d %s", adc_names[adc],
  95. +                     die_temp<0 ? "-":"+", die_temp_abs/10, die_temp_abs%10,
  96. +                     adc_units[adc]);
  97. +        } else {
  98. +            snprintf(buf, buflen, "%s: %d %s", adc_names[adc],
  99.                      value, adc_units[adc]);
  100. -
  101. +        }
  102.          return buf;
  103.      }
  104.  
  105. @@ -598,13 +619,16 @@
  106.  
  107.      switch(item) {
  108.      case AXP_DEBUG_BATTERY_STATUS: {
  109. +        int batreg = i2c_reg_read1(AXP_PMU_BUS, AXP_PMU_ADDR, AXP2101_REG_BATT_PERCENTAGE);
  110.          switch(axp2101_battery_status()) {
  111.          case AXP2101_BATT_FULL:
  112.              return "Battery: Full";
  113.          case AXP2101_BATT_CHARGING:
  114. -            return "Battery: Charging";
  115. +            snprintf(buf, buflen,"Battery: Charging [%2d%%]", batreg);
  116. +            return buf;
  117.          case AXP2101_BATT_DISCHARGING:
  118. -            return "Battery: Discharging";
  119. +            snprintf(buf, buflen, "Battery: Discharging [%2d%%]", batreg);
  120. +            return buf;
  121.          default:
  122.              return "Battery: Unknown";
  123.          }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment