X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=src%2Fdrivers%2Fao_sdcard.c;h=9d36c397552277a2baab9f0fbdd0b00d8824a997;hp=6073677a96745ddfbc69a37976e72e9c76a5edd2;hb=cdaa0d7b272505c49017f409b7c0b8e3240608f0;hpb=b34370cea662eb245e43aca20a6650b84b55ef6f diff --git a/src/drivers/ao_sdcard.c b/src/drivers/ao_sdcard.c index 6073677a..9d36c397 100644 --- a/src/drivers/ao_sdcard.c +++ b/src/drivers/ao_sdcard.c @@ -3,7 +3,8 @@ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -18,23 +19,38 @@ #include "ao.h" #include "ao_sdcard.h" -#define ao_sdcard_get_slow() ao_spi_get(AO_SDCARD_SPI_BUS, AO_SPI_SPEED_250kHz) -#define ao_sdcard_get() ao_spi_get(AO_SDCARD_SPI_BUS, AO_SPI_SPEED_FAST) -#define ao_sdcard_put() ao_spi_put(AO_SDCARD_SPI_BUS) +#if HAS_RADIO +extern uint8_t ao_radio_mutex; +#define get_radio() ao_mutex_get(&ao_radio_mutex) +#define put_radio() ao_mutex_put(&ao_radio_mutex) +#else +#define get_radio() +#define put_radio() +#endif + +#define ao_sdcard_get_slow() do { get_radio(); ao_spi_get(AO_SDCARD_SPI_BUS, AO_SPI_SPEED_250kHz); } while (0) +#define ao_sdcard_get() do { get_radio(); ao_spi_get(AO_SDCARD_SPI_BUS, AO_SPI_SPEED_FAST); } while (0) +#define ao_sdcard_put() do { ao_spi_put(AO_SDCARD_SPI_BUS); put_radio(); } while (0) #define ao_sdcard_send_fixed(d,l) ao_spi_send_fixed((d), (l), AO_SDCARD_SPI_BUS) #define ao_sdcard_send(d,l) ao_spi_send((d), (l), AO_SDCARD_SPI_BUS) #define ao_sdcard_recv(d,l) ao_spi_recv((d), (l), AO_SDCARD_SPI_BUS) -#define ao_sdcard_select() ao_gpio_set(AO_SDCARD_SPI_CS_PORT,AO_SDCARD_SPI_CS_PIN,AO_SDCARD_SPI_CS,0) -#define ao_sdcard_deselect() ao_gpio_set(AO_SDCARD_SPI_CS_PORT,AO_SDCARD_SPI_CS_PIN,AO_SDCARD_SPI_CS,1) +#define ao_sdcard_select() ao_gpio_set(AO_SDCARD_SPI_CS_PORT,AO_SDCARD_SPI_CS_PIN,0) +#define ao_sdcard_deselect() ao_gpio_set(AO_SDCARD_SPI_CS_PORT,AO_SDCARD_SPI_CS_PIN,1) /* Include SD card commands */ -#define SDCARD_DEBUG 1 +#ifndef SDCARD_DEBUG +#define SDCARD_DEBUG 0 +#endif /* Spew SD tracing */ +#ifndef SDCARD_TRACE #define SDCARD_TRACE 0 +#endif /* Emit error and warning messages */ +#ifndef SDCARD_WARN #define SDCARD_WARN 0 +#endif static uint8_t initialized; static uint8_t present; @@ -47,13 +63,13 @@ static enum ao_sdtype sdtype; #if SDCARD_TRACE #define DBG(...) printf(__VA_ARGS__) #else -#define DBG(...) +#define DBG(...) (void) 0 #endif #if SDCARD_WARN #define WARN(...) printf(__VA_ARGS__) #else -#define WARN(...) +#define WARN(...) (void) 0 #endif #define later(x,y) ((int16_t) ((x) - (y)) >= 0) @@ -91,7 +107,6 @@ ao_sdcard_send_cmd(uint8_t cmd, uint32_t arg) { uint8_t data[6]; uint8_t reply; - int i; uint16_t timeout; DBG ("\tsend_cmd %d arg %08x\n", cmd, arg); @@ -102,7 +117,7 @@ ao_sdcard_send_cmd(uint8_t cmd, uint32_t arg) return SDCARD_STATUS_TIMEOUT; } - data[0] = cmd & 0x3f | 0x40; + data[0] = (cmd & 0x3f) | 0x40; data[1] = arg >> 24; data[2] = arg >> 16; data[3] = arg >> 8; @@ -324,11 +339,11 @@ ao_sdcard_setup(void) ao_sdcard_send_fixed(0xff, 10); /* Reset the card and get it into SPI mode */ - for (i = 0; i < SDCARD_IDLE_WAIT; i++) { + for (i = 0; i < SDCARD_IDLE_RETRY; i++) { if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE) break; } - if (i == SDCARD_IDLE_WAIT) + if (i == SDCARD_IDLE_RETRY) goto bail; /* Figure out what kind of card we have */ @@ -344,14 +359,16 @@ ao_sdcard_setup(void) sdver2 = 1; } - for (i = 0; i < SDCARD_IDLE_WAIT; i++) { + for (i = 0; i < SDCARD_OP_COND_RETRY; i++) { + ao_delay(AO_MS_TO_TICKS(10)); ret = ao_sdcard_app_send_op_cond(arg); if (ret != SDCARD_STATUS_IDLE_STATE) break; } if (ret != SDCARD_STATUS_READY_STATE) { /* MMC */ - for (i = 0; i < SDCARD_IDLE_WAIT; i++) { + for (i = 0; i < SDCARD_OP_COND_RETRY; i++) { + ao_delay(AO_MS_TO_TICKS(10)); ret = ao_sdcard_send_op_cond(); if (ret != SDCARD_STATUS_IDLE_STATE) break; @@ -391,14 +408,14 @@ static uint8_t _ao_sdcard_reset(void) { int i; - uint8_t ret; + uint8_t ret = 0x3f; uint8_t response[10]; - for (i = 0; i < SDCARD_IDLE_WAIT; i++) { + for (i = 0; i < SDCARD_IDLE_RETRY; i++) { if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE) break; } - if (i == SDCARD_IDLE_WAIT) { + if (i == SDCARD_IDLE_RETRY) { ret = 0x3f; goto bail; } @@ -408,15 +425,15 @@ _ao_sdcard_reset(void) */ if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) { uint32_t arg = 0; - uint8_t sdver2 = 0; +// uint8_t sdver2 = 0; /* Check for SD version 2 */ if ((response[2] & 0xf) == 1 && response[3] == 0xaa) { arg = 0x40000000; - sdver2 = 1; +// sdver2 = 1; } - for (i = 0; i < SDCARD_IDLE_WAIT; i++) { + for (i = 0; i < SDCARD_IDLE_RETRY; i++) { ret = ao_sdcard_app_send_op_cond(arg); if (ret != SDCARD_STATUS_IDLE_STATE) break; @@ -424,7 +441,7 @@ _ao_sdcard_reset(void) if (ret != SDCARD_STATUS_READY_STATE) { /* MMC */ - for (i = 0; i < SDCARD_IDLE_WAIT; i++) { + for (i = 0; i < SDCARD_IDLE_RETRY; i++) { ret = ao_sdcard_send_op_cond(); if (ret != SDCARD_STATUS_IDLE_STATE) break; @@ -476,7 +493,7 @@ ao_sdcard_wait_block_start(void) uint8_t ao_sdcard_read_block(uint32_t block, uint8_t *data) { - uint8_t ret; + uint8_t ret = 0x3f; uint8_t start_block; uint8_t crc[2]; int tries; @@ -507,6 +524,7 @@ ao_sdcard_read_block(uint32_t block, uint8_t *data) WARN ("read block command failed %d status %02x\n", block, ret); status = _ao_sdcard_send_status(); WARN ("\tstatus now %04x\n", status); + (void) status; goto bail; } @@ -556,8 +574,6 @@ ao_sdcard_write_block(uint32_t block, uint8_t *data) uint8_t response[1]; uint8_t start_block[8]; uint16_t status; - static uint8_t check_data[512]; - int i; int tries; ao_sdcard_lock(); @@ -601,7 +617,7 @@ ao_sdcard_write_block(uint32_t block, uint8_t *data) if ((response[0] & SDCARD_DATA_RES_MASK) != SDCARD_DATA_RES_ACCEPTED) { int i; WARN("Data not accepted, response"); - for (i = 0; i < sizeof (response); i++) + for (i = 0; i < (int) sizeof (response); i++) WARN(" %02x", response[i]); WARN("\n"); ret = 0x3f; @@ -686,6 +702,9 @@ static const struct ao_cmds ao_sdcard_cmds[] = { void ao_sdcard_init(void) { + stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_SCK_PIN, STM_PUPDR_PULL_UP); + stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_MISO_PIN, STM_PUPDR_PULL_UP); + stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_MOSI_PIN, STM_PUPDR_PULL_UP); ao_spi_init_cs(AO_SDCARD_SPI_CS_PORT, (1 << AO_SDCARD_SPI_CS_PIN)); #if SDCARD_DEBUG ao_cmd_register(&ao_sdcard_cmds[0]);