1.路径优化。

This commit is contained in:
MacRsh
2023-11-30 22:13:07 +08:00
parent 842e73b3dc
commit f7654e12b2
51 changed files with 841 additions and 171 deletions

165
README.md
View File

@@ -3,14 +3,22 @@
----------
<!-- TOC -->
* [MR 框架](#mr-框架)
* [简介](#简介)
* [关键特性](#关键特性)
* [主要组成](#主要组成)
* [标准化设备接口](#标准化设备接口)
* [配置工具](#配置工具)
* [目录结构](#目录结构)
* [开始使用](#开始使用)
* [配置 `Kconfig` 环境](#配置-kconfig-环境)
* [将框架导入工程](#将框架导入工程)
* [配置菜单选项](#配置菜单选项)
* [生成配置文件](#生成配置文件)
* [添加包含路径](#添加包含路径)
* [先来点个灯吧](#先来点个灯吧)
* [Hello World](#hello-world)
* [现在您已经完成了入门基础开始进一步探索MR库吧](#现在您已经完成了入门基础开始进一步探索mr库吧)
<!-- TOC -->
----------
@@ -52,15 +60,15 @@
设备的所有操作都可通过以下接口实现:
| 接口 | 描述 |
|:---------------------|:--------|
| mr_dev_register | 注册设备 |
| mr_dev_open | 打开设备 |
| mr_dev_close | 关闭设备 |
| mr_dev_ioctl | 控制设备 |
| mr_dev_read | 从设备读取数据 |
| mr_dev_write | 向设备写入数据 |
| mr_dev_isr | 设备中断控制 |
| 接口 | 描述 |
|:----------------|:--------|
| mr_dev_register | 注册设备 |
| mr_dev_open | 打开设备 |
| mr_dev_close | 关闭设备 |
| mr_dev_ioctl | 控制设备 |
| mr_dev_read | 从设备读取数据 |
| mr_dev_write | 向设备写入数据 |
| mr_dev_isr | 设备中断控制 |
----------
@@ -93,4 +101,137 @@
| kconfig.py | 自动配置脚本 |
| LICENSE | 许可证 |
----------
----------
# 开始使用
## 配置 `Kconfig` 环境
注:`Kconfig` 并非必须的,但是推荐使用(安装和配置非常快捷,后续教程也是以应用 `Kconfig` 为例)。
1. 验证系统是否安装Python环境。在命令行中运行 `python --version` 检查Python版本`Kconfig` 依赖于 ` python`,若无 ` python`
环境请自行安装)。
2. 在命令行中使用所示命令安装 `Kconfig`
```cmd
python -m pip install windows-curses
python -m pip install kconfiglib
```
3. 在命令行中运行 `menuconfig -h` 验证安装是否成功。
## 将框架导入工程
1. 从 `Gitee` 或 `Github` 仓库下载最新版本源码到本地。
2. 将源码导入到您工程所在的目录。以STM32工程为例
![工程目录](https://gitee.com/MacRsh/mr-library/raw/develop/document/picture/README_Build.png)
3. 如使用的芯片已经做了 `BSP` 适配请参考芯片对应 `BSP` 中的配置教程,完成 `BSP` 配置。
4. 移除不需要的文件 `bsp`、`document`、`module` 目录(如不需要`GIT`也可以移除`.git`文件删除)。完成后,目录结构如下所示:
![工程目录1](https://gitee.com/MacRsh/mr-library/raw/develop/document/picture/README_Build1.png)
## 配置菜单选项
1. 在 `mr-library` 目录下打开命令行工具,运行 `menuconfig` 进行菜单配置。
![工程目录2](https://gitee.com/MacRsh/mr-library/raw/develop/document/picture/README_Build2.png)
注:当添加对应芯片驱动的后,将显示 `Device configure` 和 `Driver configure`。对应 `Driver configure` 请参考 `BSP` 下教程。
2. 选中 `Device configure` 回车进入菜单,按照需要配置功能。
![工程目录3](https://gitee.com/MacRsh/mr-library/raw/develop/document/picture/README_Build3.png)
3. 配置完成后,按 `Q` 退出菜单配置界面,按`Y` 保存配置。
## 生成配置文件
1. 在 `mr-library` 目录下打开命令行工具,运行 `python kconfig.py`,自动生成配置文件 `mr_config.h`。
## 添加包含路径
1. 在编译器中添加 `mr-library` 的包含路径,以 `keil` 为例:
![工程目录4](https://gitee.com/MacRsh/mr-library/raw/develop/document/picture/README_Build4.png)
2. 配置自动初始化GCC环境查找您工程下以 `.ld` 为后缀的连接脚本文件(通常为 `link.ld`),在脚本文件中添加代码:
注:如果您的是在 `keil` 等,能够自动生成链接脚本的环境下,请跳过此步骤。
```c
/* mr-library auto init */
. = ALIGN(4);
_mr_auto_init_start = .;
KEEP(*(SORT(.auto_init*)))
_mr_auto_init_end = .;
```
示例:
![工程目录5](https://gitee.com/MacRsh/mr-library/raw/develop/document/picture/README_Build5.png)
3. 在您的工程中引入 `#include "include/mr_lib.h"`。
4. 在 `main` 函数中添加 `mr_auto_init();` 自动初始化函数。
----------
# 先来点个灯吧
```c
#include "include/mr_lib.h"
/* 定义LED引脚PC13*/
#define LED_PIN_NUMBER 45
int main(void)
{
/* 自动初始化 */
mr_auto_init();
/* 打开PIN设备 */
int ds = mr_dev_open("pin", MR_OFLAG_RDWR);
/* 设置到LED引脚 */
mr_dev_ioctl(ds, MR_CTL_PIN_SET_NUMBER, mr_make_local(int, LED_PIN_NUMBER));
/* 设置LED引脚为推挽输出模式 */
mr_dev_ioctl(ds, MR_CTL_PIN_SET_MODE, mr_make_local(int, MR_PIN_MODE_OUTPUT));
while(1)
{
/* 点亮LED */
mr_dev_write(ds, mr_make_local(uint8_t, MR_PIN_HIGH_LEVEL), sizeof(uint8_t));
mr_delay_ms(500);
mr_dev_write(ds, mr_make_local(uint8_t, MR_PIN_LOW_LEVEL), sizeof(uint8_t));
mr_delay_ms(500);
}
}
```
# Hello World
```c
#include "include/mr_lib.h"
int main(void)
{
/* 自动初始化 */
mr_auto_init();
/* 打开Serial-1设备 */
int ds = mr_dev_open("serial1", MR_OFLAG_RDWR);
/* 输出Hello World */
mr_dev_write(ds, "Hello World\r\n", sizeof("Hello World\r\n"));
while(1);
}
```
----------
# 现在您已经完成了入门基础开始进一步探索MR库吧
1. 欢迎参考更多教程文档目录在document中。
2. 可以尝试基于某款芯片开发驱动,练习设备驱动编程。
3. 尝试编写更多设备模板和开发更多功能。
4. 欢迎您提出意见和建议。如果您对开发有兴趣,诚邀您参与到 `MR` 项目的开发中来项目交流群199915649QQ

View File

@@ -10,6 +10,15 @@
* [Standardized device interface](#standardized-device-interface)
* [Configuration tool](#configuration-tool)
* [Directory structure](#directory-structure)
* [Get Started](#get-started)
* [Configure the Kconfig Environment](#configure-the-kconfig-environment)
* [Import the Framework into the Project](#import-the-framework-into-the-project)
* [Configure Menu Options](#configure-menu-options)
* [Generate Configuration File](#generate-configuration-file)
* [Add Include Paths](#add-include-paths)
* [Let's Light an LED](#lets-light-an-led)
* [Hello World](#hello-world)
* [Now you have completed the basics, start exploring the MR library further.](#now-you-have-completed-the-basics-start-exploring-the-mr-library-further)
<!-- TOC -->
----------
@@ -58,15 +67,15 @@ drivers. This greatly improves the reusability of software and its extensibility
All operations of the device can be implemented through the following interfaces:
| interface | describe |
|:---------------------|:----------------------------|
| mr_dev_register | Registered device |
| mr_dev_open | Open device |
| mr_dev_close | Close device |
| mr_dev_ioctl | Control device |
| mr_dev_read | Read data from the device |
| mr_dev_write | Writes data to the device |
| mr_dev_isr | Device interrupt control |
| interface | describe |
|:----------------|:--------------------------|
| mr_dev_register | Registered device |
| mr_dev_open | Open device |
| mr_dev_close | Close device |
| mr_dev_ioctl | Control device |
| mr_dev_read | Read data from the device |
| mr_dev_write | Writes data to the device |
| mr_dev_isr | Device interrupt control |
----------
@@ -102,4 +111,145 @@ the `Python` script automatically generates the configuration file.
| kconfig.py | Automatic configuration script |
| LICENSE | Open-source license |
----------
----------
# Get Started
## Configure the Kconfig Environment
Note: Kconfig is not mandatory, but recommended (installation and configuration are very quick, and the following
tutorials are based on applying Kconfig).
1. Verify that the system has a Python environment installed. Run `python --version` in the command line to check the
Python version (Kconfig depends on python, please install python if it is not available).
2. Use the following commands to install Kconfig in the command line:
```cmd
python -m pip install windows-curses
python -m pip install kconfiglib
```
3. Run `menuconfig -h` in the command line to verify successful installation.
## Import the Framework into the Project
1. Download the latest version source code from the Gitee or Github repository to the local.
2. Import the source code into the directory where your project is located. Taking an STM32 project as an example:
![project directory](https://gitee.com/MacRsh/mr-library/raw/develop/document/picture/README_Build.png)
3. If the used chip has BSP adaptation, please refer to the chip's corresponding BSP configuration tutorial to complete
the BSP configuration.
4. Remove unnecessary files such as `bsp`、`document`、`module` directories (you can also remove the `.git` file to delete
GIT if not needed). The directory structure is shown below after completion:
![project directory1](https://gitee.com/MacRsh/mr-library/raw/develop/document/picture/README_Build1.png)
## Configure Menu Options
1. Open the command line tool in the `mr-library` directory and run `menuconfig` to configure the menu.
![project directory2](https://gitee.com/MacRsh/mr-library/raw/develop/document/picture/README_Build2.png)
Note: When the corresponding chip driver is added, `Device configure` and `Driver configure` will be displayed.
Please refer to the tutorial under `BSP` for `Driver configure`.
2. Enter the menu by pressing the Enter key on `Device configure`, and configure the desired functions according to
needs.
![project directory3](https://gitee.com/MacRsh/mr-library/raw/develop/document/picture/README_Build3.png)
3. After configuration is complete, press `Q` to exit the menu configuration interface, press `Y` to save the
configuration.
## Generate Configuration File
1. Run `python kconfig.py` in the command line tool under `mr-library` directory to automatically generate the
configuration file `mr_config.h`.
## Add Include Paths
1. Add the include paths of `mr-library` in the compiler, taking `keil` as an example:
![project directory4](https://gitee.com/MacRsh/mr-library/raw/develop/document/picture/README_Build4.png)
2. Configure automatic initialization (GCC environment), find the link script file with suffix `.ld` in your project
directory (usually `link.ld`), and add the following code to the script file:
```c
/* mr-library auto init */
. = ALIGN(4);
_mr_auto_init_start = .;
KEEP(*(SORT(.auto_init*)))
_mr_auto_init_end = .;
```
Example:
![project directory5](https://gitee.com/MacRsh/mr-library/raw/develop/document/picture/README_Build5.png)
3. Include `#include "include/mr_lib.h"` in your project.
4. Add the automatic initialization function `mr_auto_init();` in the main function.
----------
# Let's Light an LED
```c
#include "include/mr_lib.h"
/* Define the LED pin-number (PC13) */
#define LED_PIN_NUMBER 45
int main(void)
{
/* Automatic initialization */
mr_auto_init();
/* Open the PIN device */
int ds = mr_dev_open("pin", MR_OFLAG_RDWR);
/* Set to the LED pin */
mr_dev_ioctl(ds, MR_CTL_PIN_SET_NUMBER, mr_make_local(int, LED_PIN_NUMBER));
/* Set the LED pin to push-pull output mode */
mr_dev_ioctl(ds, MR_CTL_PIN_SET_MODE, mr_make_local(int, MR_PIN_MODE_OUTPUT));
while(1)
{
/* Light up the LED */
mr_dev_write(ds, mr_make_local(uint8_t, MR_PIN_HIGH_LEVEL), sizeof(uint8_t));
mr_delay_ms(500);
mr_dev_write(ds, mr_make_local(uint8_t, MR_PIN_LOW_LEVEL), sizeof(uint8_t));
mr_delay_ms(500);
}
}
```
# Hello World
```c
#include "include/mr_lib.h"
int main(void)
{
/* Automatic initialization */
mr_auto_init();
/* Open the Serial-1 device */
int ds = mr_dev_open("serial1", MR_OFLAG_RDWR);
/* Output Hello World */
mr_dev_write(ds, "Hello World\r\n", sizeof("Hello World\r\n"));
while(1);
}
```
----------
# Now you have completed the basics, start exploring the MR library further.
1. Welcome to refer to more tutorials, document directory is in document.
2. You can try developing drivers based on certain chips to practice device driver programming.
3. Try writing more device templates and developing more features.
4. Welcome to provide your opinions and suggestions. If you are interested in development, you are welcome to
participate in the development of the `MR` project. The project discussion group is: 199915649(QQ).

View File

@@ -9,7 +9,7 @@
#ifndef _DRV_ADC_H_
#define _DRV_ADC_H_
#include "device/adc.h"
#include "include/device/adc.h"
#include "mr_board.h"
#ifdef __cplusplus

View File

@@ -9,7 +9,7 @@
#ifndef _DRV_I2C_H_
#define _DRV_I2C_H_
#include "device/i2c.h"
#include "include/device/i2c.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -9,7 +9,7 @@
#ifndef _DRV_PIN_H_
#define _DRV_PIN_H_
#include "device/pin.h"
#include "include/device/pin.h"
#include "mr_board.h"
#ifdef __cplusplus

View File

@@ -9,7 +9,7 @@
#ifndef _DRV_SERIAL_H_
#define _DRV_SERIAL_H_
#include "device/serial.h"
#include "include/device/serial.h"
#include "mr_board.h"
#ifdef __cplusplus

View File

@@ -9,7 +9,7 @@
#ifndef _DRV_SPI_H_
#define _DRV_SPI_H_
#include "device/spi.h"
#include "include/device/spi.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -1,46 +0,0 @@
/*
* @copyright (c) 2023, MR Development Team
*
* @license SPDX-License-Identifier: Apache-2.0
*
* @date 2023-11-11 MacRsh First version
*/
#ifndef _MR_DRV_H_
#define _MR_DRV_H_
#include "mr_config.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#ifdef MR_USING_ADC
#include "drv_adc.h"
#endif
#ifdef MR_USING_CAN
#include "drv_can.h"
#endif
#ifdef MR_USING_I2C
#include "drv_i2c.h"
#endif
#ifdef MR_USING_PIN
#include "drv_pin.h"
#endif
#ifdef MR_USING_SERIAL
#include "drv_serial.h"
#endif
#ifdef MR_USING_SPI
#include "drv_spi.h"
#endif
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _MR_DRV_H_ */

View File

@@ -225,4 +225,46 @@ menu "Board configure"
endmenu
menu "Timer"
config MR_USING_TIMER1
bool "Enable Timer1 driver"
default n
config MR_USING_TIMER2
bool "Enable Timer2 driver"
default n
config MR_USING_TIMER3
bool "Enable Timer3 driver"
default n
config MR_USING_TIMER4
bool "Enable Timer4 driver"
default n
config MR_USING_TIMER5
bool "Enable Timer5 driver"
default n
config MR_USING_TIMER6
bool "Enable Timer6 driver"
default n
config MR_USING_TIMER7
bool "Enable Timer7 driver"
default n
config MR_USING_TIMER8
bool "Enable Timer8 driver"
default n
config MR_USING_TIMER9
bool "Enable Timer9 driver"
default n
config MR_USING_TIMER10
bool "Enable Timer10 driver"
default n
endmenu
endmenu

View File

@@ -162,7 +162,7 @@ static struct mr_adc_ops adc_drv_ops =
drv_adc_read
};
static struct mr_drv adc_drv[mr_array_num(adc_drv_data)] =
static struct mr_drv adc_drv[] =
{
#ifdef MR_USING_ADC1
{

View File

@@ -9,7 +9,7 @@
#ifndef _DRV_ADC_H_
#define _DRV_ADC_H_
#include "device/adc.h"
#include "include/device/adc.h"
#include "mr_board.h"
#ifdef __cplusplus

View File

@@ -9,7 +9,7 @@
#ifndef _DRV_DAC_H_
#define _DRV_DAC_H_
#include "device/dac.h"
#include "include/device/dac.h"
#include "mr_board.h"
#ifdef MR_USING_DAC

View File

@@ -242,8 +242,6 @@ static ssize_t drv_i2c_bus_read(struct mr_i2c_bus *i2c_bus, uint8_t *buf, size_t
/* Read data */
while (I2C_CheckEvent(i2c_bus_data->instance, I2C_EVENT_MASTER_BYTE_RECEIVED) == RESET)
{
}
{
i++;
if (i > UINT16_MAX)
@@ -316,7 +314,7 @@ static struct mr_i2c_bus_ops i2c_bus_drv_ops =
drv_i2c_bus_write,
};
static struct mr_drv i2c_bus_drv[mr_array_num(i2c_bus_drv_data)] =
static struct mr_drv i2c_bus_drv[] =
{
#ifdef MR_USING_I2C1
{

View File

@@ -9,7 +9,7 @@
#ifndef _DRV_I2C_H_
#define _DRV_I2C_H_
#include "device/i2c.h"
#include "include/device/i2c.h"
#include "mr_board.h"
#ifdef __cplusplus

View File

@@ -9,7 +9,7 @@
#ifndef _DRV_PIN_H_
#define _DRV_PIN_H_
#include "device/pin.h"
#include "include/device/pin.h"
#include "mr_board.h"
#ifdef __cplusplus

View File

@@ -608,7 +608,6 @@ static ssize_t drv_serial_write(struct mr_serial *serial, const uint8_t *buf, si
int i = 0;
/* Write data */
USART_SendData(serial_data->instance, buf[wr_size]);
while (USART_GetFlagStatus(serial_data->instance, USART_FLAG_TC) == RESET)
{
i++;
@@ -617,6 +616,7 @@ static ssize_t drv_serial_write(struct mr_serial *serial, const uint8_t *buf, si
return wr_size;
}
}
USART_SendData(serial_data->instance, buf[wr_size]);
}
return wr_size;
}
@@ -727,7 +727,7 @@ static struct mr_serial_ops serial_drv_ops =
drv_serial_stop_tx
};
static struct mr_drv serial_drv[mr_array_num(serial_drv_data)] =
static struct mr_drv serial_drv[] =
{
#ifdef MR_USING_UART1
{

View File

@@ -9,7 +9,7 @@
#ifndef _DRV_SERIAL_H_
#define _DRV_SERIAL_H_
#include "device/serial.h"
#include "include/device/serial.h"
#include "mr_board.h"
#ifdef __cplusplus

View File

@@ -424,7 +424,7 @@ static struct mr_spi_bus_ops spi_bus_drv_ops =
drv_spi_bus_write,
};
static struct mr_drv spi_bus_drv[mr_array_num(spi_bus_drv_data)] =
static struct mr_drv spi_bus_drv[] =
{
#ifdef MR_USING_SPI1
{

View File

@@ -9,7 +9,7 @@
#ifndef _DRV_SPI_H_
#define _DRV_SPI_H_
#include "device/spi.h"
#include "include/device/spi.h"
#include "mr_board.h"
#ifdef __cplusplus

View File

@@ -0,0 +1,408 @@
/*
* @copyright (c) 2023, MR Development Team
*
* @license SPDX-License-Identifier: Apache-2.0
*
* @date 2023-11-30 MacRsh First version
*/
#include "drv_timer.h"
#ifdef MR_USING_TIMER
#define MR_USING_TIMER1
#if !defined(MR_USING_TIMER1) && !defined(MR_USING_TIMER2) && !defined(MR_USING_TIMER3) && !defined(MR_USING_TIMER4) && !defined(MR_USING_TIMER5) && !defined(MR_USING_TIMER6) && !defined(MR_USING_TIMER7) && !defined(MR_USING_TIMER8) && !defined(MR_USING_TIMER9) && !defined(MR_USING_TIMER10)
#error "Please define at least one Timer macro like MR_USING_TIMER1. Otherwise undefine MR_USING_TIMER."
#else
enum drv_timer_index
{
#ifdef MR_USING_TIMER1
DRV_INDEX_TIMER1,
#endif /* MR_USING_TIMER1 */
#ifdef MR_USING_TIMER2
DRV_INDEX_TIMER2,
#endif /* MR_USING_TIMER2 */
#ifdef MR_USING_TIMER3
DRV_INDEX_TIMER3,
#endif /* MR_USING_TIMER3 */
#ifdef MR_USING_TIMER4
DRV_INDEX_TIMER4,
#endif /* MR_USING_TIMER4 */
#ifdef MR_USING_TIMER5
DRV_INDEX_TIMER5,
#endif /* MR_USING_TIMER5 */
#ifdef MR_USING_TIMER6
DRV_INDEX_TIMER6,
#endif /* MR_USING_TIMER6 */
#ifdef MR_USING_TIMER7
DRV_INDEX_TIMER7,
#endif /* MR_USING_TIMER7 */
#ifdef MR_USING_TIMER8
DRV_INDEX_TIMER8,
#endif /* MR_USING_TIMER8 */
#ifdef MR_USING_TIMER9
DRV_INDEX_TIMER9,
#endif /* MR_USING_TIMER9 */
#ifdef MR_USING_TIMER10
DRV_INDEX_TIMER10,
#endif /* MR_USING_TIMER10 */
};
static const char *timer_name[] =
{
#ifdef MR_USING_TIMER1
"timer1",
#endif /* MR_USING_TIMER1 */
#ifdef MR_USING_TIMER2
"timer2",
#endif /* MR_USING_TIMER2 */
#ifdef MR_USING_TIMER3
"timer3",
#endif /* MR_USING_TIMER3 */
#ifdef MR_USING_TIMER4
"timer4",
#endif /* MR_USING_TIMER4 */
#ifdef MR_USING_TIMER5
"timer5",
#endif /* MR_USING_TIMER5 */
#ifdef MR_USING_TIMER6
"timer6",
#endif /* MR_USING_TIMER6 */
#ifdef MR_USING_TIMER7
"timer7",
#endif /* MR_USING_TIMER7 */
#ifdef MR_USING_TIMER8
"timer8",
#endif /* MR_USING_TIMER8 */
#ifdef MR_USING_TIMER9
"timer9",
#endif /* MR_USING_TIMER9 */
#ifdef MR_USING_TIMER10
"timer10",
#endif /* MR_USING_TIMER10 */
};
static struct drv_timer_data timer_drv_data[] =
{
#ifdef MR_USING_TIMER1
{TIM1, RCC_APB2Periph_TIM1, TIM1_UP_IRQn},
#endif /* MR_USING_TIMER1 */
#ifdef MR_USING_TIMER2
{TIM2, RCC_APB1Periph_TIM2, TIM2_IRQn},
#endif /* MR_USING_TIMER2 */
#ifdef MR_USING_TIMER3
{TIM3, RCC_APB1Periph_TIM3, TIM3_IRQn},
#endif /* MR_USING_TIMER3 */
#ifdef MR_USING_TIMER4
{TIM4, RCC_APB1Periph_TIM4, TIM4_IRQn},
#endif /* MR_USING_TIMER4 */
#ifdef MR_USING_TIMER5
{TIM5, RCC_APB1Periph_TIM5, TIM5_IRQn},
#endif /* MR_USING_TIMER5 */
#ifdef MR_USING_TIMER6
{TIM6, RCC_APB1Periph_TIM6, TIM6_IRQn},
#endif /* MR_USING_TIMER6 */
#ifdef MR_USING_TIMER7
{TIM7, RCC_APB1Periph_TIM7, TIM7_IRQn},
#endif /* MR_USING_TIMER7 */
#ifdef MR_USING_TIMER8
{TIM8, RCC_APB2Periph_TIM8, TIM8_UP_IRQn},
#endif /* MR_USING_TIMER8 */
#ifdef MR_USING_TIMER9
{TIM9, RCC_APB2Periph_TIM9, TIM9_UP_IRQn},
#endif /* MR_USING_TIMER9 */
#ifdef MR_USING_TIMER10
{TIM10, RCC_APB2Periph_TIM10, TIM10_UP_IRQn},
#endif /* MR_USING_TIMER10 */
};
static struct mr_timer timer_dev[mr_array_num(timer_drv_data)];
static struct mr_timer_info timer_info[] =
{
#ifdef MR_USING_TIMER1
{0, UINT16_MAX, UINT16_MAX},
#endif /* MR_USING_TIMER1 */
#ifdef MR_USING_TIMER2
{0, UINT16_MAX, UINT16_MAX},
#endif /* MR_USING_TIMER2 */
#ifdef MR_USING_TIMER3
{0, UINT16_MAX, UINT16_MAX},
#endif /* MR_USING_TIMER3 */
#ifdef MR_USING_TIMER4
{0, UINT16_MAX, UINT16_MAX},
#endif /* MR_USING_TIMER4 */
#ifdef MR_USING_TIMER5
{0, UINT16_MAX, UINT16_MAX},
#endif /* MR_USING_TIMER5 */
#ifdef MR_USING_TIMER6
{0, UINT16_MAX, UINT16_MAX},
#endif /* MR_USING_TIMER6 */
#ifdef MR_USING_TIMER7
{0, UINT16_MAX, UINT16_MAX},
#endif /* MR_USING_TIMER7 */
#ifdef MR_USING_TIMER8
{0, UINT16_MAX, UINT16_MAX},
#endif /* MR_USING_TIMER8 */
#ifdef MR_USING_TIMER9
{0, UINT16_MAX, UINT16_MAX},
#endif /* MR_USING_TIMER9 */
#ifdef MR_USING_TIMER10
{0, UINT16_MAX, UINT16_MAX},
#endif /* MR_USING_TIMER10 */
};
static int drv_timer_configure(struct mr_timer *timer, int state)
{
struct drv_timer_data *timer_data = (struct drv_timer_data *)timer->dev.drv->data;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure = {0};
NVIC_InitTypeDef NVIC_InitStructure = {0};
RCC_ClocksTypeDef RCC_ClockStructure = {0};
uint32_t pclk = 0;
/* Configure clock */
RCC_GetClocksFreq(&RCC_ClockStructure);
if ((uint32_t)timer_data->instance > APB2PERIPH_BASE)
{
RCC_APB2PeriphClockCmd(timer_data->clock, state);
pclk = RCC_ClockStructure.PCLK2_Frequency;
} else
{
RCC_APB1PeriphClockCmd(timer_data->clock, state);
pclk = RCC_ClockStructure.PCLK1_Frequency;
}
/* Update timer clock(MHz) */
timer->info->clk = pclk / 1000000;
/* Configure NVIC */
NVIC_InitStructure.NVIC_IRQChannel = timer_data->irq;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = state;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(timer_data->instance, TIM_IT_Update, state);
/* Configure timer */
TIM_TimeBaseInitStructure.TIM_Period = 0;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(timer_data->instance, &TIM_TimeBaseInitStructure);
return MR_EOK;
}
static void drv_timer_start(struct mr_timer *timer, uint32_t prescaler, uint32_t period)
{
struct drv_timer_data *timer_data = (struct drv_timer_data *)timer->dev.drv->data;
/* Set the PSC and ARR, and enable the timer */
timer_data->instance->CNT = 0;
timer_data->instance->PSC = prescaler - 1;
timer_data->instance->ATRLR = period - 1;
TIM_Cmd(timer_data->instance, ENABLE);
}
static void drv_timer_stop(struct mr_timer *timer)
{
struct drv_timer_data *timer_data = (struct drv_timer_data *)timer->dev.drv->data;
/* Disable the timer */
TIM_Cmd(timer_data->instance, DISABLE);
}
static uint32_t drv_timer_get_count(struct mr_timer *timer)
{
struct drv_timer_data *timer_data = (struct drv_timer_data *)timer->dev.drv->data;
return timer_data->instance->CNT;
}
static void drv_timer_isr(struct mr_timer *timer)
{
struct drv_timer_data *timer_data = (struct drv_timer_data *)timer->dev.drv->data;
if (TIM_GetITStatus(timer_data->instance, TIM_IT_Update) != RESET)
{
mr_dev_isr(&timer->dev, MR_ISR_TIMER_TIMEOUT_INT, MR_NULL);
TIM_ClearITPendingBit(timer_data->instance, TIM_IT_Update);
}
}
#ifdef MR_USING_TIMER1
void TIM1_UP_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM1_UP_IRQHandler(void)
{
drv_timer_isr(&timer_dev[DRV_INDEX_TIMER1]);
}
#endif /* MR_USING_TIMER1 */
#ifdef MR_USING_TIMER2
void TIM2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM2_IRQHandler(void)
{
drv_timer_isr(&timer_dev[DRV_INDEX_TIMER2]);
}
#endif /* MR_USING_TIMER2 */
#ifdef MR_USING_TIMER3
void TIM3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM3_IRQHandler(void)
{
drv_timer_isr(&timer_dev[DRV_INDEX_TIMER3]);
}
#endif /* MR_USING_TIMER3 */
#ifdef MR_USING_TIMER4
void TIM4_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM4_IRQHandler(void)
{
drv_timer_isr(&timer_dev[DRV_INDEX_TIMER4]);
}
#endif /* MR_USING_TIMER4 */
#ifdef MR_USING_TIMER5
void TIM5_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM5_IRQHandler(void)
{
drv_timer_isr(&timer_dev[DRV_INDEX_TIMER5]);
}
#endif /* MR_USING_TIMER5 */
#ifdef MR_USING_TIMER6
void TIM6_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM6_IRQHandler(void)
{
drv_timer_isr(&timer_dev[DRV_INDEX_TIMER6]);
}
#endif /* MR_USING_TIMER6 */
#ifdef MR_USING_TIMER7
void TIM7_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM7_IRQHandler(void)
{
drv_timer_isr(&timer_dev[DRV_INDEX_TIMER7]);
}
#endif /* MR_USING_TIMER7 */
#ifdef MR_USING_TIMER8
void TIM8_UP_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM8_UP_IRQHandler(void)
{
drv_timer_isr(&timer_dev[DRV_INDEX_TIMER8]);
}
#endif /* MR_USING_TIMER8 */
#ifdef MR_USING_TIMER9
void TIM9_UP_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM9_UP_IRQHandler(void)
{
drv_timer_isr(&timer_dev[DRV_INDEX_TIMER9]);
}
#endif /* MR_USING_TIMER9 */
#ifdef MR_USING_TIMER10
void TIM10_UP_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM10_UP_IRQHandler(void)
{
drv_timer_isr(&timer_dev[DRV_INDEX_TIMER10]);
}
#endif /* MR_USING_TIMER10 */
static struct mr_timer_ops timer_drv_ops =
{
drv_timer_configure,
drv_timer_start,
drv_timer_stop,
drv_timer_get_count
};
static struct mr_drv timer_drv[] =
{
#ifdef MR_USING_TIMER1
{
Mr_Drv_Type_Timer,
&timer_drv_ops,
&timer_drv_data[DRV_INDEX_TIMER1]
},
#endif /* MR_USING_TIMER1 */
#ifdef MR_USING_TIMER2
{
Mr_Drv_Type_Timer,
&timer_drv_ops,
&timer_drv_data[DRV_INDEX_TIMER2]
},
#endif /* MR_USING_TIMER2 */
#ifdef MR_USING_TIMER3
{
Mr_Drv_Type_Timer,
&timer_drv_ops,
&timer_drv_data[DRV_INDEX_TIMER3]
},
#endif /* MR_USING_TIMER3 */
#ifdef MR_USING_TIMER4
{
Mr_Drv_Type_Timer,
&timer_drv_ops,
&timer_drv_data[DRV_INDEX_TIMER4]
},
#endif /* MR_USING_TIMER4 */
#ifdef MR_USING_TIMER5
{
Mr_Drv_Type_Timer,
&timer_drv_ops,
&timer_drv_data[DRV_INDEX_TIMER5]
},
#endif /* MR_USING_TIMER5 */
#ifdef MR_USING_TIMER6
{
Mr_Drv_Type_Timer,
&timer_drv_ops,
&timer_drv_data[DRV_INDEX_TIMER6]
},
#endif /* MR_USING_TIMER6 */
#ifdef MR_USING_TIMER7
{
Mr_Drv_Type_Timer,
&timer_drv_ops,
&timer_drv_data[DRV_INDEX_TIMER7]
},
#endif /* MR_USING_TIMER7 */
#ifdef MR_USING_TIMER8
{
Mr_Drv_Type_Timer,
&timer_drv_ops,
&timer_drv_data[DRV_INDEX_TIMER8]
},
#endif /* MR_USING_TIMER8 */
#ifdef MR_USING_TIMER9
{
Mr_Drv_Type_Timer,
&timer_drv_ops,
&timer_drv_data[DRV_INDEX_TIMER9]
},
#endif /* MR_USING_TIMER9 */
#ifdef MR_USING_TIMER10
{
Mr_Drv_Type_Timer,
&timer_drv_ops,
&timer_drv_data[DRV_INDEX_TIMER10]
},
#endif /* MR_USING_TIMER10 */
};
int drv_timer_init(void)
{
int index = 0;
for (index = 0; index < mr_array_num(timer_dev); index++)
{
mr_timer_register(&timer_dev[index], timer_name[index], &timer_drv[index], &timer_info[index]);
}
return MR_EOK;
}
MR_DRV_EXPORT(drv_timer_init);
#endif /* !defined(MR_USING_TIMER1) && !defined(MR_USING_TIMER2) && !defined(MR_USING_TIMER3) && !defined(MR_USING_TIMER4) && !defined(MR_USING_TIMER5) && !defined(MR_USING_TIMER6) && !defined(MR_USING_TIMER7) && !defined(MR_USING_TIMER8) && !defined(MR_USING_TIMER9) && !defined(MR_USING_TIMER10) */
#endif /* MR_USING_TIMER */

View File

@@ -0,0 +1,26 @@
/*
* @copyright (c) 2023, MR Development Team
*
* @license SPDX-License-Identifier: Apache-2.0
*
* @date 2023-11-30 MacRsh First version
*/
#ifndef _DRV_TIMER_H_
#define _DRV_TIMER_H_
#include "include/device/timer.h"
#include "mr_board.h"
#ifdef MR_USING_TIMER
struct drv_timer_data
{
TIM_TypeDef *instance;
uint32_t clock;
IRQn_Type irq;
};
#endif /* MR_USING_TIMER */
#endif /* _DRV_TIMER_H_ */

View File

@@ -1,50 +0,0 @@
/*
* @copyright (c) 2023, MR Development Team
*
* @license SPDX-License-Identifier: Apache-2.0
*
* @date 2023-11-11 MacRsh First version
*/
#ifndef _MR_DRV_H_
#define _MR_DRV_H_
#include "mr_config.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#ifdef MR_USING_ADC
#include "drv_adc.h"
#endif
#ifdef MR_USING_CAN
#include "drv_can.h"
#endif
#ifdef MR_USING_DAC
#include "drv_dac.h"
#endif
#ifdef MR_USING_I2C
#include "drv_i2c.h"
#endif
#ifdef MR_USING_PIN
#include "drv_pin.h"
#endif
#ifdef MR_USING_SERIAL
#include "drv_serial.h"
#endif
#ifdef MR_USING_SPI
#include "drv_spi.h"
#endif
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _MR_DRV_H_ */

View File

@@ -6,7 +6,7 @@
* @date 2023-11-06 MacRsh First version
*/
#include "adc.h"
#include "include/device/adc.h"
#ifdef MR_USING_ADC

View File

@@ -6,7 +6,7 @@
* @date 2023-11-22 MacRsh First version
*/
#include "can.h"
#include "include/device/can.h"
#ifdef MR_USING_CAN

View File

@@ -6,7 +6,7 @@
* @date 2023-11-08 MacRsh First version
*/
#include "dac.h"
#include "include/device/dac.h"
#ifdef MR_USING_DAC

View File

@@ -6,7 +6,7 @@
* @date 2023-11-09 MacRsh First version
*/
#include "i2c.h"
#include "include/device/i2c.h"
#ifdef MR_USING_I2C

View File

@@ -6,7 +6,7 @@
* @date 2023-11-08 MacRsh First version
*/
#include "pin.h"
#include "include/device/pin.h"
#ifdef MR_USING_PIN

View File

@@ -6,7 +6,7 @@
* @date 2023-10-20 MacRsh First version
*/
#include "serial.h"
#include "include/device/serial.h"
#ifdef MR_USING_SERIAL

View File

@@ -6,12 +6,12 @@
* @date 2023-11-01 MacRsh First version
*/
#include "spi.h"
#include "include/device/spi.h"
#ifdef MR_USING_SPI
#ifdef MR_USING_PIN
#include "pin.h"
#include "include/device/pin.h"
#else
#warning "Please define MR_USING_PIN. Otherwise SPI-CS will not work."
#endif /* MR_USING_PIN */

View File

@@ -6,7 +6,7 @@
* @date 2023-11-15 MacRsh First version
*/
#include "timer.h"
#include "include/device/timer.h"
#ifdef MR_USING_TIMER
@@ -135,7 +135,8 @@ static ssize_t mr_timer_read(struct mr_dev *dev, int off, void *buf, size_t size
uint32_t count = ops->get_count(timer);
/* Calculate the passed time */
*rd_buf = (timer->reload - timer->count) * timer->timeout + count * timer->timeout / timer->period;
*rd_buf = (timer->reload - timer->count) * timer->timeout +
(uint32_t)(((float)count / (float)timer->period) * (float)timer->timeout);
rd_buf++;
}
return rd_size;

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 129 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

View File

@@ -9,7 +9,7 @@
#ifndef _MR_ADC_H_
#define _MR_ADC_H_
#include "mr_api.h"
#include "include/mr_api.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -9,7 +9,7 @@
#ifndef _CAN_H_
#define _CAN_H_
#include "mr_api.h"
#include "include/mr_api.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -9,7 +9,7 @@
#ifndef _MR_DAC_H_
#define _MR_DAC_H_
#include "mr_api.h"
#include "include/mr_api.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -9,7 +9,7 @@
#ifndef _MR_I2C_H_
#define _MR_I2C_H_
#include "mr_api.h"
#include "include/mr_api.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -9,7 +9,7 @@
#ifndef _MR_PIN_H_
#define _MR_PIN_H_
#include "mr_api.h"
#include "include/mr_api.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -9,7 +9,7 @@
#ifndef _MR_SERIAL_H_
#define _MR_SERIAL_H_
#include "mr_api.h"
#include "include/mr_api.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -9,7 +9,7 @@
#ifndef _MR_SPI_H_
#define _MR_SPI_H_
#include "mr_api.h"
#include "include/mr_api.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -9,12 +9,12 @@
#ifndef _PWM_H_
#define _PWM_H_
#include "mr_api.h"
#include "include/mr_api.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#define MR_USING_TIMER
#ifdef MR_USING_TIMER
/**

View File

@@ -9,42 +9,42 @@
#ifndef _MR_DRV_H_
#define _MR_DRV_H_
#include "mr_config.h"
#include "include/mr_config.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#ifdef MR_USING_ADC
#include "drv_adc.h"
#include "driver/drv_adc.h"
#endif
#ifdef MR_USING_CAN
#include "drv_can.h"
#include "driver/drv_can.h"
#endif
#ifdef MR_USING_DAC
#include "drv_dac.h"
#include "driver/drv_dac.h"
#endif
#ifdef MR_USING_I2C
#include "drv_i2c.h"
#include "driver/drv_i2c.h"
#endif
#ifdef MR_USING_PIN
#include "drv_pin.h"
#include "driver/drv_pin.h"
#endif
#ifdef MR_USING_SERIAL
#include "drv_serial.h"
#include "driver/drv_serial.h"
#endif
#ifdef MR_USING_SPI
#include "drv_spi.h"
#include "driver/drv_spi.h"
#endif
#ifdef MR_USING_TIMER
#include "drv_timer.h"
#include "driver/drv_timer.h"
#endif
#ifdef __cplusplus

View File

@@ -9,7 +9,7 @@
#ifndef _ETASK_H_
#define _ETASK_H_
#include "mr_lib.h"
#include "include/mr_api.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -14,7 +14,7 @@
#error "Please define MR_USING_PIN. Otherwise HX711 will not work."
#else
#include "device/pin.h"
#include "include/device/pin.h"
static void hx711_set_sck(struct mr_hx711 *hx711, uint8_t value)
{

View File

@@ -9,7 +9,7 @@
#ifndef _HX711_H_
#define _HX711_H_
#include "mr_api.h"
#include "include/mr_api.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -9,7 +9,7 @@
#ifndef _ICM20602_H_
#define _ICM20602_H_
#include "mr_api.h"
#include "include/mr_api.h"
#ifdef __cplusplus
extern "C" {
@@ -22,7 +22,7 @@ extern "C" {
#error "Please define MR_USING_SPI. Otherwise ICM20602 will not work."
#else
#include "device/spi.h"
#include "include/device/spi.h"
/**
* @brief ICM20602 acc Range.

View File

@@ -6,7 +6,7 @@
* @date 2023-10-20 MacRsh First version
*/
#include "mr_api.h"
#include "include/mr_api.h"
static struct mr_dev *dev_find_from_list(struct mr_list *list, const char *name)
{

View File

@@ -6,7 +6,7 @@
* @date 2023-10-20 MacRsh First version
*/
#include "mr_api.h"
#include "include/mr_api.h"
static int start(void)
{