Skip to content

Commit e346da9

Browse files
Release: v4.0
Highlights: - UART interrupt support - Program memory optimisation - Support for ztest and twister - Context switch refignment - Kernel and thread test cases - Samples support - UART: Echo bot, native tty, passthrough - Blinky - Philosophers - Hello world - Pin control driver - New board support (sdPIC33AK512MPS512) Fixes: - Context switch fixes - System timer fixes - Build fixes Signed-off-by: Muhammed Zamroodh <[email protected]>
1 parent 1c40a23 commit e346da9

File tree

104 files changed

+3578
-834
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+3578
-834
lines changed

arch/Kconfig

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ config DSPIC
174174
bool
175175
select ARCH_IS_SET
176176
select STACK_GROWS_UP
177-
select ARCH_HAS_CUSTOM_SWAP_TO_MAIN
178177
select LITTLE_ENDIAN
179178
select ARCH_HAS_THREAD_LOCAL_STORAGE
180179
select TOOLCHAIN_SUPPORTS_THREAD_LOCAL_STORAGE
@@ -183,11 +182,6 @@ config DSPIC
183182
select STACK_SENTINAL
184183
select ATOMIC_OPERATIONS_C
185184
select ARCH_HAS_VECTOR_TABLE_RELOCATION
186-
select CPU_HAS_ICACHE
187-
select ARCH_HAS_CUSTOM_CPU_IDLE
188-
select ARCH_HAS_CUSTOM_CPU_ATOMIC_IDLE
189-
select DYNAMIC_INTERRUPTS
190-
select CACHE_MANAGEMENT
191185
select TICKLESS_CAPABLE
192186
help
193187
dspic architecture

arch/dspic/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ if(CONFIG_BIG_ENDIAN)
33
else()
44
set_property(GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT elf32-little)
55
endif()
6-
zephyr_include_directories(${XCDSC_TOOLCHAIN_PATH}/support/generic/h/)
6+
zephyr_include_directories(${DFP_ROOT}/support/generic/h/)
77
zephyr_include_directories(include)
88
add_subdirectory(core)

arch/dspic/Kconfig

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
1+
# Copyright (c) 2025, Microchip Technology Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
14
menu "DSPIC Options"
25
depends on DSPIC
36

47
config ARCH
58
string
69
default "dspic"
710

11+
config GEN_ISR_TABLES
12+
default y
13+
14+
config DYNAMIC_INTERRUPTS
15+
default n
16+
817
config NUM_IRQS
9-
default 287
18+
default 279
1019

1120
config GEN_IRQ_START_VECTOR
12-
default 8
21+
default 0
22+
23+
config DSPIC33_IRQ_OFFLOAD_IRQ
24+
int "IRQ number for irq_offload()"
25+
default 34
26+
help
27+
Select the interrupt number used by irq_offload().
28+
This must be a valid, software-triggerable interrupt on the dsPIC33.
1329

1430
endmenu

arch/dspic/core/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ zephyr_library_sources(
44
cpu_idle.c
55
fatal.c
66
irq_manage.c
7-
isr_wrapper.c
7+
isr_wrapper.S
88
prep_c.c
99
thread.c
1010
swap.c
@@ -13,6 +13,7 @@ zephyr_library_sources(
1313
init.S
1414
vector_table.S
1515
reset1.S
16+
irq_offload.c
1617
)
1718

18-
zephyr_linker_sources(ROM_START SORT_KEY 0x04 vector_table.ld)
19+
zephyr_linker_sources(ROM_START SORT_KEY 0x00 vector_table.ld)

arch/dspic/core/cpu_idle.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,24 @@
66
#include <zephyr/irq.h>
77
#include <zephyr/tracing/tracing.h>
88
#include <zephyr/arch/cpu.h>
9+
#include <xc.h>
910

1011
#ifndef CONFIG_ARCH_HAS_CUSTOM_CPU_IDLE
1112
void arch_cpu_idle(void)
1213
{
14+
__builtin_disable_interrupts();
15+
Idle();
16+
__builtin_enable_interrupts();
1317
}
1418
#endif
1519

1620
#ifndef CONFIG_ARCH_HAS_CUSTOM_CPU_ATOMIC_IDLE
1721
void arch_cpu_atomic_idle(unsigned int key)
1822
{
19-
(void)key;
23+
__builtin_disable_interrupts();
24+
Idle();
25+
arch_irq_unlock(key);
26+
__builtin_enable_interrupts();
2027
}
2128
#endif
2229

arch/dspic/core/fatal.c

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,23 @@
44
*/
55

66
#include <zephyr/kernel.h>
7+
#include <zephyr/logging/log.h>
8+
9+
#ifndef _ASMLANGUAGE
10+
#include <xc.h>
11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
15+
LOG_MODULE_REGISTER(dspic, 4);
716

817
volatile uint32_t reason, address;
918

10-
#define EXCEPTION_HANDLER __attribute__((interrupt, no_auto_psv, keep))
19+
#define EXCEPTION_HANDLER __attribute__((interrupt, no_auto_psv, weak, naked))
20+
#define BUS_ERROR_MASK 0xF
21+
#define MATH_ERROR_MASK 0x1F
22+
#define GENERAL_TRAP_MASK 0x8000000Fu
1123

12-
void EXCEPTION_HANDLER _ReservedTrap7(void);
1324
void __attribute__((weak)) TRAPS_halt_on_error(void);
1425
void EXCEPTION_HANDLER _BusErrorTrap(void);
1526
void EXCEPTION_HANDLER _AddressErrorTrap(void);
@@ -20,62 +31,93 @@ void EXCEPTION_HANDLER _GeneralTrap(void);
2031
void EXCEPTION_HANDLER _ReservedTrap0(void);
2132
void EXCEPTION_HANDLER _ReservedTrap7(void);
2233

34+
void EXCEPTION_HANDLER _ReservedTrap0(void)
35+
{
36+
}
37+
void EXCEPTION_HANDLER _ReservedTrap7(void)
38+
{
39+
}
40+
2341
void __attribute__((weak)) TRAPS_halt_on_error(void)
2442
{
43+
/* stay here forever */
44+
while (1) {
45+
}
2546
}
2647

2748
/** Bus error.**/
2849
void EXCEPTION_HANDLER _BusErrorTrap(void)
2950
{
30-
__asm__("nop");
31-
__asm__("retfie");
51+
/* Identify bus error via INTCON3, fetch trap address from
52+
* PCTRAP, and reset error flags
53+
*/
54+
reason = INTCON3 & BUS_ERROR_MASK;
55+
address = PCTRAP;
56+
LOG_ERR("ERROR !!! Exception reason = %d, address = 0x%x\n", reason, address);
57+
INTCON3 &= ~(BUS_ERROR_MASK);
58+
PCTRAP = 0;
59+
TRAPS_halt_on_error();
3260
}
3361

3462
/** Address error.**/
3563
void EXCEPTION_HANDLER _AddressErrorTrap(void)
3664
{
37-
__asm__("nop");
38-
__asm__("retfie");
65+
/* fetch trap address from PCTRAP
66+
* and reset error flags
67+
*/
68+
address = PCTRAP;
69+
LOG_ERR("ERROR !!! Exception reason = %s, address = 0x%x\n", "Address Error", address);
70+
INTCON1bits.ADDRERR = 0;
71+
PCTRAP = 0;
72+
TRAPS_halt_on_error();
3973
}
4074

4175
/** Illegal instruction.**/
4276
void EXCEPTION_HANDLER _IllegalInstructionTrap(void)
4377
{
44-
__asm__("nop");
45-
__asm__("retfie");
78+
address = PCTRAP;
79+
LOG_ERR("ERROR !!! Exception reason = %s, address = 0x%x\n", "Illegal Instruction",
80+
address);
81+
INTCON1bits.BADOPERR = 0;
82+
PCTRAP = 0;
83+
TRAPS_halt_on_error();
4684
}
4785

4886
/** Math error.**/
4987
void EXCEPTION_HANDLER _MathErrorTrap(void)
5088
{
51-
__asm__("nop");
52-
__asm__("retfie");
89+
/* Identify math error via INTCON4, fetch trap address from
90+
* PCTRAP, and reset error flags
91+
*/
92+
reason = INTCON4 & MATH_ERROR_MASK;
93+
address = PCTRAP;
94+
LOG_ERR("ERROR !!! Exception reason = %d, address = 0x%x\n", reason, address);
95+
INTCON4 &= ~(MATH_ERROR_MASK);
96+
PCTRAP = 0;
97+
TRAPS_halt_on_error();
5398
}
5499

55100
/** Stack error.**/
56101
void EXCEPTION_HANDLER _StackErrorTrap(void)
57102
{
58-
__asm__("nop");
59-
__asm__("retfie");
103+
INTCON1bits.STKERR = 0;
104+
PCTRAP = 0;
105+
TRAPS_halt_on_error();
60106
}
61107

62108
/** Generic error.**/
63109
void EXCEPTION_HANDLER _GeneralTrap(void)
64110
{
65-
__asm__("nop");
66-
__asm__("retfie");
111+
reason = INTCON5 & GENERAL_TRAP_MASK;
112+
address = PCTRAP;
113+
LOG_ERR("ERROR !!! Exception reason = %d, address = 0x%x\n", reason, address);
114+
INTCON5 &= ~(GENERAL_TRAP_MASK);
115+
PCTRAP = 0;
116+
TRAPS_halt_on_error();
67117
}
68118

69-
/** Reserved Trap0.**/
70-
void EXCEPTION_HANDLER _ReservedTrap0(void)
71-
{
72-
__asm__("nop");
73-
__asm__("retfie");
119+
#ifdef __cplusplus
74120
}
121+
#endif
75122

76-
/** Reserved Trap7.**/
77-
void EXCEPTION_HANDLER _ReservedTrap7(void)
78-
{
79-
__asm__("nop");
80-
__asm__("retfie");
81-
}
123+
#endif /* _ASMLANGUAGE */

arch/dspic/core/init.S

Lines changed: 5 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
.section .init,code
27
.global __custom_data_init
38
.global __custom_data_init_extended

arch/dspic/core/irq_manage.c

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,85 @@
66
#include <zephyr/kernel.h>
77
#include <kswap.h>
88

9+
#ifndef _ASMLANGUAGE
10+
#include <xc.h>
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif
15+
916
void z_irq_spurious(const void *unused)
1017
{
11-
(void)unused;
18+
ARG_UNUSED(unused);
19+
while (1) {
20+
}
1221
}
1322

1423
void arch_irq_enable(unsigned int irq)
1524
{
16-
(void)irq;
25+
volatile uint32_t *int_enable_reg[] = {&IEC0, &IEC1, &IEC2, &IEC3, &IEC4,
26+
&IEC5, &IEC6, &IEC7, &IEC8};
27+
28+
unsigned int reg_index = irq / (sizeof(uint32_t) << 3);
29+
unsigned int bit_pos = irq % (sizeof(uint32_t) << 3);
30+
31+
/* Enable the interrupt by setting it's bit in interrupt enable register*/
32+
*int_enable_reg[reg_index] |= (uint32_t)(1u << bit_pos);
1733
}
1834

1935
int arch_irq_is_enabled(unsigned int irq)
2036
{
21-
(void)irq;
22-
return 0;
37+
volatile uint32_t *int_enable_reg[] = {&IEC0, &IEC1, &IEC2, &IEC3, &IEC4,
38+
&IEC5, &IEC6, &IEC7, &IEC8};
39+
40+
unsigned int reg_index = irq / (sizeof(uint32_t) << 3);
41+
unsigned int bit_pos = irq % (sizeof(uint32_t) << 3);
42+
43+
return ((*int_enable_reg[reg_index] >> bit_pos) & 0x1u);
2344
}
2445

2546
void arch_irq_disable(unsigned int irq)
2647
{
27-
(void)irq;
48+
volatile uint32_t *int_enable_reg[] = {&IEC0, &IEC1, &IEC2, &IEC3, &IEC4,
49+
&IEC5, &IEC6, &IEC7, &IEC8};
50+
51+
unsigned int reg_index = irq / (sizeof(uint32_t) << 3);
52+
unsigned int bit_pos = irq % (sizeof(uint32_t) << 3);
53+
54+
/* Disable the interrupt by clearing it's bit in interrupt enable register*/
55+
*int_enable_reg[reg_index] &= (uint32_t)(~(1u << bit_pos));
56+
}
57+
58+
bool arch_dspic_irq_isset(unsigned int irq)
59+
{
60+
volatile uint32_t *int_ifs_reg[] = {&IFS0, &IFS1, &IFS2, &IFS3, &IFS4,
61+
&IFS5, &IFS6, &IFS7, &IFS8};
62+
volatile int ret_ifs = false;
63+
unsigned int reg_index = irq / (sizeof(uint32_t) << 3);
64+
unsigned int bit_pos = irq % (sizeof(uint32_t) << 3);
65+
66+
if ((bool)(void *)(*int_ifs_reg[reg_index] & (uint32_t)(1U << bit_pos))) {
67+
ret_ifs = true;
68+
}
69+
return ret_ifs;
2870
}
71+
72+
73+
74+
void z_dspic_enter_irq(int irq)
75+
{
76+
volatile uint32_t *int_ifs_reg[] = {&IFS0, &IFS1, &IFS2, &IFS3, &IFS4,
77+
&IFS5, &IFS6, &IFS7, &IFS8};
78+
79+
unsigned int reg_index = (unsigned int)irq / (sizeof(uint32_t) << 3);
80+
unsigned int bit_pos = (unsigned int)irq % (sizeof(uint32_t) << 3);
81+
82+
/* Enable the interrupt by setting it's bit in interrupt enable register*/
83+
*int_ifs_reg[reg_index] |= (uint32_t)(1u << bit_pos);
84+
}
85+
86+
#ifdef __cplusplus
87+
}
88+
#endif
89+
90+
#endif /* _ASMLANGUAGE */

arch/dspic/core/irq_offload.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2025, Microchip Technology Inc.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <zephyr/kernel.h>
7+
#include <zephyr/kernel_structs.h>
8+
#include <kernel_internal.h>
9+
#include <zephyr/irq.h>
10+
#include <zephyr/irq_offload.h>
11+
#include <xc.h>
12+
13+
static irq_offload_routine_t _offload_routine;
14+
static const void *offload_param;
15+
16+
void z_irq_do_offload(void)
17+
{
18+
irq_offload_routine_t tmp;
19+
20+
if (_offload_routine != NULL) {
21+
22+
tmp = _offload_routine;
23+
_offload_routine = NULL;
24+
25+
tmp((const void *)offload_param);
26+
}
27+
}
28+
29+
void handler(void)
30+
{
31+
z_irq_do_offload();
32+
}
33+
34+
void arch_irq_offload_init(void)
35+
{
36+
IRQ_CONNECT(CONFIG_DSPIC33_IRQ_OFFLOAD_IRQ, 1, handler, NULL, 0);
37+
}
38+
39+
void arch_irq_offload(irq_offload_routine_t routine, const void *parameter)
40+
{
41+
uint32_t key = irq_lock();
42+
43+
_offload_routine = routine;
44+
offload_param = parameter;
45+
irq_enable(CONFIG_DSPIC33_IRQ_OFFLOAD_IRQ);
46+
z_dspic_enter_irq(CONFIG_DSPIC33_IRQ_OFFLOAD_IRQ);
47+
irq_unlock(key);
48+
}

0 commit comments

Comments
 (0)