ec.h 2.16 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007 - 2009
	Copyright Stichting C.A. Muller Radioastronomiestation, 2007 - 2009

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

#ifndef _INCLUDE_EC_H_
#define _INCLUDE_EC_H_

#include <stdlib.h>
#include <inttypes.h>
#include <stdbool.h>

#include "canopen.h"

int ec_init(char *ifname, int single_cycle);
bool ec_initialized(void);

int ec_slave_count(void);

//TODO:
#ifndef htole32
#define le16toh(val) (val)
#define le32toh(val) (val)
#define le64toh(val) (val)
#define htole16(val) (val)
#define htole32(val) (val)
#define htole64(val) (val)
#endif

struct ec_dgram_addr {
	enum {
		EC_DGRAM_ADDR_AUTO_INC,
		EC_DGRAM_ADDR_STATION,
		EC_DGRAM_ADDR_LOGICAL,
	} type;
	union {
		struct { 
			uint16_t pos;
			uint16_t off;
		} position;
		struct {
			uint16_t addr;
			uint16_t off;
		} node;
		struct {
			uint32_t addr;
		} logical;
	} addr;
} __attribute((__packed__));

#define ec_addr_set_auto_inc_nr(add, nr) \
do { \
	(add)->type = EC_DGRAM_ADDR_AUTO_INC; \
	(add)->addr.position.pos = 0x0000 - (nr); \
	(add)->addr.position.off = 0x0000; \
} while (0)


ssize_t ec_datagram_read(struct ec_dgram_addr *addr, void *buffer, size_t len);
ssize_t ec_datagram_write(struct ec_dgram_addr *addr, void *buffer, size_t len);
void ec_datagram_sample(void);

int ec_rx_pdo_add(struct ec_dgram_addr *addr, unsigned char **data, size_t len);
int ec_tx_pdo_add(struct ec_dgram_addr *addr, unsigned char **data, size_t len);
int ec_tx_pdo(uint32_t nr);
int ec_rx_pdo(uint32_t nr);

int ec_rx_pdo_enable(int enable);
int ec_tx_pdo_enable(int enable);



#endif /* _INCLUDE_EC_H_ */