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.