Skip to content
Snippets Groups Projects
README.md 3.08 KiB
Newer Older
Rob Hart's avatar
Rob Hart committed
# MicroPython-RP2040



## Getting started

Rob Hart's avatar
Rob Hart committed
Notes on Micropython for RP2040.
Rob Hart's avatar
Rob Hart committed

Rob Hart's avatar
Rob Hart committed
Using xiao board.
Rob Hart's avatar
Rob Hart committed

Rob Hart's avatar
Rob Hart committed
Using PyCharm Micropython plugin. [Instructions here.](https://themachineshop.uk/getting-started-with-the-pi-pico-and-pycharm/) This works well.
Rob Hart's avatar
Rob Hart committed

Rob Hart's avatar
Rob Hart committed
[Quick reference for R2040 on Micropython site.](https://docs.micropython.org/en/latest/rp2/quickref.html#:~:text=RP2040%20has%20five%20ADC%20channels,range%20is%200%2D3.3V.)
Rob Hart's avatar
Rob Hart committed

Rob Hart's avatar
Rob Hart committed
Nice examples for output, PWM, and ADC.
Rob Hart's avatar
Rob Hart committed

Rob Hart's avatar
Rob Hart committed
SPI in MicroPython for adxl343 [from Digikey](https://www.digikey.be/en/maker/projects/raspberry-pi-pico-rp2040-spi-example-with-micropython-and-cc/9706ea0cf3784ee98e35ff49188ee045)
Rob Hart's avatar
Rob Hart committed

Rob Hart's avatar
Rob Hart committed
Pins on the adxl343 correspont to pins on the RP2040:
SCL = SCK P2, clock
SDA = MOSI P3, master TX
SDO = MISO P4, (Slave data out) Master RX
CS = Chip Select.  P1
Rob Hart's avatar
Rob Hart committed

Rob Hart's avatar
Rob Hart committed
This works, using alternate SPI0 pins.  Note that for each of two hardware SPI, there are three pin configs.
Rob Hart's avatar
Rob Hart committed

Rob Hart's avatar
Rob Hart committed
For representation of acceleration, check [adxl343 datasheet](https://www.analog.com/media/en/technical-documentation/data-sheets/adxl343.pdf)
Rob Hart's avatar
Rob Hart committed

Rob Hart's avatar
Rob Hart committed
According to the datasheet protocol, a read is done by 
 - CS low
 - Send register address, with bit 7 high for read, bit 6 optionally high for multiple bytes.
 - Read n bytes.
 - CS high.  (deassert).
Rob Hart's avatar
Rob Hart committed

Rob Hart's avatar
Rob Hart committed
In this program, data is read sequentially from six registers, using the SPI.read(n) function.  The sequential register reading is described on p13 of the adxl343 datasheet.  It requires setting the sixth bit of the register address high:  "To read or write multiple bytes in a single transmission, the multiple-byte bit, located after the R/W bit in the first byte transfer
(MB in Figure 27 to Figure 29), must be set. After the register
addressing and the first byte of data, each subsequent set of clock
pulses (eight clock pulses) causes the ADXL343 to point to the next
register for a read or write. This shifting continues until the clock
pulses cease and CS is deasserted."
Rob Hart's avatar
Rob Hart committed

Rob Hart's avatar
Rob Hart committed
[For diagram of the sequential reading of registers to get the six bytes needed for x,y, and z:](https://www.analog.com/media/en/technical-documentation/application-notes/AN-1077.pdf)
Rob Hart's avatar
Rob Hart committed

Rob Hart's avatar
Rob Hart committed
### ADXL343 Software SPI

Software SPI implemented to allow more direct routing of traces.  
[Code is here](./code/Soft_SPI_axdl343_RGB.Jan25.py).  Eagle Project [files are in CAD directory.](./cad)


Rob Hart's avatar
Rob Hart committed
## 23K256 memory chip as an example of SPI
Rob Hart's avatar
Rob Hart committed

Rob Hart's avatar
Rob Hart committed
The 23K256 is a 32KByte memory device with a limited set of commands and registers.  Good exercise for doing SPI at a low-ish level, using the MicroPython machine functions and referring to eh chip's datasheet.

Rob Hart's avatar
Rob Hart committed
[Datasheet for 23K256:](https://ww1.microchip.com/downloads/en/DeviceDoc/22100F.pdf)
Rob Hart's avatar
Rob Hart committed

Rob Hart's avatar
Rob Hart committed
Set Polarity equal to zero for this chip.  This is the level of the idle clock.  See [SPI spec for MicroPython](https://docs.micropython.org/en/latest/library/machine.SPI.html)
Rob Hart's avatar
Rob Hart committed

[Here is a code that works](./code/SPI_23K256/SPI_23K256.py) to store and retrieve numbers from the 23K256.  All operations are written out.  Next step would be to put them in functions and create a library file for this chip. 

This is a nice example of a simple but non-trivial protocol for SPI.