Browse Source

Split up SPI and CC2500 code.

Thomas Buck 8 years ago
parent
commit
852748ed41
6 changed files with 243 additions and 193 deletions
  1. 13
    6
      include/cc2500.h
  2. 33
    0
      include/spi.h
  3. 66
    65
      makefile
  4. 72
    0
      src/cc2500.c
  5. 0
    122
      src/cc2500_BB_SPI.c
  6. 59
    0
      src/spi.c

include/iface_cc2500.h → include/cc2500.h View File

@@ -124,10 +124,17 @@ enum {
124 124
 #define CC2500_LQI_CRC_OK_BM                   0x80
125 125
 #define CC2500_LQI_EST_BM                      0x7F
126 126
 
127
-//void CC2500_WriteReg(u8 addr, u8 data);
128
-//u8 CC2500_ReadReg(u8 addr);
129
-//void CC2500_Reset();
130
-//void CC2500_Strobe(u8 cmd);
131
-//void CC2500_WriteData(u8 *packet, u8 length);
132
-//void CC2500_ReadData(u8 *dpbuffer, int len);
127
+void cc2500ReadFifo(uint8_t *dpbuffer, int len);
128
+void cc2500WriteFifo(uint8_t *dpbuffer, uint8_t len);
129
+
130
+void cc2500ReadRegisterMulti(uint8_t address, uint8_t data[], uint8_t length);
131
+void cc2500WriteRegisterMulti(uint8_t address, const uint8_t data[], uint8_t length);
132
+
133
+unsigned char cc2500ReadReg(unsigned char address);
134
+void cc2500WriteReg(uint8_t address, uint8_t data);
135
+
136
+void cc2500Strobe(uint8_t address);
137
+void cc2500ResetChip(void);
138
+
133 139
 #endif
140
+

+ 33
- 0
include/spi.h View File

@@ -0,0 +1,33 @@
1
+/*
2
+ * Bit-Banged SPI routines
3
+ */
4
+
5
+#ifndef _SPI_H
6
+#define _SPI_H
7
+
8
+#include <stdint.h>
9
+#include <avr/io.h>
10
+
11
+#define SCK_on PORTB |= PB4
12
+#define SCK_off PORTB &= ~(PB4)
13
+
14
+#define MO_on PORTB |= PB3
15
+#define MO_off PORTB &= ~(PB3)
16
+
17
+#define CS_on PORTB |= PB1
18
+#define CS_off PORTB &= ~(PB1)
19
+
20
+#define MI_1 (PINB & PB2) == PB2
21
+#define MI_0 (PINB & PB2) != PB2
22
+
23
+#define GDO_1 (PINB & PB0) == PB0
24
+#define GDO_0 (PINB & PB0) != PB0
25
+
26
+#define NOP() __asm__ __volatile__("nop")
27
+
28
+void spi_init(void);
29
+void spi_write(uint8_t command);
30
+uint8_t spi_read(void);
31
+
32
+#endif
33
+

+ 66
- 65
makefile View File

@@ -1,65 +1,66 @@
1
-MCU = attiny85
2
-F_CPU = 16000000
3
-RM = rm -rf
4
-EXTRAINCDIR = include
5
-CSTANDARD = gnu99
6
-
7
-GCC = avr-gcc
8
-SIZE = avr-size
9
-OBJCOPY = avr-objcopy
10
-OBJDUMP = avr-objdump
11
-AVRDUDE = avrdude
12
-
13
-SRC = src/cc2500_BB_SPI.c
14
-SRC += src/frsky_arduino_rx_complete.c
15
-
16
-OBJ = $(SRC:.c=.o)
17
-
18
-CARGS = -mmcu=$(MCU)
19
-CARGS += -I$(EXTRAINCDIR)
20
-CARGS += -Os
21
-CARGS += -funsigned-char
22
-CARGS += -funsigned-bitfields
23
-CARGS += -fpack-struct
24
-CARGS += -fshort-enums
25
-CARGS += -Wall -pedantic -Wstrict-prototypes -Wshadow
26
-CARGS += -Wpointer-arith -Wcast-qual -Wextra
27
-CARGS += -Wno-write-strings -Wno-unused-parameter
28
-CARGS += -std=$(CSTANDARD)
29
-CARGS += -DF_CPU=$(F_CPU)
30
-#CARGS += -lm -lprintf_flt
31
-CARGS += -ffunction-sections
32
-
33
-LINKER = -Wl,--relax
34
-#LINKER = -Wl,--relax,-u,vfprintf,-lm,-lprintf_flt,-u,vfscanf,-lscanf_flt
35
-#LINKER += -Wl,--defsym=__heap_start=0x802200,--defsym=__heap_end=0x80ffff
36
-#LINKER += -Wl,-gc-sections
37
-
38
-PROGRAMMER = avrisp2
39
-ISPPORT = usb
40
-
41
-TARGET = rx
42
-
43
-all: $(TARGET).hex
44
-
45
-%.o: %.c
46
-	$(GCC) -c $< -o $@ $(CARGS)
47
-
48
-$(TARGET).elf: $(OBJ)
49
-	$(GCC) $(CARGS) $(OBJ) --output $@ $(LINKER) -Wl,-Map -Wl,$(@:.elf=.map)
50
-	$(SIZE) --mcu=$(MCU) -C $@
51
-
52
-$(TARGET).hex: $(TARGET).elf
53
-	$(OBJCOPY) -O ihex $< $@
54
-	$(OBJDUMP) -h -S $< > $(@:.hex=.lss)
55
-
56
-program: $(TARGET).hex
57
-	$(AVRDUDE) -p $(MCU) -c $(PROGRAMMER) -P $(ISPPORT) -e -U $(TARGET).hex
58
-
59
-clean:
60
-	$(RM) $(OBJ)
61
-	$(RM) *.o
62
-	$(RM) *.elf
63
-	$(RM) *.hex
64
-	$(RM) *.lss
65
-	$(RM) *.map
1
+MCU = attiny85
2
+F_CPU = 16000000
3
+RM = rm -rf
4
+EXTRAINCDIR = include
5
+CSTANDARD = gnu99
6
+
7
+GCC = avr-gcc
8
+SIZE = avr-size
9
+OBJCOPY = avr-objcopy
10
+OBJDUMP = avr-objdump
11
+AVRDUDE = avrdude
12
+
13
+SRC = src/spi.c
14
+SRC += src/cc2500.c
15
+SRC += src/frsky_arduino_rx_complete.c
16
+
17
+OBJ = $(SRC:.c=.o)
18
+
19
+CARGS = -mmcu=$(MCU)
20
+CARGS += -I$(EXTRAINCDIR)
21
+CARGS += -Os
22
+CARGS += -funsigned-char
23
+CARGS += -funsigned-bitfields
24
+CARGS += -fpack-struct
25
+CARGS += -fshort-enums
26
+CARGS += -Wall -pedantic -Wstrict-prototypes -Wshadow
27
+CARGS += -Wpointer-arith -Wcast-qual -Wextra
28
+CARGS += -Wno-write-strings -Wno-unused-parameter
29
+CARGS += -std=$(CSTANDARD)
30
+CARGS += -DF_CPU=$(F_CPU)
31
+#CARGS += -lm -lprintf_flt
32
+CARGS += -ffunction-sections
33
+
34
+LINKER = -Wl,--relax
35
+#LINKER = -Wl,--relax,-u,vfprintf,-lm,-lprintf_flt,-u,vfscanf,-lscanf_flt
36
+#LINKER += -Wl,--defsym=__heap_start=0x802200,--defsym=__heap_end=0x80ffff
37
+#LINKER += -Wl,-gc-sections
38
+
39
+PROGRAMMER = avrisp2
40
+ISPPORT = usb
41
+
42
+TARGET = rx
43
+
44
+all: $(TARGET).hex
45
+
46
+%.o: %.c
47
+	$(GCC) -c $< -o $@ $(CARGS)
48
+
49
+$(TARGET).elf: $(OBJ)
50
+	$(GCC) $(CARGS) $(OBJ) --output $@ $(LINKER) -Wl,-Map -Wl,$(@:.elf=.map)
51
+	$(SIZE) --mcu=$(MCU) -C $@
52
+
53
+$(TARGET).hex: $(TARGET).elf
54
+	$(OBJCOPY) -O ihex $< $@
55
+	$(OBJDUMP) -h -S $< > $(@:.hex=.lss)
56
+
57
+program: $(TARGET).hex
58
+	$(AVRDUDE) -p $(MCU) -c $(PROGRAMMER) -P $(ISPPORT) -e -U $(TARGET).hex
59
+
60
+clean:
61
+	$(RM) $(OBJ)
62
+	$(RM) *.o
63
+	$(RM) *.elf
64
+	$(RM) *.hex
65
+	$(RM) *.lss
66
+	$(RM) *.map

+ 72
- 0
src/cc2500.c View File

@@ -0,0 +1,72 @@
1
+/*
2
+ * CC2500 helper routines
3
+ */
4
+
5
+#include <util/delay.h>
6
+#include "spi.h"
7
+#include "cc2500.h"
8
+
9
+void cc2500ReadFifo(uint8_t *dpbuffer, int len) {
10
+    cc2500ReadRegisterMulti(CC2500_3F_RXFIFO | CC2500_READ_BURST, dpbuffer, len);
11
+}
12
+
13
+void cc2500ReadRegisterMulti(uint8_t address, uint8_t data[], uint8_t length) {
14
+    CS_off;
15
+    spi_write(address);
16
+    for (uint8_t i = 0; i < length; i++) {
17
+        data[i] = spi_read();
18
+    }
19
+    CS_on;
20
+}
21
+
22
+void cc2500WriteRegisterMulti(uint8_t address, const uint8_t data[], uint8_t length) {
23
+    CS_off;
24
+    spi_write(CC2500_WRITE_BURST | address);
25
+    for (uint8_t i = 0; i < length; i++) {
26
+        spi_write(data[i]);
27
+    }
28
+    CS_on;
29
+}
30
+
31
+void cc2500WriteFifo(uint8_t *dpbuffer, uint8_t len) {
32
+    cc2500Strobe(CC2500_SFTX); // 0x3B
33
+    cc2500WriteRegisterMulti(CC2500_3F_TXFIFO, dpbuffer, len);
34
+    cc2500Strobe(CC2500_STX); // 0x35
35
+}
36
+
37
+void cc2500WriteReg(uint8_t address, uint8_t data) {
38
+    CS_off;
39
+    spi_write(address);
40
+    NOP();
41
+    spi_write(data);
42
+    CS_on;
43
+}
44
+
45
+unsigned char cc2500ReadReg(unsigned char address) {
46
+    uint8_t result;
47
+    CS_off;
48
+    address |= 0x80; // bit 7 =1 for reading
49
+    spi_write(address);
50
+    result = spi_read();
51
+    CS_on;
52
+    return result;
53
+}
54
+
55
+void cc2500Strobe(uint8_t address) {
56
+    CS_off;
57
+    spi_write(address);
58
+    CS_on;
59
+}
60
+
61
+void cc2500ResetChip(void) {
62
+    // Toggle chip select signal
63
+    CS_on;
64
+    _delay_us(30);
65
+    CS_off;
66
+    _delay_us(30);
67
+    CS_on;
68
+    _delay_us(45);
69
+    cc2500Strobe(CC2500_SRES);
70
+    _delay_ms(100);
71
+}
72
+

+ 0
- 122
src/cc2500_BB_SPI.c View File

@@ -1,122 +0,0 @@
1
-//-------------------------------
2
-//-------------------------------
3
-//CC2500 SPI routines
4
-//-------------------------------
5
-//-------------------------------
6
-#include <util/delay.h>
7
-
8
-void cc2500_readFifo(uint8_t *dpbuffer, int len)
9
-{
10
-    ReadRegisterMulti(CC2500_3F_RXFIFO | CC2500_READ_BURST, dpbuffer, len);
11
-}
12
-
13
-//----------------------
14
-static void ReadRegisterMulti(uint8_t address, uint8_t data[], uint8_t length)
15
-{
16
-    unsigned char i;
17
-
18
-    CS_off;
19
-    _spi_write(address);
20
-    for (i = 0; i < length; i++) {
21
-        data[i] = _spi_read();
22
-    }
23
-    CS_on;
24
-}
25
-
26
-//*********************************************
27
-
28
-void CC2500_WriteRegisterMulti(uint8_t address, const uint8_t data[], uint8_t length)
29
-{
30
-    CS_off;
31
-    _spi_write(CC2500_WRITE_BURST | address);
32
-    for (int i = 0; i < length; i++) {
33
-        _spi_write(data[i]);
34
-    }
35
-    CS_on;
36
-}
37
-
38
-void cc2500_writeFifo(uint8_t *dpbuffer, uint8_t len)
39
-{
40
-    cc2500_strobe(CC2500_SFTX);//0x3B
41
-    CC2500_WriteRegisterMulti(CC2500_3F_TXFIFO, dpbuffer, len);
42
-    cc2500_strobe(CC2500_STX);//0x35
43
-}
44
-
45
-//--------------------------------------
46
-void _spi_write(uint8_t command)
47
-{
48
-    uint8_t n = 8;
49
-    SCK_off;//SCK start low
50
-    MO_off;
51
-    while (n--) {
52
-        if (command & 0x80)
53
-            MO_on;
54
-        else
55
-            MO_off;
56
-        SCK_on;
57
-        NOP();
58
-        SCK_off;
59
-        command = command << 1;
60
-    }
61
-    MO_on;
62
-}
63
-
64
-//----------------------------
65
-void cc2500_writeReg(uint8_t address, uint8_t data)  //same as 7105
66
-{
67
-    CS_off;
68
-    _spi_write(address);
69
-    NOP();
70
-    _spi_write(data);
71
-    CS_on;
72
-}
73
-
74
-uint8_t _spi_read(void)
75
-{
76
-    uint8_t result;
77
-    uint8_t i;
78
-    result = 0;
79
-    for (i = 0; i < 8; i++) {
80
-        if (MI_1) ///
81
-            result = (result << 1) | 0x01;
82
-        else
83
-            result = result << 1;
84
-        SCK_on;
85
-        NOP();
86
-        SCK_off;
87
-        NOP();
88
-    }
89
-    return result;
90
-}
91
-
92
-//--------------------------------------------
93
-unsigned char cc2500_readReg(unsigned char address)
94
-{
95
-    uint8_t result;
96
-    CS_off;
97
-    address |= 0x80; //bit 7 =1 for reading
98
-    _spi_write(address);
99
-    result = _spi_read();
100
-    CS_on;
101
-    return (result);
102
-}
103
-//------------------------
104
-void cc2500_strobe(uint8_t address)
105
-{
106
-    CS_off;
107
-    _spi_write(address);
108
-    CS_on;
109
-}
110
-//------------------------
111
-void cc2500_resetChip(void)
112
-{
113
-    // Toggle chip select signal
114
-    CS_on;
115
-    _delay_us(30);
116
-    CS_off;
117
-    _delay_us(30);
118
-    CS_on;
119
-    _delay_us(45);
120
-    cc2500_strobe(CC2500_SRES);
121
-    _delay_ms(100);
122
-}

+ 59
- 0
src/spi.c View File

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

Loading…
Cancel
Save