lpcmanager

Parents 21fe998eea17
Children 0df2fad481ca
Devices Bus : Removed explicit RTS pin toggling as GPIO, now that kernel side RS485 driver handles it. Also changed UART task sleep/unblock into suspend/resume, since it looks unreliable in xenomai3
--- a/LPCBus/MC9_Devices_cleanup.c Mon May 07 14:02:52 2018 +0200
+++ b/LPCBus/MC9_Devices_cleanup.c Wed May 16 10:41:17 2018 +0200
@@ -1,6 +1,8 @@
+
+UART_task_active = 0;
+rt_task_resume(&UART_task);
rt_task_delete(&UART_task);
rt_task_join(&UART_task);
closeserial(UART_fd);
-gpio_close(RS485_GPIO_dev);
rt_mutex_delete(&UART_WriteMutex);
rt_mutex_delete(&UART_ReadMutex);
--- a/LPCBus/MC9_Devices_decl.c Mon May 07 14:02:52 2018 +0200
+++ b/LPCBus/MC9_Devices_decl.c Wed May 16 10:41:17 2018 +0200
@@ -20,146 +20,6 @@
perror("closeserial()");
}
-/*------------------------- GPIO -------------------------------------*/
-/* from armadeus/target/packages/as_devices/c/as_gpio* */
-
-//#ifdef DEBUG
-# define ERROR(fmt, ...) printf(fmt, ##__VA_ARGS__)
-//#else
-//# define ERROR(fmt, ...) /*fmt, ##__VA_ARGS__*/
-//#endif
-
-struct gpio_device {
- int port_num;
- int pin_file; /* pin file for 2.6.29 interface*/
-};
-
-struct gpio_device *RS485_GPIO_dev;
-
-static int write_file_bool(int fd, int value)
-{
- int ret;
-
- ret = write(fd, value?"1":"0", 1);
- if (ret < 0) {
- ERROR("write error\n");
- return ret;
- }
- if (lseek(fd, 0, SEEK_SET) < 0) {
- ERROR("lseek error\n");
- return ret;
- }
-
- return ret;
-}
-
-#define BUFF_SIZE 256
-static struct gpio_device *gpio_open(int aGpioNum)
-{
- struct gpio_device *dev;
- int pin_file;
- int export_file;
- int gpio_dir_fd;
- int retval;
- char buf[BUFF_SIZE];
- int ret = 0;
-
- export_file = open("/sys/class/gpio/export", O_WRONLY);
- if (export_file < 0) {
- ERROR("Can't open /sys/class/gpio/export\nBe sure that gpiolib is under your kernel\n");
- return NULL;
- }
-
- snprintf(buf, BUFF_SIZE, "%%d", aGpioNum);
- retval = write(export_file, buf, strlen(buf));
- close(export_file);
- if (retval < 0) {
- ERROR("/sys/class/gpio/export can't be written\n");
- return NULL;
- }
-
- snprintf(buf, BUFF_SIZE, "/sys/class/gpio/gpio%%d/direction", aGpioNum);
- gpio_dir_fd = open(buf, O_WRONLY);
- if (gpio_dir_fd < 0) {
- ERROR("Can't open gpio%%d direction\n", aGpioNum);
- return NULL;
- }
-
- ret = write(gpio_dir_fd, "out", 3);
- close(gpio_dir_fd);
- if (ret < 0){
- ERROR("Error writing direction\n");
- return NULL;
- }
-
- snprintf(buf, BUFF_SIZE, "/sys/class/gpio/gpio%%d/value", aGpioNum);
- pin_file = open(buf, O_RDWR);
- if (pin_file < 0) {
- ERROR("Can't export gpio number %%d\n", aGpioNum);
- return NULL;
- }
-
- dev = malloc(sizeof(struct gpio_device));
- if (dev == NULL) {
- ERROR("Can't allocate gpio_device structure\n");
- close(pin_file);
- return NULL;
- }
-
- dev->port_num = aGpioNum;
- dev->pin_file = pin_file;
-
- return dev;
-}
-
-static int gpio_close(struct gpio_device *aDev)
-{
- int unexport_file;
- char buf[BUFF_SIZE];
- int retval;
-
- if(aDev == NULL){
- ERROR("device is NULL\n");
- return -1;
- }
-
- 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");
- return -1;
- }
-
- snprintf(buf, BUFF_SIZE, "%%d", aDev->port_num);
- retval = write(unexport_file, buf, strlen(buf));
- close(unexport_file);
- if (retval < 0) {
- ERROR("/sys/class/gpio/unexport can't be written\n");
- return -1;
- }
-
- close(aDev->pin_file);
-
- return 0;
-}
-
-static int gpio_set_pin_value(struct gpio_device *aDev, int aValue)
-{
- int pin_file = aDev->pin_file;
- int retval;
-
- retval = write_file_bool(pin_file, aValue);
- if (retval < 0) {
- ERROR("Can't write value\n");
- close(pin_file);
- return -1;
- }
-
- return aValue;
-}
-
-#define TransmitMode() gpio_set_pin_value(RS485_GPIO_dev, 1)
-#define RecieveMode() gpio_set_pin_value(RS485_GPIO_dev, 0)
-
/*--------------------------- Serial Port handling ---------------------------*/
int openserial(char *devicename, unsigned long baudrate)
@@ -210,8 +70,6 @@
return 0;
}
- TransmitMode();
-
return fd;
}
@@ -270,6 +128,7 @@
static RT_TASK UART_task;
static RT_MUTEX UART_WriteMutex;
static RT_MUTEX UART_ReadMutex;
+static int UART_task_active;
void UART_task_proc(void *arg)
{
@@ -278,7 +137,11 @@
uint64_t actTime;
static uint64_t lastCommTime=0;
- while (rt_task_sleep_until(TM_INFINITE) == -EINTR){
+ while (UART_task_active){
+ rt_task_suspend(NULL);
+ if(!UART_task_active)
+ break;
+
// Communicate only with initialised UART devices
if(uartPortDevices[i] != NULL)
{
@@ -443,7 +306,6 @@
fd_set set;
struct timeval timeout;
int rv;
- RTIME now;
int count = 0;
char tmp[uartBufSize];
@@ -455,21 +317,10 @@
tcflush(UART_fd, TCIOFLUSH);
- now = rt_timer_read();
-
if(write(UART_fd, tmp, uartBufSize) != uartBufSize){
goto UARTDevfail;
}
- /* Sleep until transmission completes + 0.5ms safety */
- while(rt_task_sleep_until(
- now + rt_timer_ns2ticks(
- 1000000000LL * uartBufSize * 10 / uartBaudrate
- + 500000)) == -EINTR);
-
- /* Turn to listen mode*/
- RecieveMode();
-
FD_ZERO(&set); /* clear the set */
FD_SET(UART_fd, &set); /* add our file descriptor to the set */
@@ -495,9 +346,6 @@
}
}
- /* Turn to transmit mode*/
- TransmitMode();
-
/* Copy received buffer */
if((count == uartBufSize) && (tmp[uartBufSize-TAIL_LEN] == address)){
if(ChecksumValid((unsigned char*)tmp+1, uartBufSize)){
--- a/LPCBus/MC9_Devices_init.c Mon May 07 14:02:52 2018 +0200
+++ b/LPCBus/MC9_Devices_init.c Wed May 16 10:41:17 2018 +0200
@@ -1,6 +1,5 @@
int err;
char *serialdev = "/dev/ttyAPP1";
-int RS485_GPIO_NUM = 102;
uartDevWriteBuf_plc_state = EMPTY;
uartDevReadBuf_plc_state = EMPTY;
@@ -27,12 +26,6 @@
if((err = rt_mutex_create (&UART_ReadMutex, "UART_ReadMutex")))
return err;
-RS485_GPIO_dev = gpio_open(RS485_GPIO_NUM);
-if (!RS485_GPIO_dev) {
- fprintf(stderr, "Error while initializing RS485 GPIO.\n");
- return 1;
-}
-
UART_fd = openserial(serialdev, uartBaudrate); /* uartBaudrate is initialized from Composer */
if (!UART_fd) {
fprintf(stderr, "Error while initializing %%s.\n", serialdev);
@@ -44,7 +37,9 @@
else
InitUartPortDevices_shortBuffer();
-if((err = rt_task_create(&UART_task, "UART_task", 0, 50, T_JOINABLE)))
+UART_task_active = 1;
+
+if((err = rt_task_create(&UART_task, "UART_task", 0, 0, T_JOINABLE)))
return err;
if((err = rt_task_start(&UART_task, &UART_task_proc, NULL)))
--- a/LPCBus/MC9_Devices_publish.c Mon May 07 14:02:52 2018 +0200
+++ b/LPCBus/MC9_Devices_publish.c Wed May 16 10:41:17 2018 +0200
@@ -12,5 +12,5 @@
uartDevWriteBuf_plc_state = FULL;
/* wakeup task */
- rt_task_unblock(&UART_task);
+ rt_task_resume(&UART_task);
}