Forum: Mikrocontroller und Digitale Elektronik SPI STM32 W5100


von Peter F. (peter_da_steht_er)


Lesenswert?

Hallo ich hab das Nucleo Board L152re und hab ein Arduino W5100 Borad 
draufgesteckt. Also verwendet wird SPI1. Die gesamte Kommunikation 
funktioniert, das W5100 antwortet auch so wie es soll. Nur intern 
bekomme ich beim auslesen von "SPI1->DR" nur 0x00, wo habe ich meinen 
Fehler?


Ini Funktion
1
GPIO_InitTypeDef        GPIO_InitStructure;
2
SPI_InitTypeDef SPI_InitStructure;
3
4
  
5
  
6
  
7
  
8
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB, ENABLE);  
9
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
10
  
11
// PortA Pin5 SCLK; Pin7 MOSI; Pin6 MISO und PortB Pin6 cs  
12
  
13
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7; 
14
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
15
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
16
GPIO_Init(GPIOA, &GPIO_InitStructure);  
17
  
18
  
19
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
20
GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_IN;
21
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
22
GPIO_Init(GPIOA, &GPIO_InitStructure);  
23
  
24
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
25
GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_OUT;
26
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
27
GPIO_Init(GPIOB, &GPIO_InitStructure);
28
29
GPIO_SetBits(GPIOB, GPIO_Pin_6);
30
31
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
32
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
33
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
34
35
36
37
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
38
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
39
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
40
41
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
42
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
43
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
44
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
45
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
46
SPI_Init(SPI1, &SPI_InitStructure);
47
48
SPI_Cmd(SPI1, ENABLE);

Zur erklärung cs_off selektiert den W5100.
Zum Test hab ich noch spi_read_8_ohne eingepflegt, diese liest nur das 
Register aus ohne Dummy Byte zu senden.
1
void cs_on(void)
2
{
3
    
4
  while((SPI1->SR & 0x80))
5
    {};
6
  GPIO_SetBits(GPIOB, GPIO_Pin_6);
7
  
8
};
9
10
void cs_off(void) //Startet
11
{
12
    
13
  GPIO_ResetBits(GPIOB, GPIO_Pin_6);
14
  
15
};
16
17
18
void spi_send_8(uint32_t data)
19
{
20
  
21
    
22
  
23
    while(!(SPI1->SR & 0x02))
24
    {};      
25
    SPI1->DR = (data & 0xFF);
26
        
27
          
28
      
29
return ;  
30
};
31
32
uint32_t spi_read_8(void)
33
{
34
    uint32_t data = 0;
35
    
36
      
37
    while(!(SPI1->SR & 0x0002))
38
    {};      
39
    SPI1->DR = (data & 0xFF);
40
  
41
    while(!(SPI1->SR & 0x01))  
42
    {};  
43
    data= SPI1->DR; 
44
      
45
  
46
  
47
return data;  
48
};
49
50
uint32_t spi_read_8_ohne(void)
51
{
52
    uint32_t data = 0;
53
    
54
      
55
    
56
  
57
    while(!(SPI1->SR & 0x01))  
58
    {};  
59
    data= SPI1->DR; 
60
      
61
  
62
  
63
return (data);  
64
};
65
66
67
68
void w5100_write(uint16_t adress, uint8_t data)
69
{
70
cs_off();
71
  spi_send_8(0xF0);
72
  spi_send_8((adress & 0xFF00)>>8);
73
  spi_send_8(adress & 0x00FF);
74
  spi_send_8(data);
75
  spi_read_8_ohne();
76
cs_on();  
77
return;  
78
};  
79
80
uint32_t w5100_read(uint16_t adress)
81
{
82
  uint32_t data = 0;
83
  
84
cs_off();
85
  spi_send_8(0x0F);
86
  spi_send_8((adress & 0xFF00)>>8);
87
  spi_send_8(adress & 0x00FF);
88
  spi_read_8_ohne();
89
  data= spi_read_8();
90
  cs_on();
91
return data;    
92
};

von grundschüler (Gast)


Lesenswert?

ich hing - bis grade - an genau dem gleichen Problem: Deinen Code 
genommen, mit leichten Änderungen an Coide-F103 angepasst - und es läuft 
auf Anhieb:
1
uc_net_spi_init(){
2
GPIO_InitTypeDef        GPIO_InitStructure;
3
SPI_InitTypeDef SPI_InitStructure;
4
5
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
6
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
7
8
/* Configure SPI2 pins: NSS, SCK, MISO and MOSI */
9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
12
GPIO_Init(GPIOA, &GPIO_InitStructure);
13
14
15
16
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
17
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
18
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
19
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
20
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
21
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
22
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
23
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
24
SPI_Init(SPI1, &SPI_InitStructure);
25
26
SPI_Cmd(SPI1, ENABLE);
27
}
28
29
u8 chan_xchg_spi (
30
  u8 dat  /* Data to send */
31
)
32
{
33
  SPI1->DR = dat;
34
  while (SPI1->SR & _BV(7)) ;
35
  return (u8)SPI1->DR;
36
}

von Peter F. (peter_da_steht_er)


Lesenswert?

Haha jetzt hätte von Anfang an funktioniert. Das Problem MISO darf nicht 
als INPUT def. werden sondern als alt. funktion. Aber danke, mit deiner 
Hilfe habe ichs geschafft.

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.