Skip to content

Commit 1f2ed7d

Browse files
committed
Put GPS init/thread code into its own file
1 parent 8c55963 commit 1f2ed7d

File tree

4 files changed

+73
-38
lines changed

4 files changed

+73
-38
lines changed

GPS.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
ChibiCopter - https://github.com/grantmd/ChibiCopter
3+
A quadcopter platform running under ChibiOS/RT.
4+
5+
Talks to a GPS receiver over serial
6+
*/
7+
8+
#include "ch.h"
9+
#include "hal.h"
10+
11+
#include "GPS.h"
12+
#include "TinyGPS.h"
13+
14+
/*
15+
* GPS setup
16+
*/
17+
static const SerialConfig sd1cfg = {
18+
57600,
19+
0,
20+
USART_CR2_STOP1_BITS | USART_CR2_LINEN,
21+
0
22+
};
23+
24+
static WORKING_AREA(GPSWA, 128);
25+
static msg_t GPS(void *arg){
26+
27+
(void)arg;
28+
chRegSetThreadName("GPS");
29+
while (TRUE){
30+
unsigned char newdata = 0;
31+
// Read a byte off the GPS
32+
uint8_t c = chIOGet((BaseChannel *)&SD1);
33+
if (TinyGPS_encode(c)){
34+
newdata = 1;
35+
}
36+
}
37+
return 0;
38+
}
39+
40+
/*
41+
* Activates the serial driver 1 using the driver default configuration, but at 57600
42+
* PA9(TX) and PA10(RX) are routed to USART1.
43+
*/
44+
45+
void GPSInit(void){
46+
47+
sdStart(&SD1, &sd1cfg);
48+
palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(7)); // not currently connected
49+
palSetPadMode(GPIOA, 10, PAL_MODE_ALTERNATE(7)); // incoming data from the GPS
50+
51+
chThdCreateStatic(GPSWA, sizeof(GPSWA), NORMALPRIO, GPS, NULL);
52+
}

GPS.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
ChibiCopter - https://github.com/grantmd/ChibiCopter
3+
A quadcopter platform running under ChibiOS/RT.
4+
5+
Talks to a GPS over serial
6+
*/
7+
8+
#ifndef _GPS_H_
9+
#define _GPS_H_
10+
11+
// Public functions
12+
void GPSInit(void);
13+
14+
#endif /* _GPS_H_ */

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ CSRC = $(PORTSRC) \
8181
$(CHIBIOS)/os/various/lis302dl.c \
8282
$(CHIBIOS)/os/various/chprintf.c \
8383
comms.c \
84+
GPS.c \
8485
TinyGPS.c \
8586
Motors.c \
8687
Spektrum.c \

main.c

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "lis302dl.h"
1414

1515
#include "Comms.h"
16-
#include "TinyGPS.h"
16+
#include "GPS.h"
1717
#include "Motors.h"
1818
#include "Spektrum.h"
1919

@@ -55,32 +55,6 @@ static msg_t Blink(void *arg){
5555
return 0;
5656
}
5757

58-
/*
59-
* GPS setup
60-
*/
61-
static const SerialConfig sd1cfg = {
62-
57600,
63-
0,
64-
USART_CR2_STOP1_BITS | USART_CR2_LINEN,
65-
0
66-
};
67-
68-
static WORKING_AREA(GPSWA, 128);
69-
static msg_t GPS(void *arg){
70-
71-
(void)arg;
72-
chRegSetThreadName("GPS");
73-
while (TRUE){
74-
unsigned char newdata = 0;
75-
// Read a byte off the GPS
76-
uint8_t c = chIOGet((BaseChannel *)&SD1);
77-
if (TinyGPS_encode(c)){
78-
newdata = 1;
79-
}
80-
}
81-
return 0;
82-
}
83-
8458
/*
8559
* Application entry point.
8660
*/
@@ -102,16 +76,6 @@ int main(void){
10276

10377
CommsInit();
10478

105-
/*
106-
* Activates the serial driver 1 using the driver default configuration, but at 57600
107-
* PA9(TX) and PA10(RX) are routed to USART1.
108-
*/
109-
110-
sdStart(&SD1, &sd1cfg);
111-
palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(7)); // not currently connected
112-
palSetPadMode(GPIOA, 10, PAL_MODE_ALTERNATE(7)); // incoming data from the GPS
113-
//TinyGPS_init();
114-
11579
/*
11680
* SPI1 I/O pins setup.
11781
*/
@@ -135,11 +99,15 @@ int main(void){
13599
*/
136100
MotorsInit();
137101

102+
/*
103+
* GPS
104+
*/
105+
GPSInit();
106+
138107
/*
139108
* Creates the threads
140109
*/
141110
chThdCreateStatic(BlinkWA, sizeof(BlinkWA), NORMALPRIO, Blink, NULL);
142-
chThdCreateStatic(GPSWA, sizeof(GPSWA), NORMALPRIO, GPS, NULL);
143111

144112
/*
145113
* Normal main() thread activity

0 commit comments

Comments
 (0)