-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEeprom.c
174 lines (143 loc) · 6.13 KB
/
Eeprom.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*******************************************************************************
Project: stepRocker Mini-TMCL
Module: Eeprom.c
Access to the onboard EEPROM (AT25128)
Copyright (C) 2011 TRINAMIC Motion Control GmbH & Co KG
Waterloohain 5
D - 22769 Hamburg, Germany
http://www.trinamic.com/
This program is free software; you can redistribute it and/or modify it
freely.
This program is distributed "as is" in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
*******************************************************************************/
/**
\file Eeprom.c
\author Trinamic Motion Control GmbH & Co KG
\version 1.00
\brief EEPROM access functions
This file contains EEPROM access functions.
*/
#include <stdlib.h>
#include "csp.h"
#include "s3fn41f_conf.h"
#include "bits.h"
#include "stepRocker.h"
#include "SPI.h"
/***************************************************************//**
\fn WriteEepromByte(UINT Address, UCHAR Value)
\brief Write a byte to the EEPROM
\param Address EEPROM location (0..16383)
\param Value Byte to be written
This function writes a byte to the EEPROM at the specified
EEPROM location.
********************************************************************/
void WriteEepromByte(UINT Address, UCHAR Value)
{
//Schreiben erlauben
ReadWriteSPI(SPI_DEV_EEPROM, 0x06, TRUE); //Befehl "Write Enable"
do
{
ReadWriteSPI(SPI_DEV_EEPROM, 0x05, FALSE); //Befehl "Get Status"
} while((ReadWriteSPI(SPI_DEV_EEPROM, 0x00, TRUE) & 0x02)==0x00); //Warte bis "Write Enable"-Bit gesetzt
//Eigentliches Schreiben
ReadWriteSPI(SPI_DEV_EEPROM, 0x02, FALSE); //Befehl "Write"
ReadWriteSPI(SPI_DEV_EEPROM, Address >> 8, FALSE);
ReadWriteSPI(SPI_DEV_EEPROM, Address & 0xff, FALSE);
ReadWriteSPI(SPI_DEV_EEPROM, Value, TRUE);
//Warten bis Schreibvorgang beendet ist
do
{
ReadWriteSPI(SPI_DEV_EEPROM, 0x05, FALSE); //Befehl "Get Status"
} while(ReadWriteSPI(SPI_DEV_EEPROM, 0x00, TRUE) & 0x01);
}
/***************************************************************//**
\fn WriteEepromBlock(UINT Address, UCHAR *Block, UINT Size)
\brief Copy memory block to EEPROM
\param Address EEPROM location (0..16383)
\param Block pointer at memory block to be copied to the EEPROM
\param Size size of block to be copied (bytes)
This function copies a memory block to the EEPROM. It is capable
of filling the entire EEPROM just whith one function call.
********************************************************************/
void WriteEepromBlock(UINT Address, UCHAR *Block, UINT Size)
{
UINT i;
//Schreiben erlauben
ReadWriteSPI(SPI_DEV_EEPROM, 0x06, TRUE); //Befehl "Write Enable"
do
{
ReadWriteSPI(SPI_DEV_EEPROM, 0x05, FALSE); //Befehl "Get Status"
} while((ReadWriteSPI(SPI_DEV_EEPROM, 0x00, TRUE) & 0x02)==0x00); //Warte bis "Write Enable"-Bit gesetzt
//Schreibvorgang (Startadresse)
ReadWriteSPI(SPI_DEV_EEPROM, 0x02, FALSE); //Befehl "Write"
ReadWriteSPI(SPI_DEV_EEPROM, Address >> 8, FALSE);
ReadWriteSPI(SPI_DEV_EEPROM, Address & 0xff, FALSE);
//Eigentliches Schreiben der Daten
for(i=0; i<Size; i++)
{
//Adresse mitzählen und bei Überlauf der untersten sechs Bits das EEPROM deselektieren
//und neuen Write-Befehl senden (bzw. beim letzten Datenbyte einfach nur EEPROM
//deselektieren).
//Dies ist erforderlich, da beim Beschreiben im 25128 nur die untersten sechs Bits der
//Adresse hochgezählt werden (anders als beim Lesen).
Address++;
ReadWriteSPI(SPI_DEV_EEPROM, *(Block+i), (Address & 0x0000003f)==0 || i==Size-1);
if((Address & 0x0000003f)==0 && i<Size-1) //Adressbits übergelaufen, aber noch Bytes zu schreiben?
{
//Warte bis Schreibvorgang beendet
do
{
ReadWriteSPI(SPI_DEV_EEPROM, 0x05, FALSE); //Befehl "Get Status"
} while(ReadWriteSPI(SPI_DEV_EEPROM, 0x00, TRUE) & 0x01);
//Neuer "Write Enable"-Befehl
ReadWriteSPI(SPI_DEV_EEPROM, 0x06, TRUE); //Befehl "Write Enable"
do
{
ReadWriteSPI(SPI_DEV_EEPROM, 0x05, FALSE); //Befehl "Get Status"
} while((ReadWriteSPI(SPI_DEV_EEPROM, 0x00, TRUE) & 0x02)==0x00); //Warte bis "Write Enable"-Bit gesetzt
//Neuer "Write"-Befehl (mit der nächsten Adresse)
ReadWriteSPI(SPI_DEV_EEPROM, 0x02, FALSE); //Befehl "Write"
ReadWriteSPI(SPI_DEV_EEPROM, Address >> 8, FALSE);
ReadWriteSPI(SPI_DEV_EEPROM, Address & 0xff, FALSE);
}
}
//Warte bis Schreibvorgang beendet
do
{
ReadWriteSPI(SPI_DEV_EEPROM, 0x05, FALSE); //Befehl "Get Status"
} while(ReadWriteSPI(SPI_DEV_EEPROM, 0x00, TRUE) & 0x01);
}
/***************************************************************//**
\fn ReadEepromByte(UINT Address)
\brief Read a byte from the EEPROM
\param Address EEPROM location (0..16383)
\return byte read from EEPROM
This function reads one byte from the EEPROM.
********************************************************************/
UCHAR ReadEepromByte(UINT Address)
{
ReadWriteSPI(SPI_DEV_EEPROM, 0x03, FALSE); //Befehl "Read"
ReadWriteSPI(SPI_DEV_EEPROM, Address >> 8, FALSE);
ReadWriteSPI(SPI_DEV_EEPROM, Address & 0xff, FALSE);
return ReadWriteSPI(SPI_DEV_EEPROM, 0, TRUE);
}
/***************************************************************//**
\fn ReadEepromBlock(UINT Address, UCHAR *Block, UINT Size)
\brief Copy block from EEPROM to RAM
\param Address EEPROM start address (0..16383)
\param Block RAM start address
\param Size Length of block (bytes)
Read a memory block from the EEPROM. This can also be the entire
EEPROM.
********************************************************************/
void ReadEepromBlock(UINT Address, UCHAR *Block, UINT Size)
{
UINT i;
ReadWriteSPI(SPI_DEV_EEPROM, 0x03, FALSE); //Befehl "Read"
ReadWriteSPI(SPI_DEV_EEPROM, Address >> 8, FALSE);
ReadWriteSPI(SPI_DEV_EEPROM, Address & 0xff, FALSE);
for(i=0; i<Size; i++)
*(Block+i)=ReadWriteSPI(SPI_DEV_EEPROM, 0, i==Size-1); //beim letzten Byte EEPROM deselektieren
}