Arnaud VALLEY / Mbed 2 deprecated Pinscape_Controller_V2_arnoz

Dependencies:   mbed FastIO FastPWM USBDevice

Committer:
mjr
Date:
Fri Feb 03 20:50:02 2017 +0000
Revision:
76:7f5912b6340e
Parent:
2:c174f9ee414a
Child:
79:682ae3171a08
Rework flash driver to make it truly stable (hopefully to 100% reliability); host-loaded configuration; performance improvements; more performance diagnostics.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mjr 2:c174f9ee414a 1 /*
mjr 2:c174f9ee414a 2 * Freescale FTFA Flash Memory programmer
mjr 2:c174f9ee414a 3 *
mjr 2:c174f9ee414a 4 * Sample usage:
mjr 2:c174f9ee414a 5
mjr 2:c174f9ee414a 6 #include "mbed.h"
mjr 2:c174f9ee414a 7 #include "FreescaleIAP.h"
mjr 2:c174f9ee414a 8
mjr 2:c174f9ee414a 9 int main() {
mjr 2:c174f9ee414a 10 int address = flash_size() - SECTOR_SIZE; //Write in last sector
mjr 2:c174f9ee414a 11
mjr 2:c174f9ee414a 12 int *data = (int*)address;
mjr 2:c174f9ee414a 13 printf("Starting\r\n");
mjr 2:c174f9ee414a 14 erase_sector(address);
mjr 2:c174f9ee414a 15 int numbers[10] = {0, 1, 10, 100, 1000, 10000, 1000000, 10000000, 100000000, 1000000000};
mjr 2:c174f9ee414a 16 program_flash(address, (char*)&numbers, 40); //10 integers of 4 bytes each: 40 bytes length
mjr 2:c174f9ee414a 17 printf("Resulting flash: \r\n");
mjr 2:c174f9ee414a 18 for (int i = 0; i<10; i++)
mjr 2:c174f9ee414a 19 printf("%d\r\n", data[i]);
mjr 2:c174f9ee414a 20
mjr 2:c174f9ee414a 21 printf("Done\r\n\n");
mjr 2:c174f9ee414a 22
mjr 2:c174f9ee414a 23
mjr 2:c174f9ee414a 24 while (true) {
mjr 2:c174f9ee414a 25 }
mjr 2:c174f9ee414a 26 }
mjr 2:c174f9ee414a 27
mjr 2:c174f9ee414a 28 */
mjr 2:c174f9ee414a 29
mjr 2:c174f9ee414a 30 #ifndef FREESCALEIAP_H
mjr 2:c174f9ee414a 31 #define FREESCALEIAP_H
mjr 2:c174f9ee414a 32
mjr 2:c174f9ee414a 33 #include "mbed.h"
mjr 2:c174f9ee414a 34
mjr 2:c174f9ee414a 35 #ifdef TARGET_KLXX
mjr 2:c174f9ee414a 36 #define SECTOR_SIZE 1024
mjr 2:c174f9ee414a 37 #elif TARGET_K20D5M
mjr 2:c174f9ee414a 38 #define SECTOR_SIZE 2048
mjr 2:c174f9ee414a 39 #elif TARGET_K64F
mjr 2:c174f9ee414a 40 #define SECTOR_SIZE 4096
mjr 2:c174f9ee414a 41 #else
mjr 2:c174f9ee414a 42 #define SECTOR_SIZE 1024
mjr 2:c174f9ee414a 43 #endif
mjr 2:c174f9ee414a 44
mjr 2:c174f9ee414a 45 enum IAPCode {
mjr 2:c174f9ee414a 46 BoundaryError = -99, //Commands may not span several sectors
mjr 2:c174f9ee414a 47 AlignError, //Data must be aligned on longword (two LSBs zero)
mjr 2:c174f9ee414a 48 ProtectionError, //Flash sector is protected
mjr 2:c174f9ee414a 49 AccessError, //Something went wrong
mjr 2:c174f9ee414a 50 CollisionError, //During writing something tried to flash which was written to
mjr 2:c174f9ee414a 51 LengthError, //The length must be multiples of 4
mjr 2:c174f9ee414a 52 RuntimeError,
mjr 2:c174f9ee414a 53 EraseError, //The flash was not erased before writing to it
mjr 2:c174f9ee414a 54 Success = 0
mjr 2:c174f9ee414a 55 };
mjr 2:c174f9ee414a 56
mjr 2:c174f9ee414a 57
mjr 2:c174f9ee414a 58 class FreescaleIAP
mjr 2:c174f9ee414a 59 {
mjr 2:c174f9ee414a 60 public:
mjr 76:7f5912b6340e 61 FreescaleIAP() { }
mjr 76:7f5912b6340e 62 ~FreescaleIAP() { }
mjr 2:c174f9ee414a 63
mjr 76:7f5912b6340e 64 /** Program flash. This erases the area to be written, then writes the data.
mjr 2:c174f9ee414a 65 *
mjr 2:c174f9ee414a 66 * @param address starting address where the data needs to be programmed (must be longword alligned: two LSBs must be zero)
mjr 2:c174f9ee414a 67 * @param data pointer to array with the data to program
mjr 2:c174f9ee414a 68 * @param length number of bytes to program (must be a multiple of 4)
mjr 2:c174f9ee414a 69 * @param return Success if no errors were encountered, otherwise one of the error states
mjr 2:c174f9ee414a 70 */
mjr 2:c174f9ee414a 71 IAPCode program_flash(int address, const void *data, unsigned int length);
mjr 2:c174f9ee414a 72
mjr 2:c174f9ee414a 73 /**
mjr 2:c174f9ee414a 74 * Returns size of flash memory
mjr 2:c174f9ee414a 75 *
mjr 2:c174f9ee414a 76 * This is the first address which is not flash
mjr 2:c174f9ee414a 77 *
mjr 2:c174f9ee414a 78 * @param return length of flash memory in bytes
mjr 2:c174f9ee414a 79 */
mjr 2:c174f9ee414a 80 uint32_t flash_size(void);
mjr 2:c174f9ee414a 81
mjr 2:c174f9ee414a 82 private:
mjr 2:c174f9ee414a 83 // program a word of flash
mjr 2:c174f9ee414a 84 IAPCode program_word(int address, const char *data);
mjr 2:c174f9ee414a 85
mjr 2:c174f9ee414a 86 // verify that a flash area has been erased
mjr 2:c174f9ee414a 87 IAPCode verify_erased(int address, unsigned int length);
mjr 2:c174f9ee414a 88 };
mjr 2:c174f9ee414a 89
mjr 2:c174f9ee414a 90 #endif