Subversion Repositories svn.mios

Rev

Rev 51 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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