diff --git a/Target/Demo/ARMCM3_EFM32_Olimex_EM32G880F128STK_IAR/Boot/ide/efm32G880.ewd b/Target/Demo/ARMCM3_EFM32_Olimex_EM32G880F128STK_IAR/Boot/ide/efm32G880.ewd index 1660cac8..dd860269 100644 --- a/Target/Demo/ARMCM3_EFM32_Olimex_EM32G880F128STK_IAR/Boot/ide/efm32G880.ewd +++ b/Target/Demo/ARMCM3_EFM32_Olimex_EM32G880F128STK_IAR/Boot/ide/efm32G880.ewd @@ -1,6 +1,6 @@ - 3 + 4 Debug @@ -11,7 +11,7 @@ C-SPY 2 - 31 + 33 1 1 + + + + + + + + + @@ -453,6 +489,39 @@ + + E2_ID + 2 + + 0 + 1 + 1 + + + + + + + + GDBSERVER_ID 2 @@ -494,7 +563,7 @@ IJET_ID 2 - 8 + 9 1 1 + + + + + + + + + + + + + @@ -924,7 +1045,7 @@ LMIFTDI_ID 2 - 2 + 3 1 1 + + + @@ -1003,7 +1137,7 @@ STLINK_ID 2 - 6 + 8 1 1 + + @@ -1223,7 +1365,7 @@ XDS100_ID 2 - 8 + 9 1 1 + @@ -1416,10 +1562,6 @@ $TOOLKIT_DIR$\plugins\rtos\FreeRtos\FreeRtosArmPlugin.ENU.ewplugin 0 - - $TOOLKIT_DIR$\plugins\rtos\HWRTOSplugin\HWRTOSplugin.ewplugin - 0 - $TOOLKIT_DIR$\plugins\rtos\Mbed\MbedArmPlugin.ENU.ewplugin 0 @@ -1432,30 +1574,18 @@ $TOOLKIT_DIR$\plugins\rtos\OpenRTOS\OpenRTOSPlugin.ewplugin 0 - - $TOOLKIT_DIR$\plugins\rtos\RemedyRtosViewer\RemedyRtosViewer.ewplugin - 0 - $TOOLKIT_DIR$\plugins\rtos\SafeRTOS\SafeRTOSPlugin.ewplugin 0 - $TOOLKIT_DIR$\plugins\rtos\SMX\smxAwareIarArm8.ewplugin - 0 - - - $TOOLKIT_DIR$\plugins\rtos\SMX\smxAwareIarArm8BE.ewplugin + $TOOLKIT_DIR$\plugins\rtos\SMX\smxAwareIarArm9a.ewplugin 0 $TOOLKIT_DIR$\plugins\rtos\ThreadX\ThreadXArmPlugin.ENU.ewplugin 0 - - $TOOLKIT_DIR$\plugins\rtos\TI-RTOS\tirtosplugin.ewplugin - 0 - $TOOLKIT_DIR$\plugins\rtos\uCOS-II\uCOS-II-286-KA-CSpy.ewplugin 0 @@ -1492,7 +1622,7 @@ C-SPY 2 - 31 + 33 1 0 + + + + + + + + + @@ -1934,6 +2100,39 @@ + + E2_ID + 2 + + 0 + 1 + 0 + + + + + + + + GDBSERVER_ID 2 @@ -1975,7 +2174,7 @@ IJET_ID 2 - 8 + 9 1 0 + + + + + + + + + + + + + @@ -2405,7 +2656,7 @@ LMIFTDI_ID 2 - 2 + 3 1 0 + + + @@ -2484,7 +2748,7 @@ STLINK_ID 2 - 6 + 8 1 0 + + @@ -2704,7 +2976,7 @@ XDS100_ID 2 - 8 + 9 1 0 + @@ -2897,10 +3173,6 @@ $TOOLKIT_DIR$\plugins\rtos\FreeRtos\FreeRtosArmPlugin.ENU.ewplugin 0 - - $TOOLKIT_DIR$\plugins\rtos\HWRTOSplugin\HWRTOSplugin.ewplugin - 0 - $TOOLKIT_DIR$\plugins\rtos\Mbed\MbedArmPlugin.ENU.ewplugin 0 @@ -2913,30 +3185,18 @@ $TOOLKIT_DIR$\plugins\rtos\OpenRTOS\OpenRTOSPlugin.ewplugin 0 - - $TOOLKIT_DIR$\plugins\rtos\RemedyRtosViewer\RemedyRtosViewer.ewplugin - 0 - $TOOLKIT_DIR$\plugins\rtos\SafeRTOS\SafeRTOSPlugin.ewplugin 0 - $TOOLKIT_DIR$\plugins\rtos\SMX\smxAwareIarArm8.ewplugin - 0 - - - $TOOLKIT_DIR$\plugins\rtos\SMX\smxAwareIarArm8BE.ewplugin + $TOOLKIT_DIR$\plugins\rtos\SMX\smxAwareIarArm9a.ewplugin 0 $TOOLKIT_DIR$\plugins\rtos\ThreadX\ThreadXArmPlugin.ENU.ewplugin 0 - - $TOOLKIT_DIR$\plugins\rtos\TI-RTOS\tirtosplugin.ewplugin - 0 - $TOOLKIT_DIR$\plugins\rtos\uCOS-II\uCOS-II-286-KA-CSpy.ewplugin 0 diff --git a/Target/Demo/ARMCM3_EFM32_Olimex_EM32G880F128STK_IAR/Boot/ide/efm32G880.ewp b/Target/Demo/ARMCM3_EFM32_Olimex_EM32G880F128STK_IAR/Boot/ide/efm32G880.ewp index 59d296a1..865c4184 100644 --- a/Target/Demo/ARMCM3_EFM32_Olimex_EM32G880F128STK_IAR/Boot/ide/efm32G880.ewp +++ b/Target/Demo/ARMCM3_EFM32_Olimex_EM32G880F128STK_IAR/Boot/ide/efm32G880.ewp @@ -1,6 +1,6 @@ - 3 + 4 Debug @@ -11,9 +11,13 @@ General 3 - 31 + 36 1 1 + - - - - - + + + + + + ICCARM 2 - 36 + 38 1 1 - - - + + AARM 2 - 10 + 12 1 1 + + @@ -680,26 +689,14 @@ 0 - - - - BICOMP - 0 - - - - BUILDACTION - 1 - - - + inputOutputBased ILINK 0 - 23 + 27 1 1 - + + + + + + + + + @@ -1059,8 +1088,8 @@ - BILINK - 0 + BUILDACTION + 2 @@ -1074,9 +1103,13 @@ General 3 - 31 + 36 1 0 + - - - - - + + + + + + ICCARM 2 - 36 + 38 1 0 - - - + + AARM 2 - 10 + 12 1 0 + + @@ -1735,26 +1773,14 @@ 0 - - - - BICOMP - 0 - - - - BUILDACTION - 1 - - - + inputOutputBased ILINK 0 - 23 + 27 1 0 - + + + + + + + + + @@ -2114,8 +2172,8 @@ - BILINK - 0 + BUILDACTION + 2 @@ -2372,6 +2430,9 @@ $PROJ_DIR$\..\..\..\..\Source\ARMCM3_EFM32\flash.h + + $PROJ_DIR$\..\..\..\..\Source\ARMCM3_EFM32\mbrtu.c + $PROJ_DIR$\..\..\..\..\Source\ARMCM3_EFM32\nvm.c @@ -2418,6 +2479,12 @@ $PROJ_DIR$\..\..\..\..\Source\cpu.h + + $PROJ_DIR$\..\..\..\..\Source\mb.c + + + $PROJ_DIR$\..\..\..\..\Source\mb.h + $PROJ_DIR$\..\..\..\..\Source\nvm.h diff --git a/Target/Demo/ARMCM3_EFM32_Olimex_EM32G880F128STK_IAR/Boot/ide/settings/efm32G880.wsdt b/Target/Demo/ARMCM3_EFM32_Olimex_EM32G880F128STK_IAR/Boot/ide/settings/efm32G880.wsdt index 6ffdcdab..2f0887d8 100644 --- a/Target/Demo/ARMCM3_EFM32_Olimex_EM32G880F128STK_IAR/Boot/ide/settings/efm32G880.wsdt +++ b/Target/Demo/ARMCM3_EFM32_Olimex_EM32G880F128STK_IAR/Boot/ide/settings/efm32G880.wsdt @@ -261,7 +261,7 @@ 14 - 29 + 36 1 @@ -275,12 +275,12 @@ 08000D8400000F84000008840000FFFFFFFF54840000328100001C81000009840000 - 0600048400004C000000068400004E0000000E8400005100000030840000530000000B810000220000000D81000024000000 + 0600048400004D000000068400004F00000030840000530000000E840000510000000B8100001E0000000D81000020000000 0 0A0000000A0000006E0000006E000000 - 00000000F00300008007000003040000 + 00000000F20300008007000003040000 4096 0 0 @@ -307,7 +307,7 @@ 4294967295 00000000740300008007000025040000 - 000000003F03000080070000F0030000 + 000000004103000080070000F2030000 4096 0 0 @@ -320,7 +320,7 @@ 34052 000000003500000022010000E6000000 - 04000000570300007C070000D6030000 + 04000000590300007C070000D8030000 32768 0 0 @@ -343,20 +343,20 @@ _I0 - 610 + 609 40 1220 20 2 - C:\Work\software\OpenBLT\Target\Demo\ARMCM3_EFM32_Olimex_EM32G880F128STK_IAR\Boot\ide\BuildLog.log + $WS_DIR$\BuildLog.log 0 -1 34048 000000003500000022010000E6000000 - 04000000570300007C070000D6030000 + 04000000590300007C070000D8030000 4096 0 0 @@ -369,7 +369,7 @@ 34056 000000003500000022010000E6000000 - 04000000570300007C070000D6030000 + 04000000590300007C070000D8030000 4096 0 0 @@ -383,7 +383,7 @@ 34057 000000003500000022010000E6000000 - 04000000570300007C070000D6030000 + 04000000590300007C070000D8030000 4096 0 0 @@ -397,7 +397,7 @@ 34058 000000003500000022010000E6000000 - 04000000570300007C070000D6030000 + 04000000590300007C070000D8030000 4096 0 0 @@ -411,7 +411,7 @@ 34059 000000003500000022010000E6000000 - 04000000570300007C070000D6030000 + 04000000590300007C070000D8030000 4096 0 0 @@ -425,7 +425,7 @@ 34062 000000003500000022010000E6000000 - 04000000570300007C070000D6030000 + 04000000590300007C070000D8030000 4096 0 0 @@ -509,7 +509,7 @@ 34063 00000000350000000601000096010000 - 0000000032000000060100003B030000 + 0000000032000000060100003D030000 4096 0 0 @@ -520,11 +520,11 @@ 1 - 0000000010000000000000000010000001000000FFFFFFFFFFFFFFFF06010000320000000A0100003B0300000100000002000010040000000100000000000000000000000F85000000000000000000000000000000000000010000000F850000010000000F850000000000000080000000000000FFFFFFFFFFFFFFFF000000000000000004000000040000000000000001000000040000000100000000000000000000000D85000000000000000000000000000000000000010000000D850000010000000D850000000000000080000000000000FFFFFFFFFFFFFFFF000000000000000004000000040000000000000001000000040000000100000000000000000000000C85000000000000000000000000000000000000010000000C850000010000000C850000000000000080000000000000FFFFFFFFFFFFFFFF00000000000000000400000004000000000000000100000004000000010000000000000000000000078500000000000000000000000000000000000001000000078500000100000007850000000000000080000000000000FFFFFFFFFFFFFFFF00000000000000000400000004000000000000000100000004000000010000000000000000000000068500000000000000000000000000000000000001000000068500000100000006850000000000000080000000000000FFFFFFFFFFFFFFFF00000000000000000400000004000000000000000100000004000000010000000000000000000000058500000000000000000000000000000000000001000000058500000100000005850000000000000080000001000000FFFFFFFFFFFFFFFF000000003B030000800700003F030000010000000100001004000000010000000000000000000000FFFFFFFF07000000048500000085000008850000098500000A8500000B8500000E850000FFFF02000B004354616262656450616E65008000000100000000000000740300008007000025040000000000003F03000080070000F0030000000000004080005607000000FFFEFF054200750069006C006400010000000485000001000000FFFFFFFFFFFFFFFFFFFEFF094400650062007500670020004C006F006700010000000085000001000000FFFFFFFFFFFFFFFFFFFEFF0C4400650063006C00610072006100740069006F006E007300000000000885000001000000FFFFFFFFFFFFFFFFFFFEFF0A5200650066006500720065006E00630065007300000000000985000001000000FFFFFFFFFFFFFFFFFFFEFF0D460069006E006400200069006E002000460069006C0065007300000000000A85000001000000FFFFFFFFFFFFFFFFFFFEFF1541006D0062006900670075006F0075007300200044006500660069006E006900740069006F006E007300000000000B85000001000000FFFFFFFFFFFFFFFFFFFEFF0B54006F006F006C0020004F0075007400700075007400000000000E85000001000000FFFFFFFFFFFFFFFF00000000000000000000000000000000000000000000000001000000FFFFFFFF0485000001000000FFFFFFFF04850000000000000080000000000000FFFFFFFFFFFFFFFF00000000000000000400000004000000000000000100000004000000010000000000000000000000038500000000000000000000000000000000000001000000038500000100000003850000000000000000000000000000 + 0000000010000000000000000010000001000000FFFFFFFFFFFFFFFF06010000320000000A0100003D0300000100000002000010040000000100000000000000000000000F85000000000000000000000000000000000000010000000F850000010000000F850000000000000080000000000000FFFFFFFFFFFFFFFF000000000000000004000000040000000000000001000000040000000100000000000000000000000D85000000000000000000000000000000000000010000000D850000010000000D850000000000000080000000000000FFFFFFFFFFFFFFFF000000000000000004000000040000000000000001000000040000000100000000000000000000000C85000000000000000000000000000000000000010000000C850000010000000C850000000000000080000000000000FFFFFFFFFFFFFFFF00000000000000000400000004000000000000000100000004000000010000000000000000000000078500000000000000000000000000000000000001000000078500000100000007850000000000000080000000000000FFFFFFFFFFFFFFFF00000000000000000400000004000000000000000100000004000000010000000000000000000000068500000000000000000000000000000000000001000000068500000100000006850000000000000080000000000000FFFFFFFFFFFFFFFF00000000000000000400000004000000000000000100000004000000010000000000000000000000058500000000000000000000000000000000000001000000058500000100000005850000000000000080000001000000FFFFFFFFFFFFFFFF000000003D0300008007000041030000010000000100001004000000010000000000000000000000FFFFFFFF07000000048500000085000008850000098500000A8500000B8500000E850000FFFF02000B004354616262656450616E65008000000100000000000000740300008007000025040000000000004103000080070000F2030000000000004080005607000000FFFEFF054200750069006C006400010000000485000001000000FFFFFFFFFFFFFFFFFFFEFF094400650062007500670020004C006F006700010000000085000001000000FFFFFFFFFFFFFFFFFFFEFF0C4400650063006C00610072006100740069006F006E007300000000000885000001000000FFFFFFFFFFFFFFFFFFFEFF0A5200650066006500720065006E00630065007300000000000985000001000000FFFFFFFFFFFFFFFFFFFEFF0D460069006E006400200069006E002000460069006C0065007300000000000A85000001000000FFFFFFFFFFFFFFFFFFFEFF1541006D0062006900670075006F0075007300200044006500660069006E006900740069006F006E007300000000000B85000001000000FFFFFFFFFFFFFFFFFFFEFF0B54006F006F006C0020004F0075007400700075007400000000000E85000001000000FFFFFFFFFFFFFFFF00000000000000000000000000000000000000000000000001000000FFFFFFFF0485000001000000FFFFFFFF04850000000000000080000000000000FFFFFFFFFFFFFFFF00000000000000000400000004000000000000000100000004000000010000000000000000000000038500000000000000000000000000000000000001000000038500000100000003850000000000000000000000000000 CMSIS-Pack - 00200000010000000100FFFF01001100434D4643546F6F6C426172427574746F6ED1840000020000000A000000FFFEFF00000000000000000000000000010000000100000000000000FFFEFF0A43004D005300490053002D005000610063006B0018000000 + 00200000010000000100FFFF01001100434D4643546F6F6C426172427574746F6ED18400000200000008000000FFFEFF00000000000000000000000000010000000100000000000000FFFEFF0A43004D005300490053002D005000610063006B0018000000 34049 @@ -595,7 +595,7 @@ Main - 00200000010000002000FFFF01001100434D4643546F6F6C426172427574746F6E00E100000000000036000000FFFEFF000000000000000000000000000100000001000000018001E100000000000037000000FFFEFF000000000000000000000000000100000001000000018003E100000000000039000000FFFEFF000000000000000000000000000100000001000000018000810000000000001D000000FFFEFF00000000000000000000000000010000000100000001800000000001000000FFFFFFFFFFFEFF000000000000000000000000000100000001000000018007E10000000000003C000000FFFEFF00000000000000000000000000010000000100000001800000000001000000FFFFFFFFFFFEFF000000000000000000000000000100000001000000018023E10000000004003E000000FFFEFF000000000000000000000000000100000001000000018022E10000000004003D000000FFFEFF000000000000000000000000000100000001000000018025E100000000000040000000FFFEFF00000000000000000000000000010000000100000001800000000001000000FFFFFFFFFFFEFF00000000000000000000000000010000000100000001802BE100000000040043000000FFFEFF00000000000000000000000000010000000100000001802CE100000000040044000000FFFEFF00000000000000000000000000010000000100000001800000000001000000FFFFFFFFFFFEFF000000000000000000000000000100000001000000FFFF01000D005061737465436F6D626F426F784281000000000000FFFFFFFFFFFEFF0001000000000000000100000000000000010000007800000002002050FFFFFFFFFFFEFF0096000000000000000000018021810000000004002F000000FFFEFF000000000000000000000000000100000001000000018024E10000000000003F000000FFFEFF000000000000000000000000000100000001000000018028E100000000040041000000FFFEFF000000000000000000000000000100000001000000018029E100000000000042000000FFFEFF000000000000000000000000000100000001000000018002810000000000001E000000FFFEFF0000000000000000000000000001000000010000000180298100000000000033000000FFFEFF0000000000000000000000000001000000010000000180278100000000000031000000FFFEFF0000000000000000000000000001000000010000000180288100000000000032000000FFFEFF00000000000000000000000000010000000100000001801D810000000004002B000000FFFEFF00000000000000000000000000010000000100000001801E810000000004002C000000FFFEFF00000000000000000000000000010000000100000001800000000001000000FFFFFFFFFFFEFF00000000000000000000000000010000000100000001800B8100000200000022000000FFFEFF00000000000000000000000000010000000100000001800C8100000200000023000000FFFEFF00000000000000000000000000010000000100000001805F8600000000000035000000FFFEFF00000000000000000000000000010000000100000001800000000001000000FFFFFFFFFFFEFF00000000000000000000000000010000000100000001800E8100000200000025000000FFFEFF00000000000000000000000000010000000100000001800F8100000200000026000000FFFEFF00000000000000000000000000010000000100000000000000FFFEFF044D00610069006E00E8020000 + 00200000010000002000FFFF01001100434D4643546F6F6C426172427574746F6E00E100000000000039000000FFFEFF000000000000000000000000000100000001000000018001E10000000000003A000000FFFEFF000000000000000000000000000100000001000000018003E10000000004003C000000FFFEFF0000000000000000000000000001000000010000000180008100000000000019000000FFFEFF00000000000000000000000000010000000100000001800000000001000000FFFFFFFFFFFEFF000000000000000000000000000100000001000000018007E10000000004003F000000FFFEFF00000000000000000000000000010000000100000001800000000001000000FFFFFFFFFFFEFF000000000000000000000000000100000001000000018023E100000000000041000000FFFEFF000000000000000000000000000100000001000000018022E100000000040040000000FFFEFF000000000000000000000000000100000001000000018025E100000000000042000000FFFEFF00000000000000000000000000010000000100000001800000000001000000FFFFFFFFFFFEFF00000000000000000000000000010000000100000001802BE100000000040043000000FFFEFF00000000000000000000000000010000000100000001802CE100000000040044000000FFFEFF00000000000000000000000000010000000100000001800000000001000000FFFFFFFFFFFEFF000000000000000000000000000100000001000000FFFF01000D005061737465436F6D626F426F784281000000000400FFFFFFFFFFFEFF0000000000000000000100000000000000010000007800000002002050FFFFFFFFFFFEFF0096000000000000000000018021810000000004002B000000FFFEFF000000000000000000000000000100000001000000018024E100000000040034000000FFFEFF000000000000000000000000000100000001000000018028E100000000040033000000FFFEFF000000000000000000000000000100000001000000018029E100000000040035000000FFFEFF000000000000000000000000000100000001000000018002810000000004001A000000FFFEFF000000000000000000000000000100000001000000018029810000000004002F000000FFFEFF000000000000000000000000000100000001000000018027810000000004002D000000FFFEFF000000000000000000000000000100000001000000018028810000000004002E000000FFFEFF00000000000000000000000000010000000100000001801D8100000000040027000000FFFEFF00000000000000000000000000010000000100000001801E8100000000040028000000FFFEFF00000000000000000000000000010000000100000001800000000001000000FFFFFFFFFFFEFF00000000000000000000000000010000000100000001800B810000020004001E000000FFFEFF00000000000000000000000000010000000100000001800C810000020000001F000000FFFEFF00000000000000000000000000010000000100000001805F8600000200040032000000FFFEFF00000000000000000000000000010000000100000001800000000001000000FFFFFFFFFFFEFF00000000000000000000000000010000000100000001800E8100000200000021000000FFFEFF00000000000000000000000000010000000100000001800F8100000200000022000000FFFEFF00000000000000000000000000010000000100000000000000FFFEFF044D00610069006E00E8020000 34064 @@ -632,7 +632,7 @@ - 01000000030000000100000000000000000000000100000001000000FFFFFFFF00000000010000000100000000000000280000002800000001000000010000000000000001000000FFFEFF3B43003A005C0057006F0072006B005C0073006F006600740077006100720065005C004F00700065006E0042004C0054005C005400610072006700650074005C0053006F0075007200630065005C00410052004D0043004D0033005F00450046004D00330032005C00720073003200330032002E00630001000000FFFF010014004966436F6E74656E7453746F72616765496D706CFFFEFF00FFFEFFFF28013C003F0078006D006C002000760065007200730069006F006E003D00220031002E0030002200200065006E0063006F00640069006E0067003D0022005500540046002D00380022003F003E000A003C0052006F006F0074003E000A0020002000200020003C004E0075006D0052006F00770073003E0031003C002F004E0075006D0052006F00770073003E000A0020002000200020003C004E0075006D0043006F006C0073003E0031003C002F004E0075006D0043006F006C0073003E000A0020002000200020003C00580050006F0073003E0030003C002F00580050006F0073003E000A0020002000200020003C00590050006F0073003E0030003C002F00590050006F0073003E000A0020002000200020003C00530065006C00530074006100720074003E0030003C002F00530065006C00530074006100720074003E000A0020002000200020003C00530065006C0045006E0064003E0030003C002F00530065006C0045006E0064003E000A0020002000200020003C00580050006F00730032003E0030003C002F00580050006F00730032003E000A0020002000200020003C00590050006F00730032003E003200310031003C002F00590050006F00730032003E000A0020002000200020003C00530065006C005300740061007200740032003E0039003100300035003C002F00530065006C005300740061007200740032003E000A0020002000200020003C00530065006C0045006E00640032003E0039003100300035003C002F00530065006C0045006E00640032003E000A003C002F0052006F006F0074003E000A00FFFEFF07720073003200330032002E00630000000000FFFFFFFFFFFFFFFF0000000010000000C5D4F200FFDC7800BECEA100F0A0A100BCA8E1009CC1B600F7B88600D9ADC200A5C2D700B3A6BE00EAD6A300F6FA7D00B5E99D005FC3CF00C1838300CACAD5000100000001000000020000000A010000670000008007000070030000 + 01000000030000000100000000000000000000000100000001000000FFFFFFFF00000000010000000100000000000000280000002800000000000000 diff --git a/Target/Source/ARMCM3_EFM32/mbrtu.c b/Target/Source/ARMCM3_EFM32/mbrtu.c new file mode 100644 index 00000000..926f053d --- /dev/null +++ b/Target/Source/ARMCM3_EFM32/mbrtu.c @@ -0,0 +1,517 @@ +/************************************************************************************//** +* \file Source/ARMCM3_EFM32/mbrtu.c +* \brief Bootloader Modbus RTU communication interface source file. +* \ingroup Target_ARMCM3_EFM32 +* \internal +*---------------------------------------------------------------------------------------- +* C O P Y R I G H T +*---------------------------------------------------------------------------------------- +* Copyright (c) 2023 by Feaser http://www.feaser.com All rights reserved +* +*---------------------------------------------------------------------------------------- +* L I C E N S E +*---------------------------------------------------------------------------------------- +* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or +* modify it under the terms of the GNU General Public License as published by the Free +* Software Foundation, either version 3 of the License, or (at your option) any later +* version. +* +* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; +* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +* PURPOSE. See the GNU General Public License for more details. +* +* You have received a copy of the GNU General Public License along with OpenBLT. It +* should be located in ".\Doc\license.html". If not, contact Feaser to obtain a copy. +* +* \endinternal +****************************************************************************************/ + +/**************************************************************************************** +* Include files +****************************************************************************************/ +#include "boot.h" /* bootloader generic header */ +#if (BOOT_COM_MBRTU_ENABLE > 0) +#include "efm32.h" +#include "efm32_cmu.h" +#include "efm32_gpio.h" +#include "efm32_leuart.h" + + +/**************************************************************************************** +* Macro definitions +****************************************************************************************/ +/** \brief Timeout for transmitting a byte in milliseconds. */ +#define MBRTU_BYTE_TX_TIMEOUT_MS (10u) + + +/**************************************************************************************** +* Local data declarations +****************************************************************************************/ +/** \brief Stores the number of free running counter ticks that represents the 3.5 + * character delay time (T3_5) for Modbus RTU. + */ +static blt_int16u mbRtuT3_5Ticks; + + +/**************************************************************************************** +* Function prototypes +****************************************************************************************/ +static blt_bool MbRtuReceiveByte(blt_int8u *data); +static void MbRtuTransmitByte(blt_int8u data, blt_bool end_of_packet); + + +/************************************************************************************//** +** \brief Initializes the Modbus RTU communication interface. +** \attention It is the application's responsibility to initialize a timer peripheral +** to have an upwards counting free running counter, which runs at 100 kHz. +** \return none. +** +****************************************************************************************/ +void MbRtuInit(void) +{ + blt_int16u startTimeTicks; + blt_int16u deltaTimeTicks; + blt_int16u currentTimeTicks; + blt_int8u rxDummy; + LEUART_Init_TypeDef init = LEUART_INIT_DEFAULT; + + /* currently, only LEUART1 is supported */ + ASSERT_CT(BOOT_COM_MBRTU_CHANNEL_INDEX == 1); + /* max baudrate for LEUART is 9600 bps */ + ASSERT_CT(BOOT_COM_MBRTU_BAUDRATE <= 9600); + + /* calculate the 3.5 character delay time in free running counter ticks. note that + * the free running counter runs at 100 kHz, so one tick is 10 us. For baudrates > + * 19200 bps, it can be fixed to 1750 us. + */ + if (BOOT_COM_MBRTU_BAUDRATE > 19200) + { + /* set T3_5 time to a fixed value of 1750 us. */ + mbRtuT3_5Ticks = 175; + } + /* calculate the T3_5 time, because the baudrate is <= 19200 bps. */ + else + { + /* T3_5 [us * 10] = 3.5 * Tchar = 3.5 * 11 * 100000 / baudrate = 3850000 / baudrate. + * make sure to do integer round up though. Make sure to add 1 to adjust for 10us + * timer resolution inaccuracy. + */ + mbRtuT3_5Ticks = (blt_int16u)(((3850000UL + (BOOT_COM_MBRTU_BAUDRATE - 1U)) / + BOOT_COM_MBRTU_BAUDRATE) + 1); + } + + /* configure GPIO pins */ + CMU_ClockEnable(cmuClock_GPIO, true); + /* to avoid false start, configure output as high */ + GPIO_PinModeSet(gpioPortC, 6, gpioModePushPull, 1); + GPIO_PinModeSet(gpioPortC, 7, gpioModeInput, 0); + /* enable CORE LE clock in order to access LE modules */ + CMU_ClockEnable(cmuClock_CORELE, true); + /* select LFXO for LEUARTs (and wait for it to stabilize) */ + CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFXO); + /* do not prescale clock */ + CMU_ClockDivSet(cmuClock_LEUART1, cmuClkDiv_1); + /* enable LEUART1 clock */ + CMU_ClockEnable(cmuClock_LEUART1, true); + /* configure LEUART */ + init.enable = leuartDisable; + /* set stop bits. */ + #if (BOOT_COM_MBRTU_STOPBITS == 2) + init.stopbits = leuartStopbits2; + #endif + /* set odd parity. */ + #if (BOOT_COM_MBRTU_PARITY == 1) + init.parity = leuartOddParity; + #endif + /* set even parity. */ + #if (BOOT_COM_MBRTU_PARITY == 2) + init.parity = leuartEvenParity; + #endif + LEUART_Init(LEUART1, &init); + LEUART_BaudrateSet(LEUART1, 0, BOOT_COM_RS232_BAUDRATE); + /* enable pins at default location */ + LEUART1->ROUTE = LEUART_ROUTE_RXPEN | LEUART_ROUTE_TXPEN; + /* clear previous RX interrupts */ + LEUART_IntClear(LEUART1, LEUART_IF_RXDATAV); + /* finally enable it */ + LEUART_Enable(LEUART1, leuartEnable); + + /* enable the receiver output to be able to receive. */ + MbRtuDriverOutputControlHook(BLT_FALSE); + + /* wait for idle line detection. This is T3_5 time after reception of the last byte. */ + startTimeTicks = MbRtuFreeRunningCounterGet(); + do + { + /* service the watchdog. */ + CopService(); + /* get the current value of the free running counter. */ + currentTimeTicks = MbRtuFreeRunningCounterGet(); + /* check if a byte was received while waiting for the idle line. */ + if (MbRtuReceiveByte(&rxDummy) == BLT_TRUE) + { + /* restart the idle line detection. */ + startTimeTicks = currentTimeTicks; + } + /* calculate the number of ticks that elapsed since the start or since the last + * byte reception. Note that this calculation works, even if the free running counter + * overflowed. + */ + deltaTimeTicks = currentTimeTicks - startTimeTicks; + } + while (deltaTimeTicks < mbRtuT3_5Ticks); +} /*** end of MbRtuInit ***/ + + +/************************************************************************************//** +** \brief Transmits a packet formatted for the communication interface. +** \param data Pointer to byte array with data that it to be transmitted. +** \param len Number of bytes that are to be transmitted. +** \return none. +** +****************************************************************************************/ +void MbRtuTransmitPacket(blt_int8u *data, blt_int8u len) +{ + blt_int16u data_index; + blt_int16u checksum; + blt_bool endOfPacket = BLT_FALSE; + /* Made static to lower stack load and +5 for Modbus RTU packet overhead. */ + static blt_int8u txPacket[BOOT_COM_MBRTU_TX_MAX_DATA + 5]; + + /* On Modbus RTU, there must always be a T3_5 time separation between packet trans- + * missions. + * + * This bootloader uses XCP packets embedded in Modbus RTU packets. The XCP + * communication is always request / response based. That means that this packet is + * a response packet and it will only be sent, after the reception of a request packet. + * + * A response packet is only deemed valid, after the T3_5 idle time. This module + * implements the T3_5 end-of-packet time event detection. Consequently, it is already + * guaranteed that there is T3_5 between subsequent packet transmissions. As such, no + * further T3_5 wait time is needed here. + */ + + /* verify validity of the len-parameter */ + ASSERT_RT(len <= BOOT_COM_MBRTU_TX_MAX_DATA); + + /* construct the Modbus RTU packet. start by adding the slave address. */ + txPacket[0] = BOOT_COM_MBRTU_NODE_ID; + /* add the user-defined function code for embedding XCP packets. */ + txPacket[1] = BOOT_COM_MBRTU_FCT_CODE_USER_XCP; + /* add the XCP packet length. */ + txPacket[2] = len; + + /* copy the XCP packet data. */ + CpuMemCopy((blt_int32u)&txPacket[3], (blt_int32u)data, len); + /* calculate the checksum for the packet, including slave address, function code and + * extra XCP length. + */ + checksum = MbRtuCrcCalculate(&txPacket[0], len + 3); + /* add the checksum at the end of the packet */ + txPacket[len + 3] = (blt_int8u)(checksum & 0xff); + txPacket[len + 4] = (blt_int8u)(checksum >> 8); + + /* enable the driver output to be able to send. just make sure to wait a little around + * the togglng of the DE/NRE pin. + */ + MbRtuDelay(BOOT_COM_MBRTU_DRIVER_OUTPUT_ENABLE_DELAY_US); + MbRtuDriverOutputControlHook(BLT_TRUE); + MbRtuDelay(BOOT_COM_MBRTU_DRIVER_OUTPUT_ENABLE_DELAY_US); + + /* transmit all the packet bytes one-by-one */ + for (data_index = 0; data_index < (len + 5); data_index++) + { + /* keep the watchdog happy */ + CopService(); + /* last byte of the packet? */ + if (data_index == ((len + 5) - 1)) + { + /* update the end of packet flag. */ + endOfPacket = BLT_TRUE; + } + /* write byte */ + MbRtuTransmitByte(txPacket[data_index], endOfPacket); + } + /* enable the receiver output to be able to receive again. just make sure to wait a + * little around the togglng of the DE/NRE pin. + */ + MbRtuDelay(BOOT_COM_MBRTU_DRIVER_OUTPUT_DISABLE_DELAY_US); + MbRtuDriverOutputControlHook(BLT_FALSE); + MbRtuDelay(BOOT_COM_MBRTU_DRIVER_OUTPUT_DISABLE_DELAY_US); +} /*** end of MbRtuTransmitPacket ***/ + + +/************************************************************************************//** +** \brief Receives a communication interface packet if one is present. +** \param data Pointer to byte array where the data is to be stored. +** \param len Pointer where the length of the packet is to be stored. +** \return BLT_TRUE if a packet was received, BLT_FALSE otherwise. +** +****************************************************************************************/ +blt_bool MbRtuReceivePacket(blt_int8u *data, blt_int8u *len) +{ + blt_bool result = BLT_FALSE; + blt_int8u rxByte; + blt_int16u currentTimeTicks; + blt_int16u deltaTimeTicks; + blt_int16u checksumCalculated; + blt_int16u checksumReceived; + /* Made static to lower stack load and +5 for Modbus RTU packet overhead. */ + static blt_int8u rxPacket[BOOT_COM_MBRTU_RX_MAX_DATA + 5]; + static blt_int8u rxLength = 0; + static blt_bool packetRxInProgress = BLT_FALSE; + static blt_int16u lastRxByteTimeTicks = 0; + + /* get the current value of the free running counter. */ + currentTimeTicks = MbRtuFreeRunningCounterGet(); + + /* check for a newly received byte. */ + if (MbRtuReceiveByte(&rxByte) == BLT_TRUE) + { + /* store the time at which the byte was received. */ + lastRxByteTimeTicks = currentTimeTicks; + /* is this the potential start of a new packet? */ + if (packetRxInProgress == BLT_FALSE) + { + /* initialize the reception of a new packet. */ + rxLength = 0; + packetRxInProgress = BLT_TRUE; + } + /* store the newly received byte in the buffer, with buffer overrun protection. */ + if (rxLength < (sizeof(rxPacket)/sizeof(rxPacket[0]))) + { + rxPacket[rxLength] = rxByte; + rxLength++; + } + /* buffer overrun occurred. received packet was longer than supported so discard + * the packet to try and sync to the next one. + */ + else + { + /* discard the partially received packet. */ + packetRxInProgress = BLT_FALSE; + } + } + + /* only attempt to detect the end of packet, when a reception is in progress. */ + if (packetRxInProgress == BLT_TRUE) + { + /* calculate the number of ticks that elapsed since the last byte reception. note + * that this calculation works, even if the free running counter overflowed. + */ + deltaTimeTicks = currentTimeTicks - lastRxByteTimeTicks; + /* packet reception is assumed complete after T3_5 of not receiving new data. */ + if (deltaTimeTicks >= mbRtuT3_5Ticks) + { + /* a Modbus RTU packet consists of at least the address field, function code and + * 16-bit CRC. Validate the packet length based on this info. + */ + if (rxLength >= 4) + { + /* calculate the packet checksum. */ + checksumCalculated = MbRtuCrcCalculate(&rxPacket[0], rxLength - 2); + /* extract the checksum received with the packet. */ + checksumReceived = rxPacket[rxLength - 2] | (rxPacket[rxLength - 1] << 8); + /* only continue with packet processing if the checksums match. */ + if (checksumCalculated == checksumReceived) + { + /* we are only interested in Modbus RTU packets that are addressed to us and + * have an XCP packet embedded. + */ + if ( (rxPacket[0] == BOOT_COM_MBRTU_NODE_ID) && + (rxPacket[1] == BOOT_COM_MBRTU_FCT_CODE_USER_XCP) ) + { + /* An XCP packet embedded in a Modbus RTU packet has an extra XCP packet + * length value. Use it to double-check that the packet length is valid. + */ + if (rxPacket[2] == (rxLength - 5)) + { + /* copy the packet's XCP data. */ + CpuMemCopy((blt_int32u)data, (blt_int32u)&rxPacket[3], rxLength - 5); + /* set the packet's XCP length. */ + *len = rxLength - 5; + /* update the result to success to indicate that this XCP packet is ready + * for processing. + */ + result = BLT_TRUE; + } + } + } + } + /* reset the packet reception in progress flag, to be able to receive the next. */ + packetRxInProgress = BLT_FALSE; + } + } + + /* give the result back to the caller. */ + return result; +} /*** end of MbRtuReceivePacket ***/ + + +/************************************************************************************//** +** \brief Receives a communication interface byte if one is present. +** \param data Pointer to byte where the data is to be stored. +** \return BLT_TRUE if a byte was received, BLT_FALSE otherwise. +** +****************************************************************************************/ +static blt_bool MbRtuReceiveByte(blt_int8u *data) +{ + blt_bool result = BLT_FALSE; + blt_int16u data_and_status; + + /* check to see if a new byte was received */ + if ((LEUART1->IF & LEUART_IF_RXDATAV) != 0) + { + /* update the result */ + result = BLT_TRUE; + /* read out the newly received data, including its reception status flags. */ + data_and_status = LEUART_RxExt(LEUART1); + /* check for a frame error. the frame error check is important because it can detect + * a missing stopbit. on an RS485 bus without bias resistors, the A-B differential + * voltage is 0. for an RS485 transceiver this is neither a 0 nor a 1 bit, so + * undefined. most RS485 transceivers feature a reception failsafe function to drive + * the Rx output (going to the UART Rx) to a defined state of logic 1. in case the + * used RS485 transceiver doesn't have such a feature, it typically leaves the Rx + * output in a logic 0 state. this means that after the stop bit of the last packet + * byte, the UART Rx input sees a logic 0, and assumes it is a start bit. the + * remaining data bits will always be 0 and, most importantly no stop bit is + * present, causing a framing error. Long story short: if you don't check for the + * framing error flag, you might receive an extra byte with value 0, which is not + * actually transmitted on the RS485 bus. you can catch and ignore this byte by doing + * a frame error check. + */ + if ((data_and_status & USART_RXDATAX_FERR) != 0U) + { + /* ignore the byte because of a detected frame error. */ + result = BLT_FALSE; + } + #if (BOOT_COM_MBRTU_PARITY > 0) + /* check for a parity error. */ + if ((data_and_status & USART_RXDATAX_PERR) != 0U) + { + /* ignore the byte because of a detected parity error. */ + result = BLT_FALSE; + } + #endif + /* store the newly received byte by casting it to exclude the status flags. */ + *data = (blt_int8u)data_and_status; + } + /* give the result back to the caller */ + return result; +} /*** end of MbRtuReceiveByte ***/ + + +/************************************************************************************//** +** \brief Transmits a communication interface byte. +** \param data Value of byte that is to be transmitted. +** \param end_of_packet BLT_TRUE if this is the last byte of the packet, BLT_FALSE +** otherwise. +** \return none. +** +****************************************************************************************/ +static void MbRtuTransmitByte(blt_int8u data, blt_bool end_of_packet) +{ + blt_int32u timeout; + + /* write byte to transmit holding register */ + LEUART_Tx(LEUART1, data); + /* set timeout time to wait for transmit completion. */ + timeout = TimerGet() + MBRTU_BYTE_TX_TIMEOUT_MS; + + /* not the last byte of the packet? */ + if (end_of_packet == BLT_FALSE) + { + /* wait for tx holding register to be empty */ + while ((LEUART1->STATUS & LEUART_STATUS_TXBL) == 0) + { + /* keep the watchdog happy. */ + CopService(); + /* break loop upon timeout. this would indicate a hardware failure. */ + if (TimerGet() > timeout) + { + break; + } + } + } + /* this is the last byte of a packet. */ + else + { + /* wait for tx complete flag to be set. this is needed for the last byte, otherwise + * the transceiver's transmit output gets disabled with + * MbRtuDriverOutputControlHook() before the byte reception completes. + */ + while ((LEUART1->STATUS & LEUART_STATUS_TXC) == 0) + { + /* keep the watchdog happy. */ + CopService(); + /* break loop upon timeout. this would indicate a hardware failure. */ + if (TimerGet() > timeout) + { + break; + } + } + } +} /*** end of MbRtuTransmitByte ***/ + + +/************************************************************************************//** +** \brief Obtains the counter value of the 100 kHz free running counter. Note that +** each count represent 10 us. The Modbus RTU communication module uses this +** free running counter for Modbus RTU packet timing related purposes. The +** already available 1 ms timer does not have the needed resolution for this +** purpose. +** \return Current value of the free running counter. +** +****************************************************************************************/ +blt_int16u MbRtuFreeRunningCounterGet(void) +{ + static blt_bool initialized = BLT_FALSE; + static blt_int32u counts_per_ten_us = 0; + static blt_int32u free_running_counter_last = 0; + static blt_int16u ten_us_tick_counter = 0; + blt_int32u free_running_counter_now; + blt_int32u delta_counts; + blt_int32u ten_us_ticks; + + /* perform one-time initialization.*/ + if (initialized == BLT_FALSE) + { + /* set the flag to make sure that this part only runs once. */ + initialized = BLT_TRUE; + /* calculate how many counts of the SysTick's free running counter equals 10us, + * because one count of a 100 kHz counter equals that. Note that the timer module + * already initialized and enable the SysTick. + */ + counts_per_ten_us = SystemCoreClockGet() / 100000UL; + /* initialize current value of the timer's free running counter. */ + free_running_counter_last = (SysTick->VAL & SysTick_VAL_CURRENT_Msk); + } + + /* get the current value of the free running counter. */ + free_running_counter_now = (SysTick->VAL & SysTick_VAL_CURRENT_Msk); + /* Calculate the number of counts that passed since the detection of the last + * millisecond event. Keep in mind that the SysTick has a down-counting 24-bit free + * running counter. Note that this calculation also works, in case the free running + * counter overflowed, thanks to integer math. + */ + delta_counts = free_running_counter_last - free_running_counter_now; + delta_counts &= SysTick_VAL_CURRENT_Msk; + + /* did one or more ten microsecond counts pass since the last event? */ + if (delta_counts >= counts_per_ten_us) + { + /* calculate how many ten microsecond counts passed. */ + ten_us_ticks = delta_counts / counts_per_ten_us; + /* update the counter. */ + ten_us_tick_counter += ten_us_ticks; + /* store the counter value of the last event to detect the next one. */ + free_running_counter_last -= (ten_us_ticks * counts_per_ten_us); + free_running_counter_last &= SysTick_VAL_CURRENT_Msk; + } + + /* return the current value of the 100 kHz free running counter to the caller. */ + return ten_us_tick_counter; +} /*** end of MbRtuFreeRunningCounterGet ***/ +#endif /* BOOT_COM_MBRTU_ENABLE > 0 */ + +/*********************************** end of mbrtu.c ************************************/ diff --git a/Target/Source/ARMCM3_EFM32/rs232.c b/Target/Source/ARMCM3_EFM32/rs232.c index bf3d9b8b..ce961763 100644 --- a/Target/Source/ARMCM3_EFM32/rs232.c +++ b/Target/Source/ARMCM3_EFM32/rs232.c @@ -30,13 +30,13 @@ * Include files ****************************************************************************************/ #include "boot.h" /* bootloader generic header */ +#if (BOOT_COM_RS232_ENABLE > 0) #include "efm32.h" #include "efm32_cmu.h" #include "efm32_gpio.h" #include "efm32_leuart.h" -#if (BOOT_COM_RS232_ENABLE > 0) /**************************************************************************************** * Macro definitions ****************************************************************************************/ @@ -205,7 +205,7 @@ static blt_bool Rs232ReceiveByte(blt_int8u *data) { blt_bool result = BLT_FALSE; - /* check to see if a new bytes was received */ + /* check to see if a new byte was received */ if ((LEUART1->IF & LEUART_IF_RXDATAV) != 0) { /* store the received data byte and set return value to positive */ diff --git a/Target/Source/ARMCM3_EFM32/timer.c b/Target/Source/ARMCM3_EFM32/timer.c index 2439f7c7..8480075f 100644 --- a/Target/Source/ARMCM3_EFM32/timer.c +++ b/Target/Source/ARMCM3_EFM32/timer.c @@ -30,29 +30,7 @@ * Include files ****************************************************************************************/ #include "boot.h" /* bootloader generic header */ - - -/**************************************************************************************** -* Type definitions -****************************************************************************************/ -/** \brief Systick registers. */ -typedef struct -{ - volatile blt_int32u CTRL; /**< SysTick Control and Status Register */ - volatile blt_int32u LOAD; /**< SysTick Reload Value Register */ - volatile blt_int32u VAL; /**< SysTick Current Value Register */ -} tSysTickRegs; - - -/**************************************************************************************** -* Macro definitions -****************************************************************************************/ -/** \brief CLKSOURCE bit of the system tick. */ -#define SYSTICK_BIT_CLKSOURCE ((blt_int32u)0x00000004) -/** \brief ENABLE bit of the system tick. */ -#define SYSTICK_BIT_ENABLE ((blt_int32u)0x00000001) -/** \brief COUNTERFLAG bit of the system tick. */ -#define SYSTICK_BIT_COUNTERFLAG ((blt_int32u)0x00010000) +#include "efm32.h" /**************************************************************************************** @@ -63,12 +41,13 @@ typedef struct */ static blt_int32u millisecond_counter; +/** \brief Buffer for storing the last value of the free running counter. */ +static blt_int32u free_running_counter_last; -/**************************************************************************************** -* Register definitions -****************************************************************************************/ -/** \brief Macro to access the system tick registers. */ -#define SYSTICK ((tSysTickRegs *) (blt_int32u)0xE000E010) +/** \brief Stores the number of counts of the free running counter that equals one + * millisecond. + */ +static blt_int32u counts_per_millisecond; /************************************************************************************//** @@ -78,16 +57,19 @@ static blt_int32u millisecond_counter; ****************************************************************************************/ void TimerInit(void) { - /* reset the timer configuration */ + /* Reset the timer configuration. */ TimerReset(); - /* configure the systick frequency as a 1 ms event generator */ - SYSTICK->LOAD = BOOT_CPU_SYSTEM_SPEED_KHZ - 1; - /* reset the current counter value */ - SYSTICK->VAL = 0; - /* select core clock as source and enable the timer */ - SYSTICK->CTRL = SYSTICK_BIT_CLKSOURCE | SYSTICK_BIT_ENABLE; - /* reset the millisecond counter value */ + /* Configure the reload value such that the full 24-bit range is used. */ + SysTick->LOAD = (0xFFFFFFFFUL & SysTick_LOAD_RELOAD_Msk); + /* Reset the current counter value. */ + SysTick->VAL = 0UL; + /* Select core clock as source and enable the timer. */ + SysTick->CTRL = SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_CLKSOURCE_Msk; + /* Calculate how many counts equal 1 millisecond. */ + counts_per_millisecond = SystemCoreClockGet() / 1000U; + /* Initialize remaining locals. */ millisecond_counter = 0; + free_running_counter_last = (SysTick->VAL & SysTick_VAL_CURRENT_Msk); } /*** end of TimerInit ***/ @@ -100,10 +82,10 @@ void TimerInit(void) void TimerReset(void) { /* set the systick's registers back into the default reset value */ - SYSTICK->CTRL = 0; - SYSTICK->LOAD = 0; - SYSTICK->VAL = 0; -} /* end of TimerReset */ + SysTick->CTRL = 0; + SysTick->LOAD = 0; + SysTick->VAL = 0; +} /*** end of TimerReset ***/ /************************************************************************************//** @@ -113,11 +95,30 @@ void TimerReset(void) ****************************************************************************************/ void TimerUpdate(void) { - /* check if the millisecond event occurred */ - if ((SYSTICK->CTRL & SYSTICK_BIT_COUNTERFLAG) != 0) + blt_int32u free_running_counter_now; + blt_int32u delta_counts; + blt_int32u ms_counts; + + /* Get the current value of the free running counter. */ + free_running_counter_now = (SysTick->VAL & SysTick_VAL_CURRENT_Msk); + /* Calculate the number of counts that passed since the detection of the last + * millisecond event. Keep in mind that the SysTick has a down-counting 24-bit free + * running counter. Note that this calculation also works, in case the free running + * counter overflowed, thanks to integer math. + */ + delta_counts = free_running_counter_last - free_running_counter_now; + delta_counts &= SysTick_VAL_CURRENT_Msk; + + /* Did one or more milliseconds pass since the last event? */ + if (delta_counts >= counts_per_millisecond) { - /* increment the millisecond counter */ - millisecond_counter++; + /* Calculate how many milliseconds passed. */ + ms_counts = delta_counts / counts_per_millisecond; + /* Update the millisecond counter. */ + millisecond_counter += ms_counts; + /* Store the counter value of the last millisecond event, to detect the next one. */ + free_running_counter_last -= (ms_counts * counts_per_millisecond); + free_running_counter_last &= SysTick_VAL_CURRENT_Msk; } } /*** end of TimerUpdate ***/