Author Topic: IRQ on card detect pin  (Read 17101 times)

Offline cmalan

  • Newbie
  • *
  • Posts: 14
    • View Profile
IRQ on card detect pin
« on: July 18, 2012, 11:54:48 AM »
Hi
On my board i have the card detect pin wired to a IRQ pin (IRQ1 on my 52259).
What should I do in the Interrupt so that the application know that the card is now removed and start the instillation sequence when the card is inserted again.
I know the code can check for removal periodically and retry until a card is detected, but I have the interrupt to work with.

Christo

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3243
    • View Profile
    • uTasker
Re: IRQ on card detect pin
« Reply #1 on: July 23, 2012, 09:47:21 PM »
Hi Christo

When the card is removed it should be adequate to call

fnInitialisationError(0);

This will set the card state to unmounted and restart the periodic attempt to re-mount it again.

In the case of card detection (insertion) it would be more efficient to stop the perioding re-mounting attempt until the card detection has occurred again - then a call of uTaskerMonoTimer(OWN_TASK, 1, E_POLL_SD_CARD); (single TICK delay) could be used to restart again, whereby it should normally be immediately mounted.

I have just prepared a new release version for one of the processor projects and have to do some tests - this may give me that chance to have a go at adding and checking the following three methods:
1) Polling SD card registers to detect removal or insertion of a different card (general case when no switch available, which presently is not working correctly from what I have heard)
2) Polling based on an input (non-interrupt capable one which could be used when a switch is available and connected).
3) Interrupt driven when switch available and comnnected to interrupt input - probably the best for quick detection and fast changing of cards so that this is not missed during polling.

Regards

Mark

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3243
    • View Profile
    • uTasker
Re: IRQ on card detect pin
« Reply #2 on: August 05, 2012, 04:34:01 PM »
Hi All

The utFAT1.12 now includes monitoring card presence using three possible techniques.

1) When there is no card detect switch available: Periodically verifying that the card is inserted by reading the last sector (this is a change from the previous attempt to read a configuration register to check that the same card was still there due to the fact that this read was failing after the SD card had been switched to its full operating state).
To use this technique the polling rate needs to be defined, which will then cause the method to be enabled
#define T_CHECK_CARD_REMOVAL    ((DELAY_LIMIT)(SEC * 10))            // if the card has no detection switch it can be polled to detect removal

The faster the polling rate the faster a card removal will be detected but the more reads are needed to check.
If a card is removed and re-inserted, or a different card inserted the polling will still fail since the reinserted card will not be in data mode yet.

2) When there is a card detect switch available connected to a general purpose input the input can be polled instead of reading a sector. This can be enabled by the define
#define SDCARD_DETECT_INPUT_POLL                                     // use card detect switch for detection polling (use together with T_CHECK_CARD_REMOVAL)

In addition the user must supply the macro (SDCARD_DETECTION()) used to check the input state which typically looks like:
#define SDCARD_DETECTION()   _READ_PORT_MASK(1, SPI_CARD_DETECT) // card detection input

If the pin doesn't default to input make sure that it is also configured as input. This can be added to the INITIALISE_SPI_SD_INTERFACE() macro for instance.

The disadvantage of this method is that it doesn't necessarily detect a card removal for a short time which means that a subsequent read/write would fail without knowing that the card had been removed.

3) When there is a card detect switch available connected to a port with interrupt capability (rising and falling edges) no polling is required to detect SD card removal. Instead the interrupt causes the state to be checked to synchronise to the state of the SD card.

This method is selected by disabling the polling techniqus and enabling
#define SDCARD_DETECT_INPUT_INTERRUPT                                // use card detect switch for detection by interrupt (T_CHECK_CARD_REMOVAL and SDCARD_DETECT_INPUT_POLL should be disabled)


In addition to supplying the macros as described in 2) the user needs to define where the interrupt is. This may be slightly processor dependent but the following is a typical case (for the AVR32):

        #define SDCARD_DETECT_PORT   PORT_1                              // card detection input defines when using interrupts
        #define SDCARD_DETECT_PIN    SPI_CARD_DETECT


This method is the preferred one when the card detect switch is available and connected to an appropriate interrupt since it avoid the need for polling an inserted card to monitor its removal. Detection of removal and insertion is immediate and so exchanging SD card during operation doesn't result in potential polling delays before the new card is mounted.


Note that there is a new local function in mass_storgae.c called fnPrepareDetectInterrupt(). This configures the interrupt for rising and falling edges but its content is processor specific to a degree meaning that each processor type requires slightly different code (see Port_Interrupts.h for examples of installing these interrupts for all supported processor types). This version was tested on an AVR32 and contains only this code at the moment. Other processor interrupt interfaces will be added as each project is tested - in the meantime you may need to insert code for configuring your processor type if this mode is to be used.

Regards

Mark