cancel
Showing results for 
Search instead for 
Did you mean: 

cant get x or y values on XPT2046 over SPI on STM32F407VE

3blake7
Associate II

I just got https://stm32-base.org/boards/STM32F407VET6-STM32-F4VE-V2.0 which is my first foray into embeded. I also got https://www.waveshare.com/wiki/4inch_Resistive_Touch_LCD which is TFT with XPT2046 touch. I have the LCD setup with FSMC 16-bit but I can't get touch working.

static void MX_SPI2_Init(void)
{
 
  /* USER CODE BEGIN SPI2_Init 0 */
 
  /* USER CODE END SPI2_Init 0 */
 
  LL_SPI_InitTypeDef SPI_InitStruct = {0};
 
  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  /* Peripheral clock enable */
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_SPI2);
  
  LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
  /**SPI2 GPIO Configuration  
  PB13   ------> SPI2_SCK
  PB14   ------> SPI2_MISO
  PB15   ------> SPI2_MOSI 
  */
  GPIO_InitStruct.Pin = LL_GPIO_PIN_13|LL_GPIO_PIN_15;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_5;
  LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
  GPIO_InitStruct.Pin = LL_GPIO_PIN_14;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_5;
  LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
  /* USER CODE BEGIN SPI2_Init 1 */
 
  /* USER CODE END SPI2_Init 1 */
  /* SPI2 parameter configuration*/
  SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
  SPI_InitStruct.Mode = LL_SPI_MODE_MASTER;
  SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_8BIT;
  SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_LOW;
  SPI_InitStruct.ClockPhase = LL_SPI_PHASE_1EDGE;
  SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
  SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV256;
  SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
  SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
  SPI_InitStruct.CRCPoly = 10;
  LL_SPI_Init(SPI2, &SPI_InitStruct);
  LL_SPI_SetStandard(SPI2, LL_SPI_PROTOCOL_MOTOROLA);
  /* USER CODE BEGIN SPI2_Init 2 */
 
  /* USER CODE END SPI2_Init 2 */
 
}

SPI code

static void MX_GPIO_Init(void)
{
  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  /* GPIO Ports Clock Enable */
  LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOH);
  LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
  LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
  LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOE);
  LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOD);
  LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
 
  /**/
  LL_GPIO_SetOutputPin(LCD_RST_GPIO_Port, LCD_RST_Pin);
 
  /**/
  LL_GPIO_SetOutputPin(GPIOB, BL_Pin|TP_CS_Pin);
 
  /**/
  GPIO_InitStruct.Pin = LCD_RST_Pin;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_MEDIUM;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  LL_GPIO_Init(LCD_RST_GPIO_Port, &GPIO_InitStruct);
 
  /**/
  GPIO_InitStruct.Pin = TP_IRQ_Pin;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  LL_GPIO_Init(TP_IRQ_GPIO_Port, &GPIO_InitStruct);
 
  /**/
  GPIO_InitStruct.Pin = BL_Pin;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_MEDIUM;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  LL_GPIO_Init(BL_GPIO_Port, &GPIO_InitStruct);
 
  /**/
  GPIO_InitStruct.Pin = TP_CS_Pin;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  LL_GPIO_Init(TP_CS_GPIO_Port, &GPIO_InitStruct);
 
}

GPIO code

#define Y 0x90 //9C //94 //90
#define X 0xD0 //DC //D4 //D0
 
#define CS_TOUCH_SELECT()    LL_GPIO_ResetOutputPin(TP_CS_GPIO_Port, TP_CS_Pin)
#define CS_TOUCH_DESELECT()  LL_GPIO_SetOutputPin(TP_CS_GPIO_Port, TP_CS_Pin)
 
uint32_t XPT2046_irq_read(void)
{
    return  LL_GPIO_IsInputPinSet(TP_IRQ_GPIO_Port, TP_IRQ_Pin);
}
 
static uint8_t SendReceive_Data8(SPI_TypeDef *SPIx, uint8_t data)
{
    while(!LL_SPI_IsActiveFlag_TXE(SPIx)) {}
    LL_SPI_TransmitData8 (SPIx, data);
    while(!LL_SPI_IsActiveFlag_RXNE(SPIx)) {}
    //(void) SPIx->DR; //fake Rx read;
    while (LL_SPI_IsActiveFlag_BSY(SPIx));
    return LL_SPI_ReceiveData8(SPIx);
}
 
void XPT2046_init(void)
{
    CS_TOUCH_SELECT();
    SendReceive_Data8(SPI2, 0X80);
    SendReceive_Data8(SPI2, 0X00);
    SendReceive_Data8(SPI2, 0X00);
    LL_mDelay(5);
    CS_TOUCH_DESELECT();
}

XPT2046 library by https://github.com/ScarsFun/lvgl_STM32F103_ILI9341_XPT2046

when i touch screen it registers touch, so IRQ is working. in the SendReceive_Data8 function, it gets stuck on the first two while loops. if i comment them out and add a delay, it just returns 0. maybe i setup SPI wrong or its not initializing? all the examples of XPT2046 i found seem to do the same init so i guess its my SPI config

id appreciate any help! thanks

4 REPLIES 4

If you can't get SPI to work, start with that, disconnect the attached device, loop back MOSI to MISO and try transferring - you should see echo of what you transfer.

Than attach your device.

If still no joy, read out and check the elevant GPIO and SPI registers. Observe waveforms on the pins using oscilloscope and/or logic analyzer.

JW

3blake7
Associate II

i dont have an oscilloscope yet, i guess its a must have, since you dont really get any errors or anything to work with. thanks for the miso mosi loopback idea, ill try that

You can also try to bitbang SPI.

JW

3blake7
Associate II

okay so i got an oscilloscope, had to solder it all together but it was only $20.

if i connect stm32 touch chip select pin (GPIO Output Pullup) to oscilloscope then to ground, it puts up a square wave when chip select is run. that seems to be working. when i hook it up to the chip select pin on the xpt2046, the wave is really noisy and isnt one square, its broken up, like interference pattern, i thought it wasnt working but the amplitude does increase a little bit when chip select goes high. i used volt meter to check the xpt2046 chip select pin to ground, its reading 1-1.5v. is it defective?