NessLab.c 9.2 KB

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