Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.766 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2019  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 <amiroos.h>
20
#include <ut_alld_bq27500_v1_bq241xx_v1.h>
21

    
22
#if ((AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_BQ27500) && (AMIROLLD_CFG_BQ27500 == 1) && defined(AMIROLLD_CFG_BQ241xx) && (AMIROLLD_CFG_BQ241xx == 1)) || defined(__DOXYGEN__)
23

    
24
/******************************************************************************/
25
/* LOCAL DEFINITIONS                                                          */
26
/******************************************************************************/
27

    
28
/******************************************************************************/
29
/* EXPORTED VARIABLES                                                         */
30
/******************************************************************************/
31

    
32
/******************************************************************************/
33
/* LOCAL TYPES                                                                */
34
/******************************************************************************/
35

    
36
/******************************************************************************/
37
/* LOCAL VARIABLES                                                            */
38
/******************************************************************************/
39

    
40
/******************************************************************************/
41
/* LOCAL FUNCTIONS                                                            */
42
/******************************************************************************/
43

    
44
/******************************************************************************/
45
/* EXPORTED FUNCTIONS                                                         */
46
/******************************************************************************/
47

    
48

    
49
aos_utresult_t utAlldBq27500Bq241xxFunc(BaseSequentialStream *stream, aos_unittest_t *ut)
50
{
51
  aosDbgCheck(ut->data != NULL && ((ut_bq27500bq241xxdata_t*)ut->data)->bq27500 != NULL && ((ut_bq27500bq241xxdata_t*)ut->data)->bq241xx != NULL);
52

    
53
  // local variables
54
  aos_utresult_t result = {0, 0};
55
  uint32_t status;
56
  uint16_t dst;
57
  bq27500_lld_flags_t flags;
58
  bq27500_lld_batgood_t bg;
59
  bq241xx_lld_enable_t charger_enabled;
60
  uint32_t sleeptime_s;
61
  bq241xx_lld_charge_state_t charge;
62

    
63
  chprintf(stream, "check for battery...\n");
64
  status = bq27500_lld_std_command(((ut_bq27500bq241xxdata_t*)ut->data)->bq27500, BQ27500_LLD_STD_CMD_Flags, &dst, ((ut_bq27500bq241xxdata_t*)ut->data)->timeout);
65
  flags.value = dst;
66
  chprintf(stream, "\t\tbattery detected: %s\n", flags.content.bat_det ? "yes" : "no");
67
  chprintf(stream, "\t\tbattery fully charged: %s\n", flags.content.fc ? "yes" : "no");
68
  status |= bq27500_lld_read_batgood(((ut_bq27500bq241xxdata_t*)ut->data)->bq27500, &bg);
69
  chprintf(stream, "\t\tbattery good: %s\n", (bg == BQ27500_LLD_BATTERY_GOOD) ? "yes" : "no");
70
  if (status == APAL_STATUS_SUCCESS) {
71
    if (!flags.content.bat_det) {
72
      aosUtPassedMsg(stream, &result, "no battery detected, aborting\n");
73
      return result;
74
    } else if (!bg) {
75
      aosUtPassedMsg(stream, &result, "battery damaged, aborting\n");
76
      return result;
77
    } else {
78
      aosUtPassed(stream, &result);
79
    }
80
  } else {
81
    aosUtFailedMsg(stream, &result, "0x%08X; aborting\n", status);
82
    return result;
83
  }
84

    
85
  chprintf(stream, "get current charger setting...\n");
86
  status = bq241xx_lld_get_enabled(((ut_bq27500bq241xxdata_t*)ut->data)->bq241xx, &charger_enabled);
87
  if (status == APAL_STATUS_SUCCESS) {
88
    aosUtPassedMsg(stream, &result, "currently %s\n", (charger_enabled == BQ241xx_LLD_ENABLED) ? "enabled" : "disabled");
89
  } else {
90
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
91
  }
92

    
93
  // disable/enable charger
94
  for (uint8_t iteration = 0; iteration < 2; ++iteration) {
95
    if (charger_enabled == BQ241xx_LLD_ENABLED) {
96
      sleeptime_s = 120;
97
      chprintf(stream, "disable charger...\n");
98
      status = bq241xx_lld_set_enabled(((ut_bq27500bq241xxdata_t*)ut->data)->bq241xx, BQ241xx_LLD_DISABLED);
99
      aosThdSSleep(1);
100
      status |= bq241xx_lld_get_enabled(((ut_bq27500bq241xxdata_t*)ut->data)->bq241xx, &charger_enabled);
101
      status |= (charger_enabled == BQ241xx_LLD_DISABLED) ? 0x0000 : 0x0100;
102
      for (uint32_t s = 0; s < sleeptime_s; ++s) {
103
        int nchars = chprintf(stream, "%us / %us", s, sleeptime_s);
104
        aosThdSSleep(1);
105
        for (int c = 0; c < nchars; ++c) {
106
          chprintf(stream, "\b \b");
107
        }
108
      }
109
      status |= bq241xx_lld_get_charge_status(((ut_bq27500bq241xxdata_t*)ut->data)->bq241xx, &charge);
110
      chprintf(stream, "\t\tcharge status: %scharging\n", (charge == BQ241xx_LLD_CHARGING) ? "" : "not ");
111
      status |= bq27500_lld_std_command(((ut_bq27500bq241xxdata_t*)ut->data)->bq27500, BQ27500_LLD_STD_CMD_TimeToFull, &dst, ((ut_bq27500bq241xxdata_t*)ut->data)->timeout);
112
      if (status == APAL_STATUS_SUCCESS) {
113
        if (dst == 0xFFFF || dst == 0) {
114
          aosUtPassedMsg(stream, &result, "battery %sfull\n", flags.content.fc ? "" : "not ");
115
        } else {
116
          aosUtFailedMsg(stream, &result, "battery %sfull but charging (TTF = %umin)\n", flags.content.fc ? "" : "not ", dst);
117
        }
118
      } else {
119
        aosUtFailedMsg(stream, &result, "0x%08X\n", status);
120
      }
121
    }
122
    else { /* charger_enabled != BQ241xx_LLD_ENABLED */
123
      sleeptime_s = 90;
124
      chprintf(stream, "enable charger...\n");
125
      status = bq241xx_lld_set_enabled(((ut_bq27500bq241xxdata_t*)ut->data)->bq241xx, BQ241xx_LLD_ENABLED);
126
      aosThdSSleep(1);
127
      status |= bq241xx_lld_get_enabled(((ut_bq27500bq241xxdata_t*)ut->data)->bq241xx, &charger_enabled);
128
      status |= (charger_enabled == BQ241xx_LLD_ENABLED) ? 0x0000 : 0x0100;
129
      for (uint32_t s = 0; s < sleeptime_s; ++s) {
130
        int nchars = chprintf(stream, "%us / %us", s, sleeptime_s);
131
        aosThdSSleep(1);
132
        for (int c = 0; c < nchars; ++c) {
133
          chprintf(stream, "\b \b");
134
        }
135
      }
136
      status |= bq241xx_lld_get_charge_status(((ut_bq27500bq241xxdata_t*)ut->data)->bq241xx, &charge);
137
      chprintf(stream, "\t\tcharge status: %scharging\n", (charge == BQ241xx_LLD_CHARGING) ? "" : "not ");
138
      status |= bq27500_lld_std_command(((ut_bq27500bq241xxdata_t*)ut->data)->bq27500, BQ27500_LLD_STD_CMD_TimeToFull, &dst, ((ut_bq27500bq241xxdata_t*)ut->data)->timeout);
139
      if (status == APAL_STATUS_SUCCESS) {
140
        if (((dst == 0xFFFF || dst == 0) && flags.content.fc) || (!(dst == 0xFFFF || dst == 0) && !flags.content.fc)) {
141
          aosUtPassedMsg(stream, &result, "battery %sfull and %scharging (TTF = %umin)\n", flags.content.fc ? "" : "not ", (dst != 0xFFFF || dst != 0) ? "" : "not ", dst);
142
        } else {
143
          aosUtFailedMsg(stream, &result, "battery %sfull and %scharging (TTF = %umin)\n", flags.content.fc ? "" : "not ", (dst != 0xFFFF || dst != 0) ? "" : "not ", dst);
144
        }
145
      } else {
146
        aosUtFailedMsg(stream, &result, "0x%08X\n", status);
147
      }
148
    }
149

    
150
    bq241xx_lld_get_enabled(((ut_bq27500bq241xxdata_t*)ut->data)->bq241xx, &charger_enabled);
151
  }
152

    
153
  return result;
154
}
155

    
156
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_BQ27500) && (AMIROLLD_CFG_BQ27500 == 1) && defined(AMIROLLD_CFG_BQ241xx) && (AMIROLLD_CFG_BQ241xx == 1) */