Statistics
| Branch: | Tag: | Revision:

amiro-os / unittests / periphery-lld / src / ut_alld_AT24C01B_v1.c @ 2a4dbf93

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

    
22
#if ((AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_CFG_AT24C01B) && (AMIROLLD_CFG_AT24C01B == 1)) || defined(__DOXYGEN__)
23

    
24
/******************************************************************************/
25
/* LOCAL DEFINITIONS                                                          */
26
/******************************************************************************/
27

    
28
/**
29
 * @brief   EEPROM address for page write/read access.
30
 */
31
#define _pageAddress                  (AT24C01B_LLD_SIZE_BYTES - 2*AT24C01B_LLD_PAGE_SIZE_BYTES)
32

    
33
/**
34
 * @brief   EEPROM address for byte write/read access.
35
 */
36
#define _byteAddress                  (_pageAddress + AT24C01B_LLD_PAGE_SIZE_BYTES - 1)
37

    
38
/**
39
 * @brief   Offset for overflow tests.
40
 */
41
#define _overflowOffset               3
42

    
43
/**
44
 * @brief   EEPROM address for page write/read access with overflow.
45
 */
46
#define _overflowAddress              (_pageAddress + _overflowOffset)
47

    
48
/**
49
 * @brief   Test data byte.
50
 */
51
#define _bytedata                     0xA5
52

    
53
/******************************************************************************/
54
/* EXPORTED VARIABLES                                                         */
55
/******************************************************************************/
56

    
57
/******************************************************************************/
58
/* LOCAL TYPES                                                                */
59
/******************************************************************************/
60

    
61
/******************************************************************************/
62
/* LOCAL VARIABLES                                                            */
63
/******************************************************************************/
64

    
65
/******************************************************************************/
66
/* LOCAL FUNCTIONS                                                            */
67
/******************************************************************************/
68

    
69
/******************************************************************************/
70
/* EXPORTED FUNCTIONS                                                         */
71
/******************************************************************************/
72

    
73
/**
74
 * @brief   AT24C01B unit test function.
75
 *
76
 * @param[in] stream  Stream for inout/outout.
77
 * @param[in] ut      Unit test object.
78
 *
79
 * @return            Unit test result value.
80
 */
81
aos_utresult_t utAlldAt24c01bFunc(BaseSequentialStream* stream, aos_unittest_t* ut)
82
{
83
  aosDbgCheck(ut->data != NULL && ((ut_at24c01bdata_t*)(ut->data))->driver != NULL);
84

    
85
  // local variables
86
  aos_utresult_t result = {0, 0};
87
  uint32_t status = 0;
88
  uint8_t page_data[AT24C01B_LLD_PAGE_SIZE_BYTES];
89
  uint8_t original_data[AT24C01B_LLD_PAGE_SIZE_BYTES];
90
  uint8_t read_data[AT24C01B_LLD_PAGE_SIZE_BYTES];
91
  size_t errors = 0;
92
  for (size_t dataIdx = 0; dataIdx < AT24C01B_LLD_PAGE_SIZE_BYTES; ++dataIdx) {
93
    page_data[dataIdx] = dataIdx;
94
  }
95

    
96
  chprintf(stream, "reading byte...\n");
97
  status = at24c01b_lld_read(((ut_at24c01bdata_t*)(ut->data))->driver, _byteAddress, original_data, 1, ((ut_at24c01bdata_t*)(ut->data))->timeout);
98
  if (status == APAL_STATUS_SUCCESS || status == APAL_STATUS_WARNING) {
99
    aosUtPassed(stream, &result);
100
  } else {
101
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
102
  }
103

    
104
  chprintf(stream, "writing byte...\n");
105
  status = at24c01b_lld_write_byte(((ut_at24c01bdata_t*)(ut->data))->driver, _byteAddress, _bytedata, ((ut_at24c01bdata_t*)(ut->data))->timeout);
106
  while (at24c01b_lld_poll_ack(((ut_at24c01bdata_t*)(ut->data))->driver, ((ut_at24c01bdata_t*)(ut->data))->timeout) == APAL_STATUS_FAILURE) {
107
    continue;
108
  }
109
  status |= at24c01b_lld_read(((ut_at24c01bdata_t*)(ut->data))->driver, _byteAddress, &read_data[0], 1, ((ut_at24c01bdata_t*)(ut->data))->timeout);
110
  status |= (read_data[0] == _bytedata) ? 0x00 : 0x20;
111
  status = at24c01b_lld_write_byte(((ut_at24c01bdata_t*)(ut->data))->driver, _byteAddress, (uint8_t)~_bytedata, ((ut_at24c01bdata_t*)(ut->data))->timeout);
112
  while (at24c01b_lld_poll_ack(((ut_at24c01bdata_t*)(ut->data))->driver, ((ut_at24c01bdata_t*)(ut->data))->timeout) == APAL_STATUS_FAILURE) {
113
    continue;
114
  }
115
  status |= at24c01b_lld_read(((ut_at24c01bdata_t*)(ut->data))->driver, _byteAddress, &read_data[1], 1, ((ut_at24c01bdata_t*)(ut->data))->timeout);
116
  status |= (read_data[1] == (uint8_t)~_bytedata) ? 0x00 : 0x40;
117
  status |= at24c01b_lld_write_byte(((ut_at24c01bdata_t*)(ut->data))->driver, _byteAddress, original_data[0], ((ut_at24c01bdata_t*)(ut->data))->timeout);
118
  while (at24c01b_lld_poll_ack(((ut_at24c01bdata_t*)(ut->data))->driver, ((ut_at24c01bdata_t*)(ut->data))->timeout) == APAL_STATUS_FAILURE) {
119
    continue;
120
  }
121
  status |= at24c01b_lld_read(((ut_at24c01bdata_t*)(ut->data))->driver, _byteAddress, &read_data[2], 1, ((ut_at24c01bdata_t*)(ut->data))->timeout);
122
  status |= (read_data[2] == original_data[0]) ? 0x00 : 0x80;
123
  if (status == APAL_STATUS_SUCCESS || status == APAL_STATUS_WARNING) {
124
    aosUtPassed(stream, &result);
125
  } else {
126
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
127
  }
128

    
129
  chprintf(stream, "reading page...\n");
130
  status = at24c01b_lld_read(((ut_at24c01bdata_t*)(ut->data))->driver, _pageAddress, original_data, AT24C01B_LLD_PAGE_SIZE_BYTES, ((ut_at24c01bdata_t*)(ut->data))->timeout);
131
  if (status == APAL_STATUS_SUCCESS) {
132
    aosUtPassed(stream, &result);
133
  } else {
134
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
135
  }
136

    
137
  chprintf(stream, "writing page...\n");
138
  status = at24c01b_lld_write_page(((ut_at24c01bdata_t*)(ut->data))->driver, _pageAddress, page_data, AT24C01B_LLD_PAGE_SIZE_BYTES, ((ut_at24c01bdata_t*)(ut->data))->timeout);
139
  while (at24c01b_lld_poll_ack(((ut_at24c01bdata_t*)(ut->data))->driver, ((ut_at24c01bdata_t*)(ut->data))->timeout) == APAL_STATUS_FAILURE) {
140
    continue;
141
  }
142
  status |= at24c01b_lld_read(((ut_at24c01bdata_t*)(ut->data))->driver, _pageAddress, read_data, AT24C01B_LLD_PAGE_SIZE_BYTES, ((ut_at24c01bdata_t*)(ut->data))->timeout);
143
  errors = 0;
144
  for (size_t dataIdx = 0; dataIdx < AT24C01B_LLD_PAGE_SIZE_BYTES; dataIdx++) {
145
    if (read_data[dataIdx] != page_data[dataIdx]) {
146
      ++errors;
147
    }
148
  }
149
  if (status == APAL_STATUS_OK && errors == 0) {
150
    aosUtPassed(stream, &result);
151
  } else {
152
    aosUtFailedMsg(stream, &result, "0x%08X; %u\n", status, errors);
153
  }
154

    
155
  chprintf(stream, "reading page at current address...\n");
156
  // set address counter to UT_ALLD_AT24C01B_PAGE_ADDRESS
157
  status = at24c01b_lld_read(((ut_at24c01bdata_t*)(ut->data))->driver, _pageAddress-1, read_data, 1, ((ut_at24c01bdata_t*)(ut->data))->timeout);
158
  if (status == APAL_STATUS_WARNING) {
159
    // on STM32F1 platform the address gets incremented by 2 after reading
160
    status = at24c01b_lld_read(((ut_at24c01bdata_t*)(ut->data))->driver, _pageAddress-2, read_data, 1, ((ut_at24c01bdata_t*)(ut->data))->timeout);
161
  }
162
  // read page from the current address
163
  status = at24c01b_lld_read_current_address(((ut_at24c01bdata_t*)(ut->data))->driver, read_data, AT24C01B_LLD_PAGE_SIZE_BYTES, ((ut_at24c01bdata_t*)(ut->data))->timeout);
164
  errors = 0;
165
  for (uint8_t dataIdx = 0; dataIdx < 8; dataIdx++) {
166
    if (read_data[dataIdx] != page_data[dataIdx]) {
167
      ++errors;
168
    }
169
  }
170
  if (status == APAL_STATUS_OK && errors == 0) {
171
    aosUtPassed(stream, &result);
172
  } else {
173
    aosUtFailedMsg(stream, &result, "0x%08X; %u\n", status, errors);
174
  }
175

    
176
  chprintf(stream, "writing page with overflow (7 bytes)...\n");
177
  status = at24c01b_lld_write_page(((ut_at24c01bdata_t*)(ut->data))->driver, _overflowAddress, page_data, AT24C01B_LLD_PAGE_SIZE_BYTES-1, ((ut_at24c01bdata_t*)(ut->data))->timeout);
178
  while (at24c01b_lld_poll_ack(((ut_at24c01bdata_t*)(ut->data))->driver, ((ut_at24c01bdata_t*)(ut->data))->timeout) == APAL_STATUS_FAILURE) {
179
    continue;
180
  }
181
  status |= at24c01b_lld_read(((ut_at24c01bdata_t*)(ut->data))->driver, _overflowAddress, read_data, AT24C01B_LLD_PAGE_SIZE_BYTES-_overflowOffset, ((ut_at24c01bdata_t*)(ut->data))->timeout);
182
  status |= at24c01b_lld_read(((ut_at24c01bdata_t*)(ut->data))->driver, _overflowAddress-_overflowOffset, read_data+(AT24C01B_LLD_PAGE_SIZE_BYTES-_overflowOffset), _overflowOffset-1, ((ut_at24c01bdata_t*)(ut->data))->timeout);
183
  errors = 0;
184
  for (uint8_t dataIdx = 0; dataIdx < AT24C01B_LLD_PAGE_SIZE_BYTES-1; dataIdx++) {
185
    if (read_data[dataIdx] != page_data[dataIdx]) {
186
      ++errors;
187
    }
188
  }
189
  if (status == APAL_STATUS_OK && errors == 0) {
190
    aosUtPassed(stream, &result);
191
  } else {
192
    aosUtFailedMsg(stream, &result, "0x%08X; %u\n", status, errors);
193
  }
194

    
195
  chprintf(stream, "writing page with overflow (8 bytes)...\n");
196
  status = at24c01b_lld_write_page(((ut_at24c01bdata_t*)(ut->data))->driver, _overflowAddress, page_data, AT24C01B_LLD_PAGE_SIZE_BYTES, ((ut_at24c01bdata_t*)(ut->data))->timeout);
197
  while (at24c01b_lld_poll_ack(((ut_at24c01bdata_t*)(ut->data))->driver, ((ut_at24c01bdata_t*)(ut->data))->timeout) == APAL_STATUS_FAILURE) {
198
    continue;
199
  }
200
  status |= at24c01b_lld_read(((ut_at24c01bdata_t*)(ut->data))->driver, _overflowAddress, read_data, AT24C01B_LLD_PAGE_SIZE_BYTES-_overflowOffset, ((ut_at24c01bdata_t*)(ut->data))->timeout);
201
  status |= at24c01b_lld_read(((ut_at24c01bdata_t*)(ut->data))->driver, _overflowAddress-_overflowOffset, read_data+(AT24C01B_LLD_PAGE_SIZE_BYTES-_overflowOffset), _overflowOffset, ((ut_at24c01bdata_t*)(ut->data))->timeout);
202
  errors = 0;
203
  for (uint8_t dataIdx = 0; dataIdx < AT24C01B_LLD_PAGE_SIZE_BYTES; dataIdx++) {
204
    if (read_data[dataIdx] != page_data[dataIdx]) {
205
      ++errors;
206
    }
207
  }
208
  if (status == APAL_STATUS_OK && errors == 0) {
209
    aosUtPassed(stream, &result);
210
  } else {
211
    aosUtFailedMsg(stream, &result, "0x%08X; %u\n", status, errors);
212
  }
213

    
214
  chprintf(stream, "write back original data...\n");
215
  status = at24c01b_lld_write_page(((ut_at24c01bdata_t*)(ut->data))->driver, _pageAddress, original_data, AT24C01B_LLD_PAGE_SIZE_BYTES, ((ut_at24c01bdata_t*)(ut->data))->timeout);
216
  if (status == APAL_STATUS_SUCCESS) {
217
    aosUtPassed(stream, &result);
218
  } else {
219
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
220
  }
221

    
222
  aosUtInfoMsg(stream,"driver object memory footprint: %u bytes\n", sizeof(AT24C01BDriver));
223

    
224
  return result;
225
}
226

    
227
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) && defined(AMIROLLD_USE_AT24C01B) && (AMIROLLD_USE_AT24C01B == 1) */