cancel
Showing results for 
Search instead for 
Did you mean: 

problem initializing spi2/3 on stm32f407vg discovery 4 keil 5.11 cmsis

seindich
Associate II
Posted on June 26, 2014 at 21:17

got SPI1 working in master/slave mode, but unable to init spi2 (or spi3)

logical analyzer shows flat in master mode (all pins I tried to map on PB are flat, spi2_irqn never called) here is my code:

#include ''stm32f4xx.h'' 
#include ''stm32f4xx_gpio.h'' 
#include ''stm32f4xx_spi.h'' 
#include <
misc.h
> 
#include <
stdint.h
> 
GPIO_InitTypeDef GPIO_InitStruct; 
SPI_InitTypeDef SPI_InitStruct; 
uint16_t buffer0; 
uint16_t buffer1; 
void init_SPI2(void); 
int main() 
{ 
__enable_irq(); 
init_SPI2(); 
NVIC_EnableIRQ(SPI2_IRQn); 
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE); 
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, ENABLE); 
buffer0 = 0; 
buffer1 = 0; 
while(1) 
{ 
SPI_I2S_SendData(SPI2, 0x0444); 
} 
} 
void SPI2_IRQHandler() //slave_back 
{ 
if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) == SET) //RX not empty 
{ 
buffer1 = buffer0; 
buffer0 = SPI2->DR; 
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, ENABLE); 
} 
if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_TXE) == SET) // TX empty 
{ 
SPI2->DR = 0x6666;//buffer1; 
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, DISABLE); 
} 
} 
void init_SPI2(void){ 
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); 
RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2 , ENABLE); 
NVIC_InitTypeDef NVIC_InitStructure; 
NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn; 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 
NVIC_Init(&NVIC_InitStructure); 
/* configure pins used by 
b12 = nss 
* b13 = SCK 
* b14 = MISO 
* b15 = MOSI 
*/ 
/* 
GPIOB->AFR[0] = 0x000000; // AF 
GPIOB->AFR[1] = 0x000000; // AF 
GPIOC->AFR[0] = 0x000000; // AF 
GPIOC->AFR[1] = 0x000000; // AF 
GPIOD->AFR[0] = 0x000000; // AF 
GPIOD->AFR[1] = 0x000000; // AF 
GPIOA->AFR[0] = 0x000000; // AF 
GPIOA->AFR[1] = 0x000000; // AF */ 
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ; 
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; 
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; 
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz; 
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; 
GPIO_Init(GPIOB, &GPIO_InitStruct); 
// connect SPI2 pins to SPI alternate function 
GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_SPI2); 
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2); 
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2); 
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2); 
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines 
SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // transmit in master mode, NSS pin has to be always high 
SPI_InitStruct.SPI_DataSize = SPI_DataSize_16b; // one packet of data is 8 bits wide 
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; // clock is low when idle 
SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge; 
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;// | SPI_NSSInternalSoft_Set; // set the NSS management to internal and pull internal NSS high 
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // SPI frequency is APB2 frequency / 
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first 
SPI_Init(SPI2, &SPI_InitStruct); 
SPI_Cmd(SPI2, ENABLE); // enable SPI 
} 
#ifdef USE_FULL_ASSERT 
void assert_failed(uint8_t* file, uint32_t line) 
{ 
while (1) 
{ 
} 
}/* assert_failed */ 
#endif/*USE_FULL_ASSERT*/ 

discovery4 datasheet says PB12/13/14/15 are free GPIO I notice spi1 working and distinct from spi2/3 by different APB; may be it would be a key .. am i missing something? I have checked: CMSIS->CORE Device->startup+dma Device->stdperiph->spi+rcc+gpio+framework in runtime env of keil and set USE_STDPERIPH_DRIVER in Cpreprocessor in proj. options #stm32f4-spi2
0 REPLIES 0