Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
nvmem.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 Basalte bv
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
11
12#ifndef ZEPHYR_INCLUDE_NVMEM_H_
13#define ZEPHYR_INCLUDE_NVMEM_H_
14
23
24#include <sys/types.h>
25#include <zephyr/device.h>
26#include <zephyr/devicetree.h>
29#include <zephyr/sys/util.h>
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
36
38#define NVMEM_CELL_FLAG_READ_ONLY BIT(0)
40#define NVMEM_CELL_FLAG_REVERSED BIT(1)
41
43
47struct nvmem_cell {
48 /* @cond INTERNAL_HIDDEN */
49
51 const struct device *dev;
53 off_t offset;
55 size_t size;
58
59 /* @endcond */
60};
61
106#define NVMEM_CELL_INIT(node_id) \
107 { \
108 .dev = DEVICE_DT_GET(DT_MTD_FROM_NVMEM_CELL(node_id)), \
109 .offset = DT_REG_ADDR(node_id), \
110 .size = DT_REG_SIZE(node_id), \
111 .flags = (DT_PROP(node_id, read_only) ? NVMEM_CELL_FLAG_READ_ONLY : 0U) | \
112 (DT_PROP(node_id, zephyr_reversed_byte_order) ? \
113 NVMEM_CELL_FLAG_REVERSED : 0U), \
114 }
115
167#define NVMEM_CELL_GET_BY_NAME(node_id, name) NVMEM_CELL_INIT(DT_NVMEM_CELL_BY_NAME(node_id, name))
168
181#define NVMEM_CELL_INST_GET_BY_NAME(inst, name) NVMEM_CELL_GET_BY_NAME(DT_DRV_INST(inst), name)
182
201#define NVMEM_CELL_GET_BY_NAME_OR(node_id, name, default_value) \
202 COND_CODE_1(DT_PROP_HAS_NAME(node_id, nvmem_cells, name), \
203 (NVMEM_CELL_GET_BY_NAME(node_id, name)), \
204 (default_value))
205
220#define NVMEM_CELL_INST_GET_BY_NAME_OR(inst, name, default_value) \
221 NVMEM_CELL_GET_BY_NAME_OR(DT_DRV_INST(inst), name, default_value)
222
273#define NVMEM_CELL_GET_BY_IDX(node_id, idx) NVMEM_CELL_INIT(DT_NVMEM_CELL_BY_IDX(node_id, idx))
274
286#define NVMEM_CELL_INST_GET_BY_IDX(inst, idx) NVMEM_CELL_GET_BY_IDX(DT_DRV_INST(inst), idx)
287
305#define NVMEM_CELL_GET_BY_IDX_OR(node_id, idx, default_value) \
306 COND_CODE_1(DT_PROP_HAS_IDX(node_id, nvmem_cells, idx), \
307 (NVMEM_CELL_GET_BY_IDX(node_id, idx)), \
308 (default_value))
309
323#define NVMEM_CELL_INST_GET_BY_IDX_OR(inst, idx, default_value) \
324 NVMEM_CELL_GET_BY_IDX_OR(DT_DRV_INST(inst), idx, default_value)
325
348int nvmem_cell_read(const struct nvmem_cell *cell, void *data, off_t off, size_t len);
349
376int nvmem_cell_write(const struct nvmem_cell *cell, const void *data, off_t off, size_t len);
377
385static inline bool nvmem_cell_is_ready(const struct nvmem_cell *cell)
386{
387 return cell != NULL && device_is_ready(cell->dev);
388}
389
397static inline bool nvmem_cell_is_read_only(const struct nvmem_cell *cell)
398{
399 return (cell->flags & NVMEM_CELL_FLAG_READ_ONLY) != 0U;
400}
401
411static inline int nvmem_cell_read_le16(const struct nvmem_cell *cell, uint16_t *val, off_t off)
412{
413 uint8_t buf[sizeof(uint16_t)];
414 int ret;
415
416 ret = nvmem_cell_read(cell, buf, off, sizeof(buf));
417 if (ret == 0) {
418 *val = sys_get_le16(buf);
419 }
420
421 return ret;
422}
423
433static inline int nvmem_cell_read_be16(const struct nvmem_cell *cell, uint16_t *val, off_t off)
434{
435 uint8_t buf[sizeof(uint16_t)];
436 int ret;
437
438 ret = nvmem_cell_read(cell, buf, off, sizeof(buf));
439 if (ret == 0) {
440 *val = sys_get_be16(buf);
441 }
442
443 return ret;
444}
445
455static inline int nvmem_cell_read_le32(const struct nvmem_cell *cell, uint32_t *val, off_t off)
456{
457 uint8_t buf[sizeof(uint32_t)];
458 int ret;
459
460 ret = nvmem_cell_read(cell, buf, off, sizeof(buf));
461 if (ret == 0) {
462 *val = sys_get_le32(buf);
463 }
464
465 return ret;
466}
467
477static inline int nvmem_cell_read_be32(const struct nvmem_cell *cell, uint32_t *val, off_t off)
478{
479 uint8_t buf[sizeof(uint32_t)];
480 int ret;
481
482 ret = nvmem_cell_read(cell, buf, off, sizeof(buf));
483 if (ret == 0) {
484 *val = sys_get_be32(buf);
485 }
486
487 return ret;
488}
489
499static inline int nvmem_cell_read_le48(const struct nvmem_cell *cell, uint64_t *val, off_t off)
500{
501 uint8_t buf[6];
502 int ret;
503
504 ret = nvmem_cell_read(cell, buf, off, sizeof(buf));
505 if (ret == 0) {
506 *val = sys_get_le48(buf);
507 }
508
509 return ret;
510}
511
521static inline int nvmem_cell_read_be48(const struct nvmem_cell *cell, uint64_t *val, off_t off)
522{
523 uint8_t buf[6];
524 int ret;
525
526 ret = nvmem_cell_read(cell, buf, off, sizeof(buf));
527 if (ret == 0) {
528 *val = sys_get_be48(buf);
529 }
530
531 return ret;
532}
533
543static inline int nvmem_cell_read_le64(const struct nvmem_cell *cell, uint64_t *val, off_t off)
544{
545 uint8_t buf[sizeof(uint64_t)];
546 int ret;
547
548 ret = nvmem_cell_read(cell, buf, off, sizeof(buf));
549 if (ret == 0) {
550 *val = sys_get_le64(buf);
551 }
552
553 return ret;
554}
555
565static inline int nvmem_cell_read_be64(const struct nvmem_cell *cell, uint64_t *val, off_t off)
566{
567 uint8_t buf[sizeof(uint64_t)];
568 int ret;
569
570 ret = nvmem_cell_read(cell, buf, off, sizeof(buf));
571 if (ret == 0) {
572 *val = sys_get_be64(buf);
573 }
574
575 return ret;
576}
577
587static inline int nvmem_cell_write_le16(const struct nvmem_cell *cell, uint16_t val, off_t off)
588{
589 uint8_t buf[sizeof(uint16_t)];
590
591 sys_put_le16(val, buf);
592
593 return nvmem_cell_write(cell, buf, off, sizeof(buf));
594}
595
605static inline int nvmem_cell_write_be16(const struct nvmem_cell *cell, uint16_t val, off_t off)
606{
607 uint8_t buf[sizeof(uint16_t)];
608
609 sys_put_be16(val, buf);
610
611 return nvmem_cell_write(cell, buf, off, sizeof(buf));
612}
613
623static inline int nvmem_cell_write_le32(const struct nvmem_cell *cell, uint32_t val, off_t off)
624{
625 uint8_t buf[sizeof(uint32_t)];
626
627 sys_put_le32(val, buf);
628
629 return nvmem_cell_write(cell, buf, off, sizeof(buf));
630}
631
641static inline int nvmem_cell_write_be32(const struct nvmem_cell *cell, uint32_t val, off_t off)
642{
643 uint8_t buf[sizeof(uint32_t)];
644
645 sys_put_be32(val, buf);
646
647 return nvmem_cell_write(cell, buf, off, sizeof(buf));
648}
649
659static inline int nvmem_cell_write_le48(const struct nvmem_cell *cell, uint64_t val, off_t off)
660{
661 uint8_t buf[6];
662
663 sys_put_le48(val, buf);
664
665 return nvmem_cell_write(cell, buf, off, sizeof(buf));
666}
667
677static inline int nvmem_cell_write_be48(const struct nvmem_cell *cell, uint64_t val, off_t off)
678{
679 uint8_t buf[6];
680
681 sys_put_be48(val, buf);
682
683 return nvmem_cell_write(cell, buf, off, sizeof(buf));
684}
685
695static inline int nvmem_cell_write_le64(const struct nvmem_cell *cell, uint64_t val, off_t off)
696{
697 uint8_t buf[sizeof(uint64_t)];
698
699 sys_put_le64(val, buf);
700
701 return nvmem_cell_write(cell, buf, off, sizeof(buf));
702}
703
713static inline int nvmem_cell_write_be64(const struct nvmem_cell *cell, uint64_t val, off_t off)
714{
715 uint8_t buf[sizeof(uint64_t)];
716
717 sys_put_be64(val, buf);
718
719 return nvmem_cell_write(cell, buf, off, sizeof(buf));
720}
721
722#ifdef __cplusplus
723}
724#endif
725
729
730#endif /* ZEPHYR_INCLUDE_NVMEM_H_ */
APIs and macros for the Zephyr device model.
Public NVMEM devicetree header file.
Devicetree main header.
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
static int nvmem_cell_write_be48(const struct nvmem_cell *cell, uint64_t val, off_t off)
Write a big-endian 48-bit value to an NVMEM cell.
Definition nvmem.h:677
static int nvmem_cell_write_le32(const struct nvmem_cell *cell, uint32_t val, off_t off)
Write a little-endian 32-bit value to an NVMEM cell.
Definition nvmem.h:623
static int nvmem_cell_write_le48(const struct nvmem_cell *cell, uint64_t val, off_t off)
Write a little-endian 48-bit value to an NVMEM cell.
Definition nvmem.h:659
static int nvmem_cell_read_le32(const struct nvmem_cell *cell, uint32_t *val, off_t off)
Read a little-endian 32-bit value from an NVMEM cell.
Definition nvmem.h:455
static int nvmem_cell_read_le48(const struct nvmem_cell *cell, uint64_t *val, off_t off)
Read a little-endian 48-bit value from an NVMEM cell.
Definition nvmem.h:499
static int nvmem_cell_write_be16(const struct nvmem_cell *cell, uint16_t val, off_t off)
Write a big-endian 16-bit value to an NVMEM cell.
Definition nvmem.h:605
static int nvmem_cell_write_be64(const struct nvmem_cell *cell, uint64_t val, off_t off)
Write a big-endian 64-bit value to an NVMEM cell.
Definition nvmem.h:713
static int nvmem_cell_read_le16(const struct nvmem_cell *cell, uint16_t *val, off_t off)
Read a little-endian 16-bit value from an NVMEM cell.
Definition nvmem.h:411
static int nvmem_cell_write_le16(const struct nvmem_cell *cell, uint16_t val, off_t off)
Write a little-endian 16-bit value to an NVMEM cell.
Definition nvmem.h:587
int nvmem_cell_read(const struct nvmem_cell *cell, void *data, off_t off, size_t len)
Read data from an NVMEM cell.
static bool nvmem_cell_is_ready(const struct nvmem_cell *cell)
Check if an NVMEM cell is ready.
Definition nvmem.h:385
static int nvmem_cell_read_be16(const struct nvmem_cell *cell, uint16_t *val, off_t off)
Read a big-endian 16-bit value from an NVMEM cell.
Definition nvmem.h:433
static int nvmem_cell_write_be32(const struct nvmem_cell *cell, uint32_t val, off_t off)
Write a big-endian 32-bit value to an NVMEM cell.
Definition nvmem.h:641
static int nvmem_cell_read_be64(const struct nvmem_cell *cell, uint64_t *val, off_t off)
Read a big-endian 64-bit value from an NVMEM cell.
Definition nvmem.h:565
static int nvmem_cell_write_le64(const struct nvmem_cell *cell, uint64_t val, off_t off)
Write a little-endian 64-bit value to an NVMEM cell.
Definition nvmem.h:695
static bool nvmem_cell_is_read_only(const struct nvmem_cell *cell)
Check if an NVMEM cell is read-only.
Definition nvmem.h:397
static int nvmem_cell_read_be32(const struct nvmem_cell *cell, uint32_t *val, off_t off)
Read a big-endian 32-bit value from an NVMEM cell.
Definition nvmem.h:477
static int nvmem_cell_read_be48(const struct nvmem_cell *cell, uint64_t *val, off_t off)
Read a big-endian 48-bit value from an NVMEM cell.
Definition nvmem.h:521
int nvmem_cell_write(const struct nvmem_cell *cell, const void *data, off_t off, size_t len)
Write data to an NVMEM cell.
static int nvmem_cell_read_le64(const struct nvmem_cell *cell, uint64_t *val, off_t off)
Read a little-endian 64-bit value from an NVMEM cell.
Definition nvmem.h:543
__INTPTR_TYPE__ off_t
Definition types.h:36
flags
Definition parser.h:86
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition device.h:607
void * data
Address of the device instance private data.
Definition device.h:617
Non-Volatile Memory cell representation.
Definition nvmem.h:47
Byte order helpers.
static void sys_put_be32(uint32_t val, uint8_t dst[4])
Put a 32-bit integer as big-endian to arbitrary location.
Definition byteorder.h:373
static void sys_put_be64(uint64_t val, uint8_t dst[8])
Put a 64-bit integer as big-endian to arbitrary location.
Definition byteorder.h:417
static uint32_t sys_get_be32(const uint8_t src[4])
Get a 32-bit integer stored in big-endian format.
Definition byteorder.h:553
static uint16_t sys_get_le16(const uint8_t src[2])
Get a 16-bit integer stored in little-endian format.
Definition byteorder.h:613
static uint64_t sys_get_le48(const uint8_t src[6])
Get a 48-bit integer stored in little-endian format.
Definition byteorder.h:673
static uint64_t sys_get_le64(const uint8_t src[8])
Get a 64-bit integer stored in little-endian format.
Definition byteorder.h:688
static void sys_put_be16(uint16_t val, uint8_t dst[2])
Put a 16-bit integer as big-endian to arbitrary location.
Definition byteorder.h:343
static void sys_put_le64(uint64_t val, uint8_t dst[8])
Put a 64-bit integer as little-endian to arbitrary location.
Definition byteorder.h:507
static uint16_t sys_get_be16(const uint8_t src[2])
Get a 16-bit integer stored in big-endian format.
Definition byteorder.h:523
static void sys_put_le48(uint64_t val, uint8_t dst[6])
Put a 48-bit integer as little-endian to arbitrary location.
Definition byteorder.h:492
static uint32_t sys_get_le32(const uint8_t src[4])
Get a 32-bit integer stored in little-endian format.
Definition byteorder.h:643
static void sys_put_be48(uint64_t val, uint8_t dst[6])
Put a 48-bit integer as big-endian to arbitrary location.
Definition byteorder.h:402
static uint64_t sys_get_be48(const uint8_t src[6])
Get a 48-bit integer stored in big-endian format.
Definition byteorder.h:583
static uint64_t sys_get_be64(const uint8_t src[8])
Get a 64-bit integer stored in big-endian format.
Definition byteorder.h:598
static void sys_put_le16(uint16_t val, uint8_t dst[2])
Put a 16-bit integer as little-endian to arbitrary location.
Definition byteorder.h:432
static void sys_put_le32(uint32_t val, uint8_t dst[4])
Put a 32-bit integer as little-endian to arbitrary location.
Definition byteorder.h:462
Misc utilities.