--- a/LPCBus.py Wed Dec 10 18:32:59 2014 +0100
+++ b/LPCBus.py Thu Dec 11 17:54:34 2014 +0100
@@ -210,26 +210,155 @@
+/*------------------------- GPIO -------------------------------------*/ +/* from armadeus/target/packages/as_devices/c/as_gpio* */ +# define ERROR(fmt, ...) printf(fmt, ##__VA_ARGS__) +//# define ERROR(fmt, ...) /*fmt, ##__VA_ARGS__*/ + int pin_file; /* pin file for 2.6.29 interface*/ +struct gpio_device *RS485_GPIO_dev; +int write_file_bool(int fd, int value)
- if (ioctl(fd, TIOCMBIS, &bit) == -1) {
- perror("setRTS(): TIOCMGET");
+ ret = write(fd, value?"1":"0", 1); + ERROR("write error\\n");
+ if (lseek(fd, 0, SEEK_SET) < 0) { + ERROR("lseek error\\n");
+struct gpio_device *gpio_open(int aGpioNum)
- if (ioctl(fd, TIOCMBIC, &bit) == -1) {
- perror("clearRTS(): TIOCMGET");
+ struct gpio_device *dev; + export_file = open("/sys/class/gpio/export", O_WRONLY); + ERROR("Can't open /sys/class/gpio/export\\nBe sure that gpiolib is under your kernel\\n"); + snprintf(buf, BUFF_SIZE, "%%d", aGpioNum); + retval = write(export_file, buf, strlen(buf)); + ERROR("/sys/class/gpio/export can't be written\\n"); + snprintf(buf, BUFF_SIZE, "/sys/class/gpio/gpio%%d/direction", aGpioNum); + gpio_dir_fd = open(buf, O_WRONLY); + ERROR("Can't open gpio%%d direction\\n", aGpioNum); + ret = write(gpio_dir_fd, "out", 3); + ERROR("Error writing direction\\n"); + snprintf(buf, BUFF_SIZE, "/sys/class/gpio/gpio%%d/value", aGpioNum); + pin_file = open(buf, O_RDWR); + ERROR("Can't export gpio number %%d\\n", aGpioNum); + dev = malloc(sizeof(struct gpio_device)); + ERROR("Can't allocate gpio_device structure\\n");
+ dev->port_num = aGpioNum; + dev->pin_file = pin_file; +int gpio_close(struct gpio_device *aDev) + ERROR("device is NULL\\n"); + unexport_file = open("/sys/class/gpio/unexport", O_WRONLY); + if (unexport_file < 0) { + ERROR("Can't open /sys/class/gpio/unexport\\nBe sure that gpiolib is under your kernel\\n"); + snprintf(buf, BUFF_SIZE, "%%d", aDev->port_num); + retval = write(unexport_file, buf, strlen(buf)); + ERROR("/sys/class/gpio/unexport can't be written\\n"); +int gpio_set_pin_value(struct gpio_device *aDev, int aValue) + int pin_file = aDev->pin_file; + retval = write_file_bool(pin_file, aValue); + ERROR("Can't write value\\n"); +int TransmitMode(int fd) + return gpio_set_pin_value(RS485_GPIO_dev, 1); + return gpio_set_pin_value(RS485_GPIO_dev, 0); +/*------------------------------------------------------------------------------*/ int openserial(char *devicename)
@@ -246,16 +375,17 @@
- // attr.c_cflag |= CRTSCTS | CLOCAL;
- attr.c_cflag &= ~CRTSCTS;
- attr.c_cflag &= ~PARENB; /* no parity */
- attr.c_cflag &= ~CSTOPB; /* 1 stop bit */
- attr.c_cflag &= ~CSIZE;
attr.c_cflag |= CS8 ; /* 8 bits */
+ /* no parity, 1 stop bit */ - cfsetospeed(&attr, baud); /* baud rate */
+ cfsetspeed(&attr, baud); /* baud rate */ if (tcflush(fd, TCIOFLUSH) == -1) {
perror("openserial(): tcflush()");
@@ -265,8 +395,7 @@
- /* Turn to write mode*/
@@ -330,7 +459,6 @@
while (rt_task_sleep_until(TM_INFINITE) == -EINTR){
- printf("Hellllo world\\n");
if(!rt_mutex_acquire(&UART_WriteMutex, TM_INFINITE )){
if(uartDevWriteBuf_plc_state == FULL){
uartDevWriteBuf_t *uartDevWriteBuf_tmp;
@@ -459,6 +587,8 @@
// writeBuf[45] = address;
@@ -466,41 +596,66 @@
Checksum((unsigned char*)writeBuf+1);
- // for (i=0; i<UART_BUFSIZE; i++){
- // printf("|%%c:%%d", writeBuf[i], writeBuf[i]);
- //rt_task_sleep(rt_timer_ns2ticks(500000));
+ tcflush(UART_fd, TCIOFLUSH); if(write(UART_fd, writeBuf, UART_BUFSIZE) != UART_BUFSIZE){
- FD_ZERO(&set); /* clear the set */
- FD_SET(UART_fd, &set); /* add our file descriptor to the set */
- timeout.tv_usec = 50000;
+ /* Sleep until transmission completes + 0.5ms safety */ + now + rt_timer_ns2ticks( + 1000000000LL * UART_BUFSIZE * 10 / 115200
+ while(count < UART_BUFSIZE){ - rv = select(UART_fd + 1, &set, NULL, NULL, &timeout);
- perror("select"); /* an error accured */
- printf("timeout"); /* a timeout occured */
- else if(read(UART_fd, tmp, UART_BUFSIZE) == UART_BUFSIZE){
- memcpy(readBuf,tmp,UART_BUFSIZE);
+ FD_ZERO(&set); /* clear the set */ + FD_SET(UART_fd, &set); /* add our file descriptor to the set */ + timeout.tv_usec = 50000; + rv = select(UART_fd + 1, &set, NULL, NULL, &timeout); + printf("select error\\n"); + int rr = read(UART_fd, tmp + count, UART_BUFSIZE - count); + printf("read error %%d\\n",rr); + // for (i=0; i<UART_BUFSIZE; i++){ + // printf("|%%c:%%d", tmp[i], tmp[i]); + /* Turn to transmit mode*/ + if(count == UART_BUFSIZE){ + memcpy(readBuf,tmp+1,UART_BUFSIZE-1); - /* Turn to transmit mode*/
tcflush(UART_fd, TCIOFLUSH);
@@ -589,6 +744,7 @@
char *serialdev = "/dev/ttyAPP1";
+int RS485_GPIO_NUM = 103; uartDevWriteBuf_plc_state = EMPTY;
uartDevReadBuf_plc_state = EMPTY;
@@ -612,9 +768,13 @@
if((err = rt_mutex_create (&UART_ReadMutex, "UART_ReadMutex")))
+RS485_GPIO_dev = gpio_open(RS485_GPIO_NUM); + fprintf(stderr, "Error while initializing RS485 GPIO.\\n"); UART_fd = openserial(serialdev);
fprintf(stderr, "Error while initializing %%s.\\n", serialdev);
@@ -672,6 +832,7 @@
rt_task_delete(&UART_task);
rt_task_join(&UART_task);
+gpio_close(RS485_GPIO_dev); rt_mutex_delete(&UART_WriteMutex);
rt_mutex_delete(&UART_ReadMutex);