TTK4155 Lab Node 1
Term project in the NTNU course TTK4155 Embedded and Industrial Computer Systems Design
Loading...
Searching...
No Matches
can.c File Reference
#include <avr/interrupt.h>
#include <avr/io.h>
#include <stdint.h>
#include <util/delay.h>
#include "can.h"
#include "log.h"
#include "mcp.h"
#include "spi.h"

Go to the source code of this file.

Functions

 LOG_MODULE_DEFINE ("CAN")
int8_t can_init (struct can_device *dev)
 Initialize a CAN device.
int8_t can_write (struct can_device *dev, struct CAN_frame msg)
 Write a CAN frame to the MCP2515 transmit buffer.
int8_t can_read_rx0 (struct can_device *dev, struct CAN_frame *out)
 Read a message from RX buffer 0.
int8_t can_read_rx1 (struct can_device *dev, struct CAN_frame *out)
 Read a message from RX buffer 1.
int can_rxq_add (struct CAN_frame *msg)
 Add a CAN frame to the internal receive queue.
int can_rxq_pull (struct CAN_frame *out)
 Pull a CAN frame from the internal receive queue.
 ISR (INT2_vect)
int8_t MCP2515_init (struct can_device *dev)
int8_t MCP2515_write (struct can_device *dev, uint8_t addr, uint8_t data)
int8_t MCP2515_write_n (struct can_device *dev, uint8_t addr, uint8_t *data)
int8_t MCP2515_read (struct can_device *dev, uint8_t addr, uint8_t *out)
int8_t MCP2515_request_to_send (struct can_device *dev, uint8_t buffer)
int8_t MCP2515_bit_modify (struct can_device *dev, uint8_t reg, uint8_t mask, uint8_t set_val)
int8_t MCP2515_reset (struct can_device *dev)
int8_t MCP2515_read_status (struct can_device *dev, uint8_t *out)

Variables

struct can_devicecan_irq
struct CAN_frame new_message
static volatile uint8_t rxq_head = 0
static volatile uint8_t rxq_tail = 0
struct CAN_frame rxq [CAN_RXQ_SIZE]

Function Documentation

◆ ISR()

ISR ( INT2_vect )

Definition at line 186 of file can.c.

186 {
187
188 // LOG_INF("External interrupt")
189 uint8_t status;
191 &status); // MCP2515_read_status(can_irq, &status);
192
193 if (status & MCP2515_RX0IF) {
196 LOG_INF("RX0 Full");
197 }
198
199 if (status & MCP2515_RX1IF) {
202 LOG_INF("RX1 Full");
203 }
204
205 if (status & tx_buff_0_busy) {
206 LOG_INF("TX0 Busy");
207 }
208
209 if (status & tx_buff_0_empty) {
210 LOG_INF("TX0 Empty");
211 }
212
213 // reset CANINTF / clear flag
215}
struct can_device * can_irq
Definition can.c:13
struct CAN_frame new_message
Definition can.c:14
int can_rxq_add(struct CAN_frame *msg)
Add a CAN frame to the internal receive queue.
Definition can.c:159
int8_t can_read_rx1(struct can_device *dev, struct CAN_frame *out)
Read a message from RX buffer 1.
Definition can.c:127
int8_t MCP2515_read(struct can_device *dev, uint8_t addr, uint8_t *out)
Definition can.c:354
int8_t MCP2515_write(struct can_device *dev, uint8_t addr, uint8_t data)
Definition can.c:318
int8_t can_read_rx0(struct can_device *dev, struct CAN_frame *out)
Read a message from RX buffer 0.
Definition can.c:95
#define LOG_INF(msg)
Definition log.h:59
#define MCP2515_CANINTF
Definition mcp.h:44
#define tx_buff_0_busy
Definition mcp.h:167
#define MCP2515_RX0IF
Definition mcp.h:141
#define tx_buff_0_empty
Definition mcp.h:168
#define MCP2515_RX1IF
Definition mcp.h:142

◆ LOG_MODULE_DEFINE()

LOG_MODULE_DEFINE ( "CAN" )

◆ MCP2515_write_n()

int8_t MCP2515_write_n ( struct can_device * dev,
uint8_t addr,
uint8_t * data )

Definition at line 326 of file can.c.

326 {
327
328 unsigned char frame[10] = {MCP2515_WRITE, addr, data[0], data[1], data[2],
329 data[3], data[4], data[5], data[6], data[7]};
330 spi_send_n(&dev->spi, frame, 2 + 8);
331
332 return 0;
333}
int spi_send_n(enum spi_slave *slave, unsigned char *data, int length)
Send a sequence of bytes to a slave without reading response.
Definition spi.c:81
#define MCP2515_WRITE
Definition mcp.h:70
enum spi_slave spi
Definition can.h:44

Variable Documentation

◆ can_irq

struct can_device* can_irq

Definition at line 13 of file can.c.

Referenced by can_init(), and ISR().

◆ new_message

struct CAN_frame new_message

Definition at line 14 of file can.c.

Referenced by ISR().

◆ rxq

struct CAN_frame rxq[CAN_RXQ_SIZE]

Definition at line 17 of file can.c.

Referenced by can_rxq_add(), and can_rxq_pull().

◆ rxq_head

volatile uint8_t rxq_head = 0
static

Definition at line 16 of file can.c.

Referenced by can_rxq_add(), and can_rxq_pull().

◆ rxq_tail

volatile uint8_t rxq_tail = 0
static

Definition at line 16 of file can.c.

Referenced by can_rxq_add(), and can_rxq_pull().