The Pico/W In C: Direct To Hardware
Written by Harry Fairhead   
Tuesday, 21 February 2023
Article Index
The Pico/W In C: Direct To Hardware
Single-Cycle IO Block
Example I - Events
Slew rate

The final option is to change the slew rate. There is no information on this in the data sheet, but slowing the rate of change of the GPIO line can be useful when driving long lines or reactive loads. How useful this is in practice is a matter of experimentation.

If you want to work with the PAD directly then you need to know the location and format of the PAD control registers. These start at 0x4001c000 (defined as PADS_BANK0_BASE in the SDK) and the first, controlling the GP0 PAD is at offset 0x04 and in general the register for GPn PAD is at offset 4(n+1).

The format of the PAD register is:

Name TypeReset

Bits Description








Output disable. Has priority over output enable from peripherals





Input enable





Drive strength.

0x0 → 2mA

0x1 → 4mA

0x2 → 8mA

0x3 → 12mA





Pull up enable





Pull down enable





Enable Schmitt trigger





Slew rate control. 1 = Fast, 0 = Slow



Using this information it is fairly easy to write functions to set each of the characteristics of the PAD. For example, to read the slew rate:

uint32_t pad_get_slew(uint gpio){
 return padsbank0_hw ->io[gpio] &0x01;

where we are using the SDK-defined struct padsbank0_hw which is defined in:

#include "hardware/structs/padsbank0.h"

To set the same bit we need to use a mask that only changes the bit in question. This sort of operation is so commonly required that the SDK provides some functions to do the job:

hw_set_bits (io_rw_32 *addr, uint32_t mask)
hw_clear_bits (io_rw_32 *addr, uint32_t mask)
hw_xor_bits (io_rw_32 *addr, uint32_t mask)
hw_write_masked (io_rw_32 *addr, uint32_t values, 
uint32_t write_mask)

These are not only convenient to use, they are also atomic in the sense that if both cores attempt to modify the same register the two operations will not interfere with one another – one will complete before the other begins.


Using the set_bits function, we can easily set the skew rate:

void pad_set_slew(uint gpio, bool value)
    if (value)
hw_set_bits(&padsbank0_hw->io[gpio],1ul); }

Getting and setting the Schmitt trigger is just as easy:

void pad_set_schmitt(uint gpio, bool value)
uint32_t mask = 1ul << 1; if (value) hw_set_bits(&padsbank0_hw->io[gpio],mask); else hw_set_bits(&padsbank0_hw->io[gpio],mask); }

Setting the drive is slightly more complicated as it is a three-bit value:

uint32_t pad_get_drive(uint gpio)
    return (padsbank0_hw->io[gpio] & 0xE0) >> 5;
void pad_set_drive(uint gpio, uint8_t value) {
uint32_t mask = 0xE0ul; hw_write_masked(&padsbank0_hw->io[gpio],

Of course we, don’t need to deal with the pull-ups or pull-downs as there are SDK functions that do the job.

Digging Deeper

There is so much more to explore, but now we have covered the details that make things difficult to get started. From here you can read the SDK functions to find out how they work and you should be able to modify them and add to them. If you find that you need to create a program that doesn’t include the SDK, you can also use this knowledge to create slimmed-down versions of the code that does exactly what you want. Knowledge is never superfluous.


Programming the Raspberry Pi Pico In C

By Harry Fairhead


Buy from Amazon.


  • Preface
  • Chapter 1 The Raspberry Pi Pico – Before We Begin
  • Chapter 2 Getting Started
  • Chapter 3 Getting Started With The GPIO
  • Chapter 4 Simple Output
  • Chapter 5 Some Electronics
  • Chapter 6 Simple Input
        Extract:   GPIO Input ***NEW!
  • Chapter 7 Advanced Input – Events and Interrupts
  • Chapter 8 Pulse Width Modulation
        Extract: Basic PWM
  • Chapter 9 Controlling Motors And Servos
  • Chapter 10 Getting Started With The SPI Bus
  • Chapter 11 A-To-D and The SPI Bus
  • Chapter 12 Using The I2C Bus
  • Chapter 13 Using The PIO
        Extract: A 1-Wire PIO Program  
  • Chapter 14 The DHT22 Sensor Implementing A Custom Protocol
  • Chapter 15 The 1‑Wire Bus And The DS1820
  • Chapter 16 The Serial Port
  • Chapter 17 Using the Pico W
       Extract: Simple Web Client
       Extract:A Better Connect
  • Chapter 18 The Pico/W In C: Direct To Hardware 




To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.


Eclipse Releases Theia IDE

The Eclipse Foundation has released Theia IDE, which they say is created for developers seeking a modern, open, and flexible tool for their coding pursuits. The IDE is based on the Theia Platform, whi [ ... ]

Amazon Updates Q Family And Previews App Studio

Amazon made multiple announcements at its AWS Summit in New York, including a preview of AWS App Studio, the addition of Q Developer to SageMaker Studio, and an Amazon Q Apps API.

More News

kotlin book



or email your comment to:

Last Updated ( Tuesday, 21 February 2023 )