Skip to content

Commit

Permalink
AMC: Add support for embot::hw:timer (#277)
Browse files Browse the repository at this point in the history
* AMC: Add embot::hw:timer to embot_hw_bsp_amc.cpp

* AMC: Add TEST_EMBOT_HW_TIMER

* Remove HAL_TIM_Base_MspInit and HAL_TIM_Base_MspDeInit from tim.c

* Add TIM13 an TIM15 to Resources Manager configuration. Update TIM16 CPU allocation

* Update the PROP structure in order to configure the clock speed directly.

This change preserve the backcompatibility thanks to the default contructor
  • Loading branch information
sgiraz authored Jun 23, 2022
1 parent 65f9ed0 commit bf7555b
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 74 deletions.
171 changes: 171 additions & 0 deletions emBODY/eBcode/arch-arm/board/amc/bsp/embot_hw_bsp_amc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,177 @@ namespace embot { namespace hw { namespace timer {
}
}}}

#else

namespace embot { namespace hw { namespace timer {
#if defined(STM32HAL_BOARD_AMC)
// sadly we cannot use constexpr because of the reinterpret_cast<> inside TIM6 etc.
TIM_HandleTypeDef htim13;
static const PROP tim13p = { .TIMx = TIM13, .handle = &htim13, .clock = embot::hw::CLOCK::none, .speed = 200 * 1000000, .isonepulse = false, .mastermode = true };

TIM_HandleTypeDef htim15;
static const PROP tim15p = { .TIMx = TIM15, .handle = &htim15, .clock = embot::hw::CLOCK::none, .speed = 200 * 1000000, .isonepulse = false, .mastermode = true };

TIM_HandleTypeDef htim16;
static const PROP tim16p = { .TIMx = TIM16, .handle = &htim16, .clock = embot::hw::CLOCK::none, .speed = 200 * 1000000, .isonepulse = false, .mastermode = true };

constexpr BSP thebsp {
// maskofsupported
mask::pos2mask<uint32_t>(TIMER::thirteen) | mask::pos2mask<uint32_t>(TIMER::fifteen) | mask::pos2mask<uint32_t>(TIMER::sixteen),
// properties
{{
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // from 1 to 8
nullptr, nullptr, nullptr, nullptr, &tim13p, nullptr, &tim15p, &tim16p // from 9 to 16
}}
};

void BSP::init(embot::hw::TIMER h) const {}
#else
#error embot::hw::timer::thebsp must be defined
#endif

const BSP& getBSP()
{
return thebsp;
}
}}}

// in here it is implemented in the way the good old hal2 was doing: the handler directly manages the callback
// instead the stm hal make a lot of calls before actually calling the callback code, hence it is slower.

#include "embot_hw_timer.h"

void manageInterrupt(embot::hw::TIMER t, TIM_HandleTypeDef *htim)
{
if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_UPDATE) != RESET)
{
if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_UPDATE) !=RESET)
{
__HAL_TIM_CLEAR_IT(htim, TIM_IT_UPDATE);
embot::hw::timer::execute(t);
}
}
}


extern "C" {
void TIM8_UP_TIM13_IRQHandler(void)
{
#warning TODO: cambiare il modo in cui si chiama la callback. usare le callback di stm32
manageInterrupt(embot::hw::TIMER::thirteen, &embot::hw::timer::htim13);
}

void TIM15_IRQHandler(void)
{
manageInterrupt(embot::hw::TIMER::fifteen, &embot::hw::timer::htim15);
}

void TIM16_IRQHandler(void)
{
manageInterrupt(embot::hw::TIMER::sixteen, &embot::hw::timer::htim16);
}
}


extern "C"
{
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM13)
{
/* USER CODE BEGIN TIM13_MspInit 0 */

/* USER CODE END TIM13_MspInit 0 */
/* TIM13 clock enable */
__HAL_RCC_TIM13_CLK_ENABLE();

/* TIM13 interrupt Init */
HAL_NVIC_SetPriority(TIM8_UP_TIM13_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM8_UP_TIM13_IRQn);
/* USER CODE BEGIN TIM13_MspInit 1 */

/* USER CODE END TIM13_MspInit 1 */
}
else if(tim_baseHandle->Instance==TIM15)
{
/* USER CODE BEGIN TIM15_MspInit 0 */

/* USER CODE END TIM15_MspInit 0 */
/* TIM15 clock enable */
__HAL_RCC_TIM15_CLK_ENABLE();

/* TIM15 interrupt Init */
HAL_NVIC_SetPriority(TIM15_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM15_IRQn);
/* USER CODE BEGIN TIM15_MspInit 1 */

/* USER CODE END TIM15_MspInit 1 */
}
else if(tim_baseHandle->Instance==TIM16)
{
/* USER CODE BEGIN TIM16_MspInit 0 */

/* USER CODE END TIM16_MspInit 0 */
/* TIM15 clock enable */
__HAL_RCC_TIM16_CLK_ENABLE();

/* TIM16 interrupt Init */
HAL_NVIC_SetPriority(TIM16_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM16_IRQn);
/* USER CODE BEGIN TIM15_MspInit 1 */

/* USER CODE END TIM15_MspInit 1 */
}
}

void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{

if(tim_baseHandle->Instance==TIM13)
{
/* USER CODE BEGIN TIM13_MspDeInit 0 */

/* USER CODE END TIM13_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM13_CLK_DISABLE();

/* TIM13 interrupt Deinit */
HAL_NVIC_DisableIRQ(TIM8_UP_TIM13_IRQn);
/* USER CODE BEGIN TIM13_MspDeInit 1 */

/* USER CODE END TIM13_MspDeInit 1 */
}
else if(tim_baseHandle->Instance==TIM15)
{
/* USER CODE BEGIN TIM15_MspDeInit 0 */

/* USER CODE END TIM15_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM15_CLK_DISABLE();

/* TIM15 interrupt Deinit */
HAL_NVIC_DisableIRQ(TIM15_IRQn);
/* USER CODE BEGIN TIM15_MspDeInit 1 */

/* USER CODE END TIM15_MspDeInit 1 */
}
else if(tim_baseHandle->Instance==TIM16)
{
/* USER CODE BEGIN TIM16_MspDeInit 0 */

/* USER CODE END TIM16_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM16_CLK_DISABLE();

/* TIM16 interrupt Deinit */
HAL_NVIC_DisableIRQ(TIM16_IRQn);
/* USER CODE BEGIN TIM16_MspDeInit 1 */

/* USER CODE END TIM16_MspDeInit 1 */
}
}
}

#endif // timer

// - end-of-file (leave a blank line after)----------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "embot_hw_led.h"
#include "embot_hw_sys.h"
#include "embot_hw_can.h"
#include "embot_hw_timer.h"


#include "embot_os_theScheduler.h"
Expand Down Expand Up @@ -48,6 +49,8 @@ constexpr embot::core::relTime tickperiod = 1000*embot::core::time1millisec;
//#define TEST_EMBOT_HW_CAN_gateway_CAN2toCAN1
//#define TEST_EMBOT_HW_CAN_gateway_CAN1toCAN2

//# define TEST_EMBOT_HW_TIMER

void test_embot_hw_init();
void test_embot_hw_tick();
#endif
Expand Down Expand Up @@ -302,7 +305,7 @@ void tMAIN(void *p)
t->run();
}


// entry point (first running thread)
void initSystem(embot::os::Thread *t, void* initparam)
{
volatile uint32_t cpufreq = embot::hw::sys::clock(embot::hw::CLOCK::syscore);
Expand All @@ -313,6 +316,7 @@ void initSystem(embot::os::Thread *t, void* initparam)

embot::core::print("INIT: creating the system services: timer manager + callback manager");

// start the services with default params
embot::os::theTimerManager::getInstance().start({});
embot::os::theCallbackManager::getInstance().start({});

Expand Down Expand Up @@ -454,8 +458,91 @@ void done3(void* p)
embot::core::print("SPI::three cbk called");
}

// ---------------------------------------------------------------------------------------------

static std::uint8_t on = 0;
constexpr embot::os::Event evtTIM_HW = embot::core::binary::mask::pos2mask<embot::os::Event>(0);
void timer_cbk(void* p)
{
embot::os::EventThread* thr = reinterpret_cast<embot::os::EventThread*>(p);
thr->setEvent(evtTIM_HW);
}

void toggleLED(void*)
{
if(0 == on)
{
embot::hw::led::off(embot::hw::LED::five);
on = 1;
}
else
{
embot::hw::led::on(embot::hw::LED::five);
on = 0;
}
// embot::hw::chip::testof_AS5045();
}

void tim_hw_onevent(embot::os::Thread *t, embot::os::EventMask eventmask, void *param)
{
if( eventmask == evtTIM_HW)
{
toggleLED(nullptr);
}
}


void tTIMTEST(void *p)
{
embot::os::Thread* t = reinterpret_cast<embot::os::Thread*>(p);
t->run();
}

void test_embot_hw_init()
{
#if defined(TEST_EMBOT_HW_TIMER)

embot::hw::led::init(embot::hw::LED::five);

// 1. Configure and create a thread that will toggle the LED when the event evtTIM_HW is set.
embot::core::print("Creating a thread that manages the timer callback.");

embot::os::EventThread::Config configEV {
6*1024,
embot::os::Priority::high40,
nullptr,
nullptr,
embot::core::reltimeWaitForever,
tim_hw_onevent,
"timThreadEvt"
};

embot::os::EventThread *thr {nullptr};
thr = new embot::os::EventThread;
thr->start(configEV, tTIMTEST);

// 2. Create and initialize the timer with the callback defined above
embot::core::Callback tim_hw_cbk { timer_cbk, thr };

constexpr embot::core::relTime period {embot::core::time1millisec * 1};
// constexpr embot::core::relTime period {embot::core::time1microsec * 50};
// constexpr embot::core::relTime period {embot::core::time1millisec * 1000};

embot::hw::timer::Config timerConfig {
period,
embot::hw::timer::Mode::periodic,
tim_hw_cbk,
};

constexpr embot::hw::TIMER timer2test {embot::hw::TIMER::thirteen};
//constexpr embot::hw::TIMER timer2test {embot::hw::TIMER::fifteen};
//constexpr embot::hw::TIMER timer2test {embot::hw::TIMER::sixteen};
embot::hw::timer::init(timer2test, timerConfig);

// 3. Start the timer
embot::hw::timer::start(timer2test);
#endif

#if defined(TEST_EMBOT_HW_FLASH)


Expand Down
7 changes: 4 additions & 3 deletions emBODY/eBcode/arch-arm/embot/hw/embot_hw_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ namespace embot { namespace hw { namespace timer {
#else

#if defined(STM32HAL_STM32H7)
#error pls verify embot::hw::timer for STM32H7
//#error pls verify embot::hw::timer for STM32H7
#endif


Expand Down Expand Up @@ -351,7 +351,7 @@ namespace embot { namespace hw { namespace timer {
{
// for some timers referencespeed could also be HAL_RCC_GetSysClockFreq() or HAL_RCC_GetPCLK1Freq() or HAL_RCC_GetPCLK2Freq()
// i embed teh choice into that into embot::hw::sys::clock()
uint32_t referencespeed = embot::hw::sys::clock(stm32data->clock);
uint32_t referencespeed = (embot::hw::CLOCK::none == stm32data->clock) ? stm32data->speed : embot::hw::sys::clock(stm32data->clock);

effectivetime = time;

Expand Down Expand Up @@ -429,7 +429,8 @@ namespace embot { namespace hw { namespace timer {
#if defined(STM32HAL_STM32L4) && (STM32HAL_DRIVER_VERSION >= 0x183)
phandletimx->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
#elif defined(STM32HAL_STM32H7)
#error verify it
#warning TODO: verify it
phandletimx->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
#endif


Expand Down
3 changes: 2 additions & 1 deletion emBODY/eBcode/arch-arm/embot/hw/embot_hw_timer_bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ namespace embot { namespace hw { namespace timer {
{
TIM_t* TIMx {nullptr};
TIM_Handle* handle {nullptr};
embot::hw::CLOCK clock {embot::hw::CLOCK::none}; // the clock used by the timer
embot::hw::CLOCK clock {embot::hw::CLOCK::none}; // the clock used by the timer
uint32_t speed {0};
bool isonepulse {false};
bool mastermode {false};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4641,7 +4641,7 @@

<Group>
<GroupName>board-amc-v1A0-eth</GroupName>
<tvExp>1</tvExp>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
Expand Down
Loading

0 comments on commit bf7555b

Please sign in to comment.