Skip to content

Commit 6d03ce9

Browse files
committed
Merge pull request #624 from gbcwbz/master
fix can baud rate config error in bsp/stm32f10x fix #597
2 parents 9c29333 + 0d63eb5 commit 6d03ce9

File tree

5 files changed

+74
-20
lines changed

5 files changed

+74
-20
lines changed

bsp/stm32f10x/applications/canapp.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,12 @@ int rt_can_app_init(void)
245245
512, RT_THREAD_PRIORITY_MAX / 3 - 1, 20);
246246
if (tid != RT_NULL) rt_thread_startup(tid);
247247

248+
#ifdef USING_BXCAN2
248249
tid = rt_thread_create("canapp2",
249250
rt_can_thread_entry, &can_data[1],
250251
512, RT_THREAD_PRIORITY_MAX / 3 - 1, 20);
251252
if (tid != RT_NULL) rt_thread_startup(tid);
253+
#endif
252254

253255
return 0;
254256
}

bsp/stm32f10x/drivers/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ led.c
1212
usart.c
1313
""")
1414

15+
if GetDepend(['RT_USING_PIN']):
16+
src += ['gpio.c']
17+
1518
# add canbus driver.
1619
if GetDepend('RT_USING_CAN'):
1720
src += ['bxcan.c']

bsp/stm32f10x/drivers/bxcan.c

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ struct stm_bxcan
6969
const rt_uint32_t fifo1filteroff;
7070
const struct stm_bxcanfiltermap filtermap[2];
7171
};
72+
struct stm_baud_rate_tab
73+
{
74+
rt_uint32_t baud_rate;
75+
rt_uint32_t confdata;
76+
};
7277
static void calcfiltermasks(struct stm_bxcan *pbxcan);
7378
static void bxcan1_filter_init(struct rt_can_device *can)
7479
{
@@ -254,26 +259,64 @@ static void bxcan2_filter_init(struct rt_can_device *can)
254259
#define MK_BKCAN_BAUD(SJW,BS1,BS2,PRES) \
255260
((SJW << SJWSHIFT) | (BS1 << BS1SHIFT) | (BS2 << BS2SHIFT) | (PRES << RRESCLSHIFT))
256261

257-
static const rt_uint32_t bxcan_baud_rate_tab[] =
262+
static const struct stm_baud_rate_tab bxcan_baud_rate_tab[] =
258263
{
264+
#ifdef STM32F10X_CL
259265
// 48 M
260-
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 3),
261-
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_6tq, CAN_BS2_3tq, 6),
262-
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 5),
263-
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 11),
264-
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 23),
265-
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 29),
266-
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 59),
267-
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_14tq, CAN_BS2_3tq, 149),
268-
MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_16tq, CAN_BS2_8tq, 199),
266+
{1000UL * 1000, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 3)},
267+
{1000UL * 800, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_6tq, CAN_BS2_3tq, 6)},
268+
{1000UL * 500, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 5)},
269+
{1000UL * 250, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 11)},//1
270+
{1000UL * 125, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 23)},
271+
{1000UL * 100, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 29)},
272+
{1000UL * 50, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_12tq, CAN_BS2_3tq, 59)},
273+
{1000UL * 20, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_14tq, CAN_BS2_3tq, 149)},
274+
{1000UL * 10, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_16tq, CAN_BS2_8tq, 199)}
275+
#else
276+
// 36 M
277+
{1000UL * 1000, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_8tq, CAN_BS2_3tq, 3)},
278+
{1000UL * 800, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_11tq, CAN_BS2_3tq, 3)},
279+
{1000UL * 500, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_9tq, CAN_BS2_2tq, 6)},
280+
{1000UL * 250, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_13tq, CAN_BS2_2tq, 9)},//1
281+
{1000UL * 125, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_13tq, CAN_BS2_2tq, 18)},
282+
{1000UL * 100, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_9tq, CAN_BS2_2tq, 30)},
283+
{1000UL * 50, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_13tq, CAN_BS2_2tq, 45)},
284+
{1000UL * 20, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_14tq, CAN_BS2_3tq, 100)},
285+
{1000UL * 10, MK_BKCAN_BAUD(CAN_SJW_2tq, CAN_BS1_14tq, CAN_BS2_3tq, 200)}
286+
#endif
269287
};
270288

271289
#define BAUD_DATA(TYPE,NO) \
272-
((bxcan_baud_rate_tab[NO] & TYPE##MASK) >> TYPE##SHIFT)
290+
((bxcan_baud_rate_tab[NO].confdata & TYPE##MASK) >> TYPE##SHIFT)
291+
292+
static rt_uint32_t bxcan_get_baud_index(rt_uint32_t baud)
293+
{
294+
rt_uint32_t len, index, default_index;
295+
296+
len = sizeof(bxcan_baud_rate_tab)/sizeof(bxcan_baud_rate_tab[0]);
297+
default_index = len;
298+
299+
for(index = 0; index < len; index++)
300+
{
301+
if(bxcan_baud_rate_tab[index].baud_rate == baud)
302+
return index;
303+
304+
if(bxcan_baud_rate_tab[index].baud_rate == 1000UL * 250)
305+
default_index = index;
306+
}
307+
308+
if(default_index != len)
309+
return default_index;
310+
311+
return 0;
312+
}
313+
273314

274315
static void bxcan_init(CAN_TypeDef *pcan, rt_uint32_t baud, rt_uint32_t mode)
275316
{
276317
CAN_InitTypeDef CAN_InitStructure;
318+
319+
rt_uint32_t baud_index = bxcan_get_baud_index(baud);
277320

278321
CAN_InitStructure.CAN_TTCM = DISABLE;
279322
CAN_InitStructure.CAN_ABOM = ENABLE;
@@ -296,10 +339,10 @@ static void bxcan_init(CAN_TypeDef *pcan, rt_uint32_t baud, rt_uint32_t mode)
296339
CAN_InitStructure.CAN_Mode = CAN_Mode_Silent_LoopBack;
297340
break;
298341
}
299-
CAN_InitStructure.CAN_SJW = BAUD_DATA(SJW, baud);
300-
CAN_InitStructure.CAN_BS1 = BAUD_DATA(BS1, baud);
301-
CAN_InitStructure.CAN_BS2 = BAUD_DATA(BS2, baud);
302-
CAN_InitStructure.CAN_Prescaler = BAUD_DATA(RRESCL, baud);
342+
CAN_InitStructure.CAN_SJW = BAUD_DATA(SJW, baud_index);
343+
CAN_InitStructure.CAN_BS1 = BAUD_DATA(BS1, baud_index);
344+
CAN_InitStructure.CAN_BS2 = BAUD_DATA(BS2, baud_index);
345+
CAN_InitStructure.CAN_Prescaler = BAUD_DATA(RRESCL, baud_index);
303346

304347
CAN_Init(pcan, &CAN_InitStructure);
305348
}
@@ -439,17 +482,20 @@ static rt_err_t bxcan_set_privmode(CAN_TypeDef *pcan, rt_uint32_t mode)
439482
static rt_err_t bxcan_set_baud_rate(CAN_TypeDef *pcan, rt_uint32_t baud)
440483
{
441484
rt_uint32_t mode;
485+
486+
rt_uint32_t baud_index = bxcan_get_baud_index(baud);
487+
442488
if (bxcan_enter_init(pcan) != RT_EOK)
443489
{
444490
return RT_ERROR;
445491
}
446492
pcan->BTR = 0;
447493
mode = pcan->BTR & ((rt_uint32_t)0x03 << 30);
448494
pcan->BTR = (mode | \
449-
((BAUD_DATA(SJW, baud)) << 24) | \
450-
((BAUD_DATA(BS1, baud)) << 16) | \
451-
((BAUD_DATA(BS2, baud)) << 20) | \
452-
(BAUD_DATA(RRESCL, baud)));
495+
((BAUD_DATA(SJW, baud_index)) << 24) | \
496+
((BAUD_DATA(BS1, baud_index)) << 16) | \
497+
((BAUD_DATA(BS2, baud_index)) << 20) | \
498+
(BAUD_DATA(RRESCL, baud_index)));
453499
if (bxcan_exit_init(pcan) != RT_EOK)
454500
{
455501
return RT_ERROR;

bsp/stm32f10x/rtconfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282

8383
#define RT_USING_PIN
8484

85-
//#define RT_USING_CAN
85+
#define RT_USING_CAN
8686

8787
#define RT_CAN_USING_BUS_HOOK
8888

components/drivers/misc/pin.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,21 @@ int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void
101101
/* RT-Thread Hardware PIN APIs */
102102
void rt_pin_mode(rt_base_t pin, rt_base_t mode)
103103
{
104+
RT_ASSERT(_hw_pin.ops != RT_NULL);
104105
_hw_pin.ops->pin_mode(&_hw_pin.parent, pin, mode);
105106
}
106107
FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_mode, pinMode, set hardware pin mode);
107108

108109
void rt_pin_write(rt_base_t pin, rt_base_t value)
109110
{
111+
RT_ASSERT(_hw_pin.ops != RT_NULL);
110112
_hw_pin.ops->pin_write(&_hw_pin.parent, pin, value);
111113
}
112114
FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_write, pinWrite, write value to hardware pin);
113115

114116
int rt_pin_read(rt_base_t pin)
115117
{
118+
RT_ASSERT(_hw_pin.ops != RT_NULL);
116119
return _hw_pin.ops->pin_read(&_hw_pin.parent, pin);
117120
}
118121
FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_read, pinRead, read status from hardware pin);

0 commit comments

Comments
 (0)