Contiki-NG
cfs-coffee-arch.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
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 copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30/*---------------------------------------------------------------------------*/
31/**
32 * \addtogroup gecko
33 * @{
34 *
35 * \addtogroup gecko-storage Storage drivers
36 * @{
37 *
38 * \addtogroup gecko-storage-cfs Storage CFS coffee driver
39 * @{
40 *
41 * \file
42 * storage driver for the gecko.
43 * \author
44 * Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
45 *
46 */
47/*---------------------------------------------------------------------------*/
48#include "contiki.h"
49#include "sys/cc.h"
50#include "cfs/cfs-coffee.h"
51#include "dev/watchdog.h"
52#include "cfs-coffee-arch.h"
53
54#include <stdint.h>
55#include <string.h>
56
57#define FLASH_WORD_SIZE 4
58
59/*---------------------------------------------------------------------------*/
60#if !COFFEE_SECTOR_SIZE || COFFEE_SECTOR_SIZE % FLASH_PAGE_SIZE
61#error COFFEE_SECTOR_SIZE must be a non-zero multiple of the flash page size
62#endif
63#if !COFFEE_PAGE_SIZE || COFFEE_SECTOR_SIZE % COFFEE_PAGE_SIZE
64#error COFFEE_PAGE_SIZE must be a divisor of COFFEE_SECTOR_SIZE
65#endif
66#if COFFEE_PAGE_SIZE % FLASH_WORD_SIZE
67#error COFFEE_PAGE_SIZE must be a multiple of the flash word size
68#endif
69#if COFFEE_SIZE % FLASH_PAGE_SIZE
70#error COFFEE_SIZE must be aligned with a flash page boundary
71#endif
72#if COFFEE_SIZE % COFFEE_SECTOR_SIZE
73#error COFFEE_SIZE must be a multiple of COFFEE_SECTOR_SIZE
74#endif
75#if COFFEE_SIZE / COFFEE_PAGE_SIZE > INT16_MAX
76#error Too many Coffee pages for coffee_page_t
77#endif
78/*---------------------------------------------------------------------------*/
79extern char linker_nvm_begin;
80__attribute__((used)) uint8_t
81cfs_coffee_default_storage[COFFEE_SIZE]
82__attribute__ ((section(".simee")));
83#define NVM_BASE (&linker_nvm_begin)
84/*---------------------------------------------------------------------------*/
85void
86cfs_coffee_arch_erase(uint16_t sector)
87{
88 uint32_t flash_addr;
89
90 flash_addr = (uint32_t)NVM_BASE +
91 +(sector * COFFEE_SECTOR_SIZE);
92
93 MSC_Init();
94 MSC_ErasePage((uint32_t *)flash_addr);
95 MSC_Deinit();
96}
97/*---------------------------------------------------------------------------*/
98void
99cfs_coffee_arch_write(const void *buf, unsigned int size, cfs_offset_t offset)
100{
101 const uint32_t *src = buf;
102 uint32_t flash_addr = (uint32_t)NVM_BASE + offset;
103 unsigned int align;
104 uint32_t word;
105 uint32_t len;
106 uint32_t page_buf[COFFEE_PAGE_SIZE / FLASH_WORD_SIZE];
107 unsigned int i;
108 MSC_Status_TypeDef retVal = mscReturnOk;
109
110 if(size && (align = flash_addr & (FLASH_WORD_SIZE - 1))) {
111 len = MIN(FLASH_WORD_SIZE - align, size);
112 word = ~((*src & ((1 << (len << 3)) - 1)) << (align << 3));
113 MSC_Init();
114 retVal = MSC_WriteWord((uint32_t *)(flash_addr & ~(FLASH_WORD_SIZE - 1)),
115 &word, FLASH_WORD_SIZE);
116 MSC_Deinit();
117 if(retVal != mscReturnOk) {
118 return;
119 }
120 *(const uint8_t **)&src += len;
121 size -= len;
122 flash_addr += len;
123 }
124
125 while(size >= FLASH_WORD_SIZE) {
126 len = MIN(size & ~(FLASH_WORD_SIZE - 1), COFFEE_PAGE_SIZE);
127 for(i = 0; i < len / FLASH_WORD_SIZE; i++) {
128 page_buf[i] = ~*src++;
129 }
130 MSC_Init();
131 retVal = MSC_WriteWord((uint32_t *)flash_addr, page_buf, len);
132 MSC_Deinit();
133 if(retVal != mscReturnOk) {
134 return;
135 }
136 size -= len;
137 flash_addr += len;
138 }
139
140 if(size) {
141 word = ~(*src & ((1 << (size << 3)) - 1));
142 MSC_Init();
143 retVal = MSC_WriteWord((uint32_t *)flash_addr, &word, FLASH_WORD_SIZE);
144 MSC_Deinit();
145 if(retVal != mscReturnOk) {
146 return;
147 }
148 }
149}
150/*---------------------------------------------------------------------------*/
151void
152cfs_coffee_arch_read(void *buf, unsigned int size, cfs_offset_t offset)
153{
154 const uint8_t *src;
155 uint8_t *dst;
156
157 for(src = (const void *)(NVM_BASE + offset), dst = buf; size; size--) {
158 *dst++ = ~*src++;
159 }
160}
161/*---------------------------------------------------------------------------*/
162/**
163 * @}
164 * @}
165 * @}
166 */
Default definitions of C compiler quirk work-arounds.
Header for the Coffee file system.
void cfs_coffee_arch_write(const void *buf, unsigned int size, cfs_offset_t offset)
Writes a buffer to the device.
#define COFFEE_SIZE
Total size in bytes of the file system.
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 COFFEE_SECTOR_SIZE
Logical sector size.
#define COFFEE_PAGE_SIZE
Logical page size.
int cfs_offset_t
CFS directory entry name length.
Definition: cfs.h:65
Coffee architecture-dependent header for the Zolertia Z1 platform.