elehobica / pico_fatfs

FatFs library for Raspberry Pi Pico

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SPIx as a define

Iangitpers opened this issue · comments

The project currently depends on SPI0. I changed this to a define because I'm using SPI1. Not a big issue, but I'm now out of sync with the master branch. I'd suggest a SPIx neutral naming convention with a define for the spi unit used.

Adding... Just in general, I'm using this as a library on a larger project. I have my own top level sd.c etc. It is nice that you have a fully functional init routine to help people get going. In practice I deleted everything but CSHIGH () from init because I have my own buffered IO stuff to setup and that isn't universal. I notice this init stuff was added to last revision. I'd prefer it remain in the top level main.c for the demo because that seems more flexible for users with different setups.

I've moved board and condition dependency to tf_card.h. Select spi0/1 by USE_SPIx definition.

#define USE_SPI0 // to use PIN_SPI0_xxx
//#define USE_SPI1 // to use PIN_SPI1_xxx

#define CLK_FAST	(50 * MHZ)

/* SPI pin assignment */
#define PIN_SPI0_MISO   4   // 0, 4, 16
#define PIN_SPI0_CS     5   // 1, 5, 17
#define PIN_SPI0_SCK    2   // 2, 6, 18
#define PIN_SPI0_MOSI   3   // 3, 7, 19

#define PIN_SPI1_MISO   8   //  8, 12
#define PIN_SPI1_CS     9   //  9, 13
#define PIN_SPI1_SCK    10  // 10, 14
#define PIN_SPI1_MOSI   11  // 11, 15

Second comment is indicating very good point because I've consider the same when I wrote void init_spi().
It is desirable that IO initialization should be done under user program. At the same time, it will helps if reference setting is available in library.

Therefore, I've set weak attribute on init_spi() in tf_card.c. If you put your own init_spi() on top level, it has priority to be applied.
See "Customize IO buffer init" section in Readme.md.

I did something like this for the tf_card.h defines:

#define TF_CARD_SPI spi1
#define TF_CARD_SPI_CLK 10
#define TF_CARD_SPI_CDO 11
#define TF_CARD_SPI_CDI 8
#define TF_CARD_SPI_CS 9

So I can just set the SPIn and gpios in one go.

It's simple, but we cannot choose SPI1 pins when using spi0. That's why I showed as separated.