Ein Roboter mit bürstenlosem Antrieb, differenzial und NRF24L01 Funk. Großflächig gebaut um ein großes Solarpanel aufzunehmen. https://gitlab.informatik.hs-fulda.de/fdai5253/roboter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
1.8 KiB

  1. /*
  2. * File: spi.cpp
  3. * Author:
  4. *
  5. * Created on
  6. *
  7. * Inspired from spi speed test from wiringPi
  8. * wiringPi/examples/spiSpeed.c
  9. */
  10. #include "spi.h"
  11. #include <wiringPi.h>
  12. #include <wiringPiSPI.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include <errno.h>
  18. #define RF24_SPI_SPEED 8 * 1000000 // 8Mhz
  19. #define RF24_SPI_CHANNEL 0
  20. SPI::SPI():fd(-1)
  21. {
  22. printf("wiringPi RF24 DRIVER\n");
  23. }
  24. void SPI::begin(int csn_pin)
  25. {
  26. // initialize the wiringPiSPI
  27. if ((this->fd = wiringPiSPISetup(RF24_SPI_CHANNEL, RF24_SPI_SPEED)) < 0)
  28. {
  29. printf("Cannot configure the SPI device!\n");
  30. fflush(stdout);
  31. abort();
  32. }
  33. else
  34. printf("Configured SPI fd: %d - pin: %d\n", fd, csn_pin);
  35. }
  36. uint8_t SPI::transfer(uint8_t tx)
  37. {
  38. memset(&msgByte, 0, sizeof(msgByte));
  39. memcpy(&msgByte, &tx, sizeof(tx));
  40. if(wiringPiSPIDataRW(RF24_SPI_CHANNEL, &msgByte, sizeof(tx)) < 0)
  41. {
  42. printf("transfer(): Cannot send data: %s\n", strerror(errno));
  43. fflush(stdout);
  44. abort();
  45. }
  46. return msgByte;
  47. }
  48. void SPI::transfern(char* buf, uint32_t len)
  49. {
  50. printf("transfern(tx: %s)\n", buf);
  51. if(wiringPiSPIDataRW(RF24_SPI_CHANNEL, (uint8_t *)buf, len) < 0)
  52. {
  53. printf("transfern(): Cannot send data %s\n", strerror(errno));
  54. fflush(stdout);
  55. abort();
  56. }
  57. }
  58. void SPI::transfernb(char* tbuf, char* rbuf, uint32_t len)
  59. {
  60. // using an auxiliary buffer to keep tx and rx different
  61. memset(msg, 0, sizeof(msg));
  62. memcpy(msg, tbuf, len);
  63. if(wiringPiSPIDataRW(RF24_SPI_CHANNEL, msg, len) < 0)
  64. {
  65. printf("transfernb() Cannot send data %s\n", strerror(errno));
  66. fflush(stdout);
  67. abort();
  68. }
  69. memcpy(rbuf, msg, len);
  70. }
  71. SPI::~SPI()
  72. {
  73. if (!(this->fd < 0))
  74. {
  75. close(this->fd);
  76. this->fd = -1;
  77. }
  78. }