Creating a microcontroller startup file

Mike via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Apr 9 17:05:28 PDT 2015


On Wednesday, 8 April 2015 at 15:25:20 UTC, Jens Bauer wrote:

>>> Question number 2: Is it possible to change the VectorFunc to 
>>> be a real function pointer, rather than a void* ?
>
>> immutable ISR[3] g_pfnVectors =
>> [
>>      cast(ISR)&_stack
>>    , &Reset_Handler
>>    , &NMI_Handler
>>    , &HardFault_Handler
>> ];
>
> In your example, you do not have the initial stack pointer.
> The above code gives me the following:
> src/test.d:24:13: error: reinterpreting cast from uint* to 
> void()* is not supported in CTFE
>     cast(ISR)&_stack
>              ^
>
> -That's the only reason I needed to change it to from 
> function() to void*.
> Can you successfully cast(ISR)&_stack ?

I don't know if that's a constraint of the language or a 
limitation of the current CTFE implementation, but either way, I 
never really liked it.  I know the C folks do this stuff all the 
time, but I think it's kinda janky.

I know of two potential alternatives:
1)  Do it in the linker script (my current method):

MEMORY
{
   CCRAM (rxw) : ORIGIN = 0x10000000, LENGTH = 64k
}

/* Falling stack starts at the end of the CCM */
_stackStart = ORIGIN(CCRAM) + LENGTH(CCRAM);

SECTIONS
{
     .text :
     {
         LONG(_stackStart);
         KEEP(YourISRVectorTable);
     }
}

2) Use a union (See pp. 10 here: 
http://www.state-machine.com/arm/QDK_ARM-Cortex_STM32-GNU.pdf)

C Code to be adapted to D:

typedef void (*ExceptionHandler)(void);
typedef union {
	ExceptionHandler handler;
	void            *pointer;
} VectorTableEntry;

__attribute__ ((section(".isr_vector")))
VectorTableEntry const g_pfnVectors[] = {
	{ .pointer = &__c_stack_top__           },
	{ .handler = &Reset_Handler             },
	{ .handler = &NMI_Handler               },
	... ect ...
};

Mike


More information about the Digitalmars-d-learn mailing list