Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.682 KB)

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

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 f3ac1c96 Thomas Schöpping
/******************************************************************************/
24
/* LOCAL DEFINITIONS                                                          */
25
/******************************************************************************/
26
27
/******************************************************************************/
28
/* EXPORTED VARIABLES                                                         */
29
/******************************************************************************/
30
31
/******************************************************************************/
32
/* LOCAL TYPES                                                                */
33
/******************************************************************************/
34
35
/******************************************************************************/
36
/* LOCAL VARIABLES                                                            */
37
/******************************************************************************/
38
39
/******************************************************************************/
40
/* LOCAL FUNCTIONS                                                            */
41
/******************************************************************************/
42
43
/******************************************************************************/
44
/* EXPORTED FUNCTIONS                                                         */
45
/******************************************************************************/
46
47 e545e620 Thomas Schöpping
48
aos_utresult_t utAlldBq27500Bq24103aFunc(BaseSequentialStream *stream, aos_unittest_t *ut)
49
{
50
  aosDbgCheck(ut->data != NULL && ((ut_bq27500bq24103adata_t*)ut->data)->bq27500 != NULL && ((ut_bq27500bq24103adata_t*)ut->data)->bq24103a != NULL);
51
52
  // local variables
53
  aos_utresult_t result = {0, 0};
54
  uint32_t status;
55
  uint16_t dst;
56
  bq27500_lld_flags_t flags;
57
  bq27500_lld_batgood_t bg;
58
  bq24103a_lld_enable_t charger_enabled;
59
  uint32_t sleeptime_s;
60
  bq24103a_lld_charge_state_t charge;
61
62
  chprintf(stream, "check for battery...\n");
63
  status = bq27500_lld_std_command(((ut_bq27500bq24103adata_t*)ut->data)->bq27500, BQ27500_LLD_STD_CMD_Flags, &dst, ((ut_bq27500bq24103adata_t*)ut->data)->timeout);
64
  flags.value = dst;
65
  chprintf(stream, "\t\tbattery detected: %s\n", flags.content.bat_det ? "yes" : "no");
66
  chprintf(stream, "\t\tbattery fully charged: %s\n", flags.content.fc ? "yes" : "no");
67
  status |= bq27500_lld_read_batgood(((ut_bq27500bq24103adata_t*)ut->data)->bq27500, &bg);
68
  chprintf(stream, "\t\tbattery good: %s\n", (bg == BQ27500_LLD_BATTERY_GOOD) ? "yes" : "no");
69
  if (status == APAL_STATUS_SUCCESS) {
70
    if (!flags.content.bat_det) {
71
      aosUtPassedMsg(stream, &result, "no battery detected, aborting\n");
72
      return result;
73
    } else if (!bg) {
74
      aosUtPassedMsg(stream, &result, "battery damaged, aborting\n");
75
      return result;
76
    } else {
77
      aosUtPassed(stream, &result);
78
    }
79
  } else {
80
    aosUtFailedMsg(stream, &result, "0x%08X; aborting\n", status);
81
    return result;
82
  }
83
84
  chprintf(stream, "get current charger setting...\n");
85
  status = bq24103a_lld_get_enabled(((ut_bq27500bq24103adata_t*)ut->data)->bq24103a, &charger_enabled);
86
  if (status == APAL_STATUS_SUCCESS) {
87
    aosUtPassedMsg(stream, &result, "currently %s\n", (charger_enabled == BQ24103A_LLD_ENABLED) ? "enabled" : "disabled");
88
  } else {
89
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
90
  }
91
92
  // disable/enable charger
93
  for (uint8_t iteration = 0; iteration < 2; ++iteration) {
94
    if (charger_enabled == BQ24103A_LLD_ENABLED) {
95
      sleeptime_s = 120;
96
      chprintf(stream, "disable charger...\n");
97
      status = bq24103a_lld_set_enabled(((ut_bq27500bq24103adata_t*)ut->data)->bq24103a, BQ24103A_LLD_DISABLED);
98
      aosThdSSleep(1);
99
      status |= bq24103a_lld_get_enabled(((ut_bq27500bq24103adata_t*)ut->data)->bq24103a, &charger_enabled);
100
      status |= (charger_enabled == BQ24103A_LLD_DISABLED) ? 0x0000 : 0x0100;
101
      for (uint32_t s = 0; s < sleeptime_s; ++s) {
102
        int nchars = chprintf(stream, "%us / %us", s, sleeptime_s);
103
        aosThdSSleep(1);
104
        for (int c = 0; c < nchars; ++c) {
105
          chprintf(stream, "\b \b");
106
        }
107
      }
108
      status |= bq24103a_lld_get_charge_status(((ut_bq27500bq24103adata_t*)ut->data)->bq24103a, &charge);
109
      chprintf(stream, "\t\tcharge status: %scharging\n", (charge == BQ24103A_LLD_CHARGING) ? "" : "not ");
110
      status |= bq27500_lld_std_command(((ut_bq27500bq24103adata_t*)ut->data)->bq27500, BQ27500_LLD_STD_CMD_TimeToFull, &dst, ((ut_bq27500bq24103adata_t*)ut->data)->timeout);
111
      if (status == APAL_STATUS_SUCCESS) {
112
        if (dst == 0xFFFF || dst == 0) {
113
          aosUtPassedMsg(stream, &result, "battery %sfull\n", flags.content.fc ? "" : "not ");
114
        } else {
115
          aosUtFailedMsg(stream, &result, "battery %sfull but charging (TTF = %umin)\n", flags.content.fc ? "" : "not ", dst);
116
        }
117
      } else {
118
        aosUtFailedMsg(stream, &result, "0x%08X\n", status);
119
      }
120
    }
121
    else { /* charger_enabled != BQ24103A_LLD_ENABLED */
122
      sleeptime_s = 90;
123
      chprintf(stream, "enable charger...\n");
124
      status = bq24103a_lld_set_enabled(((ut_bq27500bq24103adata_t*)ut->data)->bq24103a, BQ24103A_LLD_ENABLED);
125
      aosThdSSleep(1);
126
      status |= bq24103a_lld_get_enabled(((ut_bq27500bq24103adata_t*)ut->data)->bq24103a, &charger_enabled);
127
      status |= (charger_enabled == BQ24103A_LLD_ENABLED) ? 0x0000 : 0x0100;
128
      for (uint32_t s = 0; s < sleeptime_s; ++s) {
129
        int nchars = chprintf(stream, "%us / %us", s, sleeptime_s);
130
        aosThdSSleep(1);
131
        for (int c = 0; c < nchars; ++c) {
132
          chprintf(stream, "\b \b");
133
        }
134
      }
135
      status |= bq24103a_lld_get_charge_status(((ut_bq27500bq24103adata_t*)ut->data)->bq24103a, &charge);
136
      chprintf(stream, "\t\tcharge status: %scharging\n", (charge == BQ24103A_LLD_CHARGING) ? "" : "not ");
137
      status |= bq27500_lld_std_command(((ut_bq27500bq24103adata_t*)ut->data)->bq27500, BQ27500_LLD_STD_CMD_TimeToFull, &dst, ((ut_bq27500bq24103adata_t*)ut->data)->timeout);
138
      if (status == APAL_STATUS_SUCCESS) {
139
        if (((dst == 0xFFFF || dst == 0) && flags.content.fc) || (!(dst == 0xFFFF || dst == 0) && !flags.content.fc)) {
140
          aosUtPassedMsg(stream, &result, "battery %sfull and %scharging (TTF = %umin)\n", flags.content.fc ? "" : "not ", (dst != 0xFFFF || dst != 0) ? "" : "not ", dst);
141
        } else {
142
          aosUtFailedMsg(stream, &result, "battery %sfull and %scharging (TTF = %umin)\n", flags.content.fc ? "" : "not ", (dst != 0xFFFF || dst != 0) ? "" : "not ", dst);
143
        }
144
      } else {
145
        aosUtFailedMsg(stream, &result, "0x%08X\n", status);
146
      }
147
    }
148
149
    bq24103a_lld_get_enabled(((ut_bq27500bq24103adata_t*)ut->data)->bq24103a, &charger_enabled);
150
  }
151
152
  return result;
153
}
154
155
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_USE_BQ27500) && defined(AMIROLLD_CFG_USE_BQ24103A) */