/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / Acquisition of 16-bit data at 20kHz through SPI Protocol

Username:     
Password:     
             

Forum
  • Index
  •  » STM32 Family
  •  » Acquisition of 16-bit data at 20kHz through SPI Protocol

# 1   2011-07-13 15:02:27 Acquisition of 16-bit data at 20kHz through SPI Protocol

Zouhair
New member
Registered: 2011-07-13
Posts: 6

Acquisition of 16-bit data at 20kHz through SPI Protocol

Hi everybody,
in the framework of an internship, I am supposed to link two electronic boards through SPI protocol..the development board I am proramming is STM32-P107 with the uc stm32f107, and I am getting the data from a 16-bit converter.
Since i've always worked with microchip microcontrollers, I am finding difficulties to program the interruption needed. Here is the code I've written so far, many thing lack I know, but I am counting on you guys to help me sort this out:

Code:

   #include "stm32f10x_spi.h"
  #include "stm32f10x_rcc.h"
  #include "stm32f10x_gpio.h"
  #include "stm32f10x_conf.h"
  #include "stm32f10x.h"
  #include "stm32f10x_tim.h"

GPIO_InitTypeDef GPIO_InitStructure;

/* SPI SPE mask */
#define CR1_SPE_Set          ((uint16_t)0x0040)
#define CR1_SPE_Reset        ((uint16_t)0xFFBF)

/* I2S I2SE mask */
#define I2SCFGR_I2SE_Set     ((uint16_t)0x0400)
#define I2SCFGR_I2SE_Reset   ((uint16_t)0xFBFF)

/* SPI CRCNext mask */
#define CR1_CRCNext_Set      ((uint16_t)0x1000)

/* SPI CRCEN mask */
#define CR1_CRCEN_Set        ((uint16_t)0x2000)
#define CR1_CRCEN_Reset      ((uint16_t)0xDFFF)

/* SPI SSOE mask */
#define CR2_SSOE_Set         ((uint16_t)0x0004)
#define CR2_SSOE_Reset       ((uint16_t)0xFFFB)

/* SPI registers Masks */
#define CR1_CLEAR_Mask       ((uint16_t)0x3040)
#define I2SCFGR_CLEAR_Mask   ((uint16_t)0xF040)

/* SPI or I2S mode selection masks */
#define SPI_Mode_Select      ((uint16_t)0xF7FF)
#define I2S_Mode_Select      ((uint16_t)0x0800)



/********************Protocol SPI*********************/

//SPI_I2S_ITConfig(SPI3,SPI_I2S_IT_TXE,ENABLE);

#pragma interrupt Receive_Data
void Receive_Data(void){

        

        if (SPI_I2S_GetITStatus(SPI3,SPI_I2S_IT_RXNE) == SET){
            while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET) SPI_I2S_ReceiveData(SPI3);//write in RX and clear flag
        }
        

        //if(ItTx == SET){
        //    if(SPI_I2S_FLAG_TXE == SET) SPI_I2S_SendData(SPI3,Data);// write in TX and clear flag
            //GPIO_Write(GPIOC,SPI_I2S_SendData(SPI3,Data) );
        //}

}



void Timer_Config(){
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct;
    TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);

      /* Set the default configuration */
      TIM_TimeBaseInitStruct->TIM_Period = UINT16_MAX;
      TIM_TimeBaseInitStruct->TIM_Prescaler = (SYS_CLK / DELAY_TIM_FREQUENCY) - 1;;
      TIM_TimeBaseInitStruct->TIM_ClockDivision = TIM_CKD_DIV1;
      TIM_TimeBaseInitStruct->TIM_CounterMode = TIM_CounterMode_Up;
      TIM_TimeBaseInitStruct->TIM_RepetitionCounter = 0x0000;

      TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);

    
     
}

/********************************************************************************************
 * ******************************************************************************************
 * **************************************MAIN PROGRAM****************************************
 * ******************************************************************************************
 * ******************************************************************************************
 */
int main(void){


    /* GPIOC Periph clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
    /*Configurer le SPI en mode Master*/


    /*Initialisation*/
    SPI_InitTypeDef* SPI_InitStruct = 0;
    SPI_I2S_DeInit(SPI3);
    SPI_StructInit(SPI_InitStruct);
    SPI_Init(SPI3,SPI_InitStruct);

    /*Définir le format des données (16-bit) à l'aide du bit DFF*/

    SPI_DataSizeConfig(SPI3,SPI_DataSize_16b);

/**
  * @brief  Autorise ou arrete le peripherique SPI spécifié.*/

    SPI_Cmd(SPI3,ENABLE);
/**
  * @brief  Configures internally by software the NSS pin for the selected SPI.*/

    SPI_NSSInternalSoftwareConfig(SPI3,SPI_NSSInternalSoft_Set);


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_10; //MOSI-SCK
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //MISO
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; //commande relais
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOE, &GPIO_InitStructure);

    GPIO_WriteBit(GPIOC, GPIO_Pin_10, Bit_SET);
    GPIO_WriteBit(GPIOC, GPIO_Pin_11, Bit_RESET);
    GPIO_WriteBit(GPIOC, GPIO_Pin_12, Bit_SET);

    SPI_I2S_ITConfig(SPI3,SPI_I2S_IT_RXNE,ENABLE);

    while(1){
         Receive_Data();
    }

    SPI_I2S_ITConfig(SPI3,SPI_I2S_IT_RXNE,DISABLE);

}

to be clear, what would be wrong with this code? what do I need to add ( many things I guess)? and what are the steps to follow to set the interruption which will sample tha data at 20kHz?
Thank you in advance,
Zouhair

Offline

 

# 2   2011-07-15 09:11:54 Acquisition of 16-bit data at 20kHz through SPI Protocol

Zouhair
New member
Registered: 2011-07-13
Posts: 6

Re: Acquisition of 16-bit data at 20kHz through SPI Protocol

Hi again,
guys, any suggestions how to set a timer interruption for 16-bit data received through SPI protocol? sad

Offline

 

# 3   2011-07-15 17:43:10 Acquisition of 16-bit data at 20kHz through SPI Protocol

ntrf.zns
Member
From: Belgorod, Russia
Registered: 2009-11-01
Posts: 134

Re: Acquisition of 16-bit data at 20kHz through SPI Protocol

I don't see any NVIC setup in your code. In Microchip and Atmel MCUs you only need to specify linker flag for a function. On stm32 interrupts are not fixed by certain vector in the program memory. You actualy have to configure NVIC (Nested vectored interrupt controller):
1) set vertor's priority
2) set vector's handler function
3) enable desired interrupt vector
I can't explain anything else right now. Maybe you'll figure it out.

Offline

 

# 4   2011-07-18 07:49:22 Acquisition of 16-bit data at 20kHz through SPI Protocol

Zouhair
New member
Registered: 2011-07-13
Posts: 6

Re: Acquisition of 16-bit data at 20kHz through SPI Protocol

Hi,
Thank you very much for your reply,
here is what I added to my main as NVIC configuration:

Code:

    /* Configure two bits for preemption priority */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

    /* Enable the TIM2 Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    while(1)
    {
    }

Is that right?
Zouhair

Offline

 

# 5   2011-07-18 14:36:00 Acquisition of 16-bit data at 20kHz through SPI Protocol

Zouhair
New member
Registered: 2011-07-13
Posts: 6

Re: Acquisition of 16-bit data at 20kHz through SPI Protocol

Another question,
I want to configure the interruption with the Timer 2 as you can see, so I put the channel to TIM2_IRQn, the question is, where do I need to mention SPI3_IRQn as vector of global interruption of the SPI I am dealing with in my program?
Thanks!

Offline

 

# 6   2011-07-19 09:00:19 Acquisition of 16-bit data at 20kHz through SPI Protocol

Zouhair
New member
Registered: 2011-07-13
Posts: 6

Re: Acquisition of 16-bit data at 20kHz through SPI Protocol

Anyone sad

Offline

 

  • Index
  •  » STM32 Family
  •  » Acquisition of 16-bit data at 20kHz through SPI Protocol

Board footer