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

Subversion Repositories openrisc

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

powered by: WebSVN 2.1.0

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