Line No. | Rev | Author | Line |
---|---|---|---|
1 | 3 | kaklik | ///////////////////////////////////////////////////////////////////////// |
2 | // |
||
3 | // KNIHOVNA PRO PRACI S EXTERNI PAMETI EEPROM RADY 24XX (I2C SBERNICE) |
||
4 | // |
||
5 | ///////////////////////////////////////////////////////////////////////// |
||
6 | // |
||
7 | // PEFI S.ICZ a.s. 2003 |
||
8 | // |
||
9 | // Verze 1.0 - uvodni verze |
||
10 | // POZN: podpora HW adresy neni vyzkousena, nebyla k dispozici takova soucastka |
||
11 | // |
||
12 | ///////////////////////////////////////////////////////////////////////// |
||
13 | // |
||
14 | // funkce: |
||
15 | // ExtEE_init() |
||
16 | // - inicializuje porty a pripadne HW i2c jednotku |
||
17 | // |
||
18 | // ExtEE_write_byte(int16 adr,int8 data) |
||
19 | // - zapise bajt na adresu |
||
20 | // |
||
21 | // ExtEE_write_array(int16 adr,int8 *data, int8 Len) |
||
22 | // - zapise pole bajtu delky Len od adresy adr |
||
23 | // |
||
24 | // int8 ExtEE_read_byte(int16 adr) |
||
25 | // - precte bajt z adresy adr |
||
26 | // |
||
27 | // int8 ExtEE_read_byte_inc() |
||
28 | // - precte bajt z adresy nasledujici po adrese posledni operace a inkrementuje ji |
||
29 | // - funguje jen v ramci jedne stranky |
||
30 | // |
||
31 | // ExtEE_read_array(int16 adr,int8 *data, int8 Len) |
||
32 | // - precte pole bajtu delky Len od adresy adr |
||
33 | // |
||
34 | // definice a prepinace |
||
35 | // #define EXTEE_SDA /* pin, na kterem je pripojen signal SDA */ |
||
36 | // #define EXTEE_SCL /* pin, na kterem je pripojen signal SCL */ |
||
37 | // #define EXTEE_WRITE_BURST /* pocet bajtu, ktere lze zapsat do pameti naraz */ |
||
38 | // #define EXTEE_PAGE_CTRL /* zapne(1) / vypne(0) podporu strankovani */ |
||
39 | // #define EXT_EE_HW_ADR /* nastavi a zapne podporu hw adresy I2C zarizeni */ |
||
40 | // #define EXTEE_HW /* zapne(1) / vypne(0) HW podporu i2c */ |
||
41 | // POZN: Vypnuti podpory strankvani prodlouzi funkci ExtEE_write_array o cca 20 slov, ovsem |
||
42 | // potom je treba hlidat vztah mezi adresou a delkou zapisovaneho pole. Lze zapsat max. EXTEE_WRITE_BURST bajtu |
||
43 | // a to pocinaje adresou v nasobcich EXTEE_WRITE_BURST (0x,1x,2x...). Pokud nezapiseme cely burst a budeme priste pokracovat |
||
44 | // nasledujici adresou, lze zapsat jen cast do EXTEE_WRITE_BURST. Zbytek lze zapsat novym burstem. Opet max. EXTEE_WRITE_BURST bajtu |
||
45 | // Podporu EXTEE_HW lze zapnout u procesoru, ktere tuto jednotku maji. |
||
46 | // |
||
47 | // |
||
48 | // defaultni definice |
||
49 | // |
||
50 | #ifndef EXTEE_SDA |
||
51 | #define EXTEE_SDA PIN_C4 |
||
52 | #define EXTEE_SCL PIN_C3 |
||
53 | #endif |
||
54 | #ifndef EXTEE_WRITE_BURST |
||
55 | #define EXTEE_WRITE_BURST 16 /* pocet bajtu, ktere lze zapsat naraz */ |
||
56 | #endif |
||
57 | #ifndef EXTEE_PAGE_CTRL |
||
58 | #define EXTEE_PAGE_CTRL 1 /* podpora stranokvani zapnuta */ |
||
59 | #endif |
||
60 | #ifndef EXTEE_HW |
||
61 | #define EXTEE_HW 0 /* HW podpora i2c vypnuta */ |
||
62 | #endif |
||
63 | |||
64 | |||
65 | |||
66 | /****************************************************************************************************/ |
||
67 | // Privatni sekce, cist jen v pripade, ze neco nefunguje |
||
68 | /****************************************************************************************************/ |
||
69 | // nastaveni podpory I2C sbernice |
||
70 | #if EXTEE_HW != 0 |
||
71 | #use i2c(master,sda=EXTEE_SDA,scl=EXTEE_SCL,FORCE_HW) |
||
72 | #else |
||
73 | #use i2c(master,sda=EXTEE_SDA,scl=EXTEE_SCL) |
||
74 | #endif |
||
75 | |||
76 | |||
77 | ////////////////////////////////////////////////////////// |
||
78 | ///// FUNKCE pro inicializaci |
||
79 | ////////////////////////////////////////////////////////// |
||
80 | // inicializuje port |
||
81 | // |
||
82 | void ExtEE_init() { |
||
83 | |||
84 | output_float(EXTEE_SCL); |
||
85 | output_float(EXTEE_sda); |
||
86 | #ifdef EEPROM_HW |
||
87 | // zapne SSP jednotku, #use i2c to neudela, asi chyba v cecku |
||
88 | #ASM |
||
89 | BSF SSPCON._SSPEN // BSF 0x14.5 |
||
90 | BSF SSPCON._SSPM3 // BSF 0x14.3 |
||
91 | #endasm |
||
92 | #endif |
||
93 | } |
||
94 | |||
95 | |||
96 | |||
97 | //////////////////////////////// |
||
98 | /// FUNKCE PRO ZAPIS |
||
99 | /////////////////////////////// |
||
100 | // zapise bajt do eeprom |
||
101 | // |
||
102 | void ExtEE_write_byte(int16 adr, int8 data) { |
||
103 | #ifdef EXTEE_HW_ADR |
||
104 | do |
||
105 | i2c_start(); |
||
106 | while (i2c_write((0xa0|((EXTEE_HW_ADR<<1)&0x0f))&0xfe)); // zkousej dal pokud je pamet zaneprazdnena |
||
107 | i2c_write(make8(adr,1)); // posli vyssi cast adresy |
||
108 | #else |
||
109 | do |
||
110 | i2c_start(); |
||
111 | while (i2c_write((0xa0|((make8(adr,1)<<1)&0x0f))&0xfe)); // zkousej dal pokud je pamet zaneprazdnena |
||
112 | #endif |
||
113 | |||
114 | // i2c_write(make8(adr,0)); //je o 1 slovo delsi a asi vice koser |
||
115 | i2c_write(adr); |
||
116 | i2c_write(data); |
||
117 | i2c_stop(); |
||
118 | } |
||
119 | |||
120 | |||
121 | #if EXTEE_PAGE_CTRL != 0 |
||
122 | // zapise pole bajtu do eeprom, varianta s kontolou strankovani |
||
123 | // |
||
124 | void ExtEE_write_array(int16 adr, int8 *data, int8 Len) { |
||
125 | |||
126 | int8 burst; |
||
127 | |||
128 | while(len!=0) |
||
129 | { |
||
130 | burst=(EXTEE_WRITE_BURST-1)-(make8(adr,0)&(EXTEE_WRITE_BURST-1))+1; // pocet bajtu, ktere lze naraz zapsat |
||
131 | if(burst>len) burst=len; // zapiseme zbytek |
||
132 | len=len-burst; |
||
133 | // navazani spojeni a zapis adresy |
||
134 | #ifdef EXTEE_HW_ADR |
||
135 | do |
||
136 | i2c_start(); |
||
137 | while (i2c_write((0xa0|((EXTEE_HW_ADR<<1)&0x0f))&0xfe)); // zkousej dal pokud je pamet zaneprazdnena |
||
138 | i2c_write(make8(adr,1)); // posli vyssi cast adresy |
||
139 | #else |
||
140 | do |
||
141 | i2c_start(); |
||
142 | while(i2c_write((0xa0|((make8(adr,1)<<1)&0x0f))&0xfe)); // zkousej dal pokud je pamet zaneprazdnena |
||
143 | #endif |
||
144 | i2c_write(adr); |
||
145 | adr+=burst; // adresu posun na zacatek dalsiho burstu |
||
146 | // poslani burstu bajtu |
||
147 | do |
||
148 | i2c_write(*Data++); |
||
149 | while (--burst); |
||
150 | i2c_stop(); |
||
151 | } |
||
152 | } |
||
153 | #else |
||
154 | // zapise pole bajtu do eeprom, varianta bez kontroly strankovani, je o 21 slov kratsi |
||
155 | // |
||
156 | void ExtEE_write_array(int16 adr, int8 *data, int8 Len) { |
||
157 | // navazani spojeni a zapis adresy |
||
158 | #ifdef EXTEE_HW_ADR |
||
159 | do |
||
160 | i2c_start(); |
||
161 | while (i2c_write((0xa0|((EXTEE_HW_ADR<<1)&0x0f))&0xfe)); // zkousej dal pokud je pamet zaneprazdnena |
||
162 | i2c_write(make8(adr,1)); // posli vyssi cast adresy |
||
163 | #else |
||
164 | do |
||
165 | i2c_start(); |
||
166 | while(i2c_write((0xa0|((make8(adr,1)<<1)&0x0f))&0xfe)); // zkousej dal pokud je pamet zaneprazdnena |
||
167 | #endif |
||
168 | i2c_write(adr); |
||
169 | // poslani burstu bajtu |
||
170 | do |
||
171 | i2c_write(*Data++); |
||
172 | while (--len); |
||
173 | i2c_stop(); |
||
174 | } |
||
175 | #endif |
||
176 | |||
177 | |||
178 | |||
179 | //////////////////////////////// |
||
180 | /// FUNKCE PRO CTENI |
||
181 | /////////////////////////////// |
||
182 | // precte bajt z eeprom |
||
183 | // |
||
184 | int8 ExtEE_read_byte(int16 adr) { |
||
185 | |||
186 | int8 data; |
||
187 | |||
188 | #ifdef EXTEE_HW_ADR |
||
189 | do |
||
190 | i2c_start(); |
||
191 | while (i2c_write((0xa0|((EXTEE_HW_ADR<<1)&0x0f))&0xfe)); // zkousej dal pokud je pamet zaneprazdnena |
||
192 | i2c_write(make8(adr,1)); // posli vyssi cast adresy |
||
193 | #else |
||
194 | do |
||
195 | i2c_start(); |
||
196 | while(i2c_write((0xa0|((make8(adr,1)<<1)&0x0f))&0xfe)); // zkousej dal pokud je pamet zaneprazdnena |
||
197 | #endif |
||
198 | i2c_write(adr); |
||
199 | i2c_start(); |
||
200 | #ifdef EXTEE_HW_ADR |
||
201 | i2c_write((0xa0|((EXTEE_HW_ADR<<1)&0x0f))|1); |
||
202 | #else |
||
203 | i2c_write((0xa0|((make8(adr,1)<<1)&0x0f))|1); |
||
204 | #endif |
||
205 | data=i2c_read(0); |
||
206 | i2c_stop(); |
||
207 | return(data); |
||
208 | } |
||
209 | |||
210 | |||
211 | // precte bajt z eeprom z posledni adresy+1 a adresu inkrementuje, funguje jen v ramci jedne stranky |
||
212 | // |
||
213 | int8 ExtEE_read_byte_inc() { |
||
214 | |||
215 | int8 data; |
||
216 | |||
217 | i2c_start(); |
||
218 | #ifdef EXTEE_HW_ADR |
||
219 | i2c_write((0xa0|((EXTEE_HW_ADR<<1)&0x0f))|1); |
||
220 | #else |
||
221 | i2c_write(0xa1); |
||
222 | #endif |
||
223 | data=i2c_read(0); |
||
224 | i2c_stop(); |
||
225 | return(data); |
||
226 | } |
||
227 | |||
228 | |||
229 | // precte pole bajtu z eeprom |
||
230 | // |
||
231 | void ExtEE_read_array(int16 adr, int8 *Data, int8 Len) { |
||
232 | #ifdef EXTEE_HW_ADR |
||
233 | do |
||
234 | i2c_start(); |
||
235 | while (i2c_write((0xa0|((EXTEE_HW_ADR<<1)&0x0f))&0xfe)); // zkousej dal pokud je pamet zaneprazdnena |
||
236 | i2c_write(make8(adr,1)); // posli vyssi cast adresy |
||
237 | #else |
||
238 | do |
||
239 | i2c_start(); |
||
240 | while(i2c_write((0xa0|((make8(adr,1)<<1)&0x0f))&0xfe)); // zkousej dal pokud je pamet zaneprazdnena |
||
241 | #endif |
||
242 | i2c_write(adr); |
||
243 | i2c_start(); |
||
244 | #ifdef EXTEE_HW_ADR |
||
245 | i2c_write((0xa0|((EXTEE_HW_ADR<<1)&0x0f))&0xfe); // zkousej dal pokud je pamet zaneprazdnena |
||
246 | #else |
||
247 | i2c_write((0xa0|((make8(adr,1)<<1)&0x0f))|1); |
||
248 | #endif |
||
249 | Len--; // posledni bajt se cte zvlast, nema ACK |
||
250 | do |
||
251 | *(Data++)=i2c_read(1); |
||
252 | while (--Len); |
||
253 | *(data++)=i2c_read(0); // u posledniho bajtu neni ACK |
||
254 | i2c_stop(); |
||
255 | } |
||
256 | // End of File |
Powered by WebSVN v2.8.3