first
This commit is contained in:
15
libraries/HAL_Drivers/nano/SConscript
Normal file
15
libraries/HAL_Drivers/nano/SConscript
Normal file
@@ -0,0 +1,15 @@
|
||||
Import('RTT_ROOT')
|
||||
Import('rtconfig')
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
group = []
|
||||
src = ['drv_gpio.c']
|
||||
path = [cwd]
|
||||
|
||||
if GetDepend(['RT_USING_CONSOLE']):
|
||||
src += ['drv_console.c']
|
||||
|
||||
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = path)
|
||||
|
||||
Return('group')
|
106
libraries/HAL_Drivers/nano/drv_console.c
Normal file
106
libraries/HAL_Drivers/nano/drv_console.c
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-11-30 Meco Man First version
|
||||
*/
|
||||
|
||||
#include <board.h>
|
||||
#include <rtthread.h>
|
||||
#include <drv_common.h>
|
||||
|
||||
static UART_HandleTypeDef console_uart;
|
||||
|
||||
void rt_hw_console_init(void)
|
||||
{
|
||||
HAL_UART_DeInit(&console_uart);
|
||||
#ifdef USART1
|
||||
if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, "uart1") == 0)
|
||||
{
|
||||
console_uart.Instance = USART1;
|
||||
}
|
||||
#endif /* USART1 */
|
||||
#ifdef USART2
|
||||
else if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, "uart2") == 0)
|
||||
{
|
||||
console_uart.Instance = USART2;
|
||||
}
|
||||
#endif /* USART2 */
|
||||
#ifdef USART3
|
||||
else if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, "uart3") == 0)
|
||||
{
|
||||
console_uart.Instance = USART3;
|
||||
}
|
||||
#endif /* USART3 */
|
||||
#ifdef USART4
|
||||
else if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, "uart4") == 0)
|
||||
{
|
||||
console_uart.Instance = USART4;
|
||||
}
|
||||
#endif /* USART4 */
|
||||
#ifdef USART5
|
||||
else if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, "uart5") == 0)
|
||||
{
|
||||
console_uart.Instance = USART5;
|
||||
}
|
||||
#endif /* USART5 */
|
||||
#ifdef USART6
|
||||
else if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, "uart6") == 0)
|
||||
{
|
||||
console_uart.Instance = USART6;
|
||||
}
|
||||
#endif /* USART6 */
|
||||
#ifdef USART7
|
||||
else if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, "uart7") == 0)
|
||||
{
|
||||
console_uart.Instance = USART7;
|
||||
}
|
||||
#endif /* USART7 */
|
||||
else
|
||||
{
|
||||
RT_ASSERT(0);
|
||||
}
|
||||
console_uart.Init.BaudRate = 115200;
|
||||
console_uart.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
console_uart.Init.StopBits = UART_STOPBITS_1;
|
||||
console_uart.Init.Parity = UART_PARITY_NONE;
|
||||
console_uart.Init.Mode = UART_MODE_TX_RX;
|
||||
console_uart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
if (HAL_UART_Init(&console_uart) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
void rt_hw_console_output(const char *str)
|
||||
{
|
||||
rt_size_t i = 0, size = 0;
|
||||
char a = '\r';
|
||||
|
||||
__HAL_UNLOCK(&console_uart);
|
||||
|
||||
size = rt_strlen(str);
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
if (*(str + i) == '\n')
|
||||
{
|
||||
HAL_UART_Transmit(&console_uart, (uint8_t *)&a, 1, 1);
|
||||
}
|
||||
HAL_UART_Transmit(&console_uart, (uint8_t *)(str + i), 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
char rt_hw_console_getchar(void)
|
||||
{
|
||||
int ch = -1;
|
||||
|
||||
if (HAL_UART_Receive(&console_uart, (uint8_t *)&ch, 1, 0) != HAL_OK)
|
||||
{
|
||||
ch = -1;
|
||||
rt_thread_mdelay(10);
|
||||
}
|
||||
return ch;
|
||||
}
|
124
libraries/HAL_Drivers/nano/drv_gpio.c
Normal file
124
libraries/HAL_Drivers/nano/drv_gpio.c
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-12-02 Meco Man the first version
|
||||
*/
|
||||
|
||||
#include "drv_gpio.h"
|
||||
#include <board.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#define GET_GPIOx(pin) ((GPIO_TypeDef *)(rt_uint32_t)(pin & 0xFFFFFFFFULL))
|
||||
#define GET_GPIOPin(pin) ((uint16_t)(pin >> 32U))
|
||||
|
||||
void rt_pin_mode(rt_uint64_t pin, rt_uint8_t mode)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_TypeDef *GPIOx = GET_GPIOx(pin);
|
||||
uint16_t GPIO_Pin = GET_GPIOPin(pin);
|
||||
|
||||
RT_ASSERT(mode == PIN_MODE_OUTPUT || mode == PIN_MODE_INPUT ||
|
||||
mode == PIN_MODE_INPUT_PULLUP || mode == PIN_MODE_INPUT_PULLDOWN ||
|
||||
mode == PIN_MODE_OUTPUT_OD);
|
||||
|
||||
switch((rt_ubase_t)GPIOx)
|
||||
{
|
||||
case (rt_ubase_t)GPIOA:
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
break;
|
||||
case (rt_ubase_t)GPIOB:
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
break;
|
||||
case (rt_ubase_t)GPIOC:
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
break;
|
||||
#ifdef GPIOD
|
||||
case (rt_ubase_t)GPIOD:
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
break;
|
||||
#endif /* GPIOD */
|
||||
#ifdef GPIOE
|
||||
case (rt_ubase_t)GPIOE:
|
||||
__HAL_RCC_GPIOE_CLK_ENABLE();
|
||||
break;
|
||||
#endif /* GPIOE */
|
||||
#ifdef GPIOF
|
||||
case (rt_ubase_t)GPIOF:
|
||||
__HAL_RCC_GPIOF_CLK_ENABLE();
|
||||
break;
|
||||
#endif /* GPIOF */
|
||||
#ifdef GPIOG
|
||||
case (rt_ubase_t)GPIOG:
|
||||
__HAL_RCC_GPIOG_CLK_ENABLE();
|
||||
break;
|
||||
#endif /* GPIOG */
|
||||
#ifdef GPIOH
|
||||
case (rt_ubase_t)GPIOH:
|
||||
__HAL_RCC_GPIOH_CLK_ENABLE();
|
||||
break;
|
||||
#endif /* GPIOH */
|
||||
#ifdef GPIOI
|
||||
case (rt_ubase_t)GPIOI:
|
||||
__HAL_RCC_GPIOI_CLK_ENABLE();
|
||||
break;
|
||||
#endif /* GPIOI */
|
||||
}
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_Pin;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
|
||||
if (mode == PIN_MODE_OUTPUT)
|
||||
{
|
||||
/* output setting */
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
}
|
||||
else if (mode == PIN_MODE_INPUT)
|
||||
{
|
||||
/* input setting: not pull. */
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
}
|
||||
else if (mode == PIN_MODE_INPUT_PULLUP)
|
||||
{
|
||||
/* input setting: pull up. */
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
}
|
||||
else if (mode == PIN_MODE_INPUT_PULLDOWN)
|
||||
{
|
||||
/* input setting: pull down. */
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
|
||||
}
|
||||
else if (mode == PIN_MODE_OUTPUT_OD)
|
||||
{
|
||||
/* output setting: od. */
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
}
|
||||
|
||||
HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
|
||||
}
|
||||
|
||||
void rt_pin_write(rt_uint64_t pin, rt_uint8_t value)
|
||||
{
|
||||
GPIO_TypeDef *GPIOx = GET_GPIOx(pin);
|
||||
uint16_t GPIO_Pin = GET_GPIOPin(pin);
|
||||
|
||||
RT_ASSERT(value == PIN_LOW || value == PIN_HIGH);
|
||||
|
||||
HAL_GPIO_WritePin(GPIOx, GPIO_Pin, (value == PIN_LOW) ? GPIO_PIN_RESET : GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
rt_int8_t rt_pin_read(rt_uint64_t pin)
|
||||
{
|
||||
GPIO_TypeDef *GPIOx = GET_GPIOx(pin);
|
||||
uint16_t GPIO_Pin = GET_GPIOPin(pin);
|
||||
|
||||
return HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) == GPIO_PIN_RESET ? PIN_LOW : PIN_HIGH;
|
||||
}
|
39
libraries/HAL_Drivers/nano/drv_gpio.h
Normal file
39
libraries/HAL_Drivers/nano/drv_gpio.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-12-02 Meco Man the first version
|
||||
*/
|
||||
|
||||
#ifndef __DRV_GPIO_H__
|
||||
#define __DRV_GPIO_H__
|
||||
|
||||
#include <rtdef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define PIN_LOW 0x00
|
||||
#define PIN_HIGH 0x01
|
||||
|
||||
#define PIN_MODE_OUTPUT 0x00
|
||||
#define PIN_MODE_INPUT 0x01
|
||||
#define PIN_MODE_INPUT_PULLUP 0x02
|
||||
#define PIN_MODE_INPUT_PULLDOWN 0x03
|
||||
#define PIN_MODE_OUTPUT_OD 0x04
|
||||
|
||||
#define GET_PIN(PORTx,PIN) (rt_uint64_t)((((rt_uint64_t)GPIO_PIN_##PIN) << 32) | (rt_uint64_t)(rt_ubase_t)GPIO##PORTx)
|
||||
|
||||
void rt_pin_mode(rt_uint64_t pin, rt_uint8_t mode);
|
||||
void rt_pin_write(rt_uint64_t pin, rt_uint8_t value);
|
||||
rt_int8_t rt_pin_read(rt_uint64_t pin);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __DRV_GPIO_H__ */
|
Reference in New Issue
Block a user