Statistics
| Branch: | Tag: | Revision:

amiro-os / unittests / periphery-lld / src / ut_alld_bq27500_v1_bq241xx_v1.c @ ddf34c3d

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