2014-02-17 02:39 PM
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 PB 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?
#xpt2046 #spi #stm32f4
2014-02-18 01:34 AM
Hi
''I know, that SPI 2 is on pins PC2, PC3 and PB10. Is it true?'' Yes this is correct. However, you are missing SPI2_NSS on PB9 For ''// CS'' you have initialised PB Pin14 - this is incorrect - should be PB9 ''I init this pins in touch_init function and the function not working still.'' What do you mean by 'function not work still' ? Is the device not responding? Is the problem, you cannot read/write to the device? Please be explicit when you explain your problem. ''I'm using with FreeRTOS. Is it problem?'' Not if the SPI driver is written correctly (in a way that works with multitasking Operating Systems). It should be thread safe. It should be re-entrant. It should not be affected by IRQs.2014-02-18 03:16 AM
Thank you,
i'm using PB14, because PB9 I use for I2C. And I think that I can use PB14 like output pin only for select device. And my problem is, that device is work OK, but communication over SPI not working. Touch controller doesn't send data. I don't know, why? If I try this controller with ATMEL MEGA32 and similar function. It's OK. I don't know, where is probem. I try lot of combination. For example read only one value.2014-02-18 03:24 AM
''i'm using PB14, because PB9 I use for I2C. And I think that I can use PB14 like output pin only for select device. ''
So you are in manual control of CS (Chip Select). You (or the code) must control CS every time you want to access the SPI device. ''And my problem is, that device is work OK, but communication over SPI not working. Touch controller doesn't send data. I don't know, why? If I try this controller with ATMEL MEGA32 and similar function. It's OK. I don't know, where is probem.'' Look at the SPI communications - work out what is different between the 2. I would start by looking at the CS - yourSPI_WriteByte(
) does nothing with the CS. How is the device suppose to know you are addressing it if the CS does not goes low?
2014-02-18 03:29 AM
I set and reset CS in function HERE:
u16 x=0;
T_CS();
SpiDelay(10);
SPI_WriteByte(0x90);
SpiDelay(10);
x=SPI_WriteByte(0x0);
x<<=8;
x+=SPI_WriteByte(0x0);
T_DCS();
T_CS is set bit to low.
2014-02-18 03:48 AM
>''And my problem is, that device is work OK, but communication over SPInot working. >Touch controller doesn't send data. I don't know, why? If Itry this controller with >ATMEL MEGA32 and similar function. It's OK. Idon't know, where is probem.''
>Look at the SPI communications - work out what is different between the 2. Do you have an oscilloscope or logic analyser? You need to look at what is going on the 4 lines. Is the clock going? Is data coming out of MOSI Is data coming out of MISO I am note sure because I have not used the SPI peripheral in the STM32 series but does the STM32 SPI peripheral need to be in a special mode if it is not in control of the SS line?2014-02-18 08:23 AM
Now I borrow logic analyser and everything OK only SCK not generating. And MISO and MOSI are same signal. I think, that only from uC.
A don't know, where is problem.2014-02-18 08:44 AM
Hi
Are you calling SPI_Cmd() anywhere? ''/** * @brief Enables or disables the specified SPI peripheral. * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral. * @param NewState: new state of the SPIx peripheral. * This parameter can be: ENABLE or DISABLE. * @retval None */''2014-02-18 08:49 AM
No. Only if I init ports.
2014-02-18 09:20 AM
Now I try init SPI after all functions. Now CLK is OK. But MOSI is not send to uC.