initial commit

This commit is contained in:
bmy
2024-04-25 22:31:01 +08:00
commit 200b5703f9
119 changed files with 52620 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 and so on
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,84 @@
# dwt_delay
Microseconds delay lib based on DWT for STM32 or whatever ARM supporting it.
Just include `dwt_delay.h` in your project, call `DWT_Init()` and then use delays as needed.
Depending on MCU used, you may need to include another header file (with MCU peripherals defines) in `dwt_delay.h`.
The `stm32f1xx.h` is included by default, allowing STM32F1xx to start out of the box.
If you don't use STM32 MCU or CubeMX, read a section at the end.
Functions are named as DWT_* to be HAL-alike. Feel free to do whatever you like with this lib,
change names, indents, coding style, use it in LHC firmware.
## Example
```c
/* main.c */
#include "dwt_delay.h"
void main (void)
{
// Init section of your code
DWT_Init();
while(1) {
// Delay for 42us
DWT_Delay(42);
}
}
```
## Notes on Cortex-M0/0+/1
Unfortunately, these are not supported, since cores have no access to DWT. CMSIS library states:
```
Cortex-M0/0+/1 Core Debug Registers are only accessible over DAP and not via processor
```
You may want a delay function based on hardware timer instead.
## What about Cortex-M35/55/85?
I don't have any of these to check, but in theory they are supported.
Anyway you have to change `CoreDebug` to `DCB`, because `CoreDebug` is deprecated in these cores.
Hence, init sequence should be something like:
```c
DCB->DEMCR |= DCB_DEMCR_TRCENA_Msk;
```
## I'm not with STM but need microsec delays
There's an option to try! Also suits those, who use STM32, but dont use HAL/LL libs.
Include a CMSIS header file according to your core in `dwt_delay.c` and change `SystemCoreClock`
variable to whatever you probably have in the project representing clock frequency (in Hz).
Something like this:
```c
// In dwt_delay.c
#include "dwt_delay.h"
#include "core_cm4.h" // CMSIS header
#define SystemCoreClock NameOfTheGlobalVariableInYourProject_or_AnotherDefine
// or at least
#define SystemCoreClock 48000000UL // Clock is 48Mhz
...
```
## Changelog
- **2018-01-06**
This lib emerged.
- **2019-02-19**
Overflow check added.
- **2019-03-26**
Typo in definition fixed. Got back to short and simpler function.
- **2023-11-21**
Now it is MIT License. Added warning for Cortex-M0/0+/1 and notes regarding other Cortex-M cores.

View File

@@ -0,0 +1,73 @@
/*
* Simple microseconds delay routine, utilizing ARM's DWT
* (Data Watchpoint and Trace Unit) and HAL library.
* Intended to use with gcc compiler, but I hope it can be used
* with any other C compiler across the Universe (provided that
* ARM and CMSIS already invented) :)
* Max K
*
*
* This file is part of DWT_Delay package.
* DWT_Delay is free software: you can redistribute it and/or modify it
* under the terms of the MIT License
*/
// #include "stm32f1xx_hal.h" // change to whatever MCU or Cortex-M core you use
#include "dwt_delay.h"
#include "at32f413.h" // CMSIS header
/**
* Initialization routine.
* You might need to enable access to DWT registers on Cortex-M7
* DWT->LAR = 0xC5ACCE55
*/
void DWT_Init(void)
{
if (!(CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk)) {
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CYCCNT = 0;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
}
}
#if DWT_DELAY_NEWBIE
/**
* If you are a newbie and see magic in DWT_Delay, consider this more
* illustrative function, where you explicitly determine a counter
* value when delay should stop while keeping things in bounds of uint32.
*
* @param uint32_t us Number of microseconds to delay for
*/
void DWT_Delay(uint32_t us)
{
uint32_t startTick = DWT->CYCCNT,
targetTick = DWT->CYCCNT + us * (SystemCoreClock/1000000);
// Must check if target tick is out of bounds and overflowed
if (targetTick > startTick) {
// Not overflowed
while (DWT->CYCCNT < targetTick);
} else {
// Overflowed
while (DWT->CYCCNT > startTick || DWT->CYCCNT < targetTick);
}
}
#else
/**
* Delay routine itself.
* Time is in microseconds (1/1000000th of a second), not to be
* confused with millisecond (1/1000th).
*
* No need to check an overflow. Let it just tick :)
*
* @param uint32_t us Number of microseconds to delay for
*/
void DWT_Delay(uint32_t us)
{
uint32_t startTick = DWT->CYCCNT,
delayTicks = us * (SystemCoreClock/1000000);
while (DWT->CYCCNT - startTick < delayTicks);
}
#endif

View File

@@ -0,0 +1,31 @@
/*
* Simple microseconds delay routine, utilizing ARM's DWT
* (Data Watchpoint and Trace Unit) and HAL library.
* Intended to use with gcc compiler, but I hope it can be used
* with any other C compiler across the Universe (provided that
* ARM and CMSIS already invented) :)
* Max K
*
*
* This file is part of DWT_Delay package.
* DWT_Delay is free software: you can redistribute it and/or modify it
* under the terms of the MIT License.
*/
#if defined(__CORTEX_M) && __CORTEX_M < 3U
#warning DWT_Delay in useless in this project since DWT unit is not accessible \
by processor on Cortex-M0/0+/1 cores. You may want to implement microdelays \
with hardware timer.
#endif
#ifndef INC_DWT_DELAY_H_
#define INC_DWT_DELAY_H_
#include <stdint.h>
#define DWT_DELAY_NEWBIE 0
void DWT_Init(void);
void DWT_Delay(uint32_t us);
#endif /* INC_DWT_DELAY_H_ */