1
0
mirror of https://github.com/whowechina/aic_pico.git synced 2024-11-12 00:40:47 +01:00

More friendly as a library

This commit is contained in:
whowechina 2024-03-16 16:06:35 +08:00
parent 1461e63243
commit edcc65c998
7 changed files with 29 additions and 39 deletions

Binary file not shown.

View File

@ -35,6 +35,8 @@ typedef struct {
/* should init or attach i2c and spi port before init */
void nfc_attach_i2c(i2c_inst_t *port);
void nfc_attach_spi(spi_inst_t *port, uint8_t rst, uint8_t nss, uint8_t busy);
void nfc_init_i2c(i2c_inst_t *port, uint8_t scl, uint8_t sda, uint32_t freq);
void nfc_init_spi(spi_inst_t *port, uint8_t miso, uint8_t sck, uint8_t mosi,
uint8_t rst, uint8_t nss, uint8_t busy);

View File

@ -74,9 +74,6 @@ struct {
static struct {
i2c_inst_t *port;
uint32_t freq;
uint8_t scl;
uint8_t sda;
} i2c = {0};
void nfc_attach_i2c(i2c_inst_t *port)
@ -86,42 +83,48 @@ void nfc_attach_i2c(i2c_inst_t *port)
void nfc_init_i2c(i2c_inst_t *port, uint8_t scl, uint8_t sda, uint32_t freq)
{
i2c.port = port;
i2c.freq = freq;
i2c.scl = scl;
i2c.sda = sda;
i2c_init(port, freq);
gpio_set_function(scl, GPIO_FUNC_I2C);
gpio_set_function(sda, GPIO_FUNC_I2C);
gpio_pull_up(scl);
gpio_pull_up(sda);
nfc_attach_i2c(port);
}
static struct {
spi_inst_t *port;
uint8_t miso;
uint8_t sck;
uint8_t mosi;
uint8_t rst;
uint8_t nss;
uint8_t busy;
} spi = {0};
void nfc_init_spi(spi_inst_t *port, uint8_t miso, uint8_t sck, uint8_t mosi,
uint8_t rst, uint8_t nss, uint8_t busy)
void nfc_attach_spi(spi_inst_t *port, uint8_t rst, uint8_t nss, uint8_t busy)
{
spi.port = port;
spi.miso = miso;
spi.sck = sck;
spi.mosi = mosi;
spi.rst = rst;
spi.nss = nss;
spi.busy = busy;
}
void nfc_init_spi(spi_inst_t *port, uint8_t miso, uint8_t sck, uint8_t mosi,
uint8_t rst, uint8_t nss, uint8_t busy)
{
spi_init(port, 2000 * 1000);
spi_set_format(port, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
gpio_set_function(miso, GPIO_FUNC_SPI);
gpio_set_function(sck, GPIO_FUNC_SPI);
gpio_set_function(mosi, GPIO_FUNC_SPI);
nfc_attach_spi(port, rst, nss, busy);
}
void nfc_init()
{
if (i2c.port &&
pn532_init(i2c.port, i2c.scl, i2c.sda, i2c.freq)) {
if (i2c.port && pn532_init(i2c.port)) {
nfc_module = NFC_MODULE_PN532;
} else if (spi.port &&
pn5180_init(spi.port, spi.miso, spi.sck, spi.mosi,
spi.rst, spi.nss, spi.busy)) {
} else if (spi.port && pn5180_init(spi.port, spi.rst, spi.nss, spi.busy)) {
nfc_module = NFC_MODULE_PN5180;
}
}

View File

@ -35,16 +35,8 @@ static uint8_t gpio_rst;
static uint8_t gpio_nss;
static uint8_t gpio_busy;
bool pn5180_init(spi_inst_t *port, uint8_t miso, uint8_t sck, uint8_t mosi,
uint8_t rst, uint8_t nss, uint8_t busy)
bool pn5180_init(spi_inst_t *port, uint8_t rst, uint8_t nss, uint8_t busy)
{
spi_init(port, 2000 * 1000);
spi_set_format(port, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
gpio_set_function(miso, GPIO_FUNC_SPI);
gpio_set_function(sck, GPIO_FUNC_SPI);
gpio_set_function(mosi, GPIO_FUNC_SPI);
gpio_init(nss);
gpio_set_dir(nss, GPIO_OUT);
gpio_pull_up(nss);

View File

@ -23,8 +23,7 @@ typedef void (*pn5180_wait_loop_t)();
void pn5180_set_wait_loop(pn5180_wait_loop_t loop);
bool pn5180_init(spi_inst_t *port, uint8_t miso, uint8_t sck, uint8_t mosi,
uint8_t rst, uint8_t nss, uint8_t busy);
bool pn5180_init(spi_inst_t *port, uint8_t rst, uint8_t nss, uint8_t busy);
void pn5180_write_reg(uint8_t reg, uint32_t v32);
void pn5180_or_reg(uint8_t reg, uint32_t mask);

View File

@ -26,14 +26,8 @@
static i2c_inst_t *i2c_port = i2c0;
bool pn532_init(i2c_inst_t *i2c, uint8_t scl, uint8_t sda, uint32_t freq)
bool pn532_init(i2c_inst_t *i2c)
{
i2c_init(i2c, freq);
gpio_set_function(scl, GPIO_FUNC_I2C);
gpio_set_function(sda, GPIO_FUNC_I2C);
gpio_pull_up(scl);
gpio_pull_up(sda);
i2c_port = i2c;
uint32_t ver = pn532_firmware_ver();

View File

@ -14,7 +14,7 @@ typedef void (*pn532_wait_loop_t)();
void pn532_set_wait_loop(pn532_wait_loop_t loop);
bool pn532_init(i2c_inst_t *i2c_port, uint8_t scl, uint8_t sda, uint32_t freq);
bool pn532_init(i2c_inst_t *i2c);
int pn532_write_command(uint8_t cmd, const uint8_t *param, uint8_t len);
int pn532_read_response(uint8_t cmd, uint8_t *resp, uint8_t len);