lpcmanager

24d3d28deee7
Parents 0b384140bc8b
Children 998c52331037
Implemented double-buffering C code for RS485 bus
  • +163 -4
    LPCBus.py
  • --- a/LPCBus.py Mon Dec 08 17:33:10 2014 +0100
    +++ b/LPCBus.py Tue Dec 09 17:27:37 2014 +0100
    @@ -80,7 +80,8 @@
    /* LPCBus confnode functions */
    int __init_%(location_str)s(int argc,char **argv)
    {
    -%(init_code)s
    + %(buffer_init)s
    + %(init_code)s
    %(bus_init_code)s
    return 0;
    }
    @@ -139,6 +140,11 @@
    /* XXX TODO #include "smarteh.h" */
    """,
    + "buffer_init":"""
    +bzero(&rightReadBuf, sizeof(rightReadBuf_t));
    +bzero(&rightWriteBuf, sizeof(rightWriteBuf_t));
    +bzero(&rightI2CMod, sizeof(rightI2CMod_t));
    +""",
    "init":"""
    #define DEVICEFILENAME "rightbus"
    @@ -171,7 +177,12 @@
    "MC9:On Board": {
    "decl" : """
    #define MAX_ONBOARD_DEVICES 2
    -unsigned char onBoardDev[MAX_ONBOARD_DEVICES][2]; /**< Tables containing information about enabled on-board devices (initialized by Composer) */
    +/* Tables containing information about enabled on-board devices
    + (initialized by Composer) */
    +unsigned char onBoardDev[MAX_ONBOARD_DEVICES][2];
    +""",
    + "buffer_init":"""
    +bzero(&onBoardDev, sizeof(onBoardDev));
    """,
    "init":"""
    """,
    @@ -184,16 +195,162 @@
    },
    "MC9:Devices": {
    "decl" : """
    -#define MAX_UART_DEVICES 32
    -unsigned char uartDev[MAX_UART_DEVICES][2]; /**< Tables containing information about connected devices on UART port (initialized by Composer) */
    +#include <native/task.h>
    +#include <native/mutex.h>
    +
    +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 */
    +} commTimer;
    +#define TIM_DISABLED 0
    +#define TIM_EN_RUNNING 1
    +#define TIM_EN_EXPIRED 2
    +
    +#define EMPTY 0
    +#define LOCKED 1
    +#define FULL 2
    +
    +#define MAX_UART_DEVICES 52
    +/* UART bus read and write buffer size */
    +#define UART_BUFSIZE 48
    +
    +/* Tables containing information about connected devices on UART port
    + (initialized by Composer) */
    +unsigned char uartDev[MAX_UART_DEVICES][2];
    +
    +/* Buffers for reading data from UART port devices */
    +typedef char uartDevReadBuf_t[MAX_UART_DEVICES][UART_BUFSIZE];
    +uartDevReadBuf_t uartDevReadBufA;
    +uartDevReadBuf_t uartDevReadBufB;
    +uartDevReadBuf_t *uartDevReadBuf_drv;
    +uartDevReadBuf_t *uartDevReadBuf_plc;
    +/* MC8 compatibility crap */
    +#define uartDevReadBuf (*uartDevReadBuf_drv)
    +
    +/* Buffers for writing data to UART port devices */
    +typedef char uartDevWriteBuf_t[MAX_UART_DEVICES][UART_BUFSIZE];
    +uartDevWriteBuf_t uartDevWriteBufA;
    +uartDevWriteBuf_t uartDevWriteBufB;
    +uartDevWriteBuf_t *uartDevWriteBuf_drv;
    +uartDevWriteBuf_t *uartDevWriteBuf_plc;
    +/* MC8 compatibility crap */
    +#define uartDevWriteBuf (*uartDevWriteBuf_drv)
    +
    +int uartDevWriteBuf_plc_state;
    +int uartDevReadBuf_plc_state;
    +
    +static RT_TASK UART_task;
    +static RT_MUTEX UART_WriteMutex;
    +static RT_MUTEX UART_ReadMutex;
    +
    +void UART_task_proc(void *arg)
    +{
    + while (rt_task_sleep_until(TM_INFINITE) == -EINTR){
    + if(!rt_mutex_acquire(&UART_WriteMutex, TM_INFINITE )){
    + if(uartDevWriteBuf_plc_state == FULL){
    + uartDevWriteBuf_t *uartDevWriteBuf_tmp;
    + uartDevWriteBuf_tmp = uartDevWriteBuf_plc;
    + uartDevWriteBuf_plc = uartDevWriteBuf_drv;
    + uartDevWriteBuf_drv = uartDevWriteBuf_tmp;
    + uartDevWriteBuf_plc_state = EMPTY;
    + }
    + rt_mutex_release(&UART_WriteMutex);
    + }
    +
    + /* XXX use data from uartDevWriteBuf_drv */
    +
    + /* XXX write data to uartDevReadBuf_drv */
    +
    + if(!rt_mutex_acquire(&UART_ReadMutex, TM_INFINITE )){
    + if(uartDevReadBuf_plc_state == EMPTY){
    + uartDevReadBuf_t *uartDevReadBuf_tmp;
    + uartDevReadBuf_tmp = uartDevReadBuf_plc;
    + uartDevReadBuf_plc = uartDevReadBuf_drv;
    + uartDevReadBuf_drv = uartDevReadBuf_tmp;
    + uartDevReadBuf_plc_state = FULL;
    + }
    + rt_mutex_release(&UART_ReadMutex);
    + }
    + }
    +}
    +""",
    + "buffer_init":"""
    +uartDevWriteBuf_plc_state = EMPTY;
    +uartDevReadBuf_plc_state = EMPTY;
    +bzero(&uartDev, sizeof(uartDev));
    +
    +bzero(&uartDevReadBufA, sizeof(uartDevReadBuf_t));
    +bzero(&uartDevReadBufB, sizeof(uartDevReadBuf_t));
    +uartDevReadBuf_drv = &uartDevReadBufA;
    +uartDevReadBuf_plc = &uartDevReadBufB;
    +
    +bzero(&uartDevWriteBufA, sizeof(uartDevWriteBuf_t));
    +bzero(&uartDevWriteBufB, sizeof(uartDevWriteBuf_t));
    +uartDevWriteBuf_drv = &uartDevWriteBufA;
    +uartDevWriteBuf_plc = &uartDevWriteBufB;
    """,
    "init":"""
    +int err;
    +
    +if((err = rt_mutex_create (&UART_WriteMutex, "UART_WriteMutex")))
    + return err;
    +
    +if((err = rt_mutex_create (&UART_ReadMutex, "UART_ReadMutex")))
    + return err;
    +
    +if((err = rt_task_create(&UART_task, "UART_task", 0, 50, T_JOINABLE)))
    + return err;
    +
    +if(rt_task_start(&UART_task, &UART_task_proc, NULL))
    + return err;
    +
    """,
    "retrieve":"""
    +if(!rt_mutex_acquire(&UART_ReadMutex, TM_INFINITE )){
    +
    + int prevstate;
    + if((prevstate=uartDevReadBuf_plc_state) == FULL){
    + uartDevReadBuf_plc_state = LOCKED;
    + }
    +
    + rt_mutex_release(&UART_ReadMutex);
    +
    + if(prevstate == FULL){
    +
    + /* XXX use uartDevReadBuf_plc */
    +
    + /* unlock plc buffer */
    + uartDevReadBuf_plc_state = EMPTY;
    + }else{
    + /* No new data -> no update */
    + }
    +}
    """,
    "publish":"""
    +if(!rt_mutex_acquire(&UART_WriteMutex, TM_INFINITE )){
    +
    + int prevstate;
    + prevstate=uartDevWriteBuf_plc_state;
    + uartDevWriteBuf_plc_state = LOCKED;
    +
    + rt_mutex_release(&UART_WriteMutex);
    +
    + /* XXX use uartDevWriteBuf_plc */
    +
    + /* unlock plc buffer */
    + uartDevWriteBuf_plc_state = FULL;
    +
    + /* wakeup task */
    + rt_task_unblock(&UART_task);
    +}
    """,
    "cleanup":"""
    +rt_task_delete(&UART_task);
    +rt_task_join(&UART_task);
    +rt_mutex_delete(&UART_WriteMutex);
    +rt_mutex_delete(&UART_ReadMutex);
    """,
    },
    }
    @@ -387,6 +544,7 @@
    BusName = LPCarch + ":" + self.BaseParams.getName()
    bcode = bus_code.get(BusName,{"decl":"",
    + "buffer_init":"",
    "init":"",
    "retrieve":"",
    "publish":"",
    @@ -399,6 +557,7 @@
    "retrieve_code": "",
    "publish_code": "",
    "bus_decl":bcode["decl"],
    + "buffer_init": bcode["buffer_init"],
    "bus_init_code": bcode["init"],
    "bus_cleanup_code": bcode["cleanup"],
    "bus_retrieve_code": bcode["retrieve"],