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.

80 lines
1.8 KiB

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