/*------------------------------------------------------------------------------
 * Function: updateTask
 *
 * Description: This task observes the currently stored velocities for every
 *  game object and updates their position and rotation accordingly. It also
 *  updates the ship's velocities based on its current acceleration and angle.
 *  If a bullet has been in flight for too long, this task will delete it. This
 *  task runs every 10 milliseconds.
 *
 * param vParam: This parameter is not used.
 *----------------------------------------------------------------------------*/
void updateTask(void *vParam) {
	float vel;
	object *objIter, *objPrev;
	for (;;) {
		// spin ship1
		ship1.angle += ship1.a_vel;
		if (ship1.angle >= 360)
         ship1.angle -= 360;
		else if (ship1.angle < 0)
		   ship1.angle += 360;
         
      // spin ship2
      ship2.angle += ship2.a_vel;
      if (ship2.angle >= 360)
      ship2.angle -= 360;
      else if (ship2.angle < 0)
      ship2.angle += 360;     

		// move ship1
		ship1.vel.x += ship1.accel * -sin(ship1.angle * DEG_TO_RAD);
		ship1.vel.y += ship1.accel * -cos(ship1.angle * DEG_TO_RAD);
		vel = ship1.vel.x * ship1.vel.x + ship1.vel.y * ship1.vel.y;
		if (vel > SHIP_MAX_VEL) {
			ship1.vel.x *= SHIP_MAX_VEL / vel;
			ship1.vel.y *= SHIP_MAX_VEL / vel;
		}
		ship1.pos.x += ship1.vel.x;
		ship1.pos.y += ship1.vel.y;
		
		if (ship1.pos.x - SHIP_OFFSET < WALL_EDGE) {
   		ship1.pos.x += WALL_BOUNCE;
   		ship1.vel.x = 0;
   		ship1.vel.y = 0;
   		ship1.accel = 0;
   		ship1.a_vel = 0;
		} else if (ship1.pos.x + SHIP_OFFSET > SCREEN_W - (WALL_EDGE)) {
   		ship1.pos.x -= WALL_BOUNCE;
   		ship1.vel.x = 0;
   		ship1.vel.y = 0;
   		ship1.accel = 0;
   		ship1.a_vel = 0;
		}
		if (ship1.pos.y - SHIP_OFFSET < WALL_EDGE) {
   		ship1.pos.y += WALL_BOUNCE;
   		ship1.vel.x = 0;
   		ship1.vel.y = 0;
   		ship1.accel = 0;
   		ship1.a_vel = 0;
		} else if (ship1.pos.y + SHIP_OFFSET > SCREEN_H - (WALL_EDGE)) {
   		ship1.pos.y -= WALL_BOUNCE;
   		ship1.vel.x = 0;
   		ship1.vel.y = 0;
   		ship1.accel = 0;
   		ship1.a_vel = 0;
		}

      // move ship2
      ship2.vel.x += ship2.accel * -sin(ship2.angle * DEG_TO_RAD);
      ship2.vel.y += ship2.accel * -cos(ship2.angle * DEG_TO_RAD);
      vel = ship2.vel.x * ship2.vel.x + ship2.vel.y * ship2.vel.y;
      if (vel > SHIP_MAX_VEL) {
         ship2.vel.x *= SHIP_MAX_VEL / vel;
         ship2.vel.y *= SHIP_MAX_VEL / vel;
      }
      ship2.pos.x += ship2.vel.x;
      ship2.pos.y += ship2.vel.y;

      if (ship2.pos.x - SHIP_OFFSET < WALL_EDGE) {
         ship2.pos.x += WALL_BOUNCE;
         ship2.vel.x = 0;
         ship2.vel.y = 0;
         ship2.accel = 0;
         ship2.a_vel = 0;
      } else if (ship2.pos.x + SHIP_OFFSET > SCREEN_W - (WALL_EDGE)) {
         ship2.pos.x -= WALL_BOUNCE;
         ship2.vel.x = 0;
         ship2.vel.y = 0;
         ship2.accel = 0;
         ship2.a_vel = 0;
      }
      if (ship2.pos.y - SHIP_OFFSET < WALL_EDGE) {
         ship2.pos.y += WALL_BOUNCE;
         ship2.vel.x = 0;
         ship2.vel.y = 0;
         ship2.accel = 0;
         ship2.a_vel = 0;
      } else if (ship2.pos.y + SHIP_OFFSET > SCREEN_H - (WALL_EDGE)) {
         ship2.pos.y -= WALL_BOUNCE;
         ship2.vel.x = 0;
         ship2.vel.y = 0;
         ship2.accel = 0;
         ship2.a_vel = 0;
      }
      
		// move bullets_ship1
		objPrev = NULL;
		objIter = bullets_ship1;
		while (objIter != NULL) {
			// Kill bullet after a while
			objIter->life += FRAME_DELAY_MS;
			if (objIter->life >= BULLET_LIFE_MS) {
				xSemaphoreTake(usartMutex, portMAX_DELAY);
				vSpriteDelete(objIter->handle);
				if (objPrev != NULL) {
					objPrev->next = objIter->next;
					vPortFree(objIter);
					objIter = objPrev->next;
				} else {
					bullets_ship1 = objIter->next;
					vPortFree(objIter);
					objIter = bullets_ship1;
				}
				xSemaphoreGive(usartMutex);
			} else {
            objIter->pos.x += objIter->vel.x;
            objIter->pos.y += objIter->vel.y;

            if (objIter->pos.x < 0.0) {
             objIter->pos.x += SCREEN_W;
            } else if (objIter->pos.x > SCREEN_W) {
             objIter->pos.x -= SCREEN_W;
            }

            if (objIter->pos.y < 0.0) {
             objIter->pos.y += SCREEN_H;
            } else if (objIter->pos.y > SCREEN_H) {
             objIter->pos.y -= SCREEN_H;
            }
            objPrev = objIter;
            objIter = objIter->next;
			}			
		}

      // move bullets_ship2
      objPrev = NULL;
      objIter = bullets_ship2;
      while (objIter != NULL) {
         // Kill bullet after a while
         objIter->life += FRAME_DELAY_MS;
         if (objIter->life >= BULLET_LIFE_MS) {
            xSemaphoreTake(usartMutex, portMAX_DELAY);
            vSpriteDelete(objIter->handle);
            if (objPrev != NULL) {
               objPrev->next = objIter->next;
               vPortFree(objIter);
               objIter = objPrev->next;
            } else {
               bullets_ship2 = objIter->next;
               vPortFree(objIter);
               objIter = bullets_ship2;
            }
            xSemaphoreGive(usartMutex);
         } else {
            objIter->pos.x += objIter->vel.x;
            objIter->pos.y += objIter->vel.y;

            if (objIter->pos.x < 0.0) {
               objIter->pos.x += SCREEN_W;
            } else if (objIter->pos.x > SCREEN_W) {
               objIter->pos.x -= SCREEN_W;
            }

            if (objIter->pos.y < 0.0) {
               objIter->pos.y += SCREEN_H;
            } else if (objIter->pos.y > SCREEN_H) {
               objIter->pos.y -= SCREEN_H;
            }
            objPrev = objIter;
            objIter = objIter->next;
         }
      }
		
		vTaskDelay(FRAME_DELAY_MS / portTICK_RATE_MS);
	}
}
Exemplo n.º 2
0
void vStartSemaphoreTasks( UBaseType_t uxPriority )
{
xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;
const TickType_t xBlockTime = ( TickType_t ) 100;

	/* Create the structure used to pass parameters to the first two tasks. */
	pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );

	if( pxFirstSemaphoreParameters != NULL )
	{
		/* Create the semaphore used by the first two tasks. */
		pxFirstSemaphoreParameters->xSemaphore = xSemaphoreCreateBinary();		

		if( pxFirstSemaphoreParameters->xSemaphore != NULL )
		{
			xSemaphoreGive( pxFirstSemaphoreParameters->xSemaphore );
			
			/* Create the variable which is to be shared by the first two tasks. */
			pxFirstSemaphoreParameters->pulSharedVariable = ( uint32_t * ) pvPortMalloc( sizeof( uint32_t ) );

			/* Initialise the share variable to the value the tasks expect. */
			*( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;

			/* The first two tasks do not block on semaphore calls. */
			pxFirstSemaphoreParameters->xBlockTime = ( TickType_t ) 0;

			/* Spawn the first two tasks.  As they poll they operate at the idle priority. */
			xTaskCreate( prvSemaphoreTest, "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
			xTaskCreate( prvSemaphoreTest, "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );

			/* vQueueAddToRegistry() adds the semaphore to the registry, if one
			is in use.  The registry is provided as a means for kernel aware
			debuggers to locate semaphores and has no purpose if a kernel aware
			debugger is not being used.  The call to vQueueAddToRegistry() will
			be removed by the pre-processor if configQUEUE_REGISTRY_SIZE is not
			defined or is defined to be less than 1. */
			vQueueAddToRegistry( ( QueueHandle_t ) pxFirstSemaphoreParameters->xSemaphore, "Counting_Sem_1" );
		}
	}

	/* Do exactly the same to create the second set of tasks, only this time
	provide a block time for the semaphore calls. */
	pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
	if( pxSecondSemaphoreParameters != NULL )
	{
		pxSecondSemaphoreParameters->xSemaphore = xSemaphoreCreateBinary();		

		if( pxSecondSemaphoreParameters->xSemaphore != NULL )
		{
			xSemaphoreGive( pxSecondSemaphoreParameters->xSemaphore );
			
			pxSecondSemaphoreParameters->pulSharedVariable = ( uint32_t * ) pvPortMalloc( sizeof( uint32_t ) );
			*( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;
			pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_PERIOD_MS;

			xTaskCreate( prvSemaphoreTest, "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( TaskHandle_t * ) NULL );
			xTaskCreate( prvSemaphoreTest, "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( TaskHandle_t * ) NULL );

			/* vQueueAddToRegistry() adds the semaphore to the registry, if one
			is in use.  The registry is provided as a means for kernel aware
			debuggers to locate semaphores and has no purpose if a kernel aware
			debugger is not being used.  The call to vQueueAddToRegistry() will
			be removed by the pre-processor if configQUEUE_REGISTRY_SIZE is not
			defined or is defined to be less than 1. */
			vQueueAddToRegistry( ( QueueHandle_t ) pxSecondSemaphoreParameters->xSemaphore, "Counting_Sem_2" );
		}
	}
}
Exemplo n.º 3
0
/**
* @brief  Initializes the SPI for SPIRIT
* @param  None
* @retval None
*/
void SdkEvalSpiInit(void)
{
  SPI_InitTypeDef SPI_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  
  s_SpiPort = s_SpiPortVersion[SdkEvalGetVersion()];
  s_vectnSpiPin = (uint16_t *)&s_vectpxSpiCsPinVersion[SdkEvalGetVersion()];
  s_vectpxSpiPort = &s_vectpxSpiCsPortVersion[SdkEvalGetVersion()];
  
  if(!SdkEvalGetVersion())
  {
    /* Enable SPI periph and SCLK, MOSI, MISO and CS GPIO clocks */
    RCC_APB2PeriphClockCmd(SDK_EVAL_V2_SPI_PERIPH_RCC, ENABLE);
    RCC_AHBPeriphClockCmd(SDK_EVAL_V2_SPI_PERIPH_MOSI_RCC | SDK_EVAL_V2_SPI_PERIPH_MISO_RCC | SDK_EVAL_V2_SPI_PERIPH_SCLK_RCC | SDK_EVAL_V2_SPI_PERIPH_CS_RCC, ENABLE);
    
    /* Configure the AF for MOSI, MISO and SCLK GPIO pins*/
    GPIO_PinAFConfig(SDK_EVAL_V2_SPI_PERIPH_MOSI_PORT, SDK_EVAL_V2_SPI_PERIPH_MOSI_RCC_SOURCE, SDK_EVAL_V2_SPI_PERIPH_MOSI_AF);
    GPIO_PinAFConfig(SDK_EVAL_V2_SPI_PERIPH_MISO_PORT, SDK_EVAL_V2_SPI_PERIPH_MISO_RCC_SOURCE, SDK_EVAL_V2_SPI_PERIPH_MISO_AF);
    GPIO_PinAFConfig(SDK_EVAL_V2_SPI_PERIPH_SCLK_PORT, SDK_EVAL_V2_SPI_PERIPH_SCLK_RCC_SOURCE, SDK_EVAL_V2_SPI_PERIPH_SCLK_AF);
    
    /* Configure SPI pins:SCLK, MISO and MOSI */
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
    
    GPIO_InitStructure.GPIO_Pin = SDK_EVAL_V2_SPI_PERIPH_SCLK_PIN;
    GPIO_Init(SDK_EVAL_V2_SPI_PERIPH_SCLK_PORT, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = SDK_EVAL_V2_SPI_PERIPH_MISO_PIN;
    GPIO_Init(SDK_EVAL_V2_SPI_PERIPH_MISO_PORT, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = SDK_EVAL_V2_SPI_PERIPH_MOSI_PIN;
    GPIO_Init(SDK_EVAL_V2_SPI_PERIPH_MOSI_PORT, &GPIO_InitStructure);
    
    /* Configure SPI pin: CS */
    GPIO_InitStructure.GPIO_Pin = SDK_EVAL_V2_SPI_PERIPH_CS_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
    GPIO_Init(SDK_EVAL_V2_SPI_PERIPH_CS_PORT, &GPIO_InitStructure);
    
  }
  else
  {
    /* Enable SPI periph and SCLK, MOSI, MISO and CS GPIO clocks */
    RCC_AHBPeriphClockCmd(SDK_EVAL_V3_SPI_PERIPH_MOSI_RCC | SDK_EVAL_V3_SPI_PERIPH_MISO_RCC | SDK_EVAL_V3_SPI_PERIPH_SCLK_RCC | SDK_EVAL_V3_SPI_PERIPH_CS_RCC, ENABLE);
    
    /* Configure the AF for MOSI, MISO and SCLK GPIO pins*/
    GPIO_PinAFConfig(SDK_EVAL_V3_SPI_PERIPH_MOSI_PORT, SDK_EVAL_V3_SPI_PERIPH_MOSI_RCC_SOURCE, SDK_EVAL_V3_SPI_PERIPH_MOSI_AF);
    GPIO_PinAFConfig(SDK_EVAL_V3_SPI_PERIPH_MISO_PORT, SDK_EVAL_V3_SPI_PERIPH_MISO_RCC_SOURCE, SDK_EVAL_V3_SPI_PERIPH_MISO_AF);
    GPIO_PinAFConfig(SDK_EVAL_V3_SPI_PERIPH_SCLK_PORT, SDK_EVAL_V3_SPI_PERIPH_SCLK_RCC_SOURCE, SDK_EVAL_V3_SPI_PERIPH_SCLK_AF);
    
    /* Configure SPI pins:SCLK, MISO and MOSI */
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
    
    GPIO_InitStructure.GPIO_Pin = SDK_EVAL_V3_SPI_PERIPH_SCLK_PIN;
    GPIO_Init(SDK_EVAL_V3_SPI_PERIPH_SCLK_PORT, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = SDK_EVAL_V3_SPI_PERIPH_MISO_PIN;
    GPIO_Init(SDK_EVAL_V3_SPI_PERIPH_MISO_PORT, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = SDK_EVAL_V3_SPI_PERIPH_MOSI_PIN;
    GPIO_Init(SDK_EVAL_V3_SPI_PERIPH_MOSI_PORT, &GPIO_InitStructure);
    
    /* Configure SPI pin: CS */
    GPIO_InitStructure.GPIO_Pin = SDK_EVAL_V3_SPI_PERIPH_CS_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
    GPIO_Init(SDK_EVAL_V3_SPI_PERIPH_CS_PORT, &GPIO_InitStructure);
    
    /* Enable SPI periph and SCLK, MOSI, MISO and CS GPIO clocks */
    RCC_APB1PeriphClockCmd(SDK_EVAL_V3_SPI_PERIPH_RCC, ENABLE);     
  }
  
  /* Configure SPI peripheral */
  SPI_DeInit(s_SpiPort);
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(s_SpiPort, &SPI_InitStructure);
  
  SPI_Cmd(s_SpiPort, ENABLE);
  
#ifdef FREERTOS  
  xSpiMutex = xSemaphoreCreateMutex();
  if (!xSpiMutex)
    /* Error in resource creation. */
    for (;;);
  xSemaphoreGive(xSpiMutex);  
#endif
  
  
  SdkEvalSPICSHigh();
}
Exemplo n.º 4
0
void unlock_spi() {
    xSemaphoreGive(spiLock);
}
Exemplo n.º 5
0
void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) {
    xSemaphoreGive(mutex->handle);
}
Exemplo n.º 6
0
/* This function is usually called by the worker subsystem */
void logRunBlock(void * arg)
{
  struct log_block *blk = arg;
  struct log_ops *ops = blk->ops;
  static CRTPPacket pk;
  unsigned int timestamp;

  xSemaphoreTake(logLock, portMAX_DELAY);

  timestamp = ((long long)xTaskGetTickCount())/portTICK_RATE_MS;

  pk.header = CRTP_HEADER(CRTP_PORT_LOG, LOG_CH);
  pk.size = 4;
  pk.data[0] = blk->id;
  pk.data[1] = timestamp&0x0ff;
  pk.data[2] = (timestamp>>8)&0x0ff;
  pk.data[3] = (timestamp>>16)&0x0ff;

  while (ops)
  {
    float variable;
    int valuei = 0;
    float valuef = 0;

    // FPU instructions must run on aligned data. Make sure it is.
    variable = *(float *)ops->variable;

    switch(ops->storageType)
    {
      case LOG_UINT8:
        valuei = *(uint8_t *)&variable;
        break;
      case LOG_INT8:
        valuei = *(int8_t *)&variable;
        break;
      case LOG_UINT16:
        valuei = *(uint16_t *)&variable;
        break;
      case LOG_INT16:
        valuei = *(int16_t *)&variable;
        break;
      case LOG_UINT32:
        valuei = *(uint32_t *)&variable;
        break;
      case LOG_INT32:
        valuei = *(int32_t *)&variable;
        break;
      case LOG_FLOAT:
        valuei = *(float *)&variable;
        break;
    }

    if (ops->logType == LOG_FLOAT || ops->logType == LOG_FP16)
    {
      if (ops->storageType == LOG_FLOAT)
        valuef = *(float *)&variable;
      else
        valuef = valuei;

      // Try to append the next item to the packet.  If we run out of space,
      // drop this and subsequent items.
      if (ops->logType == LOG_FLOAT)
      {
        if (!appendToPacket(&pk, &valuef, 4)) break;
      }
      else
      {
        valuei = single2half(valuef);
        if (!appendToPacket(&pk, &valuei, 2)) break;
      }
    }
    else  //logType is an integer
    {
      if (!appendToPacket(&pk, &valuei, typeLength[ops->logType])) break;
    }

    ops = ops->next;
  }

  xSemaphoreGive(logLock);

  // Check if the connection is still up, oherwise disable
  // all the logging and flush all the CRTP queues.
  if (!crtpIsConnected())
  {
    logReset();
    crtpReset();
  }
  else
  {
    crtpSendPacket(&pk);
  }
}
Exemplo n.º 7
0
void ff_rel_grant (
	FF_SYNC_t sobj	/* Sync object to be signaled */
)
{
    xSemaphoreGive(sobj);
}
Exemplo n.º 8
0
void SetWidgetList(tMessage *pMsg)
{
  static Widget_t *pCurrWidget = NULL; // point to Widget in current Widget[]
  static Widget_t *pNextWidget = NULL; // point to Widget in new Widget[]
  static unsigned char ChangedClockWidget = INVALID_ID;

  xSemaphoreTake(SramMutex, portMAX_DELAY);

  WidgetList_t *pMsgWgtLst = (WidgetList_t *)pMsg->pBuffer;
  unsigned char WidgetNum = pMsg->Length / WIDGET_HEADER_LEN;

  unsigned char i = 0;
  PrintF(">SetWLst I:%d %s %d %s %d", WGTLST_INDEX(pMsg->Options), "T:", WGTLST_TOTAL(pMsg->Options), "Num:", WidgetNum);
  for(; i<WidgetNum; ++i) {PrintH(pMsgWgtLst[i].Id); PrintH(pMsgWgtLst[i].Layout);} PrintR();

  if (pNextWidget == NULL) // first time call, only add widgets
  {
    pCurrWidget = pCurrWidgetList;
    pNextWidget = &Widget[0];
  }
  else
  {
    if (WGTLST_INDEX(pMsg->Options) == 0 &&
      (pCurrWidget != pCurrWidgetList || (pNextWidget != &Widget[0] && pNextWidget != &Widget[MAX_WIDGET_NUM])))
    { // last SetWLst failed in the middle.Clean up whole list
      PrintS("# Last SetWgtLst broken!");

      pCurrWidget = pCurrWidgetList;
      pNextWidget = &Widget[0] + (&Widget[MAX_WIDGET_NUM] - pCurrWidgetList);
    }
  }

  while (WidgetNum) // number of list items
  {
      /* old clock widgets */
    if (!IS_CLOCK_WIDGET(pMsgWgtLst->Layout) && pMsgWgtLst->Id <= CLOCK_WIDGET_ID_RANGE) TestFaceId(pMsgWgtLst);
    unsigned char Change = GetWidgetChange(pCurrWidget->Id, pCurrWidget->Layout, pMsgWgtLst->Id, pMsgWgtLst->Layout);
    
    switch (Change)
    {
    case WGT_CHG_CLK_FACE:
      PrintS("Chg ClkFce");
      if (ON_CURRENT_PAGE(pMsgWgtLst->Layout)) ChangedClockWidget = pMsgWgtLst->Id;
      
    case WGT_CHG_SETTING:
     //cpy layout to curr; cpy curr to next; msg, curr, next ++
      PrintF("=%02X", pCurrWidget->Id);
      pCurrWidget->Id = pMsgWgtLst->Id;
      pCurrWidget->Layout = pMsgWgtLst->Layout;
      *pNextWidget++ = *pCurrWidget++;
      pMsgWgtLst ++;
      WidgetNum --;
      break;

    case WGT_CHG_CLK_ADD:
      PrintS("+Clk");
      if (ON_CURRENT_PAGE(pMsgWgtLst->Layout)) ChangedClockWidget = pMsgWgtLst->Id;

    case WGT_CHG_ADD: //pCurrWidget->Id > pMsgWgtLst->Id)
     // add new widget: cpy msg to next; msg and next ++; curr stays
      PrintF("+%02X", pMsgWgtLst->Id);

      pNextWidget->Id = pMsgWgtLst->Id;
      pNextWidget->Layout = pMsgWgtLst->Layout;
      AssignWidgetBuffer(pNextWidget);

      pNextWidget ++;
      pMsgWgtLst ++;
      WidgetNum --;
      break;
      
    case WGT_CHG_REMOVE:
    // remove widget: curr ++
      PrintF("-%02X", pCurrWidget->Id);
      FreeWidgetBuffer(pCurrWidget);
      pCurrWidget ++;
      break;
      
    default: break;
    }
  }
  PrintR();

  // if part index + 1 == parts, SetWidgetList complete
  if (WGTLST_TOTAL(pMsg->Options) == WGTLST_INDEX(pMsg->Options) + 1)
  {
//    PrintS("C:");
//    for (i=0; pCurrWidgetList[i].Id != INVALID_ID && i < MAX_WIDGET_NUM; ++i) PrintH(pCurrWidgetList[i].Id);
//    PrintR();

    while (pCurrWidget->Id != INVALID_ID && pCurrWidget < &pCurrWidgetList[MAX_WIDGET_NUM])
    {
      FreeWidgetBuffer(pCurrWidget);
      pCurrWidget->Id = INVALID_ID;
      pCurrWidget ++;
    }

    for (i = 0; i < MAX_WIDGET_NUM; ++i)
    {
      if (pCurrWidgetList[i].Id != INVALID_ID)
      { // clear the widget id in the curr list
        pCurrWidgetList[i].Id = INVALID_ID;
      }
    }

    pNextWidget = pCurrWidgetList;
    pCurrWidgetList = &Widget[0] + (&Widget[MAX_WIDGET_NUM] - pCurrWidgetList);
    pCurrWidget = pCurrWidgetList;

//    PrintS("N:");
//    for (i=0; pCurrWidgetList[i].Id != INVALID_ID; ++i) PrintH(pCurrWidgetList[i].Id);
//    PrintR();
    PrintF("Tg:%04X", BufTag);

    if (ChangedClockWidget != INVALID_ID)
    {
      CreateAndSendMessage(DrawClockWidgetMsg, ChangedClockWidget);
      ChangedClockWidget = INVALID_ID;
    }
  }
  xSemaphoreGive(SramMutex);
}
Exemplo n.º 9
0
void AnalyzerControl::WakeAnalysisRequest()
{
	xSemaphoreGive(_semaphore);
}
Exemplo n.º 10
0
void GUI_X_Unlock(void)
{ 
  xSemaphoreGive( xQueueMutex ); 
}
Exemplo n.º 11
0
void GUI_X_SignalEvent (void) 
{
  xSemaphoreGive( xSemaTxDone );
}
Exemplo n.º 12
0
void obp_uds(void *pvParameters)
{
//>>>> oobdtemple protocol initmain  >>>>
    int keeprunning = 1;
    data_packet *dp;
    data_packet actDataPacket;
    UBaseType_t busToUse = *(UBaseType_t *) pvParameters;
/* function pointers to the bus interface */
    extern bus_init actBus_init;
    extern bus_send actBus_send;
    extern bus_flush actBus_flush;
    extern bus_param actBus_param;
    extern bus_close actBus_close;
    extern QueueHandle_t protocolQueue;
    extern QueueHandle_t outputQueue;
    extern QueueHandle_t inputQueue;
    MsgData *msg;
    MsgData *ownMsg;
    param_data *args;

    extern SemaphoreHandle_t protocollBinarySemaphore;
    UBaseType_t msgType;
    UBaseType_t timeout = 0;
    UBaseType_t showBusTransfer = 0;
    int i;
    //catch the "Protocoll is running" Semaphore
    xSemaphoreTake(protocollBinarySemaphore, portMAX_DELAY);

    DEBUGPRINT("Start Bus nr %ld\n", busToUse);
    /* activate the bus... */
    odbarr[busToUse] ();
    actBus_init();
    ODPBuffer *protocolBuffer;
    protocolBuffer = NULL;
    // start with the protocol specific initalisation
//<<<< oobdtemple protocol initmain <<<<
    extern print_cbf printdata_CAN;
    UBaseType_t sequenceCounter;
    UBaseType_t remainingBytes;
    UBaseType_t actBufferPos;
    UBaseType_t actFrameLen;
    UBaseType_t separationTime_ST = 0;
    UBaseType_t actBlockSize_BS = 0;
    UBaseType_t actSeparationTime_STTicks = 0;
    UBaseType_t stateMachine_state = 0;
    unsigned char telegram[8];
    struct TPElement *tpList = NULL;	//!< keeps the list of testerPresents
    /* tell the Rx-ISR about the function to use for received data */
    busControl(ODB_CMD_RECV, odp_uds_recvdata);
    protocolBuffer = createODPBuffer(UDSSIZE);
    if (protocolBuffer == NULL) {
	keeprunning = 0;
    } else {
	protocolBuffer->len = 0;
    }
    extern protocolConfigPtr actProtConfigPtr;
    struct UdsConfig *protocolConfig;
    protocolConfig = pvPortMalloc(sizeof(struct UdsConfig));
    if (protocolConfig == NULL) {
	keeprunning = 0;
    } else {
	actProtConfigPtr = protocolConfig;
	/* Init default parameters */
	protocolConfig->recvID = 0x7DF;
	protocolConfig->sendID = 0x00;	// 0 disables special sendID
	protocolConfig->timeout = 6;
	protocolConfig->timeoutPending = 150;
	protocolConfig->blockSize = 0;
	protocolConfig->separationTime = 0;
	protocolConfig->tpFreq = 250;
	protocolConfig->tpType = 0x80;
    }
//>>>> oobdtemple protocol mainloop_start  >>>>    
    for (; keeprunning;) {

	if (MSG_NONE != (msgType = waitMsg(protocolQueue, &msg, portMAX_DELAY)))	// portMAX_DELAY
	    /* handle message */
	{
	    switch (msgType) {
//<<<< oobdtemple protocol mainloop_start <<<<
//>>>> oobdtemple protocol MSG_BUS_RECV  >>>>    
	    case MSG_BUS_RECV:
		dp = msg->addr;
//<<<< oobdtemple protocol MSG_BUS_RECV <<<<
		if (showBusTransfer > 0) {
		    odp_uds_dumpFrame(dp, printdata_CAN);
		}
		if (((protocolConfig->sendID == 0 ? dp->recv == (protocolConfig->recvID | 8) : dp->recv == protocolConfig->sendID)) || protocolConfig->recvID == 0x7DF) {	/* Tester Address correct / we sendes a broadcast (protocolConfig->recvID==0x7DF)? */
		    if (dp->data[0] == 0x03 && dp->data[1] == 0x7f && dp->data[3] == 0x78)	//Response pending
		    {
			timeout = protocolConfig->timeoutPending;
		    } else {
			if (stateMachine_state == SM_UDS_WAIT_FOR_FC) {
			    if ((dp->data[0] & 0xF0) == 0x30) {	/* FlowControl */
				DEBUGPRINT("FlowControl received\n", 'a');
				/* as we now probably have to send many frames first before we receive any
				   new answer from the module, we have to disable the timeout as long as we've sent the last frame
				 */
				timeout = 0;
				//! \todo how to correctly support "wait" if LowNibble of PCI is 1?
				if (protocolConfig->blockSize == 0) {
				    actBlockSize_BS = dp->data[1];	/* take the block size out of the FC block */
				} else {
				    actBlockSize_BS = protocolConfig->blockSize;	/* use the config value instead the one from FC */
				}
				if (actBlockSize_BS > 0) {
				    actBlockSize_BS++;
				    DEBUGPRINT
					("Blocksize  received with %ld ticks\n",
					 actBlockSize_BS);
				}
				if (protocolConfig->separationTime == 0) {
				    separationTime_ST = dp->data[2];	/* take the separation time out of the FC block */
				} else {
				    separationTime_ST = protocolConfig->separationTime;	/* use the config value instead the one from FC */
				}
				if (separationTime_ST > 0) {
				    stateMachine_state =
					SM_UDS_SLEEP_UNTIL_SINGLE_CF;
				    actSeparationTime_STTicks =
					separationTime_ST /
					portTICK_PERIOD_MS;
				    actSeparationTime_STTicks++;
				    if (actSeparationTime_STTicks < 2) {
					actSeparationTime_STTicks = 2;
				    }
				    DEBUGPRINT
					("FlowControl Delay received with %d ticks\n",
					 actSeparationTime_STTicks);

				} else {
				    stateMachine_state = SM_UDS_SEND_CF;
				}
			    } else {	/* wrong answer */
				stateMachine_state = SM_UDS_STANDBY;
				protocolBuffer->len = 0;
				createCommandResultMsg
				    (FBID_PROTOCOL_GENERIC,
				     ERR_CODE_UDS_MISSING_FLOW_CONTROL,
				     (dp->data[0] & 0xF0),
				     ERR_CODE_UDS_MISSING_FLOW_CONTROL_TEXT);
			    }

			}
			if (stateMachine_state == SM_UDS_SEND_CF) {
			    /* Caution: This "if state" needs to be straight after
			       the Flow Control handling above, so that when the state 
			       SM_UDS_SEND_CF is reached, the state machine starts straight to send
			     */

			    //! \todo delayed, block wise sending of Consecutive frame still needs to be implemented
			    while (remainingBytes > 0) {
				DEBUGPRINT("Remaining bytes: %ld\n",
					   remainingBytes);
				actFrameLen =
				    remainingBytes >
				    7 ? 7 : remainingBytes;
				odp_uds_data2CAN(&protocolBuffer->data
						 [actBufferPos],
						 &telegram[0], actFrameLen,
						 1);
				sequenceCounter =
				    sequenceCounter <
				    15 ? sequenceCounter + 1 : 0;
				actBufferPos += actFrameLen;
				remainingBytes -= actFrameLen;
				actDataPacket.data[0] = 0x20 + sequenceCounter;	// prepare CF
				if (showBusTransfer > 0) {
				    odp_uds_dumpFrame(&actDataPacket,
						      printdata_CAN);
				}
				actBus_send(&actDataPacket);
			    }
			    stateMachine_state = SM_UDS_WAIT_FOR_ANSWER;
			    timeout = protocolConfig->timeout;
			}
			if (stateMachine_state == SM_UDS_WAIT_FOR_CF) {
			    if ((dp->data[0] & 0xF0) == 0x20) {	/* consecutive Frame */
				DEBUGPRINT
				    ("Consecutive Frame seq. %ld\n",
				     sequenceCounter);
				sequenceCounter =
				    sequenceCounter >
				    14 ? 0 : sequenceCounter + 1;
				if ((dp->data[0] & 0x0F) ==
				    sequenceCounter) {
				    DEBUGPRINT("Sequence ok seq. %ld\n",
					       sequenceCounter);
				    actFrameLen =
					remainingBytes >
					7 ? 7 : remainingBytes;
				    udp_uds_CAN2data(protocolBuffer,
						     &(dp->data[1]),
						     actBufferPos,
						     actFrameLen);
				    actBufferPos += actFrameLen;
				    remainingBytes -= actFrameLen;
				    timeout = protocolConfig->timeout;
				    DEBUGPRINT
					("actualBufferPos %ld remaining Bytes %ld\n",
					 actBufferPos, remainingBytes);
				    if (remainingBytes == 0) {	/* finished */
					stateMachine_state =
					    SM_UDS_STANDBY;
					timeout = 0;
					/* to dump the  buffer, we send the address of the udsbuffer to the print routine */
					ownMsg =
					    createMsg(&protocolBuffer, 0);
					/* add correct print routine; */
					ownMsg->print =
					    odp_uds_printdata_Buffer;
					// send event information to the ILM task
					CreateEventMsg
					    (MSG_EVENT_PROTOCOL_RECEIVED,
					     0);
					/* forward data to the output task */
					if (pdPASS !=
					    sendMsg(MSG_DUMP_BUFFER,
						    outputQueue, ownMsg)) {
					    DEBUGPRINT
						("FATAL ERROR: output queue is full!\n",
						 'a');
					}
				    }
				} else {	/* sequence error! */
				    stateMachine_state = SM_UDS_STANDBY;
				    createCommandResultMsg
					(FBID_PROTOCOL_GENERIC,
					 ERR_CODE_UDS_WRONG_SEQUENCE_COUNT,
					 (dp->data[0] & 0x0F),
					 ERR_CODE_UDS_WRONG_SEQUENCE_COUNT_TEXT);
				    DEBUGPRINT
					("Sequence Error! Received %d , expected %ld\n",
					 dp->data[0] & 0x0F,
					 sequenceCounter);
				    timeout = 0;
				    if (pdPASS !=
					sendMsg(MSG_SERIAL_RELEASE,
						inputQueue, NULL)) {
					DEBUGPRINT
					    ("FATAL ERROR: input queue is full!\n",
					     'a');

				    }
				}

			    } else {
				stateMachine_state = SM_UDS_STANDBY;
				createCommandResultMsg
				    (FBID_PROTOCOL_GENERIC,
				     ERR_CODE_UDS_MISSING_FIRST_FRAME,
				     (dp->data[0] & 0xF0),
				     ERR_CODE_UDS_MISSING_FIRST_FRAME_TEXT);
				DEBUGPRINT
				    ("Wrong Frame Error! Received %d , expected 0x2x\n",
				     dp->data[0]);
				timeout = 0;
				if (pdPASS !=
				    sendMsg(MSG_SERIAL_RELEASE,
					    inputQueue, NULL)) {
				    DEBUGPRINT
					("FATAL ERROR: input queue is full!\n",
					 'a');

				}
			    }
			}
			if (stateMachine_state == SM_UDS_WAIT_FOR_ANSWER) {
			    if ((dp->data[0] & 0xF0) == 0x10) {	/* FirstFrame */
				sequenceCounter = 0;	//first Frame counts as sequence 0 already
				remainingBytes =
				    (dp->data[0] & 0xF) * 256 +
				    dp->data[1];
				actBufferPos = 6;
				DEBUGPRINT("First Frame with %ld Bytes\n",
					   remainingBytes);
				protocolBuffer->len = remainingBytes;	/* set the buffer size alredy inhope, that all goes well ;-) */
				remainingBytes -= 6;	/* the first 6 bytes are already in the FF */
				udp_uds_CAN2data(protocolBuffer,
						 &(dp->data[2]), 0, 6);
				actDataPacket.recv =
				    protocolConfig->recvID;
				actDataPacket.data = &telegram[0];
				actDataPacket.len = 8;
				for (i = 0; i < 8; i++) {	/* just fill the telegram with 0 */
				    telegram[i] = 0;
				}
				telegram[0] = 0x30;	/* 0x30 = 3=>FlowControl, 0=>CTS = ContinoueToSend */
				stateMachine_state = SM_UDS_WAIT_FOR_CF;
				timeout = protocolConfig->timeout;
				if (showBusTransfer > 0) {
				    odp_uds_dumpFrame(&actDataPacket,
						      printdata_CAN);
				}
				actBus_send(&actDataPacket);
			    } else {
				if ((dp->data[0] & 0xF0) == 0x00) {	/*Single Frame */
				    DEBUGPRINT
					("Single Frame with %d Bytes\n",
					 dp->data[0]);
				    protocolBuffer->len = dp->data[0];
				    udp_uds_CAN2data(protocolBuffer,
						     &(dp->data[1]), 0,
						     dp->data[0]);
				    stateMachine_state = SM_UDS_STANDBY;
				    timeout = 0;
				    /* to dump the  buffer, we send the address of the udsbuffer to the print routine */
				    ownMsg =
					createMsg(&protocolBuffer,
						  sizeof(protocolBuffer));
				    /* add correct print routine; */
				    ownMsg->print =
					odp_uds_printdata_Buffer;
				    // send event information to the ILM task
				    CreateEventMsg
					(MSG_EVENT_PROTOCOL_RECEIVED, 0);
				    /* forward data to the output task */
				    if (pdPASS !=
					sendMsg(MSG_DUMP_BUFFER,
						outputQueue, ownMsg)) {
					DEBUGPRINT
					    ("FATAL ERROR: output queue is full!\n",
					     'a');

				    }
				}
			    }
			}
		    }
		}
//>>>> oobdtemple protocol MSG_SERIAL_DATA  >>>>    
		break;
	    case MSG_SERIAL_DATA:
		if (stateMachine_state == SM_UDS_STANDBY) {	/* only if just nothing to do */
		    dp = (data_packet *) msg->addr;
		    // data block received from serial input which need to be handled now
//<<<< oobdtemple protocol MSG_SERIAL_DATA <<<<
		    if (((protocolBuffer->len) + dp->len) <= UDSSIZE) {
			/* copy the data into the uds- buffer */
			for (i = 0; i < dp->len; i++) {
			    protocolBuffer->data[protocolBuffer->len++] =
				dp->data[i];
			}
		    } else {
			createCommandResultMsg(FBID_PROTOCOL_GENERIC,
					       ERR_CODE_UDS_DATA_TOO_LONG_ERR,
					       (protocolBuffer->len) +
					       dp->len,
					       ERR_CODE_UDS_DATA_TOO_LONG_ERR_TEXT);
		    }
		}
//>>>> oobdtemple protocol MSG_SERIAL_PARAM_1 >>>>    
		break;
	    case MSG_SERIAL_PARAM:
		args = (UBaseType_t *) msg->addr;
		DEBUGPRINT("protocol parameter received %ld %ld %ld\n",
			   args->args[ARG_RECV], args->args[ARG_CMD],
			   args->args[ARG_VALUE_1]);

		switch (args->args[ARG_RECV]) {
		case FBID_PROTOCOL_GENERIC:
		    DEBUGPRINT
			("generic protocol parameter received %ld %ld\n",
			 args->args[ARG_CMD], args->args[ARG_VALUE_1]);
		    switch (args->args[ARG_CMD]) {
		    case PARAM_INFO:
//<<<< oobdtemple protocol MSG_SERIAL_PARAM_1 <<<<
			CreateParamOutputMsg(args, odp_uds_printParam);
//>>>> oobdtemple protocol MSG_SERIAL_PARAM_2 >>>>    
			break;
			// and here we proceed all command parameters
		    case PARAM_LISTEN:
			showBusTransfer = args->args[ARG_VALUE_1];
			createCommandResultMsg(FBID_PROTOCOL_GENERIC,
					       ERR_CODE_NO_ERR, 0, NULL);
			break;
		    default:
			createCommandResultMsg(FBID_PROTOCOL_GENERIC,
					       ERR_CODE_OS_UNKNOWN_COMMAND,
					       0,
					       ERR_CODE_OS_UNKNOWN_COMMAND_TEXT);
			break;
		    }
		    break;
//<<<< oobdtemple protocol MSG_SERIAL_PARAM_2 <<<<
		case FBID_PROTOCOL_SPEC:
		    DEBUGPRINT("uds protocol parameter received %ld %ld\n",
			       args->args[ARG_CMD],
			       args->args[ARG_VALUE_1]);
		    switch (args->args[ARG_CMD]) {
			// first we commend out all parameters  which are not used to generate the right "unknown parameter" message in the default - area
			/*
			   case PARAM_ECHO:
			   break;
			   case PARAM_TIMEOUT_PENDING:
			   break;
			   case PARAM_BLOCKSIZE:
			   break;
			   case PARAM_FRAME_DELAY:
			   break;
			 */
		    case PARAM_TIMEOUT:
			protocolConfig->timeout =
			    args->args[ARG_VALUE_1] + 1;
			createCommandResultMsg(FBID_PROTOCOL_SPEC,
					       ERR_CODE_NO_ERR, 0, NULL);
			break;
		    case PARAM_RECVID:
			protocolConfig->recvID = args->args[ARG_VALUE_1];
			createCommandResultMsg(FBID_PROTOCOL_SPEC,
					       ERR_CODE_NO_ERR, 0, NULL);
			break;
		    case PARAM_SENDID:
			protocolConfig->sendID = args->args[ARG_VALUE_1];
			createCommandResultMsg(FBID_PROTOCOL_SPEC,
					       ERR_CODE_NO_ERR, 0, NULL);
			break;
		    case PARAM_TP_ON:
			if (odp_uds_addTesterPresents(&tpList,
						      args->args
						      [ARG_VALUE_1],
						      protocolConfig->
						      tpFreq,
						      protocolConfig->
						      tpType)) {
			    createCommandResultMsg(FBID_PROTOCOL_SPEC,
						   ERR_CODE_NO_ERR, 0,
						   NULL);
			} else {
			    createCommandResultMsg(FBID_PROTOCOL_SPEC,
						   ERR_CODE_UDS_TP_OOM,
						   0,
						   ERR_CODE_UDS_TP_OOM_TEXT);
			}
			break;
		    case PARAM_TP_OFF:
			odp_uds_deleteTesterPresents(&tpList,
						     args->args
						     [ARG_VALUE_1]);
			createCommandResultMsg(FBID_PROTOCOL_SPEC,
					       ERR_CODE_NO_ERR, 0, NULL);
			break;
		    case PARAM_TP_TYPE:
			protocolConfig->tpType = args->args[ARG_VALUE_1];
			createCommandResultMsg(FBID_PROTOCOL_SPEC,
					       ERR_CODE_NO_ERR, 0, NULL);
			break;
		    case PARAM_TP_FREQ:
			protocolConfig->tpFreq = args->args[ARG_VALUE_1];
			createCommandResultMsg(FBID_PROTOCOL_SPEC,
					       ERR_CODE_NO_ERR, 0, NULL);
			break;
			createCommandResultMsg(FBID_PROTOCOL_SPEC,
					       ERR_CODE_NO_ERR, 0, NULL);
		    default:
			createCommandResultMsg(FBID_PROTOCOL_SPEC,
					       ERR_CODE_OS_UNKNOWN_COMMAND,
					       0,
					       ERR_CODE_OS_UNKNOWN_COMMAND_TEXT);
			break;
		    }
		    break;
//>>>> oobdtemple protocol MSG_OTHERS >>>>    
		case FBID_BUS_GENERIC:
		case FBID_BUS_SPEC:
		    actBus_param(args);	/* forward the received params to the underlying bus. */
		    break;
		default:
		    createCommandResultMsg(FBID_PROTOCOL_SPEC,
					   ERR_CODE_OS_UNKNOWN_COMMAND,
					   0,
					   ERR_CODE_OS_UNKNOWN_COMMAND_TEXT);
		    break;
		}
//<<<< oobdtemple protocol MSG_OTHERS <<<<
//>>>> oobdtemple protocol MSG_INIT >>>>    
	    case MSG_INIT:
		DEBUGPRINT("Reset Protocol\n", 'a');
		if (protocolBuffer != NULL) {
		    protocolBuffer->len = 0;
		}
//<<<< oobdtemple protocol MSG_INIT <<<<
//>>>> oobdtemple protocol MSG_PROTOCOL_STOP >>>>    
		break;
	    case MSG_PROTOCOL_STOP:
		DEBUGPRINT("Stop Protocol\n", 'a');
		keeprunning = 0;
		break;
//<<<< oobdtemple protocol MSG_PROTOCOL_STOP <<<<
//>>>> oobdtemple protocol MSG_SEND_BUFFER >>>>    
	    case MSG_SEND_BUFFER:
		/* let's Dance: Starting the transfer protocol */
//<<<< oobdtemple protocol MSG_SEND_BUFFER <<<<
		if (protocolBuffer->len > 0) {
		    actDataPacket.recv = protocolConfig->recvID;
		    actDataPacket.data = &telegram;
		    actDataPacket.len = 8;
		    if (protocolBuffer->len < 8) {	/* its just single frame */
			odp_uds_data2CAN(&protocolBuffer->data[0],
					 &telegram, protocolBuffer->len,
					 1);
			actDataPacket.data[0] = protocolBuffer->len;
			protocolBuffer->len = 0;	/* prepare buffer to receive */
			actBufferPos = 0;
			if (showBusTransfer > 0) {
			    odp_uds_dumpFrame(&actDataPacket,
					      printdata_CAN);
			}
			actBus_send(&actDataPacket);
			stateMachine_state = SM_UDS_WAIT_FOR_ANSWER;
			timeout = protocolConfig->timeout;
		    } else {	/* we have to send multiframes */
			odp_uds_data2CAN(&protocolBuffer->data[0],
					 &telegram, 6, 2);
			actDataPacket.data[0] = 0x10 + (protocolBuffer->len / 256);	/* prepare FF */
			actDataPacket.data[1] = protocolBuffer->len % 256;
			sequenceCounter = 0;
			remainingBytes = protocolBuffer->len - 6;
			actBufferPos = 6;
			protocolBuffer->len = 0;	/* prepare buffer to receive */
			if (showBusTransfer > 0) {
			    odp_uds_dumpFrame(&actDataPacket,
					      printdata_CAN);
			}
			actBus_send(&actDataPacket);
			stateMachine_state = SM_UDS_WAIT_FOR_FC;
			timeout = protocolConfig->timeout;
		    }
//>>>> oobdtemple protocol MSG_SEND_BUFFER_2 >>>>    

		} else {	/* no data to send? */
		    createCommandResultMsg
			(FBID_PROTOCOL_GENERIC, ERR_CODE_NO_ERR, 0, NULL);
		    DEBUGPRINT("Send input task release msg\n", 'a');
		    /* just release the input again */
		    if (pdPASS !=
			sendMsg(MSG_SERIAL_RELEASE, inputQueue, NULL)) {
			DEBUGPRINT
			    ("FATAL ERROR: input queue is full!\n", 'a');
		    }
		}
		break;
//<<<< oobdtemple protocol MSG_SEND_BUFFER_2 <<<<
//>>>> oobdtemple protocol MSG_TICK >>>>    
	    case MSG_TICK:
//<<<< oobdtemple protocol MSG_TICK <<<<
		if (timeout > 0) {	/* we just waiting for an answer */
		    if (timeout == 1) {	/* time's gone... */
			protocolBuffer->len = 0;
			DEBUGPRINT("Timeout!\n", 'a');
			createCommandResultMsg(FBID_PROTOCOL_GENERIC,
					       ERR_CODE_UDS_TIMEOUT, 0,
					       ERR_CODE_UDS_TIMEOUT_TEXT);
			stateMachine_state = SM_UDS_STANDBY;
			if (pdPASS !=
			    sendMsg(MSG_SERIAL_RELEASE, inputQueue,
				    NULL)) {
			    DEBUGPRINT
				("FATAL ERROR: input queue is full!\n",
				 'a');
			}
		    }
		    timeout--;
		}
		if (actSeparationTime_STTicks > 0) {
		    DEBUGPRINT
			("Remaining CF Waitticks: %ld , remainingBytes: %ld\n",
			 actSeparationTime_STTicks, remainingBytes);
		    stateMachine_state = SM_UDS_SLEEP_UNTIL_SINGLE_CF;
		    actSeparationTime_STTicks--;
		    if (actSeparationTime_STTicks < 1) {	//it's time for a new single CF
			stateMachine_state = SM_UDS_SEND_SINGLE_CF;
			actSeparationTime_STTicks = separationTime_ST / portTICK_PERIOD_MS;	//"reload" the counter
			actSeparationTime_STTicks++;
			if (actSeparationTime_STTicks < 2) {
			    actSeparationTime_STTicks = 2;
			}
			DEBUGPRINT
			    ("Reloaded CF Waitticks: %ld , remainingBytes: %ld\n",
			     actSeparationTime_STTicks, remainingBytes);
		    }
		}

		/* Start generating tester present messages */
		odp_uds_generateTesterPresents(tpList,
					       &telegram, actBus_send);
//>>>> oobdtemple protocol final >>>>    
		break;
	    }
	    //if (Ticker oder sonstiges Consecutife Frame){
	    if (1) {
		if (stateMachine_state == SM_UDS_SEND_CF
		    || stateMachine_state == SM_UDS_SEND_SINGLE_CF) {
		    while (remainingBytes > 0
			   && (stateMachine_state !=
			       SM_UDS_SLEEP_UNTIL_SINGLE_CF)
			   && (actBlockSize_BS != 1)) {
			if (stateMachine_state == SM_UDS_SEND_SINGLE_CF) {
			    stateMachine_state =
				SM_UDS_SLEEP_UNTIL_SINGLE_CF;
			}
			DEBUGPRINT("Remaining bytes: %ld\n",
				   remainingBytes);
			actFrameLen =
			    remainingBytes > 7 ? 7 : remainingBytes;
			odp_uds_data2CAN(&protocolBuffer->data
					 [actBufferPos],
					 &telegram[0], actFrameLen, 1);
			sequenceCounter =
			    sequenceCounter < 15 ? sequenceCounter + 1 : 0;
			actBufferPos += actFrameLen;
			remainingBytes -= actFrameLen;
			actDataPacket.data[0] = 0x20 + sequenceCounter;	// prepare CF
			if (showBusTransfer > 0) {
			    odp_uds_dumpFrame(&actDataPacket,
					      printdata_CAN);
			}
			actBus_send(&actDataPacket);
			if (actBlockSize_BS > 1) {
			    actBlockSize_BS--;
			    DEBUGPRINT("Blocksize  REDUCED to %ld \n",
				       actBlockSize_BS);

			}
		    }
		    if (actBlockSize_BS == 1) {	//in case we had some block limitations, send them and then wait for another FC Frame
			stateMachine_state = SM_UDS_WAIT_FOR_FC;
			actBlockSize_BS = 0;
			timeout = protocolConfig->timeout;
		    }
		    if (remainingBytes < 1) {	// Buffer empty?  Then finish
			stateMachine_state = SM_UDS_WAIT_FOR_ANSWER;
			actSeparationTime_STTicks = 0;
			timeout = protocolConfig->timeout;
		    }
		}
	    }
	    disposeMsg(msg);
	}



	/* vTaskDelay (5000 / portTICK_PERIOD_MS); */

    }

    /* Do all cleanup here to finish task */
    actBus_close();
    vPortFree(protocolConfig);
    freeODPBuffer(protocolBuffer);
    odp_uds_freeTPBuffers(tpList);
    xSemaphoreGive(protocollBinarySemaphore);
    vTaskDelete(NULL);
}
Exemplo n.º 13
0
/**
 * Should allocate a pbuf and transfer the bytes of the incoming
 * packet from the interface into the pbuf.
 *
 * @param netif the lwip network interface structure for this ethernetif
 * @return a pbuf filled with the received packet (including MAC header)
 *         NULL on memory error
 */
static struct pbuf *low_level_input(struct netif *netif)
{
  struct pbuf             *p = NULL;
  struct pbuf             *q;
  u16_t                   len;
#ifdef FREERTOS_USED
  static xSemaphoreHandle xRxSemaphore = NULL;
#endif


  /* Parameter not used. */
  ( void ) netif;

#ifdef FREERTOS_USED
  if( xRxSemaphore == NULL )
  {
    vSemaphoreCreateBinary( xRxSemaphore );
  }

  /* Access to the MACB is guarded using a semaphore. */
  if( xSemaphoreTake( xRxSemaphore, netifGUARD_BLOCK_NBTICKS ) )
  {
#endif
    /* Obtain the size of the packet. */
    len = ulMACBInputLength();

    if( len )
    {
#if ETH_PAD_SIZE
      len += ETH_PAD_SIZE;    /* allow room for Ethernet padding */
#endif

      /* We allocate a pbuf chain of pbufs from the pool. */
      p = pbuf_alloc( PBUF_RAW, len, PBUF_POOL );

      if( p != NULL )
      {
#if ETH_PAD_SIZE
        pbuf_header( p, -ETH_PAD_SIZE );    /* drop the padding word */
#endif

        /* Let the driver know we are going to read a new packet. */
        vMACBRead( NULL, 0, len );

        /* We iterate over the pbuf chain until we have read the entire
        packet into the pbuf. */
        for( q = p; q != NULL; q = q->next )
        {
          /* Read enough bytes to fill this pbuf in the chain. The
          available data in the pbuf is given by the q->len variable. */
          vMACBRead( q->payload, q->len, len );
        }

#if ETH_PAD_SIZE
        pbuf_header( p, ETH_PAD_SIZE );     /* reclaim the padding word */
#endif
        LINK_STATS_INC(link.recv);
      }
      else
      {
        LINK_STATS_INC(link.memerr);
        LINK_STATS_INC(link.drop);
      }
    }
#ifdef FREERTOS_USED
    xSemaphoreGive( xRxSemaphore );
  }
#endif

  return p;
}
/*------------------------------------------------------------------------------
 * Function: drawTask
 *
 * Description: This task sends the appropriate commands to update the game
 *  graphics every 10 milliseconds for a target frame rate of 100 FPS. It also
 *  checks collisions and performs the proper action based on the types of the
 *  colliding objects.
 *
 * param vParam: This parameter is not used.
 *----------------------------------------------------------------------------*/
void drawTask(void *vParam) {
	object *objIter, *objPrev;
   wall *wallIter, *wallPrev;
	xSpriteHandle hit, handle;
	point topLeft, botRight;
	uint8_t game_status = IN_PLAY;
	
	vTaskSuspend(updateTaskHandle);
	vTaskSuspend(bulletTaskHandle);
	vTaskSuspend(inputTaskHandle);
	init();
	vTaskResume(updateTaskHandle);
	vTaskResume(bulletTaskHandle);
	vTaskResume(inputTaskHandle);
	
	for (;;) {
		xSemaphoreTake(usartMutex, portMAX_DELAY);
		if (uCollide(ship1.handle, wallGroup, &hit, 1) > 0) {
   		wallPrev = NULL;
   		wallIter = walls;
   		while (wallIter != NULL) {
            if (wallIter->handle == hit) {
      		   topLeft = wallIter->topLeft;
      		   botRight = wallIter->botRight;
      		   //checks collision on x-axis
      		   if (ship1.pos.x > topLeft.x && ship1.pos.x < botRight.x) {
         		   if (abs(ship1.pos.y - topLeft.y) < abs(ship1.pos.y - botRight.y))
         		      ship1.pos.y -= WALL_BOUNCE;
         		   else
         		      ship1.pos.y += WALL_BOUNCE;
      		   }
      		   
      		   //checks collision on y-axis
      		   if (ship1.pos.y > topLeft.y && ship1.pos.y < botRight.x) {
         		   if (abs(ship1.pos.x - topLeft.x) < abs(ship1.pos.x - botRight.x))
         		      ship1.pos.x -= WALL_BOUNCE;
         		   else
         		      ship1.pos.x += WALL_BOUNCE;
               }               
               ship1.vel.x = 0;
               ship1.vel.y = 0;
               ship1.accel = 0;
               ship1.a_vel = 0;
               
               break;
            } else {
               wallPrev = wallIter;
               wallIter = wallIter->next;
            }
         }
      }
		vSpriteSetRotation(ship1.handle, (uint16_t)ship1.angle);
		vSpriteSetPosition(ship1.handle, (uint16_t)ship1.pos.x, (uint16_t)ship1.pos.y);
      
      if (uCollide(ship2.handle, wallGroup, &hit, 1) > 0) {
         wallPrev = NULL;
         wallIter = walls;
         while (wallIter != NULL) {
            if (wallIter->handle == hit) {
               topLeft = wallIter->topLeft;
               botRight = wallIter->botRight;
               //checks collision on x-axis
               if (ship2.pos.x > topLeft.x && ship2.pos.x < botRight.x) {
                  if (abs(ship2.pos.y - topLeft.y) < abs(ship2.pos.y - botRight.y))
                  ship2.pos.y -= WALL_BOUNCE;
                  else
                  ship2.pos.y += WALL_BOUNCE;
               }
         
               //checks collision on y-axis
               if (ship2.pos.y > topLeft.y && ship2.pos.y < botRight.x) {
                  if (abs(ship2.pos.x - topLeft.x) < abs(ship2.pos.x - botRight.x))
                  ship2.pos.x -= WALL_BOUNCE;
                  else
                  ship2.pos.x += WALL_BOUNCE;
               }
         
               ship2.vel.x = 0;
               ship2.vel.y = 0;
               ship2.accel = 0;
               ship2.a_vel = 0;
         
               break;
            } else {
               wallPrev = wallIter;
               wallIter = wallIter->next;
            }
         }
      }
      vSpriteSetRotation(ship2.handle, (uint16_t)ship2.angle);
      vSpriteSetPosition(ship2.handle, (uint16_t)ship2.pos.x, (uint16_t)ship2.pos.y);
      
      // Check hits from ship1
		objPrev = NULL;
		objIter = bullets_ship1;
		while (objIter != NULL) {
   		vSpriteSetPosition(objIter->handle, (uint16_t)objIter->pos.x, (uint16_t)objIter->pos.y);
         //// Check hits from ship1 on ship2
         if (uCollide(objIter->handle, shipGroup2, &hit, 1) > 0) {
      		vSpriteDelete(objIter->handle);
      		
      		if (objPrev != NULL) {
         		objPrev->next = objIter->next;
         		vPortFree(objIter);
         		objIter = objPrev->next;
      		}
            else {
         		bullets_ship1 = objIter->next;
         		vPortFree(objIter);
         		objIter = bullets_ship1;
      		}
            game_status = PLAYER_ONE_WIN;
		   }
         else if (uCollide(objIter->handle, wallGroup, &hit, 1) > 0) {
            vSpriteDelete(objIter->handle);
            
            if (objPrev != NULL) {
               objPrev->next = objIter->next;
               vPortFree(objIter);
               objIter = objPrev->next;
            }
            else {
               bullets_ship1 = objIter->next;
               vPortFree(objIter);
               objIter = bullets_ship1;
            }
         }
         else {
            objPrev = objIter;
            objIter = objIter->next;
         }
      }

      // Check hits from ship2
      objPrev = NULL;
      objIter = bullets_ship2;
      while (objIter != NULL) {
         vSpriteSetPosition(objIter->handle, (uint16_t)objIter->pos.x, (uint16_t)objIter->pos.y);
         //// Check hits from ship2 on ship1
         if (uCollide(objIter->handle, shipGroup1, &hit, 1) > 0) {
            vSpriteDelete(objIter->handle);
      
            if (objPrev != NULL) {
               objPrev->next = objIter->next;
               vPortFree(objIter);
               objIter = objPrev->next;
            }
            else {
               bullets_ship2 = objIter->next;
               vPortFree(objIter);
               objIter = bullets_ship2;
            }
            game_status = PLAYER_TWO_WIN;
         }
         else if (uCollide(objIter->handle, wallGroup, &hit, 1) > 0) {
            vSpriteDelete(objIter->handle);
            
            if (objPrev != NULL) {
               objPrev->next = objIter->next;
               vPortFree(objIter);
               objIter = objPrev->next;
            }
            else {
               bullets_ship2 = objIter->next;
               vPortFree(objIter);
               objIter = bullets_ship2;
            }
         }
         else {
            objPrev = objIter;
            objIter = objIter->next;
         }
      }

      switch(game_status)
      {
         case PLAYER_ONE_WIN:
            vTaskDelete(updateTaskHandle);
            vTaskDelete(bulletTaskHandle);
            vTaskDelete(inputTaskHandle);
            
            handle = xSpriteCreate("p1_win.png", SCREEN_W>>1, SCREEN_H>>1, 0, SCREEN_W>>1, SCREEN_H>>1, 100);
            
            vTaskDelay(3000 / portTICK_RATE_MS);
            
            vSpriteDelete(handle);
            reset();
            init();            
            
            xTaskCreate(inputTask, (signed char *) "p1", 80, NULL, 6, &inputTaskHandle);
            xTaskCreate(bulletTask, (signed char *) "b", 250, NULL, 2, &bulletTaskHandle);
            xTaskCreate(updateTask, (signed char *) "u", 200, NULL, 4, &updateTaskHandle);
            game_status = IN_PLAY;
            break;
         case PLAYER_TWO_WIN: 
            vTaskDelete(updateTaskHandle);
            vTaskDelete(bulletTaskHandle);
            vTaskDelete(inputTaskHandle);
            
            handle = xSpriteCreate("p2_win.png", SCREEN_W>>1, SCREEN_H>>1, 0, SCREEN_W>>1, SCREEN_H>>1, 100);
            
            vTaskDelay(3000 / portTICK_RATE_MS);

            vSpriteDelete(handle);
            reset();
            init();

            xTaskCreate(inputTask, (signed char *) "p1", 80, NULL, 6, &inputTaskHandle);
            xTaskCreate(bulletTask, (signed char *) "b", 250, NULL, 2, &bulletTaskHandle);
            xTaskCreate(updateTask, (signed char *) "u", 200, NULL, 4, &updateTaskHandle);
            game_status = IN_PLAY;
            break;
         default:
            break;
      }
		
		xSemaphoreGive(usartMutex);
		vTaskDelay(FRAME_DELAY_MS / portTICK_RATE_MS);
	}
}
Exemplo n.º 15
0
/**
 * \brief UART task
 *
 * This task runs in the background to handle the queued, incoming terminal
 * characters and write them to the terminal text buffer. It does not print
 * anything to the display -- that is done by \ref terminal_task().
 *
 * \param params Parameters for the task. (Not used.)
 */
static void uart_task(void *params)
{
    uint8_t *current_line_ptr;
    uint8_t *current_char_ptr;
    uint8_t current_column = 0;

    for (;;) {
        // Show that task is executing
        oled1_set_led_state(&oled1, OLED1_LED1_ID, true);

        // Grab terminal mutex
        xSemaphoreTake(terminal_mutex, portMAX_DELAY);

        current_line_ptr = terminal_buffer[terminal_line_offset];
        current_char_ptr = current_line_ptr + current_column;

        // Any characters queued? Handle them!
        while (xQueueReceive(terminal_in_queue, current_char_ptr, 0)) {
            /* Newline-handling is difficult because all terminal emulators
             * seem to do it their own way. The method below seems to work
             * with Putty and Realterm out of the box.
             */
            switch (*current_char_ptr) {
            case '\r':
                // Replace \r with \0 and move head to next line
                *current_char_ptr = '\0';

                current_column = 0;
                terminal_line_offset = (terminal_line_offset + 1)
                                       % TERMINAL_BUFFER_LINES;
                current_line_ptr = terminal_buffer[terminal_line_offset];
                current_char_ptr = current_line_ptr + current_column;
                break;

            case '\n':
                // For \n, do nothing -- it is replaced with \0 later
                break;

            default:
                // For all other characters, just move head to next char
                current_column++;
                if (current_column >= TERMINAL_COLUMNS) {
                    current_column = 0;
                    terminal_line_offset = (terminal_line_offset + 1)
                                           % TERMINAL_BUFFER_LINES;
                    current_line_ptr = terminal_buffer[terminal_line_offset];
                }
                current_char_ptr = current_line_ptr + current_column;
            }

            // Set zero-terminator at head
            *current_char_ptr = '\0';
        }

        xSemaphoreGive(terminal_mutex);

        oled1_set_led_state(&oled1, OLED1_LED1_ID, false);

        vTaskDelay(UART_TASK_DELAY);
    }
}
Exemplo n.º 16
0
/**
 * Update neighbor tables if necessary.
 *
 * Mac-layer use this function every time when received packet which LQI > 0.
 *
 * \param type indicates type of neighbor address mode
 * \param address neighbor address
 * \param lqi Last received LQI value
 * \param last_sqn last MAC sqn from this address
 *
 * \return 1 when last_sqn is different than current
 * \return 0 when sqn is same, now MAC discard packet
 */
uint8_t update_neighbour_table(addrtype_t type, address_t address, int8_t last_rssi, uint8_t last_sqn, uint8_t remove)
{
	neighbor_info_t *b;
	uint8_t i,j, sqn_check=0, length=0;
	dest_delivery_t delivery_mode;
	delivery_mode = NOT_NEIGHBOR;
	

	if( xSemaphoreTake( table_lock, ( portTickType ) 5 ) == pdTRUE )
	{
		if(type==ADDR_802_15_4_PAN_LONG)
		{
			length=8;					
		}
		if(type == ADDR_802_15_4_PAN_SHORT)
			length=4;

		delivery_mode = NOT_NEIGHBOR;
		if(neighbor_table.count > 0 && remove != ADD_CHILD)
		{
			for(i=0; i < MAX_NEIGHBOR_COUNT ; i++)
			{
				b = &(neighbor_table.neighbor_info[i]);
				if(b->type == ADDR_NONE)
					b=0;

				if(b && (type == b->type))
				{
					if(memcmp(b->address, address,length) == 0)
						delivery_mode = NEIGHBOR;
					
					/* Update lqi and compare sqn to old one */
					if( delivery_mode == NEIGHBOR )
					{
						if(type != ADDR_802_15_4_PAN_SHORT)
						{
							for(j=0; j<2; j++)
							{
								b->address[length+j] = address[length+j];
							}
						}
						if(remove == REMOVE_NEIGHBOUR)
						{
							if(b->child_dev)
								neighbor_table.child_count--;

							b->type=ADDR_NONE;
							i=neighbor_table.count;
							neighbor_table.count--;
						}
						else
						{
							/* Duplicated packet check */
							if(b->last_sqn != last_sqn)
							{
								b->last_sqn = last_sqn;
								sqn_check=1;
							}
							b->last_rssi = last_rssi;
							b->ttl=TTL;
						}
						i=MAX_NEIGHBOR_COUNT;
					}
				}
			}
		}
		/* Add new neighbor if addresstype is source */
		if((delivery_mode == NOT_NEIGHBOR && remove != REMOVE_NEIGHBOUR) && neighbor_table.count < MAX_NEIGHBOR_COUNT)
		{
			for(i=0; i<MAX_NEIGHBOR_COUNT; i++)
			{
				b = &(neighbor_table.neighbor_info[i]);
				if(b->type == ADDR_NONE)
				{
					i=MAX_NEIGHBOR_COUNT;
				}
			}

				if(type==ADDR_802_15_4_PAN_LONG)
						length+=2;

				for(j=0; j < length ; j++)
				{
					b->address[j] = address[j];
				}				
				/* add lqi value to neighbor */
				if(remove  == ADD_CHILD)
				{
					neighbor_table.child_count++;
					b->child_dev=1;
				}
				b->last_rssi =	last_rssi;
				b->last_sqn  =    last_sqn;
				b->child_dev =	0;
				sqn_check=1;
				b->ttl=TTL;
				b->type = type;
				/* Increace Neigbor count */
				neighbor_table.count++;
		}
		xSemaphoreGive( table_lock ); /*free lock*/
	}
	else
	{
		debug("No sem\r\n");
		sqn_check=1;
	}
	return sqn_check;
}
Exemplo n.º 17
0
void APP_MutexSPI0Give(void)
{
	xSemaphoreGive(xSPI0Semaphore);
	return;	
}
Exemplo n.º 18
0
child_status_type_t check_child_role(addrtype_t type, address_t address)
{
	neighbor_info_t *b;
	uint8_t i,j, length;
	child_status_type_t return_value;
	return_value = NOT_CHILD;
	
	if( xSemaphoreTake( table_lock, ( portTickType ) 5 ) == pdTRUE )
	{
		switch (type)
		{
			case ADDR_802_15_4_PAN_SHORT:		
				/* Check if broadcast address */
				length=4;
				break;
			case ADDR_802_15_4_SHORT:		
				/* Check if broadcast address */
				length=2;
				type=ADDR_802_15_4_PAN_SHORT;
				break;
			case ADDR_802_15_4_PAN_LONG:
				length=8;
				break;
			default:
				xSemaphoreGive( table_lock ); /*free lock*/
				return return_value;
				break;
		}
		if(neighbor_table.count > 0)
		{
			for(i=0; i < MAX_NEIGHBOR_COUNT ; i++)
			{
				b = &(neighbor_table.neighbor_info[i]);
				if(b->type == ADDR_NONE)
					b=0;

				if(b && (b->type == type) )
				{
					if(memcmp(b->address, address,length) == 0)
					{
						if(b->child_dev == 0)
						{
							neighbor_table.child_count++;
							b->child_dev=1;
						}
						return_value = CHILD;
						i=MAX_NEIGHBOR_COUNT;
					}
				}
			}
		}

		if((return_value==NOT_CHILD) && (neighbor_table.child_count == NWK_MAX_CHILD) )
		{
			return_value = DISCARD_ASSOC;

		}

		if((return_value==NOT_CHILD) && (neighbor_table.child_count < NWK_MAX_CHILD))
		{
			j =neighbor_table.child_count;
			j++;
				if(j == NWK_MAX_CHILD)
					return_value=NO_CAPASITY_AFTER_NEW_CHILD;
		}
		xSemaphoreGive( table_lock ); /*free lock*/
	}
return return_value;	
}
Exemplo n.º 19
0
//------------------------------------------------------------------------------------
size_t FreeRTOS_UART_write( Peripheral_Descriptor_t const pxPeripheral, const void *pvBuffer, const size_t xBytes )
{
	// Esta funcion debe poner los caracteres apuntados en pvBuffer en la cola de trasmision.
	// Actua como si fuese rprintfStr.
	// Debe tomar el semaforo antes de trasmitir. Los semaforos los manejamos en la capa FreeRTOS
	// y no en la de los drivers.

char cChar;
char *p;
size_t bytes2tx;
Peripheral_Control_t * const pxPeripheralControl = ( Peripheral_Control_t * const ) pxPeripheral;
UART_device_control_t *pUart;
size_t wBytes = 0;

	pUart = pxPeripheralControl->phDevice;
	// Controlo no hacer overflow en la cola de trasmision
	bytes2tx = xBytes;

	// Espero el semaforo en forma persistente.
	while ( xSemaphoreTake(pxPeripheralControl->xBusSemaphore, ( TickType_t ) 1 ) != pdTRUE )
		taskYIELD();

	// Trasmito.
	// Espero que los buffers esten vacios. ( La uart se va limpiando al trasmitir )
	if ( pUart->txBufferType == QUEUE ) {
		while  ( uxQueueMessagesWaiting( pUart->txStruct ) > 0 )
			taskYIELD();
	} else {
		while  ( uxFifoMessagesWaiting( pUart->txStruct ) > 0 )
			taskYIELD();
	}

	// Cargo el buffer en la cola de trasmision.
	p = (char *)pvBuffer;
	while (*p && (bytes2tx-- > 0) ) {

		// Voy cargando la cola de a uno.
		cChar = *p;
		pv_enqueue( pUart, &cChar );
		p++;
		wBytes++;	// Cuento los bytes que voy trasmitiendo

		// Si la cola esta llena, empiezo a trasmitir y espero que se vacie.
		if (  pv_queueReachHighWaterMark(pUart) ) {
			// Habilito a trasmitir para que se vacie
			vUartInterruptOn(pxPeripheralControl->portId);
			// Y espero que se haga mas lugar.
			while ( ! pv_queueReachLowWaterMark(pUart) )
				taskYIELD();
		}
	}

	// Luego inicio la trasmision invocando la interrupcion.
	vUartInterruptOn(pxPeripheralControl->portId);

	xSemaphoreGive( pxPeripheralControl->xBusSemaphore );

	//return xBytes;	// Puse todos los caracteres en la cola.
	return (wBytes);

}
Exemplo n.º 20
0
portCHAR update_routing_table(addrtype_t final_type, address_t final_destination,addrtype_t next_hop_type, address_t next_hop, uint8_t hop_count, int8_t last_rssi , uint8_t only_check)
{
	uint8_t i=0,j, tmp_8=0, final_length, next_hop_length, compare=0, update=0;
	route_info_t *ptr;
	if( xSemaphoreTake( table_lock, ( portTickType ) 5 ) == pdTRUE )
	{
		if(final_type==ADDR_802_15_4_PAN_LONG)
			final_length=8;
		else
			final_length=2;
		if(next_hop_type==ADDR_802_15_4_PAN_LONG)
			next_hop_length=8;
		else
			next_hop_length=4;

		tmp_8 = 0;
		/* Predict older route information and shuold use route */
		if(only_check != REMOVE_ROUTE)
		{
			switch	(check_time_stamp(final_type, final_destination))
			{
				case MESH_TTL_VALID:
					tmp_8=1;		/* cancel update process */
					break;
				case MESH_LOW_RSSI:
				case MESH_NOT_NEIGHBOR:
					only_check=0;
					break;
				default:
					break;
			}
		}

		if(routing_table.count > 0 && tmp_8==0)
		{
			for(i=0; i < MAX_ROUTE_INFO_COUNT ; i++)
			{
				ptr = &(routing_table.route_info[i]);
				if(ptr->dest_addr_type == ADDR_NONE)
					ptr=0;
				/* Check originator address from routing table */
				if(ptr && (final_type == ptr->dest_addr_type))
				{
					if(memcmp(ptr->destination, final_destination,final_length) ==0)
					{
						if(only_check == REMOVE_ROUTE)
						{
							ptr->dest_addr_type=ADDR_NONE;
							routing_table.count--;
						}
						else
						{
							if(next_hop_type==ptr->next_hop_addr_type)
							{
								/* compare next hop address */
								if(memcmp(next_hop, ptr->next_hop, next_hop_length) !=0)
									compare=1;
								else
									update=2;
							}
							else
								compare=1;
	
							if(compare)
							{
								if(hop_count < ptr->hop_count && last_rssi > -85)
								{
									update=1;	
								}
								else
								{
									if(hop_count==ptr->hop_count)
									{
										if(last_rssi > ptr->last_rssi || (ptr->ttl  < (ROUTING_TTL - 2)  ))
											update=1;
									}
								}
							}
							if(update)
							{
								if(update != 2)
								{
									ptr->next_hop_addr_type = next_hop_type;
									next_hop_length+=2;
									/* added new next hop info */
									for(j=0; j < next_hop_length ; j++)
									{
										ptr->next_hop[j] = next_hop[j];
									}
								}
								ptr->last_rssi=last_rssi;
								ptr->hop_count = hop_count;
								ptr->ttl=ROUTING_TTL;
							}
						}
						tmp_8=1;
						i=MAX_ROUTE_INFO_COUNT;
					}
				}	
			}
		}

		if(only_check==0 && (tmp_8==0 && routing_table.count < MAX_ROUTE_INFO_COUNT ))
		{
			//uint8_t count = routing_table.count;
			for(i=0; i<MAX_ROUTE_INFO_COUNT; i++)
			{
				ptr = &(routing_table.route_info[i]);
				if(ptr->dest_addr_type == ADDR_NONE)
				{
					i=MAX_ROUTE_INFO_COUNT;
				}
			}
			for(j=0; j < final_length ; j++)
			{
				ptr->destination[j] = final_destination[j];		
			}
			next_hop_length+=2;
			for(j=0; j < next_hop_length ; j++)
			{
				ptr->next_hop[j] = next_hop[j];
			}
			ptr->next_hop_addr_type = next_hop_type;
			ptr->dest_addr_type = final_type;

			ptr->hop_count = hop_count;
			ptr->ttl=ROUTING_TTL;
			ptr->last_rssi=last_rssi;
			routing_table.count++;
		}
		xSemaphoreGive( table_lock ); /*free lock*/
	}
return pdTRUE;
}
Exemplo n.º 21
0
static void prvCDCCommandConsoleTask( void *pvParameters )
{
char cRxedChar;
uint8_t ucInputIndex = 0;
char *pcOutputString;
static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];
portBASE_TYPE xReturned;

	( void ) pvParameters;

	/* Obtain the address of the output buffer.  Note there is no mutual
	exclusion on this buffer as it is assumed only one command console
	interface will be used at any one time. */
	pcOutputString = FreeRTOS_CLIGetOutputBuffer();

	/* Initialise the virtual com port (CDC) interface. */
	prvSetupUSBDrivers();

	/* Send the welcome message.  This probably won't be seen as the console
	will not have been connected yet. */
	USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcWelcomeMessage, strlen( pcWelcomeMessage ) );

	for( ;; )
	{
		/* No characters received yet for the current input string. */
		cRxedChar = 0;

		/* Only interested in reading one character at a time. */
		cRxedChar = cGetCDCChar();

		if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS )
		{
			/* Echo the character back. */
			USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) &cRxedChar, sizeof( uint8_t ) );

			/* Was it the end of the line? */
			if( cRxedChar == '\n' || cRxedChar == '\r' )
			{
				/* Just to space the output from the input. */
				USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcNewLine, strlen( pcNewLine ) );

				/* See if the command is empty, indicating that the last command is
				to be executed again. */
				if( ucInputIndex == 0 )
				{
					/* Copy the last command back into the input string. */
					strcpy( cInputString, cLastInputString );
				}

				/* Pass the received command to the command interpreter.  The
				command interpreter is called repeatedly until it returns pdFALSE
				(indicating there is no more output) as it might generate more than
				one string. */
				do
				{
					/* Get the next output string from the command interpreter. */
					xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );

					/* Write the generated string to the CDC. */
					USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcOutputString, strlen( pcOutputString ) );
					vTaskDelay( 1 );

				} while( xReturned != pdFALSE );

				/* All the strings generated by the input command have been sent.
				Clear the input	string ready to receive the next command.  Remember
				the command that was just processed first in case it is to be
				processed again. */
				strcpy( cLastInputString, cInputString );
				ucInputIndex = 0;
				memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );

				USB_WriteEP( CDC_DEP_IN, ( uint8_t * ) pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );
			}
			else
			{
				if( cRxedChar == '\r' )
				{
					/* Ignore the character. */
				}
				else if( cRxedChar == '\b' )
				{
					/* Backspace was pressed.  Erase the last character in the
					string - if any. */
					if( ucInputIndex > 0 )
					{
						ucInputIndex--;
						cInputString[ ucInputIndex ] = '\0';
					}
				}
				else
				{
					/* A character was entered.  Add it to the string
					entered so far.  When a \n is entered the complete
					string will be passed to the command interpreter. */
					if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )
					{
						if( ucInputIndex < cmdMAX_INPUT_SIZE )
						{
							cInputString[ ucInputIndex ] = cRxedChar;
							ucInputIndex++;
						}
					}
				}
			}

			/* Must ensure to give the mutex back. */
			xSemaphoreGive( xCDCMutex );
		}
	}
}
Exemplo n.º 22
0
void print_table_information(void)
{
	neighbor_info_t *b;
#ifdef HAVE_ROUTING
	uint8_t addres_length=0;
	route_info_t *ptr;
#endif
	if( xSemaphoreTake( table_lock, ( portTickType ) 10 ) == pdTRUE )
	{
		uint8_t i, j;
		if(neighbor_table.count)
		{
			debug("Neighbor Info count:");
			debug_hex(neighbor_table.count);
			debug("\r\n");
			debug("Child count:");
			debug_hex(neighbor_table.child_count);
			debug("\r\n");
			for(i=0; i < MAX_NEIGHBOR_COUNT; i++)
			{
				b=&(neighbor_table.neighbor_info[i]);
				if(b->type==ADDR_NONE)
					b=0;
				if(b)
				{
					if(b->type== ADDR_802_15_4_PAN_LONG)
					{
						debug("Long:  ");
						for(j=0; j < 2 ; j++)
						{
							if (j) debug_put(':');
							debug_hex( b->address[9-j]);
						}
						debug("  ");
						for(j=0; j < 8 ; j++)
						{
							if (j) debug_put(':');
							debug_hex( b->address[7-j]);
						}
						
					}
					if(b->type == ADDR_802_15_4_PAN_SHORT)
					{
						debug("Short:  ");
						for(j=0; j < 2 ; j++)
						{
							if (j) debug_put(':');
							debug_hex( b->address[3-j]);
						}
						debug("  ");
						for(j=0; j < 2 ; j++)
						{
							if (j) debug_put(':');
							debug_hex( b->address[1-j]);
						}
					}
					debug("\r\nrssi: ");
					debug_int(b->last_rssi);
					debug("\r\nTTL: ");
					debug_hex(b->ttl);
					debug("\r\n");
					pause_us(200);
				}
			}
		}
		else
		{
			debug("No Neighbor info\r\n");
		}
#ifdef HAVE_ROUTING
		if(routing_table.count)
		{
			
			debug("\r\nroute Info count:");
			debug_hex(routing_table.count);
			debug("\r\n");
			
			for(i=0; i < MAX_ROUTE_INFO_COUNT; i++)
			{
				ptr = &(routing_table.route_info[i]);
				if(ptr->dest_addr_type==ADDR_NONE)
					ptr=0;

				if(ptr)
				{
					debug("Dest:  ");
					if(ptr->dest_addr_type==ADDR_802_15_4_PAN_LONG)
						addres_length=8;
					else
						addres_length=2;

					for(j=0; j < addres_length ; j++)
					{
						if (j) debug_put(':');
						debug_hex(ptr->destination[(addres_length-1)-j]);
					}
					debug("\r\nNext hop:  ");
					if(ptr->next_hop_addr_type==ADDR_802_15_4_PAN_LONG)
						addres_length=10;
					else
						addres_length=4;

					for(j=0; j < addres_length ; j++)
					{
						if (j) debug_put(':');
						debug_hex(ptr->next_hop[(addres_length-1)-j]);
					}
		
					debug("\r\nrssi: ");
					debug_int(ptr->last_rssi);
					debug("\r\nHop count:  ");
					debug_hex(ptr->hop_count);
					debug("\r\nTTL: ");
					debug_hex(ptr->ttl);
					debug("\r\n");
				}
			}
		}
		else
		{
			debug("No route info\r\n");
		}
#else
		debug("Routing disable\r\n");
#endif
		xSemaphoreGive( table_lock ); /*free lock*/
	}
}
Exemplo n.º 23
0
void mutex_release_telemetry_string()
{
   if ( m_mutex != NULL){
      xSemaphoreGive(m_mutex);
   }
}
Exemplo n.º 24
0
 int wc_UnLockMutex(wolfSSL_Mutex* m)
 {
     xSemaphoreGive(m->mutex);
     return 0;
 }
Exemplo n.º 25
0
// Signals a semaphore
void
sys_sem_signal(sys_sem_t sem)
{
	xSemaphoreGive( sem );
}
Exemplo n.º 26
0
static void handle_received_frame(void) {
	uint8_t rx_length, length, *rx_ptr;

	// Take semaphore
	xSemaphoreTake(spi_mutex, portMAX_DELAY);

	// Check if there is at least one byte in fifo
	if (cc1101_status_rxbytes() == 0) {
		xSemaphoreGive(spi_mutex);
		restore_state();
		PRINTF("[PHY] no byte\n");
		return;
	}

	// Get length byte
	cc1101_fifo_get(&rx_length, 1);

	// Check length
	if (rx_length > PHY_MAX_LENGTH) {
		xSemaphoreGive(spi_mutex);
		restore_state();
		PRINTF("[PHY] length too big\n");
		return;
	}

	rx_data_length = rx_length;

	// Add 2 to the length for the status bytes
	rx_length += PHY_FOOTER_LENGTH;
	rx_ptr = rx_data;

	// Loop until end of packet
	while (cc1101_gdo0_read()) {
		// get the bytes in FIFO
		length = cc1101_status_rxbytes();

		// Check for overflow
		if (length & 0x80) {
			// Release semaphore
			xSemaphoreGive(spi_mutex);

			restore_state();
			PRINTF("[PHY] overflow\n");
			return;
		}

		// Check for local overflow
		if (length > rx_length) {
			// Release semaphore
			xSemaphoreGive(spi_mutex);
			restore_state();
			PRINTF("[PHY] local overflow\n");
			return;
		}

		// Read every byte but one, to prevent CC1101 bug.
		length -= 1;
		cc1101_fifo_get(rx_ptr, length);
		rx_ptr += length;
		rx_length -= length;

		// Wait until FIFO is filled above threshold, or EOP
		while (!cc1101_gdo2_read() && cc1101_gdo0_read()) {
			;
		}
	}

	// Packet complete, get the end
	length = cc1101_status_rxbytes();

	// Check for overflow
	if (length & 0x80) {
		// Release semaphore
		xSemaphoreGive(spi_mutex);
		restore_state();
		PRINTF("[PHY] overflow\n");
		return;
	}

	// Check for local overflow
	if (length > rx_length) {
		// Release semaphore
		xSemaphoreGive(spi_mutex);
		restore_state();
		PRINTF("[PHY] local overflow\n");
		return;
	}

	// Get the bytes
	cc1101_fifo_get(rx_ptr, length);
	rx_ptr += length;

	// Release semaphore
	xSemaphoreGive(spi_mutex);

	// Check CRC
	if ((rx_data[rx_data_length + 1] & 0x80) == 0) {
		// Bad CRC
		restore_state();
		PRINTF("[PHY] bad crc\n");
		return;
	}

	// Get RSSI
	int16_t rssi;
	rssi = rx_data[rx_data_length];
	if (rssi > 128) {
		rssi -= 256;
	}
	rssi -= 148;
	rssi /= 2;

	// Call callback function if any
	if (rx_cb) {
		rx_cb(rx_data, rx_data_length, (int8_t) rssi, sync_word_time);
	}

	// Restore state
	restore_state();
}
Exemplo n.º 27
0
static portTASK_FUNCTION( prvSemaphoreTest, pvParameters )
{
xSemaphoreParameters *pxParameters;
volatile uint32_t *pulSharedVariable, ulExpectedValue;
uint32_t ulCounter;
short sError = pdFALSE, sCheckVariableToUse;

	/* See which check variable to use.  sNextCheckVariable is not semaphore
	protected! */
	portENTER_CRITICAL();
		sCheckVariableToUse = sNextCheckVariable;
		sNextCheckVariable++;
	portEXIT_CRITICAL();

	/* A structure is passed in as the parameter.  This contains the shared
	variable being guarded. */
	pxParameters = ( xSemaphoreParameters * ) pvParameters;
	pulSharedVariable = pxParameters->pulSharedVariable;

	/* If we are blocking we use a much higher count to ensure loads of context
	switches occur during the count. */
	if( pxParameters->xBlockTime > ( TickType_t ) 0 )
	{
		ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;
	}
	else
	{
		ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;
	}

	for( ;; )
	{
		/* Try to obtain the semaphore. */
		if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )
		{
			/* We have the semaphore and so expect any other tasks using the
			shared variable to have left it in the state we expect to find
			it. */
			if( *pulSharedVariable != ulExpectedValue )
			{
				sError = pdTRUE;
			}

			/* Clear the variable, then count it back up to the expected value
			before releasing the semaphore.  Would expect a context switch or
			two during this time. */
			for( ulCounter = ( uint32_t ) 0; ulCounter <= ulExpectedValue; ulCounter++ )
			{
				*pulSharedVariable = ulCounter;
				if( *pulSharedVariable != ulCounter )
				{
					sError = pdTRUE;
				}
			}

			/* Release the semaphore, and if no errors have occurred increment the check
			variable. */
			if(	xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )
			{
				sError = pdTRUE;
			}

			if( sError == pdFALSE )
			{
				if( sCheckVariableToUse < semtstNUM_TASKS )
				{
					( sCheckVariables[ sCheckVariableToUse ] )++;
				}
			}

			/* If we have a block time then we are running at a priority higher
			than the idle priority.  This task takes a long time to complete
			a cycle	(deliberately so to test the guarding) so will be starving
			out lower priority tasks.  Block for some time to allow give lower
			priority tasks some processor time. */
			vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );
		}
		else
		{
			if( pxParameters->xBlockTime == ( TickType_t ) 0 )
			{
				/* We have not got the semaphore yet, so no point using the
				processor.  We are not blocking when attempting to obtain the
				semaphore. */
				taskYIELD();
			}
		}
	}
}
Exemplo n.º 28
0
/**
 * \brief Main demo task
 *
 * This task keeps track of which screen the user has selected, which tasks
 * to resume/suspend to draw the selected screen, and also draws the menu bar.
 *
 * The menu bar shows which screens the user can select by clicking the
 * corresponding buttons on the OLED1 Xplained Pro:
 * - \ref graph_task() "graph" (selected at start-up)
 * - \ref terminal_task() "term."
 * - \ref about_task() "about"
 *
 * \param params Parameters for the task. (Not used.)
 */
static void main_task(void *params)
{
    bool graph_buffer_initialized = false;
    bool selection_changed = true;
    bool select_graph_buffer;
    enum menu_items current_selection = MENU_ITEM_GRAPH;
    gfx_coord_t x, y, display_y_offset;
    xTaskHandle temp_task_handle = NULL;

    for(;;) {
        // Show that task is executing
        oled1_set_led_state(&oled1, OLED1_LED3_ID, true);

        // Check buttons to see if user changed the selection
        if (oled1_get_button_state(&oled1, OLED1_BUTTON1_ID)
                && (current_selection != MENU_ITEM_GRAPH)) {
            current_selection = MENU_ITEM_GRAPH;
            selection_changed = true;
        } else if (oled1_get_button_state(&oled1, OLED1_BUTTON2_ID)
                   && (current_selection != MENU_ITEM_TERMINAL)) {
            current_selection = MENU_ITEM_TERMINAL;
            selection_changed = true;
        } else if (oled1_get_button_state(&oled1, OLED1_BUTTON3_ID)
                   && (current_selection != MENU_ITEM_ABOUT)) {
            current_selection = MENU_ITEM_ABOUT;
            selection_changed = true;
        }

        // If selection changed, handle the selection
        if (selection_changed) {
            // Wait for and take the display semaphore before doing any changes.
            xSemaphoreTake(display_mutex, portMAX_DELAY);

            // We can now safely suspend the previously resumed task
            if (temp_task_handle) {
                vTaskSuspend(temp_task_handle);
                temp_task_handle = NULL;
            }

            // Select the new drawing task and corresponding display buffer
            switch (current_selection) {
            case MENU_ITEM_GRAPH:
                // Graph task runs continuously, no need to set task handle
                select_graph_buffer = true;
                break;

            case MENU_ITEM_TERMINAL:
                temp_task_handle = terminal_task_handle;
                select_graph_buffer = false;
                break;

            default:
            case MENU_ITEM_ABOUT:
                temp_task_handle = about_task_handle;
                select_graph_buffer = false;
            }

            // Select and initialize display buffer to use.
            display_y_offset = select_graph_buffer ? CANVAS_GRAPH_Y_OFFSET : 0;

            // Draw the menu bar (only needs to be done once for graph)
            if (!select_graph_buffer || !graph_buffer_initialized) {
                // Clear the selected display buffer first
                gfx_mono_draw_filled_rect(0, display_y_offset,
                                          GFX_MONO_LCD_WIDTH, GFX_MONO_LCD_HEIGHT / 2,
                                          GFX_PIXEL_CLR);

                // Draw menu lines, each item with height MENU_HEIGHT pixels
                y = display_y_offset + CANVAS_HEIGHT;
                gfx_mono_draw_horizontal_line(0, y, GFX_MONO_LCD_WIDTH,
                                              GFX_PIXEL_SET);

                x = MENU_ITEM_WIDTH;
                y++;

                for (uint8_t i = 0; i < (MENU_NUM_ITEMS - 1); i++) {
                    gfx_mono_draw_vertical_line(x, y, MENU_HEIGHT,
                                                GFX_PIXEL_SET);
                    x += 1 + MENU_ITEM_WIDTH;
                }

                // Highlight the current selection
                gfx_mono_draw_rect(current_selection * (1 + MENU_ITEM_WIDTH), y,
                                   MENU_ITEM_WIDTH, MENU_HEIGHT, GFX_PIXEL_SET);

                // Draw the menu item text
                x = (MENU_ITEM_WIDTH / 2) - ((5 * SYSFONT_WIDTH) / 2);
                y += (MENU_HEIGHT / 2) - (SYSFONT_HEIGHT / 2);

                for (uint8_t i = 0; i < MENU_NUM_ITEMS; i++) {
                    gfx_mono_draw_string(menu_items_text[i], x, y, &sysfont);
                    x += 1 + MENU_ITEM_WIDTH;
                }

                graph_buffer_initialized = true;
            }

            // Set display controller to output the new buffer
            ssd1306_set_display_start_line_address(display_y_offset);

            // We are done modifying the display, so give back the mutex
            xSemaphoreGive(display_mutex);

            selection_changed = false;

            // If a task handle was specified, resume it now
            if (temp_task_handle) {
                vTaskResume(temp_task_handle);
            }
        }

        // Show that task is done
        oled1_set_led_state(&oled1, OLED1_LED3_ID, false);

        vTaskDelay(MAIN_TASK_DELAY);
    }
}
Exemplo n.º 29
0
 int UnLockMutex(CyaSSL_Mutex* m)
 {
     xSemaphoreGive( *m );
     return 0;
 }
Exemplo n.º 30
0
/**
 * \ingroup freertos_usart_peripheral_control_group
 * \brief Initiate a completely multi-byte read operation on a USART peripheral.
 *
 * The FreeRTOS ASF USART driver uses the PDC to transfer data from a peripheral
 * to a circular buffer.  Reception happens in the background, while the
 * microcontroller is executing application code.* freertos_usart_read_packet()
 * copies bytes from the DMA buffer into the buffer passed as a
 * freertos_usart_read_packet() parameter.
 *
 * Readers are recommended to also reference the application note and examples
 * that accompany the FreeRTOS ASF drivers.
 *
 * The FreeRTOS ASF driver both installs and handles the USART PDC interrupts.
 * Users do not need to concern themselves with interrupt handling, and must
 * not install their own interrupt handler.
 *
 * \param p_usart    The handle to the USART port returned by the
 *     freertos_usart_serial_init() call used to initialise the port.
 * \param data    A pointer to the buffer into which received data is to be
 *     copied.
 * \param len    The number of bytes to copy.
 * \param block_time_ticks    Defines the maximum combined time the function
 *     will wait to get exclusive access to the peripheral and receive the
 *     requested number of bytes.  Other tasks will execute during any waiting
 *     time.
 *
 *     The FreeRTOS ASF USART driver is initialized using a
 *     call to freertos_usart_serial_init().  The
 *     freertos_driver_parameters.options_flags parameter passed to the
 *     initialization function defines the driver behavior.  If
 *     freertos_driver_parameters.options_flags had the USE_RX_ACCESS_MUTEX bit
 *     set, then the driver will only read from the USART buffer if it has
 *     first gained exclusive access to it.  block_time_ticks specifies the
 *     maximum amount of time the driver will wait to get exclusive access
 *     before aborting the read operation.
 *
 *     If the number of bytes available is less than the number requested then
 *     freertos_usart_serial_read_packet() will wait for more bytes to become
 *     available.  block_time_ticks specifies the maximum amount of time the
 *     driver will wait before returning fewer bytes than were requested.
 *
 *     block_time_ticks is specified in RTOS tick periods.  To specify a block
 *     time in milliseconds, divide the milliseconds value by portTICK_RATE_MS,
 *     and pass the result in  block_time_ticks.  portTICK_RATE_MS is defined by
 *     FreeRTOS.
 *
 * \return     The number of bytes that were copied into data.  This will be
 *     less than the requested number of bytes if a time out occurred.
 */
uint32_t freertos_usart_serial_read_packet(freertos_usart_if p_usart,
		uint8_t *data, uint32_t len, portTickType block_time_ticks)
{
	portBASE_TYPE usart_index, attempt_read;
	Usart *usart_base;
	xTimeOutType time_out_definition;
	uint32_t bytes_read = 0;

	usart_base = (Usart *) p_usart;
	usart_index = get_pdc_peripheral_details(all_usart_definitions,
			MAX_USARTS,
			(void *) usart_base);

	/* It is possible to initialise the peripheral to only use Tx and not Rx.
	Check that Rx has been initialised. */
	configASSERT(rx_buffer_definitions[usart_index].next_byte_to_read);
	configASSERT(rx_buffer_definitions[usart_index].next_byte_to_read !=
			RX_NOT_USED);

	/* Only do anything if the USART is valid. */
	if (usart_index < MAX_USARTS) {
		/* Must not request more bytes than will fit in the buffer. */
		if (len <=
				(rx_buffer_definitions[usart_index].past_rx_buffer_end_address
				- rx_buffer_definitions[usart_index].rx_buffer_start_address)) {
			/* Remember the time on entry. */
			vTaskSetTimeOutState(&time_out_definition);

			/* If an Rx mutex is in use, attempt to obtain it. */
			if (rx_buffer_definitions[usart_index].rx_access_mutex != NULL) {
				/* Attempt to obtain the mutex. */
				attempt_read = xSemaphoreTake(
						rx_buffer_definitions[usart_index].rx_access_mutex,
						block_time_ticks);

				if (attempt_read == pdTRUE) {
					/* The semaphore was obtained, adjust the block_time_ticks to take
					into account the time taken to obtain the semaphore. */
					if (xTaskCheckForTimeOut(&time_out_definition,
							&block_time_ticks) == pdTRUE) {
						attempt_read = pdFALSE;

						/* The port is not going to be used, so return the
						mutex now. */
						xSemaphoreGive(rx_buffer_definitions[usart_index].rx_access_mutex);
					}
				}
			} else {
				attempt_read = pdTRUE;
			}

			if (attempt_read == pdTRUE) {
				do {
					/* Wait until data is available. */
					xSemaphoreTake(rx_buffer_definitions[usart_index].rx_event_semaphore,
							block_time_ticks);

					/* Copy as much data as is available, up to however much
					a maximum of the total number of requested bytes. */
					bytes_read += freertos_copy_bytes_from_pdc_circular_buffer(
							&(rx_buffer_definitions[usart_index]),
							all_usart_definitions[usart_index].pdc_base_address->PERIPH_RPR,
							&(data[bytes_read]),
							(len - bytes_read));

					/* The Rx DMA will have stopped if the Rx buffer had become
					full before this read operation.  If bytes were removed by
					this read then there is guaranteed to be space in the Rx
					buffer and the Rx DMA can be restarted. */
					if (bytes_read > 0) {
						taskENTER_CRITICAL();
						{
							if(rx_buffer_definitions[usart_index].rx_pdc_parameters.ul_size == 0UL) {
								configure_rx_dma(usart_index, data_removed);
							}
						}
						taskEXIT_CRITICAL();
					}

				  /* Until all the requested bytes are received, or the function
				  runs out of time. */
				} while ((bytes_read < len) && (xTaskCheckForTimeOut(
						&time_out_definition,
						&block_time_ticks) == pdFALSE));

				if (rx_buffer_definitions[usart_index].rx_access_mutex != NULL) {
					/* Return the mutex. */
					xSemaphoreGive(rx_buffer_definitions[usart_index].rx_access_mutex);
				}
			}
		}
	}

	return bytes_read;
}