Naze32 clone with Frysky receiver
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.

SPISlave.h 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_SPISLAVE_H
  17. #define MBED_SPISLAVE_H
  18. #include "platform.h"
  19. #if DEVICE_SPISLAVE
  20. #include "spi_api.h"
  21. namespace mbed {
  22. /** A SPI slave, used for communicating with a SPI Master device
  23. *
  24. * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
  25. *
  26. * Example:
  27. * @code
  28. * // Reply to a SPI master as slave
  29. *
  30. * #include "mbed.h"
  31. *
  32. * SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
  33. *
  34. * int main() {
  35. * device.reply(0x00); // Prime SPI with first reply
  36. * while(1) {
  37. * if(device.receive()) {
  38. * int v = device.read(); // Read byte from master
  39. * v = (v + 1) % 0x100; // Add one to it, modulo 256
  40. * device.reply(v); // Make this the next reply
  41. * }
  42. * }
  43. * }
  44. * @endcode
  45. */
  46. class SPISlave {
  47. public:
  48. /** Create a SPI slave connected to the specified pins
  49. *
  50. * mosi or miso can be specfied as NC if not used
  51. *
  52. * @param mosi SPI Master Out, Slave In pin
  53. * @param miso SPI Master In, Slave Out pin
  54. * @param sclk SPI Clock pin
  55. * @param ssel SPI chip select pin
  56. */
  57. SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel);
  58. /** Configure the data transmission format
  59. *
  60. * @param bits Number of bits per SPI frame (4 - 16)
  61. * @param mode Clock polarity and phase mode (0 - 3)
  62. *
  63. * @code
  64. * mode | POL PHA
  65. * -----+--------
  66. * 0 | 0 0
  67. * 1 | 0 1
  68. * 2 | 1 0
  69. * 3 | 1 1
  70. * @endcode
  71. */
  72. void format(int bits, int mode = 0);
  73. /** Set the spi bus clock frequency
  74. *
  75. * @param hz SCLK frequency in hz (default = 1MHz)
  76. */
  77. void frequency(int hz = 1000000);
  78. /** Polls the SPI to see if data has been received
  79. *
  80. * @returns
  81. * 0 if no data,
  82. * 1 otherwise
  83. */
  84. int receive(void);
  85. /** Retrieve data from receive buffer as slave
  86. *
  87. * @returns
  88. * the data in the receive buffer
  89. */
  90. int read(void);
  91. /** Fill the transmission buffer with the value to be written out
  92. * as slave on the next received message from the master.
  93. *
  94. * @param value the data to be transmitted next
  95. */
  96. void reply(int value);
  97. protected:
  98. spi_t _spi;
  99. int _bits;
  100. int _mode;
  101. int _hz;
  102. };
  103. } // namespace mbed
  104. #endif
  105. #endif