Electronics > Microcontrollers

Issue with STM32F1 pull up pin

<< < (4/4)

rhodges:
Here is what I use for STM32F103 ("BLUE_PILL") and STM32F030 ("F030_DEMO").
And here is an example.

--- Code: ---void local_setup(void)
{
    /* We will use A1 as a general purpose oscilloscope signal and
     * as a trigger for the signal analyzer.
     * A0 is the strobe signal for testing with TM1638 LED+keypad.
     */
    IO_PIN      pin;

    pin.gpio = GPIOA;
    pin.pin_num = 0;
    pin_config(&pin, PIN_OUTPUT | PIN_PUSHPULL | PIN_SPEED_MED);
    GPIOA->ODR = 1;

    pin.pin_num = 1;
    pin_config(&pin, PIN_OUTPUT | PIN_PUSHPULL | PIN_SPEED_MED);
}

--- End code ---

wek:

--- Quote from: yashrk on April 12, 2024, 04:25:44 pm ---I am planning to use libopencm3 for my future experiments, any comments on that will be helpful.

--- End quote ---
Why? (Hope this counts as a comment.)

--- Quote ---Also, any suggestion on how to do this bit wise operation better.

--- Code: --- // Set Config to 10
GPIOC -> CRH &= ( uint32_t ) ~( 1 << 26 );
GPIOC -> CRH |= ( 1 << 27 );

--- End code ---

--- End quote ---
Note, that there is no "best" in microcontroller programming, as appropriateness of any solution is context-dependent.

This particular snippet can be made "better" by reducing number of reads and writes from/to the GPIO register:

--- Code: --- GPIOC->CRH = (GPIOC->CRH & ~(1 << 26)) | (1 << 27);  // set CNF to 0b10: Input with pull-up / pull-down

--- End code ---
and it can be made somewhat "better" to manipulate the whole bitfield at once, which also reveals the whole you are going to write:

--- Code: --- GPIOC->CRH = (GPIOC->CRH & ~(0b11 << 26)) | (0b10 << 26);  // set CNF to 0b10: Input with pull-up / pull-down

--- End code ---
and it can be made yet "better" by emphasizing, that you want to set this field for PC14:

--- Code: --- GPIOC->CRH = (GPIOC->CRH & ~( 0b11 << (4 * (14 - 8) + 2)) | (0b10 << (4 * (14 - 8) + 2));      // set CNF14 to 0b10: Input with pull-up / pull-down

--- End code ---

But, in the vast majority of microcontroller applications, you want to set up all the pins at once, at the startup, so that's one single write to the register; i.e.

--- Code: --- GPIOC->CRH = 0
           // define for other GPIOC pins 0..15 here, otherwise they will be set to Analog mode (MODE=0b00, CNF=0b00
           | (0b11 << (4 * (13 - 8) + 0))      // set MODE13 to 0b11: 50MHz output
           | (0b00 << (4 * (13 - 8) + 2))      // set CNF13 to 0b00: General purpose output push-pull
           | (0b00 << (4 * (14 - 8) + 0))      // set MODE14 to 0b00: Input
           | (0b10 << (4 * (14 - 8) + 2))      // set CNF14 to 0b10: Input with pull-up / pull-down
       ;

--- End code ---

You can make this somewhat more readable by writing macros for CNF and MODE fields.

Others may have different ideas about what's *better*.

JW

PS. Disclaimer: there may be errors in the lines above, did not test.

DavidAlfa:
That's a mess to maintain and understand.

wek:

--- Quote from: DavidAlfa on April 13, 2024, 02:51:35 pm ---That's a mess to maintain and understand.

--- End quote ---
Exactly. As I keep saying, "better" is subjective and context dependent.

Here, obviously, the proposed solution is not the "best", and maybe not even "better", for @DavidAlfa.

JW

Navigation

[0] Message Index

[*] Previous page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod