Пример #1
0
static void REF_MeasureRaw(SensorTimeType raw[NOF_SENSORS]) {
  uint8_t cnt; /* number of sensor */
  uint8_t i;
  TMOUT1_CounterHandle timeout;

  FRTOS1_xSemaphoreTake(mutexHandle, portMAX_DELAY);
  if (ledON) {
    LED_IR_On(); /* IR LED's on */
    WAIT1_Waitus(200);
  }
  for(i=0;i<NOF_SENSORS;i++) {
    SensorFctArray[i].SetOutput(); /* turn I/O line as output */
    SensorFctArray[i].SetVal(); /* put high */
    raw[i] = MAX_SENSOR_VALUE;
  }
  WAIT1_Waitus(20); /* give at least 10 us to charge the capacitor */
  timeout = TMOUT1_GetCounter(20/TMOUT1_TICK_PERIOD_MS); /* set up timeout counter */
  FRTOS1_vTaskSuspendAll();
  
  (void)TU1_ResetCounter(timerHandle); /* reset timer counter */
  for(i=0;i<NOF_SENSORS;i++) {
    SensorFctArray[i].SetInput(); /* turn I/O line as input */
  }
  do {
    cnt = 0;
    if (TMOUT1_CounterExpired(timeout)) {
      break; /* get out of wile loop */
    }
    for(i=0;i<NOF_SENSORS;i++) {
      if (raw[i]==MAX_SENSOR_VALUE) { /* not measured yet? */
        if (SensorFctArray[i].GetVal()==0) {
          raw[i] = TU1_GetCounterValue(timerHandle);
        }
      } else { /* have value */
        cnt++;
      }
    }
  } while(cnt!=NOF_SENSORS);
  TMOUT1_LeaveCounter(timeout);
  FRTOS1_xTaskResumeAll();
  if (ledON) {
    LED_IR_Off(); /* IR LED's off */
    WAIT1_Waitus(200);
  }
  FRTOS1_xSemaphoreGive(mutexHandle);
}
Пример #2
0
static uint8_t Transfer(uint32_t src) {
  TMOUT1_CounterHandle handle;
  bool isTimeout;

  transferComplete = FALSE;
  DMA_PDD_SetSourceAddress(DMA_BASE_PTR, DMA_PDD_CHANNEL_0, src); /* set source address */
  DMA_PDD_SetByteCount(DMA_BASE_PTR, DMA_PDD_CHANNEL_0, NEO_DMA_NOF_BYTES); /* set number of bytes to transfer */
  DMA_PDD_EnablePeripheralRequest(DMA_BASE_PTR, DMA_PDD_CHANNEL_0, PDD_ENABLE); /* enable request from peripheral */
  handle = TMOUT1_GetCounter(100/TMOUT1_TICK_PERIOD_MS);
  isTimeout = FALSE;
  while(!transferComplete) {
    /* wait until transfer is complete */
    if (TMOUT1_CounterExpired(handle)) {
      isTimeout = TRUE;
      break; /* leave loop */
    }
  }
  TMOUT1_LeaveCounter(handle);
  if (isTimeout) {
    return ERR_BUSY;
  }
  return ERR_OK;
}
Пример #3
0
static uint8_t Transfer(uint32_t dataAddress, size_t nofBytes) {
  static const uint8_t OneValue = 0xFF; /* value to clear or set the port bits */
  TMOUT1_CounterHandle handle;
  bool isTimeout;
  uint32_t done0, done1, done2;

  /* clear any pending done flags for DMA channels */
  DMA_PDD_ClearDoneFlag(DMA_BASE_PTR, DMA_PDD_CHANNEL_0);
  DMA_PDD_ClearDoneFlag(DMA_BASE_PTR, DMA_PDD_CHANNEL_1);
  DMA_PDD_ClearDoneFlag(DMA_BASE_PTR, DMA_PDD_CHANNEL_2);
  /* set DMA source addresses */
  DMA_PDD_SetSourceAddress(DMA_BASE_PTR, DMA_PDD_CHANNEL_0, (uint32_t)&OneValue); /* set source address */
  DMA_PDD_SetSourceAddress(DMA_BASE_PTR, DMA_PDD_CHANNEL_1, dataAddress); /* set source address */
  DMA_PDD_SetSourceAddress(DMA_BASE_PTR, DMA_PDD_CHANNEL_2, (uint32_t)&OneValue); /* set source address */
  /* set byte count addresses */
  DMA_PDD_SetByteCount(DMA_BASE_PTR, DMA_PDD_CHANNEL_0, nofBytes); /* set number of bytes to transfer */
  DMA_PDD_SetByteCount(DMA_BASE_PTR, DMA_PDD_CHANNEL_1, nofBytes); /* set number of bytes to transfer */
  DMA_PDD_SetByteCount(DMA_BASE_PTR, DMA_PDD_CHANNEL_2, nofBytes); /* set number of bytes to transfer */
  /* reset TPM counter */
  TPM_PDD_InitializeCounter(TPM0_DEVICE); /* reset timer counter */
  TPM_PDD_ClearChannelFlags(TPM0_DEVICE, 0x00);
  TPM_PDD_ClearOverflowInterruptFlag(TPM0_DEVICE);
  /* re-enable DMA Muxing: it will disabled at the end of the transfer */
  DMAMUX_PDD_EnableChannel(DMAMUX0_BASE_PTR, 0, PDD_ENABLE);
  DMAMUX_PDD_EnableChannel(DMAMUX0_BASE_PTR, 1, PDD_ENABLE);
  DMAMUX_PDD_EnableChannel(DMAMUX0_BASE_PTR, 2, PDD_ENABLE);
  /* enable DMA peripheral requests */
  DMA_PDD_EnablePeripheralRequest(DMA_BASE_PTR, DMA_PDD_CHANNEL_2, PDD_ENABLE); /* enable request from peripheral */
  DMA_PDD_EnablePeripheralRequest(DMA_BASE_PTR, DMA_PDD_CHANNEL_1, PDD_ENABLE); /* enable request from peripheral */
  DMA_PDD_EnablePeripheralRequest(DMA_BASE_PTR, DMA_PDD_CHANNEL_0, PDD_ENABLE); /* enable request from peripheral */
  /* clear timer flags and status so it starts from a clean starting point */
  TPM_PDD_ClearChannelFlags(TPM0_DEVICE, 0x00);
  TPM_PDD_ClearOverflowInterruptFlag(TPM0_DEVICE);
  /* enable TPM DMA */
  TPM_PDD_WriteStatusControlReg(TPM0_DEVICE,TPM_PDD_ReadStatusControlReg(TPM0_DEVICE)|TPM_SC_DMA_MASK);
  TPM_PDD_EnableChannelDma(TPM0_DEVICE, 1);
  TPM_PDD_EnableChannelDma(TPM0_DEVICE, 0);
  /* start the TPM timer */
  StartTimer();

  isTimeout = FALSE;
  handle = TMOUT1_GetCounter(100/TMOUT1_TICK_PERIOD_MS);
  for(;;) {
    /* wait until transfer is complete */
    if (TMOUT1_CounterExpired(handle)) {
      isTimeout = TRUE;
      break; /* leave loop */
    }
    done0 = DMA_PDD_GetDoneFlag(DMA_BASE_PTR, DMA_PDD_CHANNEL_0);
    done1 = DMA_PDD_GetDoneFlag(DMA_BASE_PTR, DMA_PDD_CHANNEL_1);
    done2 = DMA_PDD_GetDoneFlag(DMA_BASE_PTR, DMA_PDD_CHANNEL_2);
    if (done0 && done1 && done2) {
      break; /* done! */
    }
    WAIT1_WaitOSms(1); /* give back some time */
  }
  TMOUT1_LeaveCounter(handle);
  WAIT1_Waitus(50); /* latch, low for at least 50 us (40x1.25us) */

  /* disable DMA-Muxing: necessary, otherwise DMA events on TPM0 channel 0 might be still latched.
   * Will enable muxing for next transfer */
  DMAMUX_PDD_EnableChannel(DMAMUX0_BASE_PTR, 0, PDD_DISABLE);
  DMAMUX_PDD_EnableChannel(DMAMUX0_BASE_PTR, 1, PDD_DISABLE);
  DMAMUX_PDD_EnableChannel(DMAMUX0_BASE_PTR, 2, PDD_DISABLE);
  /* disable peripheral DMA */
  TPM_PDD_WriteStatusControlReg(TPM0_DEVICE,TPM_PDD_ReadStatusControlReg(TPM0_DEVICE)&(~TPM_SC_DMA_MASK));
  TPM_PDD_DisableChannelDma(TPM0_DEVICE, 1);
  TPM_PDD_DisableChannelDma(TPM0_DEVICE, 0);

  StopTimer(); /* stop TPM */

  if (isTimeout) {
    return ERR_BUSY;
  }
  return ERR_OK;
}
Пример #4
0
void usb_run(void)
{
	usbmidi_scheduler_state_t usbschedstate;
	usbmidi_scheduler_state_t intercoreschedstate;
	midicmd_t cmd;

	usb_timeout_counter_handle = TMOUT1_GetCounter(2);

	midiin_reset(&midiin1_state);
	midiin1_state.GetCharsInRxBuf = MIDIUART1_GetCharsInRxBuf;
	midiin1_state.RecvChar = MIDIUART1_RecvChar;

	midiin_reset(&midiin2_state);
	midiin2_state.GetCharsInRxBuf = MIDIUART2_GetCharsInRxBuf;
	midiin2_state.RecvChar = MIDIUART2_RecvChar;

	midiout_reset(&midiout1_state);
	midiout1_state.OutBufSize = MIDIUART1_OUT_BUF_SIZE;
	midiout1_state.GetCharsInTxBuf = MIDIUART1_GetCharsInTxBuf;
	midiout1_state.SendChar = MIDIUART1_SendChar;
	midiout1_state.CounterExpired = TMOUT1_CounterExpired;
	midiout1_state.SetCounter = TMOUT1_SetCounter;
	midiout1_state.midiout_counterhandle = TMOUT1_GetCounter(10);

	midiout_reset(&midiout2_state);
	midiout2_state.OutBufSize = MIDIUART2_OUT_BUF_SIZE;
	midiout2_state.GetCharsInTxBuf = MIDIUART2_GetCharsInTxBuf;
	midiout2_state.SendChar = MIDIUART2_SendChar;
	midiout2_state.CounterExpired = TMOUT1_CounterExpired;
	midiout2_state.SetCounter = TMOUT1_SetCounter;
	midiout2_state.midiout_counterhandle = TMOUT1_GetCounter(10);

	usbmidi_scheduler_reset(&usbschedstate);
	usbschedstate.RecvByte = usbmidi_receive;
	usbschedstate.bus_count = 5;
	usbschedstate.CanTransmit[0] = intercore_can_transmit;
	usbschedstate.CanTransmit[1] = midiout_can_transmit;
	usbschedstate.CanTransmit[2] = midiout_can_transmit;
	usbschedstate.CanTransmit[3] = intercore_can_transmit;
	usbschedstate.CanTransmit[4] = intercore_can_transmit;
	usbschedstate.Transmit[0] = intercore_transmit;
	usbschedstate.Transmit[1] = midiout_transmit;
	usbschedstate.Transmit[2] = midiout_transmit;
	usbschedstate.Transmit[3] = intercore_transmit;
	usbschedstate.Transmit[4] = intercore_transmit;
	usbschedstate.TransmitHook[0] = sysex_process;
	usbschedstate.TransmitHook[1] = NULL;
	usbschedstate.TransmitHook[2] = NULL;
	usbschedstate.TransmitHook[3] = NULL;
	usbschedstate.TransmitHook[4] = NULL;
	usbschedstate.busstate[0] = NULL;
	usbschedstate.busstate[1] = &midiout1_state;
	usbschedstate.busstate[2] = &midiout2_state;
	usbschedstate.busstate[3] = NULL;
	usbschedstate.busstate[4] = NULL;

	usbmidi_scheduler_reset(&intercoreschedstate);
	intercoreschedstate.RecvByte = intercore_receive;
	intercoreschedstate.bus_count = 5;
	intercoreschedstate.CanTransmit[0] = usbmidi_can_transmit;
	intercoreschedstate.CanTransmit[1] = midiout_can_transmit;
	intercoreschedstate.CanTransmit[2] = midiout_can_transmit;
	intercoreschedstate.CanTransmit[3] = usbmidi_can_transmit;
	intercoreschedstate.CanTransmit[4] = usbmidi_can_transmit;
	intercoreschedstate.Transmit[0] = usbmidi_transmit;
	intercoreschedstate.Transmit[1] = midiout_transmit;
	intercoreschedstate.Transmit[2] = midiout_transmit;
	intercoreschedstate.Transmit[3] = usbmidi_transmit;
	intercoreschedstate.Transmit[4] = usbmidi_transmit;
	intercoreschedstate.TransmitHook[0] = NULL;
	intercoreschedstate.TransmitHook[1] = NULL;
	intercoreschedstate.TransmitHook[2] = NULL;
	intercoreschedstate.TransmitHook[3] = NULL;
	intercoreschedstate.TransmitHook[4] = NULL;
	intercoreschedstate.busstate[0] = NULL;
	intercoreschedstate.busstate[1] = &midiout1_state;
	intercoreschedstate.busstate[2] = &midiout2_state;
	intercoreschedstate.busstate[3] = NULL;
	intercoreschedstate.busstate[4] = NULL;

	COREUART_Init();
	DMAT1_AllocateChannel(DMAT1_DeviceData);
	DMAT1_EnableChannel(DMAT1_DeviceData);
	//UART0_C2 &= ~(1 << 5); /* Clear RIE */
	UART0_C5 |= (1 << 5); /* RDMAE=1 Enable DMA */
	COREUART_Enable();

	intercore_sync_master();

    for(;;) {
    	if (USBMIDI1_App_Task(midi_buffer, sizeof(midi_buffer)) == ERR_BUSOFF) {
    		LED1_Off();
    		usbmidi_scheduler_reset(&usbschedstate);
    		UsbMidiRx_Init();
    		UsbMidiTx_Init();
    	}
    	else {
    		LED1_On();
    		if (sysex_flashing | slave_initialized) {
    			// Ignore intercore bus during flashing
    			usbschedstate.CanTransmit[0] = NULL;
    		}
    		usbmidi_scheduler(&usbschedstate);
    	}

    	if (TMOUT1_CounterExpired(usb_timeout_counter_handle)) {
    		// No data going out on USB, reset queue!
    		EnterCritical();
    		UsbMidiTx_Init();
    		ExitCritical();
    	}

    	if (!(sysex_flashing || slave_initialized)) {
    		usbmidi_scheduler(&intercoreschedstate);
    	}

    	int repeat = 1;
    	while (repeat) {
    		repeat = 0;
			int ready = midiin_receive(&cmd, &midiin1_state);
			if (ready) {
				repeat = 1;
				cmd.header &= 0x0F;
				cmd.header |= (0x1 << 4);
				if (usbmidi_can_transmit(4, NULL)) {
					usbmidi_transmit(cmd, NULL);
				}
				//if (intercore_can_transmit(4, NULL)) {
					intercore_transmit(cmd, NULL);
				//}
			}
			ready = midiin_receive(&cmd, &midiin2_state);
			if (ready) {
				repeat = 1;
				cmd.header &= 0x0F;
				cmd.header |= (0x2 << 4);
				if (usbmidi_can_transmit(4, NULL)) {
					usbmidi_transmit(cmd, NULL);
				}
				//if (intercore_can_transmit(4, NULL)) {
					intercore_transmit(cmd, NULL);
				//}
			}
    	}
    }
}