lpcmanager

Parents 3c7f675fa8dd
Children e584b0c04d6c
Splitted C code contained in LPCBus.py into multiple C files in LPCBus directory
--- a/LPCBus.py Fri Jan 09 13:15:03 2015 +0100
+++ b/LPCBus.py Thu Jan 22 18:12:42 2015 +0100
@@ -1,907 +1,17 @@
-
-bus_template_code = {"MC8":"""
-/* Code generated by LPCBus confnode */
-
-/* LPCBus confnode includes */
-#include "app_glue.h"
-#ifdef _WINDOWS_H
- #include "iec_types.h"
-#else
- #include "iec_std_lib.h"
-#endif
-
-%(declare_code)s
-
-/* LPCBus confnode user variables definition */
-%(var_decl)s
-
-/* LPCBus confnode functions */
-int __init_%(location_str)s(int argc,char **argv)
-{
-%(init_code)s
- return 0;
-}
-
-void __cleanup_%(location_str)s(void)
-{
-}
-
-void __retrieve_%(location_str)s(void)
-{
-%(retrieve_code)s
-}
-
-void __publish_%(location_str)s(void)
-{
-%(publish_code)s
-}
-""",
-#############################
-"MC9":"""
-/* Code generated by LPCBus confnode */
-
-#ifdef _WINDOWS_H
- #include "iec_types.h"
-#else
- #include "iec_std_lib.h"
-#endif
-
-static inline int16_t getWord(char *buffer)
-{
- return ((int16_t)*buffer<<8) + (int16_t)*(buffer+1);
-}
-
-static inline void setWord(char *buffer, int16_t word)
-{
- *buffer = (char)(word>>8);
- *(buffer+1) = (char)(word);
-}
-
-static inline int8_t getBit(char *buffer, unsigned char bitOffset)
-{
- return (*buffer & (0x01 << bitOffset)) && 1;
-}
-
-static inline void setBit(char *buffer, unsigned char bitOffset, int8_t bit)
-{
- unsigned char msk = (0x01 << bitOffset);
- if(bit)
- *buffer |= msk;
- else
- *buffer &= ~msk;
-}
-
-%(bus_decl)s
-
-%(declare_code)s
-
-/* LPCBus confnode user variables definition */
-%(var_decl)s
-
-/* LPCBus confnode functions */
-int __init_%(location_str)s(int argc,char **argv)
-{
- %(bus_init_code)s
- return 0;
-}
-
-void __cleanup_%(location_str)s(void)
-{
- %(bus_cleanup_code)s
-}
-
-void __retrieve_%(location_str)s(void)
-{
-%(bus_retrieve_code)s
-}
-
-void __publish_%(location_str)s(void)
-{
-%(bus_publish_code)s
-}
-"""}
-
-bus_code = {
-###############################################################################
-######################## Right Bus I2C (RT) #########################
-###############################################################################
- "MC9:Right": {
- "decl" : """
-#include <rtdm/rtdm.h>
-#include "beremiz.h"
-
-/* LPCBus confnode includes */
-#define RIGHT_READ_BUFSIZE 30 /**< Right bus read buffer size */
-#define RIGHT_WRITE_BUFSIZE 30 /**< Right bus write buffer size */
-
-#define MAX_RIGHT_MODULES 8
-
-/**< Buffers for reading data from right bus modules */
-typedef char rightReadBuf_t[MAX_RIGHT_MODULES][RIGHT_READ_BUFSIZE];
-
-/**< Buffers for writing data to right bus modules */
-typedef char rightWriteBuf_t[MAX_RIGHT_MODULES][RIGHT_WRITE_BUFSIZE];
-
-/**< 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, 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 rightBusInit_t rightBusInit;
-
-static int rightbusfd = -1;
-
-/* XXX TODO #include "smarteh.h" */
-""",
- "init":"""
-#define DEVICEFILENAME "rightbus"
-
-bzero(&rightReadBuf, sizeof(rightReadBuf_t));
-bzero(&rightWriteBuf, sizeof(rightWriteBuf_t));
-bzero(&rightBusInit, sizeof(rightBusInit_t));
-
-rightBusInit.common_ticktime__ = common_ticktime__;
-
-#define rightI2CMod (rightBusInit.rightI2CMod)
-%(init_code)s
-
-int err;
-rightbusfd = rt_dev_open( DEVICEFILENAME, 0);
-if (rightbusfd < 0) {
- printf("can't open %%s rtdm device, %%s\\n", DEVICEFILENAME,
- strerror(-rightbusfd));
- return rightbusfd;
-}
-
-err = rt_dev_ioctl(rightbusfd, RTSMT_RTIOC_INIT, &rightBusInit);
-if (err) {
- printf("error while RTSMT_RTIOC_INIT, %%s\\n",
- strerror(-err));
- rt_dev_close(rightbusfd);
- return err;
-}
-""",
- "retrieve":"""
-rt_dev_ioctl(rightbusfd, RTSMT_RTIOC_READ, &rightReadBuf);
-%(retrieve_code)s
-""",
- "publish":"""
-%(publish_code)s
-rt_dev_ioctl(rightbusfd, RTSMT_RTIOC_WRITE, &rightWriteBuf);
-""",
- "cleanup":"""
-rt_dev_close(rightbusfd);
-""",
- },
-###############################################################################
-######################## OnBoard devices #########################
-###############################################################################
- "MC9:On Board": {
- "decl" : """
-#define MAX_ONBOARD_DEVICES 2
-/* Tables containing information about enabled on-board devices
- (initialized by Composer) */
-unsigned char onBoardDev[MAX_ONBOARD_DEVICES][2];
-""",
- "init":"""
-bzero(&onBoardDev, sizeof(onBoardDev));
-%(init_code)s
-""",
- "retrieve":"%(retrieve_code)s",
- "publish":"%(publish_code)s",
- "cleanup":"",
- },
-###############################################################################
-######################## RS 485 non real time #########################
-###############################################################################
- "MC9:Devices": {
- "decl" : """
-#include <stdio.h>
-#include <termios.h>
-#include <unistd.h>
-#include <sys/ioctl.h>
-#include <fcntl.h>
-#include <sys/select.h>
-
-#include <native/task.h>
-#include <native/mutex.h>
-#include <native/timer.h>
+import os
-#include "beremiz.h"
-
-static struct termios oldterminfo;
-
-void closeserial(int fd)
-{
- tcsetattr(fd, TCSANOW, &oldterminfo);
- if (close(fd) < 0)
- 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;
-
-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
-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;
-}
-
-int gpio_close(struct gpio_device *aDev)
-{
- int unexport_file;
- char buf[BUFF_SIZE];
- int retval;
+modpath = os.path.split(__file__)[0]
- 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;
-}
-
-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;
-}
-
-int TransmitMode(int fd)
-{
- return gpio_set_pin_value(RS485_GPIO_dev, 1);
-}
-
-int RecieveMode(int fd)
-{
- return gpio_set_pin_value(RS485_GPIO_dev, 0);
-}
-
-/*--------------------------- Serial Port handling ---------------------------*/
-
-int openserial(char *devicename)
-{
- int fd;
- struct termios attr;
- speed_t baud = B115200; /* baud rate */
-
- if ((fd = open(devicename, O_RDWR)) == -1) {
- perror("openserial(): open()");
- return 0;
- }
- if (tcgetattr(fd, &oldterminfo) == -1) {
- perror("openserial(): tcgetattr()");
- return 0;
- }
- attr = oldterminfo;
- attr.c_oflag = 0;
- attr.c_iflag = 0;
- attr.c_lflag = 0;
-
- attr.c_cflag = 0;
- attr.c_cflag |= CREAD;
- attr.c_cflag |= CLOCAL;
- attr.c_cflag |= CS8 ; /* 8 bits */
- /* no parity, 1 stop bit */
-
- cfsetspeed(&attr, baud); /* baud rate */
-
- if (tcflush(fd, TCIOFLUSH) == -1) {
- perror("openserial(): tcflush()");
- return 0;
- }
- if (tcsetattr(fd, TCSANOW, &attr) == -1) {
- perror("initserial(): tcsetattr()");
- return 0;
- }
-
- TransmitMode(fd);
-
- return fd;
-}
-
-typedef struct /* Type definition for timers for right modules */
-{
- unsigned char status; /* Current status of timer - running / expired */
- long long actValue; /* Actual timer value */
- long long 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];
+def GetLocalCode(fname):
+ return open(os.path.join(modpath,"LPCBus",fname)).read()
-/* 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 */
-#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 */
-#define uartDevWriteBuf (*uartDevWriteBuf_drv)
-
-int uartDevWriteBuf_plc_state;
-int uartDevReadBuf_plc_state;
-
-/* Function interface definition for modules on UART (RS485) bus */
-typedef void (*uartPortFunct)(char*, char*, char);
-/* Table describing module positions on UART (RS485) bus (parsed from Composer data) */
-uartPortFunct uartPortDevices[MAX_UART_DEVICES] = {NULL};
-/* Table of timers (one for each position) */
-static commTimer uartPortTim[MAX_UART_DEVICES];
-
-static RT_TASK UART_task;
-static RT_MUTEX UART_WriteMutex;
-static RT_MUTEX UART_ReadMutex;
-
-void UART_task_proc(void *arg)
-{
- 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){
- 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);
- }
-
- /* Scan all positions for attached UART port modules */
-// for(i=0;i<MAX_UART_DEVICES;i++)
-// {
- /* Timers for right modules */
- if(uartPortTim[i].status != TIM_DISABLED)
- {
- uartPortTim[i].actValue += common_ticktime__;
- if(uartPortTim[i].actValue < uartPortTim[i].toValue)
- {
- uartPortTim[i].status = TIM_EN_RUNNING;
- }
- else
- uartPortTim[i].status = TIM_EN_EXPIRED;
- }
- if(uartPortDevices[i] != NULL)
- {
- // Check timer status
- if((uartPortTim[i].status == TIM_DISABLED)
- || (uartPortTim[i].status == TIM_EN_EXPIRED))
- {
- // Communicate with device
- (*uartPortDevices[i])(
- &uartDevReadBuf[i][0],
- &uartDevWriteBuf[i][0],
- uartDev[i][1]);
-
- // If timer is enabled, reset it's value,
- // otherwise keep it disabled
- if(uartPortTim[i].status != TIM_DISABLED)
- {
- uartPortTim[i].actValue = 0;
- uartPortTim[i].status = TIM_EN_RUNNING;
- }
- 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){
- 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);
- }
- }
-}
-
-/**************************************************************************//**
-* Calculate checksum of a buffer
-* @param [in] buffer Pointer to buffer
-* @return Checksum values on buffer locations 23 & 24
-******************************************************************************/
-void Checksum(unsigned char *buffer)
-{
- unsigned char i=0, j=0;
- unsigned char checksum1=0, checksum2=0;
- unsigned char checksum1Temp=0;
- for(i=0;i<45;i++)
- {
- checksum1Temp=buffer[i];
- for(j=0;j<8;j++) // Compute number of '1' of whole buff.
- {
- if((checksum1Temp & 0x01)>0)
- checksum1++;
- checksum1Temp = checksum1Temp >> 1;
- }
- checksum2 = checksum2 ^ buffer[i]; // Compute XOR of whole buffer
- }
- buffer[45] = checksum2; // Write number of '1' to buffer
- buffer[46] = checksum1; // Write XOR to buffer
-}
-/**************************************************************************//**
-* Check if checksum values of received buffer are valid
-* @param [in] buffer Pointer to buffer
-* @return TRUE if valid, otherwise FALSE
-******************************************************************************/
-unsigned char ChecksumValid(unsigned char *buffer)
-{
- unsigned char i=0, j=0;
- unsigned char checksum1=0, checksum2=0;
- unsigned char checksum1Temp=0;
- for(i=0;i<45;i++)
- {
- checksum1Temp = buffer[i];
- for(j=0;j<8;j++) // Compute number of '1' of whole buff.
- {
- if((checksum1Temp & 0x01)>0)
- checksum1++;
- checksum1Temp = checksum1Temp >> 1; // Compute XOR of whole buffer
- }
- checksum2 = checksum2^buffer[i];
- }
- if((checksum2==buffer[45]) && (checksum1==buffer[46])) // Check if computed checksums are the same as those in buffer (=> no error)
- return TRUE;
- else
- return FALSE;
-}
-
-#define TAIL_LEN 3 /* Length of data tail in bytes */
-static int UART_fd;
-
-/*************************************************************************//**
-* Support for UART modules
-* @param [in] readBuf Pointer to read buffer (for previously polled UART device!)
-* @param [out] writeBuf Pointer to write buffer (for current device)
-* @param [in] address UART device address
-* @return None
-*****************************************************************************/
-void UARTDevice(char* readBuf, char* writeBuf, char address)
-{
- fd_set set;
- struct timeval timeout;
- int rv;
- RTIME now;
- int count = 0;
- char tmp[UART_BUFSIZE];
-
- /* 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, tmp, UART_BUFSIZE) != UART_BUFSIZE){
- goto UARTDevfail;
- }
-
- /* Sleep until transmission completes + 0.5ms safety */
- while(rt_task_sleep_until(
- now + rt_timer_ns2ticks(
- 1000000000LL * UART_BUFSIZE * 10 / 115200
- + 500000)) == -EINTR);
-
- /* Turn to listen mode*/
- RecieveMode(UART_fd);
-
- while(count < UART_BUFSIZE){
-
- FD_ZERO(&set); /* clear the set */
- FD_SET(UART_fd, &set); /* add our file descriptor to the set */
-
- timeout.tv_sec = 0;
- timeout.tv_usec = 50000;
-
- rv = select(UART_fd + 1, &set, NULL, NULL, &timeout);
- if(rv == -1)
- printf("RS485 select error\\n");
- else if(rv == 0)
- {
- //printf("timeout\\n");
- break;
- }
- else {
- int rr = read(UART_fd, tmp + count, UART_BUFSIZE - count);
- if(rr > 0){
- count += rr;
- }else{
- printf("RS485 read error %%d\\n",rr);
- }
- }
- }
+bus_template_code = { plc:GetLocalCode(plc+".c") for plc in ["MC8", "MC9"] }
- // {
- // int i;
-// 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-TAIL_LEN-1);
- return;
- }
-
- UARTDevfail:
- tcflush(UART_fd, TCIOFLUSH);
-}
-
-/* Macro to transform milliseconds to ns */
-#define msTOns(ms) (1000000L*ms)
-
-void InitUartPortDevices(void)
-{
- unsigned char i=0;
-
- for(i=0;i<MAX_UART_DEVICES;i++)
- {
- switch(uartDev[i][1])
- {
- case(2): /* MU1 */
- case(4):
- case(6):
- case(8):
- case(10):
- case(12):
- case(14):
- case(16):
- case(18):
- case(20):
- case(22):
- case(24):
- case(26):
- case(28):
- case(30):
- case(32):
- case(34):
- case(36):
- case(38):
- case(40):
- case(42):
- case(44):
- case(46):
- case(48):
- case(66): /* MU2 */
- case(68):
- case(70):
- case(72):
- case(74): /* MU3 */
- case(76):
- case(78):
- case(80):
- uartPortDevices[i] = &UARTDevice;
- uartPortTim[i].toValue = msTOns(50); /* 50ms */
- uartPortTim[i].status = TIM_EN_RUNNING;
- break;
- case(130): /* P01, P02, P01V, P02V */
- case(132):
- case(134):
- case(136):
- case(138): /* CA1 */
- case(140):
- case(142):
- case(144):
- case(154): /* CR1 */
- case(156):
- case(158):
- case(160):
- case(162): /* IR1V */
- case(164):
- case(166):
- case(168):
- case(186): /* TH1V */
- case(188):
- case(190):
- case(192):
- uartPortDevices[i] = &UARTDevice;
- uartPortTim[i].toValue = msTOns(200); /* 200ms */
- uartPortTim[i].status = TIM_EN_RUNNING;
- break;
- case(146): /* CH1 */
- case(148):
- case(150):
- case(152):
- uartPortDevices[i] = &UARTDevice;
- uartPortTim[i].toValue = msTOns(600); /* 600ms */
- uartPortTim[i].status = TIM_EN_RUNNING;
- break;
- case(170): /* ID1, ID2, ID3 */
- case(172):
- case(174):
- case(176):
- uartPortDevices[i] = &UARTDevice;
- uartPortTim[i].toValue = msTOns(400); /* 400ms */
- uartPortTim[i].status = TIM_EN_RUNNING;
- break;
- case(194): /* DP1V */
- case(196):
- case(198):
- case(200):
- case(202): /* DP2V */
- case(204):
- case(206):
- case(208):
- case(210): /* DT1V */
- case(212):
- case(214):
- case(216):
- case(218): /* DU1V */
- case(220):
- case(222):
- case(224):
- uartPortDevices[i] = &UARTDevice;
- uartPortTim[i].toValue = msTOns(400); /* 400ms */
- uartPortTim[i].status = TIM_EN_RUNNING;
- break;
-
- default:
- uartPortDevices[i] = NULL; /* "Empty" or unknown module */
- uartPortTim[i].status = TIM_DISABLED;
- break;
- }
- }
-}
-
-""",
- "init":"""
-int err;
-char *serialdev = "/dev/ttyAPP1";
-int RS485_GPIO_NUM = 103;
-
-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_code)s
-
-if((err = rt_mutex_create (&UART_WriteMutex, "UART_WriteMutex")))
- return err;
-
-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);
-if (!UART_fd) {
- fprintf(stderr, "Error while initializing %%s.\\n", serialdev);
- return 1;
-}
-
-InitUartPortDevices();
-
-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){
-
-%(retrieve_code)s
-
- /* 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);
-
-%(publish_code)s
-
- /* 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);
-closeserial(UART_fd);
-gpio_close(RS485_GPIO_dev);
-rt_mutex_delete(&UART_WriteMutex);
-rt_mutex_delete(&UART_ReadMutex);
-""",
- },
-}
-
+bus_code = { "MC9:%s"%bus :
+ { section :
+ GetLocalCode("%s_%s_%s.c"%("MC9",''.join(bus.split()),section))
+ for section in ["decl", "init", "retrieve", "publish", "cleanup"]}
+ for bus in [ "Right", "On Board", "Devices"]}
# to be overloaded at import
LPCarch = None
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC8.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,35 @@
+/* Code generated by LPCBus confnode */
+
+/* LPCBus confnode includes */
+#include "app_glue.h"
+#ifdef _WINDOWS_H
+ #include "iec_types.h"
+#else
+ #include "iec_std_lib.h"
+#endif
+
+%(declare_code)s
+
+/* LPCBus confnode user variables definition */
+%(var_decl)s
+
+/* LPCBus confnode functions */
+int __init_%(location_str)s(int argc,char **argv)
+{
+%(init_code)s
+ return 0;
+}
+
+void __cleanup_%(location_str)s(void)
+{
+}
+
+void __retrieve_%(location_str)s(void)
+{
+%(retrieve_code)s
+}
+
+void __publish_%(location_str)s(void)
+{
+%(publish_code)s
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC9.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,62 @@
+
+/* Code generated by LPCBus confnode */
+
+#ifdef _WINDOWS_H
+ #include "iec_types.h"
+#else
+ #include "iec_std_lib.h"
+#endif
+
+static inline int16_t getWord(char *buffer)
+{
+ return ((int16_t)*buffer<<8) + (int16_t)*(buffer+1);
+}
+
+static inline void setWord(char *buffer, int16_t word)
+{
+ *buffer = (char)(word>>8);
+ *(buffer+1) = (char)(word);
+}
+
+static inline int8_t getBit(char *buffer, unsigned char bitOffset)
+{
+ return (*buffer & (0x01 << bitOffset)) && 1;
+}
+
+static inline void setBit(char *buffer, unsigned char bitOffset, int8_t bit)
+{
+ unsigned char msk = (0x01 << bitOffset);
+ if(bit)
+ *buffer |= msk;
+ else
+ *buffer &= ~msk;
+}
+
+%(bus_decl)s
+
+%(declare_code)s
+
+/* LPCBus confnode user variables definition */
+%(var_decl)s
+
+/* LPCBus confnode functions */
+int __init_%(location_str)s(int argc,char **argv)
+{
+ %(bus_init_code)s
+ return 0;
+}
+
+void __cleanup_%(location_str)s(void)
+{
+ %(bus_cleanup_code)s
+}
+
+void __retrieve_%(location_str)s(void)
+{
+%(bus_retrieve_code)s
+}
+
+void __publish_%(location_str)s(void)
+{
+%(bus_publish_code)s
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC9_Devices_cleanup.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,6 @@
+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);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC9_Devices_decl.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,595 @@
+#include <stdio.h>
+#include <termios.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <sys/select.h>
+
+#include <native/task.h>
+#include <native/mutex.h>
+#include <native/timer.h>
+
+#include "beremiz.h"
+
+static struct termios oldterminfo;
+
+void closeserial(int fd)
+{
+ tcsetattr(fd, TCSANOW, &oldterminfo);
+ if (close(fd) < 0)
+ 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;
+
+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
+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;
+}
+
+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;
+}
+
+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;
+}
+
+int TransmitMode(int fd)
+{
+ return gpio_set_pin_value(RS485_GPIO_dev, 1);
+}
+
+int RecieveMode(int fd)
+{
+ return gpio_set_pin_value(RS485_GPIO_dev, 0);
+}
+
+/*--------------------------- Serial Port handling ---------------------------*/
+
+int openserial(char *devicename)
+{
+ int fd;
+ struct termios attr;
+ speed_t baud = B115200; /* baud rate */
+
+ if ((fd = open(devicename, O_RDWR)) == -1) {
+ perror("openserial(): open()");
+ return 0;
+ }
+ if (tcgetattr(fd, &oldterminfo) == -1) {
+ perror("openserial(): tcgetattr()");
+ return 0;
+ }
+ attr = oldterminfo;
+ attr.c_oflag = 0;
+ attr.c_iflag = 0;
+ attr.c_lflag = 0;
+
+ attr.c_cflag = 0;
+ attr.c_cflag |= CREAD;
+ attr.c_cflag |= CLOCAL;
+ attr.c_cflag |= CS8 ; /* 8 bits */
+ /* no parity, 1 stop bit */
+
+ cfsetspeed(&attr, baud); /* baud rate */
+
+ if (tcflush(fd, TCIOFLUSH) == -1) {
+ perror("openserial(): tcflush()");
+ return 0;
+ }
+ if (tcsetattr(fd, TCSANOW, &attr) == -1) {
+ perror("initserial(): tcsetattr()");
+ return 0;
+ }
+
+ TransmitMode(fd);
+
+ return fd;
+}
+
+typedef struct /* Type definition for timers for right modules */
+{
+ unsigned char status; /* Current status of timer - running / expired */
+ long long actValue; /* Actual timer value */
+ long long 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 */
+#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 */
+#define uartDevWriteBuf (*uartDevWriteBuf_drv)
+
+int uartDevWriteBuf_plc_state;
+int uartDevReadBuf_plc_state;
+
+/* Function interface definition for modules on UART (RS485) bus */
+typedef void (*uartPortFunct)(char*, char*, char);
+/* Table describing module positions on UART (RS485) bus (parsed from Composer data) */
+uartPortFunct uartPortDevices[MAX_UART_DEVICES] = {NULL};
+/* Table of timers (one for each position) */
+static commTimer uartPortTim[MAX_UART_DEVICES];
+
+static RT_TASK UART_task;
+static RT_MUTEX UART_WriteMutex;
+static RT_MUTEX UART_ReadMutex;
+
+void UART_task_proc(void *arg)
+{
+ 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){
+ 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);
+ }
+
+ /* Scan all positions for attached UART port modules */
+// for(i=0;i<MAX_UART_DEVICES;i++)
+// {
+ /* Timers for right modules */
+ if(uartPortTim[i].status != TIM_DISABLED)
+ {
+ uartPortTim[i].actValue += common_ticktime__;
+ if(uartPortTim[i].actValue < uartPortTim[i].toValue)
+ {
+ uartPortTim[i].status = TIM_EN_RUNNING;
+ }
+ else
+ uartPortTim[i].status = TIM_EN_EXPIRED;
+ }
+ if(uartPortDevices[i] != NULL)
+ {
+ // Check timer status
+ if((uartPortTim[i].status == TIM_DISABLED)
+ || (uartPortTim[i].status == TIM_EN_EXPIRED))
+ {
+ // Communicate with device
+ (*uartPortDevices[i])(
+ &uartDevReadBuf[i][0],
+ &uartDevWriteBuf[i][0],
+ uartDev[i][1]);
+
+ // If timer is enabled, reset it's value,
+ // otherwise keep it disabled
+ if(uartPortTim[i].status != TIM_DISABLED)
+ {
+ uartPortTim[i].actValue = 0;
+ uartPortTim[i].status = TIM_EN_RUNNING;
+ }
+ 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){
+ 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);
+ }
+ }
+}
+
+/**************************************************************************//**
+* Calculate checksum of a buffer
+* @param [in] buffer Pointer to buffer
+* @return Checksum values on buffer locations 23 & 24
+******************************************************************************/
+void Checksum(unsigned char *buffer)
+{
+ unsigned char i=0, j=0;
+ unsigned char checksum1=0, checksum2=0;
+ unsigned char checksum1Temp=0;
+ for(i=0;i<45;i++)
+ {
+ checksum1Temp=buffer[i];
+ for(j=0;j<8;j++) // Compute number of '1' of whole buff.
+ {
+ if((checksum1Temp & 0x01)>0)
+ checksum1++;
+ checksum1Temp = checksum1Temp >> 1;
+ }
+ checksum2 = checksum2 ^ buffer[i]; // Compute XOR of whole buffer
+ }
+ buffer[45] = checksum2; // Write number of '1' to buffer
+ buffer[46] = checksum1; // Write XOR to buffer
+}
+/**************************************************************************//**
+* Check if checksum values of received buffer are valid
+* @param [in] buffer Pointer to buffer
+* @return TRUE if valid, otherwise FALSE
+******************************************************************************/
+unsigned char ChecksumValid(unsigned char *buffer)
+{
+ unsigned char i=0, j=0;
+ unsigned char checksum1=0, checksum2=0;
+ unsigned char checksum1Temp=0;
+ for(i=0;i<45;i++)
+ {
+ checksum1Temp = buffer[i];
+ for(j=0;j<8;j++) // Compute number of '1' of whole buff.
+ {
+ if((checksum1Temp & 0x01)>0)
+ checksum1++;
+ checksum1Temp = checksum1Temp >> 1; // Compute XOR of whole buffer
+ }
+ checksum2 = checksum2^buffer[i];
+ }
+ if((checksum2==buffer[45]) && (checksum1==buffer[46])) // Check if computed checksums are the same as those in buffer (=> no error)
+ return TRUE;
+ else
+ return FALSE;
+}
+
+#define TAIL_LEN 3 /* Length of data tail in bytes */
+static int UART_fd;
+
+/*************************************************************************//**
+* Support for UART modules
+* @param [in] readBuf Pointer to read buffer (for previously polled UART device!)
+* @param [out] writeBuf Pointer to write buffer (for current device)
+* @param [in] address UART device address
+* @return None
+*****************************************************************************/
+void UARTDevice(char* readBuf, char* writeBuf, char address)
+{
+ fd_set set;
+ struct timeval timeout;
+ int rv;
+ RTIME now;
+ int count = 0;
+ char tmp[UART_BUFSIZE];
+
+ /* 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, tmp, UART_BUFSIZE) != UART_BUFSIZE){
+ goto UARTDevfail;
+ }
+
+ /* Sleep until transmission completes + 0.5ms safety */
+ while(rt_task_sleep_until(
+ now + rt_timer_ns2ticks(
+ 1000000000LL * UART_BUFSIZE * 10 / 115200
+ + 500000)) == -EINTR);
+
+ /* Turn to listen mode*/
+ RecieveMode(UART_fd);
+
+ while(count < UART_BUFSIZE){
+
+ FD_ZERO(&set); /* clear the set */
+ FD_SET(UART_fd, &set); /* add our file descriptor to the set */
+
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 50000;
+
+ rv = select(UART_fd + 1, &set, NULL, NULL, &timeout);
+ if(rv == -1)
+ printf("RS485 select error\n");
+ else if(rv == 0)
+ {
+ //printf("timeout\n");
+ break;
+ }
+ else {
+ int rr = read(UART_fd, tmp + count, UART_BUFSIZE - count);
+ if(rr > 0){
+ count += rr;
+ }else{
+ printf("RS485 read error %%d\n",rr);
+ }
+ }
+ }
+
+ // {
+ // int i;
+// 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-TAIL_LEN-1);
+ return;
+ }
+
+ UARTDevfail:
+ tcflush(UART_fd, TCIOFLUSH);
+}
+
+/* Macro to transform milliseconds to ns */
+#define msTOns(ms) (1000000L*ms)
+
+void InitUartPortDevices(void)
+{
+ unsigned char i=0;
+
+ for(i=0;i<MAX_UART_DEVICES;i++)
+ {
+ switch(uartDev[i][1])
+ {
+ case(2): /* MU1 */
+ case(4):
+ case(6):
+ case(8):
+ case(10):
+ case(12):
+ case(14):
+ case(16):
+ case(18):
+ case(20):
+ case(22):
+ case(24):
+ case(26):
+ case(28):
+ case(30):
+ case(32):
+ case(34):
+ case(36):
+ case(38):
+ case(40):
+ case(42):
+ case(44):
+ case(46):
+ case(48):
+ case(66): /* MU2 */
+ case(68):
+ case(70):
+ case(72):
+ case(74): /* MU3 */
+ case(76):
+ case(78):
+ case(80):
+ uartPortDevices[i] = &UARTDevice;
+ uartPortTim[i].toValue = msTOns(50); /* 50ms */
+ uartPortTim[i].status = TIM_EN_RUNNING;
+ break;
+ case(130): /* P01, P02, P01V, P02V */
+ case(132):
+ case(134):
+ case(136):
+ case(138): /* CA1 */
+ case(140):
+ case(142):
+ case(144):
+ case(154): /* CR1 */
+ case(156):
+ case(158):
+ case(160):
+ case(162): /* IR1V */
+ case(164):
+ case(166):
+ case(168):
+ case(186): /* TH1V */
+ case(188):
+ case(190):
+ case(192):
+ uartPortDevices[i] = &UARTDevice;
+ uartPortTim[i].toValue = msTOns(200); /* 200ms */
+ uartPortTim[i].status = TIM_EN_RUNNING;
+ break;
+ case(146): /* CH1 */
+ case(148):
+ case(150):
+ case(152):
+ uartPortDevices[i] = &UARTDevice;
+ uartPortTim[i].toValue = msTOns(600); /* 600ms */
+ uartPortTim[i].status = TIM_EN_RUNNING;
+ break;
+ case(170): /* ID1, ID2, ID3 */
+ case(172):
+ case(174):
+ case(176):
+ uartPortDevices[i] = &UARTDevice;
+ uartPortTim[i].toValue = msTOns(400); /* 400ms */
+ uartPortTim[i].status = TIM_EN_RUNNING;
+ break;
+ case(194): /* DP1V */
+ case(196):
+ case(198):
+ case(200):
+ case(202): /* DP2V */
+ case(204):
+ case(206):
+ case(208):
+ case(210): /* DT1V */
+ case(212):
+ case(214):
+ case(216):
+ case(218): /* DU1V */
+ case(220):
+ case(222):
+ case(224):
+ uartPortDevices[i] = &UARTDevice;
+ uartPortTim[i].toValue = msTOns(400); /* 400ms */
+ uartPortTim[i].status = TIM_EN_RUNNING;
+ break;
+
+ default:
+ uartPortDevices[i] = NULL; /* "Empty" or unknown module */
+ uartPortTim[i].status = TIM_DISABLED;
+ break;
+ }
+ }
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC9_Devices_init.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,46 @@
+int err;
+char *serialdev = "/dev/ttyAPP1";
+int RS485_GPIO_NUM = 103;
+
+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_code)s
+
+if((err = rt_mutex_create (&UART_WriteMutex, "UART_WriteMutex")))
+ return err;
+
+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);
+if (!UART_fd) {
+ fprintf(stderr, "Error while initializing %%s.\n", serialdev);
+ return 1;
+}
+
+InitUartPortDevices();
+
+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;
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC9_Devices_publish.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,16 @@
+if(!rt_mutex_acquire(&UART_WriteMutex, TM_INFINITE )){
+
+ int prevstate;
+ prevstate=uartDevWriteBuf_plc_state;
+ uartDevWriteBuf_plc_state = LOCKED;
+
+ rt_mutex_release(&UART_WriteMutex);
+
+%(publish_code)s
+
+ /* unlock plc buffer */
+ uartDevWriteBuf_plc_state = FULL;
+
+ /* wakeup task */
+ rt_task_unblock(&UART_task);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC9_Devices_retrieve.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,19 @@
+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){
+
+%(retrieve_code)s
+
+ /* unlock plc buffer */
+ uartDevReadBuf_plc_state = EMPTY;
+ }else{
+ /* No new data -> no update */
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC9_OnBoard_cleanup.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,1 @@
+/* Hello */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC9_OnBoard_decl.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,4 @@
+#define MAX_ONBOARD_DEVICES 2
+/* Tables containing information about enabled on-board devices
+ (initialized by Composer) */
+unsigned char onBoardDev[MAX_ONBOARD_DEVICES][2];
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC9_OnBoard_init.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,2 @@
+bzero(&onBoardDev, sizeof(onBoardDev));
+%(init_code)s
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC9_OnBoard_publish.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,1 @@
+%(publish_code)s
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC9_OnBoard_retrieve.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,1 @@
+%(retrieve_code)s
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC9_Right_cleanup.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,1 @@
+rt_dev_close(rightbusfd);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC9_Right_decl.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,38 @@
+
+#include <rtdm/rtdm.h>
+#include "beremiz.h"
+
+/* LPCBus confnode includes */
+#define RIGHT_READ_BUFSIZE 30 /**< Right bus read buffer size */
+#define RIGHT_WRITE_BUFSIZE 30 /**< Right bus write buffer size */
+
+#define MAX_RIGHT_MODULES 8
+
+/**< Buffers for reading data from right bus modules */
+typedef char rightReadBuf_t[MAX_RIGHT_MODULES][RIGHT_READ_BUFSIZE];
+
+/**< Buffers for writing data to right bus modules */
+typedef char rightWriteBuf_t[MAX_RIGHT_MODULES][RIGHT_WRITE_BUFSIZE];
+
+/**< 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, 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 rightBusInit_t rightBusInit;
+
+static int rightbusfd = -1;
+
+/* XXX TODO #include "smarteh.h" */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC9_Right_init.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,26 @@
+#define DEVICEFILENAME "rightbus"
+
+bzero(&rightReadBuf, sizeof(rightReadBuf_t));
+bzero(&rightWriteBuf, sizeof(rightWriteBuf_t));
+bzero(&rightBusInit, sizeof(rightBusInit_t));
+
+rightBusInit.common_ticktime__ = common_ticktime__;
+
+#define rightI2CMod (rightBusInit.rightI2CMod)
+%(init_code)s
+
+int err;
+rightbusfd = rt_dev_open( DEVICEFILENAME, 0);
+if (rightbusfd < 0) {
+ printf("can't open %%s rtdm device, %%s\\n", DEVICEFILENAME,
+ strerror(-rightbusfd));
+ return rightbusfd;
+}
+
+err = rt_dev_ioctl(rightbusfd, RTSMT_RTIOC_INIT, &rightBusInit);
+if (err) {
+ printf("error while RTSMT_RTIOC_INIT, %%s\\n",
+ strerror(-err));
+ rt_dev_close(rightbusfd);
+ return err;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC9_Right_publish.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,2 @@
+%(publish_code)s
+rt_dev_ioctl(rightbusfd, RTSMT_RTIOC_WRITE, &rightWriteBuf);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPCBus/MC9_Right_retrieve.c Thu Jan 22 18:12:42 2015 +0100
@@ -0,0 +1,2 @@
+rt_dev_ioctl(rightbusfd, RTSMT_RTIOC_READ, &rightReadBuf);
+%(retrieve_code)s