Home | Projects | Notes > Bootloader > Host Machine Setup (Python)
Install Python on your machine
Install pyserial
For Windows
1python -m pip install pyserialFor Ubuntu
xxxxxxxxxx11sudo apt install python3-serialFor MAC
xxxxxxxxxx31curl https://bootstrap.pypa.io/get-pip.py > get-pip.py2sudo python get-pup.py3sudo pip install pyserialRun host.py (See the The Host Application section below)
Enter the COM port to use (e.g., COM5)
To see all active ports, just press Enter.
Follow the instruction displayed by the python program.
host.py)The following code is provided by "Kiran Nayak" from the "FastBit Embedded Brain Academy". (https://github.com/niekiran/BootloaderProjectSTM32/blob/master/SourceCode/HOST/python/STM32_Programmer_V1.py)
x1import serial2import struct3import os4import sys5import glob6
7Flash_HAL_OK = 0x008Flash_HAL_ERROR = 0x019Flash_HAL_BUSY = 0x0210Flash_HAL_TIMEOUT = 0x0311Flash_HAL_INV_ADDR = 0x0412
13#BL Commands14COMMAND_BL_GET_VER = 0x5115COMMAND_BL_GET_HELP = 0x5216COMMAND_BL_GET_CID =0x5317COMMAND_BL_GET_RDP_STATUS =0x5418COMMAND_BL_GO_TO_ADDR =0x5519COMMAND_BL_FLASH_ERASE =0x5620COMMAND_BL_MEM_WRITE =0x5721COMMAND_BL_EN_R_W_PROTECT =0x5822COMMAND_BL_MEM_READ =0x5923COMMAND_BL_READ_SECTOR_P_STATUS =0x5A24COMMAND_BL_OTP_READ =0x5B25COMMAND_BL_DIS_R_W_PROTECT =0x5C26COMMAND_BL_MY_NEW_COMMAND =0x5D27
28
29#len details of the command30COMMAND_BL_GET_VER_LEN =631COMMAND_BL_GET_HELP_LEN =632COMMAND_BL_GET_CID_LEN =633COMMAND_BL_GET_RDP_STATUS_LEN =634COMMAND_BL_GO_TO_ADDR_LEN =1035COMMAND_BL_FLASH_ERASE_LEN =836COMMAND_BL_MEM_WRITE_LEN = 1137COMMAND_BL_EN_R_W_PROTECT_LEN =838COMMAND_BL_READ_SECTOR_P_STATUS_LEN =639COMMAND_BL_DIS_R_W_PROTECT_LEN =640COMMAND_BL_MY_NEW_COMMAND_LEN =841
42
43verbose_mode = 144mem_write_active =045
46#----------------------------- file ops----------------------------------------47
48def calc_file_len():49 size = os.path.getsize("user_app.bin")50 return size51
52def open_the_file():53 global bin_file54 bin_file = open('user_app.bin','rb')55 #read = bin_file.read()56 #global file_contents = bytearray(read)57
58def read_the_file():59 pass60
61def close_the_file():62 bin_file.close()63
64
65
66
67#----------------------------- utilities----------------------------------------68
69def word_to_byte(addr, index , lowerfirst):70 value = (addr >> ( 8 * ( index -1)) & 0x000000FF )71 return value72
73def get_crc(buff, length):74 Crc = 0xFFFFFFFF75 #print(length)76 for data in buff[0:length]:77 Crc = Crc ^ data78 for i in range(32):79 if(Crc & 0x80000000):80 Crc = (Crc << 1) ^ 0x04C11DB781 else:82 Crc = (Crc << 1)83 return Crc84
85#----------------------------- Serial Port ----------------------------------------86def serial_ports():87 """ Lists serial port names88
89 :raises EnvironmentError:90 On unsupported or unknown platforms91 :returns:92 A list of the serial ports available on the system93 """94 if sys.platform.startswith('win'):95 ports = ['COM%s' % (i + 1) for i in range(256)]96 elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):97 # this excludes your current terminal "/dev/tty"98 ports = glob.glob('/dev/tty[A-Za-z]*')99 elif sys.platform.startswith('darwin'):100 ports = glob.glob('/dev/tty.*')101 else:102 raise EnvironmentError('Unsupported platform')103
104 result = []105 for port in ports:106 try:107 s = serial.Serial(port)108 s.close()109 result.append(port)110 except (OSError, serial.SerialException):111 pass112 return result113
114def Serial_Port_Configuration(port):115 global ser116 try:117 ser = serial.Serial(port,115200,timeout=2)118 except:119 print("\n Oops! That was not a valid port")120 121 port = serial_ports()122 if(not port):123 print("\n No ports Detected")124 else:125 print("\n Here are some available ports on your PC. Try Again!")126 print("\n ",port)127 return -1128 if ser.is_open:129 print("\n Port Open Success")130 else:131 print("\n Port Open Failed")132 return 0133
134 135def read_serial_port(length):136 read_value = ser.read(length)137 return read_value138
139def Close_serial_port():140 pass141def purge_serial_port():142 ser.reset_input_buffer()143 144def Write_to_serial_port(value, *length):145 data = struct.pack('>B', value)146 if (verbose_mode):147 value = bytearray(data)148 #print(" "+hex(value[0]), end='')149 print(" "+"0x{:02x}".format(value[0]),end=' ')150 if(mem_write_active and (not verbose_mode)):151 print("#",end=' ')152 ser.write(data)153
154
155 156#----------------------------- command processing----------------------------------------157
158def process_COMMAND_BL_MY_NEW_COMMAND(length):159 pass160
161def process_COMMAND_BL_GET_VER(length):162 ver=read_serial_port(1)163 value = bytearray(ver)164 print("\n Bootloader Ver. : ",hex(value[0]))165
166def process_COMMAND_BL_GET_HELP(length):167 #print("reading:", length)168 value = read_serial_port(length) 169 reply = bytearray(value)170 print("\n Supported Commands :",end=' ')171 for x in reply:172 print(hex(x),end=' ')173 print()174
175def process_COMMAND_BL_GET_CID(length):176 value = read_serial_port(length)177 ci = (value[1] << 8 )+ value[0]178 print("\n Chip Id. : ",hex(ci))179
180def process_COMMAND_BL_GET_RDP_STATUS(length):181 value = read_serial_port(length)182 rdp = bytearray(value)183 print("\n RDP Status : ",hex(rdp[0]))184
185def process_COMMAND_BL_GO_TO_ADDR(length):186 addr_status=0187 value = read_serial_port(length)188 addr_status = bytearray(value)189 print("\n Address Status : ",hex(addr_status[0]))190
191def process_COMMAND_BL_FLASH_ERASE(length):192 erase_status=0193 value = read_serial_port(length)194 if len(value):195 erase_status = bytearray(value)196 if(erase_status[0] == Flash_HAL_OK):197 print("\n Erase Status: Success Code: FLASH_HAL_OK")198 elif(erase_status[0] == Flash_HAL_ERROR):199 print("\n Erase Status: Fail Code: FLASH_HAL_ERROR")200 elif(erase_status[0] == Flash_HAL_BUSY):201 print("\n Erase Status: Fail Code: FLASH_HAL_BUSY")202 elif(erase_status[0] == Flash_HAL_TIMEOUT):203 print("\n Erase Status: Fail Code: FLASH_HAL_TIMEOUT")204 elif(erase_status[0] == Flash_HAL_INV_ADDR):205 print("\n Erase Status: Fail Code: FLASH_HAL_INV_SECTOR")206 else:207 print("\n Erase Status: Fail Code: UNKNOWN_ERROR_CODE")208 else:209 print("Timeout: Bootloader is not responding")210
211def process_COMMAND_BL_MEM_WRITE(length):212 write_status=0213 value = read_serial_port(length)214 write_status = bytearray(value)215 if(write_status[0] == Flash_HAL_OK):216 print("\n Write_status: FLASH_HAL_OK")217 elif(write_status[0] == Flash_HAL_ERROR):218 print("\n Write_status: FLASH_HAL_ERROR")219 elif(write_status[0] == Flash_HAL_BUSY):220 print("\n Write_status: FLASH_HAL_BUSY")221 elif(write_status[0] == Flash_HAL_TIMEOUT):222 print("\n Write_status: FLASH_HAL_TIMEOUT")223 elif(write_status[0] == Flash_HAL_INV_ADDR):224 print("\n Write_status: FLASH_HAL_INV_ADDR")225 else:226 print("\n Write_status: UNKNOWN_ERROR")227 print("\n")228 229
230def process_COMMAND_BL_FLASH_MASS_ERASE(length):231 pass232
233
234
235protection_mode= [ "Write Protection", "Read/Write Protection","No protection" ]236def protection_type(status,n):237 if( status & (1 << 15) ):238 #PCROP is active239 if(status & (1 << n) ):240 return protection_mode[1]241 else:242 return protection_mode[2]243 else:244 if(status & (1 << n)):245 return protection_mode[2]246 else:247 return protection_mode[0]248 249 250 251 252def process_COMMAND_BL_READ_SECTOR_STATUS(length):253 s_status=0254
255 value = read_serial_port(length)256 s_status = bytearray(value)257 #s_status.flash_sector_status = (uint16_t)(status[1] << 8 | status[0] )258 print("\n Sector Status : ",s_status[0])259 print("\n ====================================")260 print("\n Sector \tProtection") 261 print("\n ====================================")262 if(s_status[0] & (1 << 15)):263 #PCROP is active264 print("\n Flash protection mode : Read/Write Protection(PCROP)\n")265 else:266 print("\n Flash protection mode : \tWrite Protection\n")267
268 for x in range(8):269 print("\n Sector{0} {1}".format(x,protection_type(s_status[0],x) ) )270 271
272
273def process_COMMAND_BL_DIS_R_W_PROTECT(length):274 status=0275 value = read_serial_port(length)276 status = bytearray(value)277 if(status[0]):278 print("\n FAIL")279 else:280 print("\n SUCCESS")281
282def process_COMMAND_BL_EN_R_W_PROTECT(length):283 status=0284 value = read_serial_port(length)285 status = bytearray(value)286 if(status[0]):287 print("\n FAIL")288 else:289 print("\n SUCCESS")290
291
292
293
294def decode_menu_command_code(command):295 ret_value = 0296 data_buf = []297 for i in range(255):298 data_buf.append(0)299 300 if(command == 0 ):301 print("\n Exiting...!")302 raise SystemExit303 elif(command == 1):304 print("\n Command == > BL_GET_VER")305 COMMAND_BL_GET_VER_LEN = 6306 data_buf[0] = COMMAND_BL_GET_VER_LEN-1 307 data_buf[1] = COMMAND_BL_GET_VER 308 crc32 = get_crc(data_buf,COMMAND_BL_GET_VER_LEN-4)309 crc32 = crc32 & 0xffffffff310 data_buf[2] = word_to_byte(crc32,1,1) 311 data_buf[3] = word_to_byte(crc32,2,1) 312 data_buf[4] = word_to_byte(crc32,3,1) 313 data_buf[5] = word_to_byte(crc32,4,1) 314
315 316 Write_to_serial_port(data_buf[0],1)317 for i in data_buf[1:COMMAND_BL_GET_VER_LEN]:318 Write_to_serial_port(i,COMMAND_BL_GET_VER_LEN-1)319 320
321 ret_value = read_bootloader_reply(data_buf[1])322 323 324
325 elif(command == 2):326 print("\n Command == > BL_GET_HELP")327 COMMAND_BL_GET_HELP_LEN =6328 data_buf[0] = COMMAND_BL_GET_HELP_LEN-1 329 data_buf[1] = COMMAND_BL_GET_HELP 330 crc32 = get_crc(data_buf,COMMAND_BL_GET_HELP_LEN-4)331 crc32 = crc32 & 0xffffffff332 data_buf[2] = word_to_byte(crc32,1,1) 333 data_buf[3] = word_to_byte(crc32,2,1) 334 data_buf[4] = word_to_byte(crc32,3,1) 335 data_buf[5] = word_to_byte(crc32,4,1) 336
337 338 Write_to_serial_port(data_buf[0],1)339 for i in data_buf[1:COMMAND_BL_GET_HELP_LEN]:340 Write_to_serial_port(i,COMMAND_BL_GET_HELP_LEN-1)341 342
343 ret_value = read_bootloader_reply(data_buf[1])344 elif(command == 3):345 print("\n Command == > BL_GET_CID")346 COMMAND_BL_GET_CID_LEN =6347 data_buf[0] = COMMAND_BL_GET_CID_LEN-1 348 data_buf[1] = COMMAND_BL_GET_CID 349 crc32 = get_crc(data_buf,COMMAND_BL_GET_CID_LEN-4)350 crc32 = crc32 & 0xffffffff351 data_buf[2] = word_to_byte(crc32,1,1) 352 data_buf[3] = word_to_byte(crc32,2,1) 353 data_buf[4] = word_to_byte(crc32,3,1) 354 data_buf[5] = word_to_byte(crc32,4,1) 355
356 357 Write_to_serial_port(data_buf[0],1)358 for i in data_buf[1:COMMAND_BL_GET_CID_LEN]:359 Write_to_serial_port(i,COMMAND_BL_GET_CID_LEN-1)360 361
362 ret_value = read_bootloader_reply(data_buf[1])363
364 elif(command == 4):365 print("\n Command == > BL_GET_RDP_STATUS")366 data_buf[0] = COMMAND_BL_GET_RDP_STATUS_LEN-1367 data_buf[1] = COMMAND_BL_GET_RDP_STATUS368 crc32 = get_crc(data_buf,COMMAND_BL_GET_RDP_STATUS_LEN-4)369 crc32 = crc32 & 0xffffffff370 data_buf[2] = word_to_byte(crc32,1,1)371 data_buf[3] = word_to_byte(crc32,2,1)372 data_buf[4] = word_to_byte(crc32,3,1)373 data_buf[5] = word_to_byte(crc32,4,1)374 375 Write_to_serial_port(data_buf[0],1)376 377 for i in data_buf[1:COMMAND_BL_GET_RDP_STATUS_LEN]:378 Write_to_serial_port(i,COMMAND_BL_GET_RDP_STATUS_LEN-1)379 380 ret_value = read_bootloader_reply(data_buf[1])381 elif(command == 5):382 print("\n Command == > BL_GO_TO_ADDR")383 go_address = input("\n Please enter 4 bytes go address in hex:")384 go_address = int(go_address, 16)385 data_buf[0] = COMMAND_BL_GO_TO_ADDR_LEN-1 386 data_buf[1] = COMMAND_BL_GO_TO_ADDR 387 data_buf[2] = word_to_byte(go_address,1,1) 388 data_buf[3] = word_to_byte(go_address,2,1) 389 data_buf[4] = word_to_byte(go_address,3,1) 390 data_buf[5] = word_to_byte(go_address,4,1) 391 crc32 = get_crc(data_buf,COMMAND_BL_GO_TO_ADDR_LEN-4) 392 data_buf[6] = word_to_byte(crc32,1,1) 393 data_buf[7] = word_to_byte(crc32,2,1) 394 data_buf[8] = word_to_byte(crc32,3,1) 395 data_buf[9] = word_to_byte(crc32,4,1) 396
397 Write_to_serial_port(data_buf[0],1)398 399 for i in data_buf[1:COMMAND_BL_GO_TO_ADDR_LEN]:400 Write_to_serial_port(i,COMMAND_BL_GO_TO_ADDR_LEN-1)401 402 ret_value = read_bootloader_reply(data_buf[1])403 404 elif(command == 6):405 print("\n This command is not supported")406 elif(command == 7):407 print("\n Command == > BL_FLASH_ERASE")408 data_buf[0] = COMMAND_BL_FLASH_ERASE_LEN-1 409 data_buf[1] = COMMAND_BL_FLASH_ERASE 410 sector_num = input("\n Enter sector number(0-7 or 0xFF) here :")411 sector_num = int(sector_num, 16)412 if(sector_num != 0xff):413 nsec=int(input("\n Enter number of sectors to erase(max 8) here :"))414 415 data_buf[2]= sector_num 416 data_buf[3]= nsec 417
418 crc32 = get_crc(data_buf,COMMAND_BL_FLASH_ERASE_LEN-4) 419 data_buf[4] = word_to_byte(crc32,1,1) 420 data_buf[5] = word_to_byte(crc32,2,1) 421 data_buf[6] = word_to_byte(crc32,3,1) 422 data_buf[7] = word_to_byte(crc32,4,1) 423
424 Write_to_serial_port(data_buf[0],1)425 426 for i in data_buf[1:COMMAND_BL_FLASH_ERASE_LEN]:427 Write_to_serial_port(i,COMMAND_BL_FLASH_ERASE_LEN-1)428 429 ret_value = read_bootloader_reply(data_buf[1])430 431 elif(command == 8):432 print("\n Command == > BL_MEM_WRITE")433 bytes_remaining=0434 t_len_of_file=0435 bytes_so_far_sent = 0436 len_to_read=0437 base_mem_address=0438
439 data_buf[1] = COMMAND_BL_MEM_WRITE440
441 #First get the total number of bytes in the .bin file.442 t_len_of_file =calc_file_len()443
444 #keep opening the file445 open_the_file()446
447 bytes_remaining = t_len_of_file - bytes_so_far_sent448
449 base_mem_address = input("\n Enter the memory write address here :")450 base_mem_address = int(base_mem_address, 16)451 global mem_write_active452 while(bytes_remaining):453 mem_write_active=1454 if(bytes_remaining >= 128):455 len_to_read = 128456 else:457 len_to_read = bytes_remaining458 #get the bytes in to buffer by reading file459 for x in range(len_to_read):460 file_read_value = bin_file.read(1)461 file_read_value = bytearray(file_read_value)462 data_buf[7+x]= int(file_read_value[0])463 #read_the_file(&data_buf[7],len_to_read) 464 #print("\n base mem address = \n",base_mem_address, hex(base_mem_address)) 465
466 #populate base mem address467 data_buf[2] = word_to_byte(base_mem_address,1,1)468 data_buf[3] = word_to_byte(base_mem_address,2,1)469 data_buf[4] = word_to_byte(base_mem_address,3,1)470 data_buf[5] = word_to_byte(base_mem_address,4,1)471
472 data_buf[6] = len_to_read473
474 #/* 1 byte len + 1 byte command code + 4 byte mem base address475 #* 1 byte payload len + len_to_read is amount of bytes read from file + 4 byte CRC476 #*/477 mem_write_cmd_total_len = COMMAND_BL_MEM_WRITE_LEN+len_to_read478
479 #first field is "len_to_follow"480 data_buf[0] =mem_write_cmd_total_len-1481
482 crc32 = get_crc(data_buf,mem_write_cmd_total_len-4)483 data_buf[7+len_to_read] = word_to_byte(crc32,1,1)484 data_buf[8+len_to_read] = word_to_byte(crc32,2,1)485 data_buf[9+len_to_read] = word_to_byte(crc32,3,1)486 data_buf[10+len_to_read] = word_to_byte(crc32,4,1)487
488 #update base mem address for the next loop489 base_mem_address+=len_to_read490
491 Write_to_serial_port(data_buf[0],1)492 493 for i in data_buf[1:mem_write_cmd_total_len]:494 Write_to_serial_port(i,mem_write_cmd_total_len-1)495
496 bytes_so_far_sent+=len_to_read497 bytes_remaining = t_len_of_file - bytes_so_far_sent498 print("\n bytes_so_far_sent:{0} -- bytes_remaining:{1}\n".format(bytes_so_far_sent,bytes_remaining)) 499 500 ret_value = read_bootloader_reply(data_buf[1])501 mem_write_active=0502
503 504 505 elif(command == 9):506 print("\n Command == > BL_EN_R_W_PROTECT")507 total_sector = int(input("\n How many sectors do you want to protect ?: "))508 sector_numbers = [0,0,0,0,0,0,0,0]509 sector_details=0510 for x in range(total_sector):511 sector_numbers[x]=int(input("\n Enter sector number[{0}]: ".format(x+1) ))512 sector_details = sector_details | (1 << sector_numbers[x])513
514 #print("Sector details",sector_details)515 print("\n Mode:Flash sectors Write Protection: 1")516 print("\n Mode:Flash sectors Read/Write Protection: 2")517 mode=input("\n Enter Sector Protection Mode(1 or 2 ):")518 mode = int(mode)519 if(mode != 2 and mode != 1):520 printf("\n Invalid option : Command Dropped")521 return522 if(mode == 2):523 print("\n This feature is currently not supported !") 524 return525
526 data_buf[0] = COMMAND_BL_EN_R_W_PROTECT_LEN-1 527 data_buf[1] = COMMAND_BL_EN_R_W_PROTECT 528 data_buf[2] = sector_details 529 data_buf[3] = mode 530 crc32 = get_crc(data_buf,COMMAND_BL_EN_R_W_PROTECT_LEN-4) 531 data_buf[4] = word_to_byte(crc32,1,1) 532 data_buf[5] = word_to_byte(crc32,2,1) 533 data_buf[6] = word_to_byte(crc32,3,1) 534 data_buf[7] = word_to_byte(crc32,4,1) 535
536 Write_to_serial_port(data_buf[0],1)537 538 for i in data_buf[1:COMMAND_BL_EN_R_W_PROTECT_LEN]:539 Write_to_serial_port(i,COMMAND_BL_EN_R_W_PROTECT_LEN-1)540 541 ret_value = read_bootloader_reply(data_buf[1])542 543 544 elif(command == 10):545 print("\n Command == > COMMAND_BL_MEM_READ")546 print("\n This command is not supported")547 elif(command == 11):548 print("\n Command == > COMMAND_BL_READ_SECTOR_P_STATUS")549 data_buf[0] = COMMAND_BL_READ_SECTOR_P_STATUS_LEN-1 550 data_buf[1] = COMMAND_BL_READ_SECTOR_P_STATUS 551
552 crc32 = get_crc(data_buf,COMMAND_BL_READ_SECTOR_P_STATUS_LEN-4) 553 data_buf[2] = word_to_byte(crc32,1,1) 554 data_buf[3] = word_to_byte(crc32,2,1) 555 data_buf[4] = word_to_byte(crc32,3,1) 556 data_buf[5] = word_to_byte(crc32,4,1) 557
558 Write_to_serial_port(data_buf[0],1)559 560 for i in data_buf[1:COMMAND_BL_READ_SECTOR_P_STATUS_LEN]:561 Write_to_serial_port(i,COMMAND_BL_READ_SECTOR_P_STATUS_LEN-1)562 563 ret_value = read_bootloader_reply(data_buf[1])564
565 elif(command == 12):566 print("\n Command == > COMMAND_OTP_READ")567 print("\n This command is not supported")568 elif(command == 13):569 print("\n Command == > COMMAND_BL_DIS_R_W_PROTECT")570 data_buf[0] = COMMAND_BL_DIS_R_W_PROTECT_LEN-1 571 data_buf[1] = COMMAND_BL_DIS_R_W_PROTECT 572 crc32 = get_crc(data_buf,COMMAND_BL_DIS_R_W_PROTECT_LEN-4) 573 data_buf[2] = word_to_byte(crc32,1,1) 574 data_buf[3] = word_to_byte(crc32,2,1) 575 data_buf[4] = word_to_byte(crc32,3,1) 576 data_buf[5] = word_to_byte(crc32,4,1) 577
578 Write_to_serial_port(data_buf[0],1)579 580 for i in data_buf[1:COMMAND_BL_DIS_R_W_PROTECT_LEN]:581 Write_to_serial_port(i,COMMAND_BL_DIS_R_W_PROTECT_LEN-1)582 583 ret_value = read_bootloader_reply(data_buf[1])584 585 elif(command == 14):586 print("\n Command == > COMMAND_BL_MY_NEW_COMMAND ")587 data_buf[0] = COMMAND_BL_MY_NEW_COMMAND_LEN-1 588 data_buf[1] = COMMAND_BL_MY_NEW_COMMAND 589 crc32 = get_crc(data_buf,COMMAND_BL_MY_NEW_COMMAND_LEN-4) 590 data_buf[2] = word_to_byte(crc32,1,1) 591 data_buf[3] = word_to_byte(crc32,2,1) 592 data_buf[4] = word_to_byte(crc32,3,1) 593 data_buf[5] = word_to_byte(crc32,4,1) 594
595 Write_to_serial_port(data_buf[0],1)596 597 for i in data_buf[1:COMMAND_BL_MY_NEW_COMMAND_LEN]:598 Write_to_serial_port(i,COMMAND_BL_MY_NEW_COMMAND_LEN-1)599 600 ret_value = read_bootloader_reply(data_buf[1])601 else:602 print("\n Please input valid command code\n")603 return604
605 if ret_value == -2 :606 print("\n TimeOut : No response from the bootloader")607 print("\n Reset the board and Try Again !")608 return609
610def read_bootloader_reply(command_code):611 #ack=[0,0]612 len_to_follow=0 613 ret = -2 614
615 #read_serial_port(ack,2)616 #ack = ser.read(2)617 ack=read_serial_port(2)618 if(len(ack) ):619 a_array=bytearray(ack)620 #print("read uart:",ack) 621 if (a_array[0]== 0xA5):622 #CRC of last command was good .. received ACK and "len to follow"623 len_to_follow=a_array[1]624 print("\n CRC : SUCCESS Len :",len_to_follow)625 #print("command_code:",hex(command_code))626 if (command_code) == COMMAND_BL_GET_VER :627 process_COMMAND_BL_GET_VER(len_to_follow)628 629 elif(command_code) == COMMAND_BL_GET_HELP:630 process_COMMAND_BL_GET_HELP(len_to_follow)631 632 elif(command_code) == COMMAND_BL_GET_CID:633 process_COMMAND_BL_GET_CID(len_to_follow)634 635 elif(command_code) == COMMAND_BL_GET_RDP_STATUS:636 process_COMMAND_BL_GET_RDP_STATUS(len_to_follow)637 638 elif(command_code) == COMMAND_BL_GO_TO_ADDR:639 process_COMMAND_BL_GO_TO_ADDR(len_to_follow)640 641 elif(command_code) == COMMAND_BL_FLASH_ERASE:642 process_COMMAND_BL_FLASH_ERASE(len_to_follow)643 644 elif(command_code) == COMMAND_BL_MEM_WRITE:645 process_COMMAND_BL_MEM_WRITE(len_to_follow)646 647 elif(command_code) == COMMAND_BL_READ_SECTOR_P_STATUS:648 process_COMMAND_BL_READ_SECTOR_STATUS(len_to_follow)649 650 elif(command_code) == COMMAND_BL_EN_R_W_PROTECT:651 process_COMMAND_BL_EN_R_W_PROTECT(len_to_follow)652 653 elif(command_code) == COMMAND_BL_DIS_R_W_PROTECT:654 process_COMMAND_BL_DIS_R_W_PROTECT(len_to_follow)655 656 elif(command_code) == COMMAND_BL_MY_NEW_COMMAND:657 process_COMMAND_BL_MY_NEW_COMMAND(len_to_follow)658 659 else:660 print("\n Invalid command code\n")661 662 ret = 0663 664 elif a_array[0] == 0x7F:665 #CRC of last command was bad .. received NACK666 print("\n CRC: FAIL \n")667 ret= -1668 else:669 print("\n Timeout : Bootloader not responding")670 671 return ret672
673 674 675
676#----------------------------- Ask Menu implementation----------------------------------------677
678
679name = input("Enter the Port Name of your device(Ex: COM3):")680ret = 0681ret=Serial_Port_Configuration(name)682if(ret < 0):683 decode_menu_command_code(0)684 685
686 687 688while True:689 print("\n +==========================================+")690 print(" | Menu |")691 print(" | STM32F4 BootLoader v1 |")692 print(" +==========================================+")693
694 695 696 print("\n Which BL command do you want to send ??\n")697 print(" BL_GET_VER --> 1")698 print(" BL_GET_HLP --> 2")699 print(" BL_GET_CID --> 3")700 print(" BL_GET_RDP_STATUS --> 4")701 print(" BL_GO_TO_ADDR --> 5")702 print(" BL_FLASH_MASS_ERASE --> 6")703 print(" BL_FLASH_ERASE --> 7")704 print(" BL_MEM_WRITE --> 8")705 print(" BL_EN_R_W_PROTECT --> 9")706 print(" BL_MEM_READ --> 10")707 print(" BL_READ_SECTOR_P_STATUS --> 11")708 print(" BL_OTP_READ --> 12")709 print(" BL_DIS_R_W_PROTECT --> 13")710 print(" BL_MY_NEW_COMMAND --> 14")711 print(" MENU_EXIT --> 0")712
713 #command_code = int(input("\n Type the command code here :") )714
715 command_code = input("\n Type the command code here :")716
717 if(not command_code.isdigit()):718 print("\n Please Input valid code shown above")719 else:720 decode_menu_command_code(int(command_code))721
722 input("\n Press any key to continue :")723 purge_serial_port()724
725
726
727def check_flash_status():728 pass729
730def protection_type():731 pass732