SPISlave.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #include "drivers/SPISlave.h"
  17. #if DEVICE_SPISLAVE
  18. namespace mbed {
  19. SPISlave::SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
  20. _spi(),
  21. _bits(8),
  22. _mode(0),
  23. _hz(1000000)
  24. {
  25. spi_init(&_spi, mosi, miso, sclk, ssel);
  26. spi_format(&_spi, _bits, _mode, 1);
  27. spi_frequency(&_spi, _hz);
  28. }
  29. void SPISlave::format(int bits, int mode)
  30. {
  31. _bits = bits;
  32. _mode = mode;
  33. spi_format(&_spi, _bits, _mode, 1);
  34. }
  35. void SPISlave::frequency(int hz)
  36. {
  37. _hz = hz;
  38. spi_frequency(&_spi, _hz);
  39. }
  40. int SPISlave::receive(void)
  41. {
  42. return (spi_slave_receive(&_spi));
  43. }
  44. int SPISlave::read(void)
  45. {
  46. return (spi_slave_read(&_spi));
  47. }
  48. void SPISlave::reply(int value)
  49. {
  50. spi_slave_write(&_spi, value);
  51. }
  52. } // namespace mbed
  53. #endif