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.

SPIBCM.cpp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include "SPIBCM.h"
  22. #include <pthread.h>
  23. #include <stdlib.h>
  24. #include "log.h"
  25. static pthread_mutex_t spiMutex = PTHREAD_MUTEX_INITIALIZER;
  26. // Declare a single default instance
  27. SPIBCMClass SPIBCM = SPIBCMClass();
  28. uint8_t SPIBCMClass::initialized = 0;
  29. void SPIBCMClass::begin()
  30. {
  31. if (!initialized) {
  32. if (!BCM.isInitialized()) {
  33. BCM.init();
  34. }
  35. if (!bcm2835_spi_begin()) {
  36. logError("You need root privilege to use SPI.\n");
  37. exit(1);
  38. }
  39. }
  40. initialized++; // reference count
  41. }
  42. void SPIBCMClass::end()
  43. {
  44. if (initialized) {
  45. initialized--;
  46. }
  47. if (!initialized) {
  48. // End the SPI
  49. bcm2835_spi_end();
  50. }
  51. }
  52. void SPIBCMClass::setBitOrder(uint8_t bit_order)
  53. {
  54. bcm2835_spi_setBitOrder(bit_order);
  55. }
  56. void SPIBCMClass::setDataMode(uint8_t data_mode)
  57. {
  58. bcm2835_spi_setDataMode(data_mode);
  59. }
  60. void SPIBCMClass::setClockDivider(uint16_t divider)
  61. {
  62. bcm2835_spi_setClockDivider(divider);
  63. }
  64. void SPIBCMClass::chipSelect(int csn_pin)
  65. {
  66. if (csn_pin == RPI_GPIO_P1_26) {
  67. csn_pin = BCM2835_SPI_CS1;
  68. } else if (csn_pin == RPI_GPIO_P1_24) {
  69. csn_pin = BCM2835_SPI_CS0;
  70. } else {
  71. csn_pin = BCM2835_SPI_CS0;
  72. }
  73. bcm2835_spi_chipSelect(csn_pin);
  74. delayMicroseconds(5);
  75. }
  76. void SPIBCMClass::beginTransaction(SPISettings settings)
  77. {
  78. pthread_mutex_lock(&spiMutex);
  79. setBitOrder(settings.border);
  80. setDataMode(settings.dmode);
  81. setClockDivider(settings.cdiv);
  82. }
  83. void SPIBCMClass::endTransaction()
  84. {
  85. pthread_mutex_unlock(&spiMutex);
  86. }
  87. void SPIBCMClass::usingInterrupt(uint8_t interruptNumber)
  88. {
  89. (void)interruptNumber;
  90. }
  91. void SPIBCMClass::notUsingInterrupt(uint8_t interruptNumber)
  92. {
  93. (void)interruptNumber;
  94. }