spi_master.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <stdbool.h>
  20. #include "gpio.h"
  21. #include "chibios_config.h"
  22. #ifndef SPI_DRIVER
  23. # define SPI_DRIVER SPID2
  24. #endif
  25. #ifndef SPI_SCK_PIN
  26. # define SPI_SCK_PIN B13
  27. #endif
  28. #ifndef SPI_SCK_PAL_MODE
  29. # if defined(USE_GPIOV1)
  30. # define SPI_SCK_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL
  31. # else
  32. # define SPI_SCK_PAL_MODE 5
  33. # endif
  34. #endif
  35. #ifndef SPI_MOSI_PIN
  36. # define SPI_MOSI_PIN B15
  37. #endif
  38. #ifndef SPI_MOSI_PAL_MODE
  39. # if defined(USE_GPIOV1)
  40. # define SPI_MOSI_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL
  41. # else
  42. # define SPI_MOSI_PAL_MODE 5
  43. # endif
  44. #endif
  45. #ifndef SPI_MISO_PIN
  46. # define SPI_MISO_PIN B14
  47. #endif
  48. #ifndef SPI_MISO_PAL_MODE
  49. # if defined(USE_GPIOV1)
  50. # define SPI_MISO_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL
  51. # else
  52. # define SPI_MISO_PAL_MODE 5
  53. # endif
  54. #endif
  55. typedef int16_t spi_status_t;
  56. #define SPI_STATUS_SUCCESS (0)
  57. #define SPI_STATUS_ERROR (-1)
  58. #define SPI_STATUS_TIMEOUT (-2)
  59. #define SPI_TIMEOUT_IMMEDIATE (0)
  60. #define SPI_TIMEOUT_INFINITE (0xFFFF)
  61. #ifdef __cplusplus
  62. extern "C" {
  63. #endif
  64. void spi_init(void);
  65. bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor);
  66. spi_status_t spi_write(uint8_t data);
  67. spi_status_t spi_read(void);
  68. spi_status_t spi_transmit(const uint8_t *data, uint16_t length);
  69. spi_status_t spi_receive(uint8_t *data, uint16_t length);
  70. void spi_stop(void);
  71. #ifdef __cplusplus
  72. }
  73. #endif