The Pico In MicroPython: Direct To The Hardware
source link: https://www.i-programmer.info/programming/hardware/15076-the-pico-in-micropython-direct-to-the-hardware.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Just occasionally you need to work directly with the hardware and MicroPython has the commands to let you do this fairly easily but you need to know more about the hardware than usual. This is an extract from our latest book all about the Raspberry Pi Pico in MicroPython.
Programming the Raspberry Pi Pico In MicroPython
By Harry Fairhead & Mike James
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
- Chapter 7 Advanced Input – Events and Interrupts
- Chapter 8 Pulse Width Modulation
Extract: 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
- Chapter 14 The DHT22 Sensor Implementing A Custom Protocol
Extract: A PIO Driver For The DHT22 - Chapter 15 The 1‑Wire Bus And The DS1820
- Chapter 16 The Serial Port
- Chapter 17 WiFi Using The ESP8266
- Chapter 18 Direct To The Hardware
Extract: Direct To The Hardware ***NEW!
MicroPython provides classes and methods to let you access most of the major hardware features of the Pico. They are very simple wrappers around the basic mechanism of working with the hardware – memory-mapped registers. Unfortunately at the time of writing there are many hardware features which are simply not exposed via MicroPython. In most cases it is possible to extend what you access using lower-level interactions with the hardware – still staying in MicroPython, but writing and reading the low-level register based hardware.
The obvious reason for knowing how to use memory-mapped registers is that if MicroPython doesn’t provide a function that does what you want, create it! Perhaps a better reason is just to know how things work. In this chapter we take a look at how the Pico presents its hardware for you to use and how to access it via basic software.
Registers
Some processors have special ways of connecting devices, but the Pico’s processor uses the more common memory-mapping approach. In this, each external device is represented by a set of memory locations or “registers” that control it. Each bit in the register controls some aspect of the way the device behaves. Groups of bits also can be interpreted as short integers which set operating values or modes.
How do you access a register? MicroPython provides a number of ways of doing this but the simplest is to make use of the mem functions in the machine module:
machine.mem32[address] Returns or sets a 32-bit value at the address
machine.mem16[address] Returns or sets a 16-bit value at the address
machine.mem8[address] Returns or sets an 8-bit value at the address
The only difficult part is in working out the address you need to use and the value that sets or resets the bits you need to modify.
For example, if you look in the documentation you will find that the GPIO registers start at address 0x40014000. The registers are defined by their offset from this starting address. So for example, the table of GPIO registers is:
Offset
Register Name
Description
0x000
GPIO0_STATUS
GPIO status
0x004
GPIO0_CTRL
GPIO control including function select and overrides
0X008
GPIO1_STATUS
GPIO status
0x00c
GPIO1_CTRL
GPIO control including function select and overrides
… and so on down to
0x0ec
GPIO29_CTRL
GPIO control including function select and overrides
You can see that there are two registers for each GPIO line from GP0 to GP29, one control register and one status register.
Each register has the same format for each GPIO line. For example, the status register is:
Description
Reset
31:27
Reserved
IRQTOPROC
Interrupt to processors, after override applied
Reserved
IRQFROMPAD
Interrupt from pad, before override applied
23:20
Reserved
INTOPERI
Input signal to peripheral, after override applied
Reserved
INFROMPAD
Input signal from pad, before override applied
16:14
Reserved
OETOPAD
Output enable to pad, after override applied
OEFROMPERI
Output enable from selected peripheral, before override applied
11:10
Reserved
OUTTOPAD
Output signal to pad after override applied
OUTFROMPERI
Output signal from selected peripheral, before override applied
Reserved
You can see that many of the 32 bits in the register are not used, but bit 9 is OUTTOPAD which is the final state of the GPIO line after register overrides have been applied. You can read its current value using:
from machine import mem32 addrGP0Status= 0x40014000 value=mem32[addrGP0Status] print(bin(value))
This prints the current status of GP0 in binary. If you want to find the status of GPn you need to use address 0x40014000+2n. Usually addresses are specified as a base address, i.e. where things start, and an offset that has to be added to the base to get the address of a specific device.
This is the general way you work with peripheral devices such as the PWM units or I2C hardware, but the GPIO is special in that it has another set of registers that control it.
Recommend
-
47
Updated 22-11-17 to include Internet access. Now implements http and https GET requests via GPRS using the SIM800L HTTP AT interface - see below in section on UpyPhone Apps for example applications. UpyPhone...
-
41
Micropython之萝卜狗远程门禁控制系统
-
54
众所周知,iPhone6/6Plus内置气压传感器,不过大家对于气压传感器还是很陌生。跟字面的意思一样,气压传感器就是用来测量气压的,但测量气压对于普通的手机用户来说又有什么作用呢? 海拔高度测量 对于喜欢登山的人...
-
39
0x00 前言 之前看到一篇文章是关于TPYBoard v102控制OLED屏显示的,看到之后就想尝试一下使用OLED屏来显示中文。最近利用空余时间搞定了这个实验,特此将实验过程及源码分享出来,方便以后使用。 0x01 实验器材...
-
129
README.md Micropython based on the Sipeed Maix one platform
-
42
The machine has 8MB PSRAM so it can run micropython: https://docs.micropython.org/en/latest/esp32/tutorial/intro.html Videos of it in action: Network terminal: https://www.youtube.com/watch?v=n5c27-y5tm4...
-
13
Are you interested in the Internet of Things, home automation, and connected devices? Have you ever wondered what it would be like to build a blaster, a laser sword, or even your own robot? If so, then you’re in luck!
-
10
Pimoroni Pico Libraries and Examples Welcome to the brave new world of Pico! This repository contains the C/C++ and MicroPython libraries for our range of Raspberry Pi Pico addons. First of all you need to decide if your proje...
-
2
A Beginner's Guide to MicroPython With the Raspberry Pi Pico By Cherie Tan Published 21 hours ago Want to learn more about the Rasp...
-
6
Interview with Damien George, MicroPython creator MicroPython: “A familiar high-level language combined with the ability to directly utilise a microcontroller”
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK