Contiki-NG
Loading...
Searching...
No Matches
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
58void
60{
61 ext_flash_init(NULL);
62}
63int
64xmem_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 read!\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}
93int
94xmem_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 write!\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 ext_flash_close(NULL);
119 return size - remain;
120 }
121 }
122
123 ext_flash_close(NULL);
124
125 return size;
126}
127int
128xmem_erase(long size, unsigned long addr)
129{
130 int rv;
131
132 if(size % EXT_ERASE_UNIT_SIZE != 0) {
133 PRINTF("xmem_erase: bad size\n");
134 return -1;
135 }
136
137 if(addr % EXT_ERASE_UNIT_SIZE != 0) {
138 PRINTF("xmem_erase: bad offset\n");
139 return -1;
140 }
141
142 rv = ext_flash_open(NULL);
143
144 if(!rv) {
145 PRINTF("Could not open flash to erase\n");
146 ext_flash_close(NULL);
147 return -1;
148 }
149
150 rv = ext_flash_erase(NULL, addr, size);
151
152 ext_flash_close(NULL);
153
155
156 if(rv) {
157 return size;
158 }
159
160 PRINTF("Could not erase flash memory\n");
161 return -1;
162}
Header file for the external SPI flash API.
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition watchdog.c:85
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_close(const spi_device_t *conf)
Close the storage driver.
Definition ext-flash.c:355
bool ext_flash_init(const spi_device_t *conf)
Initialise the external flash.
Definition ext-flash.c:527
bool ext_flash_open(const spi_device_t *conf)
Initialize storage driver.
Definition ext-flash.c:325
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
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
void xmem_init(void)
Initialize the external memory.
Definition xmem.c:59
int xmem_erase(long size, unsigned long addr)
Erase a sector in the flash memory.
Definition xmem.c:128
int xmem_pwrite(const void *_buf, int size, unsigned long addr)
Definition xmem.c:94
int xmem_pread(void *_p, int size, unsigned long addr)
Read a number of bytes from an offset in the external memory.
Definition xmem.c:64
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition uip-nd6.c:107
Header file to the external flash memory (XMem) API.