diff --git a/.gitignore b/.gitignore deleted file mode 100644 index c6127b3..0000000 --- a/.gitignore +++ /dev/null @@ -1,52 +0,0 @@ -# Prerequisites -*.d - -# Object files -*.o -*.ko -*.obj -*.elf - -# Linker output -*.ilk -*.map -*.exp - -# Precompiled Headers -*.gch -*.pch - -# Libraries -*.lib -*.a -*.la -*.lo - -# Shared objects (inc. Windows DLLs) -*.dll -*.so -*.so.* -*.dylib - -# Executables -*.exe -*.out -*.app -*.i*86 -*.x86_64 -*.hex - -# Debug files -*.dSYM/ -*.su -*.idb -*.pdb - -# Kernel Module Compile Results -*.mod* -*.cmd -.tmp_versions/ -modules.order -Module.symvers -Mkfile.old -dkms.conf diff --git a/LICENSE b/LICENSE deleted file mode 100644 index fb473eb..0000000 --- a/LICENSE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely test_link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - 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. diff --git a/README.md b/README.md deleted file mode 100644 index df157d3..0000000 --- a/README.md +++ /dev/null @@ -1,44 +0,0 @@ -# 背景 - -随着国产微控制器的崛起,市场上的微控制器种类越来越多。然而,以前的微控制器开发往往忽略了整体框架和程序分层,导致更换微控制器型号往往需要更改应用层代码,这使得开发工作变得繁重且乏味。常见的开发方式大多分为两种: -常见的开发方式通常分为两种:裸机编程和RTOS编程,由于两种方式的代码编写方式存在巨大差异,因此在两种方式之间切换意味着需要进行大规模的工程修改。 -**mr-library** 的目标是帮助开发者提高开发效率和代码通用性,降低平台迁移的难度。 - - ---------- - -# mr-library 简介 - -**mr-library** 是一个嵌入式软件库,完全采用C语言编写,使用面向对象的设计方法,代码框架清晰,可以快速移植到不同的平台。它包括以下部分: - -* 内核层: **mr-library** 的核心部分,包含容器、对象、管理器等。将各种对象注册到内核维护的容器中,使得应用更加高效有序。 -* 设备框架层: 提供统一的设备接口,将不同的设备接入到内核中。在应用层,仅需调用内核设备I/O接口即可访问设备。 -* 硬件驱动层: 为设备框架层设备提供必要的驱动,当硬件更换时仅修改驱动层。 -* 组件层: 通过内核提供的API实现不同的功能。包括但不限于虚拟文件系统、通用传感器模块、网络框架等。 - - ---------- - -# 代码目录 - -**mr-library** 的代码目录结构如下表所示: - -| 名称 | 描述 | -|:---------|:-------| -| bsp | 板级支持包 | -| config | 配置文件 | -| device | 设备框架文件 | -| document | 文档 | -| driver | 硬件驱动文件 | -| include | 库头文件 | -| src | 库源文件 | - - ---------- - -# 许可协议 - -遵循 **Apache License 2.0** 开源许可协议,可免费应用于商业产品,无需公开私有代码。 - - ---------- - -# 贡献代码 - -如果您对 **mr-library** 项目感兴趣,欢迎参与开发并成为代码贡献者。欢迎加入讨论群 **199915649(QQ)** 分享您的观点。 \ No newline at end of file diff --git a/bsp/wch/driver/drv_gpio.c b/bsp/wch/driver/drv_gpio.c deleted file mode 100644 index 1055e00..0000000 --- a/bsp/wch/driver/drv_gpio.c +++ /dev/null @@ -1,312 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-27 MacRsh first version - */ - -#include "drv_gpio.h" - -#undef LOG_TAG -#define LOG_TAG "drv_gpio" - -#if (MR_CONF_DEVICE_PIN == MR_CONF_ENABLE) - -#define PIN_PORT(pin) ((uint8_t)(((pin) >> 4) & 0x0Fu)) -#define PIN_STPORT(pin) ((GPIO_TypeDef *)(GPIOA_BASE + (0x400u * PIN_PORT(pin)))) -#define PIN_STPIN(pin) ((uint16_t)(1u << (mr_uint8_t)(pin & 0x0Fu))) -#define PIN_RCC(pin) (RCC_APB2Periph_GPIOA << PIN_PORT(pin)) - -static IRQn_Type irqno[] = - { - EXTI0_IRQn, - EXTI1_IRQn, - EXTI2_IRQn, - EXTI3_IRQn, - EXTI4_IRQn, - EXTI9_5_IRQn, - EXTI9_5_IRQn, - EXTI9_5_IRQn, - EXTI9_5_IRQn, - EXTI9_5_IRQn, - EXTI15_10_IRQn, - EXTI15_10_IRQn, - EXTI15_10_IRQn, - EXTI15_10_IRQn, - EXTI15_10_IRQn, - EXTI15_10_IRQn, - }; - -static mr_int16_t mask[16] = {- 1, - - 1, - - 1, - - 1, - - 1, - - 1, - - 1, - - 1, - - 1, - - 1, - - 1, - - 1, - - 1, - - 1, - - 1, - - 1}; - -static struct mr_pin hw_pin; - -static mr_err_t ch32_pin_configure(mr_pin_t pin, struct mr_pin_config *config) -{ - GPIO_InitTypeDef GPIO_InitStruct; - EXTI_InitTypeDef EXTI_InitStructure = {0}; - NVIC_InitTypeDef NVIC_InitStructure = {0}; - - RCC_APB2PeriphClockCmd(PIN_RCC(config->number), ENABLE); - - /* Configure GPIO_InitStructure */ - GPIO_InitStruct.GPIO_Pin = PIN_STPIN(config->number); - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; - GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; - - switch (config->mode) - { - case MR_PIN_MODE_OUTPUT: - { - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; - break; - } - - case MR_PIN_MODE_OUTPUT_OD: - { - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD; - break; - } - - case MR_PIN_MODE_INPUT: - { - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; - break; - } - - case MR_PIN_MODE_INPUT_DOWN: - { - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD; - break; - } - - case MR_PIN_MODE_INPUT_UP: - { - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; - break; - } - - case MR_PIN_MODE_RISING: - { - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD; - EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; - break; - } - - case MR_PIN_MODE_FALLING: - { - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; - EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; - break; - } - - case MR_PIN_MODE_EDGE: - { - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; - EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; - break; - } - - default: return - MR_ERR_GENERIC; - } - - if (config->mode >= MR_PIN_MODE_RISING) - { - if ((mask[config->number % 16] != - 1 && mask[config->number % 16] != config->number)) - { - MR_LOG_E(LOG_TAG, "pin exit-line %d busy\r\n", config->number % 16); - return - MR_ERR_BUSY; - } - - mask[config->number % 16] = config->number; - - RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); - - EXTI_InitStructure.EXTI_Line = PIN_STPIN(config->number); - EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; - EXTI_InitStructure.EXTI_LineCmd = ENABLE; - - GPIO_EXTILineConfig(PIN_PORT(config->number), config->number % 16); - EXTI_Init(&EXTI_InitStructure); - - NVIC_InitStructure.NVIC_IRQChannel = irqno[config->number % 16]; - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; - NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; - NVIC_Init(&NVIC_InitStructure); - } else if (config->number == mask[config->number % 16]) - { - if (config->number % 16 >= 5 && config->number % 16 <= 9) - { - if (mask[5] == - 1 && mask[6] == - 1 && mask[7] == - 1 && mask[8] == - 1 && mask[9] == - 1) - { - EXTI_InitStructure.EXTI_LineCmd = DISABLE; - } else - { - EXTI_InitStructure.EXTI_LineCmd = ENABLE; - } - - } else if (config->number % 16 >= 10 && config->number % 16 <= 15) - { - if (mask[10] == - 1 && mask[11] == - 1 && mask[12] == - 1 && mask[13] == - 1 && mask[14] == - 1 - && mask[15] == - 1) - { - EXTI_InitStructure.EXTI_LineCmd = DISABLE; - } else - { - EXTI_InitStructure.EXTI_LineCmd = ENABLE; - } - } else - { - EXTI_InitStructure.EXTI_LineCmd = DISABLE; - } - - mask[config->number % 16] = - 1; - - EXTI_InitStructure.EXTI_Line = PIN_STPIN(config->number); - EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; - - GPIO_EXTILineConfig(PIN_PORT(config->number), config->number % 16); - EXTI_Init(&EXTI_InitStructure); - } - - GPIO_Init(PIN_STPORT(config->number), &GPIO_InitStruct); - - MR_LOG_D(LOG_TAG, "Config %s %d %d\r\n", pin->device.object.name, config->number, config->mode); - - return MR_ERR_OK; -} - -static void ch32_pin_write(mr_pin_t pin, mr_uint16_t number, mr_uint8_t value) -{ - GPIO_WriteBit(PIN_STPORT(number), PIN_STPIN(number), value); -} - -static mr_uint8_t ch32_pin_read(mr_pin_t pin, mr_uint16_t number) -{ - return GPIO_ReadInputDataBit(PIN_STPORT(number), PIN_STPIN(number)); -} - -void EXTI0_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); -void EXTI0_IRQHandler(void) -{ - if (EXTI_GetITStatus(EXTI_Line0) != RESET) - { - mr_hw_pin_isr(&hw_pin, 0); - EXTI_ClearITPendingBit(EXTI_Line0); - } -} - -void EXTI1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); -void EXTI1_IRQHandler(void) -{ - if (EXTI_GetITStatus(EXTI_Line1) != RESET) - { - mr_hw_pin_isr(&hw_pin, 1); - EXTI_ClearITPendingBit(EXTI_Line1); - } -} - -void EXTI2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); -void EXTI2_IRQHandler(void) -{ - if (EXTI_GetITStatus(EXTI_Line2) != RESET) - { - mr_hw_pin_isr(&hw_pin, 2); - EXTI_ClearITPendingBit(EXTI_Line2); - } -} - -void EXTI3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); -void EXTI3_IRQHandler(void) -{ - if (EXTI_GetITStatus(EXTI_Line3) != RESET) - { - mr_hw_pin_isr(&hw_pin, 3); - EXTI_ClearITPendingBit(EXTI_Line3); - } -} - -void EXTI4_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); -void EXTI4_IRQHandler(void) -{ - if (EXTI_GetITStatus(EXTI_Line4) != RESET) - { - mr_hw_pin_isr(&hw_pin, 4); - EXTI_ClearITPendingBit(EXTI_Line4); - } -} - -void EXTI9_5_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); -void EXTI9_5_IRQHandler(void) -{ - if ((EXTI_GetITStatus(EXTI_Line5) != RESET) - || (EXTI_GetITStatus(EXTI_Line6) != RESET) - || (EXTI_GetITStatus(EXTI_Line7) != RESET) - || (EXTI_GetITStatus(EXTI_Line8) != RESET) - || (EXTI_GetITStatus(EXTI_Line9) != RESET)) - { - mr_hw_pin_isr(&hw_pin, 5); - mr_hw_pin_isr(&hw_pin, 6); - mr_hw_pin_isr(&hw_pin, 7); - mr_hw_pin_isr(&hw_pin, 8); - mr_hw_pin_isr(&hw_pin, 9); - EXTI_ClearITPendingBit(EXTI_Line5 | EXTI_Line6 | EXTI_Line7 | EXTI_Line8 | EXTI_Line9); - } -} - -void EXTI15_10_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); -void EXTI15_10_IRQHandler(void) -{ - if ((EXTI_GetITStatus(EXTI_Line10) != RESET) - || (EXTI_GetITStatus(EXTI_Line11) != RESET) - || (EXTI_GetITStatus(EXTI_Line12) != RESET) - || (EXTI_GetITStatus(EXTI_Line13) != RESET) - || (EXTI_GetITStatus(EXTI_Line14) != RESET) - || (EXTI_GetITStatus(EXTI_Line15) != RESET)) - { - mr_hw_pin_isr(&hw_pin, 10); - mr_hw_pin_isr(&hw_pin, 11); - mr_hw_pin_isr(&hw_pin, 12); - mr_hw_pin_isr(&hw_pin, 13); - mr_hw_pin_isr(&hw_pin, 14); - EXTI_ClearITPendingBit(EXTI_Line10 | EXTI_Line11 | EXTI_Line12 | EXTI_Line13 | EXTI_Line14 | EXTI_Line15); - } -} - -mr_err_t mr_hw_gpio_init(void) -{ - mr_err_t ret = MR_ERR_OK; - static struct mr_pin_ops ops = - { - ch32_pin_configure, - ch32_pin_write, - ch32_pin_read, - }; - - ret = mr_hw_pin_add(&hw_pin, "pin", &ops, MR_NULL); - MR_ASSERT(ret == MR_ERR_OK); - - return MR_ERR_OK; -} -INIT_BOARD_EXPORT(mr_hw_gpio_init); - -#endif \ No newline at end of file diff --git a/bsp/wch/driver/drv_gpio.h b/bsp/wch/driver/drv_gpio.h deleted file mode 100644 index 2249e13..0000000 --- a/bsp/wch/driver/drv_gpio.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-27 MacRsh first version - */ - -#ifndef _DRV_GPIO_H_ -#define _DRV_GPIO_H_ - -#include "device/pin/pin.h" - -#if (MR_CONF_DEVICE_PIN == MR_CONF_ENABLE) - -mr_err_t mr_hw_gpio_init(void); - -#endif - -#endif \ No newline at end of file diff --git a/bsp/wch/driver/drv_spi.c b/bsp/wch/driver/drv_spi.c deleted file mode 100644 index 8cb2b79..0000000 --- a/bsp/wch/driver/drv_spi.c +++ /dev/null @@ -1,252 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-05-16 MacRsh first version - */ - -#include "drv_spi.h" - -#undef LOG_TAG -#define LOG_TAG "drv_spi" - -#if (MR_CONF_DEVICE_SPI == MR_CONF_ENABLE) - -#define PIN_PORT(pin) ((uint8_t)(((pin) >> 4) & 0x0Fu)) -#define PIN_STPORT(pin) ((GPIO_TypeDef *)(GPIOA_BASE + (0x400u * PIN_PORT(pin)))) -#define PIN_STPIN(pin) ((uint16_t)(1u << (mr_uint8_t)(pin & 0x0Fu))) - -enum -{ -#ifdef BSP_SPI_1 - SPI1_INDEX, -#endif -#ifdef BSP_SPI_2 - SPI2_INDEX, -#endif -#ifdef BSP_SPI_3 - SPI3_INDEX, -#endif -}; - -static struct ch32_spi ch32_spi[] = - { -#ifdef BSP_SPI_1 - {"spi1", - {SPI1, - RCC_APB2Periph_SPI1, - RCC_APB2Periph_GPIOA, - GPIOA, - GPIO_Pin_5, - GPIO_Pin_6, - GPIO_Pin_7}}, -#endif -#ifdef BSP_SPI_2 - {"spi2", - {SPI2, - RCC_APB1Periph_SPI2, - RCC_APB2Periph_GPIOB, - GPIOB, - GPIO_Pin_13, - GPIO_Pin_14, - GPIO_Pin_15}}, -#endif -#ifdef BSP_SPI_3 - {"spi3", - {SPI3, - RCC_APB1Periph_SPI3, - RCC_APB2Periph_GPIOB, - GPIOB, - GPIO_Pin_3, - GPIO_Pin_4, - GPIO_Pin_5}}, -#endif - }; - -static struct mr_spi_bus hw_spi[mr_array_get_length(ch32_spi)]; - -static mr_uint16_t ch32_spi_baud_rate_prescaler(mr_uint32_t pclk_freq, mr_uint32_t baud_rate) -{ - mr_uint16_t psc = pclk_freq / baud_rate; - - if (psc >= 256) return SPI_BaudRatePrescaler_256; - else if (psc >= 128) return SPI_BaudRatePrescaler_128; - else if (psc >= 64) return SPI_BaudRatePrescaler_64; - else if (psc >= 32) return SPI_BaudRatePrescaler_32; - else if (psc >= 16) return SPI_BaudRatePrescaler_16; - else if (psc >= 8) return SPI_BaudRatePrescaler_8; - else if (psc >= 4) return SPI_BaudRatePrescaler_4; - else if (psc >= 2) return SPI_BaudRatePrescaler_2; - return SPI_BaudRatePrescaler_2; -} - -static mr_err_t ch32_spi_configure(mr_spi_bus_t spi_bus, struct mr_spi_config *config) -{ - struct ch32_spi *hw = (struct ch32_spi *)spi_bus->device.data; - GPIO_InitTypeDef GPIO_InitStructure = {0}; - SPI_InitTypeDef SPI_InitStructure = {0}; - RCC_ClocksTypeDef RCC_ClockStruct = {0}; - mr_uint32_t pclk_freq = 0; - - RCC_GetClocksFreq(&RCC_ClockStruct); - - if ((uint32_t)hw->hw_spi.Instance > APB2PERIPH_BASE) - { - RCC_APB2PeriphClockCmd(hw->hw_spi.spi_periph_clock, ENABLE); - pclk_freq = RCC_ClockStruct.PCLK2_Frequency; - } else - { - RCC_APB1PeriphClockCmd(hw->hw_spi.spi_periph_clock, ENABLE); - pclk_freq = RCC_ClockStruct.PCLK1_Frequency; - } - RCC_APB2PeriphClockCmd(hw->hw_spi.gpio_periph_clock, ENABLE); - - SPI_InitStructure.SPI_BaudRatePrescaler = ch32_spi_baud_rate_prescaler(pclk_freq, config->baud_rate); - SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; - SPI_InitStructure.SPI_CRCPolynomial = 7; - SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; - - if (config->cs_active == MR_SPI_CS_ACTIVE_NONE) - { - SPI_InitStructure.SPI_NSS = SPI_NSS_Hard; - } else - { - SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; - } - - switch (config->mode) - { - case MR_SPI_MODE_0: SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; - SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; - break; - case MR_SPI_MODE_1: SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; - SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; - break; - case MR_SPI_MODE_2: SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; - SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; - break; - case MR_SPI_MODE_3: SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; - SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; - break; - default: SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; - SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; - break; - } - - switch (config->bit_order) - { - case MR_SPI_BIT_ORDER_LSB: SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB; - break; - case MR_SPI_BIT_ORDER_MSB: SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; - break; - default: SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; - break; - } - - if (config->host_slave == MR_SPI_HOST) - { - SPI_InitStructure.SPI_Mode = SPI_Mode_Master; - - GPIO_InitStructure.GPIO_Pin = hw->hw_spi.clk_gpio_pin; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_Init(hw->hw_spi.gpio_port, &GPIO_InitStructure); - - GPIO_InitStructure.GPIO_Pin = hw->hw_spi.miso_gpio_pin; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; - GPIO_Init(hw->hw_spi.gpio_port, &GPIO_InitStructure); - - GPIO_InitStructure.GPIO_Pin = hw->hw_spi.mosi_gpio_pin; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_Init(hw->hw_spi.gpio_port, &GPIO_InitStructure); - } else - { - SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; - - GPIO_InitStructure.GPIO_Pin = hw->hw_spi.clk_gpio_pin; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; - GPIO_Init(hw->hw_spi.gpio_port, &GPIO_InitStructure); - - GPIO_InitStructure.GPIO_Pin = hw->hw_spi.miso_gpio_pin; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_Init(hw->hw_spi.gpio_port, &GPIO_InitStructure); - - GPIO_InitStructure.GPIO_Pin = hw->hw_spi.mosi_gpio_pin; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; - GPIO_Init(hw->hw_spi.gpio_port, &GPIO_InitStructure); - } - - SPI_Init(hw->hw_spi.Instance, &SPI_InitStructure); - SPI_Cmd(hw->hw_spi.Instance, ENABLE); - - MR_LOG_D(LOG_TAG, - "Config %s %d %d %d\r\n", - hw->name, - config->baud_rate, - config->mode, - config->bit_order); - - return MR_ERR_OK; -} - -static void ch32_spi_write(mr_spi_bus_t spi_bus, mr_uint8_t data) -{ - struct ch32_spi *hw = (struct ch32_spi *)spi_bus->device.data; - - while (SPI_I2S_GetFlagStatus(hw->hw_spi.Instance, SPI_I2S_FLAG_TXE) == RESET); - hw->hw_spi.Instance->DATAR = data; -} - -static mr_uint8_t ch32_spi_read(mr_spi_bus_t spi_bus) -{ - struct ch32_spi *hw = (struct ch32_spi *)spi_bus->device.data; - mr_size_t count = 0; - - while (SPI_I2S_GetFlagStatus(hw->hw_spi.Instance, SPI_I2S_FLAG_RXNE) == RESET) - { - count ++; - if (count > 200) return 0; - } - - return hw->hw_spi.Instance->DATAR; -} - -static void ch32_spi_cs_set(mr_spi_bus_t spi_bus, mr_uint16_t cs_pin, mr_state_t state) -{ - if (state == MR_ENABLE) - { - GPIO_WriteBit(PIN_STPORT(cs_pin), PIN_STPIN(cs_pin), spi_bus->config.cs_active); - } else - { - GPIO_WriteBit(PIN_STPORT(cs_pin), PIN_STPIN(cs_pin), ! spi_bus->config.cs_active); - } -} - -mr_err_t mr_hw_spi_init(void) -{ - mr_err_t ret = MR_ERR_OK; - mr_size_t count = mr_array_get_length(hw_spi); - static struct mr_spi_bus_ops ops = - { - ch32_spi_configure, - ch32_spi_write, - ch32_spi_read, - ch32_spi_cs_set, - }; - - while (count --) - { - ret = mr_hw_spi_bus_add(&hw_spi[count], ch32_spi[count].name, &ops, &ch32_spi[count]); - MR_ASSERT(ret == MR_ERR_OK); - } - - return MR_ERR_OK; -} -INIT_BOARD_EXPORT(mr_hw_spi_init); - -#endif \ No newline at end of file diff --git a/bsp/wch/driver/drv_spi.h b/bsp/wch/driver/drv_spi.h deleted file mode 100644 index 4e9ef3b..0000000 --- a/bsp/wch/driver/drv_spi.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-05-16 MacRsh first version - */ - -#ifndef _DRV_SPI_H_ -#define _DRV_SPI_H_ - -#include "device/spi/spi.h" - -#if (MR_CONF_DEVICE_SPI == MR_CONF_ENABLE) - -struct ch32_hw_spi -{ - SPI_TypeDef *Instance; - - mr_uint32_t spi_periph_clock; - mr_uint32_t gpio_periph_clock; - GPIO_TypeDef *gpio_port; - mr_uint16_t clk_gpio_pin; - mr_uint16_t miso_gpio_pin; - mr_uint16_t mosi_gpio_pin; -}; - - -struct ch32_spi -{ - char *name; - - struct ch32_hw_spi hw_spi; -}; - -mr_err_t mr_hw_spi_init(void); - -#endif - -#endif \ No newline at end of file diff --git a/bsp/wch/driver/drv_uart.c b/bsp/wch/driver/drv_uart.c deleted file mode 100644 index 1190bfe..0000000 --- a/bsp/wch/driver/drv_uart.c +++ /dev/null @@ -1,398 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#include "drv_uart.h" - -#undef LOG_TAG -#define LOG_TAG "drv_uart" - -#if (MR_CONF_DEVICE_SERIAL == MR_CONF_ENABLE) - -enum -{ -#ifdef BSP_UART_1 - UART1_INDEX, -#endif -#ifdef BSP_UART_2 - UART2_INDEX, -#endif -#ifdef BSP_UART_3 - UART3_INDEX, -#endif -#ifdef BSP_UART_4 - UART4_INDEX, -#endif -#ifdef BSP_UART_5 - UART5_INDEX, -#endif -#ifdef BSP_UART_6 - UART6_INDEX, -#endif -#ifdef BSP_UART_7 - UART7_INDEX, -#endif -#ifdef BSP_UART_8 - UART8_INDEX, -#endif -}; - -static struct ch32_uart ch32_uart[] = - { -#ifdef BSP_UART_1 - {"uart1", - {USART1, - RCC_APB2Periph_USART1, - RCC_APB2Periph_GPIOA, - GPIOA, - GPIO_Pin_9, - GPIOA, - GPIO_Pin_10, - CH32_UART_GPIO_REMAP_NONE, - USART1_IRQn}}, -#endif -#ifdef BSP_UART_2 - {"uart2", - {USART2, - RCC_APB1Periph_USART2, - RCC_APB2Periph_GPIOA, - GPIOA, - GPIO_Pin_2, - GPIOA, - GPIO_Pin_3, - CH32_UART_GPIO_REMAP_NONE, - USART2_IRQn}}, -#endif -#ifdef BSP_UART_3 - {"uart3", - {USART3, - RCC_APB1Periph_USART2, - RCC_APB2Periph_GPIOB, - GPIOB, - GPIO_Pin_10, - GPIOB, - GPIO_Pin_11, - CH32_UART_GPIO_REMAP_NONE, - USART3_IRQn}}, -#endif -#ifdef BSP_UART_4 - {"uart4", - {UART4, - RCC_APB1Periph_UART4, - RCC_APB2Periph_GPIOC, - GPIOC, - GPIO_Pin_10, - GPIOC, - GPIO_Pin_11, - CH32_UART_GPIO_REMAP_NONE, - UART4_IRQn}}, -#endif -#ifdef BSP_UART_5 - {"uart5", - {UART5, - RCC_APB1Periph_UART5, - RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, - GPIOC, - GPIO_Pin_12, - GPIOD, - GPIO_Pin_2, - CH32_UART_GPIO_REMAP_NONE, - UART5_IRQn}}, -#endif -#ifdef BSP_UART_6 - {"uart6", - {UART6, - RCC_APB1Periph_UART6, - RCC_APB2Periph_GPIOC, - GPIOC, - GPIO_Pin_0, - GPIOC, - GPIO_Pin_1, - CH32_UART_GPIO_REMAP_NONE, - UART6_IRQn}}, -#endif -#ifdef BSP_UART_7 - {"uart7", - {UART7, - RCC_APB1Periph_UART7, - RCC_APB2Periph_GPIOC, - GPIOC, - GPIO_Pin_2, - GPIOC, - GPIO_Pin_3, - CH32_UART_GPIO_REMAP_NONE, - UART7_IRQn}}, -#endif -#ifdef BSP_UART_8 - {"uart8", - {UART8, - RCC_APB1Periph_UART8, - RCC_APB2Periph_GPIOC, - GPIOC, - GPIO_Pin_4, - GPIOC, - GPIO_Pin_5, - CH32_UART_GPIO_REMAP_NONE, - UART8_IRQn}}, -#endif - }; - -static struct mr_serial hw_serial[mr_array_get_length(ch32_uart)]; - -static mr_err_t ch32_serial_configure(mr_serial_t serial, struct mr_serial_config *config) -{ - struct ch32_uart *hw = (struct ch32_uart *)serial->device.data; - GPIO_InitTypeDef GPIO_InitStructure = {0}; - NVIC_InitTypeDef NVIC_InitStructure = {0}; - USART_InitTypeDef USART_InitStructure = {0}; - - /* Close the uart */ - if (config->baud_rate == 0) - { - if (hw->hw_uart.Instance == USART1) - { - RCC_APB2PeriphClockCmd(hw->hw_uart.uart_periph_clock, DISABLE); - } else - { - RCC_APB1PeriphClockCmd(hw->hw_uart.uart_periph_clock, DISABLE); - } - RCC_APB2PeriphClockCmd(hw->hw_uart.gpio_periph_clock, DISABLE); - - return MR_ERR_OK; - } - - /* UART and GPIO clk enable*/ - if (hw->hw_uart.Instance == USART1) - { - RCC_APB2PeriphClockCmd(hw->hw_uart.uart_periph_clock, ENABLE); - } else - { - RCC_APB1PeriphClockCmd(hw->hw_uart.uart_periph_clock, ENABLE); - } - RCC_APB2PeriphClockCmd(hw->hw_uart.gpio_periph_clock, ENABLE); - - USART_InitStructure.USART_BaudRate = config->baud_rate; - USART_InitStructure.USART_HardwareFlowControl = 0; - USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; - - /* Set data-bits */ - switch (config->data_bits) - { - case MR_SERIAL_DATA_BITS_8:USART_InitStructure.USART_WordLength = USART_WordLength_8b; - break; - case MR_SERIAL_DATA_BITS_9:USART_InitStructure.USART_WordLength = USART_WordLength_9b; - break; - default:USART_InitStructure.USART_WordLength = USART_WordLength_8b; - break; - } - - /* Set stop-bits */ - switch (config->stop_bits) - { - case MR_SERIAL_STOP_BITS_1:USART_InitStructure.USART_StopBits = USART_StopBits_1; - break; - case MR_SERIAL_STOP_BITS_2:USART_InitStructure.USART_StopBits = USART_StopBits_2; - break; - default:USART_InitStructure.USART_StopBits = USART_StopBits_1; - break; - } - - /* Set parity */ - switch (config->parity) - { - case MR_SERIAL_PARITY_NONE:USART_InitStructure.USART_Parity = USART_Parity_No; - break; - case MR_SERIAL_PARITY_ODD:USART_InitStructure.USART_Parity = USART_Parity_Odd; - break; - case MR_SERIAL_PARITY_EVEN:USART_InitStructure.USART_Parity = USART_Parity_Even; - break; - default:USART_InitStructure.USART_Parity = USART_Parity_No; - break; - } - - /* Set GPIO-remap */ - if (hw->hw_uart.remap != CH32_UART_GPIO_REMAP_NONE) - { - RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); - GPIO_PinRemapConfig(hw->hw_uart.remap, ENABLE); - } - - /* Tx-GPIO */ - GPIO_InitStructure.GPIO_Pin = hw->hw_uart.tx_gpio_pin; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; - GPIO_Init(hw->hw_uart.tx_gpio_port, &GPIO_InitStructure); - - /* Rx-GPIO */ - GPIO_InitStructure.GPIO_Pin = hw->hw_uart.rx_gpio_pin; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; - GPIO_Init(hw->hw_uart.rx_gpio_port, &GPIO_InitStructure); - - /* Interrupt enable */ - NVIC_InitStructure.NVIC_IRQChannel = hw->hw_uart.irqno; - NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; - NVIC_Init(&NVIC_InitStructure); - USART_ITConfig(hw->hw_uart.Instance, USART_IT_RXNE, ENABLE); - - /* UART enable */ - USART_Init(hw->hw_uart.Instance, &USART_InitStructure); - USART_Cmd(hw->hw_uart.Instance, ENABLE); - - MR_LOG_D(LOG_TAG, - "Config %s %d %d %d\r\n", - hw->name, - config->baud_rate, - config->data_bits, - config->stop_bits); - - return MR_ERR_OK; -} - -static void ch32_serial_write(mr_serial_t serial, mr_uint8_t data) -{ - struct ch32_uart *hw = (struct ch32_uart *)serial->device.data; - - while (USART_GetFlagStatus(hw->hw_uart.Instance, USART_FLAG_TC) == RESET); - hw->hw_uart.Instance->DATAR = data; -} - -static mr_uint8_t ch32_serial_read(mr_serial_t serial) -{ - struct ch32_uart *hw = (struct ch32_uart *)serial->device.data; - - if (USART_GetFlagStatus(hw->hw_uart.Instance, USART_FLAG_RXNE) != RESET) - { - return hw->hw_uart.Instance->DATAR & 0xff; - } - - return 0; -} - -static void ch32_serial_start_tx(mr_serial_t serial) -{ - struct ch32_uart *hw = (struct ch32_uart *)serial->device.data; - - USART_ITConfig(hw->hw_uart.Instance, USART_IT_TXE, ENABLE); -} - -static void ch32_serial_stop_tx(mr_serial_t serial) -{ - struct ch32_uart *hw = (struct ch32_uart *)serial->device.data; - - USART_ITConfig(hw->hw_uart.Instance, USART_IT_TXE, DISABLE); -} - -static void ch32_serial_isr(mr_serial_t serial) -{ - struct ch32_uart *hw = (struct ch32_uart *)serial->device.data; - - if (USART_GetITStatus(hw->hw_uart.Instance, USART_IT_RXNE) != RESET) - { - mr_hw_serial_isr(serial, MR_SERIAL_EVENT_RX_INT); - USART_ClearITPendingBit(hw->hw_uart.Instance, USART_IT_RXNE); - } - - if (USART_GetITStatus(hw->hw_uart.Instance, USART_IT_TXE) != RESET) - { - mr_hw_serial_isr(serial, MR_SERIAL_EVENT_TX_INT); - USART_ClearITPendingBit(hw->hw_uart.Instance, USART_IT_TXE); - } -} - -#ifdef BSP_UART_1 -void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); -void USART1_IRQHandler(void) -{ - ch32_serial_isr(&hw_serial[UART1_INDEX]); -} -#endif - -#ifdef BSP_UART_2 -void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); -void USART2_IRQHandler(void) -{ - ch32_serial_isr(&hw_serial[UART2_INDEX]); -} -#endif - -#ifdef BSP_UART_3 -void USART3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); -void USART3_IRQHandler(void) -{ - ch32_serial_isr(&hw_serial[UART3_INDEX]); -} -#endif - -#ifdef BSP_UART_4 -void UART4_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); -void UART4_IRQHandler(void) -{ - ch32_serial_isr(&hw_serial[UART4_INDEX]); -} -#endif - -#ifdef BSP_UART_5 -void UART5_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); -void UART5_IRQHandler(void) -{ - ch32_serial_isr(&hw_serial[UART5_INDEX]); -} -#endif - -#ifdef BSP_UART_6 -void UART6_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); -void UART6_IRQHandler(void) -{ - ch32_serial_isr(&hw_serial[UART6_INDEX]); -} -#endif - -#ifdef BSP_UART_7 -void UART7_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); -void UART7_IRQHandler(void) -{ - ch32_serial_isr(&hw_serial[UART7_INDEX]); -} -#endif - -#ifdef BSP_UART_8 -void UART8_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); -void UART8_IRQHandler(void) -{ - ch32_serial_isr(&hw_serial[UART8_INDEX]); -} -#endif - -mr_err_t mr_hw_uart_init(void) -{ - mr_err_t ret = MR_ERR_OK; - mr_size_t count = mr_array_get_length(hw_serial); - static struct mr_serial_ops ops = - { - ch32_serial_configure, - ch32_serial_write, - ch32_serial_read, - ch32_serial_start_tx, - ch32_serial_stop_tx, - }; - - while (count --) - { - ret = mr_hw_serial_add(&hw_serial[count], ch32_uart[count].name, &ops, &ch32_uart[count]); - MR_ASSERT(ret == MR_ERR_OK); - } - - return MR_ERR_OK; -} -INIT_BOARD_EXPORT(mr_hw_uart_init); - -#endif \ No newline at end of file diff --git a/bsp/wch/driver/drv_uart.h b/bsp/wch/driver/drv_uart.h deleted file mode 100644 index bace753..0000000 --- a/bsp/wch/driver/drv_uart.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#ifndef _DRV_UART_H_ -#define _DRV_UART_H_ - -#include "device/serial/serial.h" - -#if (MR_CONF_DEVICE_SERIAL == MR_CONF_ENABLE) - -#define CH32_UART_GPIO_REMAP_NONE 0 - -struct ch32_hw_uart -{ - USART_TypeDef *Instance; - - mr_uint32_t uart_periph_clock; - mr_uint32_t gpio_periph_clock; - GPIO_TypeDef *tx_gpio_port; - mr_uint16_t tx_gpio_pin; - GPIO_TypeDef *rx_gpio_port; - mr_uint16_t rx_gpio_pin; - mr_uint32_t remap; - - IRQn_Type irqno; -}; - -struct ch32_uart -{ - char *name; - - struct ch32_hw_uart hw_uart; -}; - -mr_err_t mr_hw_uart_init(void); - -#endif - -#endif \ No newline at end of file diff --git a/config/board.c b/config/board.c deleted file mode 100644 index 92be5a5..0000000 --- a/config/board.c +++ /dev/null @@ -1,16 +0,0 @@ -#include "board.h" - -void mr_hw_interrupt_disable(void) -{ - -} - -void mr_hw_interrupt_enable(void) -{ - -} - -void mr_hw_delay_ms(unsigned int ms) -{ - -} \ No newline at end of file diff --git a/config/board.h b/config/board.h deleted file mode 100644 index 7df1763..0000000 --- a/config/board.h +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef _BOARD_H_ -#define _BOARD_H_ - -//<---------------------------- Hardware --------------------------------------> -//<<< Please include bsp head file >>> - - -//<<< Please give your bsp a name >>> -#define BSP_NAME "CH32V303RBT6" - -//<<< Bsp system frequency >>> -#define BSP_SYSCLK_FREQ SystemCoreClock - -//<--------------------------- BSP-GPIO -------------------------------------> -//<<< Bsp using GPIOx >>> -#define BSP_GPIO_A -#define BSP_GPIO_B -#define BSP_GPIO_C -#define BSP_GPIO_D -#define BSP_GPIO_E - -//<--------------------------- BSP-UART -------------------------------------> -//<<< Bsp using UARTx >>> -#define BSP_UART_1 -#define BSP_UART_2 -#define BSP_UART_3 -#define BSP_UART_4 -#define BSP_UART_5 -#define BSP_UART_6 - -//<--------------------------- BSP-SPI --------------------------------------> -//<<< Bsp using SPIx >>> -#define BSP_SPI_1 -#define BSP_SPI_2 -#define BSP_SPI_3 -#define BSP_SPI_4 - -//<--------------------------- BSP-I2C --------------------------------------> -//<<< Bsp using I2Cx >>> -#define BSP_I2C_1 -#define BSP_I2C_2 - -//<--------------------------- BSP-ADC --------------------------------------> -//<<< Bsp using ADCx >>> -#define BSP_ADC_1 -#define BSP_ADC_2 -#define BSP_ADC_3 -#define BSP_ADC_4 - -//<--------------------------- BSP-DAC --------------------------------------> -//<<< Bsp using DACx >>> -#define BSP_DAC_1 -#define BSP_DAC_2 -#define BSP_DAC_3 - -//<--------------------------- BSP-TIMER ------------------------------------> -//<<< Bsp using TIMERx >>> -#define BSP_TIMER_1 -#define BSP_TIMER_2 -#define BSP_TIMER_3 -#define BSP_TIMER_4 - -#endif diff --git a/config/mrconfig.h b/config/mrconfig.h deleted file mode 100644 index f63217c..0000000 --- a/config/mrconfig.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef _MR_CONFIG_H_ -#define _MR_CONFIG_H_ - -#include "board.h" - -//<<< Config switch >>> -#define MR_CONF_DISABLE 0 -#define MR_CONF_ENABLE 1 - -//<<< Log level >>> -#define MR_CONF_LOG_LEVEL_ASSERT 0 -#define MR_CONF_LOG_LEVEL_ERROR 1 -#define MR_CONF_LOG_LEVEL_WARNING 2 -#define MR_CONF_LOG_LEVEL_INFO 3 -#define MR_CONF_LOG_LEVEL_DEBUG 4 - -//<---------------------------- Kernel --------------------------------------> -#define MR_CONF_NAME_MAX 12 - -//<<< Log >>> -#define MR_CONF_LOG_OUTPUT MR_CONF_DISABLE -#define MR_CONF_LOG_BUFSZ 128 -#define MR_CONF_LOG_ASSERT MR_CONF_ENABLE -#define MR_CONF_LOG_LEVEL MR_CONF_LOG_LEVEL_DEBUG - -//<<< Manager >>> -#define MR_CONF_MANAGER MR_CONF_DISABLE -#define MR_CONF_MANAGER_PARSER_BUFSZ 16 - -//<<< Device >>> -#define MR_CONF_DEVICE MR_CONF_DISABLE - -//<---------------------------- Device --------------------------------------> -//<<< GPIO >>> -#define MR_CONF_DEVICE_PIN MR_CONF_DISABLE - -//<<< SERIAL >>> -#define MR_CONF_DEVICE_SERIAL MR_CONF_DISABLE -#define MR_CONF_SERIAL_BUFSZ 64 - -//<<< SPI >>> -#define MR_CONF_DEVICE_SPI MR_CONF_DISABLE - -//<<< I2C >>> -#define MR_CONF_DEVICE_I2C MR_CONF_DISABLE - -//<<< ADC >>> -#define MR_CONF_DEVICE_ADC MR_CONF_DISABLE - -//<<< DAC >>> -#define MR_CONF_DEVICE_DAC MR_CONF_DISABLE - -//<<< TIMER >>> -#define MR_CONF_DEVICE_TIMER MR_CONF_DISABLE - -//<<< MOTION-SENSOR >>> -#define MR_CONF_DEVICE_MOTION MR_CONF_DISABLE - -#endif diff --git a/device/adc/adc.c b/device/adc/adc.c deleted file mode 100644 index 629cbda..0000000 --- a/device/adc/adc.c +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#include "device/adc/adc.h" - -#if (MR_CONF_DEVICE_ADC == MR_CONF_ENABLE) - -#undef LOG_TAG -#define LOG_TAG "adc" - -static mr_err_t mr_adc_open(mr_device_t device) -{ - mr_adc_t adc = (mr_adc_t)device; - - return adc->ops->configure(adc, MR_ENABLE); -} - -static mr_err_t mr_adc_close(mr_device_t device) -{ - mr_adc_t adc = (mr_adc_t)device; - - return adc->ops->configure(adc, MR_DISABLE); -} - -static mr_err_t mr_adc_ioctl(mr_device_t device, int cmd, void *args) -{ - mr_adc_t adc = (mr_adc_t)device; - - switch (cmd & _MR_CTRL_FLAG_MASK) - { - case MR_CTRL_CONFIG: - { - if (args) - { - return adc->ops->channel_configure(adc, - ((struct mr_adc_config *)args)->channel, - ((struct mr_adc_config *)args)->state); - } - return - MR_ERR_INVALID; - } - - default: return - MR_ERR_UNSUPPORTED; - } -} - -static mr_ssize_t mr_adc_read(mr_device_t device, mr_off_t pos, void *buffer, mr_size_t size) -{ - mr_adc_t adc = (mr_adc_t)device; - mr_uint32_t *recv_buffer = (mr_uint32_t *)buffer; - mr_size_t recv_size = 0; - - if (size < sizeof(*recv_buffer)) - { - MR_LOG_E(LOG_TAG, "Device %s: Invalid read size %d\r\n", device->object.name, size); - return - MR_ERR_INVALID; - } - - for (recv_size = 0; recv_size < size; recv_size += sizeof(*recv_buffer)) - { - *recv_buffer = adc->ops->read(adc, (mr_uint16_t)pos); - recv_buffer ++; - } - - return (mr_ssize_t)size; -} - -static mr_err_t _err_io_adc_configure(mr_adc_t adc, mr_state_t state) -{ - MR_ASSERT(0); - return - MR_ERR_IO; -} - -static mr_err_t _err_io_adc_channel_configure(mr_adc_t adc, mr_uint16_t channel, mr_state_t state) -{ - MR_ASSERT(0); - return - MR_ERR_IO; -} - -static mr_uint32_t _err_io_adc_read(mr_adc_t adc, mr_uint16_t channel) -{ - MR_ASSERT(0); - return 0; -} - -mr_err_t mr_hw_adc_add(mr_adc_t adc, const char *name, struct mr_adc_ops *ops, void *data) -{ - mr_err_t ret = MR_ERR_OK; - const static struct mr_device_ops device_ops = - { - mr_adc_open, - mr_adc_close, - mr_adc_ioctl, - mr_adc_read, - MR_NULL, - }; - - MR_ASSERT(adc != MR_NULL); - MR_ASSERT(ops != MR_NULL); - - /* Add the adc-device to the container */ - ret = mr_device_add(&adc->device, name, MR_DEVICE_TYPE_ADC, MR_OPEN_RDONLY, &device_ops, data); - if (ret != MR_ERR_OK) - return ret; - - /* Set adc operations as protect functions if ops is null */ - ops->configure = ops->configure ? ops->configure : _err_io_adc_configure; - ops->channel_configure = ops->channel_configure ? ops->channel_configure : _err_io_adc_channel_configure; - ops->read = ops->read ? ops->read : _err_io_adc_read; - adc->ops = ops; - - return MR_ERR_OK; -} - -#endif diff --git a/device/adc/adc.h b/device/adc/adc.h deleted file mode 100644 index 50e1f8a..0000000 --- a/device/adc/adc.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#ifndef _ADC_H_ -#define _ADC_H_ - -#include "mrlib.h" - -#if (MR_CONF_DEVICE_ADC == MR_CONF_ENABLE) - -struct mr_adc_config -{ - mr_uint16_t channel; - mr_state_t state; -}; - -typedef struct mr_adc *mr_adc_t; -struct mr_adc_ops -{ - mr_err_t (*configure)(mr_adc_t adc, mr_state_t state); - mr_err_t (*channel_configure)(mr_adc_t adc, mr_uint16_t channel, mr_state_t state); - mr_uint32_t (*read)(mr_adc_t adc, mr_uint16_t channel); -}; - -struct mr_adc -{ - struct mr_device device; - - const struct mr_adc_ops *ops; -}; - -mr_err_t mr_hw_adc_add(mr_adc_t adc, const char *name, struct mr_adc_ops *ops, void *data); - -#endif - -#endif diff --git a/device/dac/dac.c b/device/dac/dac.c deleted file mode 100644 index fa1cdd0..0000000 --- a/device/dac/dac.c +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#include "device/dac/dac.h" - -#if (MR_CONF_DEVICE_DAC == MR_CONF_ENABLE) - -#undef LOG_TAG -#define LOG_TAG "dac" - -static mr_err_t mr_dac_open(mr_device_t device) -{ - mr_dac_t dac = (mr_dac_t)device; - - return dac->ops->configure(dac, MR_ENABLE); -} - -static mr_err_t mr_dac_close(mr_device_t device) -{ - mr_dac_t dac = (mr_dac_t)device; - - return dac->ops->configure(dac, MR_DISABLE); -} - -static mr_err_t mr_dac_ioctl(mr_device_t device, int cmd, void *args) -{ - mr_dac_t dac = (mr_dac_t)device; - - switch (cmd & _MR_CTRL_FLAG_MASK) - { - case MR_CTRL_CONFIG: - { - if (args) - { - return dac->ops->channel_configure(dac, - ((struct mr_dac_config *)args)->channel, - ((struct mr_dac_config *)args)->state); - } - return - MR_ERR_INVALID; - } - - default: return - MR_ERR_UNSUPPORTED; - } -} - -static mr_ssize_t mr_dac_write(mr_device_t device, mr_off_t pos, const void *buffer, mr_size_t size) -{ - mr_dac_t dac = (mr_dac_t)device; - mr_uint32_t *send_buffer = (mr_uint32_t *)buffer; - mr_size_t send_size = 0; - - if (size < sizeof(*send_buffer)) - { - MR_LOG_E(LOG_TAG, "Device %s: Invalid write size %d\r\n", device->object.name, size); - return - MR_ERR_INVALID; - } - - for (send_size = 0; send_size < size; send_size += sizeof(*send_buffer)) - { - dac->ops->write(dac, (mr_uint16_t)pos, *send_buffer); - send_buffer ++; - } - - return (mr_ssize_t)size; -} - -static mr_err_t _err_io_dac_configure(mr_dac_t dac, mr_state_t state) -{ - MR_ASSERT(0); - return - MR_ERR_IO; -} - -static mr_err_t _err_io_dac_channel_configure(mr_dac_t dac, mr_uint16_t channel, mr_state_t state) -{ - MR_ASSERT(0); - return - MR_ERR_IO; -} - -static void _err_io_dac_write(mr_dac_t dac, mr_uint16_t channel, mr_uint32_t value) -{ - MR_ASSERT(0); -} - -mr_err_t mr_hw_dac_add(mr_dac_t dac, const char *name, struct mr_dac_ops *ops, void *data) -{ - mr_err_t ret = MR_ERR_OK; - const static struct mr_device_ops device_ops = - { - mr_dac_open, - mr_dac_close, - mr_dac_ioctl, - MR_NULL, - mr_dac_write, - }; - - MR_ASSERT(dac != MR_NULL); - MR_ASSERT(ops != MR_NULL); - - /* Add the dac-device to the container */ - ret = mr_device_add(&dac->device, name, MR_DEVICE_TYPE_DAC, MR_OPEN_WRONLY, &device_ops, data); - if (ret != MR_ERR_OK) - return ret; - - /* Set dac operations as protect functions if ops is null */ - ops->configure = ops->configure ? ops->configure : _err_io_dac_configure; - ops->channel_configure = ops->channel_configure ? ops->channel_configure : _err_io_dac_channel_configure; - ops->write = ops->write ? ops->write : _err_io_dac_write; - dac->ops = ops; - - return MR_ERR_OK; -} - -#endif diff --git a/device/dac/dac.h b/device/dac/dac.h deleted file mode 100644 index 78a8687..0000000 --- a/device/dac/dac.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#ifndef _DAC_H_ -#define _DAC_H_ - -#include "mrlib.h" - -#if (MR_CONF_DEVICE_DAC == MR_CONF_ENABLE) - -struct mr_dac_config -{ - mr_uint16_t channel; - mr_state_t state; -}; - -typedef struct mr_dac *mr_dac_t; -struct mr_dac_ops -{ - mr_err_t (*configure)(mr_dac_t dac, mr_state_t state); - mr_err_t (*channel_configure)(mr_dac_t dac, mr_uint16_t channel, mr_state_t state); - void (*write)(mr_dac_t dac, mr_uint16_t channel, mr_uint32_t value); -}; - -struct mr_dac -{ - struct mr_device device; - - const struct mr_dac_ops *ops; -}; - -mr_err_t mr_hw_dac_add(mr_dac_t dac, const char *name, struct mr_dac_ops *ops, void *data); - -#endif - -#endif diff --git a/device/device_utils.h b/device/device_utils.h deleted file mode 100644 index 389b054..0000000 --- a/device/device_utils.h +++ /dev/null @@ -1,220 +0,0 @@ -/* - * Copyright (c), mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-03-08 MacRsh first version - */ - -#ifndef _DEVICE_UTILS_H_ -#define _DEVICE_UTILS_H_ - -#include - -#if (MR_CONF_DEVICE_PIN == MR_CONF_ENABLE) -#include - -mr_inline mr_err_t mr_pin_init(mr_device_t pin, mr_uint16_t number, mr_uint16_t mode) -{ - struct mr_pin_config config = {number, - mode, - MR_PIN_IRQ_MODE_NONE}; - - return mr_device_ioctl(pin, MR_CTRL_CONFIG, &config); -} - -mr_inline mr_level_t mr_pin_read(mr_device_t pin, mr_uint16_t number) -{ - mr_uint8_t level = MR_LOW; - - mr_device_read(pin, number, &level, sizeof(level)); - return level; -} - -mr_inline void mr_pin_write(mr_device_t pin, mr_uint16_t number, mr_level_t level) -{ - mr_device_write(pin, number, &level, sizeof(level)); -} - -mr_inline void mr_pin_toggle(mr_device_t pin, mr_uint16_t number) -{ - mr_uint8_t level = MR_LOW; - - mr_device_read(pin, number, &level, sizeof(level)); - level = ! level; - mr_device_write(pin, number, &level, sizeof(level)); -} - -#endif - -#if (MR_CONF_DEVICE_SERIAL == MR_CONF_ENABLE) -#include - -mr_inline mr_err_t mr_serial_init(mr_device_t serial, mr_uint32_t baud_rate) -{ - struct mr_serial_config config = MR_SERIAL_CONFIG_DEFAULT; - - config.baud_rate = baud_rate; - return mr_device_ioctl(serial, MR_CTRL_CONFIG, &config); -} - -mr_inline mr_uint8_t mr_serial_read(mr_device_t serial) -{ - mr_uint8_t data = 0; - - mr_device_read(serial, - 1, &data, sizeof(data)); - return data; -} - -mr_inline mr_size_t mr_serial_read_buffer(mr_device_t serial, mr_uint8_t *buffer, mr_size_t count) -{ - return mr_device_read(serial, - 1, buffer, count); -} - -mr_inline void mr_serial_write(mr_device_t serial, mr_uint8_t data) -{ - mr_device_write(serial, - 1, &data, sizeof(data)); -} - -mr_inline mr_size_t mr_serial_write_buffer(mr_device_t serial, mr_uint8_t *buffer, mr_size_t count) -{ - return mr_device_write(serial, - 1, buffer, count); -} - -mr_inline mr_err_t mr_serial_set_rx_callback(mr_device_t serial, - mr_err_t (*rx_callback)(mr_device_t device, void *args)) -{ - return mr_device_ioctl(serial, MR_CTRL_SET_RX_CB, (void *)rx_callback); -} - -#endif - -#if (MR_CONF_DEVICE_SPI == MR_CONF_ENABLE) -#include - -mr_inline mr_err_t mr_spi_init(mr_device_t spi_device, mr_uint32_t baud_rate, mr_uint8_t mode, const char *bus_name) -{ - struct mr_spi_config config = MR_SPI_CONFIG_DEFAULT; - mr_err_t ret = MR_ERR_OK; - - config.baud_rate = baud_rate; - config.mode = mode; - ret = mr_device_ioctl(spi_device, MR_CTRL_CONFIG, &config); - if (ret != MR_ERR_OK) - return ret; - - return mr_device_ioctl(spi_device, MR_CTRL_ATTACH, (void *)bus_name); -} - -mr_inline mr_uint8_t mr_spi_read(mr_device_t spi_device) -{ - mr_uint8_t data = 0; - - mr_device_read(spi_device, - 1, &data, sizeof(data)); - return data; -} - -mr_inline mr_size_t mr_spi_read_buffer(mr_device_t spi_device, mr_uint8_t *buffer, mr_size_t count) -{ - return mr_device_read(spi_device, - 1, buffer, count); -} - -mr_inline void mr_spi_write(mr_device_t spi_device, mr_uint8_t data) -{ - mr_device_write(spi_device, - 1, &data, sizeof(data)); -} - -mr_inline mr_size_t mr_spi_write_buffer(mr_device_t spi_device, mr_uint8_t *buffer, mr_size_t count) -{ - return mr_device_write(spi_device, - 1, buffer, count); -} - -mr_inline mr_uint8_t mr_spi_write_then_read(mr_device_t spi_device, mr_uint8_t send_data) -{ - mr_uint8_t data = 0; - - mr_device_write(spi_device, - 1, &send_data, sizeof(data)); - mr_device_read(spi_device, - 1, &data, sizeof(data)); - return data; -} - -#endif - -#if (MR_CONF_DEVICE_ADC == MR_CONF_ENABLE) -#include - -mr_inline mr_err_t mr_adc_init(mr_device_t adc, mr_uint16_t channel) -{ - struct mr_adc_config config = {channel, - MR_ENABLE}; - - return mr_device_ioctl(adc, MR_CTRL_CONFIG, &config); -} - -mr_inline mr_uint16_t mr_adc_read(mr_device_t adc, mr_uint16_t channel) -{ - mr_uint16_t data = 0; - - mr_device_read(adc, - 1, &data, sizeof(data)); - return data; -} - -#endif - -#if (MR_CONF_DEVICE_DAC == MR_CONF_ENABLE) -#include - -mr_inline mr_err_t mr_dac_init(mr_device_t dac, mr_uint16_t channel) -{ - struct mr_dac_config config = {channel, - MR_ENABLE}; - - return mr_device_ioctl(dac, MR_CTRL_CONFIG, &config); -} - -mr_inline void mr_dac_write(mr_device_t dac, mr_uint16_t channel, mr_uint16_t value) -{ - mr_device_write(dac, - 1, &value, sizeof(value)); -} - -#endif - -#if (MR_CONF_DEVICE_TIMER == MR_CONF_ENABLE) -#include - -mr_inline mr_err_t mr_timer_init(mr_device_t timer, mr_uint32_t frequency, mr_uint8_t mode) -{ - struct mr_timer_config config = {frequency, - mode}; - - return mr_device_ioctl(timer, MR_CTRL_CONFIG, &config); -} - -mr_inline mr_uint32_t mr_timer_read(mr_device_t timer) -{ - mr_uint32_t data = 0; - - mr_device_read(timer, - 1, &data, sizeof(data)); - return data; -} - -mr_inline void mr_timer_write(mr_device_t timer, mr_uint32_t timeout) -{ - mr_device_write(timer, - 1, &timeout, sizeof(timeout)); -} - -mr_inline mr_err_t mr_timer_reboot(mr_device_t timer) -{ - return mr_device_ioctl(timer, MR_CTRL_REBOOT, MR_NULL); -} - -mr_inline mr_err_t mr_timer_set_rx_callback(mr_device_t timer, mr_err_t (*rx_cb)(mr_device_t device, void *args)) -{ - return mr_device_ioctl(timer, MR_CTRL_SET_RX_CB, (void *)rx_cb); -} - -#endif - -#endif diff --git a/device/i2c/i2c.c b/device/i2c/i2c.c deleted file mode 100644 index 320a69b..0000000 --- a/device/i2c/i2c.c +++ /dev/null @@ -1,315 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#include "device/i2c/i2c.h" - -#if (MR_CONF_DEVICE_I2C == MR_CONF_ENABLE) - -#undef LOG_TAG -#define LOG_TAG "i2c" - -static mr_err_t mr_take_i2c_bus(mr_i2c_device_t i2c_device) -{ - mr_err_t ret = MR_ERR_OK; - - MR_ASSERT(i2c_device->bus != MR_NULL); - - /* Take the mutex lock of the i2c-bus */ - ret = mr_mutex_take(&i2c_device->bus->lock, &i2c_device->device.object); - if (ret != MR_ERR_OK) - return ret; - - /* Check if the current i2c-device is the owner of the i2c-bus */ - if (i2c_device->bus->owner != i2c_device) - { - /* If the configuration is different, the i2c-bus is reconfigured */ - if (i2c_device->config.baud_rate != i2c_device->bus->config.baud_rate - || i2c_device->config.host_slave != i2c_device->bus->config.host_slave - || i2c_device->config.address_mode != i2c_device->bus->config.address_mode) - { - ret = i2c_device->bus->ops->configure(i2c_device->bus, &i2c_device->config); - if (ret != MR_ERR_OK) - return ret; - } - - /* Sets the i2c-bus owner to the current i2c-device */ - i2c_device->bus->config = i2c_device->config; - i2c_device->bus->owner = i2c_device; - } - - return MR_ERR_OK; -} - -static mr_err_t mr_release_i2c_bus(mr_i2c_device_t i2c_device) -{ - MR_ASSERT(i2c_device->bus != MR_NULL); - - return mr_mutex_release(&i2c_device->bus->lock, &i2c_device->device.object); -} - -static mr_err_t mr_i2c_bus_open(mr_device_t device) -{ - mr_i2c_bus_t i2c_bus = (mr_i2c_bus_t)device; - struct mr_i2c_config default_config = MR_I2C_CONFIG_DEFAULT; - - /* Setting i2c-bus to default-config */ - if (i2c_bus->config.baud_rate == 0) - i2c_bus->config = default_config; - - return i2c_bus->ops->configure(i2c_bus, &i2c_bus->config); -} - -static mr_err_t mr_i2c_bus_close(mr_device_t device) -{ - mr_i2c_bus_t i2c_bus = (mr_i2c_bus_t)device; - - /* Setting i2c-bus to close-config */ - i2c_bus->config.baud_rate = 0; - - return i2c_bus->ops->configure(i2c_bus, &i2c_bus->config); -} - -static mr_err_t mr_i2c_device_open(mr_device_t device) -{ - mr_i2c_device_t i2c_device = (mr_i2c_device_t)device; - struct mr_i2c_config default_config = MR_I2C_CONFIG_DEFAULT; - - /* Setting i2c-device to default-config */ - if (i2c_device->config.baud_rate == 0) - i2c_device->config = default_config; - - return MR_ERR_OK; -} - -static mr_err_t mr_i2c_device_close(mr_device_t device) -{ - mr_i2c_device_t i2c_device = (mr_i2c_device_t)device; - - /* Setting i2c-device to close-config */ - i2c_device->config.baud_rate = 0; - - /* Disconnect from the i2c-bus */ - i2c_device->bus = MR_NULL; - - return MR_ERR_OK; -} - -static mr_err_t mr_i2c_device_ioctl(mr_device_t device, int cmd, void *args) -{ - mr_i2c_device_t i2c_device = (mr_i2c_device_t)device; - - switch (cmd & _MR_CTRL_FLAG_MASK) - { - case MR_CTRL_CONFIG: - { - if (args) - { - i2c_device->config = *(struct mr_i2c_config *)args; - return MR_ERR_OK; - } - return - MR_ERR_INVALID; - } - - case MR_CTRL_ATTACH: - { - if (args == MR_NULL) - { - i2c_device->bus = MR_NULL; - return MR_ERR_OK; - } - - /* Find the i2c-bus */ - mr_device_t i2c_bus = mr_device_find((char *)args); - if (i2c_bus == MR_NULL) - return - MR_ERR_NOT_FOUND; - if (i2c_bus->type != MR_DEVICE_TYPE_I2C_BUS) - return - MR_ERR_INVALID; - - /* Open the i2c-bus */ - mr_device_open(i2c_bus, MR_OPEN_RDWR); - i2c_device->bus = (mr_i2c_bus_t)i2c_bus; - return MR_ERR_OK; - } - - default: return - MR_ERR_UNSUPPORTED; - } -} - -static mr_ssize_t mr_i2c_device_read(mr_device_t device, mr_off_t pos, void *buffer, mr_size_t size) -{ - mr_i2c_device_t i2c_device = (mr_i2c_device_t)device; - mr_uint8_t *recv_buffer = (mr_uint8_t *)buffer; - mr_err_t ret = MR_ERR_OK; - mr_size_t recv_size = 0; - - /* Take i2c-bus */ - ret = mr_take_i2c_bus(i2c_device); - if (ret != MR_ERR_OK) - { - MR_LOG_E("Device %s: Failed to take i2c-bus\r\n", device->object.name); - return ret; - } - - /* Send the i2c start signal and read cmd */ - i2c_device->bus->ops->start(i2c_device->bus); - if (i2c_device->config.address_mode == MR_I2C_ADDRESS_MODE_10) - { - i2c_device->bus->ops->write(i2c_device->bus, (mr_uint8_t)(0xF1 | ((i2c_device->address) >> 8) << 1)); - i2c_device->bus->ops->write(i2c_device->bus, (mr_uint8_t)(i2c_device->address << 2)); - } else - { - i2c_device->bus->ops->write(i2c_device->bus, (mr_uint8_t)(i2c_device->address << 1 | 0x01)); - } - - for (recv_size = 0; recv_size < size; recv_size ++) - { - *recv_buffer = i2c_device->bus->ops->read(i2c_device->bus, (mr_state_t)(recv_size == 0)); - recv_buffer ++; - } - - /* Release i2c-bus */ - mr_release_i2c_bus(i2c_device); - - return (mr_ssize_t)size; -} - -static mr_ssize_t mr_i2c_device_write(mr_device_t device, mr_off_t pos, const void *buffer, mr_size_t size) -{ - mr_i2c_device_t i2c_device = (mr_i2c_device_t)device; - mr_uint8_t *send_buffer = (mr_uint8_t *)buffer; - mr_err_t ret = MR_ERR_OK; - mr_size_t send_size = 0; - - /* Take i2c-bus */ - ret = mr_take_i2c_bus(i2c_device); - if (ret != MR_ERR_OK) - { - MR_LOG_E("Device %s: Failed to take i2c-bus\r\n", device->object.name); - return ret; - } - - /* Send the i2c start signal and write cmd */ - i2c_device->bus->ops->start(i2c_device->bus); - if (i2c_device->config.address_mode == MR_I2C_ADDRESS_MODE_10) - { - i2c_device->bus->ops->write(i2c_device->bus, (mr_uint8_t)(0xF0 | ((i2c_device->address) >> 8) << 1)); - i2c_device->bus->ops->write(i2c_device->bus, (mr_uint8_t)(i2c_device->address << 2)); - } else - { - i2c_device->bus->ops->write(i2c_device->bus, (mr_uint8_t)(i2c_device->address << 1)); - } - - for (send_size = 0; send_size < size; send_size ++) - { - i2c_device->bus->ops->write(i2c_device->bus, *send_buffer); - send_buffer ++; - } - - /* Release i2c-bus */ - mr_release_i2c_bus(i2c_device); - - return (mr_ssize_t)size; -} - -static mr_err_t _err_io_i2c_configure(mr_i2c_bus_t i2c_bus, struct mr_i2c_config *config) -{ - MR_ASSERT(0); - return - MR_ERR_IO; -} - -static void _err_io_i2c_start(mr_i2c_bus_t i2c_bus) -{ - MR_ASSERT(0); -} - -static void _err_io_i2c_stop(mr_i2c_bus_t i2c_bus) -{ - MR_ASSERT(0); -} - -static void _err_io_i2c_write(mr_i2c_bus_t i2c_bus, mr_uint8_t data) -{ - MR_ASSERT(0); -} - -static mr_uint8_t _err_io_i2c_read(mr_i2c_bus_t i2c_bus, mr_state_t ack_state) -{ - MR_ASSERT(0); - return 0; -} - -mr_err_t mr_hw_i2c_bus_add(mr_i2c_bus_t i2c_bus, const char *name, struct mr_i2c_bus_ops *ops, void *data) -{ - mr_err_t ret = MR_ERR_OK; - const static struct mr_device_ops device_ops = - { - mr_i2c_bus_open, - mr_i2c_bus_close, - MR_NULL, - MR_NULL, - MR_NULL, - }; - - MR_ASSERT(i2c_bus != MR_NULL); - MR_ASSERT(ops != MR_NULL); - - /* Add the i2c-bus to the container */ - ret = mr_device_add(&i2c_bus->device, name, MR_DEVICE_TYPE_I2C_BUS, MR_OPEN_RDWR, &device_ops, data); - if (ret != MR_ERR_OK) - return ret; - - /* Initialize the i2c-bus fields */ - i2c_bus->config.baud_rate = 0; - i2c_bus->owner = MR_NULL; - mr_mutex_init(&i2c_bus->lock); - - /* Set i2c-bus operations as protect functions if ops is null */ - ops->configure = ops->configure ? ops->configure : _err_io_i2c_configure; - ops->start = ops->start ? ops->start : _err_io_i2c_start; - ops->stop = ops->stop ? ops->stop : _err_io_i2c_stop; - ops->write = ops->write ? ops->write : _err_io_i2c_write; - ops->read = ops->read ? ops->read : _err_io_i2c_read; - i2c_bus->ops = ops; - - return MR_ERR_OK; -} - -mr_err_t mr_hw_i2c_device_add(mr_i2c_device_t i2c_device, - const char *name, - mr_uint16_t support_flag, - mr_uint8_t address) -{ - mr_err_t ret = MR_ERR_OK; - const static struct mr_device_ops device_ops = - { - .open = mr_i2c_device_open, - .close = mr_i2c_device_close, - .ioctl = mr_i2c_device_ioctl, - .read = mr_i2c_device_read, - .write = mr_i2c_device_write, - }; - - MR_ASSERT(i2c_device != MR_NULL); - MR_ASSERT(support_flag != MR_NULL); - - /* Add the i2c-device to the container */ - ret = mr_device_add(&i2c_device->device, name, MR_DEVICE_TYPE_I2C, support_flag, &device_ops, MR_NULL); - if (ret != MR_ERR_OK) - return ret; - - /* Initialize the i2c-device fields */ - i2c_device->address = address; - i2c_device->config.baud_rate = 0; - i2c_device->bus = MR_NULL; - - return MR_ERR_OK; -} - -#endif \ No newline at end of file diff --git a/device/i2c/i2c.h b/device/i2c/i2c.h deleted file mode 100644 index 6b5cd18..0000000 --- a/device/i2c/i2c.h +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#ifndef _I2C_H_ -#define _I2C_H_ - -#include "mrlib.h" - -#if (MR_CONF_DEVICE_I2C == MR_CONF_ENABLE) - -#define MR_I2C_HOST 0 -#define MR_I2C_SLAVE 1 - -#define MR_I2C_ADDRESS_MODE_7 0 -#define MR_I2C_ADDRESS_MODE_10 1 - -/* Default config for mr_i2c_config structure */ -#define MR_I2C_CONFIG_DEFAULT \ -{ \ - 3000000, \ - MR_I2C_HOST, \ - MR_I2C_ADDRESS_MODE_7 \ -} - -struct mr_i2c_config -{ - mr_uint32_t baud_rate; - - mr_uint8_t host_slave: 1; - mr_uint8_t address_mode: 1; - mr_uint8_t reserve: 6; -}; - -typedef struct mr_i2c_bus *mr_i2c_bus_t; -struct mr_i2c_device -{ - struct mr_device device; - - struct mr_i2c_config config; - struct mr_i2c_bus *bus; - mr_uint8_t address; -}; -typedef struct mr_i2c_device *mr_i2c_device_t; - -struct mr_i2c_bus_ops -{ - mr_err_t (*configure)(mr_i2c_bus_t i2c_bus, struct mr_i2c_config *config); - void (*start)(mr_i2c_bus_t i2c_bus); - void (*stop)(mr_i2c_bus_t i2c_bus); - void (*write)(mr_i2c_bus_t i2c_bus, mr_uint8_t data); - mr_uint8_t (*read)(mr_i2c_bus_t i2c_bus, mr_state_t ack_state); -}; - -struct mr_i2c_bus -{ - struct mr_device device; - - struct mr_i2c_config config; - struct mr_i2c_device *owner; - struct mr_mutex lock; - - const struct mr_i2c_bus_ops *ops; -}; - -mr_err_t mr_hw_i2c_bus_add(mr_i2c_bus_t i2c_bus, const char *name, struct mr_i2c_bus_ops *ops, void *data); -mr_err_t mr_hw_i2c_device_add(mr_i2c_device_t i2c_device, - const char *name, - mr_uint16_t support_flag, - mr_uint8_t address); - -#endif - -#endif diff --git a/device/link/link.c b/device/link/link.c deleted file mode 100644 index 6244e63..0000000 --- a/device/link/link.c +++ /dev/null @@ -1,184 +0,0 @@ -/* - * Copyright (c), mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-03-10 MacRsh first version - */ - -#include "link.h" - -static mr_uint32_t mr_link_get_crc(const mr_uint8_t *data, mr_size_t count) -{ - mr_uint32_t crc = 0; - - while (count --) - { - crc += *data; - data ++; - } - - return crc; -} - -static mr_err_t mr_link_callback(mr_device_t device, void *args) -{ - static struct mr_link_packet_head packet_head = {MR_NULL}; - static enum mr_link_state state = MR_LINK_CHECK_HEAD; - mr_uint8_t buffer[MR_LINK_PACKET_MAX] = {0}; - - switch (state) - { - case MR_LINK_CHECK_HEAD: - { - if (*(mr_size_t *)args >= sizeof(packet_head)) - { - /* Check frame-head */ - mr_device_read(device, - 1, &packet_head, 1); - if (packet_head.frame_head != 0x3A) - return - MR_ERR_BUSY; - - mr_device_read(device, - 1, (((mr_uint8_t *)&packet_head) + 1), sizeof(packet_head) - 1); - state = MR_LINK_CHECK_DATA; - return MR_ERR_OK; - } else - return - MR_ERR_BUSY; - } - - case MR_LINK_CHECK_DATA: - { - if (*(mr_size_t *)args >= packet_head.length) - { - mr_device_read(device, - 1, buffer, packet_head.length); - - if (packet_head.crc == mr_link_get_crc(buffer, packet_head.length)) - { - for (int i = 0; i < packet_head.length; ++ i) - { - printf("%x\r\n", buffer[i]); - } - } - } else - return - MR_ERR_BUSY; - } - - default:return - MR_ERR_BUSY; - } -} - -static mr_device_t mr_link_open_port(const char *port_name) -{ - mr_err_t error_code = MR_ERR_OK; - mr_device_t port_device = MR_NULL; - - port_device = mr_device_find(port_name); - if (port_device == MR_NULL) - return MR_NULL; - - error_code = mr_device_open(port_device, MR_OPEN_RDWR); - if (error_code != MR_ERR_OK) - return MR_NULL; - - mr_device_ioctl(port_device, MR_CMD_SET_RX_CALLBACK, mr_link_callback); - - return port_device; -} - -static void mr_link_packet_head_init(struct mr_link_packet_head *packet_head, mr_uint8_t src_addr, mr_uint8_t dst_addr) -{ - packet_head->frame_head = 0x3A; - packet_head->crc = 0; - packet_head->ack = 0; - packet_head->sequence = 0; - packet_head->sequence = 0; - packet_head->src_addr = src_addr; - packet_head->dst_addr = dst_addr; - packet_head->length = 0; -} - -mr_size_t mr_link_pack_head(struct mr_link_packet_head *head, - mr_uint8_t *buffer, - mr_size_t count) -{ - if (count > MR_LINK_PACKET_MAX) - count = MR_LINK_PACKET_MAX; - - head->sequence ++; - head->length = count; - head->crc = mr_link_get_crc(buffer, head->length); - - return count; -} - -mr_size_t mr_link_write(mr_link_t link, - mr_uint8_t src_addr, - mr_uint8_t dst_addr, - mr_uint8_t *buffer, - mr_size_t count) -{ - mr_device_t port_device = (mr_device_t)link->device.data; - struct mr_link_packet_head packet_head = {MR_NULL}; - mr_size_t send_count = 0; - - mr_link_packet_head_init(&packet_head, src_addr, dst_addr); - - while (send_count < count) - { - mr_link_pack_head(&packet_head, &buffer[send_count], (count - send_count)); - - mr_device_write(port_device, - 1, (mr_uint8_t *)&packet_head, sizeof(struct mr_link_packet_head)); - mr_device_write(port_device, - 1, &buffer[send_count], packet_head.length); - - send_count += packet_head.length; - } - - return count; -} - -mr_err_t mr_link_service_add_to_container(mr_link_service_t link_service, const char *name) -{ - mr_err_t error_code = MR_ERR_OK; - const static struct mr_device_ops device_ops = {MR_NULL}; - - /* Initialize the device and add the device to the container */ - mr_device_init(&link_service->device, name); - error_code = mr_device_add_to_container(&link_service->device, - MR_DEVICE_TYPE_LINK_SERVICE, - MR_OPEN_RDWR, - &device_ops, - MR_NULL); - if (error_code != MR_ERR_OK) - return error_code; - - /* Initialize the test_link-service fields */ - - - return MR_ERR_OK; -} - -mr_err_t mr_link_add_to_container(mr_link_t link, const char *name, char *port_name) -{ - mr_err_t error_code = MR_ERR_OK; - const static struct mr_device_ops device_ops = {MR_NULL}; - mr_device_t port_device = MR_NULL; - - /* Open service port */ - port_device = mr_link_open_port(port_name); - if (port_device == MR_NULL) - return - MR_ERR_NOT_FOUND; - - /* Initialize the device and add the device to the container */ - mr_device_init(&link->device, name); - error_code = mr_device_add_to_container(&link->device, MR_DEVICE_TYPE_LINK, MR_OPEN_RDWR, &device_ops, port_device); - if (error_code != MR_ERR_OK) - return error_code; - - /* Initialize the link fields */ - mr_list_init(&link->list); - - return MR_ERR_OK; -} - - diff --git a/device/link/link.h b/device/link/link.h deleted file mode 100644 index 2986ea6..0000000 --- a/device/link/link.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c), mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-03-10 MacRsh first version - */ - -#ifndef _LINK_H_ -#define _LINK_H_ - -#include - -#define MR_LINK_PACKET_MAX 4 - -enum mr_link_state -{ - MR_LINK_CHECK_HEAD, - MR_LINK_CHECK_DATA, - MR_LINK_OK, -}; - -struct mr_link_packet_head -{ - mr_uint8_t frame_head: 7; - mr_uint8_t ack: 1; - mr_uint8_t src_addr; - mr_uint8_t dst_addr; - mr_uint8_t sequence; - mr_uint16_t length; - mr_uint32_t crc; -}; - -typedef struct mr_link *mr_link_t; - -struct mr_link_service -{ - struct mr_device device; - - struct mr_list list; - mr_uint8_t src_addr; -}; -typedef struct mr_link_service *mr_link_service_t; - -struct mr_link -{ - struct mr_device device; - - struct mr_list list; - mr_uint8_t buffer[MR_LINK_PACKET_MAX]; -}; - -mr_size_t mr_link_write(mr_link_t link, - mr_uint8_t src_addr, - mr_uint8_t dst_addr, - mr_uint8_t *buffer, - mr_size_t count); - -mr_err_t mr_link_service_add_to_container(mr_link_service_t link_service, const char *name); -mr_err_t mr_link_add_to_container(mr_link_t link, const char *name, char *port_name); - -#endif diff --git a/device/pin/pin.c b/device/pin/pin.c deleted file mode 100644 index 411bae2..0000000 --- a/device/pin/pin.c +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#include "device/pin/pin.h" - -#if (MR_CONF_DEVICE_PIN == MR_CONF_ENABLE) - -#undef LOG_TAG -#define LOG_TAG "pin" - -static mr_err_t mr_pin_ioctl(mr_device_t device, int cmd, void *args) -{ - mr_pin_t pin = (mr_pin_t)device; - mr_err_t ret = MR_ERR_OK; - - switch (cmd & _MR_CTRL_FLAG_MASK) - { - case MR_CTRL_CONFIG: - { - if (args) - { - ret = pin->ops->configure(pin, args); - - return ret; - } - return - MR_ERR_INVALID; - } - - case MR_CTRL_SET_RX_CB: - { - device->rx_cb = args; - return MR_ERR_OK; - } - - default: return - MR_ERR_UNSUPPORTED; - } -} - -static mr_ssize_t mr_pin_read(mr_device_t device, mr_off_t pos, void *buffer, mr_size_t size) -{ - mr_pin_t pin = (mr_pin_t)device; - mr_uint8_t *recv_buffer = (mr_uint8_t *)buffer; - - if (size < sizeof(*recv_buffer)) - { - MR_LOG_E(LOG_TAG, "Device %s: Invalid read size %d\r\n", device->object.name, size); - return - MR_ERR_INVALID; - } - - *recv_buffer = pin->ops->read(pin, (mr_uint16_t)pos); - - return sizeof(*recv_buffer); -} - -static mr_ssize_t mr_pin_write(mr_device_t device, mr_off_t pos, const void *buffer, mr_size_t size) -{ - mr_pin_t pin = (mr_pin_t)device; - mr_uint8_t *send_buffer = (mr_uint8_t *)buffer; - - if (size < sizeof(*send_buffer)) - { - MR_LOG_E(LOG_TAG, "Device %s: Invalid write size %d\r\n", device->object.name, size); - return - MR_ERR_INVALID; - } - - pin->ops->write(pin, (mr_uint16_t)pos, *send_buffer); - - return sizeof(*send_buffer); -} - -static mr_err_t _err_io_pin_configure(mr_pin_t pin, struct mr_pin_config *config) -{ - MR_ASSERT(0); - return - MR_ERR_IO; -} - -static void _err_io_pin_write(mr_pin_t pin, mr_uint16_t number, mr_uint8_t value) -{ - MR_ASSERT(0); -} - -static mr_uint8_t _err_io_pin_read(mr_pin_t pin, mr_uint16_t number) -{ - MR_ASSERT(0); - return 0; -} - -mr_err_t mr_hw_pin_add(mr_pin_t pin, const char *name, struct mr_pin_ops *ops, void *data) -{ - mr_err_t ret = MR_ERR_OK; - const static struct mr_device_ops device_ops = - { - MR_NULL, - MR_NULL, - mr_pin_ioctl, - mr_pin_read, - mr_pin_write, - }; - - MR_ASSERT(pin != MR_NULL); - MR_ASSERT(ops != MR_NULL); - - /* Add the pin-device to the container */ - ret = mr_device_add(&pin->device, name, MR_DEVICE_TYPE_PIN, MR_OPEN_RDWR, &device_ops, data); - if (ret != MR_ERR_OK) - return ret; - - /* Set pin operations as protect functions if ops is null */ - ops->configure = ops->configure ? ops->configure : _err_io_pin_configure; - ops->write = ops->write ? ops->write : _err_io_pin_write; - ops->read = ops->read ? ops->read : _err_io_pin_read; - pin->ops = ops; - - return MR_ERR_OK; -} - -void mr_hw_pin_isr(mr_pin_t pin, mr_uint32_t Line) -{ - /* Invoke the rx-cb function */ - if (pin->device.rx_cb != MR_NULL) - { - pin->device.rx_cb(&pin->device, &Line); - } -} - -#endif \ No newline at end of file diff --git a/device/pin/pin.h b/device/pin/pin.h deleted file mode 100644 index 18e0d75..0000000 --- a/device/pin/pin.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#ifndef _PIN_H_ -#define _PIN_H_ - -#include "mrlib.h" - -#if (MR_CONF_DEVICE_PIN == MR_CONF_ENABLE) - -#define MR_PIN_MODE_NONE 0 -#define MR_PIN_MODE_OUTPUT 1 -#define MR_PIN_MODE_OUTPUT_OD 2 -#define MR_PIN_MODE_INPUT 3 -#define MR_PIN_MODE_INPUT_DOWN 4 -#define MR_PIN_MODE_INPUT_UP 5 - -#define MR_PIN_MODE_RISING 6 -#define MR_PIN_MODE_FALLING 7 -#define MR_PIN_MODE_EDGE 8 -#define MR_PIN_MODE_LOW 9 -#define MR_PIN_MODE_HIGH 10 - -struct mr_pin_config -{ - mr_uint16_t number; - mr_uint8_t mode; -}; - -typedef struct mr_pin *mr_pin_t; -struct mr_pin_ops -{ - mr_err_t (*configure)(mr_pin_t pin, struct mr_pin_config *config); - void (*write)(mr_pin_t pin, mr_uint16_t number, mr_uint8_t value); - mr_uint8_t (*read)(mr_pin_t pin, mr_uint16_t number); -}; - -struct mr_pin -{ - struct mr_device device; - - const struct mr_pin_ops *ops; -}; - -mr_err_t mr_hw_pin_add(mr_pin_t pin, const char *name, struct mr_pin_ops *ops, void *data); -void mr_hw_pin_isr(mr_pin_t pin, mr_uint32_t Line); - -#endif - -#endif diff --git a/device/sensor/motion/motion.c b/device/sensor/motion/motion.c deleted file mode 100644 index 6ba5800..0000000 --- a/device/sensor/motion/motion.c +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-05-17 MacRsh first version - */ - -#include "motion.h" - -#if (MR_CONF_DEVICE_MOTION == MR_CONF_ENABLE) - -#undef LOG_TAG -#define LOG_TAG "motion-sensor" - -static mr_err_t mr_motion_sensor_open(mr_device_t device) -{ - mr_motion_sensor_t sensor = (mr_motion_sensor_t)device; - struct mr_motion_sensor_config default_config_accel = MR_MOTION_SENSOR_CONFIG_DEFAULT_ACC; - struct mr_motion_sensor_config default_config_gyro = MR_MOTION_SENSOR_CONFIG_DEFAULT_GYRO; - - /* Setting motion-sensor to default-config */ - if (sensor->config.range == 0) - { - switch (sensor->type) - { - case MR_MOTION_SENSOR_TYPE_ACCELEROMETER: - sensor->config = default_config_accel; - break; - - case MR_MOTION_SENSOR_TYPE_GYROSCOPE: - sensor->config = default_config_gyro; - break; - - default:break; - } - } - - /* Initialize the motion-sensor data */ - mr_memset(&sensor->data, 0, sizeof(sensor->data)); - - return sensor->ops->configure(sensor, &sensor->config); -} - -static mr_err_t mr_motion_sensor_close(mr_device_t device) -{ - mr_motion_sensor_t sensor = (mr_motion_sensor_t)device; - - /* Empty the motion-sensor data */ - mr_memset(&sensor->data, 0, sizeof(sensor->data)); - - /* Setting motion-sensor to close-config */ - sensor->config.range = 0; - - return sensor->ops->configure(sensor, &sensor->config); -} - -static mr_err_t mr_motion_sensor_ioctl(mr_device_t device, int cmd, void *args) -{ - mr_motion_sensor_t sensor = (mr_motion_sensor_t)device; - mr_err_t ret = MR_ERR_OK; - - switch (cmd & _MR_CTRL_FLAG_MASK) - { - case MR_CTRL_CONFIG: - { - if (args) - { - ret = sensor->ops->configure(sensor, (struct mr_motion_sensor_config *)args); - if (ret == MR_ERR_OK) - sensor->config = *(struct mr_motion_sensor_config *)args; - return ret; - } - return - MR_ERR_INVALID; - } - - default: return - MR_ERR_UNSUPPORTED; - } -} - -static mr_ssize_t mr_motion_sensor_read(mr_device_t device, mr_off_t pos, void *buffer, mr_size_t size) -{ - mr_motion_sensor_t sensor = (mr_motion_sensor_t)device; - struct mr_motion_sensor_axis *recv_buffer = (struct mr_motion_sensor_axis *)buffer; - - if (size < sizeof(*recv_buffer)) - { - MR_LOG_E(LOG_TAG, "Device %s: Invalid read size %d\r\n", device->object.name, size); - return - MR_ERR_INVALID; - } - - *recv_buffer = sensor->ops->read(sensor); - - return sizeof(*recv_buffer); -} - -static mr_err_t _err_io_motion_sensor_configure(mr_motion_sensor_t sensor, struct mr_motion_sensor_config *config) -{ - MR_ASSERT(0); - return -MR_ERR_IO; -} - -static struct mr_motion_sensor_axis _err_io_motion_sensor_read(mr_motion_sensor_t sensor) -{ - struct mr_motion_sensor_axis axis = {0}; - - MR_ASSERT(0); - return axis; -} - -mr_err_t mr_hw_motion_sensor_add(mr_motion_sensor_t sensor, - const char *name, - enum mr_motion_sensor_type type, - struct mr_motion_sensor_ops *ops, - void *data) -{ - mr_err_t ret = MR_ERR_OK; - const static struct mr_device_ops device_ops = - { - mr_motion_sensor_open, - mr_motion_sensor_close, - mr_motion_sensor_ioctl, - mr_motion_sensor_read, - MR_NULL, - }; - - MR_ASSERT(sensor != MR_NULL); - MR_ASSERT(ops != MR_NULL); - - /* Add the motion-sensor to the container */ - ret = mr_device_add(&sensor->device, name, MR_DEVICE_TYPE_SENSOR, MR_OPEN_RDONLY, &device_ops, data); - if (ret != MR_ERR_OK) - return ret; - - /* Initialize the motion-sensor fields */ - sensor->type = type; - sensor->config.range = 0; - - /* Set motion-sensor operations as protect functions if ops is null */ - ops->configure = ops->configure ? ops->configure : _err_io_motion_sensor_configure; - ops->read = ops->read ? ops->read : _err_io_motion_sensor_read; - sensor->ops = ops; - - return MR_ERR_OK; -} - -#endif \ No newline at end of file diff --git a/device/sensor/motion/motion.h b/device/sensor/motion/motion.h deleted file mode 100644 index 794bc5f..0000000 --- a/device/sensor/motion/motion.h +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-05-17 MacRsh first version - */ - -#ifndef _MOTION_H_ -#define _MOTION_H_ - -#include "mrlib.h" - -#if (MR_CONF_DEVICE_MOTION == MR_CONF_ENABLE) - -#define MR_MOTION_SENSOR_RANGE_ACC_2G 2 -#define MR_MOTION_SENSOR_RANGE_ACC_4G 4 -#define MR_MOTION_SENSOR_RANGE_ACC_8G 8 -#define MR_MOTION_SENSOR_RANGE_ACC_16G 16 - -#define MR_MOTION_SENSOR_RANGE_GYRO_250DPS 250 -#define MR_MOTION_SENSOR_RANGE_GYRO_500DPS 500 -#define MR_MOTION_SENSOR_RANGE_GYRO_1000DPS 1000 -#define MR_MOTION_SENSOR_RANGE_GYRO_2000DPS 2000 - -#define MR_MOTION_SENSOR_SAMPLE_RATE_10HZ 10 -#define MR_MOTION_SENSOR_SAMPLE_RATE_20HZ 20 -#define MR_MOTION_SENSOR_SAMPLE_RATE_50HZ 50 -#define MR_MOTION_SENSOR_SAMPLE_RATE_100HZ 100 -#define MR_MOTION_SENSOR_SAMPLE_RATE_200HZ 200 -#define MR_MOTION_SENSOR_SAMPLE_RATE_500HZ 500 -#define MR_MOTION_SENSOR_SAMPLE_RATE_1000HZ 1000 - -/* Default config for mr_motion_sensor_config structure */ -#define MR_MOTION_SENSOR_CONFIG_DEFAULT_ACC \ -{ \ - MR_MOTION_SENSOR_RANGE_ACC_2G, \ - MR_MOTION_SENSOR_SAMPLE_RATE_500HZ \ -} - -#define MR_MOTION_SENSOR_CONFIG_DEFAULT_GYRO \ -{ \ - MR_MOTION_SENSOR_RANGE_GYRO_250DPS, \ - MR_MOTION_SENSOR_SAMPLE_RATE_500HZ \ -} - -enum mr_motion_sensor_type -{ - MR_MOTION_SENSOR_TYPE_ACCELEROMETER, - MR_MOTION_SENSOR_TYPE_GYROSCOPE, - MR_MOTION_SENSOR_TYPE_MAGNETOMETER, -}; - -struct mr_motion_sensor_config -{ - mr_uint32_t range; - mr_uint32_t sample_rate; -}; - -struct mr_motion_sensor_axis -{ - mr_int32_t x; - mr_int32_t y; - mr_int32_t z; -}; - -typedef struct mr_motion_sensor *mr_motion_sensor_t; -struct mr_motion_sensor_ops -{ - mr_err_t (*configure)(mr_motion_sensor_t sensor, struct mr_motion_sensor_config *config); - struct mr_motion_sensor_axis (*read)(mr_motion_sensor_t sensor); -}; - -struct mr_motion_sensor -{ - struct mr_device device; - - enum mr_motion_sensor_type type; - struct mr_motion_sensor_config config; - struct mr_motion_sensor_axis data; - - const struct mr_motion_sensor_ops *ops; -}; - -mr_err_t mr_hw_motion_sensor_add(mr_motion_sensor_t sensor, - const char *name, - enum mr_motion_sensor_type type, - struct mr_motion_sensor_ops *ops, - void *data); - -#endif - -#endif \ No newline at end of file diff --git a/device/serial/serial.c b/device/serial/serial.c deleted file mode 100644 index f37fe34..0000000 --- a/device/serial/serial.c +++ /dev/null @@ -1,268 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#include "device/serial/serial.h" - -#if (MR_CONF_DEVICE_SERIAL == MR_CONF_ENABLE) - -#undef LOG_TAG -#define LOG_TAG "serial" - -static mr_err_t mr_serial_open(mr_device_t device) -{ - mr_serial_t serial = (mr_serial_t)device; - struct mr_serial_config default_config = MR_SERIAL_CONFIG_DEFAULT; - struct mr_serial_fifo *fifo = MR_NULL; - - /* Allocation the fifo memory */ - if (serial->fifo_bufsz != 0) - { - /* Rx-fifo */ - fifo = (struct mr_serial_fifo *)mr_malloc(sizeof(struct mr_serial_fifo) + serial->fifo_bufsz); - if (fifo == MR_NULL) - return - MR_ERR_NO_MEMORY; - - mr_fifo_init(&fifo->fifo, fifo->pool, serial->fifo_bufsz); - serial->rx_fifo = fifo; - fifo = MR_NULL; - - /* Tx-fifo */ - fifo = (struct mr_serial_fifo *)mr_malloc(sizeof(struct mr_serial_fifo) + serial->fifo_bufsz); - if (fifo == MR_NULL) - return - MR_ERR_NO_MEMORY; - - mr_fifo_init(&fifo->fifo, fifo->pool, serial->fifo_bufsz); - serial->tx_fifo = fifo; - fifo = MR_NULL; - } - - /* Setting serial to default-config */ - if (serial->config.baud_rate == 0) - serial->config = default_config; - - return serial->ops->configure(serial, &serial->config); -} - -static mr_err_t mr_serial_close(mr_device_t device) -{ - mr_serial_t serial = (mr_serial_t)device; - struct mr_serial_fifo *fifo = MR_NULL; - - /* Release the fifo memory */ - if (serial->fifo_bufsz != 0) - { - /* Rx-fifo */ - fifo = (struct mr_serial_fifo *)serial->rx_fifo; - mr_free(fifo); - serial->rx_fifo = MR_NULL; - - /* Tx-fifo */ - fifo = (struct mr_serial_fifo *)serial->tx_fifo; - mr_free(fifo); - serial->tx_fifo = MR_NULL; - } - - /* Setting serial to close-config */ - serial->config.baud_rate = 0; - - return serial->ops->configure(serial, &serial->config); -} - -static mr_err_t mr_serial_ioctl(mr_device_t device, int cmd, void *args) -{ - mr_err_t ret = MR_ERR_OK; - mr_serial_t serial = (mr_serial_t)device; - - switch (cmd & _MR_CTRL_FLAG_MASK) - { - case MR_CTRL_CONFIG: - { - if (args) - { - ret = serial->ops->configure(serial, (struct mr_serial_config *)args); - if (ret == MR_ERR_OK) - serial->config = *(struct mr_serial_config *)args; - return ret; - } - return - MR_ERR_INVALID; - } - - case MR_CTRL_SET_RX_CB: - { - device->rx_cb = args; - return MR_ERR_OK; - } - - case MR_CTRL_SET_TX_CB: - { - device->tx_cb = args; - return MR_ERR_OK; - } - - default: return - MR_ERR_UNSUPPORTED; - } -} - -static mr_ssize_t mr_serial_read(mr_device_t device, mr_off_t pos, void *buffer, mr_size_t size) -{ - mr_serial_t serial = (mr_serial_t)device; - struct mr_serial_fifo *fifo = (struct mr_serial_fifo *)serial->rx_fifo; - mr_uint8_t *recv_buffer = (mr_uint8_t *)buffer; - mr_size_t recv_size = 0; - - while (recv_size < size) - { - /* Read if the fifo has data */ - recv_size += mr_fifo_read(&fifo->fifo, recv_buffer + recv_size, size - recv_size); - } - - return (mr_ssize_t)size; -} - -static mr_ssize_t mr_serial_write(mr_device_t device, mr_off_t pos, const void *buffer, mr_size_t size) -{ - mr_serial_t serial = (mr_serial_t)device; - struct mr_serial_fifo *fifo = (struct mr_serial_fifo *)serial->tx_fifo; - mr_uint8_t *send_buffer = (mr_uint8_t *)buffer; - mr_size_t send_size = 0; - - while (send_size < size) - { - /* Write if the fifo has space */ - send_size += mr_fifo_write(&fifo->fifo, send_buffer + send_size, size - send_size); - - /* Start send */ - serial->ops->start_tx(serial); - } - - return (mr_ssize_t)size; -} - -static mr_err_t _err_io_serial_configure(mr_serial_t serial, struct mr_serial_config *config) -{ - MR_ASSERT(0); - return - MR_ERR_IO; -} - -static void _err_io_serial_write(mr_serial_t serial, mr_uint8_t data) -{ - MR_ASSERT(0); -} - -static mr_uint8_t _err_io_serial_read(mr_serial_t serial) -{ - MR_ASSERT(0); - return 0; -} - -static void _err_io_serial_start_tx(mr_serial_t serial) -{ - MR_ASSERT(0); -} - -static void _err_io_serial_stop_tx(mr_serial_t serial) -{ - MR_ASSERT(0); -} - -mr_err_t mr_hw_serial_add(mr_serial_t serial, const char *name, struct mr_serial_ops *ops, void *data) -{ - mr_err_t ret = MR_ERR_OK; - const static struct mr_device_ops device_ops = - { - mr_serial_open, - mr_serial_close, - mr_serial_ioctl, - mr_serial_read, - mr_serial_write, - }; - - MR_ASSERT(serial != MR_NULL); - MR_ASSERT(ops != MR_NULL); - - /* Add the serial-device to the container */ - ret = mr_device_add(&serial->device, name, MR_DEVICE_TYPE_SERIAL, MR_OPEN_RDWR, &device_ops, data); - if (ret != MR_ERR_OK) - return ret; - - /* Initialize the serial fields */ - serial->config.baud_rate = 0; - serial->fifo_bufsz = MR_CONF_SERIAL_BUFSZ; - serial->rx_fifo = MR_NULL; - serial->tx_fifo = MR_NULL; - - /* Set serial operations as protect functions if ops is null */ - ops->configure = ops->configure ? ops->configure : _err_io_serial_configure; - ops->write = ops->write ? ops->write : _err_io_serial_write; - ops->read = ops->read ? ops->read : _err_io_serial_read; - ops->start_tx = ops->start_tx ? ops->start_tx : _err_io_serial_start_tx; - ops->stop_tx = ops->stop_tx ? ops->stop_tx : _err_io_serial_stop_tx; - serial->ops = ops; - - return MR_ERR_OK; -} - -void mr_hw_serial_isr(mr_serial_t serial, mr_uint32_t event) -{ - switch (event & _MR_SERIAL_EVENT_MASK) - { - case MR_SERIAL_EVENT_RX_INT: - { - struct mr_serial_fifo *rx_fifo = MR_NULL; - mr_uint8_t data = 0; - - rx_fifo = (struct mr_serial_fifo *)serial->rx_fifo; - - /* Read data into the ring buffer */ - data = serial->ops->read(serial); - mr_fifo_write_force(&rx_fifo->fifo, &data, 1); - - /* Invoke the rx-cb function */ - if (serial->device.rx_cb != MR_NULL) - { - mr_size_t length; - - length = mr_fifo_get_data_size(&rx_fifo->fifo); - serial->device.rx_cb(&serial->device, &length); - } - break; - } - - case MR_SERIAL_EVENT_TX_INT: - { - struct mr_serial_fifo *tx_fifo = MR_NULL; - mr_size_t length = 0; - mr_uint8_t data = 0; - - tx_fifo = (struct mr_serial_fifo *)serial->tx_fifo; - - length = mr_fifo_get_data_size(&tx_fifo->fifo); - if (length == 0) - { - serial->ops->stop_tx(serial); - - /* Invoke the tx-cb function */ - if (serial->device.tx_cb != MR_NULL) - { - serial->device.rx_cb(&serial->device, &length); - } - break; - } - - mr_fifo_read(&tx_fifo->fifo, &data, 1); - serial->ops->write(serial, data); - break; - } - - default:break; - } -} - -#endif diff --git a/device/serial/serial.h b/device/serial/serial.h deleted file mode 100644 index 816c451..0000000 --- a/device/serial/serial.h +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#ifndef _SERIAL_H_ -#define _SERIAL_H_ - -#include "mrlib.h" - -#if (MR_CONF_DEVICE_SERIAL == MR_CONF_ENABLE) - -#define MR_SERIAL_DATA_BITS_8 0 -#define MR_SERIAL_DATA_BITS_9 1 - -#define MR_SERIAL_STOP_BITS_1 0 -#define MR_SERIAL_STOP_BITS_1_5 1 -#define MR_SERIAL_STOP_BITS_2 2 - -#define MR_SERIAL_PARITY_NONE 0 -#define MR_SERIAL_PARITY_EVEN 1 -#define MR_SERIAL_PARITY_ODD 2 - -#define MR_SERIAL_BIT_ORDER_LSB 0 -#define MR_SERIAL_BIT_ORDER_MSB 1 - -#define MR_SERIAL_NRZ_NORMAL 0 -#define MR_SERIAL_NRZ_INVERTED 1 - -#define MR_SERIAL_FLOW_CONTROL_NONE 0 -#define MR_SERIAL_FLOW_CONTROL_CTSRTS 1 - -#define MR_SERIAL_BUFSZ_MIN 32 -#if MR_CONF_SERIAL_BUFSZ < MR_SERIAL_BUFSZ_MIN -#define MR_CONF_SERIAL_BUFSZ MR_SERIAL_BUFSZ_MIN -#endif - -#define MR_SERIAL_EVENT_RX_INT 0x1000 -#define MR_SERIAL_EVENT_TX_INT 0x2000 -#define _MR_SERIAL_EVENT_MASK 0xf000 - -/* Default config for mr_serial_config structure */ -#define MR_SERIAL_CONFIG_DEFAULT \ -{ \ - 115200, \ - MR_SERIAL_DATA_BITS_8, \ - MR_SERIAL_STOP_BITS_1, \ - MR_SERIAL_PARITY_NONE, \ - MR_SERIAL_BIT_ORDER_LSB, \ - MR_SERIAL_NRZ_NORMAL, \ - MR_SERIAL_FLOW_CONTROL_NONE \ -} - -struct mr_serial_config -{ - mr_uint32_t baud_rate; - - mr_uint16_t data_bits: 4; - mr_uint16_t stop_bits: 2; - mr_uint16_t parity: 2; - mr_uint16_t bit_order: 1; - mr_uint16_t invert: 1; - mr_uint16_t flow_control: 1; - mr_uint16_t reserved: 5; -}; - -struct mr_serial_fifo -{ - struct mr_fifo fifo; - mr_uint8_t pool[]; -}; - -typedef struct mr_serial *mr_serial_t; -struct mr_serial_ops -{ - mr_err_t (*configure)(mr_serial_t serial, struct mr_serial_config *config); - void (*write)(mr_serial_t serial, mr_uint8_t data); - mr_uint8_t (*read)(mr_serial_t serial); - void (*start_tx)(mr_serial_t serial); - void (*stop_tx)(mr_serial_t serial); -}; - -struct mr_serial -{ - struct mr_device device; - - struct mr_serial_config config; - mr_size_t fifo_bufsz; - void *rx_fifo; - void *tx_fifo; - - const struct mr_serial_ops *ops; -}; - -mr_err_t mr_hw_serial_add(mr_serial_t serial, const char *name, struct mr_serial_ops *ops, void *data); -void mr_hw_serial_isr(mr_serial_t serial, mr_uint32_t event); - -#endif - -#endif diff --git a/device/spi/spi.c b/device/spi/spi.c deleted file mode 100644 index 9c32e09..0000000 --- a/device/spi/spi.c +++ /dev/null @@ -1,300 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#include "device/spi/spi.h" - -#undef LOG_TAG -#define LOG_TAG "spi" - -#if (MR_CONF_DEVICE_SPI == MR_CONF_ENABLE) - -static mr_err_t mr_take_spi_bus(mr_spi_device_t spi_device) -{ - mr_err_t ret = MR_ERR_OK; - - MR_ASSERT(spi_device->bus != MR_NULL); - - /* Take the mutex lock of the spi-bus */ - ret = mr_mutex_take(&spi_device->bus->lock, &spi_device->device.object); - if (ret != MR_ERR_OK) - return ret; - - /* Check if the current spi-device is the owner of the spi-bus */ - if (spi_device->bus->owner != spi_device) - { - /* Stop the chip-select of the last spi-bus owner */ - if (spi_device->bus->owner != MR_NULL) - spi_device->bus->ops->cs_set(spi_device->bus, spi_device->bus->owner->cs_pin, MR_DISABLE); - - /* If the configuration is different, the spi-bus is reconfigured */ - if (spi_device->config.baud_rate != spi_device->bus->config.baud_rate - || spi_device->config.host_slave != spi_device->bus->config.host_slave - || spi_device->config.mode != spi_device->bus->config.mode - || spi_device->config.bit_order != spi_device->bus->config.bit_order) - { - ret = spi_device->bus->ops->configure(spi_device->bus, &spi_device->config); - if (ret != MR_ERR_OK) - return ret; - } - - /* Sets the spi-bus owner to the current spi-device */ - spi_device->bus->config = spi_device->config; - spi_device->bus->owner = spi_device; - - /* Start the chip-select of the spi-device */ - spi_device->bus->ops->cs_set(spi_device->bus, spi_device->bus->owner->cs_pin, MR_ENABLE); - } - - return MR_ERR_OK; -} - -static mr_err_t mr_release_spi_bus(mr_spi_device_t spi_device) -{ - MR_ASSERT(spi_device->bus != MR_NULL); - - return mr_mutex_release(&spi_device->bus->lock, &spi_device->device.object); -} - -static mr_err_t mr_spi_bus_open(mr_device_t device) -{ - mr_spi_bus_t spi_bus = (mr_spi_bus_t)device; - struct mr_spi_config default_config = MR_SPI_CONFIG_DEFAULT; - - /* Setting spi-bus to default-config */ - if (spi_bus->config.baud_rate == 0) - spi_bus->config = default_config; - - return spi_bus->ops->configure(spi_bus, &spi_bus->config); -} - -static mr_err_t mr_spi_bus_close(mr_device_t device) -{ - mr_spi_bus_t spi_bus = (mr_spi_bus_t)device; - - /* Setting spi-bus to close-config */ - spi_bus->config.baud_rate = 0; - - return spi_bus->ops->configure(spi_bus, &spi_bus->config); -} - -static mr_err_t mr_spi_device_open(mr_device_t device) -{ - mr_spi_device_t spi_device = (mr_spi_device_t)device; - struct mr_spi_config default_config = MR_SPI_CONFIG_DEFAULT; - - /* Setting spi-device to default-config */ - if (spi_device->config.baud_rate == 0) - spi_device->config = default_config; - - return MR_ERR_OK; -} - -static mr_err_t mr_spi_device_close(mr_device_t device) -{ - mr_spi_device_t spi_device = (mr_spi_device_t)device; - - /* Setting spi-device to close-config */ - spi_device->config.baud_rate = 0; - - /* Disconnect from the spi-bus */ - spi_device->bus = MR_NULL; - - return MR_ERR_OK; -} - -static mr_err_t mr_spi_device_ioctl(mr_device_t device, int cmd, void *args) -{ - mr_spi_device_t spi_device = (mr_spi_device_t)device; - - switch (cmd & _MR_CTRL_FLAG_MASK) - { - case MR_CTRL_CONFIG: - { - if (args) - { - spi_device->config = *(struct mr_spi_config *)args; - return MR_ERR_OK; - } - return - MR_ERR_INVALID; - } - - case MR_CTRL_ATTACH: - { - /* Find the spi-bus */ - mr_device_t spi_bus = mr_device_find((char *)args); - if (spi_bus == MR_NULL) - return - MR_ERR_NOT_FOUND; - if (spi_bus->type != MR_DEVICE_TYPE_SPI_BUS) - return - MR_ERR_INVALID; - - /* Open the spi-bus */ - mr_device_open(spi_bus, MR_OPEN_RDWR); - spi_device->bus = (mr_spi_bus_t)spi_bus; - return MR_ERR_OK; - } - - default: return - MR_ERR_UNSUPPORTED; - } -} - -static mr_ssize_t mr_spi_device_read(mr_device_t device, mr_off_t pos, void *buffer, mr_size_t size) -{ - mr_spi_device_t spi_device = (mr_spi_device_t)device; - mr_uint8_t *recv_buffer = (mr_uint8_t *)buffer; - mr_err_t ret = MR_ERR_OK; - mr_size_t recv_size = 0; - - /* Take spi-bus */ - ret = mr_take_spi_bus(spi_device); - if (ret != MR_ERR_OK) - { - MR_LOG_E(LOG_TAG, "Device %s: Failed to take spi-bus\r\n", device->object.name); - return ret; - } - - for (recv_size = 0; recv_size < size; recv_size ++) - { - spi_device->bus->ops->write(spi_device->bus, 0u); - *recv_buffer = spi_device->bus->ops->read(spi_device->bus); - recv_buffer ++; - } - - /* Release spi-bus */ - mr_release_spi_bus(spi_device); - - return (mr_ssize_t)size; -} - -static mr_ssize_t mr_spi_device_write(mr_device_t device, mr_off_t pos, const void *buffer, mr_size_t size) -{ - mr_spi_device_t spi_device = (mr_spi_device_t)device; - mr_uint8_t *send_buffer = (mr_uint8_t *)buffer; - mr_err_t ret = MR_ERR_OK; - mr_size_t send_size = 0; - - /* Take spi-bus */ - ret = mr_take_spi_bus(spi_device); - if (ret != MR_ERR_OK) - { - MR_LOG_E(LOG_TAG, "Device %s: Failed to take spi-bus\r\n", device->object.name); - return ret; - } - - for (send_size = 0; send_size < size; send_size ++) - { - spi_device->bus->ops->write(spi_device->bus, *send_buffer); - send_buffer ++; - } - - /* Release spi-bus */ - mr_release_spi_bus(spi_device); - - return (mr_ssize_t)size; -} - -static mr_err_t _err_io_spi_configure(mr_spi_bus_t spi_bus, struct mr_spi_config *config) -{ - MR_ASSERT(0); - return - MR_ERR_IO; -} - -static void _err_io_spi_write(mr_spi_bus_t spi_bus, mr_uint8_t data) -{ - MR_ASSERT(0); -} - -static mr_uint8_t _err_io_spi_read(mr_spi_bus_t spi_bus) -{ - MR_ASSERT(0); - return 0; -} - -static void _err_io_spi_cs_set(mr_spi_bus_t spi_bus, mr_uint16_t cs_pin, mr_state_t state) -{ - MR_ASSERT(0); -} - -mr_err_t mr_hw_spi_bus_add(mr_spi_bus_t spi_bus, const char *name, struct mr_spi_bus_ops *ops, void *data) -{ - mr_err_t ret = MR_ERR_OK; - const static struct mr_device_ops device_ops = - { - mr_spi_bus_open, - mr_spi_bus_close, - MR_NULL, - MR_NULL, - MR_NULL, - }; - - MR_ASSERT(spi_bus != MR_NULL); - MR_ASSERT(ops != MR_NULL); - - /* Add the spi-bus to the container */ - ret = mr_device_add(&spi_bus->device, name, MR_DEVICE_TYPE_SPI_BUS, MR_OPEN_RDWR, &device_ops, data); - if (ret != MR_ERR_OK) - return ret; - - /* Initialize the spi-bus fields */ - spi_bus->config.baud_rate = 0; - spi_bus->owner = MR_NULL; - mr_mutex_init(&spi_bus->lock); - - /* Set spi-bus operations as protect functions if ops is null */ - ops->configure = ops->configure ? ops->configure : _err_io_spi_configure; - ops->write = ops->write ? ops->write : _err_io_spi_write; - ops->read = ops->read ? ops->read : _err_io_spi_read; - ops->cs_set = ops->cs_set ? ops->cs_set : _err_io_spi_cs_set; - spi_bus->ops = ops; - - return MR_ERR_OK; -} - -mr_err_t mr_hw_spi_device_add(mr_spi_device_t spi_device, - const char *name, - mr_uint16_t support_flag, - mr_uint16_t cs_pin, - const char *bus_name) -{ - mr_err_t ret = MR_ERR_OK; - const static struct mr_device_ops device_ops = - { - mr_spi_device_open, - mr_spi_device_close, - mr_spi_device_ioctl, - mr_spi_device_read, - mr_spi_device_write, - }; - - MR_ASSERT(spi_device != MR_NULL); - MR_ASSERT(support_flag != MR_NULL); - - /* Add the spi-device to the container */ - ret = mr_device_add(&spi_device->device, name, MR_DEVICE_TYPE_SPI, support_flag, &device_ops, MR_NULL); - if (ret != MR_ERR_OK) - return ret; - - /* Initialize the spi-device fields */ - spi_device->config.baud_rate = 0; - spi_device->bus = MR_NULL; - spi_device->cs_pin = cs_pin; - - /* Attach the spi-device to the spi-bus */ - mr_device_t spi_bus = mr_device_find(bus_name); - if (spi_bus == MR_NULL) - return - MR_ERR_NOT_FOUND; - if (spi_bus->type != MR_DEVICE_TYPE_SPI_BUS) - return - MR_ERR_INVALID; - mr_device_open(spi_bus, MR_OPEN_RDWR); - spi_device->bus = (mr_spi_bus_t)spi_bus; - - return MR_ERR_OK; -} - -#endif \ No newline at end of file diff --git a/device/spi/spi.h b/device/spi/spi.h deleted file mode 100644 index 483af8e..0000000 --- a/device/spi/spi.h +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#ifndef _SPI_H_ -#define _SPI_H_ - -#include "mrlib.h" - -#if (MR_CONF_DEVICE_SPI == MR_CONF_ENABLE) - -#define MR_SPI_HOST 0 -#define MR_SPI_SLAVE 1 - -#define MR_SPI_MODE_0 0 -#define MR_SPI_MODE_1 1 -#define MR_SPI_MODE_2 2 -#define MR_SPI_MODE_3 3 - -#define MR_SPI_BIT_ORDER_MSB 0 -#define MR_SPI_BIT_ORDER_LSB 1 - -#define MR_SPI_CS_ACTIVE_LOW 0 -#define MR_SPI_CS_ACTIVE_HIGH 1 -#define MR_SPI_CS_ACTIVE_NONE 2 - -/* Default config for mr_spi_config structure */ -#define MR_SPI_CONFIG_DEFAULT \ -{ \ - 3000000, \ - MR_SPI_HOST, \ - MR_SPI_MODE_0, \ - MR_SPI_BIT_ORDER_MSB, \ - MR_SPI_CS_ACTIVE_LOW, \ -} - -struct mr_spi_config -{ - mr_uint32_t baud_rate; - - mr_uint8_t host_slave: 1; - mr_uint8_t mode: 2; - mr_uint8_t bit_order: 1; - mr_uint8_t cs_active: 1; -}; - -typedef struct mr_spi_bus *mr_spi_bus_t; -struct mr_spi_device -{ - struct mr_device device; - - struct mr_spi_config config; - struct mr_spi_bus *bus; - mr_uint16_t cs_pin; -}; -typedef struct mr_spi_device *mr_spi_device_t; - -struct mr_spi_bus_ops -{ - mr_err_t (*configure)(mr_spi_bus_t spi_bus, struct mr_spi_config *config); - void (*write)(mr_spi_bus_t spi_bus, mr_uint8_t data); - mr_uint8_t (*read)(mr_spi_bus_t spi_bus); - void (*cs_set)(mr_spi_bus_t spi_bus, mr_uint16_t cs_pin, mr_state_t state); -}; - -struct mr_spi_bus -{ - struct mr_device device; - - struct mr_spi_config config; - struct mr_spi_device *owner; - struct mr_mutex lock; - - const struct mr_spi_bus_ops *ops; -}; - -mr_err_t mr_hw_spi_bus_add(mr_spi_bus_t spi_bus, const char *name, struct mr_spi_bus_ops *ops, void *data); -mr_err_t mr_hw_spi_device_add(mr_spi_device_t spi_device, - const char *name, - mr_uint16_t support_flag, - mr_uint16_t cs_pin, - const char *bus_name); - -#endif - -#endif \ No newline at end of file diff --git a/device/timer/timer.c b/device/timer/timer.c deleted file mode 100644 index 73b7dad..0000000 --- a/device/timer/timer.c +++ /dev/null @@ -1,287 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#include "device/timer/timer.h" - -#if (MR_CONF_DEVICE_TIMER == MR_CONF_ENABLE) - -#undef LOG_TAG -#define LOG_TAG "timer" - -static mr_uint32_t mr_timer_timeout_calculate(mr_timer_t timer, mr_uint32_t timeout) -{ - mr_uint32_t count = 0, timer_period = 0, reload = 0; - mr_uint32_t i = 0, reload_min = 0, reload_max = 0, error = 0, error_min = 0, reload_best = 0; - - /* Calculate the timer-period and timeout total count */ - timer_period = 1000000 / timer->config.freq; - count = timeout / timer_period; - - if (count == 0) - count = 1; - - if (count < timer->information.max_cut) - { - timer->reload = 1; - timer->cycles = timer->reload; - timer->timeout = count * timer_period; - return count; - } - - if (count % timer->information.max_cut == 0) - { - timer->reload = count / timer->information.max_cut; - timer->cycles = timer->reload; - timer->timeout = timer->information.max_cut * timer_period; - return timer->information.max_cut; - } - - /* Calculate the Least error reload */ - reload_min = count / timer->information.max_cut + 1; - reload_max = count / 5; - error_min = reload_min; - for (i = reload_min; i < reload_max; i ++) - { - reload = count / i; - error = count - reload * i; - if (error <= 1) - { - reload_best = i; - break; - } else if (error < error_min) - { - error_min = error; - reload_best = i; - } - } - - timer->reload = reload_best; - timer->cycles = timer->reload; - timer->timeout = count / reload_best * timer_period; - - return count / reload_best; -} - -static mr_err_t mr_timer_open(mr_device_t device) -{ - mr_timer_t timer = (mr_timer_t)device; - struct mr_timer_config config = MR_TIMER_CONFIG_DEFAULT; - - if (timer->config.freq == 0) - timer->config = config; - - if (timer->config.freq > timer->information.max_freq) - return - MR_ERR_GENERIC; - - return timer->ops->configure(timer, &timer->config); -} - -static mr_err_t mr_timer_close(mr_device_t device) -{ - mr_timer_t timer = (mr_timer_t)device; - - /* Setting timer to close-config */ - timer->config.freq = 0; - - return timer->ops->configure(timer, &timer->config); -} - -static mr_err_t mr_timer_ioctl(mr_device_t device, int cmd, void *args) -{ - mr_timer_t timer = (mr_timer_t)device; - mr_err_t ret = MR_ERR_OK; - - switch (cmd & _MR_CTRL_FLAG_MASK) - { - case MR_CTRL_CONFIG: - { - if (args) - { - /* Check the frequency */ - if (((struct mr_timer_config *)args)->freq > timer->information.max_freq) - return - MR_ERR_GENERIC; - - ret = timer->ops->configure(timer, (struct mr_timer_config *)args); - if (ret == MR_ERR_OK) - timer->config = *(struct mr_timer_config *)args; - - return ret; - } - return - MR_ERR_INVALID; - } - - case MR_CTRL_SET_RX_CB: - { - device->rx_cb = args; - return MR_ERR_OK; - } - - case MR_CTRL_REBOOT: - { - timer->overflow = 0; - timer->cycles = timer->reload; - timer->ops->start(timer, timer->timeout / (1000000 / timer->config.freq)); - return MR_ERR_OK; - } - - default: return - MR_ERR_UNSUPPORTED; - } -} - -static mr_ssize_t mr_timer_read(mr_device_t device, mr_off_t pos, void *buffer, mr_size_t size) -{ - mr_timer_t timer = (mr_timer_t)device; - mr_uint32_t *recv_buffer = (mr_uint32_t *)buffer; - mr_uint32_t cut = 0; - - if (size < sizeof(*recv_buffer)) - { - MR_LOG_E(LOG_TAG, "Device %s: Invalid read size %d\r\n", device->object.name, size); - return - MR_ERR_INVALID; - } - - cut = timer->ops->get_count(timer); - if (timer->information.cut_mode == _MR_TIMER_CUT_MODE_DOWN) - cut = timer->timeout / (1000000 / timer->config.freq) - cut; - - *recv_buffer = timer->overflow * timer->timeout + cut * (1000000 / timer->config.freq); - - return sizeof(*recv_buffer); -} - -static mr_ssize_t mr_timer_write(mr_device_t device, mr_off_t pos, const void *buffer, mr_size_t size) -{ - mr_timer_t timer = (mr_timer_t)device; - mr_uint32_t *send_buffer = (mr_uint32_t *)buffer; - mr_uint32_t period_reload = 0; - - if (size < sizeof(*send_buffer)) - { - MR_LOG_E(LOG_TAG, "Device %s: Invalid write size %d\r\n", device->object.name, size); - return - MR_ERR_INVALID; - } - - if (timer->config.freq == 0) - { - MR_LOG_E(LOG_TAG, "Device %s: Invalid frequency %d\r\n", device->object.name, timer->config.freq); - return - MR_ERR_GENERIC; - } - - timer->ops->stop(timer); - period_reload = mr_timer_timeout_calculate(timer, *send_buffer); - - /* When the time is not less than one time, the timer is started */ - if (timer->cycles != 0) - timer->ops->start(timer, period_reload); - - return sizeof(*send_buffer); -} - -static mr_err_t _err_io_timer_configure(mr_timer_t timer, struct mr_timer_config *config) -{ - MR_ASSERT(0); - return - MR_ERR_IO; -} - -static mr_err_t _err_io_timer_start(mr_timer_t timer, mr_uint32_t period_reload) -{ - MR_ASSERT(0); - return - MR_ERR_IO; -} - -static mr_err_t _err_io_timer_stop(mr_timer_t timer) -{ - MR_ASSERT(0); - return - MR_ERR_IO; -} - -static mr_uint32_t _err_io_timer_get_count(mr_timer_t timer) -{ - MR_ASSERT(0); - return 0; -} - -mr_err_t mr_hw_timer_add(mr_timer_t timer, - const char *name, - struct mr_timer_ops *ops, - struct mr_timer_information *information, - void *data) -{ - mr_err_t ret = MR_ERR_OK; - const static struct mr_device_ops device_ops = - { - mr_timer_open, - mr_timer_close, - mr_timer_ioctl, - mr_timer_read, - mr_timer_write, - }; - - MR_ASSERT(timer != MR_NULL); - MR_ASSERT(ops != MR_NULL); - MR_ASSERT(information != MR_NULL); - MR_ASSERT(information->max_freq != 0); - MR_ASSERT(information->max_cut != 0); - - /* Add the timer-device to the container */ - ret = mr_device_add(&timer->device, name, MR_DEVICE_TYPE_TIMER, MR_OPEN_RDWR, &device_ops, data); - if (ret != MR_ERR_OK) - return ret; - - /* Initialize the timer fields */ - timer->config.freq = 0; - timer->information = *information; - timer->reload = 0; - timer->cycles = 0; - timer->overflow = 0; - timer->timeout = 0; - - /* Set timer operations as protect functions if ops is null */ - ops->configure = ops->configure ? ops->configure : _err_io_timer_configure; - ops->start = ops->start ? ops->start : _err_io_timer_start; - ops->stop = ops->stop ? ops->stop : _err_io_timer_stop; - ops->get_count = ops->get_count ? ops->get_count : _err_io_timer_get_count; - timer->ops = ops; - - return MR_ERR_OK; -} - -void mr_hw_timer_isr(mr_timer_t timer, mr_uint16_t event) -{ - switch (event & _MR_TIMER_EVENT_MASK) - { - case MR_TIMER_EVENT_PIT_INT: - { - timer->overflow ++; - - if (timer->cycles != 0) - { - timer->cycles --; - } - - /* Timeout */ - if (timer->cycles == 0) - { - timer->cycles = timer->reload; - - if (timer->config.mode == MR_TIMER_MODE_ONE_SHOT) - timer->ops->stop(timer); - - /* Invoke the rx-cb function */ - if (timer->device.rx_cb != MR_NULL) - timer->device.rx_cb(&timer->device, MR_NULL); - } - } - - default:break; - } -} - -#endif diff --git a/device/timer/timer.h b/device/timer/timer.h deleted file mode 100644 index 5427dbf..0000000 --- a/device/timer/timer.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#ifndef _TIMER_H_ -#define _TIMER_H_ - -#include "mrlib.h" - -#if (MR_CONF_DEVICE_TIMER == MR_CONF_ENABLE) - -#define MR_TIMER_MODE_PERIOD 0 -#define MR_TIMER_MODE_ONE_SHOT 1 - -#define _MR_TIMER_CUT_MODE_UP 0 -#define _MR_TIMER_CUT_MODE_DOWN 1 - -#define MR_TIMER_EVENT_PIT_INT 0x1000 -#define _MR_TIMER_EVENT_MASK 0xf000 - -/* Default config for mr_timer_config structure */ -#define MR_TIMER_CONFIG_DEFAULT \ -{ \ - 1000, \ - MR_TIMER_MODE_PERIOD, \ -} - -struct mr_timer_config -{ - mr_uint32_t freq; - mr_uint8_t mode; -}; - -struct mr_timer_information -{ - mr_uint32_t max_freq; - mr_uint32_t max_cut; - mr_uint8_t cut_mode; -}; - -typedef struct mr_timer *mr_timer_t; -struct mr_timer_ops -{ - mr_err_t (*configure)(mr_timer_t timer, struct mr_timer_config *config); - mr_err_t (*start)(mr_timer_t timer, mr_uint32_t period_reload); - mr_err_t (*stop)(mr_timer_t timer); - mr_uint32_t (*get_count)(mr_timer_t timer); -}; - -struct mr_timer -{ - struct mr_device device; - - struct mr_timer_config config; - struct mr_timer_information information; - mr_uint32_t reload; - mr_uint32_t cycles; - mr_uint32_t overflow; - mr_uint32_t timeout; - - const struct mr_timer_ops *ops; -}; - -mr_err_t mr_hw_timer_add(mr_timer_t timer, - const char *name, - struct mr_timer_ops *ops, - struct mr_timer_information *information, - void *data); -void mr_hw_timer_isr(mr_timer_t timer, mr_uint16_t event); - -#endif - -#endif diff --git a/document/developer/coding_style.md b/document/developer/coding_style.md deleted file mode 100644 index e8ee9d5..0000000 --- a/document/developer/coding_style.md +++ /dev/null @@ -1,177 +0,0 @@ -## 格式与风格 - -### 缩进与换行 - -1. 仅使用4个空格进行代码缩进,禁止使用tab键。 -2. 每行代码长度控制在120个字符内,超过换行,下一行缩进4个空格。 -3. 大括号`{`与`}`单独占一行。 - 例如: - - ```c - if (condition) - { - do_something(); - } - - void func(void) - { - ... - } - ``` - -### 空格与空行 - -1. 操作符两边各加一个空格。 - 例如: - - ```c - x = 1; - a + b; - ``` - -2. 逗号`,`后加一个空格。 - 例如: - - ```c - void foo(int a, char b) - ``` - -### 头文件 - -1. 头文件为避免重复包含,使用`_FILE_H_`宏进行保护。 -2. 头文件按字母顺序导入,库文件优先。库头文件使用`<>`导入,用户头文件使用`""`导入。 - 例如: - - ```c - #include - #include - #include "bar.h" - #include "foo.h" - ``` - -## 命名规范 - -### 类型 - -1. 枚举类型命名全部小写,下划线分隔,枚举常量全部大写。仅开发者使用的枚举常量以`_`开头 - 例如: - - ```c - enum color_type - { - RED, - GREEN, - BLUE, - _PRIVATE_ENUM, - }; - ``` - -2. 结构体、联合体、类型定义命名小写,下划线分隔。 - 例如: - - ```c - struct foo - { - int bar; - union - { - int baz; - char qux; - } - }; - ``` - -### 函数与变量 - -1. 函数命名小写,下划线分隔,硬件相关函数加`hw`标识,中断服务函数加`isr`标识。 - 例如: - - ```c - void do_something(int arg1, char arg2) - { - ... - } - - mr_err_t mr_hw_serial_add(mr_serial_t serial, const char *name, struct mr_serial_ops *ops, void *data) - { - ... - } - - void mr_hw_serial_isr(mr_serial_t serial, mr_uint32_t event) - { - ... - } - ``` - -2. 变量命名小写,下划线分隔 - 例如: - - ```c - int num_times; - char *p; - ``` - -3. 常量命名全部大写,下划线分隔。 - 例如: - - ```c - #define PI 3.14 - ``` - -4. typedef定义使用类型名加`_t`后缀。 - 例如: - - ```c - typedef unsigned int mr_uint32_t; - typedef struct mr_object *mr_object_t; - ``` - -### 文件名 - -1. 文件名使用小写字母和下划线,不使用大写字母,配置文件以`config`结尾。 - 例如: - - ```c - foo_bar.c - test_case.h - mrconfig.h - ``` - -### 宏 - -1. 宏定义全部大写,下划线分隔,用户可修改的宏配置需加`CONF`标识,仅开发者使用的宏以`_`开头。 - 例如: - - ```c - #define MR_CONF_NAME_MAX 15 - #define _PRIVATE_MACRO - ``` - -## 注释规范 - -1. 单行注释使用`/* */`,多行注释使用: - - ```c - /* - * 多行注释 - * 第二行 - */ - ``` - -2. 函数注释使用Doxygen风格: - - ```c - /** - * @brief This function add object to the container. - * - * @param object The object to be added. - * @param name The name of the object. - * @param type The target container flag. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ - mr_err_t mr_object_add(mr_object_t object, const char *name, enum container_type type) - { - ... - } - ``` \ No newline at end of file diff --git a/document/user/usage_guide.md b/document/user/usage_guide.md deleted file mode 100644 index 757f5c4..0000000 --- a/document/user/usage_guide.md +++ /dev/null @@ -1,45 +0,0 @@ -# 使用指南 - - ---------- - -## 准备工作 - -在开始之前,请从 [gitee仓库](https://gitee.com/MacRsh/mr-library.git) 下载源代码。 - -
源代码包含以下文件: - -| 名称 | 描述 | -|:-----------|:--------| -| mr_library | 库文件 | -| -bsp | 板级支持包 | -| -config | 配置文件 | -| -device | 设备抽象层文件 | -| -document | 文档 | -| -driver | 驱动文件 | -| -include | 库头文件 | -| -src | 库源文件 | - -选择工程所需的文件: - -| 名称 | 描述 | -|:-----------|:--------| -| mr_library | 库文件 | -| -config | 配置文件 | -| -device | 设备抽象层文件 | -| -driver | 驱动文件 | -| -include | 库头文件 | -| -src | 库源文件 | - - ---------- - -## 配置工程 - -1. 从 `bsp` 目录中选择相应芯片的驱动文件,并将它们放入 `driver` 目录中。同时,在 `config` 目录下配置 `board.c` - 和 `board.h` 文件以适配您的目标平台(或直接从 `bsp` 目录中获取相应文件并覆盖 `config` 目录中的文件)。 -2. 将 `mr_library` 文件添加到项目中,并将文件路径添加到项目设置中。 -3. 引用 `mr_lib.h` 头文件以使用内核部分。如果在 `mr_config.h` 中启用了 `Device` 下的任何设备,请引用 `mr_drv.h` - 头文件以使用设备驱动部分。 - - ---------- - -### 倘若需要更加详尽地说明与指引,请参考相应内容的使用手册,以便更好地理解与运用本库的其他部分。 \ No newline at end of file diff --git a/driver/mrdrv.h b/driver/mrdrv.h deleted file mode 100644 index f5948e0..0000000 --- a/driver/mrdrv.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#ifndef _MR_DRV_H_ -#define _MR_DRV_H_ - -#include "mrconfig.h" - -#if (MR_CONF_DEVICE_SERIAL == MR_CONF_ENABLE) -#include "drv_uart.h" -#endif - -#if (MR_CONF_DEVICE_PIN == MR_CONF_ENABLE) -#include "drv_gpio.h" -#endif - -#endif diff --git a/include/mrdef.h b/include/mrdef.h deleted file mode 100644 index 2e9db95..0000000 --- a/include/mrdef.h +++ /dev/null @@ -1,340 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-05-08 MacRsh first version - */ - -#ifndef _MR_DEF_H_ -#define _MR_DEF_H_ - -#include "mrconfig.h" - -#include -#include -#include - -#include -#include - -#define mr_malloc malloc -#define mr_free free -#define mr_strncmp strncmp -#define mr_strncpy strncpy -#define mr_memset memset -#define mr_memcpy memcpy -#define mr_printf printf - -/* mr-library version information */ -#define MR_LIBRARY_VERSION "0.0.2" - -/* Compiler Related Definitions */ -#if defined(__ARMCC_VERSION) -#define mr_section(x) __attribute__((section(x))) -#define mr_used __attribute__((used)) -#define mr_align(n) __attribute__((aligned(n))) -#define mr_weak __attribute__((weak)) -#define mr_inline static __inline - -#elif defined (__IAR_SYSTEMS_ICC__) -#define mr_section(x) @ x -#define mr_used __root -#define mr_pragma(x) _Pragma(#x) -#define mr_align(n) PRAGMA(data_alignment=n) -#define mr_weak __weak -#define mr_inline static inline - -#elif defined (__GNUC__) -#define mr_section(x) __attribute__((section(x))) -#define mr_used __attribute__((used)) -#define mr_align(n) __attribute__((aligned(n))) -#define mr_weak __attribute__((weak)) -#define mr_inline static __inline - -#elif defined (__ADSPBLACKFIN__) -#define mr_section(x) __attribute__((section(x))) -#define mr_used __attribute__((used)) -#define mr_align(n) __attribute__((aligned(n))) -#define mr_weak __attribute__((weak)) -#define mr_inline static inline - -#elif defined (_MSC_VER) -#define mr_section(x) -#define mr_used -#define mr_align(n) __declspec(align(n)) -#define mr_weak -#define mr_inline static __inline - -#elif defined (__TASKING__) -#define mr_section(x) __attribute__((section(x))) -#define mr_used __attribute__((used, protect)) -#define mr_align(n) __attribute__((__align(n))) -#define mr_weak __attribute__((weak)) -#define mr_inline static inline -#endif - -/* mr-library error code definitions */ -#define MR_ERR_OK 0 /**< There is no error */ -#define MR_ERR_GENERIC 1 /**< A generic error happens */ -#define MR_ERR_NO_MEMORY 2 /**< No memory */ -#define MR_ERR_IO 3 /**< IO error */ -#define MR_ERR_TIMEOUT 4 /**< Timed out */ -#define MR_ERR_BUSY 5 /**< Busy */ -#define MR_ERR_NOT_FOUND 6 /**< Not found */ -#define MR_ERR_UNSUPPORTED 7 /**< Unsupported feature */ -#define MR_ERR_INVALID 8 /**< Invalid parameter */ - -/* mr-library basic open flag definitions */ -#define MR_OPEN_CLOSED 0x0000 /**< Closed */ -#define MR_OPEN_RDONLY 0x1000 /**< Read only */ -#define MR_OPEN_WRONLY 0x2000 /**< Write only */ -#define MR_OPEN_RDWR 0x3000 /**< Read and write */ -#define MR_OPEN_NONBLOCKING 0x4000 /**< Non-blocking */ -#define MR_OPEN_ACTIVE 0x8000 /**< Active */ -#define _MR_OPEN_FLAG_MASK 0xf000 /**< Mask for getting open flag */ - -/* mr-library basic control flag definitions */ -#define MR_CTRL_NONE 0x0000 /**< No control */ -#define MR_CTRL_CONFIG 0x1000 /**< Configure */ -#define MR_CTRL_SET_RX_CB 0x2000 /**< Set receive callback */ -#define MR_CTRL_SET_TX_CB 0x3000 /**< Set transmit callback */ -#define MR_CTRL_ATTACH 0x4000 /**< Attach the bus */ -#define MR_CTRL_REBOOT 0x5000 /**< Reboot */ -#define _MR_CTRL_FLAG_MASK 0xf000 /**< Mask for getting control flag */ - -/* mr-library basic data type definitions */ -#define MR_NULL 0 /**< Null pointer */ -#define MR_FALSE 0 /**< False */ -#define MR_TRUE 1 /**< True */ -#define MR_LOW 0 /**< Low level */ -#define MR_HIGH 1 /**< High level */ -#define MR_DISABLE 0 /**< Disable */ -#define MR_ENABLE 1 /**< Enable */ -#define MR_UNLOCK 0 /**< Unlock */ -#define MR_LOCK 1 /**< Lock */ - -typedef signed char mr_int8_t; /**< Type for 8bit integer */ -typedef signed short mr_int16_t; /**< Type for 16bit integer */ -typedef signed int mr_int32_t; /**< Type for 32bit integer */ -typedef signed long long mr_int64_t; /**< Type for 64bit integer */ -typedef unsigned char mr_uint8_t; /**< Type for 8bit unsigned integer */ -typedef unsigned short mr_uint16_t; /**< Type for 16bit unsigned integer */ -typedef unsigned int mr_uint32_t; /**< Type for 32bit unsigned integer */ -typedef unsigned long long mr_uint64_t; /**< Type for 64bit unsigned integer */ -typedef float mr_fp32_t; /**< Type for single-precision floating point */ -typedef double mr_fp64_t; /**< Type for double-precision floating point */ - -typedef signed int mr_base_t; /**< Type for Nbit CPU related date */ -typedef unsigned int mr_ubase_t; /**< Type for Nbit unsigned CPU related data */ -typedef mr_ubase_t mr_size_t; /**< Type for size number */ -typedef mr_base_t mr_ssize_t; /**< Type for signed size number */ -typedef mr_base_t mr_err_t; /**< Type for error number */ -typedef mr_base_t mr_off_t; /**< Type for offset */ -typedef mr_int8_t mr_bool_t; /**< Type for boolean */ -typedef mr_int8_t mr_level_t; /**< Type for level */ -typedef mr_int8_t mr_state_t; /**< Type for state */ -typedef mr_int8_t mr_lock_t; /**< Type for lock */ - -/** - * Auto-Init - */ -typedef int (*init_fn_t)(void); -#define INIT_EXPORT(fn,level) \ - mr_used const init_fn_t _mr_init_##fn mr_section(".mri_fn."level) = fn - -#define INIT_BOARD_EXPORT(fn) INIT_EXPORT(fn, "1") -#define INIT_DEV_EXPORT(fn) INIT_EXPORT(fn, "2") -#define INIT_ENV_EXPORT(fn) INIT_EXPORT(fn, "3") -#define INIT_APP_EXPORT(fn) INIT_EXPORT(fn, "4") - -/** - * Double-list - */ -struct mr_list -{ - struct mr_list *next; /**< Point to next node */ - struct mr_list *prev; /**< Point to prev node */ -}; -typedef struct mr_list *mr_list_t; /**< Type for list */ - -/** - * Avl-tree - */ -struct mr_avl -{ - mr_int8_t height; /**< Balance factor */ - mr_uint32_t value; /**< Key-value */ - - struct mr_avl *left_child; /**< Point to left-child node */ - struct mr_avl *right_child; /**< Point to right-child node */ -}; -typedef struct mr_avl *mr_avl_t; /**< Type for avl-tree */ - -/** - * Fifo - */ -struct mr_fifo -{ - mr_uint8_t *buffer; /**< Buffer pool */ - - mr_uint16_t read_mirror: 1; /**< Read mirror flag */ - mr_uint16_t read_index: 15; /**< Read index */ - mr_uint16_t write_mirror: 1; /**< Write mirror flag */ - mr_uint16_t write_index: 15; /**< Write index */ - - mr_uint16_t size; /**< Buffer pool size */ -}; -typedef struct mr_fifo *mr_fifo_t; /**< Type for fifo */ - -/** - * Container - */ -enum mr_container_type -{ - MR_CONTAINER_TYPE_MISC, /**< Miscellaneous container */ - MR_CONTAINER_TYPE_DEVICE, /**< Device container */ - MR_CONTAINER_TYPE_MANAGER, /**< Event container */ - _MR_CONTAINER_TYPE_MASK, /**< Mask for getting container type */ -}; - -struct mr_container -{ - struct mr_list list; /**< Container list */ - - enum mr_container_type type; /**< Container flag */ -}; -typedef struct mr_container *mr_container_t; /**< Type for container */ - -/** - * Object - */ -#define _MR_OBJECT_TYPE_NONE 0x00 -#define _MR_OBJECT_TYPE_REGISTER 0x10 - -struct mr_object -{ - struct mr_list list; /**< Object list */ - - char name[MR_CONF_NAME_MAX + 1]; /**< Object name */ - mr_uint8_t flag; /**< Object flag */ -}; -typedef struct mr_object *mr_object_t; /**< Type for object */ - -/** - * Mutex - */ -struct mr_mutex -{ - mr_object_t owner; /**< Mutex owns the object */ - mr_lock_t lock; /**< Mutex lock state */ -}; -typedef struct mr_mutex *mr_mutex_t; /**< Type for mutex */ - -/** - * Device - */ -enum mr_device_type -{ - MR_DEVICE_TYPE_NONE, /**< No device */ - MR_DEVICE_TYPE_PIN, /**< GPIO device */ - MR_DEVICE_TYPE_SPI_BUS, /**< SPI-BUS device */ - MR_DEVICE_TYPE_SPI, /**< SPI device */ - MR_DEVICE_TYPE_I2C_BUS, /**< I2C-BUS device */ - MR_DEVICE_TYPE_I2C, /**< I2C device */ - MR_DEVICE_TYPE_SERIAL, /**< UART device */ - MR_DEVICE_TYPE_ADC, /**< ADC device */ - MR_DEVICE_TYPE_DAC, /**< DAC device */ - MR_DEVICE_TYPE_PWM, /**< PWM device */ - MR_DEVICE_TYPE_TIMER, /**< TIMER device */ - MR_DEVICE_TYPE_FLASH, /**< FLASH device */ - MR_DEVICE_TYPE_SENSOR, /**< SENSOR device */ - /* ... */ -}; - -typedef struct mr_device *mr_device_t; /**< Type for device */ -struct mr_device_ops -{ - mr_err_t (*open)(mr_device_t device); - mr_err_t (*close)(mr_device_t device); - mr_err_t (*ioctl)(mr_device_t device, int cmd, void *args); - mr_ssize_t (*read)(mr_device_t device, mr_off_t pos, void *buffer, mr_size_t size); - mr_ssize_t (*write)(mr_device_t device, mr_off_t pos, const void *buffer, mr_size_t size); -}; - -struct mr_device -{ - struct mr_object object; /**< Device object */ - - enum mr_device_type type; /**< Device type */ - mr_uint16_t support_flag; /**< Open mode supported by the device */ - mr_uint16_t open_flag; /**< Opening mode of the device */ - mr_size_t ref_count; /**< Number of device references */ - void *data; /**< Device data */ - - mr_err_t (*rx_cb)(mr_device_t device, void *args); /**< Device receives the baud function */ - mr_err_t (*tx_cb)(mr_device_t device, void *args); /**< Device sends the baud function */ - - const struct mr_device_ops *ops; /**< Operations of the device */ -}; - -/** - * Manager - */ -enum mr_manager_type -{ - MR_MANAGER_TYPE_EVENT, /**< Event manager */ - MR_MANAGER_TYPE_FSM, /**< Finite state machine(FSM) manager */ - MR_MANAGER_TYPE_AT_PARSER, /**< Attention(AT) Parser manager */ - MR_MANAGER_TYPE_CMD_PARSER, /**< Command(CMD) Parser manager */ - /* ... */ -}; - -enum mr_manager_at_parser_state -{ - MR_MANAGER_AT_STATE_NONE, /**< No state */ - MR_MANAGER_AT_STATE_START, /**< Start state */ - MR_MANAGER_AT_STATE_FLAG, /**< Flag state */ - MR_MANAGER_AT_STATE_ID, /**< Id state */ - MR_MANAGER_AT_STATE_CHECK, /**< Check state */ - MR_MANAGER_AT_STATE_ARGS, /**< Args state */ - MR_MANAGER_AT_STATE_STOP, /**< Stop state */ - MR_MANAGER_AT_STATE_HANDLE, /**< Handle state */ -}; - -typedef struct mr_manager *mr_manager_t; /**< Type for manager */ -struct mr_manager_ops -{ - mr_err_t (*add)(mr_manager_t manager); - mr_err_t (*remove)(mr_manager_t manager); - void (*handler)(mr_manager_t manage); -}; - -struct mr_manager -{ - struct mr_object object; /**< Manager object */ - - enum mr_manager_type type; /**< Manager type */ - void *data; /**< Manager data */ - struct mr_fifo queue; /**< Agent queue */ - - mr_avl_t avl; /**< Manager list */ - - const struct mr_manager_ops *ops; /**< Operations of the manager */ -}; - -/** - * Agent - */ -struct mr_agent -{ - struct mr_avl avl; /**< Avl-tree and agent id */ - - mr_err_t (*cb)(mr_manager_t manager, void *args); /**< Agent occurrence callback function */ - void *args; /**< Callback function argument */ -}; -typedef struct mr_agent *mr_agent_t; /**< Type for agent */ - -#endif \ No newline at end of file diff --git a/include/mrlib.h b/include/mrlib.h deleted file mode 100644 index 6cbd292..0000000 --- a/include/mrlib.h +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#ifndef _MR_LIB_H_ -#define _MR_LIB_H_ - -#include "mrdef.h" -#include "mrservice.h" -#include "mrlog.h" - -/** - * Export hardware functions - */ -void mr_hw_interrupt_disable(void); -void mr_hw_interrupt_enable(void); -void mr_hw_delay_ms(mr_uint32_t ms); - -/** - * Export avl-tree functions - */ -void mr_avl_init(mr_avl_t node, mr_uint32_t value); -void mr_avl_insert(mr_avl_t *tree, mr_avl_t node); -void mr_avl_remove(mr_avl_t *tree, mr_avl_t node); -mr_avl_t mr_avl_find(mr_avl_t tree, mr_uint32_t value); -mr_size_t mr_avl_get_length(mr_avl_t tree); - -/** - * Export kservice functions - */ -void mr_auto_init(void); -mr_uint32_t mr_strhase(const char *str); -mr_uint32_t mr_strnhase(const char *str, mr_size_t length); - -/** - * Export fifo functions - */ -void mr_fifo_init(mr_fifo_t fifo, mr_uint8_t *pool, mr_size_t pool_size); -void mr_fifo_reset(mr_fifo_t fifo); -mr_size_t mr_fifo_get_data_size(mr_fifo_t fifo); -mr_size_t mr_fifo_get_space_size(mr_fifo_t fifo); -mr_size_t mr_fifo_get_buffer_size(mr_fifo_t fifo); -mr_size_t mr_fifo_read(mr_fifo_t fifo, mr_uint8_t *buffer, mr_size_t size); -mr_size_t mr_fifo_write(mr_fifo_t fifo, const mr_uint8_t *buffer, mr_size_t size); -mr_size_t mr_fifo_write_force(mr_fifo_t fifo, const mr_uint8_t *buffer, mr_size_t size); - -/** - * Export container functions - */ -mr_container_t mr_container_find(enum mr_container_type type); - -/** - * Export object functions - */ -mr_object_t mr_object_find(const char *name, enum mr_container_type type); -mr_err_t mr_object_add(mr_object_t object, const char *name, enum mr_container_type container_type); -mr_err_t mr_object_remove(mr_object_t object); -mr_err_t mr_object_move(mr_object_t object, enum mr_container_type dest_type); -void mr_object_rename(mr_object_t object, char *name); - -/** - * Export mutex functions - */ -void mr_mutex_init(mr_mutex_t mutex); -mr_err_t mr_mutex_take(mr_mutex_t mutex, mr_object_t owner); -mr_err_t mr_mutex_release(mr_mutex_t mutex, mr_object_t owner); - -/** - * Export device functions - */ -mr_device_t mr_device_find(const char *name); -mr_err_t mr_device_add(mr_device_t device, - const char *name, - enum mr_device_type type, - mr_uint16_t support_flag, - const struct mr_device_ops *ops, - void *data); -mr_err_t mr_device_open(mr_device_t device, mr_uint16_t flags); -mr_err_t mr_device_close(mr_device_t device); -mr_err_t mr_device_ioctl(mr_device_t device, int cmd, void *args); -mr_ssize_t mr_device_read(mr_device_t device, mr_off_t pos, void *buffer, mr_size_t size); -mr_ssize_t mr_device_write(mr_device_t device, mr_off_t pos, const void *buffer, mr_size_t size); - -/** - * Export manager functions - */ -mr_manager_t mr_manager_find(const char *name); -mr_err_t mr_manager_add(mr_manager_t manager, - const char *name, - enum mr_manager_type type, - mr_size_t queue_number, - struct mr_manager_ops *ops, - void *data); -mr_err_t mr_manager_remove(mr_manager_t manager); -mr_err_t mr_manager_notify(mr_manager_t manager, mr_uint32_t id); -void mr_manager_handler(mr_manager_t manager); - -/** - * Export agent functions - */ -mr_agent_t mr_agent_find(mr_uint32_t id, mr_manager_t manager); -mr_err_t mr_agent_create(mr_uint32_t id, - mr_err_t (*callback)(mr_manager_t manager, void *args), - void *args, - mr_manager_t target_manager); -mr_err_t mr_agent_delete(mr_uint32_t id, mr_manager_t manager); - -/** - * Export event functions - */ -mr_err_t mr_event_manager_add(mr_manager_t manager, const char *name, mr_size_t queue_number); -mr_err_t mr_event_create(mr_uint32_t event, - mr_err_t (*callback)(mr_manager_t manager, void *args), - void *args, - mr_manager_t target_manager); -mr_err_t mr_event_delete(mr_uint32_t event, mr_manager_t manager); - -/** - * Export fsm functions - */ -mr_err_t mr_fsm_manager_add(mr_manager_t manager, const char *name, mr_size_t queue_number); -mr_err_t mr_fsm_state_create(mr_uint32_t state, - mr_err_t (*callback)(mr_manager_t manager, void *args), - void *args, - mr_manager_t target_manager); -mr_err_t mr_fsm_state_delete(mr_uint32_t state, mr_manager_t manager); - -/** - * Export at-parser functions - */ -mr_err_t mr_at_parser_manager_add(mr_manager_t manager, const char *name, mr_size_t queue_number); -mr_err_t mr_at_cmd_create(const char *at_cmd, - mr_err_t (*callback)(mr_manager_t manager, void *args), - mr_manager_t target_manager); -mr_err_t mr_at_cmd_delete(mr_uint32_t at_cmd, mr_manager_t manager); -void mr_at_parser_isr(mr_manager_t manager, char data); - -#endif \ No newline at end of file diff --git a/include/mrlog.h b/include/mrlog.h deleted file mode 100644 index 9d53cb7..0000000 --- a/include/mrlog.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#ifndef _MR_LOG_H_ -#define _MR_LOG_H_ - -#include "mrdef.h" - -#define MR_LOG_A(TAG, FORMAT, ...) mr_log_output(MR_CONF_LOG_LEVEL_ASSERT, TAG, FORMAT, ##__VA_ARGS__) -#define MR_LOG_E(TAG, FORMAT, ...) mr_log_output(MR_CONF_LOG_LEVEL_ERROR, TAG, FORMAT, ##__VA_ARGS__) -#define MR_LOG_W(TAG, FORMAT, ...) mr_log_output(MR_CONF_LOG_LEVEL_WARNING, TAG, FORMAT, ##__VA_ARGS__) -#define MR_LOG_I(TAG, FORMAT, ...) mr_log_output(MR_CONF_LOG_LEVEL_INFO, TAG, FORMAT, ##__VA_ARGS__) -#define MR_LOG_D(TAG, FORMAT, ...) mr_log_output(MR_CONF_LOG_LEVEL_DEBUG, TAG, FORMAT, ##__VA_ARGS__) - -#if (MR_CONF_LOG_ASSERT == MR_CONF_ENABLE) -#define MR_ASSERT(EX) \ - do{ \ - if (!(EX)) \ - { \ - MR_LOG_A(__FUNCTION__, \ - "File: %s, Line: %d\r\n", \ - __FILE__, \ - __LINE__); \ - mr_assert_handler(); \ - } \ - }while(0) -#else -#define MR_ASSERT(EX) -#endif - -void mr_log_output(mr_base_t level, const char *tag, const char *format, ...); -void mr_assert_handler(void); - -#endif \ No newline at end of file diff --git a/include/mrservice.h b/include/mrservice.h deleted file mode 100644 index 36c0fe9..0000000 --- a/include/mrservice.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#ifndef _MR_SERVE_H_ -#define _MR_SERVE_H_ - -#include "mrdef.h" - -#define mr_struct_of(ptr, type, member) \ - ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member))) - -#define mr_array_get_length(array) sizeof(array)/sizeof(array[0]) - -#define mr_align_down(size, align) (size & (~(align - 1))) - -#define mr_max(a, b) ((a) > (b)) ? a:b -#define mr_min(a, b) ((a) < (b)) ? a:b - -mr_inline void mr_list_init(mr_list_t list) -{ - list->next = list; - list->prev = list; -} - -mr_inline void mr_list_insert_after(mr_list_t list, mr_list_t node) -{ - list->next->prev = node; - node->next = list->next; - - list->next = node; - node->prev = list; -} - -mr_inline void mr_list_remove(mr_list_t node) -{ - node->next->prev = node->prev; - node->prev->next = node->next; - - node->next = node->prev = node; -} - -mr_inline mr_size_t mr_list_get_length(mr_list_t list) -{ - mr_size_t length = 0; - mr_list_t temp_list = list; - - while (temp_list->next != list) - { - temp_list = temp_list->next; - length ++; - } - - return length; -} - -#endif diff --git a/src/device.c b/src/device.c deleted file mode 100644 index ad277ac..0000000 --- a/src/device.c +++ /dev/null @@ -1,272 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#include "mrlib.h" - -#undef LOG_TAG -#define LOG_TAG "device" - -/** - * @brief This function finds a device. - * - * @param name The name of the device. - * - * @return A handle to the found device, or MR_NULL if not found. - */ -mr_device_t mr_device_find(const char *name) -{ - MR_ASSERT(name != MR_NULL); - - /* Find the device object from the device container */ - return (mr_device_t)mr_object_find(name, MR_CONTAINER_TYPE_DEVICE); -} - -/** - * @brief This function adds a device to the container. - * - * @param device The device to be added. - * @param name The name of the device. - * @param type The flag of the device. - * @param support_flag The open flags supported by the device. - * @param ops The operations of the device. - * @param data The data of the device. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_device_add(mr_device_t device, - const char *name, - enum mr_device_type type, - mr_uint16_t support_flag, - const struct mr_device_ops *ops, - void *data) -{ - mr_err_t ret = MR_ERR_OK; - static struct mr_device_ops null_ops = {MR_NULL}; - - MR_ASSERT(device != MR_NULL); - MR_ASSERT(name != MR_NULL); - - /* Add the object to the container */ - ret = mr_object_add(&device->object, name, MR_CONTAINER_TYPE_DEVICE); - if (ret != MR_ERR_OK) - return ret; - - /* Initialize the private fields */ - device->rx_cb = MR_NULL; - device->tx_cb = MR_NULL; - device->type = type; - device->support_flag = support_flag; - device->open_flag = MR_OPEN_CLOSED; - device->data = data; - - /* Set operations as null-ops if ops is null */ - device->ops = (ops == MR_NULL) ? &null_ops : ops; - - MR_LOG_D(LOG_TAG, "Add device %s, type %d\r\n", device->object.name, type); - - return MR_ERR_OK; -} - -/** - * @brief This function removes a device from the container. - * - * @param device The device to be removed. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_device_remove(mr_device_t device) -{ - mr_err_t ret = MR_ERR_OK; - static struct mr_device_ops null_ops = {MR_NULL}; - - MR_ASSERT(device != MR_NULL); - - /* Remove the object from the container */ - ret = mr_object_remove(&device->object); - if (ret != MR_ERR_OK) - return ret; - - /* Initialize the private fields */ - device->rx_cb = MR_NULL; - device->tx_cb = MR_NULL; - device->type = MR_DEVICE_TYPE_NONE; - device->support_flag = MR_OPEN_CLOSED; - device->open_flag = MR_OPEN_CLOSED; - device->data = MR_NULL; - - /* Set operations as null-ops */ - device->ops = &null_ops; - - MR_LOG_D(LOG_TAG, "Remove device %s\r\n", device->object.name); - - return MR_ERR_OK; -} - -/** - * @brief This function opens the device. - * - * @param device The device to be opened. - * @param flags The open flags of the device. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_device_open(mr_device_t device, mr_uint16_t flags) -{ - MR_ASSERT(device != MR_NULL); - - /* Check if the specified open flags are supported by the device */ - if (flags != (flags & device->support_flag)) - { - MR_LOG_E(LOG_TAG, "Cannot open device %s, flags not supported\r\n", device->object.name); - return - MR_ERR_UNSUPPORTED; - } - - /* Update the device open flag and reference count */ - device->open_flag |= (flags & _MR_OPEN_FLAG_MASK); - device->ref_count ++; - - MR_LOG_D(LOG_TAG, "Opened device %s, ref count %d\r\n", device->object.name, device->ref_count); - - /* Check if the device is already open */ - if ((device->open_flag & MR_OPEN_ACTIVE)) - return MR_ERR_OK; - - /* Set the device status to active */ - device->open_flag |= MR_OPEN_ACTIVE; - - /* Call the device-open function, if provided */ - if (device->ops->open == MR_NULL) - return MR_ERR_OK; - - return device->ops->open(device); -} - -/** - * @brief This function closes the device. - * - * @param device The device to be closed. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_device_close(mr_device_t device) -{ - MR_ASSERT(device != MR_NULL); - - /* Check if the device is already closed */ - if (device->open_flag == MR_OPEN_CLOSED) - return MR_ERR_OK; - - /* Decrement the reference count */ - device->ref_count --; - - MR_LOG_D(LOG_TAG, "Closed device %s, ref count %d\r\n", device->object.name, device->ref_count); - - /* If the reference count is still non-zero, return without closing the device */ - if (device->ref_count != 0) - return MR_ERR_OK; - - /* Set the device status to closed */ - device->open_flag = MR_OPEN_CLOSED; - device->rx_cb = MR_NULL; - device->tx_cb = MR_NULL; - - /* Call the device-close function, if provided */ - if (device->ops->close == MR_NULL) - return MR_ERR_OK; - - return device->ops->close(device); -} - -/** - * @brief This function controls the device. - * - * @param device The device to be controlled. - * @param cmd The operation command of the device. - * @param args The argument of command. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_device_ioctl(mr_device_t device, int cmd, void *args) -{ - MR_ASSERT(device != MR_NULL); - - /* Call the device-ioctl function, if provided */ - if (device->ops->ioctl == MR_NULL) - { - MR_LOG_E(LOG_TAG, "Device %s does not support ioctl\r\n", device->object.name); - return - MR_ERR_UNSUPPORTED; - } - - return device->ops->ioctl(device, cmd, args); -} - -/** - * @brief This function reads from the device. - * - * @param device The device to be read. - * @param pos The read position. - * @param buffer The data buffer to be read from the device. - * @param size The size of the read. - * - * @return The size of the actual read on success, otherwise an error code. - */ -mr_ssize_t mr_device_read(mr_device_t device, mr_off_t pos, void *buffer, mr_size_t size) -{ - MR_ASSERT(device != MR_NULL); - MR_ASSERT(buffer != MR_NULL); - - /* Check if the device is closed or unsupported */ - if ((device->ref_count == 0) || ! (device->open_flag & MR_OPEN_RDONLY)) - { - MR_LOG_E(LOG_TAG, "Device %s does not support read\r\n", device->object.name); - return - MR_ERR_UNSUPPORTED; - } - - /* Call the device-read function, if provided */ - if (device->ops->read == MR_NULL) - { - MR_LOG_E(LOG_TAG, "Device %s read function is null\r\n", device->object.name); - return - MR_ERR_IO; - } - - return device->ops->read(device, pos, buffer, size); -} - -/** - * @brief This function write the device. - * - * @param device The device to be written. - * @param pos The write position. - * @param buffer The data buffer to be written to device. - * @param size The size of write. - * - * @return The size of the actual write on success, otherwise return 0. - */ -mr_ssize_t mr_device_write(mr_device_t device, mr_off_t pos, const void *buffer, mr_size_t size) -{ - MR_ASSERT(device != MR_NULL); - MR_ASSERT(buffer != MR_NULL); - - /* Check if the device is closed or unsupported */ - if ((device->ref_count == 0) || ! (device->open_flag & MR_OPEN_WRONLY)) - { - MR_LOG_E(LOG_TAG, "Device %s does not support write\r\n", device->object.name); - return - MR_ERR_UNSUPPORTED; - } - - /* Call the device-write function, if provided */ - if (device->ops->write == MR_NULL) - { - MR_LOG_E(LOG_TAG, "Device %s write function is null\r\n", device->object.name); - return - MR_ERR_IO; - } - - return device->ops->write(device, pos, buffer, size); -} \ No newline at end of file diff --git a/src/fifo.c b/src/fifo.c deleted file mode 100644 index 0f04e6d..0000000 --- a/src/fifo.c +++ /dev/null @@ -1,304 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#include "mrlib.h" - -/** - * @brief This function initialize the fifo. - * - * @param fifo The fifo to initialize. - * @param pool The pool of data. - * @param pool_size The size of the pool. - */ -void mr_fifo_init(mr_fifo_t fifo, mr_uint8_t *pool, mr_size_t pool_size) -{ - MR_ASSERT(fifo != MR_NULL); - MR_ASSERT(pool != MR_NULL); - - fifo->read_index = 0; - fifo->write_index = 0; - fifo->read_mirror = 0; - fifo->write_mirror = 0; - - fifo->buffer = pool; - fifo->size = pool_size; -} - -/** - * @brief This function reset the fifo. - * - * @param fifo The fifo to reset. - */ -void mr_fifo_reset(mr_fifo_t fifo) -{ - MR_ASSERT(fifo != MR_NULL); - - /* Disable interrupt */ - mr_hw_interrupt_disable(); - - fifo->read_index = 0; - fifo->write_index = 0; - - fifo->read_mirror = 0; - fifo->write_mirror = 0; - - /* Enable interrupt */ - mr_hw_interrupt_enable(); -} - -/** - * @brief This function get the data size from the fifo. - * - * @param fifo The fifo to get the data size. - * - * @return The data size. - */ -mr_size_t mr_fifo_get_data_size(mr_fifo_t fifo) -{ - MR_ASSERT(fifo != MR_NULL); - - /* Disable interrupt */ - mr_hw_interrupt_disable(); - - /* Empty or full according to the mirror flag */ - if (fifo->read_index == fifo->write_index) - { - if (fifo->read_mirror == fifo->write_mirror) - { - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - return 0; - } else - { - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - return fifo->size; - } - } - - if (fifo->write_index > fifo->read_index) - { - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - return fifo->write_index - fifo->read_index; - } else - { - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - return fifo->size - fifo->read_index + fifo->write_index; - } -} - -/** - * @brief This function get the space size from the fifo. - * - * @param fifo The fifo to get the space size. - * - * @return The space size. - */ -mr_size_t mr_fifo_get_space_size(mr_fifo_t fifo) -{ - return fifo->size - mr_fifo_get_data_size(fifo); -} - -/** - * @brief This function get the buffer size from the fifo. - * - * @param fifo The fifo to get the buffer size. - * - * @return The buffer size. - */ -mr_size_t mr_fifo_get_buffer_size(mr_fifo_t fifo) -{ - return fifo->size; -} - -/** - * @brief This function reads from the fifo. - * - * @param fifo The fifo to be read. - * @param buffer The data buffer to be read from the fifo. - * @param size The size of the read. - * - * @return The size of the actual read. - */ -mr_size_t mr_fifo_read(mr_fifo_t fifo, mr_uint8_t *buffer, mr_size_t size) -{ - mr_size_t length = 0; - - MR_ASSERT(fifo != MR_NULL); - MR_ASSERT(buffer != MR_NULL); - - if (size == 0) - return 0; - - /* Get the data size */ - length = mr_fifo_get_data_size(fifo); - - /* If there is no data to read, return 0 */ - if (length == 0) - return 0; - - /* Adjust the number of bytes to read if it exceeds the available data */ - if (size > length) - size = length; - - /* Disable interrupt */ - mr_hw_interrupt_disable(); - - /* Copy the data from the fifo to the buffer */ - if ((fifo->size - fifo->read_index) > size) - { - mr_memcpy(buffer, &fifo->buffer[fifo->read_index], size); - fifo->read_index += size; - - /* Enable interrupt */ - mr_hw_interrupt_enable(); - return size; - } - - mr_memcpy(buffer, &fifo->buffer[fifo->read_index], fifo->size - fifo->read_index); - mr_memcpy(&buffer[fifo->size - fifo->read_index], &fifo->buffer[0], size - (fifo->size - fifo->read_index)); - - fifo->read_mirror = ~ fifo->read_mirror; - fifo->read_index = size - (fifo->size - fifo->read_index); - - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - return size; -} - -/** - * @brief This function write the fifo. - * - * @param fifo The fifo to be written. - * @param buffer The data buffer to be written to fifo. - * @param size The size of write. - * - * @return The size of the actual write. - */ -mr_size_t mr_fifo_write(mr_fifo_t fifo, const mr_uint8_t *buffer, mr_size_t size) -{ - mr_size_t length = 0; - - MR_ASSERT(fifo != MR_NULL); - MR_ASSERT(buffer != MR_NULL); - - if (size == 0) - return 0; - - /* Get the space size */ - length = mr_fifo_get_space_size(fifo); - - /* If there is no space to write, return 0 */ - if (length == 0) - return 0; - - /* Adjust the number of bytes to write if it exceeds the available data */ - if (size > length) - size = length; - - /* Disable interrupt */ - mr_hw_interrupt_disable(); - - /* Copy the data from the buffer to the fifo */ - if ((fifo->size - fifo->write_index) > size) - { - mr_memcpy(&fifo->buffer[fifo->write_index], buffer, size); - fifo->write_index += size; - - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - return size; - } - - mr_memcpy(&fifo->buffer[fifo->write_index], buffer, fifo->size - fifo->write_index); - mr_memcpy(&fifo->buffer[0], &buffer[fifo->size - fifo->write_index], size - (fifo->size - fifo->write_index)); - - fifo->write_mirror = ~ fifo->write_mirror; - fifo->write_index = size - (fifo->size - fifo->write_index); - - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - return size; -} - -/** - * @brief This function force write the fifo. - * - * @param fifo The fifo to be written. - * @param buffer The data buffer to be written to fifo. - * @param size The size of write. - * - * @return The size of the actual write. - */ -mr_size_t mr_fifo_write_force(mr_fifo_t fifo, const mr_uint8_t *buffer, mr_size_t size) -{ - mr_size_t length = 0; - - MR_ASSERT(fifo != MR_NULL); - MR_ASSERT(buffer != MR_NULL); - - if (size == 0) - return 0; - - /* Get the space size */ - length = mr_fifo_get_space_size(fifo); - - /* If the data exceeds the buffer length, the front data is discarded */ - if (size > fifo->size) - { - buffer = &buffer[size - fifo->size]; - size = fifo->size; - } - - /* Disable interrupt */ - mr_hw_interrupt_disable(); - - /* Copy the data from the buffer to the fifo */ - if ((fifo->size - fifo->write_index) > size) - { - mr_memcpy(&fifo->buffer[fifo->write_index], buffer, size); - fifo->write_index += size; - if (size > length) - fifo->read_index = fifo->write_index; - - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - return size; - } - - mr_memcpy(&fifo->buffer[fifo->write_index], buffer, fifo->size - fifo->write_index); - mr_memcpy(&fifo->buffer[0], &buffer[fifo->size - fifo->write_index], size - (fifo->size - fifo->write_index)); - - fifo->write_mirror = ~ fifo->write_mirror; - fifo->write_index = size - (fifo->size - fifo->write_index); - - if (size > length) - { - if (fifo->write_index <= fifo->read_index) - fifo->read_mirror = ~ fifo->read_mirror; - - fifo->read_index = fifo->write_index; - } - - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - return size; -} \ No newline at end of file diff --git a/src/kernel.c b/src/kernel.c deleted file mode 100644 index 6148c9c..0000000 --- a/src/kernel.c +++ /dev/null @@ -1,274 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#include "mrlib.h" - -static struct mr_container mr_kernel_container[_MR_CONTAINER_TYPE_MASK] = - { - { - .type = MR_CONTAINER_TYPE_MISC, - .list = {&mr_kernel_container[MR_CONTAINER_TYPE_MISC].list, - &mr_kernel_container[MR_CONTAINER_TYPE_MISC].list} - }, - { - .type = MR_CONTAINER_TYPE_DEVICE, - .list = {&mr_kernel_container[MR_CONTAINER_TYPE_DEVICE].list, - &mr_kernel_container[MR_CONTAINER_TYPE_DEVICE].list} - }, - { - .type = MR_CONTAINER_TYPE_MANAGER, - .list = {&mr_kernel_container[MR_CONTAINER_TYPE_MANAGER].list, - &mr_kernel_container[MR_CONTAINER_TYPE_MANAGER].list} - }, - }; - -/** - * @brief This function find the container. - * - * @param type The flag of the container. - * - * @return a handle to the find container, or MR_NULL if not find. - */ -mr_container_t mr_container_find(enum mr_container_type type) -{ - MR_ASSERT(type < _MR_CONTAINER_TYPE_MASK); - - return &mr_kernel_container[type]; -} - -/** - * @brief This function find the object. - * - * @param name The name of the object. - * @param type The container flag to which the object belongs. - * - * @return a handle to the find object, or MR_NULL if not find. - */ -mr_object_t mr_object_find(const char *name, enum mr_container_type type) -{ - mr_list_t list = MR_NULL; - mr_container_t container = MR_NULL; - mr_object_t object = MR_NULL; - - MR_ASSERT(name != MR_NULL); - - /* Get corresponding container */ - container = mr_container_find(type); - - /* Disable interrupt */ - mr_hw_interrupt_disable(); - - /* Walk through the container looking for objects */ - for (list = (container->list).next; list != &(container->list); list = list->next) - { - object = mr_struct_of(list, struct mr_object, list); - if (mr_strncmp(object->name, name, MR_CONF_NAME_MAX) == 0) - { - /* Enable interrupt */ - mr_hw_interrupt_enable(); - return object; - } - } - - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - return MR_NULL; -} - -/** - * @brief This function add object to the container. - * - * @param object The object to be added. - * @param name The name of the object. - * @param type The target container flag. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_object_add(mr_object_t object, const char *name, enum mr_container_type container_type) -{ - mr_container_t container = MR_NULL; - - MR_ASSERT(object != MR_NULL); - MR_ASSERT(name != MR_NULL); - - /* Check if the object is already registered */ - if (object->flag & _MR_OBJECT_TYPE_REGISTER) - return - MR_ERR_BUSY; - - /* Check if the object already exists in the container */ - if (mr_object_find(name, container_type) != MR_NULL) - return - MR_ERR_GENERIC; - - /* Copy the specified name to the object name */ - mr_strncpy(object->name, name, MR_CONF_NAME_MAX); - - /* Find the container for the specified flag */ - container = mr_container_find(container_type); - - /* Disable interrupt */ - mr_hw_interrupt_disable(); - - /* Insert the object into the container's list */ - mr_list_insert_after(&(container->list), &(object->list)); - object->flag |= _MR_OBJECT_TYPE_REGISTER; - - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - return MR_ERR_OK; -} - -/** - * @brief This function remove object from the container. - * - * @param object The object to be removed. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_object_remove(mr_object_t object) -{ - MR_ASSERT(object != MR_NULL); - - /* Check if the object is registered */ - if ((object->flag & _MR_OBJECT_TYPE_REGISTER) == 0) - return - MR_ERR_GENERIC; - - /* Disable interrupt */ - mr_hw_interrupt_disable(); - - /* Remove the object from the container's list */ - mr_list_remove(&(object->list)); - object->flag &= ~ _MR_OBJECT_TYPE_REGISTER; - - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - return MR_ERR_OK; -} - -/** - * @brief This function move object to specified container. - * - * @param object The object to be moved. - * @param dst_type The target container flag. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_object_move(mr_object_t object, enum mr_container_type dst_type) -{ - mr_err_t ret = MR_ERR_OK; - - MR_ASSERT(object != MR_NULL); - - /* Remove the object from its current container */ - ret = mr_object_remove(object); - if (ret != MR_ERR_OK) - return ret; - - /* Add the object to the new container */ - ret = mr_object_add(object, object->name, dst_type); - - return ret; -} - -/** - * @brief This function rename object. - * - * @param object The object to be renamed. - * @param name The name of the object. - */ -void mr_object_rename(mr_object_t object, char *name) -{ - MR_ASSERT(object != MR_NULL); - - mr_strncpy(object->name, name, MR_CONF_NAME_MAX); -} - -/** - * @brief This function initialize mutex. - * - * @param mutex The mutex to be initialized. - */ -void mr_mutex_init(mr_mutex_t mutex) -{ - MR_ASSERT(mutex != MR_NULL); - - mutex->owner = MR_NULL; - mutex->lock = MR_UNLOCK; -} - -/** - * @brief This function take the mutex. - * - * @param mutex The mutex to be taken. - * @param owner The object that take the mutex. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_mutex_take(mr_mutex_t mutex, mr_object_t owner) -{ - MR_ASSERT(mutex != MR_NULL); - MR_ASSERT(owner != MR_NULL); - - /* Disable interrupt */ - mr_hw_interrupt_disable(); - - if (mutex->owner != owner) - { - if (mutex->lock == MR_LOCK) - { - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - return - MR_ERR_BUSY; - } - - mutex->owner = owner; - } - mutex->lock = MR_LOCK; - - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - return MR_ERR_OK; -} - -/** - * @brief This function release the mutex. - * - * @param mutex The mutex to be released. - * @param owner The object that release the mutex. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_mutex_release(mr_mutex_t mutex, mr_object_t owner) -{ - MR_ASSERT(mutex != MR_NULL); - MR_ASSERT(owner != MR_NULL); - - /* Disable interrupt */ - mr_hw_interrupt_disable(); - - if (mutex->owner == owner) - { - mutex->lock = MR_UNLOCK; - - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - return MR_ERR_OK; - } - - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - return - MR_ERR_GENERIC; -} \ No newline at end of file diff --git a/src/kservice.c b/src/kservice.c deleted file mode 100644 index fa5c04f..0000000 --- a/src/kservice.c +++ /dev/null @@ -1,219 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#include - -static int mri_start(void) -{ - return 0; -} -INIT_EXPORT(mri_start, "0"); - -static int mri_board_start(void) -{ - return 0; -} -INIT_EXPORT(mri_board_start, "0.end"); - -static int mri_board_end(void) -{ - return 0; -} -INIT_EXPORT(mri_board_end, "1.end"); - -static int mri_end(void) -{ - return 0; -} -INIT_EXPORT(mri_end,"6.end"); - -void mr_auto_init(void) -{ - volatile const init_fn_t *fn_ptr; - - /* auto-init-board */ - for (fn_ptr = &_mr_init_mri_start; fn_ptr < &_mr_init_mri_board_end; fn_ptr++) - { - (*fn_ptr)(); - } - - /* auto-init-other */ - for (fn_ptr = &_mr_init_mri_board_end; fn_ptr < &_mr_init_mri_end; fn_ptr++) - { - (*fn_ptr)(); - } -} - -static mr_int8_t mr_avl_get_height(mr_avl_t node) -{ - if (node == MR_NULL) - return - 1; - - return node->height; -} - -static mr_int8_t mr_avl_get_balance(mr_avl_t node) -{ - if (node == MR_NULL) - return 0; - - return mr_avl_get_height(node->left_child) - mr_avl_get_height(node->right_child); -} - -static void mr_avl_left_rotate(mr_avl_t *node) -{ - mr_avl_t right_child = (*node)->right_child; - - (*node)->right_child = right_child->left_child; - right_child->left_child = (*node); - - (*node)->height = mr_max(mr_avl_get_height((*node)->left_child), mr_avl_get_height((*node)->right_child)) + 1; - right_child->height = - mr_max(mr_avl_get_height(right_child->left_child), mr_avl_get_height(right_child->right_child)) + 1; - - (*node) = right_child; -} - -static void mr_avl_right_rotate(mr_avl_t *node) -{ - mr_avl_t left_child = (*node)->left_child; - - (*node)->left_child = left_child->right_child; - left_child->right_child = (*node); - - (*node)->height = mr_max(mr_avl_get_height((*node)->left_child), mr_avl_get_height((*node)->right_child)) + 1; - left_child->height = - mr_max(mr_avl_get_height(left_child->left_child), mr_avl_get_height(left_child->right_child)) + 1; - - (*node) = left_child; -} - -void mr_avl_init(mr_avl_t node, mr_uint32_t value) -{ - node->height = 0; - node->value = value; - node->left_child = MR_NULL; - node->right_child = MR_NULL; -} - -void mr_avl_insert(mr_avl_t *tree, mr_avl_t node) -{ - mr_int8_t balance = 0; - - if ((*tree) == MR_NULL) - (*tree) = node; - - if (node->value < (*tree)->value) - mr_avl_insert(&(*tree)->left_child, node); - else if (node->value > (*tree)->value) - mr_avl_insert(&(*tree)->right_child, node); - else - return; - - (*tree)->height = mr_max(mr_avl_get_height((*tree)->left_child), mr_avl_get_height((*tree)->right_child)) + 1; - - balance = mr_avl_get_balance((*tree)); - if (balance > 1 && node->value < (*tree)->left_child->value) - { - mr_avl_right_rotate(&(*tree)); - return; - } - - if (balance < - 1 && node->value > (*tree)->right_child->value) - { - mr_avl_left_rotate(&(*tree)); - return; - } - - if (balance > 1 && node->value > (*tree)->left_child->value) - { - mr_avl_left_rotate(&(*tree)->left_child); - mr_avl_right_rotate(&(*tree)); - return; - } - - if (balance < - 1 && node->value < (*tree)->right_child->value) - { - mr_avl_right_rotate(&(*tree)->right_child); - mr_avl_left_rotate(&(*tree)); - return; - } -} - -void mr_avl_remove(mr_avl_t *tree, mr_avl_t node) -{ - -} - -mr_avl_t mr_avl_find(mr_avl_t tree, mr_uint32_t value) -{ - if (tree == MR_NULL) - return tree; - - if (tree->value == value) - return tree; - - if (value < tree->value) - return mr_avl_find(tree->left_child, value); - else if (value > tree->value) - return mr_avl_find(tree->right_child, value); - - return MR_NULL; -} - -mr_size_t mr_avl_get_length(mr_avl_t tree) -{ - mr_size_t length = 1; - - if (tree == MR_NULL) - return 0; - - if (tree->left_child != MR_NULL) - length += mr_avl_get_length(tree->left_child); - - if (tree->right_child != MR_NULL) - length += mr_avl_get_length(tree->right_child); - - return length; -} - -mr_uint32_t mr_strhase(const char *str) -{ - mr_uint32_t value = 0; - - while (*str) { - value ^= value << 15; - value ^= value >> 10; - value ^= *str++; - } - value ^= value << 3; - value ^= value >> 6; - value ^= value << 2; - value ^= value >> 15; - - return value; -} - -mr_uint32_t mr_strnhase(const char *str, mr_size_t length) -{ - mr_uint32_t value = 0; - - while (length--) { - value ^= value << 15; - value ^= value >> 10; - value ^= *str++; - } - value ^= value << 3; - value ^= value >> 6; - value ^= value << 2; - value ^= value >> 15; - - return value; -} \ No newline at end of file diff --git a/src/log.c b/src/log.c deleted file mode 100644 index 90b130b..0000000 --- a/src/log.c +++ /dev/null @@ -1,246 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#include "mrlib.h" - -static char *log_level_name[] = - { - "LOG-A", - "LOG-E", - "LOG-W", - "LOG-I", - "LOG-D", - }; - -static mr_size_t _mr_log_output(const char *fmt, va_list args) -{ - mr_size_t rst = 0; - char buffer[MR_CONF_LOG_BUFSZ]; - char value_buffer[12]; - char *string; - mr_int32_t value; - mr_uint32_t u_value, bits, number; - mr_fp64_t d_value; - - while (*fmt != '\0') - { - if (*fmt == '%') - { - fmt ++; - - if (*fmt == '.') - { - fmt ++; - number = (mr_uint32_t)(*fmt - '0'); - if (number > 6) - number = 6; - fmt ++; - } else - number = 6; - - /* Dispose %d,%x,%o,%u,%s,%c,%f */ - switch (*fmt) - { - /* DEC */ - case 'd': - { - value = va_arg(args, int); - if (value < 0) - { - value = - value; - buffer[rst ++] = '-'; - } - - bits = 0; - do - { - value_buffer[bits ++] = '0' + value % 10; - value /= 10; - } while (value); - - while (bits) - { - buffer[rst ++] = value_buffer[-- bits]; - } - break; - } - - /* HEX */ - case 'x': - case 'X': - { - u_value = va_arg(args, unsigned int); - - bits = 0; - do - { - value_buffer[bits] = '0' + u_value % 16; - - if (value_buffer[bits] > '9') - value_buffer[bits] = 'A' + (value_buffer[bits] - '9' - 1); - - u_value /= 16; - bits ++; - } while (u_value); - - while (bits) - { - buffer[rst ++] = value_buffer[-- bits]; - } - break; - } - - /* OCT */ - case 'o': - { - u_value = va_arg(args, unsigned int); - - bits = 0; - do - { - value_buffer[bits] = '0' + u_value % 8; - - if (value_buffer[bits] > '9') - value_buffer[bits] = 'A' + (value_buffer[bits] - '9' - 1); - - u_value /= 8; - bits ++; - } while (u_value); - - while (bits) - { - buffer[rst ++] = value_buffer[-- bits]; - } - break; - } - - /* Unsigned DEC */ - case 'u': - { - u_value = va_arg(args, int); - - bits = 0; - do - { - value_buffer[bits ++] = '0' + u_value % 10; - u_value /= 10; - } while (u_value); - - while (bits) - { - buffer[rst ++] = value_buffer[-- bits]; - } - break; - } - - case 'c': - { - buffer[rst ++] = va_arg(args, int); - break; - } - - case 's': - { - string = va_arg(args, char *); - - while (*string != '\0') - { - buffer[rst ++] = *string; - string ++; - } - break; - } - - case 'f': - { - d_value = va_arg(args, double); - if (d_value < 0) - { - d_value = - d_value; - buffer[rst ++] = '-'; - } - - value = (mr_int32_t)d_value; - d_value -= value; - - bits = 0; - do - { - value_buffer[bits ++] = '0' + value % 10; - value /= 10; - } while (value); - - while (bits) - { - buffer[rst ++] = value_buffer[-- bits]; - } - - buffer[rst ++] = '.'; - value = (int)((d_value * 1000000.0f) + 0.5f); - - bits = 0; - do - { - value_buffer[bits ++] = '0' + value % 10; - value /= 10; - } while (value); - - while (number) - { - buffer[rst ++] = value_buffer[-- bits]; - number --; - } - break; - } - } - - fmt ++; - } else - { - buffer[rst ++] = *fmt; - fmt ++; - } - } - - buffer[rst] = '\0'; - printf("%s", buffer); - - return rst; -} - -void mr_log_output(mr_base_t level, const char *tag, const char *format, ...) -{ - va_list args; - -#if (MR_CONF_LOG_OUTPUT == MR_CONF_DISABLE) - return; -#endif - - if (level > MR_CONF_LOG_LEVEL || level < 0) - return; - - va_start(args, format); - - mr_printf("[%s/%s]: ", log_level_name[level], tag); - _mr_log_output(format, args); - - va_end(args); -} - -void mr_assert_handler(void) -{ - while (1) - { - - } -} - - - diff --git a/src/manager.c b/src/manager.c deleted file mode 100644 index d1e4ce2..0000000 --- a/src/manager.c +++ /dev/null @@ -1,729 +0,0 @@ -/* - * Copyright (c) 2023, mr-library Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-04-23 MacRsh first version - */ - -#include "mrlib.h" - -#undef LOG_TAG -#define LOG_TAG "manager" - -/** - * @brief This function find the manager object. - * - * @param name The name of the manager. - * - * @return A handle to the found manager, or MR_NULL if not found. - */ -mr_manager_t mr_manager_find(const char *name) -{ - MR_ASSERT(name != MR_NULL); - - /* Find the manager object from the manager-container */ - return (mr_manager_t)mr_object_find(name, MR_CONTAINER_TYPE_MANAGER); -} - -/** - * @brief This function adds a manager to the container. - * - * @param manager The manager to be added. - * @param name The name of the manager. - * @param type The type of the manager. - * @param queue_number The number of the queue. - * @param ops The operations of the manager. - * @param data The data of the manager. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_manager_add(mr_manager_t manager, - const char *name, - enum mr_manager_type type, - mr_size_t queue_number, - struct mr_manager_ops *ops, - void *data) -{ - mr_err_t ret = MR_ERR_OK; - static struct mr_manager_ops null_ops = {MR_NULL}; - mr_uint8_t *pool = MR_NULL; - mr_size_t pool_size = 0; - - MR_ASSERT(manager != MR_NULL); - MR_ASSERT(name != MR_NULL); - MR_ASSERT(queue_number != 0); - - /* Allocate memory for the queue, pool_size = queue_number * sizeof(id) */ - pool_size = queue_number * sizeof(mr_uint32_t); - pool = mr_malloc(pool_size); - if (pool == MR_NULL) - { - MR_LOG_E(LOG_TAG, "Failed to allocate memory for manager %s\r\n", name); - return - MR_ERR_NO_MEMORY; - } - - /* Initialize the private fields */ - manager->type = type; - manager->avl = MR_NULL; - manager->data = data; - mr_fifo_init(&manager->queue, pool, pool_size); - - /* Set operations as null-ops if ops is null */ - manager->ops = (ops == MR_NULL) ? &null_ops : ops; - - /* Call the manager-add function, if provided */ - if (manager->ops->add != MR_NULL) - { - ret = manager->ops->add(manager); - if (ret != MR_ERR_OK) - { - mr_fifo_init(&manager->queue, pool, 0); - mr_free(pool); - return ret; - } - } - - /* Add the object to the container */ - ret = mr_object_add(&manager->object, name, MR_CONTAINER_TYPE_MANAGER); - if (ret != MR_ERR_OK) - return ret; - - MR_LOG_D(LOG_TAG, "Manager %s added, type %d\r\n", name, type); - - return MR_ERR_OK; -} - -/** - * @brief This function remove a manager object. - * - * @param manager The manager to be removed. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_manager_remove(mr_manager_t manager) -{ - mr_err_t ret = MR_ERR_OK; - static struct mr_manager_ops null_ops = {MR_NULL}; - - MR_ASSERT(manager != MR_NULL); - - /* Check the manager list for events */ - if (manager->avl != MR_NULL) - return - MR_ERR_BUSY; - - /* Remove the object from the container */ - ret = mr_object_remove(&manager->object); - if (ret != MR_ERR_OK) - return ret; - - /* Reset the private fields */ - manager->avl = MR_NULL; - mr_fifo_init(&manager->queue, MR_NULL, 0); - - /* Free the memory */ - mr_free(manager->queue.buffer); - - /* Call the manager-remove function, if provided */ - if (manager->ops->remove != MR_NULL) - { - ret = manager->ops->remove(manager); - if (ret != MR_ERR_OK) - return ret; - } - - /* Set operations as null-ops */ - manager->ops = &null_ops; - - MR_LOG_D(LOG_TAG, "Manager %s removed\r\n", manager->object.name); - - return MR_ERR_OK; -} - -/** - * @brief This function notify the manager that the agent has occurred. - * - * @param manager The manager to be notified. - * - * @param id The id of the agent. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_manager_notify(mr_manager_t manager, mr_uint32_t id) -{ - mr_size_t send_size = 0; - - MR_ASSERT(manager != MR_NULL); - - send_size = mr_fifo_write(&manager->queue, (mr_uint8_t *)&id, sizeof(id)); - if (send_size < sizeof(id)) - { - MR_LOG_E(LOG_TAG, "Manager %s queue is full, failed notify agent %d\r\n", manager->object.name, id); - return - MR_ERR_BUSY; - } - - return MR_ERR_OK; -} - -/** - * @brief This function handle the manager events. - * - * @param manager The manager to be handled. - */ -void mr_manager_handler(mr_manager_t manager) -{ - MR_ASSERT(manager != MR_NULL); - - if (manager->ops->handler != MR_NULL) - manager->ops->handler(manager); -} - -/** - * @brief This function finds a agent. - * - * @param id The id of the agent. - * @param manager The target manager. - * - * @return A handle to the found agent, or MR_NULL if not found. - */ -mr_agent_t mr_agent_find(mr_uint32_t id, mr_manager_t manager) -{ - MR_ASSERT(manager != MR_NULL); - - return (mr_agent_t)mr_avl_find(manager->avl, id); -} - -/** - * @brief This function creates a agent to the manager. - * - * @param id The id of the agent. - * @param callback The callback function. - * @param args The arguments of the callback function. - * @param target_manager The target manager. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_agent_create(mr_uint32_t id, - mr_err_t (*callback)(mr_manager_t manager, void *args), - void *args, - mr_manager_t target_manager) -{ - mr_agent_t agent = MR_NULL; - - MR_ASSERT(callback != MR_NULL); - MR_ASSERT(target_manager != MR_NULL); - - /* Check if the agent is already exists in the manager */ - if (mr_avl_find(target_manager->avl, id) != MR_NULL) - return - MR_ERR_GENERIC; - - /* Allocate the agent object */ - agent = (mr_agent_t)mr_malloc(sizeof(struct mr_agent)); - if (agent == MR_NULL) - return - MR_ERR_NO_MEMORY; - - /* Initialize the private fields */ - mr_avl_init(&agent->avl, id); - agent->cb = callback; - agent->args = args; - - /* Disable interrupt */ - mr_hw_interrupt_disable(); - - /* Insert the agent into the manager's list */ - mr_avl_insert(&target_manager->avl, &agent->avl); - - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - MR_LOG_D(LOG_TAG, "Manager %s, agent %d created\r\n", target_manager->object.name, id); - - return MR_ERR_OK; -} - -/** - * @brief This function delete an agent. - * - * @param id The id of the agent. - * @param manager The target manager. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_agent_delete(mr_uint32_t id, mr_manager_t manager) -{ - mr_agent_t agent = MR_NULL; - - MR_ASSERT(manager != MR_NULL); - - /* Find the agent from the manager */ - agent = mr_agent_find(id, manager); - if (agent == MR_NULL) - return - MR_ERR_NOT_FOUND; - - /* Disable interrupt */ - mr_hw_interrupt_disable(); - - /* Remove the agent from the manager's list */ - mr_avl_remove(&manager->avl, &agent->avl); - - /* Enable interrupt */ - mr_hw_interrupt_enable(); - - /* Free the agent object */ - mr_free(agent); - - MR_LOG_D(LOG_TAG, "Manager %s, agent %d deleted\r\n", manager->object.name, id); - - return MR_ERR_OK; -} - -#undef LOG_TAG -#define LOG_TAG "event" - -static void event_handler(mr_manager_t manager) -{ - mr_err_t ret = MR_ERR_OK; - mr_uint32_t id = 0; - mr_avl_t node = MR_NULL; - mr_agent_t agent = MR_NULL; - - MR_ASSERT(manager != MR_NULL); - - while (mr_fifo_get_data_size(&manager->queue) >= sizeof(id)) - { - mr_fifo_read(&manager->queue, (mr_uint8_t *)&id, sizeof(id)); - - node = mr_avl_find(manager->avl, id); - if (node == MR_NULL) - { - MR_LOG_E(LOG_TAG, "Manager %s, event %d not found\r\n", manager->object.name, id); - continue; - } - - agent = mr_struct_of(node, struct mr_agent, avl); - - /* Increase the reference count */ - MR_LOG_D(LOG_TAG, "Manager %s, event %d occurred\r\n", manager->object.name, id); - - /* Call the agent baud */ - ret = agent->cb(manager, agent->args); - if (ret != MR_ERR_OK) - { - MR_LOG_E(LOG_TAG, "Manager %s, event %d failed, error %d\r\n", manager->object.name, id, ret); - } - } -} - -/** - * @brief This function adds an event-manager to the container. - * - * @param manager The manager to be added. - * @param name The name of the manager. - * @param queue_number The number of the queue. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_event_manager_add(mr_manager_t manager, const char *name, mr_size_t queue_number) -{ - static struct mr_manager_ops manager_ops = - { - MR_NULL, - MR_NULL, - event_handler, - }; - - MR_ASSERT(manager != MR_NULL); - MR_ASSERT(manager != MR_NULL); - - return mr_manager_add(manager, name, MR_MANAGER_TYPE_EVENT, queue_number, &manager_ops, MR_NULL); -} - -mr_err_t mr_event_create(mr_uint32_t event, - mr_err_t (*callback)(mr_manager_t manager, void *args), - void *args, - mr_manager_t target_manager) -{ - MR_ASSERT(callback != MR_NULL); - MR_ASSERT(target_manager != MR_NULL); - - return mr_agent_create(event, callback, args, target_manager); -} - -mr_err_t mr_event_delete(mr_uint32_t event, mr_manager_t manager) -{ - MR_ASSERT(manager != MR_NULL); - - return mr_agent_delete(event, manager); -} - -#undef LOG_TAG -#define LOG_TAG "fsm" - -static void fsm_handler(mr_manager_t manager) -{ - mr_err_t ret = MR_ERR_OK; - mr_uint32_t id = 0, state = 0; - mr_avl_t node = MR_NULL; - mr_agent_t agent = MR_NULL; - - MR_ASSERT(manager != MR_NULL); - - while (mr_fifo_get_data_size(&manager->queue) >= sizeof(id)) - { - mr_fifo_read(&manager->queue, (mr_uint8_t *)&id, sizeof(id)); - - node = mr_avl_find(manager->avl, id); - if (node == MR_NULL) - { - MR_LOG_E(LOG_TAG, "Manager %s, fsm %d not found\r\n", manager->object.name, id); - continue; - } - - agent = mr_struct_of(node, struct mr_agent, avl); - state = agent->avl.value; - - /* Increase the reference count */ - MR_LOG_D(LOG_TAG, "Manager %s, fsm %d occurred\r\n", manager->object.name, id); - - /* Call the agent baud */ - ret = agent->cb(manager, agent->args); - if (ret != MR_ERR_OK) - { - MR_LOG_E(LOG_TAG, "Manager %s, fsm %d failed, error %d\r\n", manager->object.name, id, ret); - } - } - - mr_manager_notify(manager, state); -} - -mr_err_t mr_fsm_state_create(mr_uint32_t state, - mr_err_t (*callback)(mr_manager_t manager, void *args), - void *args, - mr_manager_t target_manager) -{ - MR_ASSERT(callback != MR_NULL); - MR_ASSERT(target_manager != MR_NULL); - - return mr_agent_create(state, callback, args, target_manager); -} - -mr_err_t mr_fsm_state_delete(mr_uint32_t state, mr_manager_t manager) -{ - MR_ASSERT(manager != MR_NULL); - - return mr_agent_delete(state, manager); -} - -/** - * @brief This function adds an fsm-manager to the container. - * - * @param manager The manager to be added. - * @param name The name of the manager. - * @param queue_number The number of the queue. - * - * @return MR_ERR_OK on success, otherwise an error code. - */ -mr_err_t mr_fsm_manager_add(mr_manager_t manager, const char *name, mr_size_t queue_number) -{ - static struct mr_manager_ops manager_ops = - { - MR_NULL, - MR_NULL, - fsm_handler, - }; - - MR_ASSERT(manager != MR_NULL); - MR_ASSERT(manager != MR_NULL); - - return mr_manager_add(manager, name, MR_MANAGER_TYPE_FSM, queue_number, &manager_ops, MR_NULL); -} - -#undef LOG_TAG -#define LOG_TAG "at-parser" - -mr_err_t at_parser_add(mr_manager_t manager) -{ - char *at_buffer = MR_NULL; - mr_size_t queue_number = 0; - - MR_ASSERT(manager != MR_NULL); - - queue_number = mr_fifo_get_buffer_size(&manager->queue) / sizeof(mr_uint32_t); - - /* Allocate memory for the parse-buffer, the one cmd = state(1byte) + cmd(4byte) + parse-buffer(MR_CONF_MANAGER_PARSER_BUFSZ) */ - at_buffer = mr_malloc(queue_number * (MR_CONF_MANAGER_PARSER_BUFSZ + 5)); - if (at_buffer == MR_NULL) - { - MR_LOG_E(LOG_TAG, "Failed to allocate memory for at-parser buffer\r\n"); - mr_free(manager->queue.buffer); - return - MR_ERR_NO_MEMORY; - } - mr_memset(at_buffer, 0, queue_number * (MR_CONF_MANAGER_PARSER_BUFSZ + 5)); - manager->data = at_buffer; - - return MR_ERR_OK; -} - -mr_err_t at_parser_remove(mr_manager_t manager) -{ - MR_ASSERT(manager != MR_NULL); - - /* Free the memory */ - mr_free(manager->data); - - return MR_ERR_OK; -} - -static void at_parser_handler(mr_manager_t manager) -{ - mr_err_t ret = MR_ERR_OK; - mr_uint32_t id = 0; - mr_avl_t node = MR_NULL; - mr_agent_t agent = MR_NULL; - - MR_ASSERT(manager != MR_NULL); - - while (mr_fifo_get_data_size(&manager->queue) >= sizeof(id)) - { - mr_fifo_read(&manager->queue, (mr_uint8_t *)&id, sizeof(id)); - - node = mr_avl_find(manager->avl, id); - if (node == MR_NULL) - { - MR_LOG_E(LOG_TAG, "Manager %s, event %d not found\r\n", manager->object.name, id); - continue; - } - - agent = mr_struct_of(node, struct mr_agent, avl); - - /* Increase the reference count */ - MR_LOG_D(LOG_TAG, "Manager %s, event %d occurred\r\n", manager->object.name, id); - - /* Call the agent baud */ - ret = agent->cb(manager, agent->args); - if (ret != MR_ERR_OK) - { - MR_LOG_E(LOG_TAG, "Manager %s, event %d failed, error %d\r\n", manager->object.name, id, ret); - } - - /* Empty the AT-buffer */ - if (agent->args != MR_NULL) - { - mr_memset(agent->args - 5, 0, 5 + MR_CONF_MANAGER_PARSER_BUFSZ); - } - } -} - -mr_err_t mr_at_parser_manager_add(mr_manager_t manager, const char *name, mr_size_t queue_number) -{ - static struct mr_manager_ops manager_ops = - { - at_parser_add, - at_parser_remove, - at_parser_handler, - }; - - MR_ASSERT(manager != MR_NULL); - MR_ASSERT(manager != MR_NULL); - - return mr_manager_add(manager, name, MR_MANAGER_TYPE_AT_PARSER, queue_number, &manager_ops, MR_NULL); -} - -mr_err_t mr_at_cmd_create(const char *at_cmd, - mr_err_t (*callback)(mr_manager_t manager, void *args), - mr_manager_t target_manager) -{ - MR_ASSERT(callback != MR_NULL); - MR_ASSERT(target_manager != MR_NULL); - - if (mr_strncmp(at_cmd, "AT+", 3) != 0) - return - MR_ERR_INVALID; - - return mr_agent_create(mr_strhase(at_cmd + 3), callback, MR_NULL, target_manager); -} - -mr_err_t mr_at_cmd_delete(mr_uint32_t at_cmd, mr_manager_t manager) -{ - MR_ASSERT(manager != MR_NULL); - - return mr_agent_delete(at_cmd, manager); -} - -void mr_at_parser_isr(mr_manager_t manager, char data) -{ - mr_uint8_t *state = MR_NULL; - mr_size_t index = 0, queue_number = 0; - mr_uint32_t id = 0; - mr_agent_t agent = MR_NULL; - - MR_ASSERT(manager->type == MR_MANAGER_TYPE_AT_PARSER); - - /* Check the at-buffer */ - if (manager->data == MR_NULL) - return; - - /* Find the idle buffer */ - queue_number = mr_fifo_get_buffer_size(&manager->queue) / sizeof(mr_uint32_t); - for (index = 0; index < queue_number; index ++) - { - state = manager->data + index * (MR_CONF_MANAGER_PARSER_BUFSZ + 5); - if (*state < MR_MANAGER_AT_STATE_HANDLE) - break; - } - /* No idle buffer */ - if (index == queue_number) - return; - - switch (*state) - { - case MR_MANAGER_AT_STATE_NONE: - { - if (data == 'A') - *state = MR_MANAGER_AT_STATE_START; - break; - } - - case MR_MANAGER_AT_STATE_START: - { - if (data == 'T') - *state = MR_MANAGER_AT_STATE_FLAG; - else - *state = MR_MANAGER_AT_STATE_NONE; - break; - } - - case MR_MANAGER_AT_STATE_FLAG: - { - if (data == '+') - *state = MR_MANAGER_AT_STATE_ID; - else - *state = MR_MANAGER_AT_STATE_NONE; - break; - } - - case MR_MANAGER_AT_STATE_ID: - { - if (data == '\r' || data == '\n' || data == ';' || data == '\0') - { - *state = MR_MANAGER_AT_STATE_STOP; - break; - } - - /* Find the idle buffer store id */ - for (index = 1; index < 4 + MR_CONF_MANAGER_PARSER_BUFSZ; index ++) - { - if (*(state + index) == 0) - { - *(state + index) = data; - break; - } - } - - if (data == '?') - *state = MR_MANAGER_AT_STATE_STOP; - - if (data == '=') - *state = MR_MANAGER_AT_STATE_CHECK; - - break; - } - - case MR_MANAGER_AT_STATE_CHECK: - { - if (data == '?') - { - /* Find the idle buffer store id */ - for (index = 1; index < 4 + MR_CONF_MANAGER_PARSER_BUFSZ; index ++) - { - if (*(state + index) == 0) - { - *(state + index) = data; - break; - } - } - - *state = MR_MANAGER_AT_STATE_STOP; - } else - { - /* Store the id */ - id = mr_strhase((char *)(state + 1)); - mr_memcpy(state + 1, &id, sizeof(id)); - - /* Release the occupied buffer */ - mr_memset(state + 5, 0, MR_CONF_MANAGER_PARSER_BUFSZ); - - /* Find the idle buffer store arguments */ - for (index = 5; index < 4 + MR_CONF_MANAGER_PARSER_BUFSZ; index ++) - { - if (*(state + index) == 0) - { - *(state + index) = data; - break; - } - } - - *state = MR_MANAGER_AT_STATE_ARGS; - } - break; - } - - case MR_MANAGER_AT_STATE_ARGS: - { - if (data == '\r' || data == '\n' || data == ';' || data == '\0') - { - /* Get the id from buffer */ - mr_memcpy(&id, state + 1, sizeof(id)); - - /* Find the agent in the manager */ - agent = mr_agent_find(id, manager); - if (agent == MR_NULL) - { - /* Release the occupied buffer */ - mr_memset(state, 0, 5 + MR_CONF_MANAGER_PARSER_BUFSZ); - - *state = MR_MANAGER_AT_STATE_NONE; - break; - } - - /* Notify the agent happened */ - agent->args = state + 5; - mr_manager_notify(manager, id); - - *state = MR_MANAGER_AT_STATE_HANDLE; - break; - } - - /* Find the idle buffer store arguments */ - for (index = 5; index < 4 + MR_CONF_MANAGER_PARSER_BUFSZ; index ++) - { - if (*(state + index) == 0) - { - *(state + index) = data; - break; - } - } - break; - } - - case MR_MANAGER_AT_STATE_STOP: - { - /* Store the id */ - id = mr_strhase((char *)(state + 1)); - mr_memcpy(state + 1, &id, sizeof(id)); - - /* Release the occupied buffer */ - mr_memset(state + 5, 0, MR_CONF_MANAGER_PARSER_BUFSZ); - - /* Notify the agent happened */ - mr_manager_notify(manager, id); - mr_memset(state, 0, 5); - - *state = MR_MANAGER_AT_STATE_NONE; - break; - } - - default:break; - } -} \ No newline at end of file