[En-Nut-Discussion] Run-time or compile-time configurable devices?

Uwe Bonnes bon at elektron.ikp.physik.tu-darmstadt.de
Wed Jan 4 18:52:49 CET 2012


Hello Ulrich,

to make a long discussion short:

>>>>> "Ulrich" == Ulrich Prinz <ulrich.prinz at googlemail.com> writes:


    Ulrich> Right, accessing the NutRegisterDevice( &DEV_UART4...  causes
    Ulrich> the linker to see that this device is addressed and therefore
    Ulrich> the code must be linked.

This would mean "compiled-time" configurable! And then in your stm32_tim.c file e.g.
void TIM_Init( TIM_TypeDef *TIM )
{
  //enable clock
  switch( ( uint32_t ) TIM ) {
    case (uint32_t) TIM2:
      RCC->APB1ENR |= RCC_APB1Periph_TIM2; 
      break;
    case (uint32_t) TIM3:
      RCC->APB1ENR |= RCC_APB1Periph_TIM3; 
      break;
    case (uint32_t) TIM4:
      RCC->APB1ENR |= RCC_APB1Periph_TIM4; 
      break;
    case (uint32_t) TIM5:
      RCC->APB1ENR |= RCC_APB1Periph_TIM5; 
      break;
    case (uint32_t) TIM6:
      RCC->APB1ENR |= RCC_APB1Periph_TIM6; 
      break;
    case (uint32_t) TIM7:
      RCC->APB1ENR |= RCC_APB1Periph_TIM7; 
      break;
  }
}

could be replaced with a macro

#define  TIM_Init(timer ) \
    ((timer) == (NUTTIMER2))?(CM3MEM(CM3BBSET(CM3ADDR(RCC_BASE, RCC_TypeDef, APB1ENR), _BI32(RCC_APB1Periph_TIM2))) = 1) \
    :((timer) == (NUTTIMER3))?(CM3MEM(CM3BBSET(CM3ADDR(RCC_BASE, RCC_TypeDef, APB1ENR), _BI32(RCC_APB1Periph_TIM3))) = 1) \
    :((timer) == (NUTTIMER4))?(CM3MEM(CM3BBSET(CM3ADDR(RCC_BASE, RCC_TypeDef, APB1ENR), _BI32(RCC_APB1Periph_TIM4))) = 1) \
    :((timer) == (NUTTIMER5))?(CM3MEM(CM3BBSET(CM3ADDR(RCC_BASE, RCC_TypeDef, APB1ENR), _BI32(RCC_APB1Periph_TIM5))) = 1) \
    :((timer) == (NUTTIMER6))?(CM3MEM(CM3BBSET(CM3ADDR(RCC_BASE, RCC_TypeDef, APB1ENR), _BI32(RCC_APB1Periph_TIM6))) = 1) \
    :((timer) == (NUTTIMER7))?(CM3MEM(CM3BBSET(CM3ADDR(RCC_BASE, RCC_TypeDef, APB1ENR), _BI32(RCC_APB1Periph_TIM7))) = 1) :0
  
For TIM_Init() this only means smaller code, but if you think of e.g. some
function like TIM_IntRegister(timer, pfnHandler), a compile time definition
like

#define TIM_IntRegister(timer, pfnHandler) \
    ((timer) == (NUTTIMER2))?IntRegister(TIM2_IRQn, pfnHandler)         \
    :((timer) == (NUTTIMER3))?IntRegister(TIM3_IRQn, pfnHandler)        \
    :((timer) == (NUTTIMER4))?IntRegister(TIM4_IRQn, pfnHandler)        \
    :((timer) == (NUTTIMER5))?IntRegister(TIM5_IRQn, pfnHandler)        \
    :((timer) == (NUTTIMER6))?IntRegister(TIM6_IRQn, pfnHandler)        \
    :((timer) == (NUTTIMER7))?IntRegister(TIM7_IRQn, pfnHandler):0

will save code and runtime, as setting of some interrupt happen quite
often. I used this already to successfully bitbang a one-wire device with a
timer on a STM32F107 device.

Bye
-- 
Uwe Bonnes                bon at elektron.ikp.physik.tu-darmstadt.de

Institut fuer Kernphysik  Schlossgartenstrasse 9  64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------



More information about the En-Nut-Discussion mailing list