Valentin Bruchet
/
a_sd_card
sd lecture
eeprom_flash.cpp@2:68c9ff91504c, 2019-02-04 (annotated)
- Committer:
- pathae
- Date:
- Mon Feb 04 15:06:07 2019 +0000
- Revision:
- 2:68c9ff91504c
- Parent:
- 0:ea2bec485611
- Child:
- 3:46c0511e25af
flash_F103RB
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
olympux | 0:ea2bec485611 | 1 | #include "mbed.h" |
olympux | 0:ea2bec485611 | 2 | #include "eeprom_flash.h" |
pathae | 2:68c9ff91504c | 3 | Serial pc(USBTX, USBRX); //Initalise PC serial comms |
pathae | 2:68c9ff91504c | 4 | DigitalOut myled(LED1); |
olympux | 0:ea2bec485611 | 5 | /* |
olympux | 0:ea2bec485611 | 6 | * Must call this first to enable writing |
olympux | 0:ea2bec485611 | 7 | */ |
olympux | 0:ea2bec485611 | 8 | void enableEEPROMWriting() { |
olympux | 0:ea2bec485611 | 9 | HAL_StatusTypeDef status = HAL_FLASH_Unlock(); |
olympux | 0:ea2bec485611 | 10 | FLASH_PageErase(EEPROM_START_ADDRESS); // required to re-write |
pathae | 2:68c9ff91504c | 11 | CLEAR_BIT(FLASH->CR, FLASH_CR_PER); // Bug fix: bit PER has been set in Flash_PageErase(), must clear it here |
olympux | 0:ea2bec485611 | 12 | } |
olympux | 0:ea2bec485611 | 13 | |
olympux | 0:ea2bec485611 | 14 | void disableEEPROMWriting() { |
olympux | 0:ea2bec485611 | 15 | HAL_FLASH_Lock(); |
olympux | 0:ea2bec485611 | 16 | } |
olympux | 0:ea2bec485611 | 17 | |
olympux | 0:ea2bec485611 | 18 | /* |
olympux | 0:ea2bec485611 | 19 | * Writing functions |
olympux | 0:ea2bec485611 | 20 | * Must call enableEEPROMWriting() first |
olympux | 0:ea2bec485611 | 21 | */ |
olympux | 0:ea2bec485611 | 22 | HAL_StatusTypeDef writeEEPROMHalfWord(uint32_t address, uint16_t data) { |
olympux | 0:ea2bec485611 | 23 | HAL_StatusTypeDef status; |
olympux | 0:ea2bec485611 | 24 | address = address + EEPROM_START_ADDRESS; |
olympux | 0:ea2bec485611 | 25 | status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, address, data); |
olympux | 0:ea2bec485611 | 26 | return status; |
olympux | 0:ea2bec485611 | 27 | } |
olympux | 0:ea2bec485611 | 28 | |
olympux | 0:ea2bec485611 | 29 | /* |
olympux | 0:ea2bec485611 | 30 | * Reading functions |
olympux | 0:ea2bec485611 | 31 | */ |
olympux | 0:ea2bec485611 | 32 | uint16_t readEEPROMHalfWord(uint32_t address) { |
olympux | 0:ea2bec485611 | 33 | uint16_t val = 0; |
olympux | 0:ea2bec485611 | 34 | address = address + EEPROM_START_ADDRESS; |
olympux | 0:ea2bec485611 | 35 | val = *(__IO uint16_t*)address; |
pathae | 2:68c9ff91504c | 36 | return val; |
olympux | 0:ea2bec485611 | 37 | } |
olympux | 0:ea2bec485611 | 38 | |
pathae | 2:68c9ff91504c | 39 | //Programme de test |
pathae | 2:68c9ff91504c | 40 | |
pathae | 2:68c9ff91504c | 41 | int main() { |
pathae | 2:68c9ff91504c | 42 | int adresse = 0x00; |
pathae | 2:68c9ff91504c | 43 | char donnee1 = 11; |
pathae | 2:68c9ff91504c | 44 | char donnee2 = 22; |
pathae | 2:68c9ff91504c | 45 | char donnee3 = 33; |
pathae | 2:68c9ff91504c | 46 | int lecture; |
pathae | 2:68c9ff91504c | 47 | pc.baud(9600); |
pathae | 2:68c9ff91504c | 48 | |
pathae | 2:68c9ff91504c | 49 | /* Ecriture*/ |
pathae | 2:68c9ff91504c | 50 | enableEEPROMWriting(); //autorisation d'ecriture dans l'eeprom |
pathae | 2:68c9ff91504c | 51 | writeEEPROMHalfWord(adresse, donnee1); |
pathae | 2:68c9ff91504c | 52 | writeEEPROMHalfWord(adresse+2, donnee2); |
pathae | 2:68c9ff91504c | 53 | writeEEPROMHalfWord(adresse+4, donnee3); |
pathae | 2:68c9ff91504c | 54 | disableEEPROMWriting(); //Interdiction d'ecriture |
pathae | 2:68c9ff91504c | 55 | |
pathae | 2:68c9ff91504c | 56 | myled=!myled; |
pathae | 2:68c9ff91504c | 57 | wait(0.5); |
pathae | 2:68c9ff91504c | 58 | myled=!myled; |
pathae | 2:68c9ff91504c | 59 | wait(0.5); |
pathae | 2:68c9ff91504c | 60 | |
pathae | 2:68c9ff91504c | 61 | /*lecture et affichage*/ |
pathae | 2:68c9ff91504c | 62 | lecture=readEEPROMHalfWord(adresse); |
pathae | 2:68c9ff91504c | 63 | if (lecture==11) |
pathae | 2:68c9ff91504c | 64 | { |
pathae | 2:68c9ff91504c | 65 | myled=!myled; |
pathae | 2:68c9ff91504c | 66 | wait(0.5); |
pathae | 2:68c9ff91504c | 67 | myled=!myled; |
pathae | 2:68c9ff91504c | 68 | wait(0.5); |
pathae | 2:68c9ff91504c | 69 | myled=!myled; |
pathae | 2:68c9ff91504c | 70 | wait(0.5); |
pathae | 2:68c9ff91504c | 71 | } |
pathae | 2:68c9ff91504c | 72 | pc.printf ("valeur lue: %d\n\r",lecture); |
pathae | 2:68c9ff91504c | 73 | |
pathae | 2:68c9ff91504c | 74 | lecture=readEEPROMHalfWord(adresse+2); |
pathae | 2:68c9ff91504c | 75 | pc.printf ("valeur lue: %d\n\r",lecture); |
pathae | 2:68c9ff91504c | 76 | lecture=readEEPROMHalfWord(adresse+4); |
pathae | 2:68c9ff91504c | 77 | pc.printf ("valeur lue: %d\n\r",lecture); |
pathae | 2:68c9ff91504c | 78 | while(1); |
pathae | 2:68c9ff91504c | 79 | } |