1 | void CAN_wrFilter (uint32_t ctrl, uint32_t id, uint32_t mask, uint8_t format) {
|
2 | CAN_TypeDef *pCAN = (ctrl == 1) ? CAN1 : CAN2;
|
3 | uint32_t CAN_msgId = 0;
|
4 | uint32_t CAN_msgMask = 0;
|
5 |
|
6 | if ((mask != 0) && (CAN_filterRegisterIdx != 0)) {
|
7 | CAN_filterIdx[ctrl-1]++;
|
8 | } /* increase filter index */
|
9 |
|
10 | if (CAN_filterIdx[ctrl-1] > 13) { /* check if Filter Memory is full*/
|
11 | return;
|
12 | }
|
13 |
|
14 | /* Setup identifier information IDE BIT */
|
15 | if (format == STANDARD_FORMAT) { /* Standard ID */
|
16 | CAN_msgId |= (uint32_t)(id << 21) | CAN_ID_STD;
|
17 | CAN_msgMask = (uint32_t)(mask << 21);
|
18 | } else { /* Extended ID */
|
19 | CAN_msgId |= (uint32_t)(id << 3) | CAN_ID_EXT;
|
20 | CAN_msgMask = (uint32_t)(mask << 3);
|
21 | }
|
22 |
|
23 | pCAN->FMR |= CAN_FMR_FINIT; /* set InitMode for filter banks */
|
24 | pCAN->FA1R &= ~(1UL << CAN_filterIdx[ctrl-1]); /* deactivate filter */
|
25 |
|
26 | pCAN->FS1R |= (uint32_t)(1 << CAN_filterIdx[ctrl-1]); /* set 32-bit scale configuration */
|
27 |
|
28 |
|
29 | if (mask == 0) { /* FBMx Bit Identifier List mode */
|
30 | pCAN->FM1R |= (uint32_t)(1 << CAN_filterIdx[ctrl-1]); /* set to 32-bit Identifier List mode */
|
31 | if (CAN_filterRegisterIdx = 0)
|
32 | pCAN->sFilterRegister[CAN_filterIdx[ctrl-1]].FR1 = CAN_msgId; /* 32-bit identifier */
|
33 | else
|
34 | pCAN->sFilterRegister[CAN_filterIdx[ctrl-1]].FR2 = CAN_msgId; /* 32-bit identifier */
|
35 | } else {
|
36 | pCAN->FM1R &= ~(uint32_t)(1 << CAN_filterIdx[ctrl-1]); /* set to 32-bit Mask mode */
|
37 | if (CAN_filterRegisterIdx = 0)
|
38 | pCAN->sFilterRegister[CAN_filterIdx[ctrl-1]].FR1 = CAN_msgId; /* 32-bit identifier */
|
39 | else
|
40 | pCAN->sFilterRegister[CAN_filterIdx[ctrl-1]].FR2 = CAN_msgMask; /* 32-bit identifier */
|
41 | CAN_filterRegisterIdx++;
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 | CAN_filterRegisterIdx++;
|
47 |
|
48 | pCAN->FFA1R &= ~(uint32_t)(1 << CAN_filterIdx[ctrl-1]); /* assign filter to FIFO 0 */
|
49 | pCAN->FA1R |= (uint32_t)(1 << CAN_filterIdx[ctrl-1]); /* activate filter */
|
50 |
|
51 | pCAN->FMR &= ~CAN_FMR_FINIT; /* reset initMode for filterBanks*/
|
52 |
|
53 | }
|