[bsp] [stm32] fix drv_can.c

This commit is contained in:
tyustli 2019-08-16 11:56:26 +08:00
parent 3b252d5b97
commit c81eebcd5d
6 changed files with 678 additions and 625 deletions

File diff suppressed because it is too large Load Diff

View File

@ -11,40 +11,49 @@
* 2019-02-19 YLZ port to BSP [stm32] * 2019-02-19 YLZ port to BSP [stm32]
* 2019-06-17 YLZ modify struct stm32_drv_can. * 2019-06-17 YLZ modify struct stm32_drv_can.
*/ */
#ifndef __DRV_CAN_H__ #ifndef __DRV_CAN_H__
#define __DRV_CAN_H__ #define __DRV_CAN_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <board.h> #include <board.h>
#include <rtdevice.h> #include <rtdevice.h>
#include <rthw.h>
#include <rtthread.h> #include <rtthread.h>
#define BS1SHIFT 16 #define BS1SHIFT 16
#define BS2SHIFT 20 #define BS2SHIFT 20
#define RRESCLSHIFT 0 #define RRESCLSHIFT 0
#define SJWSHIFT 24 #define SJWSHIFT 24
#define BS1MASK ( (0x0F) << BS1SHIFT ) #define BS1MASK ((0x0F) << BS1SHIFT )
#define BS2MASK ( (0x07) << BS2SHIFT ) #define BS2MASK ((0x07) << BS2SHIFT )
#define RRESCLMASK ( 0x3FF << RRESCLSHIFT ) #define RRESCLMASK (0x3FF << RRESCLSHIFT )
#define SJWMASK ( 0x3 << SJWSHIFT ) #define SJWMASK (0x3 << SJWSHIFT )
struct stm_baud_rate_tab struct stm32_baud_rate_tab
{ {
rt_uint32_t baud_rate; 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 */ /* stm32 can device */
struct stm32_drv_can struct stm32_can
{ {
char *name;
CAN_HandleTypeDef CanHandle; CAN_HandleTypeDef CanHandle;
CAN_FilterTypeDef FilterConfig; CAN_FilterTypeDef FilterConfig;
struct rt_can_device device; /* inherit from can device */
}; };
#ifdef __cplusplus
extern "C" {
#endif
int rt_hw_can_init(void); int rt_hw_can_init(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /*__DRV_CAN_H__ */ #endif /*__DRV_CAN_H__ */
/************************** end of file ******************/

View File

@ -29,9 +29,9 @@ STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_gpio.c
''') ''')
if GetDepend(['RT_USING_SERIAL']): if GetDepend(['RT_USING_SERIAL']):
src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c'] src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c']
src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_usart.c'] src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_usart.c']
src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart_ex.c'] src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart_ex.c']
if GetDepend(['RT_USING_I2C']): if GetDepend(['RT_USING_I2C']):
src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_i2c.c'] src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_i2c.c']
@ -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'] src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_ll_usb.c']
if GetDepend(['RT_USING_CAN']): 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']): if GetDepend(['RT_USING_HWTIMER']) or GetDepend(['RT_USING_PWM']):
src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_tim.c'] src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_tim.c']

View File

@ -27,10 +27,15 @@ static rt_err_t rt_can_init(struct rt_device *dev)
/* initialize rx/tx */ /* initialize rx/tx */
can->can_rx = RT_NULL; can->can_rx = RT_NULL;
can->can_tx = RT_NULL; can->can_tx = RT_NULL;
#ifdef RT_CAN_USING_HDR
can->hdr = RT_NULL;
#endif
/* apply configuration */ /* apply configuration */
if (can->ops->configure) if (can->ops->configure)
result = can->ops->configure(can, &can->config); result = can->ops->configure(can, &can->config);
else
result = -RT_ENOSYS;
return result; 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; can->can_rx = rx_fifo;
dev->open_flag |= RT_DEVICE_FLAG_INT_RX; 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); can->ops->control(can, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
} }
} }
@ -311,7 +316,7 @@ static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag)
tx_fifo->buffer = (struct rt_can_sndbxinx_list *)(tx_fifo + 1); tx_fifo->buffer = (struct rt_can_sndbxinx_list *)(tx_fifo + 1);
rt_memset(tx_fifo->buffer, 0, rt_memset(tx_fifo->buffer, 0,
can->config.sndboxnumber * sizeof(struct rt_can_sndbxinx_list)); can->config.sndboxnumber * sizeof(struct rt_can_sndbxinx_list));
rt_list_init(&tx_fifo->freelist); rt_list_init(&tx_fifo->freelist);
for (i = 0; i < can->config.sndboxnumber; i++) for (i = 0; i < can->config.sndboxnumber; i++)
{ {
@ -325,7 +330,7 @@ static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag)
can->can_tx = tx_fifo; can->can_tx = tx_fifo;
dev->open_flag |= RT_DEVICE_FLAG_INT_TX; 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); 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); rt_free(rx_fifo);
dev->open_flag &= ~RT_DEVICE_FLAG_INT_RX; dev->open_flag &= ~RT_DEVICE_FLAG_INT_RX;
can->can_rx = RT_NULL; 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); 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); rt_free(tx_fifo);
dev->open_flag &= ~RT_DEVICE_FLAG_INT_TX; dev->open_flag &= ~RT_DEVICE_FLAG_INT_TX;
can->can_tx = RT_NULL; 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); 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; struct rt_can_device *can;
rt_err_t res; rt_err_t res;
res = RT_EOK;
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
can = (struct rt_can_device *)dev; 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: case RT_DEVICE_CTRL_CONFIG:
/* configure device */ /* configure device */
can->ops->configure(can, (struct can_configure *)args); res = can->ops->configure(can, (struct can_configure *)args);
break; break;
case RT_CAN_CMD_SET_PRIV: case RT_CAN_CMD_SET_PRIV:
/* configure device */ /* configure device */
if ((rt_uint32_t)args != can->config.privmode) 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); res = can->ops->control(can, cmd, args);
if (res != RT_EOK) return res; if (res != RT_EOK) return res;
tx_fifo = (struct rt_can_tx_fifo *) can->can_tx; tx_fifo = (struct rt_can_tx_fifo *) can->can_tx;
if (can->config.privmode) if (can->config.privmode)
{ {
@ -522,11 +528,11 @@ static rt_err_t rt_can_control(struct rt_device *dev,
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
if(rt_list_isempty(&tx_fifo->buffer[i].list)) if(rt_list_isempty(&tx_fifo->buffer[i].list))
{ {
rt_sem_release(&(tx_fifo->sem)); rt_sem_release(&(tx_fifo->sem));
} }
else else
{ {
rt_list_remove(&tx_fifo->buffer[i].list); rt_list_remove(&tx_fifo->buffer[i].list);
} }
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
} }
@ -544,7 +550,6 @@ static rt_err_t rt_can_control(struct rt_device *dev,
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
} }
} }
return RT_EOK;
} }
break; break;
@ -561,74 +566,73 @@ static rt_err_t rt_can_control(struct rt_device *dev,
return res; 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)
{ {
struct rt_can_filter_config *pfilter; while (count)
struct rt_can_filter_item *pitem;
rt_uint32_t count;
rt_base_t level;
pfilter = (struct rt_can_filter_config *)args;
count = pfilter->count;
pitem = pfilter->items;
if (pfilter->actived)
{ {
while (count) if (pitem->hdr >= can->config.maxhdr || pitem->hdr < 0)
{ {
if (pitem->hdr >= can->config.maxhdr || pitem->hdr < 0) count--;
{ pitem++;
count--; continue;
pitem++; }
continue;
}
level = rt_hw_interrupt_disable();
if (!can->hdr[pitem->hdr].connected)
{
rt_hw_interrupt_enable(level);
rt_memcpy(&can->hdr[pitem->hdr].filter, pitem,
sizeof(struct rt_can_filter_item));
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
if (!can->hdr[pitem->hdr].connected) can->hdr[pitem->hdr].connected = 1;
can->hdr[pitem->hdr].msgs = 0;
rt_list_init(&can->hdr[pitem->hdr].list);
}
rt_hw_interrupt_enable(level);
count--;
pitem++;
}
}
else
{
while (count)
{
if (pitem->hdr >= can->config.maxhdr || pitem->hdr < 0)
{
count--;
pitem++;
continue;
}
level = rt_hw_interrupt_disable();
if (can->hdr[pitem->hdr].connected)
{
can->hdr[pitem->hdr].connected = 0;
can->hdr[pitem->hdr].msgs = 0;
if (!rt_list_isempty(&can->hdr[pitem->hdr].list))
{ {
rt_hw_interrupt_enable(level); rt_list_remove(can->hdr[pitem->hdr].list.next);
rt_memcpy(&can->hdr[pitem->hdr].filter, pitem,
sizeof(struct rt_can_filter_item));
level = rt_hw_interrupt_disable();
can->hdr[pitem->hdr].connected = 1;
can->hdr[pitem->hdr].msgs = 0;
rt_list_init(&can->hdr[pitem->hdr].list);
} }
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
rt_memset(&can->hdr[pitem->hdr].filter, 0,
count--; sizeof(struct rt_can_filter_item));
pitem++;
} }
} else
else
{
while (count)
{ {
if (pitem->hdr >= can->config.maxhdr || pitem->hdr < 0) rt_hw_interrupt_enable(level);
{
count--;
pitem++;
continue;
}
level = rt_hw_interrupt_disable();
if (can->hdr[pitem->hdr].connected)
{
can->hdr[pitem->hdr].connected = 0;
can->hdr[pitem->hdr].msgs = 0;
if (!rt_list_isempty(&can->hdr[pitem->hdr].list))
{
rt_list_remove(can->hdr[pitem->hdr].list.next);
}
rt_hw_interrupt_enable(level);
rt_memset(&can->hdr[pitem->hdr].filter, 0,
sizeof(struct rt_can_filter_item));
}
else
{
rt_hw_interrupt_enable(level);
}
count--;
pitem++;
} }
count--;
pitem++;
} }
} }
break; break;
@ -642,12 +646,16 @@ static rt_err_t rt_can_control(struct rt_device *dev,
/* control device */ /* control device */
if (can->ops->control != RT_NULL) if (can->ops->control != RT_NULL)
{ {
can->ops->control(can, cmd, args); res = can->ops->control(can, cmd, args);
}
else
{
res = -RT_ENOSYS;
} }
break; 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) 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); rt_device_control((rt_device_t)can, RT_CAN_CMD_GET_STATUS, (void *)&can->status);
if (can->status_indicate.ind != RT_NULL) if (can->status_indicate.ind != RT_NULL)
@ -690,7 +700,7 @@ const static struct rt_device_ops can_device_ops =
/* /*
* can register * can register
*/ */
rt_err_t rt_hw_can_register(struct rt_can_device *can, rt_err_t rt_hw_can_register(struct rt_can_device *can,
const char *name, const char *name,
const struct rt_can_ops *ops, const struct rt_can_ops *ops,
void *data) void *data)

View File

@ -221,7 +221,7 @@ struct rt_can_msg
rt_uint32_t rsv : 1; rt_uint32_t rsv : 1;
rt_uint32_t len : 8; rt_uint32_t len : 8;
rt_uint32_t priv : 8; rt_uint32_t priv : 8;
rt_uint32_t hdr : 8; rt_int32_t hdr : 8;
rt_uint32_t reserved : 8; rt_uint32_t reserved : 8;
rt_uint8_t data[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_RX_IND 0x01 /* Rx indication */
#define RT_CAN_EVENT_TX_DONE 0x02 /* Tx complete */ #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_RX_TIMEOUT 0x05 /* Rx timeout */
#define RT_CAN_EVENT_RXOF_IND 0x06 /* Rx overflow */ #define RT_CAN_EVENT_RXOF_IND 0x06 /* Rx overflow */
@ -278,7 +278,7 @@ struct rt_can_ops
int (*recvmsg)(struct rt_can_device *can, void *buf, rt_uint32_t boxno); int (*recvmsg)(struct rt_can_device *can, void *buf, rt_uint32_t boxno);
}; };
rt_err_t rt_hw_can_register(struct rt_can_device *can, rt_err_t rt_hw_can_register(struct rt_can_device *can,
const char *name, const char *name,
const struct rt_can_ops *ops, const struct rt_can_ops *ops,
void *data); void *data);

View File

@ -847,7 +847,7 @@ long list_device(void)
device->parent.name, device->parent.name,
(device->type <= RT_Device_Class_Unknown) ? (device->type <= RT_Device_Class_Unknown) ?
device_type_str[device->type] : device_type_str[device->type] :
device_type_str[RT_Device_Class_Unknown], device_type_str[RT_Device_Class_Unknown - 1],
device->ref_count); device->ref_count);
} }