Skip to content
Snippets Groups Projects
can.h 1.21 KiB
//
// Created by Sil on 15/07/2019.
//

#ifndef SUFST_OPENLOGGER_CONTROLLER_CAN_H
#define SUFST_OPENLOGGER_CONTROLLER_CAN_H

#include "Arduino.h"

// CAN_INT_PIN MUST BE 2
#define CAN_INT_PIN 2
#define CAN_CS_PIN 9

#define CAN_RX_BUFFER_LEN 32
#define CAN_TX_BUFFER_LEN 32

#define CAN_RESET_DEVICE_ON_TIMEOUT 0
#define CAN_NO_MSG_TIMEOUT_RESET_MS 10000

#define CAN_TX_RETRY_LIMIT 0

#define DEBUG_CAN_RX 0
#define DEBUG_CAN_RX_MINIMAL 0

#define DEBUG_CAN_TX 0
#define DEBUG_CAN_TX_MINIMAL 0
#define DEBUG_CAN_TX_REQS 0

typedef struct CanMsgTemplate
{
    uint32_t timestamp;

    union
    {
        uint16_t id;
        // if id is 0x2001 then idByte[0] is 0x01 and idByte[1] is 0x20
        // due to the endianness of the system
        uint8_t idByte[2];
    };

    uint8_t len;

    union
    {
        uint8_t data[8];
        uint16_t data16[4];
        uint32_t data32[2];
        float dataFl[2];
    };

};

uint8_t canBegin();

uint8_t canSaveMsg();

uint8_t canAddTxRequest(uint16_t id, void *data, uint8_t lenBytes);

void canBeginExtInterrupt();

void canParseRxMsg(CanMsgTemplate *canMsg);

void canProcessRx();

uint8_t canProcessTx();

void canNoActivityResetDevice();

#endif //SUFST_OPENLOGGER_CONTROLLER_CAN_H