Skip to content

Commit a60ffff

Browse files
committed
m
1 parent 1f20b8f commit a60ffff

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

spi_master.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from machine import SPI
2+
from machine import Pin
3+
import os
4+
import sys
5+
import time
6+
7+
def log(fct):
8+
last = None
9+
while True:
10+
v = fct()
11+
if v != last:
12+
print(time.time(), v)
13+
last = v
14+
time.sleep(1)
15+
16+
p_mosi = 'P11'
17+
p_miso = 'P14'
18+
# p_miso = 'P16' # ... doesn't work ... why? on a gpy
19+
p_clk = 'P10'
20+
# p_cs = 'P3'
21+
22+
# KSZ8851
23+
p_mosi = 'P23'
24+
p_miso = 'P17'
25+
p_clk = 'P21'
26+
27+
# esp32 HSPI
28+
p_mosi = 'P10' # gpio013
29+
p_miso = 'P9' # gpio012
30+
p_clk = 'P23' # gpio014
31+
32+
33+
34+
# cs = Pin(p_cs, mode=Pin.OUT, value=1)
35+
# clk = Pin(p_clk, mode=Pin.OUT, value=0)
36+
# mosi = Pin(p_mosi, mode=Pin.OUT)
37+
# miso = Pin(p_miso, mode=Pin.IN, pull=Pin.PULL_UP)
38+
39+
# CLK, MOSI MISO
40+
# spi_pins = ('P22', "P11", "P16")
41+
spi_pins = (p_clk, p_mosi, p_miso)
42+
43+
speeds = [ 10_000, 100_000, 1_000_000, 10_000_000, 11_000_000, 12_000_000, 13_000_000, 15_000_000, 20_000_000, 40_000_000 ]
44+
speeds = [ 10_000_000, 11_000_000, 11_250_000, 11_375_000, 11_500_000, 12_000_000, 13_000_000]
45+
ok = [0] * len(speeds)
46+
fail = [0] * len(speeds)
47+
# br = 20_000_000 # 20 M
48+
# br = 10_000_000 # 10 M
49+
# br = 2_000_000 # 2 M
50+
# br = 1_000_000 # 1 M
51+
br = 200_000 # 200 K
52+
# br = 100_000 # 100 K
53+
# br = 10_000 # 10 K
54+
buffer_w = bytearray([0x1, 0x2, 0x3, 0x1,0x2,0x3, 0x1,0x2,0x3, 0x66])
55+
# buffer_w = bytearray([0xa,0xa,0xa, 0xa,0xa,0xa, 0xa,0xa,0xa, 0])
56+
#buffer_w = bytearray([0xa,0xe,0xa, 0xe,0xa,0xe, 0xa,0xe,0xa, 0])
57+
buffer_r = bytearray(len(buffer_w))
58+
while True:
59+
for s in range(len(speeds)):
60+
spi = SPI(0, SPI.MASTER, baudrate=speeds[s], polarity=0, phase=0, pins=spi_pins)
61+
for i in range(10):
62+
# cs(0)
63+
l = spi.write_readinto(buffer_w, buffer_r)
64+
# cs(1)
65+
# print(buffer_r)
66+
# print(buffer_r == buffer_w)
67+
if buffer_r == buffer_w:
68+
ok[s] += 1
69+
else:
70+
fail[s] += 1
71+
time.sleep(0.01)
72+
for s in range(len(speeds)):
73+
print("{}:{}/{}".format(speeds[s],ok[s],fail[s]), end=' ')
74+
print()
75+
76+
sys.exit()
77+
log(miso)

0 commit comments

Comments
 (0)