Browse Source

Fixed pin definition problems and unified SPI reading and writing.

Thomas Buck 8 years ago
parent
commit
bc62ca1654
2 changed files with 50 additions and 47 deletions
  1. 32
    30
      include/spi.h
  2. 18
    17
      src/spi.c

+ 32
- 30
include/spi.h View File

11
 #ifdef DEBUG
11
 #ifdef DEBUG
12
 
12
 
13
 // Arduino D3 = PD3
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
 #define SCK_dir DDRD |= (1 << PD3)
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
 // Arduino D8 = PB0
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
 #define CS_dir DDRB |= (1 << PB0)
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
 // Arduino D9 = PB1
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
 #else
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
 #define SCK_dir DDRB |= (1 << PB4)
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
 #define CS_dir DDRB |= (1 << PB1)
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
 #endif
60
 #endif
61
 
61
 
62
 #define NOP() __asm__ __volatile__("nop")
62
 #define NOP() __asm__ __volatile__("nop")
63
 
63
 
64
 void spiInit(void);
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
 #endif
70
 #endif
69
 
71
 

+ 18
- 17
src/spi.c View File

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

Loading…
Cancel
Save