src.dualinventive.com/fw/dncm/libdi/examples/linux/drv_hl854x.c

153 lines
4.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
#include <di/time.h>
#include <di/drv/hl854x.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
static int fd = -1;
int32_t di_drv_hl854x_write_serial(const void *buf, size_t nbyte)
{
size_t unused __attribute__((unused));
unused = write(STDOUT_FILENO, buf, nbyte);
return write(fd, buf, nbyte);
}
int32_t di_drv_hl854x_read_serial(void *buf, size_t nbyte)
{
size_t unused __attribute__((unused));
ssize_t _nbyte = read(fd, buf, nbyte);
if (_nbyte > 0)
unused = write(STDOUT_FILENO, buf, nbyte);
return _nbyte;
}
int serial_open(const char *dev)
{
fd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd == -1)
return fd;
//CONFIGURE THE UART
//The flags (defined in /usr/include/termios.h - see http://pubs.opengroup.org/onlinepubs/007908799/xsh/termios.h.html):
// Baud rate:- B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000
// CSIZE:- CS5, CS6, CS7, CS8
// CLOCAL - Ignore modem status lines
// CREAD - Enable receiver
// IGNPAR = Ignore characters with parity errors
// ICRNL - Map CR to NL on input (Use for ASCII comms where you want to auto correct end of line characters - don't use for bianry comms!)
// PARENB - Parity enable
// PARODD - Odd parity (else even)
struct termios options;
tcgetattr(fd, &options);
options.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
tcflush(fd,TCIOFLUSH);
tcsetattr(fd, TCSANOW, &options);
return fd;
}
int
main(void)
{
struct di_time t;
const char *serialport = "/dev/ttyAMA0";
struct di_drv_hl854x_ctx ctx;
di_drv_hl854x_init(&ctx);
di_drv_hl854x_set_read(di_drv_hl854x_read_serial);
di_drv_hl854x_set_write(di_drv_hl854x_write_serial);
fd = serial_open("/dev/ttyAMA0");
if (fd == -1) {
printf("error opening %s\n", serialport);
return -1;
}
printf("Opened serial on fd %u\n", fd);
di_drv_hl854x_gps_init(&ctx);
di_drv_hl854x_gps_start(&ctx);
while(1) {
int ret = di_drv_hl854x_getc(&ctx);
if (ret == 0) {
printf("sleeping...\n");
sleep(1);
printf("gps.time.tm_year = %" PRIu16 "\n", ctx.gps.time.tm_year);
printf("gps.time.tm_mon = %" PRIu8 "\n", ctx.gps.time.tm_mon);
printf("gps.time.tm_mday = %" PRIu8 "\n", ctx.gps.time.tm_mday);
printf("---\n");
printf("gps.time.tm_hour = %" PRIu8 "\n", ctx.gps.time.tm_hour);
printf("gps.time.tm_min = %" PRIu8 "\n", ctx.gps.time.tm_min);
printf("gps.time.tm_sec = %" PRIu8 "\n", ctx.gps.time.tm_sec);
printf("---\n");
ctx.gps.time.unix = di_time_datetime_to_unix(
ctx.gps.time.tm_year,
ctx.gps.time.tm_mon,
ctx.gps.time.tm_mday,
ctx.gps.time.tm_hour,
ctx.gps.time.tm_min,
ctx.gps.time.tm_sec);
printf("gps.time.unix = %" PRIu64 "\n", ctx.gps.time.unix);
di_time_unix_to_dinet(ctx.gps.time.unix, &t);
printf("dinet time: %u.%u\n", t.min, t.msec);
printf("---\n");
#if 0
printf("con.state = %u\n", ctx.con.state);
printf("tcp.state = %u\n", ctx.tcp.state);
printf("tcp.recv_size = %u\n", ctx.tcp.recv_size);
if (ctx.con.state == HL854X_CON_STATE_UNKNOWN ||
ctx.con.state == HL854X_CON_STATE_WRITTEN) {
di_drv_hl854x_con_cfg(&ctx);
continue;
}
if (ctx.tcp.state == HL854X_TCP_STATE_DISCONNECTED) {
di_drv_hl854x_tcp_close(&ctx);
continue;
}
if (ctx.con.state == HL854X_CON_STATE_DEFINED &&
ctx.tcp.state != HL854X_TCP_STATE_CONNECTED) {
di_drv_hl854x_tcp_open(&ctx);
continue;
}
if (ctx.tcp.state == HL854X_TCP_STATE_CONNECTED) {
printf("tcp connected...\n");
if (ctx.tcp.recv_size > 0) {
di_drv_hl854x_tcp_recv(&ctx, NULL, ctx.tcp.recv_size);
ctx.tcp.recv_size = 0;
di_drv_hl854x_tcp_stat(&ctx);
continue;
} else {
uint8_t di_handshake[] = { 0x44, 0x4a, 0x52, 0x01, 0x00, 0x0A, 0x30, 0x30, 0x30, 0x31 };
di_drv_hl854x_tcp_send(&ctx, di_handshake, 10);
printf("handshake send on tcp1\n");
}
}
#endif
}
};
printf("done...");
}