Statistics
| Branch: | Tag: | Revision:

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

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

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

    
23
#include <ut_alld_AT24C01B_v1.h>
24

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

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

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

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

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

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

    
54
/******************************************************************************/
55
/* EXPORTED VARIABLES                                                         */
56
/******************************************************************************/
57

    
58
/******************************************************************************/
59
/* LOCAL TYPES                                                                */
60
/******************************************************************************/
61

    
62
/******************************************************************************/
63
/* LOCAL VARIABLES                                                            */
64
/******************************************************************************/
65

    
66
/******************************************************************************/
67
/* LOCAL FUNCTIONS                                                            */
68
/******************************************************************************/
69

    
70
/******************************************************************************/
71
/* EXPORTED FUNCTIONS                                                         */
72
/******************************************************************************/
73

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

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

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

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

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

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

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

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

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

    
215
  chprintf(stream, "write back original data...\n");
216
  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);
217
  if (status == APAL_STATUS_SUCCESS) {
218
    aosUtPassed(stream, &result);
219
  } else {
220
    aosUtFailedMsg(stream, &result, "0x%08X\n", status);
221
  }
222

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

    
225
  return result;
226
}
227

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