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