Associate III
Jul 6, 2015 12:37 PM UTC
Question
spi problem
- 5 replies
- 1489 views
Posted on July 06, 2015 at 14:37
i am using stm32f030c8t6 device .i communicate with external memory ic on spi bus.i am using stmcube32 and mdk-5.
for testing purpose i read its id using spi.i am using spi2. at first am not able to generate clock for that i spent nearly whole day and last found that hal init function dose not initialize cr1 resister so i manual add this(hspi2.Instance->CR1|=0x00000040;).now able to get clock.here is my functionuint8_t GetSrrialSPINVRAM(SPI_HandleTypeDef *deviceFd,uint8_t *rbuff){ HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_RESET); uint32_t result; uint8_t spiBuff[2]; uint16_t junkbuff[10]; spiBuff[0]=0x9F; //HAL_SPI_Receive_IT(deviceFd,rbuff,5); result=HAL_SPI_Transmit(deviceFd,spiBuff,1,15); if(result!=HAL_OK) return 0; //HAL_SPI_FlushRxFifo(deviceFd); //HAL_SPI_FlushRxFifo(deviceFd); //osDelay(1); //check=deviceFd->Instance->DR; //HAL_SPI_Receive(deviceFd,junkbuff,1,5); //HAL_SPI_Receive(deviceFd,rbuff,1,5); //HAL_SPI_Receive_IT(deviceFd,rbuff,4); //while(deviceFd->State !=HAL_SPI_STATE_READY); //while((deviceFd->Instance->SR & SPI_FLAG_FRLVL)!= SPI_FRLVL_EMPTY) //{ // junkbuff[0]=deviceFd->Instance->DR; //} //junkbuff[1]=deviceFd->Instance->DR; //junkbuff[2]=deviceFd->Instance->DR; result=HAL_SPI_Receive(deviceFd,rbuff,4,15); check=result; HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_SET); if(result!=HAL_OK) return 0; return 1;}i am expecting reply 0x0681c8a0 but i am getting 0x000681c8 and when read 5 byte got 0x000681c8a0 .so receiving one extra byte at beginning. after searching some time on net i found that its due to dummy receive while transmitting.so i try to flush rxfifo after tx.but when i try flush instruction random data received every time so please guide me whats wronghere is command information i use pb12 as gpio out put which is connected to ss of external ic and data size is 8 bit .here is init fun hspi2.Instance = SPI2; hspi2.Init.Mode = SPI_MODE_MASTER; hspi2.Init.Direction = SPI_DIRECTION_2LINES; hspi2.Init.DataSize = SPI_DATASIZE_8BIT; hspi2.Init.CLKPolarity = SPI_POLARITY_LOW; hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; hspi2.Init.NSS = SPI_NSS_SOFT; hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi2.Init.TIMode = SPI_TIMODE_DISABLED; hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; hspi2.Init.CRCPolynomial = 10; hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLED; HAL_SPI_Init(&hspi2); hspi2.Instance->CR1|=0x00000040;and source freq is 48mz