lpcmanager

Parents 71b964068f20
Children 3c7f675fa8dd
Fixed RightBus and RS485 timers erratic behaviour when PLC tick time == polling time
  • +37 -21
    LPCBus.py
  • --- a/LPCBus.py Thu Dec 11 18:35:34 2014 +0100
    +++ b/LPCBus.py Fri Dec 12 14:15:57 2014 +0100
    @@ -101,6 +101,9 @@
    """}
    bus_code = {
    +###############################################################################
    +######################## Right Bus I2C (RT) #########################
    +###############################################################################
    "MC9:Right": {
    "decl" : """
    #include <rtdm/rtdm.h>
    @@ -120,17 +123,21 @@
    /**< Tables containing information about connected modules
    on right bus (initialized by Composer) */
    typedef char rightI2CMod_t[MAX_RIGHT_MODULES][2];
    +typedef struct {
    + rightI2CMod_t rightI2CMod;
    + unsigned long long common_ticktime__;
    +} rightBusInit_t;
    #define RTIOC_TYPE_SMT RTDM_CLASS_EXPERIMENTAL
    -#define RTSMT_RTIOC_INIT _IOR(RTIOC_TYPE_SMT, 0x00, rightI2CMod_t)
    +#define RTSMT_RTIOC_INIT _IOR(RTIOC_TYPE_SMT, 0x00, rightBusInit_t)
    //#define RTSMT_RTIOC_CLEAN _IOR(RTIOC_TYPE_SMT, 0x01, int)
    #define RTSMT_RTIOC_READ _IOR(RTIOC_TYPE_SMT, 0x02, rightReadBuf_t)
    #define RTSMT_RTIOC_WRITE _IOR(RTIOC_TYPE_SMT, 0x03, rightWriteBuf_t)
    static rightReadBuf_t rightReadBuf;
    static rightWriteBuf_t rightWriteBuf;
    -static rightI2CMod_t rightI2CMod;
    +static rightBusInit_t rightBusInit;
    static int rightbusfd = -1;
    @@ -141,8 +148,9 @@
    bzero(&rightReadBuf, sizeof(rightReadBuf_t));
    bzero(&rightWriteBuf, sizeof(rightWriteBuf_t));
    -bzero(&rightI2CMod, sizeof(rightI2CMod_t));
    +bzero(&rightBusInit, sizeof(rightBusInit_t));
    +#define rightI2CMod (rightBusInit.rightI2CMod)
    %(init_code)s
    int err;
    @@ -153,7 +161,7 @@
    return rightbusfd;
    }
    -err = rt_dev_ioctl(rightbusfd, RTSMT_RTIOC_INIT, &rightI2CMod);
    +err = rt_dev_ioctl(rightbusfd, RTSMT_RTIOC_INIT, &rightBusInit);
    if (err) {
    printf("error while RTSMT_RTIOC_INIT, %%s\\n",
    strerror(-err));
    @@ -173,6 +181,9 @@
    rt_dev_close(rightbusfd);
    """,
    },
    +###############################################################################
    +######################## OnBoard devices #########################
    +###############################################################################
    "MC9:On Board": {
    "decl" : """
    #define MAX_ONBOARD_DEVICES 2
    @@ -188,6 +199,9 @@
    "publish":"%(publish_code)s",
    "cleanup":"",
    },
    +###############################################################################
    +######################## RS 485 non real time #########################
    +###############################################################################
    "MC9:Devices": {
    "decl" : """
    #include <stdio.h>
    @@ -357,7 +371,7 @@
    return gpio_set_pin_value(RS485_GPIO_dev, 0);
    }
    -/*------------------------------------------------------------------------------*/
    +/*--------------------------- Serial Port handling ---------------------------*/
    int openserial(char *devicename)
    {
    @@ -403,8 +417,8 @@
    typedef struct /* Type definition for timers for right modules */
    {
    unsigned char status; /* Current status of timer - running / expired */
    - RTIME actValue; /* Actual timer value */
    - RTIME toValue; /* Timeout value - initialized at startup */
    + long long actValue; /* Actual timer value */
    + long long toValue; /* Timeout value - initialized at startup */
    } commTimer;
    #define TIM_DISABLED 0
    @@ -455,6 +469,8 @@
    static RT_MUTEX UART_WriteMutex;
    static RT_MUTEX UART_ReadMutex;
    +extern unsigned long long common_ticktime__;
    +
    void UART_task_proc(void *arg)
    {
    int i;
    @@ -473,11 +489,11 @@
    /* Scan all positions for attached UART port modules */
    for(i=0;i<MAX_UART_DEVICES;i++)
    {
    - RTIME now = rt_timer_read();
    /* Timers for right modules */
    if(uartPortTim[i].status != TIM_DISABLED)
    {
    - if(now - uartPortTim[i].actValue < uartPortTim[i].toValue)
    + uartPortTim[i].actValue += common_ticktime__;
    + if(uartPortTim[i].actValue < uartPortTim[i].toValue)
    {
    uartPortTim[i].status = TIM_EN_RUNNING;
    }
    @@ -500,7 +516,7 @@
    // otherwise keep it disabled
    if(uartPortTim[i].status != TIM_DISABLED)
    {
    - uartPortTim[i].actValue = now;
    + uartPortTim[i].actValue = 0;
    uartPortTim[i].status = TIM_EN_RUNNING;
    }
    else /* Keep timer disabled */
    @@ -606,10 +622,10 @@
    }
    /* Sleep until transmission completes + 0.5ms safety */
    - rt_task_sleep_until(
    - now + rt_timer_ns2ticks(
    - 1000000000LL * UART_BUFSIZE * 10 / 115200
    - + 500000));
    + while(rt_task_sleep_until(
    + now + rt_timer_ns2ticks(
    + 1000000000LL * UART_BUFSIZE * 10 / 115200
    + + 500000)) == -EINTR);
    /* Turn to listen mode*/
    RecieveMode(UART_fd);
    @@ -660,8 +676,8 @@
    tcflush(UART_fd, TCIOFLUSH);
    }
    -/* Macro to transform milliseconds to xenomai ticks */
    -#define msTOxn(ms) rt_timer_ns2ticks(1000000L*ms)
    +/* Macro to transform milliseconds to ns */
    +#define msTOns(ms) (1000000L*ms)
    void InitUartPortDevices(void)
    {
    @@ -704,7 +720,7 @@
    case(78):
    case(80):
    uartPortDevices[i] = &UARTDevice;
    - uartPortTim[i].toValue = msTOxn(50); /* 50ms */
    + uartPortTim[i].toValue = msTOns(50); /* 50ms */
    uartPortTim[i].status = TIM_EN_RUNNING;
    break;
    case(130): /* P01, P02, P01V, P02V */
    @@ -728,7 +744,7 @@
    case(190):
    case(192):
    uartPortDevices[i] = &UARTDevice;
    - uartPortTim[i].toValue = msTOxn(200); /* 200ms */
    + uartPortTim[i].toValue = msTOns(200); /* 200ms */
    uartPortTim[i].status = TIM_EN_RUNNING;
    break;
    case(146): /* CH1 */
    @@ -736,7 +752,7 @@
    case(150):
    case(152):
    uartPortDevices[i] = &UARTDevice;
    - uartPortTim[i].toValue = msTOxn(600); /* 600ms */
    + uartPortTim[i].toValue = msTOns(600); /* 600ms */
    uartPortTim[i].status = TIM_EN_RUNNING;
    break;
    case(170): /* ID1, ID2, ID3 */
    @@ -744,7 +760,7 @@
    case(174):
    case(176):
    uartPortDevices[i] = &UARTDevice;
    - uartPortTim[i].toValue = msTOxn(400); /* 400ms */
    + uartPortTim[i].toValue = msTOns(400); /* 400ms */
    uartPortTim[i].status = TIM_EN_RUNNING;
    break;
    case(194): /* DP1V */
    @@ -764,7 +780,7 @@
    case(222):
    case(224):
    uartPortDevices[i] = &UARTDevice;
    - uartPortTim[i].toValue = msTOxn(400); /* 400ms */
    + uartPortTim[i].toValue = msTOns(400); /* 400ms */
    uartPortTim[i].status = TIM_EN_RUNNING;
    break;