Latest version of my quadcopter controller with an LPC1768 and MPU9250.

Dependencies:   mbed

Currently running on a custom PCB with 30.5 x 30.5mm mounts. There are also 2 PC apps that go with the software; one to set up the PID controller and one to balance the motors and props. If anyone is interested, send me a message and I'll upload them.

Committer:
Anaesthetix
Date:
Tue Jul 31 20:36:57 2018 +0000
Revision:
8:981f7e2365b6
Parent:
0:0929d3d566cf
Switched from Madgwick to Mahony as I'm having trouble with slow oscillations caused by the madgwick filter. Fixed an error on the PID algorithm also.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anaesthetix 0:0929d3d566cf 1 /** IAP : internal Flash memory access library
Anaesthetix 0:0929d3d566cf 2 *
Anaesthetix 0:0929d3d566cf 3 * The internal Flash memory access is described in the LPC1768 and LPC11U24 usermanual.
Anaesthetix 0:0929d3d566cf 4 * http://www.nxp.com/documents/user_manual/UM10360.pdf
Anaesthetix 0:0929d3d566cf 5 * http://www.nxp.com/documents/user_manual/UM10462.pdf
Anaesthetix 0:0929d3d566cf 6 *
Anaesthetix 0:0929d3d566cf 7 * LPC1768 --
Anaesthetix 0:0929d3d566cf 8 * Chapter 2: "LPC17xx Memory map"
Anaesthetix 0:0929d3d566cf 9 * Chapter 32: "LPC17xx Flash memory interface and programming"
Anaesthetix 0:0929d3d566cf 10 * refering Rev. 01 - 4 January 2010
Anaesthetix 0:0929d3d566cf 11 *
Anaesthetix 0:0929d3d566cf 12 * LPC11U24 --
Anaesthetix 0:0929d3d566cf 13 * Chapter 2: "LPC11Uxx Memory mapping"
Anaesthetix 0:0929d3d566cf 14 * Chapter 20: "LPC11Uxx Flash programming firmware"
Anaesthetix 0:0929d3d566cf 15 * refering Rev. 03 - 16 July 2012
Anaesthetix 0:0929d3d566cf 16 *
Anaesthetix 0:0929d3d566cf 17 * Released under the MIT License: http://mbed.org/license/mit
Anaesthetix 0:0929d3d566cf 18 *
Anaesthetix 0:0929d3d566cf 19 * revision 1.0 09-Mar-2010 1st release
Anaesthetix 0:0929d3d566cf 20 * revision 1.1 12-Mar-2010 chaged: to make possible to reserve flash area for user
Anaesthetix 0:0929d3d566cf 21 * it can be set by USER_FLASH_AREA_START and USER_FLASH_AREA_SIZE in IAP.h
Anaesthetix 0:0929d3d566cf 22 * revision 2.0 26-Nov-2012 LPC11U24 code added
Anaesthetix 0:0929d3d566cf 23 * revision 2.1 26-Nov-2012 EEPROM access code imported from Suga koubou san's (http://mbed.org/users/okini3939/) library
Anaesthetix 0:0929d3d566cf 24 * http://mbed.org/users/okini3939/code/M0_EEPROM_test/
Anaesthetix 0:0929d3d566cf 25 * revision 3.0 09-Jan-2015 LPC812 and LPC824 support added
Anaesthetix 0:0929d3d566cf 26 * revision 3.1 13-Jan-2015 LPC1114 support added
Anaesthetix 0:0929d3d566cf 27 * revision 3.1.1 16-Jan-2015 Target MCU name changed for better compatibility across the platforms
Anaesthetix 0:0929d3d566cf 28 */
Anaesthetix 0:0929d3d566cf 29
Anaesthetix 0:0929d3d566cf 30
Anaesthetix 0:0929d3d566cf 31 #ifndef MBED_IAP
Anaesthetix 0:0929d3d566cf 32 #define MBED_IAP
Anaesthetix 0:0929d3d566cf 33
Anaesthetix 0:0929d3d566cf 34 #include "mbed.h"
Anaesthetix 0:0929d3d566cf 35
Anaesthetix 0:0929d3d566cf 36 #if defined(TARGET_LPC176X)
Anaesthetix 0:0929d3d566cf 37
Anaesthetix 0:0929d3d566cf 38 #define USER_FLASH_AREA_START FLASH_SECTOR_29
Anaesthetix 0:0929d3d566cf 39 #define USER_FLASH_AREA_SIZE (FLASH_SECTOR_SIZE_16_TO_29 * 1)
Anaesthetix 0:0929d3d566cf 40
Anaesthetix 0:0929d3d566cf 41 /*
Anaesthetix 0:0929d3d566cf 42 * memory map information is available in next URL also.
Anaesthetix 0:0929d3d566cf 43 * http://mbed.org/projects/libraries/svn/mbed/trunk/LPC1768/LPC17xx.h
Anaesthetix 0:0929d3d566cf 44 */
Anaesthetix 0:0929d3d566cf 45
Anaesthetix 0:0929d3d566cf 46 /** Table for start adress of sectors
Anaesthetix 0:0929d3d566cf 47 *
Anaesthetix 0:0929d3d566cf 48 * LPC1768 internal flash memory sector numbers and addresses
Anaesthetix 0:0929d3d566cf 49 *
Anaesthetix 0:0929d3d566cf 50 * LPC1768 flash memory are and sector number/size
Anaesthetix 0:0929d3d566cf 51 * Table 568 "Sectors in a LPC17xx device", Section 5. "Sector numbers", usermanual
Anaesthetix 0:0929d3d566cf 52 *
Anaesthetix 0:0929d3d566cf 53 * 0x00000000 - 0x0007FFFF flash (29 sectors)
Anaesthetix 0:0929d3d566cf 54 *
Anaesthetix 0:0929d3d566cf 55 * Sector0: 0x00000000 - 0x00000FFF 4K
Anaesthetix 0:0929d3d566cf 56 * Sector1: 0x00001000 - 0x00001FFF 4K
Anaesthetix 0:0929d3d566cf 57 * Sector2: 0x00002000 - 0x00002FFF 4K
Anaesthetix 0:0929d3d566cf 58 * Sector3: 0x00003000 - 0x00003FFF 4K
Anaesthetix 0:0929d3d566cf 59 * Sector4: 0x00004000 - 0x00004FFF 4K
Anaesthetix 0:0929d3d566cf 60 * Sector5: 0x00005000 - 0x00005FFF 4K
Anaesthetix 0:0929d3d566cf 61 * Sector6: 0x00006000 - 0x00006FFF 4K
Anaesthetix 0:0929d3d566cf 62 * Sector7: 0x00007000 - 0x00007FFF 4K
Anaesthetix 0:0929d3d566cf 63 * Sector8: 0x00008000 - 0x00008FFF 4K
Anaesthetix 0:0929d3d566cf 64 * Sector9: 0x00009000 - 0x00009FFF 4K
Anaesthetix 0:0929d3d566cf 65 * Sector10: 0x0000A000 - 0x0000AFFF 4K
Anaesthetix 0:0929d3d566cf 66 * Sector11: 0x0000B000 - 0x0000BFFF 4K
Anaesthetix 0:0929d3d566cf 67 * Sector12: 0x0000C000 - 0x0000CFFF 4K
Anaesthetix 0:0929d3d566cf 68 * Sector13: 0x0000D000 - 0x0000DFFF 4K
Anaesthetix 0:0929d3d566cf 69 * Sector14: 0x0000E000 - 0x0000EFFF 4K
Anaesthetix 0:0929d3d566cf 70 * Sector15: 0x0000F000 - 0x0000FFFF 4K
Anaesthetix 0:0929d3d566cf 71 *
Anaesthetix 0:0929d3d566cf 72 * Sector16: 0x00010000 - 0x00017FFF 32K
Anaesthetix 0:0929d3d566cf 73 * Sector17: 0x00018000 - 0x0001FFFF 32K
Anaesthetix 0:0929d3d566cf 74 * Sector18: 0x00020000 - 0x00027FFF 32K
Anaesthetix 0:0929d3d566cf 75 * Sector19: 0x00028000 - 0x0002FFFF 32K
Anaesthetix 0:0929d3d566cf 76 * Sector20: 0x00030000 - 0x00037FFF 32K
Anaesthetix 0:0929d3d566cf 77 * Sector21: 0x00038000 - 0x0003FFFF 32K
Anaesthetix 0:0929d3d566cf 78 * Sector22: 0x00040000 - 0x00047FFF 32K
Anaesthetix 0:0929d3d566cf 79 * Sector23: 0x00048000 - 0x0004FFFF 32K
Anaesthetix 0:0929d3d566cf 80 * Sector24: 0x00050000 - 0x00057FFF 32K
Anaesthetix 0:0929d3d566cf 81 * Sector25: 0x00058000 - 0x0005FFFF 32K
Anaesthetix 0:0929d3d566cf 82 * Sector26: 0x00060000 - 0x00067FFF 32K
Anaesthetix 0:0929d3d566cf 83 * Sector27: 0x00068000 - 0x0006FFFF 32K
Anaesthetix 0:0929d3d566cf 84 * Sector28: 0x00070000 - 0x00077FFF 32K
Anaesthetix 0:0929d3d566cf 85 * Sector29: 0x00078000 - 0x0007FFFF 32K
Anaesthetix 0:0929d3d566cf 86 */
Anaesthetix 0:0929d3d566cf 87
Anaesthetix 0:0929d3d566cf 88 #define FLASH_SECTOR_0 0x00000000
Anaesthetix 0:0929d3d566cf 89 #define FLASH_SECTOR_1 0x00001000
Anaesthetix 0:0929d3d566cf 90 #define FLASH_SECTOR_2 0x00002000
Anaesthetix 0:0929d3d566cf 91 #define FLASH_SECTOR_3 0x00003000
Anaesthetix 0:0929d3d566cf 92 #define FLASH_SECTOR_4 0x00004000
Anaesthetix 0:0929d3d566cf 93 #define FLASH_SECTOR_5 0x00005000
Anaesthetix 0:0929d3d566cf 94 #define FLASH_SECTOR_6 0x00006000
Anaesthetix 0:0929d3d566cf 95 #define FLASH_SECTOR_7 0x00007000
Anaesthetix 0:0929d3d566cf 96 #define FLASH_SECTOR_8 0x00008000
Anaesthetix 0:0929d3d566cf 97 #define FLASH_SECTOR_9 0x00009000
Anaesthetix 0:0929d3d566cf 98 #define FLASH_SECTOR_10 0x0000A000
Anaesthetix 0:0929d3d566cf 99 #define FLASH_SECTOR_11 0x0000B000
Anaesthetix 0:0929d3d566cf 100 #define FLASH_SECTOR_12 0x0000C000
Anaesthetix 0:0929d3d566cf 101 #define FLASH_SECTOR_13 0x0000D000
Anaesthetix 0:0929d3d566cf 102 #define FLASH_SECTOR_14 0x0000E000
Anaesthetix 0:0929d3d566cf 103 #define FLASH_SECTOR_15 0x0000F000
Anaesthetix 0:0929d3d566cf 104 #define FLASH_SECTOR_16 0x00010000
Anaesthetix 0:0929d3d566cf 105 #define FLASH_SECTOR_17 0x00018000
Anaesthetix 0:0929d3d566cf 106 #define FLASH_SECTOR_18 0x00020000
Anaesthetix 0:0929d3d566cf 107 #define FLASH_SECTOR_19 0x00028000
Anaesthetix 0:0929d3d566cf 108 #define FLASH_SECTOR_20 0x00030000
Anaesthetix 0:0929d3d566cf 109 #define FLASH_SECTOR_21 0x00038000
Anaesthetix 0:0929d3d566cf 110 #define FLASH_SECTOR_22 0x00040000
Anaesthetix 0:0929d3d566cf 111 #define FLASH_SECTOR_23 0x00048000
Anaesthetix 0:0929d3d566cf 112 #define FLASH_SECTOR_24 0x00050000
Anaesthetix 0:0929d3d566cf 113 #define FLASH_SECTOR_25 0x00058000
Anaesthetix 0:0929d3d566cf 114 #define FLASH_SECTOR_26 0x00060000
Anaesthetix 0:0929d3d566cf 115 #define FLASH_SECTOR_27 0x00068000
Anaesthetix 0:0929d3d566cf 116 #define FLASH_SECTOR_28 0x00070000
Anaesthetix 0:0929d3d566cf 117 #define FLASH_SECTOR_29 0x00078000
Anaesthetix 0:0929d3d566cf 118 #define FLASH_SECTOR_SIZE_0_TO_15 ( 4 * 1024)
Anaesthetix 0:0929d3d566cf 119 #define FLASH_SECTOR_SIZE_16_TO_29 (32 * 1024)
Anaesthetix 0:0929d3d566cf 120
Anaesthetix 0:0929d3d566cf 121 static char * sector_start_adress[] = {
Anaesthetix 0:0929d3d566cf 122 (char *)FLASH_SECTOR_0,
Anaesthetix 0:0929d3d566cf 123 (char *)FLASH_SECTOR_1,
Anaesthetix 0:0929d3d566cf 124 (char *)FLASH_SECTOR_2,
Anaesthetix 0:0929d3d566cf 125 (char *)FLASH_SECTOR_3,
Anaesthetix 0:0929d3d566cf 126 (char *)FLASH_SECTOR_4,
Anaesthetix 0:0929d3d566cf 127 (char *)FLASH_SECTOR_5,
Anaesthetix 0:0929d3d566cf 128 (char *)FLASH_SECTOR_6,
Anaesthetix 0:0929d3d566cf 129 (char *)FLASH_SECTOR_7,
Anaesthetix 0:0929d3d566cf 130 (char *)FLASH_SECTOR_8,
Anaesthetix 0:0929d3d566cf 131 (char *)FLASH_SECTOR_9,
Anaesthetix 0:0929d3d566cf 132 (char *)FLASH_SECTOR_10,
Anaesthetix 0:0929d3d566cf 133 (char *)FLASH_SECTOR_11,
Anaesthetix 0:0929d3d566cf 134 (char *)FLASH_SECTOR_12,
Anaesthetix 0:0929d3d566cf 135 (char *)FLASH_SECTOR_13,
Anaesthetix 0:0929d3d566cf 136 (char *)FLASH_SECTOR_14,
Anaesthetix 0:0929d3d566cf 137 (char *)FLASH_SECTOR_15,
Anaesthetix 0:0929d3d566cf 138 (char *)FLASH_SECTOR_16,
Anaesthetix 0:0929d3d566cf 139 (char *)FLASH_SECTOR_17,
Anaesthetix 0:0929d3d566cf 140 (char *)FLASH_SECTOR_18,
Anaesthetix 0:0929d3d566cf 141 (char *)FLASH_SECTOR_19,
Anaesthetix 0:0929d3d566cf 142 (char *)FLASH_SECTOR_20,
Anaesthetix 0:0929d3d566cf 143 (char *)FLASH_SECTOR_21,
Anaesthetix 0:0929d3d566cf 144 (char *)FLASH_SECTOR_22,
Anaesthetix 0:0929d3d566cf 145 (char *)FLASH_SECTOR_23,
Anaesthetix 0:0929d3d566cf 146 (char *)FLASH_SECTOR_24,
Anaesthetix 0:0929d3d566cf 147 (char *)FLASH_SECTOR_25,
Anaesthetix 0:0929d3d566cf 148 (char *)FLASH_SECTOR_26,
Anaesthetix 0:0929d3d566cf 149 (char *)FLASH_SECTOR_27,
Anaesthetix 0:0929d3d566cf 150 (char *)FLASH_SECTOR_28,
Anaesthetix 0:0929d3d566cf 151 (char *)FLASH_SECTOR_29
Anaesthetix 0:0929d3d566cf 152 };
Anaesthetix 0:0929d3d566cf 153
Anaesthetix 0:0929d3d566cf 154 #elif defined(TARGET_LPC11UXX) || defined(TARGET_LPC11XX)
Anaesthetix 0:0929d3d566cf 155
Anaesthetix 0:0929d3d566cf 156 #define USER_FLASH_AREA_START FLASH_SECTOR_7
Anaesthetix 0:0929d3d566cf 157 #define USER_FLASH_AREA_SIZE (FLASH_SECTOR_SIZE * 1)
Anaesthetix 0:0929d3d566cf 158
Anaesthetix 0:0929d3d566cf 159 /** Table for start adress of sectors
Anaesthetix 0:0929d3d566cf 160 *
Anaesthetix 0:0929d3d566cf 161 * LPC11U24 internal flash memory sector numbers and addresses
Anaesthetix 0:0929d3d566cf 162 *
Anaesthetix 0:0929d3d566cf 163 * LPC11U24 flash memory are and sector number/size
Anaesthetix 0:0929d3d566cf 164 * Table 334 "LPC11U1x/2x flash sectors", Section 20. "Sector numbers", usermanual
Anaesthetix 0:0929d3d566cf 165 *
Anaesthetix 0:0929d3d566cf 166 * 0x00000000 - 0x00007FFF flash (8 sectors)
Anaesthetix 0:0929d3d566cf 167 *
Anaesthetix 0:0929d3d566cf 168 * Sector0: 0x00000000 - 0x00000FFF 4K
Anaesthetix 0:0929d3d566cf 169 * Sector1: 0x00001000 - 0x00001FFF 4K
Anaesthetix 0:0929d3d566cf 170 * Sector2: 0x00002000 - 0x00002FFF 4K
Anaesthetix 0:0929d3d566cf 171 * Sector3: 0x00003000 - 0x00003FFF 4K
Anaesthetix 0:0929d3d566cf 172 * Sector4: 0x00004000 - 0x00004FFF 4K
Anaesthetix 0:0929d3d566cf 173 * Sector5: 0x00005000 - 0x00005FFF 4K
Anaesthetix 0:0929d3d566cf 174 * Sector6: 0x00006000 - 0x00006FFF 4K
Anaesthetix 0:0929d3d566cf 175 * Sector7: 0x00007000 - 0x00007FFF 4K
Anaesthetix 0:0929d3d566cf 176 */
Anaesthetix 0:0929d3d566cf 177
Anaesthetix 0:0929d3d566cf 178 #define FLASH_SECTOR_0 0x00000000
Anaesthetix 0:0929d3d566cf 179 #define FLASH_SECTOR_1 0x00001000
Anaesthetix 0:0929d3d566cf 180 #define FLASH_SECTOR_2 0x00002000
Anaesthetix 0:0929d3d566cf 181 #define FLASH_SECTOR_3 0x00003000
Anaesthetix 0:0929d3d566cf 182 #define FLASH_SECTOR_4 0x00004000
Anaesthetix 0:0929d3d566cf 183 #define FLASH_SECTOR_5 0x00005000
Anaesthetix 0:0929d3d566cf 184 #define FLASH_SECTOR_6 0x00006000
Anaesthetix 0:0929d3d566cf 185 #define FLASH_SECTOR_7 0x00007000
Anaesthetix 0:0929d3d566cf 186 #define FLASH_SECTOR_SIZE (4 * 1024)
Anaesthetix 0:0929d3d566cf 187
Anaesthetix 0:0929d3d566cf 188 static char * sector_start_adress[] = {
Anaesthetix 0:0929d3d566cf 189 (char *)FLASH_SECTOR_0,
Anaesthetix 0:0929d3d566cf 190 (char *)FLASH_SECTOR_1,
Anaesthetix 0:0929d3d566cf 191 (char *)FLASH_SECTOR_2,
Anaesthetix 0:0929d3d566cf 192 (char *)FLASH_SECTOR_3,
Anaesthetix 0:0929d3d566cf 193 (char *)FLASH_SECTOR_4,
Anaesthetix 0:0929d3d566cf 194 (char *)FLASH_SECTOR_5,
Anaesthetix 0:0929d3d566cf 195 (char *)FLASH_SECTOR_6,
Anaesthetix 0:0929d3d566cf 196 (char *)FLASH_SECTOR_7,
Anaesthetix 0:0929d3d566cf 197 };
Anaesthetix 0:0929d3d566cf 198
Anaesthetix 0:0929d3d566cf 199 #elif defined(TARGET_LPC81X) || defined(TARGET_LPC82X)
Anaesthetix 0:0929d3d566cf 200
Anaesthetix 0:0929d3d566cf 201 #define USER_FLASH_AREA_START FLASH_SECTOR_15
Anaesthetix 0:0929d3d566cf 202 #define USER_FLASH_AREA_SIZE (FLASH_SECTOR_SIZE * 1)
Anaesthetix 0:0929d3d566cf 203
Anaesthetix 0:0929d3d566cf 204 /** Table for start adress of sectors
Anaesthetix 0:0929d3d566cf 205 *
Anaesthetix 0:0929d3d566cf 206 * LPC812/LPC824 internal flash memory sector numbers and addresses
Anaesthetix 0:0929d3d566cf 207 *
Anaesthetix 0:0929d3d566cf 208 * 0x00000000 - 0x00003FFF flash (16 sectors for LPC812)
Anaesthetix 0:0929d3d566cf 209 * 0x00000000 - 0x00007FFF flash (32 sectors for LPC824)
Anaesthetix 0:0929d3d566cf 210 *
Anaesthetix 0:0929d3d566cf 211 * Sector0: 0x00000000 - 0x000003FF 1K
Anaesthetix 0:0929d3d566cf 212 * Sector1: 0x00000400 - 0x000007FF 1K
Anaesthetix 0:0929d3d566cf 213 * Sector2: 0x00000800 - 0x00000BFF 1K
Anaesthetix 0:0929d3d566cf 214 * Sector3: 0x00000C00 - 0x00000FFF 1K
Anaesthetix 0:0929d3d566cf 215 * Sector4: 0x00001000 - 0x000013FF 1K
Anaesthetix 0:0929d3d566cf 216 * Sector5: 0x00001400 - 0x000017FF 1K
Anaesthetix 0:0929d3d566cf 217 * Sector6: 0x00001800 - 0x00001BFF 1K
Anaesthetix 0:0929d3d566cf 218 * Sector7: 0x00001C00 - 0x00001FFF 1K
Anaesthetix 0:0929d3d566cf 219 * Sector8: 0x00002000 - 0x000023FF 1K
Anaesthetix 0:0929d3d566cf 220 * Sector9: 0x00002400 - 0x000027FF 1K
Anaesthetix 0:0929d3d566cf 221 * Sector10: 0x00002800 - 0x00002BFF 1K
Anaesthetix 0:0929d3d566cf 222 * Sector11: 0x00002C00 - 0x00002FFF 1K
Anaesthetix 0:0929d3d566cf 223 * Sector12: 0x00003000 - 0x000033FF 1K
Anaesthetix 0:0929d3d566cf 224 * Sector13: 0x00003400 - 0x000037FF 1K
Anaesthetix 0:0929d3d566cf 225 * Sector14: 0x00003800 - 0x00003BFF 1K
Anaesthetix 0:0929d3d566cf 226 * Sector15: 0x00003C00 - 0x00003FFF 1K
Anaesthetix 0:0929d3d566cf 227 * Sector16: 0x00004000 - 0x000043FF 1K (LPC824 only)
Anaesthetix 0:0929d3d566cf 228 * Sector17: 0x00004400 - 0x000047FF 1K (LPC824 only)
Anaesthetix 0:0929d3d566cf 229 * Sector18: 0x00004800 - 0x00004BFF 1K (LPC824 only)
Anaesthetix 0:0929d3d566cf 230 * Sector19: 0x00004C00 - 0x00004FFF 1K (LPC824 only)
Anaesthetix 0:0929d3d566cf 231 * Sector20: 0x00005000 - 0x000053FF 1K (LPC824 only)
Anaesthetix 0:0929d3d566cf 232 * Sector21: 0x00005400 - 0x000057FF 1K (LPC824 only)
Anaesthetix 0:0929d3d566cf 233 * Sector22: 0x00005800 - 0x00005BFF 1K (LPC824 only)
Anaesthetix 0:0929d3d566cf 234 * Sector23: 0x00005C00 - 0x00005FFF 1K (LPC824 only)
Anaesthetix 0:0929d3d566cf 235 * Sector24: 0x00006000 - 0x000063FF 1K (LPC824 only)
Anaesthetix 0:0929d3d566cf 236 * Sector25: 0x00006400 - 0x000067FF 1K (LPC824 only)
Anaesthetix 0:0929d3d566cf 237 * Sector26: 0x00006800 - 0x00006BFF 1K (LPC824 only)
Anaesthetix 0:0929d3d566cf 238 * Sector27: 0x00006C00 - 0x00006FFF 1K (LPC824 only)
Anaesthetix 0:0929d3d566cf 239 * Sector28: 0x00007000 - 0x000073FF 1K (LPC824 only)
Anaesthetix 0:0929d3d566cf 240 * Sector29: 0x00007400 - 0x000077FF 1K (LPC824 only)
Anaesthetix 0:0929d3d566cf 241 * Sector30: 0x00007800 - 0x00007BFF 1K (LPC824 only)
Anaesthetix 0:0929d3d566cf 242 * Sector31: 0x00007C00 - 0x00007FFF 1K (LPC824 only)
Anaesthetix 0:0929d3d566cf 243 */
Anaesthetix 0:0929d3d566cf 244
Anaesthetix 0:0929d3d566cf 245 #define FLASH_SECTOR_0 0x00000000
Anaesthetix 0:0929d3d566cf 246 #define FLASH_SECTOR_1 0x00000400
Anaesthetix 0:0929d3d566cf 247 #define FLASH_SECTOR_2 0x00000800
Anaesthetix 0:0929d3d566cf 248 #define FLASH_SECTOR_3 0x00000C00
Anaesthetix 0:0929d3d566cf 249 #define FLASH_SECTOR_4 0x00001000
Anaesthetix 0:0929d3d566cf 250 #define FLASH_SECTOR_5 0x00001400
Anaesthetix 0:0929d3d566cf 251 #define FLASH_SECTOR_6 0x00001800
Anaesthetix 0:0929d3d566cf 252 #define FLASH_SECTOR_7 0x00001C00
Anaesthetix 0:0929d3d566cf 253 #define FLASH_SECTOR_8 0x00002000
Anaesthetix 0:0929d3d566cf 254 #define FLASH_SECTOR_9 0x00002400
Anaesthetix 0:0929d3d566cf 255 #define FLASH_SECTOR_10 0x00002800
Anaesthetix 0:0929d3d566cf 256 #define FLASH_SECTOR_11 0x00002C00
Anaesthetix 0:0929d3d566cf 257 #define FLASH_SECTOR_12 0x00003000
Anaesthetix 0:0929d3d566cf 258 #define FLASH_SECTOR_13 0x00003400
Anaesthetix 0:0929d3d566cf 259 #define FLASH_SECTOR_14 0x00003800
Anaesthetix 0:0929d3d566cf 260 #define FLASH_SECTOR_15 0x00003C00
Anaesthetix 0:0929d3d566cf 261 #define FLASH_SECTOR_16 0x00004000 // for LPC824 only
Anaesthetix 0:0929d3d566cf 262 #define FLASH_SECTOR_17 0x00004400 // for LPC824 only
Anaesthetix 0:0929d3d566cf 263 #define FLASH_SECTOR_18 0x00004800 // for LPC824 only
Anaesthetix 0:0929d3d566cf 264 #define FLASH_SECTOR_19 0x00004C00 // for LPC824 only
Anaesthetix 0:0929d3d566cf 265 #define FLASH_SECTOR_20 0x00005000 // for LPC824 only
Anaesthetix 0:0929d3d566cf 266 #define FLASH_SECTOR_21 0x00005400 // for LPC824 only
Anaesthetix 0:0929d3d566cf 267 #define FLASH_SECTOR_22 0x00005800 // for LPC824 only
Anaesthetix 0:0929d3d566cf 268 #define FLASH_SECTOR_23 0x00005C00 // for LPC824 only
Anaesthetix 0:0929d3d566cf 269 #define FLASH_SECTOR_24 0x00006000 // for LPC824 only
Anaesthetix 0:0929d3d566cf 270 #define FLASH_SECTOR_25 0x00006400 // for LPC824 only
Anaesthetix 0:0929d3d566cf 271 #define FLASH_SECTOR_26 0x00006800 // for LPC824 only
Anaesthetix 0:0929d3d566cf 272 #define FLASH_SECTOR_27 0x00006C00 // for LPC824 only
Anaesthetix 0:0929d3d566cf 273 #define FLASH_SECTOR_28 0x00007000 // for LPC824 only
Anaesthetix 0:0929d3d566cf 274 #define FLASH_SECTOR_29 0x00007400 // for LPC824 only
Anaesthetix 0:0929d3d566cf 275 #define FLASH_SECTOR_30 0x00007800 // for LPC824 only
Anaesthetix 0:0929d3d566cf 276 #define FLASH_SECTOR_31 0x00007C00 // for LPC824 only
Anaesthetix 0:0929d3d566cf 277 #define FLASH_SECTOR_SIZE (1 * 1024)
Anaesthetix 0:0929d3d566cf 278
Anaesthetix 0:0929d3d566cf 279 static char * sector_start_adress[] = {
Anaesthetix 0:0929d3d566cf 280 (char *)FLASH_SECTOR_0,
Anaesthetix 0:0929d3d566cf 281 (char *)FLASH_SECTOR_1,
Anaesthetix 0:0929d3d566cf 282 (char *)FLASH_SECTOR_2,
Anaesthetix 0:0929d3d566cf 283 (char *)FLASH_SECTOR_3,
Anaesthetix 0:0929d3d566cf 284 (char *)FLASH_SECTOR_4,
Anaesthetix 0:0929d3d566cf 285 (char *)FLASH_SECTOR_5,
Anaesthetix 0:0929d3d566cf 286 (char *)FLASH_SECTOR_6,
Anaesthetix 0:0929d3d566cf 287 (char *)FLASH_SECTOR_7,
Anaesthetix 0:0929d3d566cf 288 (char *)FLASH_SECTOR_8,
Anaesthetix 0:0929d3d566cf 289 (char *)FLASH_SECTOR_9,
Anaesthetix 0:0929d3d566cf 290 (char *)FLASH_SECTOR_10,
Anaesthetix 0:0929d3d566cf 291 (char *)FLASH_SECTOR_11,
Anaesthetix 0:0929d3d566cf 292 (char *)FLASH_SECTOR_12,
Anaesthetix 0:0929d3d566cf 293 (char *)FLASH_SECTOR_13,
Anaesthetix 0:0929d3d566cf 294 (char *)FLASH_SECTOR_14,
Anaesthetix 0:0929d3d566cf 295 (char *)FLASH_SECTOR_15,
Anaesthetix 0:0929d3d566cf 296 (char *)FLASH_SECTOR_16, // for LPC824 only
Anaesthetix 0:0929d3d566cf 297 (char *)FLASH_SECTOR_17, // for LPC824 only
Anaesthetix 0:0929d3d566cf 298 (char *)FLASH_SECTOR_18, // for LPC824 only
Anaesthetix 0:0929d3d566cf 299 (char *)FLASH_SECTOR_19, // for LPC824 only
Anaesthetix 0:0929d3d566cf 300 (char *)FLASH_SECTOR_20, // for LPC824 only
Anaesthetix 0:0929d3d566cf 301 (char *)FLASH_SECTOR_21, // for LPC824 only
Anaesthetix 0:0929d3d566cf 302 (char *)FLASH_SECTOR_22, // for LPC824 only
Anaesthetix 0:0929d3d566cf 303 (char *)FLASH_SECTOR_23, // for LPC824 only
Anaesthetix 0:0929d3d566cf 304 (char *)FLASH_SECTOR_24, // for LPC824 only
Anaesthetix 0:0929d3d566cf 305 (char *)FLASH_SECTOR_25, // for LPC824 only
Anaesthetix 0:0929d3d566cf 306 (char *)FLASH_SECTOR_26, // for LPC824 only
Anaesthetix 0:0929d3d566cf 307 (char *)FLASH_SECTOR_27, // for LPC824 only
Anaesthetix 0:0929d3d566cf 308 (char *)FLASH_SECTOR_28, // for LPC824 only
Anaesthetix 0:0929d3d566cf 309 (char *)FLASH_SECTOR_29, // for LPC824 only
Anaesthetix 0:0929d3d566cf 310 (char *)FLASH_SECTOR_30, // for LPC824 only
Anaesthetix 0:0929d3d566cf 311 (char *)FLASH_SECTOR_31 // for LPC824 only
Anaesthetix 0:0929d3d566cf 312 };
Anaesthetix 0:0929d3d566cf 313
Anaesthetix 0:0929d3d566cf 314 #endif
Anaesthetix 0:0929d3d566cf 315
Anaesthetix 0:0929d3d566cf 316 /** Error code by IAP routine
Anaesthetix 0:0929d3d566cf 317 *
Anaesthetix 0:0929d3d566cf 318 * Table 588 "ISP Return Codes Summary", Section 7.15 "ISP Return Codes", usermanual
Anaesthetix 0:0929d3d566cf 319 */
Anaesthetix 0:0929d3d566cf 320
Anaesthetix 0:0929d3d566cf 321 enum error_code {
Anaesthetix 0:0929d3d566cf 322 CMD_SUCCESS,
Anaesthetix 0:0929d3d566cf 323 INVALID_COMMAND,
Anaesthetix 0:0929d3d566cf 324 SRC_ADDR_ERROR,
Anaesthetix 0:0929d3d566cf 325 DST_ADDR_ERROR,
Anaesthetix 0:0929d3d566cf 326 SRC_ADDR_NOT_MAPPED,
Anaesthetix 0:0929d3d566cf 327 DST_ADDR_NOT_MAPPED,
Anaesthetix 0:0929d3d566cf 328 COUNT_ERROR,
Anaesthetix 0:0929d3d566cf 329 INVALID_SECTOR,
Anaesthetix 0:0929d3d566cf 330 SECTOR_NOT_BLANK,
Anaesthetix 0:0929d3d566cf 331 SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION,
Anaesthetix 0:0929d3d566cf 332 COMPARE_ERROR,
Anaesthetix 0:0929d3d566cf 333 BUSY,
Anaesthetix 0:0929d3d566cf 334 PARAM_ERROR,
Anaesthetix 0:0929d3d566cf 335 ADDR_ERROR,
Anaesthetix 0:0929d3d566cf 336 ADDR_NOT_MAPPED,
Anaesthetix 0:0929d3d566cf 337 CMD_LOCKED,
Anaesthetix 0:0929d3d566cf 338 INVALID_CODE,
Anaesthetix 0:0929d3d566cf 339 INVALID_BAUD_RATE,
Anaesthetix 0:0929d3d566cf 340 INVALID_STOP_BIT,
Anaesthetix 0:0929d3d566cf 341 CODE_READ_PROTECTION_ENABLED
Anaesthetix 0:0929d3d566cf 342 };
Anaesthetix 0:0929d3d566cf 343
Anaesthetix 0:0929d3d566cf 344
Anaesthetix 0:0929d3d566cf 345
Anaesthetix 0:0929d3d566cf 346 /*
Anaesthetix 0:0929d3d566cf 347 * IAP routine entry
Anaesthetix 0:0929d3d566cf 348 *
Anaesthetix 0:0929d3d566cf 349 * "IAP commands"
Anaesthetix 0:0929d3d566cf 350 */
Anaesthetix 0:0929d3d566cf 351
Anaesthetix 0:0929d3d566cf 352 #define IAP_LOCATION 0x1fff1ff1
Anaesthetix 0:0929d3d566cf 353 typedef void (*IAP_call)(unsigned int [], unsigned int []);
Anaesthetix 0:0929d3d566cf 354
Anaesthetix 0:0929d3d566cf 355
Anaesthetix 0:0929d3d566cf 356 /** IAP class
Anaesthetix 0:0929d3d566cf 357 *
Anaesthetix 0:0929d3d566cf 358 * Interface for internal flash memory access
Anaesthetix 0:0929d3d566cf 359 */
Anaesthetix 0:0929d3d566cf 360
Anaesthetix 0:0929d3d566cf 361 class IAP
Anaesthetix 0:0929d3d566cf 362 {
Anaesthetix 0:0929d3d566cf 363 public:
Anaesthetix 0:0929d3d566cf 364
Anaesthetix 0:0929d3d566cf 365 /** Constructor for IAP
Anaesthetix 0:0929d3d566cf 366 *
Anaesthetix 0:0929d3d566cf 367 */
Anaesthetix 0:0929d3d566cf 368 IAP() : iap_entry( reinterpret_cast<IAP_call>(IAP_LOCATION) ), cclk_kHz( SystemCoreClock / 1000 ) {}
Anaesthetix 0:0929d3d566cf 369
Anaesthetix 0:0929d3d566cf 370 /** Reinvoke ISP
Anaesthetix 0:0929d3d566cf 371 *
Anaesthetix 0:0929d3d566cf 372 * @return error code
Anaesthetix 0:0929d3d566cf 373 */
Anaesthetix 0:0929d3d566cf 374 int reinvoke_isp( void );
Anaesthetix 0:0929d3d566cf 375
Anaesthetix 0:0929d3d566cf 376 /** Read part identification number
Anaesthetix 0:0929d3d566cf 377 *
Anaesthetix 0:0929d3d566cf 378 * @return device ID
Anaesthetix 0:0929d3d566cf 379 * @see read_serial()
Anaesthetix 0:0929d3d566cf 380 */
Anaesthetix 0:0929d3d566cf 381 int read_ID( void );
Anaesthetix 0:0929d3d566cf 382
Anaesthetix 0:0929d3d566cf 383 /** Read device serial number
Anaesthetix 0:0929d3d566cf 384 *
Anaesthetix 0:0929d3d566cf 385 * @return device serial number
Anaesthetix 0:0929d3d566cf 386 * @see read_ID()
Anaesthetix 0:0929d3d566cf 387 */
Anaesthetix 0:0929d3d566cf 388 int *read_serial( void );
Anaesthetix 0:0929d3d566cf 389
Anaesthetix 0:0929d3d566cf 390 /** Blank check sector(s)
Anaesthetix 0:0929d3d566cf 391 *
Anaesthetix 0:0929d3d566cf 392 * @param start a Start Sector Number
Anaesthetix 0:0929d3d566cf 393 * @param end an End Sector Number (should be greater than or equal to start sector number).
Anaesthetix 0:0929d3d566cf 394 * @return error code: CMD_SUCCESS | BUSY | SECTOR_NOT_BLANK | INVALID_SECTOR
Anaesthetix 0:0929d3d566cf 395 */
Anaesthetix 0:0929d3d566cf 396 int blank_check( int start, int end );
Anaesthetix 0:0929d3d566cf 397
Anaesthetix 0:0929d3d566cf 398 /** Erase Sector(s)
Anaesthetix 0:0929d3d566cf 399 *
Anaesthetix 0:0929d3d566cf 400 * @param start a Start Sector Number
Anaesthetix 0:0929d3d566cf 401 * @param end an End Sector Number (should be greater than or equal to start sector number).
Anaesthetix 0:0929d3d566cf 402 * @return error code: CMD_SUCCESS | BUSY | SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION | INVALID_SECTOR
Anaesthetix 0:0929d3d566cf 403 */
Anaesthetix 0:0929d3d566cf 404 int erase( int start, int end );
Anaesthetix 0:0929d3d566cf 405
Anaesthetix 0:0929d3d566cf 406 /** Prepare sector(s) for write operation
Anaesthetix 0:0929d3d566cf 407 *
Anaesthetix 0:0929d3d566cf 408 * @param start a Start Sector Number
Anaesthetix 0:0929d3d566cf 409 * @param end an End Sector Number (should be greater than or equal to start sector number).
Anaesthetix 0:0929d3d566cf 410 * @return error code: CMD_SUCCESS | BUSY | INVALID_SECTOR
Anaesthetix 0:0929d3d566cf 411 */
Anaesthetix 0:0929d3d566cf 412 int prepare( int start, int end );
Anaesthetix 0:0929d3d566cf 413
Anaesthetix 0:0929d3d566cf 414 /** Copy RAM to Flash
Anaesthetix 0:0929d3d566cf 415 *
Anaesthetix 0:0929d3d566cf 416 * @param source_addr Source RAM address from which data bytes are to be read. This address should be a word boundary.
Anaesthetix 0:0929d3d566cf 417 * @param target_addr Destination flash address where data bytes are to be written. This address should be a 256 byte boundary.
Anaesthetix 0:0929d3d566cf 418 * @param size Number of bytes to be written. Should be 256 | 512 | 1024 | 4096.
Anaesthetix 0:0929d3d566cf 419 * @return error code: CMD_SUCCESS | SRC_ADDR_ERROR (Address not a word boundary) | DST_ADDR_ERROR (Address not on correct boundary) | SRC_ADDR_NOT_MAPPED | DST_ADDR_NOT_MAPPED | COUNT_ERROR (Byte count is not 256 | 512 | 1024 | 4096) | SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION | BUSY
Anaesthetix 0:0929d3d566cf 420 */
Anaesthetix 0:0929d3d566cf 421 int write( char *source_addr, char *target_addr, int size );
Anaesthetix 0:0929d3d566cf 422
Anaesthetix 0:0929d3d566cf 423 /** Compare <address1> <address2> <no of bytes>
Anaesthetix 0:0929d3d566cf 424 *
Anaesthetix 0:0929d3d566cf 425 * @param source_addr Starting flash or RAM address of data bytes to be compared. This address should be a word boundary.
Anaesthetix 0:0929d3d566cf 426 * @param target_addr Starting flash or RAM address of data bytes to be compared. This address should be a word boundary.
Anaesthetix 0:0929d3d566cf 427 * @param size Number of bytes to be compared; should be a multiple of 4.
Anaesthetix 0:0929d3d566cf 428 * @return error code: CMD_SUCCESS | COMPARE_ERROR | COUNT_ERROR (Byte count is not a multiple of 4) | ADDR_ERROR | ADDR_NOT_MAPPED
Anaesthetix 0:0929d3d566cf 429 */
Anaesthetix 0:0929d3d566cf 430 int compare( char *source_addr, char *target_addr, int size );
Anaesthetix 0:0929d3d566cf 431
Anaesthetix 0:0929d3d566cf 432 /** Read Boot code version number
Anaesthetix 0:0929d3d566cf 433 *
Anaesthetix 0:0929d3d566cf 434 * @return 2 bytes of boot code version number
Anaesthetix 0:0929d3d566cf 435 */
Anaesthetix 0:0929d3d566cf 436 int read_BootVer( void );
Anaesthetix 0:0929d3d566cf 437
Anaesthetix 0:0929d3d566cf 438 /** Get user reserved flash start address
Anaesthetix 0:0929d3d566cf 439 *
Anaesthetix 0:0929d3d566cf 440 * @return start address of user reserved flash memory
Anaesthetix 0:0929d3d566cf 441 * @see reserved_flash_area_size()
Anaesthetix 0:0929d3d566cf 442 */
Anaesthetix 0:0929d3d566cf 443
Anaesthetix 0:0929d3d566cf 444 char *reserved_flash_area_start( void );
Anaesthetix 0:0929d3d566cf 445
Anaesthetix 0:0929d3d566cf 446 /** Get user reserved flash size
Anaesthetix 0:0929d3d566cf 447 *
Anaesthetix 0:0929d3d566cf 448 * @return size of user reserved flash memory
Anaesthetix 0:0929d3d566cf 449 * @see reserved_flash_area_start()
Anaesthetix 0:0929d3d566cf 450 */
Anaesthetix 0:0929d3d566cf 451 int reserved_flash_area_size( void );
Anaesthetix 0:0929d3d566cf 452
Anaesthetix 0:0929d3d566cf 453 #if defined(TARGET_LPC11UXX)
Anaesthetix 0:0929d3d566cf 454
Anaesthetix 0:0929d3d566cf 455 /** Copy RAM to EEPROM (LPC11U24)
Anaesthetix 0:0929d3d566cf 456 *
Anaesthetix 0:0929d3d566cf 457 * @param source_addr Source RAM address from which data bytes are to be read.
Anaesthetix 0:0929d3d566cf 458 * @param target_addr Destination EEPROM address where data bytes are to be written.
Anaesthetix 0:0929d3d566cf 459 * @param size Number of bytes to be written.
Anaesthetix 0:0929d3d566cf 460 * @return error code: CMD_SUCCESS | SRC_ADDR_NOT_MAPPED | DST_ADDR_NOT_MAPPED
Anaesthetix 0:0929d3d566cf 461 * Remark: The top 64 bytes of the EEPROM memory are reserved and cannot be written to.
Anaesthetix 0:0929d3d566cf 462 */
Anaesthetix 0:0929d3d566cf 463 int write_eeprom( char *source_addr, char *target_addr, int size );
Anaesthetix 0:0929d3d566cf 464
Anaesthetix 0:0929d3d566cf 465 /** Copy EEPROM to RAM (LPC11U24)
Anaesthetix 0:0929d3d566cf 466 *
Anaesthetix 0:0929d3d566cf 467 * @param source_addr Source EEPROM address from which data bytes are to be read.
Anaesthetix 0:0929d3d566cf 468 * @param target_addr Destination RAM address where data bytes are to be written.
Anaesthetix 0:0929d3d566cf 469 * @param size Number of bytes to be written.
Anaesthetix 0:0929d3d566cf 470 * @return error code: CMD_SUCCESS | SRC_ADDR_NOT_MAPPED | DST_ADDR_NOT_MAPPED
Anaesthetix 0:0929d3d566cf 471 * Remark: The top 64 bytes of the EEPROM memory are reserved and cannot be written to.
Anaesthetix 0:0929d3d566cf 472 */
Anaesthetix 0:0929d3d566cf 473 int read_eeprom( char *source_addr, char *target_addr, int size );
Anaesthetix 0:0929d3d566cf 474
Anaesthetix 0:0929d3d566cf 475 #elif defined(TARGET_LPC81X) || defined(TARGET_LPC82X)
Anaesthetix 0:0929d3d566cf 476
Anaesthetix 0:0929d3d566cf 477 /** Erase page(s) (LPC812, LPC824)
Anaesthetix 0:0929d3d566cf 478 *
Anaesthetix 0:0929d3d566cf 479 * @param start Start page number.
Anaesthetix 0:0929d3d566cf 480 * @param end End page number (should be greater than or equal to start page).
Anaesthetix 0:0929d3d566cf 481 * @return error code: CMD_SUCCESS | BUSY | SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION | INVALID_SECTOR
Anaesthetix 0:0929d3d566cf 482 */
Anaesthetix 0:0929d3d566cf 483 int erase_page( int start, int end );
Anaesthetix 0:0929d3d566cf 484
Anaesthetix 0:0929d3d566cf 485 #endif
Anaesthetix 0:0929d3d566cf 486
Anaesthetix 0:0929d3d566cf 487 private:
Anaesthetix 0:0929d3d566cf 488 IAP_call iap_entry;
Anaesthetix 0:0929d3d566cf 489 unsigned int IAP_command[ 5 ];
Anaesthetix 0:0929d3d566cf 490 unsigned int IAP_result[ 5 ];
Anaesthetix 0:0929d3d566cf 491 int cclk_kHz;
Anaesthetix 0:0929d3d566cf 492 }
Anaesthetix 0:0929d3d566cf 493 ;
Anaesthetix 0:0929d3d566cf 494
Anaesthetix 0:0929d3d566cf 495 #endif // #ifndef MBED_IAP
Anaesthetix 0:0929d3d566cf 496