diff --git a/mik32_debug_hal/eeprom.py b/mik32_debug_hal/eeprom.py index 93f3331..d1ffa90 100644 --- a/mik32_debug_hal/eeprom.py +++ b/mik32_debug_hal/eeprom.py @@ -1,4 +1,8 @@ +import datetime from enum import Enum +import os +import pathlib +import sys from typing import Dict, List import time from tclrpc import OpenOcdTclRpc @@ -257,3 +261,55 @@ def write_pages(pages: Dict[int, List[int]], openocd: OpenOcdTclRpc) -> int: print("EEPROM page recording completed", flush=True) return 0 + + +def wait_halted(openocd: OpenOcdTclRpc, timeout_seconds: float = 2): + openocd.run(f'wait_halt {int(timeout_seconds * 1000)}') + + +def collapse_pages(pages: Dict[int, List[int]]) -> List[int]: + bytes_list: List[int] = [] + for page in range(64): + page = pages.get(page * 128) + if page is not None: + bytes_list.extend(page) + else: + bytes_list.extend([0]*128) + + return bytes_list + + +def write_memory(pages: Dict[int, List[int]], openocd: OpenOcdTclRpc) -> int: + result = 0 + + openocd.halt() + openocd.write_memory(0x02003800, 32, [1]) + print("EEPROM Init...") + + pathname = os.path.dirname(sys.argv[0]) + openocd.run("wp 0x2003800 4 w") + print("Loading Driver...", flush=True) + # openocd.run("load_image {%s}" % pathlib.Path(os.path.join(pathname, "firmware.hex"))) + openocd.run("load_image {%s}" % pathlib.Path( + "C:\\Users\\user\\.platformio\\packages\\tool-mik32-uploader\\upload_drivers\\jtag_eeprom\\.pio\\build\\mik32v2\\firmware.hex" + )) + + # openocd.resume(0x2000000) + # wait_halted(openocd) + # print(f"Check page result {openocd.read_memory(0x2003800, 32, 1)}") + + bytes_list = collapse_pages(pages) + + print("Uploading data...", flush=True) + openocd.write_memory(0x02001800, 8, bytes_list) + print("Uploading data complete!", flush=True) + + print("Run driver...", flush=True) + openocd.resume(0x2000000) + wait_halted(openocd, 10) + print(f"Check page result {openocd.read_memory(0x2003800, 32, 1)}") + + if result == 0: + # Прошивка страниц флеш памяти по SPIFI была завершена + print(f"{datetime.datetime.now().time()} EEPROM recording has been completed", flush=True) + return 0 diff --git a/mik32_upload.py b/mik32_upload.py index ae1fc4c..c5f2a1b 100644 --- a/mik32_upload.py +++ b/mik32_upload.py @@ -254,7 +254,9 @@ def upload_file( if (pages.pages_eeprom.__len__() > 0): start_time = time.perf_counter() - result |= eeprom.write_pages( + # result |= eeprom.write_pages( + # pages.pages_eeprom, openocd) + result |= eeprom.write_memory( pages.pages_eeprom, openocd) write_time = time.perf_counter() - start_time @@ -268,6 +270,8 @@ def upload_file( gpio_init(openocd, mik_version) start_time = time.perf_counter() + # result |= spifi.write_pages( + # pages.pages_spifi, openocd, use_quad_spi=use_quad_spi) result |= spifi.write_pages_by_sectors( pages.pages_spifi, openocd, use_quad_spi=use_quad_spi) diff --git a/upload_drivers/jtag_eeprom/.gitignore b/upload_drivers/jtag_eeprom/.gitignore new file mode 100644 index 0000000..edb3832 --- /dev/null +++ b/upload_drivers/jtag_eeprom/.gitignore @@ -0,0 +1,2 @@ +.vscode +idedata.json diff --git a/upload_drivers/jtag_eeprom/.pio/build/mik32v2/firmware.hex b/upload_drivers/jtag_eeprom/.pio/build/mik32v2/firmware.hex new file mode 100644 index 0000000..c8d2c54 --- /dev/null +++ b/upload_drivers/jtag_eeprom/.pio/build/mik32v2/firmware.hex @@ -0,0 +1,215 @@ +:020000040200F8 +:10000000FD62938202400100FD12E39E02FE374131 +:10001000000213010100B7110002938101CBB71553 +:100020000002938505CB37160002130606CBB716E0 +:100030000002938606CB39A083A2050023A05600B8 +:1000400091059106E3EAC5FEB7150002938505CB3D +:1000500037160002130606D4B7160002938606CBA5 +:1000600039A083A2050023A0560091059106E3EA7A +:10007000C5FEB7150002938505CB3716000213069F +:1000800006CB21A023A005009105E3EDC5FEB70036 +:100090000002E780C00AB7000002E780C00AB7008C +:1000A0000002E780002373005010F5BF828000003B +:1000B0000000000000000000000000000000000040 +:1000C0006F004000197106C20AC40EC612C816CAD3 +:1000D0001ACC1ECE22D026D22AD42ED632D836DA48 +:1000E0003ADC3EDEC2C0C6C2CAC4CEC6D2C8D6CA78 +:1000F000DACCDECEE2D0E6D2EAD4EED6F2D8F6DA28 +:10010000FADCFEDE970000009380E00482909240CB +:100110002241B2414242D2426243F24302549254DB +:100120002255B2554256D2566257F2570648964863 +:100130002649B649464AD64A664BF64B065C965C5B +:10014000265DB65D465ED65E665FF65F096173004A +:10015000203001A03D432A876373C3029377F700E1 +:10016000BDEFADE5937606FF3D8ABA960CC34CC34E +:100170000CC74CC74107E36BD7FE11E28280B30680 +:10018000C3408A069702000096966780A600230760 +:10019000B700A306B7002306B700A305B7002305E1 +:1001A000B700A304B7002304B700A303B7002303D9 +:1001B000B700A302B7002302B700A301B7002301D1 +:1001C000B700A300B7002300B700828093F5F50FB6 +:1001D00093968500D58D93960501D58D61B793963D +:1001E00027009702000096968682E78086FA96801E +:1001F000C1171D8F3E96E374C3F8A5B75D71130652 +:1002000080028145280886C6B137BD47230CF1001E +:100210008947230EF1003ED2E177938707082C0827 +:100220000A85231AF102EF00B00CB64061618280AA +:10023000557186C7A2C5A6C3CAC152DD56DB5AD9BD +:100240005ED762D54EDF5D3F371508000147814616 +:1002500021469305601113050540A12237150002C0 +:100260001305C5C3A529614681452800E535372416 +:100270000002B70707003715000293870740930570 +:1002800004801305C5C43EC291290545BD2161679F +:100290001307076A8D46130600028145480061690D +:1002A000B129130404808144014C371A0002130958 +:1002B000096A930A0008370B0001B71B0002228667 +:1002C000A68513050AC693990401092993D9090142 +:1002D000CA870147930600022286CE854800412145 +:1002E0001306000881450810B535CE854A87930668 +:1002F000000210104800E521A285814718103E97A2 +:1003000083C60500034707006309D70233866401EB +:100310003E9613850BC8D926094C93840408896737 +:1003200013040408E39DF4F8B7470002054523A031 +:100330008781E1267300501001A085078505E39FA2 +:1003400057FBE1BF0111B717080006CE02C402C671 +:10035000938707406312F502B70705000947D8CF16 +:10036000930700063EC085473EC28A853745080090 +:10037000D921F24005618280B72708009387078062 +:10038000E319F5FEB70705001147D8CF93070030F2 +:100390003EC03745080085473EC28A8513050540A3 +:1003A000C1BF011122CC26CA4AC84EC652C42A84F3 +:1003B00006CE2E89B284368ABA896937BD4701458F +:1003C00063FE27032320040023262401FD575CCC71 +:1003D0002322440193E714002324340113F744003B +:1003E0001CC0A1885C4C054501C713D56701058970 +:1003F00091CC6DD99396A700E3D606FEF2406244F5 +:10040000D2444249B249224A0561828061DDFDB78A +:10041000484D198105898280411122C406C62A846B +:100420002285FD3775DDB240224441018280931759 +:10043000050137150800C183130505401CD5E9BF28 +:10044000411122C406C62A840305040009E5B2400E +:100450002244410182800504D93FFDB75971D6CAB3 +:10046000DAC8DEC6E2C4E6C2EAC086D6A2D4A6D204 +:10047000CAD0CECED2CC6EDEAA872E8D930A0003D0 +:10048000130BA002930BC004130C0002A94C0385AC +:100490000700630605221307500213891700631132 +:1004A000E5080385170063105509038527001389A4 +:1004B000370085446316650B03240D0093074D0038 +:1004C00063550400330480408944030509003E8DD0 +:1004D00005099377F5FD639777010305090093E418 +:1004E00044000509630D051C13070006AA8763561F +:1004F000A700930705FEE207E1871307F00463876F +:10050000E70C6342F70613073004638BE70A13070F +:1005100040046380E70E1307200489466389E70AD5 +:100520003937EA8949A01307D0026306E500138929 +:100530002700814441B70385270013893700894488 +:1005400095BF3304940305092A940305F9FF1304A6 +:1005500004FD930705FD93F7F70FE374F7FE95BFCE +:1005600001442547FDB7130750056384E7081307C7 +:1005700080056382E70813073005E393E7FA832DCC +:100580000D0093094D0013FA24006E85A9252A8DCC +:1005900093041D0063150A00636A8D00268D6E8525 +:1005A0004535636A8D004E8DCA87D5B513050002A7 +:1005B000BD3D268DF1BF13050002953D050DD5B754 +:1005C00003050D0093094D009D35F1BFA14613F7BA +:1005D000440093094D0015C31306400403270D0082 +:1005E0006396C702635407023307E04093E40401B3 +:1005F00031A8A946E9BFC146D9BF13074004639596 +:10060000E70003270D00F9BF03270D00130585F848 +:100610009D4719E193077002100893F5F70F328D8B +:100620008147A5483378D7023A833385050133578C +:10063000D70263E5080113158801618513050503D9 +:100640002300A600938D17006366D3000506639F01 +:100650008D05FD4713F7040101CF13870D02140820 +:10066000B30DD7001307D0022380EDFE938D270032 +:1006700093F714001305000299C3130500036E8A53 +:10068000898813071A0081E463648A023A8AFD1D8F +:100690003307BD0103050700593BE39A0DFEE374E0 +:1006A0008AF0130500026133050AD5BFEE879DBFAE +:1006B0003AC62AC4AD3B324722453A8AD9B7B6502A +:1006C000265496540659F649664AD64A464BB64BC6 +:1006D000264C964C064DF25D6561828039712ED2B2 +:1006E0004C1006CE32D436D63AD83EDA42DC46DE5C +:1006F0002EC6AD33F240216182805DAB1C418E0677 +:1007000003A88700B3E6060193E6060894C71449D8 +:1007100099CA814663DDC600CCC323A007003A8591 +:100720000DE50D458280CCC3814663CCC60094475D +:1007300093E6360094C775D7C8477D17058965FDD0 +:10074000828023A007008506CDB703A8C7007D15CA +:1007500013781800E31608FC8506910565BF032889 +:1007600005000E0783288800336717011367070801 +:100770002324E800184905C301476353D702131522 +:1007800027002322B800329508412320A8003E8587 +:100790000DED0D4582802322B8000147634FD7003D +:1007A00003278800136757002324E800FDD303259F +:1007B000C800FD1705897DF9828093152700B29541 +:1007C0008C4105072320B800D1BF8328C8007D15C0 +:1007D00093F81800E39E08FA0507910579BF1C49B4 +:1007E00089CF814763FFD70003280500BA882322F9 +:1007F000B800639108020D45828008414CC163E452 +:10080000D700014582800C411397270032970CC313 +:100810008507F5B70323C800FD1813731300E31A07 +:1008200003FC83280800139827003298232018011E +:100830008507910545BFB7470800938707402A8879 +:100840006304F508B7570800938707806304F50A27 +:1008500037470800630DE50A05458280331E1F01F6 +:100860003376DE0129C683A345008843139318001D +:1008700033966F001346F6FF13F43300718D331473 +:100880006400418D88C3638B5302638C0302084369 +:1008900003AEC500718D331E6E003365C50108C3FC +:1008A0008842698E884533156500498E90C2850857 +:1008B00033D51E0145F53244410182802326C8010B +:1008C000F9B72324C801E1B7B716050037170500AB +:1008D000B7170500938646C1130707C19387C7C0A2 +:1008E00083AE05008148054F8D4F914233D51E01DF +:1008F00005ED8280B716050037170500B71705000C +:10090000938606C21307C7C1938787C1D1BFB716A5 +:10091000050037170500B7170500938686C0130733 +:1009200047C0938707C06DBF331E1F013376DE01BA +:1009300019E2850865BF411122C635B70547AA8768 +:100940006305E50209476300E506054591EBB70637 +:100950000600DC4A7D771307F73FF98FDCCA0145B3 +:1009600082807D1719EB0D4582809306004037A7E2 +:10097000070013070712B7050500905D7D8E75D23D +:10098000370606005C4A7D771307F73FF98FD58F4E +:1009900041115CCA02C613073006B2476359F7001B +:1009A000014541018280856693860680C9B7B247BA +:1009B00085073EC6DDB791476307F50263EAA700E6 +:1009C0008547630AF50489476309F5040545828074 +:1009D000A147E31DF5FE0947094501A8FD1781EF71 +:1009E000C8D20D45828005470D45B7A707009387FC +:1009F0000712B7060500905E798E6DD28A05C98D03 +:100A00004111CCD202C613073006B247635AF70031 +:100A10000145410182801147C9BF21470145F1B716 +:100A2000B24785073EC6D5B70547AA876305E502E5 +:100A300009476302E506054591EBB70606009C4AA7 +:100A40007D771307F73FF98F9CCA014582807D1798 +:100A500019EB0D4582809306004037A70700130766 +:100A60000712B7050500905D7D8E75D23707060029 +:100A70001C4B7D761306F63FF18FD58F1CCB854737 +:100A80001CCF411102C613073006B2476359F70065 +:100A90000145410182808566938606807DBFB2470D +:100AA00085073EC6DDB711C98547630DF5020545CB +:100AB0008280FD1791EB0D4582800946B7A707009C +:100AC00093870712B7060500985E718F7DD3411199 +:100AD000C8D602C613073006B2476357F700014570 +:100AE000410182800546D9BFB24785073EC6EDB7B2 +:100AF000011126CAB7040600DC4806CE22CC4AC83B +:100B00004EC652C456C2F19BDCC89C482A89C845CF +:100B1000F19B9CC883C7C5012E848A07DCC883C7A4 +:100B2000D5018A079CC8193D0C44AA8A0345440094 +:100B300059351848B70705002A8A98C358480850FD +:100B4000D8C3184C98C7CD35AA894850A93F8347C8 +:100B500004002A8793F6170089E6D44893E6260016 +:100B6000D4C893F6270099E637060600544A93E660 +:100B7000160054CA93F6470099E637060600144A51 +:100B800093E6260014CAA18B99E7B70606009C4A93 +:100B900093E717009CCAF2406244232059012322A4 +:100BA0004901232439012326E900D244B249224ACB +:100BB000924A4A85424905618280B7070500DC53A5 +:100BC000054721658D8BF9176376F7003755E801E6 +:100BD000130505808280937735002A879DEFB786BD +:100BE0007F7F9386F6F7FD5510431107B377D60044 +:100BF000B697D18FD58FE389B7FE8346C7FFB3077A +:100C0000A7408DCA8346D7FF9DC20345E7FF333512 +:100C1000A0003E9579158280F9D283470700050729 +:100C200093763700F5FB098F1305F7FF8280138554 +:100C3000D7FF82801385C7FF828000005354415242 +:100C400054204452495645520A0000004255464637 +:100C50004552203D203078253038780A00000000C9 +:100C6000577269746520506167652030782530348B +:100C7000782066726F6D203078253038780A000051 +:100C8000616464725B3078253034783A307825308E +:100C900038785D206275663A6D656D203D2030784C +:100CA0002530327820213D203078253032780A00F6 +:100CB000411122C406C62A840937B70705009C43A0 +:100CC0001307803E8507B357F502B3D7E7020D67D8 +:100CD0001307779CB387E7022167130707D0B3D7C1 +:100CE000E70219C4BE82FD12E39F02FE7D147DF867 +:100CF000B240224441018280411122C406C62A84A6 +:100D00006D3DB70705009C431307803E8507B35729 +:100D1000F502B3D7E7022947B387E7022167130734 +:100D200007D0B3D7E70219C4BE82FD12E39F02FECB +:100D30007D147DF8B2402244410182800000000011 +:0400000502000000F5 +:00000001FF diff --git a/upload_drivers/jtag_eeprom/include/README b/upload_drivers/jtag_eeprom/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/upload_drivers/jtag_eeprom/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/upload_drivers/jtag_eeprom/lib/README b/upload_drivers/jtag_eeprom/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/upload_drivers/jtag_eeprom/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/upload_drivers/jtag_eeprom/platformio.ini b/upload_drivers/jtag_eeprom/platformio.ini new file mode 100644 index 0000000..c7b09a0 --- /dev/null +++ b/upload_drivers/jtag_eeprom/platformio.ini @@ -0,0 +1,15 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:mik32v2] +platform = MIK32 +board = mik32v2 +framework = framework-mik32v2-sdk +board_debug.ldscript = ram diff --git a/upload_drivers/jtag_eeprom/ram.ld b/upload_drivers/jtag_eeprom/ram.ld new file mode 100644 index 0000000..a20b85d --- /dev/null +++ b/upload_drivers/jtag_eeprom/ram.ld @@ -0,0 +1,19 @@ + +OUTPUT_FORMAT("elf32-littleriscv", "elf32-littleriscv", "elf32-littleriscv") +OUTPUT_ARCH(riscv) + +ENTRY(_start) + + +MEMORY { + ram (RWX): ORIGIN = 0x02000000, LENGTH = 16K +} + +STACK_SIZE = 512; + +CL_SIZE = 16; + +REGION_ALIAS("REGION_TEXT", ram); +REGION_ALIAS("REGION_RAM", ram); + +INCLUDE sections.lds diff --git a/upload_drivers/jtag_eeprom/sections.lds b/upload_drivers/jtag_eeprom/sections.lds new file mode 100644 index 0000000..7b1d14f --- /dev/null +++ b/upload_drivers/jtag_eeprom/sections.lds @@ -0,0 +1,105 @@ + +SECTIONS { + .text ORIGIN(REGION_TEXT) : { + PROVIDE(__TEXT_START__ = .); + *crt0.o(.text .text.*) + *(.text.smallsysteminit) + *(.text.SmallSystemInit) + . = ORIGIN(REGION_TEXT) + 0xC0; + KEEP(*crt0.o(.trap_text)) + + *(.text) + *(.text.*) + *(.rodata) + *(.rodata.*) + . = ALIGN(CL_SIZE); + PROVIDE(__TEXT_END__ = .); + } >REGION_TEXT + + .data : + AT( __TEXT_END__ ) { + PROVIDE(__DATA_START__ = .); + _gp = .; + *(.srodata.cst16) *(.srodata.cst8) *(.srodata.cst4) *(.srodata.cst2) *(.srodata*) + *(.sdata .sdata.* .gnu.linkonce.s.*) + *(.data .data.*) + . = ALIGN(CL_SIZE); + PROVIDE(__DATA_END__ = .); + } >REGION_RAM + + __DATA_IMAGE_START__ = LOADADDR(.data); + __DATA_IMAGE_END__ = LOADADDR(.data) + SIZEOF(.data); + + /* thread-local data segment */ + .tdata : { + PROVIDE(_tls_data = .); + PROVIDE(_tdata_begin = .); + *(.tdata .tdata.*) + PROVIDE(_tdata_end = .); + . = ALIGN(CL_SIZE); + } >REGION_RAM + + .tbss : { + PROVIDE(__BSS_START__ = .); + *(.tbss .tbss.*) + . = ALIGN(CL_SIZE); + PROVIDE(_tbss_end = .); + } >REGION_RAM + + /* bss segment */ + .sbss : { + *(.sbss .sbss.* .gnu.linkonce.sb.*) + *(.scommon) + } >REGION_RAM + + .bss : { + *(.bss .bss.*) + *(COMMON) + . = ALIGN(CL_SIZE); + PROVIDE(__BSS_END__ = .); + } >REGION_RAM + + /* Code intended to be copied to REGION_RAM before execution */ + .ram_text : + AT( ALIGN(__DATA_IMAGE_END__, CL_SIZE) ) { + PROVIDE(__RAM_TEXT_START__ = .); + *(.ram_text) + . = ALIGN(CL_SIZE); + PROVIDE(__RAM_TEXT_END__ = .); + } > REGION_RAM + + __RAM_TEXT_IMAGE_START__ = LOADADDR(.ram_text); + __RAM_TEXT_IMAGE_END__ = LOADADDR(.ram_text) + SIZEOF(.ram_text); + ASSERT(__RAM_TEXT_IMAGE_END__ < ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT), "REGION_TEXT segment overflows") + + ASSERT(__RAM_TEXT_END__ < ORIGIN(REGION_RAM) + LENGTH(REGION_RAM) - STACK_SIZE, "REGION_RAM section overflows") + + _end = .; + PROVIDE(__end = .); + + BUFFER_SIZE = 8K; + + .buffer ORIGIN(REGION_RAM) + 6K : { + FILL(0); + PROVIDE(__BUFFER__END__ = .); + PROVIDE(BUFFER = .); + . += BUFFER_SIZE; + PROVIDE(BUFFER_STATUS = .); + . += 4; + PROVIDE(__BUFFER__END__ = .); + } >REGION_RAM + + /* End of uninitalized data segement */ + + .stack ORIGIN(REGION_RAM) + LENGTH(REGION_RAM) - STACK_SIZE : { + FILL(0); + PROVIDE(__STACK_START__ = .); + . += STACK_SIZE; + PROVIDE(__C_STACK_TOP__ = .); + PROVIDE(__STACK_END__ = .); + } >REGION_RAM + + /DISCARD/ : { + *(.eh_frame .eh_frame.*) + } +} \ No newline at end of file diff --git a/upload_drivers/jtag_eeprom/src/main.c b/upload_drivers/jtag_eeprom/src/main.c new file mode 100644 index 0000000..7943167 --- /dev/null +++ b/upload_drivers/jtag_eeprom/src/main.c @@ -0,0 +1,119 @@ +#include "mik32_hal_pcc.h" +#include "mik32_hal_eeprom.h" + +#include "uart_lib.h" +#include "xprintf.h" + +/** + * @file main.c + * + * @brief Пример демонстрирует чтение и запись значений во внешнюю флеш память Winbond W25 по Standard (Single) SPI + */ + +// extern char __HEAP_START[]; +const int BUFFER_SIZE = 8 * 1024; +extern uint8_t *BUFFER[]; +extern uint32_t *BUFFER_STATUS[]; + +#define EEPROM_OP_TIMEOUT 100000 +#define USART_TIMEOUT 1000 +#define EEPROM_PAGE_WORDS 32 +#define EEPROM_PAGE_COUNT 64 + +void SystemClock_Config(void); +void EEPROM_Init(void); + +HAL_EEPROM_HandleTypeDef heeprom; + +int main() +{ + // *BUFFER_STATUS = 1; + SystemClock_Config(); + + UART_Init(UART_0, 278, UART_CONTROL1_TE_M | UART_CONTROL1_M_8BIT_M, 0, 0); + xprintf("START DRIVER\n"); + + HAL_EEPROM_HandleTypeDef heeprom = { + .Instance = EEPROM_REGS, + + }; + + xprintf("BUFFER = 0x%08x\n", BUFFER); + // xprintf("BUFFER_STATUS = 0x%08x\n", BUFFER_STATUS); + + // *BUFFER_STATUS = 1; + + HAL_DelayMs(1); + + // xprintf("*BUFFER_STATUS 0x%08x\n", *BUFFER_STATUS); + // asm ("wfi"); + + // *BUFFER_STATUS = 1; + + HAL_EEPROM_Erase(&heeprom, 0, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_ALL, EEPROM_OP_TIMEOUT); + + int result = 0; + for (int ad = 0; ad < BUFFER_SIZE; ad += (EEPROM_PAGE_WORDS * 4)) + { + xprintf("Write Page 0x%04x from 0x%08x\n", ad, (uint8_t *)((uint32_t)BUFFER + ad)); + + HAL_EEPROM_Write( + &heeprom, + ad, + (uint32_t *)((uint32_t)BUFFER + ad), + EEPROM_PAGE_WORDS, + HAL_EEPROM_WRITE_SINGLE, + EEPROM_OP_TIMEOUT); + + uint8_t rb[EEPROM_PAGE_WORDS * 4] = {0}; + + HAL_EEPROM_Read(&heeprom, ad, (uint32_t *)rb, EEPROM_PAGE_WORDS, EEPROM_OP_TIMEOUT); + + for (uint32_t b = 0; b < (EEPROM_PAGE_WORDS * 4); b++) + { + uint8_t ebuf = *(uint8_t *)((uint32_t)BUFFER + ad + b); + if (ebuf != rb[b]) + { + xprintf("addr[0x%04x:0x%08x] buf:mem = 0x%02x != 0x%02x\n", (uint32_t)BUFFER + ad + b, 0x01000000 + ad + b, ebuf, rb[b]); + result = 2; + break; + } + } + } + + *BUFFER_STATUS = result; + HAL_DelayMs(1); + // asm ("wfi"); + + while (1) + ; +} + +void SystemClock_Config(void) +{ + PCC_InitTypeDef PCC_OscInit = {0}; + + PCC_OscInit.OscillatorEnable = PCC_OSCILLATORTYPE_ALL; + PCC_OscInit.FreqMon.OscillatorSystem = PCC_OSCILLATORTYPE_OSC32M; + PCC_OscInit.FreqMon.ForceOscSys = PCC_FORCE_OSC_SYS_UNFIXED; + PCC_OscInit.FreqMon.Force32KClk = PCC_FREQ_MONITOR_SOURCE_OSC32K; + PCC_OscInit.AHBDivider = 0; + PCC_OscInit.APBMDivider = 0; + PCC_OscInit.APBPDivider = 0; + PCC_OscInit.HSI32MCalibrationValue = 128; + PCC_OscInit.LSI32KCalibrationValue = 128; + PCC_OscInit.RTCClockSelection = PCC_RTC_CLOCK_SOURCE_AUTO; + PCC_OscInit.RTCClockCPUSelection = PCC_CPU_RTC_CLOCK_SOURCE_OSC32K; + HAL_PCC_Config(&PCC_OscInit); +} + +void EEPROM_Init() +{ + heeprom.Instance = EEPROM_REGS; + heeprom.Mode = HAL_EEPROM_MODE_TWO_STAGE; + heeprom.ErrorCorrection = HAL_EEPROM_ECC_ENABLE; + heeprom.EnableInterrupt = HAL_EEPROM_SERR_DISABLE; + + HAL_EEPROM_Init(&heeprom); + HAL_EEPROM_CalculateTimings(&heeprom, OSC_SYSTEM_VALUE); +} diff --git a/upload_drivers/jtag_eeprom/test/README b/upload_drivers/jtag_eeprom/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/upload_drivers/jtag_eeprom/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html