Contiki 2.5
rom.c
1 /*
2  * Copyright (c) 2007, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)$Id: rom.c,v 1.1 2007/02/02 14:08:22 bg- Exp $
30  */
31 
32 #include <stdio.h>
33 
34 #include <avr/boot.h>
35 #include <avr/pgmspace.h>
36 
37 #include "contiki.h"
38 
39 #include "lib/assert.h"
40 
41 #include "dev/rom.h"
42 
43 #if 0
44 #define PRINTF(...) printf(__VA_ARGS__)
45 #else
46 #define PRINTF(...) do {} while (0)
47 #endif
48 
49 BOOTLOADER_SECTION
50 int
51 rom_erase(long nbytes, off_t offset)
52 {
53  long nb = nbytes;
54 
55  if(nbytes % ROM_ERASE_UNIT_SIZE != 0) {
56  return -1;
57  }
58 
59  if(offset % ROM_ERASE_UNIT_SIZE != 0) {
60  return -1;
61  }
62 
63  PRINTF("rom_erase(%ld, %06lx)\n", nbytes, offset);
64 
65  while (nbytes > 0) {
66  spl_t s = splhigh();
67 
68  eeprom_busy_wait();
69 
70  boot_page_erase(offset);
71  boot_spm_busy_wait();
72 
73  boot_rww_enable(); /* Before return or intr. */
74 
75  splx(s);
76 
77  nbytes -= ROM_ERASE_UNIT_SIZE;
78  offset += ROM_ERASE_UNIT_SIZE;
79  }
80 
81  return nb;
82 }
83 
84 int
85 rom_pread(void *buf, int nbytes, off_t offset)
86 {
87  PRINTF("rom_pread(%p, %d, %06lx)\n", buf, nbytes, offset);
88 
89  assert(offset == (uintptr_t)offset);
90  assert((offset + nbytes) == (uintptr_t)(offset + nbytes));
91  memcpy_P(buf, (PGM_P)(uintptr_t)offset, nbytes);
92  return nbytes;
93 }
94 
95 BOOTLOADER_SECTION
96 int
97 rom_pwrite(const void *buf, int nbytes, off_t offset)
98 {
99  long nb = nbytes;
100  const unsigned char *from = buf;
101 
102  PRINTF("rom_pwrite(%p, %d, %06lx)\n", buf, nbytes, offset);
103 
104  while(nbytes > 0) {
105  int i, n = (nbytes > ROM_ERASE_UNIT_SIZE) ? ROM_ERASE_UNIT_SIZE : nbytes;
106  spl_t s = splhigh();
107 
108  eeprom_busy_wait();
109 
110  for (i = 0; i < n; i += 2) {
111  uint16_t w = *from++;
112  w |= (*from++) << 8;
113  boot_page_fill(offset + i, w);
114  }
115  boot_page_write(offset);
116  boot_spm_busy_wait();
117 
118  boot_rww_enable(); /* Before return or intr. */
119 
120  splx(s);
121 
122  nbytes -= ROM_ERASE_UNIT_SIZE;
123  offset += ROM_ERASE_UNIT_SIZE;
124  }
125 
126  return nb;
127 }