Mbed Cloud example program for workshop in W27 2018.

Dependencies:   MMA7660 LM75B

Committer:
MACRUM
Date:
Sat Jun 30 01:40:30 2018 +0000
Revision:
0:119624335925
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:119624335925 1 /* mbed Microcontroller Library
MACRUM 0:119624335925 2 * Copyright (c) 2006-2013 ARM Limited
MACRUM 0:119624335925 3 *
MACRUM 0:119624335925 4 * Licensed under the Apache License, Version 2.0 (the "License");
MACRUM 0:119624335925 5 * you may not use this file except in compliance with the License.
MACRUM 0:119624335925 6 * You may obtain a copy of the License at
MACRUM 0:119624335925 7 *
MACRUM 0:119624335925 8 * http://www.apache.org/licenses/LICENSE-2.0
MACRUM 0:119624335925 9 *
MACRUM 0:119624335925 10 * Unless required by applicable law or agreed to in writing, software
MACRUM 0:119624335925 11 * distributed under the License is distributed on an "AS IS" BASIS,
MACRUM 0:119624335925 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 0:119624335925 13 * See the License for the specific language governing permissions and
MACRUM 0:119624335925 14 * limitations under the License.
MACRUM 0:119624335925 15 */
MACRUM 0:119624335925 16
MACRUM 0:119624335925 17 #ifndef MBED_SD_BLOCK_DEVICE_H
MACRUM 0:119624335925 18 #define MBED_SD_BLOCK_DEVICE_H
MACRUM 0:119624335925 19
MACRUM 0:119624335925 20 /* If the target has no SPI support then SDCard is not supported */
MACRUM 0:119624335925 21 #ifdef DEVICE_SPI
MACRUM 0:119624335925 22
MACRUM 0:119624335925 23 #include "BlockDevice.h"
MACRUM 0:119624335925 24 #include "mbed.h"
MACRUM 0:119624335925 25 #include "platform/PlatformMutex.h"
MACRUM 0:119624335925 26
MACRUM 0:119624335925 27 /** Access an SD Card using SPI
MACRUM 0:119624335925 28 *
MACRUM 0:119624335925 29 * @code
MACRUM 0:119624335925 30 * #include "mbed.h"
MACRUM 0:119624335925 31 * #include "SDBlockDevice.h"
MACRUM 0:119624335925 32 *
MACRUM 0:119624335925 33 * SDBlockDevice sd(p5, p6, p7, p12); // mosi, miso, sclk, cs
MACRUM 0:119624335925 34 * uint8_t block[512] = "Hello World!\n";
MACRUM 0:119624335925 35 *
MACRUM 0:119624335925 36 * int main() {
MACRUM 0:119624335925 37 * sd.init();
MACRUM 0:119624335925 38 * sd.write(block, 0, 512);
MACRUM 0:119624335925 39 * sd.read(block, 0, 512);
MACRUM 0:119624335925 40 * printf("%s", block);
MACRUM 0:119624335925 41 * sd.deinit();
MACRUM 0:119624335925 42 * }
MACRUM 0:119624335925 43 */
MACRUM 0:119624335925 44 class SDBlockDevice : public BlockDevice {
MACRUM 0:119624335925 45 public:
MACRUM 0:119624335925 46 /** Lifetime of an SD card
MACRUM 0:119624335925 47 */
MACRUM 0:119624335925 48 SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz=1000000);
MACRUM 0:119624335925 49 virtual ~SDBlockDevice();
MACRUM 0:119624335925 50
MACRUM 0:119624335925 51 /** Initialize a block device
MACRUM 0:119624335925 52 *
MACRUM 0:119624335925 53 * @return 0 on success or a negative error code on failure
MACRUM 0:119624335925 54 */
MACRUM 0:119624335925 55 virtual int init();
MACRUM 0:119624335925 56
MACRUM 0:119624335925 57 /** Deinitialize a block device
MACRUM 0:119624335925 58 *
MACRUM 0:119624335925 59 * @return 0 on success or a negative error code on failure
MACRUM 0:119624335925 60 */
MACRUM 0:119624335925 61 virtual int deinit();
MACRUM 0:119624335925 62
MACRUM 0:119624335925 63 /** Read blocks from a block device
MACRUM 0:119624335925 64 *
MACRUM 0:119624335925 65 * @param buffer Buffer to write blocks to
MACRUM 0:119624335925 66 * @param addr Address of block to begin reading from
MACRUM 0:119624335925 67 * @param size Size to read in bytes, must be a multiple of read block size
MACRUM 0:119624335925 68 * @return 0 on success, negative error code on failure
MACRUM 0:119624335925 69 */
MACRUM 0:119624335925 70 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
MACRUM 0:119624335925 71
MACRUM 0:119624335925 72 /** Program blocks to a block device
MACRUM 0:119624335925 73 *
MACRUM 0:119624335925 74 * The blocks must have been erased prior to being programmed
MACRUM 0:119624335925 75 *
MACRUM 0:119624335925 76 * @param buffer Buffer of data to write to blocks
MACRUM 0:119624335925 77 * @param addr Address of block to begin writing to
MACRUM 0:119624335925 78 * @param size Size to write in bytes, must be a multiple of program block size
MACRUM 0:119624335925 79 * @return 0 on success, negative error code on failure
MACRUM 0:119624335925 80 */
MACRUM 0:119624335925 81 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
MACRUM 0:119624335925 82
MACRUM 0:119624335925 83 /** Mark blocks as no longer in use
MACRUM 0:119624335925 84 *
MACRUM 0:119624335925 85 * This function provides a hint to the underlying block device that a region of blocks
MACRUM 0:119624335925 86 * is no longer in use and may be erased without side effects. Erase must still be called
MACRUM 0:119624335925 87 * before programming, but trimming allows flash-translation-layers to schedule erases when
MACRUM 0:119624335925 88 * the device is not busy.
MACRUM 0:119624335925 89 *
MACRUM 0:119624335925 90 * @param addr Address of block to mark as unused
MACRUM 0:119624335925 91 * @param size Size to mark as unused in bytes, must be a multiple of erase block size
MACRUM 0:119624335925 92 * @return 0 on success, negative error code on failure
MACRUM 0:119624335925 93 */
MACRUM 0:119624335925 94 virtual int trim(bd_addr_t addr, bd_size_t size);
MACRUM 0:119624335925 95
MACRUM 0:119624335925 96 /** Get the size of a readable block
MACRUM 0:119624335925 97 *
MACRUM 0:119624335925 98 * @return Size of a readable block in bytes
MACRUM 0:119624335925 99 */
MACRUM 0:119624335925 100 virtual bd_size_t get_read_size() const;
MACRUM 0:119624335925 101
MACRUM 0:119624335925 102 /** Get the size of a programable block
MACRUM 0:119624335925 103 *
MACRUM 0:119624335925 104 * @return Size of a programable block in bytes
MACRUM 0:119624335925 105 * @note Must be a multiple of the read size
MACRUM 0:119624335925 106 */
MACRUM 0:119624335925 107 virtual bd_size_t get_program_size() const;
MACRUM 0:119624335925 108
MACRUM 0:119624335925 109 /** Get the total size of the underlying device
MACRUM 0:119624335925 110 *
MACRUM 0:119624335925 111 * @return Size of the underlying device in bytes
MACRUM 0:119624335925 112 */
MACRUM 0:119624335925 113 virtual bd_size_t size() const;
MACRUM 0:119624335925 114
MACRUM 0:119624335925 115 /** Enable or disable debugging
MACRUM 0:119624335925 116 *
MACRUM 0:119624335925 117 * @param State of debugging
MACRUM 0:119624335925 118 */
MACRUM 0:119624335925 119 virtual void debug(bool dbg);
MACRUM 0:119624335925 120
MACRUM 0:119624335925 121 /** Set the transfer frequency
MACRUM 0:119624335925 122 *
MACRUM 0:119624335925 123 * @param Transfer frequency
MACRUM 0:119624335925 124 * @note Max frequency supported is 25MHZ
MACRUM 0:119624335925 125 */
MACRUM 0:119624335925 126 virtual int frequency(uint64_t freq);
MACRUM 0:119624335925 127
MACRUM 0:119624335925 128
MACRUM 0:119624335925 129 private:
MACRUM 0:119624335925 130 /* Commands : Listed below are commands supported
MACRUM 0:119624335925 131 * in SPI mode for SD card : Only Mandatory ones
MACRUM 0:119624335925 132 */
MACRUM 0:119624335925 133 enum cmdSupported {
MACRUM 0:119624335925 134 CMD_NOT_SUPPORTED = -1, /**< Command not supported error */
MACRUM 0:119624335925 135 CMD0_GO_IDLE_STATE = 0, /**< Resets the SD Memory Card */
MACRUM 0:119624335925 136 CMD1_SEND_OP_COND = 1, /**< Sends host capacity support */
MACRUM 0:119624335925 137 CMD6_SWITCH_FUNC = 6, /**< Check and Switches card function */
MACRUM 0:119624335925 138 CMD8_SEND_IF_COND = 8, /**< Supply voltage info */
MACRUM 0:119624335925 139 CMD9_SEND_CSD = 9, /**< Provides Card Specific data */
MACRUM 0:119624335925 140 CMD10_SEND_CID = 10, /**< Provides Card Identification */
MACRUM 0:119624335925 141 CMD12_STOP_TRANSMISSION = 12, /**< Forces the card to stop transmission */
MACRUM 0:119624335925 142 CMD13_SEND_STATUS = 13, /**< Card responds with status */
MACRUM 0:119624335925 143 CMD16_SET_BLOCKLEN = 16, /**< Length for SC card is set */
MACRUM 0:119624335925 144 CMD17_READ_SINGLE_BLOCK = 17, /**< Read single block of data */
MACRUM 0:119624335925 145 CMD18_READ_MULTIPLE_BLOCK = 18, /**< Card transfers data blocks to host until interrupted
MACRUM 0:119624335925 146 by a STOP_TRANSMISSION command */
MACRUM 0:119624335925 147 CMD24_WRITE_BLOCK = 24, /**< Write single block of data */
MACRUM 0:119624335925 148 CMD25_WRITE_MULTIPLE_BLOCK = 25, /**< Continuously writes blocks of data until
MACRUM 0:119624335925 149 'Stop Tran' token is sent */
MACRUM 0:119624335925 150 CMD27_PROGRAM_CSD = 27, /**< Programming bits of CSD */
MACRUM 0:119624335925 151 CMD32_ERASE_WR_BLK_START_ADDR = 32, /**< Sets the address of the first write
MACRUM 0:119624335925 152 block to be erased. */
MACRUM 0:119624335925 153 CMD33_ERASE_WR_BLK_END_ADDR = 33, /**< Sets the address of the last write
MACRUM 0:119624335925 154 block of the continuous range to be erased.*/
MACRUM 0:119624335925 155 CMD38_ERASE = 38, /**< Erases all previously selected write blocks */
MACRUM 0:119624335925 156 CMD55_APP_CMD = 55, /**< Extend to Applications specific commands */
MACRUM 0:119624335925 157 CMD56_GEN_CMD = 56, /**< General Purpose Command */
MACRUM 0:119624335925 158 CMD58_READ_OCR = 58, /**< Read OCR register of card */
MACRUM 0:119624335925 159 CMD59_CRC_ON_OFF = 59, /**< Turns the CRC option on or off*/
MACRUM 0:119624335925 160 // App Commands
MACRUM 0:119624335925 161 ACMD6_SET_BUS_WIDTH = 6,
MACRUM 0:119624335925 162 ACMD13_SD_STATUS = 13,
MACRUM 0:119624335925 163 ACMD22_SEND_NUM_WR_BLOCKS = 22,
MACRUM 0:119624335925 164 ACMD23_SET_WR_BLK_ERASE_COUNT = 23,
MACRUM 0:119624335925 165 ACMD41_SD_SEND_OP_COND = 41,
MACRUM 0:119624335925 166 ACMD42_SET_CLR_CARD_DETECT = 42,
MACRUM 0:119624335925 167 ACMD51_SEND_SCR = 51,
MACRUM 0:119624335925 168 };
MACRUM 0:119624335925 169
MACRUM 0:119624335925 170 uint8_t _card_type;
MACRUM 0:119624335925 171 int _cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAcmd=0, uint32_t *resp=NULL);
MACRUM 0:119624335925 172 int _cmd8();
MACRUM 0:119624335925 173
MACRUM 0:119624335925 174 /* Move the SDCard into the SPI Mode idle state
MACRUM 0:119624335925 175 *
MACRUM 0:119624335925 176 * The card is transitioned from SDCard mode to SPI mode by sending the
MACRUM 0:119624335925 177 * CMD0 (GO_IDLE_STATE) command with CS asserted. See the notes in the
MACRUM 0:119624335925 178 * "SPI Startup" section of the comments at the head of the
MACRUM 0:119624335925 179 * implementation file for further details and specification references.
MACRUM 0:119624335925 180 *
MACRUM 0:119624335925 181 * @return Response form the card. R1_IDLE_STATE (0x1), the successful
MACRUM 0:119624335925 182 * response from CMD0. R1_XXX_XXX for more response
MACRUM 0:119624335925 183 */
MACRUM 0:119624335925 184 uint32_t _go_idle_state();
MACRUM 0:119624335925 185 int _initialise_card();
MACRUM 0:119624335925 186
MACRUM 0:119624335925 187 bd_size_t _sectors;
MACRUM 0:119624335925 188 bd_size_t _sd_sectors();
MACRUM 0:119624335925 189
MACRUM 0:119624335925 190 bool _is_valid_trim(bd_addr_t addr, bd_size_t size);
MACRUM 0:119624335925 191
MACRUM 0:119624335925 192 /* SPI functions */
MACRUM 0:119624335925 193 Timer _spi_timer; /**< Timer Class object used for busy wait */
MACRUM 0:119624335925 194 uint32_t _init_sck; /**< Intial SPI frequency */
MACRUM 0:119624335925 195 uint32_t _transfer_sck; /**< SPI frequency during data transfer/after initialization */
MACRUM 0:119624335925 196 SPI _spi; /**< SPI Class object */
MACRUM 0:119624335925 197
MACRUM 0:119624335925 198 /* SPI initialization function */
MACRUM 0:119624335925 199 void _spi_init();
MACRUM 0:119624335925 200 uint8_t _cmd_spi(SDBlockDevice::cmdSupported cmd, uint32_t arg);
MACRUM 0:119624335925 201 void _spi_wait(uint8_t count);
MACRUM 0:119624335925 202
MACRUM 0:119624335925 203 bool _wait_token(uint8_t token); /**< Wait for token */
MACRUM 0:119624335925 204 bool _wait_ready(uint16_t ms=300); /**< 300ms default wait for card to be ready */
MACRUM 0:119624335925 205 int _read(uint8_t * buffer, uint32_t length);
MACRUM 0:119624335925 206 int _read_bytes(uint8_t * buffer, uint32_t length);
MACRUM 0:119624335925 207 uint8_t _write(const uint8_t *buffer,uint8_t token, uint32_t length);
MACRUM 0:119624335925 208 int _freq(void);
MACRUM 0:119624335925 209
MACRUM 0:119624335925 210 /* Chip Select and SPI mode select */
MACRUM 0:119624335925 211 DigitalOut _cs;
MACRUM 0:119624335925 212 void _select();
MACRUM 0:119624335925 213 void _deselect();
MACRUM 0:119624335925 214
MACRUM 0:119624335925 215 virtual void lock() {
MACRUM 0:119624335925 216 _mutex.lock();
MACRUM 0:119624335925 217 }
MACRUM 0:119624335925 218
MACRUM 0:119624335925 219 virtual void unlock() {
MACRUM 0:119624335925 220 _mutex.unlock();
MACRUM 0:119624335925 221 }
MACRUM 0:119624335925 222
MACRUM 0:119624335925 223 PlatformMutex _mutex;
MACRUM 0:119624335925 224 bd_size_t _block_size;
MACRUM 0:119624335925 225 bd_size_t _erase_size;
MACRUM 0:119624335925 226 bool _is_initialized;
MACRUM 0:119624335925 227 bool _dbg;
MACRUM 0:119624335925 228 };
MACRUM 0:119624335925 229
MACRUM 0:119624335925 230 #endif /* DEVICE_SPI */
MACRUM 0:119624335925 231
MACRUM 0:119624335925 232 #endif /* MBED_SD_BLOCK_DEVICE_H */