summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/PersistDataServer/src/pdsCrcService.c
blob: de9631dc267d8d0ec215dbece21be1246b9bfb44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/***************************************************************************//**
  \file pdsCrcService.c

  \brief Persistence Data Server implementation

  \author
    Atmel Corporation: http://www.atmel.com \n
    Support email: avr@atmel.com

  Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
  Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
  History:
    22/01/08 A. Khromykh - Created
    01/11/10 A. Razinkov - Modified
*****************************************************************************/

#ifdef _COMMISSIONING_
/******************************************************************************
                   Includes section
******************************************************************************/
#include <pdsMemAbstract.h>
#include <pdsCrcService.h>
#include <pdsAuxService.h>
#include <taskManager.h>
#include <csPersistentMem.h>
#include <pdsWriteData.h>

/******************************************************************************
\brief Calculate CRC of persistent data stored in persist memory
\
\param[out] crcStation - pointer to CRC service structure
******************************************************************************/
void pdsCalculateCrc(PDS_ServiceCrc_t *crcStation)
{
  uint8_t tdata = 0;
  MEMORY_DESCRIPTOR descriptor;

  if (NULL == crcStation)
    return;

  crcStation->crc = 0;
  descriptor.address = SYSTEM_BASE_EEPROM_ADDRESS;
  descriptor.length = 1;
  while (descriptor.address < csPersistentMemorySize)
  {
    descriptor.data = &tdata;

    pdsWaitMemoryFree();

    if (EEPROM_ERROR == READ_MEMORY(&descriptor, NULL))
    {
      crcStation->eepromState = PDS_EEPROM_ERROR;
      return;
    }

    pdsWaitMemoryFree();

    crcStation->crc += tdata;
    descriptor.address++;
  }
  crcStation->eepromState = PDS_SUCCESS;
}

/******************************************************************************
\brief Read CRC of stored data from persist memory
\
\param[out] crcStation - pointer to CRC service structure
******************************************************************************/
void pdsReadCrc(PDS_ServiceCrc_t *crcStation)
{
  MEMORY_DESCRIPTOR descriptor;

  if (NULL == crcStation)
    return;

  descriptor.address = csPersistentMemorySize;
  descriptor.length = 1;
  while (descriptor.address < USER_BASE_EEPROM_ADDRESS)
  {
    descriptor.data = &(crcStation->crc);

    pdsWaitMemoryFree();

    if (EEPROM_ERROR == READ_MEMORY(&descriptor, NULL))
    {
      crcStation->eepromState = PDS_EEPROM_ERROR;
      return;
    }

    pdsWaitMemoryFree();

    if (0xFF != crcStation->crc)
    {
      crcStation->position = descriptor.address;
      crcStation->eepromState = PDS_SUCCESS;
      return;
    }
    descriptor.address++;
  }
  crcStation->position = csPersistentMemorySize;
  crcStation->eepromState = PDS_SUCCESS;
}

/******************************************************************************
\brief Clears whole CRC area in persist memory
\
\return operation result
******************************************************************************/
PDS_DataServerState_t pdsClearCrcArea(void)
{
  uint8_t value = 0xFF;
  MEMORY_DESCRIPTOR descriptor;
  uint8_t status;

  descriptor.data = &value;
  descriptor.address = csPersistentMemorySize;
  descriptor.length = 1;

  for (uint8_t i = 0; i < PDS_CRC_AREA; i++)
  {
    status = pdsWrite(&descriptor, pdsDummyCallback);
    if (status != PDS_SUCCESS)
      return status;

    descriptor.address++;
  }
  return PDS_SUCCESS;
}

/******************************************************************************
\brief Write CRC of stored data to persist memory. The ring buffer used
\      to increase persist memory life cycle
\
\param[out] crcStation - pointer to CRC service structure
******************************************************************************/
PDS_DataServerState_t pdsWriteCrc(void)
{
  PDS_ServiceCrc_t crcRead, crcCalc;
  MEMORY_DESCRIPTOR descriptor;
  uint8_t datadelete = 0xFF;
  PDS_DataServerState_t status;

  pdsReadCrc(&crcRead);
  if (PDS_EEPROM_ERROR == crcRead.eepromState)
    return PDS_EEPROM_ERROR;

  pdsCalculateCrc(&crcCalc);
  if (PDS_EEPROM_ERROR == crcCalc.eepromState)
    return PDS_EEPROM_ERROR;

  if (0 == (uint8_t)(crcCalc.crc + crcRead.crc))
    return PDS_SUCCESS;

  crcCalc.crc = 0 - crcCalc.crc;
  descriptor.address = crcRead.position;
  descriptor.data = &datadelete;
  descriptor.length = 1;

  status = pdsWrite(&descriptor, pdsDummyCallback);
  if (status != PDS_SUCCESS)
    return status;

  if (++descriptor.address >= USER_BASE_EEPROM_ADDRESS)
    descriptor.address = csPersistentMemorySize;
  descriptor.data = &(crcCalc.crc);

  status = pdsWrite(&descriptor, pdsDummyCallback);
  if (status != PDS_SUCCESS)
    return status;

  return PDS_SUCCESS;
}

/******************************************************************************
\brief Check if any valid data exists in persist memory
\
\return operation result
******************************************************************************/
PDS_DataServerState_t pdsCheckPersistMemory(void)
{
  PDS_ServiceCrc_t crcRead, crcCalc;

  pdsCalculateCrc(&crcCalc);
  if (PDS_EEPROM_ERROR == crcCalc.eepromState)
    return PDS_EEPROM_ERROR;

  pdsReadCrc(&crcRead);
  if (PDS_EEPROM_ERROR == crcRead.eepromState)
    return PDS_EEPROM_ERROR;

  if (0x00 == (uint8_t)(crcCalc.crc + crcRead.crc))
    return PDS_SUCCESS;

  return PDS_CRC_ERROR;
}

#ifdef __DBG_PDS__
  uint8_t  eepromCopy[0x400u];

  void pdsDbgReadAllEeprom(void)
  {
    PDS_DataServerState_t status;
    MEMORY_DESCRIPTOR descriptor;

    descriptor.address = 0;
    descriptor.data = eepromCopy;
    descriptor.length = 0x400u;

    status = pdsRead(&descriptor, pdsDummyCallback);
    if (status != PDS_SUCCESS)
      return;
  }
#endif
#endif /* _COMMISSIONING_ */
// eof pdsDataServer.c