Skip to content

Commit

Permalink
i2c to serial
Browse files Browse the repository at this point in the history
  • Loading branch information
edragon committed Dec 27, 2018
1 parent dc224b8 commit 9bba581
Show file tree
Hide file tree
Showing 13 changed files with 399 additions and 0 deletions.
20 changes: 20 additions & 0 deletions SC161S-I2CtoSerial/c/gpio/Makefile
@@ -0,0 +1,20 @@
OBJ_C = ${wildcard *.c}
OBJ_O = ${patsubst %.o,%,$(OBJ_C)}

TARGET = main

CC = gcc -std=c99

DEBUG = -g -O0 -Wall
CFLAGS += $(DEBUG)

LIB = -lwiringPi

${TARGET}:${OBJ_O}
$(CC) $(CFLAGS) $(OBJ_O) -o $@ $(LIB)

%.o : %.c
$(CC) $(CFLAGS) -c $< -o $@ $(LIB)

clean :
rm $(TARGET)
127 changes: 127 additions & 0 deletions SC161S-I2CtoSerial/c/gpio/SC16IS752GPIO.c
@@ -0,0 +1,127 @@
/*****************************************************************************
* | File : SC16IS752GPIO.c
* | Author : Waveshare team
* | Function : Drive SC16IS752 GPIO
* | Info :
* The SC16IS752/SC16IS762 is an I2C-bus/SPI bus interface to
* a dual-channel high performance UART offering data rates up
* to 5 Mbit/s, low operating and sleeping current;it provides
* the application with 8 additional programmable I/O pins.
*----------------
* | This version: V1.0
* | Date : 2018-09-28
* | Info : Basic version
*
******************************************************************************/
#include "SC16IS752GPIO.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 GPIOExport(int Pin)
{
char buffer[NUM_MAXBUF];
int len;
int fd;

fd = open("/sys/class/gpio/export", O_WRONLY);
if (fd < 0) {
printf( "Export Failed: Pin%d\n", Pin - 504);
return -1;
}

len = snprintf(buffer, NUM_MAXBUF, "%d", Pin);
write(fd, buffer, len);

close(fd);
return 0;
}

int GPIOUnexport(int Pin)
{
char buffer[NUM_MAXBUF];
int len;
int fd;

fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (fd < 0) {
printf( "unexport Failed: Pin%d\n", Pin - 504);
return -1;
}

len = snprintf(buffer, NUM_MAXBUF, "%d", Pin);
write(fd, buffer, len);

close(fd);
return 0;
}

int GPIODirection(int Pin, int Dir)
{
static 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) {
printf( "Set Direction failed: Pin%d\n", Pin - 504);
return -1;
}

if (write(fd, &dir_str[Dir == IN ? 0 : 3], Dir == IN ? 2 : 3) < 0) {
printf( "failed to set direction!\n");
return -1;
}

close(fd);
return 0;
}

int GPIORead(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) {
printf( "Read failed Pin%d\n", Pin - 504);
return -1;
}

if (read(fd, value_str, 3) < 0) {
printf( "failed to read value!\n");
return -1;
}

close(fd);
return(atoi(value_str));
}

int GPIOWrite(int Pin, int value)
{
static 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) {
printf( "Write failed : Pin%d,value = %d\n", Pin - 504, value);
return -1;
}

if (write(fd, &s_values_str[value == LOW ? 0 : 1], 1) < 0) {
printf( "failed to write value!\n");
return -1;
}

close(fd);
return 0;
}
43 changes: 43 additions & 0 deletions SC161S-I2CtoSerial/c/gpio/SC16IS752GPIO.h
@@ -0,0 +1,43 @@
/*****************************************************************************
* | File : SC16IS752GPIO.h
* | Author : Waveshare team
* | Function : Drive SC16IS752 GPIO
* | Info :
* The SC16IS752/SC16IS762 is an I2C-bus/SPI bus interface to
* a dual-channel high performance UART offering data rates up
* to 5 Mbit/s, low operating and sleeping current;it provides
* the application with 8 additional programmable I/O pins.
*----------------
* | This version: V1.0
* | Date : 2018-09-28
* | Info : Basic version
*
******************************************************************************/
#ifndef __SC16IS752GPIO_
#define __SC16IS752GPIO_

#define IN 0
#define OUT 1

#define LOW 0
#define HIGH 1

#define NUM_MAXBUF 4
#define DIR_MAXSIZ 60

#define PIN0 504
#define PIN1 (PIN0 + 1)
#define PIN2 (PIN0 + 2)
#define PIN3 (PIN0 + 3)
#define PIN4 (PIN0 + 4)
#define PIN5 (PIN0 + 5)
#define PIN6 (PIN0 + 6)
#define PIN7 (PIN0 + 7)

int GPIOExport(int pin);
int GPIOUnexport(int pin);
int GPIODirection(int pin, int dir);
int GPIORead(int pin);
int GPIOWrite(int pin, int value);

#endif
Binary file added SC161S-I2CtoSerial/c/gpio/main
Binary file not shown.
73 changes: 73 additions & 0 deletions SC161S-I2CtoSerial/c/gpio/main.c
@@ -0,0 +1,73 @@
/*****************************************************************************
* | File : MotorDriver.h
* | Author : Waveshare team
*----------------
* | This version: V1.0
* | Date : 2018-09-04
* | Info : Basic version
*
******************************************************************************/
#include "SC16IS752GPIO.h"
#include <stdlib.h> //exit()
#include <stdio.h> //printf()
#include <signal.h>
#include <wiringPi.h>

void GPIO_Init(void)
{
GPIOExport(PIN0);
GPIOExport(PIN1);
GPIOExport(PIN2);
GPIOExport(PIN3);
GPIOExport(PIN4);
GPIOExport(PIN5);
GPIOExport(PIN6);
GPIOExport(PIN7);
GPIODirection(PIN0, OUT);
GPIODirection(PIN1, OUT);
GPIODirection(PIN2, OUT);
GPIODirection(PIN3, OUT);
GPIODirection(PIN4, OUT);
GPIODirection(PIN5, OUT);
GPIODirection(PIN6, OUT);
GPIODirection(PIN7, OUT);
}

void GPIO_Exit(void)
{
GPIOUnexport(PIN0);
GPIOUnexport(PIN1);
GPIOUnexport(PIN2);
GPIOUnexport(PIN3);
GPIOUnexport(PIN4);
GPIOUnexport(PIN5);
GPIOUnexport(PIN6);
GPIOUnexport(PIN7);
}

void Handler(int signo)
{
//System Exit
printf("\r\nHandler:GPIO Stop\r\n");
GPIO_Exit();

exit(0);
}

int main(int argc, char *argv[])
{
int i = 0;

GPIO_Init();

// Exception handling:ctrl + c
signal(SIGINT, Handler);
for(i = 0; i < 8; i++) {
GPIOWrite(PIN0 + i, 1);
delay(200);
GPIOWrite(PIN0 + i, 0);
delay(200);
}
GPIO_Exit();
return 0;
}
8 changes: 8 additions & 0 deletions SC161S-I2CtoSerial/c/uart/receive/Makefile
@@ -0,0 +1,8 @@
CC = gcc
CFLAGS = -Wall -g -O0

uart_receive:uart_receive.c
$(CC) $(CFLAGS) -o $@ $^ -lwiringPi

clean:
$(RM) uart_receive .*.sw?
Binary file added SC161S-I2CtoSerial/c/uart/receive/uart_receive
Binary file not shown.
47 changes: 47 additions & 0 deletions SC161S-I2CtoSerial/c/uart/receive/uart_receive.c
@@ -0,0 +1,47 @@
#include <wiringSerial.h>
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h> //exit()
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>


#define UART_DEV1 "/dev/ttySC0"
#define UART_DEV2 "/dev/ttySC1"

int fd;

void Handler(int signo)
{
//System Exit
printf("\r\nHandler:serialClose \r\n");
serialClose(fd);

exit(0);
}

int main(void)
{
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");
}

if((fd = serialOpen (UART_DEV2, 115200)) < 0) {
printf("serial err\n");
return -1;
}

// Exception handling:ctrl + c
signal(SIGINT, Handler);
for (;;) {
putchar(serialGetchar(fd));
}

return 0;
}
8 changes: 8 additions & 0 deletions SC161S-I2CtoSerial/c/uart/send/Makefile
@@ -0,0 +1,8 @@
CC = gcc
CFLAGS = -Wall -g -O0

uart_send:uart_send.c
$(CC) $(CFLAGS) -o $@ $^ -lwiringPi

clean:
$(RM) uart_send .*.sw?
Binary file added SC161S-I2CtoSerial/c/uart/send/uart_send
Binary file not shown.
35 changes: 35 additions & 0 deletions SC161S-I2CtoSerial/c/uart/send/uart_send.c
@@ -0,0 +1,35 @@
#include <wiringSerial.h>
#include <wiringPi.h>
#include <stdio.h>
#include <strings.h>
#include <unistd.h>

#define UART_DEV1 "/dev/ttySC0"
#define UART_DEV2 "/dev/ttySC1"

int main(void)
{
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");
}

int fd;
if((fd = serialOpen (UART_DEV1, 115200)) < 0) {
printf("serial err\n");
return -1;
}

serialFlush(fd);
serialPrintf(fd,"\r");

char *buf = "abcdefgh";
serialPuts(fd, buf);

printf("send data: %s\r\n", buf);

serialClose(fd);
return 0;
}
19 changes: 19 additions & 0 deletions SC161S-I2CtoSerial/python/receive.py
@@ -0,0 +1,19 @@
# -*- coding:utf-8 -*-
import RPi.GPIO as GPIO
import serial
import time

ser = serial.Serial("/dev/ttySC1",115200,timeout=1)
# ser = serial.Serial("/dev/ttySC1",115200,timeout=1)
time.sleep(1)
ser.flushInput()

data = ""
while 1:
while ser.inWaiting() > 0:
data += ser.read(ser.inWaiting())
if data != "":
for i in range(len(data)):
print data[i],
print ""
data = ""

0 comments on commit 9bba581

Please sign in to comment.