2014-06-13 09:32 AM
I'm trying to get SPI2 up and running to talk to another chip and I'm having some problems. I can see my SCK and MOSI traces on my scope but my DR register always has zero. I've even tried this right after SPI config:
SPI2->DR = 1;TXE bit is high when I try this call.I guess I'm lost at what to look at next.2014-06-13 10:57 AM
Yeah, and I have no idea how your code is configuring this.
Observe that what you read from DR is inbound data (MISO for a Master), it's not the same register you write to DR for the outbound data (MOSI). It is not a memory cell, and looking at it and SR in a debug interface will interfere with it's operation.2014-06-13 11:29 AM
I'm trying to port an app from an STM32F103 to STM32L151 and when I go into debug on the working STM32F103, I can see the DR register change.
/* SPI configuration -------------------------------------------------------*/ SPI_I2S_DeInit(SPI2); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB; //SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStructure); /* Enable the SPI peripheral */ SPI_Cmd(SPI2, ENABLE);2014-06-13 11:37 AM
It's a bit hard to understand what is the exact nature of your problem.
Anyway, one noticeable difference between 103 and 151 is that the MISO line must be configured as AF_SPI on 151 whereas it was a simple input on 103. This might help ...2014-06-13 01:51 PM
ie Mapping AF functionality to pins, here for example SPI1 PA7
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); // SPI1 MOSI2014-06-13 05:53 PM
Here's my GPIO config.
/* Configure I/O for Chip select */ GPIO_InitStructure.GPIO_Pin = CR95HF_SPI_NSS_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; GPIO_Init(CR95HF_SPI_NSS_GPIO_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2); // SCK GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2); // MOSI //GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2); // MISO GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; /* SPI SCK pin configuration */ GPIO_InitStructure.GPIO_Pin = CR95HF_SPI_SCK_PIN; GPIO_Init(GPIOB, &GPIO_InitStructure); /* SPI MOSI pin configuration */ GPIO_InitStructure.GPIO_Pin = CR95HF_SPI_MOSI_PIN; GPIO_Init(GPIOB, &GPIO_InitStructure); /* SPI MISO pin configuration */ GPIO_InitStructure.GPIO_Pin = CR95HF_SPI_MISO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_Init(GPIOB, &GPIO_InitStructure); // SPI_NSS = High Level GPIO_SetBits(CR95HF_SPI_NSS_GPIO_PORT, CR95HF_SPI_NSS_PIN); Maybe something is wrong here but what I see as my first problem is the only data going out on MOSI is 0x00. If I don't know if a valid command is going to the second chip, how can I troubleshoot the MISO line? In other words, I don't think anything will come in on MISO if the data sent isn't valid.2014-06-15 06:29 PM
OK thanks for clearing that up. Should the MISO line be an input or output? Because the scope trace doesn't look right. My brain tells me input, but one of the examples has it as an output, like the other two lines. This is what I have, which is like the example.
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2); // SCK GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2); // MOSI GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2); // MISO GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; /* SPI SCK pin configuration */ GPIO_InitStructure.GPIO_Pin = CR95HF_SPI_SCK_PIN; GPIO_Init(GPIOB, &GPIO_InitStructure); /* SPI MOSI pin configuration */ GPIO_InitStructure.GPIO_Pin = CR95HF_SPI_MOSI_PIN; GPIO_Init(GPIOB, &GPIO_InitStructure); /* SPI MISO pin configuration */ GPIO_InitStructure.GPIO_Pin = CR95HF_SPI_MISO_PIN; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure I/O for Chip select */ GPIO_InitStructure.GPIO_Pin = CR95HF_SPI_NSS_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; GPIO_Init(CR95HF_SPI_NSS_GPIO_PORT, &GPIO_InitStructure); /* SPI configuration -------------------------------------------------------*/ SPI_I2S_DeInit(SPI2); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStructure);2014-06-16 08:06 AM
Turns out I had a hardware problem. Corrected that and boom, works!