-
Notifications
You must be signed in to change notification settings - Fork 3k
M2351: Override wait_ns to provide more accurate implementation #10741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
targets/TARGET_NUVOTON/TARGET_M2351/TARGET_M23_NS/wait_ns.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (c) 2018-2020, Nuvoton Technology Corporation | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "cmsis.h" | ||
#include "platform/mbed_toolchain.h" | ||
#include "platform/mbed_wait_api.h" | ||
|
||
/* Override wait_ns to provide more accurate implementation | ||
* | ||
* At high HCLK rate, M2351 cannot provide zero-wait-state flash performance. Besides, | ||
* cache is off for non-secure land (for internal reason). To fix it, borrowing from | ||
* mbed-os/platform/mbed_wait_api_no_rtos.c wait_ns, we locate 'delay_loop_code' from | ||
* flash to SRAM to achieve zero-wait-state performance. | ||
* | ||
* NOTE1: With MPU, RAM is marked non-executable. We must mark RAM executable for | ||
* running 'delay_loop_code' in SRAM. | ||
* NOTE2: Cache is on for secure land. This override is necessary only for non-secure | ||
* land. | ||
*/ | ||
|
||
// Cortex-M0+, M3, M4 and M23 take 5 cycles per iteration - SUBS = 1, 2xNOP = 2, BCS = 2 | ||
#define LOOP_SCALER 5000 | ||
|
||
MBED_ALIGN(8) | ||
static uint16_t delay_loop_code[] = { | ||
0x1E40, // SUBS R0,R0,#1 | ||
0xBF00, // NOP | ||
0xBF00, // NOP | ||
0xD2FB, // BCS .-3 (0x00 would be .+2, so 0xFB = -5 = .-3) | ||
0x4770 // BX LR | ||
}; | ||
|
||
/* Take the address of the code, set LSB to indicate Thumb, and cast to void() function pointer */ | ||
#define delay_loop ((void(*)()) ((uintptr_t) delay_loop_code | 1)) | ||
|
||
void wait_ns(unsigned int ns) | ||
{ | ||
uint32_t cycles_per_us = SystemCoreClock / 1000000; | ||
// Note that this very calculation, plus call overhead, will take multiple | ||
// cycles. Could well be 100ns on its own... So round down here, startup is | ||
// worth at least one loop iteration. | ||
uint32_t count = (cycles_per_us * ns) / LOOP_SCALER; | ||
|
||
mbed_mpu_manager_lock_ram_execution(); | ||
delay_loop(count); | ||
mbed_mpu_manager_unlock_ram_execution(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a change for the main implementation that micro-optimises (nano-optimises?) this - not sure if it didn't land or you're based on old code.
If you use
+ 1
instead of| 1
that can be done at link time, rather than run-time. (The linker can offset symbols, but not bit-manipulate them). Saves 1 instruction and cycle.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not see the change on master. Still go with
| 1
.