OpenCores
URL https://opencores.org/ocsvn/gecko3/gecko3/trunk

Subversion Repositories gecko3

[/] [gecko3/] [trunk/] [GECKO3COM/] [gecko3com-fw/] [firmware/] [src/] [eeprom_io.c] - Blame information for rev 9

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 nussgipfel
/* -*- c++ -*- */
2
/*
3
 * Copyright 2006 Free Software Foundation, Inc.
4
 *
5
 * This file is part of GNU Radio
6
 *
7
 * GNU Radio is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3, or (at your option)
10
 * any later version.
11
 *
12
 * GNU Radio is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with GNU Radio; see the file COPYING.  If not, write to
19
 * the Free Software Foundation, Inc., 51 Franklin Street,
20
 * Boston, MA 02110-1301, USA.
21
 */
22
 
23
/************************************************************************/
24
/** \file         eeprom_io.c
25
 *************************************************************************
26
 *  \brief        read and write functions for i2c eeproms
27
 *
28
 *  \author       GNU Radio
29
 */
30
 
31
#include <stdint.h>
32
#include <stdio.h>
33
#include "gecko3com_i2c.h" /* in this file are the device adresses defined */
34
#include "eeprom_io.h"
35
#include "i2c.h"
36
#include "delay.h"
37
#include "debugprint.h"
38
 
39
#define EEPROM_HIGH_ADDR 0x3FFF
40
#define PAGE_LEN  0x40
41
#define PAGE_START_MASK 0xFFC0
42
#define PAGE_MASK 0x003F
43
 
44
 
45
/* returns non-zero if successful, else 0 */
46
uint8_t eeprom_read (uint16_t eeprom_offset, xdata uint8_t *buf, uint8_t len)
47
{
48
  /* We setup a random read by first doing a "zero byte write".
49
     Writes carry an address.  Reads use an implicit address. */
50
 
51
  static xdata uint8_t cmd[2];
52
  cmd[0] = eeprom_offset>>8;
53
  cmd[1] = eeprom_offset & 0xFF;
54
  if (!i2c_write(I2C_ADDR_BOOT, cmd, 2))
55
    return 0;
56
 
57
  return i2c_read(I2C_ADDR_BOOT, buf, len);
58
}
59
 
60
 
61
/* returns non-zero if successful, else 0 */
62
//uint8_t eeprom_write (idata uint8_t i2c_addr, idata uint16_t eeprom_offset,
63
//            const xdata uint8_t *buf, uint8_t len)
64
uint8_t eeprom_write (uint16_t eeprom_offset, const xdata uint8_t *buf, \
65
                      uint8_t len)
66
{
67
  uint8_t i = 0, byte_count;
68
  static xdata uint8_t cmd[66];
69
 
70
  if(eeprom_offset > EEPROM_HIGH_ADDR){
71
    return 0;
72
  }
73
  //print_info("w\n");
74
  while (len > 0){
75
 
76
    if(eeprom_offset + len > (eeprom_offset & PAGE_START_MASK) + PAGE_LEN){
77
      byte_count = PAGE_LEN - (eeprom_offset & PAGE_MASK);
78
    }
79
    else if(len < PAGE_LEN){
80
      byte_count = len;
81
    }
82
    else {
83
      byte_count = PAGE_LEN;
84
    }
85
 
86
    cmd[0] = eeprom_offset>>8;
87
    cmd[1] = eeprom_offset & 0xFF;
88
 
89
    for(i=0; i < byte_count;i++) {
90
      cmd[i+2] = buf[i];
91
    }
92
 
93
    if (!i2c_write(I2C_ADDR_BOOT, cmd, byte_count+2))
94
      return 0;
95
 
96
    len -= byte_count;
97
    eeprom_offset += byte_count;
98
    mdelay(8);          /* delay 8ms worst case write time */
99
  }
100
  return 1;
101
}
102
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.