Initial SPI integration. Known issue: RC522 reports invalid version id.
This commit is contained in:
@@ -45,12 +45,35 @@
|
||||
#include "driver_mfrc522_interface.h"
|
||||
|
||||
#define MFRC522_CFG_RST_GPIO 9
|
||||
#define MFRC522_CFG_SPI_INSTANCE spi1
|
||||
#define MFRC522_CFG_SPI_MISO_GPIO 12
|
||||
#define MFRC522_CFG_SPI_MOSI_GPIO 11
|
||||
#define MFRC522_CFG_SPI_CLK_GPIO 10
|
||||
#define MFRC522_CFG_SPI_SS_GPIO 13
|
||||
#define MFRC522_CFG_SPI_SPEED_MHZ (1000 * 1000)
|
||||
|
||||
/**
|
||||
* @brief Helper function to pull the CS line low
|
||||
*
|
||||
* Taken from pico-examples, bme280_spi.c
|
||||
*/
|
||||
static inline void cs_select() {
|
||||
asm volatile("nop \n nop \n nop");
|
||||
gpio_put(MFRC522_CFG_SPI_SS_GPIO, 0); // Active low
|
||||
asm volatile("nop \n nop \n nop");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Helper function to release the CS line low
|
||||
*
|
||||
* Taken from pico-examples, bme280_spi.c
|
||||
*/
|
||||
static inline void cs_deselect() {
|
||||
asm volatile("nop \n nop \n nop");
|
||||
gpio_put(MFRC522_CFG_SPI_SS_GPIO, 1);
|
||||
asm volatile("nop \n nop \n nop");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief interface reset gpio init
|
||||
* @return status code
|
||||
@@ -164,14 +187,19 @@ uint8_t mfrc522_interface_spi_init(void)
|
||||
gpio_set_function(MFRC522_CFG_SPI_MISO_GPIO, GPIO_FUNC_SPI);
|
||||
gpio_set_function(MFRC522_CFG_SPI_MOSI_GPIO, GPIO_FUNC_SPI);
|
||||
gpio_set_function(MFRC522_CFG_SPI_CLK_GPIO, GPIO_FUNC_SPI);
|
||||
gpio_set_function(MFRC522_CFG_SPI_SS_GPIO, GPIO_FUNC_SPI);
|
||||
//gpio_set_function(MFRC522_CFG_SPI_SS_GPIO, GPIO_FUNC_SPI);
|
||||
//gpio_set_dir(MFRC522_CFG_SPI_CLK_GPIO, GPIO_OUT);
|
||||
|
||||
gpio_init(MFRC522_CFG_SPI_SS_GPIO);
|
||||
gpio_set_dir(MFRC522_CFG_SPI_SS_GPIO, GPIO_OUT);
|
||||
gpio_put(MFRC522_CFG_SPI_SS_GPIO, 1);
|
||||
|
||||
/*
|
||||
* From the manual:
|
||||
* "There is no guarantee that the baudrate requested can be achieved exactly;
|
||||
* the nearest will be chosen and returned"
|
||||
*/
|
||||
return !spi_init(spi0, MFRC522_CFG_SPI_SPEED_MHZ);
|
||||
return !spi_init(MFRC522_CFG_SPI_INSTANCE, MFRC522_CFG_SPI_SPEED_MHZ);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,6 +211,7 @@ uint8_t mfrc522_interface_spi_init(void)
|
||||
*/
|
||||
uint8_t mfrc522_interface_spi_deinit(void)
|
||||
{
|
||||
mfrc522_interface_debug_print("WARN: %s not implemented!\n", __FUNCTION__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -198,7 +227,10 @@ uint8_t mfrc522_interface_spi_deinit(void)
|
||||
*/
|
||||
uint8_t mfrc522_interface_spi_read(uint8_t reg, uint8_t *buf, uint16_t len)
|
||||
{
|
||||
return 0;
|
||||
cs_select();
|
||||
uint16_t n_bytes_read = spi_read_blocking(MFRC522_CFG_SPI_INSTANCE, reg, buf, len);
|
||||
cs_deselect();
|
||||
return (uint8_t) (n_bytes_read == len) ? 0 : 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,7 +245,10 @@ uint8_t mfrc522_interface_spi_read(uint8_t reg, uint8_t *buf, uint16_t len)
|
||||
*/
|
||||
uint8_t mfrc522_interface_spi_write(uint8_t reg, uint8_t *buf, uint16_t len)
|
||||
{
|
||||
return 0;
|
||||
cs_select();
|
||||
uint16_t n_bytes_written = spi_write_blocking(MFRC522_CFG_SPI_INSTANCE, buf, len);
|
||||
cs_deselect();
|
||||
return (uint8_t) (n_bytes_written == len) ? 0 : 1;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user