Forum: Mikrocontroller und Digitale Elektronik Problem: SPI Remap am STM32F407ZG


von Hans (Gast)


Angehängte Dateien:

Lesenswert?

Hallo Forengemeinde,

der REMAP von MOSI funktioniert nicht, woran kann das liegen? Ist ein 
REMAP für SPI überhaupt möglich? Hab ein Eval-Board wo der MOSI auf 
einer anderen Peripherie liegt, daher wollte ich einen REMAP 
durchführen.

Besten Dank
1
void init_SPI1(void){
2
  
3
  GPIO_InitTypeDef GPIO_InitStruct;
4
  SPI_InitTypeDef SPI_InitStruct;
5
        
6
        // enable peripheral clock
7
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
8
  
9
  // enable clock for used IO pins
10
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
11
        
12
        /* GPIO Deinitialisation */
13
        GPIO_DeInit(GPIOA);
14
        GPIO_DeInit(GPIOA);
15
        GPIO_DeInit(GPIOA);
16
  
17
  /* configure pins used by SPI1
18
   * PA4 = SCK
19
   * PA5 = MISO
20
   * PA6 = MOSI
21
   */
22
        
23
        // connect SPI1 pins to SPI alternate function
24
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI1);
25
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
26
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
27
        
28
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
29
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
30
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
31
        GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_DOWN;
32
        
33
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
34
  GPIO_Init(GPIOA, &GPIO_InitStruct);
35
        
36
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
37
  GPIO_Init(GPIOA, &GPIO_InitStruct);
38
        
39
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
40
  GPIO_Init(GPIOA, &GPIO_InitStruct);
41
  
42
  // enable clock for used IO pins
43
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
44
  
45
  /* Configure the chip select pin
46
     in this case we will use PE7 */
47
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
48
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
49
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
50
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
51
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
52
  GPIO_Init(GPIOE, &GPIO_InitStruct);
53
  
54
  GPIOE->BSRRL |= GPIO_Pin_7; // set PE7 high
55
  
56
  /* configure SPI1 in Mode 0 
57
   * CPOL = 0 --> clock is low when idle
58
   * CPHA = 0 --> data is sampled at the first edge
59
   */
60
        
61
        SPI_I2S_DeInit(SPI1);
62
  SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO line
63
        SPI_InitStruct.SPI_Mode = SPI_Mode_Master;     // transmit in master mode, NSS pin has to be always high
64
  SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide
65
  SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;        // clock is low when idle
66
  SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;      // data sampled at first edge
67
  SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set; // set the NSS management to internal and pull internal NSS high
68
  SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // SPI frequency is APB2 frequency / 4
69
  SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
70
  SPI_Init(SPI1, &SPI_InitStruct); 
71
  
72
  SPI_Cmd(SPI1, ENABLE); // enable SPI1
73
}
74
75
/* This funtion is used to transmit and receive data 
76
 * with SPI1
77
 *       data --> data to be transmitted
78
 *       returns received value
79
 */
80
uint8_t SPI1_send(uint8_t data){
81
82
  SPI1->DR = data; // write data to be transmitted to the SPI data register
83
  while( !(SPI1->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete
84
  while( !(SPI1->SR & SPI_I2S_FLAG_RXNE) ); // wait until receive complete
85
  while( SPI1->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore
86
  return SPI1->DR; // return received data from SPI data register
87
}
88
89
int main(void){
90
  
91
  uint8_t received_val = 0;
92
  
93
  init_SPI1();
94
  
95
  while(1){
96
    
97
    GPIOE->BSRRH |= GPIO_Pin_7; // set PE7 (CS) low
98
    SPI1_send(0xFA);  // transmit data
99
    received_val = SPI1_send(0x00); // transmit dummy byte and receive data
100
    GPIOE->BSRRL |= GPIO_Pin_7; // set PE7 (CS) high
101
  }
102
}

von Hans (Gast)


Lesenswert?

PS: An die Standard SPI IOs je Port bekomme ich die Signale ohne Problem 
gemapped.

von Hans (Gast)


Lesenswert?

PS: An die Standard SPI IOs je Port bekomme ich die Signale ohne Problem
gemapped.
1
#include "includes.h"
2
3
void init_SPI1(void){
4
  
5
  GPIO_InitTypeDef GPIO_InitStruct;
6
  SPI_InitTypeDef SPI_InitStruct;
7
  
8
  // enable clock for used IO pins
9
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
10
        
11
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
12
  
13
  /* configure pins used by SPI1
14
   * PA5 = SCK
15
   * PA6 = MISO
16
   * PB5 = MOSI
17
   */
18
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_5;
19
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
20
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
21
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
22
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
23
  GPIO_Init(GPIOA, &GPIO_InitStruct);
24
        
25
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
26
        GPIO_Init(GPIOB, &GPIO_InitStruct);  
27
  
28
  // connect SPI1 pins to SPI alternate function
29
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
30
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
31
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI1);
32
  
33
  // enable clock for used IO pins
34
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
35
  
36
  /* Configure the chip select pin
37
     in this case we will use PE7 */
38
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
39
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
40
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
41
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
42
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
43
  GPIO_Init(GPIOE, &GPIO_InitStruct);
44
  
45
  GPIOE->BSRRL |= GPIO_Pin_7; // set PE7 high
46
  
47
  // enable peripheral clock
48
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
49
  
50
  /* configure SPI1 in Mode 0 
51
   * CPOL = 0 --> clock is low when idle
52
   * CPHA = 0 --> data is sampled at the first edge
53
   */
54
  SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
55
  SPI_InitStruct.SPI_Mode = SPI_Mode_Master;     // transmit in master mode, NSS pin has to be always high
56
  SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide
57
  SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;        // clock is low when idle
58
  SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;      // data sampled at first edge
59
  SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set; // set the NSS management to internal and pull internal NSS high
60
  SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; // SPI frequency is APB2 frequency / 4
61
  SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
62
  SPI_Init(SPI1, &SPI_InitStruct); 
63
  
64
  SPI_Cmd(SPI1, ENABLE); // enable SPI1
65
}
66
67
/* This funtion is used to transmit and receive data 
68
 * with SPI1
69
 *       data --> data to be transmitted
70
 *       returns received value
71
 */
72
uint8_t SPI1_send(uint8_t data){
73
74
  SPI1->DR = data; // write data to be transmitted to the SPI data register
75
  while( !(SPI1->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete
76
  while( !(SPI1->SR & SPI_I2S_FLAG_RXNE) ); // wait until receive complete
77
  while( SPI1->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore
78
  return SPI1->DR; // return received data from SPI data register
79
}
80
81
int main(void){
82
  
83
  uint8_t received_val = 0;
84
  
85
  init_SPI1();
86
  
87
  while(1){
88
    
89
    GPIOE->BSRRH |= GPIO_Pin_7; // set PE7 (CS) low
90
    SPI1_send(0xAF);  // transmit data
91
    received_val = SPI1_send(0xAA); // transmit dummy byte and receive data
92
    GPIOE->BSRRL |= GPIO_Pin_7; // set PE7 (CS) high
93
  }
94
}

von Matthias S. (Firma: matzetronics) (mschoeldgen)


Lesenswert?

Hans schrieb:
> /* GPIO Deinitialisation */
>         GPIO_DeInit(GPIOA);
>         GPIO_DeInit(GPIOA);
>         GPIO_DeInit(GPIOA);

Ich sags dreimal und dann ist es wahr? Hehehe, einmal reicht hier.
Ich glaube, du hast dich in den AF Funktionen verlesen, Datenblatt Seite 
60, Funktion AF5:

PA4 = NSS
PA5 = SCK
PA6 = MISO
PA7 = MOSI

Hans schrieb:
> GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
>   GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
>   GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
>         GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_DOWN;
>
>         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
>   GPIO_Init(GPIOA, &GPIO_InitStruct);
>
>         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
>   GPIO_Init(GPIOA, &GPIO_InitStruct);
>
>         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
>   GPIO_Init(GPIOA, &GPIO_InitStruct);

Das kannst du einfacher haben:
1
// SPI1 korrigiert
2
 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
3
   GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
4
   GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
5
   GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_DOWN; 
6
   GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
7
   GPIO_Init(GPIOA, &GPIO_InitStruct);
8
   GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
9
   GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
10
   GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
11
   GPIO_Init(GPIOA, &GPIO_InitStruct);

: Bearbeitet durch User
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.