// 3 w0rd timer (1 word secs + 2 word fraction )
// $Id: cdefs.h,v 1.1 2014/12/16 00:23:07 daddio Exp $
// published under terms of gnu open source license, (c) copyright G.P. Eckersley

// sufficient in line assembly definitions to allow context switching/multi-tasking on STM32F103
// written in gcc "C"

#define setstack(x) __asm__("mov r3, %0\n\t"  "msr psp,r3\n\t" : :  "r" (x))    
// set stack to c

#define svccode(code) __asm__("SVC %0" : : "I"(code) : "lr")
// supervisor call - sets privilege bit in control reg.

#define svcdata4(strt) __asm__(" .word " strt "\n\t" : : "i" (strt)   : )       
// svc parameter 


// decode svc, skip up to 32 * 2 bytes of following parameter data, moves address of following parameters to "params"
// when upper bit of svc code is clear , then
// skips 2*(lower 3 bits of svc code); used for task control
#define getsvccp(code,params) __asm__(" mrs R3,psp\n\t \
        ldr R2,[R3,#6*4]\n\t \
        mov %1, R2\n\t \
        ldr R1,[R2,#-2]\n\t \
        and R1,#0xff\n\t \
        mov %0,R1\n\t \
        tst R1,#0x80\n\t \
        bne 1f\n\t \
        and R1,#0x7\n\t \
        add R2,R1\n\t \
        add R2,R1\n\t \
        str R2,[R3,#6*4]\n\t \
        1:\n\t ":  "=r" (code) , "=r" (params) : )

#define getpsp(h3)  __asm__("MRS R3, psp\n\t mov %[result],R3\n\t" : [result] "=r" (h3)  ); 
// current program sp to h3
 
#define setcontrol(val) __asm__("mov R3,#%c0\n\t   msr control,R3\n\t" : : "I" (val) );     
 // set control reg

#define startp0(procst) svccode(0x42);svcdata4(procst);

// push remaining registers to psp (thread stack)
#define svectxt(h3) __asm__("MRS R3, psp\n\t \
                         stmdb R3!, {r4-r11}\n\t \
                         mov %[result],R3\n\t" : [result] "=r" (h3)  );

// pull remaining registers and 
// return to unpriviledged thread 
#define getctxt(h3) __asm__("MOV r3, %0\n\t \
                         ldmia R3!, {r4-r11}\n\t \
                         msr psp,R3\n\t \
                         mvn R3,#0xff-0xfd\n\t \
                         bx R3\n\t " : :  "r" (h3)  );

// mov #0xFFFFFFFD to PC is return, see pm0056 page 41


// generate svc (code) with no subsequent parameters and then return
// usage eg : Static Void startp1(p) int*p; {svcret(0x40);}
//   In svc handler R0 will contain p if startp is "naked" as set below
#define svcret(code) __asm__("SVC %0 \n\t \
                               bx lr\n\t "    : : "I"(code) : )

//    fa,fd,ea,ed {full/empty} {ascending/descending}
// or ia,ib,da,db {inc/dec} { b4/after };


#define svregs() __asm__( "stmdb sp!, {lr}\n\t "  );                             
// push lr

#define getregs() __asm__("ldmia sp!, {lr}\n\t "  );                             
// pop lr


// exit routine if mask bits set. eg  testsvcret(0xE000E010,0x8080) exits taskswapper if in svc handler
// not necessary if context swapper has lower priority
#define testsvcret(reg,mask) __asm__(" \
                               ldr r3,[r3]\n\t \
                               and r3,r2\n\t \
                               cmp	r3, #0\n\t \
                               beq 1f\n\t \
                               bx lr\n\t \
                               1:\n\t" : : "r" (reg) , "r" (mask)  );

// increment/decrement memory location using only registers stacked on exception entry (r0-4,r12)
// gcc "loc+=inc" does this but unnecessarily reloads the pointer register (eg r3)! 
// this saves significant cycles in tight loops and interrupts
#define incloc(loc,inc) __asm__(" \
                              ldr r2,[r3]\n\t \
                              add r2,#%c1\n\t \
                              str r2,[r3]\n\t" : : "m" (loc), "i" (inc) : "r2"   );

// "timerupd" updates a 96 bit fractional timer every "Tick" intterupt. 
// E.G upper 32 bits Epoch seconds, lower 64 bits fraction
// (loc) points to a single ram  parameter, the lower 12 bytes are the timer,
// the upper 12 are the increment value
// if the tick rate is 62.5 kHz, then the upper 12 bytes are little
// endian 0x10c6f7a0b5ed
#define timerupd(loc) __asm__(" \
                              ldr r2,[r3,#(0*4)]\n\t \
                              ldr r1,[r3,#((0+3)*4)]\n\t \
                              adds r2,r1\n\t \
                              str r2,[r3,#(0*4)]\n\t \
                              ldr r2,[r3,#(1*4)]\n\t \
                              ldr r1,[r3,#((1+3)*4)]\n\t \
                              adcs r2,r1\n\t \
                              str r2,[r3,#(1*4)]\n\t \
                              ldr r2,[r3,#(2*4)]\n\t \
                              adc r2,#0\n\t \
                              str r2,[r3,#(2*4)]\n\t" : : "m" (loc) : "r2"   );

// used to tag following "C" routines as "naked"
#define defbare(name) void name(void)  __attribute__( ( naked ) ) ;              
#define defbarep(name) static void name(p)  __attribute__( ( naked ) ) ;         
//#define defbarep2(name) void name(p,p,p)  __attribute__( ( naked ) ) ;

// The following routines will be qualified as "attribute naked"
 defbare(SVC_Handler);   
 defbare(SysTick_Handler);
 defbarep(startp1);        // start process, param=r0

