Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (10.238 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 <ut_alld_hmc5883l.h>
20

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

    
23
/******************************************************************************/
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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
210
  return result;
211
}
212

    
213
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_USE_HMC5883L) */