F103 and F072 SPI communication
I am trying to communicate with a device (a radio module which has inside STM32F103 MCU ) with my STM32f072B disco board using SPI
I can program the radio module almost as a standard STM32 MCU the only difference is that I don't have access to all pins.
Disco board is set as master, the RF module is a slave.
Both devices run at 16MHz; SPI prescaler is 256, 8bit data size, H polarity, 2edge phase, soft management of nss
I managed so far to output the clock signal but I don't have nothing on the MOSI line, allthough I'm sending data
I am not fully cleared about how the pins must be configured
The program for disco board:
#include 'stm32f0xx.h'
#define u8 uint8_t
#define u16 uint16_ttypedef uint32_t u32;typedef signed short s8;void spi1_config(void);
void delay(uint32_t t);#define NSS_Port GPIOA
#define NSS_Pin GPIO_Pin_4/* Private macro -------------------------------------------------------------*/
#define NSS_hi() NSS_Port->BSRR = NSS_Pin#define NSS_lo() NSS_Port->BRR = NSS_Pin/* Private variables ---------------------------------------------------------*//* Private function prototypes -----------------------------------------------*//* Private functions ---------------------------------------------------------*/
u8 t=0;extern u8 h;extern u8 y;/** * @brief Main program * @param None * @retval None */int main(void){ spi1_config();NSS_hi();
while(1) { delay(100); NSS_lo(); delay(150);SPI_SendData8(SPI1,0xab);
delay(200); NSS_hi(); }}void spi1_config(void)//master
{ GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; SPI_InitTypeDef SPI_InitStructure;/* Enable the SPI periph */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);/* Enable SCK, MOSI, MISO and NSS GPIO clocks */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);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_Level_3;/* SPI SCK MOSI MISO pins configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; GPIO_Init(GPIOA, &GPIO_InitStructure);/* SPI NSS pin configuration as GPIO Pin */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_Init(GPIOA, &GPIO_InitStructure);GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0); GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_0);/* SPI configuration -------------------------------------------------------*/
SPI_I2S_DeInit(SPI1); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; 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_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_InitStructure.SPI_Mode=SPI_Mode_Master;SPI_Init(SPI1, &SPI_InitStructure);
/* Initialize the FIFO threshold */
SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);SPI_NSSPulseModeCmd(SPI2,DISABLE);
SPI_SSOutputCmd(SPI2,DISABLE);/* Enable the Rx buffer not empty interrupt */
SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);/* Configure the SPI interrupt priority */
NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = !ENABLE; NVIC_Init(&NVIC_InitStructure);/* Enable the SPI peripheral */
SPI_Cmd(SPI1, ENABLE);}void delay(uint32_t t)
{ for(;t!=0;t--) { asm('nop'); }}I can't see anything moving on the MOSI pin; only the SCK and NSS
Here is the code for the STM32F103 MCU
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include 'stm32f10x.h'
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;u16 t=0;
int main(void){ //GPIOA,GPIOB,GPIOC, USART1,SPI1 Periph clock enable RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_USART1|RCC_APB2Periph_SPI1, ENABLE); // USART2, I2C1, CAN1 Periph clock enable RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_I2C1|RCC_APB1Periph_CAN1|RCC_APB1Periph_TIM2,ENABLE);spi1_config();
while (1) { asm('nop'); }}
void spi1_config(void)//slave
{ //3. Configurare SPI1............................................................... //Configure SPI1 pins: SCK, MISO and MOSI ----------//Configure SCK, MOSI and NSS pins as floating inputs
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7|GPIO_Pin_4; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);// Configure MISO (slave out) pin as AF Push pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure);//SPI1 configuration ----------
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; 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_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure);//Disable SPI1 CRC calculation
SPI_CalculateCRC(SPI1, DISABLE);NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = !ENABLE; NVIC_Init(&NVIC_InitStructure);SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_RXNE,ENABLE);
//Enable SPI1
SPI_Cmd(SPI1, ENABLE);}void delay(uint32_t t)
{for(; t != 0; t--){};
}