Hello, what im trying to do is reverse engineering unknown device. What i need is catch a 16bytes data from it. Each clk's pulse speed is about 20 nanoseconds. I'm using stm32f4 discovery board, so i should be able to catch it as stm32 powerful uC. The fact that im really new in Keil, and i dont know if im doing it a propper way, for example to detect a GPIO goes down, im using this code
while (HAL_GPIO_ReadPin (GPIOC,GPIO_PIN_7) == 0){}
Is it proper way to do it? THe whole code with comments are:
while (1) { uint8_t collect_bits = 0; char string_array[21] = {0}; // HAL_Delay(200); while (HAL_GPIO_ReadPin (GPIOC,GPIO_PIN_9) == 0){} // wait until GPIOC 9 goes high while (HAL_GPIO_ReadPin (GPIOC,GPIO_PIN_9) == 1){} // wait until GPIOC 9 goes low for (int i = 0; i < 8; i++) // make a loop for reading byte { while (HAL_GPIO_ReadPin (GPIOC,GPIO_PIN_7) == 0){} // wait until clock goes high int32_t current_bit = HAL_GPIO_ReadPin (GPIOC,GPIO_PIN_9); // READ bit on PINC 9 collect_bits |= current_bit << i; // Shift current_bit to position i and // put it into collect_bits using bit wise OR while (HAL_GPIO_ReadPin (GPIOC,GPIO_PIN_7) == 1){} // wait until clock goes low, then repeat while loop untill we collect 8 bits } sprintf(string_array, "%X", collect_bits); // just simple convertation for PC output CDC_Transmit_FS((uint8_t*)string_array, sizeof(string_array)); // print result. // //break; /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ }
The problem is that output is not correct.What am i doing wrong? thanks
So 20ns (50MHz), going to be a tad challenging to pull that off with this methodology.
Consider being an SPI Slave, or using DCMI
Thank you Pier for reply. unfortunately i cant control this unkown device, therefore i cant get smt32 work with it through SPI. Honestly i dont get why it doesnt work, the stm32 has 168mhz crystal it should be enough to catch nanoseconds, no? Any suggestions please
So its not possible to bit bang like dat?
>>Honestly i dont get why it doesnt work..
Stop thinking like a SW engineer.
You're writing in C, looping and reading data across a slower bus. Suggest you look at a disassembly of the code you've generated, and start counting cycles.
At 168 MHz you've got 3.39 cycles, and you're reading from the AHB, testing bits, looping.
You have data and a clock, what more does an SPI slave need?
Perhaps you can stage the bit stream in your own shift register, and then read that? Now your time constraints are 1/8th or 1/16th of what they were before.
Maybe you are right, my code slows down AHB's speed. What about spi slave? As i undderstand SPI mode should be "receive only slave" with CLK and MOSI connected to the stm32 pins.
Honestly i didnt completly get, what did you mean here: >>Perhaps you can stage the bit stream in your own shift register, and then read that? Now your time constraints are 1/8th or 1/16th of what they were before.
Or say more precisely, i got what you mean, but i have no idea how to do this. How to stage bit stream to the shift register? Should i do it with CMSIS? Sorry for the newbie questions, And im appreciate a lot your help.
Does SPI slave on AHB1 with 42mHz will be enough?
Surely a (simple) logic analyser or oscilloscope would be a more appropriate tool here?
This is probably not fast enough for you:
hobbycomponents.com/.../243-hobby-components-usb-8ch-24mhz-8-channel-logic-analyser
but that's the kind of idea ...
I have a very expensive oscilloscope, it can see nanosecond etc. I dont need to read data with oscilloscope or logic analazyer. Im making device that will read the data from that unknown stuff and print it on PC software. Btw what about 84mHz SPI? for 50 nanosecond clock, is it fast enough?
As @Westonsupermare Pier considered, im trying to use SPI as a slave. And im struggling with it. I have this data flow which i want to catch: onedrive.live.com/. Clock is yellow and green is data. I need only 16bytes from it, when data line goes low, we can see this 16bytes: onedrive.live.com/ And there is the speed of the clock, as you can see its around 1.5us each clock pulse. So what i have to do is configure spi with HAL, i toolk SPI1(PA5 - CLOCK, PA7- DATA) cuz it has more powerful MHZ, chose spi mode "RECEIVE ONLY SLAVE", HARDWARE NSS SIGNAL: DISABLED. And here is my spi1 config: onedrive.live.com/
static void MX_SPI1_Init(void) { /* SPI1 parameter configuration*/ hspi1.Instance = SPI1; hspi1.Init.Mode = SPI_MODE_SLAVE; hspi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY; hspi1.Init.DataSize = SPI_DATASIZE_8BIT; hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH; hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; hspi1.Init.NSS = SPI_NSS_SOFT; hspi1.Init.FirstBit = SPI_FIRSTBIT_LSB; hspi1.Init.TIMode = SPI_TIMODE_DISABLE; hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; hspi1.Init.CRCPolynomial = 10; if (HAL_SPI_Init(&hspi1) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } }
What im doing in main.c, as oscilo's pictures says we need to catch the moment when data goes low and then try to read a data, im trying to catch first pulse then wait until data goes high then low and only they read spi, at least only first byte - it should be 0.
int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); MX_USB_DEVICE_Init(); MX_SPI1_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ uint8_t collect_bits = 0; char string_array[21] = {0}; uint8_t spi_buffer[16] ={0} ; spi_buffer[0] = 5; while (HAL_GPIO_ReadPin (GPIOA,GPIO_PIN_7) == 0){} // wait until GPIOC 9 goes high while (HAL_GPIO_ReadPin (GPIOA,GPIO_PIN_7) == 1){} // wait until GPIOC 9 goes high while (HAL_GPIO_ReadPin (GPIOA,GPIO_PIN_7) == 0){} // wait until GPIOC 9 goes high while (HAL_GPIO_ReadPin (GPIOA,GPIO_PIN_7) == 1){} // wait until GPIOC 9 goes high HAL_SPI_Receive(&hspi1, (uint8_t*)spi_buffer, 16,0); } /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ // } /* USER CODE END 3 */ }
And i catch nothing at all, my spi buffer all 16 bytes are 0. I tried SPI_DMA it chacnged only first byte on 0XFF. What am i doing wrong? Thanks
Sorry for none working links on images. Correct: 1) 1drv.ms/.../s!An1Mbb4B4_TibpzhLbqjdOJDr0s 2) 1drv.ms/.../s!An1Mbb4B4_Tiba8II3AK6cRp8AM 3) 1drv.ms/.../s!An1Mbb4B4_Tib3KqNa-PHo02kuo
The OneDrive documents are not reachable.
as you can see its around 1.5us each clock pulse Hmmm... originally you claimed they were 20 ns, now they're 1500 ns. Where did that slow-down by a factor of 75 suddenly come from?
Maybe you should consider just getting (or renting) a better scope. Useful mixed-signal oscilloscopes these days have little to no problem decoding SPI signals all by themselves.
as oscilo's pictures says we need to catch the moment when data goes low
Even not having had a chance to look at those oscilloscop pictures, I'll say that's unlikely to be the correct approach. The clock line in an SPI link is called that because that's the one which determines at whicht moment you have to catch what. It's clock that tells you how many '1' bits there actually are in a given stretch of logic high, and in the absence of a CS line, it's clock that has to tell you where the telegram begins.
Thanks for reply Hans-Bernhard Broeker, Strange, OneDrive works for me. I uploaded to imgur, i hope this ok :) https://imgur.com/a/prxkKoC https://imgur.com/a/Eko6Svc ( SEE THIS IMAGE MY OSCILO SHOWS DATA IT BEGINS FROM FE 00 10 etc (SPI PROTOCOL)) https://imgur.com/a/95AkqGl ( The begining of package, from here i want to read) I took a slower device which has 1.5 microseocnds pulses in purpose to debugg with slower clock speed. And yes the first one was around 50 nanoseconds ( not 20, my mistake). My oscilloscope catching this data as SPI( it catches data with clock and decode it to hex value, as you can see in images, the data is correct). As i understand if cs is low( i pulled it down as hardware not soft), the slave will receive messages, yes maybe it will not be correct cuz of wrong timings but it will receive something, and now it doesnt receive at all, my buffer dont change. Even if just loop HAL_SPI RECEIVE function it will not read anything.
i managed to synchronize slave and master with HAL function:
HAL_SPI_TransmitReceive(&hspi1, (uint8_t*)spi_buffer, (uint8_t *)spi_buffer, 16, 2000);
Im sending from slave the bytes which i get from master, in purpose of debugging, and the data is same, so its perfectly synchronized and seems like slave reading all data master sends. Here is the images:
1) Slave is my STM32f4 and master that i catching, its the same https://imgur.com/wa6rF5J 2) https://imgur.com/a/ZVCPaNT - zoomed up The problem is that i dont know how to read that data, its 16bytes which i need somehow to read without slowing the spi down. All my attempts to do that are failed. The whole code are:
int main(void) { /* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */ SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); MX_USB_DEVICE_Init(); MX_SPI1_Init(); /* USER CODE BEGIN 2 */ //while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_7) != RESET){} // while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_7) != SET){} // while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_7) != RESET){}
/* USER CODE END 2 */
/* Infinite loop */ /* USER CODE BEGIN WHILE */ uint8_t collect_bits[16] = {0}; char string_array[21] = {0}; uint8_t spi_buffer[16] ={0} ; uint8_t flag_when_start_print = 0; spi_buffer[0] = 5; uint8_t spi_transmit[16] = {0};
HAL_SPI_Init(&hspi1);
while (1) { HAL_SPI_TransmitReceive(&hspi1, (uint8_t*)spi_buffer, (uint8_t *)spi_buffer, 16, 2000); } }
The important fact that the beggining of the messge i want to catch is always starts with 0xFE0010, so after those bytes i need to read next 13bytes
Is this thread for real?
It was until some random a-hole drowned it in click-spam. This happens entirely too often on this forum, most likely because it totally lacks user identification.