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