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 36

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 32 nussgipfel
#define EEPROM_HIGH_ADDR 0x3FFF /**< highest available addres, length */
40
#define PAGE_LEN  0x40          /**< lenght of a memory page */
41
/** bit mask to get the corresponding page start adress */
42
#define PAGE_START_MASK 0xFFC0  
43
/** bit mask to select the adress inside a memory page */
44
#define PAGE_MASK 0x003F        
45 9 nussgipfel
 
46
 
47
/* returns non-zero if successful, else 0 */
48
uint8_t eeprom_read (uint16_t eeprom_offset, xdata uint8_t *buf, uint8_t len)
49
{
50
  /* We setup a random read by first doing a "zero byte write".
51
     Writes carry an address.  Reads use an implicit address. */
52
 
53
  static xdata uint8_t cmd[2];
54
  cmd[0] = eeprom_offset>>8;
55
  cmd[1] = eeprom_offset & 0xFF;
56
  if (!i2c_write(I2C_ADDR_BOOT, cmd, 2))
57
    return 0;
58
 
59
  return i2c_read(I2C_ADDR_BOOT, buf, len);
60
}
61
 
62
 
63
/* returns non-zero if successful, else 0 */
64
//uint8_t eeprom_write (idata uint8_t i2c_addr, idata uint16_t eeprom_offset,
65
//            const xdata uint8_t *buf, uint8_t len)
66
uint8_t eeprom_write (uint16_t eeprom_offset, const xdata uint8_t *buf, \
67
                      uint8_t len)
68
{
69
  uint8_t i = 0, byte_count;
70
  static xdata uint8_t cmd[66];
71
 
72
  if(eeprom_offset > EEPROM_HIGH_ADDR){
73
    return 0;
74
  }
75
  //print_info("w\n");
76
  while (len > 0){
77
 
78
    if(eeprom_offset + len > (eeprom_offset & PAGE_START_MASK) + PAGE_LEN){
79
      byte_count = PAGE_LEN - (eeprom_offset & PAGE_MASK);
80
    }
81
    else if(len < PAGE_LEN){
82
      byte_count = len;
83
    }
84
    else {
85
      byte_count = PAGE_LEN;
86
    }
87
 
88
    cmd[0] = eeprom_offset>>8;
89
    cmd[1] = eeprom_offset & 0xFF;
90
 
91
    for(i=0; i < byte_count;i++) {
92
      cmd[i+2] = buf[i];
93
    }
94
 
95
    if (!i2c_write(I2C_ADDR_BOOT, cmd, byte_count+2))
96
      return 0;
97
 
98
    len -= byte_count;
99
    eeprom_offset += byte_count;
100
    mdelay(8);          /* delay 8ms worst case write time */
101
  }
102
  return 1;
103
}
104
 

powered by: WebSVN 2.1.0

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