Is this a bug? AF1_TIM2

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
User avatar
dbc
Posts: 89
Joined: Fri Aug 28, 2015 11:02 pm
Location: Sunnyvale, CA

Is this a bug? AF1_TIM2

Post by dbc » Sat Sep 05, 2015 7:17 pm

In chasing a problem in my code, I noticed something suspicious (bearing in mind that I'm not yet very familiar with the C code...)

Alternate functions for pin X6 are listed as:

Code: Select all

X6 [Pin.AF1_TIM2, Pin.AF1_TIM2, Pin.AF3_TIM8, Pin.AF5_SPI1]
Note AF1_TIM2 appears twice.

In file pins_PYBV10.c (is this automatically generated???) the definition table is:

Code: Select all

const pin_af_obj_t pin_A5_af[] = {
  AF( 1, TIM     ,  2, CH1       , TIM2    ), // TIM2_CH1
  AF( 1, TIM     ,  2, ETR       , TIM2    ), // TIM2_ETR
  AF( 3, TIM     ,  8, CH1N      , TIM8    ), // TIM8_CH1N
#if (defined(MICROPY_HW_ENABLE_SPI1) && MICROPY_HW_ENABLE_SPI1)
  AF( 5, SPI     ,  1, SCK       , SPI1    ), // SPI1_SCK
#endif
  //(10, OTG     ,  0, HS_ULPI_CK, OTG     ), // OTG_HS_ULPI_CK
  //(15, EVENTOUT,  0,           , EVENTOUT), // EVENTOUT
};
Which looks like CH1 and ETR are both mapped to AF1?? Am I reading that correctly? Perhaps some copy/pasta here?

-dave

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Is this a bug? AF1_TIM2

Post by dhylands » Sat Sep 05, 2015 7:48 pm

They really are mapped to the same alternate function.

ETR is the external clock (and would be considered an input to the timer).

In order to use the external clock feature on TIM2, the timer would need to be configured properly, and you'd lose the ability to use that pin as CH1.

Think of it as a pin that has a multiplexer at the pin level, and an additional multiplexer at the timer level.

When I created the pin definitions, the data sheet showed it as TIM2_CH1_ETR and for simplicity in the pin parsing code I decided to put it in the csv file as TIM2_CH1/TIM2_ETR.

If you search through the csv file for the forward slash character / you'll find several other examples where multiple functions are mapped onto the same alternate function. Which function is actually used depends on further configuration of the peripherals.

I think that the history behind this is that originally, the alternate functions was more expressive and included not just the alternate function index, but also the function, so when this was first written, the alternate function list would have shown as:

Code: Select all

X6 [Pin.AF1_TIM2_CH1, Pin.AF1_TIM2_ETR, Pin.AF3_TIM8_CH1N, Pin.AF5_SPI1_SCK]
but that wound up consuming quite a bit of extra memory.

So we opted instead to generate build-PYBV10/pins_af.py and people could include that in their project if they wanted more detail.

User avatar
dbc
Posts: 89
Joined: Fri Aug 28, 2015 11:02 pm
Location: Sunnyvale, CA

Re: Is this a bug? AF1_TIM2

Post by dbc » Sat Sep 05, 2015 9:05 pm

I understand that CH1 and ETR share the same pin.

I was testing this code:

Code: Select all

pina = pyb.Pin('X6',pyb.Pin.AF_PP, pull=pyb.Pin.PULL_NONE, af=pyb.Pin.AF1_TIM2)
pinb = pyb.Pin('X17',pyb.Pin.AF_PP, pull=pyb.Pin.PULL_NONE, af=pyb.Pin.AF1_TIM2)

tim = pyb.Timer(2, prescaler=0, period=65535)
tim.channel(1,Timer.ENC_AB)
I was getting no counts after several attempts.

Now today, it all appears to be happy --- so I must have had some flakey wiring issue or something.

Post Reply