访问内存映射I/O
Access Memory Mapped I/O
我对嵌入式系统编程很陌生,我只需要学习如何通过c++代码操纵给定的。
给定:
电机1映射到0x60000000
电机2映射到0x50000000
以下是当前32位寄存器的定义REGISTER NAME | BYTE OFFSET | NOTES
----------------------------------------------------------------------
motor_interrupt 0x00 service interrupts
motor_status 0x01 enable on demand access to status elements
motor_command 0x02 enable command of the motor
REGISTER NAME | NAME | BITS | ACCESS TYPE | DESC
----------------------------------------------------------------------------------
motor_interrupt_register
CLOSED 0 R/W high when motor transitions to CLOSED position
OPEN 1 R/W high when motor transitions to OPEN position
RESERVED 2.31 N/A reserved for future use
motor_status
SPEED 0.2 R speed in counts/seconds
STATE 3 R current state of motor
POSITION 4.13 R current position of the motor
RESERVED 14.31 n/a reserved for future use
我发现很难看到使用给定的示例c++代码,此时我所知道的是我需要访问register_name并设置它们的位来执行特定的任务或读取寄存器名以获得状态。
我想如果在c++代码中使用,我可以更好地理解它。给定的是一个自动门系统(我没有写按钮的细节)。我需要在c++中访问register_name或byte_offset吗?
非常感谢您的帮助
C/c++读中断/状态寄存器的例子:
volatile uint32_t * const motor1 = (uint32_t *)0x60000000; // base addresses for motors 1 and 2
volatile uint32_t * const motor2 = (uint32_t *)0x50000000;
enum // register offsets from base address
{
motor_interrupt, // 0x00 - service interrupts
motor_status, // 0x01 - enable on demand access to status elements
motor_command // 0x02 - enable command of the motor
}
// read status/interrupt registers
uint32_t current_int_1 = motor1[motor_interrupt];
uint32_t current_int_2 = motor2[motor_interrupt];
uint32_t current_status_1 = motor1[motor_status];
uint32_t current_status_2 = motor2[motor_status];
类似地将32位值写入命令寄存器:
motor1[motor_command] = 0x8000 | (0x12 << 6) | 0x01;
motor2[motor_command] = 0x0;
相关文章:
- 添加静态constexpr成员是否会更改结构/类的内存映射
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- C/C++ - 查询平台相关的换行符(用于内存映射文件)
- 字符串共享内存映射的向量
- 准确了解对象在内存中的映射方式
- 将布局映射到内存地址
- 确保内存映射页位于内存中
- <char> 使用 Vulkan 映射内存时如何使用 std::vector 而不是 void**?
- 我可以将新的 std::tuple 放入内存映射区域,并在以后读回吗?
- C++从 Java 角度设置和映射内存管理
- 如何在 OpenCL 中使用缓冲区分配和映射内存机制
- 两个进程之间的共享映射内存在编辑时未更新
- 频繁访问文件映射内存
- 由于动态数组而导致的哈希映射内存泄漏
- 映射和多映射:内存使用/管理差异
- 在自定义硬件上映射内存
- 内存映射内存是否可能
- 具有动态分配内存和映射内存的Shared_ptr
- mmap小尺寸内存,当munmap为glibc时,会将映射内存释放到操作系统
- C++ 标准::映射内存管理