1.新增CAN设备测试版本。
This commit is contained in:
383
device/can.c
Normal file
383
device/can.c
Normal file
@@ -0,0 +1,383 @@
|
||||
/*
|
||||
* @copyright (c) 2023, MR Development Team
|
||||
*
|
||||
* @license SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* @date 2023-11-22 MacRsh First version
|
||||
*/
|
||||
|
||||
#include "include/device/can.h"
|
||||
|
||||
#ifdef MR_USING_CAN
|
||||
|
||||
static int mr_can_bus_open(struct mr_dev *dev)
|
||||
{
|
||||
struct mr_can_bus *can_bus = (struct mr_can_bus *)dev;
|
||||
struct mr_can_bus_ops *ops = (struct mr_can_bus_ops *)dev->drv->ops;
|
||||
|
||||
/* Reset the hold */
|
||||
can_bus->hold = MR_FALSE;
|
||||
|
||||
return ops->configure(can_bus, &can_bus->config);
|
||||
}
|
||||
|
||||
static int mr_can_bus_close(struct mr_dev *dev)
|
||||
{
|
||||
struct mr_can_bus *can_bus = (struct mr_can_bus *)dev;
|
||||
struct mr_can_bus_ops *ops = (struct mr_can_bus_ops *)dev->drv->ops;
|
||||
struct mr_can_config close_config = {0};
|
||||
|
||||
return ops->configure(can_bus, &close_config);
|
||||
}
|
||||
|
||||
static ssize_t mr_can_bus_read(struct mr_dev *dev, int off, void *buf, size_t size, int async)
|
||||
{
|
||||
return MR_EIO;
|
||||
}
|
||||
|
||||
static ssize_t mr_can_bus_write(struct mr_dev *dev, int off, const void *buf, size_t size, int async)
|
||||
{
|
||||
return MR_EIO;
|
||||
}
|
||||
|
||||
static ssize_t mr_can_bus_isr(struct mr_dev *dev, int event, void *args)
|
||||
{
|
||||
struct mr_can_bus *can_bus = (struct mr_can_bus *)dev;
|
||||
struct mr_can_bus_ops *ops = (struct mr_can_bus_ops *)dev->drv->ops;
|
||||
|
||||
switch (event)
|
||||
{
|
||||
case MR_ISR_CAN_RD_INT:
|
||||
{
|
||||
struct mr_list *list = MR_NULL;
|
||||
uint8_t data[8] = {0};
|
||||
int id = ops->get_id(can_bus);
|
||||
ssize_t ret = ops->read(can_bus, data, sizeof(data));
|
||||
|
||||
/* Search the matching device */
|
||||
for (list = dev->slist.next; list != &dev->slist; list = list->next)
|
||||
{
|
||||
struct mr_can_dev *can_dev = (struct mr_can_dev *)mr_container_of(list, struct mr_dev, list);
|
||||
|
||||
/* Check id is valid */
|
||||
if (can_dev->id == (id & ((1 << 29) - 1)))
|
||||
{
|
||||
/* Read data to FIFO. if callback is set, call it */
|
||||
mr_ringbuf_write_force(&can_dev->rd_fifo, data, ret);
|
||||
if (can_dev->dev.rd_call.call != MR_NULL)
|
||||
{
|
||||
ssize_t size = (ssize_t)mr_ringbuf_get_data_size(&can_dev->rd_fifo);
|
||||
can_dev->dev.rd_call.call(can_dev->dev.rd_call.desc, &size);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return MR_ENOTSUP;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
return MR_ENOTSUP;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function registers a can-bus.
|
||||
*
|
||||
* @param can_dev The can-device.
|
||||
* @param name The name of the can-bus.
|
||||
* @param drv The driver of the can-bus.
|
||||
*
|
||||
* @return MR_EOK on success, otherwise an error code.
|
||||
*/
|
||||
int mr_can_bus_register(struct mr_can_bus *can_bus, const char *name, struct mr_drv *drv)
|
||||
{
|
||||
static struct mr_dev_ops ops =
|
||||
{
|
||||
mr_can_bus_open,
|
||||
mr_can_bus_close,
|
||||
mr_can_bus_read,
|
||||
mr_can_bus_write,
|
||||
MR_NULL,
|
||||
mr_can_bus_isr
|
||||
};
|
||||
struct mr_can_config default_config = MR_CAN_CONFIG_DEFAULT;
|
||||
|
||||
mr_assert(can_bus != MR_NULL);
|
||||
mr_assert(name != MR_NULL);
|
||||
mr_assert(drv != MR_NULL);
|
||||
mr_assert(drv->ops != MR_NULL);
|
||||
|
||||
/* Initialize the fields */
|
||||
can_bus->config = default_config;
|
||||
can_bus->owner = MR_NULL;
|
||||
|
||||
/* Register the can-bus */
|
||||
return mr_dev_register(&can_bus->dev, name, Mr_Dev_Type_Can, MR_SFLAG_RDWR, &ops, drv);
|
||||
}
|
||||
|
||||
static int can_dev_filter_configure(struct mr_can_dev *can_dev, int id, int ide, int state)
|
||||
{
|
||||
struct mr_can_bus *can_bus = (struct mr_can_bus *)can_dev->dev.link;
|
||||
struct mr_can_bus_ops *ops = (struct mr_can_bus_ops *)can_bus->dev.drv->ops;
|
||||
|
||||
return ops->filter_configure(can_bus, id, ide, state);
|
||||
}
|
||||
|
||||
MR_INLINE int can_dev_take_bus(struct mr_can_dev *can_dev)
|
||||
{
|
||||
struct mr_can_bus *can_bus = (struct mr_can_bus *)can_dev->dev.link;
|
||||
struct mr_can_bus_ops *ops = (struct mr_can_bus_ops *)can_bus->dev.drv->ops;
|
||||
|
||||
if ((can_dev != can_bus->owner) && (can_bus->owner != MR_NULL))
|
||||
{
|
||||
return MR_EBUSY;
|
||||
}
|
||||
|
||||
if (can_dev != can_bus->owner)
|
||||
{
|
||||
if (can_dev->config.baud_rate != can_bus->config.baud_rate)
|
||||
{
|
||||
int ret = ops->configure(can_bus, &can_dev->config);
|
||||
if (ret != MR_EOK)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
can_bus->config = can_dev->config;
|
||||
can_bus->owner = can_dev;
|
||||
}
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
MR_INLINE int can_dev_release_bus(struct mr_can_dev *can_dev)
|
||||
{
|
||||
struct mr_can_bus *can_bus = (struct mr_can_bus *)can_dev->dev.link;
|
||||
|
||||
if (can_dev != can_bus->owner)
|
||||
{
|
||||
return MR_EINVAL;
|
||||
}
|
||||
|
||||
can_bus->owner = MR_NULL;
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
MR_INLINE ssize_t can_dev_read(struct mr_can_dev *can_dev, uint8_t *buf, size_t size)
|
||||
{
|
||||
struct mr_can_bus *can_bus = (struct mr_can_bus *)can_dev->dev.link;
|
||||
struct mr_can_bus_ops *ops = (struct mr_can_bus_ops *)can_bus->dev.drv->ops;
|
||||
ssize_t rd_size = 0;
|
||||
|
||||
for (rd_size = 0; rd_size < size;)
|
||||
{
|
||||
/* Check id is valid */
|
||||
int id = ops->get_id(can_bus);
|
||||
if (can_dev->id != (id & ((1 << 29) - 1)))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
ssize_t ret = ops->read(can_bus, (buf + rd_size), (size - rd_size));
|
||||
if (ret <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
rd_size += ret;
|
||||
}
|
||||
return rd_size;
|
||||
}
|
||||
|
||||
MR_INLINE ssize_t can_dev_write(struct mr_can_dev *can_dev, int id, int ide, int rtr, const uint8_t *buf, size_t size)
|
||||
{
|
||||
struct mr_can_bus *can_bus = (struct mr_can_bus *)can_dev->dev.link;
|
||||
struct mr_can_bus_ops *ops = (struct mr_can_bus_ops *)can_bus->dev.drv->ops;
|
||||
|
||||
return ops->write(can_bus, id, ide, rtr, buf, size);
|
||||
}
|
||||
|
||||
static int mr_can_dev_open(struct mr_dev *dev)
|
||||
{
|
||||
struct mr_can_dev *can_dev = (struct mr_can_dev *)dev;
|
||||
|
||||
/* Allocate FIFO buffers */
|
||||
int ret = mr_ringbuf_allocate(&can_dev->rd_fifo, can_dev->rd_bufsz);
|
||||
if (ret != MR_EOK)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
return can_dev_filter_configure(can_dev->dev.link, can_dev->id, can_dev->ide, MR_ENABLE);
|
||||
}
|
||||
|
||||
static int mr_can_dev_close(struct mr_dev *dev)
|
||||
{
|
||||
struct mr_can_dev *can_dev = (struct mr_can_dev *)dev;
|
||||
|
||||
/* Free FIFO buffers */
|
||||
mr_ringbuf_free(&can_dev->rd_fifo);
|
||||
|
||||
return can_dev_filter_configure(can_dev->dev.link, can_dev->id, can_dev->ide, MR_DISABLE);
|
||||
}
|
||||
|
||||
static ssize_t mr_can_dev_read(struct mr_dev *dev, int off, void *buf, size_t size, int async)
|
||||
{
|
||||
struct mr_can_dev *can_dev = (struct mr_can_dev *)dev;
|
||||
|
||||
ssize_t ret = can_dev_take_bus(can_dev);
|
||||
if (ret != MR_EOK)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (mr_ringbuf_get_bufsz(&can_dev->rd_fifo) == 0)
|
||||
{
|
||||
ret = can_dev_read(can_dev, (uint8_t *)buf, size);
|
||||
} else
|
||||
{
|
||||
ret = (ssize_t)mr_ringbuf_read(&can_dev->rd_fifo, buf, size);
|
||||
}
|
||||
|
||||
can_dev_release_bus(can_dev);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ssize_t mr_can_dev_write(struct mr_dev *dev, int off, const void *buf, size_t size, int async)
|
||||
{
|
||||
struct mr_can_dev *can_dev = (struct mr_can_dev *)dev;
|
||||
|
||||
/* Check offset is valid */
|
||||
if (off < 0)
|
||||
{
|
||||
return MR_EINVAL;
|
||||
}
|
||||
|
||||
ssize_t ret = can_dev_take_bus(can_dev);
|
||||
if (ret != MR_EOK)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = can_dev_write(can_dev,
|
||||
(off & ((1 << 29) - 1)),
|
||||
mr_bits_is_set(off, MR_CAN_IDE_EXT),
|
||||
mr_bits_is_set(off, MR_CAN_RTR_REMOTE),
|
||||
(uint8_t *)buf,
|
||||
size);
|
||||
|
||||
can_dev_release_bus(can_dev);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int mr_can_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
|
||||
{
|
||||
struct mr_can_dev *can_dev = (struct mr_can_dev *)dev;
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case MR_CTRL_SET_CONFIG:
|
||||
{
|
||||
if (args != MR_NULL)
|
||||
{
|
||||
struct mr_can_bus *can_bus = (struct mr_can_bus *)dev->link;
|
||||
struct mr_can_config config = *(struct mr_can_config *)args;
|
||||
|
||||
/* If holding the bus, release it */
|
||||
if (can_dev == can_bus->owner)
|
||||
{
|
||||
can_bus->hold = MR_FALSE;
|
||||
can_bus->owner = MR_NULL;
|
||||
}
|
||||
|
||||
can_dev->config = config;
|
||||
return MR_EOK;
|
||||
}
|
||||
return MR_EINVAL;
|
||||
}
|
||||
case MR_CTRL_SET_RD_BUFSZ:
|
||||
{
|
||||
if (args != MR_NULL)
|
||||
{
|
||||
size_t bufsz = *(size_t *)args;
|
||||
|
||||
int ret = mr_ringbuf_allocate(&can_dev->rd_fifo, bufsz);
|
||||
can_dev->rd_bufsz = 0;
|
||||
if (ret == MR_EOK)
|
||||
{
|
||||
can_dev->rd_bufsz = bufsz;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
return MR_EINVAL;
|
||||
}
|
||||
|
||||
case MR_CTRL_GET_CONFIG:
|
||||
{
|
||||
if (args != MR_NULL)
|
||||
{
|
||||
struct mr_can_config *config = (struct mr_can_config *)args;
|
||||
|
||||
*config = can_dev->config;
|
||||
return MR_EOK;
|
||||
}
|
||||
return MR_EINVAL;
|
||||
}
|
||||
case MR_CTRL_GET_RD_BUFSZ:
|
||||
{
|
||||
if (args != MR_NULL)
|
||||
{
|
||||
*(size_t *)args = can_dev->rd_bufsz;
|
||||
return MR_EOK;
|
||||
}
|
||||
return MR_EINVAL;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
return MR_ENOTSUP;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function registers a can-device.
|
||||
*
|
||||
* @param can_dev The can-device.
|
||||
* @param name The name of the can-device.
|
||||
* @param id The id of the can-device.
|
||||
* @param ide The id identifier of the can-device.
|
||||
*
|
||||
* @return MR_EOK on success, otherwise an error code.
|
||||
*/
|
||||
int mr_can_dev_register(struct mr_can_dev *can_dev, const char *name, int id, int ide)
|
||||
{
|
||||
static struct mr_dev_ops ops =
|
||||
{
|
||||
mr_can_dev_open,
|
||||
mr_can_dev_close,
|
||||
mr_can_dev_read,
|
||||
mr_can_dev_write,
|
||||
mr_can_dev_ioctl,
|
||||
MR_NULL
|
||||
};
|
||||
struct mr_can_config default_config = MR_CAN_CONFIG_DEFAULT;
|
||||
|
||||
mr_assert(can_dev != MR_NULL);
|
||||
mr_assert(name != MR_NULL);
|
||||
|
||||
/* Initialize the fields */
|
||||
can_dev->config = default_config;
|
||||
mr_ringbuf_init(&can_dev->rd_fifo, MR_NULL, 0);
|
||||
#ifndef MR_CFG_CAN_RD_BUFSZ
|
||||
#define MR_CFG_CAN_RD_BUFSZ (0)
|
||||
#endif /* MR_CFG_CAN_RD_BUFSZ */
|
||||
can_dev->rd_bufsz = MR_CFG_CAN_RD_BUFSZ;
|
||||
can_dev->id = id;
|
||||
can_dev->ide = ide;
|
||||
|
||||
/* Register the can-dev */
|
||||
return mr_dev_register(&can_dev->dev, name, Mr_Dev_Type_Can, MR_SFLAG_RDWR | MR_SFLAG_NONDRV, &ops, MR_NULL);
|
||||
}
|
||||
|
||||
#endif /* MR_USING_CAN */
|
||||
@@ -93,7 +93,7 @@
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
2. 函数中功能模块应分块书写,当出现较大块且无法通过代码简单明了的解读时需要标注代码块功能。当返回值无特殊功能时允许紧贴其他代码块,当有其余功能时,需单独成块。
|
||||
|
||||
```c
|
||||
|
||||
@@ -19,28 +19,28 @@ extern "C" {
|
||||
#include "drv_adc.h"
|
||||
#endif
|
||||
|
||||
#ifdef MR_USING_DAC
|
||||
#include "drv_dac.h"
|
||||
#ifdef MR_USING_CAN
|
||||
#include "drv_can.h"
|
||||
#endif
|
||||
|
||||
#ifdef MR_USING_GPIO
|
||||
#include "drv_gpio.h"
|
||||
#ifdef MR_USING_DAC
|
||||
#include "drv_dac.h"
|
||||
#endif
|
||||
|
||||
#ifdef MR_USING_I2C
|
||||
#include "drv_i2c.h"
|
||||
#endif
|
||||
|
||||
#ifdef MR_USING_SPI
|
||||
#include "drv_spi.h"
|
||||
#ifdef MR_USING_PIN
|
||||
#include "drv_gpio.h"
|
||||
#endif
|
||||
|
||||
#ifdef MR_USING_UART
|
||||
#ifdef MR_USING_SERIAL
|
||||
#include "drv_uart.h"
|
||||
#endif
|
||||
|
||||
#ifdef MR_USING_PWM
|
||||
#include "drv_pwm.h"
|
||||
#ifdef MR_USING_SPI
|
||||
#include "drv_spi.h"
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
127
include/device/can.h
Normal file
127
include/device/can.h
Normal file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* @copyright (c) 2023, MR Development Team
|
||||
*
|
||||
* @license SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* @date 2023-11-22 MacRsh First version
|
||||
*/
|
||||
|
||||
#ifndef _CAN_H_
|
||||
#define _CAN_H_
|
||||
|
||||
#include "mr_api.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#ifdef MR_USING_CAN
|
||||
|
||||
/**
|
||||
* @brief CAN default configuration.
|
||||
*/
|
||||
#define MR_CAN_CONFIG_DEFAULT \
|
||||
{ \
|
||||
500000, \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CAN configuration structure.
|
||||
*/
|
||||
struct mr_can_config
|
||||
{
|
||||
uint32_t baud_rate; /**< Baud rate */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief CAN identifier.
|
||||
*/
|
||||
#define MR_CAN_IDE_STD (0 << 29)
|
||||
#define MR_CAN_IDE_EXT (1 << 29)
|
||||
|
||||
/**
|
||||
* @brief CAN remote transmission request.
|
||||
*/
|
||||
#define MR_CAN_RTR_DATA (0 << 30)
|
||||
#define MR_CAN_RTR_REMOTE (1 << 30)
|
||||
|
||||
/**
|
||||
* @brief Help to set id.
|
||||
*/
|
||||
#define MR_CAN_ID(id, ide, rtr) ((id) | (ide) | (rtr)) /**< Set id-ide-rtr */
|
||||
|
||||
/**
|
||||
* @brief CAN id command.
|
||||
*/
|
||||
#define MR_CTRL_CAN_SET_ID MR_CTRL_SET_OFFSET /**< Set id */
|
||||
#define MR_CTRL_CAN_GET_ID MR_CTRL_GET_OFFSET /**< Get id */
|
||||
|
||||
/**
|
||||
* @brief CAN data type.
|
||||
*/
|
||||
typedef uint8_t mr_can_data_t; /**< CAN read/write data type */
|
||||
|
||||
/**
|
||||
* @brief CAN ISR events.
|
||||
*/
|
||||
#define MR_ISR_CAN_RD_INT (MR_ISR_RD | (0x01 << 16)) /**< Read interrupt */
|
||||
|
||||
/**
|
||||
* @brief CAN bus structure.
|
||||
*/
|
||||
struct mr_can_bus
|
||||
{
|
||||
struct mr_dev dev; /**< Device */
|
||||
|
||||
struct mr_can_config config; /**< Configuration */
|
||||
volatile void *owner; /**< Owner */
|
||||
volatile int hold; /**< Owner hold */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief CAN bus operations structure.
|
||||
*/
|
||||
struct mr_can_bus_ops
|
||||
{
|
||||
int (*configure)(struct mr_can_bus *can_bus, struct mr_can_config *config);
|
||||
int (*filter_configure)(struct mr_can_bus *can_bus, int id, int ide, int state);
|
||||
int (*get_id)(struct mr_can_bus *can_bus);
|
||||
ssize_t (*read)(struct mr_can_bus *can_bus, uint8_t *buf, size_t size);
|
||||
ssize_t (*write)(struct mr_can_bus *can_bus, int id, int ide, int rtr, const uint8_t *buf, size_t size);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief CAN id identifier.
|
||||
*/
|
||||
#define MR_CAN_ID_STD (0) /**< Standard identifier */
|
||||
#define MR_CAN_ID_EXT (1) /**< Extended identifier */
|
||||
|
||||
/**
|
||||
* @brief CAN device structure.
|
||||
*/
|
||||
struct mr_can_dev
|
||||
{
|
||||
struct mr_dev dev;
|
||||
|
||||
struct mr_can_config config;
|
||||
struct mr_ringbuf rd_fifo;
|
||||
size_t rd_bufsz;
|
||||
uint32_t id: 29;
|
||||
uint32_t ide: 1;
|
||||
uint32_t reserved: 2;
|
||||
};
|
||||
|
||||
/**
|
||||
* @addtogroup CAN.
|
||||
* @{
|
||||
*/
|
||||
int mr_can_bus_register(struct mr_can_bus *can_bus, const char *name, struct mr_drv *drv);
|
||||
int mr_can_dev_register(struct mr_can_dev *can_dev, const char *name, int id, int ide);
|
||||
/** @} */
|
||||
#endif /* MR_USING_CAN */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* _CAN_H_ */
|
||||
13
mr_config.h
13
mr_config.h
@@ -18,13 +18,13 @@ extern "C" {
|
||||
|
||||
#define MR_USING_RDWR_CTRL
|
||||
#define MR_USING_ADC
|
||||
#define MR_USING_DAC
|
||||
#define MR_USING_PIN
|
||||
#define MR_USING_I2C
|
||||
#define MR_USING_SPI
|
||||
#define MR_USING_SERIAL
|
||||
#define MR_USING_TIMER
|
||||
#define MR_USING_CAN
|
||||
#define MR_USING_DAC
|
||||
#define MR_USING_I2C
|
||||
#define MR_USING_PIN
|
||||
#define MR_USING_SERIAL
|
||||
#define MR_USING_SPI
|
||||
#define MR_USING_TIMER
|
||||
|
||||
#define MR_CFG_HEAP_SIZE (4 * 1024)
|
||||
#define MR_CFG_CONSOLE_NAME "uart1"
|
||||
@@ -32,6 +32,7 @@ extern "C" {
|
||||
#define MR_CFG_SERIAL_WR_BUFSZ (0)
|
||||
#define MR_CFG_SPI_RD_BUFSZ (32)
|
||||
#define MR_CFG_I2C_RD_BUFSZ (32)
|
||||
#define MR_CFG_CAN_RD_BUFSZ (32)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user