1 /*!
2  * \file      pam7q.c
3  *
4  * \brief     PAM7Q GPS receiver 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 "utilities.h"
24 #include "i2c.h"
25 #include "pam7q.h"
26 
27 extern I2c_t I2c;
28 
29 static uint8_t I2cDeviceAddr = 0;
30 
PAM7QInit(void)31 void PAM7QInit( void )
32 {
33     PAM7QSetDeviceAddr( PAM7Q_I2C_ADDRESS );
34 }
35 
PAM7QGetGpsData(uint8_t * nmeaString,uint8_t * nmeaStringSize,uint16_t nmeaMaxStringSize)36 bool PAM7QGetGpsData( uint8_t *nmeaString, uint8_t *nmeaStringSize, uint16_t nmeaMaxStringSize )
37 {
38     uint8_t status;
39     uint16_t pendingBytes;
40     bool result = false;
41 
42     *nmeaStringSize = 0;
43 
44     status = PAM7QReadBuffer( MESSAGE_SIZE_1, nmeaString, 2 );
45 
46     if( status == LMN_STATUS_OK )
47     {
48         // build a 16bit number
49         pendingBytes = ( uint16_t )( ( nmeaString[0] << 8 ) | nmeaString[1] );
50 
51         // check for invalid length
52         if( pendingBytes == 0xFFFF )
53         {
54             pendingBytes = 0;
55         }
56         // just to buffer size
57         if( pendingBytes > ( nmeaMaxStringSize - 1 ) )
58         {
59             pendingBytes = nmeaMaxStringSize - 1;
60         }
61 
62         // read pending data from GPS module
63         status = PAM7QReadBuffer( PAYLOAD, nmeaString, pendingBytes );
64 
65         // make sure the string is terminated
66         if( status == LMN_STATUS_OK )
67         {
68             nmeaString[pendingBytes] = 0x00;
69 
70             // copy length indication to argument
71             *nmeaStringSize = pendingBytes;
72 
73             // return success only if there is data to process
74             if( pendingBytes > 0 )
75             {
76                 result = true;
77             }
78         }
79     }
80     return result;
81 }
82 
PAM7QGetDeviceAddr(void)83 uint8_t PAM7QGetDeviceAddr( void )
84 {
85     return I2cDeviceAddr;
86 }
87 
PAM7QSetDeviceAddr(uint8_t addr)88 void PAM7QSetDeviceAddr( uint8_t addr )
89 {
90     I2cDeviceAddr = addr;
91 }
92 
PAM7QWrite(uint8_t addr,uint8_t data)93 uint8_t PAM7QWrite( uint8_t addr, uint8_t data )
94 {
95     return PAM7QWriteBuffer( addr, &data, 1 );
96 }
97 
PAM7QWriteBuffer(uint8_t addr,uint8_t * data,uint8_t size)98 uint8_t PAM7QWriteBuffer( uint8_t addr, uint8_t *data, uint8_t size )
99 {
100     return I2cWriteMemBuffer( &I2c, I2cDeviceAddr << 1, addr, data, size );
101 }
102 
PAM7QRead(uint8_t addr,uint8_t * data)103 uint8_t PAM7QRead( uint8_t addr, uint8_t *data )
104 {
105     return PAM7QReadBuffer( addr, data, 1 );
106 }
107 
PAM7QReadBuffer(uint8_t addr,uint8_t * data,uint8_t size)108 uint8_t PAM7QReadBuffer( uint8_t addr, uint8_t *data, uint8_t size )
109 {
110     return I2cReadMemBuffer( &I2c, I2cDeviceAddr << 1, addr, data, size );
111 }
112