[bsp] [stm32] fix drv_can.c
This commit is contained in:
parent
3b252d5b97
commit
c81eebcd5d
File diff suppressed because it is too large
Load Diff
|
@ -11,11 +11,16 @@
|
|||
* 2019-02-19 YLZ port to BSP [stm32]
|
||||
* 2019-06-17 YLZ modify struct stm32_drv_can.
|
||||
*/
|
||||
|
||||
#ifndef __DRV_CAN_H__
|
||||
#define __DRV_CAN_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <board.h>
|
||||
#include <rtdevice.h>
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#define BS1SHIFT 16
|
||||
|
@ -27,24 +32,28 @@
|
|||
#define RRESCLMASK (0x3FF << RRESCLSHIFT )
|
||||
#define SJWMASK (0x3 << SJWSHIFT )
|
||||
|
||||
struct stm_baud_rate_tab
|
||||
struct stm32_baud_rate_tab
|
||||
{
|
||||
rt_uint32_t baud_rate;
|
||||
rt_uint32_t confdata;
|
||||
rt_uint32_t config_data;
|
||||
};
|
||||
#define BAUD_DATA(TYPE,NO) ((can_baud_rate_tab[NO].config_data & TYPE##MASK))
|
||||
|
||||
/* STM32 can driver */
|
||||
struct stm32_drv_can
|
||||
/* stm32 can device */
|
||||
struct stm32_can
|
||||
{
|
||||
char *name;
|
||||
CAN_HandleTypeDef CanHandle;
|
||||
CAN_FilterTypeDef FilterConfig;
|
||||
struct rt_can_device device; /* inherit from can device */
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
int rt_hw_can_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /*__DRV_CAN_H__ */
|
||||
|
||||
/************************** end of file ******************/
|
||||
|
|
|
@ -49,7 +49,7 @@ if GetDepend(['RT_USING_USB_HOST']) or GetDepend(['RT_USING_USB_DEVICE']):
|
|||
src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_ll_usb.c']
|
||||
|
||||
if GetDepend(['RT_USING_CAN']):
|
||||
src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_can.c']
|
||||
src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_fdcan.c']
|
||||
|
||||
if GetDepend(['RT_USING_HWTIMER']) or GetDepend(['RT_USING_PWM']):
|
||||
src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_tim.c']
|
||||
|
|
|
@ -27,10 +27,15 @@ static rt_err_t rt_can_init(struct rt_device *dev)
|
|||
/* initialize rx/tx */
|
||||
can->can_rx = RT_NULL;
|
||||
can->can_tx = RT_NULL;
|
||||
#ifdef RT_CAN_USING_HDR
|
||||
can->hdr = RT_NULL;
|
||||
#endif
|
||||
|
||||
/* apply configuration */
|
||||
if (can->ops->configure)
|
||||
result = can->ops->configure(can, &can->config);
|
||||
else
|
||||
result = -RT_ENOSYS;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -293,7 +298,7 @@ static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag)
|
|||
can->can_rx = rx_fifo;
|
||||
|
||||
dev->open_flag |= RT_DEVICE_FLAG_INT_RX;
|
||||
/* configure low level device */
|
||||
/* open can rx interrupt */
|
||||
can->ops->control(can, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
|
||||
}
|
||||
}
|
||||
|
@ -325,7 +330,7 @@ static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag)
|
|||
can->can_tx = tx_fifo;
|
||||
|
||||
dev->open_flag |= RT_DEVICE_FLAG_INT_TX;
|
||||
/* configure low level device */
|
||||
/* open can tx interrupt */
|
||||
can->ops->control(can, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_TX);
|
||||
}
|
||||
}
|
||||
|
@ -406,7 +411,7 @@ static rt_err_t rt_can_close(struct rt_device *dev)
|
|||
rt_free(rx_fifo);
|
||||
dev->open_flag &= ~RT_DEVICE_FLAG_INT_RX;
|
||||
can->can_rx = RT_NULL;
|
||||
/* configure low level device */
|
||||
/* clear can rx interrupt */
|
||||
can->ops->control(can, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_FLAG_INT_RX);
|
||||
}
|
||||
|
||||
|
@ -420,7 +425,7 @@ static rt_err_t rt_can_close(struct rt_device *dev)
|
|||
rt_free(tx_fifo);
|
||||
dev->open_flag &= ~RT_DEVICE_FLAG_INT_TX;
|
||||
can->can_tx = RT_NULL;
|
||||
/* configure low level device */
|
||||
/* clear can tx interrupt */
|
||||
can->ops->control(can, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_FLAG_INT_TX);
|
||||
}
|
||||
|
||||
|
@ -484,6 +489,7 @@ static rt_err_t rt_can_control(struct rt_device *dev,
|
|||
struct rt_can_device *can;
|
||||
rt_err_t res;
|
||||
|
||||
res = RT_EOK;
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
can = (struct rt_can_device *)dev;
|
||||
|
||||
|
@ -501,8 +507,9 @@ static rt_err_t rt_can_control(struct rt_device *dev,
|
|||
|
||||
case RT_DEVICE_CTRL_CONFIG:
|
||||
/* configure device */
|
||||
can->ops->configure(can, (struct can_configure *)args);
|
||||
res = can->ops->configure(can, (struct can_configure *)args);
|
||||
break;
|
||||
|
||||
case RT_CAN_CMD_SET_PRIV:
|
||||
/* configure device */
|
||||
if ((rt_uint32_t)args != can->config.privmode)
|
||||
|
@ -513,7 +520,6 @@ static rt_err_t rt_can_control(struct rt_device *dev,
|
|||
|
||||
res = can->ops->control(can, cmd, args);
|
||||
if (res != RT_EOK) return res;
|
||||
|
||||
tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;
|
||||
if (can->config.privmode)
|
||||
{
|
||||
|
@ -544,7 +550,6 @@ static rt_err_t rt_can_control(struct rt_device *dev,
|
|||
rt_hw_interrupt_enable(level);
|
||||
}
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -561,13 +566,13 @@ static rt_err_t rt_can_control(struct rt_device *dev,
|
|||
return res;
|
||||
}
|
||||
|
||||
{
|
||||
struct rt_can_filter_config *pfilter;
|
||||
struct rt_can_filter_item *pitem;
|
||||
rt_uint32_t count;
|
||||
rt_base_t level;
|
||||
|
||||
pfilter = (struct rt_can_filter_config *)args;
|
||||
RT_ASSERT(pfilter);
|
||||
count = pfilter->count;
|
||||
pitem = pfilter->items;
|
||||
if (pfilter->actived)
|
||||
|
@ -630,7 +635,6 @@ static rt_err_t rt_can_control(struct rt_device *dev,
|
|||
pitem++;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
#endif /*RT_CAN_USING_HDR*/
|
||||
#ifdef RT_CAN_USING_BUS_HOOK
|
||||
|
@ -642,12 +646,16 @@ static rt_err_t rt_can_control(struct rt_device *dev,
|
|||
/* control device */
|
||||
if (can->ops->control != RT_NULL)
|
||||
{
|
||||
can->ops->control(can, cmd, args);
|
||||
res = can->ops->control(can, cmd, args);
|
||||
}
|
||||
else
|
||||
{
|
||||
res = -RT_ENOSYS;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
return res;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -655,8 +663,10 @@ static rt_err_t rt_can_control(struct rt_device *dev,
|
|||
*/
|
||||
static void cantimeout(void *arg)
|
||||
{
|
||||
rt_can_t can = (rt_can_t)arg;
|
||||
rt_can_t can;
|
||||
|
||||
can = (rt_can_t)arg;
|
||||
RT_ASSERT(can);
|
||||
rt_device_control((rt_device_t)can, RT_CAN_CMD_GET_STATUS, (void *)&can->status);
|
||||
|
||||
if (can->status_indicate.ind != RT_NULL)
|
||||
|
|
|
@ -221,7 +221,7 @@ struct rt_can_msg
|
|||
rt_uint32_t rsv : 1;
|
||||
rt_uint32_t len : 8;
|
||||
rt_uint32_t priv : 8;
|
||||
rt_uint32_t hdr : 8;
|
||||
rt_int32_t hdr : 8;
|
||||
rt_uint32_t reserved : 8;
|
||||
rt_uint8_t data[8];
|
||||
};
|
||||
|
@ -252,7 +252,7 @@ struct rt_can_rx_fifo
|
|||
|
||||
#define RT_CAN_EVENT_RX_IND 0x01 /* Rx indication */
|
||||
#define RT_CAN_EVENT_TX_DONE 0x02 /* Tx complete */
|
||||
#define RT_CAN_EVENT_TX_FAIL 0x03 /* Tx complete */
|
||||
#define RT_CAN_EVENT_TX_FAIL 0x03 /* Tx fail */
|
||||
#define RT_CAN_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
|
||||
#define RT_CAN_EVENT_RXOF_IND 0x06 /* Rx overflow */
|
||||
|
||||
|
|
|
@ -847,7 +847,7 @@ long list_device(void)
|
|||
device->parent.name,
|
||||
(device->type <= RT_Device_Class_Unknown) ?
|
||||
device_type_str[device->type] :
|
||||
device_type_str[RT_Device_Class_Unknown],
|
||||
device_type_str[RT_Device_Class_Unknown - 1],
|
||||
device->ref_count);
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue