regnar86
Posts: 2
Joined: Thu Jun 26, 2014 12:51 am

Assembly Language w/ Gertboard

Thu Jun 26, 2014 12:56 am

I'm developing a control/navigation program for an autonomous vehicle using a LIDAR. I am going to be using the Gertboard as it has many features and options. I've been reading a great book on Assembly Language in Raspian. However, there is no examples and information on how to control the Gertboard through assembly language. I was hoping someone could point me in the right direction on some documentation and most importantly some instruction on how to achieve this. Thanks for any assistance you can lend me.

mimi123
Posts: 583
Joined: Thu Aug 22, 2013 3:32 pm

Re: Assembly Language w/ Gertboard

Thu Jun 26, 2014 8:22 am

You can manage the GPIOs as you're in bare metal if you mmap() the HW Registers.
Anyway, the main() function should always be written in C(for mmap() memory, ...).

allexoll
Posts: 43
Joined: Sat Feb 23, 2013 12:27 pm

Re: Assembly Language w/ Gertboard

Thu Jun 26, 2014 11:11 am

mimi123 wrote:Anyway, the main() function should always be written in C(for mmap() memory, ...).
correct me if I'm wrong, but you can totally write your main function in assembly. random example would be the baking Pi tutorial : http://www.cl.cam.ac.uk/projects/raspbe ... orials/os/ but to get back to regnar86 question, you will have to use the GPIO pins, and maybe a few peripherals such as I2C, SPI, Uart...
The documentation for thoses are in the peripherals datasheet: http://www.raspberrypi.org/wp-content/u ... herals.pdf

Allexoll

dwelch67
Posts: 955
Joined: Sat May 26, 2012 5:32 pm

Re: Assembly Language w/ Gertboard

Thu Jun 26, 2014 2:48 pm

Let's back up, this is a bare metal forum...

You want to use assembly language that is fine anything you can do in C you can do in asm. Now do you want to run this on linux or bare metal, because that makes a difference.

regnar86
Posts: 2
Joined: Thu Jun 26, 2014 12:51 am

Re: Assembly Language w/ Gertboard

Thu Jun 26, 2014 3:06 pm

I will be running my assembly program on the Linux distro "Raspbian". I've never heard the term bare metal before so I don't understand what you are saying. Is this a technical term? Are you referring to assembly language as "bare metal" or are you referring to it as "machine language" or something entirely different.

allexoll
Posts: 43
Joined: Sat Feb 23, 2013 12:27 pm

Re: Assembly Language w/ Gertboard

Thu Jun 26, 2014 3:32 pm

This sub-forum is concerning bare metal, meaning we don't use an OS. It allows us to do things you could not do with an OS, like real time execution, use every peripheral like we want, have more control over memory, optimizing what we want, and more. We use the processor on the board like a micro controller. Anybody correct me if I said something wrong.
You might find what you're looking for about asm in the "Other languages" sub-forum: http://www.raspberrypi.org/forums/viewforum.php?f=34

mimi123
Posts: 583
Joined: Thu Aug 22, 2013 3:32 pm

Re: Assembly Language w/ Gertboard

Fri Jun 27, 2014 6:52 am

We threat the Acorn RISC Machine as a microcontroller, but the VPU is still running ThreadX. True bare metal, at least for me, should be in bootcode.bin.

User avatar
rpdom
Posts: 14997
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Assembly Language w/ Gertboard

Fri Jun 27, 2014 7:22 am

mimi123 wrote:We threat the Acorn RISC Machine as a microcontroller
Acorn? Not unless you are stuck in a time warp ;)

User avatar
DexOS
Posts: 876
Joined: Wed May 16, 2012 6:32 pm
Contact: Website

Re: Assembly Language w/ Gertboard

Fri Jun 27, 2014 1:29 pm

Uuder linux you would need to do something like this

Code: Select all

;-------------------------------------------------------
; By Dex (Craig Bamford)
;
;  (c)Craig Bamford  01/22/2013
;-------------------------------------------------------
format ELF executable                                  ;
    entry start                                        ; tell linker entry point
 BLOCK_SIZE          =  1024*4                         ;
 O_RDWR              =  2                              ;
                                                       ;
 include 'SysCalls\SysCall.inc'                        ;
 include 'R_PI_Address\R_PI_Address.inc'               ;
 include 'ArmMacro\ArmMacro.inc'                       ;
 include 'Macro\Macro.inc'                             ;
                                                       ;
 segment readable writeable executable                 ;
                                                       ;
code32                                                 ; we want to use 32 bits
align 4                                                ;
start:                                                 ;
;======================================================;
; Open /dev/mem (file)                                 ;
;======================================================;
        load_reg  r0, MemFileName                      ; MemFileName
        load_reg  r1, O_RDWR                           ; O_RDWR 0x1002
        load_reg  r2, 0                                ;
        bl        Open                                 ; call function
        teq       r0, 0                                ; check for errors
        bpl       @f                                   ; if positive branch
;======================================================;
; Print Open exit Error! message                       ;
;======================================================;
        mvn       r0, r0                               ; convert error code
        add       r0, 1                                ;
        bl        Convert2Hex                          ; call convert to hex function
                                                       ;
        load_reg  r1, msgEF                            ; address
        load_reg  r2, msg_sizeEF                       ; size
        bl        PrintString                          ; call print string function
                                                       ;
        load_reg  r1, print_hex_string                 ; address
        load_reg  r2, print_hex_string_size            ; size
        bl        PrintString                          ; call printstring function
                                                       ;
;======================================================;
; Exit                                                 ;
;======================================================;
        mov       r0, 0x0                              ; status -> 0
        mov       r7, sys_exit                         ; exit is syscall 1
        swi       0x0                                  ; invoke syscall
;======================================================;
;    **************** Exit Error *****************     ;
;======================================================;
align 4                                                ;
@@:                                                    ;
;======================================================;
; Mmap  GPIO                                           ;
;======================================================;
        load_reg  r1, Fd                               ;
        str       r0, [r1]                             ;
        eor       r0, r0                               ; the system will choose the address
        load_reg  r1, BLOCK_SIZE                       ; the length of the mapped area
        mov       r2, 3                                ; PROT_READ | PROT_WRITE, read + write
        mov       r3, 1                                ; MAP_SHARED - sharing mode
        load_reg  r4, Fd                               ; the descriptor of the memory file
        ldr       r4, [r4]                             ; from sys_open in the previous step
        load_reg  r5, GPIO_BASE                        ; starting file offset
        mov       r5, r5, lsr 12                       ; This is needed, to make address multiy of page size
        bl        Mmap2                                ; call function
        teq       r0, 0                                ; check for errors
        bpl       @f                                   ; if positive branch
;======================================================;
; Print Mmap Gpio Error! Message                       ;
;======================================================;
        mvn       r0, r0                               ;
        add       r0, 1                                ;
        bl        Convert2Hex                          ; call convert to hex function
                                                       ;
        load_reg  r1, msgEMGpio                        ; address
        load_reg  r2, msg_sizeEMGpio                   ; size
        bl        PrintString                          ; call print function
                                                       ;
        load_reg  r1, print_hex_string                 ; address
        load_reg  r2, print_hex_string_size            ; size
        bl        PrintString                          ; call print function
                                                       ;
;======================================================;
; Close  /dev/mem (file)                               ;
;======================================================;
        load_reg  r0, Fd                               ;
        ldr       r0, [r0]                             ; file discriptor
        bl        Close                                ;
;======================================================;
; Exit                                                 ;
;======================================================;
        mov       r0, 0x0                              ; status -> 0
        mov       r7, sys_exit                         ; exit is syscall 1
        swi       0x0                                  ; invoke syscall
;======================================================;
;    **************** Exit Error *****************     ;
;======================================================;
;                                                      ;
;======================================================;
; Store GPIO base address                              ;
;======================================================;
align 4                                                ;
@@:                                                    ;
        load_reg  r1, GpioBase                         ;
        str       r0, [r1]                             ; save pointer to the mapped area
;======================================================;
; Mmap  Arm Timer                                      ;
;======================================================;
        eor       r0, r0                               ; the system will choose the address
        load_reg  r1, BLOCK_SIZE                       ; the length of the mapped area
        mov       r2, 3                                ; PROT_READ | PROT_WRITE, read + write
        mov       r3, 1                                ; MAP_SHARED - sharing mode
        load_reg  r4, Fd                               ; the descriptor of the memory file
        ldr       r4, [r4]                             ; from sys_open in the previous step
        load_reg  r5, ARM_TIMER_BASE                   ; starting file offset
        mov       r5, r5, lsr 12                       ; This is needed, to make address multiy of page size
        bl        Mmap2                                ; call function
        teq       r0, 0                                ; check for errors
        bpl       @f                                   ; if positive branch
;======================================================;
; Print ARM_TIMER Mmap Error! Message                  ;
;======================================================;
        mvn       r0, r0                               ;
        add       r0, 1                                ;
        bl        Convert2Hex                          ; call convert to hex function
                                                       ;
        load_reg  r1, msgEMArmTimer                    ; address
        load_reg  r2, msg_sizeEMArmTimer               ; size
        bl        PrintString                          ; call print function
                                                       ;
        load_reg  r1, print_hex_string                 ; address
        load_reg  r2, print_hex_string_size            ; size
        bl        PrintString                          ; call print function
                                                       ;
;======================================================;
; Close  /dev/mem (file)                               ;
;======================================================;
        load_reg  r0, Fd                               ;
        ldr       r0, [r0]                             ; file discriptor
        bl        Close                                ;
;======================================================;
; Exit                                                 ;
;======================================================;
        mov       r0, 0x0                              ; status -> 0
        mov       r7, sys_exit                         ; exit is syscall 1
        swi       0x0                                  ; invoke syscall
;======================================================;
;    **************** Exit Error *****************     ;
;======================================================;
;                                                      ;
;======================================================;
; Store  ARM timer base address                        ;
;======================================================;
align 4                                                ;
@@:                                                    ;
        load_reg  r1, ArmTimerBase                     ;
        str       r0, [r1]                             ; save pointer to the mapped area
;======================================================;
;  ************* Add code here **********************  ;
;======================================================;


;======================================================;
; Munmap  Gpio                                         ;
;======================================================;
        load_reg  r0, GpioBase                         ;
        ldr       r0, [r0]                             ; pointer to the mapped area
        load_reg  r1, BLOCK_SIZE                       ; size
        bl        Munmap                               ;
;======================================================;
; Munmap  Arm Timer                                    ;
;======================================================;
        load_reg  r0, ArmTimerBase                     ;
        ldr       r0, [r0]                             ; pointer to the mapped area
        load_reg  r1, BLOCK_SIZE                       ; size
        bl        Munmap                               ;
;======================================================;
; Close  /dev/mem (file)                               ;
;======================================================;
        load_reg  r0, Fd                               ;
        ldr       r0, [r0]                             ; file discriptor
        bl        Close                                ;
;======================================================;
; Print exit message Ok                                ;
;======================================================;
ExitOk:                                                ;
        load_reg  r1, msg                              ; address
        load_reg  r2, msg_size                         ; size
        bl        PrintString                          ; call print string function
;======================================================;
; Exit                                                 ;
;======================================================;
        mov       r0, 0x0                              ; status -> 0
        mov       r7, sys_exit                         ; exit is syscall 1
        swi       0x0                                  ; invoke syscall
;======================================================;
;    ****************   Exit OK  ******************    ;
;======================================================;
;                                                      ;
;======================================================;
; Includes                                             ;
;======================================================;
align 4                                                ;
GpioBase            dw  0                              ; GpioBase mmap address
ArmTimerBase        dw  0                              ; Arm Timer base mmap address
PwmBase             dw  0                              ; Pwm base mmap address
ClockBase           dw  0                              ; Clock Base mmap address
include 'GPIO\GPIO.inc'                                ;
include 'SysCalls\SysFunctions.inc'                    ;
include 'Functions\Functions.inc'                      ;
segment readable writeable                             ;
align 4                                                ;
;======================================================;
; Data                                                 ;
;======================================================;
Fd:                 dw  0                              ; FD
;======================================================;
; Strings                                              ;
;======================================================;
align 4                                                ;
MemFileName:      db "/dev/mem",0                      ;
align 4                                                ;
msg               db "Exit OK :)",0xa                  ;
msg_size  = $-msg                                      ;
align 4                                                ;
msgEMGpio         db " Gpio Mmap error  :(", 0xa       ;
                  db "Error number = 0x"               ;
msg_sizeEMGpio = $-msgEMGpio                           ;
align 4                                                ;
msgEMArmTimer     db " Arm Timer Mmap error  :(", 0xa  ;
                  db "Error number = 0x"               ;
msg_sizeEMArmTimer = $-msgEMArmTimer                   ;
align 4                                                ;
msgEF             db "Can't open /dev/mem  :(", 0xa    ;
                  db "Error number = 0x"               ;
msg_sizeEF = $-msgEF                                   ;
align 4                                                ;
                                                                        
Batteries not included, Some assembly required.

dwelch67
Posts: 955
Joined: Sat May 26, 2012 5:32 pm

Re: Assembly Language w/ Gertboard

Fri Jun 27, 2014 2:06 pm

bare metal meaning not having an operating system and talking to peripherals somewhat directly. Which you can do on linux at times, but I assumed you were on linux so using asm simply means linking with existing libraries. The short answer to your question is do it the same way you would do it in C, just call the libraries.

David

mimi123
Posts: 583
Joined: Thu Aug 22, 2013 3:32 pm

Re: Assembly Language w/ Gertboard

Fri Jun 27, 2014 3:11 pm

What I wanted to say, that in such a case, a C compiler can generate the same assembly as you written that in hand. So it is useless to progran Hand-Optimized assembly on such a config.

Return to “Bare metal, Assembly language”