Statistics
| Branch: | Tag: | Revision:

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

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