spi_master.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* Copyright 2020
  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 <stdbool.h>
  18. #include "gpio.h"
  19. typedef int16_t spi_status_t;
  20. // Hardware SS pin is defined in the header so that user code can refer to it
  21. #if defined(__AVR_AT90USB162__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
  22. # define SPI_SS_PIN B0
  23. #elif defined(__AVR_ATmega32A__)
  24. # define SPI_SS_PIN B4
  25. #elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__)
  26. # define SPI_SS_PIN B2
  27. #endif
  28. #define SPI_STATUS_SUCCESS (0)
  29. #define SPI_STATUS_ERROR (-1)
  30. #define SPI_STATUS_TIMEOUT (-2)
  31. #define SPI_TIMEOUT_IMMEDIATE (0)
  32. #define SPI_TIMEOUT_INFINITE (0xFFFF)
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. void spi_init(void);
  37. bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor);
  38. spi_status_t spi_write(uint8_t data);
  39. spi_status_t spi_read(void);
  40. spi_status_t spi_transmit(const uint8_t *data, uint16_t length);
  41. spi_status_t spi_receive(uint8_t *data, uint16_t length);
  42. void spi_stop(void);
  43. #ifdef __cplusplus
  44. }
  45. #endif