9b6db2340d69e2a1f9f6e41582a1742773f84519
[fw/altos] / src / ao_ee.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19 #include "25lc1024.h"
20
21 /*
22  * Using SPI on USART 0, with P1_2 as the chip select
23  */
24
25 #define EE_CS           P1_2
26 #define EE_CS_INDEX     2
27
28 __xdata uint8_t ao_ee_dma_in_done;
29 __xdata uint8_t ao_ee_dma_out_done;
30 __xdata uint8_t ao_ee_mutex;
31
32 uint8_t ao_ee_dma_out_id;
33 uint8_t ao_ee_dma_in_id;
34
35 static __xdata uint8_t  ao_ee_const = 0xff;
36
37 #define ao_ee_delay() do { \
38         _asm nop _endasm; \
39         _asm nop _endasm; \
40         _asm nop _endasm; \
41 } while(0)
42
43 void ao_ee_cs_low(void)
44 {
45         ao_ee_delay();
46         EE_CS = 0;
47         ao_ee_delay();
48 }
49
50 void ao_ee_cs_high(void)
51 {
52         ao_ee_delay();
53         EE_CS = 1;
54         ao_ee_delay();
55 }
56
57 /* Send bytes over SPI.
58  *
59  * This sets up two DMA engines, one writing the data and another reading
60  * bytes coming back.  We use the bytes coming back to tell when the transfer
61  * is complete, as the transmit register is double buffered and hence signals
62  * completion one byte before the transfer is actually complete
63  */
64 static void
65 ao_ee_send(void __xdata *block, uint16_t len)
66 {
67         ao_dma_set_transfer(ao_ee_dma_in_id,
68                             &U0DBUFXADDR,
69                             &ao_ee_const,
70                             len,
71                             DMA_CFG0_WORDSIZE_8 |
72                             DMA_CFG0_TMODE_SINGLE |
73                             DMA_CFG0_TRIGGER_URX0,
74                             DMA_CFG1_SRCINC_0 |
75                             DMA_CFG1_DESTINC_0 |
76                             DMA_CFG1_PRIORITY_NORMAL);
77
78         ao_dma_set_transfer(ao_ee_dma_out_id,
79                             block,
80                             &U0DBUFXADDR,
81                             len,
82                             DMA_CFG0_WORDSIZE_8 |
83                             DMA_CFG0_TMODE_SINGLE |
84                             DMA_CFG0_TRIGGER_UTX0,
85                             DMA_CFG1_SRCINC_1 |
86                             DMA_CFG1_DESTINC_0 |
87                             DMA_CFG1_PRIORITY_NORMAL);
88
89         ao_dma_start(ao_ee_dma_in_id);
90         ao_dma_start(ao_ee_dma_out_id);
91         ao_dma_trigger(ao_ee_dma_out_id);
92         __critical while (!ao_ee_dma_in_done)
93                 ao_sleep(&ao_ee_dma_in_done);
94 }
95
96 /* Receive bytes over SPI.
97  *
98  * This sets up tow DMA engines, one reading the data and another
99  * writing constant values to the SPI transmitter as that is what
100  * clocks the data coming in.
101  */
102 static void
103 ao_ee_recv(void __xdata *block, uint16_t len)
104 {
105         ao_dma_set_transfer(ao_ee_dma_in_id,
106                             &U0DBUFXADDR,
107                             block,
108                             len,
109                             DMA_CFG0_WORDSIZE_8 |
110                             DMA_CFG0_TMODE_SINGLE |
111                             DMA_CFG0_TRIGGER_URX0,
112                             DMA_CFG1_SRCINC_0 |
113                             DMA_CFG1_DESTINC_1 |
114                             DMA_CFG1_PRIORITY_NORMAL);
115
116         ao_dma_set_transfer(ao_ee_dma_out_id,
117                             &ao_ee_const,
118                             &U0DBUFXADDR,
119                             len,
120                             DMA_CFG0_WORDSIZE_8 |
121                             DMA_CFG0_TMODE_SINGLE |
122                             DMA_CFG0_TRIGGER_UTX0,
123                             DMA_CFG1_SRCINC_0 |
124                             DMA_CFG1_DESTINC_0 |
125                             DMA_CFG1_PRIORITY_NORMAL);
126
127         ao_dma_start(ao_ee_dma_in_id);
128         ao_dma_start(ao_ee_dma_out_id);
129         ao_dma_trigger(ao_ee_dma_out_id);
130         __critical while (!ao_ee_dma_in_done)
131                 ao_sleep(&ao_ee_dma_in_done);
132 }
133
134 #define EE_BLOCK        256
135
136 struct ao_ee_instruction {
137         uint8_t instruction;
138         uint8_t address[3];
139 } __xdata ao_ee_instruction;
140
141 static void
142 ao_ee_write_enable(void)
143 {
144         ao_ee_cs_low();
145         ao_ee_instruction.instruction = EE_WREN;
146         ao_ee_send(&ao_ee_instruction, 1);
147         ao_ee_cs_high();
148 }
149
150 static uint8_t
151 ao_ee_rdsr(void)
152 {
153         ao_ee_cs_low();
154         ao_ee_instruction.instruction = EE_RDSR;
155         ao_ee_send(&ao_ee_instruction, 1);
156         ao_ee_recv(&ao_ee_instruction, 1);
157         ao_ee_cs_high();
158         return ao_ee_instruction.instruction;
159 }
160
161 static void
162 ao_ee_wrsr(uint8_t status)
163 {
164         ao_ee_cs_low();
165         ao_ee_instruction.instruction = EE_WRSR;
166         ao_ee_instruction.address[0] = status;
167         ao_ee_send(&ao_ee_instruction, 2);
168         ao_ee_cs_high();
169 }
170
171 #define EE_BLOCK_NONE   0xffff
172
173 static __xdata uint8_t ao_ee_data[EE_BLOCK];
174 static __pdata uint16_t ao_ee_block = EE_BLOCK_NONE;
175 static __pdata uint8_t  ao_ee_block_dirty;
176
177 /* Write the current block to the EEPROM */
178 static void
179 ao_ee_write_block(void)
180 {
181         uint8_t status;
182
183         status = ao_ee_rdsr();
184         if (status & (EE_STATUS_BP0|EE_STATUS_BP1|EE_STATUS_WPEN)) {
185                 status &= ~(EE_STATUS_BP0|EE_STATUS_BP1|EE_STATUS_WPEN);
186                 ao_ee_wrsr(status);
187         }
188         ao_ee_write_enable();
189         ao_ee_cs_low();
190         ao_ee_instruction.instruction = EE_WRITE;
191         ao_ee_instruction.address[0] = ao_ee_block >> 8;
192         ao_ee_instruction.address[1] = ao_ee_block;
193         ao_ee_instruction.address[2] = 0;
194         ao_ee_send(&ao_ee_instruction, 4);
195         ao_ee_send(ao_ee_data, EE_BLOCK);
196         ao_ee_cs_high();
197         for (;;) {
198                 uint8_t status = ao_ee_rdsr();
199                 if ((status & EE_STATUS_WIP) == 0)
200                         break;
201         }
202 }
203
204 /* Read the current block from the EEPROM */
205 static void
206 ao_ee_read_block(void)
207 {
208         ao_ee_cs_low();
209         ao_ee_instruction.instruction = EE_READ;
210         ao_ee_instruction.address[0] = ao_ee_block >> 8;
211         ao_ee_instruction.address[1] = ao_ee_block;
212         ao_ee_instruction.address[2] = 0;
213         ao_ee_send(&ao_ee_instruction, 4);
214         ao_ee_recv(ao_ee_data, EE_BLOCK);
215         ao_ee_cs_high();
216 }
217
218 static void
219 ao_ee_flush_internal(void)
220 {
221         if (ao_ee_block_dirty) {
222                 ao_ee_write_block();
223                 ao_ee_block_dirty = 0;
224         }
225 }
226
227 static void
228 ao_ee_fill(uint16_t block)
229 {
230         if (block != ao_ee_block) {
231                 ao_ee_flush_internal();
232                 ao_ee_block = block;
233                 ao_ee_read_block();
234         }
235 }
236
237 uint8_t
238 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant
239 {
240         uint16_t block;
241         uint16_t this_len;
242         uint8_t this_off;
243
244         if (pos >= AO_EE_DATA_SIZE || pos + len > AO_EE_DATA_SIZE)
245                 return 0;
246         while (len) {
247
248                 /* Compute portion of transfer within
249                  * a single block
250                  */
251                 this_off = pos;
252                 this_len = 256 - (uint16_t) this_off;
253                 block = (uint16_t) (pos >> 8);
254                 if (this_len > len)
255                         this_len = len;
256                 if (this_len & 0xff00)
257                         ao_panic(AO_PANIC_EE);
258
259                 /* Transfer the data */
260                 ao_mutex_get(&ao_ee_mutex); {
261                         if (this_len != 256)
262                                 ao_ee_fill(block);
263                         else {
264                                 ao_ee_flush_internal();
265                                 ao_ee_block = block;
266                         }
267                         memcpy(ao_ee_data + this_off, buf, this_len);
268                         ao_ee_block_dirty = 1;
269                 } ao_mutex_put(&ao_ee_mutex);
270
271                 /* See how much is left */
272                 buf += this_len;
273                 len -= this_len;
274                 pos += this_len;
275         }
276         return 1;
277 }
278
279 uint8_t
280 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant
281 {
282         uint16_t block;
283         uint16_t this_len;
284         uint8_t this_off;
285
286         if (pos >= AO_EE_DATA_SIZE || pos + len > AO_EE_DATA_SIZE)
287                 return 0;
288         while (len) {
289
290                 /* Compute portion of transfer within
291                  * a single block
292                  */
293                 this_off = pos;
294                 this_len = 256 - (uint16_t) this_off;
295                 block = (uint16_t) (pos >> 8);
296                 if (this_len > len)
297                         this_len = len;
298                 if (this_len & 0xff00)
299                         ao_panic(AO_PANIC_EE);
300
301                 /* Transfer the data */
302                 ao_mutex_get(&ao_ee_mutex); {
303                         ao_ee_fill(block);
304                         memcpy(buf, ao_ee_data + this_off, this_len);
305                 } ao_mutex_put(&ao_ee_mutex);
306
307                 /* See how much is left */
308                 buf += this_len;
309                 len -= this_len;
310                 pos += this_len;
311         }
312         return 1;
313 }
314
315 void
316 ao_ee_flush(void) __reentrant
317 {
318         ao_mutex_get(&ao_ee_mutex); {
319                 ao_ee_flush_internal();
320         } ao_mutex_put(&ao_ee_mutex);
321 }
322
323 /*
324  * Read/write the config block, which is in
325  * the last block of the ao_eeprom
326  */
327 uint8_t
328 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant
329 {
330         if (len > AO_EE_BLOCK_SIZE)
331                 return 0;
332         ao_mutex_get(&ao_ee_mutex); {
333                 ao_ee_fill(AO_EE_CONFIG_BLOCK);
334                 memcpy(ao_ee_data, buf, len);
335                 ao_ee_block_dirty = 1;
336                 ao_ee_flush_internal();
337         } ao_mutex_put(&ao_ee_mutex);
338         return 1;
339 }
340
341 uint8_t
342 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant
343 {
344         if (len > AO_EE_BLOCK_SIZE)
345                 return 0;
346         ao_mutex_get(&ao_ee_mutex); {
347                 ao_ee_fill(AO_EE_CONFIG_BLOCK);
348                 memcpy(buf, ao_ee_data, len);
349         } ao_mutex_put(&ao_ee_mutex);
350         return 1;
351 }
352
353 static void
354 ee_dump(void)
355 {
356         __xdata uint8_t b;
357         __xdata uint16_t block;
358         __xdata uint8_t i;
359
360         ao_cmd_hex();
361         block = ao_cmd_lex_i;
362         if (ao_cmd_status != ao_cmd_success)
363                 return;
364         i = 0;
365         do {
366                 if ((i & 7) == 0) {
367                         if (i)
368                                 putchar('\n');
369                         ao_cmd_put16((uint16_t) i);
370                 }
371                 putchar(' ');
372                 ao_ee_read(((uint32_t) block << 8) | i, &b, 1);
373                 ao_cmd_put8(b);
374                 ++i;
375         } while (i != 0);
376         putchar('\n');
377 }
378
379 static void
380 ee_store(void)
381 {
382         __xdata uint16_t block;
383         __xdata uint8_t i;
384         __xdata uint16_t len;
385         __xdata uint8_t b;
386         __xdata uint32_t addr;
387
388         ao_cmd_hex();
389         block = ao_cmd_lex_i;
390         ao_cmd_hex();
391         i = ao_cmd_lex_i;
392         addr = ((uint32_t) block << 8) | i;
393         ao_cmd_hex();
394         len = ao_cmd_lex_i;
395         if (ao_cmd_status != ao_cmd_success)
396                 return;
397         while (len--) {
398                 ao_cmd_hex();
399                 if (ao_cmd_status != ao_cmd_success)
400                         return;
401                 b = ao_cmd_lex_i;
402                 ao_ee_write(addr, &b, 1);
403                 addr++;
404         }
405         ao_ee_flush();
406 }
407
408 __code struct ao_cmds ao_ee_cmds[] = {
409         { 'e', ee_dump,         "e <block>                          Dump a block of EEPROM data" },
410         { 'w', ee_store,        "w <block> <start> <len> <data> ... Write data to EEPROM" },
411         { 0,   ee_store, NULL },
412 };
413
414 /*
415  * To initialize the chip, set up the CS line and
416  * the SPI interface
417  */
418 void
419 ao_ee_init(void)
420 {
421         /* set up CS */
422         EE_CS = 1;
423         P1DIR |= (1 << EE_CS_INDEX);
424         P1SEL &= ~(1 << EE_CS_INDEX);
425
426         /* Set up the USART pin assignment */
427         PERCFG = (PERCFG & ~PERCFG_U0CFG_ALT_MASK) | PERCFG_U0CFG_ALT_2;
428
429         /* Ensure that USART0 takes precidence over USART1 for pins that
430          * they share
431          */
432         P2SEL = (P2SEL & ~P2SEL_PRI3P1_MASK) | P2SEL_PRI3P1_USART0;
433
434         /* Make the SPI pins be controlled by the USART peripheral */
435         P1SEL |= ((1 << 5) | (1 << 4) | (1 << 3));
436
437         /* Set up OUT DMA */
438         ao_ee_dma_out_id = ao_dma_alloc(&ao_ee_dma_out_done);
439
440         /* Set up IN DMA */
441         ao_ee_dma_in_id = ao_dma_alloc(&ao_ee_dma_in_done);
442
443         /* Set up the USART.
444          *
445          * SPI master mode
446          */
447         U0CSR = (UxCSR_MODE_SPI | UxCSR_RE | UxCSR_MASTER);
448
449         /* Set the baud rate and signal parameters
450          *
451          * The cc1111 is limited to a 24/8 MHz SPI clock,
452          * while the 25LC1024 is limited to 20MHz. So,
453          * use the 3MHz clock (BAUD_E 17, BAUD_M 0)
454          */
455         U0BAUD = 0;
456         U0GCR = (UxGCR_CPOL_NEGATIVE |
457                  UxGCR_CPHA_FIRST_EDGE |
458                  UxGCR_ORDER_MSB |
459                  (17 << UxGCR_BAUD_E_SHIFT));
460         ao_cmd_register(&ao_ee_cmds[0]);
461 }