Hi, Please help me to know why the following example code for SPI of STM32L152VD is not working, I do not see any signals in scope on pins SCK and MOSI niether in Data register in dubugger.
void SPI_Master_init(void) { GPIO_InitTypeDef GPIO_InitStruct; SPI_InitTypeDef SPI_InitStruct; /* Initialization and Configuration functions *********************************/ // enable peripheral clock RCC_AHBPeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); RCC_AHBPeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_40MHz; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStruct); 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_40MHz; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStruct); // connect SPI1 pins to SPI alternate function GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); // // connect SPI2 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); SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // transmit in master mode, NSS pin has 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_Hard; // set the NSS HARD SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; // SPI frequency is SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first SPI_Init(SPI1, &SPI_InitStruct); SPI_SSOutputCmd(SPI1,ENABLE); //Set SSOE bit in SPI_CR1 register SPI_Cmd(SPI1, ENABLE); // enable SPI1 SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // transmit in master mode, NSS pin has 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_Hard; // set the NSS HARD SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; // SPI frequency is SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first SPI_Init(SPI2, &SPI_InitStruct); SPI_SSOutputCmd(SPI2,ENABLE); //Set SSOE bit in SPI_CR1 register SPI_Cmd(SPI2, ENABLE); // enable SPI2
and in main:
int main(void){ SPI_Master_init(); while(1) { SPI_I2S_SendData(SPI1, 0xFFFF); SPI1->DR = 0x11; SPI_I2S_SendData(SPI2,0xFFFF); SPI2->DR = 0x11;
Dear Gary, I initialized SPI1 and SPI2 to check if any will work. (both master and independent)
http://www.keil.com/forum/59935/
I have made a header board for STM32L152VD after every restart it takes few seconds for the board to start running. I used the same program to run on STM32L152RC discovery board but I had no problem.
-->
Did the mentioned SPI problem happen on the header board for STM32L152VD or the STM32L152RC discovery board?
looking at your code. I am wondering about if you are setting up SPI2. I see some of the lower code as duplicate of code you already typed in. You declare master mode two times. the code looks the same.
Dear Pier,
Thanks. I corrected the code.
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
Yet I don't have signals.
RCC_AHBPeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); RCC_AHBPeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
Not on the AHB
It seems sth is making SPI disabled. I added wait cycle after sending data, but in scope no signal appear.
You'd probably want to be waiting on TXE before jamming data in the Data Register, and understand there are TWO registers, the one you can read in the debugger is the receiving one, not the transmitting value you're jamming in there.
View all questions in Keil forum