diff --git a/.vscode/settings.json b/.vscode/settings.json index 1a4b429..b9359b5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -27,6 +27,10 @@ "app_config.h": "c", "type_traits": "c", "task_btn.h": "c", - "bsp_btn.h": "c" + "bsp_btn.h": "c", + "app_data_save.h": "c", + "task_modbus.h": "c", + "app_task_tcp.h": "c", + "msp_eeprom.h": "c" } } \ No newline at end of file diff --git a/source/app/app_config.c b/source/app/app_config.c index 0df36d2..925064b 100644 --- a/source/app/app_config.c +++ b/source/app/app_config.c @@ -1,7 +1,8 @@ #include "app_config.h" ///#include "../bsp/cs1232.h" #include "../bsp/chipid.h" -#include "../msp/eeprom.h" +// #include "../msp/eeprom.h" +#include "../msp/msp_eeprom.h" #include "../clib/clib.h" diff --git a/source/app/app_data_save.c b/source/app/app_data_save.c new file mode 100644 index 0000000..866d697 --- /dev/null +++ b/source/app/app_data_save.c @@ -0,0 +1,58 @@ +#include "../app/app_data_save.h" +#include "../msp/msp_eeprom.h" +#include "app_config.h" + + +DATA_SAVE data_save_arr[DATA_SAVE_LEN] = {0}; + +//初始化存放数据的结构体数组data_save_arr +void data_save_init() +{ + int i = 0; + for(i = 0;i < DATA_SAVE_LEN; i++) + { + data_save_arr[i].head[0] = HEAD0; + data_save_arr[i].head[1] = HEAD1; + data_save_arr[i].available = 1; + data_save_arr[i].val = 0; + data_save_arr[i].tail[0] = TAIL0; + data_save_arr[i].tail[1] = TAIL1; + } +} +//存放数据到eeprom中:1.从eeprom中取出数组 2.遍历寻找空位 3.把数组放回(写入)eeprom中 +void data_save_push(U8 Data) +{ + int i = 0; + int free = 0; + // 从eeprom中取出数组 + L0_Iap_Read_array(EEPROM_DATA_ADDR, data_save_arr,sizeof(data_save_arr)); + if(data_save_arr[0].head[0] != HEAD0 || data_save_arr[0].head[1] != HEAD1 + || data_save_arr[0].tail[0] != TAIL0 || data_save_arr[0].tail[1] != TAIL1) + { + data_save_init(); + } + // 遍历寻找空位 + for(i = 0;i < DATA_SAVE_LEN; i++) + { + if(data_save_arr[i].available == 1) + { + free = i; + break; + } + } + // 当都有数据时 + if(i == DATA_SAVE_LEN) + { + for(i = 0; i < DATA_SAVE_LEN - 1; i++) + { + data_save_arr[i] = data_save_arr[i+1]; + } + free = DATA_SAVE_LEN - 1; + } + // 放到free位置 + data_save_arr[free].available = 0; + data_save_arr[free].val = Data; + + // 把数组放回(写入)eeprom中 + L0_Iap_Program_array(EEPROM_DATA_ADDR, data_save_arr,sizeof(data_save_arr)); +} diff --git a/source/app/app_data_save.h b/source/app/app_data_save.h new file mode 100644 index 0000000..1ac0042 --- /dev/null +++ b/source/app/app_data_save.h @@ -0,0 +1,32 @@ +#ifndef _APP_DATA_SAVE_H +#define _APP_DATA_SAVE_H + +#include "../bsp/bsp_config.h" + +//定义所使用内存的起始和结尾标志 +#define HEAD0 0xa3 +#define HEAD1 0xa4 +#define TAIL0 0xa5 +#define TAIL1 0xa6 + +#define DATA_SAVE_LEN 10 //寄存器需要存放的最大数量为10组 + +#define EEPROM_DATA_ADDR 0x00 //定义一个起始地址为0x00的寄存器 + +//用来存储重量数据的结构体(数组) +typedef struct +{ + U8 head[2]; + U8 available; + U8 val; + U8 tail[2]; + +}DATA_SAVE; + +extern DATA_SAVE data_save_arr[DATA_SAVE_LEN]; + + +extern void data_save_init(void); +extern void data_save_push(U8 Data); + +#endif \ No newline at end of file diff --git a/source/app/main.c b/source/app/main.c index fc3df0c..d54bd9e 100644 --- a/source/app/main.c +++ b/source/app/main.c @@ -65,7 +65,9 @@ void L0_main_init(void) void main(void) { int i; - u8 voice_30[6] = {0x7E,0x04,0xAE,0x1E,0xD0,0xEF}; + u8 voice_30[6] = {0x7E,0x04,0xAE,0x1E,0xD0,0xEF}; + u8 voice_13[6] = {0x7E,0x04,0xAE,0x0D,0xBF,0xEF}; + //初始化 L0_main_init(); //获取mcu id @@ -83,6 +85,14 @@ void main(void) Lc_delay_ms(1000); L0_uart3_sendArray((U8 *)&audio_up,9); + // 上电读出寄存器中的值,放到数组中 + L0_Iap_Read_array(EEPROM_DATA_ADDR, data_save_arr,sizeof(data_save_arr)); + if(data_save_arr[0].head[0] != HEAD0 || data_save_arr[0].head[1] != HEAD1 + || data_save_arr[0].tail[0] != TAIL0 || data_save_arr[0].tail[1] != TAIL1) + { + data_save_init(); + } + while(1) { L1_Oid_readoid(); @@ -92,15 +102,25 @@ void main(void) s_nos_tick.t1s_heartbeat = 0;//置0清空 D_print_heartbeat() L0_uart0_uc('.'); + L0_uart0_sendstr("eeprom test :"); + L0_uart0_0d0a(); + for (i = 0; i < DATA_SAVE_LEN; i++) + { + L0_uart0_uchex(i); + L0_uart0_uc('='); + L0_uart0_uchex(data_save_arr[i].val); + L0_uart0_0d0a(); + } + // L0_uart0_sendArrayHex(data_save_arr, 30); } - #if 0 + // 串口2的 接收数据 测试 - // if(ts_uart[uNum2].r.ok == 1) - // { - // ts_uart[uNum2].r.ok = 0; - // L0_uart0_sendArray(ts_uart[uNum2].r.buf, D_recv2_max); - // } - + if(ts_uart[uNum2].r.ok == 1) + { + ts_uart[uNum2].r.ok = 0; + L0_uart0_sendArray(ts_uart[uNum2].r.buf, D_recv2_max); + } + #if 0 // 串口3的 接收数据 测试 if(ts_uart[uNum3].r.ok == 1) { diff --git a/source/app/main.h b/source/app/main.h index 79d6f0b..f891d3e 100644 --- a/source/app/main.h +++ b/source/app/main.h @@ -46,7 +46,8 @@ #include "../msp/uart3.h" #include "../msp/uart4.h" -#include "../msp/eeprom.h" +// #include "../msp/eeprom.h" +#include "../msp/msp_eeprom.h" #include "../bsp/bsp_config.h" #include "../bsp/chipid.h" @@ -65,6 +66,8 @@ #include "../app/app_task_tcp_control.h" #include "../app/app_task_speech.h" #include "../app/task_game.h" +#include "../app/task_btn.h" +#include "../app/app_data_save.h" #include "../asp/asp_oid.h" diff --git a/source/app/task_btn.c b/source/app/task_btn.c index f7cf894..0e07d23 100644 --- a/source/app/task_btn.c +++ b/source/app/task_btn.c @@ -2,6 +2,8 @@ #include "../bsp/bsp_btn.h" #include "../bsp/bsp_config.h" #include "../msp/UART0.h" +#include "../app/app_data_save.h" + void task_btn(void) { @@ -14,7 +16,8 @@ void task_btn(void) L0_uart0_uchex(g_Key); L0_uart0_0d0a(); g_Key = KEY_NULL; - g_KeyActionFlag = NULL_KEY; + g_KeyActionFlag = NULL_KEY; + data_save_push(0xab); } else if(g_KeyActionFlag == SHORT_KEY) { @@ -24,6 +27,7 @@ void task_btn(void) LED1 = ~LED1; g_Key = KEY_NULL; g_KeyActionFlag = NULL_KEY; + data_save_push(0xcd); } break; case KEY_DOWN: diff --git a/source/bsp/bsp_btn.c b/source/bsp/bsp_btn.c index c1aadde..1116683 100644 --- a/source/bsp/bsp_btn.c +++ b/source/bsp/bsp_btn.c @@ -9,7 +9,6 @@ #include "bsp_btn.h" #include "bsp_config.h" -#include "bsp_config.h" #include "../msp/UART0.h" extern KEY_STATE KeyState = KEY_CHECK; // 初始化按键状态为检测状态 diff --git a/source/bsp/bsp_config.c b/source/bsp/bsp_config.c index a280703..7dd29bb 100644 --- a/source/bsp/bsp_config.c +++ b/source/bsp/bsp_config.c @@ -1,6 +1,7 @@ #include "bsp_config.h" #include "../msp/msp_UART0.h" -#include "../msp/eeprom.h" +// #include "../msp/eeprom.h" +#include "../msp/msp_eeprom.h" //////////////////////////////////////////////////////////////////////////// ///@copyright Copyright (c) 2018, 传控科技 All rights reserved. diff --git a/source/msp/eeprom.c b/source/msp/msp/eeprom.c similarity index 97% rename from source/msp/eeprom.c rename to source/msp/msp/eeprom.c index 78ff57c..628c0cc 100644 --- a/source/msp/eeprom.c +++ b/source/msp/msp/eeprom.c @@ -93,7 +93,8 @@ char L0_Iap_Read(vU16 addr) char dat; IAP_CONTR = 0x80; //使能IAP - IAP_TPS = 12; + // IAP_TPS = 12; + IAP_TPS = 22; IAP_CMD = 1; //设置IAP读命令 IAP_ADDRL = addr; //设置IAP低地址 IAP_ADDRH = addr >> 8; //设置IAP高地址 @@ -109,7 +110,8 @@ char L0_Iap_Read(vU16 addr) void L0_Iap_Program(vU16 addr, char dat) { IAP_CONTR = 0x80; //使能IAP - IAP_TPS = 12; //设置擦除等待参数 12MHz + // IAP_TPS = 12; + IAP_TPS = 22; //设置擦除等待参数 12MHz IAP_CMD = 2; //设置IAP写命令 IAP_ADDRL = addr; //设置IAP低地址 IAP_ADDRH = addr >> 8; //设置IAP高地址 @@ -125,7 +127,8 @@ void L0_Iap_Program(vU16 addr, char dat) void L0_Iap_Erase(vU16 addr) { IAP_CONTR = 0x80; //使能IAP - IAP_TPS = 12; //设置擦除等待参数 12MHz + // IAP_TPS = 12; + IAP_TPS = 22; //设置擦除等待参数 12MHz IAP_CMD = 3; //设置IAP擦除命令 IAP_ADDRL = addr; //设置IAP低地址 IAP_ADDRH = addr >> 8; //设置IAP高地址 diff --git a/source/msp/eeprom.h b/source/msp/msp/eeprom.h similarity index 99% rename from source/msp/eeprom.h rename to source/msp/msp/eeprom.h index e1e7f40..5eec452 100644 --- a/source/msp/eeprom.h +++ b/source/msp/msp/eeprom.h @@ -46,8 +46,10 @@ extern U8 L1_eep_erase_sector(U8 sector); extern void L0_Iap_Erase(vU16 addr); extern void L0_Iap_Program(vU16 addr, char dat); extern char L0_Iap_Read(vU16 addr); + extern void L0_Iap_Program_array(vU16 addr,U8 *buf,U8 len); extern void L0_Iap_Read_array(vU16 addr,U8 *buf,U8 len); + extern void L1_Iap_main(void); extern void L3_eeprom_fun(U8 *pPara); diff --git a/source/msp/msp_eeprom.c b/source/msp/msp_eeprom.c new file mode 100644 index 0000000..3a89cfa --- /dev/null +++ b/source/msp/msp_eeprom.c @@ -0,0 +1,238 @@ +//////////////////////////////////////////////////////////////////////////// +///@copyright Copyright (c) 2018, 传控科技 All rights reserved. +///------------------------------------------------------------------------- +/// @file msp_eeprom.c +/// @brief msp @ driver config +///------------------------------------------------------------------------- +/// @version 1.0 +/// @author CC +/// @date 20190106 +/// @note cc_AS_stc02 由stc-isp v6.0860 +////////////////////////////////////////////////////////////////////////////// + +///cc 2023/04/07--8:44:38 +/* +EEPROM 的写操作只能将字节中的 1 写为 0,当需要将字节中的 0 写为 1,则必须执行扇区 擦除操作。 +EEPROM 的读/写操作是以 1 字节为单位进行,而 EEPROM 擦除操作是以 1 扇区(512 字节) 为单位进行, +在执行擦除操作时,如果目标扇区中有需要保留的数据,则必须预先将这些数据读取到 RAM 中暂存, +待擦除完成后再将保存的数据和需要更新的数据一起再写回 +EEPROM Write operation can only write 1 in the byte as 0, when you need to write 0 in the byte as 1, +you must be sector erased. EEPROM Read / write operation is conducted in 1 byte, +while EEPROM, erasure operation is conducted in 1 sector (512 bytes). During the wipe operation, +if the data in the target sector, the data must be read in the RAM in advance, +and the saved data will be written back together with the data to be updated + +fixme:注意时钟和flash 相关 同时关注掉电和容错 + +*/ + +#include "msp_eeprom.h" + +#include "debug_drv.h" + +#if(TYPE_MCU == TYPE_MCU_STC_8A || TYPE_MCU == TYPE_MCU_STC_8F) +xxx +#define WT_30M 0x80 +#define WT_24M 0x81 +#define WT_20M 0x82 +#define WT_12M 0x83 +#define WT_6M 0x84 +#define WT_3M 0x85 +#define WT_2M 0x86 +#define WT_1M 0x87 + +//sdfasdfasdf +void L0_Iap_Idle() +{ + IAP_CONTR = 0; //关闭IAP功能 + IAP_CMD = 0; //清除命令寄存器 + IAP_TRIG = 0; //清除触发寄存器 + IAP_ADDRH = 0x80; //将地址设置到非IAP区域 + IAP_ADDRL = 0; +} + +char L0_Iap_Read(vU16 addr) +{ + char dat; + + IAP_CONTR = WT_12M; //使能IAP + IAP_CMD = 1; //设置IAP读命令 + IAP_ADDRL = addr; //设置IAP低地址 + IAP_ADDRH = addr >> 8; //设置IAP高地址 + IAP_TRIG = 0x5a; //写触发命令(0x5a) + IAP_TRIG = 0xa5; //写触发命令(0xa5) + _nop_(); + dat = IAP_DATA; //读IAP数据 + L0_Iap_Idle(); //关闭IAP功能 + + return dat; +} + +void L0_Iap_Program(vU16 addr, char dat) +{ + IAP_CONTR = WT_12M; //使能IAP + IAP_CMD = 2; //设置IAP写命令 + IAP_ADDRL = addr; //设置IAP低地址 + IAP_ADDRH = addr >> 8; //设置IAP高地址 + IAP_DATA = dat; //写IAP数据 + IAP_TRIG = 0x5a; //写触发命令(0x5a) + IAP_TRIG = 0xa5; //写触发命令(0xa5) + _nop_(); + L0_Iap_Idle(); //关闭IAP功能 +} + +///每个扇区512字节 +///指定地址可以为当前扇区内的任意地址,都会完整擦除当前扇区 +void L0_Iap_Erase(vU16 addr) +{ + IAP_CONTR = WT_12M; //使能IAP + IAP_CMD = 3; //设置IAP擦除命令 + IAP_ADDRL = addr; //设置IAP低地址 + IAP_ADDRH = addr >> 8; //设置IAP高地址 + IAP_TRIG = 0x5a; //写触发命令(0x5a) + IAP_TRIG = 0xa5; //写触发命令(0xa5) + _nop_(); // + L0_Iap_Idle(); //关闭IAP功能 +} + +#elif (TYPE_MCU == TYPE_MCU_STC_8G || TYPE_MCU == TYPE_MCU_STC_8H) +void L0_Iap_Idle() +{ + IAP_CONTR = 0; //关闭IAP功能 + IAP_CMD = 0; //清除命令寄存器 + IAP_TRIG = 0; //清除触发寄存器 + IAP_ADDRH = 0x80; //将地址设置到非IAP区域 + IAP_ADDRL = 0; +} + +char L0_Iap_Read(vU16 addr) +{ + char dat; + + IAP_CONTR = 0x80; //使能IAP + IAP_TPS = 12; + IAP_CMD = 1; //设置IAP读命令 + IAP_ADDRL = addr; //设置IAP低地址 + IAP_ADDRH = addr >> 8; //设置IAP高地址 + IAP_TRIG = 0x5a; //写触发命令(0x5a) + IAP_TRIG = 0xa5; //写触发命令(0xa5) + _nop_(); + dat = IAP_DATA; //读IAP数据 + L0_Iap_Idle(); //关闭IAP功能 + + return dat; +} + +void L0_Iap_Program(vU16 addr, char dat) +{ + IAP_CONTR = 0x80; //使能IAP + IAP_TPS = 12; //设置擦除等待参数 12MHz + IAP_CMD = 2; //设置IAP写命令 + IAP_ADDRL = addr; //设置IAP低地址 + IAP_ADDRH = addr >> 8; //设置IAP高地址 + IAP_DATA = dat; //写IAP数据 + IAP_TRIG = 0x5a; //写触发命令(0x5a) + IAP_TRIG = 0xa5; //写触发命令(0xa5) + _nop_(); + L0_Iap_Idle(); //关闭IAP功能 +} + +///每个扇区512字节 +///指定地址可以为当前扇区内的任意地址,都会完整擦除当前扇区 +void L0_Iap_Erase(vU16 addr) +{ + IAP_CONTR = 0x80; //使能IAP + IAP_TPS = 12; //设置擦除等待参数 12MHz + IAP_CMD = 3; //设置IAP擦除命令 + IAP_ADDRL = addr; //设置IAP低地址 + IAP_ADDRH = addr >> 8; //设置IAP高地址 + IAP_TRIG = 0x5a; //写触发命令(0x5a) + IAP_TRIG = 0xa5; //写触发命令(0xa5) + _nop_(); // + L0_Iap_Idle(); //关闭IAP功能 +} +#endif + + +void L0_Iap_Program_array(vU16 addr,U8 *buf,U8 len) +{ + U8 i = 0; + L0_Iap_Erase(addr);/////fixme: 需要和addr配合好 一次就擦除512字节 + for(i=0;icmd) + { + //Ae 03 03 04 00 + //xx 03 R2 R3 xx + case 4: + L0_Iap_Erase(uf_ee_add.us); + break; + case 5: + L0_Iap_Program(uf_ee_add.us,p->R2); + break; + case 6: + L0_uart0_uc(L0_Iap_Read(uf_ee_add.us)); + break; + case 7:///选择地址 + uf_ee_add.BYTE2.H = p->R2; //h + uf_ee_add.BYTE2.L = p->R3; //L + break; + default: + break; + }; +} + + +****************************************************/ + + + diff --git a/source/msp/msp_eeprom.h b/source/msp/msp_eeprom.h new file mode 100644 index 0000000..b6015dc --- /dev/null +++ b/source/msp/msp_eeprom.h @@ -0,0 +1,41 @@ +////////////////////////////////////////////////////////////////////////// +/// COPYRIGHT NOTICE +/// Copyright (c) 2018, 传控科技 +/// All rights reserved. +/// +/// @file msp_eeprom +/// @brief msp_eeprom +/// @info +///(本文件实现的功能的详述) +/// +/// @version 1.1 CCsens technology +/// @author CC +/// @date 20190106 + +// +////////////////////////////////////////////////////////////////////////// + +#ifndef _msp_eeprom_H_ +#define _msp_eeprom_H_ + +#include "c_type51.h" +#include "c_lib.h" + + +///>>>端口位定义,可修改!!!!!!>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + +#include "../bsp/bsp_config.h" + +extern void L3_eeprom_fun(U8 *pPara); + +///每个扇区512字节 +extern void L0_Iap_Erase(vU16 addr); +extern void L0_Iap_Program(vU16 addr, char dat); +extern char L0_Iap_Read(vU16 addr); +extern void L0_Iap_Program_array(vU16 addr,U8 *buf,U8 len); +extern void L0_Iap_Read_array(vU16 addr,U8 *buf,U8 len); +extern void L1_Iap_main(void); + +#endif// #ifndef _msp_eeprom_H_ + +