2013-11-13 09:04:14 +00:00
var STM32 _protocol = function ( ) {
2013-12-10 07:19:18 +00:00
this . hex ; // ref
2013-11-13 09:04:14 +00:00
this . receive _buffer ;
this . bytes _to _read = 0 ; // ref
this . read _callback ; // ref
this . flashing _memory _address ;
this . verify _memory _address ;
this . bytes _flashed ;
this . bytes _verified ;
this . verify _hex = new Array ( ) ;
this . upload _time _start ;
this . steps _executed ;
this . steps _executed _last ;
this . status = {
ACK : 0x79 ,
NACK : 0x1F
} ;
this . command = {
get : 0x00 , // Gets the version and the allowed commands supported by the current version of the bootloader
get _ver _r _protect _s : 0x01 , // Gets the bootloader version and the Read Protection status of the Flash memory
get _ID : 0x02 , // Gets the chip ID
read _memory : 0x11 , // Reads up to 256 bytes of memory starting from an address specified by the application
go : 0x21 , // Jumps to user application code located in the internal Flash memory or in SRAM
write _memory : 0x31 , // Writes up to 256 bytes to the RAM or Flash memory starting from an address specified by the application
erase : 0x43 , // Erases from one to all the Flash memory pages
extended _erase : 0x44 , // Erases from one to all the Flash memory pages using two byte addressing mode (v3.0+ usart).
write _protect : 0x63 , // Enables the write protection for some sectors
write _unprotect : 0x73 , // Disables the write protection for all Flash memory sectors
readout _protect : 0x82 , // Enables the read protection
readout _unprotect : 0x92 // Disables the read protection
} ;
// Erase (x043) and Extended Erase (0x44) are exclusive. A device may support either the Erase command or the Extended Erase command but not both.
2013-12-06 18:02:31 +00:00
// debug variables
this . serial _bytes _send ;
this . serial _bytes _received ;
2013-11-13 09:04:14 +00:00
} ;
2013-11-13 09:56:14 +00:00
// string = string .. duh
STM32 _protocol . prototype . GUI _status = function ( string ) {
$ ( 'span.status' ) . html ( string ) ;
} ;
2013-11-13 09:04:14 +00:00
// no input parameters
2013-12-10 07:19:18 +00:00
STM32 _protocol . prototype . connect = function ( hex ) {
2013-11-13 09:04:14 +00:00
var self = this ;
2013-12-10 07:19:18 +00:00
self . hex = hex ;
2013-11-13 09:04:14 +00:00
2013-11-14 15:41:09 +00:00
var selected _port = String ( $ ( 'div#port-picker .port select' ) . val ( ) ) ;
var baud = parseInt ( $ ( 'div#port-picker #baud' ) . val ( ) ) ;
2013-11-13 09:04:14 +00:00
if ( selected _port != '0' ) {
2013-12-04 17:13:05 +00:00
// get fastest supported bitrate for current platform
// (serial API for windows support baud > 256000 from chrome v 33+)
// recommended windows - 921600, rest - 576000, default - 115200
2013-11-15 17:03:10 +00:00
switch ( GUI . operating _system ) {
case 'Windows' :
2013-12-04 17:13:05 +00:00
var flashing _bitrate = 256000 ;
2013-11-15 17:03:10 +00:00
break ;
case 'MacOS' :
case 'ChromeOS' :
case 'Linux' :
case 'UNIX' :
2013-12-04 17:13:05 +00:00
var flashing _bitrate = 230400 ;
2013-11-15 17:03:10 +00:00
break ;
2013-12-04 16:30:55 +00:00
2013-12-04 13:11:36 +00:00
default :
var flashing _bitrate = 115200 ;
2013-11-15 17:03:10 +00:00
}
2013-11-14 15:41:09 +00:00
if ( ! $ ( 'input.updating' ) . is ( ':checked' ) ) {
chrome . serial . open ( selected _port , { bitrate : baud } , function ( openInfo ) {
2013-12-06 17:23:40 +00:00
if ( openInfo . connectionId > 0 ) {
connectionId = openInfo . connectionId ;
2013-12-04 17:13:05 +00:00
console . log ( 'Connection was opened with ID: ' + connectionId + ' Baud: ' + baud ) ;
2013-11-14 15:48:37 +00:00
console . log ( 'Sending ascii "R" to reboot' ) ;
2013-11-13 09:04:14 +00:00
2013-11-14 15:41:09 +00:00
// we are connected, disabling connect button in the UI
GUI . connect _lock = true ;
self . send ( [ 0x52 ] ) ;
GUI . timeout _add ( 'reboot_into_bootloader' , function ( ) {
chrome . serial . close ( connectionId , function ( result ) {
if ( result ) {
2013-11-14 15:48:37 +00:00
console . log ( 'Connection closed successfully.' ) ;
2013-11-15 17:03:10 +00:00
chrome . serial . open ( selected _port , { bitrate : flashing _bitrate , parityBit : 'evenparity' , stopBit : 'onestopbit' } , function ( openInfo ) {
2013-12-06 17:23:40 +00:00
if ( openInfo . connectionId > 0 ) {
connectionId = openInfo . connectionId ;
2013-12-04 17:13:05 +00:00
console . log ( 'Connection was opened with ID: ' + connectionId + ' Baud: ' + flashing _bitrate ) ;
2013-11-14 15:48:37 +00:00
2013-11-14 15:41:09 +00:00
self . initialize ( ) ;
}
} ) ;
2013-11-14 18:16:33 +00:00
} else {
console . log ( 'There was an error that happened during "connection-close" procedure' ) ;
GUI . connect _lock = false ;
2013-11-14 15:41:09 +00:00
}
} ) ;
} , 100 ) ;
}
} ) ;
} else {
2013-11-15 17:03:10 +00:00
chrome . serial . open ( selected _port , { bitrate : flashing _bitrate , parityBit : 'evenparity' , stopBit : 'onestopbit' } , function ( openInfo ) {
2013-12-06 17:23:40 +00:00
if ( openInfo . connectionId > 0 ) {
connectionId = openInfo . connectionId ;
2013-12-04 13:11:36 +00:00
console . log ( 'Connection was opened with ID: ' + connectionId + ' Baud: ' + flashing _bitrate ) ;
2013-11-14 15:48:37 +00:00
// we are connected, disabling connect button in the UI
GUI . connect _lock = true ;
2013-11-14 15:41:09 +00:00
self . initialize ( ) ;
}
} ) ;
}
2013-11-13 09:04:14 +00:00
} else {
console . log ( 'Please select valid serial port' ) ;
2013-11-13 09:59:07 +00:00
STM32 . GUI _status ( '<span style="color: red">Please select valid serial port</span>' ) ;
2013-11-13 09:04:14 +00:00
}
} ;
// initialize certain variables and start timers that oversee the communication
STM32 _protocol . prototype . initialize = function ( ) {
var self = this ;
// reset and set some variables before we start
self . receive _buffer = [ ] ;
2013-11-16 18:14:34 +00:00
self . flashing _memory _address = self . hex . extended _linear _address [ 0 ] ;
self . verify _memory _address = self . hex . extended _linear _address [ 0 ] ;
2013-11-13 09:04:14 +00:00
self . bytes _flashed = 0 ;
self . bytes _verified = 0 ;
self . verify _hex = [ ] ;
2013-12-06 18:02:31 +00:00
self . serial _bytes _send = 0 ;
self . serial _bytes _received = 0 ;
2013-11-13 09:04:14 +00:00
self . upload _time _start = microtime ( ) ;
self . steps _executed = 0 ;
self . steps _executed _last = 0 ;
2013-11-16 12:03:42 +00:00
// reset progress bar to initial state
self . progress _bar _e = $ ( '.progress' ) ;
self . progress _bar _e . val ( 0 ) ;
self . progress _bar _e . removeClass ( 'valid invalid' ) ;
2013-11-13 09:04:14 +00:00
GUI . interval _add ( 'firmware_uploader_read' , function ( ) {
self . read ( ) ;
} , 1 , true ) ;
GUI . interval _add ( 'STM32_timeout' , function ( ) {
if ( self . steps _executed > self . steps _executed _last ) { // process is running
self . steps _executed _last = self . steps _executed ;
} else {
console . log ( 'STM32 - timed out, programming failed ...' ) ;
2013-11-13 09:56:14 +00:00
STM32 . GUI _status ( 'STM32 - timed out, programming: <strong style="color: red">FAILED</strong>' ) ;
2013-11-13 09:04:14 +00:00
// protocol got stuck, clear timer and disconnect
GUI . interval _remove ( 'STM32_timeout' ) ;
// exit
self . upload _procedure ( 99 ) ;
}
} , 1000 ) ;
2013-11-14 15:41:09 +00:00
self . upload _procedure ( 1 ) ;
2013-11-13 09:04:14 +00:00
} ;
// no input parameters
// this method should be executed every 1 ms via interval timer
STM32 _protocol . prototype . read = function ( ) {
var self = this ;
// routine that fills the buffer
chrome . serial . read ( connectionId , 128 , function ( readInfo ) {
if ( readInfo && readInfo . bytesRead > 0 ) {
var data = new Uint8Array ( readInfo . data ) ;
for ( var i = 0 ; i < data . length ; i ++ ) {
self . receive _buffer . push ( data [ i ] ) ;
}
2013-12-06 18:02:31 +00:00
self . serial _bytes _received += data . length ;
2013-11-13 09:04:14 +00:00
}
} ) ;
// routine that fetches data from buffer if statement is true
if ( self . receive _buffer . length >= self . bytes _to _read && self . bytes _to _read != 0 ) {
var data = self . receive _buffer . slice ( 0 , self . bytes _to _read ) ; // bytes requested
self . receive _buffer . splice ( 0 , self . bytes _to _read ) ; // remove read bytes
self . bytes _to _read = 0 ; // reset trigger
self . read _callback ( data ) ;
}
} ;
// Array = array of bytes that will be send over serial
// bytes_to_read = received bytes necessary to trigger read_callback
// callback = function that will be executed after received bytes = bytes_to_read
STM32 _protocol . prototype . send = function ( Array , bytes _to _read , callback ) {
2013-12-06 18:02:31 +00:00
var self = this ;
2013-11-13 09:04:14 +00:00
var bufferOut = new ArrayBuffer ( Array . length ) ;
var bufferView = new Uint8Array ( bufferOut ) ;
// set Array values inside bufferView (alternative to for loop)
bufferView . set ( Array ) ;
// update references
this . bytes _to _read = bytes _to _read ;
this . read _callback = callback ;
// send over the actual data
2013-12-06 18:02:31 +00:00
chrome . serial . write ( connectionId , bufferOut , function ( writeInfo ) {
if ( writeInfo . bytesWritten > 0 ) {
self . serial _bytes _send += writeInfo . bytesWritten ;
}
} ) ;
2013-11-13 09:04:14 +00:00
} ;
// val = single byte to be verified
// data = response of n bytes from mcu (array)
// result = true/false
STM32 _protocol . prototype . verify _response = function ( val , data ) {
if ( val != data [ 0 ] ) {
console . log ( 'STM32 Communication failed, wrong response, expected: ' + val + ' received: ' + data [ 0 ] ) ;
2013-11-15 15:17:43 +00:00
STM32 . GUI _status ( 'STM32 Communication <span style="color: red">failed</span>, wrong response, expected: ' + val + ' received: ' + data [ 0 ] ) ;
2013-11-13 09:04:14 +00:00
// disconnect
this . upload _procedure ( 99 ) ;
return false ;
}
return true ;
} ;
// input = 16 bit value
// result = true/false
STM32 _protocol . prototype . verify _chip _signature = function ( signature ) {
switch ( signature ) {
2013-11-15 15:31:16 +00:00
case 0x412 : // not tested
2013-11-15 15:17:43 +00:00
console . log ( 'Chip recognized as F1 Low-density' ) ;
return true ;
2013-11-13 09:04:14 +00:00
break ;
case 0x410 :
console . log ( 'Chip recognized as F1 Medium-density' ) ;
return true ;
break ;
2013-11-15 15:31:16 +00:00
case 0x414 : // not tested
2013-11-15 15:17:43 +00:00
console . log ( 'Chip recognized as F1 High-density' ) ;
return true ;
2013-11-13 09:04:14 +00:00
break ;
2013-11-15 15:31:16 +00:00
case 0x418 : // not tested
2013-11-15 15:17:43 +00:00
console . log ( 'Chip recognized as F1 Connectivity line' ) ;
return true ;
2013-11-13 09:04:14 +00:00
break ;
2013-11-15 15:31:16 +00:00
case 0x420 : // not tested
2013-11-15 15:17:43 +00:00
console . log ( 'Chip recognized as F1 Medium-density value line' ) ;
return true ;
2013-11-13 09:04:14 +00:00
break ;
2013-11-15 15:31:16 +00:00
case 0x428 : // not tested
2013-11-15 15:17:43 +00:00
console . log ( 'Chip recognized as F1 High-density value line' ) ;
return true ;
2013-11-13 09:04:14 +00:00
break ;
2013-11-15 15:31:16 +00:00
case 0x430 : // not tested
2013-11-15 15:17:43 +00:00
console . log ( 'Chip recognized as F1 XL-density value line' ) ;
2013-11-15 15:31:16 +00:00
return true ;
break ;
case 0x416 : // not tested
console . log ( 'Chip recognized as L1 Medium-density ultralow power' ) ;
return true ;
break ;
2013-11-15 15:35:36 +00:00
case 0x436 : // not tested
2013-11-15 15:31:16 +00:00
console . log ( 'Chip recognized as L1 High-density ultralow power' ) ;
return true ;
break ;
2013-11-15 15:35:36 +00:00
case 0x427 : // not tested
2013-11-15 15:31:16 +00:00
console . log ( 'Chip recognized as L1 Medium-density plus ultralow power' ) ;
return true ;
break ;
2013-11-15 15:35:36 +00:00
case 0x411 : // not tested
2013-11-15 15:31:16 +00:00
console . log ( 'Chip recognized as F2 STM32F2xxxx' ) ;
return true ;
break ;
2013-11-15 15:35:36 +00:00
case 0x440 : // not tested
2013-11-15 15:31:16 +00:00
console . log ( 'Chip recognized as F0 STM32F051xx' ) ;
return true ;
break ;
2013-11-15 15:35:36 +00:00
case 0x444 : // not tested
2013-11-15 15:31:16 +00:00
console . log ( 'Chip recognized as F0 STM32F050xx' ) ;
return true ;
break ;
2013-11-15 15:35:36 +00:00
case 0x413 : // not tested
2013-11-15 15:31:16 +00:00
console . log ( 'Chip recognized as F4 STM32F40xxx/41xxx' ) ;
return true ;
break ;
2013-11-15 15:35:36 +00:00
case 0x419 : // not tested
2013-11-15 15:31:16 +00:00
console . log ( 'Chip recognized as F4 STM32F427xx/437xx, STM32F429xx/439xx' ) ;
return true ;
break ;
2013-11-15 15:35:36 +00:00
case 0x432 : // not tested
2013-11-15 15:31:16 +00:00
console . log ( 'Chip recognized as F3 STM32F37xxx, STM32F38xxx' ) ;
return true ;
break ;
2013-11-15 15:35:36 +00:00
case 0x422 : // not tested
2013-11-15 15:31:16 +00:00
console . log ( 'Chip recognized as F3 STM32F30xxx, STM32F31xxx' ) ;
2013-11-15 15:17:43 +00:00
return true ;
2013-11-13 09:04:14 +00:00
break ;
default :
2013-11-15 15:17:43 +00:00
console . log ( 'Chip NOT recognized: ' + signature ) ;
2013-11-13 09:04:14 +00:00
return false ;
} ;
} ;
// first_array = usually hex_to_flash array
// second_array = usually verify_hex array
// result = true/false
STM32 _protocol . prototype . verify _flash = function ( first _array , second _array ) {
for ( var i = 0 ; i < first _array . length ; i ++ ) {
if ( first _array [ i ] != second _array [ i ] ) {
console . log ( 'Verification failed on byte: ' + i + ' expected: 0x' + first _array [ i ] . toString ( 16 ) + ' received: 0x' + second _array [ i ] . toString ( 16 ) ) ;
return false ;
}
}
console . log ( 'Verification successful, matching: ' + first _array . length + ' bytes' ) ;
return true ;
} ;
// step = value depending on current state of upload_procedure
STM32 _protocol . prototype . upload _procedure = function ( step ) {
var self = this ;
self . steps _executed ++ ;
switch ( step ) {
case 1 :
// initialize serial interface on the MCU side, auto baud rate settings
2013-12-03 20:23:31 +00:00
var send _counter = 0 ;
GUI . interval _add ( 'stm32_initialize_mcu' , function ( ) { // 200 ms interval (just in case mcu was already initialized), we need to break the 2 bytes command requirement
self . send ( [ 0x7F ] , 1 , function ( reply ) {
if ( reply [ 0 ] == self . status . ACK || reply [ 0 ] == self . status . NACK ) {
GUI . interval _remove ( 'stm32_initialize_mcu' ) ;
console . log ( 'STM32 - Serial interface initialized on the MCU side' ) ;
// proceed to next step
self . upload _procedure ( 2 ) ;
} else {
GUI . interval _remove ( 'stm32_initialize_mcu' ) ;
STM32 . GUI _status ( 'STM32 Communication with bootloader <span style="color: red">failed</span>' ) ;
2013-11-13 09:04:14 +00:00
2013-12-03 20:23:31 +00:00
// disconnect
self . upload _procedure ( 99 ) ;
}
} ) ;
2013-12-03 19:52:00 +00:00
2013-12-03 20:23:31 +00:00
if ( send _counter ++ > 3 ) {
// stop retrying, its too late to get any response from MCU
GUI . interval _remove ( 'stm32_initialize_mcu' ) ;
2013-11-13 09:04:14 +00:00
}
2013-12-03 20:23:31 +00:00
} , 200 ) ;
2013-11-13 09:04:14 +00:00
break ;
case 2 :
// get version of the bootloader and supported commands
self . send ( [ self . command . get , 0xFF ] , 2 , function ( data ) { // 0x00 ^ 0xFF
if ( self . verify _response ( self . status . ACK , data ) ) {
self . send ( [ ] , data [ 1 ] + 2 , function ( data ) { // data[1] = number of bytes that will follow (should be 12 + ack)
console . log ( 'STM32 - Bootloader version: ' + ( parseInt ( data [ 0 ] . toString ( 16 ) ) / 10 ) . toFixed ( 1 ) ) ; // convert dec to hex, hex to dec and add floating point
// proceed to next step
self . upload _procedure ( 3 ) ;
} ) ;
}
} ) ;
break ;
case 3 :
// get ID (device signature)
self . send ( [ self . command . get _ID , 0xFD ] , 2 , function ( data ) { // 0x01 ^ 0xFF
if ( self . verify _response ( self . status . ACK , data ) ) {
self . send ( [ ] , data [ 1 ] + 2 , function ( data ) { // data[1] = number of bytes that will follow (should be 1 + ack), its 2 + ack, WHY ???
var signature = ( data [ 0 ] << 8 ) | data [ 1 ] ;
console . log ( 'STM32 - Signature: 0x' + signature . toString ( 16 ) ) ; // signature in hex representation
if ( self . verify _chip _signature ( signature ) ) {
// proceed to next step
self . upload _procedure ( 4 ) ;
} else {
// disconnect
self . upload _procedure ( 99 ) ;
}
} ) ;
}
} ) ;
break ;
case 4 :
// erase memory
console . log ( 'Executing global chip erase' ) ;
2013-11-13 09:56:14 +00:00
STM32 . GUI _status ( 'Erasing' ) ;
2013-11-13 09:04:14 +00:00
self . send ( [ self . command . erase , 0xBC ] , 1 , function ( reply ) { // 0x43 ^ 0xFF
if ( self . verify _response ( self . status . ACK , reply ) ) {
self . send ( [ 0xFF , 0x00 ] , 1 , function ( reply ) {
if ( self . verify _response ( self . status . ACK , reply ) ) {
console . log ( 'Erasing: done' ) ;
console . log ( 'Writing data ...' ) ;
2013-11-13 09:56:14 +00:00
STM32 . GUI _status ( '<span style="color: green">Flashing ...</span>' ) ;
2013-11-13 09:04:14 +00:00
// proceed to next step
self . upload _procedure ( 5 ) ;
}
} ) ;
}
} ) ;
break ;
case 5 :
// upload
2013-11-15 16:03:50 +00:00
if ( self . bytes _flashed < self . hex . data . length ) {
if ( ( self . bytes _flashed + 256 ) <= self . hex . data . length ) {
2013-11-13 09:04:14 +00:00
var data _length = 256 ;
} else {
2013-11-15 16:03:50 +00:00
var data _length = self . hex . data . length - self . bytes _flashed ;
2013-11-13 09:04:14 +00:00
}
console . log ( 'STM32 - Writing to: 0x' + self . flashing _memory _address . toString ( 16 ) + ', ' + data _length + ' bytes' ) ;
self . send ( [ self . command . write _memory , 0xCE ] , 1 , function ( reply ) { // 0x31 ^ 0xFF
if ( self . verify _response ( self . status . ACK , reply ) ) {
// address needs to be transmitted as 32 bit integer, we need to bit shift each byte out and then calculate address checksum
2013-11-16 18:14:34 +00:00
var address = [ ( self . flashing _memory _address >> 24 ) , ( self . flashing _memory _address >> 16 ) , ( self . flashing _memory _address >> 8 ) , self . flashing _memory _address ] ;
2013-11-13 09:04:14 +00:00
var address _checksum = address [ 0 ] ^ address [ 1 ] ^ address [ 2 ] ^ address [ 3 ] ;
self . send ( [ address [ 0 ] , address [ 1 ] , address [ 2 ] , address [ 3 ] , address _checksum ] , 1 , function ( reply ) { // write start address + checksum
if ( self . verify _response ( self . status . ACK , reply ) ) {
var array _out = new Array ( data _length + 2 ) ; // 2 byte overhead [N, ...., checksum]
array _out [ 0 ] = data _length - 1 ; // number of bytes to be written (to write 128 bytes, N must be 127, to write 256 bytes, N must be 255)
var checksum = array _out [ 0 ] ;
for ( var i = 0 ; i < data _length ; i ++ ) {
2013-11-15 16:03:50 +00:00
array _out [ i + 1 ] = self . hex . data [ self . bytes _flashed ] ; // + 1 because of the first byte offset
checksum ^= self . hex . data [ self . bytes _flashed ] ;
2013-11-13 09:04:14 +00:00
self . bytes _flashed ++ ;
self . flashing _memory _address ++ ;
}
array _out [ array _out . length - 1 ] = checksum ; // checksum (last byte in the array_out array)
self . send ( array _out , 1 , function ( reply ) {
if ( self . verify _response ( self . status . ACK , reply ) ) {
// flash another page
self . upload _procedure ( 5 ) ;
}
} ) ;
}
} ) ;
}
} ) ;
2013-11-16 12:03:42 +00:00
// update progress bar
self . progress _bar _e . val ( self . bytes _flashed / ( self . hex . bytes * 2 ) * 100 ) ;
2013-11-13 09:04:14 +00:00
} else {
console . log ( 'Writing: done' ) ;
console . log ( 'Verifying data ...' ) ;
2013-11-13 09:56:14 +00:00
STM32 . GUI _status ( '<span style="color: green">Verifying ...</span>' ) ;
2013-11-13 09:04:14 +00:00
// proceed to next step
self . upload _procedure ( 6 ) ;
}
break ;
case 6 :
// verify
2013-11-15 16:03:50 +00:00
if ( self . bytes _verified < self . hex . data . length ) {
if ( ( self . bytes _verified + 256 ) <= self . hex . data . length ) {
2013-11-13 09:04:14 +00:00
var data _length = 256 ;
} else {
2013-11-15 16:03:50 +00:00
var data _length = self . hex . data . length - self . bytes _verified ;
2013-11-13 09:04:14 +00:00
}
console . log ( 'STM32 - Reading from: 0x' + self . verify _memory _address . toString ( 16 ) + ', ' + data _length + ' bytes' ) ;
self . send ( [ self . command . read _memory , 0xEE ] , 1 , function ( reply ) { // 0x11 ^ 0xFF
if ( self . verify _response ( self . status . ACK , reply ) ) {
2013-11-16 18:14:34 +00:00
var address = [ ( self . verify _memory _address >> 24 ) , ( self . verify _memory _address >> 16 ) , ( self . verify _memory _address >> 8 ) , self . verify _memory _address ] ;
2013-11-13 09:04:14 +00:00
var address _checksum = address [ 0 ] ^ address [ 1 ] ^ address [ 2 ] ^ address [ 3 ] ;
self . send ( [ address [ 0 ] , address [ 1 ] , address [ 2 ] , address [ 3 ] , address _checksum ] , 1 , function ( reply ) { // read start address + checksum
if ( self . verify _response ( self . status . ACK , reply ) ) {
var bytes _to _read _n = data _length - 1 ;
self . send ( [ bytes _to _read _n , ( ~ bytes _to _read _n ) & 0xFF ] , 1 , function ( reply ) { // bytes to be read + checksum XOR(complement of bytes_to_read_n)
if ( self . verify _response ( self . status . ACK , reply ) ) {
self . send ( [ ] , data _length , function ( data ) {
for ( var i = 0 ; i < data . length ; i ++ ) {
self . verify _hex . push ( data [ i ] ) ;
self . bytes _verified ++ ;
}
self . verify _memory _address += data _length ;
// verify another page
self . upload _procedure ( 6 ) ;
} ) ;
}
} ) ;
}
} ) ;
}
} ) ;
2013-11-16 12:03:42 +00:00
// update progress bar
self . progress _bar _e . val ( ( self . bytes _flashed + self . bytes _verified ) / ( self . hex . bytes * 2 ) * 100 ) ;
2013-11-13 09:04:14 +00:00
} else {
2013-11-15 16:03:50 +00:00
var result = self . verify _flash ( self . hex . data , self . verify _hex ) ;
2013-11-13 09:04:14 +00:00
if ( result ) {
console . log ( 'Verifying: done' ) ;
console . log ( 'Programming: SUCCESSFUL' ) ;
2013-11-13 09:56:14 +00:00
STM32 . GUI _status ( 'Programming: <strong style="color: green">SUCCESSFUL</strong>' ) ;
2013-11-13 09:04:14 +00:00
2013-11-16 12:03:42 +00:00
// update progress bar
self . progress _bar _e . addClass ( 'valid' ) ;
2013-11-13 09:04:14 +00:00
// proceed to next step
self . upload _procedure ( 7 ) ;
} else {
console . log ( 'Verifying: failed' ) ;
console . log ( 'Programming: FAILED' ) ;
2013-11-13 09:56:14 +00:00
STM32 . GUI _status ( 'Programming: <strong style="color: red">FAILED</strong>' ) ;
2013-11-13 09:04:14 +00:00
2013-11-16 12:03:42 +00:00
// update progress bar
self . progress _bar _e . addClass ( 'invalid' ) ;
2013-11-13 09:04:14 +00:00
// disconnect
self . upload _procedure ( 99 ) ;
}
}
break ;
case 7 :
// go
// memory address = 4 bytes, 1st high byte, 4th low byte, 5th byte = checksum XOR(byte 1, byte 2, byte 3, byte 4)
2013-11-16 18:14:34 +00:00
console . log ( 'Sending GO command: 0x' + self . hex . extended _linear _address [ 0 ] . toString ( 16 ) ) ;
2013-11-13 09:04:14 +00:00
self . send ( [ self . command . go , 0xDE ] , 1 , function ( reply ) { // 0x21 ^ 0xFF
if ( self . verify _response ( self . status . ACK , reply ) ) {
2013-11-16 18:14:34 +00:00
var gt _address = self . hex . extended _linear _address [ 0 ] ;
var address = [ ( gt _address >> 24 ) , ( gt _address >> 16 ) , ( gt _address >> 8 ) , gt _address ] ;
2013-11-15 15:17:43 +00:00
var address _checksum = address [ 0 ] ^ address [ 1 ] ^ address [ 2 ] ^ address [ 3 ] ;
self . send ( [ address [ 0 ] , address [ 1 ] , address [ 2 ] , address [ 3 ] , address _checksum ] , 1 , function ( reply ) {
2013-11-13 09:04:14 +00:00
if ( self . verify _response ( self . status . ACK , reply ) ) {
// disconnect
self . upload _procedure ( 99 ) ;
}
} ) ;
}
} ) ;
break ;
case 99 :
// disconnect
GUI . interval _remove ( 'firmware_uploader_read' ) ; // stop reading serial
GUI . interval _remove ( 'STM32_timeout' ) ; // stop STM32 timeout timer (everything is finished now)
2013-12-06 18:02:31 +00:00
console . log ( 'Transfered: ' + self . serial _bytes _send + ' bytes, Received: ' + self . serial _bytes _received + ' bytes' ) ;
console . log ( 'Script finished after: ' + ( microtime ( ) - self . upload _time _start ) . toFixed ( 4 ) + ' seconds, ' + self . steps _executed + ' steps' ) ;
2013-11-13 09:04:14 +00:00
// close connection
chrome . serial . close ( connectionId , function ( result ) {
2013-12-06 17:23:40 +00:00
connectionId = - 1 ; // reset connection id
2013-11-13 09:04:14 +00:00
if ( result ) { // All went as expected
console . log ( 'Connection closed successfully.' ) ;
} else { // Something went wrong
2013-11-14 18:16:33 +00:00
console . log ( 'There was an error that happened during "connection-close" procedure' ) ;
2013-11-13 09:04:14 +00:00
}
// unlocking connect button
GUI . connect _lock = false ;
} ) ;
break ;
}
} ;
// initialize object
var STM32 = new STM32 _protocol ( ) ;