Traxxas TQ 1st gen: try 5
[DIY-Multiprotocol-TX-Module.git] / Multiprotocol / Multi_Config.ino
blobf5852d9f3099ac7d1f90c07369ceaf31fbdfe49f
1 /*
2  This project is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License as published by
4  the Free Software Foundation, either version 3 of the License, or
5  (at your option) any later version.
7  Multiprotocol is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  GNU General Public License for more details.
12  You should have received a copy of the GNU General Public License
13  along with Multiprotocol.  If not, see <http://www.gnu.org/licenses/>.
14  */
16 #if defined(MULTI_CONFIG_INO)
18 #ifdef CYRF6936_INSTALLED
19 #include "iface_cyrf6936.h"
20 #endif
22 void CONFIG_write_GID(uint32_t id)
24         for(uint8_t i=0;i<4;i++)
25                 eeprom_write_byte((EE_ADDR)EEPROM_ID_OFFSET+i,id >> (i*8));
26         //eeprom_write_byte((EE_ADDR)(EEPROM_ID_OFFSET+10),0xf0);
29 void CONFIG_write_CID(uint8_t *data)
31         for(uint8_t i=0;i<6;i++)
32                 eeprom_write_byte((EE_ADDR)EEPROM_CID_OFFSET+i, data[i]);
33         //eeprom_write_byte((EE_ADDR)EEPROM_CID_INIT_OFFSET, 0xf0);
35 uint16_t CONFIG_callback()
37         static uint8_t line=0, page=0;
38         uint32_t id=0;
39         // [0] = page<<4|line number
40         // [1..6] = max 6 bytes
41         if(CONFIG_SerialRX)
42         {
43                 debug("config");
44                 for(uint8_t i=0; i<7; i++)
45                         debug("%02X ",CONFIG_SerialRX_val[i]);
46                 debugln("");
47                 CONFIG_SerialRX = false;
48                 switch(CONFIG_SerialRX_val[0]&0x0F)
49                 {
50                         //case 0:
51                                 // Page change
52                         //      break;
53                         case 1:
54                                 for(uint8_t i=0; i<4; i++)
55                                 {
56                                         id <<= 8;
57                                         id |= CONFIG_SerialRX_val[i+1];
58                                 }
59                                 debugln("Update ID to %lx", id);
60                                 CONFIG_write_GID(id);
61                                 break;
62                         case 2:
63                                 if(CONFIG_SerialRX_val[1]==0xAA)
64                                 {
65                                         #define STM32_UUID ((uint32_t *)0x1FFFF7E8)
66                                         id = STM32_UUID[0] ^ STM32_UUID[1] ^ STM32_UUID[2];
67                                         debugln("Reset GID to %lx", id);
68                                         CONFIG_write_GID(id);
69                                 }
70                                 break;
71 #ifdef CYRF6936_INSTALLED
72                         case 4:
73                                 debug("Update CID to ");
74                                 for(uint8_t i=0; i<6; i++)
75                                         debug("%02X ",CONFIG_SerialRX_val[i+1]);
76                                 debugln("");
77                                 CONFIG_write_CID(&CONFIG_SerialRX_val[1]);
78                         case 5:
79                                 if(CONFIG_SerialRX_val[1]==0xAA)
80                                 {
81                                         uint8_t data[6];
82                                         CYRF_WriteRegister(CYRF_25_MFG_ID, 0xFF);       /* Fuses power on */
83                                         CYRF_ReadRegisterMulti(CYRF_25_MFG_ID, data, 6);
84                                         CYRF_WriteRegister(CYRF_25_MFG_ID, 0x00);       /* Fuses power off */
85                                         debug("Reset CID to ");
86                                         for(uint8_t i=0; i<6; i++)
87                                                 debug("%02X ",data[i]);
88                                         debugln("");
89                                         CONFIG_write_CID(data);
90                                 }
91                                 break;
92 #endif
93                         case 7:
94                                 if(CONFIG_SerialRX_val[1]==0xAA)
95                                 {
96                                         debugln("Format EE");
97                                         #if defined(STM32_BOARD)
98                                                 EEPROM.format();
99                                         #else
100                                                 for (uint16_t i = 0; i < 512; i++)
101                                                 eeprom_write_byte((EE_ADDR)i, 0xFF);
102                                         #endif
103                                 }
104                                 break;
105                 }
106         }
108         if(     telemetry_link )
109                 return 10000;
110         // [0] = page<<4|line number
111         // line=0: VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION, VERSION_PATCH_LEVEL, Channel order:RUDDER<<6|THROTTLE<<4|ELEVATOR<<2|AILERON
112         // [1..21] = max 20 characters, any displayable chars followed by:
113         //    0x00    : end of line
114         //    0x80+len:  selectable text to follow
115         //    0x90+len:  selectable text to follow with "Are you sure?"
116         //    0xA0+len:  not editable dec value
117         //    0xB0+len:  editable dec value
118         //    0xC0+len:  not editable hex value
119         //    0xD0+len:  editable hex value
120         memset(&packet_in[1],0,20);
121         do
122         {
123                 packet_in[0] = (page<<4) | line;
124                 switch(line)
125                 {
126                         case 0:
127                                 packet_in[1]=VERSION_MAJOR;
128                                 packet_in[2]=VERSION_MINOR;
129                                 packet_in[3]=VERSION_REVISION;
130                                 packet_in[4]=VERSION_PATCH_LEVEL;
131                                 packet_in[5]=RUDDER<<6|THROTTLE<<4|ELEVATOR<<2|AILERON;
132                                 break;
133                         case 1:
134                                 //Global ID
135                                 #ifndef FORCE_GLOBAL_ID
136                                         memcpy(&packet_in[1],"Global ID",9);
137                                         packet_in[10] = 0xD0 + 4;
138                                 #else
139                                         memcpy(&packet_in[1],"Fixed ID ",9);
140                                         packet_in[10] = 0xC0 + 4;
141                                 #endif
142                                 MProtocol_id_master = random_id(EEPROM_ID_OFFSET,false);
143                                 set_rx_tx_addr(MProtocol_id_master);
144                                 for(uint8_t i=0; i<4; i++)
145                                         packet_in[11+i]=rx_tx_addr[i];
146                                 break;
147                         #if defined(STM32_BOARD) && not defined(FORCE_GLOBAL_ID)
148                         case 2:
149                                 //Reset global ID
150                                 packet_in[1] = 0x90+9;
151                                 memcpy(&packet_in[2],"Reset GID",9);
152                                 break;
153                         #endif
154 #ifdef CYRF6936_INSTALLED
155                         case 4:
156                                 //Cyrf ID
157                                 #ifndef FORCE_CYRF_ID
158                                         memcpy(&packet_in[1],"Cyrf ID",7);
159                                         packet_in[8] = 0xD0 + 6;
160                                         CYRF_GetMfgData(&packet_in[9]);
161                                 #else
162                                         memcpy(&packet_in[1],"Fixed CID",9);
163                                         packet_in[10] = 0xC0 + 6;
164                                         CYRF_GetMfgData(&packet_in[11]);
165                                 #endif
166                                 break;
167                         #ifndef FORCE_CYRF_ID
168                         case 5:
169                                 //Reset Cyrf ID
170                                 packet_in[1] = 0x90+9;
171                                 memcpy(&packet_in[2],"Reset CID",9);
172                                 break;
173                         #endif
174 #endif
175                         case 7:
176                                 packet_in[1] = 0x90+13;
177                                 memcpy(&packet_in[2],"Format EEPROM",13);
178                                 break;
179                 }
180                 line++;
181                 line %= 8;
182         }
183         while(packet_in[1]==0); // next line if empty
184         telemetry_link = 1;
185         return 10000;
188 void CONFIG_init()
192 #endif