This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

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

Parents
  • 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

Reply
  • 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

Children
No data