gpio test sample for edu-riscv64
This commit is contained in:
parent
b8cf052d49
commit
00b39cdd55
|
@ -5,27 +5,58 @@
|
||||||
#define MAX_READ_LENGTH 1000
|
#define MAX_READ_LENGTH 1000
|
||||||
|
|
||||||
// sd card here is loaded as "/"
|
// sd card here is loaded as "/"
|
||||||
void TestSD(void)
|
void TestFs(void)
|
||||||
{
|
{
|
||||||
//open the file in sdcard
|
//open the file in sdcard
|
||||||
int fd = open(SD_FPATH,O_RDWR|O_CREAT);
|
int fd = open(SD_FPATH,O_RDWR|O_CREAT);
|
||||||
|
if(fd<0){
|
||||||
|
printf("fs fd open error:%d\n",fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
char filewords[MAX_READ_LENGTH];
|
char filewords[MAX_READ_LENGTH];
|
||||||
memset(filewords,0,MAX_READ_LENGTH);
|
memset(filewords,0,MAX_READ_LENGTH);
|
||||||
|
const char *input_words = "these words are going to write in fs\n";
|
||||||
|
|
||||||
//read and write then close file
|
//read and write then close file
|
||||||
read(fd,filewords,MAX_READ_LENGTH);
|
int err_flag = read(fd,filewords,MAX_READ_LENGTH);
|
||||||
|
if(err_flag<0){
|
||||||
|
printf("read failed,error:%d\n",err_flag);
|
||||||
|
return;
|
||||||
|
}
|
||||||
printf("read data is \n%s\n",filewords);
|
printf("read data is \n%s\n",filewords);
|
||||||
const char *input_words = "these words are going to write in fs\n";
|
|
||||||
write(fd,input_words,strlen(input_words));
|
err_flag = write(fd,input_words,strlen(input_words));
|
||||||
close(fd);
|
if(err_flag<0){
|
||||||
|
printf("write failed,error:%d\n",err_flag);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
err_flag = close(fd);
|
||||||
|
if(err_flag<0){
|
||||||
|
printf("close failed,error %d\n",err_flag);
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
//re-open the file and re-read the file
|
//re-open the file and re-read the file
|
||||||
fd = open(SD_FPATH,O_RDWR);
|
fd = open(SD_FPATH,O_RDWR);
|
||||||
read(fd,filewords,MAX_READ_LENGTH);
|
if(fd<0){
|
||||||
|
printf("fs fd open error:%d\n",fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
err_flag = read(fd,filewords,MAX_READ_LENGTH);
|
||||||
|
if(err_flag<0){
|
||||||
|
printf("read failed,error:%d\n",err_flag);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
printf("read data is \n%s\n",filewords);
|
printf("read data is \n%s\n",filewords);
|
||||||
close(fd);
|
err_flag = close(fd);
|
||||||
|
if(err_flag<0){
|
||||||
|
printf("close failed,error:%d\n",err_flag);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
PRIV_SHELL_CMD_FUNCTION(TestSD, a sdcard test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
PRIV_SHELL_CMD_FUNCTION(TestFs, a sd or usb filesystem test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
|
@ -2,9 +2,80 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <transform.h>
|
#include <transform.h>
|
||||||
|
|
||||||
|
#define BSP_LED_PIN 29
|
||||||
|
#define BSP_KEY_PIN 31
|
||||||
|
#define NULL_PARAMETER 0
|
||||||
|
|
||||||
void TestGpio(void)
|
void TestGpio(void)
|
||||||
{
|
{
|
||||||
|
int pin_fd = PrivOpen(GPIO_DEV_DRIVER, O_RDWR);
|
||||||
|
if(pin_fd<0){
|
||||||
|
printf("open pin fd error:%d\n",pin_fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//config led pin in board
|
||||||
|
struct PinParam parameter;
|
||||||
|
parameter.cmd = GPIO_CONFIG_MODE;
|
||||||
|
parameter.pin = BSP_LED_PIN;
|
||||||
|
parameter.mode = GPIO_CFG_OUTPUT;
|
||||||
|
|
||||||
|
struct PrivIoctlCfg ioctl_cfg;
|
||||||
|
ioctl_cfg.ioctl_driver_type = PIN_TYPE;
|
||||||
|
ioctl_cfg.args = (void *)¶meter;
|
||||||
|
|
||||||
|
if (0 != PrivIoctl(pin_fd, OPE_CFG, &ioctl_cfg)) {
|
||||||
|
printf("ioctl pin fd error %d\n", pin_fd);
|
||||||
|
PrivClose(pin_fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//config key pin in board
|
||||||
|
parameter.cmd = GPIO_CONFIG_MODE;
|
||||||
|
parameter.pin = BSP_KEY_PIN;
|
||||||
|
parameter.mode = GPIO_CFG_INPUT;
|
||||||
|
|
||||||
|
ioctl_cfg.ioctl_driver_type = PIN_TYPE;
|
||||||
|
ioctl_cfg.args = (void *)¶meter;
|
||||||
|
|
||||||
|
if (0 != PrivIoctl(pin_fd, OPE_CFG, &ioctl_cfg)) {
|
||||||
|
printf("ioctl pin fd error %d\n", pin_fd);
|
||||||
|
PrivClose(pin_fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct PinStat pin_led;
|
||||||
|
struct PinStat pin_key;
|
||||||
|
|
||||||
|
pin_led.pin = BSP_LED_PIN;
|
||||||
|
pin_key.pin = BSP_KEY_PIN;
|
||||||
|
|
||||||
|
//recycle read pin and write pin until key break
|
||||||
|
while(1){
|
||||||
|
if(0>PrivRead(pin_fd,&pin_key,NULL_PARAMETER)){
|
||||||
|
printf("read pin fd error %d\n", pin_fd);
|
||||||
|
PrivClose(pin_fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//led on if key pressed,or led off
|
||||||
|
if(pin_key.val){
|
||||||
|
pin_led.val = GPIO_HIGH;
|
||||||
|
if(0>PrivWrite(pin_fd,&pin_led,NULL_PARAMETER)){
|
||||||
|
printf("write pin fd error %d\n", pin_fd);
|
||||||
|
PrivClose(pin_fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
pin_led.val = GPIO_LOW;
|
||||||
|
if(0>PrivWrite(pin_fd,&pin_led,NULL_PARAMETER)){
|
||||||
|
printf("write pin fd error %d\n", pin_fd);
|
||||||
|
PrivClose(pin_fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PRIV_SHELL_CMD_FUNCTION(TestGpio, a gpio test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
|
@ -89,6 +89,9 @@ enum HS_GPIO_CONFIG
|
||||||
#define BSP_SOFT_SPI_MIOS_PIN 25
|
#define BSP_SOFT_SPI_MIOS_PIN 25
|
||||||
#define BSP_SOFT_SPI_MSOI_PIN 27
|
#define BSP_SOFT_SPI_MSOI_PIN 27
|
||||||
#define BSP_SOFT_SPI_NCS_PIN 28
|
#define BSP_SOFT_SPI_NCS_PIN 28
|
||||||
|
|
||||||
|
#define BSP_LED_PIN 29
|
||||||
|
#define BSP_KEY_PIN 31
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern int IoConfigInit(void);
|
extern int IoConfigInit(void);
|
||||||
|
|
Loading…
Reference in New Issue