Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions arch/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,28 @@ config RX
help
Renesas RX architecture

config DSPIC
bool
select ARCH_IS_SET
select STACK_GROWS_UP
select ARCH_HAS_CUSTOM_SWAP_TO_MAIN
select LITTLE_ENDIAN
select ARCH_HAS_THREAD_LOCAL_STORAGE
select TOOLCHAIN_SUPPORTS_THREAD_LOCAL_STORAGE
select THREAD_LOCAL_STORAGE
select INIT_STACKS
select STACK_SENTINAL
select ATOMIC_OPERATIONS_C
select ARCH_HAS_VECTOR_TABLE_RELOCATION
select CPU_HAS_ICACHE
select ARCH_HAS_CUSTOM_CPU_IDLE
select ARCH_HAS_CUSTOM_CPU_ATOMIC_IDLE
select DYNAMIC_INTERRUPTS
select CACHE_MANAGEMENT
select TICKLESS_CAPABLE
help
dspic architecture

config ARCH_IS_SET
bool
help
Expand Down Expand Up @@ -376,6 +398,11 @@ config STACK_GROWS_UP
Select this option if the architecture has upward growing thread
stacks. This is not common.

config STACK_SENTINAL
bool
help
It is a debugging and safety feature at the boundaries of the stack.

config NO_UNUSED_STACK_INSPECTION
bool
help
Expand Down
3 changes: 3 additions & 0 deletions arch/archs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ archs:
- name: rx
path: rx
full_name: Renesas RX
- name: dspic
path: dspic
full_name: Microchip dsPIC
8 changes: 8 additions & 0 deletions arch/dspic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if(CONFIG_BIG_ENDIAN)
set_property(GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT elf32-big)
else()
set_property(GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT elf32-little)
endif()
zephyr_include_directories(${XCDSC_TOOLCHAIN_PATH}/support/generic/h/)
zephyr_include_directories(include)
add_subdirectory(core)
17 changes: 17 additions & 0 deletions arch/dspic/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2025, Microchip Technology Inc.
Copy link
Collaborator

@AzharMCHP AzharMCHP Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comma is not required in the copyright header.
It should be as follows:

# Copyright (c) 2025 Microchip Technology Inc.
# SPDX-License-Identifier: Apache-2.0

This can be updated everywhere

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will start changing all files in coming commits. Please expect this in coming release

# SPDX-License-Identifier: Apache-2.0

menu "DSPIC Options"
depends on DSPIC

config ARCH
string
default "dspic"

config NUM_IRQS
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can vary from SoC to SoC, in that case how do we handle this?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IRQ shall only be part of SoC definitions

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, will analyse the changes needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is Kconfig file and we can have different values for this in respective SOC definictions

default 287

config GEN_IRQ_START_VECTOR
default 8

endmenu
18 changes: 18 additions & 0 deletions arch/dspic/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
zephyr_library()

zephyr_library_sources(
cpu_idle.c
fatal.c
irq_manage.c
isr_wrapper.c
prep_c.c
thread.c
swap.c
tls.c
reset0.S
init.S
vector_table.S
reset1.S
)

zephyr_linker_sources(ROM_START SORT_KEY 0x04 vector_table.ld)
27 changes: 27 additions & 0 deletions arch/dspic/core/cpu_idle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) 2025, Microchip Technology Inc.
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/irq.h>
#include <zephyr/tracing/tracing.h>
#include <zephyr/arch/cpu.h>

#ifndef CONFIG_ARCH_HAS_CUSTOM_CPU_IDLE
void arch_cpu_idle(void)
{
}
#endif

#ifndef CONFIG_ARCH_HAS_CUSTOM_CPU_ATOMIC_IDLE
void arch_cpu_atomic_idle(unsigned int key)
{
(void)key;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldnt we use pwrsav instruction here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harishagari
We will be adding the logic soon
Right now we just added the stubs

}
#endif

FUNC_NORETURN void arch_system_halt(unsigned int reason)
{
(void)reason;
CODE_UNREACHABLE;
}
81 changes: 81 additions & 0 deletions arch/dspic/core/fatal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2025, Microchip Technology Inc.
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/kernel.h>

volatile uint32_t reason, address;

#define EXCEPTION_HANDLER __attribute__((interrupt, no_auto_psv, keep))

void EXCEPTION_HANDLER _ReservedTrap7(void);
void __attribute__((weak)) TRAPS_halt_on_error(void);

Check warning on line 13 in arch/dspic/core/fatal.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

FUNCTION_ARGUMENTS

arch/dspic/core/fatal.c:13 function definition argument 'void' should also have an identifier name
void EXCEPTION_HANDLER _BusErrorTrap(void);
void EXCEPTION_HANDLER _AddressErrorTrap(void);
void EXCEPTION_HANDLER _IllegalInstructionTrap(void);
void EXCEPTION_HANDLER _MathErrorTrap(void);
void EXCEPTION_HANDLER _StackErrorTrap(void);
void EXCEPTION_HANDLER _GeneralTrap(void);
void EXCEPTION_HANDLER _ReservedTrap0(void);
void EXCEPTION_HANDLER _ReservedTrap7(void);

void __attribute__((weak)) TRAPS_halt_on_error(void)
{
}

/** Bus error.**/
void EXCEPTION_HANDLER _BusErrorTrap(void)
{
__asm__("nop");
__asm__("retfie");
}

/** Address error.**/
void EXCEPTION_HANDLER _AddressErrorTrap(void)
{
__asm__("nop");
__asm__("retfie");
}

/** Illegal instruction.**/
void EXCEPTION_HANDLER _IllegalInstructionTrap(void)
{
__asm__("nop");
__asm__("retfie");
}

/** Math error.**/
void EXCEPTION_HANDLER _MathErrorTrap(void)
{
__asm__("nop");
__asm__("retfie");
}

/** Stack error.**/
void EXCEPTION_HANDLER _StackErrorTrap(void)
{
__asm__("nop");
__asm__("retfie");
}

/** Generic error.**/
void EXCEPTION_HANDLER _GeneralTrap(void)
{
__asm__("nop");
__asm__("retfie");
}

/** Reserved Trap0.**/
void EXCEPTION_HANDLER _ReservedTrap0(void)
{
__asm__("nop");
__asm__("retfie");
}

/** Reserved Trap7.**/
void EXCEPTION_HANDLER _ReservedTrap7(void)
{
__asm__("nop");
__asm__("retfie");
}
62 changes: 62 additions & 0 deletions arch/dspic/core/init.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2025, Microchip Technology Inc.
* SPDX-License-Identifier: Apache-2.0
*/

.section .init,code
.global __custom_data_init
.global __custom_data_init_extended

.equ __custom_data_init, __custom_data_init_extended
__custom_data_init_extended:

.equiv FMT_CLEAR,0
.equiv FMT_COPY2,1
.equiv FMT_COPY3,2
.equiv FMT_CALL, 3
.equiv FMT_DUO_COPY3,0x1F

#define DINIT w0
#define TLSOFFSET w1
#define TBLOFFSET w9
#define DSTOFFSET w10
#define LEN w11
#define FORMAT w12

mov.l w0, TBLOFFSET
bra 4f

1:
add.l DSTOFFSET,TLSOFFSET,DSTOFFSET
mov.l [TBLOFFSET++], LEN
mov.l [TBLOFFSET++], FORMAT

cp.b FORMAT, #FMT_CALL
bra nz, 2f
call DSTOFFSET
bra 4f

2:
cp.b FORMAT, #FMT_CLEAR
bra nz, 2f
9:
sub.l LEN, #1, LEN
repeat LEN
clr.b [DSTOFFSET++]
bra 4f

2:
cp.b FORMAT, #FMT_COPY2
bra z, 3f

3:
sub.l LEN, #1, LEN
repeat LEN
mov.b [TBLOFFSET++], [DSTOFFSET++]
add.l TBLOFFSET, #3, TBLOFFSET
and1.l TBLOFFSET, #0x7C, TBLOFFSET

4:
sub.l [TBLOFFSET++], #0, DSTOFFSET
bra nz, 1b
return
28 changes: 28 additions & 0 deletions arch/dspic/core/irq_manage.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2025, Microchip Technology Inc.
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/kernel.h>
#include <kswap.h>

void z_irq_spurious(const void *unused)
{
(void)unused;
}

void arch_irq_enable(unsigned int irq)
{
(void)irq;
}

int arch_irq_is_enabled(unsigned int irq)
{
(void)irq;
return 0;
}

void arch_irq_disable(unsigned int irq)
{
(void)irq;
}
22 changes: 22 additions & 0 deletions arch/dspic/core/isr_wrapper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2025, Microchip Technology Inc.
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/kernel.h>
#include <zephyr/irq.h>
#include <zephyr/pm/pm.h>
#include <zephyr/sw_isr_table.h>
#include <kernel_arch_func.h>

/* dsPIC33A interrtup exit routine. Will check if a context
* switch is required. If so, z_dspic_do_swap() will be called
* to affect the context switch
*/
void __attribute__((naked)) z_dspic_exc_exit(void)
{
}

void __attribute__((interrupt, naked)) _COMMONInterrupt(void)
{
}
10 changes: 10 additions & 0 deletions arch/dspic/core/offsets/offsets.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c) 2025, Microchip Technology Inc.
* SPDX-License-Identifier: Apache-2.0
*/

#include <gen_offset.h>
#include <zephyr/kernel.h>
#include <kernel_offsets.h>

GEN_ABS_SYM_END
25 changes: 25 additions & 0 deletions arch/dspic/core/prep_c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2025, Microchip Technology Inc.
* SPDX-License-Identifier: Apache-2.0
*/

#include <kernel_internal.h>
#include <zephyr/irq.h>
#include <zephyr/platform/hooks.h>
#include <zephyr/arch/cache.h>

void z_prep_c(void);
/**
* @brief Prepare to and run C code
*
* This routine prepares for the execution of and runs C code.
*/

void z_prep_c(void)
{
#if defined(CONFIG_SOC_PREP_HOOK)
soc_prep_hook();
#endif
z_cstart();
CODE_UNREACHABLE;
}
43 changes: 43 additions & 0 deletions arch/dspic/core/reset0.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2025, Microchip Technology Inc.
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/toolchain.h>
#include <zephyr/linker/sections.h>
#include <zephyr/arch/cpu.h>
#include <zephyr/offsets.h>

;;This reset version support data initialization
;;Refer reset1.S for without data initialization

.section .text, code ; Use .text to avoid section attribute errors
.weak __start,__user_init, __has_user_init, ___tls_crt_init
.extern __custom_data_init_extended
.extern __user_init

__start:
;; Initialize stack pointer and limit
mov.l #__SP_init, w15 ; Stack pointer (W15)
mov.l #__SPLIM_init, w14 ; Stack limit (W14)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W14 is stack frame pointer , SPLIM is stack pointer limit register. Do we want to initialize w14 or SPLIM here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we needed to write back the value from w14 to SPLIM. This was a mistake, will take care with next changes.


;; Data initialization
mov.l #__dinit_tbloffset, w0
cp0.l w0
bra eq, 1f
clr w1
rcall __custom_data_init_extended
1:
;; Thread-local storage init (if available)
mov.l #___tls_crt_init, w0
cp0.l w0
bra eq, 1f
rcall ___tls_crt_init
1:
mov.l #__user_init,w0
cp0.l w0 ; user init functions
bra eq,1f
call __user_init
1:
;; Call the main application
call _z_prep_c
Loading
Loading