| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- /*!
- @file iso15765_2.h
- @brief Header file of the ISO15765-2 library
- @t.odo -
- ---------------------------------------------------------------------------
- MIT License
- Copyright (c) 2020 Io. D (Devcoons.com)
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- */
- /******************************************************************************
- * Preprocessor Definitions & Macros
- ******************************************************************************/
- #ifndef DEVCOONS_ISO15765_2_H_
- #define DEVCOONS_ISO15765_2_H_
- #define I15765_MSG_SIZE 516 /* Max. size of the TP up to 4095 bytes */
- #define I15765_QUEUE_ELMS 32 /* No. of max incoming frames that the* reception buffer can hold */
- #define ARM_M4 1
- /* Alignment is required for Microcontrollers */
- #if defined(__clang__)
- #define ALIGNMENT __attribute__ ((aligned (4)))
- #elif defined(__GNUC__) || defined(__GNUG__)
- #define ALIGNMENT __attribute__ ((aligned (4)))
- #elif defined(_MSC_VER)
- #define ALIGNMENT __declspec(align(4))
- #endif
- #ifndef ISO_15675_UNUSED
- #define ISO_15675_UNUSED(x) ((void)(x))
- #endif
- /******************************************************************************
- * Includes
- ******************************************************************************/
- #include <stdlib.h>
- #include <stdint.h>
- #include "lib_iqueue.h"
- /******************************************************************************
- * Enumerations, structures & Variables
- ******************************************************************************/
- /* --- CANTP Mode - Frame Type (ref: ) ----------------- */
- #ifndef CBUS_FR_FORMAT
- #define CBUS_FR_FORMAT
- typedef enum
- {
- CBUS_FR_FRM_STD = 0x01, /* Standard CANBUS */
- CBUS_FR_FRM_FD = 0x02 /* FD CANBUS */
- } cbus_fr_format;
- #endif
- /* --- CANBus Mode [ID Type] (ref: iso15765-2 p.8) -------==---------------- */
- #ifndef CBUS_ID_TYPE
- #define CBUS_ID_TYPE
- typedef enum
- {
- CBUS_ID_T_STANDARD = 0x04U, /* 11bits CAN Identifier */
- CBUS_ID_T_EXTENDED = 0x08U /* 29bits CAN Identifier */
- } cbus_id_type;
- #endif
- /* --- CANBus Frame (ref: iso15765-2 p.) ----------------------------------- */
- #ifndef CANBUS_FRAME
- #define CANBUS_FRAME
- typedef struct
- {
- uint32_t id; /* CAN Frame Id */
- uint32_t id_type; /* CAN Frame Id Type `cbus_id_type` */
- uint16_t fr_format; /* CAN Frame Format `cbus_fr_format` */
- uint16_t dlc; /* Size of data */
- uint8_t dt[8]; /* Actual data of the frame */
- } canbus_frame_t;
- #endif
- /* --- CANTP Addressing Mode (ref: iso15765-2 p.28) ------------------------ */
- typedef enum
- {
- N_ADM_UNKN = 0x00, /* Not defined */
- N_ADM_NORMAL = 0x14, /* Normal Mode */
- N_ADM_FIXED = 0x28, /* Extended Mode */
- N_ADM_MIXED11 = 0x35, /* Mixed 11bits ID Mode */
- N_ADM_EXTENDED = 0x45, /* Fixed Mode */
- N_ADM_MIXED29 = 0x59, /* Mixed 29bits ID Mode */
- N_ADM_BMW = 0x60, /* BMW */
- } addr_md;
- /* --- Protocol Control Information Type (ref: iso15765-2 p.17) ------------ */
- typedef enum
- {
- N_PCI_T_SF = 0x00U, /* Single Frame µ¥Ö¡*/
- N_PCI_T_FF = 0x01U, /* First Frame Ê×Ö¡*/
- N_PCI_T_CF = 0x02U, /* Consecutive Frame Á¬ÐøÖ¡*/
- N_PCI_T_FC = 0x03U, /* Flow Control Frame Á÷¿ØÖ¡*/
- N_PCI_T_UN = 0xFF /* Unknown */
- } pci_type;
- /* --- Network Target Address Type (ref: iso15765-2 p.9,29,30) ------------- */
- typedef enum
- {
- N_TA_T_PHY = 0x01, /* Physical */
- N_TA_T_FUNC = 0x02 /* Functional */
- } ta_type;
- /* --- Message Type (ref: iso15765-2 p.8) ---------------------------------- */
- typedef enum
- {
- N_MT_DIAG = 0x00, /* Diagnostics */
- N_MT_RDIAG = 0x01 /* Remote Diagnostics */
- } mtype;
- /* --- FlowControl Parameters (ref: iso15765-2 p.--------------------------- */
- typedef enum
- {
- N_ST_MIN = 0x00U, /* SeparationTimeMin: the minimum time the sender should wait between the transmissions of two CF N_PDUs */
- N_BS = 0x01U, /* BlockSize: the max. number of N_PDUs the receiver allows the sender to send, before waiting for an authorization to continue transmission of the following N_PDUs */
- } fl_param;
- /* --- N rslts (ref: iso15765-2 p.10-11) ----------------------------------- */
- typedef enum
- {
- N_OK = 0x0000, /* service execution has completed successfully */
- N_TIMEOUT_A = 0x0001, /* when the timer N_Ar/N_As has passed its time-out * value N_Asmax / N_Armax; */
- N_TIMEOUT_Bs = 0x0002, /* when the timer N_Bs has passed its time-out value * N_Bsmax */
- N_TIMEOUT_Cr = 0x0003, /* when the timer N_Cr has passed its time-out value * N_Crmax */
- N_WRG_SN = 0x0004, /* upon reception of an unexpected sequence number * (PCI.SN) value */
- N_INV_FS = 0x0005, /* when an invalid or unknown FlowStatus value has * been received in a flow control(FC) N_PDU */
- N_UNE_PDU = 0x0006, /* upon reception of an unexpected protocol data unit; */
- N_WFT_OVRN = 0x0007, /* upon reception of flow control WAIT frame that * exceeds the maximum counter N_WFTmax */
- N_BUFFER_OVFLW = 0x0008, /* upon reception of a flow control (FC) N_PDU with * FlowStatus = OVFLW */
- N_ERROR = 0x0009, /* when an error has been detected by the network layer * and no other parameter value can be used to better * describe the error. */
- N_IDLE = 0x0010, /* service execution has completed successfully and not TXRX */
- N_TX_BUSY = 0x0011, /* service execution has completed successfully and TX pending */
- N_RX_BUSY = 0x0012, /* service execution has completed successfully and RX pending */
- N_WRG_PARAM = 0x0013, /* the service did not execute due to an undefined <Parameter> */
- N_WRG_VALUE = 0x0014, /* the service did not execute due to an out of range <Parameter_Value>; */
- N_INV = 0x0015, /* the service did not execute due to an invalid requested operation */
- N_MISSING_CLB = 0x0016, /* the service did not execute due to missing mandatory callback functions */
- N_NULL = 0x0017, /* the service has a null pointer (this should never happen) */
- N_FC_TIMEOUT = 0x0018, /* Flow Control Timeout. @n_timeouts.n_bs */
- N_CF_TIMEOUT = 0x0019, /* ConsecutiveFrame Timeout. @n_timeouts.n_cr */
- N_CAN_DT_INV = 0x0020, /* Invalid CAN Data Error */
- N_UNE_FC = 0x0021, /* FlowControl was received without being expected. */
- N_UNE_CF = 0x0022, /* ConsecutiveFrame was received without being expected. */
- N_RCV_INT_SF = 0x0023, /* Frames sequence was not correct. Service received a * single frame during a Multi-frame reception. */
- N_RCV_INT_FF = 0x0024, /* Frames sequence was not correct. Service received a * first frame during a Multi-frame reception. */
- N_RCV_INT_FC = 0x0025, /* ReceptionInterruptedWithFC */
- N_INV_SEQ_NUM = 0x0026, /* ConsecutiveFrame sequence number was invalid. */
- N_UNS_WF = 0x0027, /* Unsuported WaitFrame */
- N_MAX_WF_REAC = 0x0028, /* Maximum Wait Frames Reached */
- N_INV_PDU = 0x0029, /* PDU Conversion failed */
- N_INV_REQ_SZ = 0x0030, /* Invalid Request Size */
- N_UNE_FC_STS = 0x0031, /* Flow control status was invalid and/or unexpected. */
- N_OVFLW = 0x0032, /* Buffer Overflow. */
- } n_rslt;
- /* --- N_PCI_T_FC Status (ref: iso15765-2 p.8) ---------------------------- */
- typedef enum
- {
- N_CONTINUE = 0x00U, /* continue to send, the authorization to continue */
- N_WAIT = 0x01U, /* the request to continue to wait */
- N_OVERFLOW = 0x02U /* buffer overflow, the indication that the number of bytes specified in the FirstFrame of the segmented message exceeds the number of bytes that can be stored in the buffer of the receiver entity */
- } flow_sts;
- /* --- dt I/O Stream Status (ref: iso15765-2 p.8) ------------------------ */
- typedef enum
- {
- N_S_IDLE = 0x00, /* Has to pending action */
- N_S_RX_BUSY = 0x02, /* Reception is in progress */
- N_S_TX_BUSY = 0x04, /* Transmission is in progress */
- N_S_TX_READY = 0x05, /* Transmission is ready to begin */
- N_S_TX_WAIT_FC = 0x10, /* Transmission is in progress and waits
- * for a Flow control frame */
- } stream_sts;
- /* --- Network Layer Timing PARAMs (ref: iso15765-2 p.) -------------------- */
- #if ARM_M4
- #pragma pack(4)
- typedef struct
- #else
- typedef struct ALIGNMENT
- #endif //ARM_M4
- {
- uint32_t n_bs; /* FlowControl N_PDU not received (lost, overwritten) on the
- * sender side or preceding FirstFrame N_PDU or ConsecutiveFrame
- * N_PDU not received(lost, overwritten) on the receiver side.
- * Abort message transmission and issue N_USData.confirm with
- * <N_Result> = N_TIMEOUT_Bs. */
- uint32_t n_cr; /* ConsecutiveFrame N_PDU not received (lost, overwritten) on
- * the receiver side or preceding FC N_PDU not received(lost,
- * overwritten) on the sender side. Abort message reception and issue
- * N_USData.indication with <N_Result> = N_TIMEOUT_Cr */
- uint32_t n_cs;
- } n_timeouts;
- #if ARM_M4
- #pragma pack()
- #endif //ARM_M4
- /* --- Address information (ref: iso15765-2 p.) ---------------------------- */
- #if ARM_M4
- #pragma pack(4)
- typedef struct
- #else
- typedef struct ALIGNMENT
- #endif //ARM_M4
- {
- uint8_t n_pr; /* Network Address Priority */
- uint8_t n_sa; /* Network Source Address */
- uint8_t n_ta; /* Network Target Address */
- uint8_t n_ae; /* Network Address Extension */
- ta_type n_tt; /* Network Target Address type */
- } n_ai_t;
- #if ARM_M4
- #pragma pack()
- #endif //ARM_M4
- /* --- Protocol control information (ref: iso15765-2 p.) ------------------- */
- #if ARM_M4
- #pragma pack(4)
- typedef struct
- #else
- typedef struct ALIGNMENT
- #endif //ARM_M4
- {
- uint8_t fs; /* FlowStatus: whether the sending network entity can proceed with the message transmission 'flow_sts' */
- uint8_t bs; /* BlockSize */
- uint8_t sn; /* SequenceNumber: specify the order of the consecutive frames */
- uint8_t st; /* SeparationTime: Requested separation time */
- pci_type pt; /* Type of the received pdu 'pci_type' */
- uint16_t dl; /* PCI data length (if 0 frame must be ignored) */
- } n_pci_t;
- #if ARM_M4
- #pragma pack()
- #endif //ARM_M4
- /* --- Protocol dt unit (ref: iso15765-2 p.) ------------------------------- */
- #if ARM_M4
- #pragma pack(4)
- typedef struct
- #else
- typedef struct ALIGNMENT
- #endif //ARM_M4
- {
- mtype n_mt; /* Message Type */
- uint16_t sz; /* Actual data size */
- n_ai_t n_ai; /* Address information */
- n_pci_t n_pci; /* Protocol control information */
- uint8_t dt[128]; /* Data Field */
- uint8_t sf_dt[8]; /* single frame data field*/
- } n_pdu_t;
- #if ARM_M4
- #pragma pack()
- #endif //ARM_M4
- /* --- N_USdt.cfm (ref: iso15765-2 p.6) ------------------------------------ */
- #if ARM_M4
- #pragma pack(4)
- typedef struct
- #else
- typedef struct ALIGNMENT
- #endif //ARM_M4
- {
- n_ai_t n_ai; /* Address information */
- n_pci_t n_pci; /* Protocol control information */
- n_rslt rslt; /* Result of the request */
- pci_type frame_type; /* Received frame type*/
- } n_cfm_t;
- #if ARM_M4
- #pragma pack()
- #endif //ARM_M4
- /* --- N_FF.indn (ref: iso15765-2 p.6) ------------------------------------ */
- #if ARM_M4
- #pragma pack(4)
- typedef struct
- #else
- typedef struct ALIGNMENT
- #endif //ARM_M4
- {
- cbus_fr_format fr_fmt; /* CANBus Frame format */
- n_ai_t n_ai; /* Address information */
- n_pci_t n_pci; /* Protocol control information */
- uint16_t msg_sz; /* Size of the message that will be received */
- pci_type frame_type; /* Received frame type*/
- } n_ff_indn_t;
- #if ARM_M4
- #pragma pack()
- #endif //ARM_M4
- /* --- N_USData.request (ref: iso15765-2 p6.) ------------------------------ */
- #if ARM_M4
- #pragma pack(4)
- typedef struct
- #else
- typedef struct ALIGNMENT
- #endif //ARM_M4
- {
- cbus_fr_format fr_fmt; /* CANBus Frame format */
- n_ai_t n_ai; /* Address information */
- n_pci_t n_pci; /* Protocol control information */
- uint16_t msg_sz; /* Message actual size */
- uint8_t msg[I15765_MSG_SIZE]; /* Message to be transmitted */
- } n_req_t;
- #if ARM_M4
- #pragma pack()
- #endif //ARM_M4
- /* --- N_USdt.indn (ref: iso15765-2 p.7) ---------------------------------- */
- #if ARM_M4
- #pragma pack(4)
- typedef struct
- #else
- typedef struct ALIGNMENT
- #endif //ARM_M4
- {
- cbus_fr_format fr_fmt; /* CANBus Frame format */
- n_ai_t n_ai; /* Address information */
- n_pci_t n_pci; /* Protocol control information */
- n_rslt rslt; /* Result of the reception */
- uint16_t msg_sz; /* Received message actual size */
- pci_type frame_type; /* Received frame type*/
- uint8_t msg[I15765_MSG_SIZE]; /* Received message data */
- uint8_t sf_msg[8]; /* Received single frame message data */
- } n_indn_t;
- #if ARM_M4
- #pragma pack()
- #endif //ARM_M4
- #if ARM_M4
- #pragma pack(4)
- typedef struct
- #else
- typedef struct ALIGNMENT
- #endif //ARM_M4
- {
- n_ai_t n_ai; /* Address information. Not supported:the lib handles only 1 stream */
- n_pci_t n_pci; /* Protocol control information. Not supported:the lib handles only 1 stream) */
- fl_param param; /* Parameter to change */
- uint8_t pval; /* Value to set */
- } n_chg_param_req_t;
- #if ARM_M4
- #pragma pack()
- #endif //ARM_M4
- #if ARM_M4
- #pragma pack(4)
- typedef struct
- #else
- typedef struct ALIGNMENT
- #endif //ARM_M4
- {
- n_ai_t n_ai; /* Address information. Not supported:the lib handles only 1 stream */
- n_pci_t n_pci; /* Protocol control information. Not supported: because the lib handles only 1 stream */
- fl_param param; /* Requested Parameter to change */
- uint8_t pval; /* Requested Value to set */
- n_rslt rslt; /* Result of the request */
- } n_chg_param_cfm_t;
- #if ARM_M4
- #pragma pack()
- #endif //ARM_M4
- typedef enum
- {
- N_INDN = 0x01, /* N_USData.indication */
- N_FF_INDN = 0x02, /* N_USData_FF.indication */
- N_CONF = 0x03, /* N_USData.confirm */
- N_CHG_P_CONF = 0x04, /* N_ChangeParameter.confirm */
- N_SF_INDN = 0x05, /* N_USData.single frame indication */
- } signal_tp;
- /* --- Callbacks ---------------------------------------------------------- */
- #if ARM_M4
- #pragma pack(4)
- typedef struct
- #else
- typedef struct ALIGNMENT
- #endif //ARM_M4
- {
- void (*indn)(n_indn_t *); /* Indication Callback: Will be fired when a reception is available or an error occured during the reception. */
- void (*ff_indn)(n_ff_indn_t *); /* First Frame Indication Callback: Will be fired when a FF is received, giving back some useful information */
- void (*cfm)(n_cfm_t *); /* This callback confirms to the higher layers that the requested service has been carried out */
- void (*cfg_cfm)(n_chg_param_cfm_t *); /* This service confirms to the upper layer that the request to change a specific protocol has been carried out */
- void (*pdu_custom_pack)(n_pdu_t *, uint32_t *); /* Custom CAN ID packing for 11bits ID. If assinged the default packing will be skipped */
- void (*pdu_custom_unpack)(n_pdu_t *, uint32_t *); /* Custom CAN ID uppacking for 11bits ID. If assinged the default uppacking will be skipped */
- void (*on_error)(n_rslt); /* Will be fired in any occured error. */
- uint32_t(*get_ms)(); /* Time-source for the library in ms(required) */
- uint8_t(*send_frame) /* Callback to assing the Network Layer. This callback will be fired when a transmission of a canbus frame is ready.*/
- (
- cbus_id_type, /* - CANBus Frame ID Type [Standard or Extended] */
- uint32_t, /* - Frame ID */
- cbus_fr_format, /* - Frame Type: [CLASSIC or FD]*/
- uint8_t, /* - Frame Data Length */
- uint8_t * /* - Frame Data Array */
- );
- } n_callbacks_t;
- #if ARM_M4
- #pragma pack()
- #endif //ARM_M4
- /* --- PDU Stream --------------------------------------------------------- */
- #if ARM_M4
- #pragma pack(4)
- typedef struct
- #else
- typedef struct ALIGNMENT
- #endif //ARM_M4
- {
- cbus_fr_format fr_fmt; /* CANBus Frame format */
- n_pdu_t pdu; /* Keep information about the in/out frame in a PDU format */
- uint8_t cf_cnt; /* Current block sequence number (ConsecutiveFrame) */
- uint8_t wf_cnt; /* Current received wait flow control frames */
- uint8_t cfg_wf; /* Max supported Wait Flow Control frames */
- uint8_t stmin; /* Frames transmission rate */
- uint8_t cfg_bs; /* Max. supported block sequence (ConsecutiveFrame) */
- stream_sts sts; /* Stream status */
- uint16_t msg_sz; /* Actual message buffer size */
- uint16_t msg_pos; /* Transmit message buffer position */
- n_timeouts last_upd; /* Time keeper for timouts */
- uint8_t msg[I15765_MSG_SIZE]; /* Received/Transmit message buffer */
- uint8_t sf_msg[8]; /* Received/Transmit single frame message buffer */
- } n_iostream_t;
- #if ARM_M4
- #pragma pack()
- #endif //ARM_M4
- /* --- iso15765 timing configuration (ref: iso15765-2 p.25)----------------- */
- #if ARM_M4
- #pragma pack(4)
- typedef struct
- #else
- typedef struct ALIGNMENT
- #endif //ARM_M4
- {
- uint8_t stmin; /* Default min. frame transmission separation */
- uint8_t bs; /* Max. Block size during transmission */
- uint8_t wf; /* Max. accepted Wait Requests from the FlowControl */
- uint16_t n_bs; /* Time until reception of the next FlowControl N_PDU */
- uint16_t n_cr; /* Time until reception of the next ConsecutiveFrame N_PDU */
- } n_config_t;
- #if ARM_M4
- #pragma pack()
- #endif //ARM_M4
- /* --- iso15765 Handler --------------------------------------------------- */
- #if ARM_M4
- #pragma pack(4)
- typedef struct
- #else
- typedef struct ALIGNMENT
- #endif //ARM_M4
- {
- addr_md addr_md; /* Selected address mode of the TP */
- cbus_id_type fr_id_type; /* CANBus frame Id Type */
- n_iostream_t in; /* Incoming data stream (reception) */
- n_iostream_t out; /* Outcoming data stream (transmission) */
- n_pdu_t fl_pdu; /* Flow control pdu */
- n_callbacks_t clbs; /* Callbacks */
- n_config_t config; /* Default configuration to be used. (timing etc) */
- n_timeouts cfg_timeout; /* Timeouts configuration */
- iqueue_t inqueue; /* Queue handler for the incoming canbus frames */
- uint8_t inq_buf[I15765_QUEUE_ELMS * sizeof(canbus_frame_t)]; /* Queue buffer */
- } iso15765_t;
- #if ARM_M4
- #pragma pack()
- #endif //ARM_M4
- /******************************************************************************
- * Declaration | Public Functions
- ******************************************************************************/
- n_rslt iso15765_init(iso15765_t *instance);
- n_rslt iso15765_send(iso15765_t *instance, n_req_t *frame);
- n_rslt iso15765_enqueue(iso15765_t *instance, canbus_frame_t *frame);
- n_rslt iso15765_process(iso15765_t *instance);
- /******************************************************************************
- * EOF - NO CODE AFTER THIS LINE
- ******************************************************************************/
- #endif
|