int CharDev::printf(const char *format, ...)
{
    if (taskSCHEDULER_RUNNING == xTaskGetSchedulerState()) {
        xSemaphoreTake(mPrintfSemaphore, portMAX_DELAY);
    }

        int len = 0;
        va_list args;
        va_start(args, format);

        do {
            va_list args_copy;
            va_copy(args_copy, args);
            len = vsnprintf(mpPrintfMem, mPrintfMemSize, format, args_copy);
            va_end(args_copy);

            if (len >= mPrintfMemSize) {
                const int align = 16;
                mPrintfMemSize = (align + ((len/align) * align));
                /* TO DO :
                 * Do not know why realloc() doesn't work.  It is a combination of C++ class
                 * use combined with va_args and realloc itself.  It seems to work in vector
                 * and str classes though.
                 */
                if (0) {
                    if (mpPrintfMem) {
                        free(mpPrintfMem);
                    }
                    mpPrintfMem = (char*) malloc(mPrintfMemSize);
                }
                else {
                    mpPrintfMem = (char*) realloc(mpPrintfMem, mPrintfMemSize);
                }
            }
            else {
                break;
            }
        } while (mpPrintfMem);

        va_end(args);
        this->put(mpPrintfMem);

    if (taskSCHEDULER_RUNNING == xTaskGetSchedulerState()) {
        xSemaphoreGive(mPrintfSemaphore);
    }

    return len;
}
Exemple #2
0
bool UartDev::putChar(char out, unsigned int timeout)
{
    /* If OS not running, just send data using polling and return */
    if (taskSCHEDULER_RUNNING != xTaskGetSchedulerState()) {
        mpUARTRegBase->THR = out;
        while(! (mpUARTRegBase->LSR & (1 << 6)));
        return true;
    }

    /* FreeRTOS running, so send to queue and if queue is full, return false */
    if(! xQueueSend(mTxQueue, &out, timeout)) {
        return false;
    }

    /* If transmitter is not busy, send out the oldest char from the queue,
     * and let the transmitter empty interrupt empty out the queue thereafter.
     */
    const int uart_tx_is_idle = (1 << 6);
    if (mpUARTRegBase->LSR & uart_tx_is_idle)
    {
        if (xQueueReceive(mTxQueue, &out, 0)) {
            mpUARTRegBase->THR = out;
        }
    }

    return true;
}
Exemple #3
0
int SDCardSendCommand(uint8_t command, uint32_t param, uint8_t crc, void* buffer, size_t recvSize) {
	Chip_GPIO_SetPinState(LPC_GPIO, 0, 2, !Chip_GPIO_GetPinState(LPC_GPIO, 0, 2));
	if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING)
		xSemaphoreTake(xMutexSDCard, portMAX_DELAY);
	int result = SDCARD_ERROR_GENERIC;
	int wait = SDCARD_SPI_MAX_WAIT;
	int i;
	uint8_t read = 0;
	uint8_t* data = (uint8_t*) buffer;
	command += 0x40;
	SDCardSetSS();
	for (i = 0; i < SDCARD_IDLE_PRE_WAIT_ATTEMPTS; i++) {
		if (spi_transceive_byte(SDCARD_SPI_DEVICE, 0xff) == 0xff) break;
	}

	result = SDCARD_ERROR_TRANSMIT_INTERRUPTED;

	CommandBuffer[0] = command;
	CommandBuffer[1] = param >> 24;
	CommandBuffer[2] = param >> 16;
	CommandBuffer[3] = param >> 8;
	CommandBuffer[4] = param;
	CommandBuffer[5] = (crc_crc7(CommandBuffer, 5) << 1) | 1;

	spi_transceive(SDCARD_SPI_DEVICE, CommandBuffer, 6);

	for (i = 0; i < 6; i++) {
		if (CommandBuffer[i] != 0xff) {
//			MSS_GPIO_set_output(MSS_GPIO_27, 0);
			__asm volatile ("nop");
//			MSS_GPIO_set_output(MSS_GPIO_27, 1);
			goto fail;
		}
	}
Exemple #4
0
/**
 * \brief      Initialize the managed memory module
 * \author     Adam Dunkels
 *
 *             This function initializes the managed memory module and
 *             should be called before any other function from the
 *             module.
 *
 */
int
mmem_init(void)
{
	/* Only execute the initalisation before the scheduler was started.
	 * So there exists only one thread and
	 * no locking is needed for the initialisation
	 */
	configASSERT(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED);

	/* cancle, if initialisation was already done */
	if(mutex != NULL) {
		return -1;
	}
	/* Do not use an recursive mutex,
	 * because mmem_check() will not work vital then.
	 */
	mutex = xSemaphoreCreateMutex();
	if(mutex == NULL) {
		return -2;
	}

	list_init(mmemlist);
	avail_memory = MMEM_SIZE;

	return 0;
}
Exemple #5
0
/*------------------------------------------------------------------------------
 * MSS_I2C_wait_complete()
 * See "mss_i2c.h" for details of how to use this function.
 */
mss_i2c_status_t MSS_I2C_wait_complete
(
    mss_i2c_instance_t * this_i2c
)
{
    ASSERT( (this_i2c == &g_mss_i2c0) || (this_i2c == &g_mss_i2c1) );

#ifdef USE_OLD_I2C_POLLING_CODE
    while ( this_i2c->status == MSS_I2C_IN_PROGRESS ) {
        /* Wait for transaction to compltete.*/
        ;
    }
#else
    configASSERT( ( this_i2c->xI2CCompleteSemaphore ) );
    if( xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED ) {
        while ( this_i2c->status == MSS_I2C_IN_PROGRESS ) {
            /* Wait for transaction to compltete.*/
            ;
        }
    } else {
        xSemaphoreTake( this_i2c->xI2CCompleteSemaphore, portMAX_DELAY );
    }
#endif

    return this_i2c->status;
}
Exemple #6
0
void wireless_service(void)
{
    /*
     * If FreeRTOS is running, then a task should be calling us, so we can block on
     * the nordic activity semaphore.
     *
     * There are three cases of block time :
     *  1 - If nordic interrupt signal is still pending, then we haven't read
     *      all Nordic FIFO, so we don't block on semaphore at all.
     *  2 - There are pending packets that need either ACK or retry, so we
     *      block just for one tick to carry out mesh logic.
     *  3 - No RX and no TX, so block until either a packet is sent, or until
     *      we receive a packet; both cases will give the semaphore.
     */
    if (taskSCHEDULER_RUNNING == xTaskGetSchedulerState()) {
        if (!nordic_intr_signal()) {
            const TickType_t blockTime = mesh_get_pnd_pkt_count() ? 1 : portMAX_DELAY;
            xSemaphoreTake(g_nrf_activity_sem, blockTime);
        }
        mesh_service();
    }
    /* A timer ISR is calling us, so we can't use FreeRTOS API, hence we poll */
    else {
        if (nordic_intr_signal() || mesh_get_pnd_pkt_count() > 0) {
            mesh_service();
        }
    }
}
/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
  if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
  {
    xPortSysTickHandler();
  }
}
char I2C_Base::writeReg(char deviceAddress, char registerAddress, char value)
{
    if(mDisableOperation) {
        return 0;
    }

    // If scheduler not running , perform polling transaction
    if(taskSCHEDULER_RUNNING != xTaskGetSchedulerState())
    {
        NVIC_DisableIRQ(mIRQ);
        {
            i2cKickOffTransfer(i2cWrite, deviceAddress, registerAddress, &value, 1);
            do {
                // Wait until SI flag is set, then call i2cStateMachine()
                while(! (mpI2CRegs->I2CONSET & (1 << 3)) )
                {
                    ;
                }
            }while (writeComplete != i2cStateMachine());
        }
        NVIC_EnableIRQ(mIRQ);
        return true;
    }

    // Wait for I2C Access, and just return, I2C Lock will be given by ISR()
    xSemaphoreTake(mI2CMutex, portMAX_DELAY);
    i2cKickOffTransfer(i2cWrite, deviceAddress, registerAddress, &value, 1);
    return 0;
}
/* If FreeRTOS is not running, then we rely on RIT service to call this function,
 * otherwise we rely on FreeRTOS tick hook to provide system timer
 */
static void hl_periodic_service(void)
{
    sys_watchdog_feed();

    /* If FreeRTOS is running, user should use a dedicated task to call mesh service,
     * so we will not call it if freertos is running
     */
    if (taskSCHEDULER_RUNNING == xTaskGetSchedulerState()) {
        g_system_uptime_ms += MS_PER_TICK();

        /* We don't need RIT if FreeRTOS is running */
        if (sys_rit_running()) {
            sys_rit_disable();

            /* The timer value so far may be an odd number, and if MS_PER_TICK() is not 1
             * then we may increment it like 12, 22, 32, etc. so round this number once.
             */
            g_system_uptime_ms = (g_system_uptime_ms / 10) * 10;
        }
    }
    else {
        g_system_uptime_ms += g_time_per_rit_isr_ms;
        wireless_service();

        /**
         * Small hack to support interrupts if FreeRTOS is not running :
         * FreeRTOS API resets our base priority register, then all
         * interrupts higher priority than IP_SYSCALL will not get locked out.
         *   @see more notes at isr_priorities.h.  @see IP_SYSCALL
         */
        __set_BASEPRI(0);
    }
}
Exemple #10
0
extern "C" void __malloc_unlock ( struct _reent *_r )
{
	if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING)		// don't release mutex if scheduler not started or suspended
	{
		mallocMutex.Release();
	}
}
unsigned char prvTraceIsSchedulerSuspended(void)
{
    /* Assumed to be available in FreeRTOS. According to the FreeRTOS docs, 
	INCLUDE_xTaskGetSchedulerState or configUSE_TIMERS must be set to 1 in
	FreeRTOSConfig.h for this function to be available. */

	return xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED;
}
char isFreeRtosRunning()
{
    if (!critialRegionSemaphore) {
        critialRegionSemaphore = xSemaphoreCreateMutex();
    }

    return (taskSCHEDULER_RUNNING == xTaskGetSchedulerState());
}
void ff_rel_grant (
	_SYNC_t sobj	/* Sync object to be signaled */
)
{
    if(taskSCHEDULER_RUNNING == xTaskGetSchedulerState())
    {
        xSemaphoreGive(sobj);	/* FreeRTOS */
    }
}
void __env_unlock ( struct _reent *_r )
{
#if OS_THREAD_SAFE_NEWLIB
	if (!xTaskGetSchedulerState())
		return;
	  
	xSemaphoreGiveRecursive(alt_envsem);
#endif /* OS_THREAD_SAFE_NEWLIB */
}
/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler (void)
{
  HAL_IncTick();
  if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
  {
    xPortSysTickHandler();
  }
  Toggle_Leds();
}
void osResumeAllTasks(void)
{
   //Make sure the operating system is running
   if(xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
   {
      //Resume all tasks
      xTaskResumeAll();
   }
}
void osSuspendAllTasks(void)
{
   //Make sure the operating system is running
   if(xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
   {
      //Suspend all tasks
      vTaskSuspendAll();
   }
}
Exemple #18
0
	BaseType_t MPU_xTaskGetSchedulerState( void )
	{
	BaseType_t xReturn;
	BaseType_t xRunningPrivileged = xPortRaisePrivilege();

		xReturn = xTaskGetSchedulerState();
		vPortResetPrivilege( xRunningPrivileged );
		return xReturn;
	}
Exemple #19
0
BaseType_t MPU_xTaskGetSchedulerState( void )
{
    BaseType_t xReturn;
    BaseType_t xRunningPrivileged = prvRaisePrivilege();

    xReturn = xTaskGetSchedulerState();
    portRESET_PRIVILEGE( xRunningPrivileged );
    return xReturn;
}
Exemple #20
0
	portBASE_TYPE MPU_xTaskGetSchedulerState( void )
	{
	portBASE_TYPE xReturn;
    portBASE_TYPE xRunningPrivileged = prvRaisePrivilege();

		xReturn = xTaskGetSchedulerState();
        portRESET_PRIVILEGE( xRunningPrivileged );
		return xReturn;
	}
/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
	HAL_IncTick();
	if(xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
	{
		xPortSysTickHandler();
	}
	// HAL_SYSTICK_IRQHandler();
	systick_count++;
}
Exemple #22
0
bool UartDev::flush(void)
{
    if (taskSCHEDULER_RUNNING == xTaskGetSchedulerState()) {
        while (getTxQueueSize() > 0) {
            vTaskDelay(1);
        }
    }

    return true;
}
/********************************************************************* 
* 
*       _cbSendTaskList()
*
*  Function description
*    This function is part of the link between FreeRTOS and SYSVIEW.
*    Called from SystemView when asked by the host, it uses SYSVIEW
*    functions to send the entire task list to the host.
*/
static void _cbSendTaskList(void) {
  TaskStatus_t*         pxTaskStatusArray;
  UBaseType_t           uxArraySize;
  UBaseType_t           x;
  char                  cStatus;

#if INCLUDE_xTaskGetIdleTaskHandle
  TaskHandle_t          hIdle;
  hIdle = xTaskGetIdleTaskHandle();
#endif

  /* Take a snapshot of the number of tasks in case it changes while this
  function is executing. */
  uxArraySize = uxTaskGetNumberOfTasks();

  /* Allocate an array index for each task. */
  pxTaskStatusArray = pvPortMalloc( uxArraySize * sizeof( TaskStatus_t ) );

  if( pxTaskStatusArray != NULL ) {
    /* Generate the (binary) data. */
    uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );

#if INCLUDE_xTaskGetIdleTaskHandle
    /* only get Idle task handle if scheduler has been started */
    if (xTaskGetSchedulerState()!=taskSCHEDULER_NOT_STARTED) {
      hIdle = xTaskGetIdleTaskHandle();
    } else {
      hIdle = NULL;
    }
#endif
    
    /* Create a human readable table from the binary data. */
    for( x = 0; x < uxArraySize; x++ ) {
      uint8_t* pStack;
#if INCLUDE_pxTaskGetStackStart
      pStack = pxTaskGetStackStart(pxTaskStatusArray[x].xHandle);
#else
      pStack = (uint8_t*)0;
#endif

#if INCLUDE_xTaskGetIdleTaskHandle
      if (pxTaskStatusArray[x].xHandle != hIdle) {
        SYSVIEW_SendTaskInfo((unsigned)pxTaskStatusArray[x].xHandle, pxTaskStatusArray[x].pcTaskName, pxTaskStatusArray[x].uxCurrentPriority, (unsigned int)pStack, 0);
      }
#else
      if (memcmp(pxTaskStatusArray[x].pcTaskName, "IDLE", 5) != 0) {
        SYSVIEW_SendTaskInfo((unsigned)pxTaskStatusArray[x].xHandle, pxTaskStatusArray[x].pcTaskName, pxTaskStatusArray[x].uxCurrentPriority, (unsigned int)pStack, 0);
      }
#endif
    }

    /* Free the array again. */
    vPortFree( pxTaskStatusArray );
  }
}
 void log(String text) {
     // ToDo: Add other debug outputs
     if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {
         if (xSemaphoreTake(serialPortMutex, ( TickType_t ) 10) == pdTRUE ) {
             DEBUG_SERIAL.println(text);
         }
         xSemaphoreGive(serialPortMutex);
     } else {
         DEBUG_SERIAL.println(text);
     }
 }
Exemple #25
0
void SysTick_Handler(void)
{
    //-- FreeRTOS 인터럽트 호출
    //
    if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
    {
        xPortSysTickHandler();
    }

    sysTickUptime++;
}
Exemple #26
0
/**
* @brief  Check if the RTOS kernel is already started
* @param  None
* @retval (0) RTOS is not started
*         (1) RTOS is started
*         (-1) if this feature is disabled in FreeRTOSConfig.h 
* @note  MUST REMAIN UNCHANGED: \b osKernelRunning shall be consistent in every CMSIS-RTOS.
*/
int32_t osKernelRunning(void)
{
#if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED)
    return 0;
  else
    return 1;
#else
	return (-1);
#endif	
}
Exemple #27
0
void uart0_write(const uint8_t* data, size_t size) {
	if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
		uart0_write_internal(data, size, false);
	} else {
		xSemaphoreTake(mutex_uart_in_use, portMAX_DELAY);
		// locked
		uart0_write_internal(data, size, true);
		xSemaphoreTake(sem_uart_ready, portMAX_DELAY);

		xSemaphoreGive(mutex_uart_in_use);
	}
}
Exemple #28
0
/**
 * @remarks: behavior changed to block only when txring buffer is full (prior to return)
 * Guarantees that everything is written (thus retries whenever necessary)
 */
static void uart0_write_internal(const uint8_t* data, size_t size, bool in_rtos) {
	uint32_t written;
	while (size > 0) {
		written = Chip_UART0_SendRB(LPC_USART0, &txring, data, size);
		data += written;
		size -= written;

		if (in_rtos && RingBuffer_IsFull(&txring) && xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {
			xSemaphoreTake(sem_uart_ready, portMAX_DELAY);
		}
	}
}
Exemple #29
0
/**
* @brief  Handles the tick increment
* @param  none.
* @retval none.
*/
void osSystickHandler(void)
{

#if (INCLUDE_xTaskGetSchedulerState  == 1 )
  if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
  {
#endif  /* INCLUDE_xTaskGetSchedulerState */  
    xPortSysTickHandler();
#if (INCLUDE_xTaskGetSchedulerState  == 1 )
  }
#endif  /* INCLUDE_xTaskGetSchedulerState */  
}
int ff_req_grant (	/* TRUE:Got a grant to access the volume, FALSE:Could not get a grant */
	_SYNC_t sobj	/* Sync object to wait */
)
{
	int ret = __true;

    if(taskSCHEDULER_RUNNING == xTaskGetSchedulerState())
    {
        ret = (xSemaphoreTake(sobj, _FS_TIMEOUT) == pdTRUE) ? __true : __false;	/* FreeRTOS */
    }

	return ret;
}