1. 前言
U-Boot 是一個主要用于嵌入式系統(tǒng)的引導(dǎo)加載程序,可以支持多種不同的計算機系統(tǒng)結(jié)構(gòu)。
U-Boot的命令為用戶提供了交互功能,并且已經(jīng)實現(xiàn)了幾十個常用的命令,前面兩篇文章介紹了uboot自帶的常用命令使用。
如果開發(fā)板需要很特殊的操作,可以添加新的U-Boot命令。U-Boot的每一個命令都是通過U_Boot_CMD宏定義的。
這個宏在頭文件中定義。
#define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \
?
cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage}
復(fù)制代碼
參數(shù)介紹:
name:命令的名字,他不是一個字符串,不能用雙引號括起來
maxargs:最大的參數(shù)個數(shù)
command:對應(yīng)的函數(shù)指針
usage:一個字符串,簡短的使用說明
help:一個字符串,比較詳細(xì)的使用說明
UBOOT命令文件命名規(guī)則:cmd_xxx.c
復(fù)制代碼
將寫好的命令.c文件放入UBOOT源碼頂層的/common目錄下,并且修改Makefile文件,將加入的.c文件添加到編譯選項中。
在Makefile文件大約50行出進行添加即可:
格式:COBJS-y += cmd_xxx.o
復(fù)制代碼
添加完畢,回到uboot頂層目錄下,重新生成u-boot.bin文件,再下載到開發(fā)板測試。
2. 自定義UBOOT命令代碼
2.1 編寫蜂鳴器控制命令
#include
#include
#define GPD0CON (*(volatile unsigned int *)0x114000A0) //定義蜂鳴器IO口的地址
#define GPD0DAT (*(volatile unsigned int *)0x114000A4)
?
int do_beep( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
GPD0CON&=~(0xf<<0);
? GPD0CON|=(0x1<<0);
?
? if(!strcmp(argv[1],"on")) ?//strcmp是比較字符串的函數(shù),如果傳進來的是on,就打開蜂鳴器
? {
? GPD0DAT|=(1<<0);
? }
? if(!strcmp(argv[1],"off"))//strcmp是比較字符串的函數(shù),如果傳進來的是off,就關(guān)閉蜂鳴器
? {
? GPD0DAT&=~(1<<0);
? }
? else
? printf("Usage:beep !\n"); //如果不是on 也不是off 就輸出提示
?}
??
??
?U_BOOT_CMD(
? beep, ? ?//在u-boot命令行里顯示的命令名稱
? 2, ? ? ? //形參最大個數(shù)
? 1, ? ? ? //重復(fù)次數(shù) (按下回車--自動執(zhí)行上一次命令)
? do_beep, //命令執(zhí)行函數(shù)(回調(diào)函數(shù)--)
? "傳參格式: beep ", ? //用法提示
? "傳承示例:beep on 或者 beep off......." ?//幫助命令的提示信息
?);
復(fù)制代碼|off>|off>
2.2 編寫LED燈控制命令
#include
#include
?
/* 1、LED燈接口配置寄存器 */
#define GPM4CON (*(volatile unsigned int *)0x110002E0)
#define GPM4DAT (*(volatile unsigned int *)0x110002E4)
?
int do_led(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
// led 1 on
// led 1 off
GPM4CON &= ~(0xf << 0 * 4); ? ?//清除寄存器1
? ?GPM4CON |= (1 ? << 0 * 4); ? ?//輸出模式
? ?
? ?GPM4CON &= ~(0xf << 1 * 4); ? ?//清除寄存器2
? ?GPM4CON |= (1 ? << 1 * 4); ? ?//輸出模式
? ?
? ?GPM4CON &= ~(0xf << 2 * 4); ? ?//清除寄存器3
? ?GPM4CON |= (1 ? << 2 * 4); ? ?//輸出模式
? ?
? ?GPM4CON &= ~(0xf << 3 * 4); ? ?//清除寄存器4
? ?GPM4CON |= (1 ? << 3 * 4); ? ?//輸出模式
? ?
? ?
? ?/*第一盞燈*/
? if(!strcmp(argv[1],"1")) ?//strcmp是比較字符串的函數(shù),如果傳進來的是on,就打開蜂鳴器
? {
? if(!strcmp(argv[2],"on"))
? {
? GPM4DAT &= ~(1 << 0); ? ? ?//點亮第一個燈
? }
? else if(!strcmp(argv[2],"off"))
? {
? GPM4DAT |=1 << 0; ? ? ? ? ?//關(guān)閉第一個燈
? }
? }
?
? /*第二盞燈*/
? else if(!strcmp(argv[1],"2")) ?//strcmp是比較字符串的函數(shù),如果傳進來的是on,就打開蜂鳴器
? {
? if(!strcmp(argv[2],"on"))
? {
? GPM4DAT &= ~(1 << 1); ? ? ?//點亮第二個燈
? }
? else if(!strcmp(argv[2],"off"))
? {
? GPM4DAT |=1 << 1; ? ? ? ? ?//關(guān)閉第二個燈
? }
? }
?
? /*第三盞燈*/
? else if(!strcmp(argv[1],"3")) ?//strcmp是比較字符串的函數(shù),如果傳進來的是on,就打開蜂鳴器
? {
? if(!strcmp(argv[2],"on"))
? {
? GPM4DAT &= ~(1 << 2); ? ? ?//點亮第三個燈
? }
? else if(!strcmp(argv[2],"off"))
? {
? GPM4DAT |=1 << 2; ? ? ? ? ?//關(guān)閉第三個燈
? }
? }
?
? /*第四盞燈*/
? else if(!strcmp(argv[1],"4")) ?//strcmp是比較字符串的函數(shù),如果傳進來的是on,就打開蜂鳴器
? {
? if(!strcmp(argv[2],"on"))
? {
? GPM4DAT &= ~(1 << 3); ? ? ?//點亮第四個燈
? }
? else if(!strcmp(argv[2],"off"))
? {
? GPM4DAT |=1 << 3; ? ? ? ? ?//關(guān)閉第四個燈
? }
? }
??
? else
? printf("Usage:led <1~4> \n"); //如果不是on 也不是off 就輸出提示
}
?
?
U_BOOT_CMD(
led, //在u-boot命令行里顯示的命令名稱
3, //形參最大個數(shù)
1, //重復(fù)次數(shù)
do_led, //命令執(zhí)行函數(shù)
"user: LED count ", //用法提示
"cmd : (1)led 1 on (2)led 1 off...." //幫助命令的提示信息
);
復(fù)制代碼|off>|off>
2.3 設(shè)計自己的movi命令
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
?
int do_mymovi(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
{
int r_cnt,w_cnt;
/*1. 查找設(shè)備0-SD卡*/
struct mmc *mmc0 = find_mmc_device(0);
if(mmc0==NULL)
{
printf("設(shè)備0查找失敗!\n");
return 0;
}
/*2. 查找設(shè)備1--MMC*/
struct mmc *mmc1 = find_mmc_device(1);
if(mmc1==NULL)
{
printf("設(shè)備1查找失敗!\n");
return 0;
}
/*3. 初始化SD卡*/
mmc_init(mmc0); /*設(shè)備0初始化--SD卡*/
/*4. 初始化EMMC*/
mmc_init(mmc1); /*設(shè)備1初始化--EMMC卡*/
emmc_boot_open(mmc1); /*設(shè)備1打開---EMMC*/
/*5. 燒寫數(shù)據(jù)*/
/*5.1 BL1*/
r_cnt=movi_read(0,1,16,(void*)0x40008000); //讀出SD卡里存放到所有數(shù)據(jù)到DDR指定地址
w_cnt=movi_write(1,0,16,(void*)0x40008000);//將讀出的數(shù)據(jù)寫入到EMMC
printf("BL1_r_cnt=%d\n",r_cnt);
printf("BL1_w_cnt=%d\n",w_cnt);
/*5.2 BL2*/
r_cnt=movi_read(0,17,32,(void*)0x40008000); //讀出SD卡里存放到所有數(shù)據(jù)到DDR指定地址
w_cnt=movi_write(1,16,32,(void*)0x40008000);//將讀出的數(shù)據(jù)寫入到EMMC
printf("BL2_r_cnt=%d\n",r_cnt);
printf("BL2_w_cnt=%d\n",w_cnt);
?
/*5.3 UBOOT\這里最好使用malloc申請空間,太大的地址可能會被其他數(shù)據(jù)覆蓋掉*/
r_cnt=movi_read(0,49,656,(void*)0x40008000); //讀出SD卡里存放到所有數(shù)據(jù)到DDR指定地址
w_cnt=movi_write(1,48,656,(void*)0x40008000);//將讀出的數(shù)據(jù)寫入到EMMC
printf("UBOOT_r_cnt=%d\n",r_cnt);
printf("UBOOT_w_cnt=%d\n",w_cnt);
/*5.4 TZSW*/
r_cnt=movi_read(0,705,320,(void*)0x40008000); //讀出SD卡里存放到所有數(shù)據(jù)到DDR指定地址
w_cnt=movi_write(1,704,320,(void*)0x40008000);//將讀出的數(shù)據(jù)寫入到EMMC
printf("TZSW_r_cnt=%d\n",r_cnt);
printf("TZSW_w_cnt=%d\n",w_cnt);
/*5.5 Linux內(nèi)核*/
r_cnt=movi_read(0,1057,12288,(void*)0x40008000); //讀出SD卡里存放到所有數(shù)據(jù)到DDR指定地址
w_cnt=movi_write(1,1057,12288,(void*)0x40008000);//將讀出的數(shù)據(jù)寫入到EMMC
printf("Linux內(nèi)核_r_cnt=%d\n",r_cnt);
printf("Linux內(nèi)核_w_cnt=%d\n",w_cnt);
emmc_boot_close(mmc1); //關(guān)閉EMMC
/*5.5 環(huán)境變量*/
r_cnt=movi_read(0,1025,32,(void*)0x40008000); //讀出SD卡里存放到所有數(shù)據(jù)到DDR指定地址
w_cnt=movi_write(1,1025,32,(void*)0x40008000);//將讀出的數(shù)據(jù)寫入到EMMC
printf("環(huán)境變量_r_cnt=%d\n",r_cnt);
printf("環(huán)境變量_w_cnt=%d\n",w_cnt);
printf("環(huán)境變量拷貝成功!\n");
return 0;
}
?
U_BOOT_CMD(
mymovi, /*命令的名稱*/
1, /*形參的最大個數(shù)*/
0, /*命令執(zhí)行重復(fù)次數(shù)*/
do_mymovi,/*命令處理函數(shù)*/
"將SD卡的BL1/BL2/uboot/簽名文件/內(nèi)核拷貝到EMMC", /*簡短提示*/
"\n"
"將SD卡的BL1/BL2/uboot/簽名文件/內(nèi)核拷貝到EMMC\n" /*完整提示*/
"注意: 該命令在開發(fā)板以SD卡啟動方式時運用\n"
);
復(fù)制代碼
2.4 設(shè)計環(huán)境變量拷貝命令
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
?
/*
//以MMC方式啟動,運行下面命令即可完成環(huán)境變量拷貝(SD-->EMMC)
mmc read 1 40000000 401 20
mmc write 0 40000000 401 20
?
//以SD方式啟動,運行下面命令即可完成環(huán)境變量拷貝 (SD--->EMMC)
mmc read 0 40000000 401 20
mmc write 1 40000000 401 20
*/
int do_copyenv(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
{
int r_cnt,w_cnt;
mmc_init(find_mmc_device(0)); /*設(shè)備0初始化--SD卡*/
mmc_init(find_mmc_device(1)); /*設(shè)備1初始化--EMMC卡*/
?
/*5.5 環(huán)境變量*/
r_cnt=movi_read(0,1025,32,(void*)0x40000000); //讀出SD卡里存放到所有數(shù)據(jù)到DDR指定地址
w_cnt=movi_write(1,1025,32,(void*)0x40000000);//將讀出的數(shù)據(jù)寫入到EMMC
printf("環(huán)境變量_r_cnt=%d\n",r_cnt);
printf("環(huán)境變量_w_cnt=%d\n",w_cnt);
printf("環(huán)境變量拷貝成功!\n");
return 0;
}
?
?
U_BOOT_CMD(
copyenv, /*命令的名稱*/
1, /*形參的最大個數(shù)*/
0, /*命令執(zhí)行重復(fù)次數(shù)*/
do_copyenv,/*命令處理函數(shù)*/
"將SD卡的環(huán)境變量拷貝到EMMC", /*簡短提示*/
"\n"
"將SD卡的環(huán)境變量拷貝到EMMC\n" /*完整提示*/
"注意: 該命令在開發(fā)板以SD卡啟動方式時運用\n"
);
審核編輯:湯梓紅
-
嵌入式
+關(guān)注
關(guān)注
5092文章
19177瀏覽量
307686 -
Linux
+關(guān)注
關(guān)注
87文章
11345瀏覽量
210398 -
Uboot
+關(guān)注
關(guān)注
4文章
125瀏覽量
28350
發(fā)布評論請先 登錄
相關(guān)推薦
嵌入式Linux學(xué)習(xí)引導(dǎo)-Uboot移植之添加自定義命令
嵌入式開發(fā)中自定義協(xié)議的解析與組包相關(guān)案例分享
Zybo全棧開發(fā)入門教程(基于Linux嵌入式系統(tǒng)):10個步驟自定義IP模塊
![Zybo全棧<b class='flag-5'>開發(fā)</b>入門教程(基于<b class='flag-5'>Linux</b><b class='flag-5'>嵌入式</b>系統(tǒng)):10個步驟<b class='flag-5'>自定義</b>IP模塊](https://file1.elecfans.com//web2/M00/A6/A5/wKgZomUMP2GAMcqiAAAZdC9tTOc516.jpg)
AM335x如何在Uboot時增加自定義的命令控制LCD功能中文概述
![AM335x如何在<b class='flag-5'>Uboot</b>時增加<b class='flag-5'>自定義</b>的<b class='flag-5'>命令</b>控制LCD功能中文概述](https://file.elecfans.com/web1/M00/4F/84/o4YBAFrdo4SAVAIjAAEOT0o6VL4748.png)
英創(chuàng)信息技術(shù)嵌入式Linux工控主板uboot命令簡介
![英創(chuàng)信息技術(shù)<b class='flag-5'>嵌入式</b><b class='flag-5'>Linux</b>工控主板<b class='flag-5'>uboot</b><b class='flag-5'>命令</b>簡介](https://file.elecfans.com/web1/M00/B3/9E/pIYBAF4eqpmAVbtZAABQjAyBhiE777.png)
基于嵌入式中央處理單元(CPU)的自定義指令
【嵌入式】構(gòu)建嵌入式Linux系統(tǒng)(uboot、內(nèi)核、文件系統(tǒng))
![【<b class='flag-5'>嵌入式</b>】構(gòu)建<b class='flag-5'>嵌入式</b><b class='flag-5'>Linux</b>系統(tǒng)(<b class='flag-5'>uboot</b>、內(nèi)核、文件系統(tǒng))](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
嵌入式Linux開發(fā)教程:Linux常見命令(上篇)
![<b class='flag-5'>嵌入式</b><b class='flag-5'>Linux</b><b class='flag-5'>開發(fā)</b>教程:<b class='flag-5'>Linux</b>常見<b class='flag-5'>命令</b>(上篇)](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
嵌入式linux開發(fā)的學(xué)習(xí)之路
![<b class='flag-5'>嵌入式</b><b class='flag-5'>linux</b><b class='flag-5'>開發(fā)</b>的學(xué)習(xí)之路](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
嵌入式linux學(xué)習(xí) Day1 uboot基礎(chǔ)
![<b class='flag-5'>嵌入式</b><b class='flag-5'>linux</b>學(xué)習(xí) Day1 <b class='flag-5'>uboot</b>基礎(chǔ)](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
嵌入式開發(fā)中自定義協(xié)議的解析與組包
![<b class='flag-5'>嵌入式開發(fā)</b>中<b class='flag-5'>自定義</b>協(xié)議的解析與組包](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
評論