bootloader dla Mega328p potrzebuje

Pytania, kody i porady dotyczące nie tylko Bascom.
ODPOWIEDZ
Awatar użytkownika
Pikczu
Posty: 390
Rejestracja: 17 sie 2015, 13:46
Lokalizacja: Dublin, Ireland
Kontakt:

bootloader dla Mega328p potrzebuje

Post autor: Pikczu » 10 sie 2019, 23:10

Chłopy na gwałt bootloadera jakiegoś potrzebuje na mege 328p najlepiej MCS owego ale strona MCS nie działa :(
Awatar użytkownika
niveasoft
Posty: 1216
Rejestracja: 17 sie 2015, 12:13
Kontakt:

Re: bootloader dla Mega328p potrzebuje

Post autor: niveasoft » 10 sie 2019, 23:15

Pikczu zakomentuj 168 a odhacz 328p :P
  1. '----------------------------------------------------------------
  2. '                          (c) 1995-2015, MCS
  3. '                        Bootloader.bas
  4. '  This sample demonstrates how you can write your own bootloader
  5. '  in BASCOM BASIC
  6. '  VERSION 2 of the BOOTLOADER. The waiting for the NAK is stretched
  7. '  further a bug was resolved for the M64/M128 that have a big page size
  8. '-----------------------------------------------------------------
  9. 'This sample will be extended to support other chips with bootloader
  10. 'The loader is supported from the IDE
  11. $hwstack = 40
  12. $swstack = 40
  13. $framesize = 40
  14. $crystal = 11059200
  15. '$crystal = 14745600
  16. $baud = 115200                                              'this loader uses serial com
  17. 'It is VERY IMPORTANT that the baud rate matches the one of the boot loader
  18. 'do not try to use buffered com as we can not use interrupts
  19.  
  20. 'possible return codes of the PC bootloader.exe
  21. ' -6005    Cancel requested
  22. ' -6006    Fatal time out
  23. ' -6007    Unrecoverable event during protocol
  24. ' -6008    Too many errors during protocol
  25. ' -6009    Block sequence error in Xmodem
  26. ' -6016    Session aborted
  27.  
  28.  
  29.  
  30.  
  31. '$regfile = "m8def.dat"
  32. 'Const Loaderchip = 8
  33.  
  34. $regfile = "m168def.dat"
  35. Const Loaderchip = 168
  36.  
  37. '$regfile = "m16def.dat"
  38. 'Const Loaderchip = 16
  39.  
  40. '$regfile = "m328pdef.dat"
  41. 'Const Loaderchip = 32
  42.  
  43. '$regfile = "m88def.dat"
  44. 'Const Loaderchip = 88
  45.  
  46. '$regfile = "m162def.dat"
  47. 'Const Loaderchip = 162
  48.  
  49. '$regfile = "m8515.dat"
  50. 'Const Loaderchip = 8515
  51.  
  52. '$regfile = "m128def.dat"
  53. 'Const Loaderchip = 128
  54.  
  55. '$regfile = "m64def.dat"
  56. 'Const Loaderchip = 64
  57.  
  58. '$regfile = "m2561def.dat"
  59. 'Const Loaderchip = 2561
  60.  
  61.  
  62. '$regfile = "m2560def.dat"
  63. 'Const Loaderchip = 2560
  64.  
  65. '$regfile = "m329def.dat"
  66. 'Const Loaderchip = 329
  67.  
  68. '$regfile = "m324pdef.dat"
  69. 'Const Loaderchip = 324
  70.  
  71.  
  72. '$regfile = "m644def.dat"
  73. '$regfile = "m644Pdef.dat"
  74. 'Const Loaderchip = 644
  75.  
  76. 'regfile="m1284def.dat"
  77. 'const loaderchip=1284
  78.  
  79.  
  80. '$regfile = "m164Pdef.dat"
  81. 'Const Loaderchip = 164
  82.  
  83. #if Loaderchip = 88                                         'Mega88
  84.     $loader = $c00                                          'this address you can find in the datasheet
  85.     'the loader address is the same as the boot vector address
  86.     Const Maxwordbit = 5
  87.     Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
  88. #elseif Loaderchip = 168                                    'Mega168
  89.     $loader = $1c00                                         'this address you can find in the datasheet
  90.     'the loader address is the same as the boot vector address
  91.     Const Maxwordbit = 6
  92.     Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
  93. #elseif Loaderchip = 32                                     ' Mega32
  94.     $loader = $3c00                                         ' 1024 words
  95.     Const Maxwordbit = 6                                    'Z6 is maximum bit                                   '
  96.     Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
  97. #elseif Loaderchip = 8                                      ' Mega8
  98.     $loader = $c00                                          ' 1024 words
  99.     Const Maxwordbit = 5                                    'Z5 is maximum bit                                   '
  100.     Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
  101. #elseif Loaderchip = 161                                    ' Mega161
  102.     $loader = $1e00                                         ' 1024 words
  103.     Const Maxwordbit = 6                                    'Z6 is maximum bit                                   '
  104. #elseif Loaderchip = 162                                    ' Mega162
  105.     $loader = $1c00                                         ' 1024 words
  106.     Const Maxwordbit = 6                                    'Z6 is maximum bit                                   '
  107.     Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
  108. #elseif Loaderchip = 8515                                   ' Mega8515
  109.     $loader = $c00                                          ' 1024 words
  110.     Const Maxwordbit = 5                                    'Z6 is maximum bit                                   '
  111.     Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
  112.     Osccal = &HB3                                           ' the internal osc needed a new value
  113. #elseif Loaderchip = 64                                     ' Mega64
  114.     $loader = $7c00                                         ' 1024 words
  115.     Const Maxwordbit = 7                                    'Z7 is maximum bit                                   '
  116.     Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
  117. #elseif Loaderchip = 128                                    ' Mega128
  118.     $loader = &HFC00                                        ' 1024 words
  119.     Const Maxwordbit = 7                                    'Z7 is maximum bit                                   '
  120.     Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
  121. #elseif Loaderchip = 2561                                   ' Mega2561
  122.     $loader = &H1FC00                                       ' 1024 words
  123.     Const Maxwordbit = 7                                    'Z7 is maximum bit                                   '
  124.     Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
  125. #elseif Loaderchip = 2560                                   ' Mega2560
  126.     $loader = &H1FC00                                       ' 1024 words
  127.     Const Maxwordbit = 7                                    'Z7 is maximum bit                                   '
  128.     Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
  129. #elseif Loaderchip = 16                                     ' Mega16
  130.     $loader = $1c00                                         ' 1024 words
  131.     Const Maxwordbit = 6                                    'Z6 is maximum bit                                   '
  132.     Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
  133. #elseif Loaderchip = 329                                    ' Mega32
  134.     $loader = $3c00                                         ' 1024 words
  135.     Const Maxwordbit = 6                                    'Z6 is maximum bit                                   '
  136.     Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
  137. #elseif Loaderchip = 324                                    ' Mega32
  138.     $loader = $3c00                                         ' 1024 words
  139.     Const Maxwordbit = 6                                    'Z6 is maximum bit                                   '
  140.     Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
  141. #elseif Loaderchip = 644                                    ' Mega644P
  142.     $loader = $7c00                                         ' 1024 words
  143.     Const Maxwordbit = 7                                    'Z7 is maximum bit                                   '
  144.     Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
  145. #elseif Loaderchip = 1284
  146.     $loader = &HFC00                                        ' 1024 words
  147.     Const Maxwordbit = 7                                    'Z7 is maximum bit                                   '
  148.     Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
  149. #elseif Loaderchip = 164                                    ' Mega164
  150.     $loader = $1c00                                         ' 1024 words
  151.     Const Maxwordbit = 6                                    'Z6 is maximum bit                                   '
  152.     Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
  153. #endif
  154.  
  155. 'MAXWORDBIT value, how to determine it?
  156. 'Maxwordbit should have the same value as the maximum Z bit.
  157. 'For Example , For The M88 the PDF Shows Pcword : Pc[4 : 0] And Z Value Z5 : Z1.
  158. ' In That Case The Maximum Bit is Z5 , Thus Bit 5 , Meaning Maxwordbit Is 5.
  159.  
  160. Const Maxword =(2 ^ Maxwordbit) * 2                         '128
  161. Const Maxwordshift = Maxwordbit + 1
  162. Const Cdebug = 0                                            ' leave this to 0
  163.  
  164. '#if Cdebug
  165. '   Print Maxword
  166. '   Print Maxwordshift
  167. '#endif
  168.  
  169.  
  170.  
  171. 'Dim the used variables
  172. Dim Bstatus As Byte , Bretries As Byte , Bblock As Byte , Bblocklocal As Byte
  173. Dim Bcsum1 As Byte , Bcsum2 As Byte , Buf(128) As Byte , Csum As Byte
  174. Dim J As Byte , Spmcrval As Byte                            ' self program command byte value
  175.  
  176. Dim Z As Long                                               'this is the Z pointer word
  177. Dim Vl As Byte , Vh As Byte                                 ' these bytes are used for the data values
  178. Dim Wrd As Word , Page As Word                              'these vars contain the page and word address
  179. Dim Bkind As Byte , Bstarted As Byte
  180. 'Mega 88 : 32 words, 128 pages
  181.  
  182. Dim Maxaddr As Long
  183.  
  184. Disable Interrupts                                          'we do not use ints
  185.  
  186.  
  187. Waitms 100                                                  'wait 100 msec sec
  188. 'We start with receiving a file. The PC must send this binary file
  189.  
  190. 'some constants used in serial com
  191. Const Nak = &H15
  192. Const Ack = &H06
  193. Const Can = &H18
  194.  
  195. 'we use some leds as indication in this sample , you might want to remove it
  196. 'Config Pinb.2 = Output
  197. 'Portb.2 = 1                                                 'the stk200 has inverted logic for the leds
  198. 'Config Pinb.3 = Output
  199. 'Portb.3 = 1
  200.  
  201. $timeout = 200000                                           'we use a timeout
  202. 'When you get LOADER errors during the upload, increase the timeout value
  203. 'for example at 16 Mhz, use 200000
  204.  
  205. Bretries = 10                                               'we try 5 times
  206. Testfor123:
  207. #if Cdebug
  208.     Print "Try " ; Bretries
  209.     Print "Wait"
  210. #endif
  211. Bstatus = Waitkey()                                         'wait for the loader to send a byte
  212. #if Cdebug
  213.    Print "Got "
  214. #endif
  215.  
  216. Print Chr(bstatus);
  217.  
  218. If Bstatus = 123 Then                                       'did we received value 123 ?
  219.    Bkind = 0                                                'normal flash loader
  220.    Goto Loader
  221. Elseif Bstatus = 124 Then                                   ' EEPROM
  222.    Bkind = 1                                                ' EEPROM loader
  223.    Goto Loader
  224. Elseif Bstatus <> 0 Then
  225.    Decr Bretries
  226.    If Bretries <> 0 Then Goto Testfor123                    'we test again
  227. End If
  228.  
  229. 'For J = 1 To 10                                             'this is a simple indication that we start the normal reset vector
  230. '    Waitms 100                                              'Toggle Portb.2 :
  231. 'Next
  232.  
  233. #if Cdebug
  234.   Print "RESET"
  235. #endif
  236. Goto _reset                                                 'goto the normal reset vector at address 0
  237.  
  238.  
  239. 'this is the loader routine. It is a Xmodem-checksum reception routine
  240. Loader:
  241.   #if Cdebug
  242.       Print "Clear buffer"
  243.   #endif
  244.   Do
  245.      Bstatus = Waitkey()
  246.   Loop Until Bstatus = 0
  247.  
  248.  
  249. '  For J = 1 To 3                                            'this is a simple indication that we start the normal reset vector
  250. '     Waitms 50                                              'Toggle Portb.2 :
  251. '  Next
  252.  
  253.   If Bkind = 0 Then
  254.      Spmcrval = 3 : Gosub Do_spm                            ' erase  the first page
  255.      Spmcrval = 17 : Gosub Do_spm                           ' re-enable page
  256.   End If
  257.  
  258.  
  259. Bretries = 10                                               'number of retries
  260.  
  261. Do
  262.   Bblocklocal = 1
  263.   Bstarted = 0                                              ' we were not started yet
  264.   Csum = 0                                                  'checksum is 0 when we start
  265.   Print Chr(nak);                                           ' firt time send a nack
  266.   Do
  267.  
  268.     Bstatus = Waitkey()                                     'wait for statuse byte
  269.  
  270.     Select Case Bstatus
  271.        Case 1:                                              ' start of heading, PC is ready to send
  272.             Csum = 1                                        'checksum is 1
  273.             Bblock = Waitkey() : Csum = Csum + Bblock       'get block
  274.             Bcsum1 = Waitkey() : Csum = Csum + Bcsum1       'get checksum first byte
  275.             For J = 1 To 128                                'get 128 bytes
  276.               Buf(j) = Waitkey() : Csum = Csum + Buf(j)
  277.             Next
  278.             Bcsum2 = Waitkey()                              'get second checksum byte
  279.             If Bblocklocal = Bblock Then                    'are the blocks the same?
  280.                If Bcsum2 = Csum Then                        'is the checksum the same?
  281.                   Gosub Writepage                           'yes go write the page
  282.                   Print Chr(ack);                           'acknowledge
  283.                   Incr Bblocklocal                          'increase local block count
  284.  
  285.                  Maxaddr = Maxaddr + 128
  286.                  If Maxaddr >= 30720 Then Exit Do
  287.  
  288.                Else                                         'no match so send nak
  289.                   Print Chr(nak);
  290.                End If
  291.             Else
  292.                Print Chr(nak);                              'blocks do not match
  293.             End If
  294.        Case 4:                                              ' end of transmission , file is transmitted
  295.              If Wrd > 0 And Bkind = 0 Then                  'if there was something left in the page
  296.                  Wrd = 0                                    'Z pointer needs wrd to be 0
  297.                  Spmcrval = 5 : Gosub Do_spm                'write page
  298.                  Spmcrval = 17 : Gosub Do_spm               ' re-enable page
  299.              End If
  300.             '  Waitms 100                                   ' OPTIONAL REMARK THIS IF THE DTR SIGNAL ARRIVES TO EARLY
  301.              Print Chr(ack);                                ' send ack and ready
  302.  
  303.              'Portb.3 = 0                                    ' simple indication that we are finished and ok
  304.              Waitms 20
  305.              Goto _reset                                    ' start new program
  306.        Case &H18:                                           ' PC aborts transmission
  307.              Goto _reset                                    ' ready
  308.        Case 123 : Exit Do                                   'was probably still in the buffer
  309.        Case 124 : Exit Do
  310.        Case Else
  311.           Exit Do                                           ' no valid data
  312.     End Select
  313.   Loop
  314.   If Bretries > 0 Then                                      'attempte left?
  315.      Waitms 1000
  316.      Decr Bretries                                          'decrease attempts
  317.   Else
  318.      Goto _reset                                            'reset chip
  319.   End If
  320. Loop
  321.  
  322.  
  323.  
  324. 'write one or more pages
  325. Writepage:
  326.  If Bkind = 0 Then
  327.    For J = 1 To 128 Step 2                                  'we write 2 bytes into a page
  328.       Vl = Buf(j) : Vh = Buf(j + 1)                         'get Low and High bytes
  329.       R0 = Vl                                               'store them into r0 and r1 registers
  330.       R1 = Vh
  331.       Spmcrval = 1 : Gosub Do_spm                           'write value into page at word address
  332.       Wrd = Wrd + 2                                         ' word address increases with 2 because LS bit of Z is not used
  333.       If Wrd = Maxword Then                                 ' page is full
  334.           Wrd = 0                                           'Z pointer needs wrd to be 0
  335.           Spmcrval = 5 : Gosub Do_spm                       'write page
  336.           Spmcrval = 17 : Gosub Do_spm                      ' re-enable page
  337.  
  338.           Page = Page + 1                                   'next page
  339.           Spmcrval = 3 : Gosub Do_spm                       ' erase  next page
  340.           Spmcrval = 17 : Gosub Do_spm                      ' re-enable page
  341.       End If
  342.    Next
  343.  
  344.  Else                                                       'eeprom
  345.      For J = 1 To 128
  346.        Writeeeprom Buf(j) , Wrd
  347.        Wrd = Wrd + 1
  348.      Next
  349.  End If
  350.  'Toggle Portb.2 : Waitms 10 : Toggle Portb.2                'indication that we write
  351. Return
  352.  
  353.  
  354. Do_spm:
  355.   Bitwait Spmcsr.0 , Reset                                  ' check for previous SPM complete
  356.   Bitwait Eecr.1 , Reset                                    'wait for eeprom
  357.  
  358.   Z = Page                                                  'make equal to page
  359.   Shift Z , Left , Maxwordshift                             'shift to proper place
  360.   Z = Z + Wrd                                               'add word
  361. !  lds r30,{Z}
  362. !  lds r31,{Z+1}
  363.  
  364.   #if _romsize > 65536
  365.       lds r24,{Z+2}
  366.       sts rampz,r24                                         ' we need to set rampz also for the M128
  367.   #endif
  368.  
  369.   Spmcsr = Spmcrval                                         'assign register
  370. !  spm                                                      'this is an asm instruction
  371. !  nop
  372. !  nop
  373. Return
  374.  
  375.  
  376. 'How you need to use this program:
  377. '1- compile this program
  378. '2- program into chip with sample elctronics programmer
  379. '3- select MCS Bootloader from programmers
  380. '4- compile a new program for example M88.bas
  381. '5- press F4 and reset your micro
  382. ' the program will now be uploaded into the chip with Xmodem Checksum
  383. ' you can write your own loader.too
  384. 'A stand alone command line loader is also available
  385.  
  386.  
  387. 'How to call the bootloader from your program without a reset ???
  388. 'Do
  389. '   Print "test"
  390. '   Waitms 1000
  391. '   If Inkey() = 27 Then
  392. '      Print "boot"
  393. '      Goto &H1C00
  394. '   End If
  395. 'Loop
  396.  
  397. 'The GOTO will do the work, you need to specify the correct bootloader address
  398. 'this is the same as the $LOADER statement.
Awatar użytkownika
Pikczu
Posty: 390
Rejestracja: 17 sie 2015, 13:46
Lokalizacja: Dublin, Ireland
Kontakt:

Re: bootloader dla Mega328p potrzebuje

Post autor: Pikczu » 10 sie 2019, 23:19

Dzięki wielkie Panie Inżynierze :)
ODPOWIEDZ