From 33ceffd1e7a850fd16aa422a5e7de30b2b6b39d4 Mon Sep 17 00:00:00 2001 From: agarratt Date: Mon, 24 Jun 2024 14:35:02 +0200 Subject: [PATCH] Suggested context switching for dsPIC33CK. Now compiles and links. Untested on HW. --- portable/MPLAB/PIC24_dsPIC/port.c | 42 ++++++++++- portable/MPLAB/PIC24_dsPIC/portasm_dsPIC.S | 81 ++++++++++++++++++++++ portable/MPLAB/PIC24_dsPIC/portmacro.h | 2 +- 3 files changed, 123 insertions(+), 2 deletions(-) diff --git a/portable/MPLAB/PIC24_dsPIC/port.c b/portable/MPLAB/PIC24_dsPIC/port.c index 0299ec0ee..2eab34452 100644 --- a/portable/MPLAB/PIC24_dsPIC/port.c +++ b/portable/MPLAB/PIC24_dsPIC/port.c @@ -145,6 +145,41 @@ UBaseType_t uxCriticalNesting = 0xef; #endif /* defined( __dsPIC30F__ ) || defined( __dsPIC33F__ ) */ +#if defined( __dsPIC33C__ ) + + #define portRESTORE_CONTEXT() \ + asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \ + "MOV [W0], W15 \n" \ + "POP W0 \n" /* Restore the critical nesting counter for the task. */ \ + "MOV W0, _uxCriticalNesting \n" \ + "POP DSWPAG \n" /* See https://forums.freertos.org/t/i-have-a-problem-in-using-freertosv10-1-1-the-mcu-is-iteratively-rebooting-and-not-executing-task-function/7551 */ \ + "POP DSRPAG \n" \ + "POP CORCON \n" \ + "POP DOENDH \n" \ + "POP DOENDL \n" \ + "POP DOSTARTH \n" \ + "POP DOSTARTL \n" \ + "POP DCOUNT \n" \ + "POP ACCBU \n" \ + "POP ACCBH \n" \ + "POP ACCBL \n" \ + "POP ACCAU \n" \ + "POP ACCAH \n" \ + "POP ACCAL \n" \ + "POP TBLPAG \n" \ + "POP RCOUNT \n" /* Restore the registers from the stack. */ \ + "POP W14 \n" \ + "POP.D W12 \n" \ + "POP.D W10 \n" \ + "POP.D W8 \n" \ + "POP.D W6 \n" \ + "POP.D W4 \n" \ + "POP.D W2 \n" \ + "POP.D W0 \n" \ + "POP SR " ); + +#endif /* defined( __dsPIC33C__ ) */ + #ifndef portRESTORE_CONTEXT #error Unrecognised device selected @@ -185,7 +220,7 @@ const StackType_t xInitialStack[] = 0xabac, /* TBLPAG */ /* dsPIC specific registers. */ - #if defined( __dsPIC30F__ ) || defined( __dsPIC33F__ ) + #if defined( __dsPIC30F__ ) || defined( __dsPIC33F__ ) || defined( __dsPIC33C__ ) 0x0202, /* ACCAL */ 0x0303, /* ACCAH */ 0x0404, /* ACCAU */ @@ -295,8 +330,13 @@ const uint32_t ulCompareMatch = ( ( configCPU_CLOCK_HZ / portTIMER_PRESCALE ) / IEC0bits.T1IE = 1; /* Setup the prescale value. */ +#if defined( __dsPIC33C__ ) + /* Microchip's header file p33CK128MP505.h does not have separate TCKPSx */ + T1CONbits.TCKPS = 1; /* Value of 1 simply copied over from old code */ +#else T1CONbits.TCKPS0 = 1; T1CONbits.TCKPS1 = 0; +#endif /* if defined( __dsPIC33C__ ) */ /* Start the timer. */ T1CONbits.TON = 1; diff --git a/portable/MPLAB/PIC24_dsPIC/portasm_dsPIC.S b/portable/MPLAB/PIC24_dsPIC/portasm_dsPIC.S index ec1f070a4..5f6db6275 100644 --- a/portable/MPLAB/PIC24_dsPIC/portasm_dsPIC.S +++ b/portable/MPLAB/PIC24_dsPIC/portasm_dsPIC.S @@ -104,3 +104,84 @@ _vPortYield: .end #endif /* defined( __dsPIC30F__ ) || defined( __dsPIC33F__ ) */ + +#if defined( __dsPIC33C__ ) + + .global _vPortYield + .extern _vTaskSwitchContext + .extern uxCriticalNesting + +_vPortYield: + + PUSH SR /* Save the SR used by the task.... */ + PUSH W0 /* ....then disable interrupts. */ + MOV #32, W0 + MOV W0, SR + PUSH W1 /* Save registers to the stack. */ + PUSH.D W2 + PUSH.D W4 + PUSH.D W6 + PUSH.D W8 + PUSH.D W10 + PUSH.D W12 + PUSH W14 + PUSH RCOUNT + PUSH TBLPAG + PUSH ACCAL + PUSH ACCAH + PUSH ACCAU + PUSH ACCBL + PUSH ACCBH + PUSH ACCBU + PUSH DCOUNT + PUSH DOSTARTL + PUSH DOSTARTH + PUSH DOENDL + PUSH DOENDH + + + PUSH CORCON + PUSH DSRPAG /* See https://forums.freertos.org/t/i-have-a-problem-in-using-freertosv10-1-1-the-mcu-is-iteratively-rebooting-and-not-executing-task-function/7551 */ + PUSH DSWPAG + MOV _uxCriticalNesting, W0 /* Save the critical nesting counter for the task. */ + PUSH W0 + MOV _pxCurrentTCB, W0 /* Save the new top of stack into the TCB. */ + MOV W15, [W0] + + call _vTaskSwitchContext + + MOV _pxCurrentTCB, W0 /* Restore the stack pointer for the task. */ + MOV [W0], W15 + POP W0 /* Restore the critical nesting counter for the task. */ + MOV W0, _uxCriticalNesting + POP DSWPAG + POP DSRPAG /* See https://forums.freertos.org/t/i-have-a-problem-in-using-freertosv10-1-1-the-mcu-is-iteratively-rebooting-and-not-executing-task-function/7551 */ + POP CORCON + POP DOENDH + POP DOENDL + POP DOSTARTH + POP DOSTARTL + POP DCOUNT + POP ACCBU + POP ACCBH + POP ACCBL + POP ACCAU + POP ACCAH + POP ACCAL + POP TBLPAG + POP RCOUNT /* Restore the registers from the stack. */ + POP W14 + POP.D W12 + POP.D W10 + POP.D W8 + POP.D W6 + POP.D W4 + POP.D W2 + POP.D W0 + POP SR + + return + + .end + +#endif /* defined( __dsPIC33C__ ) */ diff --git a/portable/MPLAB/PIC24_dsPIC/portmacro.h b/portable/MPLAB/PIC24_dsPIC/portmacro.h index 9e1e3edf7..53e9ba17b 100644 --- a/portable/MPLAB/PIC24_dsPIC/portmacro.h +++ b/portable/MPLAB/PIC24_dsPIC/portmacro.h @@ -54,8 +54,8 @@ #define portSTACK_TYPE uint16_t #define portBASE_TYPE short #define portPOINTER_SIZE_TYPE size_t -/* Microchip xc16 compilers already define SIZE_MAX in stdint.h */ #ifndef SIZE_MAX +/* Microchip xc16 and xc-dsc compilers already define SIZE_MAX in stdint.h */ #define SIZE_MAX ( ( size_t ) -1 ) #endif