2014-02-20 04:42 PM
hi guys.
i want to use my stm32f4 spi2 and i wrote some code and i think it should be true,because i wrote the same code for SPI1 in this board and it works very well,all i want is to send a data through mosi and receive that data from miso and show that specific data through usart and serial port in my pc.pls tell me what is the problem. is my code wrong ??here is mu code : &sharpinclude <stm32f4xx.h> void RCC_configuration(){ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);// RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);}void init_GPIO(){ GPIO_InitTypeDef GPIO_InitStruct; /* configure pins used by SPI1 * PB13 = SCK * PB14 = MISO * PB15 = MOSI */ GPIO_InitStruct.GPIO_Pin = 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_50MHz; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB, &GPIO_InitStruct); // connect SPI1 pins to SPI alternate function GPIO_PinAFConfig(GPIOB, GPIO_PinSource13 , GPIO_AF_SPI2); GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2); GPIO_PinAFConfig(GPIOB, GPIO_PinSource15 , GPIO_AF_SPI2); }void init_SPI2(){ SPI_InitTypeDef SPI_InitStruct; SPI_InitStruct.SPI_Direction =SPI_Direction_2Lines_FullDuplex; // set to FullDuplexmode, seperate MOSI and MISO lines SPI_InitStruct.SPI_Mode = SPI_Mode_Master; SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // 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_1Edge; // data sampled at first edge SPI_InitStruct.SPI_NSS =SPI_NSS_Soft; SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB; // data is transmitted MSB first SPI_Init(SPI2, &SPI_InitStruct); SPI_Cmd(SPI2, ENABLE); // enable SPI2}void init_usart(void){ GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx; USART_Init(USART2, &USART_InitStructure); USART_Cmd(USART2, ENABLE); } int main(){ RCC_configuration(); init_GPIO(); init_SPI2(); init_usart(); uint8_t received_val = 50; while (1) { SPI_I2S_SendData(SPI2,0x33); received_val = SPI_I2S_ReceiveData(SPI2); USART_SendData(USART2,received_val); }} #master #stm32f4 #discovery #spi2014-02-20 06:38 PM
RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
So APB1 then. SPI_I2S_SendData(SPI1,0x33); received_val = SPI_I2S_ReceiveData(SPI1); SPI2?? And you should perhaps be attentive to the TXE/RXNE states of the SPI/USART?2014-02-21 12:35 AM
that's right i fixed it in my code(SPI) but i forgot to fix it in forum. but the problem is exist.
what you mean by TEX/RXNE state of the SPI/USART??you know i test exactly the same code for SPI1 then connected PA6 and PA7 together and it worked well. i just changed the GPIO pins and configuration for SPI and connected PB14 to PB15 but it doesn't work. is there any thing that i should do??2014-02-21 01:13 AM
Clive pointed it out above:
RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); should be RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); JW2014-02-21 01:31 AM
thank you soooooo much guys