Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <stdint.h>
- #include <string.h>
- #include <sys/time.h>
- #include "libsoc_spi.h"
- #include "libsoc_debug.h"
- #define SPI_DEVICE 0
- #define CHIP_SELECT 0
- #define LEN 8
- static uint8_t tx[LEN], rx[LEN];
- // For testing SPI with loopback: MISO connected to MOSI
- int main()
- {
- int i;
- libsoc_set_debug(1);
- spi* spi_dev = libsoc_spi_init(SPI_DEVICE, CHIP_SELECT);
- if (!spi_dev)
- {
- printf("Failed to get spidev device!\n");
- return EXIT_FAILURE;
- }
- // SPI Settings
- libsoc_spi_set_mode(spi_dev, MODE_0);
- libsoc_spi_get_mode(spi_dev);
- libsoc_spi_set_speed(spi_dev, 1000000);
- libsoc_spi_get_speed(spi_dev);
- libsoc_spi_set_bits_per_word(spi_dev, BITS_8);
- libsoc_spi_get_bits_per_word(spi_dev);
- // set TX buffer to 0,1,2,3,...
- for (i = 0; i < LEN; i++)
- {
- tx[i] = i;
- }
- // clear RX buffer
- memset(rx, 0, LEN);
- // Do a transfer
- if (libsoc_spi_rw(spi_dev, tx, rx, LEN) != EXIT_SUCCESS)
- {
- printf("SPI RW Failed.\n");
- }
- // Print received bytes
- for (i = 0; i < LEN; i++)
- {
- printf("[0x%02X]", rx[i]);
- }
- printf("\n");
- libsoc_spi_free(spi_dev);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement