2022-03-28 10:09 PM
2022-03-29 06:24 AM
There are SPI examples you can follow in the CubeMX repository for your chip.
Including more detail in your post will lead to more helpful replies. If code is stuck, debug it, hit pause, determine why, and fix the issue.
2022-03-29 09:45 PM
//SPI CONFIGURATION
void TSC2046Config(void)//16.3.2022
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStruct; //11.2.2020
SPI_InitTypeDef SPI_InitStruct;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure; //13.1.2020
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;//16.3.2022 PENIRQ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource8);
EXTI_InitStruct.EXTI_Line = EXTI_Line8;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStruct);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;// SPI2 SCK ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource3, GPIO_AF_SPI2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//SPI2MOSI;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//GPIO_PuPd_NOPULL;
GPIO_Init(GPIOI, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOI, GPIO_PinSource3, GPIO_AF_SPI2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;// SPI2 MISO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource2, GPIO_AF_SPI2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //SPI2 Slave select
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
//GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOH, &GPIO_InitStructure);
GPIO_SetBits(GPIOH, GPIO_Pin_5);
// enable peripheral clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // transmit in master mode, NSS pin has to be always high
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; // clock is low when idle
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;//SPI_CPHA_2Edge; // data sampled at first edge
SPI_InitStruct.SPI_NSS = SPI_NSS_Hard;//SPI_NSS_Soft | SPI_NSSInternalSoft_Set; // set the NSS management to internal and pull internal NSS high
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
// SPI_InitStruct.SPI_CRCPolynomial = 7; //19.12.2019
SPI_DataSizeConfig(SPI2,SPI_DataSize_8b);//20.12.2019
SPI_Init(SPI2, &SPI_InitStruct);
SPI_Cmd(SPI2, ENABLE); // enable SPI3//19.12.2019
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //16.3.2022
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
uint16_t TSC2046_SendCommand(uint8_t cmd)
{
uint8_t spiBuf[3] = {0,0,0};
uint16_t return16=0;
int i=0;
_TS_CS_ENBALE;
spiBuf[0] = cmd;
while(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE));
SPI_I2S_SendData(SPI2,spiBuf[0]);
for(i=0 ;i<2;i++)
{
// while(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE));
spiBuf[i+1] = SPI_I2S_ReceiveData(SPI2);
}
//return16 = (spiBuf[1]<<4) + (spiBuf[2]>>4);
return16 = (spiBuf[1]<<4);
_TS_CS_DISABLE;
return return16;
}
uint16_t TSC2046_getRaw_Z(void)
{
return TSC2046_SendCommand(CMD_Z_AXIS | CMD_Default);
}
uint16_t TSC2046_getRaw_X(void)
{
return TSC2046_SendCommand(CMD_X_AXIS | CMD_Default);
}
//ii. get y-axis raw touch 12-bit value
uint16_t TSC2046_getRaw_Y(void)
{
return TSC2046_SendCommand(CMD_Y_AXIS | CMD_Default);
}
void main()
{
if(TouchDetected)
{
TouchDetected =0;
Touchz = TSC2046_getRaw_Z(); //MOSI line is low but after transmitting data its high
Touchx = TSC2046_getRaw_X(); //code stuck here bcz signal is continuously busy
Touchy = TSC2046_getRaw_Y();
}
}
2022-03-29 09:50 PM
in above image after transmitting data, DIN pin is low but in my case idle condition is low but after data transmission its show high
2022-03-29 10:07 PM
2022-04-11 11:41 PM
sir i am using HAL library its successfully work but when i trying without HAL library its not work. i was already share you the details , MOSI pin waveform & code flow.