OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [CORTEX_STM32F103_IAR/] [STM32F10xFWLib/] [src/] [stm32f10x_can.c] - Blame information for rev 582

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 582 jeremybenn
/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
2
* File Name          : stm32f10x_can.c
3
* Author             : MCD Application Team
4
* Date First Issued  : 09/29/2006
5
* Description        : This file provides all the CAN firmware functions.
6
********************************************************************************
7
* History:
8
* 04/02/2007: V0.2
9
* 02/05/2007: V0.1
10
* 09/29/2006: V0.01
11
********************************************************************************
12
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
14
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
15
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
16
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
17
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18
*******************************************************************************/
19
 
20
/* Includes ------------------------------------------------------------------*/
21
#include "stm32f10x_can.h"
22
#include "stm32f10x_rcc.h"
23
 
24
/* Private typedef -----------------------------------------------------------*/
25
 
26
/* Private define ------------------------------------------------------------*/
27
/* CAN Master Control Register bits */
28
#define CAN_MCR_INRQ     ((u32)0x00000001) /* Initialization request */
29
#define CAN_MCR_SLEEP    ((u32)0x00000002) /* Sleep mode request */
30
#define CAN_MCR_TXFP     ((u32)0x00000004) /* Transmit FIFO priority */
31
#define CAN_MCR_RFLM     ((u32)0x00000008) /* Receive FIFO locked mode */
32
#define CAN_MCR_NART     ((u32)0x00000010) /* No automatic retransmission */
33
#define CAN_MCR_AWUM     ((u32)0x00000020) /* Automatic wake up mode */
34
#define CAN_MCR_ABOM     ((u32)0x00000040) /* Automatic bus-off management */
35
#define CAN_MCR_TTCM     ((u32)0x00000080) /* time triggered communication */
36
 
37
/* CAN Master Status Register bits */
38
#define CAN_MSR_INAK     ((u32)0x00000001)    /* Initialization acknowledge */
39
#define CAN_MSR_WKUI     ((u32)0x00000008)    /* Wake-up interrupt */
40
#define CAN_MSR_SLAKI    ((u32)0x00000010)    /* Sleep acknowledge interrupt */
41
 
42
/* CAN Transmit Status Register bits */
43
#define CAN_TSR_RQCP0    ((u32)0x00000001)    /* Request completed mailbox0 */
44
#define CAN_TSR_TXOK0    ((u32)0x00000002)    /* Transmission OK of mailbox0 */
45
#define CAN_TSR_ABRQ0    ((u32)0x00000080)    /* Abort request for mailbox0 */
46
#define CAN_TSR_RQCP1    ((u32)0x00000100)    /* Request completed mailbox1 */
47
#define CAN_TSR_TXOK1    ((u32)0x00000200)    /* Transmission OK of mailbox1 */
48
#define CAN_TSR_ABRQ1    ((u32)0x00008000)    /* Abort request for mailbox1 */
49
#define CAN_TSR_RQCP2    ((u32)0x00010000)    /* Request completed mailbox2 */
50
#define CAN_TSR_TXOK2    ((u32)0x00020000)    /* Transmission OK of mailbox2 */
51
#define CAN_TSR_ABRQ2    ((u32)0x00800000)    /* Abort request for mailbox2 */
52
#define CAN_TSR_TME0     ((u32)0x04000000)    /* Transmit mailbox 0 empty */
53
#define CAN_TSR_TME1     ((u32)0x08000000)    /* Transmit mailbox 1 empty */
54
#define CAN_TSR_TME2     ((u32)0x10000000)    /* Transmit mailbox 2 empty */
55
 
56
/* CAN Receive FIFO 0 Register bits */
57
#define CAN_RF0R_FULL0   ((u32)0x00000008)    /* FIFO 0 full */
58
#define CAN_RF0R_FOVR0   ((u32)0x00000010)    /* FIFO 0 overrun */
59
#define CAN_RF0R_RFOM0   ((u32)0x00000020)    /* Release FIFO 0 output mailbox */
60
 
61
/* CAN Receive FIFO 1 Register bits */
62
#define CAN_RF1R_FULL1   ((u32)0x00000008)    /* FIFO 1 full */
63
#define CAN_RF1R_FOVR1   ((u32)0x00000010)    /* FIFO 1 overrun */
64
#define CAN_RF1R_RFOM1   ((u32)0x00000020)    /* Release FIFO 1 output mailbox */
65
 
66
/* CAN Error Status Register bits */
67
#define CAN_ESR_EWGF     ((u32)0x00000001)    /* Error warning flag */
68
#define CAN_ESR_EPVF     ((u32)0x00000002)    /* Error passive flag */
69
#define CAN_ESR_BOFF     ((u32)0x00000004)    /* Bus-off flag */
70
 
71
/* CAN Mailbox Transmit Request */
72
#define CAN_TMIDxR_TXRQ    ((u32)0x00000001) /* Transmit mailbox request */
73
 
74
/* CAN Filter Master Register bits */
75
#define CAN_FMR_FINIT ((u32)0x00000001) /* Filter init mode */
76
 
77
 
78
/* Private macro -------------------------------------------------------------*/
79
/* Private variables ---------------------------------------------------------*/
80
/* Private function prototypes -----------------------------------------------*/
81
static ITStatus CheckITStatus(u32 CAN_Reg, u32 It_Bit);
82
 
83
/* Private functions ---------------------------------------------------------*/
84
/*******************************************************************************
85
* Function Name  : CAN_DeInit
86
* Description    : Deinitializes the CAN peripheral registers to their default
87
*                  reset values.
88
* Input          : None.
89
* Output         : None.
90
* Return         : None.
91
*******************************************************************************/
92
void CAN_DeInit(void)
93
{
94
  /* Enable CAN reset state */
95
  RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN, ENABLE);
96
  /* Release CAN from reset state */
97
  RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN, DISABLE);
98
}
99
 
100
/*******************************************************************************
101
* Function Name  : CAN_Init
102
* Description    : Initializes the CAN peripheral according to the specified
103
*                  parameters in the CAN_InitStruct.
104
* Input          : CAN_InitStruct: pointer to a CAN_InitTypeDef structure that
105
                   contains the configuration information for the CAN peripheral.
106
* Output         : None.
107
* Return         : Constant indicates initialization succeed which will be
108
*                  CANINITFAILED or CANINITOK.
109
*******************************************************************************/
110
u8 CAN_Init(CAN_InitTypeDef* CAN_InitStruct)
111
{
112
  u8 InitStatus = 0;
113
 
114
  /* Check the parameters */
115
  assert(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TTCM));
116
  assert(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_ABOM));
117
  assert(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_AWUM));
118
  assert(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_NART));
119
  assert(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_RFLM));
120
  assert(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TXFP));
121
  assert(IS_CAN_MODE(CAN_InitStruct->CAN_Mode));
122
  assert(IS_CAN_SJW(CAN_InitStruct->CAN_SJW));
123
  assert(IS_CAN_BS1(CAN_InitStruct->CAN_BS1));
124
  assert(IS_CAN_BS2(CAN_InitStruct->CAN_BS2));
125
  assert(IS_CAN_CLOCK(CAN_InitStruct->CAN_Clock));
126
  assert(IS_CAN_PRESCALER(CAN_InitStruct->CAN_Prescaler));
127
 
128
  /* Request initialisation */
129
  CAN->MCR = CAN_MCR_INRQ;
130
 
131
  /* ...and check acknowledged */
132
  if ((CAN->MSR & CAN_MSR_INAK) == 0)
133
  {
134
    InitStatus = CANINITFAILED;
135
  }
136
  else
137
  {
138
    /* Set the time triggered communication mode */
139
    if (CAN_InitStruct->CAN_TTCM == ENABLE)
140
    {
141
      CAN->MCR |= CAN_MCR_TTCM;
142
    }
143
    else
144
    {
145
      CAN->MCR &= ~CAN_MCR_TTCM;
146
    }
147
 
148
    /* Set the automatic bus-off management */
149
    if (CAN_InitStruct->CAN_ABOM == ENABLE)
150
    {
151
      CAN->MCR |= CAN_MCR_ABOM;
152
    }
153
    else
154
    {
155
      CAN->MCR &= ~CAN_MCR_ABOM;
156
    }
157
 
158
    /* Set the automatic wake-up mode */
159
    if (CAN_InitStruct->CAN_AWUM == ENABLE)
160
    {
161
      CAN->MCR |= CAN_MCR_AWUM;
162
    }
163
    else
164
    {
165
      CAN->MCR &= ~CAN_MCR_AWUM;
166
    }
167
 
168
    /* Set the no automatic retransmission */
169
    if (CAN_InitStruct->CAN_NART == ENABLE)
170
    {
171
      CAN->MCR |= CAN_MCR_NART;
172
    }
173
    else
174
    {
175
      CAN->MCR &= ~CAN_MCR_NART;
176
    }
177
 
178
    /* Set the receive FIFO locked mode */
179
    if (CAN_InitStruct->CAN_RFLM == ENABLE)
180
    {
181
      CAN->MCR |= CAN_MCR_RFLM;
182
    }
183
    else
184
    {
185
      CAN->MCR &= ~CAN_MCR_RFLM;
186
    }
187
 
188
    /* Set the transmit FIFO priority */
189
    if (CAN_InitStruct->CAN_TXFP == ENABLE)
190
    {
191
      CAN->MCR |= CAN_MCR_TXFP;
192
    }
193
    else
194
    {
195
      CAN->MCR &= ~CAN_MCR_TXFP;
196
    }
197
 
198
    /* Set the bit timing register */
199
    CAN->BTR = (u32)((u32)CAN_InitStruct->CAN_Mode << 30) | ((u32)CAN_InitStruct->CAN_SJW << 24) |
200
               ((u32)CAN_InitStruct->CAN_BS1 << 16) | ((u32)CAN_InitStruct->CAN_BS2 << 20) |
201
               ((u32)CAN_InitStruct->CAN_Clock << 15) | ((u32)CAN_InitStruct->CAN_Prescaler - 1);
202
 
203
    InitStatus = CANINITOK;
204
 
205
    /* Request leave initialisation */
206
    CAN->MCR &= ~CAN_MCR_INRQ;
207
 
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 = (u16)((u16)0x0001 << ((u16)CAN_FilterInitStruct->CAN_FilterNumber));
240
 
241
  /* Initialisation mode for the filter */
242
  CAN->FMR |= CAN_FMR_FINIT;
243
 
244
  /* Filter Deactivation */
245
  CAN->FA0R &= ~(u32)FilterNumber_BitPos;
246
 
247
  /* Filter Scale */
248
  if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_16bit)
249
  {
250
    /* 16-bit scale for the filter */
251
    CAN->FS0R &= ~(u32)FilterNumber_BitPos;
252
 
253
    /* First 16-bit identifier and First 16-bit mask */
254
    /* Or First 16-bit identifier and Second 16-bit identifier */
255
    CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR0 = ((u32)((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterMaskIdLow) << 16) |
256
        ((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterIdLow);
257
 
258
    /* Second 16-bit identifier and Second 16-bit mask */
259
    /* Or Third 16-bit identifier and Fourth 16-bit identifier */
260
    CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 = ((u32)((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
261
        ((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterIdHigh);
262
  }
263
  if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_32bit)
264
  {
265
    /* 32-bit scale for the filter */
266
    CAN->FS0R |= FilterNumber_BitPos;
267
 
268
    /* 32-bit identifier or First 32-bit identifier */
269
    CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR0 = ((u32)((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterIdHigh) << 16) |
270
        ((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterIdLow);
271
 
272
    /* 32-bit mask or Second 32-bit identifier */
273
    CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 = ((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_0tq;
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_Clock member */
355
  CAN_InitStruct->CAN_Clock = CAN_Clock_APB;
356
 
357
  /* Initialize the CAN_Prescaler member */
358
  CAN_InitStruct->CAN_Prescaler = 1;
359
}
360
 
361
/*******************************************************************************
362
* Function Name  : CAN_ITConfig
363
* Description    : Enables or disables the CAN interrupts.
364
* Input          : - CAN_IT: specifies the CAN interrupt sources to be enabled or
365
*                    disabled.
366
*                  - NewState: new state of the CAN interrupts.
367
*                    This parameter can be: ENABLE or DISABLE.
368
* Output         : None.
369
* Return         : None.
370
*******************************************************************************/
371
void CAN_ITConfig(u32 CAN_IT, FunctionalState NewState)
372
{
373
  /* Check the parameters */
374
  assert(IS_CAN_IT(CAN_IT));
375
  assert(IS_FUNCTIONAL_STATE(NewState));
376
 
377
  if (NewState != DISABLE)
378
  {
379
    /* Enable the selected CAN interrupt */
380
    CAN->IER |= CAN_IT;
381
  }
382
  else
383
  {
384
    /* Disable the selected CAN interrupt */
385
    CAN->IER &= ~CAN_IT;
386
  }
387
}
388
 
389
/*******************************************************************************
390
* Function Name  : CAN_Transmit
391
* Description    : Initiates the transmission of a message.
392
* Input          : TxMessage: pointer to a structure which contains CAN Id, CAN
393
*                  DLC and CAN datas.
394
* Output         : None.
395
* Return         : The number of the mailbox that is used for transmission
396
*                  or CAN_NO_MB if there is no empty mailbox.
397
*******************************************************************************/
398
u8 CAN_Transmit(CanTxMsg* TxMessage)
399
{
400
  u8 TransmitMailbox = 0;
401
 
402
  /* Check the parameters */
403
  assert(IS_CAN_STDID(TxMessage->StdId));
404
  assert(IS_CAN_EXTID(TxMessage->StdId));
405
  assert(IS_CAN_IDTYPE(TxMessage->IDE));
406
  assert(IS_CAN_RTR(TxMessage->RTR));
407
  assert(IS_CAN_DLC(TxMessage->DLC));
408
 
409
  /* Select one empty transmit mailbox */
410
  if ((CAN->TSR&CAN_TSR_TME0) == CAN_TSR_TME0)
411
  {
412
    TransmitMailbox = 0;
413
  }
414
  else if ((CAN->TSR&CAN_TSR_TME1) == CAN_TSR_TME1)
415
  {
416
    TransmitMailbox = 1;
417
  }
418
  else if ((CAN->TSR&CAN_TSR_TME2) == CAN_TSR_TME2)
419
  {
420
    TransmitMailbox = 2;
421
  }
422
  else
423
  {
424
    TransmitMailbox = CAN_NO_MB;
425
  }
426
 
427
  if (TransmitMailbox != CAN_NO_MB)
428
  {
429
    /* Set up the Id */
430
    TxMessage->StdId &= (u32)0x000007FF;
431
    TxMessage->StdId = TxMessage->StdId << 21;
432
    TxMessage->ExtId &= (u32)0x0003FFFF;
433
    TxMessage->ExtId <<= 3;
434
 
435
    CAN->sTxMailBox[TransmitMailbox].TIR &= CAN_TMIDxR_TXRQ;
436
    CAN->sTxMailBox[TransmitMailbox].TIR |= (TxMessage->StdId | TxMessage->ExtId |
437
                                            TxMessage->IDE | TxMessage->RTR);
438
 
439
    /* Set up the DLC */
440
    TxMessage->DLC &= (u8)0x0000000F;
441
    CAN->sTxMailBox[TransmitMailbox].TDTR &= (u32)0xFFFFFFF0;
442
    CAN->sTxMailBox[TransmitMailbox].TDTR |= TxMessage->DLC;
443
 
444
    /* Set up the data field */
445
    CAN->sTxMailBox[TransmitMailbox].TDLR = (((u32)TxMessage->Data[3] << 24) | ((u32)TxMessage->Data[2] << 16) |
446
                                            ((u32)TxMessage->Data[1] << 8) | ((u32)TxMessage->Data[0]));
447
    CAN->sTxMailBox[TransmitMailbox].TDHR = (((u32)TxMessage->Data[7] << 24) | ((u32)TxMessage->Data[6] << 16) |
448
                                            ((u32)TxMessage->Data[5] << 8) | ((u32)TxMessage->Data[4]));
449
 
450
    /* Request transmission */
451
    CAN->sTxMailBox[TransmitMailbox].TIR |= CAN_TMIDxR_TXRQ;
452
  }
453
 
454
  return TransmitMailbox;
455
}
456
 
457
/*******************************************************************************
458
* Function Name  : CAN_TransmitStatus
459
* Description    : Check the transmission of a message.
460
* Input          : TransmitMailbox: the number of the mailbox that is used for
461
*                  transmission.
462
* Output         : None.
463
* Return         : CANTXOK if the CAN driver transmits the message, CANTXFAILED
464
*                  in an other case.
465
*******************************************************************************/
466
u32 CAN_TransmitStatus(u8 TransmitMailbox)
467
{
468
  /* RQCP, TXOK and TME bits */
469
  u32 State = 0;
470
 
471
  /* Check the parameters */
472
  assert(IS_CAN_TRANSMITMAILBOX(TransmitMailbox));
473
 
474
  switch (TransmitMailbox)
475
  {
476
    case (0): State |= ((CAN->TSR & CAN_TSR_RQCP0) << 2);
477
      State |= ((CAN->TSR & CAN_TSR_TXOK0) >> 0);
478
      State |= ((CAN->TSR & CAN_TSR_TME0) >> 26);
479
      break;
480
    case (1): State |= ((CAN->TSR & CAN_TSR_RQCP1) >> 6);
481
      State |= ((CAN->TSR & CAN_TSR_TXOK1) >> 8);
482
      State |= ((CAN->TSR & CAN_TSR_TME1) >> 27);
483
      break;
484
    case (2): State |= ((CAN->TSR & CAN_TSR_RQCP2) >> 14);
485
      State |= ((CAN->TSR & CAN_TSR_TXOK2) >> 16);
486
      State |= ((CAN->TSR & CAN_TSR_TME2) >> 28);
487
      break;
488
    default:
489
      State = CANTXFAILED;
490
      break;
491
  }
492
 
493
  switch (State)
494
  {
495
      /* transmit pending  */
496
    case (0x0): State = CANTXPENDING;
497
      break;
498
      /* transmit failed  */
499
    case (0x5): State = CANTXFAILED;
500
      break;
501
      /* transmit succedeed  */
502
    case (0x7): State = CANTXOK;
503
      break;
504
    default:
505
      State = CANTXFAILED;
506
      break;
507
  }
508
 
509
  return State;
510
}
511
 
512
/*******************************************************************************
513
* Function Name  : CAN_CancelTransmit
514
* Description    : Cancels a transmit request.
515
* Input          : Mailbox number.
516
* Output         : None.
517
* Return         : None.
518
*******************************************************************************/
519
void CAN_CancelTransmit(u8 Mailbox)
520
{
521
  /* Check the parameters */
522
  assert(IS_CAN_TRANSMITMAILBOX(Mailbox));
523
 
524
  /* abort transmission */
525
  switch (Mailbox)
526
  {
527
    case (0): CAN->TSR |= CAN_TSR_ABRQ0;
528
      break;
529
    case (1): CAN->TSR |= CAN_TSR_ABRQ1;
530
      break;
531
    case (2): CAN->TSR |= CAN_TSR_ABRQ2;
532
      break;
533
    default:
534
      break;
535
  }
536
}
537
 
538
/*******************************************************************************
539
* Function Name  : CAN_FIFORelease
540
* Description    : Release a FIFO.
541
* Input          : FIFONumber: FIFO to release, CAN_FIFO0 or CAN_FIFO1.
542
* Output         : None.
543
* Return         : None.
544
*******************************************************************************/
545
void CAN_FIFORelease(u8 FIFONumber)
546
{
547
  /* Check the parameters */
548
  assert(IS_CAN_FIFO(FIFONumber));
549
 
550
  /* Release FIFO0 */
551
  if (FIFONumber == CAN_FIFO0)
552
  {
553
    CAN->RF0R = CAN_RF0R_RFOM0;
554
  }
555
  /* Release FIFO1 */
556
  else /* FIFONumber == CAN_FIFO1 */
557
  {
558
    CAN->RF1R = CAN_RF1R_RFOM1;
559
  }
560
}
561
 
562
/*******************************************************************************
563
* Function Name  : CAN_MessagePending
564
* Description    : Return the number of pending messages.
565
* Input          : FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
566
* Output         : None.
567
* Return         : NbMessage which is the number of pending message.
568
*******************************************************************************/
569
u8 CAN_MessagePending(u8 FIFONumber)
570
{
571
  u8 MessagePending=0;
572
 
573
  /* Check the parameters */
574
  assert(IS_CAN_FIFO(FIFONumber));
575
 
576
  if (FIFONumber == CAN_FIFO0)
577
  {
578
    MessagePending = (u8)(CAN->RF0R&(u32)0x03);
579
  }
580
  else if (FIFONumber == CAN_FIFO1)
581
  {
582
    MessagePending = (u8)(CAN->RF1R&(u32)0x03);
583
  }
584
  else
585
  {
586
    MessagePending = 0;
587
  }
588
  return MessagePending;
589
}
590
 
591
/*******************************************************************************
592
* Function Name  : CAN_Receive
593
* Description    : Receives a message.
594
* Input          : FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
595
* Output         : RxMessage: pointer to a structure which contains CAN Id,
596
*                  CAN DLC, CAN datas and FMI number.
597
* Return         : None.
598
*******************************************************************************/
599
void CAN_Receive(u8 FIFONumber, CanRxMsg* RxMessage)
600
{
601
  /* Check the parameters */
602
  assert(IS_CAN_FIFO(FIFONumber));
603
 
604
  /* Get the Id */
605
  RxMessage->StdId = (u32)0x000007FF & (CAN->sFIFOMailBox[FIFONumber].RIR >> 21);
606
  RxMessage->ExtId = (u32)0x0003FFFF & (CAN->sFIFOMailBox[FIFONumber].RIR >> 3);
607
 
608
  RxMessage->IDE = (u32)0x00000004 & CAN->sFIFOMailBox[FIFONumber].RIR;
609
  RxMessage->RTR = (u32)0x00000002 & CAN->sFIFOMailBox[FIFONumber].RIR;
610
 
611
  /* Get the DLC */
612
  RxMessage->DLC = (u32)0x0000000F & CAN->sFIFOMailBox[FIFONumber].RDTR;
613
 
614
  /* Get the FMI */
615
  RxMessage->FMI = (u32)0x000000FF & (CAN->sFIFOMailBox[FIFONumber].RDTR >> 8);
616
 
617
  /* Get the data field */
618
  RxMessage->Data[0] = (u32)0x000000FF & CAN->sFIFOMailBox[FIFONumber].RDLR;
619
  RxMessage->Data[1] = (u32)0x000000FF & (CAN->sFIFOMailBox[FIFONumber].RDLR >> 8);
620
  RxMessage->Data[2] = (u32)0x000000FF & (CAN->sFIFOMailBox[FIFONumber].RDLR >> 16);
621
  RxMessage->Data[3] = (u32)0x000000FF & (CAN->sFIFOMailBox[FIFONumber].RDLR >> 24);
622
 
623
  RxMessage->Data[4] = (u32)0x000000FF & CAN->sFIFOMailBox[FIFONumber].RDHR;
624
  RxMessage->Data[5] = (u32)0x000000FF & (CAN->sFIFOMailBox[FIFONumber].RDHR >> 8);
625
  RxMessage->Data[6] = (u32)0x000000FF & (CAN->sFIFOMailBox[FIFONumber].RDHR >> 16);
626
  RxMessage->Data[7] = (u32)0x000000FF & (CAN->sFIFOMailBox[FIFONumber].RDHR >> 24);
627
 
628
  /* Release the FIFO */
629
  CAN_FIFORelease(FIFONumber);
630
}
631
 
632
/*******************************************************************************
633
* Function Name  : CAN_Sleep
634
* Description    : Enters the low power mode.
635
* Input          : None.
636
* Output         : None.
637
* Return         : CANSLEEPOK if sleep entered, CANSLEEPFAILED in an other case.
638
*******************************************************************************/
639
u8 CAN_Sleep(void)
640
{
641
  u8 SleepStatus = 0;
642
 
643
  /* Sleep mode entering request */
644
  CAN->MCR |= CAN_MCR_SLEEP;
645
  SleepStatus = CANSLEEPOK;
646
 
647
  /* Sleep mode status */
648
  if ((CAN->MCR&CAN_MCR_SLEEP) == 0)
649
  {
650
    /* Sleep mode not entered */
651
    SleepStatus = CANSLEEPFAILED;
652
  }
653
 
654
  /* At this step, sleep mode status */
655
  return SleepStatus;
656
}
657
 
658
/*******************************************************************************
659
* Function Name  : CAN_WakeUp
660
* Description    : Wakes the CAN up.
661
* Input          : None.
662
* Output         : None.
663
* Return         : CANWAKEUPOK if sleep mode left, CANWAKEUPFAILED in an other
664
*                  case.
665
*******************************************************************************/
666
u8 CAN_WakeUp(void)
667
{
668
  u8 WakeUpStatus = 0;
669
 
670
  /* Wake up request */
671
  CAN->MCR &= ~CAN_MCR_SLEEP;
672
  WakeUpStatus = CANWAKEUPFAILED;
673
 
674
  /* Sleep mode status */
675
  if ((CAN->MCR&CAN_MCR_SLEEP) == 0)
676
  {
677
    /* Sleep mode exited */
678
    WakeUpStatus = CANWAKEUPOK;
679
  }
680
 
681
  /* At this step, sleep mode status */
682
  return WakeUpStatus;
683
}
684
 
685
/*******************************************************************************
686
* Function Name  : CAN_GetFlagStatus
687
* Description    : Checks whether the CAN flag is set or not.
688
* Input          : CAN_FLAG: specifies the flag to check.
689
* Output         : None.
690
* Return         : The new state of CAN_FLAG (SET or RESET).
691
*******************************************************************************/
692
FlagStatus CAN_GetFlagStatus(u32 CAN_FLAG)
693
{
694
  FlagStatus bitstatus = RESET;
695
 
696
  /* Check the parameters */
697
  assert(IS_CAN_FLAG(CAN_FLAG));
698
 
699
  /* Check the status of the specified CAN flag */
700
  if ((CAN->ESR & CAN_FLAG) != (u32)RESET)
701
  {
702
    /* CAN_FLAG is set */
703
    bitstatus = SET;
704
  }
705
  else
706
  {
707
    /* CAN_FLAG is reset */
708
    bitstatus = RESET;
709
  }
710
  /* Return the CAN_FLAG status */
711
  return  bitstatus;
712
}
713
 
714
/*******************************************************************************
715
* Function Name  : CAN_ClearFlag
716
* Description    : Clears the CAN's pending flags.
717
* Input          : CAN_FLAG: specifies the flag to clear.
718
* Output         : None.
719
* Return         : None.
720
*******************************************************************************/
721
void CAN_ClearFlag(u32 CAN_FLAG)
722
{
723
  /* Check the parameters */
724
  assert(IS_CAN_FLAG(CAN_FLAG));
725
 
726
  /* Clear the selected CAN flags */
727
  CAN->ESR &= ~CAN_FLAG;
728
}
729
 
730
/*******************************************************************************
731
* Function Name  : CAN_GetITStatus
732
* Description    : Checks whether the CAN interrupt has occurred or not.
733
* Input          : CAN_IT: specifies the CAN interrupt source to check.
734
* Output         : None.
735
* Return         : The new state of CAN_IT (SET or RESET).
736
*******************************************************************************/
737
ITStatus CAN_GetITStatus(u32 CAN_IT)
738
{
739
  ITStatus pendingbitstatus = RESET;
740
 
741
  /* Check the parameters */
742
  assert(IS_CAN_IT(CAN_IT));
743
 
744
  switch (CAN_IT)
745
  {
746
    case CAN_IT_RQCP0:
747
      pendingbitstatus = CheckITStatus(CAN->TSR, CAN_TSR_RQCP0);
748
      break;
749
    case CAN_IT_RQCP1:
750
      pendingbitstatus = CheckITStatus(CAN->TSR, CAN_TSR_RQCP1);
751
      break;
752
    case CAN_IT_RQCP2:
753
      pendingbitstatus = CheckITStatus(CAN->TSR, CAN_TSR_RQCP2);
754
      break;
755
    case CAN_IT_FF0:
756
      pendingbitstatus = CheckITStatus(CAN->RF0R, CAN_RF0R_FULL0);
757
      break;
758
    case CAN_IT_FOV0:
759
      pendingbitstatus = CheckITStatus(CAN->RF0R, CAN_RF0R_FOVR0);
760
      break;
761
    case CAN_IT_FF1:
762
      pendingbitstatus = CheckITStatus(CAN->RF1R, CAN_RF1R_FULL1);
763
      break;
764
    case CAN_IT_FOV1:
765
      pendingbitstatus = CheckITStatus(CAN->RF1R, CAN_RF1R_FOVR1);
766
      break;
767
    case CAN_IT_EWG:
768
      pendingbitstatus = CheckITStatus(CAN->ESR, CAN_ESR_EWGF);
769
      break;
770
    case CAN_IT_EPV:
771
      pendingbitstatus = CheckITStatus(CAN->ESR, CAN_ESR_EPVF);
772
      break;
773
    case CAN_IT_BOF:
774
      pendingbitstatus = CheckITStatus(CAN->ESR, CAN_ESR_BOFF);
775
      break;
776
    case CAN_IT_SLK:
777
      pendingbitstatus = CheckITStatus(CAN->MSR, CAN_MSR_SLAKI);
778
      break;
779
    case CAN_IT_WKU:
780
      pendingbitstatus = CheckITStatus(CAN->MSR, CAN_MSR_WKUI);
781
      break;
782
 
783
    default :
784
      pendingbitstatus = RESET;
785
      break;
786
  }
787
 
788
  /* Return the CAN_IT status */
789
  return  pendingbitstatus;
790
}
791
 
792
/*******************************************************************************
793
* Function Name  : CAN_ClearITPendingBit
794
* Description    : Clears the CAN’s interrupt pending bits.
795
* Input          : CAN_IT: specifies the interrupt pending bit to clear.
796
* Output         : None.
797
* Return         : None.
798
*******************************************************************************/
799
void CAN_ClearITPendingBit(u32 CAN_IT)
800
{
801
  /* Check the parameters */
802
  assert(IS_CAN_IT(CAN_IT));
803
 
804
  switch (CAN_IT)
805
  {
806
    case CAN_IT_RQCP0:
807
      CAN->TSR = CAN_TSR_RQCP0; /* rc_w1*/
808
      break;
809
    case CAN_IT_RQCP1:
810
      CAN->TSR = CAN_TSR_RQCP1; /* rc_w1*/
811
      break;
812
    case CAN_IT_RQCP2:
813
      CAN->TSR = CAN_TSR_RQCP2; /* rc_w1*/
814
      break;
815
    case CAN_IT_FF0:
816
      CAN->RF0R = CAN_RF0R_FULL0; /* rc_w1*/
817
      break;
818
    case CAN_IT_FOV0:
819
      CAN->RF0R = CAN_RF0R_FOVR0; /* rc_w1*/
820
      break;
821
    case CAN_IT_FF1:
822
      CAN->RF1R = CAN_RF1R_FULL1; /* rc_w1*/
823
      break;
824
    case CAN_IT_FOV1:
825
      CAN->RF1R = CAN_RF1R_FOVR1; /* rc_w1*/
826
      break;
827
    case CAN_IT_EWG:
828
      CAN->ESR &= ~ CAN_ESR_EWGF; /* rw */
829
      break;
830
    case CAN_IT_EPV:
831
      CAN->ESR &= ~ CAN_ESR_EPVF; /* rw */
832
      break;
833
    case CAN_IT_BOF:
834
      CAN->ESR &= ~ CAN_ESR_BOFF; /* rw */
835
      break;
836
    case CAN_IT_WKU:
837
      CAN->MSR = CAN_MSR_WKUI;  /* rc_w1*/
838
      break;
839
    case CAN_IT_SLK:
840
      CAN->MSR = CAN_MSR_SLAKI;  /* rc_w1*/
841
      break;
842
    default :
843
      break;
844
  }
845
}
846
 
847
/*******************************************************************************
848
* Function Name  : CheckITStatus
849
* Description    : Checks whether the CAN interrupt has occurred or not.
850
* Input          : CAN_Reg: specifies the CAN interrupt register to check.
851
*                  It_Bit: specifies the interrupt source bit to check.
852
* Output         : None.
853
* Return         : The new state of the CAN Interrupt (SET or RESET).
854
*******************************************************************************/
855
static ITStatus CheckITStatus(u32 CAN_Reg, u32 It_Bit)
856
{
857
  ITStatus pendingbitstatus = RESET;
858
 
859
  if ((CAN_Reg & It_Bit) != (u32)RESET)
860
  {
861
    /* CAN_IT is set */
862
    pendingbitstatus = SET;
863
  }
864
  else
865
  {
866
    /* CAN_IT is reset */
867
    pendingbitstatus = RESET;
868
  }
869
 
870
  return pendingbitstatus;
871
}
872
 
873
/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.