📄 stm32f10x_can.lst
字号:
220 * Output : None.
221 * Return : None.
222 *******************************************************************************/
223 void CAN_FilterInit(CAN_FilterInitTypeDef* CAN_FilterInitStruct)
224 {
225 u16 FilterNumber_BitPos = 0;
226
227 /* Check the parameters */
228 assert_param(IS_CAN_FILTER_NUMBER(CAN_FilterInitStruct->CAN_FilterNumber));
229 assert_param(IS_CAN_FILTER_MODE(CAN_FilterInitStruct->CAN_FilterMode));
230 assert_param(IS_CAN_FILTER_SCALE(CAN_FilterInitStruct->CAN_FilterScale));
231 assert_param(IS_CAN_FILTER_FIFO(CAN_FilterInitStruct->CAN_FilterFIFOAssignment));
232 assert_param(IS_FUNCTIONAL_STATE(CAN_FilterInitStruct->CAN_FilterActivation));
233
234 FilterNumber_BitPos =
235 (u16)((u16)0x0001 << ((u16)CAN_FilterInitStruct->CAN_FilterNumber));
236
237 /* Initialisation mode for the filter */
238 CAN->FMR |= CAN_FMR_FINIT;
239
240 /* Filter Deactivation */
241 CAN->FA0R &= ~(u32)FilterNumber_BitPos;
242
243 /* Filter Scale */
244 if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_16bit)
245 {
246 /* 16-bit scale for the filter */
247 CAN->FS0R &= ~(u32)FilterNumber_BitPos;
248
249 /* First 16-bit identifier and First 16-bit mask */
250 /* Or First 16-bit identifier and Second 16-bit identifier */
251 CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR0 =
252 ((u32)((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterMaskIdLow) << 16) |
253 ((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterIdLow);
254
255 /* Second 16-bit identifier and Second 16-bit mask */
256 /* Or Third 16-bit identifier and Fourth 16-bit identifier */
257 CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 =
258 ((u32)((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
259 ((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterIdHigh);
260 }
261 if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_32bit)
262 {
263 /* 32-bit scale for the filter */
264 CAN->FS0R |= FilterNumber_BitPos;
265
266 /* 32-bit identifier or First 32-bit identifier */
267 CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR0 =
268 ((u32)((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterIdHigh) << 16) |
269 ((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterIdLow);
270
271 /* 32-bit mask or Second 32-bit identifier */
272 CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 =
273 ((u32)((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
274 ((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterMaskIdLow);
275
276 }
277
278 /* Filter Mode */
279 if (CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdMask)
280 {
281 /*Id/Mask mode for the filter*/
282 CAN->FM0R &= ~(u32)FilterNumber_BitPos;
283 }
284 else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
285 {
286 /*Identifier list mode for the filter*/
287 CAN->FM0R |= (u32)FilterNumber_BitPos;
288 }
289
290 /* Filter FIFO assignment */
291 if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_FilterFIFO0)
292 {
293 /* FIFO 0 assignation for the filter */
294 CAN->FFA0R &= ~(u32)FilterNumber_BitPos;
295 }
296 if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_FilterFIFO1)
297 {
298 /* FIFO 1 assignation for the filter */
299 CAN->FFA0R |= (u32)FilterNumber_BitPos;
300 }
301
302 /* Filter activation */
303 if (CAN_FilterInitStruct->CAN_FilterActivation == ENABLE)
304 {
305 CAN->FA0R |= FilterNumber_BitPos;
306 }
307
308 /* Leave the initialisation mode for the filter */
309 CAN->FMR &= ~CAN_FMR_FINIT;
310 }
311
312 /*******************************************************************************
313 * Function Name : CAN_StructInit
314 * Description : Fills each CAN_InitStruct member with its default value.
315 * Input : CAN_InitStruct: pointer to a CAN_InitTypeDef structure which
316 * will be initialized.
317 * Output : None.
318 * Return : None.
319 *******************************************************************************/
320 void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct)
321 {
322 /* Reset CAN init structure parameters values */
323
324 /* Initialize the time triggered communication mode */
325 CAN_InitStruct->CAN_TTCM = DISABLE;
326
327 /* Initialize the automatic bus-off management */
328 CAN_InitStruct->CAN_ABOM = DISABLE;
329
330 /* Initialize the automatic wake-up mode */
331 CAN_InitStruct->CAN_AWUM = DISABLE;
332
333 /* Initialize the no automatic retransmission */
334 CAN_InitStruct->CAN_NART = DISABLE;
335
336 /* Initialize the receive FIFO locked mode */
337 CAN_InitStruct->CAN_RFLM = DISABLE;
338
339 /* Initialize the transmit FIFO priority */
340 CAN_InitStruct->CAN_TXFP = DISABLE;
341
342 /* Initialize the CAN_Mode member */
343 CAN_InitStruct->CAN_Mode = CAN_Mode_Normal;
344
345 /* Initialize the CAN_SJW member */
346 CAN_InitStruct->CAN_SJW = CAN_SJW_1tq;
347
348 /* Initialize the CAN_BS1 member */
349 CAN_InitStruct->CAN_BS1 = CAN_BS1_4tq;
350
351 /* Initialize the CAN_BS2 member */
352 CAN_InitStruct->CAN_BS2 = CAN_BS2_3tq;
353
354 /* Initialize the CAN_Prescaler member */
355 CAN_InitStruct->CAN_Prescaler = 1;
356 }
357
358 /*******************************************************************************
359 * Function Name : CAN_ITConfig
360 * Description : Enables or disables the specified CAN interrupts.
361 * Input : - CAN_IT: specifies the CAN interrupt sources to be enabled or
362 * disabled.
363 * This parameter can be: CAN_IT_TME, CAN_IT_FMP0, CAN_IT_FF0,
364 * CAN_IT_FOV0, CAN_IT_FMP1, CAN_IT_FF1,
365 * CAN_IT_FOV1, CAN_IT_EWG, CAN_IT_EPV,
366 * CAN_IT_LEC, CAN_IT_ERR, CAN_IT_WKU or
367 * CAN_IT_SLK.
368 * - NewState: new state of the CAN interrupts.
369 * This parameter can be: ENABLE or DISABLE.
370 * Output : None.
371 * Return : None.
372 *******************************************************************************/
373 void CAN_ITConfig(u32 CAN_IT, FunctionalState NewState)
374 {
375 /* Check the parameters */
376 assert_param(IS_CAN_ITConfig(CAN_IT));
377 assert_param(IS_FUNCTIONAL_STATE(NewState));
378
379 if (NewState != DISABLE)
380 {
381 /* Enable the selected CAN interrupt */
382 CAN->IER |= CAN_IT;
383 }
384 else
385 {
386 /* Disable the selected CAN interrupt */
387 CAN->IER &= ~CAN_IT;
388 }
389 }
390
391 /*******************************************************************************
392 * Function Name : CAN_Transmit
393 * Description : Initiates the transmission of a message.
394 * Input : TxMessage: pointer to a structure which contains CAN Id, CAN
395 * DLC and CAN datas.
396 * Output : None.
397 * Return : The number of the mailbox that is used for transmission
398 * or CAN_NO_MB if there is no empty mailbox.
399 *******************************************************************************/
400 u8 CAN_Transmit(CanTxMsg* TxMessage)
401 {
402 u8 TransmitMailbox = 0;
403
404 /* Check the parameters */
405 assert_param(IS_CAN_STDID(TxMessage->StdId));
406 assert_param(IS_CAN_EXTID(TxMessage->StdId));
407 assert_param(IS_CAN_IDTYPE(TxMessage->IDE));
408 assert_param(IS_CAN_RTR(TxMessage->RTR));
409 assert_param(IS_CAN_DLC(TxMessage->DLC));
410
411 /* Select one empty transmit mailbox */
412 if ((CAN->TSR&CAN_TSR_TME0) == CAN_TSR_TME0)
413 {
414 TransmitMailbox = 0;
415 }
416 else if ((CAN->TSR&CAN_TSR_TME1) == CAN_TSR_TME1)
417 {
418 TransmitMailbox = 1;
419 }
420 else if ((CAN->TSR&CAN_TSR_TME2) == CAN_TSR_TME2)
421 {
422 TransmitMailbox = 2;
423 }
424 else
425 {
426 TransmitMailbox = CAN_NO_MB;
427 }
428
429 if (TransmitMailbox != CAN_NO_MB)
430 {
431 /* Set up the Id */
432 TxMessage->StdId &= (u32)0x000007FF;
433 TxMessage->StdId = TxMessage->StdId << 21;
434 TxMessage->ExtId &= (u32)0x0003FFFF;
435 TxMessage->ExtId <<= 3;
436
437 CAN->sTxMailBox[TransmitMailbox].TIR &= CAN_TMIDxR_TXRQ;
438 CAN->sTxMailBox[TransmitMailbox].TIR |= (TxMessage->StdId | TxMessage->ExtId |
439 TxMessage->IDE | TxMessage->RTR);
440
441 /* Set up the DLC */
442 TxMessage->DLC &= (u8)0x0000000F;
443 CAN->sTxMailBox[TransmitMailbox].TDTR &= (u32)0xFFFFFFF0;
444 CAN->sTxMailBox[TransmitMailbox].TDTR |= TxMessage->DLC;
445
446 /* Set up the data field */
447 CAN->sTxMailBox[TransmitMailbox].TDLR = (((u32)TxMessage->Data[3] << 24) |
448 ((u32)TxMessage->Data[2] << 16) |
449 ((u32)TxMessage->Data[1] << 8) |
450 ((u32)TxMessage->Data[0]));
451 CAN->sTxMailBox[TransmitMailbox].TDHR = (((u32)TxMessage->Data[7] << 24) |
452 ((u32)TxMessage->Data[6] << 16) |
453 ((u32)TxMessage->Data[5] << 8) |
454 ((u32)TxMessage->Data[4]));
455
456 /* Request transmission */
457 CAN->sTxMailBox[TransmitMailbox].TIR |= CAN_TMIDxR_TXRQ;
458 }
459
460 return TransmitMailbox;
461 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -