Statistics
| Branch: | Tag: | Revision:

amiro-os / unittests / periphery-lld / src / ut_alld_hmc5883l.c @ f3ac1c96

History | View | Annotate | Download (10.355 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_hmc5883l.h>
20
21
#if ((AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_USE_HMC5883L)) || 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 e545e620 Thomas Schöpping
#include <aos_debug.h>
47
#include <chprintf.h>
48
#include <alld_hmc5883l.h>
49
#include <aos_thread.h>
50
#include <aos_system.h>
51
52
/**
53
 * @brief   HMC5338L unit test function.
54
 *
55
 * @param[in] stream  Stream for intput/output.
56
 * @param[in] ut      Unit test object.
57
 *
58
 * @return            Unit test result value.
59
 */
60
aos_utresult_t utAlldHmc5883lFunc(BaseSequentialStream* stream, aos_unittest_t* ut)
61
{
62
  aosDbgCheck(ut->data != NULL && (((ut_hmc5883ldata_t*)(ut->data))->drdyEvtSrc != NULL));
63
64
  // local variables
65
  aos_utresult_t result = {0, 0};
66
  uint32_t status;
67
  uint8_t rxbuffer[3];
68
  uint8_t data;
69
  uint8_t conf = 0x70;
70
  hmc5883l_lld_config_t cfg;
71
  uint16_t mdata[3];
72
  uint8_t state;
73
  eventmask_t event_mask = 0;
74
  event_listener_t el;
75
  uint16_t data_reads = 0;
76
  aos_timestamp_t start;
77
  aos_timestamp_t t;
78
79
  chprintf(stream, "check ID...\n");
80
  status = hmc5883l_lld_check(((ut_hmc5883ldata_t*)ut->data)->driver, rxbuffer, 3, ((ut_hmc5883ldata_t*)ut->data)->timeout);
81
  if (status == APAL_STATUS_SUCCESS && rxbuffer[0] == HMC5883L_LLD_IDENTIFICATION_A
82
                                   && rxbuffer[1] == HMC5883L_LLD_IDENTIFICATION_B
83
                                   && rxbuffer[2] == HMC5883L_LLD_IDENTIFICATION_C) {
84
    aosUtPassed(stream, &result);
85
  } else {
86
    chprintf(stream, "\tfailed\n");
87
    ++result.failed;
88
    chprintf(stream, "\t\tstatus: %d, idA: %d, idB: %d, idC: %d\n", status, rxbuffer[0], rxbuffer[1], rxbuffer[2]);
89
    chprintf(stream, "\t\texpected idA: %d, idB: %d, idC: %d\n", HMC5883L_LLD_IDENTIFICATION_A, HMC5883L_LLD_IDENTIFICATION_B, HMC5883L_LLD_IDENTIFICATION_C);
90
  }
91
92
  chprintf(stream, "read register...\n");
93
  status = hmc5883l_lld_read_register(((ut_hmc5883ldata_t*)ut->data)->driver, HMC5883L_LLD_REGISTER_IDENTIFICATION_A, &data, 1, ((ut_hmc5883ldata_t*)ut->data)->timeout);
94
  if ((status == APAL_STATUS_OK || status == APAL_STATUS_WARNING) && data == HMC5883L_LLD_IDENTIFICATION_A) {
95
    aosUtPassed(stream, &result);
96
  } else {
97
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
98
  }
99
100
  chprintf(stream, "write register...\n");
101
  status = hmc5883l_lld_write_register(((ut_hmc5883ldata_t*)ut->data)->driver, HMC5883L_LLD_REGISTER_CONFIG_A, &conf, 1, ((ut_hmc5883ldata_t*)ut->data)->timeout);
102
  status |= hmc5883l_lld_read_register(((ut_hmc5883ldata_t*)ut->data)->driver, HMC5883L_LLD_REGISTER_CONFIG_A, &data, 1, ((ut_hmc5883ldata_t*)ut->data)->timeout);
103
  if ((status == APAL_STATUS_SUCCESS || status == APAL_STATUS_WARNING) && data == 0x70) {
104
    aosUtPassed(stream, &result);
105
  } else {
106
    aosUtFailedMsg(stream, &result, "0x%08X\n, data: d", status, data);
107
  }
108
109
  chprintf(stream, "read configuration\n");
110
  status = hmc5883l_lld_read_config(((ut_hmc5883ldata_t*)ut->data)->driver, &cfg, ((ut_hmc5883ldata_t*)ut->data)->timeout);
111
  if (status == APAL_STATUS_SUCCESS) {
112
    aosUtPassed(stream, &result);
113
  } else {
114
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
115
  }
116
117
  chprintf(stream, "write configuration\n");
118
  cfg.avg = HMC5883L_LLD_AVG1;
119
  cfg.outrate = HMC5883L_LLD_75_HZ;
120
  cfg.mbias = HMC5883L_LLD_MB_NORMAL;
121
  cfg.gain = HMC5883L_LLD_GN_0_GA;
122
  cfg.highspeed = HMC5883L_LLD_HS_DISABLE;
123
  cfg.mode = HMC5883L_LLD_MM_CONTINUOUS;
124
  status = hmc5883l_lld_write_config(((ut_hmc5883ldata_t*)ut->data)->driver, cfg, ((ut_hmc5883ldata_t*)ut->data)->timeout);
125
  status |= hmc5883l_lld_read_config(((ut_hmc5883ldata_t*)ut->data)->driver, &cfg, ((ut_hmc5883ldata_t*)ut->data)->timeout);
126
  if (status == APAL_STATUS_OK &&
127
      cfg.avg == HMC5883L_LLD_AVG1 &&
128
      cfg.outrate == HMC5883L_LLD_75_HZ &&
129
      cfg.mbias == HMC5883L_LLD_MB_NORMAL &&
130
      cfg.gain == HMC5883L_LLD_GN_0_GA &&
131
      cfg.highspeed == HMC5883L_LLD_HS_DISABLE &&
132
      cfg.mode == HMC5883L_LLD_MM_CONTINUOUS) {
133
    aosUtPassed(stream, &result);
134
  } else {
135
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
136
  }
137
138
  chprintf(stream, "read all data for five seconds...\n");
139
  status = hmc5883l_lld_set_register(((ut_hmc5883ldata_t*)ut->data)->driver,HMC5883L_LLD_REGISTER_MODE,HMC5883L_LLD_MM_CONTINUOUS,((ut_hmc5883ldata_t*)ut->data)->timeout);
140
  for (uint8_t i = 0; i < 5; ++i) {
141
    status |= hmc5883l_lld_read_data(((ut_hmc5883ldata_t*)ut->data)->driver, mdata, ((ut_hmc5883ldata_t*)ut->data)->timeout);
142
    chprintf(stream, "\t\tX = 0x%04X\tY = 0x%04X\tZ = 0x%04X\n",mdata[0],mdata[1],mdata[2]);
143
    aosThdSSleep(1);
144
  }
145
  if (status == APAL_STATUS_OK) {
146
    aosUtPassed(stream, &result);
147
  } else {
148
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
149
  }
150
151
  chprintf(stream, "read status...\n");
152
  status = hmc5883l_lld_read_lock(((ut_hmc5883ldata_t*)ut->data)->driver, &state, ((ut_hmc5883ldata_t*)ut->data)->timeout);
153
  chprintf(stream, "\t\tsensor lock: %d\n", state);
154
  status |= hmc5883l_lld_read_rdy(((ut_hmc5883ldata_t*)ut->data)->driver, &state, ((ut_hmc5883ldata_t*)ut->data)->timeout);
155
  chprintf(stream, "\t\tsensor rdy: %d\n", state);
156
  status |= hmc5883l_lld_read_status(((ut_hmc5883ldata_t*)ut->data)->driver, &state, ((ut_hmc5883ldata_t*)ut->data)->timeout);
157
  chprintf(stream, "\t\tsensor status: %d\n", state);
158
  if (status == APAL_STATUS_OK || status == APAL_STATUS_WARNING) {
159
    aosUtPassed(stream, &result);
160
  } else {
161
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
162
  }
163
164
  chprintf(stream, "interrupt (partial data read)...\n");
165
  chEvtRegister(((ut_hmc5883ldata_t*)ut->data)->drdyEvtSrc, &el, 0);
166
  status = hmc5883l_lld_write_config(((ut_hmc5883ldata_t*)ut->data)->driver, cfg, ((ut_hmc5883ldata_t*)ut->data)->timeout);
167
  aosThdSSleep(1);
168
  status |= hmc5883l_lld_read_data(((ut_hmc5883ldata_t*)ut->data)->driver, mdata, ((ut_hmc5883ldata_t*)ut->data)->timeout);
169
  aosThdMSleep(10);
170
  event_mask = chEvtWaitOneTimeout(EVENT_MASK(0), TIME_IMMEDIATE);
171
  status |= hmc5883l_lld_read_register(((ut_hmc5883ldata_t*)ut->data)->driver, HMC5883L_LLD_REGISTER_DATA_OUT_X_MSB, &data, 1, ((ut_hmc5883ldata_t*)ut->data)->timeout);
172
  aosThdMSleep(10);
173
  status |= hmc5883l_lld_read_register(((ut_hmc5883ldata_t*)ut->data)->driver, HMC5883L_LLD_REGISTER_DATA_OUT_X_LSB, &data, 1, ((ut_hmc5883ldata_t*)ut->data)->timeout);
174
  aosThdMSleep(10);
175
  event_mask = chEvtWaitOneTimeout(EVENT_MASK(0), TIME_IMMEDIATE);
176
  chprintf(stream, "\t\teventmask = 0x%08X (should be 0)\n", event_mask);
177
  if (event_mask != EVENT_MASK(0)) {
178
    aosUtPassed(stream, &result);
179
  } else {
180
    aosUtFailed(stream, &result);
181
  }
182
183
  chprintf(stream, "interrupt (full data read)...\n");
184
  status |= hmc5883l_lld_read_data(((ut_hmc5883ldata_t*)ut->data)->driver, mdata, ((ut_hmc5883ldata_t*)ut->data)->timeout);
185 1e5f7648 Thomas Schöpping
  event_mask = chEvtWaitOneTimeout(EVENT_MASK(0), chTimeUS2I(100 * MICROSECONDS_PER_MILLISECOND));
186 e545e620 Thomas Schöpping
  chprintf(stream, "\t\teventmask = 0x%08X (should be 1)\n", event_mask);
187
  if (event_mask == EVENT_MASK(0)) {
188
    aosUtPassed(stream, &result);
189
  } else {
190
    aosUtFailed(stream, &result);
191
  }
192
193
  chprintf(stream, "read data based on interrupt...\n");
194
  status = hmc5883l_lld_read_config(((ut_hmc5883ldata_t*)ut->data)->driver, &cfg, ((ut_hmc5883ldata_t*)ut->data)->timeout);
195
  uint8_t mode = HMC5883L_LLD_MM_SINGLE;
196
  cfg.mode = mode;
197
  status |= hmc5883l_lld_write_config(((ut_hmc5883ldata_t*)ut->data)->driver, cfg, ((ut_hmc5883ldata_t*)ut->data)->timeout);
198
  aosSysGetUptime(&start);
199
  aosSysGetUptime(&t);
200
  status |= hmc5883l_lld_read_data(((ut_hmc5883ldata_t*)ut->data)->driver, mdata, ((ut_hmc5883ldata_t*)ut->data)->timeout);
201
  while (t - start < MICROSECONDS_PER_SECOND) {
202 1e5f7648 Thomas Schöpping
    event_mask = chEvtWaitOneTimeout(EVENT_MASK(0), chTimeUS2I(1000));
203 e545e620 Thomas Schöpping
    if (event_mask == EVENT_MASK(0)) {
204
      status |= hmc5883l_lld_read_data(((ut_hmc5883ldata_t*)ut->data)->driver, mdata, ((ut_hmc5883ldata_t*)ut->data)->timeout);
205
      data_reads++;
206
      status = hmc5883l_lld_write_register(((ut_hmc5883ldata_t*)ut->data)->driver, HMC5883L_LLD_REGISTER_CONFIG_A+2, &mode, 1, ((ut_hmc5883ldata_t*)ut->data)->timeout);
207
    }
208
    aosSysGetUptime(&t);
209
  }
210
  chEvtUnregister(((ut_hmc5883ldata_t*)ut->data)->drdyEvtSrc, &el);
211
  aosUtInfoMsg(stream,"Read data %u times in a second\n", data_reads);
212
213
  aosUtInfoMsg(stream,"driver object memory footprint: %u bytes\n", sizeof(HMC5883LDriver));
214
215
  return result;
216
}
217
218
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_USE_HMC5883L) */