2017-04-17 08:30 AM
I'm new to STM32 line of Microcontrollers, However I have worked extensively on TI MSP430 line.
I recently bought STM32L-DISCOVERY with STM32L152RCT6 controller onboard.
I got the en.stsw-stm32077 standard peripherals and CSIR from STM website.
I'm using IAR-EWARM Toolchain with a linker supplied from STM32 Website. So could not be a compatibility issue of some kind.
Im just trying to intialize SPI and transmit a dummy data and observe the same in my Logic Analyser.
I got the CSn to toggle since its GPIO but my MOSI & SCLK pins are always 0. From the Function Block Diagram I understood that SPI1 is connected to AHB Bus & APB2 Bus so enabled that.
The following is my main.c code.
#include 'stm32l1xx.h'
#include 'functions.h'void InitSysCLK(void);
void configure_SPI1(void);char buffer;
int main()
{ InitSysCLK(); configure_SPI1(); while(1) { GPIO_ResetBits(GPIOA, GPIO_Pin_4); delay_us(20); while(!(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE))); // Check if Tx Buffer is empty SPI_I2S_SendData(SPI1, 0x01); // Transmit Data while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY)); // Check if SPI Pherip is busy or Wait for TX to complete delay_us(16); // 17us while(!(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE))); // Check if Rx Buffer is emptybuffer = SPI_I2S_ReceiveData(SPI1);
GPIO_SetBits(GPIOA, GPIO_Pin_4); delay_ms(100); }}void InitSysCLK(void)
{ RCC_DeInit(); RCC_HSICmd(ENABLE); RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); CLK = RCC_GetSYSCLKSource(); RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div1); RCC_PCLK2Config(RCC_HCLK_Div1); RCC_MCOConfig(RCC_MCOSource_HSI, RCC_MCODiv_1);}void configure_SPI1(void){ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); GPIO_InitTypeDef GPIO_SPI1_InitStruct; // Configure SPI1 - PA4-NSS, PA5-SCK, PA11-MISO, PA12-MOSI GPIO_SPI1_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_11 | GPIO_Pin_12; GPIO_SPI1_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_SPI1_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_SPI1_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_SPI1_InitStruct.GPIO_Speed = GPIO_Speed_40MHz; GPIO_Init(GPIOA, &GPIO_SPI1_InitStruct); GPIO_PinAFConfig(GPIOA, GPIO_Pin_5, GPIO_AF_SPI1); //SCK GPIO_PinAFConfig(GPIOA, GPIO_Pin_11, GPIO_AF_SPI1); //MISO GPIO_PinAFConfig(GPIOA, GPIO_Pin_12, GPIO_AF_SPI1); //MOSI GPIO_InitTypeDef GPIO_CSn_InitStruct; GPIO_CSn_InitStruct.GPIO_Pin = GPIO_Pin_4; GPIO_CSn_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_CSn_InitStruct.GPIO_Speed = GPIO_Speed_40MHz; GPIO_CSn_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_SPI1_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;GPIO_Init(GPIOA, &GPIO_CSn_InitStruct);
SPI_InitTypeDef SPI1_InitStruct; SPI_StructInit(&SPI1_InitStruct); // Setting SPI1 with default values SPI1_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI1_InitStruct.SPI_Mode = SPI_Mode_Master; SPI1_InitStruct.SPI_DataSize = SPI_DataSize_8b; SPI1_InitStruct.SPI_CPOL = SPI_CPOL_Low; SPI1_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; SPI1_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set; SPI1_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // 16MHZ/8 = 2MHZ SPI1_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB; SPI_Init(SPI1, &SPI1_InitStruct); GPIO_SetBits(GPIOA, GPIO_Pin_4); SPI_SSOutputCmd(SPI1, ENABLE); SPI_Cmd(SPI1, ENABLE);}
Could any one be kind enough to tell me what the problem is , I have been at it for more than a day , its quite embarrassing .
2017-04-17 08:49 AM
>>GPIO_PinAFConfig(GPIOA, GPIO_Pin_5, GPIO_AF_SPI1); //SCK
Use PinSource, an index
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); //SCK
2017-04-17 08:55 AM
Try to comment out the
SPI_SSOutputCmd
function.If the pin is managed by SW, it should stay that way.
For the code, delays are optional, the polling for the busy bit is probably useless as RXNE will tell you the byte transfer is complete and a full byte has parked in the DR (RX) register.
2017-04-20 01:36 AM
Clive One was spot on.
Thank you guys.