spi_master.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Copyright 2020 Nick Brassel (tzarc)
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #include <ch.h>
  18. #include <hal.h>
  19. #include "quantum.h"
  20. #ifndef SPI_DRIVER
  21. # define SPI_DRIVER SPID2
  22. #endif
  23. #ifndef SPI_SCK_PIN
  24. # define SPI_SCK_PIN B13
  25. #endif
  26. #ifndef SPI_SCK_PAL_MODE
  27. # define SPI_SCK_PAL_MODE 5
  28. #endif
  29. #ifndef SPI_MOSI_PIN
  30. # define SPI_MOSI_PIN B15
  31. #endif
  32. #ifndef SPI_MOSI_PAL_MODE
  33. # define SPI_MOSI_PAL_MODE 5
  34. #endif
  35. #ifndef SPI_MISO_PIN
  36. # define SPI_MISO_PIN B14
  37. #endif
  38. #ifndef SPI_MISO_PAL_MODE
  39. # define SPI_MISO_PAL_MODE 5
  40. #endif
  41. typedef int16_t spi_status_t;
  42. #define SPI_STATUS_SUCCESS (0)
  43. #define SPI_STATUS_ERROR (-1)
  44. #define SPI_STATUS_TIMEOUT (-2)
  45. #define SPI_TIMEOUT_IMMEDIATE (0)
  46. #define SPI_TIMEOUT_INFINITE (0xFFFF)
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. void spi_init(void);
  51. bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor);
  52. spi_status_t spi_write(uint8_t data);
  53. spi_status_t spi_read(void);
  54. spi_status_t spi_transmit(const uint8_t *data, uint16_t length);
  55. spi_status_t spi_receive(uint8_t *data, uint16_t length);
  56. void spi_stop(void);
  57. #ifdef __cplusplus
  58. }
  59. #endif