Skip to content

Commit 02f0e0d

Browse files
dspic: UART polling method (zephyrproject-rtos#31)
* dspic: UART polling method Added UART polling support for dsPIC33AK128MC106 Signed-off-by: Udhayanandhan Jayakumar <[email protected]> * dspic: Logging using UART (zephyrproject-rtos#33) * dspic: Logging using UART Added support for logging using UART Signed-off-by: Udhayanandhan Jayakumar <[email protected]> * dspic: copright headers (zephyrproject-rtos#36) dspic: copyright headers fix Added copyrights to files Signed-off-by: Udhayanandhan Jayakumar <[email protected]> --------- Signed-off-by: Udhayanandhan Jayakumar <[email protected]> --------- Signed-off-by: Udhayanandhan Jayakumar <[email protected]>
1 parent 3ce5456 commit 02f0e0d

File tree

11 files changed

+187
-9
lines changed

11 files changed

+187
-9
lines changed

boards/microchip/dspic33/dspic33a_curiosity/dspic33a_curiosity_p33ak128mc106.dts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Copyright (c) 2025, Microchip Technology Inc.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
/dts-v1/;
27
#include "p33ak128mc106.dtsi"
38

@@ -8,6 +13,12 @@
813
chosen {
914
zephyr,flash = &flash0;
1015
zephyr,sram = &sram0;
16+
zephyr,serial = &uart1;
17+
zephyr,console = &uart1;
18+
};
19+
20+
aliases {
21+
uart-0 = &uart1;
1122
};
1223
};
1324

@@ -21,3 +32,7 @@
2132
clock-frequency = <8000000>;
2233
status = "okay";
2334
};
35+
36+
&uart1 {
37+
status = "okay";
38+
};

boards/microchip/dspic33/dspic33a_curiosity/dspic33a_curiosity_p33ak128mc106_defconfig

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1-
# Enables the RAM-based console backend for logging output.
1+
# Copyright (c) 2025, Microchip Technology Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
24
CONFIG_LOG=y
35
CONFIG_LOG_MODE_MINIMAL=y
46
CONFIG_PRINTK=y
57
CONFIG_CONSOLE=y
6-
CONFIG_RAM_CONSOLE=y
8+
CONFIG_STDOUT_CONSOLE=n
9+
10+
# For LOG_MODE_MINIMAL using UART
11+
CONFIG_UART_CONSOLE=y
12+
CONFIG_LOG_PRINTK=n
13+
CONFIG_EARLY_CONSOLE=y
14+
CONFIG_SERIAL=y
15+
16+
# for LOG_MODE_IMMEDIATE using UART
17+
# Uncomment below if using LOG_MODE_IMMEDIATE instead
18+
# CONFIG_UART_CONSOLE=y
19+
# CONFIG_LOG_ALWAYS_RUNTIME=y
20+
# CONFIG_EARLY_CONSOLE=y
21+
# CONFIG_SERIAL=y
722

823
# Enables support for GPIO drivers on the target platform.
924
# CONFIG_GPIO=y

drivers/serial/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ zephyr_library_sources_ifdef(CONFIG_UART_CC23X0 uart_cc23x0.c)
3131
zephyr_library_sources_ifdef(CONFIG_UART_CC32XX uart_cc32xx.c)
3232
zephyr_library_sources_ifdef(CONFIG_UART_CDNS uart_cdns.c)
3333
zephyr_library_sources_ifdef(CONFIG_UART_CMSDK_APB uart_cmsdk_apb.c)
34+
zephyr_library_sources_ifdef(CONFIG_UART_DSPIC uart_dspic.c)
3435
zephyr_library_sources_ifdef(CONFIG_UART_EFINIX_SAPPIHIRE uart_efinix_sapphire.c)
3536
zephyr_library_sources_ifdef(CONFIG_UART_EMUL uart_emul.c)
3637
zephyr_library_sources_ifdef(CONFIG_UART_ENE_KB106X uart_ene_kb106x.c)

drivers/serial/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ rsource "Kconfig.cc23x0"
172172
rsource "Kconfig.cc32xx"
173173
rsource "Kconfig.cdns"
174174
rsource "Kconfig.cmsdk_apb"
175+
rsource "Kconfig.dspic_uart"
175176
rsource "Kconfig.efinix_sapphire"
176177
rsource "Kconfig.emul"
177178
rsource "Kconfig.ene"

drivers/serial/Kconfig.dspic_uart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2025, Microchip Technology Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
5+
config UART_DSPIC
6+
bool "dsPIC33A UART driver"
7+
default y
8+
select SERIAL_HAS_DRIVER
9+
help
10+
Enable support for the dsPIC33A UART driver.

drivers/serial/uart_dspic.c

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright (c) 2025, Microchip Technology Inc.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <zephyr/device.h>
7+
#include <zephyr/drivers/uart.h>
8+
#include <zephyr/kernel.h>
9+
#include <zephyr/sys/util.h>
10+
#include <soc.h>
11+
#include <math.h>
12+
13+
#ifndef _ASMLANGUAGE
14+
#include <xc.h>
15+
#endif
16+
17+
#define DT_DRV_COMPAT microchip_dspic33_uart
18+
19+
#define OFFSET_MODE 0x00U
20+
#define OFFSET_STA 0x04U
21+
#define OFFSET_TXREG 0x10U
22+
#define OFFSET_BRG 0x08U
23+
#define OFFSET_RXREG 0x0CU
24+
#define BIT_UTXEN 0x00000020U
25+
#define BIT_URXEN 0x00000010U
26+
#define BIT_UARTEN 0x00008000U
27+
#define BIT_TXBF 0x00100000U
28+
#define BIT_RXBE 0x00020000U
29+
#define FRACTIONAL_BRG 0x8000000U
30+
31+
#define CALCULATE_BRG(baudrate) \
32+
((ceil(((double)(sys_clock_hw_cycles_per_sec())) / ((double)(2U * (baudrate))))))
33+
const struct device *dev = DEVICE_DT_GET(DT_NODELABEL(uart1));
34+
35+
struct uart_dspic_config {
36+
uint32_t base;
37+
uint32_t baudrate;
38+
};
39+
40+
static void uart_dspic_poll_out(const struct device *dev, unsigned char c)
41+
{
42+
const struct uart_dspic_config *cfg = dev->config;
43+
volatile uint32_t *UxSTA = (void *)(cfg->base + OFFSET_STA);
44+
volatile uint32_t *UxTXREG = (void *)(cfg->base + OFFSET_TXREG);
45+
46+
/* Wait until there is space in the TX FIFO */
47+
while ((bool)(void *)((*UxSTA) & BIT_TXBF)) {
48+
;
49+
}
50+
51+
*UxTXREG = c;
52+
}
53+
54+
static int uart_dspic_poll_in(const struct device *dev, unsigned char *c)
55+
{
56+
const struct uart_dspic_config *cfg = dev->config;
57+
volatile uint32_t *UxSTA = (void *)(cfg->base + OFFSET_STA);
58+
volatile uint32_t *UxRXREG = (void *)(cfg->base + OFFSET_RXREG);
59+
int ret_val;
60+
61+
/* If receiver buffer is empty, return -1 */
62+
if ((*UxSTA & BIT_RXBE) != 0U) {
63+
ret_val = -EPERM;
64+
}
65+
66+
else {
67+
*c = *UxRXREG & 0xFF;
68+
ret_val = 0;
69+
}
70+
71+
return ret_val;
72+
}
73+
74+
static int uart_dspic_init(const struct device *dev)
75+
{
76+
LATB = 0x0040UL;
77+
TRISB = 0x0FBFUL;
78+
ANSELA = 0x0FFFUL;
79+
ANSELB = 0x033FUL;
80+
81+
/* Assign U1TX to RP23 and U1RX to RP24*/
82+
_RP23R = 9;
83+
_U1RXR = 24;
84+
const struct uart_dspic_config *cfg = dev->config;
85+
volatile uint32_t *UxCON = (void *)(cfg->base + OFFSET_MODE);
86+
87+
/* Setting the baudrate */
88+
*UxCON = FRACTIONAL_BRG;
89+
volatile uint32_t *UxBRG = (void *)(cfg->base + OFFSET_BRG);
90+
*UxBRG = (uint32_t)CALCULATE_BRG(cfg->baudrate);
91+
92+
/* Enable UART */
93+
*UxCON |= BIT_UARTEN | BIT_UTXEN | BIT_URXEN;
94+
95+
return 0;
96+
}
97+
98+
static const struct uart_driver_api uart_dspic_api = {
99+
.poll_out = uart_dspic_poll_out,
100+
.poll_in = uart_dspic_poll_in,
101+
};
102+
103+
#define UART_DSPIC_INIT(inst) \
104+
static const struct uart_dspic_config uart_dspic_config_##inst = { \
105+
.base = DT_REG_ADDR(DT_INST(inst, microchip_dspic33_uart)), \
106+
.baudrate = DT_PROP(DT_INST(inst, microchip_dspic33_uart), current_speed), \
107+
}; \
108+
DEVICE_DT_INST_DEFINE(inst, uart_dspic_init, NULL, NULL, &uart_dspic_config_##inst, \
109+
PRE_KERNEL_1, CONFIG_SERIAL_INIT_PRIORITY, &uart_dspic_api);
110+
111+
DT_INST_FOREACH_STATUS_OKAY(UART_DSPIC_INIT)

drivers/timer/Kconfig.mchp_dspic33

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Copyright (C) 2025 Microchip Technology Inc.
2-
#
32
# SPDX-License-Identifier: Apache-2.0
4-
#
53

64
config MCHP_DSPIC33_TIMER
75
bool "Microchip DSPIC33A timer"

drivers/timer/mchp_dspic33_timer.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
/* zephyr/arch/dspic33AK/include/arch.h */
1+
/*
2+
* Copyright (c) 2025, Microchip Technology Inc.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
25

36
#ifndef ZEPHYR_ARCH_DSPIC33AK_INCLUDE_ARCH_H_
47
#define ZEPHYR_ARCH_DSPIC33AK_INCLUDE_ARCH_H_
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# microchip,dspic33-uart.yaml
2+
3+
description: Microchip dsPIC33 UART controller
4+
5+
compatible: "microchip,dspic33-uart"
6+
7+
include: uart-controller.yaml
8+
9+
properties:
10+
reg:
11+
required: true
12+
13+
clock-frequency:
14+
type: int
15+
required: true
16+
17+
status:
18+
type: string
19+
required: true

dts/dspic/p33ak128mc106.dtsi

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,13 @@
6969
label = "TIMER_1";
7070
status = "okay";
7171
};
72+
73+
uart1: uart@1700 {
74+
compatible = "microchip,dspic33-uart";
75+
reg = <0x1700 0x28>;
76+
clock-frequency = <4000000>;
77+
current-speed = <115200>;
78+
status = "okay";
79+
};
7280
};
73-
};
81+
};

0 commit comments

Comments
 (0)