dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
serial_4way.c
Go to the documentation of this file.
1 /*
2  * This file is part of Cleanflight.
3  *
4  * Cleanflight is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Cleanflight is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
16  * Author: 4712
17 */
18 
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include <string.h>
22 
23 #include "platform.h"
24 
25 #ifdef USE_SERIAL_4WAY_BLHELI_INTERFACE
26 
27 #ifndef DRONIN_TARGET
28 
29 #include "drivers/buf_writer.h"
30 #include "drivers/io.h"
31 #include "drivers/serial.h"
32 #include "drivers/timer.h"
33 #include "drivers/pwm_output.h"
34 #include "drivers/light_led.h"
35 #include "drivers/system.h"
36 
37 #include "flight/mixer.h"
38 
39 #include "io/beeper.h"
40 #endif
41 #include "io/serial_4way.h"
42 
43 #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
45 #endif
46 #if defined(USE_SERIAL_4WAY_SK_BOOTLOADER)
48 #endif
49 
50 #if defined(USE_HAL_DRIVER)
51 #define Bit_RESET GPIO_PIN_RESET
52 #endif
53 
54 #ifndef DRONIN_TARGET
55 #define USE_TXRX_LED
56 #endif
57 
58 #ifdef USE_TXRX_LED
59 #define RX_LED_OFF LED0_OFF
60 #define RX_LED_ON LED0_ON
61 #ifdef LED1
62 #define TX_LED_OFF LED1_OFF
63 #define TX_LED_ON LED1_ON
64 #else
65 #define TX_LED_OFF LED0_OFF
66 #define TX_LED_ON LED0_ON
67 #endif
68 #else
69 #define RX_LED_OFF
70 #define RX_LED_ON
71 #define TX_LED_OFF
72 #define TX_LED_ON
73 #endif
74 
75 #define SERIAL_4WAY_INTERFACE_NAME_STR "m4wFCIntf"
76 // *** change to adapt Revision
77 #define SERIAL_4WAY_VER_MAIN 20
78 #define SERIAL_4WAY_VER_SUB_1 (uint8_t) 0
79 #define SERIAL_4WAY_VER_SUB_2 (uint8_t) 01
80 
81 #define SERIAL_4WAY_PROTOCOL_VER 107
82 // *** end
83 
84 #if (SERIAL_4WAY_VER_MAIN > 24)
85 #error "beware of SERIAL_4WAY_VER_SUB_1 is uint8_t"
86 #endif
87 
88 #define SERIAL_4WAY_VERSION (uint16_t) ((SERIAL_4WAY_VER_MAIN * 1000) + (SERIAL_4WAY_VER_SUB_1 * 100) + SERIAL_4WAY_VER_SUB_2)
89 
90 #define SERIAL_4WAY_VERSION_HI (uint8_t) (SERIAL_4WAY_VERSION / 100)
91 #define SERIAL_4WAY_VERSION_LO (uint8_t) (SERIAL_4WAY_VERSION % 100)
92 
93 static uint8_t escCount;
94 
95 escHardware_t escHardware[MAX_SUPPORTED_MOTORS];
96 
97 uint8_t selected_esc;
98 
99 uint8_32_u DeviceInfo;
100 
101 #define DeviceInfoSize 4
102 
103 inline bool isMcuConnected(void)
104 {
105  return (DeviceInfo.bytes[0] > 0);
106 }
107 
108 inline bool isEscHi(uint8_t selEsc)
109 {
110  return (IORead(escHardware[selEsc].io) != Bit_RESET);
111 }
112 inline bool isEscLo(uint8_t selEsc)
113 {
114  return (IORead(escHardware[selEsc].io) == Bit_RESET);
115 }
116 
117 inline void setEscHi(uint8_t selEsc)
118 {
119  IOHi(escHardware[selEsc].io);
120 }
121 
122 inline void setEscLo(uint8_t selEsc)
123 {
124  IOLo(escHardware[selEsc].io);
125 }
126 
127 inline void setEscInput(uint8_t selEsc)
128 {
129  IOConfigGPIO(escHardware[selEsc].io, IOCFG_IPU);
130 }
131 
132 inline void setEscOutput(uint8_t selEsc)
133 {
134  IOConfigGPIO(escHardware[selEsc].io, IOCFG_OUT_PP);
135 }
136 
137 uint8_t esc4wayInit(void)
138 {
139  // StopPwmAllMotors();
140  pwmDisableMotors();
141  escCount = 0;
142  memset(&escHardware, 0, sizeof(escHardware));
143  pwmOutputPort_t *pwmMotors = pwmGetMotors();
144  for (volatile uint8_t i = 0; i < MAX_SUPPORTED_MOTORS; i++) {
145  if (pwmMotors[i].enabled) {
146  if (pwmMotors[i].io != IO_NONE) {
147  escHardware[escCount].io = pwmMotors[i].io;
148  setEscInput(escCount);
149  setEscHi(escCount);
150  escCount++;
151  }
152  }
153  }
154  return escCount;
155 }
156 
157 void esc4wayRelease(void)
158 {
159  while (escCount > 0) {
160  escCount--;
161  IOConfigGPIO(escHardware[escCount].io, IOCFG_AF_PP);
162  setEscLo(escCount);
163  }
164  pwmEnableMotors();
165 }
166 
167 
168 #define SET_DISCONNECTED DeviceInfo.words[0] = 0
169 
170 #define INTF_MODE_IDX 3 // index for DeviceInfostate
171 
172 // Interface related only
173 // establish and test connection to the Interface
174 
175 // Send Structure
176 // ESC + CMD PARAM_LEN [PARAM (if len > 0)] CRC16_Hi CRC16_Lo
177 // Return
178 // ESC CMD PARAM_LEN [PARAM (if len > 0)] + ACK (uint8_t OK or ERR) + CRC16_Hi CRC16_Lo
179 
180 #define cmd_Remote_Escape 0x2E // '.'
181 #define cmd_Local_Escape 0x2F // '/'
182 
183 // Test Interface still present
184 #define cmd_InterfaceTestAlive 0x30 // '0' alive
185 // RETURN: ACK
186 
187 // get Protocol Version Number 01..255
188 #define cmd_ProtocolGetVersion 0x31 // '1' version
189 // RETURN: uint8_t VersionNumber + ACK
190 
191 // get Version String
192 #define cmd_InterfaceGetName 0x32 // '2' name
193 // RETURN: String + ACK
194 
195 //get Version Number 01..255
196 #define cmd_InterfaceGetVersion 0x33 // '3' version
197 // RETURN: uint8_t AVersionNumber + ACK
198 
199 
200 // Exit / Restart Interface - can be used to switch to Box Mode
201 #define cmd_InterfaceExit 0x34 // '4' exit
202 // RETURN: ACK
203 
204 // Reset the Device connected to the Interface
205 #define cmd_DeviceReset 0x35 // '5' reset
206 // RETURN: ACK
207 
208 // Get the Device ID connected
209 // #define cmd_DeviceGetID 0x36 //'6' device id removed since 06/106
210 // RETURN: uint8_t DeviceID + ACK
211 
212 // Initialize Flash Access for Device connected
213 #define cmd_DeviceInitFlash 0x37 // '7' init flash access
214 // RETURN: ACK
215 
216 // Erase the whole Device Memory of connected Device
217 #define cmd_DeviceEraseAll 0x38 // '8' erase all
218 // RETURN: ACK
219 
220 // Erase one Page of Device Memory of connected Device
221 #define cmd_DevicePageErase 0x39 // '9' page erase
222 // PARAM: uint8_t APageNumber
223 // RETURN: ACK
224 
225 // Read to Buffer from Device Memory of connected Device // Buffer Len is Max 256 Bytes
226 // BuffLen = 0 means 256 Bytes
227 #define cmd_DeviceRead 0x3A // ':' read Device
228 // PARAM: uint8_t ADRESS_Hi + ADRESS_Lo + BuffLen[0..255]
229 // RETURN: PARAM: uint8_t ADRESS_Hi + ADRESS_Lo + BUffLen + Buffer[0..255] ACK
230 
231 // Write to Buffer for Device Memory of connected Device // Buffer Len is Max 256 Bytes
232 // BuffLen = 0 means 256 Bytes
233 #define cmd_DeviceWrite 0x3B // ';' write
234 // PARAM: uint8_t ADRESS_Hi + ADRESS_Lo + BUffLen + Buffer[0..255]
235 // RETURN: ACK
236 
237 // Set C2CK low infinite ) permanent Reset state
238 #define cmd_DeviceC2CK_LOW 0x3C // '<'
239 // RETURN: ACK
240 
241 // Read to Buffer from Device Memory of connected Device //Buffer Len is Max 256 Bytes
242 // BuffLen = 0 means 256 Bytes
243 #define cmd_DeviceReadEEprom 0x3D // '=' read Device
244 // PARAM: uint8_t ADRESS_Hi + ADRESS_Lo + BuffLen[0..255]
245 // RETURN: PARAM: uint8_t ADRESS_Hi + ADRESS_Lo + BUffLen + Buffer[0..255] ACK
246 
247 // Write to Buffer for Device Memory of connected Device // Buffer Len is Max 256 Bytes
248 // BuffLen = 0 means 256 Bytes
249 #define cmd_DeviceWriteEEprom 0x3E // '>' write
250 // PARAM: uint8_t ADRESS_Hi + ADRESS_Lo + BUffLen + Buffer[0..255]
251 // RETURN: ACK
252 
253 // Set Interface Mode
254 #define cmd_InterfaceSetMode 0x3F // '?'
255 // #define imC2 0
256 // #define imSIL_BLB 1
257 // #define imATM_BLB 2
258 // #define imSK 3
259 // PARAM: uint8_t Mode
260 // RETURN: ACK or ACK_I_INVALID_CHANNEL
261 
262 //Write to Buffer for Verify Device Memory of connected Device //Buffer Len is Max 256 Bytes
263 //BuffLen = 0 means 256 Bytes
264 #define cmd_DeviceVerify 0x40 //'@' write
265 //PARAM: uint8_t ADRESS_Hi + ADRESS_Lo + BUffLen + Buffer[0..255]
266 //RETURN: ACK
267 
268 
269 // responses
270 #define ACK_OK 0x00
271 // #define ACK_I_UNKNOWN_ERROR 0x01
272 #define ACK_I_INVALID_CMD 0x02
273 #define ACK_I_INVALID_CRC 0x03
274 #define ACK_I_VERIFY_ERROR 0x04
275 // #define ACK_D_INVALID_COMMAND 0x05
276 // #define ACK_D_COMMAND_FAILED 0x06
277 // #define ACK_D_UNKNOWN_ERROR 0x07
278 
279 #define ACK_I_INVALID_CHANNEL 0x08
280 #define ACK_I_INVALID_PARAM 0x09
281 #define ACK_D_GENERAL_ERROR 0x0F
282 
283 /* Copyright (c) 2002, 2003, 2004 Marek Michalkiewicz
284  Copyright (c) 2005, 2007 Joerg Wunsch
285  Copyright (c) 2013 Dave Hylands
286  Copyright (c) 2013 Frederic Nadeau
287  All rights reserved.
288 
289  Redistribution and use in source and binary forms, with or without
290  modification, are permitted provided that the following conditions are met:
291 
292  * Redistributions of source code must retain the above copyright
293  notice, this list of conditions and the following disclaimer.
294 
295  * Redistributions in binary form must reproduce the above copyright
296  notice, this list of conditions and the following disclaimer in
297  the documentation and/or other materials provided with the
298  distribution.
299 
300  * Neither the name of the copyright holders nor the names of
301  contributors may be used to endorse or promote products derived
302  from this software without specific prior written permission.
303 
304  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
305  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
306  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
307  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
308  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
309  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
310  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
311  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
312  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
313  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
314  POSSIBILITY OF SUCH DAMAGE. */
315 uint16_t _crc_xmodem_update (uint16_t crc, uint8_t data) {
316  int i;
317 
318  crc = crc ^ ((uint16_t)data << 8);
319  for (i=0; i < 8; i++){
320  if (crc & 0x8000)
321  crc = (crc << 1) ^ 0x1021;
322  else
323  crc <<= 1;
324  }
325  return crc;
326 }
327 // * End copyright
328 
329 
330 #define ATMEL_DEVICE_MATCH ((pDeviceInfo->words[0] == 0x9307) || (pDeviceInfo->words[0] == 0x930A) || \
331  (pDeviceInfo->words[0] == 0x930F) || (pDeviceInfo->words[0] == 0x940B))
332 
333 #define SILABS_DEVICE_MATCH ((pDeviceInfo->words[0] == 0xF310)||(pDeviceInfo->words[0] ==0xF330) || \
334  (pDeviceInfo->words[0] == 0xF410) || (pDeviceInfo->words[0] == 0xF390) || \
335  (pDeviceInfo->words[0] == 0xF850) || (pDeviceInfo->words[0] == 0xE8B1) || \
336  (pDeviceInfo->words[0] == 0xE8B2))
337 
338 #define ARM_DEVICE_MATCH ((pDeviceInfo->words[0] == 0x1F06) || \
339  (pDeviceInfo->words[0] == 0x3306) || (pDeviceInfo->words[0] == 0x3406))
340 
341 static uint8_t CurrentInterfaceMode;
342 
343 static uint8_t Connect(uint8_32_u *pDeviceInfo)
344 {
345  for (uint8_t I = 0; I < 3; ++I) {
346  #if (defined(USE_SERIAL_4WAY_BLHELI_BOOTLOADER) && defined(USE_SERIAL_4WAY_SK_BOOTLOADER))
347  if ((CurrentInterfaceMode != imARM_BLB) && Stk_ConnectEx(pDeviceInfo) && ATMEL_DEVICE_MATCH) {
348  CurrentInterfaceMode = imSK;
349  return 1;
350  } else {
351  if (BL_ConnectEx(pDeviceInfo)) {
352  if SILABS_DEVICE_MATCH {
353  CurrentInterfaceMode = imSIL_BLB;
354  return 1;
355  } else if ATMEL_DEVICE_MATCH {
356  CurrentInterfaceMode = imATM_BLB;
357  return 1;
358  } else if ARM_DEVICE_MATCH {
359  CurrentInterfaceMode = imARM_BLB;
360  return 1;
361  }
362  }
363  }
364  #elif defined(USE_SERIAL_4WAY_BLHELI_BOOTLOADER)
365  if (BL_ConnectEx(pDeviceInfo)) {
366  if SILABS_DEVICE_MATCH {
367  CurrentInterfaceMode = imSIL_BLB;
368  return 1;
369  } else if ATMEL_DEVICE_MATCH {
370  CurrentInterfaceMode = imATM_BLB;
371  return 1;
372  } else if ARM_DEVICE_MATCH {
373  CurrentInterfaceMode = imARM_BLB;
374  return 1;
375  }
376  }
377  #elif defined(USE_SERIAL_4WAY_SK_BOOTLOADER)
378  if (Stk_ConnectEx(pDeviceInfo)) {
379  CurrentInterfaceMode = imSK;
380  if ATMEL_DEVICE_MATCH return 1;
381  }
382  #endif
383  }
384  return 0;
385 }
386 
387 static serialPort_t *port;
388 
389 static uint8_t ReadByte(void)
390 {
391  // need timeout?
392  while (!serialRxBytesWaiting(port));
393  return serialRead(port);
394 }
395 
396 static uint8_16_u CRC_in;
397 static uint8_t ReadByteCrc(void)
398 {
399  uint8_t b = ReadByte();
400  CRC_in.word = _crc_xmodem_update(CRC_in.word, b);
401  return b;
402 }
403 
404 static void WriteByte(uint8_t b)
405 {
406  serialWrite(port, b);
407 }
408 
409 static uint8_16_u CRCout;
410 static void WriteByteCrc(uint8_t b)
411 {
412  WriteByte(b);
413  CRCout.word = _crc_xmodem_update(CRCout.word, b);
414 }
415 
416 void esc4wayProcess(serialPort_t *mspPort)
417 {
418 
419  uint8_t ParamBuf[256];
420  uint8_t ESC;
421  uint8_t I_PARAM_LEN;
422  uint8_t CMD;
423  uint8_t ACK_OUT;
424  uint8_16_u CRC_check;
425  uint8_16_u Dummy;
426  uint8_t O_PARAM_LEN;
427  uint8_t *O_PARAM;
428  uint8_t *InBuff;
429  ioMem_t ioMem;
430 
431  port = mspPort;
432 
433  // Start here with UART Main loop
434  #ifdef BEEPER
435  // fix for buzzer often starts beeping continuously when the ESCs are read
436  // switch beeper silent here
437  beeperSilence();
438  #endif
439  bool isExitScheduled = false;
440 
441  while(1) {
442  // restart looking for new sequence from host
443  do {
444  CRC_in.word = 0;
445  ESC = ReadByteCrc();
446  } while (ESC != cmd_Local_Escape);
447 
448  RX_LED_ON;
449 
450  Dummy.word = 0;
451  O_PARAM = &Dummy.bytes[0];
452  O_PARAM_LEN = 1;
453  CMD = ReadByteCrc();
454  ioMem.D_FLASH_ADDR_H = ReadByteCrc();
455  ioMem.D_FLASH_ADDR_L = ReadByteCrc();
456  I_PARAM_LEN = ReadByteCrc();
457 
458  InBuff = ParamBuf;
459  uint8_t i = I_PARAM_LEN;
460  do {
461  *InBuff = ReadByteCrc();
462  InBuff++;
463  i--;
464  } while (i != 0);
465 
466  CRC_check.bytes[1] = ReadByte();
467  CRC_check.bytes[0] = ReadByte();
468 
469  if(CRC_check.word == CRC_in.word) {
470  ACK_OUT = ACK_OK;
471  } else {
472  ACK_OUT = ACK_I_INVALID_CRC;
473  }
474 
475  TX_LED_ON;
476 
477  if (ACK_OUT == ACK_OK)
478  {
479  // wtf.D_FLASH_ADDR_H=Adress_H;
480  // wtf.D_FLASH_ADDR_L=Adress_L;
481  ioMem.D_PTR_I = ParamBuf;
482 
483 
484 #ifdef DRONIN_TARGET
486 #endif
487 
488  switch(CMD) {
489  // ******* Interface related stuff *******
490  case cmd_InterfaceTestAlive:
491  {
492  if (isMcuConnected()){
493  switch(CurrentInterfaceMode)
494  {
495  #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
496  case imATM_BLB:
497  case imSIL_BLB:
498  case imARM_BLB:
499  {
500  if (!BL_SendCMDKeepAlive()) { // SetStateDisconnected() included
501  ACK_OUT = ACK_D_GENERAL_ERROR;
502  }
503  break;
504  }
505  #endif
506  #ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
507  case imSK:
508  {
509  if (!Stk_SignOn()) { // SetStateDisconnected();
510  ACK_OUT = ACK_D_GENERAL_ERROR;
511  }
512  break;
513  }
514  #endif
515  default:
516  ACK_OUT = ACK_D_GENERAL_ERROR;
517  }
518  if ( ACK_OUT != ACK_OK) SET_DISCONNECTED;
519  }
520  break;
521  }
522  case cmd_ProtocolGetVersion:
523  {
524  // Only interface itself, no matter what Device
525  Dummy.bytes[0] = SERIAL_4WAY_PROTOCOL_VER;
526  break;
527  }
528 
529  case cmd_InterfaceGetName:
530  {
531  // Only interface itself, no matter what Device
532  // O_PARAM_LEN=16;
533  O_PARAM_LEN = strlen(SERIAL_4WAY_INTERFACE_NAME_STR);
534  O_PARAM = (uint8_t *)SERIAL_4WAY_INTERFACE_NAME_STR;
535  break;
536  }
537 
538  case cmd_InterfaceGetVersion:
539  {
540  // Only interface itself, no matter what Device
541  // Dummy = iUart_res_InterfVersion;
542  O_PARAM_LEN = 2;
543  Dummy.bytes[0] = SERIAL_4WAY_VERSION_HI;
544  Dummy.bytes[1] = SERIAL_4WAY_VERSION_LO;
545  break;
546  }
547  case cmd_InterfaceExit:
548  {
549  isExitScheduled = true;
550  break;
551  }
552  case cmd_InterfaceSetMode:
553  {
554 #if defined(USE_SERIAL_4WAY_BLHELI_BOOTLOADER) && defined(USE_SERIAL_4WAY_SK_BOOTLOADER)
555  if ((ParamBuf[0] <= imARM_BLB) && (ParamBuf[0] >= imSIL_BLB)) {
556 #elif defined(USE_SERIAL_4WAY_BLHELI_BOOTLOADER)
557  if (((ParamBuf[0] <= imATM_BLB)||(ParamBuf[0] == imARM_BLB)) && (ParamBuf[0] >= imSIL_BLB)) {
558 #elif defined(USE_SERIAL_4WAY_SK_BOOTLOADER)
559  if (ParamBuf[0] == imSK) {
560 #endif
561  CurrentInterfaceMode = ParamBuf[0];
562  } else {
563  ACK_OUT = ACK_I_INVALID_PARAM;
564  }
565  break;
566  }
567 
568  case cmd_DeviceReset:
569  {
570  if (ParamBuf[0] < escCount) {
571  // Channel may change here
572  selected_esc = ParamBuf[0];
573  }
574  else {
575  ACK_OUT = ACK_I_INVALID_CHANNEL;
576  break;
577  }
578  switch (CurrentInterfaceMode)
579  {
580  case imSIL_BLB:
581  #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
582  case imATM_BLB:
583  case imARM_BLB:
584  {
585  BL_SendCMDRunRestartBootloader(&DeviceInfo);
586  break;
587  }
588  #endif
589  #ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
590  case imSK:
591  {
592  break;
593  }
594  #endif
595  }
596  SET_DISCONNECTED;
597  break;
598  }
599  case cmd_DeviceInitFlash:
600  {
601  SET_DISCONNECTED;
602  if (ParamBuf[0] < escCount) {
603  //Channel may change here
604  //ESC_LO or ESC_HI; Halt state for prev channel
605  selected_esc = ParamBuf[0];
606  } else {
607  ACK_OUT = ACK_I_INVALID_CHANNEL;
608  break;
609  }
610  O_PARAM_LEN = DeviceInfoSize; //4
611  O_PARAM = (uint8_t *)&DeviceInfo;
612  if(Connect(&DeviceInfo)) {
613  DeviceInfo.bytes[INTF_MODE_IDX] = CurrentInterfaceMode;
614  } else {
615  SET_DISCONNECTED;
616  ACK_OUT = ACK_D_GENERAL_ERROR;
617  }
618  break;
619  }
620 
621  #ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
622  case cmd_DeviceEraseAll:
623  {
624  switch(CurrentInterfaceMode)
625  {
626  case imSK:
627  {
628  if (!Stk_Chip_Erase()) ACK_OUT=ACK_D_GENERAL_ERROR;
629  break;
630  }
631  default:
632  ACK_OUT = ACK_I_INVALID_CMD;
633  }
634  break;
635  }
636  #endif
637 
638  #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
639  case cmd_DevicePageErase:
640  {
641  switch (CurrentInterfaceMode)
642  {
643  case imSIL_BLB:
644  case imARM_BLB:
645  {
646  Dummy.bytes[0] = ParamBuf[0];
647  if (CurrentInterfaceMode == imARM_BLB) {
648  // Address =Page * 1024
649  ioMem.D_FLASH_ADDR_H = (Dummy.bytes[0] << 2);
650  } else {
651  // Address =Page * 512
652  ioMem.D_FLASH_ADDR_H = (Dummy.bytes[0] << 1);
653  }
654  ioMem.D_FLASH_ADDR_L = 0;
655  if (!BL_PageErase(&ioMem)) ACK_OUT = ACK_D_GENERAL_ERROR;
656  break;
657  }
658  default:
659  ACK_OUT = ACK_I_INVALID_CMD;
660  }
661  break;
662  }
663  #endif
664 
665  //*** Device Memory Read Ops ***
666  case cmd_DeviceRead:
667  {
668  ioMem.D_NUM_BYTES = ParamBuf[0];
669  /*
670  wtf.D_FLASH_ADDR_H=Adress_H;
671  wtf.D_FLASH_ADDR_L=Adress_L;
672  wtf.D_PTR_I = BUF_I;
673  */
674  switch(CurrentInterfaceMode)
675  {
676  #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
677  case imSIL_BLB:
678  case imATM_BLB:
679  case imARM_BLB:
680  {
681  if(!BL_ReadFlash(CurrentInterfaceMode, &ioMem))
682  {
683  ACK_OUT = ACK_D_GENERAL_ERROR;
684  }
685  break;
686  }
687  #endif
688  #ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
689  case imSK:
690  {
691  if(!Stk_ReadFlash(&ioMem))
692  {
693  ACK_OUT = ACK_D_GENERAL_ERROR;
694  }
695  break;
696  }
697  #endif
698  default:
699  ACK_OUT = ACK_I_INVALID_CMD;
700  }
701  if (ACK_OUT == ACK_OK)
702  {
703  O_PARAM_LEN = ioMem.D_NUM_BYTES;
704  O_PARAM = (uint8_t *)&ParamBuf;
705  }
706  break;
707  }
708 
709  case cmd_DeviceReadEEprom:
710  {
711  ioMem.D_NUM_BYTES = ParamBuf[0];
712  /*
713  wtf.D_FLASH_ADDR_H = Adress_H;
714  wtf.D_FLASH_ADDR_L = Adress_L;
715  wtf.D_PTR_I = BUF_I;
716  */
717  switch (CurrentInterfaceMode)
718  {
719  #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
720  case imATM_BLB:
721  {
722  if (!BL_ReadEEprom(&ioMem))
723  {
724  ACK_OUT = ACK_D_GENERAL_ERROR;
725  }
726  break;
727  }
728  #endif
729  #ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
730  case imSK:
731  {
732  if (!Stk_ReadEEprom(&ioMem))
733  {
734  ACK_OUT = ACK_D_GENERAL_ERROR;
735  }
736  break;
737  }
738  #endif
739  default:
740  ACK_OUT = ACK_I_INVALID_CMD;
741  }
742  if(ACK_OUT == ACK_OK)
743  {
744  O_PARAM_LEN = ioMem.D_NUM_BYTES;
745  O_PARAM = (uint8_t *)&ParamBuf;
746  }
747  break;
748  }
749 
750  //*** Device Memory Write Ops ***
751  case cmd_DeviceWrite:
752  {
753  ioMem.D_NUM_BYTES = I_PARAM_LEN;
754  /*
755  wtf.D_FLASH_ADDR_H=Adress_H;
756  wtf.D_FLASH_ADDR_L=Adress_L;
757  wtf.D_PTR_I = BUF_I;
758  */
759  switch (CurrentInterfaceMode)
760  {
761  #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
762  case imSIL_BLB:
763  case imATM_BLB:
764  case imARM_BLB:
765  {
766  if (!BL_WriteFlash(&ioMem)) {
767  ACK_OUT = ACK_D_GENERAL_ERROR;
768  }
769  break;
770  }
771  #endif
772  #ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
773  case imSK:
774  {
775  if (!Stk_WriteFlash(&ioMem))
776  {
777  ACK_OUT = ACK_D_GENERAL_ERROR;
778  }
779  break;
780  }
781  #endif
782  }
783  break;
784  }
785 
786  case cmd_DeviceWriteEEprom:
787  {
788  ioMem.D_NUM_BYTES = I_PARAM_LEN;
789  ACK_OUT = ACK_D_GENERAL_ERROR;
790  /*
791  wtf.D_FLASH_ADDR_H=Adress_H;
792  wtf.D_FLASH_ADDR_L=Adress_L;
793  wtf.D_PTR_I = BUF_I;
794  */
795  switch (CurrentInterfaceMode)
796  {
797  #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
798  case imSIL_BLB:
799  {
800  ACK_OUT = ACK_I_INVALID_CMD;
801  break;
802  }
803  case imATM_BLB:
804  {
805  if (BL_WriteEEprom(&ioMem))
806  {
807  ACK_OUT = ACK_OK;
808  }
809  break;
810  }
811  #endif
812  #ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
813  case imSK:
814  {
815  if (Stk_WriteEEprom(&ioMem))
816  {
817  ACK_OUT = ACK_OK;
818  }
819  break;
820  }
821  #endif
822  }
823  break;
824  }
825  //*** Device Memory Verify Ops ***
826  #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
827  case cmd_DeviceVerify:
828  {
829  switch (CurrentInterfaceMode)
830  {
831  case imARM_BLB:
832  {
833  ioMem.D_NUM_BYTES = I_PARAM_LEN;
834  /*
835  wtf.D_FLASH_ADDR_H=Adress_H;
836  wtf.D_FLASH_ADDR_L=Adress_L;
837  wtf.D_PTR_I = BUF_I;
838  */
839 
840  ACK_OUT = BL_VerifyFlash(&ioMem);
841  switch (ACK_OUT) {
842  case brSUCCESS:
843  ACK_OUT = ACK_OK;
844  break;
845  case brERRORVERIFY:
846  ACK_OUT = ACK_I_VERIFY_ERROR;
847  break;
848  default:
849  ACK_OUT = ACK_D_GENERAL_ERROR;
850  break;
851  }
852  break;
853  }
854  default:
855  {
856  ACK_OUT = ACK_I_INVALID_CMD;
857  break;
858  }
859  }
860  break;
861  }
862  #endif
863  default:
864  {
865  ACK_OUT = ACK_I_INVALID_CMD;
866  }
867  }
868 
869 #ifdef DRONIN_TARGET
870  PIOS_IRQ_Enable();
871 #endif
872  }
873 
874  CRCout.word = 0;
875 
876  RX_LED_OFF;
877 
878  serialBeginWrite(port);
879  WriteByteCrc(cmd_Remote_Escape);
880  WriteByteCrc(CMD);
881  WriteByteCrc(ioMem.D_FLASH_ADDR_H);
882  WriteByteCrc(ioMem.D_FLASH_ADDR_L);
883  WriteByteCrc(O_PARAM_LEN);
884 
885  i=O_PARAM_LEN;
886  do {
887  while (!serialTxBytesFree(port));
888 
889  WriteByteCrc(*O_PARAM);
890  O_PARAM++;
891  i--;
892  } while (i > 0);
893 
894  WriteByteCrc(ACK_OUT);
895  WriteByte(CRCout.bytes[1]);
896  WriteByte(CRCout.bytes[0]);
897  serialEndWrite(port);
898 
899  TX_LED_OFF;
900  if (isExitScheduled) {
901  esc4wayRelease();
902  return;
903  }
904  };
905 }
906 
907 
908 
909 #endif
union @12 crc
#define TX_LED_OFF
Definition: pios_board.h:114
#define imATM_BLB
Definition: serial_4way.h:30
uint8_t BL_VerifyFlash(ioMem_t *pMem)
uint8_t selected_esc
int32_t PIOS_IRQ_Enable(void)
Definition: pios_irq.c:53
void setEscInput(uint8_t selEsc)
uint8_t esc4wayInit(void)
uint8_32_u
Definition: serial_4way.h:47
#define imSIL_BLB
Definition: serial_4way.h:29
uint8_t Stk_WriteEEprom(ioMem_t *pMem)
uint8_t BL_ReadEEprom(ioMem_t *pMem)
void setEscHi(uint8_t selEsc)
void BL_SendCMDRunRestartBootloader(uint8_32_u *pDeviceInfo)
uint8_t Stk_WriteFlash(ioMem_t *pMem)
bool isEscLo(uint8_t selEsc)
#define RX_LED_OFF
Definition: pios_board.h:110
#define TX_LED_ON
Definition: pios_board.h:113
bool isMcuConnected(void)
uint8_t BL_WriteEEprom(ioMem_t *pMem)
uint8_t D_NUM_BYTES
void esc4wayRelease(void)
uint8_t BL_SendCMDKeepAlive(void)
uint8_t data[XFER_BYTES_PER_PACKET]
Definition: bl_messages.h:129
uint8_16_u
Definition: serial_4way.h:41
uint8_t Stk_ConnectEx(uint8_32_u *pDeviceInfo)
ioMem_t ioMem
uint8_t BL_PageErase(ioMem_t *pMem)
uint8_t D_FLASH_ADDR_L
bool isEscHi(uint8_t selEsc)
uint8_t i
Definition: msp_messages.h:97
uint8_t Stk_Chip_Erase(void)
uint8_t Stk_ReadFlash(ioMem_t *pMem)
#define imARM_BLB
Definition: serial_4way.h:32
uint8_t D_FLASH_ADDR_H
uint8_t BL_ReadFlash(uint8_t interface_mode, ioMem_t *pMem)
void setEscLo(uint8_t selEsc)
int32_t PIOS_IRQ_Disable(void)
Definition: pios_irq.c:40
#define brSUCCESS
void esc4wayProcess(struct serialPort_s *mspPort)
uint8_t Stk_SignOn(void)
#define brERRORVERIFY
uint8_t BL_WriteFlash(ioMem_t *pMem)
uint8_t BL_ConnectEx(uint8_32_u *pDeviceInfo)
uint8_t * D_PTR_I
uint8_t Stk_ReadEEprom(ioMem_t *pMem)
#define RX_LED_ON
Definition: pios_board.h:109
#define imSK
Definition: serial_4way.h:31
void setEscOutput(uint8_t selEsc)