machine.SPI.write() taking too long?

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: machine.SPI.write() taking too long?

Post by Roberthh » Thu May 19, 2016 3:52 pm

IMHO, DMA would only help for transferring larger chunks of data at high SPI speed because then the transfer from memory to the SPI controller would not be done by code in the SPI driver. But that does not seem to be the limiting factor here. In your case, the speed is limited by the time needed for setting up the call in the python environment to send 2 bytes of data.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: machine.SPI.write() taking too long?

Post by dhylands » Thu May 19, 2016 5:33 pm

To get the time between your CS switch and the SPI write to be decently fast there are really only 2 solutions:

1 - have the driver do it. In my mind this is a perfectly reasonable extension (being able to specify a CS pin)
2 - do it in assembler. I would up doing it this way to get fast switching the UART from Tx to Rx mode (see: https://github.com/dhylands/bioloid3/bl ... ort.py#L60)
3 - (ok I can't count) - not sure about this, but viper might help here.

danielm
Posts: 167
Joined: Mon Oct 05, 2015 12:24 pm

Re: machine.SPI.write() taking too long?

Post by danielm » Wed May 25, 2016 4:50 pm

Thanks a lot for your answers and opinions. I now understand that DMA will not help in my case.

Entire problem is a little bit more difficult, because it is not just about calling machine.SPI.write() on a large list of data and switching CS pin in between each two bytes.

I need to do this sequential SPI writing in my laser 3D printer controller project:
http://forum.micropython.org/viewtopic.php?f=5&t=966

I have a G-code file (with my own command interpretation) exported by my cloud component (3D object slicer). It can be downloaded to local file system of my MP host based on CC3200, then opened, read line by line and parsed.
Basically two command related to X-Y axis are possible:
- move to X,Y
- scan to X,Y
First one directly sets new coordinates by transmiting proper payload value to 2-channel SPI DAC (2-bytes for X coordinate and 2 bytes for Y-coordiate). Second one calculates all (integer) coordinates between current and target coordinates on a straight line and step by step moves to target coordinates by sequential sending of SPI data.

The goal is to maintain fixed time period between setting coordinates step-by-step either within one "scan to" command and also between two commands. I would like to achieve 20000 X-Y coordinates per second as this is performance of typical scanning galvanometer set. Total number of coordinates transmitted during print of one 3D object may be in order of hundreds of milions and the printing process may take hours or even several tens of hours in extreme cases.

With pure MP implementation I can achieve approx. 2500-3000 X-Y coordinates per second (on CC3200).

I now see two options:
1. wait for more powerful HW platform than CC3200, that could handle to do it fast enough with pure MP implementation (maybe ESP32 but I doubt it).
2. write my own module in C that would open G-code file, do the parsing, calculate coordinates for "scan to" command and than write to my SPI DAC - this module would need to be written for each HW platform separately

I saw that Dave suggested to follow source of existing modules (e.g. I2C in this thread http://forum.micropython.org/viewtopic.php?f=3&t=1858) if one wants to call C function from MP. Is there any other how-to document or tutorial how to write custom MP module in C?
Let's say I would be able to do it - do I then always need to recompile MP image to incorporate my custom module into the build or is there any way how to add "3rd party" module writen in C to my MP host? E.g. by storing pre-compiled library in the file system and then performing import?

And I should also test viper code emitter as Dave suggested :)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: machine.SPI.write() taking too long?

Post by dhylands » Wed May 25, 2016 5:29 pm

You may want to look at this thread:
http://forum.micropython.org/viewtopic. ... 8740#p8740

I put together a C sample module where C calls python and python calls C.

danielm
Posts: 167
Joined: Mon Oct 05, 2015 12:24 pm

Re: machine.SPI.write() taking too long?

Post by danielm » Thu May 26, 2016 1:50 pm

Thank you, I will take a look at that thread.

Is it possible to import MP module written in C from a file stored in the file system (Flash memory or SD-card) ?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: machine.SPI.write() taking too long?

Post by dhylands » Thu May 26, 2016 4:03 pm

If you mean that the compiled C code lives on the filesystem, then no that currently isn't possible.

All of the C code needs to be linked into the main micropython code.

If the code on the sdcard is python code, then yes it can import the code written in C (that's contained in the main firmare).

Post Reply