jeremyherbert / mcp2210-python

MCP22100 python driver
6 stars 3 forks source link

Adjustable Bit Rate? #9

Closed ejasta37 closed 2 weeks ago

ejasta37 commented 2 weeks ago

Is there a way to change the bit rate of the SPI transaction?

phoenixofhp commented 2 weeks ago

It can be done in official MCP2210 Utility by Microchip, if someone really need ability to change bitrate by using Python - i can try do add this

ejasta37 commented 2 weeks ago

Thanks for the response! When I run this code, I can change the bit_rate, but it's not very stable. I can bring it down to 10kHz, but anything lower and it gives me the error below. The reason I want to be able to change it is so I can talk to the MCP3564R Del-Sig ADC which has an adjustable bit rate, and its default is 4800Hz. Thanks for your help!

############################################################# import time from mcp2210 import Mcp2210, Mcp2210GpioDesignation, Mcp2210GpioDirection mcp = Mcp2210(serial_number="0001006799") mcp._spi_settings.bit_rate = 4800 mcp._set_spi_configuration()

#############################################################

AssertionError Traceback (most recent call last) Cell In[33], line 8 5 mcp._spi_settings.bit_rate = 4800 7 # Step 4: Apply the changes ----> 8 mcp._set_spi_configuration() 11 mcp.set_spi_mode(0) 12 mcp.set_gpio_designation(1,Mcp2210GpioDesignation.CHIP_SELECT)

File ~\AppData\Roaming\Python\Python310\site-packages\mcp2210\mcp2210.py:565, in Mcp2210._set_spi_configuration(self) 562 response = self._execute_command(bytes(request) + packed) 564 payload = bytes(response[4:21]) --> 565 assert packed == payload 567 logger.debug("MCP2210: SPI settings sent to device: " + str(self._spi_settings))

AssertionError:

phoenixofhp commented 2 weeks ago

Are you sure you need to set the bitrate manually to communicate with your ADC? Its datasheet says that it will safely handle SPI at speeds up to 20MHz. The SPI Slave device adjusts to the frequency of the Master device, so there should be no problem communicating with the ADC you quoted. In any case, if you can't make it work, I can check if it works at reduced frequency on my mcp2210 and try to fix the bug if it occurs.

ejasta37 commented 2 weeks ago

I'll keep looking into it, as I'm still troubleshooting and I'm relatively new to SPI communication. I have been able to communicate with the ADC, but when I try to read back some data, I start getting strange values (various letters/symbols not within 0 - F hex): b'\x13\xff\xcbQ\xff\xcbQ\xff\xcbQ'

I'm not really sure how to send a 1-byte command and get back the 10+ bytes back using the spi_exchange function. I've been trying it like this, but it's clearly not working: rx_data = mcp.spi_exchange(b'\x41'*10, cs_pin_number=1)

phoenixofhp commented 2 weeks ago

Python prints bytes() objects very weird, to print bytes() object in human-readable format use hex() method, example:

b = bytes([0x00, 0xBE, 0xEF])
print(b.hex())

To send 1 byte and get 10 bytes back you need to send empty bytes:

payload = [0x41] + 9 * [0x00]
response = mcp.spi_exchange(bytes(payload), cs_pin_number=1)
print(f'Got response: {response.hex()}')
ejasta37 commented 2 weeks ago

Ahh! I should have known. Ok I'll give that a shot. Thanks for the support!

phoenixofhp commented 2 weeks ago

Ok, report back if it works out and close the Issue

ejasta37 commented 2 weeks ago

Looks like it works, thanks!