Subversion Repositories idreammicro-avr

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
65 jlesech 1
/**************************************************************************//**
2
 * \brief EEPROM library
3
 * \author Copyright (C) 2011  Julien Le Sech - www.idreammicro.com
4
 * \version 1.0
5
 * \date 20111017
6
 *
7
 * This file is part of the iDreamMicro library.
8
 *
9
 * This library is free software: you can redistribute it and/or modify it under
10
 * the terms of the GNU Lesser General Public License as published by the Free
11
 * Software Foundation, either version 3 of the License, or (at your option) any
12
 * later version.
13
 *
14
 * This library is distributed in the hope that it will be useful, but WITHOUT
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17
 * details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program. If not, see http://www.gnu.org/licenses/
21
 ******************************************************************************/
22
 
23
/**************************************************************************//**
24
 * \file eeprom.c
25
 ******************************************************************************/
26
 
27
/******************************************************************************
28
 * Header file inclusions.
29
 ******************************************************************************/
30
 
31
#include <eeprom/eeprom.h>
32
 
33
#include <avr/io.h>
34
 
35
#include <assert.h>
36
#include <stdint.h>
37
#include <stdlib.h>
38
 
39
/******************************************************************************
40
 * Public function definitions.
41
 ******************************************************************************/
42
 
43
/**************************************************************************//**
44
 * \fn void eeprom__write_byte(
45
 * uint16_t address,
46
 * uint8_t  data)
47
 *
48
 * \brief Write a byte in EEPROM.
49
 *
50
 * \param   address Address.
51
 * \param   data    Data to write.
52
 ******************************************************************************/
53
void
54
eeprom__write_byte
55
(
56
    uint16_t    address,
57
    uint8_t     data
58
){
59
    // Wait for completion of previous write.
60
    while (EECR & (1 << EEPE));
61
 
62
    // Set up address.
63
    EEAR = address;
64
 
65
    // Set up data.
66
    EEDR = data;
67
 
68
    // Write logical one to EEMPE.
69
    EECR |= (1 << EEMPE);
70
 
71
    // Start EEPROM write.
72
    EECR |= (1 << EEPE);
73
}
74
 
75
/**************************************************************************//**
76
 * \fn void eeprom__write_bytes(
77
 * uint16_t address,
78
 * uint16_t size,
79
 * uint8_t* p_data)
80
 *
81
 * \brief Write bytes into EEPOM.
82
 *
83
 * \param       address Address.
84
 * \param       size    Data size.
85
 * \param[in]   p_data  Data to write.
86
 ******************************************************************************/
87
void
88
eeprom__write_bytes
89
(
90
    uint16_t    address,
91
    uint16_t    size,
92
    uint8_t*    p_data
93
){
94
    // Check the preconditions.
95
    assert(NULL != p_data);
96
 
97
    for (uint16_t i = 0; i < size; i++)
98
    {
99
        eeprom__write_byte(address + i, p_data[i]);
100
    }
101
}
102
 
103
/**************************************************************************//**
104
 * \fn uint8_t eeprom__read_byte(
105
 * uint16_t address)
106
 *
107
 * \brief Read data into EEPROM.
108
 *
109
 * \param   address Address of data to read.
110
 *
111
 * \return Read data.
112
 ******************************************************************************/
113
uint8_t
114
eeprom__read_byte
115
(
116
    uint16_t    address
117
){
118
    // Wait for completion of previous write.
119
    while (EECR & (1 << EEPE));
120
 
121
    // Set up address.
122
    EEAR = address;
123
 
124
    // Start EEPROM read.
125
    EECR |= (1 << EERE);
126
 
127
    // Read data register.
128
    uint8_t data = EEDR;
129
 
130
    return data;
131
}
132
 
133
/**************************************************************************//**
134
 * \fn void eeprom__read_bytes(
135
 * uint16_t address,
136
 * uint16_t size,
137
 * uint8_t* p_data)
138
 *
139
 * \brief Read bytes from EEPROM.
140
 *
141
 * \param   address     Address to read.
142
 * \param   size        Data size to read.
143
 * \param[in]   p_data  Data buffer.
144
 ******************************************************************************/
145
void
146
eeprom__read_bytes
147
(
148
    uint16_t    address,
149
    uint16_t    size,
150
    uint8_t*    p_data
151
){
152
    // Check the preconditions.
153
    assert(NULL != p_data);
154
 
155
    for (uint16_t i = 0; i < size; i++)
156
    {
157
        p_data[i] = eeprom__read_byte(address + i);
158
    }
159
}