AnsweredAssumed Answered

STM32F4 Discovery + FreeRTOS + TouchScreen

Question asked by zavodny.vilem on Feb 17, 2014
Latest reply on Feb 19, 2014 by zavodny.vilem

Hello,

can you help me with touching controller and STM32F4 Discovery kit? I use controller XPT2046 with SPI and I want use SPI2. I know, that SPI 2 is on pins PC2, PC3 and PB10. Is it true? I init this pins in touch_init function and the function not working still. I'm using with FreeRTOS. Is it problem?

 

Here is my code for init:


void touch_init(void
  SPI_InitTypeDef  SPI_InitStructure; 
  GPIO_InitTypeDef GPIO_InitStruct;   
  NVIC_InitTypeDef NVIC_InitStructure; 
  EXTI_InitTypeDef EXTI_InitStructure; 
   
  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOD, ENABLE); 
     
  GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF; 
  GPIO_InitStruct.GPIO_Speed=GPIO_Speed_25MHz; 
  GPIO_InitStruct.GPIO_OType=GPIO_OType_PP; 
  GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;   
   
   
  GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10; 
  GPIO_Init(GPIOB,&GPIO_InitStruct);                       
        
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_SPI2);      //SCLK 
   
  GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF; 
  GPIO_InitStruct.GPIO_Speed=GPIO_Speed_25MHz; 
  GPIO_InitStruct.GPIO_OType=GPIO_OType_PP; 
  GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;   
   
   
  GPIO_InitStruct.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3; 
  GPIO_Init(GPIOC,&GPIO_InitStruct); 
   
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource2, GPIO_AF_SPI2); //MISO 
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource3, GPIO_AF_SPI2); //MOSI 
     
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);   
       
  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_CPOL_Low SPI_CPOL_High 
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;  
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;   //SPI_NSS_Hard //SPI_NSS_Soft 
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //16 
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;  
  SPI_InitStructure.SPI_CRCPolynomial = 7;  
  SPI_Init(SPI2,&SPI_InitStructure);  
  SPI_Cmd(SPI2,ENABLE);  
  //CS 
  GPIO_InitStruct.GPIO_Mode=GPIO_Mode_OUT; 
  GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; 
  GPIO_InitStruct.GPIO_OType=GPIO_OType_PP; 
  GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;   
  GPIO_InitStruct.GPIO_Pin=GPIO_Pin_14; // 3 
  GPIO_Init(GPIOB,&GPIO_InitStruct);    // d       
  T_DCS();   
  //T_PEN 
  GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN; 
  GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; 
  GPIO_InitStruct.GPIO_OType=GPIO_OType_PP; 
  GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;   
  GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6; 
  GPIO_Init(GPIOD,&GPIO_InitStruct);      
   
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); 
   
   
  SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOD, EXTI_PinSource6);     
  EXTI_InitStructure.EXTI_Line = EXTI_Line6; 
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; 
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;   
  EXTI_InitStructure.EXTI_LineCmd = ENABLE; 
  EXTI_Init(&EXTI_InitStructure); 
     
  NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; 
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; 
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; 
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
  NVIC_Init(&NVIC_InitStructure);       
}

And then I write and read data by this function:

                                                         
unsigned char SPI_WriteByte(u8 num)   
  unsigned char Data = 0;
  while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
  SPI_I2S_SendData(SPI2,num);  
  while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET);
  Data = SPI_I2S_ReceiveData(SPI2);
 
  return Data;                    
}  

Can you help me please?

Outcomes