diff --git a/bsp/nxp/mcx/mcxn/Libraries/drivers/SConscript b/bsp/nxp/mcx/mcxn/Libraries/drivers/SConscript index 7081288d85..c193f1f106 100644 --- a/bsp/nxp/mcx/mcxn/Libraries/drivers/SConscript +++ b/bsp/nxp/mcx/mcxn/Libraries/drivers/SConscript @@ -31,6 +31,9 @@ if GetDepend('BSP_USING_ADC'): if GetDepend('BSP_USING_DAC'): src += ['drv_dac.c'] +if GetDepend('BSP_USING_CAN'): + src += ['drv_can.c'] + if GetDepend('BSP_USING_HWTIMER'): src += ['drv_hwtimer.c'] diff --git a/bsp/nxp/mcx/mcxn/Libraries/drivers/drv_can.c b/bsp/nxp/mcx/mcxn/Libraries/drivers/drv_can.c index ded9082da2..20889b8998 100644 --- a/bsp/nxp/mcx/mcxn/Libraries/drivers/drv_can.c +++ b/bsp/nxp/mcx/mcxn/Libraries/drivers/drv_can.c @@ -15,7 +15,7 @@ #include "fsl_common.h" #include "fsl_flexcan.h" -#define TX_MB_IDX (7) +#define TX_MB_IDX (6) #define RX_MB_COUNT (1) static flexcan_frame_t frame[RX_MB_COUNT]; /* one frame buffer per RX MB */ static rt_uint32_t filter_mask = 0; @@ -26,6 +26,9 @@ enum #ifdef BSP_USING_CAN0 CAN0_INDEX, #endif +#ifdef BSP_USING_CAN1 + CAN1_INDEX, +#endif }; struct imxrt_can @@ -52,6 +55,16 @@ struct imxrt_can flexcans[] = .clock_attach_id = kFRO_HF_to_FLEXCAN0, }, #endif +#ifdef BSP_USING_CAN1 + { + .name = "can1", + .base = CAN1, + .instance = 1, + .irqn = CAN1_IRQn, + .clock_div_name = kCLOCK_DivFlexcan1Clk, + .clock_attach_id = kFRO_HF_to_FLEXCAN1, + }, +#endif }; @@ -62,33 +75,21 @@ static void flexcan_callback(CAN_Type *base, flexcan_handle_t *handle, status_t flexcan_mb_transfer_t rxXfer; can = (struct imxrt_can *)userData; + switch (status) { - case kStatus_FLEXCAN_RxIdle: - rt_hw_can_isr(&can->can_dev, RT_CAN_EVENT_RX_IND | result << 8); - rxXfer.frame = &frame[result - 1]; - rxXfer.mbIdx = result; - FLEXCAN_TransferReceiveNonBlocking(can->base, &can->handle, &rxXfer); - break; + case kStatus_FLEXCAN_RxIdle: + rt_hw_can_isr(&can->can_dev, RT_CAN_EVENT_RX_IND | result << 8); + rxXfer.frame = &frame[result - 1]; + rxXfer.mbIdx = result; + FLEXCAN_TransferReceiveNonBlocking(can->base, &can->handle, &rxXfer); + break; -// case kStatus_FLEXCAN_TxIdle: -// rt_hw_can_isr(&can->can_dev, RT_CAN_EVENT_TX_DONE | (63 - result) << 8); -// break; - -// case kStatus_FLEXCAN_WakeUp: - -// case kStatus_FLEXCAN_ErrorStatus: -// if ((result >= 47) && (result <= 63)) -// { -// rt_hw_can_isr(&can->can_dev, RT_CAN_EVENT_TX_FAIL | (63 - result) << 8); -// } -// break; - -// case kStatus_FLEXCAN_TxSwitchToRx: -// break; - - default: - break; + case kStatus_FLEXCAN_TxIdle: + rt_hw_can_isr(&can->can_dev, RT_CAN_EVENT_TX_DONE | result << 8); + break; + default: + break; } } @@ -105,6 +106,8 @@ static rt_err_t can_cfg(struct rt_can_device *can_dev, struct can_configure *cfg FLEXCAN_GetDefaultConfig(&config); config.baudRate = cfg->baud_rate; config.enableIndividMask = true; /* one filter per MB */ + config.disableSelfReception = true; + switch (cfg->mode) { case RT_CAN_MODE_NORMAL: @@ -129,7 +132,7 @@ static rt_err_t can_cfg(struct rt_can_device *can_dev, struct can_configure *cfg } else { - rt_kprintf("No found Improved Timing Configuration. Just used default configuration\n"); + //rt_kprintf("No found Improved Timing Configuration. Just used default configuration\n"); } FLEXCAN_Init(can->base, &config, CLOCK_GetFlexcanClkFreq(can->instance)); @@ -279,8 +282,20 @@ static rt_err_t can_control(struct rt_can_device *can_dev, int cmd, void *arg) break; case RT_CAN_CMD_SET_BAUD: - res = -RT_ERROR; - break; + { + struct can_configure *cfg = (struct can_configure *)arg; + if (cfg != RT_NULL) + { + can->can_dev.config = *cfg; + can_cfg(can_dev, cfg); + res = RT_EOK; + } + else + { + res = -RT_ERROR; + } + break; + } case RT_CAN_CMD_SET_MODE: res = -RT_ERROR; break; @@ -300,18 +315,24 @@ static rt_err_t can_control(struct rt_can_device *can_dev, int cmd, void *arg) return res; } - static rt_ssize_t can_send(struct rt_can_device *can_dev, const void *buf, rt_uint32_t boxno) { struct imxrt_can *can; struct rt_can_msg *msg; status_t ret; flexcan_frame_t frame; + flexcan_mb_transfer_t txXfer; + + RT_ASSERT(can_dev != RT_NULL); + RT_ASSERT(buf != RT_NULL); can = (struct imxrt_can *)can_dev->parent.user_data; msg = (struct rt_can_msg *) buf; + + RT_ASSERT(can != RT_NULL); + RT_ASSERT(msg != RT_NULL); - FLEXCAN_SetTxMbConfig(can->base, TX_MB_IDX, true); + FLEXCAN_SetTxMbConfig(can->base, boxno, true); if (RT_CAN_STDID == msg->ide) { @@ -343,7 +364,10 @@ static rt_ssize_t can_send(struct rt_can_device *can_dev, const void *buf, rt_ui frame.dataByte6 = msg->data[6]; frame.dataByte7 = msg->data[7]; - ret = FLEXCAN_TransferSendBlocking(can->base, TX_MB_IDX, &frame); + txXfer.mbIdx = boxno; + txXfer.frame = &frame; + + ret = FLEXCAN_TransferSendNonBlocking(can->base, &can->handle, &txXfer); switch (ret) { case kStatus_Success: @@ -360,15 +384,17 @@ static rt_ssize_t can_send(struct rt_can_device *can_dev, const void *buf, rt_ui return (rt_ssize_t)ret; } - static rt_ssize_t can_recv(struct rt_can_device *can_dev, void *buf, rt_uint32_t boxno) { struct imxrt_can *can; struct rt_can_msg *pmsg; rt_uint8_t index; + RT_ASSERT(can_dev != RT_NULL); + can = (struct imxrt_can *)can_dev->parent.user_data; pmsg = (struct rt_can_msg *) buf; + RT_ASSERT(can != RT_NULL); index = boxno - 1; @@ -406,6 +432,7 @@ static rt_ssize_t can_recv(struct rt_can_device *can_dev, void *buf, rt_uint32_t } + static struct rt_can_ops imxrt_can_ops = { .configure = can_cfg, diff --git a/bsp/nxp/mcx/mcxn/frdm-mcxn236/.config b/bsp/nxp/mcx/mcxn/frdm-mcxn236/.config index a5e8be5e75..6c3226ebde 100644 --- a/bsp/nxp/mcx/mcxn/frdm-mcxn236/.config +++ b/bsp/nxp/mcx/mcxn/frdm-mcxn236/.config @@ -145,7 +145,9 @@ CONFIG_RT_USING_SERIAL_V1=y # CONFIG_RT_USING_SERIAL_V2 is not set CONFIG_RT_SERIAL_USING_DMA=y CONFIG_RT_SERIAL_RB_BUFSZ=64 -# CONFIG_RT_USING_CAN is not set +CONFIG_RT_USING_CAN=y +# CONFIG_RT_CAN_USING_HDR is not set +# CONFIG_RT_CAN_USING_CANFD is not set # CONFIG_RT_USING_CPUTIME is not set # CONFIG_RT_USING_I2C is not set # CONFIG_RT_USING_PHY is not set @@ -1213,6 +1215,9 @@ CONFIG_SOC_MCXN947=y # # CONFIG_BSP_USING_DMA is not set CONFIG_BSP_USING_PIN=y +CONFIG_BSP_USING_CAN=y +# CONFIG_BSP_USING_CAN0 is not set +CONFIG_BSP_USING_CAN1=y CONFIG_BSP_USING_UART=y CONFIG_BSP_USING_UART4=y CONFIG_BSP_USING_UART5=y diff --git a/bsp/nxp/mcx/mcxn/frdm-mcxn236/applications/uart_sample.c b/bsp/nxp/mcx/mcxn/frdm-mcxn236/applications/uart_sample.c deleted file mode 100644 index fb1840724b..0000000000 --- a/bsp/nxp/mcx/mcxn/frdm-mcxn236/applications/uart_sample.c +++ /dev/null @@ -1,109 +0,0 @@ -/** - * @file uart_sample.c - * @brief UART5 interrupt-driven sample for RT-Thread on MCX N236 - */ - -#include -#include - -#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); diff --git a/bsp/nxp/mcx/mcxn/frdm-mcxn236/applications/wdt_sample.c b/bsp/nxp/mcx/mcxn/frdm-mcxn236/applications/wdt_sample.c deleted file mode 100644 index 4acc78f6af..0000000000 --- a/bsp/nxp/mcx/mcxn/frdm-mcxn236/applications/wdt_sample.c +++ /dev/null @@ -1,58 +0,0 @@ -#include -#include - -#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); \ No newline at end of file diff --git a/bsp/nxp/mcx/mcxn/frdm-mcxn236/board/Kconfig b/bsp/nxp/mcx/mcxn/frdm-mcxn236/board/Kconfig index aea20769cd..cd39268f14 100644 --- a/bsp/nxp/mcx/mcxn/frdm-mcxn236/board/Kconfig +++ b/bsp/nxp/mcx/mcxn/frdm-mcxn236/board/Kconfig @@ -19,6 +19,19 @@ menu "On-chip Peripheral Drivers" select RT_USING_PIN default y + config BSP_USING_CAN + bool "Enable CAN" + default n + select RT_USING_CAN if BSP_USING_CAN + if BSP_USING_CAN + config BSP_USING_CAN0 + bool "Enable CAN0" + default n + config BSP_USING_CAN1 + bool "Enable CAN1" + default n + endif + menuconfig BSP_USING_UART bool "Enable UART" default y diff --git a/bsp/nxp/mcx/mcxn/frdm-mcxn236/board/MCUX_Config/board/pin_mux.c b/bsp/nxp/mcx/mcxn/frdm-mcxn236/board/MCUX_Config/board/pin_mux.c index 130212aca3..ff98fe4884 100644 --- a/bsp/nxp/mcx/mcxn/frdm-mcxn236/board/MCUX_Config/board/pin_mux.c +++ b/bsp/nxp/mcx/mcxn/frdm-mcxn236/board/MCUX_Config/board/pin_mux.c @@ -125,6 +125,12 @@ void BOARD_InitBootPins(void) PORT4->PCR[21] = PORT_PCR_MUX(FLEXIO_DATA13_MUX) | PORT_PCR_PE_MASK | PORT_PCR_PS(0); /* FXIO0_D13 */ PORT4->PCR[22] = PORT_PCR_MUX(FLEXIO_DATA14_MUX) | PORT_PCR_PE_MASK | PORT_PCR_PS(0); /* FXIO0_D14 */ PORT4->PCR[23] = PORT_PCR_MUX(FLEXIO_DATA15_MUX) | PORT_PCR_PE_MASK | PORT_PCR_PS(0); /* FXIO0_D15 */ + +#ifdef BSP_USING_CAN1 + PORT4->PCR[15] = PORT_PCR_MUX(11) | PORT_PCR_IBE(1); /* CAN1_RXD */ + PORT4->PCR[16] = PORT_PCR_MUX(11) | PORT_PCR_IBE(1); /* CAN1_TXD */ +#endif + } diff --git a/bsp/nxp/mcx/mcxn/frdm-mcxn236/board/linker_scripts/MCXN236_flash.scf b/bsp/nxp/mcx/mcxn/frdm-mcxn236/board/linker_scripts/MCXN236_flash.scf index 36b28a9ff6..8013afa8ec 100644 --- a/bsp/nxp/mcx/mcxn/frdm-mcxn236/board/linker_scripts/MCXN236_flash.scf +++ b/bsp/nxp/mcx/mcxn/frdm-mcxn236/board/linker_scripts/MCXN236_flash.scf @@ -29,7 +29,7 @@ #if (defined(__stack_size__)) #define Stack_Size __stack_size__ #else - #define Stack_Size 0x0400 + #define Stack_Size 0x1000 #endif #if (defined(__heap_size__)) diff --git a/bsp/nxp/mcx/mcxn/frdm-mcxn236/project.ewp b/bsp/nxp/mcx/mcxn/frdm-mcxn236/project.ewp index f8e8a1d6d2..cebc66c1d8 100644 --- a/bsp/nxp/mcx/mcxn/frdm-mcxn236/project.ewp +++ b/bsp/nxp/mcx/mcxn/frdm-mcxn236/project.ewp @@ -358,27 +358,27 @@ CCIncludePath2 $PROJ_DIR$\..\Libraries\drivers\config - $PROJ_DIR$\applications - $PROJ_DIR$\..\..\..\..\..\components\drivers\include - $PROJ_DIR$\..\Libraries\CMSIS\Core\Include - $PROJ_DIR$\..\..\..\..\..\components\utilities\ulog - $PROJ_DIR$\board\MCUX_Config\board - $PROJ_DIR$\..\Libraries\MCXN236\MCXN236\drivers - $PROJ_DIR$\..\..\..\..\..\components\libc\compilers\common\include - $PROJ_DIR$\..\..\..\..\..\components\libc\posix\ipc - $PROJ_DIR$\..\..\..\..\..\components\libc\posix\io\eventfd - $PROJ_DIR$\..\Libraries\drivers - $PROJ_DIR$\..\Libraries\MCXN236\MCXN236 - $PROJ_DIR$\..\..\..\..\..\components\libc\posix\io\poll - $PROJ_DIR$\..\..\..\..\..\components\libc\compilers\common\extension\fcntl\octal - $PROJ_DIR$\..\..\..\..\..\libcpu\arm\cortex-m33 - $PROJ_DIR$\..\..\..\..\..\libcpu\arm\common - $PROJ_DIR$\..\..\..\..\..\components\finsh - $PROJ_DIR$\..\..\..\..\..\include - $PROJ_DIR$\..\..\..\..\..\components\libc\compilers\common\extension - $PROJ_DIR$\. - $PROJ_DIR$\board $PROJ_DIR$\..\..\..\..\..\components\libc\posix\io\epoll + $PROJ_DIR$\..\..\..\..\..\components\libc\posix\io\poll + $PROJ_DIR$\..\..\..\..\..\components\libc\posix\ipc + $PROJ_DIR$\applications + $PROJ_DIR$\..\..\..\..\..\components\utilities\ulog + $PROJ_DIR$\..\..\..\..\..\components\drivers\include + $PROJ_DIR$\..\Libraries\MCXN236\MCXN236\drivers + $PROJ_DIR$\..\..\..\..\..\libcpu\arm\cortex-m33 + $PROJ_DIR$\..\Libraries\MCXN236\MCXN236 + $PROJ_DIR$\..\..\..\..\..\include + $PROJ_DIR$\..\..\..\..\..\components\libc\posix\io\eventfd + $PROJ_DIR$\..\..\..\..\..\components\libc\compilers\common\extension + $PROJ_DIR$\..\..\..\..\..\components\libc\compilers\common\include + $PROJ_DIR$\..\Libraries\drivers + $PROJ_DIR$\..\..\..\..\..\components\libc\compilers\common\extension\fcntl\octal + $PROJ_DIR$\..\..\..\..\..\components\finsh + $PROJ_DIR$\..\..\..\..\..\libcpu\arm\common + $PROJ_DIR$\board + $PROJ_DIR$\board\MCUX_Config\board + $PROJ_DIR$\..\Libraries\CMSIS\Core\Include + $PROJ_DIR$\.