Subversion Repositories svn.mios

Rev

Rev 52 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
53 tk 1
; $Id: mios_eeprom.inc 53 2008-01-30 22:52:41Z tk $
1 tk 2
;
3
; MIOS EEPROM routines
4
;
5
; ==========================================================================
6
;
7
;  Copyright 1998-2006 Thorsten Klose (tk@midibox.org)
8
;  Licensed for personal non-commercial use only.
9
;  All other rights reserved.
10
;
11
; ==========================================================================
12
 
13
;; --------------------------------------------------------------------------
14
;;  FUNCTION: MIOS_EEPROM_Write
15
;;  C_DECLARATION: unsigned char MIOS_EEPROM_Write(unsigned char addr, unsigned char value)
16
;;  DESCRIPTION: writes a byte into EEPROM. <BR>
17
;;  Write access will be skipped if content is equal to the byte which should
18
;;  be written<BR>
19
;;  Returned Error Status:<BR>
20
;;     0x00: no error<BR>
21
;;     0x01: byte mismatch (write failed)
22
;;  IN:	  byte in WREG, address in EEADR (0x00-0xff) and EEADRH (0-3, PIC18F4620 only)
23
;;  C_IN: byte in <value>, address in <addr> (0x00-0xff), high byte in EEADRH (0-3, PIC18F4620 only)
24
;;  OUT:  error status in WREG and MIOS_PARAMETER1
25
;;        EEADR will be incremented
26
;;        EEADRH (PIC18F4620) will *not* be incremented on EEADR overrun!
27
;;  C_OUT: error status
28
;;  USES: BSR, EEADR, EEDATA, EECON1, EECON2
29
;;  EXAMPLES:
30
;;
31
;;	;; write 0x47 0x11 into EEPROM at address 0x80/0x81
32
;;	movlw	0x80			; write 0x80 to address register
33
;;	movwf	EEADR
34
;;	movlw	0x47			; write 0x47 to 0x80
35
;;	call	MIOS_EEPROM_Write	; (EEADR will be incremented)
36
;;	bnz	EEPROMProgrammingFailed	; branch to your exception handler
37
;;                                      ; if necessary
38
;;	movlw	0x11			; write 0x11 to 0x81
39
;;	call	MIOS_EEPROM_Write	; (EEADR will be incremented)
40
;;	bnz	EEPROMProgrammingFailed	; branch to your exception handler
41
;;                                      ; if necessary
42
;;
43
;;  C_EXAMPLE:
44
;;
45
;;	unsigned char error = 0;
46
;;
47
;;	// write 0x47 0x11 into EEPROM at address 0x80/0x81
48
;;	error |= MIOS_EEPROM_Write(0x80, 0x47);
49
;;	error |= MIOS_EEPROM_Write(0x81, 0x11);
50
;;
51
;;	if( error ) {
52
;;	  // here you could do some error handling
53
;;	}
54
;;
55
;;
56
;;
57
;;	// note: it's very unlikely that an error happens when writing
58
;;	// to internal EEPROM, therefore you can use following shortcut:
59
;;
60
;;	// write 0x47 0x11 into EEPROM at address 0x80/0x81
61
;;	MIOS_EEPROM_Write(0x80, 0x47);
62
;;	MIOS_EEPROM_Write(0x81, 0x11);
63
;;
64
;; --------------------------------------------------------------------------
65
MIOS_EEPROM_Write
66
	;; NOTE: don't use MIOS_TMP2 within this function, it's used by MIOS_EEPROM_WritePage!
67
 
68
	SET_BSR	MIOS_TMP1
69
	movwf	MIOS_TMP1, BANKED
70
 
71
	rcall	MIOS_EEPROM_Read		; don't write if value is equal
72
	cpfseq	MIOS_TMP1, BANKED
73
	rgoto MIOS_EEPROM_WriteNow
74
	movlw	0x00
75
	rgoto	MIOS_EEPROM_Write_End
76
 
77
MIOS_EEPROM_WriteNow
78
#if PIC_DERIVATIVE_EEPROM_SIZE > 0x100
79
	;; TK: no auto increment of EEADRH to avoid compatibility issues with older applications,
80
	;;     where EEADRH is not initialized (it doesn't exist on a PIC18F452)
81
	;; 	movf	EEADR, W
82
	;; 	skpnz
83
	;; 	decf	EEADRH, F
84
#endif
85
	decf	EEADR, F			; decrement EEADR
86
 
87
 
88
	movff	MIOS_TMP1, EEDATA	; move byte to EEDATA register
89
 
90
	IRQ_DISABLE				; disable interrupts like recommented in the Errata sheet
91
 
92
	bcf	EECON1, EEPGD			; point to data memory
93
	bsf	EECON1, WREN			; EEPROM write enable
94
	movlw	0x55				; unlock sequence
95
	movwf	EECON2
96
	movlw	0xaa
97
	movwf	EECON2
98
	bsf	EECON1, WR			; EEPROM write
99
EEPROM_Write_PollEECON
100
	GOTO_IFSET EECON1, WR, ACCESS, EEPROM_Write_PollEECON
101
 
102
	IRQ_ENABLE				; enable IRQs
103
 
104
	rcall	MIOS_EEPROM_Read		; read address again
105
	cpfseq	MIOS_TMP1, BANKED
106
	rgoto MIOS_EEPROM_Write_Error
107
	movlw	0x00				; error status = OK
108
	rgoto	MIOS_EEPROM_Write_End
109
MIOS_EEPROM_Write_Error
110
	movlw	0x01				; error status = failed
111
	;; 	rgoto	MIOS_EEPROM_Write_End
112
MIOS_EEPROM_Write_End
113
	movwf	MIOS_PARAMETER1
114
	andlw	0xff				; update STATUS register
115
	return
116
 
117
;; --------------------------------------------------------------------------
118
;;  FUNCTION: MIOS_EEPROM_WritePage
119
;;  C_DECLARATION: unsigned char MIOS_EEPROM_WritePage(unsigned char addr, unsigned char *buffer)
120
;;  DESCRIPTION: writes a 64 bytes page into EEPROM.<BR>
121
;;  The internal EEPROM of PIC18F452 doesn't provide a page write by itself,
122
;;  therefore this function calls MIOS_EEPROM_Write 64 times.<BR>
123
;;  Returned Error Status:<BR>
124
;;     0x00: no error<BR>
125
;;     0x01: byte mismatch (one or more writes failed)
126
;;  IN:	  pointer to write buffer (64 bytes) in FSR1
127
;;        address in EEADR (0x00-0xc0) and EEADRH (0-3, PIC18F4620 only)
128
;;  C_IN:  pointer to write buffer (64 bytes) in <buffer>
129
;;        address in <addr> (0x00-0xc0) and EEADRH (0-3, PIC18F4620 only)
130
;;  OUT:  error status in WREG and MIOS_PARAMETER1
131
;;        EEADR will be incremented to the next page address (+64)
132
;;        EEADRH (PIC18F4620) will *not* be incremented on EEADR overrun!
133
;;        FSR1 will be left untouched
134
;;  C_OUT: error status
135
;;  USES: BSR, EEADR, EEDATA, EECON1, EECON2
136
;;  EXAMPLE:
137
;;
138
;;	;; write a page of 64 bytes to EEPROM memory at address 0x80
139
;;	movlw	0x80			; set start address of page
140
;;      movwf   EEADR                   ; (must be aligned to 64)
141
;;	lfsr	FSR1, 0x100		; a free 64 byte buffer in RAM which
142
;;					: contains some data
143
;;	call	MIOS_EEPROM_WritePage	; initiate the write
144
;;	bnz	EEPROMProgrammingFailed	; branch to your exception handler
145
;;					; if necessary
146
;;
147
;;  C_EXAMPLE:
148
;;
149
;;	unsigned char buffer[64];
150
;;	unsigned char i;
151
;;
152
;;	// fill buffer with some bytes
153
;;	for(i=0; i<64; ++i)
154
;;	  buffer[i] = i;
155
;;
156
;;	// write a page of 64 bytes to EEPROM memory at address 0x80
157
;;	MIOS_EEPROM_WritePage(0x80, buffer);
158
;;
159
;; --------------------------------------------------------------------------
160
MIOS_EEPROM_WritePage
161
	;; ensure that address is aligned to 0x40
162
	movlw	0xc0
163
	andwf	EEADR, F
164
 
165
	;; start loop
166
	SET_BSR	MIOS_TMP2
167
	clrf	MIOS_TMP2		; MIOS_TMP2 holds the error status
168
MIOS_EEPROM_WritePage_Loop
169
	movf	EEADR, W		; get byte from buffer
170
	andlw	0x3f
171
	movf	PLUSW1, W
172
	rcall	MIOS_EEPROM_Write	; write to EEPROM
173
	;; OR the result
174
	iorwf	MIOS_TMP2, F, BANKED
175
	;; loop until end of page reached
176
	movf	EEADR, W
177
	andlw	0x3f
178
	bnz	MIOS_EEPROM_WritePage_Loop
179
 
180
	;; copy write result into MIOS_PARAMETER1
181
	movf	MIOS_TMP2, W, BANKED
182
	movwf	MIOS_PARAMETER1
183
 
184
	return
185
 
186
 
187
;; --------------------------------------------------------------------------
188
;;  FUNCTION: MIOS_EEPROM_Read
189
;;  C_DECLARATION: unsigned char MIOS_EEPROM_Read(unsigned char addr)
190
;;  DESCRIPTION: reads a byte from EEPROM.
191
;;  IN:	  address in EEADR (0x00-0xff) and EEADRH (0-3, PIC18F4620 only)
192
;;  C_IN: address in <addr> (0x00-0xff) and EEADRH (0-3, PIC18F4620 only)
193
;;  OUT:  EEPROM content in WREG
194
;;        EEADR will be incremented
195
;;        EEADRH (PIC18F4620) will *not* be incremented on EEADR overrun!
196
;;  C_OUT: returns EEPROM content
197
;;  USES: BSR, EEADR, EEDATA, EECON1, EECON2
198
;;  EXAMPLE:
199
;;
200
;;	;; load a byte from address 0x80
201
;;	movlw	0x80			; write 0x80 to address register
202
;;	movwf	EEADR
203
;;	call	MIOS_EEPROM_Read	; read from EEPROM
204
;;
205
;;  C_EXAMPLE:
206
;;
207
;;	// load a byte from address 0x80
208
;;	value = MIOS_EEPROM_Read(0x80);
209
;;
210
;; --------------------------------------------------------------------------
211
MIOS_EEPROM_Read
212
	;; NOTE: don't use MIOS_TMP2 within this function, it's used by MIOS_EEPROM_ReadPage!
213
 
214
	IRQ_DISABLE				; disable interrupts like recommented in the Errata sheet
215
 
216
	bcf	EECON1, EEPGD			; point to data memory
217
	bcf	EECON1, CFGS			; don't access config sector
218
	bsf	EECON1, RD			; EEPROM read
219
	movf	EEDATA, W			; get data word
220
 
221
	IRQ_ENABLE
222
 
223
	incf	EEADR, F			; increment EEADR
224
#if PIC_DERIVATIVE_EEPROM_SIZE > 0x100
225
	;; TK: no auto increment of EEADRH to avoid compatibility issues with older applications,
226
	;;     where EEADRH is not initialized (it doesn't exist on a PIC18F452)
227
	;; 	skpnz
228
	;; 	incf	EEADRH, F			; increment EEADRH on overrun
229
#endif
230
	return
231
 
232
;; --------------------------------------------------------------------------
233
;;  FUNCTION: MIOS_EEPROM_ReadPage
234
;;  C_DECLARATION: unsigned char MIOS_EEPROM_ReadPage(unsigned char addr, unsigned char *buffer)
235
;;  DESCRIPTION: reads a 64 bytes page from EEPROM<BR>
236
;;  The internal EEPROM of PIC18F452 doesn't provide a page read by itself,
237
;;  therefore this function calls MIOS_EEPROM_Read 64 times.
238
;;  IN:	  pointer to read buffer (64 bytes) in FSR1
239
;;        address in EEADR (0x00-0xc0) and EEADRH (0-3, PIC18F4620 only)
240
;;  C_IN: pointer to read buffer (64 bytes) in <buffer>
241
;;        address in <addr> (0x00-0xc0) and EEADRH (0-3, PIC18F4620 only)
242
;;  OUT:  EEPROM content in read buffer
243
;;        EEADR will be incremented to next page address (+64)
244
;;        EEADRH (PIC18F4620) will *not* be incremented on EEADR overrun!
245
;;        FSR1 will be left untouched
246
;;  C_OUT:  EEPROM content in read <buffer>
247
;;  USES: BSR, EEADR, EEDATA, EECON1, EECON2
248
;;  EXAMPLE:
249
;;
250
;;	;; read a page of 64 bytes from EEPROM at address 0x40
251
;;	movlw	0x40			; set start address of page
252
;;      movwf   EEADR                   ; (must be aligned to 64)
253
;;	lfsr	FSR1, 0x100		; a free 64 byte buffer in RAM which
254
;;					: contains some data
255
;;	call	MIOS_EEPROM_ReadPage	; start the page read
256
;;
257
;;  C_EXAMPLE:
258
;;
259
;;	unsigned char buffer[64];
260
;;
261
;;	// read a page of 64 bytes from EEPROM at address 0x40
262
;;	MIOS_EEPROM_ReadPage(0x40, buffer);
263
;;
264
;; --------------------------------------------------------------------------
265
MIOS_EEPROM_ReadPage
266
	;; ensure that address is aligned to 0x40
267
	movlw	0xc0
268
	andwf	EEADR, F
269
 
270
	;; start loop
271
MIOS_EEPROM_ReadPage_Loop
272
	rcall	MIOS_EEPROM_Read	; read from EEPROM
273
	SET_BSR	MIOS_TMP2
274
	movwf	MIOS_TMP2, BANKED
275
	decf	EEADR, W		; write return value into buffer
276
	andlw	0x3f
277
	movff	MIOS_TMP2, PLUSW1
278
	;; loop until end of page reached
279
	movf	EEADR, W
280
	andlw	0x3f
281
	bnz	MIOS_EEPROM_ReadPage_Loop
282
 
283
	return