Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read-Only register with fixed value is not comparable to readout #27

Open
trembel opened this issue Nov 2, 2024 · 0 comments
Open

Read-Only register with fixed value is not comparable to readout #27

trembel opened this issue Nov 2, 2024 · 0 comments

Comments

@trembel
Copy link

trembel commented Nov 2, 2024

The following YAML:

VERSION: !Register
    adr: 0x7F
    reset_val: 0x41
    doc: Holds the version number of the IC, currently 0x41.
    layout: !Layout
      VERSION_NUMBER:
        bits: [0-7]
        access: [R]
        accepts: !Fixed 0x41

gets transpiled into the following C code:

// =============================================================================
// ==== VERSION Register =======================================================
// =============================================================================
// Holds the version number of the IC, currently 0x41.
// Fields:
//  - [7:0] VERSION_NUMBER (fixed: 0x41)

#define FH101RF_VERSION_ADDRESS  (0x7FU) //!< VERSION register address
#define FH101RF_VERSION_RESET_LE {0x41U} //!< VERSION register reset value
#define FH101RF_VERSION_RESET_BE {0x41U} //!< VERSION register reset value

// Register Layout Struct:

/**
 * @brief Holds the version number of the IC, currently 0x41.
 * @note use pack/unpack functions for conversion to/from packed binary value
 */
struct fh101rf_version {
  int dummy; // Register contains no variable fields.
};

// Layout struct conversion functions:

/**
 * @brief Convert @ref struct fh101rf_version struct to packed little-endian value.
 * @note use pack/unpack functions for conversion to/from packed binary value
 */
static inline void fh101rf_version_pack_le(const struct fh101rf_version *r, uint8_t val[1]) {
  // VERSION_NUMBER @ version[7:0]:
  val[0] &= (uint8_t)~0xFFU;
  val[0] |= (uint8_t)0x41; // Fixed value.
  (void)r;
}

/**
 * @brief Convert @ref struct fh101rf_version struct to packed big-endian value.
 * @note use pack/unpack functions for conversion to/from packed binary value
 */
static inline void fh101rf_version_pack_be(const struct fh101rf_version *r, uint8_t val[1]) {
  // VERSION_NUMBER @ version[7:0]:
  val[0] &= (uint8_t)~0xFFU;
  val[0] |= (uint8_t)0x41; // Fixed value.
  (void)r;
}

/** @brief Convert packed {endian} binary value to struct. */
static inline struct fh101rf_version fh101rf_version_unpack_le(const uint8_t val[1]) {
  struct fh101rf_version r = {0};
  (void)val;
  (void)r;
  return r;
}

/** @brief Convert packed {endian} binary value to struct. */
static inline struct fh101rf_version fh101rf_version_unpack_be(const uint8_t val[1]) {
  struct fh101rf_version r = {0};
  (void)val;
  (void)r;
  return r;
}

/**
 * @brief Validate struct
 * @returns 0 if valid.
 * @returns the position of the first invalid field if invalid.
 * Confirms that all enums are valid, and all values fit into respective fields
 */
static inline int fh101rf_validate_version(const struct fh101rf_version *r) {
  (void)r;
  return 0;
}

/**
 * @brief Attempt to convert packed {endian} binary value to struct.
 * @returns 0 if valid.
 * @returns the position of the first invalid field if invalid.
 */
static inline int fh101rf_version_try_unpack_le(const uint8_t val[1], struct fh101rf_version *r) {
  *r = fh101rf_version_unpack_le(val);
  return fh101rf_validate_version(r);
}

/**
 * @brief Attempt to convert packed {endian} binary value to struct.
 * @returns 0 if valid.
 * @returns the position of the first invalid field if invalid.
 */
static inline int fh101rf_version_try_unpack_be(const uint8_t val[1], struct fh101rf_version *r) {
  *r = fh101rf_version_unpack_be(val);
  return fh101rf_validate_version(r);
}

Unfortunately, this does not allow to do (pseudo code):

uint8_t result;
read_reg(VERSION_ADDRESS, &result);
struct fh101rf_version ver = fh101rf_version_unpack_le(&result);
ver.version_number == dev->version.version_number, as this field does not even exist.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant