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?
View all questions in Keil forum