Merge pull request #624 from gbcwbz/master

fix can baud rate config error in bsp/stm32f10x  fix #597
This commit is contained in:
Bernard Xiong 2016-04-15 13:17:27 +08:00
commit 6d03ce9a7b
5 changed files with 74 additions and 20 deletions

View File

@ -245,10 +245,12 @@ int rt_can_app_init(void)
512, RT_THREAD_PRIORITY_MAX / 3 - 1, 20); 512, RT_THREAD_PRIORITY_MAX / 3 - 1, 20);
if (tid != RT_NULL) rt_thread_startup(tid); if (tid != RT_NULL) rt_thread_startup(tid);
#ifdef USING_BXCAN2
tid = rt_thread_create("canapp2", tid = rt_thread_create("canapp2",
rt_can_thread_entry, &can_data[1], rt_can_thread_entry, &can_data[1],
512, RT_THREAD_PRIORITY_MAX / 3 - 1, 20); 512, RT_THREAD_PRIORITY_MAX / 3 - 1, 20);
if (tid != RT_NULL) rt_thread_startup(tid); if (tid != RT_NULL) rt_thread_startup(tid);
#endif
return 0; return 0;
} }

View File

@ -12,6 +12,9 @@ led.c
usart.c usart.c
""") """)
if GetDepend(['RT_USING_PIN']):
src += ['gpio.c']
# add canbus driver. # add canbus driver.
if GetDepend('RT_USING_CAN'): if GetDepend('RT_USING_CAN'):
src += ['bxcan.c'] src += ['bxcan.c']

View File

@ -69,6 +69,11 @@ struct stm_bxcan
const rt_uint32_t fifo1filteroff; const rt_uint32_t fifo1filteroff;
const struct stm_bxcanfiltermap filtermap[2]; const struct stm_bxcanfiltermap filtermap[2];
}; };
struct stm_baud_rate_tab
{
rt_uint32_t baud_rate;
rt_uint32_t confdata;
};
static void calcfiltermasks(struct stm_bxcan *pbxcan); static void calcfiltermasks(struct stm_bxcan *pbxcan);
static void bxcan1_filter_init(struct rt_can_device *can) static void bxcan1_filter_init(struct rt_can_device *can)
{ {
@ -254,27 +259,65 @@ static void bxcan2_filter_init(struct rt_can_device *can)
#define MK_BKCAN_BAUD(SJW,BS1,BS2,PRES) \ #define MK_BKCAN_BAUD(SJW,BS1,BS2,PRES) \
((SJW << SJWSHIFT) | (BS1 << BS1SHIFT) | (BS2 << BS2SHIFT) | (PRES << RRESCLSHIFT)) ((SJW << SJWSHIFT) | (BS1 << BS1SHIFT) | (BS2 << BS2SHIFT) | (PRES << RRESCLSHIFT))
static const rt_uint32_t bxcan_baud_rate_tab[] = static const struct stm_baud_rate_tab bxcan_baud_rate_tab[] =
{ {
#ifdef STM32F10X_CL
// 48 M // 48 M
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 3), {1000UL * 1000, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 3)},
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_6tq, CAN_BS2_3tq, 6), {1000UL * 800, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_6tq, CAN_BS2_3tq, 6)},
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 5), {1000UL * 500, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 5)},
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 11), {1000UL * 250, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 11)},//1
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 23), {1000UL * 125, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 23)},
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 29), {1000UL * 100, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 29)},
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 59), {1000UL * 50, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 59)},
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_14tq, CAN_BS2_3tq, 149), {1000UL * 20, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_14tq, CAN_BS2_3tq, 149)},
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_16tq, CAN_BS2_8tq, 199), {1000UL * 10, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_16tq, CAN_BS2_8tq, 199)}
#else
// 36 M
{1000UL * 1000, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_8tq, CAN_BS2_3tq, 3)},
{1000UL * 800, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_11tq, CAN_BS2_3tq, 3)},
{1000UL * 500, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_9tq, CAN_BS2_2tq, 6)},
{1000UL * 250, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_13tq, CAN_BS2_2tq, 9)},//1
{1000UL * 125, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_13tq, CAN_BS2_2tq, 18)},
{1000UL * 100, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_9tq, CAN_BS2_2tq, 30)},
{1000UL * 50, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_13tq, CAN_BS2_2tq, 45)},
{1000UL * 20, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_14tq, CAN_BS2_3tq, 100)},
{1000UL * 10, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_14tq, CAN_BS2_3tq, 200)}
#endif
}; };
#define BAUD_DATA(TYPE,NO) \ #define BAUD_DATA(TYPE,NO) \
((bxcan_baud_rate_tab[NO] & TYPE##MASK) >> TYPE##SHIFT) ((bxcan_baud_rate_tab[NO].confdata & TYPE##MASK) >> TYPE##SHIFT)
static rt_uint32_t bxcan_get_baud_index(rt_uint32_t baud)
{
rt_uint32_t len, index, default_index;
len = sizeof(bxcan_baud_rate_tab)/sizeof(bxcan_baud_rate_tab[0]);
default_index = len;
for(index = 0; index < len; index++)
{
if(bxcan_baud_rate_tab[index].baud_rate == baud)
return index;
if(bxcan_baud_rate_tab[index].baud_rate == 1000UL * 250)
default_index = index;
}
if(default_index != len)
return default_index;
return 0;
}
static void bxcan_init(CAN_TypeDef *pcan, rt_uint32_t baud, rt_uint32_t mode) static void bxcan_init(CAN_TypeDef *pcan, rt_uint32_t baud, rt_uint32_t mode)
{ {
CAN_InitTypeDef CAN_InitStructure; CAN_InitTypeDef CAN_InitStructure;
rt_uint32_t baud_index = bxcan_get_baud_index(baud);
CAN_InitStructure.CAN_TTCM = DISABLE; CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = ENABLE; CAN_InitStructure.CAN_ABOM = ENABLE;
CAN_InitStructure.CAN_AWUM = DISABLE; CAN_InitStructure.CAN_AWUM = DISABLE;
@ -296,10 +339,10 @@ static void bxcan_init(CAN_TypeDef *pcan, rt_uint32_t baud, rt_uint32_t mode)
CAN_InitStructure.CAN_Mode = CAN_Mode_Silent_LoopBack; CAN_InitStructure.CAN_Mode = CAN_Mode_Silent_LoopBack;
break; break;
} }
CAN_InitStructure.CAN_SJW = BAUD_DATA(SJW, baud); CAN_InitStructure.CAN_SJW = BAUD_DATA(SJW, baud_index);
CAN_InitStructure.CAN_BS1 = BAUD_DATA(BS1, baud); CAN_InitStructure.CAN_BS1 = BAUD_DATA(BS1, baud_index);
CAN_InitStructure.CAN_BS2 = BAUD_DATA(BS2, baud); CAN_InitStructure.CAN_BS2 = BAUD_DATA(BS2, baud_index);
CAN_InitStructure.CAN_Prescaler = BAUD_DATA(RRESCL, baud); CAN_InitStructure.CAN_Prescaler = BAUD_DATA(RRESCL, baud_index);
CAN_Init(pcan, &CAN_InitStructure); CAN_Init(pcan, &CAN_InitStructure);
} }
@ -439,6 +482,9 @@ static rt_err_t bxcan_set_privmode(CAN_TypeDef *pcan, rt_uint32_t mode)
static rt_err_t bxcan_set_baud_rate(CAN_TypeDef *pcan, rt_uint32_t baud) static rt_err_t bxcan_set_baud_rate(CAN_TypeDef *pcan, rt_uint32_t baud)
{ {
rt_uint32_t mode; rt_uint32_t mode;
rt_uint32_t baud_index = bxcan_get_baud_index(baud);
if (bxcan_enter_init(pcan) != RT_EOK) if (bxcan_enter_init(pcan) != RT_EOK)
{ {
return RT_ERROR; return RT_ERROR;
@ -446,10 +492,10 @@ static rt_err_t bxcan_set_baud_rate(CAN_TypeDef *pcan, rt_uint32_t baud)
pcan->BTR = 0; pcan->BTR = 0;
mode = pcan->BTR & ((rt_uint32_t)0x03 << 30); mode = pcan->BTR & ((rt_uint32_t)0x03 << 30);
pcan->BTR = (mode | \ pcan->BTR = (mode | \
((BAUD_DATA(SJW, baud)) << 24) | \ ((BAUD_DATA(SJW, baud_index)) << 24) | \
((BAUD_DATA(BS1, baud)) << 16) | \ ((BAUD_DATA(BS1, baud_index)) << 16) | \
((BAUD_DATA(BS2, baud)) << 20) | \ ((BAUD_DATA(BS2, baud_index)) << 20) | \
(BAUD_DATA(RRESCL, baud))); (BAUD_DATA(RRESCL, baud_index)));
if (bxcan_exit_init(pcan) != RT_EOK) if (bxcan_exit_init(pcan) != RT_EOK)
{ {
return RT_ERROR; return RT_ERROR;

View File

@ -82,7 +82,7 @@
#define RT_USING_PIN #define RT_USING_PIN
//#define RT_USING_CAN #define RT_USING_CAN
#define RT_CAN_USING_BUS_HOOK #define RT_CAN_USING_BUS_HOOK

View File

@ -101,18 +101,21 @@ int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void
/* RT-Thread Hardware PIN APIs */ /* RT-Thread Hardware PIN APIs */
void rt_pin_mode(rt_base_t pin, rt_base_t mode) void rt_pin_mode(rt_base_t pin, rt_base_t mode)
{ {
RT_ASSERT(_hw_pin.ops != RT_NULL);
_hw_pin.ops->pin_mode(&_hw_pin.parent, pin, mode); _hw_pin.ops->pin_mode(&_hw_pin.parent, pin, mode);
} }
FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_mode, pinMode, set hardware pin mode); FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_mode, pinMode, set hardware pin mode);
void rt_pin_write(rt_base_t pin, rt_base_t value) void rt_pin_write(rt_base_t pin, rt_base_t value)
{ {
RT_ASSERT(_hw_pin.ops != RT_NULL);
_hw_pin.ops->pin_write(&_hw_pin.parent, pin, value); _hw_pin.ops->pin_write(&_hw_pin.parent, pin, value);
} }
FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_write, pinWrite, write value to hardware pin); FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_write, pinWrite, write value to hardware pin);
int rt_pin_read(rt_base_t pin) int rt_pin_read(rt_base_t pin)
{ {
RT_ASSERT(_hw_pin.ops != RT_NULL);
return _hw_pin.ops->pin_read(&_hw_pin.parent, pin); return _hw_pin.ops->pin_read(&_hw_pin.parent, pin);
} }
FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_read, pinRead, read status from hardware pin); FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_read, pinRead, read status from hardware pin);