Program Listing for File mem.h

Return to documentation for file (rcsw/utils/mem.h)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "rcsw/rcsw.h"
#include "rcsw/common/fpc.h"
#include "rcsw/utils/utils.h"

/*******************************************************************************
 * Inline Functions
 *
 * These functions are inlined, rather than prototyped, so that the compiler can
 * still optimize them as if it was just a direct memory assignment without
 * checks.
 ******************************************************************************/
BEGIN_C_DECLS

static inline status_t mem_write32(size_t addr, uint32_t wval) {
    RCSW_FPC_NV(ERROR, RCSW_IS_MEM_ALIGNED(addr, sizeof(uint32_t)));
    *((volatile uint32_t *)addr) = wval;
    return OK;
} /* mem_write32() */

static inline uint32_t mem_read32(size_t addr) {
    RCSW_FPC_NV(0, (RCSW_IS_MEM_ALIGNED(addr, sizeof(uint32_t))));
    return *((volatile uint32_t*)addr);
} /* mem_read32() */

static inline status_t mem_rmwr32(uint32_t addr,
                                  uint32_t wval,
                                  uint32_t mask) {
    RCSW_FPC_NV(ERROR, RCSW_IS_MEM_ALIGNED(addr, sizeof(uint32_t)));

    volatile uint32_t curr_val = 0;

    /*
     * If some bits masked, read the memory address, mask off all bits
     * NOT in mask in the result. OR this value with all bits in wval that
     * ARE in the mask.
     */
    if (mask != 0) {
        curr_val = mem_read32(addr);
        wval     = (curr_val & ~mask) | (wval & mask);
    }

    /*
     * Write masked off bits back to memory/register, or just write wval to
     * memory, if mask was 0.
     */
    mem_write32(addr, wval);

    /* read & compare */
    curr_val = mem_read32(addr);
    RCSW_CHECK((curr_val & mask) == (wval & mask));

    return OK;

error:
    return ERROR;
} /* mem_rmwr32() */

/*******************************************************************************
 * Function Protoypes
 ******************************************************************************/
RCSW_API void *mem_cpy32(void * __restrict__ dest,
                         const void * __restrict__ src,
                         size_t n_bytes);

RCSW_API status_t mem_dump32(const void * buf, size_t n_bytes);

RCSW_API status_t mem_dump16(const void * buf, size_t n_bytes);

RCSW_API void mem_dump8(const void * buf, size_t n_bytes);

RCSW_API status_t mem_dump32v(const void * buf, size_t n_bytes);

RCSW_API status_t mem_dump16v(const void * buf, size_t n_bytes);

RCSW_API void mem_dump8v(const void * buf, size_t n_bytes);

RCSW_API status_t mem_bswap16(uint16_t * buf, size_t n_bytes);

RCSW_API status_t mem_bswap32(uint32_t * buf, size_t n_bytes);

END_C_DECLS