/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / Newbie: Primer2 - problem with SPI2

Username:     
Password:     
             

Forum

# 1   2010-11-21 18:04:29 Newbie: Primer2 - problem with SPI2

gucio
New member
Registered: 2010-10-06
Posts: 1

Newbie: Primer2 - problem with SPI2

Hi!

I am trying to configure SPI2 in my primer2 to connect with TLX905 radio module, but without any results;/  I am using GPIO_Pin_4 for SPI_NSS.

The problem is that, my program is stopping into this loop:

Code:

 while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);

I don't know what I configured wrong. I checked TLX905 with AVR and there everything works fine:
I'm sending byte (0x15) to TLX905, and I'm getting 5 bytes of data.

I have read, that I should disable microphone when I want to use SPI2, but I dont know how to do that.


Please, help me sad

Here is my code:

Code:

#include "stm32f10x_lib.h"
#define NEEDEDVERSION "V 3.7"

static enum MENU_code MsgVersion(void);

const char Application_Name[8+1] = {"myApp"};      
GPIO_InitTypeDef  GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;

#define PWR_UP GPIO_Pin_0
#define TX_EN GPIO_Pin_6
#define TRX_CE GPIO_Pin_7

#define SPI_MISO GPIO_Pin_14
#define SPI_MOSI GPIO_Pin_15
#define SPI_SCK GPIO_Pin_13
#define SPI_NSS GPIO_Pin_4

static void sleep(int i)
{
int j,l;

for (j=0;j<255;j++)
   for (l=i;l>0;l--);
}
void NVIC_Configuration(void)
{
#ifdef  VECT_TAB_RAM
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else  /* VECT_TAB_FLASH  */
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}

void RCC_Configuration(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
}

static void GPIO_init()
{
//MISO, MOSI, SCK
    GPIO_InitStructure.GPIO_Pin = SPI_SCK | SPI_MISO | SPI_MOSI;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

// TX_EN, TRX_EN, PWR_UP
    GPIO_InitStructure.GPIO_Pin = PWR_UP | TX_EN | TRX_CE;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

// SPI_NSS
    GPIO_InitStructure.GPIO_Pin = SPI_NSS;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    GPIO_WriteBit(GPIOC,SPI_NSS , Bit_SET);
}
static void SPI_init()
{
    SPI_StructInit(&SPI_InitStructure);
    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_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; // min 8 at 72Mhz (step 5)
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;

    SPI_Init(SPI2, &SPI_InitStructure);
    SPI_Cmd(SPI2, ENABLE);
}
static  void setStandby()
{
    GPIO_WriteBit(GPIOB, PWR_UP, Bit_SET);
    GPIO_WriteBit(GPIOB, TX_EN, Bit_SET);
    GPIO_WriteBit(GPIOB, TRX_CE, Bit_RESET);
    sleep(2000);
}
static void sendData()
{
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
    SPI_I2S_SendData(SPI2,0x15);
}
static void receiveData()
{
    int i=0;
    char buffer[20];
    u8 dane=0xFF;
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
    for(i=0; i<5;i++) {    
        dane = SPI_I2S_ReceiveData( SPI2 );
        }
}
enum MENU_code Application_Ini(void)
    {
    // Ensure that the current OS version is recent enough
    if(strcmp(UTIL_GetVersion(), NEEDEDVERSION) < 0)
        {
        return MsgVersion();
        }
    
    RCC_Configuration();
    NVIC_Configuration();
    GPIO_init();
    SPI_init();
    setStandby();
    return MENU_CONTINUE_COMMAND;
    }

enum MENU_code Application_Handler(void)
    {
    
    GPIO_WriteBit(GPIOC,SPI_NSS , Bit_RESET);
    sendData();
    receiveData();
    GPIO_WriteBit(GPIOC,SPI_NSS , Bit_SET);

#if 1
    // If the button is pressed, the application is exited
    if(BUTTON_GetState() == BUTTON_PUSHED)
        {
        BUTTON_WaitForRelease();
        return MENU_Quit();
        }
#endif

    return MENU_CONTINUE;   // Returning MENU_LEAVE will quit to CircleOS
    }

Offline

 

# 2   2010-11-22 08:32:48 Newbie: Primer2 - problem with SPI2

yrt
Administrator
From: Grenoble-France
Registered: 2008-06-11
Posts: 520
Website

Re: Newbie: Primer2 - problem with SPI2

Hi,

I think the mistake is SPI2_NSS that is connected to PB12 and not PC4.
Secondly, with SPI bus, you must send dummy data during receiving, in order to maintain the SPI clock running. In your code, the clock will stop after the send of the 0x15 value.

You should use this type of function :

Code:

u8 SendByte( u8 byte )
{
    /* Loop while DR register in not empty */
    while ( SPI_I2S_GetFlagStatus( SPIX, SPI_I2S_FLAG_TXE ) == RESET );

    /* Send byte through the SPIx peripheral */
    SPI_I2S_SendData( SPIX, byte );

    /* Wait to receive a byte */
    while ( SPI_I2S_GetFlagStatus( SPIX, SPI_I2S_FLAG_RXNE ) == RESET );

    /* Return the byte read from the SPI bus */
    return SPI_I2S_ReceiveData( SPIX );
}

I also suggest that you call first the "SPI_I2S_DeInit" function in order to stop the I2S2 used by the CircleOS audio handler.

At last, I don't understand the usefulness of the "NVIC_Configuration" function, because the vector table is already configured during the init of the CircleOS.

Yves

Last edited by yrt (2010-11-22 09:38:08)

Offline

 

Board footer