lib: Add can2040 code

The "can2040" project implements "software canbus" support on rp2040
micro-controllers.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2022-06-02 12:40:18 -04:00
parent 1636a9759b
commit f10fd7c2fa
3 changed files with 1312 additions and 0 deletions

View File

@ -128,3 +128,7 @@ The canboot directory contains code from:
revision 870200826561b150137913d42fd7edc6515229ff. The Python module, revision 870200826561b150137913d42fd7edc6515229ff. The Python module,
flash_can.py, is taken from the repo's scripts directory. It may be flash_can.py, is taken from the repo's scripts directory. It may be
used to upload firmware to devices flashed with the CanBoot bootloader. used to upload firmware to devices flashed with the CanBoot bootloader.
The can2040 directory contains code from:
https://github.com/KevinOConnor/can2040
revision 17b8ace15584077cd0bf0c3e038c2a2a8edd70ed.

1230
lib/can2040/can2040.c Normal file

File diff suppressed because it is too large Load Diff

78
lib/can2040/can2040.h Normal file
View File

@ -0,0 +1,78 @@
#ifndef _CAN2040_H
#define _CAN2040_H
#include <stdint.h> // uint32_t
struct can2040_msg {
uint32_t id;
uint32_t dlc;
union {
uint8_t data[8];
uint32_t data32[2];
};
};
enum {
CAN2040_ID_RTR = 1<<30,
CAN2040_ID_EFF = 1<<31,
};
enum {
CAN2040_NOTIFY_RX = 1<<20,
CAN2040_NOTIFY_TX = 1<<21,
CAN2040_NOTIFY_ERROR = 1<<23,
};
struct can2040;
typedef void (*can2040_rx_cb)(struct can2040 *cd, uint32_t notify
, struct can2040_msg *msg);
void can2040_setup(struct can2040 *cd, uint32_t pio_num);
void can2040_callback_config(struct can2040 *cd, can2040_rx_cb rx_cb);
void can2040_start(struct can2040 *cd, uint32_t sys_clock, uint32_t bitrate
, uint32_t gpio_rx, uint32_t gpio_tx);
void can2040_shutdown(struct can2040 *cd);
void can2040_pio_irq_handler(struct can2040 *cd);
int can2040_check_transmit(struct can2040 *cd);
int can2040_transmit(struct can2040 *cd, struct can2040_msg *msg);
/****************************************************************
* Internal definitions
****************************************************************/
struct can2040_bitunstuffer {
uint32_t stuffed_bits, count_stuff;
uint32_t unstuffed_bits, count_unstuff;
};
struct can2040_transmit {
struct can2040_msg msg;
uint32_t crc, stuffed_words, stuffed_data[5];
};
struct can2040 {
// Setup
uint32_t pio_num;
void *pio_hw;
uint32_t gpio_rx, gpio_tx;
can2040_rx_cb rx_cb;
// Bit unstuffing
struct can2040_bitunstuffer unstuf;
uint32_t raw_bit_count;
// Input data state
uint32_t parse_state;
uint32_t parse_crc;
struct can2040_msg parse_msg;
// Reporting
uint32_t report_state;
// Transmits
uint32_t tx_state;
uint32_t tx_pull_pos, tx_push_pos;
struct can2040_transmit tx_queue[4];
};
#endif // can2040.h