comparison sd_raw.c @ 44:2a47c458d6ed

a bit of debugging tht didn't work
author Matt Johnston <matt@ucc.asn.au>
date Sat, 29 Jun 2013 23:46:35 +0800
parents 9e888708f33d
children
comparison
equal deleted inserted replaced
43:69cbf9ce72b5 44:2a47c458d6ed
8 * published by the Free Software Foundation. 8 * published by the Free Software Foundation.
9 */ 9 */
10 10
11 #include <string.h> 11 #include <string.h>
12 #include <avr/io.h> 12 #include <avr/io.h>
13 #include <util/delay.h>
14 #include <avr/pgmspace.h>
15 #include <stdio.h>
13 #include "sd_raw.h" 16 #include "sd_raw.h"
14 17
15 /** 18 /**
16 * \addtogroup sd_raw MMC/SD/SDHC card raw access 19 * \addtogroup sd_raw MMC/SD/SDHC card raw access
17 * 20 *
161 /* private helper functions */ 164 /* private helper functions */
162 static void sd_raw_send_byte(uint8_t b); 165 static void sd_raw_send_byte(uint8_t b);
163 static uint8_t sd_raw_rec_byte(); 166 static uint8_t sd_raw_rec_byte();
164 static uint8_t sd_raw_send_command(uint8_t command, uint32_t arg); 167 static uint8_t sd_raw_send_command(uint8_t command, uint32_t arg);
165 168
169 static uint8_t orig_pb;
170
166 /** 171 /**
167 * \ingroup sd_raw 172 * \ingroup sd_raw
168 * Initializes memory card communication. 173 * Initializes memory card communication.
169 * 174 *
170 * \returns 0 on failure, 1 on success. 175 * \returns 0 on failure, 1 on success.
172 uint8_t sd_raw_init() 177 uint8_t sd_raw_init()
173 { 178 {
174 /* enable inputs for reading card status */ 179 /* enable inputs for reading card status */
175 configure_pin_available(); 180 configure_pin_available();
176 configure_pin_locked(); 181 configure_pin_locked();
182
183 orig_pb = PORTB;
184
185 printf_P(PSTR("startup portb %hhx mosi %d, sck %d, ss, miso %d\n"),
186 PORTB,
187 orig_pb & _BV(PB3),
188 orig_pb & _BV(PB5),
189 orig_pb & _BV(PB2),
190 orig_pb & _BV(PB4));
177 191
178 /* enable outputs for MOSI, SCK, SS, input for MISO */ 192 /* enable outputs for MOSI, SCK, SS, input for MISO */
179 configure_pin_mosi(); 193 configure_pin_mosi();
180 configure_pin_sck(); 194 configure_pin_sck();
181 configure_pin_ss(); 195 configure_pin_ss();
331 } 345 }
332 346
333 void 347 void
334 sd_raw_deinit(void) 348 sd_raw_deinit(void)
335 { 349 {
336 configure_pin_mosi(); 350
337 configure_pin_sck(); 351 printf_P(PSTR("deinit mosi %d, sck %d, ss, miso %d\n"),
338 configure_pin_ss(); 352 PORTB & _BV(PB3),
353 PORTB & _BV(PB5),
354 PORTB & _BV(PB2),
355 PORTB & _BV(PB4));
356
357 // set to slave mode
358 SPCR = (0 << SPIE) | /* SPI Interrupt Enable */
359 (1 << SPE) | /* SPI Enable */
360 (0 << DORD) | /* Data Order: MSB first */
361 (0 << MSTR) | /* Master mode */
362 (0 << CPOL) | /* Clock Polarity: SCK low when idle */
363 (0 << CPHA) | /* Clock Phase: sample on rising SCK edge */
364 (1 << SPR1) | /* Clock Frequency: f_OSC / 128 */
365 (1 << SPR0);
366
367 _delay_ms(15);
368
369 // disable it
370 SPCR = (0 << SPIE) | /* SPI Interrupt Enable */
371 (0 << SPE) | /* SPI Enable */
372 (0 << DORD) | /* Data Order: MSB first */
373 (0 << MSTR) | /* Master mode */
374 (0 << CPOL) | /* Clock Polarity: SCK low when idle */
375 (0 << CPHA) | /* Clock Phase: sample on rising SCK edge */
376 (1 << SPR1) | /* Clock Frequency: f_OSC / 128 */
377 (1 << SPR0);
378
379 _delay_ms(15);
380
381 uint8_t pb_mask = _BV(PB3) | _BV(PB5) | _BV(PB2);
382 printf_P(PSTR("PORTB was %hhx\n"),
383 PORTB);
384 PORTB = (PORTB & ~pb_mask) | (orig_pb & pb_mask);
385 printf_P(PSTR("PORTB now %hhx\n"),
386 PORTB);
387
388 _delay_ms(15);
389
390 deconfigure_pin_mosi();
391 deconfigure_pin_sck();
392 deconfigure_pin_ss();
339 } 393 }
340 394
341 /** 395 /**
342 * \ingroup sd_raw 396 * \ingroup sd_raw
343 * Checks wether a memory card is located in the slot. 397 * Checks wether a memory card is located in the slot.