/*------------------------- GPIO -------------------------------------*/
/* from armadeus/target/packages/as_devices/c/as_gpio* */
# define ERROR(fmt, ...) printf(fmt, ##__VA_ARGS__)
//# define ERROR(fmt, ...) /*fmt, ##__VA_ARGS__*/
int pin_file; /* pin file for 2.6.29 interface*/
struct gpio_device *RTU_GPIO_dev;
static int write_file_bool(int fd, int value)
ret = write(fd, value?"1":"0", 1);
if (lseek(fd, 0, SEEK_SET) < 0) {
static struct gpio_device *gpio_open(int aGpioNum)
export_file = open("/sys/class/gpio/export", O_WRONLY);
ERROR("Can't open /sys/class/gpio/export\nBe sure that gpiolib is under your kernel\n");
snprintf(buf, BUFF_SIZE, "%%d", aGpioNum);
retval = write(export_file, buf, strlen(buf));
ERROR("/sys/class/gpio/export can't be written\n");
snprintf(buf, BUFF_SIZE, "/sys/class/gpio/gpio%%d/direction", aGpioNum);
gpio_dir_fd = open(buf, O_WRONLY);
ERROR("Can't open gpio%%d direction\n", aGpioNum);
ret = write(gpio_dir_fd, "out", 3);
ERROR("Error writing direction\n");
snprintf(buf, BUFF_SIZE, "/sys/class/gpio/gpio%%d/value", aGpioNum);
pin_file = open(buf, O_RDWR);
ERROR("Can't export gpio number %%d\n", aGpioNum);
dev = malloc(sizeof(struct gpio_device));
ERROR("Can't allocate gpio_device structure\n");
dev->port_num = aGpioNum;
dev->pin_file = pin_file;
static int gpio_close(struct gpio_device *aDev)
ERROR("device is NULL\n");
unexport_file = open("/sys/class/gpio/unexport", O_WRONLY);
ERROR("Can't open /sys/class/gpio/unexport\nBe sure that gpiolib is under your kernel\n");
snprintf(buf, BUFF_SIZE, "%%d", aDev->port_num);
retval = write(unexport_file, buf, strlen(buf));
ERROR("/sys/class/gpio/unexport can't be written\n");
static int gpio_set_pin_value(struct gpio_device *aDev, int aValue)
int pin_file = aDev->pin_file;
retval = write_file_bool(pin_file, aValue);
ERROR("Can't write value\n");
#define REG_DISCRETE_START 0 /**< Input discrete start address */
#define REG_DISCRETE_NREGS 511 /**< 512 bits (type: single bit, Read only) */
#define REG_COILS_START 0 /**< Coils start address */
#define REG_COILS_NREGS 511 /**< 512 bits (type: single bit, Read/Write) */
#define REG_INPUT_START 0 /**< Input registers start address */
#define REG_INPUT_NREGS 511 /**< 512 words (type: 16-bit word, Read only) */
#define REG_HOLDING_START 0 /**< Holding registers start address */
#define REG_HOLDING_NREGS 511 /**< 512 words (type: 16-bit word, Read/Write) */
#define MAX_MOD_RTU_DEVICES 16
typedef struct _mbRtuRdHoldingRegs {
unsigned short startAddr;
typedef struct _mbRtuRdDiscInputs {
unsigned short startAddr;
// unsigned short* buffer;
typedef struct _mbRtuRdCoils {
unsigned short startAddr;
typedef struct _mbRtuWrSingleCoil {
unsigned short startAddr;
typedef struct _mbRtuWrCoils {
unsigned short startAddr;
typedef struct _mbRtuRdInputRegs {
unsigned short startAddr;
typedef struct _mbRtuWrSingleReg {
unsigned short startAddr;
typedef struct _mbRtuWrMultiRegs {
unsigned short startAddr;
typedef struct _mbRtuSlaveConfig {
mbRtuRdDiscInputs rdDiscInputs;
mbRtuWrSingleCoil wrSingleCoil;
mbRtuRdInputRegs rdInputRegs;
mbRtuRdHoldingRegs rdHoldingRegs;
mbRtuWrSingleReg wrSingleReg;
mbRtuWrMultiRegs wrMultiRegs;
mbRtuSlaveConfig mbRtuSlaveDev[MAX_MOD_RTU_DEVICES]; /**< Tables containing information about connected Modbus network devices (initialized by Composer) */
commTimer mbRtuDevTim[MAX_MOD_RTU_DEVICES]; /**< Table of timers (one for each position) */
unsigned short usRegInputValue[REG_INPUT_NREGS]; /**< Array of Modbus input registers */
unsigned short usRegHoldingValue[REG_HOLDING_NREGS]; /**< Array of Modbus holding registers */
unsigned char ubCoilValue[REG_COILS_NREGS/8]; /**< Array of Modbus coils (8 coils per byte) */
unsigned char ubRegDiscreteValue[REG_DISCRETE_NREGS/8]; /**< Array of Modbus discrete inputs (8 inputs per byte) */
/* prototypes for functions defined in shared library */
void mbmrtu_BusUpdate(void);
static RT_TASK RTU_UART_task;
static RT_MUTEX RTU_BuffMutex;
void LockMBRTUBuffer(void){
rt_mutex_acquire(&RTU_BuffMutex, TM_INFINITE );
void UnLockMBRTUBuffer(void){
rt_mutex_release(&RTU_BuffMutex);
typedef void (RTU_UART_task_proc_t*)(void*);
void CreateMBRTUSerialTask(RTU_UART_task_proc_t* RTU_UART_task_proc, void* param){
if(rt_task_start(&RTU_UART_task, RTU_UART_task_proc, param))
static void mbmrtu_BusUpdate_proc(void){
while (rt_task_sleep_until(TM_INFINITE) == -EINTR){
gpio_set_pin_value(RTU_GPIO_dev, 1);
void RecieveMode(int us){
while(rt_task_sleep_until(
now + rt_timer_ns2ticks(1000LL*us)
gpio_set_pin_value(RTU_GPIO_dev, 0);