root/RF_BT_Tail/JDY08.c

Revision 244, 3.9 kB (checked in by phil, 4 years ago)

added initial Tail Source Package

Line 
1 // File: JDY08.c
2 //------------------------------------------------------------------------------
3 // Author: Giovanni de Sanctis
4 // Email: info@lateral-technologies.com
5 //------------------------------------------------------------------------------
6 // Date: Dec 2018
7 // HAL for the JDY-08 Bluetooth module (based on the TI chip CC2541)
8 //------------------------------------------------------------------------------
9
10 #include "JDY08.h"
11 #include "Definitions.h"
12 #include "./mcc_generated_files/mcc.h"
13 #include <string.h>
14 #include <stdint.h>
15
16 //AT commands
17 #define HOSTEN  "AT+HOSTEN0"                    //Sets the right mode for BT serial slave
18 #define RST             "AT+RST"                                //Resets the BT module
19 #define NAME    "AT+NAME(!)Tail1"               //Sets the BT name
20 #define REVERSE "AT+REVERSE1"                   //
21 #define SPLASH  "Tail sw.ver."STR_VER   //Sent after initialisation
22
23 char    rxBuff[RX_BUFF_LEN];                    //rx buffer
24 uint8_t rxRdPtr;                                                //Reading Pointer to rxBuff     
25
26 //Return the pointer to the buffer read, or a NULL pointer if no chars were received
27 char* btReadStr()
28 {
29   char c;                                                               //temp char
30   uint8_t i;                                                    //iterator
31   uint8_t rxPtr;                                                //index to the rx buffer
32  
33   //Flush buffer
34   memset(rxBuff,0,sizeof(rxBuff));
35   rxPtr=0;
36  
37   //If no data received then return
38   if (!EUSART_is_rx_ready()) return 0;
39    
40   while (EUSART_is_rx_ready())                  //Keeps reading until there is data
41   {
42         c=EUSART_Read();
43         rxBuff[rxPtr++]=c;
44         if (c=='\n') break;
45         if (rxPtr>sizeof(rxBuff)-1) break;
46   }
47  
48   rxBuff[rxPtr]=0;                                              //Terminates the string
49   rxRdPtr = 0;                                                  //Resets the buffer read pointer
50  
51   //Convert to uppercase
52   for (i=0; (rxBuff[i]!=0 && i<RX_BUFF_LEN); i++)
53   {
54         c=rxBuff[i];
55         if (c>='a' && c<='z') rxBuff[i]=c-('a'-'A');
56   }
57  
58   if (rxPtr) return rxBuff;                             //Return pointer to string if not empty
59   else return 0;                                                //A null pointer means nothing was received
60 }
61
62 void btSendStr(const char* str,uint8_t len)
63 {
64   uint8_t i;
65  
66   if (len==0) len=strlen(str);//+1;             //if len=0 set len to strlen (+terminator)
67  
68   for (i=0; i<len; i++)
69   {
70         EUSART_Write(str[i]);
71   }
72 }
73
74 //Returns >0 if rx string contains the string passed
75 //Must be called after btReadStr()
76 //This is used to seek for a command string
77 uint8_t btRxStrContains(const char* str)
78 {
79   char* currPtr = strstr(&rxBuff[rxRdPtr],str);
80   if (currPtr)
81   {
82         rxRdPtr+=strlen(str);                   //Move pointer past the recognised substring
83         if (rxRdPtr>=sizeof(rxBuff)) rxRdPtr=sizeof(rxBuff);
84         return 1;
85   }
86   else currPtr=rxBuff;
87   return 0;
88 }
89
90 //Reads the next numerical value (must be preceded by the specified char)
91 //Must be called after btReadStr()
92 uint8_t btRxValue(uint16_t * const val,char * const type,const uint16_t maxVal)
93 {
94   char c;
95   uint8_t valOK = 0;
96  
97   *type = 0;
98   *val = 0;
99    
100   while (rxRdPtr<sizeof(rxBuff) )
101   {
102         c=rxBuff[rxRdPtr++];
103         if (valOK==0 && c>='A' && c<='Z')
104         {
105           *type = c;
106           *val = 0;
107           valOK = 0;
108         }
109         else if (*type && c>='0' && c<='9')
110         {
111           if (*val<maxVal)
112           *val = (*val)*10 + (uint16_t)(c-'0');
113           valOK = 1;
114         }
115         else
116           if (*type && valOK)
117           {
118                 if (rxRdPtr) rxRdPtr--;
119                 return 0;
120           }
121   }
122   return 1;
123 }
124
125 //Sends an AT command, waits for the response and returns the response string
126 uint8_t btSendATcmd(const char *cmd)
127 {
128   //Sends AT command (without the string termination)
129   while (*cmd)
130   {
131     EUSART_Write(*cmd++);
132   }
133   __delay_ms(50);
134   return 0;
135 }
136
137 void initJDY08()
138 {
139   __delay_ms(50);                                 //Wait for the serial line to stabilise
140  
141   //Serial port was already initialised in SYSTEM_Initialize()
142   btSendATcmd(HOSTEN);                    //Set proper mode of operation
143   btSendATcmd(RST);                               //Reset the module
144   __delay_ms(100);
145   btSendATcmd(NAME);                      //Make sure the BT name is correct
146   btSendATcmd(RST);                               //Reset the module
147   __delay_ms(100);
148   btSendATcmd(SPLASH);
149   //Flush any remaining data
150   while (EUSART_is_rx_ready()) EUSART_Read();
151  
152   rxRdPtr=0;
153 }
Note: See TracBrowser for help on using the browser.