PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

177 lines
3.5KB

  1. // RasPi.cpp
  2. //
  3. // Routines for implementing RadioHead on Raspberry Pi
  4. // using BCM2835 library for GPIO
  5. //
  6. // Contributed by Mike Poublon and used with permission
  7. #include <RadioHead.h>
  8. #if (RH_PLATFORM == RH_PLATFORM_RASPI)
  9. #include <sys/time.h>
  10. #include <time.h>
  11. #include "RasPi.h"
  12. //Initialize the values for sanity
  13. timeval RHStartTime;
  14. void SPIClass::begin()
  15. {
  16. //Set SPI Defaults
  17. uint16_t divider = BCM2835_SPI_CLOCK_DIVIDER_256;
  18. uint8_t bitorder = BCM2835_SPI_BIT_ORDER_MSBFIRST;
  19. uint8_t datamode = BCM2835_SPI_MODE0;
  20. begin(divider, bitorder, datamode);
  21. }
  22. void SPIClass::begin(uint16_t divider, uint8_t bitOrder, uint8_t dataMode)
  23. {
  24. setClockDivider(divider);
  25. setBitOrder(bitOrder);
  26. setDataMode(dataMode);
  27. //Set CS pins polarity to low
  28. bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, 0);
  29. bcm2835_spi_begin();
  30. //Initialize a timestamp for millis calculation
  31. gettimeofday(&RHStartTime, NULL);
  32. }
  33. void SPIClass::end()
  34. {
  35. //End the SPI
  36. bcm2835_spi_end();
  37. }
  38. void SPIClass::setBitOrder(uint8_t bitOrder)
  39. {
  40. //Set the SPI bit Order
  41. bcm2835_spi_setBitOrder(bitOrder);
  42. }
  43. void SPIClass::setDataMode(uint8_t mode)
  44. {
  45. //Set SPI data mode
  46. bcm2835_spi_setDataMode(mode);
  47. }
  48. void SPIClass::setClockDivider(uint16_t rate)
  49. {
  50. //Set SPI clock divider
  51. bcm2835_spi_setClockDivider(rate);
  52. }
  53. byte SPIClass::transfer(byte _data)
  54. {
  55. //Set which CS pin to use for next transfers
  56. bcm2835_spi_chipSelect(BCM2835_SPI_CS0);
  57. //Transfer 1 byte
  58. byte data;
  59. data = bcm2835_spi_transfer((uint8_t)_data);
  60. return data;
  61. }
  62. void pinMode(unsigned char pin, unsigned char mode)
  63. {
  64. if (mode == OUTPUT)
  65. {
  66. bcm2835_gpio_fsel(pin,BCM2835_GPIO_FSEL_OUTP);
  67. }
  68. else
  69. {
  70. bcm2835_gpio_fsel(pin,BCM2835_GPIO_FSEL_INPT);
  71. }
  72. }
  73. void digitalWrite(unsigned char pin, unsigned char value)
  74. {
  75. bcm2835_gpio_write(pin,value);
  76. }
  77. unsigned long millis()
  78. {
  79. //Declare a variable to store current time
  80. struct timeval RHCurrentTime;
  81. //Get current time
  82. gettimeofday(&RHCurrentTime,NULL);
  83. //Calculate the difference between our start time and the end time
  84. unsigned long difference = ((RHCurrentTime.tv_sec - RHStartTime.tv_sec) * 1000);
  85. difference += ((RHCurrentTime.tv_usec - RHStartTime.tv_usec)/1000);
  86. //Return the calculated value
  87. return difference;
  88. }
  89. void delay (unsigned long ms)
  90. {
  91. //Implement Delay function
  92. struct timespec ts;
  93. ts.tv_sec=0;
  94. ts.tv_nsec=(ms * 1000);
  95. nanosleep(&ts,&ts);
  96. }
  97. long random(long min, long max)
  98. {
  99. long diff = max - min;
  100. long ret = diff * rand() + min;
  101. return ret;
  102. }
  103. void SerialSimulator::begin(int baud)
  104. {
  105. //No implementation neccesary - Serial emulation on Linux = standard console
  106. //
  107. //Initialize a timestamp for millis calculation - we do this here as well in case SPI
  108. //isn't used for some reason
  109. gettimeofday(&RHStartTime, NULL);
  110. }
  111. size_t SerialSimulator::println(const char* s)
  112. {
  113. print(s);
  114. printf("\n");
  115. }
  116. size_t SerialSimulator::print(const char* s)
  117. {
  118. printf(s);
  119. }
  120. size_t SerialSimulator::print(unsigned int n, int base)
  121. {
  122. if (base == DEC)
  123. printf("%d", n);
  124. else if (base == HEX)
  125. printf("%02x", n);
  126. else if (base == OCT)
  127. printf("%o", n);
  128. // TODO: BIN
  129. }
  130. size_t SerialSimulator::print(char ch)
  131. {
  132. printf("%c", ch);
  133. }
  134. size_t SerialSimulator::println(char ch)
  135. {
  136. printf("%c\n", ch);
  137. }
  138. size_t SerialSimulator::print(unsigned char ch, int base)
  139. {
  140. return print((unsigned int)ch, base);
  141. }
  142. size_t SerialSimulator::println(unsigned char ch, int base)
  143. {
  144. print((unsigned int)ch, base);
  145. printf("\n");
  146. }
  147. #endif