lpcmanager

Added retain init cleanup stubs

2015-03-13, Edouard Tisserant
3d567dc4c1ae
Added retain init cleanup stubs
/*------------------------- 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 *RTU_GPIO_dev;
static 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
static 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;
}
static 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;
}
static 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;
}
#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 int use;
unsigned short startAddr;
unsigned char length;
unsigned short* buffer;
} mbRtuRdHoldingRegs;
typedef struct _mbRtuRdDiscInputs {
unsigned int use;
unsigned short startAddr;
unsigned short length;
unsigned char* buffer;
// unsigned short* buffer;
} mbRtuRdDiscInputs;
typedef struct _mbRtuRdCoils {
unsigned int use;
unsigned short startAddr;
unsigned short length;
unsigned char* buffer;
} mbRtuRdCoils;
typedef struct _mbRtuWrSingleCoil {
unsigned int use;
unsigned short startAddr;
unsigned char* buffer;
} mbRtuWrSingleCoil;
typedef struct _mbRtuWrCoils {
unsigned int use;
unsigned short startAddr;
unsigned short length;
unsigned char* buffer;
} mbRtuWrCoils;
typedef struct _mbRtuRdInputRegs {
unsigned int use;
unsigned short startAddr;
unsigned char length;
unsigned short* buffer;
} mbRtuRdInputRegs;
typedef struct _mbRtuWrSingleReg {
unsigned int use;
unsigned short startAddr;
unsigned short* buffer;
} mbRtuWrSingleReg;
typedef struct _mbRtuWrMultiRegs {
unsigned int use;
unsigned short startAddr;
unsigned char length;
unsigned short* buffer;
} mbRtuWrMultiRegs;
typedef struct _mbRtuSlaveConfig {
unsigned char slaveAddr;
mbRtuRdDiscInputs rdDiscInputs;
mbRtuRdCoils rdCoils;
mbRtuWrSingleCoil wrSingleCoil;
mbRtuWrCoils wrCoils;
mbRtuRdInputRegs rdInputRegs;
mbRtuRdHoldingRegs rdHoldingRegs;
mbRtuWrSingleReg wrSingleReg;
mbRtuWrMultiRegs wrMultiRegs;
} mbRtuSlaveConfig;
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 */
int mbmrtu_init();
void mbmrtu_BusUpdate(void);
static RT_TASK RTU_UART_task;
static RT_TASK RTU_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){
int err;
if(rt_task_start(&RTU_UART_task, RTU_UART_task_proc, param))
return err;
return 0;
}
static void mbmrtu_BusUpdate_proc(void){
while (rt_task_sleep_until(TM_INFINITE) == -EINTR){
mbmrtu_BusUpdate();
}
}
RTIME now;
void TransmitMode(void){
gpio_set_pin_value(RTU_GPIO_dev, 1);
now = rt_timer_read();
}
void RecieveMode(int us){
while(rt_task_sleep_until(
now + rt_timer_ns2ticks(1000LL*us)
)) == -EINTR);
gpio_set_pin_value(RTU_GPIO_dev, 0);
}