NessLab.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * NessLab.c
  3. *
  4. * Created on: Aug 3, 2020
  5. * Author: parkyj
  6. */
  7. #include "main.h"
  8. #include "NessLab.h"
  9. #include "adc.h"
  10. #include "crc.h"
  11. #include "flash.h"
  12. #include "NessLab.h"
  13. //extern uint8_t NessLab_Checksum(uint8_t* data,uint8_t size);
  14. uint8_t NessLab_Adc_Convert_db();
  15. #define NESSLAB_DATARESPONSE_INDEXSIZE 23
  16. #define NESSLAB_TABLE_LENGTH 110
  17. Nesslab_Prot Currstatus;
  18. extern volatile uint16_t ADC1value[ADC1_CNT];
  19. extern volatile uint32_t UartRxTimerCnt ;
  20. extern volatile uint32_t DC_FAIL_ALARM_CNT ;
  21. extern volatile uint32_t OVER_INPUT_ALARM_CNT ;
  22. extern volatile uint32_t OVER_TEMP_ALARM_CNT ;
  23. extern volatile uint32_t ALC_ALARM_CNT ;
  24. extern volatile uint32_t OVER_POWER_ALARM_CNT ;
  25. extern volatile uint32_t VSWR_ALARM_CNT ;
  26. volatile uint8_t NessLab_TxData[200] = {0,};
  27. uint8_t Flash_DataArray[200] = {0,};
  28. uint8_t DB_Define[100];
  29. void NessLab_Init(){
  30. FLASH_Read_Func(FLASH_USER_USE_START_ADDR + 2,&DB_Define[0],104);
  31. HAL_GPIO_WritePin(PAU_RESET_GPIO_Port,PAU_RESET_Pin, GPIO_PIN_SET);
  32. }
  33. double Round_Function(double value){
  34. double val = value * 100;
  35. val = (int)(val + 0.5);
  36. val *= 0.1;
  37. val = (int)(val + 0.5);
  38. val *= 0.1;
  39. return val;
  40. }
  41. uint16_t Absolute_value_Convert(int16_t val){
  42. if(val < 0)
  43. val *= -1;
  44. return val;
  45. }
  46. uint8_t NessLab_Adc_Convert_db() // ?占쏙옙湲고븿?占쏙옙
  47. {
  48. double CurrAdc = (float)((Currstatus.DownLink_Forward_Det_H << 8 | Currstatus.DownLink_Forward_Det_L)*0.001);
  49. double TableVal = 0;
  50. float ret = 0;
  51. int16_t calc_val = 0,Prev_calc_val = 3300 ;
  52. uint8_t Curr_DB = 0 ;
  53. uint16_t CurrAdc_Temp = 0,TableVal_Temp = 0;
  54. ret = Round_Function(CurrAdc);
  55. // CurrAdc *= 1000;
  56. CurrAdc_Temp = CurrAdc * 1000;
  57. // CurrAdc_Temp = 155;
  58. // CurrAdc_Temp = 156;
  59. for(int i = 0; i <= 50; i++){
  60. TableVal_Temp = ((DB_Define[i * 2] << 8 | DB_Define[(i * 2)+ 1]));
  61. if(TableVal_Temp == 0)
  62. continue;
  63. calc_val = CurrAdc_Temp - TableVal_Temp;
  64. calc_val = Absolute_value_Convert(calc_val);
  65. // printf("%d - %d calc_val : %d \r\n",CurrAdc_Temp,TableVal_Temp,calc_val);
  66. if(Prev_calc_val > calc_val && TableVal_Temp != 0){
  67. Prev_calc_val = calc_val;
  68. if(CurrAdc_Temp < TableVal_Temp)
  69. Curr_DB = i;
  70. else
  71. Curr_DB = i + 1;
  72. // printf("Curr_DB : %d \r\n",Curr_DB);
  73. }else{
  74. // printf("Prev_calc_val : %d \r\n",Prev_calc_val);
  75. // printf("Curr_calc_val : %d \r\n" , calc_val);
  76. }
  77. }
  78. // DB_Define[]
  79. // printf("Curr Db : %d \r\n",Curr_DB);
  80. return Curr_DB;
  81. }
  82. void NessLab_Operate(uint8_t* data){
  83. uint8_t datatype = data[NessLab_MsgID0];
  84. uint8_t UartLength = 0;
  85. static uint16_t MSG_SNCnt = 0;
  86. switch(datatype){
  87. case NessLab_STATUS_REQ:
  88. ADC_Check();
  89. UartLength = NessLab_MAX_INDEX + 1;
  90. MSG_SNCnt = data[NessLab_Req_MsgSN0] << 8 | data[NessLab_Req_MsgSN1];
  91. MSG_SNCnt++;
  92. // if(data[NessLab_Req_Data_Cnt0] > 0)
  93. // NessLab_TxData[NessLab_Over_Power_Alarm] = 1;
  94. // else
  95. // NessLab_TxData[NessLab_Over_Power_Alarm] = 0;
  96. // if(data[NessLab_Req_Data_Cnt1] > 0)
  97. // NessLab_TxData[NessLab_VSWR_ALARM] = 1;
  98. // else
  99. // NessLab_TxData[NessLab_VSWR_ALARM] = 0;
  100. NessLab_TxData[NessLab_MsgSN0] = (uint8_t)((MSG_SNCnt & 0xFF00) >>8);//data[NessLab_Req_MsgSN0];
  101. NessLab_TxData[NessLab_MsgSN1] = (uint8_t)((MSG_SNCnt & 0x00FF));//data[NessLab_Req_MsgSN1] ;
  102. NessLab_Frame_Set(NessLab_TxData,12,NessLab_STATUS_RES);
  103. // NessLab_TxData[10] = 1;
  104. // NessLab_TxData[11] = 0;
  105. // NessLab_TxData[12] = 1;
  106. // NessLab_TxData[13] = 0;
  107. // NessLab_TxData[14] = 1;
  108. // NessLab_TxData[15] = 0;
  109. // NessLab_TxData[16] = 1;
  110. // NessLab_TxData[17] = 0;
  111. break;
  112. case NessLab_Table_REQ:
  113. UartLength = NESSLAB_TABLE_LENGTH;
  114. FLASH_Read_Func(FLASH_USER_USE_START_ADDR,&NessLab_TxData[NessLab_Req_Data_Cnt0],data[NessLab_DataLength]);
  115. NessLab_Table_Frame_Set(NessLab_TxData,102,NessLab_Table_RES);
  116. printf("NessLab_Table_REQ \r\n");
  117. break;
  118. case NessLab_TableSet_REQ:
  119. DataErase_Func(FLASH_USER_USE_START_ADDR,200);
  120. printf("Ram Data Display \r\n");
  121. for(int i = 0; i < data[NessLab_DataLength]; i++){
  122. Flash_DataArray[i] = data[NessLab_Data_ADC1_H + i];
  123. printf("%x ",Flash_DataArray[i]);
  124. }
  125. FLASH_Write_Func(FLASH_USER_USE_START_ADDR,&Flash_DataArray[0],data[NessLab_DataLength]);
  126. UartLength = NESSLAB_TABLE_LENGTH;
  127. NessLab_Table_Frame_Set(NessLab_TxData,104,NessLab_TableSet_RES);
  128. FLASH_Read_Func(FLASH_USER_USE_START_ADDR + 2,&DB_Define[0],104);
  129. // NessLab_Init();
  130. printf("\r\nNessLab_TableSet_REQ \r\n");
  131. break;
  132. case NessLab_PAU_Enable_Req:
  133. HAL_GPIO_WritePin(AMP_EN_GPIO_Port,AMP_EN_Pin, GPIO_PIN_SET);
  134. break;
  135. case NessLab_PAU_Disable_Req:
  136. HAL_GPIO_WritePin(AMP_EN_GPIO_Port,AMP_EN_Pin, GPIO_PIN_RESET);
  137. break;
  138. }
  139. Uart1_Data_Send(&NessLab_TxData[NessLab_Header0], UartLength);
  140. }
  141. /*
  142. Rx Total Byte
  143. 7e 7e 65 00 01 ff 02 00 00 99 7e 7e
  144. Tx Total Byte :
  145. 7e 7e 66 00 02 00 0c 04 20 00 00 00 00 00 00 00 00 00 68 7e 7e 0a
  146. */
  147. void NessLab_Frame_Set(uint8_t* data,uint8_t size){
  148. data[NessLab_Header0] = 0x7E;
  149. data[NessLab_Header1] = 0x7E;
  150. data[NessLab_MsgID0] = NessLab_STATUS_RES;// ID
  151. // data[NessLab_MsgSN0] = 0; // SEQ NUMBER
  152. // data[NessLab_MsgSN1] = 0; // SEQ NUMBER
  153. data[NessLab_Reserve0] = 0; // NessLab_Reserve0
  154. data[NessLab_DataLength] = size; // Nesslab Size
  155. data[NessLab_Data_ADC1_H] = Currstatus.DownLink_Forward_Det_H;//(uint8_t)((ADC1value[0] & 0xFF00) >> 8);
  156. data[NessLab_Data_ADC1_L] = Currstatus.DownLink_Forward_Det_L;//(uint8_t)(ADC1value[0] & 0x00FF);
  157. data[NessLab_Data_ADC1_Table_Value] = NessLab_Adc_Convert_db();
  158. if(DC_FAIL_ALARM_CNT > 3000)
  159. data[NessLab_DC_FAIL_ALARM] = 1;
  160. else
  161. data[NessLab_DC_FAIL_ALARM] = 0;
  162. if(OVER_INPUT_ALARM_CNT > 3000)
  163. data[NessLab_Over_Input_Alarm] = 1;
  164. else
  165. data[NessLab_Over_Input_Alarm] = 0;
  166. if(OVER_TEMP_ALARM_CNT > 3000)
  167. data[NessLab_Over_Temp_Alarm] = 1;
  168. else
  169. data[NessLab_Over_Temp_Alarm] = 0;
  170. if(ALC_ALARM_CNT > 3000)
  171. data[NessLab_ALC_ALARM] = 1;
  172. else
  173. data[NessLab_ALC_ALARM] = 0;
  174. if(OVER_POWER_ALARM_CNT > 3000)
  175. data[NessLab_Over_Power_Alarm] = 1;
  176. else
  177. data[NessLab_Over_Power_Alarm] = 0;
  178. if(VSWR_ALARM_CNT > 3000)
  179. data[NessLab_VSWR_ALARM] = 1;
  180. else
  181. data[NessLab_VSWR_ALARM] = 0;
  182. data[NessLab_DownLink_Status] = 0;
  183. data[NessLab_Temp_Monitor] = Currstatus.Temp_Monitor;
  184. data[NessLab_ChecksumVal] = NessLab_Checksum(&data[NessLab_MsgID0], NessLab_MAX_INDEX - 5);
  185. /* Exception Header Tail Checksum */
  186. data[NessLab_Tail0] = 0x7E;
  187. data[NessLab_Tail1] = 0x7E;
  188. data[NessLab_Tail1 + 1] = 0x0A;
  189. NessLab_Protocol_LastCheck(&data[NessLab_MsgID0],16);
  190. }
  191. void NessLab_Protocol_LastCheck(uint8_t* data,uint8_t size){
  192. int cnt = NessLab_MsgID0;
  193. for(int i = cnt; i < 17; i++){
  194. if(data[i] == 0x7e){
  195. data[cnt++] = 0x7d;
  196. data[cnt++] = 0x5e;
  197. }
  198. else if(data[i] == 0x7d){
  199. data[cnt++] = 0x7d;
  200. data[cnt++] = 0x5d;
  201. }else{
  202. data[i++] = data[i];
  203. }
  204. }
  205. }
  206. void NessLab_Table_Frame_Set(uint8_t* data,uint8_t size,uint8_t mode){
  207. uint32_t i = 0;
  208. uint32_t CurrApiAddress = 0;
  209. CurrApiAddress = FLASH_USER_USE_START_ADDR;
  210. uint8_t* Currdata = (uint8_t*)CurrApiAddress;
  211. uint8_t* pdata;
  212. data[i++] = 0x7E;
  213. data[i++] = 0x7E;
  214. data[i++] = mode;// ID
  215. data[i++] = 0; // SEQ NUMBER
  216. data[i++] = 0; // SEQ NUMBER
  217. data[i++] = 0; // NessLab_Reserve0
  218. data[i++] = size; // Nesslab Size
  219. // NessLab_TalbleFlash_Read(&data[NessLab_DataLength + 1],100);
  220. for(int a = 0; a < size; a++){
  221. data[i++] = Currdata[a];
  222. // printf("%02x ",Currdata[i]);
  223. }
  224. data[i++] = NessLab_Checksum(&data[NessLab_MsgID0], 100 + 5);
  225. /* Exception Header Tail Checksum */
  226. data[i++] = 0x7E;
  227. data[i++] = 0x7E;
  228. }
  229. void NessLab_Status_Check(){
  230. //HAL_GPIO_ReadPin(, GPIO_Pin)
  231. }
  232. void NessLab_PAU_Enable(){
  233. #if 1
  234. if( HAL_GPIO_ReadPin(PAU_EN_GPIO_Port, PAU_EN_Pin) != HAL_GPIO_ReadPin(AMP_EN_GPIO_Port, AMP_EN_Pin) ){
  235. HAL_GPIO_WritePin(AMP_EN_GPIO_Port,AMP_EN_Pin, HAL_GPIO_ReadPin(PAU_EN_GPIO_Port, PAU_EN_Pin));
  236. printf("HAL_GPIO_ReadPin(PAU_EN_GPIO_Port, PAU_EN_Pin) : %d \r\n",HAL_GPIO_ReadPin(PAU_EN_GPIO_Port, PAU_EN_Pin));
  237. printf("AMP_EN_GPIO_Port : %d \r\n",HAL_GPIO_ReadPin(AMP_EN_GPIO_Port, AMP_EN_Pin));
  238. // HAL_Delay(1000);
  239. }
  240. //HAL_GPIO_WritePin(AMP_EN_GPIO_Port,AMP_EN_Pin, HAL_GPIO_ReadPin(PAU_RESET_GPIO_Port, PAU_RESET_Pin));
  241. #else
  242. HAL_GPIO_WritePin(AMP_EN_GPIO_Port,AMP_EN_Pin, 0);
  243. printf("AMP_EN_GPIO_Port : %d \r\n",HAL_GPIO_ReadPin(AMP_EN_GPIO_Port, AMP_EN_Pin));
  244. #endif
  245. }
  246. void NessLab_GPIO_Operate(){
  247. NessLab_PAU_Enable();
  248. }
  249. void NessLab_ADC_Convert_Table(){
  250. }