Contiki 3.x
xmem.c
Go to the documentation of this file.
1 /**
2  * \file
3  * Xmem implementation for IRoad platform using flash driver and FLASH_ID0.
4  *
5  * \author
6  * Henrik Makitaavola <henrik@makitaavola.se>
7  */
8 
9 #include "xmem.h"
10 #include "flash.h"
11 #include "interrupt.h"
12 
13 void
14 xmem_init(void)
15 {
16  MK60_ENTER_CRITICAL_REGION();
17  flash_init();
18  MK60_LEAVE_CRITICAL_REGION();
19 }
20 int
21 xmem_pread(void *buf, int nbytes, unsigned long offset)
22 {
23  flash_error_t r;
24  MK60_ENTER_CRITICAL_REGION();
25  r = flash_readi(FLASH_ID0, offset, buf, nbytes, FLASH_WAIT);
26  MK60_LEAVE_CRITICAL_REGION();
27  if(r == E_FLASH_OK) {
28  return nbytes;
29  }
30  return 0;
31 }
32 int
33 xmem_pwrite(const void *buf, int nbytes, unsigned long offset)
34 {
35  flash_error_t r;
36  MK60_ENTER_CRITICAL_REGION();
37  r = flash_writei(FLASH_ID0, offset, (uint8_t *)buf, nbytes, FLASH_WAIT | FLASH_FINISH);
38  MK60_LEAVE_CRITICAL_REGION();
39  if(r == E_FLASH_OK) {
40  return nbytes;
41  }
42  return 0;
43 }
44 int
45 xmem_erase(long nbytes, unsigned long offset)
46 {
47  uint16_t i;
48  uint16_t first;
49 
50  /* Offset must be at a start of a sector */
51  if(offset % FLASH_SECTOR_SIZE != 0) {
52  return 0;
53  }
54  /* nbytes must be a multiple of sectors */
55  if(nbytes % FLASH_SECTOR_SIZE != 0) {
56  return 0;
57  }
58  first = offset / FLASH_SECTOR_SIZE;
59  for(i = first; i < (first + nbytes / FLASH_SECTOR_SIZE); ++i) {
60  flash_error_t r;
61  MK60_ENTER_CRITICAL_REGION();
62  r = flash_erase_sector(FLASH_ID0, i, FLASH_WAIT | FLASH_FINISH);
63  MK60_LEAVE_CRITICAL_REGION();
64  if(r != E_FLASH_OK) {
65  /* TODO(henrik) fix return code so that number of erased bytes is returned. */
66  return 0;
67  }
68  }
69 
70  return nbytes;
71 }
Flash device driver header file for the Mulle platform.
K60 interrupt save/restore macros.