Example #1
0
/*@brief: switch out old pcb (p_pcb_old), run the new pcb (gp_current_process)
 *@param: p_pcb_old, the old pcb that was in RUN
 *@return: RTX_OK upon success
 *         RTX_ERR upon failure
 *PRE:  p_pcb_old and gp_current_process are pointing to valid PCBs.
 *POST: if gp_current_process was NULL, then it gets set to pcbs[0].
 *      No other effect on other global variables.
 */
int process_switch(PCB *p_pcb_old) 
{
	PROC_STATE_E state;
	
	state = gp_current_process->m_state;

	if (state == NEW) {
		if (gp_current_process != p_pcb_old && p_pcb_old->m_state != NEW) {
			p_pcb_old->m_state = RDY;
			p_pcb_old->mp_sp = (U32 *) __get_MSP();
		}
		gp_current_process->m_state = RUN;
		__set_MSP((U32) gp_current_process->mp_sp);
		__rte();  // pop exception stack frame from the stack for a new processes
	} 
	
	/* The following will only execute if the if block above is FALSE */

	if (gp_current_process != p_pcb_old) {
		if (state == RDY){ 		
			p_pcb_old->m_state = RDY; 
			p_pcb_old->mp_sp = (U32 *) __get_MSP(); // save the old process's sp
			gp_current_process->m_state = RUN;
			__set_MSP((U32) gp_current_process->mp_sp); //switch to the new proc's stack    
		} else {
			gp_current_process = p_pcb_old; // revert back to the old proc on error
			return RTX_ERR;
		} 
	}
	return RTX_OK;
}
Example #2
0
int context_switch(PCB* p_pcb_old) {
	PROC_STATE_E state;
	
	state = gp_current_process->m_state;

	if (state == NEW) {
		if (gp_current_process != p_pcb_old && p_pcb_old->m_state != NEW) {
			if (p_pcb_old->m_state != BLOCKED_ON_RECEIVE &&
					p_pcb_old->m_state != BLOCKED_ON_MEMORY) {
				p_pcb_old->m_state = READY;
			}
			p_pcb_old->mp_sp = (U32*) __get_MSP();
		}
		gp_current_process->m_state = RUN;
		__set_MSP((U32) gp_current_process->mp_sp);
		__rte();  // pop exception stack frame from the stack for a new processes
	}
	
	if (gp_current_process != p_pcb_old) {
		if (state == READY){
			if (p_pcb_old->m_state != BLOCKED_ON_MEMORY && p_pcb_old->m_state != BLOCKED_ON_RECEIVE) {
				p_pcb_old->m_state = READY;
			}
			p_pcb_old->mp_sp = (U32*) __get_MSP(); // save the old process's sp
			gp_current_process->m_state = RUN;
			__set_MSP((U32) gp_current_process->mp_sp); //switch to the new proc's stack    
		} else {
			gp_current_process = p_pcb_old; // revert back to the old proc on error
			return RTX_ERR;
		} 
	}
	return RTX_OK;
}	
Example #3
0
void *_sbrk(int incr) {

    //extern char _ebss; // Defined by the linker
    static char *heap_end;
    char *prev_heap_end;

    if (heap_end == 0) {
        heap_end = &_ebss;
    }
    prev_heap_end = heap_end;

#if 0
char * stack = (char*) __get_MSP();
     if (heap_end + incr >  stack)
     {
 //        _write (STDERR_FILENO, "Heap and stack collision\n", 25);
 //        errno = ENOMEM;
         return  (void *) -1;
         //abort ();
     }
#endif

    heap_end += incr;
    return (void *) prev_heap_end;

}
Example #4
0
caddr_t _sbrk(int Size)
{
    // - Current heap end pointer
    static caddr_t hHeapEnd;

    // - Previous heap end pointer
    caddr_t hPrevHeapEnd;

    // - Init heap pointer
    if (hHeapEnd == 0)
	hHeapEnd = _sbrk_getHeapStartAddress();

    // - Get current stack pointer
    caddr_t hStackTop = (caddr_t)__get_MSP();

    // - Store current heap pointer
    hPrevHeapEnd = hHeapEnd;    

    // - Check is heap overlaps stack
    if (hHeapEnd + Size > hStackTop) {
	errno = ENOMEM;
	return (caddr_t)-1;
    }

    // - Add heap size
    hHeapEnd += Size;

    // - Return result
    return hPrevHeapEnd;
}
Example #5
0
static void tt_use_PSP(unsigned int irq_stack_addr)
{
	__set_PSP(__get_MSP());
	__set_MSP(irq_stack_addr);
	__set_CONTROL(2);
	__ISB();
}
Example #6
0
File: kos.c Project: puchinya/kos
void kos_init_regs(void)
{
	uint32_t msp = __get_MSP();
	__set_PSP(msp);
	__set_MSP((uint32_t)g_kos_isr_stk + g_kos_isr_stksz);
	__set_CONTROL(2);
}
Example #7
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/
       startup_stm32f429_439xx.s/startup_stm32f401xx.s) before to branch to 
       application main. To reconfigure the default setting of SystemInit() 
       function, refer to system_stm32f4xx.c file
     */      
     
  for (uwIndex = 0; uwIndex <1024 ; uwIndex++)
  {
     aTable[uwIndex] =uwIndex;
  }

  uwTabAddr = (uint32_t)aTable; /* should be 0x640xxxxx */

  /* Get main stack pointer value */
  MSPValue = __get_MSP(); /* should be 0x640xxxxx */

  while (1)
  {
  }
  
}
Example #8
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 168 MHz */
  SystemClock_Config();
  
  for (uwIndex = 0; uwIndex <1024; uwIndex++)
  {
    aTable[uwIndex] =uwIndex;
  }
  
  uwTabAddr = (uint32_t)aTable; /* should be 0x640xxxxx */
  
  /* Get main stack pointer value */
  MSPValue = __get_MSP(); /* should be 0x640xxxxx */

  /* Infinite loop */  
  while (1)
  {
  }
}
// ****************************************************************************
// Simple gcc-compatible C runtime initialization
//
// This works in conjunction with lpc81x.ld
// ****************************************************************************
void Reset_handler(void)
{
    unsigned int *source;
    unsigned int *destination;
    unsigned int *end;
    uint32_t temp_entropy;

    // Calculate the entropy random value from the RAM contents after reset
    // Only process RAM locations unitl we reach the stack.
    temp_entropy = 0x0817;  // Just an arbitrary initialization value
    source = &_ram;
    end = (unsigned int *)__get_MSP();
    while (source < end) {
        temp_entropy ^= *(source++);
    }

#ifndef NODEBUG
    // Place stack canaries into RAM, but only up to the current stack pointer
    destination = &_ram;
    end = (unsigned int *)__get_MSP();
    while (destination < end) {
        *(destination++) = 0xcafebabe;
    }
#endif

    // Copy initialization values from Flash to RAM
    source = &_data_load;
    destination = &_data;
    end = &_edata;
    while (destination < end) {
        *(destination++) = *(source++);
    }

    // Zero out uninitialized RAM
    destination = &_bss;
    end = &_ebss;
    while (destination < end) {
        *(destination++) = 0;
    }

    // Copy the calculated random value into the final destination RAM
    // (which we just erased above ...)
    entropy = temp_entropy;

    main();
    while (1);  // Hang if main() returns
}
Example #10
0
/* Dumps the contents of the stack to stdout */
void DebugDumpStack(void)
{
    unsigned int TopOfStack = *((unsigned int*)0);
    unsigned int BottomOfStack = __get_MSP();
    unsigned int StackLength = TopOfStack - BottomOfStack;
    
    DebugDumpMemory((void*)BottomOfStack, StackLength, 4);
}
Example #11
0
RFLPC_IRQ_HANDLER _default_exception_handler()
{
#ifdef RFLPC_IRQ_DEBUG_ENABLE
    printf("CFSR: %0x\r\n", SCB->CFSR);
    printf("HFSR: %0x\r\n", SCB->HFSR);
    printf("DFSR: %0x\r\n", SCB->DFSR);
    if (SCB->CFSR & (1UL << 7)) /* Memory Management Fault Address Register Valid */
       printf("Mem Managment Fault Adress: %p\r\n", SCB->MMFAR);
    if (SCB->CFSR & (1UL << 4)) /* MSTKERR */
       printf("Memory Fault caused by stacking for exception entry\r\n");
    if (SCB->CFSR & (1UL << (3))) /* MUNSTKERR */
       printf("Memory Fault caused by unstacking for return from exception\r\n");
    if (SCB->CFSR & (1UL << (1))) /* DACCVIOL */
       printf("Data Access Violation. PC value stacked point to the faulting instruction\r\n");
    if (SCB->CFSR & (1UL << (0))) /* IACCVIOL */
       printf("Instruction Access Violation. PC value stacked point to the faulting instruction\r\n");
    if (SCB->CFSR & (1UL << (8+7))) /* BFARVALID */
       printf("Bus fault with known address: %p\r\n", SCB->BFAR);
    if (SCB->CFSR & (1UL << (8+4))) /* STKERR */
       printf("Bus fault caused by stacking for exception entry\r\n");
    if (SCB->CFSR & (1UL << (8+3))) /* UNSTKERR */
       printf("Bus fault caused by unstacking for return from exception\r\n");
    if (SCB->CFSR & (1UL << (8+2))) /* IMPRECISERR */
       printf("Imprecise bus fault\r\n");
    if (SCB->CFSR & (1UL << (8+1))) /* PRECISERR */
       printf("Precise bus fault\r\n");
    if (SCB->CFSR & (1UL << (8+0))) /* IBUSERR */
       printf("Intruction bus error\r\n");

    if (SCB->CFSR & (1UL << (16+9))) /* DIVZERO */
       printf("Division by 0\r\n");
    if (SCB->CFSR & (1UL << (16+8))) /* UNALIGNED */
       printf("Unaligned access usage\r\n");
    if (SCB->CFSR & (1UL << (16+3))) /* NOCP */
       printf("Fault while accessing to a coprocessor\r\n");
    if (SCB->CFSR & (1UL << (16+2))) /* INVPC */
       printf("Invalid PC Load\r\n");
    if (SCB->CFSR & (1UL << (16+1))) /* INVSTATE */
       printf("The processor has attempted to execute an instruction that makes illegal use of the EPSR\r\n");
    if (SCB->CFSR & (1UL << (16+0))) /* UNDEFINSTR */
       printf("The processor has attempted to execute an undefined instruction\r\n");

    if (SCB->HFSR & (1UL << (30))) /* FORCED */
       printf("Forced hardware fault\r\n");
    if (SCB->HFSR & (1UL << (1))) /* FORCED */
       printf("Bus fault on vector table read\r\n");
    /* Stack frame when entering exception is:
     * R0-R3, R12 [0->4]
     * PC [5]
     * PSR [6]
     * LR [7]
     */
    printf("PC value at exception: %p\r\n", ((uint32_t*)__get_MSP())[5+4]); /* +4 is because GCC pushes r0,r4,r5 and lr on the stack */
    RFLPC_DUMP_STACK();
    /* stops the execution with a O--O <-> -OO- led pattern. */
    RFLPC_STOP(RFLPC_LED_1|RFLPC_LED_4, 2000000);
#endif
}
Example #12
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
    /* Configure the MPU attributes as Write Through */
    MPU_Config();

    /* Enable the CPU Cache */
    CPU_CACHE_Enable();

    /* STM32F7xx HAL library initialization:
         - Configure the Flash ART accelerator on ITCM interface
         - Systick timer is configured by default as source of time base, but user
           can eventually implement his proper time base source (a general purpose
           timer for example or other time source), keeping in mind that Time base
           duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
           handled in milliseconds basis.
         - Set NVIC Group Priority to 4
         - Low Level Initialization
       */
    HAL_Init();

    /* Configure LED1 and LED3 */
    BSP_LED_Init(LED1);
    BSP_LED_Init(LED3);

    /* Configure the system clock to 200 MHz */
    /* This function will be executed from SDRAM */
    SystemClock_Config();

    /*##-1- Fill the buffer in the SDRAM device ##########################################*/
    Fill_Buffer(aTable, 1024, 0);

    /*##-2- Read address of the buffer and stack pointer address ########################*/
    uwTabAddr = (uint32_t)aTable; /* should be above 0x60000000 */

    /* Get main stack pointer value */
    MSPValue = __get_MSP(); /* should be above 0x60000000 */

    /*##-3- Activate LEDs pending on read values ########################################*/
    if ((uwTabAddr >= SDRAM_ADDRESS) && (MSPValue >= SDRAM_ADDRESS))
    {
        BSP_LED_On(LED1);
    }
    else
    {   /* Toggle LED3 in an infinite loop */
        while (1)
        {
            BSP_LED_Toggle(LED3);
            /* Insert delay 100 ms */
            HAL_Delay(100);
        }
    }

    /* Infinite loop */
    while (1)
    {
    }
}
Example #13
0
/**
 * @brief release_processor(). 
 * @return -1 on error and zero on success
 * POST: gp_current_process gets updated
 */
int k_release_processor(void)
{
	 volatile int pid;
	 volatile proc_state_t state;
	 pcb_t * p_pcb_old = NULL;

	 pid = scheduler();
	 if (gp_current_process == NULL) {
	     return -1;  
	 }

	 p_pcb_old = gp_current_process;


	 if (pid == 1) {
	     gp_current_process = &pcb1;
	 } else if (pid ==2){
	     gp_current_process = &pcb2;
	 } else {
	     return -1;
	 }					 

	 state = gp_current_process->m_state;

     if (state == NEW) {
	     if (p_pcb_old->m_state != NEW) {
		     p_pcb_old->m_state = RDY;
			 p_pcb_old->mp_sp = (uint32_t *) __get_MSP();
		 }
		 gp_current_process->m_state = RUN;
		 __set_MSP((uint32_t) gp_current_process->mp_sp);
		 __rte();  // pop exception stack frame from the stack for new processes
	 } else if (state == RDY){     
		 p_pcb_old->m_state = RDY; 
		 p_pcb_old->mp_sp = (uint32_t *) __get_MSP(); // save the old process's sp
		 
		 gp_current_process->m_state = RUN;
		 __set_MSP((uint32_t) gp_current_process->mp_sp); //switch to the new proc's stack		
	 } else {
	     gp_current_process = p_pcb_old; // revert back to the old proc on error
	     return -1;
	 }	 	 
	 return 0;
}
void TCM_StackInit(void)
{
	uint32_t offset = (uint32_t)SRAM_STACK_LIMIT - (uint32_t)DTCM_STACK_LIMIT;
	volatile char *dst = (volatile char *)DTCM_STACK_LIMIT;
	volatile char *src = (volatile char *)SRAM_STACK_LIMIT;
	/* copy code_TCM from flash to ITCM */
	while(src > (volatile char *)SRAM_STACK_BASE){
		*--dst = *--src;
	}
	__set_MSP(__get_MSP() - offset);
}
void TCM_StackInit(void)
{
	uint32_t offset = (uint32_t)SRAM_STACK_LIMIT - (uint32_t)DTCM_STACK_LIMIT;
	volatile char *dst = (volatile char *)DTCM_STACK_LIMIT;
	volatile char *src = (volatile char *)SRAM_STACK_LIMIT;

	/* copy stack data from SRAM to DTCM */
	while (src > (volatile char *)SRAM_STACK_BASE)
		*--dst = *--src;

	__set_MSP(__get_MSP() - offset);
}
Example #16
0
/*===========================================================================
    _   B O S S _ S P Y _ M S P _ S E T U P
---------------------------------------------------------------------------*/
void _Boss_spy_msp_setup(void)
{
  boss_stk_t *msp_curr = (boss_stk_t *)__get_MSP(); /* Get Current Main Stack Pointer (MSP) */
  boss_stk_t *msp_base = _Boss_spy_msp_base();

  _boss_spy_msp_peak = msp_curr;
    
  for(; msp_base < msp_curr; msp_base++)
  {
    *msp_base = (boss_stk_t)0xEEEEEEEE;  // 스택 [E] empty
  }
}
Example #17
0
/*@brief: switch out old pcb (p_pcb_old), run the new pcb (gp_current_process)
 *@param: p_pcb_old, the old pcb that was in RUN
 *@return: RTX_OK upon success
 *         RTX_ERR upon failure
 *PRE:  p_pcb_old and gp_current_process are pointing to valid PCBs.
 *POST: if gp_current_process was NULL, then it gets set to pcbs[0].
 *      No other effect on other global variables.
 */
int process_switch(PCB *p_pcb_old) 
{
	PROC_STATE_E state;
	#ifdef DEBUG_0 
		//printf("switching from process %d to process %d\n\r", p_pcb_old->m_pid, gp_current_process->m_pid);
	#endif /* ! DEBUG_0 */
	state = gp_current_process->m_state;

	if (state == NEW) {
		if (gp_current_process != p_pcb_old && p_pcb_old->m_state != NEW) {
			p_pcb_old->mp_sp = (U32 *) __get_MSP();
			if (p_pcb_old->m_state != BLK && p_pcb_old->m_state != BLK_RCV) {
				p_pcb_old->m_state = RDY;
				pq_push(ready_queue, p_pcb_old);
			}
		}
		gp_current_process->m_state = RUN;
		__set_MSP((U32) gp_current_process->mp_sp);
		__rte();  // pop exception stack frame from the stack for a new processes
	} 
	
	/* The following will only execute if the if block above is FALSE */

	if (gp_current_process != p_pcb_old) {
		if (state == RDY){
			p_pcb_old->mp_sp = (U32 *) __get_MSP(); // save the old process's sp
			if (p_pcb_old->m_state != BLK && p_pcb_old->m_state != BLK_RCV) {
				p_pcb_old->m_state = RDY;
				pq_push(ready_queue, p_pcb_old);
			}
			gp_current_process->m_state = RUN;
			__set_MSP((U32) gp_current_process->mp_sp); //switch to the new proc's stack    
		} else {
			gp_current_process = p_pcb_old; // revert back to the old proc on error
			return RTX_ERR;
		} 
	}
	return RTX_OK;
}
Example #18
0
/* stack dump and information dump
 * for stack dump, a common rules is:
 * 0x2000xxxx (r7) 0x0000xxxx(lr), r7 will in stack and lr will in flash.
 * usually 12th long word is the address which calls panic().
 */
void panic(char *infostr)
{
	uint32_t sp;
	uint32_t size;
	fsave();
	
	kprintf("PANIC: %s\n", infostr);
#if 0
	if(get_psr() & 0xFF){
		/* in exception context, dump exception stack */
		sp = __get_MSP();
		if((sp>(uint32_t)_irq_stack_start) && (sp<(uint32_t)_irq_stack_start+1024))
		{
			size = (uint32_t)_irq_stack_start+1024-sp;
			kprintf("exception stacks: sp=0x%x depth=%d bytes\n", sp, size);
			dump_buffer((uint8_t *)sp, size);
		}
		else
			kprintf("broken MSP: 0x%x\n", sp);
	}

	
	if((current>=&systask[0]) && (current<&systask[MAX_TASK_NUMBER]))
	{
		/* dump task stack */
		sp = __get_PSP();
		if((sp>(uint32_t)current->stack_base) && (sp<(uint32_t)current->stack_base+current->stack_size))
		{
			size = (uint32_t)current->stack_base+current->stack_size-sp;
			kprintf("task stacks: sp=0x%x depth=%d bytes\n", sp, size);
			dump_buffer((uint8_t *)sp, size);
		}
		else
			kprintf("broken PSP: 0x%x\n", sp);

		/* dump current task info */
		kprintf("current=0x%x last sp=0x%x stack_base=0x%x taskname=%s state=%d taskq=0x%x\n", 
			current, current->sp, current->stack_base, current->name, current->state, current->taskq);
	}
	else
		kprintf("current is overwriten! current=0x%x\n", current);
#endif // 0

	/* dump system ready queue */

	/* dump memory usage */
	memory_dump();
	
    while(1);
}
Example #19
0
/**
\brief Test case: TC_CoreFunc_MSP
\details
- Check if __get_MSP and __set_MSP intrinsic can be used to manipulate main stack pointer.
*/
void TC_CoreFunc_MSP (void) {
  // don't use stack for this variables
  static uint32_t orig;
  static uint32_t msp;
  static uint32_t result;
  static uint32_t ctrl;

  ctrl = __get_CONTROL();
  __set_CONTROL(ctrl | CONTROL_SPSEL_Msk); // switch to PSP

  orig = __get_MSP();

  msp = orig + 0x12345678U;
  __set_MSP(msp);

  result = __get_MSP();

  __set_MSP(orig);

  __set_CONTROL(ctrl);

  ASSERT_TRUE(result == msp);
}
Example #20
0
extern caddr_t _sbrk(int incr) {
  extern char _end; // Defined by the linker
  static char *heap_end;
  char *prev_heap_end;
  if(heap_end == 0) {heap_end = &_end;}
  prev_heap_end = heap_end;
  char * stack = (char*) __get_MSP();
  if (heap_end+incr >  stack)
  {
    _write(1, (char *)"Heap and stack collision\n", 25);
    return  (caddr_t) -1;
  }
  heap_end += incr;
  return (caddr_t) prev_heap_end;
}
Example #21
0
/*!
 * \brief An MQX-provided default ISR for unhandled interrupts. The function
 * depends on the PSP.
 *
 * The function changes the state of the active task to UNHANDLED_INT_BLOCKED and
 * blocks the task.
 * \n The function uses the default I/O channel to display at least:
 * \li Vector number that caused the unhandled exception.
 * \li Task ID and task descriptor of the active task.
 *
 * \n Depending on the PSP, more information might be displayed.
 *
 * \param[in] parameter Parameter passed to the default ISR.
 *
 * \note
 * Since the ISR uses printf() to display information to the default I/O channel,
 * default I/O must not be on a channel that uses interrupt-driven I/O or the
 * debugger.
 *
 * \warning Blocks the active task.
 *
 * \see _int_install_unexpected_isr
 */
void _int_unexpected_isr
(
    void   *parameter
)
{ /* Body */
    KERNEL_DATA_STRUCT_PTR     kernel_data;
    TD_STRUCT_PTR              td_ptr;

    _GET_KERNEL_DATA(kernel_data);
    td_ptr = kernel_data->ACTIVE_PTR;

#if MQXCFG_PRINT_MEM_DUMP
#error  "MQXCFG_PRINT_MEM_DUMP functionality is currently not supported. This feature will be supported again in future release.\
         Please set MQXCFG_PRINT_MEM_DUMP to zero."
#endif

#if MQXCFG_PRINT_MEM_DUMP
    {
        unsigned int                psp, msp, i;
        printf("\n\r*** UNHANDLED INTERRUPT ***\n\r");
        printf("Vector #: 0x%02x Task Id: 0x%0x Td_ptr 0x%x\n\r",
        (unsigned int)parameter, (unsigned int)td_ptr->TASK_ID, (unsigned int)td_ptr);

        psp = __get_PSP();
        msp = __get_MSP();
        printf("PSP: 0x%08x MSP: 0x%08x PSR: 0x%08x\n\r", psp, msp, (unsigned int)__get_xPSR());

        printf("\n\r\n\rMemory dump:\n\r");
        for (i = 0; i < 32; i += 4) {
            printf("0x%08x : 0x%08x 0x%08x 0x%08x 0x%08x\n\r", psp + i * 4, ((unsigned int*)psp)[i], ((unsigned int*)psp)[i + 1], ((unsigned int*)psp)[i + 2], ((unsigned int*)psp)[i + 3]);
        }

        printf("\n\r\n\rMemory dump:\n\r");
        for (i = 0; i < 32; i += 4) {
            printf("0x%08x : 0x%08x 0x%08x 0x%08x 0x%08x\n\r", msp + i * 4, ((unsigned int*)msp)[i], ((unsigned int*)msp)[i + 1], ((unsigned int*)msp)[i + 2], ((unsigned int*)msp)[i + 3]);
        }
    }
#endif /* MQXCFG_PRINT_MEM_DUMP */
    _INT_DISABLE();
    if (td_ptr->STATE != UNHANDLED_INT_BLOCKED) {
        td_ptr->STATE = UNHANDLED_INT_BLOCKED;
        td_ptr->INFO  = (_mqx_uint)parameter;

        _QUEUE_UNLINK(td_ptr);
    } /* Endif */
   _INT_ENABLE();

} /* Endbody */
void *_sbrk_r(struct _reent *r, ptrdiff_t incr) {
	extern char end;   // provided by the linker script

	if (__brkval == 0)
		__brkval = &end;

	if (__brkval + incr > (char*) __get_MSP() - __malloc_margin) {
		r->_errno = ENOMEM;
		return (void*) -1;
	}

	void *ret = __brkval;
	__brkval += incr;

	return ret;
}
Example #23
0
/*===========================================================================
    _   B O S S _ S P Y _ M S P _ S E T U P
---------------------------------------------------------------------------*/
void _Boss_spy_msp_setup(void)
{
  extern const boss_u32_t CSTACK$$Base;
  extern const boss_u32_t CSTACK$$Limit;
  
  boss_stk_t *msp = (boss_stk_t *)__get_MSP(); /* Get Current Main Stack Pointer (MSP) */
  
  _spy_msp.sp_base  = (boss_stk_t *)&CSTACK$$Base;
  _spy_msp.sp_limit = (boss_stk_t *)&CSTACK$$Limit;
  _spy_msp.sp_peak  = (boss_stk_t *)msp;
  
  msp = msp - 1;    /* FD(Full Descending) Stack */
  
  for(; (boss_stk_t *)&CSTACK$$Base <= msp; --msp)
  {
    *msp = (boss_stk_t)0xEEEEEEEE;  // 스택 [E] empty
  }
}
Example #24
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization: global MSP (MCU Support Package) initialization
     */
  HAL_Init();

  /* Configure the system clock to 180 MHz */
  SystemClock_Config();
  
  /* Configure LED1 and LED2 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED3);

  /*##-1- Fill the buffer in the SRAM device ##########################################*/
  Fill_Buffer(aTable, 1024, 0);

  /*##-2- Read address of the buffer and stack pointer address ########################*/
  uwTabAddr = (uint32_t)aTable; /* should be above 0x64000000 */
  
  /* Get main stack pointer value */
  MSPValue = __get_MSP(); /* should be above 0x64000000 */

  /*##-3- Activate LEDs pending on read values ########################################*/
  if ((uwTabAddr >= SRAM_ADDRESS) && (MSPValue >= SRAM_ADDRESS))
  {
    BSP_LED_On(LED1);
  } else
  {
    BSP_LED_On(LED3);
  }

  /* Infinite loop */
  while (1)
  {
  }
}
Example #25
0
int CortexM_StackCheck( void )
{
    static int IsFirstCheck = 1;
    int temp = 0;

    if(0 != IsFirstCheck)
        //第一次执行此函数.
        //检查此时此刻是否栈溢出,并在栈底设定检查标志.
    {
        if(__get_MSP() <= (u32)Stack_Mem + 8)
            //需要使用8字节的检查空间,如果现在就不够了肿么办..
        {
            return 1;
        }
        else
            //有足够的空间用于填入检查用的数据.
        {
            //填入数据
            *((int *)Stack_Mem + 0) = CheckKey1;
            *((int *)Stack_Mem + 1) = CheckKey2;

            //标记已经填入了数据,以后就不来这里玩了.
            IsFirstCheck = 0;

            //OK
            return 0;
        }
    }
    else
        //前面已经埋下了好东西,看看还在不在...
    {
        if(CheckKey1 != *((int *)Stack_Mem + 0)
                || CheckKey2 != *((int *)Stack_Mem + 1))
            //好东西不在了...
        {
            return 1;
        }
        else
            //东西还在哈哈哈..
        {
            return 0;
        }
    }
}
Example #26
0
void _int_unexpected_isr
   (
      /* [IN] the parameter passed to the default ISR, the vector */
      pointer parameter
   )
{ /* Body */
   KERNEL_DATA_STRUCT_PTR     kernel_data;
   TD_STRUCT_PTR              td_ptr;
   //PSP_INT_CONTEXT_STRUCT_PTR exception_frame_ptr;
   uint_32                    psp, msp, i;

   _GET_KERNEL_DATA(kernel_data);
   td_ptr = kernel_data->ACTIVE_PTR;
   //exception_frame_ptr = kernel_data->INTERRUPT_CONTEXT_PTR;

   printf("\n\r*** UNHANDLED INTERRUPT ***\n\r"); 
   printf("Vector #: 0x%02x Task Id: 0x%0x Td_ptr 0x%x\n\r",
      (uint_32)parameter, (uint_32)td_ptr->TASK_ID, (uint_32)td_ptr);

   psp = __get_PSP();
   msp = __get_MSP();
   printf("PC: 0x%08x LR: 0x%08x PSP: 0x%08x MSP: 0x%08x PSR: 0x%08x\n\r", __get_PC(), __get_LR(), psp, msp, __get_PSR());

   printf("\n\r\n\rMemory dump:\n\r");
   for (i = 0; i < 32; i += 4) {
       printf("0x%08x : 0x%08x 0x%08x 0x%08x 0x%08x\n\r", psp + i * 4, ((uint_32*)psp)[i], ((uint_32*)psp)[i + 1], ((uint_32*)psp)[i + 2], ((uint_32*)psp)[i + 3]);
   }

   printf("\n\r\n\rMemory dump:\n\r");
   for (i = 0; i < 32; i += 4) {
       printf("0x%08x : 0x%08x 0x%08x 0x%08x 0x%08x\n\r", msp + i * 4, ((uint_32*)msp)[i], ((uint_32*)msp)[i + 1], ((uint_32*)msp)[i + 2], ((uint_32*)msp)[i + 3]);
   }

   _INT_ENABLE();
   if (td_ptr->STATE != UNHANDLED_INT_BLOCKED) {
      td_ptr->STATE = UNHANDLED_INT_BLOCKED;
      td_ptr->INFO  = (_mqx_uint)parameter;

      _QUEUE_UNLINK(td_ptr);
   } /* Endif */
   _INT_DISABLE();

} /* Endbody */
Example #27
0
void nOS_InitSpecific(void)
{
#if (NOS_CONFIG_DEBUG > 0)
    size_t i;

    for (i = 0; i < NOS_CONFIG_ISR_STACK_SIZE; i++) {
        _isrStack[i] = 0xFFFFFFFFUL;
    }
#endif

    /* Copy MSP to PSP */
    __set_PSP(__get_MSP());
    /* Set MSP to local ISR stack */
    __set_MSP((uint32_t)&_isrStack[NOS_CONFIG_ISR_STACK_SIZE] & 0xFFFFFFF8UL);
    /* Set current stack to PSP and privileged mode */
    __set_CONTROL(__get_CONTROL() | 0x00000002UL);
    /* Set PendSV exception to lowest priority */
    *(volatile uint32_t *)0xE000ED20UL |= 0x00FF0000UL;
}
caddr_t _sbrk(int incr)
{
  extern char _ebss; // Defined by the linker
  static char *heap_end;
  char *prev_heap_end;
  if (heap_end == 0)
  {
    heap_end = &_ebss;
  }
  prev_heap_end = heap_end;
  char * stack = (char*) __get_MSP();
  if (heap_end + incr > stack)
  {
    errno = ENOMEM;
    return (caddr_t) -1;
  }
  heap_end += incr;
  return (caddr_t) prev_heap_end;
}
/**************************************************************************//**
 * @brief
 *  Increase heap. Needed for printf
 *
 * @param[in] incr
 *  Number of bytes you want increment the program's data space.
 *
 * @return
 *  Returns a pointer to the start of the new area.
 *****************************************************************************/
caddr_t _sbrk(int incr)
{
  static char       *heap_end;
  char              *prev_heap_end;

  if (heap_end == 0)
  {
    heap_end = &_end;
  }

  prev_heap_end = heap_end;
  if ((heap_end + incr) > (char*) __get_MSP())
  {
    exit(1);
  }
  heap_end += incr;

  return (caddr_t) prev_heap_end;
}
Example #30
0
static uint32_t get_mem()
{
   // In order to get free mem within RTOS
   // we need to get the main thread's stack pointer
   // and subtract it with the top of the heap
   // ------+-------------------+   Last Address of RAM (INITIAL_SP)
   //       | Scheduler Stack   |
   //       +-------------------+
   //       | Main Thread Stack |
   //       |         |         |
   //       |         v         |
   //       +-------------------+ <- bottom_of_stack/__get_MSP()
   // RAM   |                   | 
   //       |  Available RAM    |  
   //       |                   |  
   //       +-------------------+ <- top_of_heap
   //       |         ^         |
   //       |         |         |
   //       |       Heap        |
   //       +-------------------+ <- __end__ / HEAP_START (linker defined var)
   //       | ZI                |
   //       +-------------------+
   //       | ZI: Shell Stack   |
   //       +-------------------+
   //       | ZI: Idle Stack    |
   //       +-------------------+
   //       | ZI: Timer Stack   |
   //       +-------------------+
   //       | RW                |  
   // ------+===================+  First Address of RAM
   //       |                   |
   // Flash |                   |
   //

   uint32_t bottom_of_stack = __get_MSP();
   char     * top_of_heap =  (char *) malloc(sizeof(char));
   uint32_t diff = bottom_of_stack - (uint32_t) top_of_heap;

   free((void *) top_of_heap);
   
   return diff;    
}