From 97dce7b94464b891a25170e6556daac5bb30f7ef Mon Sep 17 00:00:00 2001 From: gdisirio Date: Thu, 10 May 2012 12:26:24 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@4180 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/hal/src/mmcsd.c | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 os/hal/src/mmcsd.c (limited to 'os/hal/src/mmcsd.c') diff --git a/os/hal/src/mmcsd.c b/os/hal/src/mmcsd.c new file mode 100644 index 000000000..88f8a5505 --- /dev/null +++ b/os/hal/src/mmcsd.c @@ -0,0 +1,120 @@ +/* + ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010, + 2011,2012 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + ChibiOS/RT 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; either version 3 of the License, or + (at your option) any later version. + + ChibiOS/RT is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/** + * @file mmcsd.c + * @brief MMC/SD cards common code. + * + * @addtogroup MMCSD + * @{ + */ + +#include "ch.h" +#include "hal.h" + +#if HAL_USE_MMC_SPI || HAL_USE_SDC || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver local definitions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local functions. */ +/*===========================================================================*/ + +/** + * @brief Get slice with data from uint32_t[4] array. + * + * @notapi + */ +static uint32_t mmcsd_get_slice(uint32_t *data, uint32_t end, uint32_t start) { + uint32_t word = 0; + uint32_t mask = 0; + + chDbgCheck(end >= start, "sdc_get_slice"); + + while ((start - 32 * word) > 31){ + word++; + data++; + } + + end -= 32 * word; + start -= 32 * word; + + if (end < 31){ + /* Value lays in one word.*/ + mask = (1 << (end - start + 1)) - 1; + return (*data >> start) & mask; + } + else{ + /* Value spread on separate words.*/ + uint32_t lsb, msb; + lsb = *data >> start; + data++; + mask = (1 << (end - 32 + 1)) - 1; + msb = *data & mask; + msb = msb << (32 - start); + return msb | lsb; + } +} + +/*===========================================================================*/ +/* Driver exported functions. */ +/*===========================================================================*/ + +/** + * @brief Extract card capacity from a CSD. + * @details The capacity is returned as number of available blocks. + * + * @param[in] csd the CSD record + * + * @return The card capacity. + * @retval 0 CSD format error + */ +uint32_t mmcsdGetCapacity(uint32_t csd[4]) { + + switch (csd[3] >> 30) { + uint32_t a, b, c; + case 0: + /* CSD version 1.0 */ + a = mmcsd_get_slice(csd, MMCSD_CSD_10_C_SIZE_SLICE); + b = mmcsd_get_slice(csd, MMCSD_CSD_10_C_SIZE_MULT_SLICE); + c = mmcsd_get_slice(csd, MMCSD_CSD_10_READ_BL_LEN_SLICE); + return (a + 1) << (b + 2) << (c - 9); /* 2^9 == MMCSD_BLOCK_SIZE. */ + case 1: + /* CSD version 2.0.*/ + return 1024 * (mmcsd_get_slice(csd, MMCSD_CSD_20_C_SIZE_SLICE) + 1); + default: + /* Reserved value detected.*/ + return 0; + } +} + +#endif /* HAL_USE_MMC_SPI || HAL_USE_SDC */ + +/** @} */ -- cgit v1.2.3 From eba4b7056128ae85402984fb03882a5a9eb88213 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Thu, 10 May 2012 15:07:40 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@4182 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/hal/src/mmcsd.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'os/hal/src/mmcsd.c') diff --git a/os/hal/src/mmcsd.c b/os/hal/src/mmcsd.c index 88f8a5505..44a552cbd 100644 --- a/os/hal/src/mmcsd.c +++ b/os/hal/src/mmcsd.c @@ -58,7 +58,7 @@ static uint32_t mmcsd_get_slice(uint32_t *data, uint32_t end, uint32_t start) { chDbgCheck(end >= start, "sdc_get_slice"); - while ((start - 32 * word) > 31){ + while ((start - 32 * word) > 31) { word++; data++; } @@ -66,12 +66,12 @@ static uint32_t mmcsd_get_slice(uint32_t *data, uint32_t end, uint32_t start) { end -= 32 * word; start -= 32 * word; - if (end < 31){ + if (end < 31) { /* Value lays in one word.*/ mask = (1 << (end - start + 1)) - 1; return (*data >> start) & mask; } - else{ + else { /* Value spread on separate words.*/ uint32_t lsb, msb; lsb = *data >> start; -- cgit v1.2.3 From ab9c48576c984d67cf3bc66e29e9e154e8c934ce Mon Sep 17 00:00:00 2001 From: gdisirio Date: Fri, 11 May 2012 18:14:39 +0000 Subject: Small optimization. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@4186 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/hal/src/mmcsd.c | 48 +++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) (limited to 'os/hal/src/mmcsd.c') diff --git a/os/hal/src/mmcsd.c b/os/hal/src/mmcsd.c index 44a552cbd..62a3d368d 100644 --- a/os/hal/src/mmcsd.c +++ b/os/hal/src/mmcsd.c @@ -48,39 +48,33 @@ /*===========================================================================*/ /** - * @brief Get slice with data from uint32_t[4] array. + * @brief Gets a bit field from a words array. + * @note The bit zero is the LSb of the first word. + * + * @param[in] data pointer to the words array + * @param[in] end bit offset of the last bit of the field, inclusive + * @param[in] start bit offset of the first bit of the field, inclusive + * + * @return The bits field value, left aligned. * * @notapi */ static uint32_t mmcsd_get_slice(uint32_t *data, uint32_t end, uint32_t start) { - uint32_t word = 0; - uint32_t mask = 0; - - chDbgCheck(end >= start, "sdc_get_slice"); + unsigned startidx, endidx, startoff; + uint32_t endmask; - while ((start - 32 * word) > 31) { - word++; - data++; - } + chDbgCheck((end >= start) && ((end - start) < 32), "mmcsd_get_slice"); - end -= 32 * word; - start -= 32 * word; + startidx = start / 32; + startoff = start % 32; + endidx = end / 32; + endmask = (1 << ((end % 32) + 1)) - 1; - if (end < 31) { - /* Value lays in one word.*/ - mask = (1 << (end - start + 1)) - 1; - return (*data >> start) & mask; - } - else { - /* Value spread on separate words.*/ - uint32_t lsb, msb; - lsb = *data >> start; - data++; - mask = (1 << (end - 32 + 1)) - 1; - msb = *data & mask; - msb = msb << (32 - start); - return msb | lsb; - } + /* One or two pieces?*/ + if (startidx < endidx) + return (data[startidx] >> startoff) | /* Two pieces case. */ + ((data[endidx] & endmask) << (32 - startoff)); + return (data[startidx] & endmask) >> startoff; /* One piece case. */ } /*===========================================================================*/ @@ -105,7 +99,7 @@ uint32_t mmcsdGetCapacity(uint32_t csd[4]) { a = mmcsd_get_slice(csd, MMCSD_CSD_10_C_SIZE_SLICE); b = mmcsd_get_slice(csd, MMCSD_CSD_10_C_SIZE_MULT_SLICE); c = mmcsd_get_slice(csd, MMCSD_CSD_10_READ_BL_LEN_SLICE); - return (a + 1) << (b + 2) << (c - 9); /* 2^9 == MMCSD_BLOCK_SIZE. */ + return (a + 1) << (b + 2) << (c - 9); /* 2^9 == MMCSD_BLOCK_SIZE. */ case 1: /* CSD version 2.0.*/ return 1024 * (mmcsd_get_slice(csd, MMCSD_CSD_20_C_SIZE_SLICE) + 1); -- cgit v1.2.3 From 184a71345c6a36a9a8664eda8fbcc3ea728267a8 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sat, 2 Feb 2013 10:58:09 +0000 Subject: Updated license years. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@5102 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/hal/src/mmcsd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'os/hal/src/mmcsd.c') diff --git a/os/hal/src/mmcsd.c b/os/hal/src/mmcsd.c index 62a3d368d..d7b3a87a8 100644 --- a/os/hal/src/mmcsd.c +++ b/os/hal/src/mmcsd.c @@ -1,6 +1,6 @@ /* ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010, - 2011,2012 Giovanni Di Sirio. + 2011,2012,2013 Giovanni Di Sirio. This file is part of ChibiOS/RT. -- cgit v1.2.3 From 01f971ba1d63d8568789adf51cde22fb35f69e73 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Thu, 28 Feb 2013 16:23:19 +0000 Subject: Adjusted C file templates. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@5339 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/hal/src/mmcsd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'os/hal/src/mmcsd.c') diff --git a/os/hal/src/mmcsd.c b/os/hal/src/mmcsd.c index d7b3a87a8..c83095981 100644 --- a/os/hal/src/mmcsd.c +++ b/os/hal/src/mmcsd.c @@ -40,7 +40,7 @@ /*===========================================================================*/ /*===========================================================================*/ -/* Driver local variables. */ +/* Driver local variables and types. */ /*===========================================================================*/ /*===========================================================================*/ -- cgit v1.2.3