Loading...
Searching...
No Matches
mtd.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 OTA keys S.A.
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
73#ifndef MTD_H
74#define MTD_H
75
76#include <stdbool.h>
77#include <stddef.h>
78#include <stdint.h>
79
80#include "xfa.h"
81
82#ifdef __cplusplus
83extern "C" {
84#endif
85
93
104typedef struct mtd_desc mtd_desc_t;
105
112typedef struct {
114 uint32_t sector_count;
116 uint32_t page_size;
117 uint32_t write_size;
118#if defined(MODULE_MTD_WRITE_PAGE) || DOXYGEN
119 void *work_area;
120#endif
121} mtd_dev_t;
122
136#if !DOXYGEN
138#else
140#endif
141
166#define MTD_XFA_ADD(dev, idx) \
167 XFA_CONST(mtd_dev_t, mtd_dev_xfa, idx) *mtd ## idx = (mtd_dev_t *)&(dev)
168
172#define MTD_NUMOF XFA_LEN(mtd_dev_t *, mtd_dev_xfa)
173
179#define MTD_DRIVER_FLAG_DIRECT_WRITE (1 << 0)
180
192#define MTD_DRIVER_FLAG_CLEARING_OVERWRITE (1 << 1)
193
204struct mtd_desc {
213 int (*init)(mtd_dev_t *dev);
214
228 int (*read)(mtd_dev_t *dev,
229 void *buff,
230 uint32_t addr,
231 uint32_t size);
232
248 int (*read_page)(mtd_dev_t *dev,
249 void *buff,
250 uint32_t page,
251 uint32_t offset,
252 uint32_t size);
253
270 const void *buff,
271 uint32_t page,
272 uint32_t offset,
273 uint32_t size);
274
287 int (*erase)(mtd_dev_t *dev,
288 uint32_t addr,
289 uint32_t size);
290
303 uint32_t sector,
304 uint32_t count);
305
316
320 uint8_t flags;
321};
322
334
352int mtd_read(mtd_dev_t *mtd, void *dest, uint32_t addr, uint32_t count);
353
373int mtd_read_page(mtd_dev_t *mtd, void *dest, uint32_t page, uint32_t offset, uint32_t size);
374
397int mtd_write(mtd_dev_t *mtd, const void *src, uint32_t addr, uint32_t count);
398
423int mtd_write_page_raw(mtd_dev_t *mtd, const void *src, uint32_t page,
424 uint32_t offset, uint32_t size);
425
453int mtd_write_page(mtd_dev_t *mtd, const void *src, uint32_t page,
454 uint32_t offset, uint32_t size);
455
472int mtd_erase(mtd_dev_t *mtd, uint32_t addr, uint32_t count);
473
488int mtd_erase_sector(mtd_dev_t *mtd, uint32_t sector, uint32_t num);
489
511int mtd_write_sector(mtd_dev_t *mtd, const void *src, uint32_t sector, uint32_t num);
512
525int mtd_power(mtd_dev_t *mtd, enum mtd_power_state power);
526
535static inline mtd_dev_t *mtd_dev_get(unsigned idx)
536{
537 assert(MTD_NUMOF != 0);
538 assert(idx < MTD_NUMOF);
539
540 return mtd_dev_xfa[idx];
541}
542
543#ifdef __cplusplus
544}
545#endif
546
547#endif /* MTD_H */
#define assert(cond)
abort the program if assertion is false
Definition assert.h:136
mtd_dev_t *const mtd_dev_xfa[]
MTD device array as XFA.
Definition mtd.h:139
mtd_power_state
MTD power states.
Definition mtd.h:89
int mtd_write_sector(mtd_dev_t *mtd, const void *src, uint32_t sector, uint32_t num)
Write data to a MTD device with whole sector writes.
int mtd_write(mtd_dev_t *mtd, const void *src, uint32_t addr, uint32_t count)
Write data to a MTD device.
int mtd_init(mtd_dev_t *mtd)
mtd_init Initialize a MTD device
int mtd_write_page_raw(mtd_dev_t *mtd, const void *src, uint32_t page, uint32_t offset, uint32_t size)
Write data to a MTD device with pagewise addressing.
int mtd_read(mtd_dev_t *mtd, void *dest, uint32_t addr, uint32_t count)
Read data from a MTD device.
int mtd_read_page(mtd_dev_t *mtd, void *dest, uint32_t page, uint32_t offset, uint32_t size)
Read data from a MTD device with pagewise addressing.
int mtd_erase(mtd_dev_t *mtd, uint32_t addr, uint32_t count)
Erase sectors of a MTD device.
static mtd_dev_t * mtd_dev_get(unsigned idx)
Get an MTD device by index.
Definition mtd.h:535
int mtd_write_page(mtd_dev_t *mtd, const void *src, uint32_t page, uint32_t offset, uint32_t size)
Write data to a MTD device with pagewise addressing.
#define MTD_NUMOF
Number of MTDs defined in the MTD device array in XFA.
Definition mtd.h:172
int mtd_power(mtd_dev_t *mtd, enum mtd_power_state power)
Set power mode on a MTD device.
int mtd_erase_sector(mtd_dev_t *mtd, uint32_t sector, uint32_t num)
Erase sectors of a MTD device.
@ MTD_POWER_DOWN
Power down.
Definition mtd.h:91
@ MTD_POWER_UP
Power up.
Definition mtd.h:90
MTD driver interface.
Definition mtd.h:204
int(* read)(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t size)
Read from the Memory Technology Device (MTD)
Definition mtd.h:228
int(* read_page)(mtd_dev_t *dev, void *buff, uint32_t page, uint32_t offset, uint32_t size)
Read from the Memory Technology Device (MTD) using pagewise addressing.
Definition mtd.h:248
int(* erase)(mtd_dev_t *dev, uint32_t addr, uint32_t size)
Erase sector(s) over the Memory Technology Device (MTD)
Definition mtd.h:287
int(* erase_sector)(mtd_dev_t *dev, uint32_t sector, uint32_t count)
Erase sector(s) of the Memory Technology Device (MTD)
Definition mtd.h:302
int(* power)(mtd_dev_t *dev, enum mtd_power_state power)
Control power of Memory Technology Device (MTD)
Definition mtd.h:315
int(* write_page)(mtd_dev_t *dev, const void *buff, uint32_t page, uint32_t offset, uint32_t size)
Write to the Memory Technology Device (MTD) using pagewise addressing.
Definition mtd.h:269
uint8_t flags
Properties of the MTD driver.
Definition mtd.h:320
int(* init)(mtd_dev_t *dev)
Initialize Memory Technology Device (MTD)
Definition mtd.h:213
MTD device descriptor.
Definition mtd.h:112
uint32_t write_size
Minimum size and alignment of writes to the device.
Definition mtd.h:117
uint32_t pages_per_sector
Number of pages per sector in the MTD.
Definition mtd.h:115
uint32_t sector_count
Number of sector in the MTD.
Definition mtd.h:114
const mtd_desc_t * driver
MTD driver.
Definition mtd.h:113
void * work_area
sector-sized buffer (only present when mtd_write_page is enabled)
Definition mtd.h:119
uint32_t page_size
Size of the pages in the MTD.
Definition mtd.h:116
Cross File Arrays.
#define XFA_USE_CONST(type, name)
Declare an external read-only cross-file array.
Definition xfa.h:114