u-boot-2014.10 4th day----Bare Metal buzzer Program

Source: Internet
Author: User

Hardware platform: tq2440

Development environment: Ubuntu-3.11

U-boot version: 2014.10

This article allows reprint, please specify the source: Http://blog.csdn.net/fulinus


First, the buzzer ASM Assembler Program:

/*********************************************************************** * File:beep.  S * version:1.0.0 * copyright:2014 (c) fulinux <[email protected]> * description:this ASM code used To turn beep on/off TQ2440 board ***********************************************************************/#define    Gpbcon 0x56000010#define gpbdat 0x56000014#define gpbup 0x56000018#define DELAY 0X40000000. Text. Align 2    . Global _start_start:/*set gpb5,gpb6,gpb7,gpb8 as GPIO OUTPUT mode*/ldr r0, =gpbcon ldr R1, [R0] BIC R1, R1, #0x03/*set Gpbcon for GPB0 as XX */Orr R1, R1, #0x01/*set Gpbcon for GPB0 as Gpioout  , 0x01*/str R1, [r0]/*set internal pullup resister*/ldr R0, =gpbup ldr R1, [R0] @orr R1, R1, #0x01/*set bit 0, disable Pullup resister*/bic r1, R1, #0x01/*clear bit 0, enable Pullup resister*   /STR R1, [r0]loop:/*turn off beep*/LDR  R2, =gpbdat LDR R3, [R2] Orr R3, R3, #1/*set bit 0 as high level*/str R3, [R2] Ldr           R0, =delay/*sleep for a while*/bl DELAY/*turn on beep*/LDR R3, [R2] BIC R3, R3, #1      /*set bit 0 as high level*/str R3, [R2] Ldr r0, =delay/*sleep for a while*/bl    Delay B Loop/*loop running*/delay:sub r0, R0, #1 cmp r0, #0x0 bne delay mov pc, LR

The program is mainly based on yesterday led. s program has been modified. The makefile file, like yesterday's file, just changed the name of the executable:

Biname = beep

II. Arm Assembly and C language integration

Jumping from ASM to C functions is also an important technical point, and we do the following work in the ARM assembler program

1, close the watchdog;

2, shielding interruption;

3, jump to the C code in the main function to go.

1. ARM Assembler Program

/*********************************************************************** * File:start. S * version:1.0.0 * copyright:2011 (c) Guo wenxue <[email protected]> * description:this ASM used T o Disable watch dog and interrupt, then call C code to * turn the buzzer on/off FL2440 board. * Changelog:1, Release initial version on "Mon Mar 21:09:52 CST 2011" * ******************************************* /#define PWTCON 0x53000000/* Watch Dog Register address */#define INTMSK 0x4a000008/ * Interupt-controller Base addresses */#define INTSUBMSK 0x4a00001c. Text. Align 2. Global _start_start:/* Di     Sable Watch Dog */Ldr r0, =pwtcon/*save pwtcon address in r0*/mov r1, #0x0/*set r1=0x0*/str R1, [R0]    /*move the data in R1 to the address specify by r0*//* Mask all IRQs by setting all bits in the intmr-default */ mov r1, #0xffffffff ldr r0, =intmsk str R1, [R0] LDr R0, =intsubmsk Ldr R1, =0x7fff/*there is-bits used in Intsubmsk on s3c2440*/str R1, [r0] BL Mainhalt_ Loop:b Halt_loop

2. C Language Program

/*********************************************************************** * FILE:BEEP.C * version:1.0.0 * copyright:2011 (c) Guo Wenxue <[email protected]> * description:this C code used to turn buzzer on/off F L2440 Board * changelog:1, Release initial version on "Mon Mar 21:09:52 CST 2011" * ******************************* /#define GPBCON (* (unsigned long volatile *) 0x56000010) #define GPBDAT (* (Unsi  gned long Volatile *) 0x56000014) #define GPBUP (* (unsigned long volatile *) 0x56000018) #define BEEP 0/*buzzer            US GPB0 */#define delay_time 40000000static inline void DELAY (unsigned long loops) {__asm__ volatile ("1:\n" "Subs%0,%1, #1 \ n" "" Bne 1b ":" =r "(Loops):" 0 "(loops));}               int main (void) {Gpbcon = (gpbcon|0x3) &0x1;/* Set GPB0 as GPIO output mode (0x01) */Gpbup &= to;    /* Enable pullup resister */Gpbdat |= 0x560;       while (1) { Gpbdat &= ~ (1<<beep);        /* Set Beep GPIO as Low level */delay (delay_time);     Gpbdat |= 1<<beep;    /* Set Beep GPIO as High level */delay (delay_time); }}

Obviously, we need to execute the ARM assembler first, and then the assembler jumps to the C program, but the compiler does not know whether to put the C code snippet or the arm assembler code snippet first, so we need to tell the compiler how to compile and connect. There is an LDS file in which the contents of the file determine the location of each code snippet in the program, and you can see how the file is passed to the compiler in the following makefile file.

3. lds file

/*********************************************************************** *        File:  beep.lds *     Version:  1.0.0 *   Copyright:  (c) Guo Wenxue <[email protected]> * Description: Cross  tool link text, refer to U-boot.lds *   ChangeLog:  1, Release initial version on "Mon Mar 21:09:52 CST 2011" *************************************** /                             Output_format ("Elf32-littlearm", "Elf32-littlearm", "Elf32-littlearm") Output_arch (ARM) ENTRY (_start) sections{    . = 0x33000000;    . Text: {        start.o (. text*)/* by Fulinux Modified */        * (. text*)                            * (. rodata)    }    . Data ALIGN (4): { c17/>* (. Data)    }    . BSS ALIGN (4): {        * (. BSS)    }


4. Makefile File
# ***********************************************************************# * file:makefile# * version:1.0.0 # * COPYRIGHT:2011 (c) Guo wenxue <[email protected]># * Description:makefile used to cross compile the A SM and C Source code# * changelog:1, Release initial version on "Mon Mar 21:09:52 CST 2011" # *# ******************* Biname = Beeptextbase = 0x33000000cross =/opt/buildroot-2012.08  /ARM920T/USR/BIN/ARM-LINUX-CC = $ (cross) GCCLD = $ (cross) LdAR = $ (cross) Arobjcopy = $ (cross) Objcopyobjdump = $ (Cross) Objdumpstrip = $ (cross) Stripreadelf = $ (cross) Readelfcflags =-G-O2-WALL-NOSTDINC-NOSTDLIB-FNO-BUILTINAFL  AGS = $ (CFLAGS)-d__assembly__ldscript = ${biname}.ldsldflags =-nostartfiles-t $ (ldscript)-ttext $ (textbase) SRC_C = $ (wildcard *.c) src_s = $ (wildcard *.) S) Obj_c = $ (patsubst%.c,%.o,$ (src_c)) obj_s = $ (patsubst%. s,%.o,$ (src_s)) Obj_all = $ (obj_c) $ (obj_s). Phony: Allall: ${obj_all} ${ld} $ (ldflags)-O ${biname}.elf ${obj_all} ${objcopy}-o binary-s ${biname}.elf ${BINAME}.bin Rm-f *.elf *.o%.o:%. S $ (CC) $ (aflags)-c-o [email protected] $&LT;%.O:%.C $ (CC) $ (CFLAGS)-c-o [email prote CTED] $&LT;INSTALL:CP ${biname}.bin ~/winxp-f--reply=yesclean:rm-f *.elf *.o rm-f ${biname}.bin

In the file

Ldscript = ${biname}.lds

That is the Beep.lds file.

5. Compile and run

After compiling, the Beep.bin program was burned to the SDRAM by J-link to execute:

H speed  12000  loadbin D:\kupan\temp\beep.bin 0x33000000  setpc 0x33000000  g

You can hear the sounds of nature!



Third, led marquee arm Assembly and C language program LED marquee C program is as follows:
/*********************************************************************** * FILE:LED.C * version:1.0.0 * C opyright:2011 (c) Guo Wenxue <[email protected]> * description:this C code used to turn LED0~LED4 on FL2 Board * changelog:1, Release initial version on "Mon Mar 21:09:52 CST" * Modify: [email protect Ed] ***********************************************************************/#define GPBCON (* (unsigned long volatile *) 0x56000010) #define GPBDAT (* (unsigned long volatile *) 0x56000014) #define GPBUP (* (Unsigned long volatile *  ) 0x56000018) #define LED0 5/*led0 use gpb5*/#define LED1 6/*led1 use gpb6*/#define LED2 7/*led2 use     gpb7*/#define LED3 8/*led3 use gpb8*/#define delay_time 40000000static inline void DELAY (unsigned long loops) { __asm__ volatile ("1:\n" "Subs%0,%1, #1 \ n" "Bne 1b": "=r" (Loops): "0" (loops));} void Led_init (void) {/* Set Gpb5,gpb6,gpb7,gpb8 as GPIO Mode (0x01) */Gpbcon = (Gpbcon & ~0x3fc00) |    0x15400;    Gpbup &= ~0x01e0; /* Set Gpb5,gpb6,gpb7,gpb8 as high level, to turn led0,led1,led2,led3 off */Gpbdat |= 0x01e0;}    void Led_off (void) {/* Set Gpb5,gpb6,gpb7,gpb8 as high level, to turn led0,led1,led2,led3 off */Gpbdat |= 0x01e0; Delay (delay_time);}    void led_on (int led) {/* Turn LED0 on */Gpbdat &= ~ (1<<led); Delay (delay_time);}    int main (void) {led_init ();        while (1) {Led_off ();        LED_ON (LED0);        LED_ON (LED1);        LED_ON (LED2);    LED_ON (LED3); }}

Other parts of the document are identical, slightly modified.



u-boot-2014.10 4th day----Bare Metal buzzer Program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.