AnsweredAssumed Answered

SPI buffer zero reading : 1 line rx only in master mode

Question asked by chevallier.william on Apr 30, 2014
Latest reply on Nov 3, 2014 by chevallier.william
Hello all,
I am strugling with an spi communication between my "stm32f3discovey " and a resolver chip AS5311.

Took me some time but I managed to make the clock and the communication working has I can see on my scope. Only problem is despite good  transmission, when i read the buffer, it return only zeros.

Here is my program :

void init_spi_spi2(void)
{
     GPIO_InitTypeDef GPIO_InitStructure;
     NVIC_InitTypeDef NVIC_InitStructure;
     SPI_InitTypeDef SPI_InitStructure;

     /* Enable SCK, MOSI, MISO and NSS GPIO clocks */
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

     /* Enable the SPI periph */
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

     /* GPIO SPI port init structure */
     /* SPI NSS pin configuration */
     GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_5);
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
     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(GPIOB, &GPIO_InitStructure);
     /* SPI SCK pin configuration */
     GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_5);
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
     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(GPIOB, &GPIO_InitStructure);
     /* SPI MISO pin configuration */
     GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_5);
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//GPIO_Mode_AF
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;//GPIO_OType_PP
     GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;//GPIO_PuPd_UP
     GPIO_Init(GPIOB, &GPIO_InitStructure);
     /* SPI  MOSI pin configuration */
//     GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_6);
//     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
//     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(GPIOB, &GPIO_InitStructure);

     /* SPI configuration */
     SPI_I2S_DeInit(SPI2);
     SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx;
     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_1Edge;
     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 = 1;
     SPI_Init(SPI2, &SPI_InitStructure);

     SPI_RxFIFOThresholdConfig(SPI2, SPI_RxFIFOThreshold_QF);

     SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);

     /* Configure the SPI interrupt priority */
     NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
     NVIC_Init(&NVIC_InitStructure);
}

void begin_spi_transmission(void)
{
     /* Enable SPI com for data retrieval */
     dataCounter = 0;
     SPI_NSSInternalSoftwareConfig(SPI2, SPI_NSSInternalSoft_Set);
     SPI_SSOutputCmd(SPI2, ENABLE);
     SPI_Cmd(SPI2, ENABLE);
}

void SPI2_IRQHandler(void)
{
     static uint8_t temp = 0;

     /* SPI in Master Receiver mode */
          temp = SPI_ReceiveData8(SPI2);
          //temp = SPI_I2S_ReceiveData16(SPI2);
          //temp = SPI2->DR;
          printf("%u\n", temp);
     /* The disable control must occur just between the sampling time of the first */
     /* bit of the last received frame and first bit of next (unwanted dummy) data frame. */
     if (dataCounter == (spiMessage2->numberOfdata-1))
          SPI_Cmd(SPI2, DISABLE);
     /* there is no more data to receive */
     if (dataCounter >= spiMessage2->numberOfdata) {
          STM_EVAL_LEDOff(LED7);
          /* we need to shut down the SPI com */
          SPI_NSSInternalSoftwareConfig(SPI2, SPI_NSSInternalSoft_Reset);
          SPI_SSOutputCmd(SPI2, DISABLE);
          //SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, DISABLE);
          if (spiMessage2->spiSuccessCallback != NULL)
               (*spiMessage2->spiSuccessCallback)();
          del_voidCircularBuffer_bufferHead(spiBuffer);
     }

}


Thanks for your time.


tags :
SPI_Direction_1Line_Rx
rxonly
interrupt
SPI_Mode_Master
zero

Outcomes