Search code examples
stm32mbed

mbed os: using timer


I am using the board STM32F446RE to run [this project][1]. I am trying to set up and use timer 10 to count. The main.c file is below.

#include "radio.h"
#include <cstdio>

#if defined(SX128x_H)
    #define BW_KHZ              200
    #define SPREADING_FACTOR    7
    #define CF_HZ               2487000000
    #define TX_DBM              6
#else
    #if defined(SX126x_H)
        #define TX_DBM              (Radio::chipType == CHIP_TYPE_SX1262 ? 20 : 14)
    #else
        #define TX_DBM              20
    #endif
    #define BW_KHZ              125
    #define SPREADING_FACTOR    7
    #define CF_HZ               915000000
#endif

/**********************************************************************/
EventQueue queue(4 * EVENTS_EVENT_SIZE);

TIM_HandleTypeDef htim10;
static void MX_TIM10_Init(void)
{

  /* USER CODE BEGIN TIM10_Init 0 */

  /* USER CODE END TIM10_Init 0 */

  /* USER CODE BEGIN TIM10_Init 1 */

  /* USER CODE END TIM10_Init 1 */
  htim10.Instance = TIM10;
  htim10.Init.Prescaler = 0;
  htim10.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim10.Init.Period = 65535;
  htim10.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  //htim10.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim10) != HAL_OK)
  {
    printf("Error Error\r\n");
  }
  /* USER CODE BEGIN TIM10_Init 2 */

  /* USER CODE END TIM10_Init 2 */

}


void tx_test()
{
    static uint8_t seq = 0;

    Radio::radio.tx_buf[0] = seq++;  /* set payload */
    Radio::Send(1, 0, 0, 0);   /* begin transmission */
    printf("sent\r\n");

/*    {
        mbed_stats_cpu_t stats;
        mbed_stats_cpu_get(&stats);
        printf("canDeep:%u ", sleep_manager_can_deep_sleep());
        printf("Uptime: %llu ", stats.uptime / 1000);
        printf("Sleep time: %llu ", stats.sleep_time / 1000);
        printf("Deep Sleep: %llu\r\n", stats.deep_sleep_time / 1000);
    }*/
}

void txDoneCB()
{
    printf("got-tx-done\r\n");
    queue.call_in(500, tx_test);
}

void rxDoneCB(uint8_t size, float rssi, float snr)
{
}


void radio_irq_callback()
{
    queue.call(Radio::service);
}


const RadioEvents_t rev = {
    /* DioPin_top_half */   radio_irq_callback,
    /* TxDone_topHalf */    NULL,
    /* TxDone_botHalf */    txDoneCB,
    /* TxTimeout  */        NULL,
    /* RxDone  */           rxDoneCB,
    /* RxTimeout  */        NULL,
    /* RxError  */          NULL,
    /* FhssChangeChannel  */NULL,
    /* CadDone  */          NULL
};

int main()
{
    printf("\r\nreset-tx ");

    Radio::Init(&rev);

    Radio::Standby();
    Radio::LoRaModemConfig(BW_KHZ, SPREADING_FACTOR, 1);
    Radio::SetChannel(CF_HZ);

    Radio::set_tx_dbm(TX_DBM);

               // preambleLen, fixLen, crcOn, invIQ
    Radio::LoRaPacketConfig(8, false, true, false);
    MX_TIM10_Init();
    HAL_TIM_Base_Start_IT(&htim10);
    HAL_Delay(500);
    __HAL_TIM_SET_COUNTER(&htim10, 15);
    uint16_t timer_val = __HAL_TIM_GET_COUNTER(&htim10);

    printf("timer value = %d", timer_val);


    queue.call_in(500, tx_test);

    queue.dispatch();
}

Basically, I used the initialisation code taken from STM32CubeMX and I configured the clock of timer 10 in the file system_clock.c (normally in STM32CubeMX, clocks are configured in Clock Configuration tab). Then I initialized and started the time before getting the counter from that function. However, all I get from __HAL_TIM_GET_COUNTER is 0.

I want to ask what I did wrong.

Thank you, Huy Nguyen [1]: https://os.mbed.com/users/dudmuck/code/simple_tx/


Solution

  • This is purely speculation, but maybe you've not enabled the clock for TIM10. I don't know if that would explain what you are seeing or not. You might need something like __HAL_RCC_TIM10_CLK_ENABLE();