Generic Pelion Device Management example for various Advantech modules.
This example is known to work great on the following platforms:
- WISE-1530 WiFi Module using DB-1505 carrier board and external SD card reader.
Example Functionality
This example showcases the following device functionality:
- On timer button increment, simulate Pelion LWM2M button resource change
Use this example with Mbed CLI
1. Import the application into your desktop:
mbed import https://os.mbed.com/teams/Advantech/code/pelion-example-common cd pelion-example-common
2. Download your developer certificate from pelion portal
3. Compile the program
mbed compile -t <toolchain> -m <TARGET_BOARD>
(supported toolchains : GCC_ARM / ARM / IAR)
4. Copy the binary file pelion-example-common.bin to your mbed device.
drivers/storage/COMPONENT_NUSD/NuSDBlockDevice.h@0:43ff9e3bc244, 2019-03-12 (annotated)
- Committer:
- chuanga
- Date:
- Tue Mar 12 13:48:39 2019 +0800
- Revision:
- 0:43ff9e3bc244
copying sources from github repository
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chuanga | 0:43ff9e3bc244 | 1 | /* mbed Microcontroller Library |
chuanga | 0:43ff9e3bc244 | 2 | * Copyright (c) 2015-2016 Nuvoton |
chuanga | 0:43ff9e3bc244 | 3 | * |
chuanga | 0:43ff9e3bc244 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
chuanga | 0:43ff9e3bc244 | 5 | * you may not use this file except in compliance with the License. |
chuanga | 0:43ff9e3bc244 | 6 | * You may obtain a copy of the License at |
chuanga | 0:43ff9e3bc244 | 7 | * |
chuanga | 0:43ff9e3bc244 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
chuanga | 0:43ff9e3bc244 | 9 | * |
chuanga | 0:43ff9e3bc244 | 10 | * Unless required by applicable law or agreed to in writing, software |
chuanga | 0:43ff9e3bc244 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
chuanga | 0:43ff9e3bc244 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
chuanga | 0:43ff9e3bc244 | 13 | * See the License for the specific language governing permissions and |
chuanga | 0:43ff9e3bc244 | 14 | * limitations under the License. |
chuanga | 0:43ff9e3bc244 | 15 | */ |
chuanga | 0:43ff9e3bc244 | 16 | #ifndef __NU_SD_BLOCK_DEVICE_H__ |
chuanga | 0:43ff9e3bc244 | 17 | #define __NU_SD_BLOCK_DEVICE_H__ |
chuanga | 0:43ff9e3bc244 | 18 | |
chuanga | 0:43ff9e3bc244 | 19 | #if TARGET_NUVOTON |
chuanga | 0:43ff9e3bc244 | 20 | |
chuanga | 0:43ff9e3bc244 | 21 | #include "BlockDevice.h" |
chuanga | 0:43ff9e3bc244 | 22 | #include "platform/PlatformMutex.h" |
chuanga | 0:43ff9e3bc244 | 23 | #include "mbed.h" |
chuanga | 0:43ff9e3bc244 | 24 | |
chuanga | 0:43ff9e3bc244 | 25 | struct nu_modinit_s; |
chuanga | 0:43ff9e3bc244 | 26 | |
chuanga | 0:43ff9e3bc244 | 27 | class NuSDBlockDevice : public BlockDevice { |
chuanga | 0:43ff9e3bc244 | 28 | public: |
chuanga | 0:43ff9e3bc244 | 29 | /** Lifetime of an SD card |
chuanga | 0:43ff9e3bc244 | 30 | */ |
chuanga | 0:43ff9e3bc244 | 31 | NuSDBlockDevice(); |
chuanga | 0:43ff9e3bc244 | 32 | NuSDBlockDevice(PinName sd_dat0, PinName sd_dat1, PinName sd_dat2, PinName sd_dat3, |
chuanga | 0:43ff9e3bc244 | 33 | PinName sd_cmd, PinName sd_clk, PinName sd_cdn); |
chuanga | 0:43ff9e3bc244 | 34 | virtual ~NuSDBlockDevice(); |
chuanga | 0:43ff9e3bc244 | 35 | |
chuanga | 0:43ff9e3bc244 | 36 | /** Initialize a block device |
chuanga | 0:43ff9e3bc244 | 37 | * |
chuanga | 0:43ff9e3bc244 | 38 | * @return 0 on success or a negative error code on failure |
chuanga | 0:43ff9e3bc244 | 39 | */ |
chuanga | 0:43ff9e3bc244 | 40 | virtual int init(); |
chuanga | 0:43ff9e3bc244 | 41 | |
chuanga | 0:43ff9e3bc244 | 42 | /** Deinitialize a block device |
chuanga | 0:43ff9e3bc244 | 43 | * |
chuanga | 0:43ff9e3bc244 | 44 | * @return 0 on success or a negative error code on failure |
chuanga | 0:43ff9e3bc244 | 45 | */ |
chuanga | 0:43ff9e3bc244 | 46 | virtual int deinit(); |
chuanga | 0:43ff9e3bc244 | 47 | |
chuanga | 0:43ff9e3bc244 | 48 | /** Read blocks from a block device |
chuanga | 0:43ff9e3bc244 | 49 | * |
chuanga | 0:43ff9e3bc244 | 50 | * @param buffer Buffer to write blocks to |
chuanga | 0:43ff9e3bc244 | 51 | * @param addr Address of block to begin reading from |
chuanga | 0:43ff9e3bc244 | 52 | * @param size Size to read in bytes, must be a multiple of read block size |
chuanga | 0:43ff9e3bc244 | 53 | * @return 0 on success, negative error code on failure |
chuanga | 0:43ff9e3bc244 | 54 | */ |
chuanga | 0:43ff9e3bc244 | 55 | virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); |
chuanga | 0:43ff9e3bc244 | 56 | |
chuanga | 0:43ff9e3bc244 | 57 | /** Program blocks to a block device |
chuanga | 0:43ff9e3bc244 | 58 | * |
chuanga | 0:43ff9e3bc244 | 59 | * The blocks must have been erased prior to being programmed |
chuanga | 0:43ff9e3bc244 | 60 | * |
chuanga | 0:43ff9e3bc244 | 61 | * @param buffer Buffer of data to write to blocks |
chuanga | 0:43ff9e3bc244 | 62 | * @param addr Address of block to begin writing to |
chuanga | 0:43ff9e3bc244 | 63 | * @param size Size to write in bytes, must be a multiple of program block size |
chuanga | 0:43ff9e3bc244 | 64 | * @return 0 on success, negative error code on failure |
chuanga | 0:43ff9e3bc244 | 65 | */ |
chuanga | 0:43ff9e3bc244 | 66 | virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); |
chuanga | 0:43ff9e3bc244 | 67 | |
chuanga | 0:43ff9e3bc244 | 68 | /** Erase blocks on a block device |
chuanga | 0:43ff9e3bc244 | 69 | * |
chuanga | 0:43ff9e3bc244 | 70 | * The state of an erased block is undefined until it has been programmed |
chuanga | 0:43ff9e3bc244 | 71 | * |
chuanga | 0:43ff9e3bc244 | 72 | * @param addr Address of block to begin erasing |
chuanga | 0:43ff9e3bc244 | 73 | * @param size Size to erase in bytes, must be a multiple of erase block size |
chuanga | 0:43ff9e3bc244 | 74 | * @return 0 on success, negative error code on failure |
chuanga | 0:43ff9e3bc244 | 75 | */ |
chuanga | 0:43ff9e3bc244 | 76 | virtual int erase(bd_addr_t addr, bd_size_t size); |
chuanga | 0:43ff9e3bc244 | 77 | |
chuanga | 0:43ff9e3bc244 | 78 | /** Get the size of a readable block |
chuanga | 0:43ff9e3bc244 | 79 | * |
chuanga | 0:43ff9e3bc244 | 80 | * @return Size of a readable block in bytes |
chuanga | 0:43ff9e3bc244 | 81 | */ |
chuanga | 0:43ff9e3bc244 | 82 | virtual bd_size_t get_read_size() const; |
chuanga | 0:43ff9e3bc244 | 83 | |
chuanga | 0:43ff9e3bc244 | 84 | /** Get the size of a programable block |
chuanga | 0:43ff9e3bc244 | 85 | * |
chuanga | 0:43ff9e3bc244 | 86 | * @return Size of a programable block in bytes |
chuanga | 0:43ff9e3bc244 | 87 | * @note Must be a multiple of the read size |
chuanga | 0:43ff9e3bc244 | 88 | */ |
chuanga | 0:43ff9e3bc244 | 89 | virtual bd_size_t get_program_size() const; |
chuanga | 0:43ff9e3bc244 | 90 | |
chuanga | 0:43ff9e3bc244 | 91 | /** Get the size of an erasable block |
chuanga | 0:43ff9e3bc244 | 92 | * |
chuanga | 0:43ff9e3bc244 | 93 | * @return Size of an erasable block in bytes |
chuanga | 0:43ff9e3bc244 | 94 | * @note Must be a multiple of the program size |
chuanga | 0:43ff9e3bc244 | 95 | */ |
chuanga | 0:43ff9e3bc244 | 96 | virtual bd_size_t get_erase_size() const; |
chuanga | 0:43ff9e3bc244 | 97 | |
chuanga | 0:43ff9e3bc244 | 98 | /** Get the size of an erasable block given address |
chuanga | 0:43ff9e3bc244 | 99 | * |
chuanga | 0:43ff9e3bc244 | 100 | * @param addr Address within the erasable block |
chuanga | 0:43ff9e3bc244 | 101 | * @return Size of an erasable block in bytes |
chuanga | 0:43ff9e3bc244 | 102 | * @note Must be a multiple of the program size |
chuanga | 0:43ff9e3bc244 | 103 | */ |
chuanga | 0:43ff9e3bc244 | 104 | virtual bd_size_t get_erase_size(bd_addr_t addr) const; |
chuanga | 0:43ff9e3bc244 | 105 | |
chuanga | 0:43ff9e3bc244 | 106 | /** Get the total size of the underlying device |
chuanga | 0:43ff9e3bc244 | 107 | * |
chuanga | 0:43ff9e3bc244 | 108 | * @return Size of the underlying device in bytes |
chuanga | 0:43ff9e3bc244 | 109 | */ |
chuanga | 0:43ff9e3bc244 | 110 | virtual bd_size_t size() const; |
chuanga | 0:43ff9e3bc244 | 111 | |
chuanga | 0:43ff9e3bc244 | 112 | /** Enable or disable debugging |
chuanga | 0:43ff9e3bc244 | 113 | * |
chuanga | 0:43ff9e3bc244 | 114 | * @param State of debugging |
chuanga | 0:43ff9e3bc244 | 115 | */ |
chuanga | 0:43ff9e3bc244 | 116 | virtual void debug(bool dbg); |
chuanga | 0:43ff9e3bc244 | 117 | |
chuanga | 0:43ff9e3bc244 | 118 | /** Get the BlockDevice class type. |
chuanga | 0:43ff9e3bc244 | 119 | * |
chuanga | 0:43ff9e3bc244 | 120 | * @return A string representation of the BlockDevice class type. |
chuanga | 0:43ff9e3bc244 | 121 | */ |
chuanga | 0:43ff9e3bc244 | 122 | virtual const char *get_type() const; |
chuanga | 0:43ff9e3bc244 | 123 | |
chuanga | 0:43ff9e3bc244 | 124 | private: |
chuanga | 0:43ff9e3bc244 | 125 | int _init_sdh(); |
chuanga | 0:43ff9e3bc244 | 126 | uint32_t _sd_sectors(); |
chuanga | 0:43ff9e3bc244 | 127 | void _sdh_irq(); |
chuanga | 0:43ff9e3bc244 | 128 | |
chuanga | 0:43ff9e3bc244 | 129 | uint32_t _sectors; |
chuanga | 0:43ff9e3bc244 | 130 | bool _dbg; |
chuanga | 0:43ff9e3bc244 | 131 | PlatformMutex _lock; |
chuanga | 0:43ff9e3bc244 | 132 | |
chuanga | 0:43ff9e3bc244 | 133 | const struct nu_modinit_s * _sdh_modinit; |
chuanga | 0:43ff9e3bc244 | 134 | SDName _sdh; |
chuanga | 0:43ff9e3bc244 | 135 | SDH_T * _sdh_base; |
chuanga | 0:43ff9e3bc244 | 136 | #if TARGET_NUMAKER_PFM_NUC472 |
chuanga | 0:43ff9e3bc244 | 137 | uint32_t _sdh_port; |
chuanga | 0:43ff9e3bc244 | 138 | #endif |
chuanga | 0:43ff9e3bc244 | 139 | |
chuanga | 0:43ff9e3bc244 | 140 | CThunk<NuSDBlockDevice> _sdh_irq_thunk; |
chuanga | 0:43ff9e3bc244 | 141 | |
chuanga | 0:43ff9e3bc244 | 142 | PinName _sd_dat0; |
chuanga | 0:43ff9e3bc244 | 143 | PinName _sd_dat1; |
chuanga | 0:43ff9e3bc244 | 144 | PinName _sd_dat2; |
chuanga | 0:43ff9e3bc244 | 145 | PinName _sd_dat3; |
chuanga | 0:43ff9e3bc244 | 146 | PinName _sd_cmd; |
chuanga | 0:43ff9e3bc244 | 147 | PinName _sd_clk; |
chuanga | 0:43ff9e3bc244 | 148 | PinName _sd_cdn; |
chuanga | 0:43ff9e3bc244 | 149 | |
chuanga | 0:43ff9e3bc244 | 150 | bool _is_initialized; |
chuanga | 0:43ff9e3bc244 | 151 | uint32_t _init_ref_count; |
chuanga | 0:43ff9e3bc244 | 152 | }; |
chuanga | 0:43ff9e3bc244 | 153 | |
chuanga | 0:43ff9e3bc244 | 154 | #endif /* TARGET_NUVOTON */ |
chuanga | 0:43ff9e3bc244 | 155 | #endif /* __NU_SD_BLOCK_DEVICE_H__ */ |