从16位硬件寄存器读取 [英] Reading from 16-bit hardware registers

查看:417
本文介绍了从16位硬件寄存器读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在嵌入式系统中,我们有一个设置,允许我们通过命令行界面读取任意数据进行诊断。对于大多数数据,这可以正常工作,我们使用 memcpy()复制所请求的地址上的数据,并通过串行连接发送。

On an embedded system we have a setup that allows us to read arbitrary data over a command-line interface for diagnostic purposes. For most data, this works fine, we use memcpy() to copy data at the requested address and send it back across a serial connection.

但是,对于16位硬件寄存器, memcpy()会导致一些问题。如果我尝试使用两个8位访问访问16位硬件寄存器,高位字节读取不正确。

However, for 16-bit hardware registers, memcpy() causes some problems. If I try to access a 16-bit hardware register using two 8-bit accesses, the high-order byte doesn't read correctly.

有没有人遇到这个问题?我是一个高级(C#/ Java / Python / Ruby)的人,它更接近硬件,这是外星人的领土。

Has anyone encountered this issue? I'm a 'high-level' (C#/Java/Python/Ruby) guy that's moving closer to the hardware and this is alien territory.

最好的方法来处理这个?我看到一些信息,具体来说,有点令人困惑的[ here 。这篇文章的作者已经完全相同的问题,但是我讨厌在不完全理解我在做什么的情况下实施一个解决方案。

What's the best way to deal with this? I see some info, specifically, a somewhat confusing [to me] post here. The author of this post has exactly the same issue I do but I hate to implement a solution without fully understanding what I'm doing.

任何可以解决这个问题的光线都是非常感激的。谢谢!

Any light you can shed on this issue is much appreciated. Thanks!

推荐答案

除了 Eddie说,你通常需要使用一个易失性指针来读取一个硬件寄存器(假设一个内存映射寄存器,这不是所有的系统,但这听起来对你来说是真实的)。如下所示:

In addition to what Eddie said, you typically need to use a volatile pointer to read a hardware register (assuming a memory mapped register, which is not the case for all systems, but it sounds like is true for yours). Something like:

// using types from stdint.h to ensure particular size values
// most systems that access hardware registers will have typedefs
// for something similar (for 16-bit values might be uint16_t, INT16U,
// or something)

uint16_t volatile* pReg = (int16_t volatile*) 0x1234abcd;  // whatever the reg address is

uint16_t val = *pReg;  // read the 16-bit wide register

这是Dan Saks的一系列文章,应该给你几乎所有你需要知道的一切,以便能够有效地使用C / C ++中的内存映射寄存器:

Here's a series of articles by Dan Saks that should give you pretty much everything you need to know to be able to effectively use memory mapped registers in C/C++:

使用易失性

这篇关于从16位硬件寄存器读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆