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

31:8

Reserved.

-

-

-

7

OD

Output disable. Has priority over output enable from peripherals

RW

0x0

6

IE

Input enable

RW

0x1

5:4

DRIVE

Drive strength.

0x0 → 2mA

0x1 → 4mA

0x2 → 8mA

0x3 → 12mA

RW

0x1

3

PUE

Pull up enable

RW

0x0

2

PDE

Pull down enable

RW

0x1

1

SCHMITT

Enable Schmitt trigger

RW

0x1

0

SLEWFAST

Slew rate control. 1 = Fast, 0 = Slow

RW

0x0

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);
    else
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],
value<<5,mask);
}

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

picoC2E360

Buy from Amazon.

Contents

  • 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 

Extra: Adding WiFi To The Pico 2

<ASIN:1871962803>

<ASIN:187196279X>

 

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.

Banner


Improved Code Completion With JetBrains Mellum
29/10/2024

JetBrains has launched Mellum, a proprietary large language model specifically built for coding. Currently available only with JetBrains AI Assistant, Mellum is claimed to provide faster, sm [ ... ]



Rust And C++ Should Be Friends?
20/11/2024

The Rust Foundation has just released a statement on Rust and C++ interoperability and Google is ponying up $1 to see that it gets done.


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Tuesday, 21 February 2023 )