lpcmanager

Reflect beremiz commit fe3394697b02 : 'Make some POU libraries selected by default or not...'
#include <unistd.h>
#include <fcntl.h>
#include "beremiz.h"
/*------------------------- 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 *RUN_LED_dev;
#ifdef CAN0_EN_GPIO_0_21
struct gpio_device *CAN0_EN_dev;
#endif /* CAN0_EN_GPIO_0_21 */
#ifdef CAN1_EN_GPIO_0_17
struct gpio_device *CAN1_EN_dev;
#endif /* CAN1_EN_GPIO_0_17 */
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 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;
}
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;
gpio_set_pin_value(dev, 1);
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;
}
/* TODO : DELETE !... but keep this until composer stops sending code snippets for mbrtu */
typedef enum
{
MB_PAR_ODD = 0, /**< ODD parity. */
MB_PAR_EVEN = 1, /**< Even parity. */
MB_PAR_NONE = 2 /**< No parity. */
} eMBSerialParity;
unsigned long mbBaudRate = 115200; /**< Modbus baud-rate setting: 9600, 19200, 38400, 57600, 115200 */
eMBSerialParity mbParity = MB_PAR_NONE; /**< Modbus parity setting: odd, even, none */
#ifdef ONBOARD_I2C
#define MAX_ONBOARD_DEVICES 2
#define ONBOARD_READ_BUFSIZE 30
#define ONBOARD_WRITE_BUFSIZE 30
typedef char onBoardReadBuf_t[MAX_ONBOARD_DEVICES][ONBOARD_READ_BUFSIZE];
typedef char onBoardWriteBuf_t[MAX_ONBOARD_DEVICES][ONBOARD_WRITE_BUFSIZE];
typedef char onBoardDev_t[MAX_ONBOARD_DEVICES][2];
typedef struct {
onBoardDev_t onBoardDev;
unsigned long long common_ticktime__;
} onBoardBusInit_t;
#define RTIOC_TYPE_SMT_ONBOARD RTDM_CLASS_EXPERIMENTAL
#define RTSMT_ONBOARD_RTIOC_INIT _IOR(RTIOC_TYPE_SMT_ONBOARD, 0x00, onBoardBusInit_t)
#define RTSMT_ONBOARD_RTIOC_READ _IOR(RTIOC_TYPE_SMT_ONBOARD, 0x01, onBoardReadBuf_t)
#define RTSMT_ONBOARD_RTIOC_WRITE _IOR(RTIOC_TYPE_SMT_ONBOARD, 0x02, onBoardWriteBuf_t)
static onBoardReadBuf_t onBoardReadBuf;
static onBoardWriteBuf_t onBoardWriteBuf;
static onBoardBusInit_t onBoardBusInit;
static int onboardbusfd = -1;
#endif /* ONBOARD_I2C */