Naze32 clone with Frysky receiver
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.

spi.h 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Bit-Banged SPI routines
  3. */
  4. #ifndef _SPI_H
  5. #define _SPI_H
  6. #include <stdint.h>
  7. #include <avr/io.h>
  8. #ifdef DEBUG
  9. // Arduino D3 = PD3
  10. #define SCK_on PORTD |= (1 << PD3)
  11. #define SCK_off PORTD &= ~(1 << PD3)
  12. #define SCK_dir DDRD |= (1 << PD3)
  13. // Arduino D7 = PD7
  14. #define MO_on PORTD |= (1 << PD7)
  15. #define MO_off PORTD &= ~(1 << PD7)
  16. #define MO_dir DDRD |= (1 << PD7)
  17. // Arduino D8 = PB0
  18. #define CS_on PORTB |= (1 << PB0)
  19. #define CS_off PORTB &= ~(1 << PB0)
  20. #define CS_dir DDRB |= (1 << PB0)
  21. // Arduino D2 = PD2
  22. #define MI_1 (PIND & (1 << PD2)) != 0
  23. #define MI_0 (PIND & (1 << PD2)) == 0
  24. #define MI_dir DDRD &= ~(1 << PD2); PORTD |= (1 << PD2)
  25. // Arduino D9 = PB1
  26. #define GDO_1 (PINB & (1 << PB1)) != 0
  27. #define GDO_0 (PINB & (1 << PB1)) == 0
  28. #define GDO_dir DDRB &= ~(1 << PB1); PORTB |= (1 << PB1)
  29. #else
  30. #define SCK_on PORTB |= (1 << PB4)
  31. #define SCK_off PORTB &= ~(1 << PB4)
  32. #define SCK_dir DDRB |= (1 << PB4)
  33. #define MO_on PORTB |= (1 << PB2)
  34. #define MO_off PORTB &= ~(1 << PB2)
  35. #define MO_dir DDRB |= (1 << PB2)
  36. #define CS_on PORTB |= (1 << PB1)
  37. #define CS_off PORTB &= ~(1 << PB1)
  38. #define CS_dir DDRB |= (1 << PB1)
  39. #define MI_1 (PINB & (1 << PB3)) != 0
  40. #define MI_0 (PINB & (1 << PB3)) == 0
  41. #define MI_dir DDRB &= ~(1 << PB3); PORTB |= (1 << PB3)
  42. #define GDO_1 (PINB & (1 << PB0)) != 0
  43. #define GDO_0 (PINB & (1 << PB0)) == 0
  44. #define GDO_dir DDRB &= ~(1 << PB0); PORTB |= (1 << PB0)
  45. #endif
  46. #define NOP() __asm__ __volatile__("nop")
  47. void spiInit(void);
  48. uint8_t spiReadWrite(uint8_t command);
  49. #define spiWrite(x) spiReadWrite(x)
  50. #define spiRead() spiReadWrite(0)
  51. #endif