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

Dependencies:   mbed

Committer:
AjK
Date:
Mon Oct 11 10:34:55 2010 +0000
Revision:
0:0a841b89d614
Totally Alpha quality as this project isn\t completed. Just publishing it as it answers many questions asked in the forums

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:0a841b89d614 1 /****************************************************************************
AjK 0:0a841b89d614 2 * Copyright 2010 Andy Kirkham, Stellar Technologies Ltd
AjK 0:0a841b89d614 3 *
AjK 0:0a841b89d614 4 * This file is part of the Satellite Observers Workbench (SOWB).
AjK 0:0a841b89d614 5 *
AjK 0:0a841b89d614 6 * SOWB is free software: you can redistribute it and/or modify
AjK 0:0a841b89d614 7 * it under the terms of the GNU General Public License as published by
AjK 0:0a841b89d614 8 * the Free Software Foundation, either version 3 of the License, or
AjK 0:0a841b89d614 9 * (at your option) any later version.
AjK 0:0a841b89d614 10 *
AjK 0:0a841b89d614 11 * SOWB is distributed in the hope that it will be useful,
AjK 0:0a841b89d614 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
AjK 0:0a841b89d614 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
AjK 0:0a841b89d614 14 * GNU General Public License for more details.
AjK 0:0a841b89d614 15 *
AjK 0:0a841b89d614 16 * You should have received a copy of the GNU General Public License
AjK 0:0a841b89d614 17 * along with SOWB. If not, see <http://www.gnu.org/licenses/>.
AjK 0:0a841b89d614 18 *
AjK 0:0a841b89d614 19 * $Id: main.cpp 5 2010-07-12 20:51:11Z ajk $
AjK 0:0a841b89d614 20 *
AjK 0:0a841b89d614 21 ***************************************************************************/
AjK 0:0a841b89d614 22
AjK 0:0a841b89d614 23 #include "sowb.h"
AjK 0:0a841b89d614 24 #include "user.h"
AjK 0:0a841b89d614 25 #include "debug.h"
AjK 0:0a841b89d614 26 #include "rit.h"
AjK 0:0a841b89d614 27 #include "gps.h"
AjK 0:0a841b89d614 28 #include "gpio.h"
AjK 0:0a841b89d614 29 #include "ff.h"
AjK 0:0a841b89d614 30
AjK 0:0a841b89d614 31 FATFS MyFileSystem;
AjK 0:0a841b89d614 32 bool card_detected_A;
AjK 0:0a841b89d614 33 bool card_detected_B;
AjK 0:0a841b89d614 34 bool card_mounted;
AjK 0:0a841b89d614 35 bool card_bad;
AjK 0:0a841b89d614 36 uint32_t sdcard_detect_shift_register;
AjK 0:0a841b89d614 37
AjK 0:0a841b89d614 38 /** sdcard_is_mounted
AjK 0:0a841b89d614 39 *
AjK 0:0a841b89d614 40 * Tell the caller if an SD card is mounted.
AjK 0:0a841b89d614 41 *
AjK 0:0a841b89d614 42 * @return bool True if a card is mounted, false otherwise.
AjK 0:0a841b89d614 43 */
AjK 0:0a841b89d614 44 bool sdcard_is_mounted(void) {
AjK 0:0a841b89d614 45 return card_mounted;
AjK 0:0a841b89d614 46 }
AjK 0:0a841b89d614 47
AjK 0:0a841b89d614 48 /** sdcard_init()
AjK 0:0a841b89d614 49 */
AjK 0:0a841b89d614 50 void sdcard_init(void) {
AjK 0:0a841b89d614 51 DEBUG_INIT_START;
AjK 0:0a841b89d614 52 card_bad = false;
AjK 0:0a841b89d614 53 card_mounted = false;
AjK 0:0a841b89d614 54 card_detected_A = false;
AjK 0:0a841b89d614 55 card_detected_B = false;
AjK 0:0a841b89d614 56 sdcard_detect_shift_register = 0;
AjK 0:0a841b89d614 57 rit_timer_set_counter(SDCARD_TIMER_CB, 10);
AjK 0:0a841b89d614 58 DEBUG_INIT_END;
AjK 0:0a841b89d614 59 }
AjK 0:0a841b89d614 60
AjK 0:0a841b89d614 61 /** sdcard_process()
AjK 0:0a841b89d614 62 */
AjK 0:0a841b89d614 63 void sdcard_process(void) {
AjK 0:0a841b89d614 64 int retry;
AjK 0:0a841b89d614 65 int f_return;
AjK 0:0a841b89d614 66 DIR root_directory;
AjK 0:0a841b89d614 67
AjK 0:0a841b89d614 68 /* card_detected_A is set by the RIT timer callback below which performs
AjK 0:0a841b89d614 69 a rough debounce of the switch. So, if here we need to do a rising
AjK 0:0a841b89d614 70 edge detect so we only flag a card insertion once. */
AjK 0:0a841b89d614 71 if (card_detected_A && !card_detected_B && !card_mounted && !card_bad) {
AjK 0:0a841b89d614 72 card_detected_B = true;
AjK 0:0a841b89d614 73 debug_printf("Card insertion detected.\r\n");
AjK 0:0a841b89d614 74 /* Have found that some cards require a "kick" to bring them to
AjK 0:0a841b89d614 75 life. So we retry 3 times to init the card. If after 3 tries
AjK 0:0a841b89d614 76 it fails we mark the card as "bad". */
AjK 0:0a841b89d614 77 for(retry = 0; retry < 3; retry++) {
AjK 0:0a841b89d614 78 KICK_WATCHDOG;
AjK 0:0a841b89d614 79 f_return = f_mount(0,&MyFileSystem);
AjK 0:0a841b89d614 80 if (f_return == FR_OK) {
AjK 0:0a841b89d614 81 f_return = f_opendir(&root_directory, "/");
AjK 0:0a841b89d614 82 if (f_return == FR_OK) {
AjK 0:0a841b89d614 83 card_mounted = true;
AjK 0:0a841b89d614 84 debug_printf("SD card mounted.\r\n");
AjK 0:0a841b89d614 85 retry = 3; /* Break loop. */
AjK 0:0a841b89d614 86 }
AjK 0:0a841b89d614 87 }
AjK 0:0a841b89d614 88 }
AjK 0:0a841b89d614 89
AjK 0:0a841b89d614 90 if (!card_mounted) {
AjK 0:0a841b89d614 91 card_bad = true;
AjK 0:0a841b89d614 92 debug_printf("Failed to mount SD Card\r\n");
AjK 0:0a841b89d614 93 }
AjK 0:0a841b89d614 94 }
AjK 0:0a841b89d614 95 }
AjK 0:0a841b89d614 96
AjK 0:0a841b89d614 97 /** _sdcard_timer_callback
AjK 0:0a841b89d614 98 *
AjK 0:0a841b89d614 99 * RIT timer callback.
AjK 0:0a841b89d614 100 * @see rit.c
AjK 0:0a841b89d614 101 */
AjK 0:0a841b89d614 102 void _sdcard_timer_callback(int index) {
AjK 0:0a841b89d614 103
AjK 0:0a841b89d614 104 /* Shift left 1bit... */
AjK 0:0a841b89d614 105 sdcard_detect_shift_register = sdcard_detect_shift_register << 1;
AjK 0:0a841b89d614 106
AjK 0:0a841b89d614 107 /* And then mask in bit0 position. */
AjK 0:0a841b89d614 108 if (SDCARD_DETECT != 0) {
AjK 0:0a841b89d614 109 sdcard_detect_shift_register |= 1;
AjK 0:0a841b89d614 110 }
AjK 0:0a841b89d614 111
AjK 0:0a841b89d614 112 /* 16 consecutive 1s mean card detected. Rough debouncing of the
AjK 0:0a841b89d614 113 mechanical "card detect" switch in the SD card holder. */
AjK 0:0a841b89d614 114 if ((sdcard_detect_shift_register & 0x0000FFFFUL) == 0x0000FFFFUL) {
AjK 0:0a841b89d614 115 card_detected_A = true;
AjK 0:0a841b89d614 116 }
AjK 0:0a841b89d614 117 else {
AjK 0:0a841b89d614 118 card_bad = false;
AjK 0:0a841b89d614 119 card_mounted = false;
AjK 0:0a841b89d614 120 card_detected_A = false;
AjK 0:0a841b89d614 121 card_detected_B = false;
AjK 0:0a841b89d614 122 }
AjK 0:0a841b89d614 123
AjK 0:0a841b89d614 124 /* Restart timer. */
AjK 0:0a841b89d614 125 rit_timer_set_counter(SDCARD_TIMER_CB, 10);
AjK 0:0a841b89d614 126 }