Contiki-NG
cfs-coffee-arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, ADVANSEE - http://www.advansee.com/
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  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /**
32  * \addtogroup cc2538-cfs-coffee-arch
33  * @{
34  *
35  * \file
36  * Module for the cc2538 Coffee port
37  */
38 #include "contiki.h"
39 #include "sys/cc.h"
40 #include "cfs/cfs-coffee.h"
41 #include "dev/cc2538-dev.h"
42 #include "dev/rom-util.h"
43 #include "dev/flash.h"
44 #include "dev/watchdog.h"
45 #include "cpu.h"
46 #include "cfs-coffee-arch.h"
47 
48 #include <stdint.h>
49 
50 #ifndef COFFEE_CONF_CUSTOM_PORT
51 /*---------------------------------------------------------------------------*/
52 #if !COFFEE_SECTOR_SIZE || COFFEE_SECTOR_SIZE % FLASH_PAGE_SIZE
53 #error COFFEE_SECTOR_SIZE must be a non-zero multiple of the flash page size
54 #endif
55 #if !COFFEE_PAGE_SIZE || COFFEE_SECTOR_SIZE % COFFEE_PAGE_SIZE
56 #error COFFEE_PAGE_SIZE must be a divisor of COFFEE_SECTOR_SIZE
57 #endif
58 #if COFFEE_PAGE_SIZE % FLASH_WORD_SIZE
59 #error COFFEE_PAGE_SIZE must be a multiple of the flash word size
60 #endif
61 #if COFFEE_START % FLASH_PAGE_SIZE
62 #error COFFEE_START must be aligned with a flash page boundary
63 #endif
64 #if COFFEE_SIZE % COFFEE_SECTOR_SIZE
65 #error COFFEE_SIZE must be a multiple of COFFEE_SECTOR_SIZE
66 #endif
67 #if COFFEE_SIZE / COFFEE_PAGE_SIZE > INT16_MAX
68 #error Too many Coffee pages for coffee_page_t
69 #endif
70 #if COFFEE_START < CC2538_DEV_FLASH_ADDR || \
71  COFFEE_START + COFFEE_SIZE > FLASH_CCA_ADDR
72 #error Coffee does not fit in flash
73 #endif
74 /*---------------------------------------------------------------------------*/
75 void
76 cfs_coffee_arch_erase(uint16_t sector)
77 {
80  rom_util_page_erase(COFFEE_START + sector * COFFEE_SECTOR_SIZE,
81  COFFEE_SECTOR_SIZE);
83 }
84 /*---------------------------------------------------------------------------*/
85 void
86 cfs_coffee_arch_write(const void *buf, unsigned int size, cfs_offset_t offset)
87 {
88  const uint32_t *src = buf;
89  uint32_t flash_addr = COFFEE_START + offset;
90  unsigned int align;
91  uint32_t word, len;
92  uint32_t page_buf[COFFEE_PAGE_SIZE / FLASH_WORD_SIZE];
93  unsigned int i;
94 
95  if(size && (align = flash_addr & (FLASH_WORD_SIZE - 1))) {
96  len = MIN(FLASH_WORD_SIZE - align, size);
97  word = ~((*src & ((1 << (len << 3)) - 1)) << (align << 3));
100  rom_util_program_flash(&word, flash_addr & ~(FLASH_WORD_SIZE - 1),
101  FLASH_WORD_SIZE);
103  *(const uint8_t **)&src += len;
104  size -= len;
105  flash_addr += len;
106  }
107 
108  while(size >= FLASH_WORD_SIZE) {
109  len = MIN(size & ~(FLASH_WORD_SIZE - 1), COFFEE_PAGE_SIZE);
110  for(i = 0; i < len / FLASH_WORD_SIZE; i++) {
111  page_buf[i] = ~*src++;
112  }
115  rom_util_program_flash(page_buf, flash_addr, len);
117  size -= len;
118  flash_addr += len;
119  }
120 
121  if(size) {
122  word = ~(*src & ((1 << (size << 3)) - 1));
125  rom_util_program_flash(&word, flash_addr, FLASH_WORD_SIZE);
127  }
128 }
129 /*---------------------------------------------------------------------------*/
130 void
131 cfs_coffee_arch_read(void *buf, unsigned int size, cfs_offset_t offset)
132 {
133  const uint8_t *src;
134  uint8_t *dst;
135 
137  for(src = (const void *)(COFFEE_START + offset), dst = buf; size; size--) {
138  *dst++ = ~*src++;
139  }
140 }
141 
142 #endif /* COFFEE_CONF_CUSTOM_PORT */
143 
144 /** @} */
void cfs_coffee_arch_read(void *buf, unsigned int size, cfs_offset_t offset)
Reads from the device to a buffer.
void cfs_coffee_arch_erase(uint16_t sector)
Erases a device sector.
#define INTERRUPTS_DISABLE()
Disables all CPU interrupts.
Definition: cpu.h:54
Header for the Coffee file system.
#define INTERRUPTS_ENABLE()
Enables all CPU interrupts.
Definition: cpu.h:51
void cfs_coffee_arch_write(const void *buf, unsigned int size, cfs_offset_t offset)
Writes a buffer to the device.
Header file with prototypes for interrupt control on the cc2538 Cortex-M3 micro.
Header file for the cc2538 devices definitions.
#define COFFEE_START
Start offset of the file system.
Header file for the cc2538 ROM utility function library driver.
#define COFFEE_SECTOR_SIZE
Logical sector size.
Header file for the cc2538 Coffee port module.
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:85
Default definitions of C compiler quirk work-arounds.
#define COFFEE_PAGE_SIZE
Logical page size.