123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- #include "nodelinkmaster.h"
- #include "NodeLinkCommon.h"
- #include "encrypt_xxtea.h"
- #include <string.h>
- #include <stdint.h>
- #include "stm32f10x.h"
- #include "core_cm3.h"
- #include "board.h"
- #include "global.h"
- unsigned char LifeSignal;
- unsigned char ResetFlg;
- //unsigned char slave_info[17];
- sw_timer_t decode_err_timer;
- unsigned char BeginRecvFlg;
- /**
- * 超时处理
- *
- * @author lxz (042620 16:02:40)
- *
- * @param device
- */
- static void deivce0_on_timeout(struct nodelink_device *device) {
- }
- /**
- * 接收事件处理
- *
- * @author lxz (042620 16:02:58)
- *
- * @param device
- * @param data
- */
- static void device0_on_recv(struct nodelink_device *device, unsigned char *data) {
- }
- /**
- * 发送事件
- *
- * @author lxz (042620 16:03:17)
- *
- * @param device
- * @param data
- * @param len
- */
- static int device0_on_send(struct nodelink_device *device, unsigned char *data) {
- return 0;
- }
- static nodelink_device_ops device0 = {
- device0_on_recv,
- device0_on_send,
- deivce0_on_timeout
- };
- /**
- * 执行主站初始化
- *
- * @author LXZ (070720)
- *
- * @param master
- */
- void nodelink_master_init(nodelink_master_t *master) {
- unsigned char code[16];
- unsigned char i;
- unsigned long cDelay=1000000;
- //产生主站随机code,这里使用定时器最后8位,
- //由于晶振细微差异,可以保证每次上电不同
- for(i=0;i<16;i++)
- {
- while(cDelay--);
- code[i]=(unsigned char)(SysTick->VAL & 0xff);
- cDelay=1000000;
- }
- //清空内存
- memset(master, 0, sizeof(nodelink_master_t));
- master->device[0].ops = &device0;
- //设置code
- nodelink_master_set_code(master,code);
- //设置开始扫描从站
- nodelink_master_reset(master);
-
- BeginRecvFlg=0;
- }
- /**
- * 通过ID查找设备
- *
- * @author LXZ (070720)
- *
- * @param master
- */
- unsigned char nodelink_find_device_by_ID(nodelink_master_t *master,unsigned char id) {
- int i;
- for(i=1;i<=master->dev_count;i++)
- {
- if(master->device[i].id == id) return i;
- }
- return 0;
- }
- /**
- * 执行主站的接收动作
- *
- * @author lxz (041820 16:51:42)
- *
- * @param master 主站对象
- * @param src 接收缓冲
- * @param len 接收长度
- *
- */
- void nodelink_master_recv(nodelink_master_t *master,
- unsigned char *src,
- int len) {
- int i = 0;
- unsigned char id;
- unsigned long length;
- length = nodelink_decode(&(master->data_in[0]), src, len);
- if (length > 0) {
- //复位
- if(master->data_in[1]==0xA5 && master->data_in[2]==1 && master->bus_status != 0xFF)
- {
- //复位一下总线
- ResetFlg=1;
- }
- else if(master->data_in[1]==0xA4 && master->data_in[2]==0x81)
- {
- //memcpy(slave_info,&master->data_in[0],17);
- for(i=0;i<17;i++)
- {
- user_datas[512+i]=master->data_in[i];
- }
- }
- //分配ID过程中
- else if (master->bus_status == 0xF0)
- {
- if (master->step == 0)//发送了KEY,接收返回信息
- {
- if(master->data_in[1]==0xA0)//数据帧类型
- {
- if (memcmp(&(master->data_in[2]), master->code, 16) == 0) //判断设备合法性
- {
- master->step=1;
- master->count = 5;
- sw_timer_start(&decode_err_timer, 1, 0);
- }
- }
- }
- else if(master->step == 1)//Key验证通过,接收物理ID
- {
- if(master->data_in[1]==0xA1)//数据帧类型
- {
- for(i=length-3;i>=2;i-=3)//从队尾(靠近主站的板卡)开始添加设备
- {
- master->dev_count++;
- master->count = 5;
- master->device[master->dev_count].id = master->data_in[i];
- master->device[master->dev_count].uniqueid = master->data_in[i+1];
- master->device[master->dev_count].model = master->data_in[i+2];
- if (master->callback != 0) {
- master->callback(master,NL_MASTER_EVENT_FOUND_DEVICE, &master->device[master->dev_count]);
- }
- }
- }
- if(master->dev_count>0)
- {
- master->step = 2;//主板通知配置完成
- master->count=50;
- sw_timer_start(&decode_err_timer, 0, 500000);
- }
- }
- else
- {
- sw_timer_start(&decode_err_timer, 0, 500000);
- }
- }
- //正常通信模式
- else if(master->bus_status == 0xF1)
- {
- if(master->data_in[1]==0xA3)//数据帧类型
- {
- id = nodelink_find_device_by_ID(master,master->data_in[0]);
- if(id>0)
- {
- if (master->device[id].ops != 0)
- master->device[id].ops->on_recv(&master->device[id], &(master->data_in[0]));
- sw_timer_start(&decode_err_timer, 0, 200000);
- }
- }
- }
- }
- if (BeginRecvFlg==1 && sw_timer_expire(&decode_err_timer))
- {
- ResetFlg=1;
- }
- }
- /**
- *
- *
- * @author lxz (042620 16:49:13)
- *
- * @param master
- */
- void nodelink_master_reset(nodelink_master_t *master) {
- master->bus_status = 0xFF;
- master->step = 0;
- master->count = 100;
- ResetFlg=0;
- BeginRecvFlg=0;
- }
- /**
- *
- *
- * @author lxz (042620 16:49:17)
- *
- * @param master
- */
- void nodelink_master_begin_scan(nodelink_master_t *master) {
- master->bus_status = 0xF0;
- master->step = 0;
- master->dev_count = 0;
- master->count = 100;
- memset(&master->device[0],0,sizeof(master->device));
- }
- /**
- * 进入工作模式
- *
- * @author LXZ (070720)
- *
- * @param master
- */
- void nodelink_master_begin_working(nodelink_master_t *master) {
- master->bus_status = 0xF1;
- master->step = 0;
- master->count = 5;
- }
- /**
- * 注册16字节识别码
- *
- * @author LXZ (070720)
- *
- * @param master
- * @param code
- */
- void nodelink_master_set_code(nodelink_master_t * master, unsigned char * code)
- {
- memcpy(master->code,code,16);
- }
- /**
- * 注册回调事件
- *
- * @author LXZ (070720)
- *
- * @param master
- */
- void nodelink_master_set_callback(nodelink_master_t * master,
- void(*callback)(nodelink_master_t * master, char , void *))
- {
- master->callback = callback;
- }
- /**
- * 执行主站输入输出处理
- *
- * @author lxz (041820 16:20:35)
- *
- * @param master 主站对象
- * @param dst 目标缓冲
- * @param src 当前缓冲
- * @param len 长度
- */
- int nodelink_master_send(nodelink_master_t *master,
- unsigned char *dst) {
- int i = 0;
- int res = 0;
- int length = 0;
- uint32_t key[4] = { 0x00112233, 0x44556677, 0x8899AABB, 0xCCDDEEFF };
- uint32_t code[4];
- //static uint8_t k;
- static unsigned char curDevice;
- if (master->bus_status == 0xFF) {
- //复位总线,发送指定个数的包
- if (master->count > 0) {
- if(master->count % 2)
- master->data_out[0] = 0x80;//广播ID
- else
- master->data_out[0] = 0x81;//广播ID
- master->data_out[1] = 0xAA;//复位命令
- master->data_out[2] = 4;//帧长度,不包含校验
- master->data_out[3] = LifeSignal++;//生命信号
- length = nodelink_encode(&dst[0], &(master->data_out[0]), 4);
- res = length;
- master->count--;
- }
- else {
- //进入配置ID模式
- nodelink_master_begin_scan(master);
- sw_timer_start(&decode_err_timer, 0, 500000);
- BeginRecvFlg=1;
- }
- }
- else if (master->bus_status == 0xF0) {
- //定向分配ID
- switch (master->step) {
- case 0:
- //需要先执行设置设备进入ID配置模式
- //if (master->count > 0) {
- code[0] = (master->code[0]) | (master->code[1] << 8) | (master->code[2] << 16) | (master->code[3] << 24);
- code[1] = (master->code[4]) | (master->code[5] << 8) | (master->code[6] << 16) | (master->code[7] << 24);
- code[2] = (master->code[8]) | (master->code[9] << 8) | (master->code[10] << 16) | (master->code[11] << 24);
- code[3] = (master->code[12]) | (master->code[13] << 8) | (master->code[14] << 16) | (master->code[15] << 24);
- xxtea_uint_encrypt(code, 4, key);
- master->data_out[0] = 0x80;//广播ID
- master->data_out[1] = 0xA0;//验证CODE命令
- memcpy(&master->data_out[2], code, 16);
- master->data_out[18] = 20;//帧长度
- master->data_out[19] = LifeSignal++;//生命信号
- res = nodelink_encode(dst, &(master->data_out[0]), 20);
- //master->count--;
- //}
- break;
- case 1:
- code[0] = (master->code[0]) | (master->code[1] << 8) | (master->code[2] << 16) | (master->code[3] << 24);
- code[1] = (master->code[4]) | (master->code[5] << 8) | (master->code[6] << 16) | (master->code[7] << 24);
- code[2] = (master->code[8]) | (master->code[9] << 8) | (master->code[10] << 16) | (master->code[11] << 24);
- code[3] = (master->code[12]) | (master->code[13] << 8) | (master->code[14] << 16) | (master->code[15] << 24);
- xxtea_uint_encrypt(code, 4, key);
- master->data_out[0] = 0x80;//广播ID
- master->data_out[1] = 0xA0;//验证CODE命令
- memcpy(&master->data_out[2], code, 16);
- master->data_out[18] = 20;//帧长度
- master->data_out[19] = LifeSignal++;//生命信号
- res = nodelink_encode(dst, &(master->data_out[0]), 20);
- curDevice=1;
- break;
- case 2:
- //配置成功通知
- if (master->count > 0) {
- master->data_out[0] = master->device[curDevice].id;
- master->data_out[1] = 0xA2;//通知命令
- master->data_out[2] = master->device[curDevice].uniqueid;//发送唯一码
- master->data_out[3] = 5;//帧长度
- master->data_out[4] = LifeSignal++;//生命信号
- res = nodelink_encode(dst, &(master->data_out[0]), 5);
- master->count--;
- }
- else
- {
- if(curDevice<master->dev_count)
- {
- curDevice++;
- master->count=50;
- }
- else
- {
- curDevice=1;
- master->count = 0;
- //进入工作模式
- nodelink_master_begin_working(master);
- }
- }
- break;
- }
- }
- else if (master->bus_status == 0xF1) {
- //执行周期数据
- if(curDevice<master->dev_count)
- {
- curDevice++;
- }
- else
- {
- curDevice=1;
- }
- //让设备执行输入
- if (master->device[curDevice].ops != 0) {
- length = master->device[curDevice].ops->on_send(&master->device[curDevice], &(master->data_out[0]));
- if (length > 0) {
- master->data_out[length] = length+2;//帧长度
- length++;
- master->data_out[length] = LifeSignal++;//生命信号
- length++;
- length = nodelink_encode(&dst[0], &(master->data_out[0]), length);
- res = length;
- master->device[curDevice].txcount++;
- }
- }
- }
- return res;
- }
|