Forum: Mikrocontroller und Digitale Elektronik STM32f4 CAN2 AF Fehler, Warum?


von Nils P. (ert)


Lesenswert?

Hallo,

Bin soeben am experimentieren mit CAN am STM32f4 µC

CAN1, funktioniert einwandfrei. Bei CAN2 geht die GPIO_PinAFConfig() 
nicht.

Das geht:
1
GPIO_PinAFConfig(GPIOD, GPIO_Pin0, GPIO_AF_CAN1);
2
GPIO_PinAFConfig(GPIOD, GPIO_Pin1, GPIO_AF_CAN1);
,

das geht nicht:
1
GPIO_PinAFConfig(GPIOB, GPIO_Pin_6, GPIO_AF_CAN2);
2
GPIO_PinAFConfig(GPIOB, GPIO_Pin_12, GPIO_AF_CAN2);

Alles andere ist auskommentiert, bei schrittweise im Debugger schreibt 
er wie er soll beim GPIOD seine Werte rein, nur beim B Register nicht 
-.-

Wenn ich es aber manuell mache:
1
GPIOB->AFR[0]|=(0x09<<24);
2
GPIOB->AFR[1]|=(0x09<<16);
geht es, ich ändere sonst nichts...

Kann mir einer sagen warum? Nicht das die PinAFConfig fürn Ar*** ist und 
ich meine Register immer manuell beschreiben sollte.

Nutze Keil-IDE. Eins von den Standard Bsp ein wenig abgeändert.

Anhang:
1
void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
2
{
3
  uint32_t temp = 0x00;
4
  uint32_t temp_2 = 0x00;
5
  
6
  /* Check the parameters */
7
  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
8
  assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
9
  assert_param(IS_GPIO_AF(GPIO_AF));
10
  
11
  temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
12
  GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
13
  temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] | temp;
14
  GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_2;
15
}

von Dr. Sommer (Gast)


Lesenswert?

Nils P. schrieb:
> GPIO_PinAFConfig(GPIOB, GPIO_Pin_6, GPIO_AF_CAN2);
> GPIO_PinAFConfig(GPIOB, GPIO_Pin_12, GPIO_AF_CAN2);
Die GPIO_PinAFConfig möchte die Nummer des Pins (0-15) haben, 
GPIO_Pin_6 aber ist 0x40, hat also das 6. Bit auf 1. Du musst einfach 6 
bzw. 12 übergeben. (Willkommen bei der hochqualitativen maximal 
intuitiven Standard Periphal Library!).

von Markus M (Gast)


Lesenswert?

Hi

Hat jetzt das ganze funktioniert?
Ich wäre sehr interessiert an deiner CAN - Library, da ich mich seit 
mehreren Tagen am CAN versuche, aber dennoch keinen Pieps aus dem CAN 
herausbekomme. Würdest du die Files zur Verfügung stellen?

Gruss
Markus

von Nils P. (ert)


Lesenswert?

Jop kan ich, kostet dich nen Käffchen ;-)

CAN1 empfängt nur, CAN2 sendet nur-->
1
//CAN1
2
#define GPIO_CAN1                         GPIOD
3
#define GPIO_Pin_CAN1_RX                  GPIO_Pin_0
4
#define GPIO_Pin_CAN1_TX                  GPIO_Pin_1
5
6
//CAN2
7
#define GPIO_CAN2                         GPIOB
8
#define GPIO_Pin_CAN2_RX                  GPIO_Pin_12
9
#define GPIO_Pin_CAN2_TX                  GPIO_Pin_6
10
11
12
void CAN_init(void)
13
{
14
  //Periph-Clock an
15
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 | RCC_APB1Periph_CAN2, ENABLE);
16
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOD, ENABLE);
17
18
  //CAN1  
19
  GPIO_StructInit(&GPIO_InitStructure);  
20
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN1_RX;
21
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
22
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
23
  GPIO_Init(GPIO_CAN1, &GPIO_InitStructure);
24
   
25
  GPIO_StructInit(&GPIO_InitStructure);  
26
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN1_TX;
27
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
28
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
29
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
30
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
31
  GPIO_Init(GPIO_CAN1, &GPIO_InitStructure);
32
  
33
  GPIO_PinAFConfig(GPIO_CAN1, GPIO_PinSource0, GPIO_AF_CAN1);
34
  GPIO_PinAFConfig(GPIO_CAN1, GPIO_PinSource1, GPIO_AF_CAN1);
35
  
36
  //CAN2
37
  GPIO_StructInit(&GPIO_InitStructure);  
38
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN2_RX;
39
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
40
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
41
  GPIO_Init(GPIO_CAN2, &GPIO_InitStructure);
42
  
43
  GPIO_StructInit(&GPIO_InitStructure);  
44
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN2_TX;
45
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
46
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
47
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
48
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
49
  GPIO_Init(GPIO_CAN2, &GPIO_InitStructure);
50
     
51
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_CAN2);
52
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_CAN2); 
53
//
54
//GPIOB->AFR[0]|=(0x09<<24);
55
//GPIOB->AFR[1]|=(0x09<<16);
56
  
57
  CAN_DeInit(CAN1);
58
  CAN_DeInit(CAN2);
59
60
   
61
  //Merde: überprüfen für 42Mhz Clock APB1 
62
  CAN_StructInit(&CAN_InitStructure);
63
  CAN_InitStructure.CAN_Prescaler = 4;          //!< Specifies the length of a time quantum. It ranges from 1 to 1024.
64
  CAN_InitStructure.CAN_SJW = CAN_SJW_2tq;      //!< Specifies the maximum number of time quanta the CAN hardware is allowed to lengthen or shorten a bit to perform resynchronization.
65
  CAN_InitStructure.CAN_BS1 = CAN_BS1_13tq;      //!< Specifies the number of time quanta in Bit Segment 1.
66
  CAN_InitStructure.CAN_BS2 = CAN_BS2_7tq;      //!< Specifies the number of time quanta in Bit Segment 2.
67
  //  tq=4/42Mhz   1Bit=SJW+BS1+BS2 bei 500kbaud= 2µSek
68
  
69
  CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;  //!< Specifies the CAN operating mode.
70
  CAN_InitStructure.CAN_TTCM = DISABLE;    //!< Enable or disable the time triggered communication mode.
71
  CAN_InitStructure.CAN_ABOM = DISABLE;    //!< Enable or disable the automatic bus-off management.
72
  CAN_InitStructure.CAN_AWUM = DISABLE;    //!< Enable or disable the automatic wake-up mode.
73
  CAN_InitStructure.CAN_NART = ENABLE;     //!< Enable or disable the non-automatic retransmission mode.
74
  CAN_InitStructure.CAN_RFLM = DISABLE;   //!< Enable or disable the Receive FIFO Locked mode.
75
  CAN_InitStructure.CAN_TXFP = DISABLE;   //!< Enable or disable the transmit FIFO priority.
76
  
77
  CAN_Init(CAN1, &CAN_InitStructure);
78
  CAN_Init(CAN2, &CAN_InitStructure);
79
    
80
  
81
 // Can filter Init
82
  CAN_FilterInit(&CAN_FilterInitStructure);
83
  CAN_FilterInitStructure.CAN_FilterNumber = 0;
84
  CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
85
  CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
86
  CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0112<<5;
87
  CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
88
  CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF;
89
  CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF;
90
  CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
91
  CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
92
  CAN_FilterInit(&CAN_FilterInitStructure);  
93
}

 Interruptaufruf-->
1
CanTxMsg TxMessage;
2
CanRxMsg RxMessage1;
3
4
// 0.5ms interrupt
5
void TIM8_TRG_COM_TIM14_IRQHandler(void)
6
{    
7
//uint16_t Transmit_u16;    
8
        
9
TIM_ClearITPendingBit(TIM14, TIM_IT_Update);
10
11
TxMessage.StdId = 0x64;            //Standart Identifier
12
TxMessage.ExtId = 0x01;             //extendet Identifier
13
TxMessage.RTR = CAN_RTR_DATA;       //Data frame
14
TxMessage.IDE = CAN_ID_STD;         //Standart Identifier
15
TxMessage.DLC = 8;                  //length of the frame
16
  
17
18
TxMessage.Data[0] = xxx&0xff;
19
TxMessage.Data[1] = (xxx>>8)&0xff;
20
TxMessage.Data[2] =   yyy&0xff;
21
TxMessage.Data[3] =  (yyy>>8)&0xff;  
22
TxMessage.Data[4] = zzz&0xff;
23
TxMessage.Data[5] = (zzz>>8)&0xff;
24
TxMessage.Data[6] = (uint16_t) (zzz_float*1000)&0xff;
25
TxMessage.Data[7] = (((uint16_t)(zzz_float*1000))>>8)&0xff;
26
27
CAN_Transmit(CAN2, &TxMessage);
28
29
CAN_Receive(CAN1, 0, &RxMessage1);
30
}

Edit: das ganz läuft auf dem stm32f407VG also das STM32F4Discovery

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.