Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of Flashcounter by
main.cpp@1:a201f2861b94, 2018-11-07 (annotated)
- Committer:
- krissl
- Date:
- Wed Nov 07 16:04:42 2018 +0000
- Revision:
- 1:a201f2861b94
- Parent:
- 0:7460f6983ba2
PUB
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
krissl | 0:7460f6983ba2 | 1 | #include "mbed.h" |
krissl | 0:7460f6983ba2 | 2 | #include <iostream> |
krissl | 0:7460f6983ba2 | 3 | |
krissl | 0:7460f6983ba2 | 4 | #include "FlashIAP.h" |
krissl | 0:7460f6983ba2 | 5 | #pragma pack(push,1) |
krissl | 0:7460f6983ba2 | 6 | struct packed_A { |
krissl | 0:7460f6983ba2 | 7 | uint32_t magic; |
krissl | 0:7460f6983ba2 | 8 | uint32_t value; |
krissl | 0:7460f6983ba2 | 9 | |
krissl | 0:7460f6983ba2 | 10 | } |
krissl | 0:7460f6983ba2 | 11 | #pragma pack(pop) |
krissl | 0:7460f6983ba2 | 12 | ; |
krissl | 0:7460f6983ba2 | 13 | |
krissl | 0:7460f6983ba2 | 14 | Ticker toggle_led_ticker; |
krissl | 0:7460f6983ba2 | 15 | enum { MAGIC_VALUE = 0xDEADBEEF }; |
krissl | 0:7460f6983ba2 | 16 | DigitalOut led1(LED1); |
krissl | 0:7460f6983ba2 | 17 | int counter; |
krissl | 0:7460f6983ba2 | 18 | uint32_t sector_size; |
krissl | 0:7460f6983ba2 | 19 | uint32_t page_size; |
krissl | 0:7460f6983ba2 | 20 | uint32_t prog_size; |
krissl | 0:7460f6983ba2 | 21 | uint32_t address; |
krissl | 0:7460f6983ba2 | 22 | FlashIAP flash_device; |
krissl | 0:7460f6983ba2 | 23 | DigitalIn button(USER_BUTTON); |
krissl | 0:7460f6983ba2 | 24 | |
krissl | 0:7460f6983ba2 | 25 | bool initializeDevice() |
krissl | 0:7460f6983ba2 | 26 | { |
krissl | 0:7460f6983ba2 | 27 | return flash_device.init() == 0; |
krissl | 0:7460f6983ba2 | 28 | } |
krissl | 0:7460f6983ba2 | 29 | |
krissl | 0:7460f6983ba2 | 30 | void determineValues() |
krissl | 0:7460f6983ba2 | 31 | { |
krissl | 0:7460f6983ba2 | 32 | sector_size = flash_device.get_sector_size(flash_device.get_flash_start() + flash_device.get_flash_size() - 1UL); |
krissl | 0:7460f6983ba2 | 33 | page_size = flash_device.get_page_size(); |
krissl | 0:7460f6983ba2 | 34 | address = (flash_device.get_flash_start() + flash_device.get_flash_size()) - (sector_size); |
krissl | 0:7460f6983ba2 | 35 | printf("Sector size: %d \r\n", sector_size); |
krissl | 0:7460f6983ba2 | 36 | printf("Page size: %d \r\n", page_size); |
krissl | 0:7460f6983ba2 | 37 | printf("Address: %d \r\n", address); |
krissl | 0:7460f6983ba2 | 38 | } |
krissl | 0:7460f6983ba2 | 39 | |
krissl | 0:7460f6983ba2 | 40 | int main() |
krissl | 0:7460f6983ba2 | 41 | { |
krissl | 0:7460f6983ba2 | 42 | //Setting up... |
krissl | 0:7460f6983ba2 | 43 | if(initializeDevice()) { |
krissl | 0:7460f6983ba2 | 44 | //Device has been initialized. |
krissl | 0:7460f6983ba2 | 45 | printf("\r\nDevice initialized\r\n"); |
krissl | 0:7460f6983ba2 | 46 | determineValues(); |
krissl | 0:7460f6983ba2 | 47 | |
krissl | 0:7460f6983ba2 | 48 | uint8_t *data_flashed = new uint8_t[8]; |
krissl | 1:a201f2861b94 | 49 | uint8_t buffer[8]; |
krissl | 1:a201f2861b94 | 50 | flash_device.read(buffer, address, 8); |
krissl | 1:a201f2861b94 | 51 | packed_A const *ptr = (packed_A const *) buffer; |
krissl | 1:a201f2861b94 | 52 | printf("%d", ptr->magic) |
krissl | 1:a201f2861b94 | 53 | |
krissl | 0:7460f6983ba2 | 54 | packed_A readMemory; |
krissl | 0:7460f6983ba2 | 55 | |
krissl | 0:7460f6983ba2 | 56 | flash_device.read(data_flashed, address, 8); |
krissl | 0:7460f6983ba2 | 57 | |
krissl | 0:7460f6983ba2 | 58 | for(int j = 4; j >= 0 ; j--) { |
krissl | 0:7460f6983ba2 | 59 | readMemory.magic <<= 8; //Bit-shifting the input because input is (239 190 173 222) and deadbeef is (222 173 190 239) |
krissl | 0:7460f6983ba2 | 60 | readMemory.magic += data_flashed[j]; |
krissl | 0:7460f6983ba2 | 61 | } |
krissl | 0:7460f6983ba2 | 62 | for(int j = 8; j >= 4 ; j--) { |
krissl | 0:7460f6983ba2 | 63 | readMemory.value <<= 8; //Bit-shifting the data into a uint |
krissl | 0:7460f6983ba2 | 64 | readMemory.value += data_flashed[j]; |
krissl | 0:7460f6983ba2 | 65 | } |
krissl | 0:7460f6983ba2 | 66 | |
krissl | 0:7460f6983ba2 | 67 | printf("Magic: 0x%x \r\n", readMemory.magic); |
krissl | 0:7460f6983ba2 | 68 | |
krissl | 0:7460f6983ba2 | 69 | if(readMemory.magic == MAGIC_VALUE) { |
krissl | 0:7460f6983ba2 | 70 | printf("Address initialized, counter is at: %d \r\n", readMemory.value); |
krissl | 0:7460f6983ba2 | 71 | counter = readMemory.value; |
krissl | 0:7460f6983ba2 | 72 | } else { |
krissl | 0:7460f6983ba2 | 73 | printf("Uninitialized, setting up first time"); |
krissl | 0:7460f6983ba2 | 74 | packed_A newmemory; |
krissl | 0:7460f6983ba2 | 75 | newmemory.magic = MAGIC_VALUE; |
krissl | 0:7460f6983ba2 | 76 | newmemory.value = 0; |
krissl | 0:7460f6983ba2 | 77 | counter = 0; |
krissl | 0:7460f6983ba2 | 78 | flash_device.erase(address, flash_device.get_sector_size(address)); |
krissl | 0:7460f6983ba2 | 79 | flash_device.program(&newmemory, address, sizeof(newmemory)); |
krissl | 0:7460f6983ba2 | 80 | printf("Flashed"); |
krissl | 0:7460f6983ba2 | 81 | } |
krissl | 0:7460f6983ba2 | 82 | } |
krissl | 0:7460f6983ba2 | 83 | while(1) { |
krissl | 0:7460f6983ba2 | 84 | if (button == 0) { // Button is pressed |
krissl | 0:7460f6983ba2 | 85 | counter++; |
krissl | 0:7460f6983ba2 | 86 | packed_A newmemory; |
krissl | 0:7460f6983ba2 | 87 | newmemory.magic = MAGIC_VALUE; |
krissl | 0:7460f6983ba2 | 88 | newmemory.value = counter; |
krissl | 0:7460f6983ba2 | 89 | flash_device.erase(address, flash_device.get_sector_size(address)); |
krissl | 0:7460f6983ba2 | 90 | flash_device.program(&newmemory, address, sizeof(newmemory)); |
krissl | 0:7460f6983ba2 | 91 | printf("Counter: %d \r\n", counter); |
krissl | 0:7460f6983ba2 | 92 | wait(0.2); //Waiting to make sure that holding down the button doesn't make the machine go crazy |
krissl | 0:7460f6983ba2 | 93 | } |
krissl | 0:7460f6983ba2 | 94 | } |
krissl | 0:7460f6983ba2 | 95 | } |