NXP Pins Tool: Clock Gates and Controlling the Bits
Learn about clock gates in this follow up article about muxing with the NXP pins tool.
Join the DZone community and get the full member experience.
Join For FreeWith the NXP Pins Tool (see "Tutorial: Muxing with the New NXP Pins Tool") I can configure and mux (multiplex) the microcontroller pins. What is really powerful and what might not be so obvious at first sight is that it gives me deep control over every register bit and setting. For example, I have below the PTB1 (Port B, pin 1) muxed as GPIO (General Purpose I/O):
PTB1 Muxed with Pins Tool
But it only generates this:
void BOARD_InitPins(void) {
CLOCK_EnableClock(kCLOCK_PortB); /* Port B Clock Gate Control: Clock enabled */
PORT_SetPinMux(PORTB, PIN1_IDX, kPORT_MuxAsGpio); /* PORTB1 (pin 54) is configured as PTB1 */
}
So what about all the other bits and pieces?
Clock Gates
The first line turns on the clock gates (clocks the peripheral, otherwise any access to the peripheral will create a hard fault):
CLOCK_EnableClock(kCLOCK_PortB); /* Port B Clock Gate Control: Clock enabled */
If I do that myself in my application, I can configure this with the Properties menu:
Pins Properties
Which provides a setting for this:
Clock Gate Enable Setting
Init or Not Init, That’s the Question
The other thing is the code it generates for the muxing:
PORT_SetPinMux(PORTB, PIN1_IDX, kPORT_MuxAsGpio); /* PORTB1 (pin 54) is configured as PTB1 */
It ‘only’ configures the muxing, but what about all the other settings, like direction, slew rate and so on?
Direction and other settings
The thing is that the tool is using/assuming ‘out-of-power-on/reset’ values. So the code is optimized for that case. But there are cases where I would like to do a ‘full’ initialization, e.g. because I need to re-configure the hardware based on different reset (like a software reset), or because some safety rules require me to do a full initialization.
The visual cue is that the values in italic font show me which values are implicitly set, and which ones are explicitly configured by me. Such as in the hardware the slew rate is set to ‘fast’ by default:
Slew rate
I can explicitly set the slew rate to Fast:
Setting Slew Rate
Now it is explicitly set to fast (not in italic)
Set Slew Rate
And with this I get the slew rate explicitly set:
void BOARD_InitPins(void) {
CLOCK_EnableClock(kCLOCK_PortB); /* Port B Clock Gate Control: Clock enabled */
PORT_SetPinMux(PORTB, PIN1_IDX, kPORT_MuxAsGpio); /* PORTB1 (pin 54) is configured as PTB1 */
PORTB->PCR[1] = ((PORTB->PCR[1] &
(~(PORT_PCR_SRE_MASK | PORT_PCR_ISF_MASK))) /* Mask bits to zero which are setting */
| PORT_PCR_SRE(PCR_SRE_FAST) /* Slew Rate Enable: Fast slew rate is configured on the corresponding pin, if the pin is configured as a digital output. */
);
}
If I want to explicitly set it to ‘do not initialize’, there is a menu item for this too:
No init
Then this bit and setting does not get configured.
Summary
If I need the clock gates turned on or not, depends on the application. As for myself, I prefer to have it initialized by the Pins tool. If I do it in the application code, there is a setting for it to turn it off.
By default, the tool only initializes the bits and registers needed, coming out of reset. The values in italic are not configured as already in that state. If I want to explicitly initialize such a setting, I can configure it to be initialized. In that case, the setting turns from ‘italic’ to ‘non-italic.
The above is actually really powerful: I can use the ‘tabs’ or ‘functions’ to create initialization code depending on my needs. But that would be probably.
Happy Biting!
Published at DZone with permission of Erich Styger, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments