summaryrefslogtreecommitdiff
path: root/firmware/drivers/eeprom_24cxx.c
blob: 4cd50d4e3a75eae632939177d6e4465b1c6cfa9d (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 by Miika Pekkarinen
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "logf.h"
#include "string.h"
#include "inttypes.h"

#include "sw_i2c.h"

#include "eeprom_24cxx.h"

/* Use cache to speedup writing to the chip. */
static char data_cache[EEPROM_SIZE];
static uint8_t cached_bitfield[EEPROM_SIZE/8];

#define IS_CACHED(addr) (cached_bitfield[addr/8] & (1 << (addr % 8)))
#define SET_CACHED(addr) (cached_bitfield[addr/8] |= 1 << (addr % 8)) 

void eeprom_24cxx_init(void)
{
    sw_i2c_init();
    memset(cached_bitfield, 0, sizeof cached_bitfield);
}

int eeprom_24cxx_read_byte(unsigned int address, char *c)
{
    int ret;
    char byte;
    int count = 0;

    if (address >= EEPROM_SIZE)
    {
        logf("EEPROM address: %d", address);
        return -9;
    }
    
    /* Check from cache. */
    if (IS_CACHED(address))
    {
        logf("EEPROM RCached: %d", address);
        *c = data_cache[address];
        return 0;
    }
    
    *c = 0;
    do
    {
        ret = sw_i2c_read(EEPROM_ADDR, address, &byte, 1);
    } while (ret < 0 && count++ < 200);

    if (ret < 0)
    {
        logf("EEPROM RFail: %d/%d/%d", ret, address, count);
        return ret;
    }
    
    if (count)
    {
        /* keep between {} as logf is whitespace in normal builds */
        logf("EEPROM rOK: %d retries", count);
    }
    
    /* Cache the byte. */
    data_cache[address] = byte;
    SET_CACHED(address);
    
    *c = byte;
    return 0;
}

int eeprom_24cxx_write_byte(unsigned int address, char c)
{
    int ret;
    int count = 0;

    if (address >= EEPROM_SIZE)
    {
        logf("EEPROM address: %d", address);
        return -9;
    }
    
    /* Check from cache. */
    if (IS_CACHED(address) && data_cache[address] == c)
    {
        logf("EEPROM WCached: %d", address);
        return 0;
    }
    
    do
    {
        ret = sw_i2c_write(EEPROM_ADDR, address, &c, 1);
    } while (ret < 0 && count++ < 200) ;

    if (ret < 0)
    {
        logf("EEPROM WFail: %d/%d", ret, address);
        return ret;
    }
    
    if (count)
    {
        /* keep between {} as logf is whitespace in normal builds */
        logf("EEPROM wOK: %d retries", count);
    }
    
    SET_CACHED(address);
    data_cache[address] = c;
    
    return 0;
}

int eeprom_24cxx_read(unsigned char address, void *dest, int length)
{
    char *buf = (char *)dest;
    int ret = 0;
    int i;
    
    for (i = 0; i < length; i++)
    {
        ret = eeprom_24cxx_read_byte(address+i, &buf[i]);
        if (ret < 0)
            return ret;
    }
    
    return ret;
}

int eeprom_24cxx_write(unsigned char address, const void *src, int length)
{
    const char *buf = (const char *)src;
    int i;
    
    for (i = 0; i < length; i++)
    {
        if (eeprom_24cxx_write_byte(address+i, buf[i]) < 0)
            return -1;
    }
    
    return 0;
}