Zephyr API Documentation 4.4.0-rc1
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
30#ifdef __cplusplus
31extern "C" {
32#endif
33
37struct nvmem_cell {
38 /* @cond INTERNAL_HIDDEN */
39
41 const struct device *dev;
43 off_t offset;
45 size_t size;
47 bool read_only;
48
49 /* @endcond */
50};
51
96#define NVMEM_CELL_INIT(node_id) \
97 { \
98 .dev = DEVICE_DT_GET(DT_MTD_FROM_NVMEM_CELL(node_id)), \
99 .offset = DT_REG_ADDR(node_id), \
100 .size = DT_REG_SIZE(node_id), \
101 .read_only = DT_PROP(node_id, read_only), \
102 }
103
155#define NVMEM_CELL_GET_BY_NAME(node_id, name) NVMEM_CELL_INIT(DT_NVMEM_CELL_BY_NAME(node_id, name))
156
169#define NVMEM_CELL_INST_GET_BY_NAME(inst, name) NVMEM_CELL_GET_BY_NAME(DT_DRV_INST(inst), name)
170
189#define NVMEM_CELL_GET_BY_NAME_OR(node_id, name, default_value) \
190 COND_CODE_1(DT_PROP_HAS_NAME(node_id, nvmem_cells, name), \
191 (NVMEM_CELL_GET_BY_NAME(node_id, name)), \
192 (default_value))
193
208#define NVMEM_CELL_INST_GET_BY_NAME_OR(inst, name, default_value) \
209 NVMEM_CELL_GET_BY_NAME_OR(DT_DRV_INST(inst), name, default_value)
210
261#define NVMEM_CELL_GET_BY_IDX(node_id, idx) NVMEM_CELL_INIT(DT_NVMEM_CELL_BY_IDX(node_id, idx))
262
274#define NVMEM_CELL_INST_GET_BY_IDX(inst, idx) NVMEM_CELL_GET_BY_IDX(DT_DRV_INST(inst), idx)
275
293#define NVMEM_CELL_GET_BY_IDX_OR(node_id, idx, default_value) \
294 COND_CODE_1(DT_PROP_HAS_IDX(node_id, nvmem_cells, idx), \
295 (NVMEM_CELL_GET_BY_IDX(node_id, idx)), \
296 (default_value))
297
311#define NVMEM_CELL_INST_GET_BY_IDX_OR(inst, idx, default_value) \
312 NVMEM_CELL_GET_BY_IDX_OR(DT_DRV_INST(inst), idx, default_value)
313
332int nvmem_cell_read(const struct nvmem_cell *cell, void *data, off_t off, size_t len);
333
354int nvmem_cell_write(const struct nvmem_cell *cell, const void *data, off_t off, size_t len);
355
363static inline bool nvmem_cell_is_ready(const struct nvmem_cell *cell)
364{
365 return cell != NULL && device_is_ready(cell->dev);
366}
367
375static inline bool nvmem_cell_is_read_only(const struct nvmem_cell *cell)
376{
377 return cell->read_only;
378}
379
389static inline int nvmem_cell_read_le16(const struct nvmem_cell *cell, uint16_t *val, off_t off)
390{
391 uint8_t buf[sizeof(uint16_t)];
392 int ret;
393
394 ret = nvmem_cell_read(cell, buf, off, sizeof(buf));
395 if (ret == 0) {
396 *val = sys_get_le16(buf);
397 }
398
399 return ret;
400}
401
411static inline int nvmem_cell_read_be16(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_be16(buf);
419 }
420
421 return ret;
422}
423
433static inline int nvmem_cell_read_le32(const struct nvmem_cell *cell, uint32_t *val, off_t off)
434{
435 uint8_t buf[sizeof(uint32_t)];
436 int ret;
437
438 ret = nvmem_cell_read(cell, buf, off, sizeof(buf));
439 if (ret == 0) {
440 *val = sys_get_le32(buf);
441 }
442
443 return ret;
444}
445
455static inline int nvmem_cell_read_be32(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_be32(buf);
463 }
464
465 return ret;
466}
467
477static inline int nvmem_cell_read_le48(const struct nvmem_cell *cell, uint64_t *val, off_t off)
478{
479 uint8_t buf[6];
480 int ret;
481
482 ret = nvmem_cell_read(cell, buf, off, sizeof(buf));
483 if (ret == 0) {
484 *val = sys_get_le48(buf);
485 }
486
487 return ret;
488}
489
499static inline int nvmem_cell_read_be48(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_be48(buf);
507 }
508
509 return ret;
510}
511
521static inline int nvmem_cell_read_le64(const struct nvmem_cell *cell, uint64_t *val, off_t off)
522{
523 uint8_t buf[sizeof(uint64_t)];
524 int ret;
525
526 ret = nvmem_cell_read(cell, buf, off, sizeof(buf));
527 if (ret == 0) {
528 *val = sys_get_le64(buf);
529 }
530
531 return ret;
532}
533
543static inline int nvmem_cell_read_be64(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_be64(buf);
551 }
552
553 return ret;
554}
555
565static inline int nvmem_cell_write_le16(const struct nvmem_cell *cell, uint16_t val, off_t off)
566{
567 uint8_t buf[sizeof(uint16_t)];
568
569 sys_put_le16(val, buf);
570
571 return nvmem_cell_write(cell, buf, off, sizeof(buf));
572}
573
583static inline int nvmem_cell_write_be16(const struct nvmem_cell *cell, uint16_t val, off_t off)
584{
585 uint8_t buf[sizeof(uint16_t)];
586
587 sys_put_be16(val, buf);
588
589 return nvmem_cell_write(cell, buf, off, sizeof(buf));
590}
591
601static inline int nvmem_cell_write_le32(const struct nvmem_cell *cell, uint32_t val, off_t off)
602{
603 uint8_t buf[sizeof(uint32_t)];
604
605 sys_put_le32(val, buf);
606
607 return nvmem_cell_write(cell, buf, off, sizeof(buf));
608}
609
619static inline int nvmem_cell_write_be32(const struct nvmem_cell *cell, uint32_t val, off_t off)
620{
621 uint8_t buf[sizeof(uint32_t)];
622
623 sys_put_be32(val, buf);
624
625 return nvmem_cell_write(cell, buf, off, sizeof(buf));
626}
627
637static inline int nvmem_cell_write_le48(const struct nvmem_cell *cell, uint64_t val, off_t off)
638{
639 uint8_t buf[6];
640
641 sys_put_le48(val, buf);
642
643 return nvmem_cell_write(cell, buf, off, sizeof(buf));
644}
645
655static inline int nvmem_cell_write_be48(const struct nvmem_cell *cell, uint64_t val, off_t off)
656{
657 uint8_t buf[6];
658
659 sys_put_be48(val, buf);
660
661 return nvmem_cell_write(cell, buf, off, sizeof(buf));
662}
663
673static inline int nvmem_cell_write_le64(const struct nvmem_cell *cell, uint64_t val, off_t off)
674{
675 uint8_t buf[sizeof(uint64_t)];
676
677 sys_put_le64(val, buf);
678
679 return nvmem_cell_write(cell, buf, off, sizeof(buf));
680}
681
691static inline int nvmem_cell_write_be64(const struct nvmem_cell *cell, uint64_t val, off_t off)
692{
693 uint8_t buf[sizeof(uint64_t)];
694
695 sys_put_be64(val, buf);
696
697 return nvmem_cell_write(cell, buf, off, sizeof(buf));
698}
699
700#ifdef __cplusplus
701}
702#endif
703
707
708#endif /* ZEPHYR_INCLUDE_NVMEM_H_ */
irp nz macro MOVR cc s mov cc s endm endr irp aw macro LDR aa off
Definition asm-macro-32-bit-gnu.h:17
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:655
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:601
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:637
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:433
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:477
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:583
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:691
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:389
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:565
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:363
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:411
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:619
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:543
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:673
static bool nvmem_cell_is_read_only(const struct nvmem_cell *cell)
Check if an NVMEM cell is read-only.
Definition nvmem.h:375
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:455
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:499
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:521
#define NULL
Definition iar_missing_defs.h:20
__INTPTR_TYPE__ off_t
Definition types.h:36
__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:513
void * data
Address of the device instance private data.
Definition device.h:523
Non-Volatile Memory cell representation.
Definition nvmem.h:37
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:353
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:397
static uint32_t sys_get_be32(const uint8_t src[4])
Get a 32-bit integer stored in big-endian format.
Definition byteorder.h:533
static uint16_t sys_get_le16(const uint8_t src[2])
Get a 16-bit integer stored in little-endian format.
Definition byteorder.h:593
static uint64_t sys_get_le48(const uint8_t src[6])
Get a 48-bit integer stored in little-endian format.
Definition byteorder.h:653
static uint64_t sys_get_le64(const uint8_t src[8])
Get a 64-bit integer stored in little-endian format.
Definition byteorder.h:668
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:323
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:487
static uint16_t sys_get_be16(const uint8_t src[2])
Get a 16-bit integer stored in big-endian format.
Definition byteorder.h:503
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:472
static uint32_t sys_get_le32(const uint8_t src[4])
Get a 32-bit integer stored in little-endian format.
Definition byteorder.h:623
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:382
static uint64_t sys_get_be48(const uint8_t src[6])
Get a 48-bit integer stored in big-endian format.
Definition byteorder.h:563
static uint64_t sys_get_be64(const uint8_t src[8])
Get a 64-bit integer stored in big-endian format.
Definition byteorder.h:578
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:412
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:442