control_table.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #include "stm32l1xx_hal.h"
  2. #include "core/csect.h"
  3. #include "core/config.h"
  4. #include "core/config_pins.h"
  5. #include "drivers/switchboard_control/switchboard_control.h"
  6. #include "app/control_table/control_table.h"
  7. static bool Table_Init();
  8. static bool Table_Add_Points( uint8_t * val, uint8_t size );
  9. static bool Table_Clear( );
  10. static void Table_Set_Edit_State(bool state);
  11. static bool Table_Get_Edit_State();
  12. static uint16_t Table_Get_Number_Of_Points();
  13. static bool Table_Get_Point(uint8_t point_number, sTableTablePoint_t* point);
  14. static void Table_DeInit();
  15. static void Start_Meas();
  16. static void Stop_Meas();
  17. static void Continue_Meas( );
  18. static uint8_t Mode_Meas( );
  19. static void PortSet(sTableTablePoint_t portCommutation);
  20. static void SetDefaultState( );
  21. static sTableTablePoint_t PortRead();
  22. static void TriggerPolaritySet(uint8_t polarity);
  23. static uint8_t TriggerPolarityGet ();
  24. static uint8_t TriggerCounterGet ();
  25. const Table_Handle_t TableHandle = {
  26. Table_Init,
  27. Table_Add_Points,
  28. Table_Clear,
  29. Table_Set_Edit_State,
  30. Table_Get_Edit_State,
  31. Table_Get_Number_Of_Points,
  32. Table_Get_Point,
  33. Table_DeInit,
  34. };
  35. const Control_Handle_t ControlHandle = {
  36. Start_Meas,
  37. Stop_Meas,
  38. Continue_Meas,
  39. Mode_Meas,
  40. PortSet,
  41. SetDefaultState,
  42. PortRead,
  43. TriggerPolaritySet,
  44. TriggerPolarityGet,
  45. TriggerCounterGet,
  46. };
  47. static uint32_t currentTableAddr = 0; // Òåêóùèé àäðåñ â òàáëèöå ñîñòîÿíèé (àäðåñ ïîñëåäíåé çàïèñè)
  48. static bool isTableEditState = false;
  49. static bool Table_Init()
  50. {
  51. // Èíèöèàëèçàöèÿ SPI äëÿ ðàáîòû ñ êîììóòàòîðîì
  52. SBHandle.Init();
  53. Table_Get_Edit_State(true);
  54. currentTableAddr = Table_Get_Number_Of_Points();
  55. return true;
  56. }
  57. static void Table_DeInit()
  58. {
  59. Table_Set_Edit_State(false);
  60. currentTableAddr = 0;
  61. }
  62. static bool Validate_Points(uint8_t * point, uint8_t size)
  63. {
  64. if( size % 2 || !size ) return false;
  65. sTableTablePoint_t TablePoint;
  66. for(int i = 0; i < size; i += 2)
  67. {
  68. TablePoint.port1 = *point++;
  69. TablePoint.port2 = *point++;
  70. if( TablePoint.port1 > 16 || TablePoint.port2 > 16
  71. || !TablePoint.port1 || !TablePoint.port2 )
  72. {
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. // Add cell to the end of list
  79. static bool Table_Add_Points( uint8_t * val, uint8_t size )
  80. {
  81. if(!Table_Get_Edit_State()) return false;
  82. if( NULL == val || !size || ! Validate_Points(val, size) ) return false;
  83. for(int i = 0; i < size; i += 2)
  84. {
  85. sTableTablePoint_t TablePoint;
  86. TablePoint.port1 = *val++;
  87. TablePoint.port2 = *val++;
  88. SB_command_t tablePointCmd = {
  89. .cmd = SWB_TABLE_NEW_RECORD,
  90. .addr = currentTableAddr,
  91. .responce_2 = TablePoint.port2,
  92. .responce_1 = TablePoint.port1
  93. };
  94. SBHandle.ClrNSS();
  95. SBHandle.Transmit((uint8_t*)&tablePointCmd, sizeof(tablePointCmd));
  96. SBHandle.SetNSS();
  97. currentTableAddr++;
  98. //if(!SW_ROM_SetDataPoint(eChValues, &TablePoint)) return false;
  99. }
  100. return true;
  101. }
  102. static bool Table_Clear( )
  103. {
  104. SB_command_t tableClearCmd = {
  105. .cmd = SWB_TABLE_CLEAR,
  106. .addr = 0,
  107. .responce_2 = 0,
  108. .responce_1 = 0
  109. };
  110. SBHandle.ClrNSS();
  111. SBHandle.Transmit((uint8_t*)&tableClearCmd, sizeof(uint8_t));
  112. SBHandle.SetNSS();
  113. currentTableAddr = 0;
  114. return true;
  115. //return SW_ROM_Table_Clear();
  116. }
  117. static void Table_Set_Edit_State(bool state)
  118. {
  119. isTableEditState = state;
  120. }
  121. static bool Table_Get_Edit_State()
  122. {
  123. return isTableEditState;
  124. }
  125. static uint16_t Table_Get_Number_Of_Points()
  126. {
  127. if(!Table_Get_Edit_State()) false;
  128. SB_command_t tableGetPointCountCmd = {
  129. .cmd = SWB_TABLE_TABLE_COUNT,
  130. .addr = 0,
  131. .responce_2 = 0,
  132. .responce_1 = 0
  133. };
  134. SBHandle.ClrNSS();
  135. SBHandle.Transmit((uint8_t*)&tableGetPointCountCmd, sizeof(uint8_t));
  136. SBHandle.Receive((uint8_t*)&tableGetPointCountCmd.responce_2, sizeof(uint8_t));
  137. SBHandle.SetNSS();
  138. currentTableAddr = tableGetPointCountCmd.responce_2;
  139. return currentTableAddr;
  140. //return SW_ROM_GetNumberOfPoints();
  141. }
  142. static bool Table_Get_Point(uint8_t point_number, sTableTablePoint_t* point)
  143. {
  144. if( point_number >= currentTableAddr)
  145. {
  146. return false;
  147. }
  148. SB_command_t tableGetPointCmd = {
  149. .cmd = SWB_TABLE_GET_RECORD,
  150. .addr = point_number,
  151. .responce_2 = 0,
  152. .responce_1 = 0
  153. };
  154. SBHandle.ClrNSS();
  155. SBHandle.Transmit((uint8_t*)&tableGetPointCmd, sizeof(uint16_t));
  156. if(!SBHandle.Receive((uint8_t*)&tableGetPointCmd.responce_2, sizeof(uint16_t)))
  157. {
  158. return false;
  159. }
  160. SBHandle.SetNSS();
  161. point->port2 = tableGetPointCmd.responce_2;
  162. point->port1 = tableGetPointCmd.responce_1;
  163. return true;
  164. //return SW_ROM_GetDataPoint( eChValues, point_number, (uint8_t *)point, sizeof(point) );
  165. }
  166. static void Start_Meas()
  167. {
  168. SB_command_t controlStartMeasCmd = {
  169. .cmd = SWB_TABLE_START_MEASURING,
  170. .addr = 0,
  171. .responce_2 = 0,
  172. .responce_1 = 0
  173. };
  174. SBHandle.ClrNSS();
  175. SBHandle.Transmit((uint8_t*)&controlStartMeasCmd, sizeof(uint8_t));
  176. SBHandle.SetNSS();
  177. }
  178. static void Stop_Meas()
  179. {
  180. SB_command_t controlStopMeasCmd = {
  181. .cmd = SWB_TABLE_STOP_MEASURING,
  182. .addr = 0,
  183. .responce_2 = 0,
  184. .responce_1 = 0
  185. };
  186. SBHandle.ClrNSS();
  187. SBHandle.Transmit((uint8_t*)&controlStopMeasCmd, sizeof(uint8_t));
  188. SBHandle.SetNSS();
  189. }
  190. static void Continue_Meas( )
  191. {
  192. SB_command_t controlContinueMeasCmd = {
  193. .cmd = SWB_TABLE_CONTINUE_MEASURING,
  194. .addr = 0,
  195. .responce_2 = 0,
  196. .responce_1 = 0
  197. };
  198. SBHandle.ClrNSS();
  199. SBHandle.Transmit((uint8_t*)&controlContinueMeasCmd, sizeof(uint8_t));
  200. SBHandle.SetNSS();
  201. }
  202. // Çàïðîñ ðåæèìà èçìåðåíèÿ ïðèáîðà
  203. static uint8_t Mode_Meas()
  204. {
  205. SB_command_t controlModeMeasCmd = {
  206. .cmd = SWB_CONTROL_MEASURING_MODE,
  207. .addr = 0,
  208. .responce_2 = 0,
  209. .responce_1 = 0
  210. };
  211. bool result = false;
  212. SBHandle.ClrNSS();
  213. // 0 - Manual, 1 - Table
  214. SBHandle.Transmit((uint8_t*)&controlModeMeasCmd.cmd, sizeof(uint8_t));
  215. result = SBHandle.Receive((uint8_t*)&controlModeMeasCmd.responce_2, sizeof(uint8_t));
  216. SBHandle.SetNSS();
  217. return result ? controlModeMeasCmd.responce_2 : ERROR_CODE;
  218. }
  219. static void PortSet(sTableTablePoint_t portCommutation)
  220. {
  221. struct
  222. {
  223. uint8_t cmd;
  224. uint8_t responce_2;
  225. uint8_t responce_1;
  226. }portSetCmd;
  227. portSetCmd.cmd = SWB_CONTROL_PORT;
  228. portSetCmd.responce_2 = portCommutation.port2;
  229. portSetCmd.responce_1 = portCommutation.port1;
  230. SBHandle.ClrNSS();
  231. SBHandle.Transmit((uint8_t*)&portSetCmd, sizeof(portSetCmd));
  232. SBHandle.SetNSS();
  233. }
  234. static void SetDefaultState( )
  235. {
  236. struct
  237. {
  238. uint8_t cmd;
  239. uint8_t responce_2;
  240. uint8_t responce_1;
  241. }portSetCmd;
  242. portSetCmd.cmd = SWB_CONTROL_PORT;
  243. portSetCmd.responce_2 = 0;
  244. portSetCmd.responce_1 = 0;
  245. SBHandle.ClrNSS();
  246. SBHandle.Transmit((uint8_t*)&portSetCmd, sizeof(portSetCmd));
  247. SBHandle.SetNSS();
  248. }
  249. static sTableTablePoint_t PortRead()
  250. {
  251. SB_command_t controlPortReadCmd = {
  252. .cmd = SWB_CONTROL_READ,
  253. .addr = 0,
  254. .responce_2 = 0,
  255. .responce_1 = 0
  256. };
  257. // ×òåíèå ïåðâîãî ïîðòà
  258. SBHandle.ClrNSS();
  259. SBHandle.Transmit((uint8_t*)&controlPortReadCmd, sizeof(uint8_t));
  260. SBHandle.Receive((uint8_t*)&controlPortReadCmd.responce_2, sizeof(uint16_t));
  261. SBHandle.SetNSS();
  262. sTableTablePoint_t portCommutation;
  263. portCommutation.port1 = controlPortReadCmd.responce_1;
  264. portCommutation.port2 = controlPortReadCmd.responce_2;
  265. return portCommutation;
  266. }
  267. static void TriggerPolaritySet(uint8_t polarity)
  268. {
  269. struct
  270. {
  271. uint8_t cmd;
  272. uint8_t responce_2;
  273. }trigSetCmd;
  274. trigSetCmd.cmd = SWB_CONTROL_SET_TRIGGER_MODE;
  275. trigSetCmd.responce_2 = polarity;
  276. SBHandle.ClrNSS();
  277. SBHandle.Transmit((uint8_t*)&trigSetCmd, sizeof(trigSetCmd));
  278. SBHandle.SetNSS();
  279. }
  280. static uint8_t TriggerPolarityGet()
  281. {
  282. SB_command_t triggerModeCmd = {
  283. .cmd = SWB_CONTROL_GET_TRIGGER_MODE,
  284. .addr = 0,
  285. .responce_2 = 0,
  286. .responce_1 = 0
  287. };
  288. // ×òåíèå ñîñòîÿíèÿ òðèããåðà
  289. SBHandle.ClrNSS();
  290. SBHandle.Transmit((uint8_t*)&triggerModeCmd, sizeof(uint8_t));
  291. SBHandle.Receive((uint8_t*)&triggerModeCmd.responce_2, sizeof(uint8_t));
  292. SBHandle.SetNSS();
  293. return triggerModeCmd.responce_2;
  294. }
  295. static uint8_t TriggerCounterGet()
  296. {
  297. SB_command_t triggerModeCmd = {
  298. .cmd = SWB_CONTROL_GET_TRIGGER_COUN,
  299. .addr = 0,
  300. .responce_2 = 0,
  301. .responce_1 = 0
  302. };
  303. // ×òåíèå ñîñòîÿíèÿ òðèããåðà
  304. SBHandle.ClrNSS();
  305. SBHandle.Transmit((uint8_t*)&triggerModeCmd.cmd, sizeof(uint8_t));
  306. SBHandle.Receive((uint8_t*)&triggerModeCmd.responce_2, sizeof(uint8_t));
  307. SBHandle.SetNSS();
  308. return triggerModeCmd.responce_2;
  309. }