Skip to content

Instantly share code, notes, and snippets.

@Arachnid
Created December 3, 2013 08:07
Show Gist options
  • Save Arachnid/7765642 to your computer and use it in GitHub Desktop.
Save Arachnid/7765642 to your computer and use it in GitHub Desktop.
/*
* uCAN.h
*
* Created on: Dec 1, 2013
* Author: nick
*/
#ifndef UCAN_H_
#define UCAN_H_
#define UCAN_BROADCAST_NODE_ID 0xFF
#define UCAN_PRIORITY_EMERGENCY 0
#define UCAN_PRIORITY_HIGH 1
#define UCAN_PRIORITY_NORMAL 2
#define UCAN_PRIORITY_LOW 3
#define UCAN_NODE_NOT_FOUND -1
#define UCAN_PROTOCOL_YARP 0
#define UCAN_PROTOCOL_RAP 1
typedef union {
struct {
unsigned int sender : 8;
unsigned int recipient : 8;
unsigned int subfields : 6;
unsigned int protocol : 4;
unsigned int broadcast : 1;
unsigned int priority : 2;
} unicast;
struct {
unsigned int sender : 8;
unsigned int subfields : 14;
unsigned int protocol : 4;
unsigned int broadcast : 1;
unsigned int priority : 2;
} broadcast;
uint32_t raw;
} uCANMessageID;
typedef struct {
uint8_t address[6];
} uCANHardwareID;
typedef struct {
uCANMessageID id;
uint8_t len;
uint8_t body[8];
} uCANMessage;
typedef int16_t uCANNodeAddress;
typedef void (*uCANReceiveMessageFunc)(uCAN *ucan, uCANMessage *message);
typedef void (*uCANSendMessageFunc)(uCAN *ucan, uCANMessage *message);
typedef void (*uCANAddressChangeHandler)(uint8_t node_id);
typedef uint8_t (*uCANRegisterReadHandler)(uCANNodeAddress address, uint8_t page, uint8_t reg);
typedef void (*uCANRegisterWriteHandler)(uCANNodeAddress address, uint8_t page, uint8_t reg, uint8_t data);
typedef struct {
uint8_t page;
uCANRegisterReadHandler read;
uCANRegisterWriteHandler write;
} uCANRegisterHandlers;
typedef struct {
uint8_t node_id;
uCANHardwareID hardware_id;
uCANReceiveMessageFunc recv;
uCANSendMessageFunc send;
uCANAddressChangeHandler address_change_handler;
uCANRegisterHandlers *registers;
uint16_t timeout;
} uCAN;
void uCAN_Init(uCAN *ucan, uCANHardwareID hardware_id, uint8_t default_node_id,
uCANReceiveMessageFunc recv, uCANSendMessageFunc send);
uint8_t uCAN_HandleMessage(uCAN *ucan, uCANMessage *message);
uint8_t uCAN_Receive(uCAN *ucan);
void uCAN_SetTimeout(uCAN *ucan, int timeout);
// YARP methods
NodeAddress uCAN_GetNodeFromNodeID(uCAN *ucan, uint8_t node_id);
NodeAddress uCAN_GetNodeFromHardwareID(uCAN *ucan, HardwareID hardware_id);
uint8_t uCAN_Ping(uCAN *ucan, NodeAddress node, HardwareID *hardware_id);
void uCAN_RegisterAddressChangeHandler(uCAN *ucan, AddressChangeHandler handler);
void uCAN_SetAddress(uCAN *ucan, HardareID hardware_id, uint8_t node_id);
// RAP methods
void uCAN_ConfigureRegisters(uCAN *ucan, RegisterHandlers *handlers);
uint8_t uCAN_ReadRegisters(uCAN *ucan, NodeAddress node, uint8_t page, uint8_t reg, uint8_t len, uint8_t *data);
uint8_t uCAN_WriteRegisters(uCAN *ucan, NodeAddress node, uint8_t page, uint8_t reg, uint8_t len, uint8_t *data);
#endif /* UCAN_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment