Rasberry PI PICO & PYTHON (LIB, SOFT, и т.д)

ua6em
ua6em аватар
Offline
Зарегистрирован: 17.08.2016

В этой теме предлагаю делиться опытом в программировании на Питоне для RP2040, используемом программном обеспечении, проверенными библиотеками для широкого спектра модулей.

ua6em
ua6em аватар
Offline
Зарегистрирован: 17.08.2016
ua6em
ua6em аватар
Offline
Зарегистрирован: 17.08.2016

Пример оверклокинга:
 

from machine import Pin
import utime
import framebuf
import math
from ST7735 import TFT
from sysfont import sysfont
from machine import SPI,Pin
from rp2 import PIO, StateMachine, asm_pio
from micropython import const
from uctypes import addressof
import time

# Possibility to change the system clock freq if needed - 125MHz (default/False) or 250MHz (True)
OVCLK=True

# Routine to boost system clock

@micropython.viper
def set_freq(fclock:int)->int:
    #clock frequency to run the pico default 125MHz. Allow 100-250
    if (fclock<100000000 or fclock>250000000):
        print("invalid clock speed",fclock)
        print("Clock speed must be set between 100MHz and 250MHz")
        return
    if fclock<=130000000:
        FBDIV=fclock//1000000
        POSTDIV1=6  #default 6
        POSTDIV2=2  #default 2
    else: 
        FBDIV=fclock//2000000
        POSTDIV1=3  #default 6
        POSTDIV2=2  #default 2
    ptr32(0x4002800c)[0] = (POSTDIV1<<16)|(POSTDIV2<<12)
    ptr32(0x40028008)[0] = FBDIV
    cs=FBDIV*12//(POSTDIV1*POSTDIV2)
    print('clock speed',cs,'MHz')

if OVCLK:
    set_freq(250000000) # 
    SPI_FREQ = 25000000
else:
    set_freq(125000000)
    SPI_FREQ = 50000000

WIDTH  = 160     # tft display width
HEIGHT = 128     # tft display height

"""
// для RP2040
#define TFT_CS         GP5 - CS 
#define TFT_RST        GP6 - RESET
#define TFT_DC         GP7 - A0
#define TFT_SCK        GP2 - SCK
#define TFT_SDA        GP3 - SDA (MOSI, TX) 
#define TFT_LED        Vout
#define TFT_VCC	       3V3 - 3V3
#define TFT_GND	       GND - GND
"""

spi = SPI(0, baudrate=SPI_FREQ, polarity=0, phase=0,
          sck=Pin(2), mosi=Pin(3), miso=None)
tft=TFT(spi,7,6,5)   #(CLK, DC, RESET, CS)
tft.initg()
tft.rgb(True)
tft.rotation(3)
tft.fill(TFT.BLACK)
tft.text((38, 30), "CTAPT:", TFT.YELLOW, sysfont, 3, nowrap=True)
utime.sleep(3)
tft.fill(TFT.BLACK)
tft.text((12, 30), "Distance:", TFT.YELLOW, sysfont, 3, nowrap=False)
   
trigger = Pin(15, Pin.OUT)
echo = Pin(14, Pin.IN)

def get_distance():
   trigger.low()
   utime.sleep_us(2)
   trigger.high()
   utime.sleep_us(5)
   trigger.low()
   while echo.value() == 0:
       signaloff = utime.ticks_us()
   while echo.value() == 1:
       signalon = utime.ticks_us()
   timepassed = signalon - signaloff
   distance = (timepassed * 0.0343) / 2
   print("The distance from object is ",distance,"cm")
   return distance
while True:
   #tft.fill(TFT.BLACK);
   ret_val = get_distance()
   #tft.text((12, 30), "Distance:", TFT.YELLOW, sysfont, 3, nowrap=True)
   tft.text((22, 60), str(ret_val) + " cm", TFT.YELLOW, sysfont, 2, nowrap=True)   
   utime.sleep(2)
   tft.text((22, 60), str(ret_val) + " cm", TFT.BLACK, sysfont, 2, nowrap=True)