简体   繁体   中英

Access Memory Mapped I/O

I am very new to embedded system programming, I just need to learn how to manipulate the given via c++ code.

Given:

Motor 1 is mapped to 0x60000000

Motor 2 is mapped to 0x50000000

the following are the definitions of current 32-bit registers

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

I find it hard to see a sample c++ code using the given, At this point what i know is I need to access the register_name and set their bits to perform specific task or read the register name to get the status for example.

I think I can understand it more if it is used in a c++ code. The given is a automatic door system ( i didnt write the button details). Will I need to access the register_name or the byte_offset in c++?

Your help would be very much appreciated

C/C++ example to read interrupt/status registers:

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];

Similarly to write 32 bit values to to the command registers:

motor1[motor_command] = 0x8000 | (0x12 << 6) | 0x01;
motor2[motor_command] = 0x0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM