SPISlave.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/platform.h"
  19. #include "platform/NonCopyable.h"
  20. #if defined (DEVICE_SPISLAVE) || defined(DOXYGEN_ONLY)
  21. #include "hal/spi_api.h"
  22. namespace mbed {
  23. /** \addtogroup drivers */
  24. /** A SPI slave, used for communicating with a SPI Master device
  25. *
  26. * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
  27. *
  28. * @note Synchronization level: Not protected
  29. *
  30. * Example:
  31. * @code
  32. * // Reply to a SPI master as slave
  33. *
  34. * #include "mbed.h"
  35. *
  36. * SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
  37. *
  38. * int main() {
  39. * device.reply(0x00); // Prime SPI with first reply
  40. * while(1) {
  41. * if(device.receive()) {
  42. * int v = device.read(); // Read byte from master
  43. * v = (v + 1) % 0x100; // Add one to it, modulo 256
  44. * device.reply(v); // Make this the next reply
  45. * }
  46. * }
  47. * }
  48. * @endcode
  49. * @ingroup drivers
  50. */
  51. class SPISlave : private NonCopyable<SPISlave> {
  52. public:
  53. /** Create a SPI slave connected to the specified pins
  54. *
  55. * mosi or miso can be specified as NC if not used
  56. *
  57. * @param mosi SPI Master Out, Slave In pin
  58. * @param miso SPI Master In, Slave Out pin
  59. * @param sclk SPI Clock pin
  60. * @param ssel SPI chip select pin
  61. */
  62. SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel);
  63. /** Configure the data transmission format
  64. *
  65. * @param bits Number of bits per SPI frame (4 - 16)
  66. * @param mode Clock polarity and phase mode (0 - 3)
  67. *
  68. * @code
  69. * mode | POL PHA
  70. * -----+--------
  71. * 0 | 0 0
  72. * 1 | 0 1
  73. * 2 | 1 0
  74. * 3 | 1 1
  75. * @endcode
  76. */
  77. void format(int bits, int mode = 0);
  78. /** Set the spi bus clock frequency
  79. *
  80. * @param hz SCLK frequency in hz (default = 1MHz)
  81. */
  82. void frequency(int hz = 1000000);
  83. /** Polls the SPI to see if data has been received
  84. *
  85. * @returns
  86. * 0 if no data,
  87. * 1 otherwise
  88. */
  89. int receive(void);
  90. /** Retrieve data from receive buffer as slave
  91. *
  92. * @returns
  93. * the data in the receive buffer
  94. */
  95. int read(void);
  96. /** Fill the transmission buffer with the value to be written out
  97. * as slave on the next received message from the master.
  98. *
  99. * @param value the data to be transmitted next
  100. */
  101. void reply(int value);
  102. protected:
  103. spi_t _spi;
  104. int _bits;
  105. int _mode;
  106. int _hz;
  107. };
  108. } // namespace mbed
  109. #endif
  110. #endif