The Pico/W In C: Direct To Hardware |
Written by Harry Fairhead | ||||||||||||||||||||||||||||||||||||||||||||||||||
Tuesday, 21 February 2023 | ||||||||||||||||||||||||||||||||||||||||||||||||||
Page 4 of 4
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:
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, 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 Getting and setting the Schmitt trigger is just as easy: void pad_set_schmitt(uint gpio, bool value) { 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; } 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 DeeperThere 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 CBy Harry FairheadBuy from Amazon. Contents
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.
Comments
or email your comment to: comments@i-programmer.info |
||||||||||||||||||||||||||||||||||||||||||||||||||
Last Updated ( Tuesday, 21 February 2023 ) |