summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/eeprom.c
blob: 8e17055b46241293b4196b794f3ee3fe2d15a9ac (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
/**************************************************************************//**
  \file eeprom.c

  \brief Implementation of the EEPROM interface.

  \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:
      5/12/07 A. Khromykh - Created
 ******************************************************************************/
/******************************************************************************
 *   WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK.  *
 *   EXPERT USERS SHOULD PROCEED WITH CAUTION.                                *
 ******************************************************************************/

/******************************************************************************
                   Includes section
******************************************************************************/
#include <halEeprom.h>

/******************************************************************************
                   Types section
******************************************************************************/
typedef enum
{
  EEPROM_IDLE = 0,
  EEPROM_BUSY
} EepromState_t;

/******************************************************************************
                   Global variables section
******************************************************************************/
EepromState_t halEepromState = EEPROM_IDLE; // state
HAL_EepromParams_t halEepromParams;
void (*halEepromDone)();

/******************************************************************************
                   Implementations section
******************************************************************************/
/******************************************************************************
Reads some number of bytes defined by HAL_EepromParams_t from the EEPROM.
Parameters:
  params - address of HAL_EepromParams_t defined by user.
  readDone - callback method
Returns:
  0  - success.
  -1 - the EEPROM has request that was not completed,
       number of byte to read too much.
  -2 - eeprom is busy
******************************************************************************/
int HAL_ReadEeprom(HAL_EepromParams_t *params, void (*readDone)())
{
  uint16_t i;

  if (EEPROM_IDLE != halEepromState)
    return -2;
  if (NULL == params)
    return -1;
  if ((uint16_t)(params->address + params->length) > EEPROM_DATA_MEMORY_SIZE)
    return -1;

  halEepromState = EEPROM_BUSY;
  halEepromDone = readDone;
  halEepromParams = *params;
  halWaitEepromReady(); // wait for completion of previous operation
  for (i = 0; i < halEepromParams.length; i++)
  {
    halEepromParams.data[i] = halReadEeprom(halEepromParams.address++);
  }
  halEepromParams.length = 0;
  halPostTask3(HAL_EE_READY);

  return 0;
}

/******************************************************************************
Writes number of bytes defined by HAL_EepromParams_t to EEPROM.
By writeDone parameter user can control if write operation will be asynchronous
or synchronous.
Parameters:
  params - address of HAL_EepromParams_t defined by user.
  writeDone - address of callback. if writeDone is NULL write operation will be
              synchronous.
Returns:
   0 - success.
  -1 - the EEPROM has request that was not completed,
       number of byte to write too much.
  -2 - eeprom is busy
******************************************************************************/
int HAL_WriteEeprom(HAL_EepromParams_t *params, void (*writeDone)())
{
  uint16_t i;

  if (EEPROM_IDLE != halEepromState)
    return -2;
  if (NULL == params)
    return -1;
  if ((uint16_t)(params->address + params->length) > EEPROM_DATA_MEMORY_SIZE)
    return -1;

  halEepromState = EEPROM_BUSY;
  halEepromParams = *params;
  halEepromDone = writeDone;
  if (halEepromDone)
  {// asynchronous operation
    halEepromWrite(HAL_EEPROM_WRITE_MASK_INT, halEepromParams.address++, *halEepromParams.data++);
    halEepromParams.length--;
    return 0;
  }
  for (i = 0; i < halEepromParams.length; i++)
  {
    halEepromWrite(HAL_EEPROM_WRITE_MASK, halEepromParams.address++, *halEepromParams.data++);
  }
  halWaitEepromReady(); // wait for completion of previous write
  halEepromState = EEPROM_IDLE;

  return 0;
}

/******************************************************************************
Checks the eeprom state.

Returns:
  true  - eeprom is busy;
  false - eeprom is free;
******************************************************************************/
bool HAL_IsEepromBusy(void)
{
  if (EEPROM_BUSY == halEepromState)
    return true;
  else
    return false;
}

/******************************************************************************
Interrupt handler about write completion to EEPROM.
******************************************************************************/
void halSigEepromReadyHandler(void)
{
  if (!halEepromParams.length)
  {
    halEepromState = EEPROM_IDLE;
    if (NULL != halEepromDone)
      halEepromDone();
    return;
  }
  halEepromWrite(HAL_EEPROM_WRITE_MASK_INT, halEepromParams.address++, *halEepromParams.data++);
  halEepromParams.length--;
}
//eof eeprom.c