sarabhy
Posts: 4
Joined: Mon Jul 06, 2020 1:51 pm

SPI via DMA

Thu Jul 09, 2020 9:52 am

Hello guys,

Is there any python module that support DMA. I want to receive data with SPI in Raspberry PI4 using DMA.

User avatar
joan
Posts: 14887
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: SPI via DMA

Thu Jul 09, 2020 10:33 am

Why? What is wrong with the standard Python module for talking to the SPI hardware?

sarabhy
Posts: 4
Joined: Mon Jul 06, 2020 1:51 pm

Re: SPI via DMA

Thu Jul 09, 2020 11:22 am

joan wrote:
Thu Jul 09, 2020 10:33 am
Why? What is wrong with the standard Python module for talking to the SPI hardware?
I used spidev, turns out that it doesn't support DMA. and i need DMA to remove the delay between bytes and gain time.

User avatar
joan
Posts: 14887
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: SPI via DMA

Thu Jul 09, 2020 11:28 am

If spidev isn't good enough you will have to write your own DMA based driver.

sarabhy
Posts: 4
Joined: Mon Jul 06, 2020 1:51 pm

Re: SPI via DMA

Thu Jul 09, 2020 11:44 am

joan wrote:
Thu Jul 09, 2020 11:28 am
If spidev isn't good enough you will have to write your own DMA based driver.
Well i'm new with Raspberry so i don't know how

jayben
Posts: 38
Joined: Mon Aug 19, 2019 9:56 pm

Re: SPI via DMA

Fri Jul 10, 2020 6:46 pm

Here is an example of SPI DMA programming: https://iosoft.blog/fast-data-capture-raspberry-pi/

However, it is in the C language. I'm not aware of a Python implementation, but if you really need high speed, you may have to code the whole thing in C anyway.

cleverca22
Posts: 581
Joined: Sat Aug 18, 2012 2:33 pm

Re: SPI via DMA

Fri Jul 10, 2020 7:03 pm

i think i saw in the kernel source, that spidev will use dma, of the amount of data is over a certain size limit

edit:

yep: https://github.com/raspberrypi/linux/bl ... 2835.c#L77
any transfer over 96 bytes will use DMA

sarabhy
Posts: 4
Joined: Mon Jul 06, 2020 1:51 pm

Re: SPI via DMA

Mon Jul 13, 2020 11:52 am

cleverca22 wrote:
Fri Jul 10, 2020 7:03 pm
i think i saw in the kernel source, that spidev will use dma, of the amount of data is over a certain size limit

edit:

yep: https://github.com/raspberrypi/linux/bl ... 2835.c#L77
any transfer over 96 bytes will use DMA
hey could you please give me the link that indicates this information. It did work but i want to understand why and i couldn't find the perfect justification. i also want to know if we can change the maximum number of data, i mean is it possible to change 96 ?
Than you in advance it means a lot.

cleverca22
Posts: 581
Joined: Sat Aug 18, 2012 2:33 pm

Re: SPI via DMA

Mon Jul 13, 2020 12:53 pm

if you check the source in that same file, youll find comments like this:

Code: Select all

static bool bcm2835_spi_can_dma(struct spi_master *master,
				struct spi_device *spi,
				struct spi_transfer *tfr)
{
	/* only run for gpio_cs */
	if (!gpio_is_valid(spi->cs_gpio))
		return false;

	/* we start DMA efforts only on bigger transfers */
	if (tfr->len < BCM2835_SPI_DMA_MIN_LENGTH)
		return false;
i think the reason for that, is that you need to write a dma control structure to ram, and then poke some dma control registers to initiate the DMA
and it would be a dumb idea to setup a 32* byte dma control structure, just to "speed up" copying 1 byte of data

not sure exactly why 96 was picked as the limit, but if you dont mind rebuilding the kernel, you can try other limits out


* guessing on the size, just using it as an example

Return to “General discussion”