Page 1 of 2

SPI driver improvements

Posted: Tue Aug 29, 2017 2:10 pm
by Marco
Hi,

I found some problems in the AVR SPI driver.

1) There is a bug in spi_lld_start(), the SPI interrupt is enabled before the peripheral has been configured properly. This can cause an unwanted interrupt.

2) For some reason, the MSTR bit gets cleared when set individually when the \SS pin is configured as output and pulled low externally. Setting it together with the SPE bit fixes this.

3) The DDR setting of the pins should not be done inside the driver but outside in the application code.

4) The chip select pin should not be set in the driver but outside in the application code.

5) The driver does not work in case of a re-start with a different configuration.

Attached is a patch fixing these problems.

Marco

Re: SPI driver improvements

Posted: Tue Aug 29, 2017 4:11 pm
by tfAteba
Hi Marco,

Thanks, for the code, I will merge it tonight.

Re: SPI driver improvements

Posted: Wed Aug 30, 2017 11:29 am
by Marco
Hi,

I noticed the spi_lld_start function could be optimized more, like the STM32 port does. The 'intelligence' is removed and the function just writes the configuration to the registers. This saves 1 byte of RAM per configuration structure and roughly 150 bytes of FLASH. Attached is the new patch which replaces the previous one. It contains changes to both the source and header file.

Note that all applications that use the SPI driver should be modified. I can help with this if needed.

The previous configuration was:

Code: Select all

static SPIConfig io_on_cfg =
{
  IOPORT1,
  7,
  SPI_MODE_0,
  SPI_MSB_FIRST,
  SPI_SCK_FOSC_2,
  NULL
};


The new configuration is:

Code: Select all

static SPIConfig io_on_cfg =
{
  NULL,
  IOPORT1,
  7,
  SPI_CR_DORD_MSB_FIRST | SPI_CR_CPOL_CPHA_MODE(0) | SPI_CR_SCK_FOSC_2,
  SPI_SR_SCK_FOSC_2
};


Let me know what you think about it.

Marco

Re: SPI driver improvements

Posted: Wed Aug 30, 2017 10:25 pm
by tfAteba
Hi Marco,

Thanks for the modifications, I'm sorry that it take me more than a said to integrate your work. I hope I will have time tomorrow to look at it.
I will merge it if every thing is fine in the trunk for tomorrow ;)

Thanks.

Re: SPI driver improvements

Posted: Thu Aug 31, 2017 11:25 pm
by tfAteba
Marco,

Could you please send me a patch file instead a diff?

It will help me to easily and quickly apply your work.

Thanks

Re: SPI driver improvements

Posted: Thu Aug 31, 2017 11:36 pm
by utzig
Btw, those patches don't seem to apply to latest trunk where AVR has subdirectories for specific families. Even adjusting the paths in the patch, it still fails to merge. Which version are you working with?

Re: SPI driver improvements

Posted: Thu Aug 31, 2017 11:39 pm
by tfAteba
Hi Utzig,

I m working on the trunk version.

Re: SPI driver improvements

Posted: Thu Aug 31, 2017 11:57 pm
by utzig
The question was for Marco! :P

Re: SPI driver improvements

Posted: Fri Sep 01, 2017 3:59 pm
by Marco
Hi,

The patches are based on an older version of trunk, before tfAteba made changes to the structure. The patches were made using git, but they should also apply on svn i guess. I'll just send the whole files so you can replace them to see the diffs. See attached zip.

Hopefully it will be possible to send a pull request on github at some point in the future :P :roll:

Marco

Re: SPI driver improvements

Posted: Fri Sep 01, 2017 10:03 pm
by tfAteba
Marco,

Marco wrote:
Hopefully it will be possible to send a pull request on github at some point in the future :P :roll:

For me it is not a problem to send a pool request next time, but I prefer the old method (post a patch in the forum) because it was define by Giovanni and we have to respect that even if we are not agree whit that. He must have is reasons :)

Any way, thanks for sending me the original file, it will be easy to merge your work. For next time, you can just send a patch even if it is made with git or svn.

Thanks you