lpcmanager

Parents e8f413523307
Children c9b717d8fc23
Finished UART device communication. Fixed missing least significant byte in 16-bit int ("analog values"). Changed communication with MUs so that only one is called each time UART task is unblocked.
  • +36 -25
    LPCBus.py
  • --- a/LPCBus.py Fri Dec 12 14:15:57 2014 +0100
    +++ b/LPCBus.py Fri Jan 09 13:15:03 2015 +0100
    @@ -48,12 +48,13 @@
    static inline int16_t getWord(char *buffer)
    {
    - return *((int16_t*)buffer);
    + return ((int16_t)*buffer<<8) + (int16_t)*(buffer+1);
    }
    static inline void setWord(char *buffer, int16_t word)
    {
    - *((int16_t*)buffer) = word;
    + *buffer = (char)(word>>8);
    + *(buffer+1) = (char)(word);
    }
    static inline int8_t getBit(char *buffer, unsigned char bitOffset)
    @@ -107,6 +108,7 @@
    "MC9:Right": {
    "decl" : """
    #include <rtdm/rtdm.h>
    +#include "beremiz.h"
    /* LPCBus confnode includes */
    #define RIGHT_READ_BUFSIZE 30 /**< Right bus read buffer size */
    @@ -150,6 +152,8 @@
    bzero(&rightWriteBuf, sizeof(rightWriteBuf_t));
    bzero(&rightBusInit, sizeof(rightBusInit_t));
    +rightBusInit.common_ticktime__ = common_ticktime__;
    +
    #define rightI2CMod (rightBusInit.rightI2CMod)
    %(init_code)s
    @@ -215,6 +219,8 @@
    #include <native/mutex.h>
    #include <native/timer.h>
    +#include "beremiz.h"
    +
    static struct termios oldterminfo;
    void closeserial(int fd)
    @@ -469,11 +475,9 @@
    static RT_MUTEX UART_WriteMutex;
    static RT_MUTEX UART_ReadMutex;
    -extern unsigned long long common_ticktime__;
    -
    void UART_task_proc(void *arg)
    {
    - int i;
    + static int i=0;
    while (rt_task_sleep_until(TM_INFINITE) == -EINTR){
    if(!rt_mutex_acquire(&UART_WriteMutex, TM_INFINITE )){
    if(uartDevWriteBuf_plc_state == FULL){
    @@ -487,8 +491,8 @@
    }
    /* Scan all positions for attached UART port modules */
    - for(i=0;i<MAX_UART_DEVICES;i++)
    - {
    +// for(i=0;i<MAX_UART_DEVICES;i++)
    +// {
    /* Timers for right modules */
    if(uartPortTim[i].status != TIM_DISABLED)
    {
    @@ -522,8 +526,12 @@
    else /* Keep timer disabled */
    uartPortTim[i].status = TIM_DISABLED;
    }
    +
    + i++;
    }
    - }
    + else i=0;
    +
    +// }
    if(!rt_mutex_acquire(&UART_ReadMutex, TM_INFINITE )){
    if(uartDevReadBuf_plc_state == EMPTY){
    @@ -589,6 +597,7 @@
    return FALSE;
    }
    +#define TAIL_LEN 3 /* Length of data tail in bytes */
    static int UART_fd;
    /*************************************************************************//**
    @@ -605,19 +614,19 @@
    int rv;
    RTIME now;
    int count = 0;
    + char tmp[UART_BUFSIZE];
    - // writeBuf[45] = address;
    - writeBuf[45] = 2;
    - char tmp[UART_BUFSIZE];
    - Checksum((unsigned char*)writeBuf+1);
    -
    - writeBuf[0] = 'S';
    + /* Prepare transmit buffer */
    + memcpy(tmp+1,writeBuf,UART_BUFSIZE-TAIL_LEN-1);
    + tmp[0] = 'S';
    + tmp[45] = address;
    + Checksum((unsigned char*)tmp+1);
    tcflush(UART_fd, TCIOFLUSH);
    now = rt_timer_read();
    - if(write(UART_fd, writeBuf, UART_BUFSIZE) != UART_BUFSIZE){
    + if(write(UART_fd, tmp, UART_BUFSIZE) != UART_BUFSIZE){
    goto UARTDevfail;
    }
    @@ -640,10 +649,10 @@
    rv = select(UART_fd + 1, &set, NULL, NULL, &timeout);
    if(rv == -1)
    - printf("select error\\n");
    + printf("RS485 select error\\n");
    else if(rv == 0)
    {
    - printf("timeout\\n");
    + //printf("timeout\\n");
    break;
    }
    else {
    @@ -651,24 +660,26 @@
    if(rr > 0){
    count += rr;
    }else{
    - printf("read error %%d\\n",rr);
    + printf("RS485 read error %%d\\n",rr);
    }
    }
    }
    // {
    // int i;
    - // for (i=0; i<UART_BUFSIZE; i++){
    - // printf("|%%c:%%d", tmp[i], tmp[i]);
    - // }
    - // printf("\\n");
    +// printf("tmp:\\n");
    +// for (i=0; i<UART_BUFSIZE; i++){
    +// printf("|%%c:%%d", tmp[i], tmp[i]);
    +// }
    +// printf("\\n");
    // }
    /* Turn to transmit mode*/
    TransmitMode(UART_fd);
    + /* Copy received buffer */
    if(count == UART_BUFSIZE){
    - memcpy(readBuf,tmp+1,UART_BUFSIZE-1);
    + memcpy(readBuf,tmp+1,UART_BUFSIZE-TAIL_LEN-1);
    return;
    }
    @@ -688,8 +699,8 @@
    switch(uartDev[i][1])
    {
    case(2): /* MU1 */
    - case(4):
    - case(6):
    + case(4):
    + case(6):
    case(8):
    case(10):
    case(12):