Advantech / Mbed OS pelion-example-common
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NuSDBlockDevice.h Source File

NuSDBlockDevice.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2015-2016 Nuvoton
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef __NU_SD_BLOCK_DEVICE_H__
00017 #define __NU_SD_BLOCK_DEVICE_H__
00018 
00019 #if TARGET_NUVOTON
00020 
00021 #include "BlockDevice.h"
00022 #include "platform/PlatformMutex.h"
00023 #include "mbed.h"
00024 
00025 struct nu_modinit_s;
00026 
00027 class NuSDBlockDevice : public BlockDevice {
00028 public:
00029     /** Lifetime of an SD card
00030      */
00031     NuSDBlockDevice();
00032     NuSDBlockDevice(PinName sd_dat0, PinName sd_dat1, PinName sd_dat2, PinName sd_dat3,
00033         PinName sd_cmd, PinName sd_clk, PinName sd_cdn);
00034     virtual ~NuSDBlockDevice();
00035 
00036     /** Initialize a block device
00037      *
00038      *  @return         0 on success or a negative error code on failure
00039      */
00040     virtual int init();
00041 
00042     /** Deinitialize a block device
00043      *
00044      *  @return         0 on success or a negative error code on failure
00045      */
00046     virtual int deinit();
00047 
00048     /** Read blocks from a block device
00049      *
00050      *  @param buffer   Buffer to write blocks to
00051      *  @param addr     Address of block to begin reading from
00052      *  @param size     Size to read in bytes, must be a multiple of read block size
00053      *  @return         0 on success, negative error code on failure
00054      */
00055     virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
00056 
00057     /** Program blocks to a block device
00058      *
00059      *  The blocks must have been erased prior to being programmed
00060      *
00061      *  @param buffer   Buffer of data to write to blocks
00062      *  @param addr     Address of block to begin writing to
00063      *  @param size     Size to write in bytes, must be a multiple of program block size
00064      *  @return         0 on success, negative error code on failure
00065      */
00066     virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
00067 
00068     /** Erase blocks on a block device
00069      *
00070      *  The state of an erased block is undefined until it has been programmed
00071      *
00072      *  @param addr     Address of block to begin erasing
00073      *  @param size     Size to erase in bytes, must be a multiple of erase block size
00074      *  @return         0 on success, negative error code on failure
00075      */
00076     virtual int erase(bd_addr_t addr, bd_size_t size);
00077 
00078     /** Get the size of a readable block
00079      *
00080      *  @return         Size of a readable block in bytes
00081      */
00082     virtual bd_size_t get_read_size() const;
00083 
00084     /** Get the size of a programable block
00085      *
00086      *  @return         Size of a programable block in bytes
00087      *  @note Must be a multiple of the read size
00088      */
00089     virtual bd_size_t get_program_size() const;
00090 
00091     /** Get the size of an erasable block
00092      *
00093      *  @return         Size of an erasable block in bytes
00094      *  @note Must be a multiple of the program size
00095      */
00096     virtual bd_size_t get_erase_size() const;
00097 
00098     /** Get the size of an erasable block given address
00099      *
00100      *  @param addr     Address within the erasable block
00101      *  @return         Size of an erasable block in bytes
00102      *  @note Must be a multiple of the program size
00103      */
00104     virtual bd_size_t get_erase_size(bd_addr_t addr) const;
00105 
00106     /** Get the total size of the underlying device
00107      *
00108      *  @return         Size of the underlying device in bytes
00109      */
00110     virtual bd_size_t size() const;
00111 
00112     /** Enable or disable debugging
00113      *
00114      *  @param          State of debugging
00115      */
00116     virtual void debug(bool dbg);
00117     
00118     /** Get the BlockDevice class type.
00119      *
00120      *  @return         A string representation of the BlockDevice class type.
00121      */
00122     virtual const char *get_type() const;
00123 
00124 private:
00125     int _init_sdh();
00126     uint32_t _sd_sectors();
00127     void _sdh_irq();
00128 
00129     uint32_t _sectors;
00130     bool _dbg;
00131     PlatformMutex _lock;
00132     
00133     const struct nu_modinit_s * _sdh_modinit;
00134     SDName      _sdh;
00135     SDH_T *     _sdh_base;
00136 #if TARGET_NUMAKER_PFM_NUC472
00137     uint32_t    _sdh_port;
00138 #endif
00139 
00140     CThunk<NuSDBlockDevice>     _sdh_irq_thunk;
00141 
00142     PinName _sd_dat0;
00143     PinName _sd_dat1;
00144     PinName _sd_dat2;
00145     PinName _sd_dat3;
00146     PinName _sd_cmd;
00147     PinName _sd_clk;
00148     PinName _sd_cdn;
00149 
00150     bool                        _is_initialized;
00151     uint32_t                    _init_ref_count;
00152 };
00153 
00154 #endif  /* TARGET_NUVOTON */
00155 #endif  /* __NU_SD_BLOCK_DEVICE_H__ */