瀏覽代碼

Now seems to be working on mbed

Thomas Buck 8 年之前
父節點
當前提交
ac19426f8b
共有 8 個檔案被更改,包括 206 行新增42 行删除
  1. 27
    18
      include/arm/spi.h
  2. 6
    5
      include/timer.h
  3. 4
    9
      makefile
  4. 3
    0
      src/arm/cppm.cpp
  5. 55
    9
      src/arm/serial.cpp
  6. 84
    0
      src/arm/spi.cpp
  7. 23
    1
      src/arm/timer.cpp
  8. 4
    0
      src/spi.c

+ 27
- 18
include/arm/spi.h 查看文件

@@ -5,30 +5,39 @@
5 5
 #ifndef _ARM_SPI_H
6 6
 #define _ARM_SPI_H
7 7
 
8
-// TODO obviously
8
+#include <stdint.h>
9 9
 
10
-#define SCK_on
11
-#define SCK_off
12
-#define SCK_dir
10
+#define DIR_OUTPUT 0
11
+#define DIR_INPUT 1
13 12
 
14
-#define MO_on
15
-#define MO_off
16
-#define MO_dir
13
+void gpioInit(void);
14
+void gpioDir(uint8_t num, uint8_t dir);
15
+void gpioSet(uint8_t num, uint8_t val);
16
+uint8_t gpioGet(uint8_t num);
17 17
 
18
-#define CS_on
19
-#define CS_off
20
-#define CS_dir
18
+#define SCK_on gpioSet(22, 1)
19
+#define SCK_off gpioSet(22, 0)
20
+#define SCK_dir gpioDir(22, DIR_OUTPUT)
21 21
 
22
-#define MI_1 1
23
-#define MI_0 0
24
-#define MI_dir
22
+#define MO_on gpioSet(21, 1)
23
+#define MO_off gpioSet(21, 0)
24
+#define MO_dir gpioDir(21, DIR_OUTPUT)
25 25
 
26
-#define GDO_1 1
27
-#define GDO_0 0
28
-#define GDO_dir
26
+#define CS_on gpioSet(24, 1)
27
+#define CS_off gpioSet(24, 0)
28
+#define CS_dir gpioDir(24, DIR_OUTPUT)
29 29
 
30
-// TODO delay 62.5ns!!
31
-#define NOP()
30
+#define MI_1 gpioGet(23)
31
+#define MI_0 gpioGet(23)
32
+#define MI_dir gpioDir(23, DIR_INPUT)
33
+
34
+#define GDO_1 gpioGet(25)
35
+#define GDO_0 gpioGet(25)
36
+#define GDO_dir gpioDir(25, DIR_INPUT)
37
+
38
+#include "timer.h"
39
+
40
+#define NOP() delay_us(1) // could be 62.5ns
32 41
 
33 42
 #endif
34 43
 

+ 6
- 5
include/timer.h 查看文件

@@ -9,6 +9,8 @@
9 9
 
10 10
 #ifdef __AVR__
11 11
 
12
+typedef uint64_t time_t;
13
+
12 14
 #include <util/delay.h>
13 15
 
14 16
 #define delay_us(x) _delay_us(x)
@@ -16,13 +18,12 @@
16 18
 
17 19
 #else
18 20
 
19
-// TODO!!!
20
-#define delay_us(x)
21
-#define delay_ms(x)
21
+typedef long int time_t;
22 22
 
23
-#endif
23
+void delay_us(int x);
24
+void delay_ms(int x);
24 25
 
25
-typedef uint64_t time_t;
26
+#endif
26 27
 
27 28
 void timerInit(void);
28 29
 time_t timerGet(void);

+ 4
- 9
makefile 查看文件

@@ -46,6 +46,7 @@ endif
46 46
 else
47 47
 	CPPSRC += src/arm/cppm.cpp
48 48
 	CPPSRC += src/arm/serial.cpp
49
+	CPPSRC += src/arm/spi.cpp
49 50
 	CPPSRC += src/arm/timer.cpp
50 51
 endif
51 52
 
@@ -76,7 +77,7 @@ else
76 77
 	CARGS += -Ilib
77 78
 	CARGS += -Os
78 79
 	CARGS += -std=$(CSTANDARD)
79
-	CARGS += -Wall -Wextra -pedantic #-Werror
80
+	CARGS += -Wall -Wextra -pedantic -Werror
80 81
 	CARGS += -Wno-main
81 82
 	CARGS += -mcpu=$(MCU)
82 83
 	CARGS += -mthumb
@@ -85,7 +86,7 @@ else
85 86
 	CARGS += -fomit-frame-pointer
86 87
 	CARGS += $(PRE_DEFS)
87 88
 	CPPARGS = -I$(EXTRAINCDIR)
88
-	CPPARGS += -Ilib
89
+	CPPARGS += -Ilib/mbed
89 90
 	CPPARGS += -Ilib/mbed/TARGET_LPC1768
90 91
 	CPPARGS += -Ilib/mbed/TARGET_LPC1768/TARGET_NXP
91 92
 	CPPARGS += -Ilib/mbed/TARGET_LPC1768/TARGET_NXP/TARGET_LPC176X
@@ -93,7 +94,7 @@ else
93 94
 	CPPARGS += -Ilib/mbed/TARGET_LPC1768/TOOLCHAIN_GCC_ARM
94 95
 	CPPARGS += -Os
95 96
 	CPPARGS += -std=$(CPPSTANDARD)
96
-	CPPARGS += -Wall -Wextra -pedantic #-Werror
97
+	CPPARGS += -Wall -Wextra -pedantic -Werror
97 98
 	CPPARGS += -Wno-main
98 99
 	CPPARGS += -mcpu=$(MCU)
99 100
 	CPPARGS += -mthumb
@@ -196,13 +197,7 @@ $(BUILDDIR)/$(TARGET).elf: $(OBJ)
196 197
 $(BUILDDIR)/$(TARGET).hex: $(BUILDDIR)/$(TARGET).elf
197 198
 	$(OBJCOPY) -O ihex $< $@
198 199
 	$(OBJDUMP) -h -S $< > $(@:.hex=.lss)
199
-	@echo "*****"
200
-	@echo "***** You must modify vector checksum value in *.bin and *.hex files."
201
-	@echo "*****"
202 200
 
203 201
 $(BUILDDIR)/$(TARGET).bin: $(BUILDDIR)/$(TARGET).elf
204 202
 	$(OBJCOPY) -O binary $< $@
205
-	@echo "*****"
206
-	@echo "***** You must modify vector checksum value in *.bin and *.hex files."
207
-	@echo "*****"
208 203
 

+ 3
- 0
src/arm/cppm.cpp 查看文件

@@ -15,6 +15,9 @@ void cppmInit(void) {
15 15
 }
16 16
 
17 17
 void cppmCopy(uint16_t *data) {
18
+    for (int i = 0; i < CHANNELS; i++) {
19
+        cppmData[i] = data[i];
20
+    }
18 21
 }
19 22
 
20 23
 }

+ 55
- 9
src/arm/serial.cpp 查看文件

@@ -2,37 +2,77 @@
2 2
  * serial.c
3 3
  */
4 4
 
5
+#pragma GCC diagnostic push
6
+#pragma GCC diagnostic ignored "-Wunused-parameter"
7
+#pragma GCC diagnostic ignored "-pedantic"
8
+#include "mbed.h"
9
+#pragma GCC diagnostic pop
10
+
11
+#define UART_COUNT 1
12
+
13
+static Serial serial(USBTX, USBRX);
14
+
5 15
 extern "C" {
6 16
 
7 17
 #include "serial.h"
8 18
 
9 19
 uint8_t serialAvailable(void) {
10
-    return 1;
20
+    return UART_COUNT;
11 21
 }
12 22
 
13 23
 void serialInit(uint8_t uart, uint16_t baud) {
24
+    if (uart >= UART_COUNT)
25
+        return;
26
+
27
+    serial.baud(baud);
14 28
 }
15 29
 
16 30
 void serialClose(uint8_t uart) {
31
+    if (uart >= UART_COUNT)
32
+        return;
33
+
34
+    // Nothing to do
17 35
 }
18 36
 
19 37
 uint8_t serialHasChar(uint8_t uart) {
20
-    return 0;
38
+    if (uart >= UART_COUNT)
39
+        return 0;
40
+
41
+    return serial.readable();
21 42
 }
22 43
 
23 44
 uint8_t serialGetBlocking(uint8_t uart) {
24
-    return 0;
45
+    if (uart >= UART_COUNT)
46
+        return 0;
47
+
48
+    if (!serial.readable()) {
49
+        return 0;
50
+    }
51
+
52
+    return serial.getc();
25 53
 }
26 54
 
27 55
 uint8_t serialGet(uint8_t uart) {
28
-    return 0;
56
+    if (uart >= UART_COUNT)
57
+        return 0;
58
+
59
+    while (!serial.readable());
60
+
61
+    return serial.getc();
29 62
 }
30 63
 
31 64
 uint8_t serialRxBufferFull(uint8_t uart) {
32
-    return 0;
65
+    if (uart >= UART_COUNT)
66
+        return 0;
67
+
68
+    return serial.readable();
33 69
 }
34 70
 
35 71
 void serialWrite(uint8_t uart, uint8_t data) {
72
+    if (uart >= UART_COUNT)
73
+        return;
74
+
75
+    serial.putc(data);
36 76
 }
37 77
 
38 78
 void serialWriteString(uint8_t uart, const char *data) {
@@ -54,14 +94,14 @@ void serialWriteHex(uint8_t uart, uint8_t value) {
54 94
     }
55 95
     buff[1] = value;
56 96
 
57
-    if ((buff[0] >= 0) && (buff[0] <= 9)) {
97
+    if (buff[0] <= 9) {
58 98
         buff[0] += '0';
59 99
     } else {
60 100
         buff[0] -= 10;
61 101
         buff[0] += 'A';
62 102
     }
63 103
 
64
-    if ((buff[1] >= 0) && (buff[1] <= 9)) {
104
+    if (buff[1] <= 9) {
65 105
         buff[1] += '0';
66 106
     } else {
67 107
         buff[1] -= 10;
@@ -141,11 +181,17 @@ void serialWriteUnsigned64(uint8_t uart, uint64_t value) {
141 181
 }
142 182
 
143 183
 uint8_t serialTxBufferFull(uint8_t uart) {
144
-    return 0;
184
+    if (uart >= UART_COUNT)
185
+        return 1;
186
+
187
+    return !serial.writeable();
145 188
 }
146 189
 
147 190
 uint8_t serialTxBufferEmpty(uint8_t uart) {
148
-    return 0;
191
+    if (uart >= UART_COUNT)
192
+        return 0;
193
+
194
+    return serial.writeable();
149 195
 }
150 196
 
151 197
 }

+ 84
- 0
src/arm/spi.cpp 查看文件

@@ -0,0 +1,84 @@
1
+/*
2
+ * mbed GPIO
3
+ */
4
+
5
+#pragma GCC diagnostic push
6
+#pragma GCC diagnostic ignored "-Wunused-parameter"
7
+#pragma GCC diagnostic ignored "-pedantic"
8
+#include "mbed.h"
9
+#pragma GCC diagnostic pop
10
+
11
+extern "C" {
12
+#include "spi.h"
13
+}
14
+
15
+static PinName numToPin(uint8_t num) {
16
+    switch (num) {
17
+      case 20:
18
+        return p20;
19
+      case 21:
20
+        return p21;
21
+      case 22:
22
+        return p22;
23
+      case 23:
24
+        return p23;
25
+      case 24:
26
+        return p24;
27
+      case 25:
28
+        return p25;
29
+      case 26:
30
+        return p26;
31
+    }
32
+
33
+    return p5;
34
+}
35
+
36
+#define GPIO_MAX_COUNT 42
37
+DigitalInOut *gpios[GPIO_MAX_COUNT];
38
+
39
+extern "C" {
40
+
41
+void gpioInit(void) {
42
+    for (int i = 0; i < GPIO_MAX_COUNT; i++) {
43
+        gpios[i] = NULL;
44
+    }
45
+}
46
+
47
+void gpioDir(uint8_t num, uint8_t dir) {
48
+    PinName n = numToPin(num);
49
+
50
+    if (gpios[num] == NULL) {
51
+        gpios[num] = new DigitalInOut(n);
52
+    }
53
+
54
+    if (dir == DIR_OUTPUT) {
55
+        gpios[num]->output();
56
+    } else {
57
+        gpios[num]->input();
58
+    }
59
+}
60
+
61
+void gpioSet(uint8_t num, uint8_t val) {
62
+    PinName n = numToPin(num);
63
+
64
+    if (gpios[num] == NULL) {
65
+        gpios[num] = new DigitalInOut(n);
66
+        gpios[num]->output();
67
+    }
68
+
69
+    gpios[num]->write(val);
70
+}
71
+
72
+uint8_t gpioGet(uint8_t num) {
73
+    PinName n = numToPin(num);
74
+
75
+    if (gpios[num] == NULL) {
76
+        gpios[num] = new DigitalInOut(n);
77
+        gpios[num]->input();
78
+    }
79
+
80
+    return gpios[num]->read();
81
+}
82
+
83
+}
84
+

+ 23
- 1
src/arm/timer.cpp 查看文件

@@ -2,15 +2,37 @@
2 2
  * Time-Keeping helper
3 3
  */
4 4
 
5
+#pragma GCC diagnostic push
6
+#pragma GCC diagnostic ignored "-Wunused-parameter"
7
+#pragma GCC diagnostic ignored "-pedantic"
8
+#include "mbed.h"
9
+#pragma GCC diagnostic pop
10
+
11
+static Ticker t;
12
+static time_t systemTime = 0;
13
+
14
+static void timerISR(void) {
15
+    systemTime++;
16
+}
17
+
5 18
 extern "C" {
6 19
 
7 20
 #include "timer.h"
8 21
 
22
+void delay_us(int x) {
23
+    wait_us(x);
24
+}
25
+
26
+void delay_ms(int x) {
27
+    wait_ms(x);
28
+}
29
+
9 30
 void timerInit(void) {
31
+    t.attach_us(&timerISR, 100);
10 32
 }
11 33
 
12 34
 time_t timerGet(void) {
13
-    return 0;
35
+    return systemTime;
14 36
 }
15 37
 
16 38
 }

+ 4
- 0
src/spi.c 查看文件

@@ -7,6 +7,10 @@
7 7
 #include "spi.h"
8 8
 
9 9
 void spiInit(void) {
10
+#if !defined(__AVR__)
11
+    gpioInit();
12
+#endif
13
+
10 14
     MI_dir;
11 15
     MO_dir;
12 16
     SCK_dir;

Loading…
取消
儲存