Example #1
0
File: hub75.c Project: emax73/HUB75
void hubInit(void) {
	initIO();
	calculatePeriod();
	hubLUTInit();
	initRows();
	hubOrientation = HUB_ROTATE_0;
	hubSetOrient(hubOrientation);
	clearScreen();
	//fillScreen(COLOR_RGB(255, 255, 255));
	hubTestBmp();
	screenRedraw();
	
	// 100 Hz * 16 Phases
  HUB_TIMER_CLK_ENABLE();
  TIM_ClockConfigTypeDef sClockSourceConfig;

  hubtim.Instance = HUB_TIMER;
  hubtim.Init.Prescaler = HUB_PRESCALER - 1;
  hubtim.Init.CounterMode = TIM_COUNTERMODE_UP;
  hubtim.Init.Period = period0 - 1;
  hubtim.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  HAL_TIM_Base_Init(&hubtim);

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  HAL_TIM_ConfigClockSource(&hubtim, &sClockSourceConfig);

	HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  HAL_NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);
	
	HAL_TIM_Base_Start_IT(&hubtim);
	
	// Timer 2 for NOE
	HUB_TIMER2_CLK_ENABLE();

  TIM_OC_InitTypeDef sConfigOC;

  hubtim2.Instance = HUB_TIMER2;
  hubtim2.Init.Prescaler = 0;
  hubtim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  hubtim2.Init.Period = 0xffff;
  hubtim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  HAL_TIM_Base_Init(&hubtim2);

  HAL_TIM_OC_Init(&hubtim2);

  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = period2Min;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  HAL_TIM_OC_ConfigChannel(&hubtim2, &sConfigOC, NOE_TIM_CHANNEL);

	HAL_TIM_PWM_Start(&hubtim2, NOE_TIM_CHANNEL); 

}
void CostAdaptiveStateManager::saveState(const VTime& currentTime, SimulationObject *object) {
   ObjectID *currentObjectID = object->getObjectID();
   unsigned int simObjectID = currentObjectID->getSimulationObjectID(); 

   // The period is only recalculated after the specified number of events.
   if (forwardExecutionLength[simObjectID] < eventsBetweenRecalculation[simObjectID]) {
      forwardExecutionLength[simObjectID]++;
   }
   else{
      calculatePeriod( object );
   }

   AdaptiveStateManagerBase::saveState(currentTime, object);
}
void ThreadedCostAdaptiveStateManager::saveState(const VTime& currentTime,
		SimulationObject *object, int threadID) {
	ObjectID *currentObjectID = object->getObjectID();
	unsigned int simObjectID = currentObjectID->getSimulationObjectID();

	// The period is only recalculated after the specified number of events.
	if (forwardExecutionLength[simObjectID]
			< eventsBetweenRecalculation[simObjectID]) {
		forwardExecutionLength[simObjectID]++;
	} else {
		calculatePeriod(object);
	}
	if (periodCounter[simObjectID] <= 1) {
		// we need to first allocate a state. Copy the current state into
		// this newly created state. Then allocate a state object and
		// fill in the current time and a pointer to the newly copied
		// current state.
		startStateTiming(simObjectID);

		State *newState = object->allocateState();
		newState->copyState(object->getState());
		SetObject<State> stateObject(currentTime, newState);

		// look up the state queue of the simulation object and insert the
		// newly allocated state object
		ThreadedStateManagerImplementationBase::getStateQueueLock(threadID,
				simObjectID);
		myStateQueue[simObjectID].insert(stateObject);
		/*cout << "Saving at the Time Stamp :::::"
		 << currentTime.getApproximateIntTime() << endl;*/
		ThreadedStateManagerImplementationBase::releaseStateQueueLock(threadID,
				simObjectID);

		// reset period counter to state period
		periodCounter[simObjectID] = objectStatePeriod[simObjectID];
		stopStateTiming(simObjectID);
	} else {
		// decrement period counter
		periodCounter[simObjectID] = periodCounter[simObjectID] - 1;
	}
}
void ThreadedCostAdaptiveStateManager::saveState(const VTime& currentTime,
		unsigned int eventNumber, SimulationObject *object,
		const ObjectID senderId, int threadID) {
	ObjectID *currentObjectID = object->getObjectID();
	unsigned int simObjectID = currentObjectID->getSimulationObjectID();

	// The period is only recalculated after the specified number of events.
	if (forwardExecutionLength[simObjectID]
			< eventsBetweenRecalculation[simObjectID]) {
		forwardExecutionLength[simObjectID]++;
	} else {
		calculatePeriod(object);
	}
	if (mySimulationManager->isRollbackJustCompleted(simObjectID)) {
		//	cout << "Last Rollback Time ::{{{{{{{{{{{{{{{{{}}}}}}}} "
		//			<< *(lastRollbackTime[simObjectID]) << endl;
		multiset<SetObject<State> >::iterator iter_end =
				myStateQueue[simObjectID].end();
		/*cout << "Current Num = " << eventNumber << ":::::  Old Number = "
		 << rollbackEventNumber[simObjectID] << endl;*/
		if (*(lastRollbackTime[simObjectID]) == currentTime && eventNumber
				<= rollbackEventNumber[simObjectID]) {
			//		cout << " Need to update the Event Number in the state ::::"
			//				<< eventNumber << endl;
			ThreadedStateManagerImplementationBase::getStateQueueLock(threadID,
					simObjectID);
			State *newState = object->allocateState();
			newState->copyState(object->getState());
			SetObject<State> stateObject(currentTime, newState, eventNumber,
					senderId.getSimulationObjectID(),
					senderId.getSimulationObjectID());
			//object->deallocateState((*iter_end).getElement());
			//myStateQueue[simObjectID].erase(iter_end);
			// look up the state queue of the simulation object and insert the
			// newly allocated state object

			myStateQueue[simObjectID].insert(stateObject);
			//		cout << "Saving at the Time Stamp :::::"
			//				<< currentTime.getApproximateIntTime() << endl;
			//		cout << "Imm. After savig the EventId ::::;"
			//				<< stateObject.getEventNumber() << endl;
			// *iter_end = stateObject;
			ThreadedStateManagerImplementationBase::releaseStateQueueLock(
					threadID, simObjectID);
		}
		mySimulationManager->resetRollbackCompletedStatus(simObjectID);
		return;
	}
	if (periodCounter[simObjectID] <= 0) {
		// we need to first allocate a state. Copy the current state into
		// this newly created state. Then allocate a state object and
		// fill in the current time and a pointer to the newly copied
		// current state.
		State *newState = object->allocateState();
		newState->copyState(object->getState());
		SetObject<State> stateObject(currentTime, newState, eventNumber,
				senderId.getSimulationObjectID(),
				senderId.getSimulationObjectID());

		// look up the state queue of the simulation object and insert the
		// newly allocated state object
		ThreadedStateManagerImplementationBase::getStateQueueLock(threadID,
				simObjectID);
		myStateQueue[simObjectID].insert(stateObject);
		//	cout << "Saving at the Time Stamp :::::"
		//			<< currentTime.getApproximateIntTime()
		//			<< "  With Event Number :::: " << eventNumber
		//			<< "  With senderObjectId :::: "
		//			<< senderId.getSimulationObjectID() << endl;

		multiset<SetObject<State> >::iterator iter_end =
				myStateQueue[simObjectID].end();
		iter_end--;
		/*cout << "Imm. After saving the EventId ::::::: "
		 << (*iter_end).getEventNumber() << endl;
		 cout << "Imm. After saving the SenderId ::::::: "
		 << (*iter_end).getsenderObjectId() << endl;*/

		ThreadedStateManagerImplementationBase::releaseStateQueueLock(threadID,
				simObjectID);
		// reset period counter to state period
		periodCounter[simObjectID] = objectStatePeriod[simObjectID];
	} else {
		// decrement period counter
		periodCounter[simObjectID] = periodCounter[simObjectID] - 1;
	}
}