Esempio n. 1
0
/**************************************************************************//**
 * @brief
 *   Callback function called each time the USB device state is changed.
 *   Starts keyboard scanning when device has been configured by USB host.
 *
 * @param[in] oldState The device state the device has just left.
 * @param[in] newState The new device state.
 *****************************************************************************/
static void StateChange(USBD_State_TypeDef oldState,
                        USBD_State_TypeDef newState)
{
  /* Call HIDKBD drivers state change event handler. */
  HIDKBD_StateChangeEvent( oldState, newState );

  if ( newState == USBD_STATE_CONFIGURED )
  {
    /* We have been configured, start scanning the keyboard ! */
    if ( oldState != USBD_STATE_SUSPENDED ) /* Resume ?   */
    {
      keySeqNo        = 0;
      keyPushed       = false;
    }
    USBTIMER_Start( SCAN_TIMER, SCAN_RATE, ScanTimeout );
  }

  else if ( ( oldState == USBD_STATE_CONFIGURED ) &&
            ( newState != USBD_STATE_SUSPENDED  )    )
  {
    /* We have been de-configured, stop keyboard scanning. */
    USBTIMER_Stop( SCAN_TIMER );
  }

  else if ( newState == USBD_STATE_SUSPENDED )
  {
    /* We have been suspended, stop keyboard scanning. */
    /* Reduce current consumption to below 2.5 mA.     */
    USBTIMER_Stop( SCAN_TIMER );
  }
}
Esempio n. 2
0
/**************************************************************************//**
 * @brief
 *   Timeout function for keyboard scan timer.
 *   Scan keyboard to check for key press/release events.
 *   This function is called at a fixed rate.
 *****************************************************************************/
static void ScanTimeout( void )
{
  bool pushedPB0;
  bool pushedPB1;
  HIDKBD_KeyReport_t *report;

  /* Check pushbutton PB0 */
  pushedPB0 = GPIO_PinInGet( BUTTON0_PORT, BUTTON0_PIN ) == 0;

  if ( pushedPB0 != keyPushed )  /* Any change in keyboard status ? */
  {
    if ( pushedPB0 )
    {
      report = (void*)&USBDESC_reportTable[ 0 ];
    }
    else
    {
      report = (void*)&USBDESC_noKeyReport;
    }

    /* Pass keyboard report on to the HID keyboard driver. */
    HIDKBD_KeyboardEvent( report );
  }

  /* Keep track of the new keypush event (if any) */
  if ( pushedPB0 && !keyPushed )
  {
    /* Advance to next position in report table */
    keySeqNo++;
    if ( keySeqNo == (sizeof(USBDESC_reportTable) / sizeof(HIDKBD_KeyReport_t)))
    {
      keySeqNo = 0;
    }
  }
  keyPushed = pushedPB0;

  /* Check pushbutton PB1 */
  pushedPB1 = GPIO_PinInGet( BUTTON1_PORT, BUTTON1_PIN ) == 0;

  if ( pushedPB1 != keyPushed )  /* Any change in keyboard status ? */
  {
    if ( pushedPB1 )
    {
      report = (void*)&USBDESC_reportTable[ 1 ];
    }
    else
    {
      report = (void*)&USBDESC_noKeyReport;
    }

    /* Pass keyboard report on to the HID keyboard driver. */
    HIDKBD_KeyboardEvent( report );
  }
  keyPushed = pushedPB1;

  /* Restart keyboard scan timer */
  USBTIMER_Start( SCAN_TIMER, SCAN_RATE, ScanTimeout );
}
Esempio n. 3
0
/**************************************************************************//**
 * @brief
 *   Write to indirectly accessed media.
 *
 * @param[in] pCmd
 *   Points to a MSDD_CmdStatus_TypeDef structure which holds info about the
 *   current transfer.
 *
 * @param[in] data
 *   Pointer to data buffer.
 *
 * @param[in] sectors
 *   Number of 512 byte sectors to write to media.
 *****************************************************************************/
void MSDDMEDIA_Write( MSDD_CmdStatus_TypeDef *pCmd, uint8_t *data, uint32_t sectors )
{
  #if ( MSD_MEDIA == MSD_SRAM_MEDIA ) || ( MSD_MEDIA == MSD_PSRAM_MEDIA )
  (void)pCmd;
  (void)data;
  (void)sectors;
  #endif

  #if ( MSD_MEDIA == MSD_SDCARD_MEDIA )
  disk_write( 0, data, pCmd->lba, sectors );
  #endif

  #if ( MSD_MEDIA == MSD_FLASH_MEDIA ) || ( MSD_MEDIA == MSD_NORFLASH_MEDIA )
  unsigned int i;
  uint32_t offset;

  i = 0;
  while ( i < sectors )
  {
    if ( !flashStatus.pendingWrite )
    {
      /* Copy an entire flash page to the page buffer */
      flashStatus.pendingWrite = true;
      flashStatus.pPageBase    = (uint8_t*)((uint32_t)pCmd->pData & ~( flashPageSize - 1 ));
      offset                    = pCmd->pData - flashStatus.pPageBase;
      memcpy( flashPageBuf, flashStatus.pPageBase, flashPageSize );

      /* Write the received data in the page buffer */
      memcpy( flashPageBuf + offset, data, 512 );
      data        += 512;
      pCmd->pData += 512;

      USBTIMER_Start( FLUSH_TIMER_ID, FLUSH_TIMER_TIMEOUT, FlushTimerTimeout );
    }
    else
    {
      /* Check if current sector is located in the page buffer. */
      offset = pCmd->pData - flashStatus.pPageBase;
      if ( offset >= flashPageSize )
      {
        /*
         * Current sector not located in page buffer, flush pending data
         * before continuing.
         */
        MSDDMEDIA_Flush();
        i--;
      }
      else
      {
        /* Write the received data in the page buffer */
        memcpy( flashPageBuf + offset, data, 512 );
        data        += 512;
        pCmd->pData += 512;

        USBTIMER_Start( FLUSH_TIMER_ID, FLUSH_TIMER_TIMEOUT, FlushTimerTimeout );
      }
    }
    i++;
  }
  #endif
}