[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

@ -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);
} }
} }
@ -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)
{ {
@ -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,13 +566,13 @@ 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_config *pfilter;
struct rt_can_filter_item *pitem; struct rt_can_filter_item *pitem;
rt_uint32_t count; rt_uint32_t count;
rt_base_t level; rt_base_t level;
pfilter = (struct rt_can_filter_config *)args; pfilter = (struct rt_can_filter_config *)args;
RT_ASSERT(pfilter);
count = pfilter->count; count = pfilter->count;
pitem = pfilter->items; pitem = pfilter->items;
if (pfilter->actived) if (pfilter->actived)
@ -630,7 +635,6 @@ static rt_err_t rt_can_control(struct rt_device *dev,
pitem++; pitem++;
} }
} }
}
break; break;
#endif /*RT_CAN_USING_HDR*/ #endif /*RT_CAN_USING_HDR*/
#ifdef RT_CAN_USING_BUS_HOOK #ifdef RT_CAN_USING_BUS_HOOK
@ -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)

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 */

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);
} }