Statistics
| Branch: | Tag: | Revision:

amiro-os / test / periphery-lld / HMC5883L_v1 / aos_test_HMC5883L.c @ 7368d8da

History | View | Annotate | Download (10.605 KB)

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