I want to transmit card ID information of RFID RC522 module to beaglebone board through SPI communication. At this time, in order to activate each other's devices, I added the following code to open the SCK, SS, MISO, MOSI pins to save and close the output, direction, and value.
Is it correct to add code to directly open and close pins like this to send and receive two devices?
#define SPI_DEVICE "/dev/spidev9.0" // SPI device path (X: bus number, Y: device number)
#define UART_DEVICE "/dev/ttyS0" // UART device path
#define SS_PIN 328 // SPI SS(Chip Select) GPIO pin number
#define MOSI_PIN 339
#define MISO_PIN 340
#define SCK_PIN 338
int main() {
// initialize SPI communication
if (SpiInit() != 0) {
return -1;
}
// set SS pin
int ss_gpio_fd; // fd: A file descriptor, an abstract value used to access a specific file
char ss_gpio_path[64];
// write value to SS pin
snprintf(ss_gpio_path, sizeof(ss_gpio_path), "/sys/class/gpio/gpio%d/value", SS_PIN);
ss_gpio_fd = open(ss_gpio_path, O_WRONLY);
if (ss_gpio_fd < 0) {
printf("Failed to open SS value file\n");
return -1;
}
write(ss_gpio_fd, "1", 1); // high
// Write value to SS pin (set to 0)
write(ss_gpio_fd, "0", 1); // low
// set SS direction
snprintf(ss_gpio_path, sizeof(ss_gpio_path), "/sys/class/gpio/gpio%d/direction", SS_PIN);
ss_gpio_fd = open(ss_gpio_path, O_WRONLY); // O_WRONLY : Open file write-only flag
if (ss_gpio_fd < 0) {
printf("Failed to open SS direction file\n");
return -1;
}
// set SS pin as output
write(ss_gpio_fd, "out", 3); // or "in"
close(ss_gpio_fd);
// MOSI pin settings
int mosi_gpio_fd;
char mosi_gpio_path[64];
// set MOSI direction
snprintf(mosi_gpio_path, sizeof(mosi_gpio_path), "/sys/class/gpio/gpio%d/direction", MOSI_PIN);
mosi_gpio_fd = open(mosi_gpio_path, O_WRONLY);
if (mosi_gpio_fd < 0) {
printf("Failed to open MOSI direction file\n");
return -1;
}
// set the MOSI pin as an output
write(mosi_gpio_fd, "out", 3);
// write value to MOSI pin
snprintf(mosi_gpio_path, sizeof(mosi_gpio_path), "/sys/class/gpio/gpio%d/value", MOSI_PIN);
mosi_gpio_fd = open(mosi_gpio_path, O_WRONLY);
if (mosi_gpio_fd < 0) {
printf("Failed to open MOSI value file\n");
return -1;
}
write(mosi_gpio_fd, "1", 1);
// close MOSI pin
close(mosi_gpio_fd);
// set MISO pin
int miso_gpio_fd;
char miso_gpio_path[64];
// set MISO direction
snprintf(miso_gpio_path, sizeof(miso_gpio_path), "/sys/class/gpio/gpio%d/direction", MISO_PIN);
miso_gpio_fd = open(miso_gpio_path, O_WRONLY);
if (miso_gpio_fd < 0) {
printf("Failed to open SCK direction file\n");
return -1;
}
// set MISO pin as input
write(miso_gpio_fd, "in", 2);
// write value to MISO pin
snprintf(miso_gpio_path, sizeof(miso_gpio_path), "/sys/class/gpio/gpio%d/value", MISO_PIN);
miso_gpio_fd = open(miso_gpio_path, O_WRONLY);
if (miso_gpio_fd < 0) {
printf("Failed to open MISO value file\n");
return -1;
}
write(miso_gpio_fd, "1", 1);
// close MISO pin
close(miso_gpio_fd);
// set SCK pin
int sck_gpio_fd;
char sck_gpio_path[64];
// set SCK direction
snprintf(sck_gpio_path, sizeof(sck_gpio_path), "/sys/class/gpio/gpio%d/direction", SCK_PIN);
sck_gpio_fd = open(sck_gpio_path, O_WRONLY);
if (sck_gpio_fd < 0) {
printf("Failed to open SCK direction file\n");
return -1;
}
// set SCK pin as output
write(sck_gpio_fd, "out", 3);
// write value to SCK pin
snprintf(sck_gpio_path, sizeof(sck_gpio_path), "/sys/class/gpio/gpio%d/value", SCK_PIN);
sck_gpio_fd = open(sck_gpio_path, O_WRONLY);
if (sck_gpio_fd < 0) {
printf("Failed to open SCK value file\n");
return -1;
}
write(sck_gpio_fd, "1", 1);
// close SCK pin
close(sck_gpio_fd);