5、IO控制小程序
嵌入式设备系统一般为了节省空间,一般都会对系统进行裁剪,所以很多有用的命令都会被删除。在嵌入式设备中要调试代码也是比较麻烦的,一般只能看串口打印。现在写了个小程序,专门用来查看和控制海思Hi3520DV300芯片的IO电平状态。
/*=============================================================================
# FileName: Hi3520_IO_CTRL.cpp
# Desc: Hi3520DV300 IO Write and Read
# Author: Caibiao Lee
# Version:
# LastChange: 2018-11-30
# History:
=============================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include "hstGpioAL.h"
int PrintfInputTips(char *ps8Name)
{
printf("=========== error!!! ========\n\n");
printf("usage Write: %s GPIO bit value \n", ps8Name);
printf("usage Read : %s GPIO bit \n", ps8Name);
printf("eg Write 1 to GPIO1_bit02 : %s 1 2 1\n", ps8Name);
printf("eg Read GPIO1_bit02 Value : %s 1 2 \n\n", ps8Name);
printf("=============BT20==================\n")
printf("USB HUB GPIO_0_2 1_UP; 0_Down \n");
printf("RESET_HD GPIO_13_0 0_EN; 1_disEN\n");
printf("Power_HD GPIO_13_3 1_UP; 0_Down \n");
return 0;
}
int main(int argc, char **argv)
{
if((3!=argc)&&(4!=argc))
{
PrintfInputTips(argv[0]);
return -1;
}
unsigned char l_u8GPIONum = 0;
unsigned char l_u8GPIOBit = 0;
unsigned char l_u8SetValue = 0;
GPIO_GROUP_E l_eGpioGroup;
GPIO_BIT_E l_eBit;
GPIO_DATA_E l_eData;
l_u8GPIONum = atoi(argv[1]);
l_u8GPIOBit = atoi(argv[2]);
if(l_u8GPIONum<14)
{
l_eGpioGroup = (GPIO_GROUP_E)l_u8GPIONum;
}else
{
printf("l_u8GPIONum error l_u8GPIONum = %d\n",l_u8GPIONum);
return -1;
};
if(l_u8GPIOBit<8)
{
l_eBit = (GPIO_BIT_E)l_u8GPIOBit;
}else
{
printf("l_u8GPIOBit error l_u8GPIOBit = %d\n",l_u8GPIOBit);
return -1;
}
if(NULL!=argv[3])
{
l_u8SetValue = atoi(argv[3]);
if(0==l_u8SetValue)
{
l_eData = (GPIO_DATA_E)l_u8SetValue;
}else if(1==l_u8SetValue)
{
l_eData = (GPIO_DATA_E)l_u8SetValue;
}else
{
printf("l_u8SetValue error l_u8SetValue = %d\n",l_u8SetValue);
}
}
if(3==argc)
{/**read**/
printf("read GPIO%d Bit%d \n",l_u8GPIONum,l_u8GPIOBit);
/**set input**/
HstGpio_Set_Direction(l_eGpioGroup, l_eBit, GPIO_INPUT);
/**read **/
char l_s8bit_val = 0;
HstGpio_Get_Value(l_eGpioGroup, l_eBit, &l_s8bit_val);
printf("read Data = %d \n",l_s8bit_val);
}else if(4==argc)
{/**write**/
printf("Write GPIO %d; Bit %d; Value %d\n",l_u8GPIONum,l_u8GPIOBit,l_u8SetValue);
/***set IO output*/
HstGpio_Set_Direction(l_eGpioGroup, l_eBit, GPIO_OUPUT);
/**Write To IO**/
HstGpio_Set_Value(l_eGpioGroup,l_eBit,l_eData);
}else
{
}
return 0;
}
6、文件固定位置插入数据
在文件的固定位置插入固定的数据。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BASIC_FILE_NAME "./nandflash.bin"
#define UBOOT_FILE_NAME "./u-boot.bin"
#define KERNEL_FILE_NAME "./kernel.bin"
#define ROOTFS_FILE_NAME "./rootfs.bin"
#define APP_FILE_NAME "./app.bin"
#define UBOOT_POSITION 0x00
#define KERNEL_POSITION 0x100000
#define ROOTFS_POSITION 0x500000
#define APP_POSITION 0x2700000
int InsertData(FILE *pfBasic,FILE *psInsert,int s32Position)
{
int l_S32Ret = 0;
unsigned char l_arru8Temp[1024] = {0xff};
fseek(pfBasic,s32Position,SEEK_SET);
fseek(psInsert,0,SEEK_SET);
while(1)
{
l_S32Ret = fread(l_arru8Temp,1,1024,psInsert);
if(l_S32Ret > 0)
{
l_S32Ret = fwrite(l_arru8Temp,1,l_S32Ret,pfBasic);
if(l_S32Ret<=0)
{
printf("line %d error l_S32Ret = %d \n",__LINE__,l_S32Ret);
return -1;
}
}else
{
break;
}
}
return 0;
}
int main(void)
{
int l_s32Ret = 0;
FILE *l_pfBasec = NULL;
FILE *l_pfUboot = NULL;
FILE *l_pfKernel = NULL;
FILE *l_pfRootfs = NULL;
FILE *l_pfApp = NULL;
l_pfBasec = fopen(BASIC_FILE_NAME,"r+");
if(NULL==l_pfBasec)
{
printf("line %d error \n",__LINE__);
goto ERROR;
}
l_pfUboot = fopen(UBOOT_FILE_NAME,"r");
if(NULL==l_pfUboot)
{
printf("line %d error \n",__LINE__);
goto ERROR;
}
l_pfKernel = fopen(KERNEL_FILE_NAME,"r");
if(NULL==l_pfKernel)
{
printf("line %d error \n",__LINE__);
goto ERROR;
}
l_pfRootfs = fopen(ROOTFS_FILE_NAME,"r");
if(NULL==l_pfRootfs)
{
printf("line %d error \n",__LINE__);
goto ERROR;
}
l_pfApp = fopen(APP_FILE_NAME,"r");
if(NULL==l_pfApp)
{
printf("line %d error \n",__LINE__);
goto ERROR;
}
if(0> InsertData(l_pfBasec,l_pfUboot,UBOOT_POSITION))
{
printf("line %d error \n",__LINE__);
goto ERROR;
}
if(0> InsertData(l_pfBasec,l_pfKernel,KERNEL_POSITION))
{
printf("line %d error \n",__LINE__);
goto ERROR;
}
if(0> InsertData(l_pfBasec,l_pfRootfs,ROOTFS_POSITION))
{
printf("line %d error \n",__LINE__);
goto ERROR;
}
if(0> InsertData(l_pfBasec,l_pfApp,APP_POSITION))
{
printf("line %d error \n",__LINE__);
goto ERROR;
}
ERROR:
if(NULL!=l_pfBasec)
{
fclose(l_pfBasec);
l_pfBasec = NULL;
}
if(NULL!=l_pfUboot)
{
fclose(l_pfUboot);
l_pfUboot = NULL;
}
if(NULL!=l_pfKernel)
{
fclose(l_pfKernel);
l_pfKernel = NULL;
}
if(NULL!=l_pfRootfs)
{
fclose(l_pfRootfs);
l_pfRootfs = NULL;
}
if(NULL!=l_pfApp)
{
fclose(l_pfApp);
l_pfApp = NULL;
}
return 0;
} |