Statistics
| Branch: | Tag: | Revision:

amiro-os / unittests / periphery-lld / src / ut_alld_HMC5883L_v1.c @ ddf34c3d

History | View | Annotate | Download (10.3 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

    
21
#if ((AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_HMC5883L) && (AMIROLLD_CFG_HMC5883L == 1)) || defined(__DOXYGEN__)
22

    
23
#include <ut_alld_HMC5883L_v1.h>
24

    
25
/******************************************************************************/
26
/* LOCAL DEFINITIONS                                                          */
27
/******************************************************************************/
28

    
29
/******************************************************************************/
30
/* EXPORTED VARIABLES                                                         */
31
/******************************************************************************/
32

    
33
/******************************************************************************/
34
/* LOCAL TYPES                                                                */
35
/******************************************************************************/
36

    
37
/******************************************************************************/
38
/* LOCAL VARIABLES                                                            */
39
/******************************************************************************/
40

    
41
/******************************************************************************/
42
/* LOCAL FUNCTIONS                                                            */
43
/******************************************************************************/
44

    
45
/******************************************************************************/
46
/* EXPORTED FUNCTIONS                                                         */
47
/******************************************************************************/
48

    
49
/**
50
 * @brief   HMC5338L unit test function.
51
 *
52
 * @param[in] stream  Stream for intput/output.
53
 * @param[in] ut      Unit test object.
54
 *
55
 * @return            Unit test result value.
56
 */
57
aos_utresult_t utAlldHmc5883lFunc(BaseSequentialStream* stream, aos_unittest_t* ut)
58
{
59
  aosDbgCheck(ut->data != NULL && (((ut_hmc5883ldata_t*)(ut->data))->drdyEvtSrc != NULL));
60

    
61
  // local variables
62
  aos_utresult_t result = {0, 0};
63
  uint32_t status;
64
  uint8_t rxbuffer[3];
65
  uint8_t data;
66
  uint8_t conf = 0x70;
67
  hmc5883l_lld_config_t cfg;
68
  uint16_t mdata[3];
69
  uint8_t state;
70
  eventmask_t event_mask = 0;
71
  event_listener_t el;
72
  uint16_t data_reads = 0;
73
  aos_timestamp_t start;
74
  aos_timestamp_t t;
75

    
76
  chprintf(stream, "check ID...\n");
77
  status = hmc5883l_lld_check(((ut_hmc5883ldata_t*)ut->data)->driver, rxbuffer, 3, ((ut_hmc5883ldata_t*)ut->data)->timeout);
78
  if (status == APAL_STATUS_SUCCESS && rxbuffer[0] == HMC5883L_LLD_IDENTIFICATION_A
79
                                   && rxbuffer[1] == HMC5883L_LLD_IDENTIFICATION_B
80
                                   && rxbuffer[2] == HMC5883L_LLD_IDENTIFICATION_C) {
81
    aosUtPassed(stream, &result);
82
  } else {
83
    chprintf(stream, "\tfailed\n");
84
    ++result.failed;
85
    chprintf(stream, "\t\tstatus: %d, idA: %d, idB: %d, idC: %d\n", status, rxbuffer[0], rxbuffer[1], rxbuffer[2]);
86
    chprintf(stream, "\t\texpected idA: %d, idB: %d, idC: %d\n", HMC5883L_LLD_IDENTIFICATION_A, HMC5883L_LLD_IDENTIFICATION_B, HMC5883L_LLD_IDENTIFICATION_C);
87
  }
88

    
89
  chprintf(stream, "read register...\n");
90
  status = hmc5883l_lld_read_register(((ut_hmc5883ldata_t*)ut->data)->driver, HMC5883L_LLD_REGISTER_IDENTIFICATION_A, &data, 1, ((ut_hmc5883ldata_t*)ut->data)->timeout);
91
  if ((status == APAL_STATUS_OK || status == APAL_STATUS_WARNING) && data == HMC5883L_LLD_IDENTIFICATION_A) {
92
    aosUtPassed(stream, &result);
93
  } else {
94
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
95
  }
96

    
97
  chprintf(stream, "write register...\n");
98
  status = hmc5883l_lld_write_register(((ut_hmc5883ldata_t*)ut->data)->driver, HMC5883L_LLD_REGISTER_CONFIG_A, &conf, 1, ((ut_hmc5883ldata_t*)ut->data)->timeout);
99
  status |= hmc5883l_lld_read_register(((ut_hmc5883ldata_t*)ut->data)->driver, HMC5883L_LLD_REGISTER_CONFIG_A, &data, 1, ((ut_hmc5883ldata_t*)ut->data)->timeout);
100
  if ((status == APAL_STATUS_SUCCESS || status == APAL_STATUS_WARNING) && data == 0x70) {
101
    aosUtPassed(stream, &result);
102
  } else {
103
    aosUtFailedMsg(stream, &result, "0x%08X\n, data: d", status, data);
104
  }
105

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

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

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

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

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

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

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

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

    
212
  return result;
213
}
214

    
215
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_HMC5883L) && (AMIROLLD_CFG_HMC5883L == 1) */