Statistics
| Branch: | Tag: | Revision:

amiro-os / unittests / periphery-lld / src / ut_alld_bq27500_bq24103a.c @ f39a7138

History | View | Annotate | Download (6.319 KB)

1 e545e620 Thomas Schöpping
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2018  Thomas Schöpping et al.
4

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU General Public License for more details.
14

15
You should have received a copy of the GNU General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#include <ut_alld_bq27500_bq24103a.h>
20
21
#if ((AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_USE_BQ27500) && defined(AMIROLLD_CFG_USE_BQ24103A)) || defined(__DOXYGEN__)
22
23
#include <aos_debug.h>
24
#include <chprintf.h>
25
#include <aos_thread.h>
26
27
aos_utresult_t utAlldBq27500Bq24103aFunc(BaseSequentialStream *stream, aos_unittest_t *ut)
28
{
29
  aosDbgCheck(ut->data != NULL && ((ut_bq27500bq24103adata_t*)ut->data)->bq27500 != NULL && ((ut_bq27500bq24103adata_t*)ut->data)->bq24103a != NULL);
30
31
  // local variables
32
  aos_utresult_t result = {0, 0};
33
  uint32_t status;
34
  uint16_t dst;
35
  bq27500_lld_flags_t flags;
36
  bq27500_lld_batgood_t bg;
37
  bq24103a_lld_enable_t charger_enabled;
38
  uint32_t sleeptime_s;
39
  bq24103a_lld_charge_state_t charge;
40
41
  chprintf(stream, "check for battery...\n");
42
  status = bq27500_lld_std_command(((ut_bq27500bq24103adata_t*)ut->data)->bq27500, BQ27500_LLD_STD_CMD_Flags, &dst, ((ut_bq27500bq24103adata_t*)ut->data)->timeout);
43
  flags.value = dst;
44
  chprintf(stream, "\t\tbattery detected: %s\n", flags.content.bat_det ? "yes" : "no");
45
  chprintf(stream, "\t\tbattery fully charged: %s\n", flags.content.fc ? "yes" : "no");
46
  status |= bq27500_lld_read_batgood(((ut_bq27500bq24103adata_t*)ut->data)->bq27500, &bg);
47
  chprintf(stream, "\t\tbattery good: %s\n", (bg == BQ27500_LLD_BATTERY_GOOD) ? "yes" : "no");
48
  if (status == APAL_STATUS_SUCCESS) {
49
    if (!flags.content.bat_det) {
50
      aosUtPassedMsg(stream, &result, "no battery detected, aborting\n");
51
      return result;
52
    } else if (!bg) {
53
      aosUtPassedMsg(stream, &result, "battery damaged, aborting\n");
54
      return result;
55
    } else {
56
      aosUtPassed(stream, &result);
57
    }
58
  } else {
59
    aosUtFailedMsg(stream, &result, "0x%08X; aborting\n", status);
60
    return result;
61
  }
62
63
  chprintf(stream, "get current charger setting...\n");
64
  status = bq24103a_lld_get_enabled(((ut_bq27500bq24103adata_t*)ut->data)->bq24103a, &charger_enabled);
65
  if (status == APAL_STATUS_SUCCESS) {
66
    aosUtPassedMsg(stream, &result, "currently %s\n", (charger_enabled == BQ24103A_LLD_ENABLED) ? "enabled" : "disabled");
67
  } else {
68
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
69
  }
70
71
  // disable/enable charger
72
  for (uint8_t iteration = 0; iteration < 2; ++iteration) {
73
    if (charger_enabled == BQ24103A_LLD_ENABLED) {
74
      sleeptime_s = 120;
75
      chprintf(stream, "disable charger...\n");
76
      status = bq24103a_lld_set_enabled(((ut_bq27500bq24103adata_t*)ut->data)->bq24103a, BQ24103A_LLD_DISABLED);
77
      aosThdSSleep(1);
78
      status |= bq24103a_lld_get_enabled(((ut_bq27500bq24103adata_t*)ut->data)->bq24103a, &charger_enabled);
79
      status |= (charger_enabled == BQ24103A_LLD_DISABLED) ? 0x0000 : 0x0100;
80
      for (uint32_t s = 0; s < sleeptime_s; ++s) {
81
        int nchars = chprintf(stream, "%us / %us", s, sleeptime_s);
82
        aosThdSSleep(1);
83
        for (int c = 0; c < nchars; ++c) {
84
          chprintf(stream, "\b \b");
85
        }
86
      }
87
      status |= bq24103a_lld_get_charge_status(((ut_bq27500bq24103adata_t*)ut->data)->bq24103a, &charge);
88
      chprintf(stream, "\t\tcharge status: %scharging\n", (charge == BQ24103A_LLD_CHARGING) ? "" : "not ");
89
      status |= bq27500_lld_std_command(((ut_bq27500bq24103adata_t*)ut->data)->bq27500, BQ27500_LLD_STD_CMD_TimeToFull, &dst, ((ut_bq27500bq24103adata_t*)ut->data)->timeout);
90
      if (status == APAL_STATUS_SUCCESS) {
91
        if (dst == 0xFFFF || dst == 0) {
92
          aosUtPassedMsg(stream, &result, "battery %sfull\n", flags.content.fc ? "" : "not ");
93
        } else {
94
          aosUtFailedMsg(stream, &result, "battery %sfull but charging (TTF = %umin)\n", flags.content.fc ? "" : "not ", dst);
95
        }
96
      } else {
97
        aosUtFailedMsg(stream, &result, "0x%08X\n", status);
98
      }
99
    }
100
    else { /* charger_enabled != BQ24103A_LLD_ENABLED */
101
      sleeptime_s = 90;
102
      chprintf(stream, "enable charger...\n");
103
      status = bq24103a_lld_set_enabled(((ut_bq27500bq24103adata_t*)ut->data)->bq24103a, BQ24103A_LLD_ENABLED);
104
      aosThdSSleep(1);
105
      status |= bq24103a_lld_get_enabled(((ut_bq27500bq24103adata_t*)ut->data)->bq24103a, &charger_enabled);
106
      status |= (charger_enabled == BQ24103A_LLD_ENABLED) ? 0x0000 : 0x0100;
107
      for (uint32_t s = 0; s < sleeptime_s; ++s) {
108
        int nchars = chprintf(stream, "%us / %us", s, sleeptime_s);
109
        aosThdSSleep(1);
110
        for (int c = 0; c < nchars; ++c) {
111
          chprintf(stream, "\b \b");
112
        }
113
      }
114
      status |= bq24103a_lld_get_charge_status(((ut_bq27500bq24103adata_t*)ut->data)->bq24103a, &charge);
115
      chprintf(stream, "\t\tcharge status: %scharging\n", (charge == BQ24103A_LLD_CHARGING) ? "" : "not ");
116
      status |= bq27500_lld_std_command(((ut_bq27500bq24103adata_t*)ut->data)->bq27500, BQ27500_LLD_STD_CMD_TimeToFull, &dst, ((ut_bq27500bq24103adata_t*)ut->data)->timeout);
117
      if (status == APAL_STATUS_SUCCESS) {
118
        if (((dst == 0xFFFF || dst == 0) && flags.content.fc) || (!(dst == 0xFFFF || dst == 0) && !flags.content.fc)) {
119
          aosUtPassedMsg(stream, &result, "battery %sfull and %scharging (TTF = %umin)\n", flags.content.fc ? "" : "not ", (dst != 0xFFFF || dst != 0) ? "" : "not ", dst);
120
        } else {
121
          aosUtFailedMsg(stream, &result, "battery %sfull and %scharging (TTF = %umin)\n", flags.content.fc ? "" : "not ", (dst != 0xFFFF || dst != 0) ? "" : "not ", dst);
122
        }
123
      } else {
124
        aosUtFailedMsg(stream, &result, "0x%08X\n", status);
125
      }
126
    }
127
128
    bq24103a_lld_get_enabled(((ut_bq27500bq24103adata_t*)ut->data)->bq24103a, &charger_enabled);
129
  }
130
131
  return result;
132
}
133
134
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_USE_BQ27500) && defined(AMIROLLD_CFG_USE_BQ24103A) */