اطلاعیه

Collapse
No announcement yet.

نشناختن SD card با lpc1768

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    نشناختن SD card با lpc1768

    سلام
    من مموری رو با lpc 1788 در مد native با استفاده از کتابخانه RL کیل راه انداختم و بدون هیچ مشکلی کار کرد.
    حالا میخوام مموری رو با lpc 1768 راه بندازم، من فایل config.c رو مطابق با سخت افزار خودم تنظیم میکنم برنامه بدون هیچ اروری کامپایل میشه ولی زمانی که برنامه رو اجرا میکنم مموری نمیشناسه(یعنی تابع finit یک رو بر میگردونه ). سخت افزار رو هم چک کردم اتصالات هم درست وصل شدن.
    من مموری هم به این شکل به میکرو وصل کردم
    p1.20(sck0)------->clk
    p1.23(miso)------>data0(do) i
    p1.24(mosi)------>cmd(di) i
    p1.21(ssel)------>data3(cs)

    این فایل config.c :

    کد:
    /*----------------------------------------------------------------------------
     *      RL-ARM - FlashFS
     *----------------------------------------------------------------------------
     *      Name:    SPI_LPC17xx.c
     *      Purpose: Serial Peripheral Interface Driver for NXP LPC17xx
     *      Rev.:    V4.20
     *----------------------------------------------------------------------------
     *      This code is part of the RealView Run-Time Library.
     *      Copyright (c) 2004-2011 KEIL - An ARM Company. All rights reserved.<div style="text-align: center"></div>
     *---------------------------------------------------------------------------*/
    
    [URL="http://www.eca.ir/forums/usertag.php?do=list&action=hash&hash=include"]#include[/URL]  <File_Config.h>
    [URL="http://www.eca.ir/forums/usertag.php?do=list&action=hash&hash=include"]#include[/URL]  <LPC17xx.H>                 /* LPC17xx Definitions                  */
    
    /*----------------------------------------------------------------------------
      SPI Driver instance definition
       spi0_drv: First SPI driver
       spi1_drv: Second SPI driver
     *---------------------------------------------------------------------------*/
    
    [URL="http://www.eca.ir/forums/usertag.php?do=list&action=hash&hash=define"]#define[/URL]  __DRV_ID  spi0_drv
    [URL="http://www.eca.ir/forums/usertag.php?do=list&action=hash&hash=define"]#define[/URL]  __PCLK    100000000
    
    /* SPI Driver Interface functions */
    static BOOL Init (void);
    static BOOL UnInit (void);
    static U8   Send (U8 outb);
    static BOOL SendBuf (U8 *buf, U32 sz);
    static BOOL RecBuf (U8 *buf, U32 sz);
    static BOOL BusSpeed (U32 kbaud);
    static BOOL SetSS (U32 ss);
    static U32  CheckMedia (void);        /* Optional function for SD card check */
    
    /* SPI Device Driver Control Block */
    SPI_DRV __DRV_ID = {
      Init,
      UnInit,
      Send,
      SendBuf,
      RecBuf,
      BusSpeed,
      SetSS,
      CheckMedia                          /* Can be NULL if not existing         */
    };
    
    
    /* SSPxSR - bit definitions. */
    [URL="http://www.eca.ir/forums/usertag.php?do=list&action=hash&hash=define"]#define[/URL]  TFE     0x01
    [URL="http://www.eca.ir/forums/usertag.php?do=list&action=hash&hash=define"]#define[/URL]  TNF     0x02
    [URL="http://www.eca.ir/forums/usertag.php?do=list&action=hash&hash=define"]#define[/URL]  RNE     0x04
    [URL="http://www.eca.ir/forums/usertag.php?do=list&action=hash&hash=define"]#define[/URL]  RFF     0x08
    [URL="http://www.eca.ir/forums/usertag.php?do=list&action=hash&hash=define"]#define[/URL]  BSY     0x10
    
    /*--------------------------- Init ------------------------------------------*/
    
    static BOOL Init (void) {
      /* Initialize and enable the SSP Interface module. */
    
      LPC_SC->PCONP       |= (1 << 21);           /* Enable power to SSPI0 block */
    
      /* SSEL is GPIO, output set to high. */
      LPC_GPIO1->FIODIR   |=  (1<<21);            /* P0.16 is output             */
      LPC_GPIO1->FIOPIN   |=  (1<<21);            /* set P0.16 high (SSEL inact.)*/
      LPC_PINCON->PINSEL3 &= ~(0<<10);             /* P0.16 SSEL (used as GPIO)   */
    
      /* SCK, MISO, MOSI are SSP pins. */
      LPC_PINCON->PINSEL3 &= ~(3UL<<8);          /* P0.15 cleared               */
      LPC_PINCON->PINSEL3 |=  (3UL<<8);          /* P0.15 SCK0                  */
      LPC_PINCON->PINSEL3 &= ~((3<<14) | (3<<16));  /* P0.17, P0.18 cleared        */
      LPC_PINCON->PINSEL3 |=  ((3<<14) | (3<<16));  /* P0.17 MISO0, P0.18 MOSI0    */
    
      LPC_SC->PCLKSEL1    &= ~(3<<10);            /* PCLKSP0 = CCLK/4 ( 25MHz)   */
      LPC_SC->PCLKSEL1    |=  (1<<10);            /* PCLKSP0 = CCLK   (100MHz)   */
    
      LPC_SSP0->CPSR       = 250;                 /* 100MHz / 250 = 400kBit      */
                                                  /* maximum of 18MHz is possible*/    
      LPC_SSP0->CR0        = 0x0007;              /* 8Bit, CPOL=0, CPHA=0        */
      LPC_SSP0->CR1        = 0x0002;              /* SSP0 enable, master         */
    
      return (__TRUE);
    }
    
    
    /*--------------------------- UnInit ----------------------------------------*/
    
    static BOOL UnInit (void) {
      /* Return SSP interface to default state. */
    
      LPC_GPIO0->FIODIR   &=  ~(1<<16);           /* P0.16 is input              */
      LPC_PINCON->PINSEL1 &= ~(3<<0);             /* P0.16 SSEL (used as GPIO)   */ 
    
      LPC_PINCON->PINSEL0 &= ~(3UL<<30);          /* P0.15 GPIO port 0.15        */
      LPC_PINCON->PINSEL1 &= ~((3<<2) | (3<<4));  /* P0.17 and P0.18  GPIO       */
    
      LPC_SC->PCLKSEL1    &= ~(3<<10);            /* PCLKSP0 = CCLK/4 ( 25MHz)   */
    
      LPC_SSP0->CPSR      &= ~0xFF; 
      
      LPC_SSP0->CR0       &= ~0xFFFF;   
      LPC_SSP0->CR1       &= ~0x0F;
    
      return (__TRUE);
    }
    
    
    /*--------------------------- Send ------------------------------------------*/
    
    static U8 Send (U8 outb) {
      /* Write and Read a byte on SPI interface. */
    
      LPC_SSP0->DR = outb;
      while (LPC_SSP0->SR & BSY);                 /* Wait for transfer to finish */
      return (LPC_SSP0->DR);                      /* Return received value       */
    }
    
    
    /*--------------------------- SendBuf ---------------------------------------*/
    
    static BOOL SendBuf (U8 *buf, U32 sz) {
      /* Send buffer to SPI interface. */
      U32 i;
    
      for (i = 0; i < sz; i++) {
        LPC_SSP0->DR = buf[i];
        /* Wait if Tx FIFO is full. */
        while (!(LPC_SSP0->SR & TNF));
        LPC_SSP0->DR;
      }
      /* Wait until Tx finished, drain Rx FIFO. */
      while (LPC_SSP0->SR & (BSY | RNE)) {
        LPC_SSP0->DR;
      }           
      return (__TRUE);
    }
    
    
    /*--------------------------- RecBuf ----------------------------------------*/
    
    static BOOL RecBuf (U8 *buf, U32 sz) {
      /* Receive SPI data to buffer. */
      U32 i;
    
      for (i = 0; i < sz; i++) {
        LPC_SSP0->DR = 0xFF; 
        /* Wait while Rx FIFO is empty. */
        while (!(LPC_SSP0->SR & RNE));
        buf[i] = LPC_SSP0->DR;
      }
      return (__TRUE);
    }
    
    /*--------------------------- BusSpeed --------------------------------------*/
    
    static BOOL BusSpeed (U32 kbaud) {
      /* Set an SPI clock to required baud rate. */
      U32 div;
    
      div = (__PCLK/1000 + kbaud - 1) / kbaud;
      if (div == 0)   div = 0x02;
      if (div & 1)    div++;
      if (div > 0xFE) div = 0xFE;
      LPC_SSP0->CPSR = div;
      return (__TRUE);
    }
    
    
    /*--------------------------- SetSS -----------------------------------------*/
    
    static BOOL SetSS (U32 ss) {
      /* Enable/Disable SPI Chip Select (drive it high or low). */
    
      if (ss) {
        /* SSEL is GPIO, output set to high. */
        LPC_GPIO1->FIOSET = 1<<21;
      } 
      else {
        /* SSEL is GPIO, output set to low. */
        LPC_GPIO1->FIOCLR = 1<<21;
      }
      return (__TRUE);
    }
    
    
    /*--------------------------- CheckMedia ------------------------------------*/
    
    static U32 CheckMedia (void) {
      /* Read CardDetect and WriteProtect SD card socket pins. */
      U32 stat = 0;
     
      if (!(LPC_GPIO4->FIOPIN & (1 << 29))) {
        /* Card is inserted (CD=0). */
        stat |= M_INSERTED;
      } 
    [URL="http://www.eca.ir/forums/usertag.php?do=list&action=hash&hash=if"]#if[/URL]  0
      if ((LPC_GPIO4->FIOPIN & (1 << 29))) {
        /* Write Protect switch is active (WP=1). */
        stat |= M_PROTECTED;
      }
    [URL="http://www.eca.ir/forums/usertag.php?do=list&action=hash&hash=endif"]#endif[/URL] 
      return (stat);
    }
    
    /*----------------------------------------------------------------------------
     * end of file
     *---------------------------------------------------------------------------*/
    
    [HR][/HR]اینم فایل main :
    
    
    [URL="http://www.eca.ir/forums/usertag.php?do=list&action=hash&hash=include"]#include[/URL]  "LPC17xx.h"
    [URL="http://www.eca.ir/forums/usertag.php?do=list&action=hash&hash=include"]#include[/URL]  "RTL.h"
    
    int main(void)
    {
        unsigned int r;
        LPC_GPIO0->FIODIR = 0xFF;
        LPC_GPIO0->FIOSET = 0xFF;
        while(1)
        {
             r = finit(NULL);
             if(r == 0) LPC_GPIO0->FIOCLR = 0xFF;
        }
    }

    دوستان اگه ممکنه یه راهنمایی کنید که اشتباه من کجاست که برنامه کار نمی کنه

    جدیدترین ویرایش توسط اشکان غفارزاده; ۱۸:۲۱ ۱۳۹۶/۰۳/۳۱.

    #2
    پاسخ : نشناختن SD card با lpc1768

    سلام دوست عزیز
    من هم با این مشکل مواجه شدم در فایل
    File_Config قسمتی هست زمانی که مموری توی سوکت می گذارید به میکرو می فهمونه که کارت وارد شده شما احتمالا باپه مربوط به این کار تعریف نکردید .شما می تونید با دستکار هدر مربوطه اونه غیر فعال کنید یا پایه برای این براش معرفی کنید

    برای غیر فعال کردنش می تونید تابع مربوطه تغییر بدید
    کد:
    /*--------------------------- CheckMedia ------------------------------------*/
    
    static U32 CheckMedia (void) {
     
     
      /* Card is inserted (CD=0). */
        stat |= M_INSERTED;
      
      return (stat);
    }
    
    /*----------------------------------------------------------------------------[SIZE=4][/SIZE]

    دیدگاه

    لطفا صبر کنید...
    X