Skip to content
Snippets Groups Projects
Commit 58184015 authored by michael.divia's avatar michael.divia
Browse files

UART finished

parent 7260a848
Branches
No related tags found
No related merge requests found
from machine import Pin, Uart
from machine import Pin, UART
from neopixel import NeoPixel
from rp2 import PIO, StateMachine, asm_pio
import utime
nb_line = 8
......@@ -268,33 +269,91 @@ def show_text(text, Color, speed):
class Uart:
channel = None
direction = None
def __init__(self, dir, baudrate=9600, parity=None, bits=8, stop=1):
self.direction = dir
if (dir == Direction.NORTH):
self.channel = Uart(0, baudrate=baudrate, tx=Pin(12), rx=Pin(13), parity=parity, bits=bits, stop=stop)
self.channel = UART(0, baudrate=baudrate, tx=Pin(12), rx=Pin(13), parity=parity, bits=bits, stop=stop)
elif (dir == Direction.SOUTH):
self.channel = Uart(1, baudrate=baudrate, tx=Pin(8), rx=Pin(9), parity=parity, bits=bits, stop=stop)
self.channel = UART(1, baudrate=baudrate, tx=Pin(8), rx=Pin(9), parity=parity, bits=bits, stop=stop)
elif (dir == Direction.EAST):
self.tx = StateMachine(0, Uart.uart_tx, freq=8 * baudrate, sideset_base=Pin(14), out_base=Pin(14))
self.tx.active(1)
self.rx = StateMachine(1, Uart.uart_rx, freq=8 * baudrate, in_base= Pin(15, Pin.IN, Pin.PULL_UP), jmp_pin= Pin(15, Pin.IN, Pin.PULL_UP),)
self.rx.active(1)
elif (dir == Direction.WEST):
self.tx = StateMachine(2, Uart.uart_tx, freq=8 * baudrate, sideset_base=Pin(20), out_base=Pin(20))
self.tx.active(1)
self.rx = StateMachine(3, Uart.uart_rx, freq=8 * baudrate, in_base= Pin(21, Pin.IN, Pin.PULL_UP), jmp_pin= Pin(21, Pin.IN, Pin.PULL_UP),)
self.rx.active(1)
else:
raise ValueError("Uart direction does not exist")
self.direction = dir
@asm_pio(sideset_init=PIO.OUT_HIGH, out_init=PIO.OUT_HIGH, out_shiftdir=PIO.SHIFT_RIGHT)
def uart_tx():
# Block with TX deasserted until data available
pull()
# Initialize bit counter, assert start bit for 8 cycles
set(x, 7) .side(0) [7]
# Shift out 8 data bits, 8 execution cycles per bit
label("bitloop")
out(pins, 1) [6]
jmp(x_dec, "bitloop")
# Assert stop bit for 8 cycles total (incl 1 for pull())
nop() .side(1) [6]
@asm_pio(in_shiftdir=PIO.SHIFT_RIGHT,)
def uart_rx():
# fmt: off
label("start")
# Stall until start bit is asserted
wait(0, pin, 0)
# Preload bit counter, then delay until halfway through
# the first data bit (12 cycles incl wait, set).
set(x, 7) [10]
label("bitloop")
# Shift data bit into ISR
in_(pins, 1)
# Loop 8 times, each loop iteration is 8 cycles
jmp(x_dec, "bitloop") [6]
# Check stop bit (should be high)
jmp(pin, "good_stop")
# Either a framing error or a break. Set a sticky flag
# and wait for line to return to idle state.
irq(block, 4)
wait(1, pin, 0)
# Don't push data if we didn't see good framing.
jmp("start")
# No delay before returning to start; a little slack is
# important in case the TX clock is slightly too fast.
label("good_stop")
push(block)
# fmt: on
def pio_uart_print(sm, s):
for c in s:
sm.put(ord(c))
def send(self, data):
if self.direction == Direction.NORTH or self.direction == Direction.SOUTH:
self.channel.write(data)
else:
i = 0
Uart.pio_uart_print(self.tx, data)
def sendline(self, data):
if self.direction == Direction.NORTH or self.direction == Direction.SOUTH:
self.channel.write(data+'\n')
else:
i = 0
Uart.pio_uart_print(self.tx, data+'\n')
def receive(self, length=1):
data = None
......@@ -305,7 +364,9 @@ class Uart:
if data is None:
utime.sleep(0.1)
else:
i = 0
data = ""
for i in range(length):
data += chr(self.rx.get() >> 24)
return data
......@@ -318,9 +379,11 @@ class Uart:
if data is None:
utime.sleep(0.1)
else:
i = 0
data = ""
while not data.endswith("\n"):
data += chr(self.rx.get() >> 24)
return data
return data.rstrip()
def christmas():
......
from hl3 import *
#uart_north = Uart(Direction.NORTH)
#uart_south = Uart(Direction.SOUTH)
uart_east = Uart(Direction.EAST, 9600)
#uart_west = Uart(Direction.WEST)
while True:
christmas()
#uart_east.sendline("Pipo")
received_data = uart_east.receiveline()
#received_data = uart_north.receiveline()
print(f"Data received: {received_data}")
#utime.sleep(0.1)
......@@ -49,13 +49,13 @@ J'ai aussi profité d'implémenter toute les couleurs que nous devions implémen
J'ai ensuite créer un fonction de démo `christmas` afin de démontré le bon fonctionnement des LEDs. Cette fonction est une ré-implémentation de la fonction de M. Gluck que vous pouvez trouver [ici](https://gitedu.hesge.ch/cores/projects/hepialight2/hepialight2-examples/-/blob/02261b68aad94dd52c6b35fdb83ed1d54028061b/peripherals/display/christmas_ball.py).
Suite à cela nous avons, à l'aide de M. Escribano, à implémenter les fonction `UART` suivante : `send`, `snedline`, `receive` et `receiveline` et nous avons commencé à les tester entre 2 cartes mais nous avons eux quelque problème, nous allons donc devoir continuer à investiguer cela demain.
Suite à cela nous avons, avec l'aide de M. Escribano, à implémenter les fonction `UART` suivante : `send`, `snedline`, `receive` et `receiveline` et nous avons commencé à les tester entre 2 cartes mais nous avons eux quelque problème, nous allons donc devoir continuer à investiguer cela demain.
## Mercredi, 19 Juin 2024
A l'aide de M. Escribano nous avons finalisé la correction des fonctions `receive` et `receiveline` afin de pouvoir communiqué en `UART` entre 2 cartes en utilisant les ports prévu à cet effet sur les RPI Pico (`NORTH` et `SOUTH` dans notre code).
Avec l'aide de M. Escribano nous avons finalisé la correction des fonctions `receive` et `receiveline` afin de pouvoir communiqué en `UART` entre 2 cartes en utilisant les ports prévu à cet effet sur les RPI Pico (`NORTH` et `SOUTH` dans notre code).
Nous avons ensuite enchaîner avec la communication `UART` via les `GPIO`.
Nous avons ensuite enchaîné avec la communication `UART` via les `GPIO`, pour cela nous avons utilisé `rp2 PIO`. Nous avons donc adapté tout les fonctions crées précédemment afin de fonctionner avec ces derniers.
# Creators
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment