Example #1
0
void main(void)
{
    int cdc_in;
    int uart_in;
    volatile UINT8 u8Error;

    hw_init();
    MCU_init();
    GL_Init(
        GL_FD_HORIZONTAL | GL_FD_TOUCH_BEEP_ON,
        GLF_65K_COLORS,
        GLF_16M_COLORS,
        &OnDrawScreenBackground
    );
    win_main_font = (GL_HFONT) & Arial_16px;
    GL_Screen_Activate((GL_HSCREEN) &screen);
    SPI_Init();
    if ((u8Error = SD_Init()) != INIT_FAILS) {
        FAT_Read_Master_Block();
        loadFiles();
        redraw();
        SD_LOADED_BEEP();
    } else {
        NO_SD_BEEP();
    }

    EnableInterrupts;
    while (1) {
        // handle touch (FIXME typo in GL lib (HaDNle))
        GL_TS_HadnleActions();
    }
}
Example #2
0
void main(void)
{
  int cdc_in;
  int uart_in; 
  volatile UINT8 u8Error;
  
  PTBPE_PTBPE5 = 1;                     // DEMOFLEXISJMSD board 
  PTBDD_PTBDD5 = _IN;                     // Initialize Button in 

  hw_init();                            /* MCU Initialization */
  SPI_Init();                           /* SPI Module Init */
  u8Error=SD_Init();                    /* SD Card Init */
  FAT_Read_Master_Block();              /* FAT Driver Init */
  
  cdc_in=uart_in=0xff+1;                /* USB CMX Init */
  usb_cfg_init();
  cdc_init();


  while(PTBD_PTBD5);                    /* Wait until button clic */  
  CDC_Send_String("\r\n\r\n\tSD Card Terminal\r\n");  
  

  while(1)
  {
    cdc_process();                      /* USB CMX CDC process */
    FAT_LS();                           /* Simple function that shows the content*/
    MiniCom();                          /* User Interface call */
  }
}
Example #3
0
void reload()
{
    UINT8 file;
    volatile UINT8 u8Error;

    if ((u8Error = SD_Init()) != INIT_FAILS) {
        FAT_Read_Master_Block();
        loadFiles();
        SD_LOADED_BEEP();
    } else {
        for (file=0; file<FILES_IN_LIST; file++) {
            emptyButton(file);
        }
        NO_SD_BEEP();
    }
    redraw();
}
Example #4
0
void DC16_RX_Mode (void)
{
  unsigned int i, j, k;
  volatile UINT16 u16Error;
  UINT16 u16CRC;
  UINT32 fileSize, progressCount, ledcnt;

  RTCSC_RTCPS = 0xA; // re-enable RTC (set prescaler = /4, interrupt every 4ms)
  IR_TX_Off;
      
  // enable IR receive mode
  // prepare TPM2 to be used for IR TPM2MOD = IR_RC_PERIOD; TPM2C1 as falling edge
  ir_init(IR_RC_PERIOD);    
 
  (void)memset(gau8Minicom, 0x00, MINICOM_BUFFER_SIZE); // clear buffer

  // wait to start receiving data
  // and when we do, get filename (should be in 8.3 format + NULL)
  k = 0; 
  do
  {
    while (!ir_rx_byte) // wait to start receiving IR data...
    {
      if (SD_PRESENT == HIGH) // if no SD card, exit mode
      {
        delay_ms(500);
        return;
      }
        
      if (!SW_MODE)
      {
        delay_ms(50); // poor man's debounce
          
        while (!SW_MODE)
        {
          IR_RX_Off; // disable RX when we're done
          led_state = ALL_OFF;
          state_change_flag = 1; // go to the TX state, since we haven't started receiving yet
       }
        return; // get outta here!
      }
    }

    // if we get here, that means we've received a valid byte
    ir_rx_byte = FALSE;

    gau8Minicom[k] = ir_command;
    ++k;
  } while ((ir_command != '\0') && (k < 13));
  gau8Minicom[k-1] = '\0'; // add a null in case we've received incorrect filename data

  if (gau8Minicom[0] == '\0') // if no filename is received, abort transfer
  {
      if (usb_enabled_flag && USB_DETECT) Terminal_Send_String("Filename invalid.\n\r");
      DC16_Error(errRX, 0);
      return;     
  }
  
  // ensure the filename is uppercase before working with it
  // gotta love FAT16!
  for (j = 0; gau8Minicom[j] != '\0'; ++j)
    gau8Minicom[j] = (UINT8)toupper(gau8Minicom[j]);
     
  // get file size (4 bytes long)
  fileSize = 0;
  for (i = 0; i < 4; ++i)
  {
    timeout = 0;
    while (!ir_rx_byte) // wait to start receiving IR data...
    {
      if (timeout == 1250) // if we haven't received a byte for ~5 seconds, abort
      {             
        DC16_Error(errRX, 0);
        return;                          
      }
        
      if (!SW_MODE)
      {
        delay_ms(50); // poor man's debounce
          
        while (!SW_MODE)
        {
          IR_RX_Off; // disable RX when we're done
          led_state = ALL_OFF;
          state = TX; // go to SLEEP mode to avoid sending data as soon as we cancel a receive
          state_change_flag = 1;
        }
        return; // get outta here!
      }
    }

    // if we get here, that means we've received a valid byte
    ir_rx_byte = FALSE;

    fileSize <<= 8;
    fileSize |= ir_command; 
  }
  
  if (fileSize == 0xFFFFFFFF) // we get this if one badge receives another badge's power on IR test string (the Sony TV power off code), so just ignore it
  {
    delay_ms(500);
    return;
  }
  //else if ((fileSize > FAT16_MAX_FILE_SIZE) || (fileSize == 0)) // if the received file size is bigger than the possible FAT16 limit, we obviously have a corrupted transfer
  else if ((fileSize > 131072) || (fileSize == 0)) // limit filesize to 128KB to reduce transfer errors and prevent people from standing in front of each other for hours!
  {
     // so abort...
     if (usb_enabled_flag && USB_DETECT) Terminal_Send_String("Filesize error!\n\r");
     DC16_Error(errRX, 0);
     return;        
  }
  
  // if the filename and file size appear to be valid, we can now proceed...
  if (SD_PRESENT != HIGH)
  {
    u16Error=SD_Init(); // SD card init
    if (u16Error == OK) 
    {
      FAT_Read_Master_Block(); // FAT driver init
    }
    else 
    {
      DC16_Error(errSD, u16Error);
      return;
    }
    
    // see if a file already exists with the name we've just received
    u16Error=FAT_FileOpen(gau8Minicom, READ); 
    if (u16Error == FILE_FOUND) 
    { 
      for (j = '0'; j <= '9'; ++j)
      {
        // replace the last character in the filename with a numeral
        // this will let us receive 10 more files with the same name (0-9) 
        gau8Minicom[k-2] = (UINT8)j; // k is the index of the filename string         
        u16Error=FAT_FileOpen(gau8Minicom, READ); 
    
        if (u16Error != FILE_FOUND) break; // once we've found a filename that doesn't exist, we can move on...
      }
    }
          
    if (WP_ENABLED == HIGH)
    {
      if (usb_enabled_flag && USB_DETECT) Terminal_Send_String("SD card write protect enabled.\n\r");
      DC16_Error(errRX, 0);
      return;    
    }
    else
    {
      // create a new file on the SD card
      u16Error=FAT_FileOpen(gau8Minicom, CREATE); 
      if (u16Error != FILE_CREATE_OK) 
      { 
        DC16_Error(errFAT, u16Error);
        return;
      }
    }
  }
  else
  {
    if (usb_enabled_flag && USB_DETECT) Terminal_Send_String("No SD card inserted.\n\r");
    state = TX; // go to SLEEP mode to avoid receiving unsynchronized data
    state_change_flag = 1;
    return;
  }
  
  if (usb_enabled_flag && USB_DETECT)
  { 
    Terminal_Send_String("File name: ");
    Terminal_Send_String(gau8Minicom);
    Terminal_Send_String("\n\r");
     
    itoa(fileSize, gau8Minicom, 12);      
    Terminal_Send_String("File size: ");
    Terminal_Send_String(gau8Minicom);
    Terminal_Send_String(" bytes\n\r");
    Terminal_Send_String("Starting IR file receive.\n\r");
  }
  
  led_state = IGNORE; // set this so the timer interrupt routine doesn't mess with our LED output
  do_cool_LED_stuff(ALL_OFF); // we're going to use LEDs as a transfer progress indicator, so we want complete control

  progressCount = fileSize >> 3;
  ledcnt = 0;
  gu8Led = 0x00;
  
  if (fileSize >= MINICOM_BUFFER_SIZE)
  {
    do
    {
      // assuming each block sent is MINICOM_BUFFER_SIZE (512 bytes), except for the last one which might be shorter
      for (i = 0; i < MINICOM_BUFFER_SIZE; ++i) 
      {  
        timeout = 0;  
        while (!ir_rx_byte) // wait to start receiving IR data...
        {
          if (timeout == 1250) // if we haven't received a byte for ~5 seconds, abort
          {
            FAT_FileClose();
            DC16_Error(errRX, 0);
            return;                          
          }
          
          if (!SW_MODE)
          {
            delay_ms(50); // poor man's debounce
              
            while (!SW_MODE)
            {
              IR_RX_Off; // disable RX when we're done
              led_state = ALL_OFF;
              state = TX; // go to SLEEP mode to avoid sending data as soon as we cancel a receive
              state_change_flag = 1;
            }
            
            FAT_FileClose(); // properly close the file if we're aborting the transfer
            return; // get outta here!
          }
        }

        // if we get here, that means we've received a valid byte
        ir_rx_byte = FALSE; 
        gau8Minicom[i] = ir_command;
        fileSize--;
        ledcnt++;
        if (ledcnt == progressCount) // decrement LEDs as transfer progresses
        {
          ledcnt = 0;
          gu8Led <<= 1;
          gu8Led |= 1;
          do_cool_LED_stuff(BYTE);
        }    
      }
      
      u16CRC = DC16_RX_CRC();
      if (u16CRC == 0) return;
      
      // calculate CRC16 checksum on this block 
      j = crc16_ccitt(gau8Minicom, MINICOM_BUFFER_SIZE);
      
      if (usb_enabled_flag && USB_DETECT)
      {
        Terminal_Send_String("\n\rCRC16 = 0x");
        Terminal_Send_String(num2asc((j >> 8) & 0xFF));
        Terminal_Send_String(num2asc(j & 0xFF));
      }    
       
      // if it doesn't match what we've just received, abort the transfer
      if (u16CRC != j)
      {
        FAT_FileClose();
        DC16_Error(errRX, 0);
        return;      
      }
      else // if it matches, write the data to the card
      {
        FAT_FileWrite(gau8Minicom,MINICOM_BUFFER_SIZE); 
      }
    } while (fileSize >= MINICOM_BUFFER_SIZE);
  }
Example #5
0
UINT8 SD_Init(void) {
  //volatile UINT8 DelayFlag; 
  UINT16 u16Counter;
  volatile UINT8  u8R1;
  UINT8  CSD[16];
  UINT32 u32Ifc;
  UINT32 u32Ocr;
  
  gSDCard.NUM_BLOCKS = 0;
  gSDCard.SDHC = False;
  gSDCard.VERSION2 = False;
  
  /* Initialize SPI Module */
  SPI_Init();
  
	//for (u16Counter = 0; u16Counter < 3; u16Counter++) {
	  
    /* Send 80 dummy clocks without CS */
    SPI_SS = DISABLE;      

    /* Wait until voltage stabilize before start initialization */
    //DelayFlag = 0;
    //tmrStart(250, &DelayFlag);
    //while(DelayFlag == 0) __RESET_WATCHDOG(); 		

    for(u16Counter = 0; u16Counter < 10; u16Counter++) SPI_SendByte(0xFF); 
    
    /* Send CMD0 = go idle */ 
    for(u16Counter = 0; u16Counter < SD_INIT_RETRIES; u16Counter++) {
      SPI_SS = ENABLE;
      u8R1 = SD_SendCommand(SD_CMD0_GO_IDLE_STATE, 0, NULL, 0);
      SPI_SS = DISABLE;
      if(u8R1 ==  SD_R1_IDLE_STATE) break;
    }
    if(u16Counter >= SD_INIT_RETRIES) return (SD_FAIL_INIT);

	//}

  /* Send CMD8 = test version */
  SPI_SS = ENABLE;
  u8R1 = SD_SendCommand(SD_CMD8_SEND_IF_COND, SD_IF_COND_VHS_27_36 | 0xAA,(UINT8*)&u32Ifc, 4);
  SPI_SS = DISABLE;
  if(u8R1 == SD_FAIL_TIMEOUT) return (SD_FAIL_INIT);
  if(0 == (u8R1 & SD_R1_ILLEGAL_CMD)) {
    /* command supported, v2 card */
    gSDCard.VERSION2 = True;
    if(((u32Ifc & 0xFF) != 0xAA) || 
      ((u32Ifc & SD_IF_COND_VHS_MASK) != SD_IF_COND_VHS_27_36))
      return (SD_FAIL_INIT);
  }

  /* Send CMD58 = read OCR ... 3.3V */
  SPI_SS = ENABLE;
  u8R1 = SD_SendCommand(SD_CMD58_READ_OCR, 0, (UINT8*)&u32Ocr, 4); 
  SPI_SS = DISABLE;
  if(u8R1 == SD_FAIL_TIMEOUT) return (SD_FAIL_INIT);  
  if(0 == (u8R1 & SD_R1_ILLEGAL_CMD)) {
    if(u8R1 & SD_R1_ERROR_MASK) return (SD_FAIL_INIT);
    if(SD_OCR_VOLTAGE_3_3V != (u32Ocr & SD_OCR_VOLTAGE_3_3V)) return (SD_FAIL_INIT);
  }
  
  /* Send CMD55 + ACMD41 = initialize */
  for(u16Counter = 0; u16Counter < SD_INIT_RETRIES; u16Counter++) {
      SPI_SS = ENABLE;      
      u8R1 = SD_SendCommand(SD_CMD55_APP_CMD, 0, NULL, 0);
      SPI_SS = DISABLE;
      
      if(u8R1 == SD_FAIL_TIMEOUT) return (SD_FAIL_INIT);  
      
      if(u8R1 & SD_R1_ERROR_MASK) return (SD_FAIL_INIT);

      SPI_SS = ENABLE;
      u8R1 = SD_SendCommand(SD_ACMD41_SEND_OP_COND, gSDCard.VERSION2 ? SD_ACMD41_HCS : 0, NULL, 0);
      SPI_SS = DISABLE;
      
      if(u8R1 == SD_FAIL_TIMEOUT) return (SD_FAIL_INIT);  
      
      if(u8R1 & SD_R1_ERROR_MASK) return (SD_FAIL_INIT);

      if(0 == (u8R1 & SD_R1_IDLE_STATE)) break;
  }
  
  if(u16Counter >= SD_INIT_RETRIES) return (SD_FAIL_INIT);
  
  /* Version 2 or later card */ 
  if (gSDCard.VERSION2) { 
        /* Send CMD58 = read OCR to check CCS */
        SPI_SS = ENABLE;
        u8R1 = SD_SendCommand(SD_CMD58_READ_OCR, 0, (UINT8*)&u32Ocr, 4);
        SPI_SS = DISABLE;
        
        if(u8R1 == SD_FAIL_TIMEOUT) return (SD_FAIL_INIT);          
        
        if(u8R1 & SD_R1_ERROR_MASK) return (SD_FAIL_INIT);

        if(0 == (u32Ocr & SD_OCR_POWER_UP_STATUS)) return (SD_FAIL_INIT);

        if(u32Ocr & SD_OCR_CCS)  gSDCard.SDHC = True;
  }

  /* Send CMD9 = get CSD */
  
  SPI_SS = ENABLE;
  u8R1 = SD_SendCommand(SD_CMD9_SEND_CSD, 0, NULL, 0);
  if((u8R1 == SD_FAIL_TIMEOUT) || (u8R1 & SD_R1_ERROR_MASK)) {
    SPI_SS = DISABLE;
    return (SD_FAIL_INIT);
  } else {
    if(SD_ReadData(CSD, 16) != SD_OK) return (SD_FAIL_INIT);
    SPI_SS = DISABLE;
  }
  
  //gSDCard.NUM_BLOCKS = _io_sdcard_csd_capacity(csd); 
  
  /* Send CMD16 = set block length to 512 */
  SPI_SS = ENABLE;
  u8R1 = SD_SendCommand(SD_CMD16_SET_BLOCKLEN, 512, NULL, 0);
  SPI_SS = DISABLE;
  
  if(u8R1 == SD_FAIL_TIMEOUT) return (SD_FAIL_INIT);
  
  if(u8R1 & SD_R1_ERROR_MASK) return (SD_FAIL_INIT);
  
  //SPI_HighRate();	//Cambio el baudrate
  FAT_Read_Master_Block((UINT8 *)Buffer_Envio);
  (void)SD_LeerDireccion();

  return (SD_OK);
}