Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
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.

SPIDEV.h 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * The MySensors Arduino library handles the wireless radio link and protocol
  3. * between your home built sensors/actuators and HA controller of choice.
  4. * The sensors forms a self healing radio network with optional repeaters. Each
  5. * repeater and gateway builds a routing tables in EEPROM which keeps track of the
  6. * network topology allowing messages to be routed to nodes.
  7. *
  8. * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
  9. * Copyright (C) 2013-2018 Sensnology AB
  10. * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
  11. *
  12. * Documentation: http://www.mysensors.org
  13. * Support Forum: http://forum.mysensors.org
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. *
  19. * Based on TMRh20 RF24 library, Copyright (c) 2015 Charles-Henri Hallard <tmrh20@gmail.com>
  20. */
  21. #ifndef SPIDEV_h
  22. #define SPIDEV_h
  23. #include <stdint.h>
  24. #include <string>
  25. #include <linux/spi/spidev.h>
  26. #define SPI_HAS_TRANSACTION
  27. #define MSBFIRST 0
  28. #define LSBFIRST SPI_LSB_FIRST
  29. #define SPI_CLOCK_BASE 16000000 // 16Mhz
  30. #define SPI_CLOCK_DIV1 1
  31. #define SPI_CLOCK_DIV2 2
  32. #define SPI_CLOCK_DIV4 4
  33. #define SPI_CLOCK_DIV8 8
  34. #define SPI_CLOCK_DIV16 16
  35. #define SPI_CLOCK_DIV32 32
  36. #define SPI_CLOCK_DIV64 64
  37. #define SPI_CLOCK_DIV128 128
  38. #define SPI_CLOCK_DIV256 256
  39. // SPI Data mode
  40. #define SPI_MODE0 SPI_MODE_0
  41. #define SPI_MODE1 SPI_MODE_1
  42. #define SPI_MODE2 SPI_MODE_2
  43. #define SPI_MODE3 SPI_MODE_3
  44. #ifndef SPI_SPIDEV_DEVICE
  45. #define SPI_SPIDEV_DEVICE "/dev/spidev0.0"
  46. #endif
  47. // Default to Raspberry Pi
  48. const uint8_t SS = 24;
  49. const uint8_t MOSI = 19;
  50. const uint8_t MISO = 21;
  51. const uint8_t SCK = 23;
  52. /**
  53. * SPISettings class
  54. */
  55. class SPISettings
  56. {
  57. public:
  58. /**
  59. * @brief SPISettings constructor.
  60. */
  61. SPISettings()
  62. {
  63. init(SPI_CLOCK_BASE, MSBFIRST, SPI_MODE0);
  64. }
  65. /**
  66. * @brief SPISettings constructor.
  67. *
  68. * @param clock SPI clock speed in Hz.
  69. * @param bitOrder SPI bit order.
  70. * @param dataMode SPI data mode.
  71. */
  72. SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
  73. {
  74. init(clock, bitOrder, dataMode);
  75. }
  76. uint32_t clock; //!< @brief SPI clock.
  77. uint8_t border; //!< @brief SPI bit order.
  78. uint8_t dmode; //!< @brief SPI data mode.
  79. private:
  80. /**
  81. * @brief Initialized class members.
  82. *
  83. * @param clk SPI clock.
  84. * @param bitOrder SPI bit order.
  85. * @param dataMode SPI data mode.
  86. */
  87. void init(uint32_t clk, uint8_t bitOrder, uint8_t dataMode)
  88. {
  89. clock = clk;
  90. border = bitOrder;
  91. dmode = dataMode;
  92. }
  93. friend class SPIDEVClass;
  94. };
  95. /**
  96. * SPIDEV class
  97. */
  98. class SPIDEVClass
  99. {
  100. public:
  101. /**
  102. * @brief SPIDEVClass constructor.
  103. */
  104. SPIDEVClass();
  105. /**
  106. * @brief Start SPI operations.
  107. */
  108. static void begin(int busNo=0);
  109. /**
  110. * @brief End SPI operations.
  111. */
  112. static void end();
  113. /**
  114. * @brief Sets the SPI bit order.
  115. *
  116. * @param bit_order The desired bit order.
  117. */
  118. static void setBitOrder(uint8_t bit_order);
  119. /**
  120. * @brief Sets the SPI data mode.
  121. *
  122. * @param data_mode The desired data mode.
  123. */
  124. static void setDataMode(uint8_t data_mode);
  125. /**
  126. * @brief Sets the SPI clock divider and therefore the SPI clock speed.
  127. *
  128. * @param divider The desired SPI clock divider.
  129. */
  130. static void setClockDivider(uint16_t divider);
  131. /**
  132. * @brief Sets the chip select pin.
  133. *
  134. * @param csn_chip Specifies the CS chip.
  135. */
  136. static void chipSelect(int csn_chip);
  137. /**
  138. * @brief Transfer a single byte
  139. *
  140. * @param data Byte to send
  141. * @return Data returned via spi
  142. */
  143. static uint8_t transfer(uint8_t data);
  144. /**
  145. * @brief Transfer a buffer of data
  146. *
  147. * @param tbuf Transmit buffer
  148. * @param rbuf Receive buffer
  149. * @param len Length of the data
  150. */
  151. static void transfernb(char* tbuf, char* rbuf, uint32_t len);
  152. /**
  153. * @brief Transfer a buffer of data without an rx buffer
  154. *
  155. * @param buf Pointer to a buffer of data
  156. * @param len Length of the data
  157. */
  158. static void transfern(char* buf, uint32_t len);
  159. /**
  160. * @brief Start SPI transaction.
  161. *
  162. * @param settings for SPI.
  163. */
  164. static void beginTransaction(SPISettings settings);
  165. /**
  166. * @brief End SPI transaction.
  167. */
  168. static void endTransaction();
  169. /**
  170. * @brief Not implemented.
  171. *
  172. * @param interruptNumber ignored parameter.
  173. */
  174. static void usingInterrupt(uint8_t interruptNumber);
  175. /**
  176. * @brief Not implemented.
  177. *
  178. * @param interruptNumber ignored parameter.
  179. */
  180. static void notUsingInterrupt(uint8_t interruptNumber);
  181. private:
  182. static uint8_t initialized; //!< @brief SPI initialized flag.
  183. static int fd; //!< @brief SPI device file descriptor.
  184. static std::string device; //!< @brief Default SPI device.
  185. static uint8_t mode; //!< @brief SPI mode.
  186. static uint32_t speed; //!< @brief SPI speed.
  187. static uint8_t bit_order; //!< @brief SPI bit order.
  188. static struct spi_ioc_transfer tr; //!< @brief Auxiliar struct for data transfer.
  189. static void init();
  190. };
  191. extern SPIDEVClass SPIDEV;
  192. #endif