Просмотр исходного кода

Fixed pin definition problems and unified SPI reading and writing.

Thomas Buck 8 лет назад
Родитель
Сommit
bc62ca1654
2 измененных файлов: 50 добавлений и 47 удалений
  1. 32
    30
      include/spi.h
  2. 18
    17
      src/spi.c

+ 32
- 30
include/spi.h Просмотреть файл

@@ -11,59 +11,61 @@
11 11
 #ifdef DEBUG
12 12
 
13 13
 // Arduino D3 = PD3
14
-#define SCK_on PORTD |= PD3
15
-#define SCK_off PORTD &= ~(PD3)
14
+#define SCK_on PORTD |= (1 << PD3)
15
+#define SCK_off PORTD &= ~(1 << PD3)
16 16
 #define SCK_dir DDRD |= (1 << PD3)
17 17
 
18
-// Arduino D2 = PD2
19
-#define MO_on PORTD |= PD2
20
-#define MO_off PORTD &= ~(PD2)
21
-#define MO_dir DDRD &= ~(1 << PD2)
18
+// Arduino D7 = PD7
19
+#define MO_on PORTD |= (1 << PD7)
20
+#define MO_off PORTD &= ~(1 << PD7)
21
+#define MO_dir DDRD |= (1 << PD7)
22 22
 
23 23
 // Arduino D8 = PB0
24
-#define CS_on PORTB |= PB0
25
-#define CS_off PORTB &= ~(PB0)
24
+#define CS_on PORTB |= (1 << PB0)
25
+#define CS_off PORTB &= ~(1 << PB0)
26 26
 #define CS_dir DDRB |= (1 << PB0)
27 27
 
28
-// Arduino D7 = PD7
29
-#define MI_1 (PIND & PD7) == PD7
30
-#define MI_0 (PIND & PD7) != PD7
31
-#define MI_dir DDRD |= (1 << PD7)
28
+// Arduino D2 = PD2
29
+#define MI_1 (PIND & (1 << PD2)) != 0
30
+#define MI_0 (PIND & (1 << PD2)) == 0
31
+#define MI_dir DDRD &= ~(1 << PD2); PORTD |= (1 << PD2)
32 32
 
33 33
 // Arduino D9 = PB1
34
-#define GDO_1 (PINB & PB1) == PB1
35
-#define GDO_0 (PINB & PB1) != PB1
36
-#define GDO_dir DDRB &= ~(1 << PB1)
34
+#define GDO_1 (PINB & (1 << PB1)) != 0
35
+#define GDO_0 (PINB & (1 << PB1)) == 0
36
+#define GDO_dir DDRB &= ~(1 << PB1); PORTB |= (1 << PB1)
37 37
 
38 38
 #else
39 39
 
40
-#define SCK_on PORTB |= PB4
41
-#define SCK_off PORTB &= ~(PB4)
40
+#define SCK_on PORTB |= (1 << PB4)
41
+#define SCK_off PORTB &= ~(1 << PB4)
42 42
 #define SCK_dir DDRB |= (1 << PB4)
43 43
 
44
-#define MO_on PORTB |= PB3
45
-#define MO_off PORTB &= ~(PB3)
46
-#define MO_dir DDRB &= ~(1 << PB3)
44
+#define MO_on PORTB |= (1 << PB2)
45
+#define MO_off PORTB &= ~(1 << PB2)
46
+#define MO_dir DDRB |= (1 << PB2)
47 47
 
48
-#define CS_on PORTB |= PB1
49
-#define CS_off PORTB &= ~(PB1)
48
+#define CS_on PORTB |= (1 << PB1)
49
+#define CS_off PORTB &= ~(1 << PB1)
50 50
 #define CS_dir DDRB |= (1 << PB1)
51 51
 
52
-#define MI_1 (PINB & PB2) == PB2
53
-#define MI_0 (PINB & PB2) != PB2
54
-#define MI_dir DDRB |= (1 << PB2)
52
+#define MI_1 (PINB & (1 << PB3)) != 0
53
+#define MI_0 (PINB & (1 << PB3)) == 0
54
+#define MI_dir DDRB &= ~(1 << PB3); PORTB |= (1 << PB3)
55 55
 
56
-#define GDO_1 (PINB & PB0) == PB0
57
-#define GDO_0 (PINB & PB0) != PB0
58
-#define GDO_dir DDRB &= ~(1 << PB0)
56
+#define GDO_1 (PINB & (1 << PB0)) != 0
57
+#define GDO_0 (PINB & (1 << PB0)) == 0
58
+#define GDO_dir DDRB &= ~(1 << PB0); PORTB |= (1 << PB0)
59 59
 
60 60
 #endif
61 61
 
62 62
 #define NOP() __asm__ __volatile__("nop")
63 63
 
64 64
 void spiInit(void);
65
-void spiWrite(uint8_t command);
66
-uint8_t spiRead(void);
65
+uint8_t spiReadWrite(uint8_t command);
66
+
67
+#define spiWrite(x) spiReadWrite(x)
68
+#define spiRead() spiReadWrite(0)
67 69
 
68 70
 #endif
69 71
 

+ 18
- 17
src/spi.c Просмотреть файл

@@ -9,39 +9,25 @@ void spiInit(void) {
9 9
     MO_dir;
10 10
     SCK_dir;
11 11
     CS_dir;
12
-    GDO_dir;
13 12
 
14 13
     SCK_off;
15 14
     MO_off;
16 15
     CS_on;
17 16
 }
18 17
 
19
-void spiWrite(uint8_t command) {
18
+uint8_t spiReadWrite(uint8_t command) {
20 19
     SCK_off;
21 20
     MO_off;
22 21
 
23
-    uint8_t n = 8;
24
-    while (n--) {
22
+    uint8_t result = 0;
23
+    for (uint8_t i = 0; i < 8; i++) {
25 24
         if (command & 0x80) {
26 25
             MO_on;
27 26
         } else {
28 27
             MO_off;
29 28
         }
30
-
31
-        SCK_on;
32
-        NOP();
33
-        SCK_off;
34
-
35 29
         command = command << 1;
36
-    }
37
-
38
-    MO_on;
39
-}
40
-
41
-uint8_t spiRead(void) {
42
-    uint8_t result = 0;
43 30
 
44
-    for (uint8_t i = 0; i < 8; i++) {
45 31
         if (MI_1) {
46 32
             result = (result << 1) | 0x01;
47 33
         } else {
@@ -50,8 +36,23 @@ uint8_t spiRead(void) {
50 36
 
51 37
         SCK_on;
52 38
         NOP();
39
+
40
+#ifdef DEBUG
41
+        NOP();
42
+        NOP();
43
+        NOP();
44
+        NOP();
45
+#endif
46
+
53 47
         SCK_off;
54 48
         NOP();
49
+
50
+#ifdef DEBUG
51
+        NOP();
52
+        NOP();
53
+        NOP();
54
+        NOP();
55
+#endif
55 56
     }
56 57
 
57 58
     return result;

Загрузка…
Отмена
Сохранить