Statistics
| Branch: | Tag: | Revision:

amiro-os / test / periphery-lld / HMC5883L_v1 / aos_test_HMC5883L.c @ 57a5d1df

History | View | Annotate | Download (10.585 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 <aos_test_HMC5883L.h>
21

    
22
#if (AMIROOS_CFG_TESTS_ENABLE == true) || 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
 * @brief   HMC5338L test function.
50
 *
51
 * @param[in] stream  Stream for intput/output.
52
 * @param[in] test    Test object.
53
 *
54
 * @return            Test result value.
55
 */
56
aos_testresult_t aosTestHmc5883lFunc(BaseSequentialStream* stream, const aos_test_t* test)
57
{
58
  aosDbgCheck(test->data != NULL && (((aos_test_hmc5883ldata_t*)(test->data))->drdyEvtSrc != NULL));
59

    
60
  // local variables
61
  aos_testresult_t result = {0, 0};
62
  uint32_t status;
63
  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
  chprintf(stream, "check ID...\n");
76
  status = hmc5883l_lld_check(((aos_test_hmc5883ldata_t*)test->data)->driver, rxbuffer, 3, ((aos_test_hmc5883ldata_t*)test->data)->timeout);
77
  if (status == APAL_STATUS_SUCCESS && rxbuffer[0] == HMC5883L_LLD_IDENTIFICATION_A
78
                                   && rxbuffer[1] == HMC5883L_LLD_IDENTIFICATION_B
79
                                   && rxbuffer[2] == HMC5883L_LLD_IDENTIFICATION_C) {
80
    aosTestPassed(stream, &result);
81
  } else {
82
    chprintf(stream, "\tfailed\n");
83
    ++result.failed;
84
    chprintf(stream, "\t\tstatus: %d, idA: %d, idB: %d, idC: %d\n", status, rxbuffer[0], rxbuffer[1], rxbuffer[2]);
85
    chprintf(stream, "\t\texpected idA: %d, idB: %d, idC: %d\n", HMC5883L_LLD_IDENTIFICATION_A, HMC5883L_LLD_IDENTIFICATION_B, HMC5883L_LLD_IDENTIFICATION_C);
86
  }
87

    
88
  chprintf(stream, "read register...\n");
89
  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);
90
  if ((status == APAL_STATUS_OK || status == APAL_STATUS_WARNING) && data == HMC5883L_LLD_IDENTIFICATION_A) {
91
    aosTestPassed(stream, &result);
92
  } else {
93
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
94
  }
95

    
96
  chprintf(stream, "write register...\n");
97
  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);
98
  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);
99
  if ((status == APAL_STATUS_SUCCESS || status == APAL_STATUS_WARNING) && data == 0x70) {
100
    aosTestPassed(stream, &result);
101
  } else {
102
    aosTestFailedMsg(stream, &result, "0x%08X\n, data: d", status, data);
103
  }
104

    
105
  chprintf(stream, "read configuration\n");
106
  status = hmc5883l_lld_read_config(((aos_test_hmc5883ldata_t*)test->data)->driver, &cfg, ((aos_test_hmc5883ldata_t*)test->data)->timeout);
107
  if (status == APAL_STATUS_SUCCESS) {
108
    aosTestPassed(stream, &result);
109
  } else {
110
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
111
  }
112

    
113
  chprintf(stream, "write configuration\n");
114
  cfg.avg = HMC5883L_LLD_AVG1;
115
  cfg.outrate = HMC5883L_LLD_75_HZ;
116
  cfg.mbias = HMC5883L_LLD_MB_NORMAL;
117
  cfg.gain = HMC5883L_LLD_GN_0_GA;
118
  cfg.highspeed = HMC5883L_LLD_HS_DISABLE;
119
  cfg.mode = HMC5883L_LLD_MM_CONTINUOUS;
120
  status = hmc5883l_lld_write_config(((aos_test_hmc5883ldata_t*)test->data)->driver, cfg, ((aos_test_hmc5883ldata_t*)test->data)->timeout);
121
  status |= hmc5883l_lld_read_config(((aos_test_hmc5883ldata_t*)test->data)->driver, &cfg, ((aos_test_hmc5883ldata_t*)test->data)->timeout);
122
  if (status == APAL_STATUS_OK &&
123
      cfg.avg == HMC5883L_LLD_AVG1 &&
124
      cfg.outrate == HMC5883L_LLD_75_HZ &&
125
      cfg.mbias == HMC5883L_LLD_MB_NORMAL &&
126
      cfg.gain == HMC5883L_LLD_GN_0_GA &&
127
      cfg.highspeed == HMC5883L_LLD_HS_DISABLE &&
128
      cfg.mode == HMC5883L_LLD_MM_CONTINUOUS) {
129
    aosTestPassed(stream, &result);
130
  } else {
131
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
132
  }
133

    
134
  chprintf(stream, "read all data for five seconds...\n");
135
  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);
136
  for (uint8_t i = 0; i < 5; ++i) {
137
    status |= hmc5883l_lld_read_data(((aos_test_hmc5883ldata_t*)test->data)->driver, mdata, ((aos_test_hmc5883ldata_t*)test->data)->timeout);
138
    chprintf(stream, "\t\tX = 0x%04X\tY = 0x%04X\tZ = 0x%04X\n",mdata[0],mdata[1],mdata[2]);
139
    aosThdSSleep(1);
140
  }
141
  if (status == APAL_STATUS_OK) {
142
    aosTestPassed(stream, &result);
143
  } else {
144
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
145
  }
146

    
147
  chprintf(stream, "read status...\n");
148
  status = hmc5883l_lld_read_lock(((aos_test_hmc5883ldata_t*)test->data)->driver, &state, ((aos_test_hmc5883ldata_t*)test->data)->timeout);
149
  chprintf(stream, "\t\tsensor lock: %d\n", state);
150
  status |= hmc5883l_lld_read_rdy(((aos_test_hmc5883ldata_t*)test->data)->driver, &state, ((aos_test_hmc5883ldata_t*)test->data)->timeout);
151
  chprintf(stream, "\t\tsensor rdy: %d\n", state);
152
  status |= hmc5883l_lld_read_status(((aos_test_hmc5883ldata_t*)test->data)->driver, &state, ((aos_test_hmc5883ldata_t*)test->data)->timeout);
153
  chprintf(stream, "\t\tsensor status: %d\n", state);
154
  if (status == APAL_STATUS_OK || status == APAL_STATUS_WARNING) {
155
    aosTestPassed(stream, &result);
156
  } else {
157
    aosTestFailedMsg(stream, &result, "0x%08X\n", status);
158
  }
159

    
160
  chprintf(stream, "interrupt (partial data read)...\n");
161
  chEvtRegister(((aos_test_hmc5883ldata_t*)test->data)->drdyEvtSrc, &el, 0);
162
  status = hmc5883l_lld_write_config(((aos_test_hmc5883ldata_t*)test->data)->driver, cfg, ((aos_test_hmc5883ldata_t*)test->data)->timeout);
163
  aosThdSSleep(1);
164
  status |= hmc5883l_lld_read_data(((aos_test_hmc5883ldata_t*)test->data)->driver, mdata, ((aos_test_hmc5883ldata_t*)test->data)->timeout);
165
  aosThdMSleep(10);
166
  event_mask = chEvtWaitOneTimeout(EVENT_MASK(0), TIME_IMMEDIATE);
167
  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);
168
  aosThdMSleep(10);
169
  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);
170
  aosThdMSleep(10);
171
  event_mask = chEvtWaitOneTimeout(EVENT_MASK(0), TIME_IMMEDIATE);
172
  chprintf(stream, "\t\teventmask = 0x%08X (should be 0)\n", event_mask);
173
  if (event_mask != EVENT_MASK(0)) {
174
    aosTestPassed(stream, &result);
175
  } else {
176
    aosTestFailed(stream, &result);
177
  }
178

    
179
  chprintf(stream, "interrupt (full data read)...\n");
180
  status |= hmc5883l_lld_read_data(((aos_test_hmc5883ldata_t*)test->data)->driver, mdata, ((aos_test_hmc5883ldata_t*)test->data)->timeout);
181
  event_mask = chEvtWaitOneTimeout(EVENT_MASK(0), chTimeUS2I(100 * MICROSECONDS_PER_MILLISECOND));
182
  chprintf(stream, "\t\teventmask = 0x%08X (should be 1)\n", event_mask);
183
  if (event_mask == EVENT_MASK(0)) {
184
    aosTestPassed(stream, &result);
185
  } else {
186
    aosTestFailed(stream, &result);
187
  }
188

    
189
  chprintf(stream, "read data based on interrupt...\n");
190
  status = hmc5883l_lld_read_config(((aos_test_hmc5883ldata_t*)test->data)->driver, &cfg, ((aos_test_hmc5883ldata_t*)test->data)->timeout);
191
  uint8_t mode = HMC5883L_LLD_MM_SINGLE;
192
  cfg.mode = mode;
193
  status |= hmc5883l_lld_write_config(((aos_test_hmc5883ldata_t*)test->data)->driver, cfg, ((aos_test_hmc5883ldata_t*)test->data)->timeout);
194
  aosSysGetUptime(&start);
195
  aosSysGetUptime(&t);
196
  status |= hmc5883l_lld_read_data(((aos_test_hmc5883ldata_t*)test->data)->driver, mdata, ((aos_test_hmc5883ldata_t*)test->data)->timeout);
197
  while (t - start < MICROSECONDS_PER_SECOND) {
198
    event_mask = chEvtWaitOneTimeout(EVENT_MASK(0), chTimeUS2I(1000));
199
    if (event_mask == EVENT_MASK(0)) {
200
      status |= hmc5883l_lld_read_data(((aos_test_hmc5883ldata_t*)test->data)->driver, mdata, ((aos_test_hmc5883ldata_t*)test->data)->timeout);
201
      data_reads++;
202
      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);
203
    }
204
    aosSysGetUptime(&t);
205
  }
206
  chEvtUnregister(((aos_test_hmc5883ldata_t*)test->data)->drdyEvtSrc, &el);
207
  aosTestInfoMsg(stream,"Read data %u times in a second\n", data_reads);
208

    
209
  aosTestInfoMsg(stream,"driver object memory footprint: %u bytes\n", sizeof(HMC5883LDriver));
210

    
211
  return result;
212
}
213

    
214
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
215