SPI driver improvements Topic is solved

ChibiOS public support forum for topics related to the Atmel AVR family of micro-controllers.

Moderators: utzig, tfAteba

Marco
Posts: 128
Joined: Tue Apr 16, 2013 8:22 pm
Has thanked: 4 times
Been thanked: 11 times

SPI driver improvements

Postby Marco » Tue Aug 29, 2017 2:10 pm

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
Attachments
hal_spi_lld.zip
(1.05 KiB) Downloaded 26 times

User avatar
tfAteba
Posts: 513
Joined: Fri Oct 16, 2015 11:03 pm
Location: Chartres, France
Has thanked: 75 times
Been thanked: 44 times

Re: SPI driver improvements

Postby tfAteba » Tue Aug 29, 2017 4:11 pm

Hi Marco,

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

Theo.

Marco
Posts: 128
Joined: Tue Apr 16, 2013 8:22 pm
Has thanked: 4 times
Been thanked: 11 times

Re: SPI driver improvements

Postby Marco » Wed Aug 30, 2017 11:29 am

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
Attachments
hal_spi_lld.zip
(2.04 KiB) Downloaded 29 times

User avatar
tfAteba
Posts: 513
Joined: Fri Oct 16, 2015 11:03 pm
Location: Chartres, France
Has thanked: 75 times
Been thanked: 44 times

Re: SPI driver improvements

Postby tfAteba » Wed Aug 30, 2017 10:25 pm

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.
regards,

Theo.

User avatar
tfAteba
Posts: 513
Joined: Fri Oct 16, 2015 11:03 pm
Location: Chartres, France
Has thanked: 75 times
Been thanked: 44 times

Re: SPI driver improvements

Postby tfAteba » Thu Aug 31, 2017 11:25 pm

Marco,

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

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

Thanks
regards,

Theo.

utzig
Posts: 354
Joined: Sat Jan 07, 2012 6:22 pm
Location: Brazil
Has thanked: 1 time
Been thanked: 16 times
Contact:

Re: SPI driver improvements

Postby utzig » Thu Aug 31, 2017 11:36 pm

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?

User avatar
tfAteba
Posts: 513
Joined: Fri Oct 16, 2015 11:03 pm
Location: Chartres, France
Has thanked: 75 times
Been thanked: 44 times

Re: SPI driver improvements

Postby tfAteba » Thu Aug 31, 2017 11:39 pm

Hi Utzig,

I m working on the trunk version.
regards,

Theo.

utzig
Posts: 354
Joined: Sat Jan 07, 2012 6:22 pm
Location: Brazil
Has thanked: 1 time
Been thanked: 16 times
Contact:

Re: SPI driver improvements

Postby utzig » Thu Aug 31, 2017 11:57 pm

The question was for Marco! :P

Marco
Posts: 128
Joined: Tue Apr 16, 2013 8:22 pm
Has thanked: 4 times
Been thanked: 11 times

Re: SPI driver improvements

Postby Marco » Fri Sep 01, 2017 3:59 pm

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
Attachments
hal_spi_lld.zip
(4.65 KiB) Downloaded 28 times

User avatar
tfAteba
Posts: 513
Joined: Fri Oct 16, 2015 11:03 pm
Location: Chartres, France
Has thanked: 75 times
Been thanked: 44 times

Re: SPI driver improvements

Postby tfAteba » Fri Sep 01, 2017 10:03 pm

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
regards,

Theo.


Return to “AVR Support”

Who is online

Users browsing this forum: No registered users and 1 guest