balcony_weather_station/can_bus.h
2025-09-06 21:25:32 +02:00

38 lines
934 B
C

#include "boards/pico.h"
#include "hardware/spi.h"
#include "pico/stdlib.h"
#include "pico/time.h"
// 16MHz 125Kbps
#define MCP_CFG1 (0x03)
#define MCP_CFG2 (0xF0)
#define MCP_CFG3 (0x86)
typedef struct {
spi_inst_t *CHANNEL;
uint8_t CS_PIN;
// uint8_t TX_PIN;
// uint8_t RX_PIN;
// uint8_t SCK_PIN;
// uint32_t SPI_CLOCK;
} mcp2515_config;
struct can_frame {
uint32_t can_id; // 32 bit CAN_ID + EFF/RTR/ERR flags
char can_dlc; // frame payload length in byte (0 .. CAN_MAX_DLEN) */
char data[8]; // msg
};
enum MCP_ERROR {
ERROR_OK = 0,
ERROR_FAIL = 1,
ERROR_ALLTXBUSY = 2,
ERROR_FAILINIT = 3,
ERROR_FAILTX = 4,
ERROR_NOMSG = 5
};
void mcp2515_init(mcp2515_config *config, uint8_t cs_pin, uint8_t tx_pin,
uint8_t rx_pin, uint8_t sck_pin, spi_inst_t *spi_inst);
enum MCP_ERROR sendMessage(const struct can_frame *frame);
enum MPC_ERROR readMessage(struct can_frame *frame);