add wdog driver and refine default project

This commit is contained in:
yandld 2024-08-09 17:11:17 +08:00 committed by Meco Man
parent dec34f91bf
commit 6ef77d7d32
12 changed files with 3191 additions and 538 deletions

View File

@ -52,6 +52,7 @@ src += ['MCXN236/drivers/fsl_lpspi_edma.c']
src += ['MCXN236/drivers/fsl_lptmr.c']
src += ['MCXN236/drivers/fsl_lpuart.c']
src += ['MCXN236/drivers/fsl_mrt.c']
src += ['MCXN236/drivers/fsl_wwdt.c']
src += ['MCXN236/drivers/fsl_reset.c']
src += ['MCXN236/drivers/fsl_spc.c']
src += ['MCXN236/drivers/fsl_vref.c']

View File

@ -1,12 +1,12 @@
///*
// * Copyright (c) 2006-2024, RT-Thread Development Team
// *
// * SPDX-License-Identifier: Apache-2.0
// *
// * Change Logs:
// * Date Author Notes
// * 2019-07-15 yandld The first version for MCXN
// */
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-07-15 yandld The first version for MCXN
*/
#include <rtthread.h>
#include "drv_uart.h"

View File

@ -0,0 +1,118 @@
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-08-09 Your Name The first version for MCXN236
*/
#include <rtthread.h>
#include "drv_wdt.h"
#include "fsl_wwdt.h"
#include "fsl_clock.h"
#ifdef RT_USING_WDT
#define WDT_CLK_FREQ CLOCK_GetWdtClkFreq(0)
struct mcx_wdt
{
rt_watchdog_t watchdog;
WWDT_Type *wdt_base;
clock_name_t clock_src;
clock_ip_name_t clock_ip_name;
};
static struct mcx_wdt wdt_dev;
static rt_err_t wdt_init(rt_watchdog_t *wdt)
{
uint32_t wdtFreq;
wwdt_config_t config;
/* The WDT divides the input frequency into it by 4 */
wdtFreq = WDT_CLK_FREQ / 4;
/* Enable FRO 1M clock for WWDT module. */
SYSCON->CLOCK_CTRL |= SYSCON_CLOCK_CTRL_FRO1MHZ_CLK_ENA_MASK;
WWDT_GetDefaultConfig(&config);
config.timeoutValue = wdtFreq * 1;
config.windowValue = wdtFreq * 1;
/* Configure WWDT to reset on timeout */
config.enableWatchdogReset = true;
/* Setup watchdog clock frequency(Hz). */
config.clockFreq_Hz = WDT_CLK_FREQ;
CLOCK_EnableClock(wdt_dev.clock_ip_name);
CLOCK_SetClkDiv(kCLOCK_DivWdt0Clk, 1U);
WWDT_Init(wdt_dev.wdt_base, &config);
return RT_EOK;
}
static rt_err_t wdt_control(rt_watchdog_t *wdt, int cmd, void *arg)
{
switch (cmd)
{
case RT_DEVICE_CTRL_WDT_START:
WWDT_Enable(wdt_dev.wdt_base);
return RT_EOK;
case RT_DEVICE_CTRL_WDT_STOP:
WWDT_Disable(wdt_dev.wdt_base);
return RT_EOK;
case RT_DEVICE_CTRL_WDT_KEEPALIVE:
WWDT_Refresh(wdt_dev.wdt_base);
return RT_EOK;
case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
if (arg != RT_NULL)
{
uint32_t timeout = *((uint32_t *)arg);
timeout = timeout * WDT_CLK_FREQ / 4;
WWDT_SetTimeoutValue(wdt_dev.wdt_base, timeout);
return RT_EOK;
}
return -RT_ERROR;
default:
return -RT_ERROR;
}
}
static struct rt_watchdog_ops wdt_ops =
{
wdt_init,
wdt_control,
};
int rt_hw_wdt_init(void)
{
wdt_dev.wdt_base = WWDT0;
wdt_dev.clock_src = kCLOCK_Clk1M;
wdt_dev.clock_ip_name = kCLOCK_Wwdt0;
wdt_dev.watchdog.ops = &wdt_ops;
if (rt_hw_watchdog_register(&wdt_dev.watchdog, "wdt", RT_DEVICE_FLAG_DEACTIVATE, RT_NULL) != RT_EOK)
{
rt_kprintf("wdt register failed\n");
return -RT_ERROR;
}
return RT_EOK;
}
INIT_BOARD_EXPORT(rt_hw_wdt_init);
#endif /* RT_USING_WDT */

View File

@ -0,0 +1,19 @@
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-08-09 Your Name The first version for MCXN236
*/
#ifndef __DRV_WDT_H__
#define __DRV_WDT_H__
#include <rtthread.h>
#include <rtdevice.h>
int rt_hw_wdt_init(void);
#endif /* __DRV_WDT_H__ */

View File

@ -21,7 +21,7 @@ CONFIG_RT_HOOK_USING_FUNC_PTR=y
# CONFIG_RT_USING_HOOKLIST is not set
CONFIG_RT_USING_IDLE_HOOK=y
CONFIG_RT_IDLE_HOOK_LIST_SIZE=4
CONFIG_IDLE_THREAD_STACK_SIZE=256
CONFIG_IDLE_THREAD_STACK_SIZE=512
CONFIG_RT_USING_TIMER_SOFT=y
CONFIG_RT_TIMER_THREAD_PRIO=4
CONFIG_RT_TIMER_THREAD_STACK_SIZE=512
@ -163,7 +163,7 @@ CONFIG_RT_SERIAL_RB_BUFSZ=64
# CONFIG_RT_USING_RTC is not set
# CONFIG_RT_USING_SDIO is not set
# CONFIG_RT_USING_SPI is not set
# CONFIG_RT_USING_WDT is not set
CONFIG_RT_USING_WDT=y
# CONFIG_RT_USING_AUDIO is not set
# CONFIG_RT_USING_SENSOR is not set
# CONFIG_RT_USING_TOUCH is not set
@ -1218,11 +1218,12 @@ CONFIG_BSP_USING_UART4=y
CONFIG_BSP_USING_UART5=y
CONFIG_BSP_USING_UART2=y
# CONFIG_BSP_USING_I2C is not set
# CONFIG_BSP_USING_SW_I2C is not set
# CONFIG_BSP_USING_SPI is not set
# CONFIG_BSP_USING_ADC is not set
# CONFIG_BSP_USING_DAC is not set
# CONFIG_BSP_USING_RTC is not set
# CONFIG_BSP_USING_WDT is not set
CONFIG_BSP_USING_WDT=y
# CONFIG_BSP_USING_HWTIMER is not set
# CONFIG_BSP_USING_PWM is not set
# end of On-chip Peripheral Drivers

View File

@ -0,0 +1,109 @@
/**
* @file uart_sample.c
* @brief UART5 interrupt-driven sample for RT-Thread on MCX N236
*/
#include <rtthread.h>
#include <rtdevice.h>
#define UART_DEVICE_NAME "uart5"
#define UART_THREAD_STACK 1024
#define UART_THREAD_PRIORITY 25
#define UART_THREAD_TIMESLICE 10
static rt_device_t g_uart_device;
static struct rt_semaphore g_rx_sem;
/**
* @brief UART receive callback function
* @param dev Pointer to device
* @param size Size of received data
* @return RT_EOK
*/
static rt_err_t uart_rx_callback(rt_device_t dev, rt_size_t size)
{
rt_sem_release(&g_rx_sem);
return RT_EOK;
}
/**
* @brief UART processing thread
* @param parameter Thread parameter (unused)
*/
static void uart_thread_entry(void *parameter)
{
char ch;
while (1)
{
/* Wait for incoming data */
rt_sem_take(&g_rx_sem, RT_WAITING_FOREVER);
/* Read and echo back each character */
while (rt_device_read(g_uart_device, -1, &ch, 1) == 1)
{
rt_device_write(g_uart_device, 0, &ch, 1);
}
}
}
/**
* @brief Initialize and start UART sample
* @return RT_EOK on success, RT_ERROR otherwise
*/
static int uart_sample(void)
{
rt_err_t result = RT_EOK;
rt_thread_t thread;
const char *welcome_msg = "RT-Thread MCX N236 UART5 Sample Started!\r\n";
/* Find UART device */
g_uart_device = rt_device_find(UART_DEVICE_NAME);
if (!g_uart_device)
{
rt_kprintf("Failed to find %s device!\n", UART_DEVICE_NAME);
return RT_ERROR;
}
/* Open UART device */
result = rt_device_open(g_uart_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
if (result != RT_EOK)
{
rt_kprintf("Failed to open %s device!\n", UART_DEVICE_NAME);
return result;
}
/* Initialize receive semaphore */
result = rt_sem_init(&g_rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
if (result != RT_EOK)
{
rt_kprintf("Failed to initialize receive semaphore!\n");
return result;
}
/* Set receive callback */
rt_device_set_rx_indicate(g_uart_device, uart_rx_callback);
/* Send welcome message */
rt_device_write(g_uart_device, 0, welcome_msg, rt_strlen(welcome_msg));
/* Create UART processing thread */
thread = rt_thread_create("uart_thread", uart_thread_entry, RT_NULL,
UART_THREAD_STACK, UART_THREAD_PRIORITY, UART_THREAD_TIMESLICE);
if (thread != RT_NULL)
{
rt_kprintf("Starting UART processing thread...\n");
rt_thread_startup(thread);
}
else
{
rt_kprintf("Failed to create UART processing thread!\n");
result = RT_ERROR;
}
return result;
}
/* Export the initialization function to MSH */
MSH_CMD_EXPORT(uart_sample, Initialize UART5 sample);

View File

@ -0,0 +1,58 @@
#include <rtthread.h>
#include <rtdevice.h>
#define WDT_DEVICE_NAME "wdt"
static rt_device_t wdg_dev;
static void idle_hook(void)
{
rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
// rt_kprintf("feed the dog!\n ");
}
static int wdt_sample(int argc, char *argv[])
{
rt_err_t ret = RT_EOK;
rt_uint32_t timeout = 1;
char device_name[RT_NAME_MAX];
if (argc == 2)
{
rt_strncpy(device_name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(device_name, WDT_DEVICE_NAME, RT_NAME_MAX);
}
wdg_dev = rt_device_find(device_name);
if (!wdg_dev)
{
rt_kprintf("find %s failed!\n", device_name);
return RT_ERROR;
}
rt_device_init(wdg_dev);
ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
if (ret != RT_EOK)
{
rt_kprintf("set %s timeout failed!\n", device_name);
return RT_ERROR;
}
ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_START, RT_NULL);
if (ret != RT_EOK)
{
rt_kprintf("start %s failed!\n", device_name);
return -RT_ERROR;
}
rt_thread_idle_sethook(idle_hook);
return ret;
}
MSH_CMD_EXPORT(wdt_sample, wdt sample);

View File

@ -21,13 +21,13 @@ void BOARD_InitBootPins(void)
/* UART */
/* MCU_LINK UART */
PORT1->PCR[8] = PORT_PCR_MUX(2) | PORT_PCR_PS(0) | PORT_PCR_IBE(1); /* FC4_P0 */
PORT1->PCR[9] = PORT_PCR_MUX(2) | PORT_PCR_PS(0) | PORT_PCR_IBE(1); /* FC4_P1 */
/* Mikro Bus UART */
PORT1->PCR[16] = PORT_PCR_MUX(2) | PORT_PCR_PS(0) | PORT_PCR_PE(1) | PORT_PCR_IBE(1); /* FC5_UART */
PORT1->PCR[17] = PORT_PCR_MUX(2) | PORT_PCR_PS(0) | PORT_PCR_PE(1) | PORT_PCR_IBE(1); /* FC5_UART */
PORT1->PCR[16] = PORT_PCR_MUX(2) | PORT_PCR_PS(0) | PORT_PCR_PE(1) | PORT_PCR_IBE(1); /* FC5_P0 UART */
PORT1->PCR[17] = PORT_PCR_MUX(2) | PORT_PCR_PS(0) | PORT_PCR_PE(1) | PORT_PCR_IBE(1); /* FC5_P1 UART */
/* MCX_RST UART */
PORT4->PCR[2] = PORT_PCR_MUX(2) | PORT_PCR_PS(0) | PORT_PCR_PE(1) | PORT_PCR_IBE(1); /* FC2_UART */

View File

@ -113,7 +113,7 @@ void rt_hw_board_init()
#endif
#ifdef RT_USING_HEAP
rt_kprintf("sram heap, begin: 0x%p, end: 0x%p\n", HEAP_BEGIN, HEAP_END);
rt_kprintf("sram heap, begin: %p, end: %p\n", HEAP_BEGIN, HEAP_END);
rt_system_heap_init((void *)HEAP_BEGIN, (void *)(HEAP_END));
#endif
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -15,7 +15,7 @@
#define RT_HOOK_USING_FUNC_PTR
#define RT_USING_IDLE_HOOK
#define RT_IDLE_HOOK_LIST_SIZE 4
#define IDLE_THREAD_STACK_SIZE 256
#define IDLE_THREAD_STACK_SIZE 512
#define RT_USING_TIMER_SOFT
#define RT_TIMER_THREAD_PRIO 4
#define RT_TIMER_THREAD_STACK_SIZE 512
@ -97,6 +97,7 @@
#define RT_USING_SERIAL_V1
#define RT_SERIAL_USING_DMA
#define RT_SERIAL_RB_BUFSZ 64
#define RT_USING_WDT
#define RT_USING_PIN
#define RT_USING_HWTIMER
/* end of Device Drivers */
@ -355,6 +356,7 @@
#define BSP_USING_UART4
#define BSP_USING_UART5
#define BSP_USING_UART2
#define BSP_USING_WDT
/* end of On-chip Peripheral Drivers */
/* Board extended module Drivers */