LIS3DH & BLE broadcast example for VTT Node V3 & mbed

Dependencies:   BLE_API TMP_nrf51 mbed nRF51822

Committer:
jejuho
Date:
Mon Jan 25 13:27:15 2016 +0000
Revision:
1:bd7fd35251ab
Parent:
0:de3e4a57ebe0
Initial version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jejuho 0:de3e4a57ebe0 1 /* mbed AT45 Library, for driving the Atmel AT45 series Dataflash with Serial Interface (SPI)
jejuho 0:de3e4a57ebe0 2 * Copyright (c) 2012, Created by Steen Joergensen (stjo2809) inspired by Chris Styles AT45 library
jejuho 0:de3e4a57ebe0 3 *
jejuho 0:de3e4a57ebe0 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
jejuho 0:de3e4a57ebe0 5 * of this software and associated documentation files (the "Software"), to deal
jejuho 0:de3e4a57ebe0 6 * in the Software without restriction, including without limitation the rights
jejuho 0:de3e4a57ebe0 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jejuho 0:de3e4a57ebe0 8 * copies of the Software, and to permit persons to whom the Software is
jejuho 0:de3e4a57ebe0 9 * furnished to do so, subject to the following conditions:
jejuho 0:de3e4a57ebe0 10 *
jejuho 0:de3e4a57ebe0 11 * The above copyright notice and this permission notice shall be included in
jejuho 0:de3e4a57ebe0 12 * all copies or substantial portions of the Software.
jejuho 0:de3e4a57ebe0 13 *
jejuho 0:de3e4a57ebe0 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jejuho 0:de3e4a57ebe0 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jejuho 0:de3e4a57ebe0 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jejuho 0:de3e4a57ebe0 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jejuho 0:de3e4a57ebe0 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jejuho 0:de3e4a57ebe0 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jejuho 0:de3e4a57ebe0 20 * THE SOFTWARE.
jejuho 0:de3e4a57ebe0 21 *
jejuho 0:de3e4a57ebe0 22 *
jejuho 0:de3e4a57ebe0 23 * This driver supports 021,041,081,161,321,641 variants of the AT45DBxxx family
jejuho 0:de3e4a57ebe0 24 *
jejuho 0:de3e4a57ebe0 25 * || Code || Density || Page size || Pages || Package ||
jejuho 0:de3e4a57ebe0 26 * || 021 || 2 || 256 || 1024 || 8 SOIC ||
jejuho 0:de3e4a57ebe0 27 * || 041 || 4 || 256 || 2048 || 8 SOIC ||
jejuho 0:de3e4a57ebe0 28 * || 081 || 8 || 256 || 4096 || 8 SOIC ||
jejuho 0:de3e4a57ebe0 29 * || 161 || 16 || 512 || 4096 || 8 SOIC ||
jejuho 0:de3e4a57ebe0 30 * || 321 || 32 || 512 || 8192 || 8 SOIC ||
jejuho 0:de3e4a57ebe0 31 * || 641 || 64 || 1024 || 8192 || 28 TSOP ||
jejuho 0:de3e4a57ebe0 32 */
jejuho 0:de3e4a57ebe0 33
jejuho 0:de3e4a57ebe0 34 #include "mbed.h"
jejuho 0:de3e4a57ebe0 35
jejuho 0:de3e4a57ebe0 36 #ifndef AT45_H
jejuho 0:de3e4a57ebe0 37 #define AT45_H
jejuho 0:de3e4a57ebe0 38
jejuho 0:de3e4a57ebe0 39 //=============================================================================
jejuho 0:de3e4a57ebe0 40 // Functions Declaration
jejuho 0:de3e4a57ebe0 41 //=============================================================================
jejuho 0:de3e4a57ebe0 42
jejuho 0:de3e4a57ebe0 43 /** Interface to the Atmel AT45 series Dataflash with Serial Interface (SPI)
jejuho 0:de3e4a57ebe0 44 *
jejuho 0:de3e4a57ebe0 45 * Using the driver:
jejuho 0:de3e4a57ebe0 46 * - remenber to setup SPI in main routine.
jejuho 0:de3e4a57ebe0 47 * - remenber to setup Chipselect in main routine.
jejuho 0:de3e4a57ebe0 48 * - remenber to use FAT functions set pagesize to binary.
jejuho 0:de3e4a57ebe0 49 *
jejuho 0:de3e4a57ebe0 50 * Limitations of using this driver:
jejuho 0:de3e4a57ebe0 51 * - can't use lockdown functions.
jejuho 0:de3e4a57ebe0 52 * - can't use protections functions.
jejuho 0:de3e4a57ebe0 53 * - can't use security functions.
jejuho 0:de3e4a57ebe0 54 *
jejuho 0:de3e4a57ebe0 55 */
jejuho 0:de3e4a57ebe0 56 class AT45 {
jejuho 0:de3e4a57ebe0 57 public:
jejuho 0:de3e4a57ebe0 58 /** Create an instance of the AT45 connected to specfied SPI pins, with the specified address.
jejuho 0:de3e4a57ebe0 59 *
jejuho 0:de3e4a57ebe0 60 * @param spi The mbed SPI instance (make in main routine)
jejuho 0:de3e4a57ebe0 61 * @param nCs The SPI chip select pin.
jejuho 0:de3e4a57ebe0 62 */
jejuho 0:de3e4a57ebe0 63 AT45(SPI& spi, PinName ncs);
jejuho 0:de3e4a57ebe0 64
jejuho 0:de3e4a57ebe0 65 void initialize() { _initialize(); }
jejuho 0:de3e4a57ebe0 66
jejuho 0:de3e4a57ebe0 67 /** Read a byte.
jejuho 0:de3e4a57ebe0 68 *
jejuho 0:de3e4a57ebe0 69 * @param address The address of the byte to read.
jejuho 0:de3e4a57ebe0 70 * @return The data in the byte.
jejuho 0:de3e4a57ebe0 71 */
jejuho 0:de3e4a57ebe0 72 char read_byte(int address);
jejuho 0:de3e4a57ebe0 73
jejuho 0:de3e4a57ebe0 74 /** Read a page.
jejuho 0:de3e4a57ebe0 75 *
jejuho 0:de3e4a57ebe0 76 * @param data The data is pointer to a userdefined array that the page is read into.
jejuho 0:de3e4a57ebe0 77 * @param page The page number of the page to read (0 to device page size).
jejuho 0:de3e4a57ebe0 78 * @return Returns "0" or "-1" for error.
jejuho 0:de3e4a57ebe0 79 */
jejuho 0:de3e4a57ebe0 80 int read_page(char* data, int page);
jejuho 0:de3e4a57ebe0 81
jejuho 0:de3e4a57ebe0 82 /** Read a block (from 1 dimension array).
jejuho 0:de3e4a57ebe0 83 *
jejuho 0:de3e4a57ebe0 84 * @param data The data is pointer to a userdefined array that holds 4096 bytes of the data that is read into.
jejuho 0:de3e4a57ebe0 85 * @param block The block number of the block to read (0 to device block size).
jejuho 0:de3e4a57ebe0 86 * @return Returns "0" or "-1" for error.
jejuho 0:de3e4a57ebe0 87 */
jejuho 0:de3e4a57ebe0 88 int read_block(char *data, int block);
jejuho 0:de3e4a57ebe0 89
jejuho 0:de3e4a57ebe0 90 /** Read a block (from 2 dimension array).
jejuho 0:de3e4a57ebe0 91 *
jejuho 0:de3e4a57ebe0 92 * Then using 2 dimension array, the array shall have a starting point like "read_block(data[0],0)"
jejuho 0:de3e4a57ebe0 93 * @param data The data is pointer to a userdefined array that holds 8 x 512 bytes of the data that is read into.
jejuho 0:de3e4a57ebe0 94 * @param block The block number of the block to read (0 to device block size).
jejuho 0:de3e4a57ebe0 95 * @return Returns "0" or "-1" for error.
jejuho 0:de3e4a57ebe0 96 */
jejuho 0:de3e4a57ebe0 97 int read_block(char *data[], int block);
jejuho 0:de3e4a57ebe0 98
jejuho 0:de3e4a57ebe0 99 /** Write a byte.
jejuho 0:de3e4a57ebe0 100 *
jejuho 0:de3e4a57ebe0 101 * @param address The address to where the data is storage in the flash.
jejuho 0:de3e4a57ebe0 102 * @param data The data to write into the flash.
jejuho 0:de3e4a57ebe0 103 */
jejuho 0:de3e4a57ebe0 104 void write_byte(int address, char data);
jejuho 0:de3e4a57ebe0 105
jejuho 0:de3e4a57ebe0 106 /** Write a page.
jejuho 0:de3e4a57ebe0 107 *
jejuho 0:de3e4a57ebe0 108 * @param data The data is pointer to a userdefined array that holds the data to write into.
jejuho 0:de3e4a57ebe0 109 * @param page The page number of the page to write into (0 to device page size).
jejuho 0:de3e4a57ebe0 110 * @return Returns "0" or "-1" for error.
jejuho 0:de3e4a57ebe0 111 */
jejuho 0:de3e4a57ebe0 112 int write_page(char* data, int page);
jejuho 0:de3e4a57ebe0 113
jejuho 0:de3e4a57ebe0 114 /** Write a block (from 1 dimension array).
jejuho 0:de3e4a57ebe0 115 *
jejuho 0:de3e4a57ebe0 116 * @param data The data is pointer to a userdefined array that holds 4096 bytes of the data to write into.
jejuho 0:de3e4a57ebe0 117 * @param block The block number of the block to write into (0 to device block size).
jejuho 0:de3e4a57ebe0 118 * @return Returns "0" or "-1" for error.
jejuho 0:de3e4a57ebe0 119 */
jejuho 0:de3e4a57ebe0 120 int write_block(char *data, int block);
jejuho 0:de3e4a57ebe0 121
jejuho 0:de3e4a57ebe0 122 /** Write a block (from 2 dimension array).
jejuho 0:de3e4a57ebe0 123 *
jejuho 0:de3e4a57ebe0 124 * Then using 2 dimension array, the array shall have a starting point like "write_block(data[0],0)"
jejuho 0:de3e4a57ebe0 125 * @param data The data is pointer to a userdefined array that holds 8 x 512 bytes of the data to write into (remenber it has to be a 2 dimension array).
jejuho 0:de3e4a57ebe0 126 * @param block The block number of the block to write into (0 to device block size).
jejuho 0:de3e4a57ebe0 127 * @return Returns "0" or "-1" for error.
jejuho 0:de3e4a57ebe0 128 */
jejuho 0:de3e4a57ebe0 129 int write_block(char *data[], int block);
jejuho 0:de3e4a57ebe0 130
jejuho 0:de3e4a57ebe0 131 /** FAT Read (512 bits).
jejuho 0:de3e4a57ebe0 132 *
jejuho 0:de3e4a57ebe0 133 * Remenber to set page size to binary.
jejuho 0:de3e4a57ebe0 134 * @param data The data is pointer to a userdefined array that the page is read into.
jejuho 0:de3e4a57ebe0 135 * @param page The page number of the page to read (0 to device page size).
jejuho 0:de3e4a57ebe0 136 * @return Returns "0".
jejuho 0:de3e4a57ebe0 137 */
jejuho 0:de3e4a57ebe0 138 int FAT_read(char* data, int page);
jejuho 0:de3e4a57ebe0 139
jejuho 0:de3e4a57ebe0 140 /** FAT Write (512bits).
jejuho 0:de3e4a57ebe0 141 *
jejuho 0:de3e4a57ebe0 142 * Remenber to set page size to binary.
jejuho 0:de3e4a57ebe0 143 * @param data The data is pointer to a userdefined array that holds the data to write into.
jejuho 0:de3e4a57ebe0 144 * @param page The page number of the page to write into (0 to device page size).
jejuho 0:de3e4a57ebe0 145 * @return Returns "0" or "-1" for error.
jejuho 0:de3e4a57ebe0 146 */
jejuho 0:de3e4a57ebe0 147 int FAT_write(char* data, int page);
jejuho 0:de3e4a57ebe0 148
jejuho 0:de3e4a57ebe0 149 /** Function to erase the entire chip.
jejuho 0:de3e4a57ebe0 150 *
jejuho 0:de3e4a57ebe0 151 * Issue:
jejuho 0:de3e4a57ebe0 152 * In a certain percentage of units, the chip erase feature may not function correctly and may adversely affect device operation.
jejuho 0:de3e4a57ebe0 153 * Therefore, it is recommended that the chip erase commands (opcodes C7H, 94H, 80H, and 9AH) not be used.
jejuho 0:de3e4a57ebe0 154 * Workaround:
jejuho 0:de3e4a57ebe0 155 * Use block erase (opcode 50H) as an alternative. The block erase function is not affected by the chip erase issue.
jejuho 0:de3e4a57ebe0 156 * Resolution:
jejuho 0:de3e4a57ebe0 157 * The chip erase feature may be fixed with a new revision of the device. Please contact Atmel for the estimated availability of
jejuho 0:de3e4a57ebe0 158 * devices with the fix.
jejuho 0:de3e4a57ebe0 159 */
jejuho 0:de3e4a57ebe0 160 void chip_erase(void);
jejuho 0:de3e4a57ebe0 161
jejuho 0:de3e4a57ebe0 162 /** Function to erase the selected block.
jejuho 0:de3e4a57ebe0 163 *
jejuho 0:de3e4a57ebe0 164 * @param block The selected block to erase.
jejuho 0:de3e4a57ebe0 165 */
jejuho 0:de3e4a57ebe0 166 void block_erase(int block);
jejuho 0:de3e4a57ebe0 167
jejuho 0:de3e4a57ebe0 168 /** Function to erase the selected block.
jejuho 0:de3e4a57ebe0 169 *
jejuho 0:de3e4a57ebe0 170 * @param page The number of the page to erase.
jejuho 0:de3e4a57ebe0 171 */
jejuho 0:de3e4a57ebe0 172 void page_erase(int page);
jejuho 0:de3e4a57ebe0 173
jejuho 0:de3e4a57ebe0 174 /** Device size in mbits.
jejuho 0:de3e4a57ebe0 175 *
jejuho 0:de3e4a57ebe0 176 * @return device size.
jejuho 0:de3e4a57ebe0 177 */
jejuho 0:de3e4a57ebe0 178 int device_size(void);
jejuho 0:de3e4a57ebe0 179
jejuho 0:de3e4a57ebe0 180 /** Pages in flash.
jejuho 0:de3e4a57ebe0 181 *
jejuho 0:de3e4a57ebe0 182 * @return Numbers af pages.
jejuho 0:de3e4a57ebe0 183 */
jejuho 0:de3e4a57ebe0 184 int pages(void);
jejuho 0:de3e4a57ebe0 185
jejuho 0:de3e4a57ebe0 186 /** Page size.
jejuho 0:de3e4a57ebe0 187 *
jejuho 0:de3e4a57ebe0 188 * for 2-8 Mbits 256 or 264
jejuho 0:de3e4a57ebe0 189 * for 16-32 Mbits 512 or 528
jejuho 0:de3e4a57ebe0 190 * for 64 Mbits 1024 or 1056
jejuho 0:de3e4a57ebe0 191 *
jejuho 0:de3e4a57ebe0 192 * @return Page size.
jejuho 0:de3e4a57ebe0 193 */
jejuho 0:de3e4a57ebe0 194 int pagesize(void);
jejuho 0:de3e4a57ebe0 195
jejuho 0:de3e4a57ebe0 196 /** Function to set the page size to binary.
jejuho 0:de3e4a57ebe0 197 *
jejuho 0:de3e4a57ebe0 198 * Remenber is a one-time programmable configuration.
jejuho 0:de3e4a57ebe0 199 * Remenber to total power down after the functions has been run.
jejuho 0:de3e4a57ebe0 200 */
jejuho 0:de3e4a57ebe0 201 void set_pageszie_to_binary(void);
jejuho 0:de3e4a57ebe0 202
jejuho 0:de3e4a57ebe0 203 /** blocks in flash.
jejuho 0:de3e4a57ebe0 204 *
jejuho 0:de3e4a57ebe0 205 * @return Numbers af blocks.
jejuho 0:de3e4a57ebe0 206 */
jejuho 0:de3e4a57ebe0 207 int blocks(void);
jejuho 0:de3e4a57ebe0 208
jejuho 0:de3e4a57ebe0 209 /** ID of the device.
jejuho 0:de3e4a57ebe0 210 *
jejuho 0:de3e4a57ebe0 211 * @return Manufacturer, Family and Density code.
jejuho 0:de3e4a57ebe0 212 */
jejuho 0:de3e4a57ebe0 213 int id(void);
jejuho 0:de3e4a57ebe0 214
jejuho 0:de3e4a57ebe0 215 /** Status register.
jejuho 0:de3e4a57ebe0 216 *
jejuho 0:de3e4a57ebe0 217 * @return The status register.
jejuho 0:de3e4a57ebe0 218 */
jejuho 0:de3e4a57ebe0 219 int status(void); // Status register
jejuho 0:de3e4a57ebe0 220
jejuho 0:de3e4a57ebe0 221 /** busy ?.
jejuho 0:de3e4a57ebe0 222 *
jejuho 0:de3e4a57ebe0 223 * Function will want to the device is not busy.
jejuho 0:de3e4a57ebe0 224 */
jejuho 0:de3e4a57ebe0 225 void busy(void); // Wait until Flash is not busy
jejuho 0:de3e4a57ebe0 226
jejuho 0:de3e4a57ebe0 227 /** Deep Power Down.
jejuho 0:de3e4a57ebe0 228 *
jejuho 0:de3e4a57ebe0 229 * Remenber that you have to want 35uS after the wake up to use the device.
jejuho 0:de3e4a57ebe0 230 * @param True = Activate and False = Wake Up.
jejuho 0:de3e4a57ebe0 231 */
jejuho 0:de3e4a57ebe0 232 void deep_power_down(bool onoff);
jejuho 0:de3e4a57ebe0 233 void ultra_deep_power_down(bool onoff);
jejuho 0:de3e4a57ebe0 234
jejuho 0:de3e4a57ebe0 235 /** Is the device deep power down.
jejuho 0:de3e4a57ebe0 236 *
jejuho 0:de3e4a57ebe0 237 * @return True = Activate and False = Awake.
jejuho 0:de3e4a57ebe0 238 */
jejuho 0:de3e4a57ebe0 239 bool is_it_awake(void);
jejuho 0:de3e4a57ebe0 240
jejuho 0:de3e4a57ebe0 241 private:
jejuho 0:de3e4a57ebe0 242
jejuho 0:de3e4a57ebe0 243 SPI _spi;
jejuho 0:de3e4a57ebe0 244 DigitalOut _ncs;
jejuho 0:de3e4a57ebe0 245
jejuho 0:de3e4a57ebe0 246 int _pages; // Integer number of pages
jejuho 0:de3e4a57ebe0 247 int _pagesize; // page size, in bytes
jejuho 0:de3e4a57ebe0 248 int _devicesize; // device size in bytes
jejuho 0:de3e4a57ebe0 249 int _blocks; // Number of blocks
jejuho 0:de3e4a57ebe0 250 bool _deep_down; // True = the device is deep down
jejuho 0:de3e4a57ebe0 251 bool _deep_down_onoff; // variable for deep power down function (On/Off)
jejuho 0:de3e4a57ebe0 252
jejuho 0:de3e4a57ebe0 253 // Helper routunes
jejuho 0:de3e4a57ebe0 254 void _initialize();
jejuho 0:de3e4a57ebe0 255 void _select();
jejuho 0:de3e4a57ebe0 256 void _deselect();
jejuho 0:de3e4a57ebe0 257 void _busy (void);
jejuho 0:de3e4a57ebe0 258
jejuho 0:de3e4a57ebe0 259 // accessing SRAM buffers
jejuho 0:de3e4a57ebe0 260 void _sramwrite (int buffer, int address, int data);
jejuho 0:de3e4a57ebe0 261 int _sramread (int buffer, int address);
jejuho 0:de3e4a57ebe0 262
jejuho 0:de3e4a57ebe0 263 // Transferring SRAM buffers to/from FLASH
jejuho 0:de3e4a57ebe0 264 void _flashwrite (int buffer, int paddr);
jejuho 0:de3e4a57ebe0 265 void _flashread (int buffer, int paddr);
jejuho 0:de3e4a57ebe0 266
jejuho 0:de3e4a57ebe0 267 // Reading FLASH directly
jejuho 0:de3e4a57ebe0 268 int _memread (int address);
jejuho 0:de3e4a57ebe0 269
jejuho 0:de3e4a57ebe0 270 // Calculate page/subpage addresses
jejuho 0:de3e4a57ebe0 271 int _getpaddr (int);
jejuho 0:de3e4a57ebe0 272 int _getbaddr (int);
jejuho 0:de3e4a57ebe0 273
jejuho 0:de3e4a57ebe0 274 // Send 3 byte address
jejuho 0:de3e4a57ebe0 275 void _sendaddr (int address);
jejuho 0:de3e4a57ebe0 276
jejuho 0:de3e4a57ebe0 277 };
jejuho 0:de3e4a57ebe0 278 #endif
jejuho 0:de3e4a57ebe0 279
jejuho 0:de3e4a57ebe0 280