Test Makefile on Pi

This commit is contained in:
2024-11-19 21:50:22 -05:00
parent f580cac132
commit 2aca002000
29 changed files with 13311 additions and 81 deletions

522
c/libs/Config/DEV_Config.c Normal file
View File

@@ -0,0 +1,522 @@
/*****************************************************************************
* | File : DEV_Config.c
* | Author : Waveshare team
* | Function : Hardware underlying interface
* | Info :
*----------------
* | This version: V3.0
* | Date : 2019-07-31
* | Info :
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documnetation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of theex Software, and to permit persons to whom the Software is
# furished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
******************************************************************************/
#include "DEV_Config.h"
#if USE_LGPIO_LIB
int GPIO_Handle;
int SPI_Handle;
#endif
/**
* GPIO
**/
int EPD_RST_PIN;
int EPD_DC_PIN;
int EPD_CS_PIN;
int EPD_BUSY_PIN;
int EPD_PWR_PIN;
int EPD_MOSI_PIN;
int EPD_SCLK_PIN;
/**
* GPIO read and write
**/
void DEV_Digital_Write(UWORD Pin, UBYTE Value)
{
#ifdef RPI
#ifdef USE_BCM2835_LIB
bcm2835_gpio_write(Pin, Value);
#elif USE_WIRINGPI_LIB
digitalWrite(Pin, Value);
#elif USE_LGPIO_LIB
lgGpioWrite(GPIO_Handle, Pin, Value);
#elif USE_DEV_LIB
GPIOD_Write(Pin, Value);
#endif
#endif
#ifdef JETSON
#ifdef USE_DEV_LIB
SYSFS_GPIO_Write(Pin, Value);
#elif USE_HARDWARE_LIB
Debug("not support");
#endif
#endif
}
UBYTE DEV_Digital_Read(UWORD Pin)
{
UBYTE Read_value = 0;
#ifdef RPI
#ifdef USE_BCM2835_LIB
Read_value = bcm2835_gpio_lev(Pin);
#elif USE_WIRINGPI_LIB
Read_value = digitalRead(Pin);
#elif USE_LGPIO_LIB
Read_value = lgGpioRead(GPIO_Handle,Pin);
#elif USE_DEV_LIB
Read_value = GPIOD_Read(Pin);
#endif
#endif
#ifdef JETSON
#ifdef USE_DEV_LIB
Read_value = SYSFS_GPIO_Read(Pin);
#elif USE_HARDWARE_LIB
Debug("not support");
#endif
#endif
return Read_value;
}
/**
* SPI
**/
void DEV_SPI_WriteByte(uint8_t Value)
{
#ifdef RPI
#ifdef USE_BCM2835_LIB
bcm2835_spi_transfer(Value);
#elif USE_WIRINGPI_LIB
wiringPiSPIDataRW(0,&Value,1);
#elif USE_LGPIO_LIB
lgSpiWrite(SPI_Handle,(char*)&Value, 1);
#elif USE_DEV_LIB
DEV_HARDWARE_SPI_TransferByte(Value);
#endif
#endif
#ifdef JETSON
#ifdef USE_DEV_LIB
SYSFS_software_spi_transfer(Value);
#elif USE_HARDWARE_LIB
Debug("not support");
#endif
#endif
}
void DEV_SPI_Write_nByte(uint8_t *pData, uint32_t Len)
{
#ifdef RPI
#ifdef USE_BCM2835_LIB
char rData[Len];
bcm2835_spi_transfernb((char *)pData,rData,Len);
#elif USE_WIRINGPI_LIB
wiringPiSPIDataRW(0, pData, Len);
#elif USE_LGPIO_LIB
lgSpiWrite(SPI_Handle,(char*)pData, Len);
#elif USE_DEV_LIB
DEV_HARDWARE_SPI_Transfer(pData, Len);
#endif
#endif
#ifdef JETSON
#ifdef USE_DEV_LIB
//JETSON nano waits for hardware SPI
// Debug("not support");
uint32_t i;
for(i = 0; i<Len; i++)
SYSFS_software_spi_transfer(pData[i]);
#elif USE_HARDWARE_LIB
Debug("not support");
#endif
#endif
}
/**
* GPIO Mode
**/
void DEV_GPIO_Mode(UWORD Pin, UWORD Mode)
{
#ifdef RPI
#ifdef USE_BCM2835_LIB
if(Mode == 0 || Mode == BCM2835_GPIO_FSEL_INPT) {
bcm2835_gpio_fsel(Pin, BCM2835_GPIO_FSEL_INPT);
} else {
bcm2835_gpio_fsel(Pin, BCM2835_GPIO_FSEL_OUTP);
}
#elif USE_WIRINGPI_LIB
if(Mode == 0 || Mode == INPUT) {
pinMode(Pin, INPUT);
pullUpDnControl(Pin, PUD_UP);
} else {
pinMode(Pin, OUTPUT);
// Debug (" %d OUT \r\n",Pin);
}
#elif USE_LGPIO_LIB
if(Mode == 0 || Mode == LG_SET_INPUT){
lgGpioClaimInput(GPIO_Handle,LFLAGS,Pin);
// printf("IN Pin = %d\r\n",Pin);
}else{
lgGpioClaimOutput(GPIO_Handle, LFLAGS, Pin, LG_LOW);
// printf("OUT Pin = %d\r\n",Pin);
}
#elif USE_DEV_LIB
if(Mode == 0 || Mode == GPIOD_IN) {
GPIOD_Direction(Pin, GPIOD_IN);
// Debug("IN Pin = %d\r\n",Pin);
} else {
GPIOD_Direction(Pin, GPIOD_OUT);
// Debug("OUT Pin = %d\r\n",Pin);
}
#endif
#endif
#ifdef JETSON
#ifdef USE_DEV_LIB
SYSFS_GPIO_Export(Pin);
SYSFS_GPIO_Direction(Pin, Mode);
#elif USE_HARDWARE_LIB
Debug("not support");
#endif
#endif
}
/**
* delay x ms
**/
void DEV_Delay_ms(UDOUBLE xms)
{
#ifdef RPI
#ifdef USE_BCM2835_LIB
bcm2835_delay(xms);
#elif USE_WIRINGPI_LIB
delay(xms);
#elif USE_LGPIO_LIB
lguSleep(xms/1000.0);
#elif USE_DEV_LIB
UDOUBLE i;
for(i=0; i < xms; i++) {
usleep(1000);
}
#endif
#endif
#ifdef JETSON
UDOUBLE i;
for(i=0; i < xms; i++) {
usleep(1000);
}
#endif
}
static int DEV_Equipment_Testing(void)
{
FILE *fp;
char issue_str[64];
fp = fopen("/etc/issue", "r");
if (fp == NULL) {
Debug("Unable to open /etc/issue");
return -1;
}
if (fread(issue_str, 1, sizeof(issue_str), fp) <= 0) {
Debug("Unable to read from /etc/issue");
return -1;
}
issue_str[sizeof(issue_str)-1] = '\0';
fclose(fp);
printf("Current environment: ");
#ifdef RPI
char systems[][9] = {"Raspbian", "Debian", "NixOS"};
int detected = 0;
for(int i=0; i<3; i++) {
if (strstr(issue_str, systems[i]) != NULL) {
printf("%s\n", systems[i]);
detected = 1;
}
}
if (!detected) {
printf("not recognized\n");
printf("Built for Raspberry Pi, but unable to detect environment.\n");
printf("Perhaps you meant to 'make JETSON' instead?\n");
return -1;
}
#endif
#ifdef JETSON
char system[] = {"Ubuntu"};
if (strstr(issue_str, system) != NULL) {
printf("%s\n", system);
} else {
printf("not recognized\n");
printf("Built for Jetson, but unable to detect environment.\n");
printf("Perhaps you meant to 'make RPI' instead?\n");
return -1;
}
#endif
return 0;
}
void DEV_GPIO_Init(void)
{
#ifdef RPI
EPD_RST_PIN = 17;
EPD_DC_PIN = 25;
EPD_CS_PIN = 8;
EPD_PWR_PIN = 18;
EPD_BUSY_PIN = 24;
EPD_MOSI_PIN = 10;
EPD_SCLK_PIN = 11;
#elif JETSON
EPD_RST_PIN = GPIO17;
EPD_DC_PIN = GPIO25;
EPD_CS_PIN = SPI0_CS0;
EPD_PWR_PIN = GPIO18;
EPD_BUSY_PIN = GPIO24;
EPD_MOSI_PIN = SPI0_MOSI;
EPD_SCLK_PIN = SPI0_SCLK;
#endif
DEV_GPIO_Mode(EPD_BUSY_PIN, 0);
DEV_GPIO_Mode(EPD_RST_PIN, 1);
DEV_GPIO_Mode(EPD_DC_PIN, 1);
DEV_GPIO_Mode(EPD_CS_PIN, 1);
DEV_GPIO_Mode(EPD_PWR_PIN, 1);
// DEV_GPIO_Mode(EPD_MOSI_PIN, 0);
// DEV_GPIO_Mode(EPD_SCLK_PIN, 1);
DEV_Digital_Write(EPD_CS_PIN, 1);
DEV_Digital_Write(EPD_PWR_PIN, 1);
}
void DEV_SPI_SendnData(UBYTE *Reg)
{
UDOUBLE size;
size = sizeof(Reg);
for(UDOUBLE i=0 ; i<size ; i++)
{
DEV_SPI_SendData(Reg[i]);
}
}
void DEV_SPI_SendData(UBYTE Reg)
{
UBYTE i,j=Reg;
DEV_GPIO_Mode(EPD_MOSI_PIN, 1);
DEV_Digital_Write(EPD_CS_PIN, 0);
for(i = 0; i<8; i++)
{
DEV_Digital_Write(EPD_SCLK_PIN, 0);
if (j & 0x80)
{
DEV_Digital_Write(EPD_MOSI_PIN, 1);
}
else
{
DEV_Digital_Write(EPD_MOSI_PIN, 0);
}
DEV_Digital_Write(EPD_SCLK_PIN, 1);
j = j << 1;
}
DEV_Digital_Write(EPD_SCLK_PIN, 0);
DEV_Digital_Write(EPD_CS_PIN, 1);
}
UBYTE DEV_SPI_ReadData()
{
UBYTE i,j=0xff;
DEV_GPIO_Mode(EPD_MOSI_PIN, 0);
DEV_Digital_Write(EPD_CS_PIN, 0);
for(i = 0; i<8; i++)
{
DEV_Digital_Write(EPD_SCLK_PIN, 0);
j = j << 1;
if (DEV_Digital_Read(EPD_MOSI_PIN))
{
j = j | 0x01;
}
else
{
j= j & 0xfe;
}
DEV_Digital_Write(EPD_SCLK_PIN, 1);
}
DEV_Digital_Write(EPD_SCLK_PIN, 0);
DEV_Digital_Write(EPD_CS_PIN, 1);
return j;
}
/******************************************************************************
function: Module Initialize, the library and initialize the pins, SPI protocol
parameter:
Info:
******************************************************************************/
UBYTE DEV_Module_Init(void)
{
printf("/***********************************/ \r\n");
if(DEV_Equipment_Testing() < 0) {
return 1;
}
#ifdef RPI
#ifdef USE_BCM2835_LIB
if(!bcm2835_init()) {
printf("bcm2835 init failed !!! \r\n");
return 1;
} else {
printf("bcm2835 init success !!! \r\n");
}
// GPIO Config
DEV_GPIO_Init();
bcm2835_spi_begin(); //Start spi interface, set spi pin for the reuse function
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); //High first transmission
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); //spi mode 0
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_128); //Frequency
bcm2835_spi_chipSelect(BCM2835_SPI_CS0); //set CE0
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); //enable cs0
#elif USE_WIRINGPI_LIB
//if(wiringPiSetup() < 0)//use wiringpi Pin number table
if(wiringPiSetupGpio() < 0) { //use BCM2835 Pin number table
printf("set wiringPi lib failed !!! \r\n");
return 1;
} else {
printf("set wiringPi lib success !!! \r\n");
}
// GPIO Config
DEV_GPIO_Init();
wiringPiSPISetup(0,10000000);
// wiringPiSPISetupMode(0, 32000000, 0);
#elif USE_LGPIO_LIB
char buffer[NUM_MAXBUF];
FILE *fp;
fp = popen("cat /proc/cpuinfo | grep 'Raspberry Pi 5'", "r");
if (fp == NULL) {
Debug("It is not possible to determine the model of the Raspberry PI\n");
return -1;
}
if(fgets(buffer, sizeof(buffer), fp) != NULL)
{
GPIO_Handle = lgGpiochipOpen(4);
if (GPIO_Handle < 0)
{
Debug( "gpiochip4 Export Failed\n");
return -1;
}
}
else
{
GPIO_Handle = lgGpiochipOpen(0);
if (GPIO_Handle < 0)
{
Debug( "gpiochip0 Export Failed\n");
return -1;
}
}
SPI_Handle = lgSpiOpen(0, 0, 10000000, 0);
DEV_GPIO_Init();
#elif USE_DEV_LIB
printf("Write and read /dev/spidev0.0 \r\n");
GPIOD_Export();
DEV_GPIO_Init();
DEV_HARDWARE_SPI_begin("/dev/spidev0.0");
DEV_HARDWARE_SPI_setSpeed(10000000);
#endif
#elif JETSON
#ifdef USE_DEV_LIB
DEV_GPIO_Init();
printf("Software spi\r\n");
SYSFS_software_spi_begin();
SYSFS_software_spi_setBitOrder(SOFTWARE_SPI_MSBFIRST);
SYSFS_software_spi_setDataMode(SOFTWARE_SPI_Mode0);
SYSFS_software_spi_setClockDivider(SOFTWARE_SPI_CLOCK_DIV4);
#elif USE_HARDWARE_LIB
printf("Write and read /dev/spidev0.0 \r\n");
DEV_GPIO_Init();
DEV_HARDWARE_SPI_begin("/dev/spidev0.0");
#endif
#endif
printf("/***********************************/ \r\n");
return 0;
}
/******************************************************************************
function: Module exits, closes SPI and BCM2835 library
parameter:
Info:
******************************************************************************/
void DEV_Module_Exit(void)
{
#ifdef RPI
#ifdef USE_BCM2835_LIB
DEV_Digital_Write(EPD_CS_PIN, LOW);
DEV_Digital_Write(EPD_PWR_PIN, LOW);
DEV_Digital_Write(EPD_DC_PIN, LOW);
DEV_Digital_Write(EPD_RST_PIN, LOW);
bcm2835_spi_end();
bcm2835_close();
#elif USE_WIRINGPI_LIB
DEV_Digital_Write(EPD_CS_PIN, 0);
DEV_Digital_Write(EPD_PWR_PIN, 0);
DEV_Digital_Write(EPD_DC_PIN, 0);
DEV_Digital_Write(EPD_RST_PIN, 0);
#elif USE_LGPIO_LIB
// DEV_Digital_Write(EPD_CS_PIN, 0);
// DEV_Digital_Write(EPD_PWR_PIN, 0);
// DEV_Digital_Write(EPD_DC_PIN, 0);
// DEV_Digital_Write(EPD_RST_PIN, 0);
// lgSpiClose(SPI_Handle);
// lgGpiochipClose(GPIO_Handle);
#elif USE_DEV_LIB
DEV_HARDWARE_SPI_end();
DEV_Digital_Write(EPD_CS_PIN, 0);
DEV_Digital_Write(EPD_PWR_PIN, 0);
DEV_Digital_Write(EPD_DC_PIN, 0);
DEV_Digital_Write(EPD_RST_PIN, 0);
GPIOD_Unexport(EPD_PWR_PIN);
GPIOD_Unexport(EPD_DC_PIN);
GPIOD_Unexport(EPD_RST_PIN);
GPIOD_Unexport(EPD_BUSY_PIN);
GPIOD_Unexport_GPIO();
#endif
#elif JETSON
#ifdef USE_DEV_LIB
SYSFS_GPIO_Unexport(EPD_CS_PIN);
SYSFS_GPIO_Unexport(EPD_PWR_PIN;
SYSFS_GPIO_Unexport(EPD_DC_PIN);
SYSFS_GPIO_Unexport(EPD_RST_PIN);
SYSFS_GPIO_Unexport(EPD_BUSY_PIN);
#elif USE_HARDWARE_LIB
Debug("not support");
#endif
#endif
}

170
c/libs/Config/RPI_gpiod.c Normal file
View File

@@ -0,0 +1,170 @@
/*****************************************************************************
* | File : RPI_GPIOD.c
* | Author : Waveshare team
* | Function : Drive GPIO
* | Info : Read and write gpio
*----------------
* | This version: V1.0
* | Date : 2023-11-15
* | Info : Basic version
*
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documnetation files (the "Software"), to deal
# GPIOD_IN the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furished to do so, subject to the folGPIOD_LOWing conditions:
#
# The above copyright notice and this permission notice shall be included GPIOD_IN
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. GPIOD_IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY WHETHER GPIOD_IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# GPIOD_OUT OF OR GPIOD_IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS GPIOD_IN
# THE SOFTWARE.
#
******************************************************************************/
#include "RPI_gpiod.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <gpiod.h>
struct gpiod_chip *gpiochip;
struct gpiod_line *gpioline;
int ret;
int GPIOD_Export()
{
char buffer[NUM_MAXBUF];
FILE *fp;
fp = popen("cat /proc/cpuinfo | grep 'Raspberry Pi 5'", "r");
if (fp == NULL) {
GPIOD_Debug("It is not possible to determine the model of the Raspberry PI\n");
return -1;
}
if(fgets(buffer, sizeof(buffer), fp) != NULL)
{
gpiochip = gpiod_chip_open("/dev/gpiochip4");
if (gpiochip == NULL)
{
GPIOD_Debug( "gpiochip4 Export Failed\n");
return -1;
}
}
else
{
gpiochip = gpiod_chip_open("/dev/gpiochip0");
if (gpiochip == NULL)
{
GPIOD_Debug( "gpiochip0 Export Failed\n");
return -1;
}
}
return 0;
}
int GPIOD_Unexport(int Pin)
{
gpioline = gpiod_chip_get_line(gpiochip, Pin);
if (gpioline == NULL)
{
GPIOD_Debug( "Export Failed: Pin%d\n", Pin);
return -1;
}
gpiod_line_release(gpioline);
GPIOD_Debug( "Unexport: Pin%d\r\n", Pin);
return 0;
}
int GPIOD_Unexport_GPIO(void)
{
gpiod_line_release(gpioline);
gpiod_chip_close(gpiochip);
return 0;
}
int GPIOD_Direction(int Pin, int Dir)
{
gpioline = gpiod_chip_get_line(gpiochip, Pin);
if (gpioline == NULL)
{
GPIOD_Debug( "Export Failed: Pin%d\n", Pin);
return -1;
}
if(Dir == GPIOD_IN)
{
ret = gpiod_line_request_input(gpioline, "gpio");
if (ret != 0)
{
GPIOD_Debug( "Export Failed: Pin%d\n", Pin);
return -1;
}
GPIOD_Debug("Pin%d:intput\r\n", Pin);
}
else
{
ret = gpiod_line_request_output(gpioline, "gpio", 0);
if (ret != 0)
{
GPIOD_Debug( "Export Failed: Pin%d\n", Pin);
return -1;
}
GPIOD_Debug("Pin%d:Output\r\n", Pin);
}
return 0;
}
int GPIOD_Read(int Pin)
{
gpioline = gpiod_chip_get_line(gpiochip, Pin);
if (gpioline == NULL)
{
GPIOD_Debug( "Export Failed: Pin%d\n", Pin);
return -1;
}
ret = gpiod_line_get_value(gpioline);
if (ret < 0)
{
GPIOD_Debug( "failed to read value!\n");
return -1;
}
return(ret);
}
int GPIOD_Write(int Pin, int value)
{
gpioline = gpiod_chip_get_line(gpiochip, Pin);
if (gpioline == NULL)
{
GPIOD_Debug( "Export Failed: Pin%d\n", Pin);
return -1;
}
ret = gpiod_line_set_value(gpioline, value);
if (ret != 0)
{
GPIOD_Debug( "failed to write value! : Pin%d\n", Pin);
return -1;
}
return 0;
}

88
c/libs/Config/RPI_gpiod.h Normal file
View File

@@ -0,0 +1,88 @@
/*****************************************************************************
* | File : gpiod.h
* | Author : Waveshare team
* | Function : Drive GPIO
* | Info : Read and write gpio
*----------------
* | This version: V1.0
* | Date : 2023-11-15
* | Info : Basic version
*d
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documnetation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furished to do so, subject to the following conditions:
#D
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
************************D******************************************************/
#ifndef __GPIOD_
#define __GPIOD_
#include <stdio.h>
#include <gpiod.h>
#define GPIOD_IN 0
#define GPIOD_OUT 1
#define GPIOD_LOW 0
#define GPIOD_HIGH 1
#define NUM_MAXBUF 4
#define DIR_MAXSIZ 60
#define GPIOD_DEBUG 0
#if GPIOD_DEBUG
#define GPIOD_Debug(__info,...) printf("Debug: " __info,##__VA_ARGS__)
#else
#define GPIOD_Debug(__info,...)
#endif
// BCM GPIO for Jetson nano
#define GPIO4 4 // 7, 4
#define GPIO17 7 // 11, 17
#define GPIO18 18 // 12, 18
#define GPIO27 27 // 13, 27
#define GPIO22 22 // 15, 22
#define GPIO23 23 // 16, 23
#define GPIO24 24 // 18, 24
#define SPI0_MOSI 10 // 19, 10
#define SPI0_MISO 9 // 21, 9
#define GPIO25 28 // 22, 25
#define SPI0_SCK 11 // 23, 11
#define SPI0_CS0 8 // 24, 8
#define SPI0_CS1 7 // 26, 7
#define GPIO5 5 // 29, 5
#define GPIO6 6 // 31, 6
#define GPIO12 12 // 32, 12
#define GPIO13 13 // 33, 13
#define GPIO19 19 // 35, 19
#define GPIO16 16 // 36, 16
#define GPIO26 26 // 37, 26
#define GPIO20 20 // 38, 20
#define GPIO21 21 // 40, 21
extern struct gpiod_chip *gpiochip;
extern struct gpiod_line *gpioline;
extern int ret;
int GPIOD_Export();
int GPIOD_Unexport(int Pin);
int GPIOD_Unexport_GPIO(void);
int GPIOD_Direction(int Pin, int Dir);
int GPIOD_Read(int Pin);
int GPIOD_Write(int Pin, int value);
#endif

View File

@@ -0,0 +1,366 @@
/*****************************************************************************
* | File : dev_hardware_SPI.c
* | Author : Waveshare team
* | Function : Read and write /dev/SPI, hardware SPI
* | Info :
*----------------
* | This version: V1.0
* | Date : 2019-06-26
* | Info : Basic version
*
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documnetation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
******************************************************************************/
#include "dev_hardware_SPI.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
HARDWARE_SPI hardware_SPI;
static uint8_t bits = 8;
#define SPI_CS_HIGH 0x04 //Chip select high
#define SPI_LSB_FIRST 0x08 //LSB
#define SPI_3WIRE 0x10 //3-wire mode SI and SO same line
#define SPI_LOOP 0x20 //Loopback mode
#define SPI_NO_CS 0x40 //A single device occupies one SPI bus, so there is no chip select
#define SPI_READY 0x80 //Slave pull low to stop data transmission
struct spi_ioc_transfer tr;
/******************************************************************************
function: SPI port initialization
parameter:
SPI_device : Device name
Info:
/dev/spidev0.0
/dev/spidev0.1
******************************************************************************/
void DEV_HARDWARE_SPI_begin(char *SPI_device)
{
//device
int ret = 0;
if((hardware_SPI.fd = open(SPI_device, O_RDWR )) < 0) {
perror("Failed to open SPI device.\n");
printf("Failed to open SPI device\r\n");
DEV_HARDWARE_SPI_Debug("Failed to open SPI device\r\n");
exit(1);
} else {
printf("open : %s\r\n", SPI_device);
DEV_HARDWARE_SPI_Debug("open : %s\r\n", SPI_device);
}
hardware_SPI.mode = 0;
ret = ioctl(hardware_SPI.fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1) {
printf("can't set bits per word\r\n");
DEV_HARDWARE_SPI_Debug("can't set bits per word\r\n");
}
ret = ioctl(hardware_SPI.fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1) {
printf("can't get bits per word\r\n");
DEV_HARDWARE_SPI_Debug("can't get bits per word\r\n");
}
tr.bits_per_word = bits;
DEV_HARDWARE_SPI_Mode(SPI_MODE_0);
DEV_HARDWARE_SPI_ChipSelect(SPI_CS_Mode_LOW);
DEV_HARDWARE_SPI_SetBitOrder(SPI_BIT_ORDER_LSBFIRST);
DEV_HARDWARE_SPI_setSpeed(20000000);
DEV_HARDWARE_SPI_SetDataInterval(5);
}
void DEV_HARDWARE_SPI_beginSet(char *SPI_device, SPIMode mode, uint32_t speed)
{
//device
int ret = 0;
hardware_SPI.mode = 0;
if((hardware_SPI.fd = open(SPI_device, O_RDWR )) < 0) {
perror("Failed to open SPI device.\n");
exit(1);
} else {
DEV_HARDWARE_SPI_Debug("open : %s\r\n", SPI_device);
}
ret = ioctl(hardware_SPI.fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1)
DEV_HARDWARE_SPI_Debug("can't set bits per word\r\n");
ret = ioctl(hardware_SPI.fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1)
DEV_HARDWARE_SPI_Debug("can't get bits per word\r\n");
DEV_HARDWARE_SPI_Mode(mode);
DEV_HARDWARE_SPI_ChipSelect(SPI_CS_Mode_LOW);
DEV_HARDWARE_SPI_setSpeed(speed);
DEV_HARDWARE_SPI_SetDataInterval(0);
}
/******************************************************************************
function: SPI device End
parameter:
Info:
******************************************************************************/
void DEV_HARDWARE_SPI_end(void)
{
hardware_SPI.mode = 0;
if (close(hardware_SPI.fd) != 0){
DEV_HARDWARE_SPI_Debug("Failed to close SPI device\r\n");
perror("Failed to close SPI device.\n");
}
}
/******************************************************************************
function: Set SPI speed
parameter:
Info: Return 1 success
Return -1 failed
******************************************************************************/
int DEV_HARDWARE_SPI_setSpeed(uint32_t speed)
{
uint32_t speed1 = hardware_SPI.speed;
hardware_SPI.speed = speed;
//Write speed
if (ioctl(hardware_SPI.fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
DEV_HARDWARE_SPI_Debug("can't set max speed hz\r\n");
hardware_SPI.speed = speed1;//Setting failure rate unchanged
return -1;
}
//Read the speed of just writing
if (ioctl(hardware_SPI.fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) == -1) {
DEV_HARDWARE_SPI_Debug("can't get max speed hz\r\n");
hardware_SPI.speed = speed1;//Setting failure rate unchanged
return -1;
}
hardware_SPI.speed = speed;
tr.speed_hz = hardware_SPI.speed;
return 1;
}
/******************************************************************************
function: Set SPI Mode
parameter:
Info:
SPIMode:
SPI_MODE0
SPI_MODE1
SPI_MODE2
SPI_MODE3
Return :
Return 1 success
Return -1 failed
******************************************************************************/
int DEV_HARDWARE_SPI_Mode(SPIMode mode)
{
hardware_SPI.mode &= 0xfC;//Clear low 2 digits
hardware_SPI.mode |= mode;//Setting mode
//Write device
if (ioctl(hardware_SPI.fd, SPI_IOC_WR_MODE, &hardware_SPI.mode) == -1) {
DEV_HARDWARE_SPI_Debug("can't set spi mode\r\n");
return -1;
}
return 1;
}
/******************************************************************************
function: Set SPI CS Enable
parameter:
Info:
EN:
DISABLE
ENABLE
Return :
Return 1 success
Return -1 failed
******************************************************************************/
int DEV_HARDWARE_SPI_CSEN(SPICSEN EN)
{
if(EN == ENABLE){
hardware_SPI.mode |= SPI_NO_CS;
}else {
hardware_SPI.mode &= ~SPI_NO_CS;
}
//Write device
if (ioctl(hardware_SPI.fd, SPI_IOC_WR_MODE, &hardware_SPI.mode) == -1) {
DEV_HARDWARE_SPI_Debug("can't set spi CS EN\r\n");
return -1;
}
return 1;
}
/******************************************************************************
function: Chip Select
parameter:
Info:
CS_Mode:
SPI_CS_Mode_LOW
SPI_CS_Mode_HIGH
SPI_CS_Mode_NONE
Return :
Return 1 success
Return -1 failed
******************************************************************************/
int DEV_HARDWARE_SPI_ChipSelect(SPIChipSelect CS_Mode)
{
if(CS_Mode == SPI_CS_Mode_HIGH){
hardware_SPI.mode |= SPI_CS_HIGH;
hardware_SPI.mode &= ~SPI_NO_CS;
DEV_HARDWARE_SPI_Debug("CS HIGH \r\n");
}else if(CS_Mode == SPI_CS_Mode_LOW){
hardware_SPI.mode &= ~SPI_CS_HIGH;
hardware_SPI.mode &= ~SPI_NO_CS;
}else if(CS_Mode == SPI_CS_Mode_NONE){
hardware_SPI.mode |= SPI_NO_CS;
}
if (ioctl(hardware_SPI.fd, SPI_IOC_WR_MODE, &hardware_SPI.mode) == -1) {
DEV_HARDWARE_SPI_Debug("can't set spi mode\r\n");
return -1;
}
return 1;
}
/******************************************************************************
function: Sets the SPI bit order
parameter:
Info:
Order:
SPI_BIT_ORDER_LSBFIRST
SPI_BIT_ORDER_MSBFIRST
Return :
Return 1 success
Return -1 failed
******************************************************************************/
int DEV_HARDWARE_SPI_SetBitOrder(SPIBitOrder Order)
{
if(Order == SPI_BIT_ORDER_LSBFIRST){
hardware_SPI.mode |= SPI_LSB_FIRST;
DEV_HARDWARE_SPI_Debug("SPI_LSB_FIRST\r\n");
}else if(Order == SPI_BIT_ORDER_MSBFIRST){
hardware_SPI.mode &= ~SPI_LSB_FIRST;
DEV_HARDWARE_SPI_Debug("SPI_MSB_FIRST\r\n");
}
// DEV_HARDWARE_SPI_Debug("hardware_SPI.mode = 0x%02x\r\n", hardware_SPI.mode);
int fd = ioctl(hardware_SPI.fd, SPI_IOC_WR_MODE, &hardware_SPI.mode);
DEV_HARDWARE_SPI_Debug("fd = %d\r\n",fd);
if (fd == -1) {
DEV_HARDWARE_SPI_Debug("can't set spi SPI_LSB_FIRST\r\n");
return -1;
}
return 1;
}
/******************************************************************************
function: Sets the SPI Bus Mode
parameter:
Info:
Order:
SPI_3WIRE_Mode
SPI_4WIRE_Mode
Return :
Return 1 success
Return -1 failed
******************************************************************************/
int DEV_HARDWARE_SPI_SetBusMode(BusMode mode)
{
if(mode == SPI_3WIRE_Mode){
hardware_SPI.mode |= SPI_3WIRE;
}else if(mode == SPI_4WIRE_Mode){
hardware_SPI.mode &= ~SPI_3WIRE;
}
if (ioctl(hardware_SPI.fd, SPI_IOC_WR_MODE, &hardware_SPI.mode) == -1) {
DEV_HARDWARE_SPI_Debug("can't set spi mode\r\n");
return -1;
}
return 1;
}
/******************************************************************************
function:
Time interval after transmission of one byte during continuous transmission
parameter:
us : Interval time (us)
Info:
******************************************************************************/
void DEV_HARDWARE_SPI_SetDataInterval(uint16_t us)
{
hardware_SPI.delay = us;
tr.delay_usecs = hardware_SPI.delay;
}
/******************************************************************************
function: SPI port sends one byte of data
parameter:
buf : Sent data
Info:
******************************************************************************/
uint8_t DEV_HARDWARE_SPI_TransferByte(uint8_t buf)
{
uint8_t rbuf[1];
tr.len = 1;
tr.tx_buf = (unsigned long)&buf;
tr.rx_buf = (unsigned long)rbuf;
//ioctl Operation, transmission of data
if ( ioctl(hardware_SPI.fd, SPI_IOC_MESSAGE(1), &tr) < 1 )
DEV_HARDWARE_SPI_Debug("can't send spi message\r\n");
return rbuf[0];
}
/******************************************************************************
function: The SPI port reads a byte
parameter:
Info: Return read data
******************************************************************************/
int DEV_HARDWARE_SPI_Transfer(uint8_t *buf, uint32_t len)
{
tr.len = len;
tr.tx_buf = (unsigned long)buf;
tr.rx_buf = (unsigned long)buf;
//ioctl Operation, transmission of data
if (ioctl(hardware_SPI.fd, SPI_IOC_MESSAGE(1), &tr) < 1 ){
DEV_HARDWARE_SPI_Debug("can't send spi message\r\n");
return -1;
}
return 1;
}

View File

@@ -0,0 +1,120 @@
/*****************************************************************************
* | File : dev_hardware_SPI.h
* | Author : Waveshare team
* | Function : Read and write /dev/SPI, hardware SPI
* | Info :
*----------------
* | This version: V1.0
* | Date : 2019-06-26
* | Info : Basic version
*
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documnetation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
******************************************************************************/
#ifndef __DEV_HARDWARE_SPI_
#define __DEV_HARDWARE_SPI_
#include <stdint.h>
#define DEV_HARDWARE_SPI_DEBUG 0
#if DEV_HARDWARE_SPI_DEBUG
#define DEV_HARDWARE_SPI_Debug(__info,...) printf("Debug: " __info,##__VA_ARGS__)
#else
#define DEV_HARDWARE_SPI_Debug(__info,...)
#endif
#define SPI_CPHA 0x01
#define SPI_CPOL 0x02
#define SPI_MODE_0 (0|0)
#define SPI_MODE_1 (0|SPI_CPHA)
#define SPI_MODE_2 (SPI_CPOL|0)
#define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
typedef enum{
SPI_MODE0 = SPI_MODE_0, /*!< CPOL = 0, CPHA = 0 */
SPI_MODE1 = SPI_MODE_1, /*!< CPOL = 0, CPHA = 1 */
SPI_MODE2 = SPI_MODE_2, /*!< CPOL = 1, CPHA = 0 */
SPI_MODE3 = SPI_MODE_3 /*!< CPOL = 1, CPHA = 1 */
}SPIMode;
typedef enum{
DISABLE = 0,
ENABLE = 1
}SPICSEN;
typedef enum{
SPI_CS_Mode_LOW = 0, /*!< Chip Select 0 */
SPI_CS_Mode_HIGH = 1, /*!< Chip Select 1 */
SPI_CS_Mode_NONE = 3 /*!< No CS, control it yourself */
}SPIChipSelect;
typedef enum
{
SPI_BIT_ORDER_LSBFIRST = 0, /*!< LSB First */
SPI_BIT_ORDER_MSBFIRST = 1 /*!< MSB First */
}SPIBitOrder;
typedef enum
{
SPI_3WIRE_Mode = 0,
SPI_4WIRE_Mode = 1
}BusMode;
/**
* Define SPI attribute
**/
typedef struct SPIStruct {
//GPIO
uint16_t SCLK_PIN;
uint16_t MOSI_PIN;
uint16_t MISO_PIN;
uint16_t CS0_PIN;
uint16_t CS1_PIN;
uint32_t speed;
uint16_t mode;
uint16_t delay;
int fd; //
} HARDWARE_SPI;
void DEV_HARDWARE_SPI_begin(char *SPI_device);
void DEV_HARDWARE_SPI_beginSet(char *SPI_device, SPIMode mode, uint32_t speed);
void DEV_HARDWARE_SPI_end(void);
int DEV_HARDWARE_SPI_setSpeed(uint32_t speed);
uint8_t DEV_HARDWARE_SPI_TransferByte(uint8_t buf);
int DEV_HARDWARE_SPI_Transfer(uint8_t *buf, uint32_t len);
void DEV_HARDWARE_SPI_SetDataInterval(uint16_t us);
int DEV_HARDWARE_SPI_SetBusMode(BusMode mode);
int DEV_HARDWARE_SPI_SetBitOrder(SPIBitOrder Order);
int DEV_HARDWARE_SPI_ChipSelect(SPIChipSelect CS_Mode);
int DEV_HARDWARE_SPI_CSEN(SPICSEN EN);
int DEV_HARDWARE_SPI_Mode(SPIMode mode);
#endif

152
c/libs/Config/sysfs_gpio.c Normal file
View File

@@ -0,0 +1,152 @@
/*****************************************************************************
* | File : SYSFS_GPIO.c
* | Author : Waveshare team
* | Function : Drive SYSFS_ GPIO
* | Info : Read and write /sys/class/gpio
*----------------
* | This version: V1.0
* | Date : 2019-06-04
* | Info : Basic version
*
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documnetation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
******************************************************************************/
#include "sysfs_gpio.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int SYSFS_GPIO_Export(int Pin)
{
char buffer[NUM_MAXBUF];
int len;
int fd;
fd = open("/sys/class/gpio/export", O_WRONLY);
if (fd < 0) {
SYSFS_GPIO_Debug( "Export Failed: Pin%d\n", Pin);
return -1;
}
len = snprintf(buffer, NUM_MAXBUF, "%d", Pin);
write(fd, buffer, len);
SYSFS_GPIO_Debug( "Export: Pin%d\r\n", Pin);
close(fd);
return 0;
}
int SYSFS_GPIO_Unexport(int Pin)
{
char buffer[NUM_MAXBUF];
int len;
int fd;
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (fd < 0) {
SYSFS_GPIO_Debug( "unexport Failed: Pin%d\n", Pin);
return -1;
}
len = snprintf(buffer, NUM_MAXBUF, "%d", Pin);
write(fd, buffer, len);
SYSFS_GPIO_Debug( "Unexport: Pin%d\r\n", Pin);
close(fd);
return 0;
}
int SYSFS_GPIO_Direction(int Pin, int Dir)
{
const char dir_str[] = "in\0out";
char path[DIR_MAXSIZ];
int fd;
snprintf(path, DIR_MAXSIZ, "/sys/class/gpio/gpio%d/direction", Pin);
fd = open(path, O_WRONLY);
if (fd < 0) {
SYSFS_GPIO_Debug( "Set Direction failed: Pin%d\n", Pin);
return -1;
}
if (write(fd, &dir_str[Dir == IN ? 0 : 3], Dir == IN ? 2 : 3) < 0) {
SYSFS_GPIO_Debug("failed to set direction!\r\n");
return -1;
}
if(Dir == IN){
SYSFS_GPIO_Debug("Pin%d:intput\r\n", Pin);
}else{
SYSFS_GPIO_Debug("Pin%d:Output\r\n", Pin);
}
close(fd);
return 0;
}
int SYSFS_GPIO_Read(int Pin)
{
char path[DIR_MAXSIZ];
char value_str[3];
int fd;
snprintf(path, DIR_MAXSIZ, "/sys/class/gpio/gpio%d/value", Pin);
fd = open(path, O_RDONLY);
if (fd < 0) {
SYSFS_GPIO_Debug( "Read failed Pin%d\n", Pin);
return -1;
}
if (read(fd, value_str, 3) < 0) {
SYSFS_GPIO_Debug( "failed to read value!\n");
return -1;
}
close(fd);
return(atoi(value_str));
}
int SYSFS_GPIO_Write(int Pin, int value)
{
const char s_values_str[] = "01";
char path[DIR_MAXSIZ];
int fd;
snprintf(path, DIR_MAXSIZ, "/sys/class/gpio/gpio%d/value", Pin);
fd = open(path, O_WRONLY);
if (fd < 0) {
SYSFS_GPIO_Debug( "Write failed : Pin%d,value = %d\n", Pin, value);
return -1;
}
if (write(fd, &s_values_str[value == LOW ? 0 : 1], 1) < 0) {
SYSFS_GPIO_Debug( "failed to write value!\n");
return -1;
}
close(fd);
return 0;
}

View File

@@ -0,0 +1,84 @@
/*****************************************************************************
* | File : sysfs_gpio.h
* | Author : Waveshare team
* | Function : Drive SC16IS752 GPIO
* | Info : Read and write /sys/class/gpio
*----------------
* | This version: V1.0
* | Date : 2019-06-04
* | Info : Basic version
*
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documnetation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
******************************************************************************/
#ifndef __SYSFS_GPIO_
#define __SYSFS_GPIO_
#include <stdio.h>
#define IN 0
#define OUT 1
#define LOW 0
#define HIGH 1
#define NUM_MAXBUF 4
#define DIR_MAXSIZ 60
#define SYSFS_GPIO_DEBUG 1
#if SYSFS_GPIO_DEBUG
#define SYSFS_GPIO_Debug(__info,...) printf("Debug: " __info,##__VA_ARGS__)
#else
#define SYSFS_GPIO_Debug(__info,...)
#endif
// BCM GPIO for Jetson nano
#define GPIO4 216 // 7, 4
#define GPIO17 50 // 11, 17
#define GPIO18 79 // 12, 18
#define GPIO27 14 // 13, 27
#define GPIO22 194 // 15, 22
#define GPIO23 232 // 16, 23
#define GPIO24 15 // 18, 24
#define SPI0_MOSI 16 // 19, 10
#define SPI0_MISO 17 // 21, 9
#define GPIO25 13 // 22, 25
#define SPI0_SCK 18 // 23, 11
#define SPI0_CS0 19 // 24, 8
#define SPI0_CS1 20 // 26, 7
#define GPIO5 149 // 29, 5
#define GPIO6 200 // 31, 6
#define GPIO12 168 // 32, 12
#define GPIO13 38 // 33, 13
#define GPIO19 76 // 35, 19
#define GPIO16 51 // 36, 16
#define GPIO26 12 // 37, 26
#define GPIO20 77 // 38, 20
#define GPIO21 78 // 40, 21
// 22PIN + 2PIN UART0 + 2PIN I2C0 + 2PIN I2C
// + 2PIN 3V3 + 2PIN 5V + 8PIN GND = 40PIN
int SYSFS_GPIO_Export(int Pin);
int SYSFS_GPIO_Unexport(int Pin);
int SYSFS_GPIO_Direction(int Pin, int Dir);
int SYSFS_GPIO_Read(int Pin);
int SYSFS_GPIO_Write(int Pin, int value);
#endif

View File

@@ -0,0 +1,204 @@
/*****************************************************************************
* | File : sysfs_software_spi.h
* | Author : Waveshare team
* | Function : Read and write /sys/class/gpio, software spi
* | Info :
*----------------
* | This version: V1.0
* | Date : 2019-06-05
* | Info : Basic version
*
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documnetation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# Read_data OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
******************************************************************************/
#include "sysfs_software_spi.h"
SOFTWARE_SPI software_spi;
/******************************************************************************
function:
parameter:
Info:
******************************************************************************/
void SYSFS_software_spi_begin(void)
{
// gpio
software_spi.SCLK_PIN = SPI0_SCK;
software_spi.MOSI_PIN = SPI0_MOSI;
software_spi.MISO_PIN = SPI0_MISO;
//software spi configure
software_spi.Mode = SOFTWARE_SPI_Mode0;
software_spi.Type = SOFTWARE_SPI_Master;
software_spi.Delay = SOFTWARE_SPI_CLOCK_DIV2;
software_spi.Order = SOFTWARE_SPI_MSBFIRST; // MSBFIRST
SYSFS_GPIO_Export(software_spi.SCLK_PIN);
SYSFS_GPIO_Export(software_spi.MOSI_PIN);
SYSFS_GPIO_Export(software_spi.MISO_PIN);
SYSFS_GPIO_Direction(software_spi.SCLK_PIN, OUT);
SYSFS_GPIO_Direction(software_spi.MOSI_PIN, OUT);
SYSFS_GPIO_Direction(software_spi.MISO_PIN, IN);
}
void SYSFS_software_spi_end(void)
{
SYSFS_GPIO_Write(software_spi.SCLK_PIN, LOW);
SYSFS_GPIO_Write(software_spi.MOSI_PIN, LOW);
SYSFS_GPIO_Unexport(software_spi.SCLK_PIN);
SYSFS_GPIO_Unexport(software_spi.MOSI_PIN);
}
void SYSFS_software_spi_setBitOrder(uint8_t order)
{
software_spi.Order = order & 1;
}
void SYSFS_software_spi_setDataMode(uint8_t mode)
{
if(mode > 4) {
SYSFS_SOFTWARE_SPI_Debug("MODE must be 0-3\r\n");
return;
}
software_spi.Mode = mode;
switch (software_spi.Mode) {
case SOFTWARE_SPI_Mode0:
software_spi.CPOL = 0;
software_spi.CPHA = 0;
break;
case SOFTWARE_SPI_Mode1:
software_spi.CPOL = 0;
software_spi.CPHA = 1;
break;
case SOFTWARE_SPI_Mode2:
software_spi.CPOL = 1;
software_spi.CPHA = 0;
break;
case SOFTWARE_SPI_Mode3:
software_spi.CPOL = 1;
software_spi.CPHA = 1;
break;
}
}
void SYSFS_software_spi_setClockDivider(uint8_t div)
{
if(div > 8) {
SYSFS_SOFTWARE_SPI_Debug("div must be 0-7\r\n");
return;
}
switch (div) {
case SOFTWARE_SPI_CLOCK_DIV2:
software_spi.Delay = 2;
break;
case SOFTWARE_SPI_CLOCK_DIV4:
software_spi.Delay = 4;
break;
case SOFTWARE_SPI_CLOCK_DIV8:
software_spi.Delay = 8;
break;
case SOFTWARE_SPI_CLOCK_DIV16:
software_spi.Delay = 16;
break;
case SOFTWARE_SPI_CLOCK_DIV32:
software_spi.Delay = 32;
break;
case SOFTWARE_SPI_CLOCK_DIV64:
software_spi.Delay = 64;
break;
case SOFTWARE_SPI_CLOCK_DIV128:
software_spi.Delay = 128;
break;
default:
software_spi.Delay = 128;
break;
}
}
/******************************************************************************
function: SPI Mode 0
parameter:
Info:
******************************************************************************/
uint8_t SYSFS_software_spi_transfer(uint8_t value)
{
// printf("value = %d\r\n", value);
uint8_t Read_data;
if (software_spi.Order == SOFTWARE_SPI_LSBFIRST) {
uint8_t temp =
((value & 0x01) << 7) |
((value & 0x02) << 5) |
((value & 0x04) << 3) |
((value & 0x08) << 1) |
((value & 0x10) >> 1) |
((value & 0x20) >> 3) |
((value & 0x40) >> 5) |
((value & 0x80) >> 7);
value = temp;
}
uint8_t delay = software_spi.Delay >> 1;
for(int j=delay; j > 0; j--);
// printf("value = %d\r\n", value);
uint8_t Read_miso = 0;
SYSFS_GPIO_Write(software_spi.SCLK_PIN, 0);
for (uint8_t bit = 0; bit < 8; bit++) {
SYSFS_GPIO_Write(software_spi.SCLK_PIN, 0);
// for(int j=delay; j > 0; j--);// DELAY
if (software_spi.CPHA) {
Read_miso = SYSFS_GPIO_Read(software_spi.MISO_PIN);
if (software_spi.Order == SOFTWARE_SPI_LSBFIRST) {
Read_data <<= 1;
Read_data |= Read_miso;
} else {
Read_data >>= 1;
Read_data |= Read_miso << 7;
}
} else {
SYSFS_GPIO_Write(software_spi.MOSI_PIN, ((value<<bit) & 0x80) ? HIGH : LOW);
}
// for(int j=delay; j > 0; j--);// DELAY
SYSFS_GPIO_Write(software_spi.SCLK_PIN, 1);
// for(int j=delay; j > 0; j--);// DELAY
if (software_spi.CPHA) {
SYSFS_GPIO_Write(software_spi.MOSI_PIN, ((value<<bit) & 0x80) ? HIGH : LOW);
} else {
Read_miso = SYSFS_GPIO_Read(software_spi.MISO_PIN);
if (software_spi.Order == SOFTWARE_SPI_LSBFIRST) {
Read_data <<= 1;
Read_data |= Read_miso;
} else {
Read_data >>= 1;
Read_data |= Read_miso << 7;
}
}
// for(int j=delay; j > 0; j--);// DELAY
}
return Read_data;
}

View File

@@ -0,0 +1,111 @@
/*****************************************************************************
* | File : sysfs_software_spi.h
* | Author : Waveshare team
* | Function : Read and write /sys/class/gpio, software spi
* | Info :
*----------------
* | This version: V1.0
* | Date : 2019-06-05
* | Info : Basic version
*
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documnetation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
******************************************************************************/
#ifndef __SYSFS_SOFTWARE_SPI_
#define __SYSFS_SOFTWARE_SPI_
#include "sysfs_gpio.h"
#include <stdint.h>
#include <stdio.h>
#define SYSFS_SOFTWARE_SPI_DEBUG 1
#if SYSFS_SOFTWARE_SPI_DEBUG
#define SYSFS_SOFTWARE_SPI_Debug(__info,...) printf("Debug: " __info,##__VA_ARGS__)
#else
#define SYSFS_SOFTWARE_SPI_Debug(__info,...)
#endif
/**
* SPI communication mode
**/
typedef enum {
SOFTWARE_SPI_Mode0, /* Clock Polarity is 0 and Clock Phase is 0 */
SOFTWARE_SPI_Mode1, /* Clock Polarity is 0 and Clock Phase is 1 */
SOFTWARE_SPI_Mode2, /* Clock Polarity is 1 and Clock Phase is 0 */
SOFTWARE_SPI_Mode3, /* Clock Polarity is 1 and Clock Phase is 1 */
} SOFTWARE_SPI_Mode;
/**
* SPI clock(div)
**/
typedef enum {
SOFTWARE_SPI_CLOCK_DIV2,
SOFTWARE_SPI_CLOCK_DIV4,
SOFTWARE_SPI_CLOCK_DIV8,
SOFTWARE_SPI_CLOCK_DIV16,
SOFTWARE_SPI_CLOCK_DIV32,
SOFTWARE_SPI_CLOCK_DIV64,
SOFTWARE_SPI_CLOCK_DIV128,
} SOFTWARE_SPI_Clock;
/**
* Define SPI type
**/
typedef enum {
SOFTWARE_SPI_Master,
SOFTWARE_SPI_Slave,
} SOFTWARE_SPI_Type;
/**
* Define SPI order
**/
typedef enum {
SOFTWARE_SPI_LSBFIRST,
SOFTWARE_SPI_MSBFIRST,
} SOFTWARE_SPI_Order;
/**
* Define SPI attribute
**/
typedef struct SPIStruct {
//GPIO
uint16_t SCLK_PIN;
uint16_t MOSI_PIN;
uint16_t MISO_PIN;
uint16_t CS_PIN;
//Mode
SOFTWARE_SPI_Mode Mode;
uint8_t CPOL;
uint8_t CPHA;
SOFTWARE_SPI_Clock Delay;
SOFTWARE_SPI_Type Type;
SOFTWARE_SPI_Order Order;
} SOFTWARE_SPI;
void SYSFS_software_spi_begin(void);
void SYSFS_software_spi_end(void);
void SYSFS_software_spi_setBitOrder(uint8_t order);
void SYSFS_software_spi_setDataMode(uint8_t mode);
void SYSFS_software_spi_setClockDivider(uint8_t div);
uint8_t SYSFS_software_spi_transfer(uint8_t value);
#endif