Keil Logo

Proper way to bit bang with KEIL

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Details Message
Author
John Ruff
Posted
24-May-2018 03:20 GMT
Toolset
None
New! Proper way to bit bang with KEIL

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

Author
Westonsupermare Pier
Posted
24-May-2018 04:22 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

So 20ns (50MHz), going to be a tad challenging to pull that off with this methodology.

Consider being an SPI Slave, or using DCMI

Author
John Ruff
Posted
24-May-2018 04:27 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

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

Author
John Ruff
Posted
24-May-2018 05:19 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

So its not possible to bit bang like dat?

Author
Westonsupermare Pier
Posted
24-May-2018 05:54 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

>>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.

Author
John Ruff
Posted
24-May-2018 06:43 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

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.

Author
John Ruff
Posted
24-May-2018 10:49 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

Does SPI slave on AHB1 with 42mHz will be enough?

Author
Andrew Neil
Posted
24-May-2018 12:42 GMT
Toolset
None
New! RE: reverse engineering unknown device

Surely a (simple) logic analyser or oscilloscope would be a more appropriate tool here?

This is probably not fast enough for you:

http://hobbycomponents.com/testing/243-hobby-components-usb-8ch-24mhz-8-channel-logic-analyser

but that's the kind of idea ...

Author
John Ruff
Posted
24-May-2018 13:14 GMT
Toolset
None
New! RE: reverse engineering unknown device

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?

Author
John Ruff
Posted
28-May-2018 07:31 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

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: https://onedrive.live.com/?cid=E2F4E301BE6D4C7D&id=E2F4E301BE6D4C7D%21110&parId=root&o=OneUp.
Clock is yellow and green is data.
I need only 16bytes from it, when data line goes low, we can see this 16bytes:
https://onedrive.live.com/?cid=E2F4E301BE6D4C7D&id=E2F4E301BE6D4C7D%21109&parId=root&o=OneUp
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: https://onedrive.live.com/?cid=E2F4E301BE6D4C7D&id=E2F4E301BE6D4C7D%21111&parId=root&o=OneUp

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

Author
John Ruff
Posted
28-May-2018 07:37 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

Sorry for none working links on images. Correct:
1) https://1drv.ms/u/s!An1Mbb4B4_TibpzhLbqjdOJDr0s
2) https://1drv.ms/u/s!An1Mbb4B4_Tiba8II3AK6cRp8AM
3) https://1drv.ms/u/s!An1Mbb4B4_Tib3KqNa-PHo02kuo

Author
Hans-Bernhard Broeker
Posted
28-May-2018 12:00 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

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.

Author
John Ruff
Posted
28-May-2018 12:35 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

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.

Author
John Ruff
Posted
29-May-2018 10:50 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

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

Author
Ross Walder
Posted
2-Jun-2018 14:19 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

What I am trying to do is reverse engineering unknown device. What I need is to catch 16bytes of 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 is a powerful µC. The fact that I'm really new in С and Keil, and I don't know if I'm doing it in a proper way. For example to detect a GPIO goes down, im using this code:
http://www.glazingwindowsglasgow.co.uk

Author
Ross Walder
Posted
2-Jun-2018 14:21 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

Hi

I have been struggling to develop bit-banging in C to implement the UART at the receiver in my AT89C51 chip. My codes didn't work at the moment

If you have already UART software-driven code for receiver in C, please let me know asap. I need it for my wireless project.

Thank you - http://www.notarypubliclondon-mmk.co.uk

Maverick Max

Author
Ross Walder
Posted
2-Jun-2018 14:22 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

Hello again

As you know that I want to develop software UART (bit-banging) for my receiver. Since you could not give me any C code, I have been forced to develop my C code but I am not 100% sure that it would work. You are welcome to use it

I intend to use 4200 Baud therefore 1/baud which yields 416uS.

When the start bit occurs, the first delay will be generated bitwidth/2 (208uS). The Interrupt Service Routine activates which repeats at 8 times for every 416us in order to capture the data bits because i want to detect the data bit in the middle of data bit'speriod.

Finally the delay is generated at twice time (208us + 208us = 416us) to check if the stop bit is 1 then the data bits (buffer_G) is placed in the P1...

After reading various documents from internets, I was thinking of using external interrupt in my code but I am going to save it for later cuz I need to make this code right first.
https://www.abogadosdeaccidentesflorida.com

Any feedback?

Author
Ross Walder
Posted
2-Jun-2018 14:23 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

There seems to be one slight problem. When you detect the first bit, chances are that you will detect it a little time after the actual occuance of the start bit. It depends on your detection methood and software latency. Then if you start your algorithm of waiting half a bit for the center of the start bit, you will probably be somewhere near the end of the start bit, not in the middle.
http://www.abogadosdeaccidentesflorida.com

Author
P Pusted
Posted
2-Jun-2018 19:37 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

Is this thread for real?

Author
Hans-Bernhard Broeker
Posted
2-Jun-2018 22:11 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

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.

Author
rebecca hickshq
Posted
21-Aug-2018 01:06 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

Thank you for sharing your details and experience. I think it very good for me. http://bloonstowerdefense5game.com |=>bloons tower defense 5
http://supersmashflash-2.com super smash flash 2

Author
juanca lina
Posted
13-Sep-2018 09:11 GMT
Toolset
None
New! RE: Proper way to bit bang with KEIL

more links info:
http://forum.opticallimits.com/member.php?action=profile&uid=10297
http://www.webestools.com/profile-37330.html
http://anffhub.latrobe.edu.au/members/1711
http://www.gearbest.com/forum/topic-t1449.html
http://slavrada.gov.ua/forum/viewtopic.php?f=2&t=18784
http://www.ciazowy.pl/forum/viewtopic.php?f=73&t=18278
http://forums.seattletimes.com/forums/viewtopic.php?f=46&t=243025
http://community.fema.gov/a0bt0000000Z2Wc
http://forums.seattletimes.com/forums/viewtopic.php?f=53&t=243034
http://forums.seattletimes.com/forums/viewtopic.php?f=53&t=243035
http://forums.seattletimes.com/forums/viewtopic.php?f=73&t=243036
http://forums.seattletimes.com/forums/viewtopic.php?f=73&t=243037
http://www.thisisant.com/forum/viewthread/6283/
http://smotra.ru/users/jimbox/
http://relatosx.neowordpress.fr/milenita-mi-nena-preciosa/
http://onlinehairaffair.com/author/jimbox/
http://www.thenewsletterplugin.com/forums/users/juancax
http://admin.phacility.com/p/micuki/
http://www.wantedly.com/users/18430296
http://www.openlearning.com/u/juancax
http://www.marketingprofs.com/directory/profile.asp?id=2185618
http://www.quia.com/profiles/juancali
http://twubs.com/zoraya
http://www.insightsassociation.org/users/publiwebmaxtergmailcom
http://www.blogadda.com/jimbox/
http://www.nfomedia.com/profile?uid=rNjYgf
http://hanson.net/users/jimbox
http://www.searchquotes.com/Publiwebmaxter/answers/
http://www.signegeneve.ch/members/jimbox/profile/
http://forums.kerio.cz/m/17422/
http://tesera.ru/forum/showthread.php?tid=775
http://ccmixter.org/people/sushix/
http://ccmixter.org/people/zoraya/
http://wiseintro.co/jimbox
http://www.behej.com/topic/44134-la-boda
http://spyropress.com/forums/users/jimbox/
http://forum.dokuwiki.org/thread/14892
http://forum.dji.com/thread-108991-1-1.html
http://www.anoox.com/ask_answer/qanda.php?q_id=136532&ans=60378
http://www.silabs.com/community/wireless/wi-fi/forum.topic.html/dkwf121_not_populati-UVIL

Next Thread | Thread List | Previous Thread Start a Thread | Settings

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.