Contiki-NG
xmem.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, 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  */
30 
31 /**
32  * \file
33  * Device driver for the MX25R8035F 1Mbyte external memory.
34  * \author
35  * Dongda Lee <dongdongbhbh@gmail.com>
36  *
37  * Data is written bit inverted (~-operator) to flash so that
38  * unwritten data will read as zeros (UNIX style).
39  */
40 
41 #include "contiki.h"
42 #include "ext-flash.h"
43 #include "dev/xmem.h"
44 #include "dev/watchdog.h"
45 #include "cfs-coffee-arch.h"
46 #include <stdio.h> /* For PRINTF() */
47 
48 #define EXT_ERASE_UNIT_SIZE 4096UL
49 
50 #define XMEM_BUFF_LENGHT 128
51 
52 #if 0
53 #define PRINTF(...) printf(__VA_ARGS__)
54 #else
55 #define PRINTF(...) do {} while(0)
56 #endif
57 
58 void
59 xmem_init(void)
60 {
61  ext_flash_open(NULL);
62 }
63 int
64 xmem_pread(void *_p, int size, unsigned long addr)
65 {
66  int rv;
67  uint8_t x;
68  int i;
69 
70  rv = ext_flash_open(NULL);
71 
72  if(!rv) {
73  PRINTF("Could not open flash to save config\n");
74  ext_flash_close(NULL);
75  return -1;
76  }
77 
78  rv = ext_flash_read(NULL, addr, size, _p);
79  for(i = 0; i < size; i++) {
80  x = ~*((uint8_t *)_p + i);
81  *((uint8_t *)_p + i) = x;
82  }
83 
84  ext_flash_close(NULL);
85 
86  if(rv) {
87  return size;
88  }
89 
90  PRINTF("Could not read flash memory!\n");
91  return -1;
92 }
93 int
94 xmem_pwrite(const void *_buf, int size, unsigned long addr)
95 {
96  int rv;
97  int i, j;
98  int remain;
99 
100  uint8_t tmp_buf[XMEM_BUFF_LENGHT];
101 
102  rv = ext_flash_open(NULL);
103 
104  if(!rv) {
105  PRINTF("Could not open flash to save config!\n");
106  ext_flash_close(NULL);
107  return -1;
108  }
109 
110  for(remain = size, j = 0; remain > 0; remain -= XMEM_BUFF_LENGHT, j += XMEM_BUFF_LENGHT) {
111  int to_write = MIN(XMEM_BUFF_LENGHT, remain);
112  for(i = 0; i < to_write; i++) {
113  tmp_buf[i] = ~*((uint8_t *)_buf + j + i);
114  }
115  rv = ext_flash_write(NULL, addr + j, to_write, tmp_buf);
116  if(!rv) {
117  PRINTF("Could not write flash memory!\n");
118  return size - remain;
119  }
120  }
121 
122  ext_flash_close(NULL);
123 
124  return size;
125 }
126 int
127 xmem_erase(long size, unsigned long addr)
128 {
129  int rv;
130 
131  rv = ext_flash_open(NULL);
132 
133  if(!rv) {
134  PRINTF("Could not open flash to save config\n");
135  ext_flash_close(NULL);
136  return -1;
137  }
138 
139  if(size % EXT_ERASE_UNIT_SIZE != 0) {
140  PRINTF("xmem_erase: bad size\n");
141  return -1;
142  }
143 
144  if(addr % EXT_ERASE_UNIT_SIZE != 0) {
145  PRINTF("xmem_erase: bad offset\n");
146  return -1;
147  }
148 
149  rv = ext_flash_erase(NULL, addr, size);
150 
151  ext_flash_close(NULL);
152 
154 
155  if(rv) {
156  return size;
157  }
158 
159  PRINTF("Could not erase flash memory\n");
160  return -1;
161 }
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:107
bool ext_flash_read(const spi_device_t *conf, uint32_t offset, uint32_t length, uint8_t *buf)
Read storage content.
Definition: ext-flash.c:374
bool ext_flash_open(const spi_device_t *conf)
Initialize storage driver.
Definition: ext-flash.c:325
bool ext_flash_erase(const spi_device_t *conf, uint32_t offset, uint32_t length)
Erase storage sectors corresponding to the range.
Definition: ext-flash.c:475
bool ext_flash_close(const spi_device_t *conf)
Close the storage driver.
Definition: ext-flash.c:355
Header file for the external SPI flash API.
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:85
bool ext_flash_write(const spi_device_t *conf, uint32_t offset, uint32_t length, const uint8_t *buf)
Write to storage sectors.
Definition: ext-flash.c:415