2018-06-12 14:23:20 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Karsten Koenig
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _MCP2515_H_
|
|
|
|
#define _MCP2515_H_
|
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/gpio.h>
|
|
|
|
#include <zephyr/drivers/can.h>
|
2018-06-12 14:23:20 +08:00
|
|
|
|
2019-07-21 12:59:53 +08:00
|
|
|
#define MCP2515_RX_CNT 2
|
2022-02-11 00:04:27 +08:00
|
|
|
/* Reduce the number of Tx buffers to 1 in order to avoid priority inversion. */
|
|
|
|
#define MCP2515_TX_CNT 1
|
2018-06-12 14:23:20 +08:00
|
|
|
#define MCP2515_FRAME_LEN 13
|
|
|
|
|
|
|
|
struct mcp2515_tx_cb {
|
|
|
|
struct k_sem sem;
|
|
|
|
can_tx_callback_t cb;
|
2019-02-13 17:26:17 +08:00
|
|
|
void *cb_arg;
|
2018-06-12 14:23:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mcp2515_data {
|
|
|
|
/* interrupt data */
|
|
|
|
struct gpio_callback int_gpio_cb;
|
|
|
|
struct k_thread int_thread;
|
|
|
|
k_thread_stack_t *int_thread_stack;
|
|
|
|
struct k_sem int_sem;
|
|
|
|
|
|
|
|
/* tx data */
|
|
|
|
struct k_sem tx_sem;
|
|
|
|
struct mcp2515_tx_cb tx_cb[MCP2515_TX_CNT];
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t tx_busy_map;
|
2018-06-12 14:23:20 +08:00
|
|
|
|
|
|
|
/* filter data */
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t filter_usage;
|
2021-01-09 01:48:17 +08:00
|
|
|
can_rx_callback_t rx_cb[CONFIG_CAN_MAX_FILTER];
|
|
|
|
void *cb_arg[CONFIG_CAN_MAX_FILTER];
|
2022-08-11 21:31:59 +08:00
|
|
|
struct can_filter filter[CONFIG_CAN_MAX_FILTER];
|
2021-12-29 03:00:34 +08:00
|
|
|
can_state_change_callback_t state_change_cb;
|
2022-01-10 19:32:19 +08:00
|
|
|
void *state_change_cb_data;
|
2018-06-20 00:26:31 +08:00
|
|
|
|
|
|
|
/* general data */
|
|
|
|
struct k_mutex mutex;
|
|
|
|
enum can_state old_state;
|
2022-08-31 20:28:42 +08:00
|
|
|
uint8_t mcp2515_mode;
|
|
|
|
bool started;
|
2021-04-25 23:15:56 +08:00
|
|
|
uint8_t sjw;
|
2018-06-12 14:23:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mcp2515_config {
|
|
|
|
/* spi configuration */
|
2021-08-01 14:00:49 +08:00
|
|
|
struct spi_dt_spec bus;
|
2018-06-12 14:23:20 +08:00
|
|
|
|
|
|
|
/* interrupt configuration */
|
2022-01-27 22:42:01 +08:00
|
|
|
struct gpio_dt_spec int_gpio;
|
2018-06-12 14:23:20 +08:00
|
|
|
size_t int_thread_stack_size;
|
|
|
|
int int_thread_priority;
|
|
|
|
|
|
|
|
/* CAN timing */
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t tq_sjw;
|
|
|
|
uint8_t tq_prop;
|
|
|
|
uint8_t tq_bs1;
|
|
|
|
uint8_t tq_bs2;
|
|
|
|
uint32_t bus_speed;
|
|
|
|
uint32_t osc_freq;
|
2020-04-28 00:58:05 +08:00
|
|
|
uint16_t sample_point;
|
2022-03-01 21:45:01 +08:00
|
|
|
|
|
|
|
/* CAN transceiver */
|
|
|
|
const struct device *phy;
|
|
|
|
uint32_t max_bitrate;
|
2018-06-12 14:23:20 +08:00
|
|
|
};
|
|
|
|
|
2020-04-28 00:58:05 +08:00
|
|
|
/*
|
|
|
|
* Startup time of 128 OSC1 clock cycles at 1MHz (minimum clock in frequency)
|
|
|
|
* see MCP2515 datasheet section 8.1 Oscillator Start-up Timer
|
|
|
|
*/
|
|
|
|
#define MCP2515_OSC_STARTUP_US 128U
|
|
|
|
|
2018-06-12 14:23:20 +08:00
|
|
|
/* MCP2515 Opcodes */
|
|
|
|
#define MCP2515_OPCODE_WRITE 0x02
|
|
|
|
#define MCP2515_OPCODE_READ 0x03
|
|
|
|
#define MCP2515_OPCODE_BIT_MODIFY 0x05
|
2019-08-07 22:08:08 +08:00
|
|
|
#define MCP2515_OPCODE_LOAD_TX_BUFFER 0x40
|
2019-08-07 22:23:34 +08:00
|
|
|
#define MCP2515_OPCODE_RTS 0x80
|
2019-07-21 12:59:53 +08:00
|
|
|
#define MCP2515_OPCODE_READ_RX_BUFFER 0x90
|
2018-06-12 14:23:20 +08:00
|
|
|
#define MCP2515_OPCODE_READ_STATUS 0xA0
|
|
|
|
#define MCP2515_OPCODE_RESET 0xC0
|
|
|
|
|
|
|
|
/* MCP2515 Registers */
|
|
|
|
#define MCP2515_ADDR_CANSTAT 0x0E
|
|
|
|
#define MCP2515_ADDR_CANCTRL 0x0F
|
2018-06-20 00:26:31 +08:00
|
|
|
#define MCP2515_ADDR_TEC 0x1C
|
|
|
|
#define MCP2515_ADDR_REC 0x1D
|
2018-06-12 14:23:20 +08:00
|
|
|
#define MCP2515_ADDR_CNF3 0x28
|
|
|
|
#define MCP2515_ADDR_CNF2 0x29
|
|
|
|
#define MCP2515_ADDR_CNF1 0x2A
|
|
|
|
#define MCP2515_ADDR_CANINTE 0x2B
|
|
|
|
#define MCP2515_ADDR_CANINTF 0x2C
|
2018-06-20 00:26:31 +08:00
|
|
|
#define MCP2515_ADDR_EFLG 0x2D
|
2018-06-12 14:23:20 +08:00
|
|
|
#define MCP2515_ADDR_TXB0CTRL 0x30
|
|
|
|
#define MCP2515_ADDR_TXB1CTRL 0x40
|
|
|
|
#define MCP2515_ADDR_TXB2CTRL 0x50
|
|
|
|
#define MCP2515_ADDR_RXB0CTRL 0x60
|
|
|
|
#define MCP2515_ADDR_RXB1CTRL 0x70
|
|
|
|
|
|
|
|
#define MCP2515_ADDR_OFFSET_FRAME2FRAME 0x10
|
|
|
|
#define MCP2515_ADDR_OFFSET_CTRL2FRAME 0x01
|
|
|
|
|
|
|
|
/* MCP2515 Operation Modes */
|
|
|
|
#define MCP2515_MODE_NORMAL 0x00
|
|
|
|
#define MCP2515_MODE_LOOPBACK 0x02
|
|
|
|
#define MCP2515_MODE_SILENT 0x03
|
|
|
|
#define MCP2515_MODE_CONFIGURATION 0x04
|
|
|
|
|
|
|
|
/* MCP2515_FRAME_OFFSET */
|
|
|
|
#define MCP2515_FRAME_OFFSET_SIDH 0
|
|
|
|
#define MCP2515_FRAME_OFFSET_SIDL 1
|
|
|
|
#define MCP2515_FRAME_OFFSET_EID8 2
|
|
|
|
#define MCP2515_FRAME_OFFSET_EID0 3
|
|
|
|
#define MCP2515_FRAME_OFFSET_DLC 4
|
|
|
|
#define MCP2515_FRAME_OFFSET_D0 5
|
|
|
|
|
|
|
|
/* MCP2515_CANINTF */
|
|
|
|
#define MCP2515_CANINTF_RX0IF BIT(0)
|
|
|
|
#define MCP2515_CANINTF_RX1IF BIT(1)
|
|
|
|
#define MCP2515_CANINTF_TX0IF BIT(2)
|
|
|
|
#define MCP2515_CANINTF_TX1IF BIT(3)
|
|
|
|
#define MCP2515_CANINTF_TX2IF BIT(4)
|
|
|
|
#define MCP2515_CANINTF_ERRIF BIT(5)
|
|
|
|
#define MCP2515_CANINTF_WAKIF BIT(6)
|
|
|
|
#define MCP2515_CANINTF_MERRF BIT(7)
|
|
|
|
|
2018-06-20 00:26:31 +08:00
|
|
|
#define MCP2515_INTE_RX0IE BIT(0)
|
|
|
|
#define MCP2515_INTE_RX1IE BIT(1)
|
|
|
|
#define MCP2515_INTE_TX0IE BIT(2)
|
|
|
|
#define MCP2515_INTE_TX1IE BIT(3)
|
|
|
|
#define MCP2515_INTE_TX2IE BIT(4)
|
|
|
|
#define MCP2515_INTE_ERRIE BIT(5)
|
|
|
|
#define MCP2515_INTE_WAKIE BIT(6)
|
|
|
|
#define MCP2515_INTE_MERRE BIT(7)
|
|
|
|
|
|
|
|
#define MCP2515_EFLG_EWARN BIT(0)
|
|
|
|
#define MCP2515_EFLG_RXWAR BIT(1)
|
|
|
|
#define MCP2515_EFLG_TXWAR BIT(2)
|
|
|
|
#define MCP2515_EFLG_RXEP BIT(3)
|
|
|
|
#define MCP2515_EFLG_TXEP BIT(4)
|
|
|
|
#define MCP2515_EFLG_TXBO BIT(5)
|
|
|
|
#define MCP2515_EFLG_RX0OVR BIT(6)
|
|
|
|
#define MCP2515_EFLG_RX1OVR BIT(7)
|
2018-06-12 14:23:20 +08:00
|
|
|
|
|
|
|
#define MCP2515_TXCTRL_TXREQ BIT(3)
|
|
|
|
|
|
|
|
#define MCP2515_CANSTAT_MODE_POS 5
|
|
|
|
#define MCP2515_CANSTAT_MODE_MASK (0x07 << MCP2515_CANSTAT_MODE_POS)
|
|
|
|
#define MCP2515_CANCTRL_MODE_POS 5
|
|
|
|
#define MCP2515_CANCTRL_MODE_MASK (0x07 << MCP2515_CANCTRL_MODE_POS)
|
|
|
|
|
|
|
|
#endif /*_MCP2515_H_*/
|