/** * @file STC8G_EEPROM_IAP_ATY.c * * @param Project DEVICE_GENERAL_ATY_LIB * * @author ATY * * @copyright * - Copyright 2017 - 2026 MZ-ATY * - This code follows: * - MZ-ATY Various Contents Joint Statement - * * https://mengze.top/MZ-ATY_VCJS * - CC 4.0 BY-NC-SA - * * https://creativecommons.org/licenses/by-nc-sa/4.0/ * - Your use will be deemed to have accepted the terms of this statement. * * @brief functions of EEPROM_IAP for STC51 * * @version * - 1_01_230114 > ATY * -# Preliminary version, first Release ******************************************************************************** */ #ifndef __STC8G_EEPROM_IAP_ATY_C #define __STC8G_EEPROM_IAP_ATY_C #include "STC8G_EEPROM_IAP_ATY.h" /******************************* For user *************************************/ /******************************************************************************/ /** * @brief close IAP */ void IAP_Idle(void) { IAP_CONTR = 0; // close IAP IAP_CMD = 0; // clear command reg IAP_TRIG = 0; // clear trig reg IAP_ADDRH = 0x80; // set addr to none IAP area IAP_ADDRL = 0; } /** * @brief erase IAP * @param addr page start address to erase */ void IAP_Erase(uint16_t addr) { IAP_CONTR = 0x80; // enable IAP IAP_TPS = 24; // set waitting param 24MHz /* 11: Erase the EEPROM. Erase the 1 page (1 sector /512 bytes) where the target address is located. Note: The erase operation erases one sector (512 bytes) at a time, turning the entire sector contents into FFH. */ IAP_CMD = 3; // erase IAP IAP_ADDRL = addr; // set IAP low addr IAP_ADDRH = addr >> 8; // set IAP high addr IAP_TRIG = 0x5a; // write trigger command(0x5a) IAP_TRIG = 0xa5; // write trigger command(0xa5) __NOP_ATY; IAP_Idle(); // close IAP } /** * @brief read byte from IAP * @param addr address to read * @return read byte data */ uint8_t IAP_ReadByte(uint16_t addr) { uint8_t temp_uint8; IAP_CONTR = 0x80; // enable IAP IAP_TPS = 24; // set waitting param 24MHz IAP_CMD = 1; // set IAP read IAP_ADDRL = addr; // set IAP low addr IAP_ADDRH = addr >> 8; // set IAP high addr IAP_TRIG = 0x5a; // write trigger command(0x5a) IAP_TRIG = 0xa5; // write trigger command(0xa5) __NOP_ATY; temp_uint8 = IAP_DATA; // read IAP data IAP_Idle(); // close IAP return temp_uint8; } #ifdef EEPROM_USE_MOVC uint8_t IAP_ReadByte(uint16_t addr) { #define IAP_OFFSET 0x2000 //STC8G1K08 addr += IAP_OFFSET; return *(uint8_t code*)(addr); // read daa use MOVC } #endif /* EEPROM_USE_MOVC */ /** * @brief write byte to IAP * @param addr address to write * @param dataByte data to write */ void IAP_WriteByte(uint16_t addr, uint8_t dataByte) { IAP_CONTR = 0x80; // enable IAP IAP_TPS = 24; // set waitting param 24MHz IAP_CMD = 2; // set IAP write IAP_ADDRL = addr; // set IAP low addr IAP_ADDRH = addr >> 8; // set IAP high addr IAP_DATA = dataByte; // write IAP data IAP_TRIG = 0x5a; // write trigger command(0x5a) IAP_TRIG = 0xa5; // write trigger command(0xa5) __NOP_ATY; IAP_Idle(); // close IAP } /** * @brief read 4 byte float from IAP * @param addr address to read * @return read float data */ float IAP_ReadFloat(uint16_t addr) { union { float f_t; uint8_t u8_t[4]; }tempUnion; tempUnion.u8_t[0] = IAP_ReadByte(addr); tempUnion.u8_t[1] = IAP_ReadByte(addr + 1); tempUnion.u8_t[2] = IAP_ReadByte(addr + 2); tempUnion.u8_t[3] = IAP_ReadByte(addr + 3); return tempUnion.f_t; } /** * @brief write 4 byte float to IAP * @param addr address to write * @param dataFloat data to write */ void IAP_WriteFloat(uint16_t addr, float dataFloat) { union { float f_t; uint8_t u8_t[4]; }tempUnion; tempUnion.f_t = dataFloat; IAP_WriteByte(addr, tempUnion.u8_t[0]); IAP_WriteByte(addr + 1, tempUnion.u8_t[1]); IAP_WriteByte(addr + 2, tempUnion.u8_t[2]); IAP_WriteByte(addr + 3, tempUnion.u8_t[3]); } #ifdef __DEBUG_STC8G_EEPROM_IAP_ATY /** * @brief test IAP * @note put at init or long cycle, do not put at fast cycle loop */ void IAP_Test(void) { if(IAP_ReadByte(0x0002) == 0xAA) UartSendStr("\r\n\r\nEE OK\r\n"); else { IAP_Erase(0x0000); IAP_WriteByte(0x0002, 0xAA); } { uint8_t count_t = 0; count_t = IAP_ReadByte(0x0003); IAP_Erase(0x0000); IAP_WriteByte(0x0002, 0xAA); IAP_WriteByte(0x0003, count_t + 1); UartSendStr("EE read\r\n"); UartSendByte((count_t / 100) + '0'); UartSendByte(((count_t % 100) / 10) + '0'); UartSendByte((count_t % 10) + '0'); } } #endif /* __DEBUG_STC8G_EEPROM_IAP_ATY */ #endif /* __STC8G_EEPROM_IAP_ATY_C */ /******************************** End Of File *********************************/