Satellite Observers Workbench. NOT yet complete, just published for forum posters to \"cherry pick\" pieces of code as requiered as an example.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sdcard.c Source File

sdcard.c

00001 /****************************************************************************
00002  *    Copyright 2010 Andy Kirkham, Stellar Technologies Ltd
00003  *    
00004  *    This file is part of the Satellite Observers Workbench (SOWB).
00005  *
00006  *    SOWB is free software: you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation, either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    SOWB is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with SOWB.  If not, see <http://www.gnu.org/licenses/>.
00018  *
00019  *    $Id: main.cpp 5 2010-07-12 20:51:11Z ajk $
00020  *    
00021  ***************************************************************************/
00022  
00023 #include "sowb.h"
00024 #include "user.h"
00025 #include "debug.h"
00026 #include "rit.h"
00027 #include "gps.h"
00028 #include "gpio.h"
00029 #include "ff.h"
00030 
00031 FATFS MyFileSystem;
00032 bool card_detected_A;
00033 bool card_detected_B;
00034 bool card_mounted;
00035 bool card_bad;
00036 uint32_t sdcard_detect_shift_register;
00037 
00038 /** sdcard_is_mounted
00039  *
00040  * Tell the caller if an SD card is mounted.
00041  *
00042  * @return bool True if a card is mounted, false otherwise.
00043  */
00044 bool sdcard_is_mounted(void) {
00045     return card_mounted;
00046 }
00047 
00048 /** sdcard_init()
00049  */
00050 void sdcard_init(void) {
00051     DEBUG_INIT_START;
00052     card_bad        = false;
00053     card_mounted    = false;
00054     card_detected_A = false;
00055     card_detected_B = false;
00056     sdcard_detect_shift_register = 0;
00057     rit_timer_set_counter(SDCARD_TIMER_CB, 10);
00058     DEBUG_INIT_END;
00059 }
00060 
00061 /** sdcard_process()
00062  */             
00063 void sdcard_process(void) {
00064     int retry;
00065     int f_return;
00066     DIR root_directory;
00067     
00068     /* card_detected_A is set by the RIT timer callback below which performs
00069        a rough debounce of the switch. So, if here we need to do a rising
00070        edge detect so we only flag a card insertion once. */
00071     if (card_detected_A && !card_detected_B && !card_mounted && !card_bad) {
00072         card_detected_B = true;        
00073         debug_printf("Card insertion detected.\r\n");
00074         /* Have found that some cards require a "kick" to bring them to
00075            life. So we retry 3 times to init the card. If after 3 tries
00076            it fails we mark the card as "bad". */
00077         for(retry = 0; retry < 3; retry++) {
00078             KICK_WATCHDOG;
00079             f_return = f_mount(0,&MyFileSystem);
00080             if (f_return == FR_OK) {
00081                 f_return = f_opendir(&root_directory, "/");
00082                 if (f_return == FR_OK) {
00083                     card_mounted = true;
00084                     debug_printf("SD card mounted.\r\n");
00085                     retry = 3; /* Break loop. */
00086                 }
00087             }    
00088         }
00089         
00090         if (!card_mounted) {
00091             card_bad = true;
00092             debug_printf("Failed to mount SD Card\r\n");
00093         }
00094     }
00095 }
00096 
00097 /** _sdcard_timer_callback
00098  *
00099  * RIT timer callback.
00100  * @see rit.c
00101  */
00102 void _sdcard_timer_callback(int index) {
00103     
00104     /* Shift left 1bit... */
00105     sdcard_detect_shift_register = sdcard_detect_shift_register << 1;
00106     
00107     /* And then mask in bit0 position. */
00108     if (SDCARD_DETECT != 0) {  
00109         sdcard_detect_shift_register |= 1;
00110     }
00111 
00112     /* 16 consecutive 1s mean card detected. Rough debouncing of the
00113        mechanical "card detect" switch in the SD card holder. */
00114     if ((sdcard_detect_shift_register & 0x0000FFFFUL) == 0x0000FFFFUL) { 
00115         card_detected_A = true; 
00116     }
00117     else {
00118         card_bad        = false;
00119         card_mounted    = false;
00120         card_detected_A = false;
00121         card_detected_B = false;
00122     }
00123     
00124     /* Restart timer. */
00125     rit_timer_set_counter(SDCARD_TIMER_CB, 10);
00126 }