spi.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * spi.cpp
  3. *
  4. * Created on: Nov 16, 2015
  5. * Author: Philipp Hinz
  6. */
  7. #include <wiringPi.h>
  8. #include <wiringPiSPI.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include "global.h"
  13. #include "logger.h"
  14. #include <inttypes.h>
  15. #include <unistd.h>
  16. #include "spi.h"
  17. /**
  18. * writes the data on the SPI bus and returns the reply
  19. * @param data byte to write to the bus
  20. * @return byte read from the bus
  21. */
  22. uint8_t spi_readwrite(uint8_t data) {
  23. uint8_t tmp[1];
  24. tmp[0] = data;
  25. wiringPiSPIDataRW(SPI_CHANNEL, tmp, 1); // int wiringPiSPIDataRW (int channel, unsigned char *data, int len) ;
  26. if (verbose >= V_SPI)
  27. logger(V_SPI, "SPI Write %x Read %x\n", data, tmp[0]);
  28. return tmp[0];
  29. }
  30. /**
  31. * initializes the SPI bus
  32. */
  33. void spi_init(void) {
  34. if (wiringPiSPISetup(SPI_CHANNEL, SPI_SPEED) < 0) { //int wiringPiSPISetup (int channel, int speed) ;
  35. logger_error("Unable to setup SPI on channel %d: %s\n", SPI_CHANNEL,
  36. strerror(errno));
  37. return;
  38. }
  39. }
  40. void shift_init(void) {
  41. pinMode(SHIFT_G, OUTPUT);
  42. pinMode(SHIFT_RCK, OUTPUT);
  43. pinMode(SHIFT_SCLR, OUTPUT);
  44. digitalWrite(SHIFT_G, HIGH);
  45. digitalWrite(SHIFT_RCK, HIGH);
  46. digitalWrite(SHIFT_SCLR, LOW);
  47. usleep(10);
  48. digitalWrite(SHIFT_G, LOW);
  49. digitalWrite(SHIFT_RCK, LOW);
  50. digitalWrite(SHIFT_SCLR, HIGH);
  51. }
  52. void shift_data(uint8_t data) {
  53. spi_readwrite(data);
  54. digitalWrite(SHIFT_RCK, HIGH);
  55. usleep(10);
  56. digitalWrite(SHIFT_RCK, LOW);
  57. }