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.c 921B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Bit-Banged SPI routines
  3. */
  4. #include "spi.h"
  5. void spiInit(void) {
  6. DDRB |= (1 << PB2); // SI output
  7. DDRB &= ~(1 << PB3); // SO input
  8. DDRB |= (1 << PB4); // SCLK output
  9. DDRB |= (1 << PB1); // CS output
  10. DDRB &= ~(1 << PB0); // GDO0 input
  11. SCK_off;
  12. MO_off;
  13. CS_on;
  14. }
  15. void spiWrite(uint8_t command) {
  16. SCK_off; //SCK start low
  17. MO_off;
  18. uint8_t n = 8;
  19. while (n--) {
  20. if (command & 0x80) {
  21. MO_on;
  22. } else {
  23. MO_off;
  24. }
  25. SCK_on;
  26. NOP();
  27. SCK_off;
  28. command = command << 1;
  29. }
  30. MO_on;
  31. }
  32. uint8_t spiRead(void) {
  33. uint8_t result = 0;
  34. for (uint8_t i = 0; i < 8; i++) {
  35. if (MI_1) {
  36. result = (result << 1) | 0x01;
  37. } else {
  38. result = result << 1;
  39. }
  40. SCK_on;
  41. NOP();
  42. SCK_off;
  43. NOP();
  44. }
  45. return result;
  46. }