Hi, I have a chip that sends a string with this format
obrazki.elektroda.pl/9967733000_1484077005.png
it is my program
#include <stdio.h> #include <string.h> #include "stm32f10x_gpio.h" #include "stm32f10x_rcc.h" #include "stm32f10x_usart.h" #include "stm32f10x_spi.h" #define SPI_FLASH SPI3 #define SPI_CLK RCC_APB1Periph_SPI3 #define SPI_GPIO GPIOC #define SPI_GPIO_CLK RCC_APB2Periph_GPIOC #define SPI_PIN_SCK GPIO_Pin_10 #define SPI_PIN_MISO GPIO_Pin_11 #define SPI_PIN_MOSI GPIO_Pin_12 void SPIConfig(void){ SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable SPI and GPIO clocks */ RCC_APB2PeriphClockCmd( SPI_GPIO_CLK , ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3,ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE); /* Configure SPI pins: SCK, MISO and MOSI */ GPIO_InitStructure.GPIO_Pin = SPI_PIN_SCK | SPI_PIN_MISO | SPI_PIN_MOSI; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SPI_GPIO, &GPIO_InitStructure); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI3, &SPI_InitStructure); /* Enable the SPI */ SPI_Cmd(SPI3, ENABLE); } void delay(){ int i,j; for(i=0;i<10000;i++) for(j=0;j<250;j++); } int main(){ unsgined char p[3] SPIConfig(); while(1){ delay();delay(); while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI3,0xffff); if(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == SET){ p[0]=SPI_I2S_ReceiveData(SPI3); SPI_I2S_SendData(SPI3,0xffff); if(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == SET){ p[1]=SPI_I2S_ReceiveData(SPI3); } //print to terminal p[1] and p[0] } }
But the P[0] and P[1] have the same value(it means in both reading the IC start a new connection and send two first bytes).(I read 2bytes each time by setting SPI_DataSize_16b)
obrazki.elektroda.pl/2165160300_1484077218.png How can read four bytes with(or without) error bit with SPI protocol? or other efficient way