Page 1 of 1

implementation of STM32/GPIOv1/pal_lld.c

Posted: Wed Aug 08, 2012 9:02 am
by hazelnusse
In my board.h file, I define things like

Code: Select all

/*
 * Port C setup:
 * Everything input with pull-up except:
 * PC0  - Push Pull output  (Control timing pin)
 * PC3  - Push Pull output  (XBee Reset)
 * PC6  - Push Pull output  (STAT1 green LED).
 * PC7  - Push Pull output  (STAT2 yellow LED).
 * PC8  - Push Pull output  (Steer Motor Direction).
 * PC9  - Push Pull output  (Steer Motor Enable, low enables).
 * PC11 - Push Pull output  (Rear Wheel Motor Enable, low enables).
 * PC12 - Push Pull output  (Rear Wheel Motor Direction, high forward).
 * PC14 - Normal input      (OSC32 IN).
 * PC15 - Normal input      (OSC32 OUT).
 */
#define VAL_GPIOCCRL            0x22882882      /*  PC7...PC0 */
#define VAL_GPIOCCRH            0x44822822      /* PC15...PC8 */
#define VAL_GPIOCODR            0xFFFFF53F      /* Output data register


This gets put into pal_default_config in board.c, and then subsequently used in
pal_lld.c by the function _pal_lld_init in the following manner:

Code: Select all

GPIOC->ODR = config->PCData.odr;
GPIOC->CRH = config->PCData.crh;
GPIOC->CRL = config->PCData.crl;


Take PC11 as an example. This pin enables a motor drive when it is clear. I
have an external pull up on this pin to ensure the drive is not enabled during
power on. The problem with writing to ODR before CRH and CRL is the following:

1) "During and just after reset, the alternate functions are not active and the
I/O ports are configured in Input Floating mode" (Section 9.1.1 of RM0008)
2) "When the I/O Port is programmed as Input: The Output Buffer is disabled"
(Section 9.1.7 of RM0008).

The implication of 1) and 2) is that any bits that are set in the ODR are
ignored. They don't latch to the ODR because of item 2).

In my case, the next implication of this is that my motor drive is enabled even
though 1) have an external pull up and 2) have supposedely configured the
default value of the ODR. Depending on the state of the duty cycle line, this
could mean my motor turns on when I don't want it to and this is very bad.

This approach to configuring the default bits only works if the default pin
state is low.

Consider instead writing to CRH and CRL before ODR. The reset state of the ODR
is 0x0000 0000, so as soon as outputs are enabled, the pins will be pulled low.
However, as soon as the ODR is written with the value programmed in
VAL_GPIOCODR, the pins will be at the desired state. This might cause a small
blip in the output of the pin, but at least the state will set to whatever is
programmed in board.h. At 8Mhz, and lets say 10 instructions (I think it is
actually less) to get ODR written, this is 1.25 microseconds. At 72 Mhz, it is 139
nanoseconds. In the latter case, this is probably not enough time to actually pull
the line low and enable the drive. But even it if it was, there is really no other
option: the output buffer is disconnected when the pins are input mode, and the
reset value of the ODR is 0x00000000, so no matter how you slice it, when you
put a pin into output mode, it is going to pull low until you write to the ODR.

Is there a design decision which justifies not being able to make the default
state of an output pin to be set? Currently because ODR is written to first,
you can only have a default state that is clear.

Re: implementation of STM32/GPIOv1/pal_lld.c

Posted: Wed Aug 08, 2012 9:14 am
by Giovanni
Hi,

2) "When the I/O Port is programmed as Input: The Output Buffer is disabled"
(Section 9.1.7 of RM0008).


Disabled does not mean it does not latch what you write in it, just that the latched value is not driven on the pin.

This is how it is meant to work (unless I misunderstood it):

Assume that your idle state is one, but the reasoning does apply also for zero.

0) Reset.
1) Initially PC11 is floating (you should have an external pullup to ensure state one).
2) ODR is written with the value one, the value is latched in the ODR register but the pin is still a floating input so the value is not yet output, just latched in the register, nothing changes.
3) PC11 is still a floating input, the external resistor keeps the safe state one.
4) CRH is written and PC1 becomes a push-pull output, the value one previously written in ODR is actually driven on PC11 with no transients.

The state one is kept from reset until the system is initialized.

Giovanni

Re: implementation of STM32/GPIOv1/pal_lld.c

Posted: Wed Aug 08, 2012 9:32 am
by hazelnusse
I'll defer to your experience on this. I must have some other bug somewhere. I have an external pull up, but my drive is still being enabled, so it somehow being pull low. I don't know why. I'll investigate further and report back when I have it figured out.

Thanks very much for the clear and rapid response.

Luke

Re: implementation of STM32/GPIOv1/pal_lld.c

Posted: Thu Aug 09, 2012 7:17 am
by hazelnusse
Giovanni,
You are correct. I had an issue with the order in which I configured the GPIO's associated with my TIM, and that was causing the issue. All is right in the world. Thanks again.

Luke