0
\$\begingroup\$

I have the following timer interrupt configuration

static void MX_TIM6_Init(void)
{

  /* USER CODE BEGIN TIM6_Init 0 */

  /* USER CODE END TIM6_Init 0 */

  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM6_Init 1 */

  /* USER CODE END TIM6_Init 1 */
  htim6.Instance = TIM6;
  htim6.Init.Prescaler = 1080;
  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim6.Init.Period = 10000;
  htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
  /* USER CODE BEGIN TIM6_Init 2 */

  /* USER CODE END TIM6_Init 2 */
}

I need to process a web server at the background of the application and a web client in the foreground of the application.

So I created a timer interrupt, but the problem that the calls to the wifi functions gets blocked.

I have tried to set the NVIC to highest priority and the timer to lowest one, but still it stucks there.

  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

  HAL_NVIC_SetPriority(TIM6_IRQn, 15,15); //Enable the peripheral IRQ

Here is the timer interrupt function:

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  // This callback is automatically called by the HAL on the UEV event
  if (htim->Instance == TIM6)
  {
    processServer = true;
    if (WIFI_StartServer(0, WIFI_TCP_PROTOCOL, "TCP_SERVER", 80) != WIFI_STATUS_OK)
      return;

    HAL_Delay(10);

    WIFI_ReceiveData(0, resp, 1200, &respLen, WIFI_READ_TIMEOUT);

    if (respLen > 0)
    {
      char string0[20] = {0};
      float kwh = 1.1;
      if (strstr((char *)resp, "GET")) /* GET: put web page */
      {

        //  modbus_read_kwh(string0, &kwh);
        if (SendWebPage(string0) != WIFI_STATUS_OK)
        {
        }
      }
      if (SendWebPage(string0) != WIFI_STATUS_OK)
      {
      }
    }

    WIFI_StopServer(0);

    processServer = false;
  }
}

Here is the main loop after suggestion

  while (1)
  {

    uint8_t ip[4] = {8, 8, 8, 8};
    if (processServer)
    {
      if (WIFI_StartServer(0, WIFI_TCP_PROTOCOL, "TCP_SERVER", 80) != WIFI_STATUS_OK)
        return;
      WIFI_ReceiveData(0, resp, 1200, &respLen, WIFI_READ_TIMEOUT);

      if (respLen > 0)
      {
        char string0[20] = {0};
        float kwh = 1.1;
        if (strstr((char *)resp, "GET")) /* GET: put web page */
        {

          //  modbus_read_kwh(string0, &kwh);
          if (SendWebPage(string0) != WIFI_STATUS_OK)
          {
          }
        }
        if (SendWebPage(string0) != WIFI_STATUS_OK)
        {
        }
      }

      WIFI_StopServer(0);

      processServer = false;
    }
    else
    {
      WIFI_DNS_Lookup("ipdb-eu2.riddleandcode.com", ip);
      WIFI_OpenClientConnection(0, WIFI_TCP_PROTOCOL, "TCP_CLIENT", ip, 80, 0);
      uint32_t amnt_int = 0;
      //int r = get_balance_amount( end_pos + 11, &amnt_int);
      int r = get_balance_amount("CJL6QoHLS9vmWfk5zRi7qQc6WrEmp2Jh8UpgWMaxHctK", &amnt_int);
      char str[20];
      sprintf(str, "amount %d \r\n", amnt_int);
      // print our string
      SWO_PrintString(str);
      WIFI_CloseClientConnection(0);
    }
  }
\$\endgroup\$
  • \$\begingroup\$ You already know what the problem is. Don't try to do delays (or any other long and/or complicated operations) inside an interrupt. Just use the interrupt to set a flag, then in your main loop (or a task if you have an OS) check the flag and do your complicated stuff there. You will ofc remember to declare your flag variable as volatile I'm sure. \$\endgroup\$ – brhans May 10 at 14:48
  • \$\begingroup\$ The problem I wanna make both work, the board as a client and a server, don't know how to structure it... so what I understand the timer interrupt will just set a flag, and then process the server, but what about the client ? \$\endgroup\$ – andreahmed May 10 at 15:10
  • \$\begingroup\$ Why can't you do the same thing for the client? \$\endgroup\$ – brhans May 10 at 15:41
  • \$\begingroup\$ so I do another timer for the client too ? \$\endgroup\$ – andreahmed May 10 at 15:48
  • \$\begingroup\$ Sure. Or figure out a way to use the same timer for both (with different flag variables or something like that). \$\endgroup\$ – brhans May 10 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.