1 /*!
2  * \file      gps-board.c
3  *
4  * \brief     Target board GPS driver implementation
5  *
6  * \copyright Revised BSD License, see section \ref LICENSE.
7  *
8  * \code
9  *                ______                              _
10  *               / _____)             _              | |
11  *              ( (____  _____ ____ _| |_ _____  ____| |__
12  *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
13  *               _____) ) ____| | | || |_| ____( (___| | | |
14  *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
15  *              (C)2013-2017 Semtech
16  *
17  * \endcode
18  *
19  * \author    Miguel Luis ( Semtech )
20  *
21  * \author    Gregory Cristian ( Semtech )
22  */
23 #include "board-config.h"
24 #include "board.h"
25 #include "gpio.h"
26 #include "gps.h"
27 #include "uart.h"
28 #include "lpm-board.h"
29 #include "rtc-board.h"
30 #include "gps-board.h"
31 
32 /*!
33  * FIFO buffers size
34  */
35 //#define FIFO_TX_SIZE                                128
36 #define FIFO_RX_SIZE                                128
37 
38 //uint8_t TxBuffer[FIFO_TX_SIZE];
39 static uint8_t RxBuffer[FIFO_RX_SIZE];
40 
41 /*!
42  * \brief Buffer holding the  raw data received from the gps
43  */
44 static uint8_t NmeaString[128];
45 
46 /*!
47  * \brief Maximum number of data byte that we will accept from the GPS
48  */
49 static volatile uint8_t NmeaStringSize = 0;
50 
51 static Gpio_t GpsPowerEn;
52 static Gpio_t GpsPps;
53 
54 static volatile bool GpsPowerEnInverted = false;
55 
56 extern Uart_t Uart1;
57 
GpsMcuOnPpsSignal(void * context)58 void GpsMcuOnPpsSignal( void* context )
59 {
60     bool parseData = false;
61 
62     GpsPpsHandler( &parseData );
63 
64     if( parseData == true )
65     {
66         // Disables lowest power modes
67         LpmSetStopMode( LPM_GPS_ID , LPM_DISABLE );
68 
69         UartInit( &Uart1, UART_1, GPS_UART_TX, GPS_UART_RX );
70         UartConfig( &Uart1, RX_ONLY, 9600, UART_8_BIT, UART_1_STOP_BIT, NO_PARITY, NO_FLOW_CTRL );
71     }
72 }
73 
GpsMcuInvertPpsTrigger(void)74 void GpsMcuInvertPpsTrigger( void )
75 {
76 
77 }
78 
GpsMcuInit(void)79 void GpsMcuInit( void )
80 {
81     NmeaStringSize = 0;
82 
83     switch( BoardGetVersion( ).Fields.Major )
84     {
85         case 2:
86             GpsPowerEnInverted = true;
87             break;
88         case 3:
89             GpsPowerEnInverted = false;
90             break;
91         default:
92             break;
93     }
94     GpioInit( &GpsPowerEn, GPS_POWER_ON, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 1 );
95 
96     GpioInit( &GpsPps, GPS_PPS, PIN_INPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
97     GpioSetInterrupt( &GpsPps, IRQ_FALLING_EDGE, IRQ_VERY_LOW_PRIORITY, &GpsMcuOnPpsSignal );
98 
99     FifoInit( &Uart1.FifoRx, RxBuffer, FIFO_RX_SIZE );
100     Uart1.IrqNotify = GpsMcuIrqNotify;
101 
102     GpsMcuStart( );
103 }
104 
GpsMcuStart(void)105 void GpsMcuStart( void )
106 {
107     if( GpsPowerEnInverted == true )
108     {
109         GpioWrite( &GpsPowerEn, 0 );    // power up the GPS
110     }
111     else
112     {
113         GpioWrite( &GpsPowerEn, 1 );    // power up the GPS
114     }
115 }
116 
GpsMcuStop(void)117 void GpsMcuStop( void )
118 {
119     if( GpsPowerEnInverted == true )
120     {
121         GpioWrite( &GpsPowerEn, 1 );    // power down the GPS
122     }
123     else
124     {
125         GpioWrite( &GpsPowerEn, 0 );    // power down the GPS
126     }
127 }
128 
GpsMcuProcess(void)129 void GpsMcuProcess( void )
130 {
131 
132 }
133 
GpsMcuIrqNotify(UartNotifyId_t id)134 void GpsMcuIrqNotify( UartNotifyId_t id )
135 {
136     uint8_t data;
137     if( id == UART_NOTIFY_RX )
138     {
139         if( UartGetChar( &Uart1, &data ) == 0 )
140         {
141             if( ( data == '$' ) || ( NmeaStringSize >= 127 ) )
142             {
143                 NmeaStringSize = 0;
144             }
145 
146             NmeaString[NmeaStringSize++] = ( int8_t )data;
147 
148             if( data == '\n' )
149             {
150                 NmeaString[NmeaStringSize++] = '\0';
151                 GpsParseGpsData( ( int8_t* )NmeaString, NmeaStringSize );
152                 UartDeInit( &Uart1 );
153                 // Enables lowest power modes
154                 LpmSetStopMode( LPM_GPS_ID , LPM_ENABLE );
155             }
156         }
157     }
158 }
159