Author Topic: micropython SPI syntax problem for nodemcu  (Read 676 times)

0 Members and 1 Guest are viewing this topic.

Offline john23Topic starter

  • Regular Contributor
  • *
  • Posts: 129
  • Country: aq
micropython SPI syntax problem for nodemcu
« on: August 17, 2024, 04:59:34 am »
Hello, I am trying to communicate with DAC8004 using SPI and thonny micropython.
from the manufacturer manual the periphery is pins 12-15.

However when i try to run the following code of the manual, I get an error shown below that says thatI have a problem in the SPI command.
Where did i go wrong implementing this command?
Thanks.
import machine
from machine import SPI, Pin
from machine import Pin
from time import sleep

cs=machine.Pin(15,machine.Pin.OUT)
spi=machine.SPI(0,baudrate=1000000,polarity=0,phase=0,bits=8,firstbit=machine.SPI.MSB,
                sck=machine.Pin(14),
                mosi=machine.Pin(13),
                miso=machine.Pin(12))

ERROR:
Traceback (most recent call last):
File "<stdin>", line 10, in <module>
ValueError:
>>>

https://docs.micropython.org/en/latest/esp8266/quickref.html#hardware-spi-bus
https://www.espressif.com/sites/default/files/documentation/0a-esp8266ex_datasheet_en.pdf

 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 6664
  • Country: ro
Re: micropython SPI syntax problem for nodemcu
« Reply #1 on: August 17, 2024, 08:08:27 am »
You are importing Pin from the machine module twice, maybe that's why the error.  :-//
I would remove the 3rd line completely (remove from machine import Pin), then try again.

Offline john23Topic starter

  • Regular Contributor
  • *
  • Posts: 129
  • Country: aq
Re: micropython SPI syntax problem for nodemcu
« Reply #2 on: August 17, 2024, 09:52:30 am »
Hello, I need to send the following command for my DAC8004.
0000 0011 0000 1111 1111 1111 1111 0000
Its 32bit command.So i need to lower CS send 4 times 8 bits and raise CS.
from the manual below I see a sample of command spi.write(b"12345678").
The problem with this command is that its not binary.

In C i have written the following variables.
uint8_t B7_B0=0b11110000; //first 4 are dont care,first 4 bits data
uint8_t B15_B8=0b11111111;//data bits
uint8_t B23_B16=0b00000000;//channel A DATA total 0000 1111 1111 1111
uint8_t B31_B24=0b00000011;//write to buffer and update DAC ,Write /rest dont care
How do i send these variables in micropython?
Thanks.

https://docs.micropython.org/en/latest/library/machine.SPI.html
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3247
  • Country: us
Re: micropython SPI syntax problem for nodemcu
« Reply #3 on: August 17, 2024, 11:04:51 am »
Try:

spi.write(b"\xf0")

or:

spi.write((0xf0).to_bytes(1,"big")) // or maybe "little"

or:

Code: [Select]
buf=bytearray(1)
spi.readinto(buf, 0xf0)

 

Offline john23Topic starter

  • Regular Contributor
  • *
  • Posts: 129
  • Country: aq
Re: micropython SPI syntax problem for nodemcu
« Reply #4 on: August 17, 2024, 11:34:31 am »

Hello , I have built the following code and defined hspi.
I defined buf2[0] but as you can see below i cant send it.
Where di i go wrong sending this variable?


*************************************
import machine
from machine import Pin, SPI
from time import sleep
hspi = SPI(1, baudrate=1000000, polarity=0, phase=0)
#hspi = SPI(1)  # sck=14 (scl), mosi=13 (sda), miso=12 (unused)
dc = Pin(4)    # data/command
rst = Pin(5)   # reset
cs = Pin(15)   # chip select, some modules do not have a pin for this
cs.value(0)

cs.value(1)



***********************************************
>>> hspi
HSPI(id=1, baudrate=1000000, polarity=0, phase=0)
>>> buf2[0]
3
>>> hspi.write(buf[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object with buffer protocol required
>>>
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3247
  • Country: us
Re: micropython SPI syntax problem for nodemcu
« Reply #5 on: August 17, 2024, 11:51:00 am »
buf is a bytearray

buf[0] is a byte

the .write method needs a bytearray not a byte
 

Offline john23Topic starter

  • Regular Contributor
  • *
  • Posts: 129
  • Country: aq
Re: micropython SPI syntax problem for nodemcu
« Reply #6 on: August 17, 2024, 06:14:31 pm »
Hello,my command is, SPI is defined as MSB first. did i send the data properly?
0000 0011 0000 1111 1111 1111 1111 0000
MSB                                                       LSB


import machine
from machine import Pin, SPI
from time import sleep
hspi = SPI(1, baudrate=1000000, polarity=0, phase=0)
#hspi = SPI(1)  # sck=14 (scl), mosi=13 (sda), miso=12 (unused)
buf = bytearray(4)
buf[0] = 0b11110000  # B7_B0
buf[1] = 0b11111111  # B15_B8
buf[2] = 0b00000000  # B23_B16
buf[3] = 0b00000011  # B31_B24

dc = Pin(4)    # data/command
rst = Pin(5)   # reset
cs = Pin(15)   # chip select, some modules do not have a pin for this
cs.value(0)
# Send the binary byte over SPI
hspi.write(buf[3:4])  # Send B31_B24
hspi.write(buf[2:3])  # Send B23_B16
hspi.write(buf[1:2])  # Send B15_B8
hspi.write(buf[0:1])  # Send B7_B0
cs.value(1)
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3247
  • Country: us
Re: micropython SPI syntax problem for nodemcu
« Reply #7 on: August 17, 2024, 08:33:29 pm »
Since you are writing the four bytes all at once you can just do:

spi.write(b"\x03\x00\xff\xf0")

or put them in the correct order to begin with and issue a single write call:

buf = bytearray(4)
buf[0] = 3
buf[1] = 0
buf[2] = 255
buf[3] = 240

spi.write(buf)

I would expect the SPI write call to send the MSB first (as a default) but you'll have to check that yourself with a scope or logic analyzer.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf