For DOS Protected Mode version of sensor core software
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1956 lines
73 KiB

3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
3 years ago
​
​
​
​
​
​
​
​
3 years ago
​
3 years ago
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
3 years ago
3 years ago
​
​
​
3 years ago
​
​
​
​
​
​
​
​
3 years ago
​
3 years ago
​
​
​
3 years ago
3 years ago
3 years ago
​
​
​
3 years ago
​
3 years ago
3 years ago
​
3 years ago
​
3 years ago
​
​
​
​
​
​
​
​
​
​
3 years ago
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
3 years ago
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
3 years ago
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
3 years ago
​
​
3 years ago
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
3 years ago
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
3 years ago
​
​
3 years ago
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
3 years ago
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
3 years ago
​
3 years ago
​
​
3 years ago
3 years ago
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
3 years ago
​
3 years ago
3 years ago
​
​
3 years ago
3 years ago
​
3 years ago
​
​
3 years ago
​
​
3 years ago
​
​
3 years ago
3 years ago
​
​
3 years ago
3 years ago
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
3 years ago
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: board_.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of board_.cpp
  13. //----------------------------------------------------------------------------
  14. #include "board.h"
  15. #ifdef WATCHDOG_ENABLED
  16. #include "watchdog.h"
  17. #endif
  18. #include "hardware.h"
  19. #ifndef NO_WDT2_INIT
  20. #include "clock.h"
  21. #endif
  22. #define io_delay( X ) {for( uint16_t i=0; i<(X*50); i++ ) io_wait();}
  23. /*
  24. bool IsVortex( void ){
  25. #ifdef VORTEX_BRIDGES
  26. unsigned long CHID;
  27. unsigned int CC;
  28. //CHID = read_nb(0x90);
  29. outl( 0x80000090, 0xcf8 );
  30. CHID = inl( 0xcfc );
  31. if( CHID == 0x31504d44L ) return true;
  32. if( CHID == 0x32504d44L ) return true;
  33. if( CHID == 0x33504d44L ) return true;
  34. //! Returnung \b false if no Board ID is matched
  35. // Old PC - Wait State long
  36. //make_ws8();
  37. return false;
  38. #else
  39. return true;
  40. #endif
  41. };
  42. */
  43. #ifdef DMA_TRANSFERS
  44. #ifdef __DJGPP__
  45. //int dma_buffer_selector;
  46. //int dma_buffer_segment;
  47. uint32_t _dma_dos_linear_address[2] = {0};
  48. uint8_t _dma_dos_buffer_iterator = 0;
  49. //__dpmi_raddr _dma_buffer_ra = {0};
  50. __dpmi_paddr _dma_buffer_pa = {0};
  51. int _dma_dos_selector[2] = {-1};
  52. unsigned char * DMA_BUFFER = NULL;
  53. #else
  54. unsigned char * DMA_BUFFER = NULL;
  55. //uint32_t DMA_BUFER_LINEAR_ADDRESS = 0;
  56. #endif
  57. #ifdef __DJGPP__
  58. void KillDMA( int DMA ){
  59. if( _dma_dos_selector[_dma_dos_buffer_iterator] == -1 ) return;
  60. if( _dma_dos_linear_address[_dma_dos_buffer_iterator] == 0 ) return;
  61. outportb( 0x0A, 4 | DMA ); //Mask DMA channel
  62. };
  63. void Free_DMA_Buffer(){
  64. if( _dma_dos_selector[0] != -1 ) {
  65. if( __dpmi_free_dos_memory( _dma_dos_selector[0] ) == 0 ){
  66. _dma_dos_selector[0] = -1;
  67. _dma_dos_linear_address[0] = 0;
  68. };
  69. };
  70. if( _dma_dos_selector[1] != -1 ) {
  71. if( __dpmi_free_dos_memory( _dma_dos_selector[1] ) == 0 ){
  72. _dma_dos_selector[1] = -1;
  73. _dma_dos_linear_address[1] = 0;
  74. };
  75. };
  76. _dma_dos_buffer_iterator = 0;
  77. if( DMA_BUFFER ) {
  78. free( DMA_BUFFER );
  79. DMA_BUFFER = NULL;
  80. };
  81. };
  82. bool InitDMA( int DMA ){
  83. #else
  84. void InitDMA( int DMA, unsigned char *Buffer , unsigned int Size ){
  85. #endif
  86. #ifndef BOARD_SIMULATION
  87. #ifdef __DJGPP__
  88. //if no memory assigned
  89. if( _dma_dos_selector[_dma_dos_buffer_iterator] == -1 ) return false;
  90. if( _dma_dos_linear_address[_dma_dos_buffer_iterator] == 0 ) return false;
  91. uint16_t Page, OffSet;
  92. Page = _dma_dos_linear_address[_dma_dos_buffer_iterator] >> 16; //Calculate page
  93. OffSet = _dma_dos_linear_address[_dma_dos_buffer_iterator] & 0xFFFF; //Calculate offset in the page
  94. outportb (0x0A, 4 | DMA); //Mask DMA channel
  95. outportb (0x0C, 0); //Clear byte pointer
  96. outportb (0x0B, 0x14 | DMA); //Set mode
  97. /*
  98. The mode consists of the following:
  99. 0x14 + x = binary 00 01 01 xx
  100. || || || ||
  101. || || || ++- DMA channel
  102. || || |+--- Write operation (the DSP writes to memory)
  103. || || +---- Read operation (the DSP reads from memory)
  104. || |+------ Autoinit
  105. || +------- Increment - 1/Decrement - 0
  106. |+--------- Single mode - 1 -+--- 00 - on Demand
  107. +---------- Block mode - 1 -+
  108. */
  109. outportb (DMA << 1, OffSet & 0xFF ); //Write the offset to the DMA controller
  110. outportb (DMA << 1, OffSet >> 8 );
  111. if (DMA == 0) outportw ( 0x87, Page );
  112. if (DMA == 1) outportw ( 0x83, Page );
  113. if (DMA == 3) outportw ( 0x82, Page );
  114. outportb (( DMA << 1 ) + 1, ( DMA_TRANSFER_CHUNK - 1 ) & 0xFF );
  115. outportb (( DMA << 1 ) + 1, ( ( DMA_TRANSFER_CHUNK - 1 ) >> 8 ) & 0xFF );
  116. outportb (0x0A, DMA); //Unmask DMA channel
  117. return true;
  118. #else
  119. long LinearAddress;
  120. unsigned int Page, OffSet;
  121. //Convert pointer to linear address
  122. LinearAddress = FP_SEG ( Buffer );
  123. LinearAddress = ( LinearAddress << 4 ) + FP_OFF( Buffer );
  124. Page = LinearAddress >> 16; //Calculate page
  125. OffSet = LinearAddress & 0xFFFF; //Calculate offset in the page
  126. outportb (0x0A, 4 | DMA); //Mask DMA channel
  127. outportb (0x0C, 0); //Clear byte pointer
  128. outportb (0x0B, 0x14 | DMA); //Set mode
  129. /*
  130. The mode consists of the following:
  131. 0x14 + x = binary 00 01 01 xx
  132. || || || ||
  133. || || || ++- DMA channel
  134. || || |+--- Write operation (the DSP writes to memory)
  135. || || +---- Read operation (the DSP reads from memory)
  136. || |+------ Autoinit
  137. || +------- Increment - 1/Decrement - 0
  138. |+--------- Single mode - 1 -+--- 00 - on Demand
  139. +---------- Block mode - 1 -+
  140. */
  141. outportb (DMA << 1, OffSet & 0xFF ); //Write the offset to the DMA controller
  142. outportb (DMA << 1, OffSet >> 8 );
  143. if (DMA == 0) outportb ( 0x87, Page );
  144. if (DMA == 1) outportb ( 0x83, Page );
  145. if (DMA == 3) outportb ( 0x82, Page );
  146. outportb (( DMA << 1 ) + 1, ( Size - 1 ) & 0xFF );
  147. outportb (( DMA << 1 ) + 1, ( ( Size - 1 ) >> 8 ) & 0xFF );
  148. outportb (0x0A, DMA); //Unmask DMA channel
  149. #endif
  150. #endif // BOARD_SIMULATION
  151. };
  152. #ifdef __DJGPP__
  153. bool Assign_DMA_Buffer ( unsigned int Size ){
  154. #else
  155. void Assign_DMA_Buffer ( unsigned int Size ){
  156. #endif
  157. #ifndef BOARD_SIMULATION
  158. #ifdef __DJGPP__
  159. if( DMA_BUFFER ) return false;
  160. DMA_BUFFER = ( uint8_t * ) malloc( Size );
  161. _dma_dos_buffer_iterator = 0;
  162. if( DMA_BUFFER ) {
  163. memset( DMA_BUFFER, 0, Size );
  164. int paragraphs = ( ( DMA_TRANSFER_CHUNK + 15 ) >> 4 );
  165. int rv = __dpmi_allocate_dos_memory( paragraphs, &_dma_dos_selector[0] );
  166. if( rv == -1 ){
  167. _dma_dos_selector[0] = -1;
  168. _dma_dos_linear_address[0] = 0;
  169. free( DMA_BUFFER );
  170. DMA_BUFFER = NULL;
  171. } else {
  172. _dma_dos_linear_address[0] = rv << 4;
  173. #ifdef DEBUG
  174. printf( "Buffer[0] DOS Linear Address: 0x%08X\n", _dma_dos_linear_address[0] );
  175. #endif
  176. };
  177. } else {
  178. _dma_dos_selector[0] = -1;
  179. _dma_dos_linear_address[0] = 0;
  180. };
  181. if( DMA_BUFFER ) {
  182. int paragraphs = ( ( DMA_TRANSFER_CHUNK + 15 ) >> 4 );
  183. int rv = __dpmi_allocate_dos_memory( paragraphs, &_dma_dos_selector[1] );
  184. if( rv == -1 ){
  185. _dma_dos_selector[1] = -1;
  186. _dma_dos_linear_address[1] = 0;
  187. // Clear First DOS Buffer
  188. if( __dpmi_free_dos_memory( _dma_dos_selector[0] ) == 0 ){
  189. _dma_dos_selector[0] = -1;
  190. _dma_dos_linear_address[0] = 0;
  191. };
  192. free( DMA_BUFFER );
  193. DMA_BUFFER = NULL;
  194. } else {
  195. _dma_dos_linear_address[1] = rv << 4;
  196. #ifdef DEBUG
  197. printf( "Buffer[1] DOS Linear Address: 0x%08X\n", _dma_dos_linear_address[1] );
  198. #endif
  199. return true;
  200. };
  201. } else {
  202. _dma_dos_selector[1] = -1;
  203. _dma_dos_linear_address[1] = 0;
  204. };
  205. return false;
  206. #else // !__DJGPP__
  207. unsigned char far *TempBuf=NULL; //Temporary pointer
  208. long LinearAddress;
  209. unsigned int Page1, Page2; //Words
  210. if( DMA_BUFFER == NULL ){
  211. do {
  212. TempBuf = ( unsigned char far * ) farmalloc( Size );
  213. if( DMA_BUFFER ) free( DMA_BUFFER );
  214. LinearAddress = FP_SEG (TempBuf);
  215. LinearAddress = (LinearAddress << 4) + FP_OFF ( TempBuf );
  216. Page1 = LinearAddress >> 16;
  217. Page2 = ( LinearAddress + Size ) >> 16;
  218. DMA_BUFFER = TempBuf;
  219. } while ( Page1 != Page2 );
  220. }; // DMA_BUFFER == 0
  221. memset ( DMA_BUFFER, 0, Size );
  222. #endif // !__DJGPP__
  223. #endif // BOARD_SIMULATION
  224. };
  225. #endif // DMA_TRANSFERS
  226. CBoard *CBoard::_instance = NULL;
  227. CBoard *CBoard::getInstance(){
  228. if( _instance == NULL ) {
  229. _instance = new CBoard();
  230. };
  231. return _instance;
  232. };
  233. CBoard::CBoard(): CIsr(){
  234. m_bWM = WM_NONE;
  235. _dma_dos_buffer_iterator = 0;
  236. m_bWorkEnable = false;
  237. m_bDMA_first_chunk_transfered = false;
  238. m_bDMA_buffer_assigned = false;
  239. m_bDMA_is_crashed = false;
  240. m_bResetIssued = false;
  241. m_bIRQ_handler_installed = false;
  242. m_eBoardType = BT_UNKNOWN;
  243. //m_pFirmwareFileID = NULL;
  244. m_pFirmwareSentLine = 0;
  245. m_bFirmwareUpgradeDone = false;
  246. __dpmi_version_ret dpmi_version;
  247. __dpmi_get_version( &dpmi_version );
  248. #ifdef DEBUG
  249. printf( "DPMI Major: 0x%02X Minor: 0x%02X\n\tCPU:\t[%d]\n\tHOST:\t[%d bits]\n\tMODE:\t[%s]\n\tVirtual Memory:\t[%s]\n",
  250. dpmi_version.major,
  251. dpmi_version.minor,
  252. dpmi_version.cpu,
  253. (( dpmi_version.flags & 0x01 )?32:16),
  254. (( dpmi_version.flags & 0x02 )?_T("REAL"):_T("V86")),
  255. (( dpmi_version.flags & 0x04 )?_T("SUPPORTED"):_T("NOT SUPPORTED"))
  256. );
  257. #endif
  258. if( ( dpmi_version.flags & 0x02 ) == 0 ) m_bWM = WM_DPMI_OK;
  259. //if ( __djgpp_nearptr_enable() == 0 ){
  260. // printf("DPMI near pointers are not available\n");
  261. //exit(-1);
  262. //};
  263. if( m_eBoardType == BT_UNKNOWN ) {
  264. /* char *pCoreModule4DXeStr = _T( "CM/4DXi" );
  265. if( !_fmemcmp( (void far*)0xF0000101UL, pCoreModule4DXeStr, 7 ) &&
  266. !_fmemcmp( (void far*)0xF0002B6DUL, pCoreModule4DXeStr, 7 ) )
  267. m_eBoardType = BT_COREMODULE_4DXE;
  268. */
  269. };
  270. if( m_eBoardType == BT_UNKNOWN ) {
  271. uint32_t CHID;
  272. //unsigned int CC;
  273. CHID = read_nb( 0x90 );
  274. if( CHID == 0x31504d44L ){
  275. #ifdef DEBUG
  276. printf("Board is Vortex SX\n");
  277. #endif
  278. m_eBoardType = BT_VORTEX_SX;
  279. //make_ws8();
  280. };
  281. if( CHID == 0x32504d44L ){
  282. #ifdef DEBUG
  283. printf("Board is Vortex DX\n");
  284. #endif
  285. m_eBoardType = BT_VORTEX_DX;
  286. //make_ws8();
  287. };
  288. if( CHID == 0x33504d44L ){
  289. #ifdef DEBUG
  290. printf("Board is Vortex MX\n");
  291. #endif
  292. m_eBoardType = BT_VORTEX_MX;
  293. //make_ws8();
  294. };
  295. };
  296. if( m_eBoardType == BT_UNKNOWN ) {
  297. /*
  298. asm
  299. {
  300. mov AL,0x02 // Index
  301. mov DX,ZFLINDEX // Index Address
  302. out DX,AL // Set Index
  303. mov DX,ZFLDATA16 // read value:
  304. in AX,DX // AX=1234H
  305. }
  306. */
  307. if( ZFLReadw( 0x02 ) == 0x1234 ) m_eBoardType = BT_TRI_M;
  308. };
  309. mOld_TV = clock();
  310. m_wTimer = 0;
  311. mwBoardID=0;
  312. mwSoftwareID=0;
  313. //g_bIsVortex = ( m_eBoardType == BT_VORTEX_SX || m_eBoardType == BT_VORTEX_DX || m_eBoardType == BT_VORTEX_MX );
  314. //g_bIsDMABoard = ( m_eBoardType == BT_VORTEX_SX || m_eBoardType == BT_VORTEX_DX || m_eBoardType == BT_VORTEX_MX ); //false;
  315. #ifdef DEFAULT_DISC_C
  316. nHID = 2;
  317. #else
  318. if( m_eBoardType == BT_VORTEX_SX || m_eBoardType == BT_VORTEX_DX || m_eBoardType == BT_VORTEX_MX ){
  319. nHID = 2;
  320. } else {
  321. // Old Boards Run From A: -> Virtual Disk is C:
  322. nHID = 0;
  323. };
  324. #endif
  325. BSNS = NULL;
  326. BFCD = NULL;
  327. BSCD = NULL;
  328. bl_day = 0;
  329. bl_month = 0;
  330. bl_year = 0;
  331. fw_day = 0;
  332. fw_month = 0;
  333. fw_year = 0;
  334. #ifndef BOARD_DEFFERED_PROCESSING
  335. m_bReady = false;
  336. memset( &m_BoardData, 0, sizeof(BOARDDATA) );
  337. #endif
  338. m_bOutputDirty = false;
  339. m_Settings.M_LRFDisable = true;
  340. m_Settings.S_LRFDisable = true;
  341. #ifdef DEBUG_ENABLE_OLD_BOARD
  342. // used for A/D conversion
  343. s_pDataArray[0] = s_pDataArray[1] = 0;
  344. #endif
  345. // reset mask
  346. m_bOutputMask = 0;
  347. // Moved from Destructor since CBoard now is Singletone
  348. g_bFlashDriveID = nHID;
  349. }
  350. CBoard::~CBoard(){
  351. if( BSNS != NULL ){
  352. free( BSNS );
  353. };
  354. if( BFCD != NULL ){
  355. free( BFCD );
  356. };
  357. if( BSCD != NULL ){
  358. free( BSCD );
  359. };
  360. Destroy();
  361. }
  362. void CBoard::freeHexImage( FIRMWARE *ptr ){
  363. if( ptr ) {
  364. if( ptr->pLines ) {
  365. free( ptr->pLines );
  366. };
  367. free( ptr );
  368. };
  369. };
  370. FIRMWARE *CBoard::LoadHexImage( char *FileName ){
  371. FIRMWARE *rV = (FIRMWARE *) malloc( sizeof(FIRMWARE));
  372. if( rV ) {
  373. memset( rV, 0, sizeof(FIRMWARE) );
  374. FILE *fileID = fopen( FileName, "r" );
  375. if( fileID ) {
  376. char Sig[2];
  377. uint8_t InpB;
  378. bool inLine = false;
  379. uint8_t Stage = 0;
  380. uint8_t RecLen = 0;
  381. uint8_t CRec = 0;
  382. uint16_t OffSet = 0;
  383. uint8_t RecType = 0;
  384. uint8_t RCheck = 0;
  385. uint8_t CCheck = 0;
  386. char InpC[3];
  387. char InpW[5];
  388. while( !feof( fileID ) ){
  389. if( !inLine ){
  390. fread( Sig, 1, 1, fileID );
  391. if( Sig[0] == ':' ) {
  392. inLine = true;
  393. rV->nLines++;
  394. HEXLINE *tPtr = (HEXLINE *) realloc( rV->pLines, rV->nLines * sizeof( HEXLINE ));
  395. if( tPtr == NULL ) {
  396. freeHexImage( rV );
  397. fclose( fileID );
  398. return NULL;
  399. };
  400. rV->pLines = tPtr;
  401. memset( &rV->pLines[rV->nLines-1], 0, sizeof(HEXLINE));
  402. };
  403. CCheck = 0;
  404. RecLen = 0;
  405. RecType = 0;
  406. RCheck = 0;
  407. Stage = 0;
  408. CRec = 0;
  409. } else {
  410. switch( Stage ){
  411. case 0:{
  412. fread( InpC, 2, 1, fileID );
  413. InpC[2]='\0';
  414. if( sscanf( InpC, "%x", &RecLen ) == 0 ){
  415. freeHexImage( rV );
  416. fclose( fileID );
  417. return NULL;
  418. };
  419. rV->pLines[rV->nLines-1].len = RecLen;
  420. CCheck = RecLen;
  421. Stage = 1;
  422. break;
  423. };
  424. case 1:{
  425. fread( InpW, 4, 1, fileID );
  426. InpW[4]='\0';
  427. if( sscanf( InpW, "%x", &OffSet )== 0 ) {
  428. freeHexImage( rV );
  429. fclose( fileID );
  430. return NULL;
  431. };
  432. rV->pLines[rV->nLines-1].offset_lb = OffSet & 0xFF;
  433. rV->pLines[rV->nLines-1].offset_hb = (OffSet >> 8 ) & 0xFF;
  434. CCheck = ( CCheck + rV->pLines[rV->nLines-1].offset_lb ) & 0xFF ;
  435. CCheck = ( CCheck + rV->pLines[rV->nLines-1].offset_hb ) & 0xFF ;
  436. CRec = 0;
  437. Stage = 2;
  438. break;
  439. };
  440. case 2:{
  441. fread( InpC, 2, 1, fileID );
  442. InpC[2]='\0';
  443. if( sscanf( InpC, "%x", &RecType ) == 0 ){
  444. freeHexImage( rV );
  445. fclose( fileID );
  446. return NULL;
  447. };
  448. rV->pLines[rV->nLines-1].type = RecType;
  449. CCheck = ( CCheck + RecType ) & 0xFF ;
  450. Stage = 3;
  451. break;
  452. };
  453. default:{
  454. fread( InpC, 2, 1, fileID );
  455. InpC[2]='\0';
  456. if( sscanf( InpC, "%x", &InpB ) == 0 ){
  457. freeHexImage( rV );
  458. fclose( fileID );
  459. return NULL;
  460. };
  461. CCheck = ( CCheck + InpB ) & 0xFF ;
  462. rV->pLines[rV->nLines-1].data[CRec] = InpB;
  463. if( CRec == rV->pLines[rV->nLines-1].len ){
  464. if( CCheck != 0 ){
  465. freeHexImage( rV );
  466. fclose( fileID );
  467. return NULL;
  468. };
  469. inLine = false;
  470. } else CRec++;
  471. break;
  472. };
  473. };
  474. };
  475. };
  476. fclose( fileID );
  477. } else {
  478. freeHexImage( rV );
  479. rV = NULL;
  480. };
  481. };
  482. return rV;
  483. };
  484. uint8_t wait_board( uint8_t ack = 0x00, uint16_t loop = 100 );
  485. void CBoard::BeginService(){
  486. #ifdef DEBUG
  487. printf("Board Service Begins!\n");
  488. #endif
  489. switch( m_bWM ) {
  490. case WM_FW_UPGRADE:{
  491. if( m_pFirmwareImage ) {
  492. int nono = 2500;
  493. unsigned char TestBYTE = inportb( BLO_READ_ID_PORT );
  494. while ( nono && ( TestBYTE != 0xAA ) ){
  495. nono--;
  496. delay(1);
  497. outportb( NEW_BL_ADDRESS, 0xAA ); // Start BootLoader Proc
  498. delay(1);
  499. TestBYTE = inportb( BLO_READ_ID_PORT );
  500. };
  501. if( TestBYTE == 0xAA ) {
  502. if(!m_bISR ) CIsr::initISR( NEW_INTERRUPT_BOARD );
  503. #ifdef DEBUG
  504. printf("Start FW Service!\n");
  505. #endif
  506. m_pFirmwareSentLine = 0;
  507. m_bFirmwareUpgradeDone = false;
  508. outportb( BLO_WRITE_START_TRANSFER, m_pFirmwareImage->pLines[m_pFirmwareSentLine].len );
  509. outportb( BLO_WRITE_DATA_PORT, m_pFirmwareImage->pLines[m_pFirmwareSentLine].type );
  510. outportb( BLO_WRITE_DATA_PORT, m_pFirmwareImage->pLines[m_pFirmwareSentLine].offset_hb );
  511. outportb( BLO_WRITE_DATA_PORT, m_pFirmwareImage->pLines[m_pFirmwareSentLine].offset_lb );
  512. for( uint8_t idx = 0; idx < (m_pFirmwareImage->pLines[m_pFirmwareSentLine].len+1); idx++ ){
  513. outportb( BLO_WRITE_DATA_PORT, m_pFirmwareImage->pLines[m_pFirmwareSentLine].data[idx] );
  514. };
  515. outportb( BLO_WRITE_END_TRANSFER, 0xFF );
  516. m_pFirmwareSentLine++;
  517. } else {
  518. if( m_bISR ) CIsr::resignISR();
  519. #ifdef DEBUG
  520. printf("Failure To Enter Board into Bootloader Mode!\n");
  521. #endif
  522. m_bWM = WM_ONLY_INFO; // Not Entered into Bootloader Mode
  523. };
  524. };
  525. break;
  526. };
  527. case WM_DMA_WORK:{
  528. #if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND ) || defined( LGS_FOD ) || defined( ALS_300_BM )
  529. outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
  530. delay(1);
  531. #else
  532. outportb( NEW_RST_ADDRESS, 5 ); // Status + DMA Upload
  533. delay(1);
  534. #endif
  535. outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // Work Status
  536. delay(1);
  537. outportb( NEW_DTA_ADDRESS, 1 ); // Init DMA
  538. break;
  539. };
  540. };
  541. };
  542. void CBoard::EndService( void ){
  543. if( !m_bWorkEnable ) return;
  544. #ifndef BOARD_SIMULATION
  545. switch( m_eBoardType ){
  546. case BT_VORTEX_SX:
  547. case BT_VORTEX_DX:
  548. case BT_VORTEX_MX:{
  549. // disable interrupts
  550. // disable();
  551. // disable laser devices
  552. // outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1) );
  553. //SetStatus( m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1) );
  554. m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
  555. SetRelayBit( m_Settings.RelayDefaultMode );
  556. switch( m_bWM ){
  557. case WM_DMA_WORK:{
  558. StopDMA();
  559. if( m_bIRQ_handler_installed ) {
  560. resignISR();
  561. m_bDMA_first_chunk_transfered = false;
  562. };
  563. if( !m_bWorkEnable ){
  564. outportb( NEW_RST_ADDRESS, 0 );// Full Upload
  565. delay(1);
  566. BSNLB = inportb( NEW_DTA_ADDRESS );
  567. delay(1);
  568. BSNHB = inportb( NEW_DTA_ADDRESS );
  569. mwBoardID = (( BSNHB<<8) | BSNLB );
  570. if ( ( ~( mwBoardID ^ mwSoftwareID ) & 0xFFFF ) == 0 ) m_bWorkEnable = true;
  571. outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
  572. outportb( NEW_DTA_ADDRESS, mwSoftwareID & 0xFF ); // Serial Number
  573. outportb( NEW_DTA_ADDRESS, ( mwSoftwareID >> 8 ) & 0xFF );
  574. } else {
  575. outportb( NEW_RST_ADDRESS, 2 ); // Partial Upload
  576. };
  577. #if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND ) || defined( LGS_FOD )
  578. #ifdef LGS_PLS_200
  579. outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 * 3 ) & 0xFF );
  580. outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 * 3 ) >> 8 ) & 0xFF );
  581. #else
  582. #ifdef LGS_FOD
  583. outportb( NEW_DTA_ADDRESS, 0xFF );
  584. outportb( NEW_DTA_ADDRESS, 0xFF );
  585. #else
  586. outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 ) & 0xFF );
  587. outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 ) >> 8 ) & 0xFF );
  588. #endif
  589. #endif
  590. #endif
  591. #if defined( LGS_MND )
  592. outportb( NEW_DTA_ADDRESS, m_Settings.nZeroShift & 0xFF );
  593. outportb( NEW_DTA_ADDRESS, m_Settings.nZeroShift >> 8 ); // Zero offset
  594. #else
  595. outportb( NEW_DTA_ADDRESS, 0x00 );
  596. outportb( NEW_DTA_ADDRESS, 0x00 ); // Zero offset
  597. #endif
  598. outportb( NEW_DTA_ADDRESS, m_Settings.nTemperature ); // Temp
  599. outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // Work Status
  600. outportb( NEW_DTA_ADDRESS, 3 ); // Reset DMA
  601. delay(10); // To ensure stop of DMA conveyor
  602. break;
  603. };
  604. case WM_IRQ_WORK:{
  605. // disable interrupts
  606. disable();
  607. // disable laser devices
  608. outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
  609. enable();
  610. delay(100);
  611. if( !m_bWorkEnable ){
  612. BSNLB=inportb( NEW_BASE_ADDRESS + 6 );
  613. BSNHB=inportb( NEW_BASE_ADDRESS + 7 );
  614. mwBoardID = (( BSNHB<<8) | BSNLB );
  615. if ( ( ~( mwBoardID ^ mwSoftwareID ) & 0xFFFF ) == 0 ){
  616. printf("License is OK\n WORK ENABLE!\n");
  617. m_bWorkEnable = true;
  618. } else {
  619. printf("License is NOK\n WORK DISABLE!\n");
  620. }
  621. outportb( NEW_BASE_ADDRESS + 6, mwSoftwareID & 0xFF );
  622. outportb( NEW_BASE_ADDRESS + 7, ( mwSoftwareID >> 8 ) & 0xFF );
  623. } else {
  624. printf("WORK ALLREADY ENABLED!\n");
  625. };
  626. //outportb( NEW_BASE_ADDRESS + 3, 0 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nGearRatio >> 8 ); // motor ratio
  627. //outportb( NEW_BASE_ADDRESS + 3, 1 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nGearRatio & 0xFF );
  628. //outportb( NEW_BASE_ADDRESS + 3, 2 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nEncoderSteps >> 8 ); // encoder count
  629. //outportb( NEW_BASE_ADDRESS + 3, 3 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nEncoderSteps & 0xFF );
  630. outportb( NEW_BASE_ADDRESS + 3, 4 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nTemperature ); // temperature
  631. delay( 10 );
  632. outportb( NEW_BASE_ADDRESS + 3, 5 ); outportb( NEW_BASE_ADDRESS + 4, 0x00 );//m_Settings.nZeroShift >> 8 ); // zero offset
  633. delay( 10 );
  634. outportb( NEW_BASE_ADDRESS + 3, 6 ); outportb( NEW_BASE_ADDRESS + 4, 0x00 );//m_Settings.nZeroShift & 0xFF );
  635. //outportb( NEW_BASE_ADDRESS + 3, 7 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nMaxRange >> 8 );
  636. //outportb( NEW_BASE_ADDRESS + 3, 8 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nMaxRange & 0xFF ); // max range
  637. m_BoardData.Clear();
  638. break;
  639. };
  640. };
  641. m_nRealAngle = -1;
  642. m_nErrorAngle = -1;
  643. break;
  644. };// case BT_NEW
  645. };// end of SWITCH board type
  646. #endif // BOARD_SIBULATION
  647. };
  648. void CBoard::StopDMA(){
  649. m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
  650. SetRelayBit( m_Settings.RelayDefaultMode );
  651. m_bResetIssued = true;
  652. int nono = 100; // 300 mSecond
  653. while ( nono && m_bResetIssued ){
  654. nono--;
  655. delay(3);
  656. };
  657. // NO DMA TRANSFERS ONGOING -> STOP Forced
  658. if( m_bResetIssued ) { // No DMA stop during IRQ! so stop it anyway
  659. m_bResetIssued = false;
  660. #if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND ) || defined( LGS_FOD ) || defined( ALS_300_BM )
  661. outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
  662. #else
  663. outportb( NEW_RST_ADDRESS, 5 ); // Status + DMA Upload
  664. #endif
  665. delay(1);
  666. outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // work status
  667. delay(1);
  668. outportb( NEW_DTA_ADDRESS, 3 ); // Stop DMA
  669. delay(3);
  670. };
  671. };
  672. uint8_t wait_board( uint8_t ack, uint16_t loop ){
  673. uint8_t tByte = inportb( NEW_ID_ADDRESS );
  674. if( ack ) {
  675. while( loop && tByte!= ack ){
  676. delay(1);
  677. tByte = inportb( NEW_ID_ADDRESS );
  678. loop--;
  679. };
  680. } else {
  681. while( loop && tByte!= 0x5A && tByte!= 0xA5 && tByte != 0xAA ){
  682. delay(1);
  683. tByte = inportb( NEW_ID_ADDRESS );
  684. loop--;
  685. };
  686. };
  687. return ( loop > 0 )?tByte:0x00;
  688. };
  689. bool CBoard::InitBoard(){
  690. m_bDMA_first_chunk_transfered = false;
  691. if( m_bWM == WM_SIMULATION ) return true;
  692. #ifdef RELAY_ACTIVATE_BY_ZERO
  693. m_bOutputMask = BE_NEW_DC0;
  694. #else
  695. m_bOutputMask = 0;
  696. #endif
  697. // disable laser devices
  698. //outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1) );
  699. #ifdef BOARD_LAZY_INIT
  700. delay(2000); // Wait 2 seconds
  701. #endif
  702. /*
  703. * Cecking Before All if Firmware must be upgraded
  704. */
  705. ffblk buffer[1];
  706. if( findfirst(_T("firmware"), buffer, 0 )==0 ){
  707. #ifdef DEBUG
  708. //printf("Found Board Firmware Upgrade!\n");
  709. #endif
  710. //if( validateHexImage( _T("firmware"), BSNHB, BSNLB ) ){
  711. m_pFirmwareImage = LoadHexImage( _T( "firmware" ) );
  712. if( m_pFirmwareImage ){
  713. #ifdef DEBUG
  714. //printf("Board Firmware Upgrade is Valid!\n");
  715. #endif
  716. m_bFirmwareUpgradeDone = false;
  717. m_pFirmwareSentLine = 0;
  718. #ifdef DEBUG
  719. //printf("Stoping WD\n");
  720. #endif
  721. //Disable Watchdog
  722. unsigned char valb;
  723. outp( 0x22, 0x13 ); // lock register
  724. outp( 0x23, 0xc5 ); // unlock config
  725. outp( 0x22, 0x37 );
  726. valb = inp( 0x23 );
  727. outp( 0x22, 0x37 );
  728. outp( 0x23, valb & 0xBF );
  729. outp( 0x22, 0x13 ); //Lock reg
  730. outp( 0x22, 0x00 ); // Lock config
  731. if( m_bWM != WM_BLO_ONLY ) {
  732. // If Board in BLO mode no need to start BootLoader manually
  733. outp( NEW_BL_ADDRESS, 0xAA ); // Start BootLoader Proc
  734. delay( 1 );
  735. outp( NEW_BL_ADDRESS, 0x55 ); // Start BootLoader Proc
  736. delay( 1 );
  737. outp( NEW_BL_ADDRESS, 0xAA ); // Start BootLoader Proc
  738. delay( 1 );
  739. outp( NEW_BL_ADDRESS, 0x55 ); // Start BootLoader Proc
  740. delay( 1500 ); // Wait till BL checksumm complete
  741. };
  742. #ifdef DEBUG
  743. //printf("Board Mode NOW is - Firmware Upgrade!\n");
  744. #endif
  745. m_bWM = WM_FW_UPGRADE;
  746. }// else {
  747. #ifdef DEBUG
  748. //printf("HEX File is Invalid!\n");
  749. #endif
  750. // Delete Firmware File in any case
  751. // if the FW is ok it's already loadded to memory
  752. unlink(_T("firmware"));
  753. //};
  754. };
  755. /*
  756. * After BootLoader - Firmware checking do regular INIT
  757. */
  758. m_bOutputMask |= (BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
  759. SetRelayBit( m_Settings.RelayDefaultMode );
  760. BSNLB=0;
  761. BSNHB=0;
  762. switch( m_bWM ) {
  763. case WM_FW_UPGRADE:{
  764. /*
  765. if( wait_board() ) {
  766. delay(1);
  767. BSNLB = inportb( NEW_ID_ADDRESS );
  768. delay(1);
  769. BSNLB = inportb( NEW_DTA_ADDRESS );
  770. delay(1);
  771. BSNHB = inportb( NEW_DTA_ADDRESS );
  772. delay(1);
  773. fw_year = inportb( NEW_DTA_ADDRESS );
  774. delay(1);
  775. fw_month = inportb( NEW_DTA_ADDRESS );
  776. delay(1);
  777. fw_day = inportb( NEW_DTA_ADDRESS );
  778. delay(1);
  779. bl_year = inportb( NEW_DTA_ADDRESS );
  780. delay(1);
  781. bl_month = inportb( NEW_DTA_ADDRESS );
  782. delay(1);
  783. bl_day = inportb( NEW_DTA_ADDRESS );
  784. mwBoardID = (( BSNHB<<8) | BSNLB );
  785. if ( ( ~( mwBoardID ^ mwSoftwareID ) & 0xFFFF ) == 0 ){
  786. #ifdef DEBUG
  787. printf("The License is OK\n");
  788. #endif
  789. m_bWorkEnable = true;
  790. };
  791. CIsr::initISR( NEW_INTERRUPT_BOARD );
  792. break;
  793. } else m_bWM = WM_ONLY_INFO;*/
  794. m_bIRQ_handler_installed = CIsr::initISR( NEW_INTERRUPT_BOARD );
  795. m_bWorkEnable = true;
  796. break;
  797. };
  798. case WM_BLO_ONLY:{
  799. m_bWorkEnable = true;
  800. break;
  801. }
  802. case WM_ONLY_INFO:{
  803. if( wait_board() ) {
  804. delay(1);
  805. BSNLB = inportb( NEW_DTA_ADDRESS );
  806. delay(1);
  807. BSNHB = inportb( NEW_DTA_ADDRESS );
  808. delay(1);
  809. fw_year = inportb( NEW_DTA_ADDRESS );
  810. delay(1);
  811. fw_month = inportb( NEW_DTA_ADDRESS );
  812. delay(1);
  813. fw_day = inportb( NEW_DTA_ADDRESS );
  814. delay(1);
  815. bl_year = inportb( NEW_DTA_ADDRESS );
  816. delay(1);
  817. bl_month = inportb( NEW_DTA_ADDRESS );
  818. delay(1);
  819. bl_day = inportb( NEW_DTA_ADDRESS );
  820. mwBoardID = (( BSNHB<<8) | BSNLB );
  821. //if ( ( ~( mwBoardID ^ mwSoftwareID ) & 0xFFFF ) == 0 ) m_bWorkEnable = true;
  822. };
  823. m_bWorkEnable = true;
  824. break;
  825. };
  826. case WM_DMA_WORK:{
  827. if( wait_board() ) {
  828. outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
  829. outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
  830. outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
  831. delay(1);
  832. outportb( NEW_DTA_ADDRESS, m_bOutputMask );
  833. delay(1);
  834. outportb( NEW_DTA_ADDRESS, 3 ); // Stop DMA
  835. // nessesary to make delay
  836. if( !m_bWorkEnable ){
  837. BSNLB = inportb( NEW_RST_ADDRESS );
  838. delay(1);
  839. BSNLB = inportb( NEW_DTA_ADDRESS );
  840. delay(1);
  841. BSNHB = inportb( NEW_DTA_ADDRESS );
  842. delay(1);
  843. fw_year = inportb( NEW_DTA_ADDRESS );
  844. delay(1);
  845. fw_month = inportb( NEW_DTA_ADDRESS );
  846. delay(1);
  847. fw_day = inportb( NEW_DTA_ADDRESS );
  848. delay(1);
  849. bl_year = inportb( NEW_DTA_ADDRESS );
  850. delay(1);
  851. bl_month = inportb( NEW_DTA_ADDRESS );
  852. delay(1);
  853. bl_day = inportb( NEW_DTA_ADDRESS );
  854. mwBoardID = (( BSNHB<<8) | BSNLB );
  855. if ( ( ~( mwBoardID ^ mwSoftwareID ) & 0xFFFF ) == 0 ) {
  856. m_bWorkEnable = true;
  857. } else {
  858. m_bWM = WM_ONLY_INFO;
  859. };
  860. };
  861. if( m_bWorkEnable ) {
  862. delay(1);
  863. outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
  864. outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
  865. outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
  866. delay(1);
  867. outportb( NEW_DTA_ADDRESS, mwSoftwareID & 0xFF ); // Serial Number
  868. delay(1);
  869. outportb( NEW_DTA_ADDRESS, ( mwSoftwareID >> 8 ) & 0xFF );
  870. #ifdef LGS_PLS_200
  871. delay(1);
  872. outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 * 3 ) & 0xFF );
  873. delay(1);
  874. outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 * 3 ) >> 8 ) & 0xFF );
  875. #else
  876. #ifdef LGS_FOD
  877. delay(1);
  878. outportb( NEW_DTA_ADDRESS, 0xFF );
  879. delay(1);
  880. outportb( NEW_DTA_ADDRESS, 0xFF );
  881. #else
  882. #ifdef ALS_300_BM
  883. delay(1);
  884. outportb( NEW_DTA_ADDRESS, 0xFF );
  885. delay(1);
  886. outportb( NEW_DTA_ADDRESS, 0xFF );
  887. #else
  888. delay(1);
  889. outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 ) & 0xFF );
  890. delay(1);
  891. outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 ) >> 8 ) & 0xFF );
  892. #endif
  893. #endif
  894. #endif
  895. #if defined( LGS_MND )
  896. delay(1);
  897. outportb( NEW_DTA_ADDRESS, m_Settings.nZeroShift & 0xFF );
  898. delay(1);
  899. outportb( NEW_DTA_ADDRESS, ( m_Settings.nZeroShift >> 8 ) & 0xFF ); // Zero offset
  900. #else
  901. delay(1);
  902. outportb( NEW_DTA_ADDRESS, 0x00 );//m_Settings.nZeroShift & 0xFF );
  903. delay(1);
  904. outportb( NEW_DTA_ADDRESS, 0x00 );//m_Settings.nZeroShift >> 8 ); // Zero offset
  905. #endif
  906. delay(1);
  907. outportb( NEW_DTA_ADDRESS, m_Settings.nTemperature ); // Temp
  908. delay(1);
  909. outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // Work Status
  910. delay(1);
  911. outportb( NEW_DTA_ADDRESS, 3 ); // Reset DMA
  912. delay(1);
  913. if( !Assign_DMA_Buffer( DMA_TRANSFER_CHUNK * DMA_TRANSFER_ZONES )) return false;
  914. m_nbWriteChunk = 0;
  915. m_nbReadChunk = 0;
  916. m_nwChunkInterator = 0;
  917. _dma_dos_buffer_iterator = 0;
  918. if( !InitDMA( DMA_CHANNEL ) ) return false;
  919. m_bResetIssued = false;
  920. m_bIRQ_handler_installed = CIsr::initISR( NEW_INTERRUPT_BOARD );
  921. };
  922. if( m_bWorkEnable ) {
  923. m_bDMA_is_crashed = false;
  924. m_bOutputMask |= (m_Settings.M_LRFDisable?0:BE_NEW_DEVICE0) | (m_Settings.S_LRFDisable?0:BE_NEW_DEVICE1);
  925. SetRelayBit( m_Settings.RelayDefaultMode );
  926. delay(1);
  927. outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
  928. outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
  929. outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
  930. delay(1);
  931. outportb( NEW_DTA_ADDRESS, m_bOutputMask );
  932. delay(1);
  933. outportb( NEW_DTA_ADDRESS, 3 ); // Reset DMA -> Don't Work After Init !!
  934. };
  935. };
  936. break;
  937. };
  938. case WM_IRQ_WORK:{
  939. // disable interrupts
  940. disable();
  941. outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
  942. // nessesary to make delay
  943. delay(100);
  944. BSNLB=inportb( NEW_BASE_ADDRESS + 6 );
  945. BSNHB=inportb( NEW_BASE_ADDRESS + 7 );
  946. mwBoardID = (( BSNHB<<8) | BSNLB );
  947. if( (~( mwBoardID ^ mwSoftwareID ) & 0xFFFF )== 0 ) m_bWorkEnable = true;
  948. if( m_bWorkEnable ) {
  949. outportb( NEW_BASE_ADDRESS + 6, ( unsigned char )( mwSoftwareID & 0xFF )); // Sending SoftwareID to the Board
  950. outportb( NEW_BASE_ADDRESS + 7, ( unsigned char )(( mwSoftwareID >> 8 ) & 0xFF));
  951. //outportb( NEW_BASE_ADDRESS + 3, 0 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nGearRatio >> 8 ); // motor ratio
  952. //outportb( NEW_BASE_ADDRESS + 3, 1 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nGearRatio & 0xFF );
  953. //outportb( NEW_BASE_ADDRESS + 3, 2 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nEncoderSteps >> 8 ); // encoder count
  954. //outportb( NEW_BASE_ADDRESS + 3, 3 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nEncoderSteps & 0xFF );
  955. outportb( NEW_BASE_ADDRESS + 3, 4 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nTemperature ); // temperature
  956. delay( 10 );
  957. outportb( NEW_BASE_ADDRESS + 3, 5 ); outportb( NEW_BASE_ADDRESS + 4, 0x00 );//m_Settings.nZeroShift >> 8 ); // zero offset
  958. delay( 10 );
  959. outportb( NEW_BASE_ADDRESS + 3, 6 ); outportb( NEW_BASE_ADDRESS + 4, 0x00 );//m_Settings.nZeroShift & 0xFF );
  960. // outportb( NEW_BASE_ADDRESS + 3, 7 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nMaxRange >> 8 );
  961. // outportb( NEW_BASE_ADDRESS + 3, 8 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nMaxRange & 0xFF ); // max range
  962. m_bWorkEnable = CIsr::initISR( NEW_INTERRUPT_BOARD );
  963. // enable devices here ?? or in Reset Board?
  964. delay( 10 );
  965. m_bOutputMask |= (m_Settings.M_LRFDisable?0:BE_NEW_DEVICE0) | (m_Settings.S_LRFDisable?0:BE_NEW_DEVICE1);
  966. SetRelayBit( m_Settings.RelayDefaultMode );
  967. outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
  968. };
  969. break;
  970. };
  971. };
  972. // no need to check m_bWorkEnable for FW upgrade?
  973. m_nRealAngle = -1;
  974. m_nErrorAngle = -1;
  975. #ifdef DEBUG
  976. printf( "Board [0x%04X] Init Done\n", mwBoardID );
  977. #endif
  978. if( m_bWM == WM_DMA_WORK || m_bWM == WM_IRQ_WORK ) {
  979. return m_bWorkEnable;
  980. };
  981. return true;
  982. };
  983. bool CBoard::Create( BOARD_SETTINGS* pSettings ){
  984. // Initial Create Always with Settings!!
  985. if( pSettings != NULL ) memcpy( &m_Settings, pSettings, sizeof( BOARD_SETTINGS ));
  986. #ifdef BOARD_SIMULATION
  987. m_bWM = WM_SIMULATION;
  988. #else // !BOARD_SIMULATION
  989. //Destroy();
  990. #ifndef NO_WDT2_INIT
  991. //if( g_bIsDMABoard ) init_sys_clock();
  992. #endif
  993. if( !m_bWorkEnable ) {
  994. if ( m_Settings.nHID < 255 ){
  995. nHID = m_Settings.nHID;
  996. g_bFlashDriveID = nHID;
  997. };
  998. if( !UpdateLicensing() ) SetupLicense();
  999. };
  1000. SetRelayBit( m_Settings.RelayDefaultMode );
  1001. // Init Global VARS and Board Type!
  1002. #ifdef COMPILE_AS_GETFILE
  1003. #ifdef LGS_STEPPER
  1004. delay( 15000 );// Wait 15 seconds anyway to spinup motors.
  1005. #endif
  1006. #endif
  1007. #ifdef BOARD_LAZY_INIT
  1008. uint8_t TestBYTE = wait_board( 0x00, 150 );
  1009. #else
  1010. uint8_t TestBYTE = wait_board( 0x00, 150 );
  1011. #endif
  1012. if( TestBYTE ){
  1013. #ifdef DEBUG
  1014. //printf("Board Test Byte is: 0x%02X\n", TestBYTE );
  1015. #endif
  1016. if( TestBYTE == 0x5A ) m_bWM = WM_IRQ_WORK;
  1017. if( TestBYTE == 0xA5 ) m_bWM = WM_DMA_WORK;
  1018. if( TestBYTE == 0xAA ) m_bWM = WM_BLO_ONLY;
  1019. // First INIT
  1020. if( !m_bWorkEnable ){
  1021. if( m_bWM != WM_IRQ_WORK ) {
  1022. if( m_bWM == WM_DMA_WORK ) StopDMA();
  1023. outportb( NEW_RST_ADDRESS, 0 );// Full Upload
  1024. delay(1);
  1025. BSNLB = inportb( NEW_DTA_ADDRESS );
  1026. delay(1);
  1027. BSNHB = inportb( NEW_DTA_ADDRESS );
  1028. delay(1);
  1029. fw_year = inportb( NEW_DTA_ADDRESS );
  1030. delay(1);
  1031. fw_month = inportb( NEW_DTA_ADDRESS );
  1032. delay(1);
  1033. fw_day = inportb( NEW_DTA_ADDRESS );
  1034. delay(1);
  1035. bl_year = inportb( NEW_DTA_ADDRESS );
  1036. delay(1);
  1037. bl_month = inportb( NEW_DTA_ADDRESS );
  1038. delay(1);
  1039. bl_day = inportb( NEW_DTA_ADDRESS );
  1040. delay(1);
  1041. //Send SW ID in any case
  1042. // No need to send to BootLoader
  1043. if( m_bWM == WM_DMA_WORK ) {
  1044. outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
  1045. delay(1);
  1046. outportb( NEW_DTA_ADDRESS, mwSoftwareID & 0xFF );
  1047. delay(1);
  1048. outportb( NEW_DTA_ADDRESS, ( mwSoftwareID >> 8 ) & 0xFF );
  1049. delay(1);
  1050. };
  1051. } else
  1052. //if( m_bWM == WM_IRQ_WORK )
  1053. {
  1054. outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
  1055. delay(100);
  1056. BSNLB=inportb( NEW_BASE_ADDRESS + 6 );
  1057. BSNHB=inportb( NEW_BASE_ADDRESS + 7 );
  1058. //Send SW ID in any case
  1059. outportb( NEW_BASE_ADDRESS + 6, mwSoftwareID & 0xFF );
  1060. outportb( NEW_BASE_ADDRESS + 7, ( mwSoftwareID >> 8 ) & 0xFF );
  1061. };
  1062. mwBoardID = (( BSNHB<<8) | BSNLB );
  1063. #ifdef DEBUG
  1064. //printf("Board ID: %04X\n", mwBoardID );
  1065. #endif
  1066. if ( ( ~( mwBoardID ^ mwSoftwareID ) & 0xFFFF ) == 0 ) m_bWorkEnable = true;
  1067. };
  1068. } else {
  1069. // No Board Communication!
  1070. #ifdef DEBUG
  1071. //printf( "Board Link Failure!\n" );
  1072. #endif
  1073. #ifdef COMPILE_AS_GETFILE
  1074. // Get File Mode -> Continue even No Board
  1075. m_bWM = WM_ONLY_INFO;
  1076. #else // ! COMPILE_AS_GETFILE
  1077. #ifdef RESTART_PC_BY_WATCHDOG
  1078. //No Board Init -> Try Restart WDT
  1079. outportb( NEW_WDT_ADDRESS, 0xAA ); // Try To restart Board
  1080. #ifdef WATCHDOG_ENABLED
  1081. CWatchDog m_WatchDog;
  1082. m_WatchDog.Create();
  1083. m_WatchDog.Update( 0xFF );
  1084. while(1); // Hangup PC -> Full Restart
  1085. #endif
  1086. delay( 3000 ); // Wait 3 Seconds anyway -> then restart App
  1087. #else
  1088. return false;
  1089. #endif
  1090. #endif // ! COMPILE_AS_GETFILE
  1091. };
  1092. /*
  1093. #ifdef COMPILE_AS_GETFILE
  1094. m_bWM = WM_ONLY_INFO;
  1095. #else
  1096. // If License is Not OK -> WM is INFO
  1097. if( !m_bWorkEnable ) m_bWM = WM_ONLY_INFO;
  1098. #endif
  1099. */
  1100. #endif // BOARD_SIMULATION
  1101. return InitBoard();
  1102. }
  1103. void CBoard::Destroy(){
  1104. #ifndef BOARD_SIMULATION
  1105. #ifdef DEBUG_PRINTOUT_MSGS
  1106. debugProcBegin("BOARD:Destroy");
  1107. #endif
  1108. SetRelayBit( m_Settings.RelayDefaultMode );
  1109. #ifdef __DJGPP__
  1110. resignISR();
  1111. m_bDMA_first_chunk_transfered = false;
  1112. #else //__DJGPP__
  1113. if( m_pOldIsrProc )
  1114. {
  1115. if( m_nBoardType == BT_NEW )
  1116. {
  1117. if( g_bIsDMABoard ){
  1118. m_bResetIssued = true;
  1119. int nono = 10;
  1120. while ( nono && m_bResetIssued ){
  1121. nono--;
  1122. delay(100);
  1123. };
  1124. m_bResetIssued = false;
  1125. };
  1126. _disable();
  1127. outportb( PICIMR, inportb(PICIMR) | !NEW_INTERRUPT_MASK );
  1128. _dos_setvect( NEW_INTERRUPT_BOARD, m_pOldIsrProc );
  1129. _enable();
  1130. }
  1131. #ifdef DEBUG_ENABLE_OLD_BOARD
  1132. else
  1133. if( m_nBoardType == BT_OLD )
  1134. {
  1135. #ifndef BOARD_BIND_IRQ_TO_SAPHIR
  1136. outportb( PICIMR, inportb(PICIMR) | !OLD_INTERRUPT_MASK );
  1137. m_bOutputMask &= ~0x07;
  1138. SetRelayBit( m_Settings.RelayDefaultMode );
  1139. outportb( OLD_BASE_ADDRESS, m_bOutputMask ); // Disable interrupt
  1140. #endif
  1141. _disable();
  1142. _dos_setvect( OLD_INTERRUPT_BOARD, m_pOldIsrProc );
  1143. _enable();
  1144. }
  1145. #endif
  1146. m_pOldIsrProc = NULL;
  1147. #ifdef DEBUG_PRINTOUT_MSGS
  1148. debug("BOARD:Clear Data");
  1149. #endif
  1150. #ifndef BOARD_DEFFERED_PROCESSING
  1151. m_bReady = false;
  1152. memset( &m_BoardData, 0, sizeof(BOARDDATA) );
  1153. #endif
  1154. }
  1155. #endif //__DJGPP__
  1156. // unknown type of board
  1157. // m_eBoardType = BT_UNKNOWN;
  1158. // used for A/D conversion
  1159. #ifdef DEBUG_ENABLE_OLD_BOARD
  1160. s_pDataArray[0] = s_pDataArray[1] = 0;
  1161. #endif
  1162. #ifdef DEBUG_PRINTOUT_MSGS
  1163. debugProcEnd();
  1164. #endif
  1165. #else // BOARD_SIMULATION
  1166. #ifdef DEBUG_PRINTOUT_MSGS
  1167. debugProcBegin("BOARD:Destroy SIM-Board");
  1168. debugProcEnd();
  1169. #endif
  1170. #endif // BOARD_SIMULATION
  1171. #ifndef NO_WDT2_INIT
  1172. #ifdef DEBUG
  1173. printf("Stop Sysclock\n");
  1174. #endif
  1175. //if( g_bIsVortex ) stop_sys_clock();
  1176. #ifdef DEBUG
  1177. printf("Done\n");
  1178. #endif
  1179. #endif
  1180. };
  1181. void CBoard::AsyncReset(){
  1182. #ifdef DEBUG_PRINTOUT_MSGS
  1183. debug( "AsyncReset" );
  1184. #endif
  1185. #ifndef BOARD_SIMULATION
  1186. switch( m_bWM ){
  1187. case WM_DMA_WORK:{
  1188. // disable interrupts
  1189. // disable();
  1190. // disable laser devices
  1191. // outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1) );
  1192. //SetStatus( m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1) );
  1193. m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
  1194. SetRelayBit( m_Settings.RelayDefaultMode );
  1195. // if MCU board
  1196. #if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND ) || defined( LGS_FOD ) || defined( ALS_300_BM )
  1197. outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
  1198. #else
  1199. outportb( NEW_RST_ADDRESS, 5 ); // Status + DMA Upload
  1200. #endif
  1201. delay(1);
  1202. outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // work status
  1203. delay(1);
  1204. outportb( NEW_DTA_ADDRESS, 3 ); // Stop DMA
  1205. delay(10);
  1206. if( !m_bWorkEnable ){
  1207. outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
  1208. outportb( NEW_DTA_ADDRESS, mwSoftwareID & 0xFF ); // Serial Number
  1209. outportb( NEW_DTA_ADDRESS, ( mwSoftwareID >> 8 ) & 0xFF );
  1210. } else {
  1211. outportb( NEW_RST_ADDRESS, 2 ); //Params + Status + DMA Upload - SerNum
  1212. };
  1213. #if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND ) || defined( LGS_FOD ) || defined( ALS_300_BM )
  1214. #ifdef LGS_PLS_200
  1215. delay(1);
  1216. outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 * 3 ) & 0xFF );
  1217. delay(1);
  1218. outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 * 3 ) >> 8 ) & 0xFF );
  1219. #else
  1220. #ifdef LGS_FOD
  1221. delay(1);
  1222. outportb( NEW_DTA_ADDRESS, 0xFF );
  1223. delay(1);
  1224. outportb( NEW_DTA_ADDRESS, 0xFF );
  1225. #else
  1226. #ifdef ALS_300_BM
  1227. delay(1);
  1228. outportb( NEW_DTA_ADDRESS, 0xFF );
  1229. delay(1);
  1230. outportb( NEW_DTA_ADDRESS, 0xFF );
  1231. #else
  1232. delay(1);
  1233. outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 ) & 0xFF );
  1234. delay(1);
  1235. outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 ) >> 8 ) & 0xFF );
  1236. #endif
  1237. #endif
  1238. #endif
  1239. #endif
  1240. #if defined( LGS_MND )
  1241. delay(1);
  1242. outportb( NEW_DTA_ADDRESS, m_Settings.nZeroShift & 0xFF );
  1243. delay(1);
  1244. outportb( NEW_DTA_ADDRESS, m_Settings.nZeroShift >> 8 ); // Zero offset
  1245. #else
  1246. delay(1);
  1247. outportb( NEW_DTA_ADDRESS, 0x00 );
  1248. delay(1);
  1249. outportb( NEW_DTA_ADDRESS, 0x00 ); // Zero offset
  1250. #endif
  1251. delay(1);
  1252. outportb( NEW_DTA_ADDRESS, m_Settings.nTemperature ); // Temp
  1253. delay(1);
  1254. outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // Work Status
  1255. delay(1);
  1256. outportb( NEW_DTA_ADDRESS, 3 ); // Reset DMA
  1257. delay(10); // To ensure stop of DMA conveyor
  1258. m_nbWriteChunk = 0;
  1259. m_nbReadChunk = 0;
  1260. m_nwChunkInterator = 0;
  1261. m_bDMA_is_crashed = false;
  1262. memset ( DMA_BUFFER, 0, DMA_TRANSFER_CHUNK );
  1263. #ifdef __DJGPP__
  1264. //uint32_t LA = _dma_buffer_ra.segment << 4 + _dma_buffer_ra.offset16;
  1265. InitDMA( DMA_CHANNEL );
  1266. #else
  1267. InitDMA( DMA_CHANNEL , DMA_BUFFER, DMA_TRANSFER_CHUNK );
  1268. #endif
  1269. m_bOutputMask |= (BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
  1270. SetRelayBit( m_Settings.RelayDefaultMode );
  1271. #if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND ) || defined( LGS_FOD ) || defined( ALS_300_BM )
  1272. outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
  1273. #else
  1274. outportb( NEW_RST_ADDRESS, 5 ); // Status + DMA Upload
  1275. #endif
  1276. delay(1);
  1277. outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // Work Status
  1278. delay(1);
  1279. outportb( NEW_DTA_ADDRESS, 1 ); // Init DMA
  1280. // enable interrupts
  1281. //enable();
  1282. m_nRealAngle = -1;
  1283. m_nErrorAngle = -1;
  1284. break;
  1285. };
  1286. case WM_IRQ_WORK:{
  1287. // disable interrupts
  1288. disable();
  1289. // disable laser devices
  1290. outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
  1291. enable();
  1292. delay(100);
  1293. if( !m_bWorkEnable ){
  1294. BSNLB=inportb( NEW_BASE_ADDRESS + 6 );
  1295. BSNHB=inportb( NEW_BASE_ADDRESS + 7 );
  1296. mwBoardID = (( BSNHB<<8) | BSNLB );
  1297. if ( ( ~( mwBoardID ^ mwSoftwareID ) & 0xFFFF )== 0 ) m_bWorkEnable = true;
  1298. outportb( NEW_BASE_ADDRESS + 6, mwSoftwareID & 0xFF );
  1299. outportb( NEW_BASE_ADDRESS + 7, ( mwSoftwareID >> 8 ) & 0xFF );
  1300. };
  1301. delay(1);
  1302. //outportb( NEW_BASE_ADDRESS + 3, 0 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nGearRatio >> 8 ); // motor ratio
  1303. //outportb( NEW_BASE_ADDRESS + 3, 1 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nGearRatio & 0xFF );
  1304. //outportb( NEW_BASE_ADDRESS + 3, 2 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nEncoderSteps >> 8 ); // encoder count
  1305. //outportb( NEW_BASE_ADDRESS + 3, 3 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nEncoderSteps & 0xFF );
  1306. outportb( NEW_BASE_ADDRESS + 3, 4 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nTemperature ); // temperature
  1307. delay(1);
  1308. outportb( NEW_BASE_ADDRESS + 3, 5 ); outportb( NEW_BASE_ADDRESS + 4, 0x00 );//m_Settings.nZeroShift >> 8 ); // zero offset
  1309. delay(1);
  1310. outportb( NEW_BASE_ADDRESS + 3, 6 ); outportb( NEW_BASE_ADDRESS + 4, 0x00 );//m_Settings.nZeroShift & 0xFF );
  1311. //outportb( NEW_BASE_ADDRESS + 3, 7 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nMaxRange >> 8 );
  1312. //outportb( NEW_BASE_ADDRESS + 3, 8 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nMaxRange & 0xFF ); // max range
  1313. // enable interrupts
  1314. delay( 10 );
  1315. m_bOutputMask |= (BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
  1316. SetRelayBit( m_Settings.RelayDefaultMode );
  1317. // enable laser devices
  1318. outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
  1319. m_BoardData.Clear();
  1320. m_nRealAngle = -1;
  1321. m_nErrorAngle = -1;
  1322. break;
  1323. };
  1324. }; // end of SWITCH WM
  1325. // if( g_UI_Debug ){
  1326. #ifdef DEBUG_BOARD_PRINT_NO_IRQ
  1327. printf("Board Async Reset\n");
  1328. #endif
  1329. // };
  1330. #endif // BOARD_SIMULATION
  1331. }
  1332. void CBoard::OnISR(){
  1333. switch( m_bWM ) {
  1334. case WM_FW_UPGRADE:{
  1335. if( m_pFirmwareImage ) {
  1336. if( m_pFirmwareSentLine < m_pFirmwareImage->nLines ){
  1337. outportb( BLO_WRITE_START_TRANSFER, m_pFirmwareImage->pLines[m_pFirmwareSentLine].len );
  1338. outportb( BLO_WRITE_DATA_PORT, m_pFirmwareImage->pLines[m_pFirmwareSentLine].type );
  1339. outportb( BLO_WRITE_DATA_PORT, m_pFirmwareImage->pLines[m_pFirmwareSentLine].offset_hb );
  1340. outportb( BLO_WRITE_DATA_PORT, m_pFirmwareImage->pLines[m_pFirmwareSentLine].offset_lb );
  1341. for( uint8_t idx = 0; idx < (m_pFirmwareImage->pLines[m_pFirmwareSentLine].len+1); idx++ ){
  1342. outportb( BLO_WRITE_DATA_PORT, m_pFirmwareImage->pLines[m_pFirmwareSentLine].data[idx] );
  1343. };
  1344. outportb( BLO_WRITE_END_TRANSFER, 0xFF );
  1345. m_pFirmwareSentLine++;
  1346. };
  1347. if( m_pFirmwareSentLine == m_pFirmwareImage->nLines ){
  1348. m_bFirmwareUpgradeDone = true;
  1349. };
  1350. };
  1351. break;
  1352. };
  1353. case WM_IRQ_WORK:{
  1354. // Port Mode
  1355. // TestBit( true );
  1356. // update timer
  1357. m_wTimer++;
  1358. if( m_eBoardType == BT_VORTEX_SX || m_eBoardType == BT_VORTEX_DX || m_eBoardType == BT_VORTEX_MX ){
  1359. enum { BF_NONE = 0x00, BF_ZERO = 0x10, BF_SUN1 = 0x20, BF_SUN2 = 0x40, BF_POWER = 0x80, BF_MASK = 0xF0 };
  1360. // special structure for data converting
  1361. typedef union DATA {
  1362. uint16_t wValue[2];
  1363. uint8_t bValue[4];
  1364. } DATA, *PDATA;
  1365. if( m_bOutputDirty ){
  1366. m_bOutputDirty = false;
  1367. outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
  1368. outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask ); // fix of bug with write to MCU board
  1369. }
  1370. DATA data;
  1371. // ---1--- read value for first laser
  1372. data.bValue[0] = inportb(NEW_BASE_ADDRESS);
  1373. data.bValue[1] = inportb(NEW_BASE_ADDRESS + 1);
  1374. // read value for second laser
  1375. data.bValue[2] = inportb(NEW_BASE_ADDRESS);
  1376. data.bValue[3] = inportb(NEW_BASE_ADDRESS + 1);
  1377. // store flags
  1378. uint8_t bFlags = data.bValue[1];
  1379. // initialize sum
  1380. uint16_t wRange[2] = { (uint16_t)(data.wValue[0] & 0x0FFF), (uint16_t)(data.wValue[1] & 0x0FFF) };
  1381. // release interrupt controler
  1382. //outp( PICICR, EOI );
  1383. wRange[0] = 0x07FF - (wRange[0] >> 1);
  1384. wRange[1] = 0x07FF - (wRange[1] >> 1);
  1385. #ifdef BOARD_DEFFERED_PROCESSING
  1386. BOARDDATA bd;
  1387. // check for zero mark and sun bit
  1388. bd.bZero = bFlags & BF_ZERO; // read zero
  1389. bd.bPower = bFlags & BF_POWER; // read power bit
  1390. bd.bSun[0] = bFlags & BF_SUN1; // read sun from device 1
  1391. bd.bSun[1] = bFlags & BF_SUN2; // read sun from device 2
  1392. // prepare values ( convert from 4095..0 to 0..2047 )
  1393. bd.nRange[0] = wRange[0];
  1394. bd.nRange[1] = wRange[1];
  1395. // put values into conveier
  1396. m_BoardData.Put( bd );
  1397. #else // BOARD_DEFFERED_PROCESSING
  1398. // check for zero mark and sun bit
  1399. m_BoardData.bZero = bFlags & BF_ZERO; // read zero
  1400. m_BoardData.bPower = bFlags & BF_POWER; // read zero
  1401. m_BoardData.bSun[0] = bFlags & BF_SUN1; // read sun from device 1
  1402. m_BoardData.bSun[1] = bFlags & BF_SUN2; // read sun from device 2
  1403. // prepare values ( convert from 4095..0 to 0..2047 )
  1404. m_BoardData.nRange[0] = wRange[0];
  1405. m_BoardData.nRange[1] = wRange[1];
  1406. // value ready
  1407. m_bReady = true;
  1408. #endif // BOARD_DEFFERED_PROCESSING
  1409. } else {
  1410. // release interrupt controler
  1411. //outp( PICICR, EOI );
  1412. };
  1413. break;
  1414. };
  1415. case WM_DMA_WORK:{
  1416. unsigned long irqsave = save_irqdisable();
  1417. #ifndef COMM_WITHOUT_INTERRUPTS
  1418. // To Disable IRQ line
  1419. outportb( MASTER_PIC_DATA, ( inportb( MASTER_PIC_DATA ) | 0x18 ) ); /* disable IRQ3 and IRQ4 (com ports) 0x8+0x10 */
  1420. #endif
  1421. //Wait Bord to be ready for BUS cycle
  1422. //for( uint16_t i=0; i<100; i++ ) io_wait();
  1423. //io_delay( 1 );
  1424. outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
  1425. outportb( NEW_RST_ADDRESS, 7 );
  1426. outportb( NEW_RST_ADDRESS, 7 );
  1427. //io_delay( 1 );
  1428. m_nbWriteChunk ++;
  1429. if( m_nbWriteChunk == DMA_TRANSFER_ZONES ) m_nbWriteChunk = 0;
  1430. if( m_bDMA_first_chunk_transfered && ( m_nbWriteChunk == m_nbReadChunk ) ){
  1431. m_bDMA_is_crashed = true;
  1432. } else {
  1433. #ifdef __DJGPP__
  1434. if( DMA_BUFFER ){
  1435. uint8_t oIterator = _dma_dos_buffer_iterator;
  1436. _dma_dos_buffer_iterator = 1 - _dma_dos_buffer_iterator;
  1437. if( _dma_dos_selector[_dma_dos_buffer_iterator] != -1 ) {
  1438. InitDMA( DMA_CHANNEL );
  1439. //outportb( ICR_REGISTER, END_OF_INTERRUPT );
  1440. //io_wait();
  1441. //io_wait();
  1442. outportb( NEW_DTA_ADDRESS, m_bOutputMask );
  1443. io_wait();
  1444. //io_delay( 1 );
  1445. //io_wait();
  1446. if( m_bDMA_is_crashed || m_bResetIssued ){
  1447. outportb( NEW_DTA_ADDRESS, 3 ); // Stop DMA
  1448. m_bResetIssued = false;
  1449. } else {
  1450. outportb( NEW_DTA_ADDRESS, 2 );
  1451. };
  1452. };
  1453. if( _dma_dos_selector[oIterator] != -1 ) {
  1454. // save Transfered memory to PM memory
  1455. //uint32_t LA = _dma_buffer_ra.segment << 4 + _dma_buffer_ra.offset16;
  1456. //Get Back RM to PM
  1457. dosmemget( _dma_dos_linear_address[oIterator], DMA_TRANSFER_CHUNK, DMA_BUFFER + DMA_TRANSFER_CHUNK * m_nbWriteChunk );
  1458. if( !m_bDMA_first_chunk_transfered ) m_bDMA_first_chunk_transfered = true;
  1459. };
  1460. };
  1461. #else
  1462. InitDMA( DMA_CHANNEL , DMA_BUFFER + DMA_TRANSFER_CHUNK * m_nbWriteChunk , DMA_TRANSFER_CHUNK );
  1463. #endif
  1464. };
  1465. #ifndef COMM_WITHOUT_INTERRUPTS
  1466. /* Unmask (i.e. enable) IRQ3. This requires turning bit 3 to 0 */
  1467. /* and bits 0,1,2,4,5,6,7 to 1. Hence 11110011 binary = F3 hex */
  1468. outportb( MASTER_PIC_DATA, ( inportb( MASTER_PIC_DATA ) & 0xE7 ) ); /* Unmask (Enable) IRQ3 and IRQ4 (com ports) */
  1469. #endif
  1470. irqrestore( irqsave );
  1471. break;
  1472. };
  1473. };
  1474. };
  1475. void CBoard::Reset(){
  1476. switch( m_bWM ){
  1477. case WM_DMA_WORK:{
  1478. // disable laser devices
  1479. // outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1) );
  1480. //SetStatus( m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1) );
  1481. m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
  1482. SetRelayBit( m_Settings.RelayDefaultMode );
  1483. StopDMA();
  1484. if( !m_bWorkEnable ){
  1485. outportb( NEW_RST_ADDRESS, 0 );// Full Upload
  1486. outportb( NEW_RST_ADDRESS, 0 );// Full Upload
  1487. outportb( NEW_RST_ADDRESS, 0 );// Full Upload
  1488. delay(1);
  1489. BSNLB = inportb( NEW_DTA_ADDRESS );
  1490. delay(1);
  1491. BSNHB = inportb( NEW_DTA_ADDRESS );
  1492. delay(1);
  1493. fw_year = inportb( NEW_DTA_ADDRESS );
  1494. delay(1);
  1495. fw_month = inportb( NEW_DTA_ADDRESS );
  1496. delay(1);
  1497. fw_day = inportb( NEW_DTA_ADDRESS );
  1498. delay(1);
  1499. bl_year = inportb( NEW_DTA_ADDRESS );
  1500. delay(1);
  1501. bl_month = inportb( NEW_DTA_ADDRESS );
  1502. delay(1);
  1503. bl_day = inportb( NEW_DTA_ADDRESS );
  1504. delay(1);
  1505. mwBoardID = (( BSNHB<<8) | BSNLB );
  1506. //printf("Board ser num: %04X\n", mwBoardID );
  1507. if ( ( ~( mwBoardID ^ mwSoftwareID ) & 0xFFFF ) == 0 ) m_bWorkEnable = true;
  1508. };
  1509. //Send SW ID in any case
  1510. outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
  1511. outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
  1512. outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
  1513. delay(1);
  1514. // Serial Number
  1515. outportb( NEW_DTA_ADDRESS, mwSoftwareID & 0xFF );
  1516. delay(1);
  1517. outportb( NEW_DTA_ADDRESS, ( mwSoftwareID >> 8 ) & 0xFF );
  1518. delay(1);
  1519. // Motor Speed
  1520. #if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND ) || defined( LGS_FOD ) || defined( ALS_300_BM )
  1521. #ifdef LGS_PLS_200
  1522. outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 * 3 ) & 0xFF );
  1523. delay(1);
  1524. outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 * 3 ) >> 8 ) & 0xFF );
  1525. delay(1);
  1526. #else
  1527. #ifdef LGS_FOD
  1528. outportb( NEW_DTA_ADDRESS, 0xFF );
  1529. delay(1);
  1530. outportb( NEW_DTA_ADDRESS, 0xFF );
  1531. delay(1);
  1532. #else
  1533. #ifdef ALS_300_BM
  1534. outportb( NEW_DTA_ADDRESS, 0xFF );
  1535. delay(1);
  1536. outportb( NEW_DTA_ADDRESS, 0xFF );
  1537. delay(1);
  1538. #else
  1539. outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 ) & 0xFF );
  1540. delay(1);
  1541. outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 ) >> 8 ) & 0xFF );
  1542. delay(1);
  1543. #endif
  1544. #endif
  1545. #endif
  1546. #endif
  1547. // Zero Shift
  1548. #if defined( LGS_MND )
  1549. outportb( NEW_DTA_ADDRESS, m_Settings.nZeroShift & 0xFF );
  1550. delay(1);
  1551. outportb( NEW_DTA_ADDRESS, m_Settings.nZeroShift >> 8 );
  1552. delay(1);
  1553. #else
  1554. outportb( NEW_DTA_ADDRESS, 0x00 );
  1555. delay(1);
  1556. outportb( NEW_DTA_ADDRESS, 0x00 );
  1557. delay(1);
  1558. #endif
  1559. // Temperature
  1560. outportb( NEW_DTA_ADDRESS, m_Settings.nTemperature );
  1561. delay(1);
  1562. // Work Status
  1563. outportb( NEW_DTA_ADDRESS, m_bOutputMask );
  1564. delay(1);
  1565. // DMA Status
  1566. outportb( NEW_DTA_ADDRESS, 3 ); // Stop DMA
  1567. delay(10);
  1568. m_nbWriteChunk = 0;
  1569. m_nbReadChunk = 0;
  1570. m_nwChunkInterator = 0;
  1571. m_bDMA_first_chunk_transfered = false;
  1572. m_bResetIssued = false;
  1573. m_bDMA_is_crashed = false;
  1574. memset ( DMA_BUFFER, 0, DMA_TRANSFER_CHUNK * DMA_TRANSFER_ZONES );
  1575. _dma_dos_buffer_iterator = 0;
  1576. #ifdef __DJGPP__
  1577. //uint32_t LA = _dma_buffer_ra.segment << 4 + _dma_buffer_ra.offset16;
  1578. InitDMA( DMA_CHANNEL );
  1579. #else
  1580. InitDMA( DMA_CHANNEL , DMA_BUFFER, DMA_TRANSFER_CHUNK );
  1581. #endif
  1582. m_bOutputMask |= (BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
  1583. SetRelayBit( m_Settings.RelayDefaultMode );
  1584. #if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND ) || defined( LGS_FOD ) || defined( ALS_300_BM )
  1585. outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
  1586. outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
  1587. outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
  1588. delay(1);
  1589. #else
  1590. outportb( NEW_RST_ADDRESS, 5 ); // Status + DMA Upload
  1591. outportb( NEW_RST_ADDRESS, 5 ); // Status + DMA Upload
  1592. outportb( NEW_RST_ADDRESS, 5 ); // Status + DMA Upload
  1593. delay(1);
  1594. #endif
  1595. outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // Work Status
  1596. delay(1);
  1597. outportb( NEW_DTA_ADDRESS, 1 ); // Init DMA
  1598. m_nRealAngle = -1;
  1599. m_nErrorAngle = -1;
  1600. break;
  1601. }; // case WM_DMA
  1602. case WM_IRQ_WORK:{
  1603. // disable interrupts
  1604. //disable();
  1605. // disable laser devices
  1606. outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
  1607. //enable();
  1608. delay(100);
  1609. if( !m_bWorkEnable ){
  1610. BSNLB=inportb( NEW_BASE_ADDRESS + 6 );
  1611. BSNHB=inportb( NEW_BASE_ADDRESS + 7 );
  1612. mwBoardID = (( BSNHB<<8) | BSNLB );
  1613. if ( ( ~( mwBoardID ^ mwSoftwareID ) & 0xFFFF ) == 0 ) m_bWorkEnable = true;
  1614. outportb( NEW_BASE_ADDRESS + 6, mwSoftwareID & 0xFF );
  1615. outportb( NEW_BASE_ADDRESS + 7, ( mwSoftwareID >> 8 ) & 0xFF );
  1616. };
  1617. //outportb( NEW_BASE_ADDRESS + 3, 0 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nGearRatio >> 8 ); // motor ratio
  1618. //outportb( NEW_BASE_ADDRESS + 3, 1 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nGearRatio & 0xFF );
  1619. //outportb( NEW_BASE_ADDRESS + 3, 2 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nEncoderSteps >> 8 ); // encoder count
  1620. //outportb( NEW_BASE_ADDRESS + 3, 3 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nEncoderSteps & 0xFF );
  1621. outportb( NEW_BASE_ADDRESS + 3, 4 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nTemperature ); // temperature
  1622. delay( 10 );
  1623. outportb( NEW_BASE_ADDRESS + 3, 5 ); outportb( NEW_BASE_ADDRESS + 4, 0x00 );//m_Settings.nZeroShift >> 8 ); // zero offset
  1624. delay( 10 );
  1625. outportb( NEW_BASE_ADDRESS + 3, 6 ); outportb( NEW_BASE_ADDRESS + 4, 0x00 );//m_Settings.nZeroShift & 0xFF );
  1626. //outportb( NEW_BASE_ADDRESS + 3, 7 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nMaxRange >> 8 );
  1627. //outportb( NEW_BASE_ADDRESS + 3, 8 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nMaxRange & 0xFF ); // max range
  1628. delay( 10 );
  1629. // enable laser devices
  1630. m_bOutputMask |= (BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
  1631. SetRelayBit( m_Settings.RelayDefaultMode );
  1632. outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
  1633. m_BoardData.Clear();
  1634. m_nRealAngle = -1;
  1635. m_nErrorAngle = -1;
  1636. break;
  1637. }; // case WM_IRQ
  1638. }; // end of SWITCH WM
  1639. };
  1640. // special structure for data converting
  1641. #ifdef RANGE_16BIT
  1642. enum { BF_NONE = 0x00, BF_ZERO = 0x10, BF_SUN1 = 0x20, BF_SUN2 = 0x40, BF_POWER = 0x80, BF_MASK = 0xF0 };
  1643. #else
  1644. enum { BF_NONE = 0x00, BF_ZERO = 0x10, BF_SUN1 = 0x20, BF_SUN2 = 0x40, BF_POWER = 0x80, BF_MASK = 0xF0 };
  1645. #endif
  1646. uint8_t CBoard::GetData( PBOOL pZero, PBOOL pSun, PWORD pRange, PBOOL pPower ){
  1647. uint8_t bRetVal = 0;
  1648. switch( m_bWM ) {
  1649. case WM_DMA_WORK:{
  1650. FPDATA data;
  1651. uint8_t bFlags;
  1652. uint16_t wRange[2];
  1653. if( m_bDMA_first_chunk_transfered ){
  1654. if( m_bDMA_is_crashed ){
  1655. #ifdef DEBUG_BOARD_PRINT_NO_IRQ
  1656. printf( "DMA Overlaps [%d]=[%d] - BR\n", m_nbReadChunk , m_nbWriteChunk );
  1657. #endif
  1658. this->Reset();
  1659. bRetVal = 2;
  1660. } else {
  1661. if( m_nbWriteChunk != m_nbReadChunk ){
  1662. data = ( FPDATA ) ( DMA_BUFFER + DMA_TRANSFER_CHUNK * m_nbReadChunk );
  1663. #ifdef DEBUG_BOARD_DUMP_DATA
  1664. if( m_nwChunkInterator == 0 && m_nbReadChunk == ( DMA_TRANSFER_ZONES - 1 ) ){
  1665. FILE *FID;
  1666. FID = fopen("BUFFER.txt","w+b"); //a
  1667. fwrite( DMA_BUFFER, DMA_TRANSFER_ZONES, DMA_TRANSFER_CHUNK, FID );
  1668. fclose( FID );
  1669. };
  1670. #endif
  1671. m_wTimer++;
  1672. #ifdef RANGE_16BIT
  1673. // store flags
  1674. bFlags = data[ m_nwChunkInterator ].bValue[2] & BF_MASK;
  1675. wRange[0] = ( 0xFFFF - ( data[m_nwChunkInterator].wValue[0] ) & 0xFFFF );
  1676. #ifndef DMA_SINGLE_LRF
  1677. //wRange[1] = ( 0xFFFF - ( data[m_nwChunkInterator].bValue[4] << 8 + data[m_nwChunkInterator].bValue[3] ) & 0xFFFF );// >> 5;
  1678. wRange[1] = ( 0xFFFF - ( data[m_nwChunkInterator].wValue[1] ) & 0xFFFF );
  1679. #else // DMA_SINGLE_LRF
  1680. wRange[1] = wRange[0];
  1681. #endif // DMA_SINGLE_LRF
  1682. #else
  1683. // store flags
  1684. bFlags = data[ m_nwChunkInterator ].bValue[1] & BF_MASK;
  1685. wRange[0] = 0x07FF - (( data[m_nwChunkInterator].wValue[0] & 0x0FFF ) >> 1);
  1686. #ifndef DMA_SINGLE_LRF
  1687. wRange[1] = 0x07FF - (( data[m_nwChunkInterator].wValue[1] & 0x0FFF ) >> 1);
  1688. #else // DMA_SINGLE_LRF
  1689. wRange[1] = wRange[0];
  1690. #endif // DMA_SINGLE_LRF
  1691. #endif
  1692. #ifndef LGS_MND
  1693. if( bFlags & BF_ZERO ){
  1694. //putch( 0x2A );
  1695. if( m_nRealAngle < 0 ){
  1696. m_nRealAngle = m_Settings.nZeroShift;
  1697. } else {
  1698. m_nErrorAngle = m_Settings.nZeroShift;
  1699. };
  1700. };
  1701. *pZero = ( m_nRealAngle == 0 ) || ( m_nErrorAngle == 0 );
  1702. if( m_nRealAngle > (-1) ) m_nRealAngle--;
  1703. if( m_nErrorAngle > (-1) ) m_nErrorAngle--;
  1704. //if( *pZero ) putch( 0x3e );
  1705. #else
  1706. *pZero = ( bFlags & BF_ZERO ) > 0; // read zero
  1707. if( *pZero ) {
  1708. m_nRealAngle = 0;
  1709. }else{
  1710. m_nRealAngle++;
  1711. };
  1712. #endif
  1713. #ifdef BOARD_POWER_ENABLE
  1714. *pPower = bFlags & BF_POWER; // read power bit
  1715. #else
  1716. *pPower = true;
  1717. #endif
  1718. #ifdef BOARD_SUN_BIT_DISABLE
  1719. pSun[0] = pSun[1] = false;
  1720. #else
  1721. pSun[0] = bFlags & BF_SUN1; // read sun from device 1
  1722. #ifndef DMA_SINGLE_LRF
  1723. pSun[1] = bFlags & BF_SUN2; // read sun from device 2
  1724. #else
  1725. pSun[1] = pSun[0];
  1726. #endif
  1727. #endif // BOARD_SUN_BIT_DISABLE
  1728. // prepare values ( convert from 4095..0 to 0..2047 )
  1729. pRange[0] = wRange[0];
  1730. #ifndef DMA_SINGLE_LRF
  1731. pRange[1] = wRange[1];
  1732. #endif
  1733. m_nwChunkInterator++;
  1734. if ( m_nwChunkInterator >= ( DMA_TRANSFER_CHUNK / DMA_ANGLE_SHOT ) ){
  1735. m_nwChunkInterator = 0;
  1736. m_nbReadChunk++;
  1737. if ( m_nbReadChunk == DMA_TRANSFER_ZONES ) m_nbReadChunk = 0;
  1738. };
  1739. bRetVal = 1;
  1740. } else {
  1741. bRetVal = 0;
  1742. }
  1743. }; // DMA CRASH
  1744. } else bRetVal = 0;
  1745. break;
  1746. };
  1747. case WM_IRQ_WORK:{
  1748. #ifdef BOARD_DEFFERED_PROCESSING
  1749. // check data in conveier
  1750. BOARDDATA bd;
  1751. if( m_BoardData.Get( &bd ) ) {
  1752. // return data
  1753. *pZero = bd.bZero;
  1754. #ifdef BOARD_POWER_ENABLE
  1755. *pPower = bd.bPower;
  1756. #else
  1757. *pPower = true;
  1758. #endif
  1759. #ifdef BOARD_SUN_BIT_DISABLE
  1760. pSun[0] = pSun[1] = false;
  1761. #else
  1762. pSun[0] = bd.bSun[0];
  1763. pSun[1] = bd.bSun[1];
  1764. #endif
  1765. pRange[0] = bd.nRange[0];
  1766. pRange[1] = bd.nRange[1];
  1767. bRetVal = 1;
  1768. } else bRetVal = 0;
  1769. #else
  1770. // make sure that new data arrived
  1771. if( m_bReady ) {
  1772. // data will be readed
  1773. m_bReady = false;
  1774. // fill data fields
  1775. *pZero = m_BoardData.bZero;
  1776. #ifdef BOARD_POWER_ENABLE
  1777. *pPower = m_BoardData.bPower;
  1778. #else
  1779. *pPower = true;
  1780. #endif
  1781. #ifdef BOARD_SUN_BIT_DISABLE
  1782. pSun[0] = pSun[1] = false;
  1783. #else
  1784. pSun[0] = m_BoardData.bSun[0];
  1785. pSun[1] = m_BoardData.bSun[1];
  1786. #endif
  1787. pRange[0] = m_BoardData.nRange[0];
  1788. pRange[1] = m_BoardData.nRange[1];
  1789. bRetVal = 1;
  1790. } else bRetVal = 0;
  1791. #endif
  1792. break;
  1793. };
  1794. default:{
  1795. if( m_bWM == WM_FW_UPGRADE ) {
  1796. if( m_pFirmwareImage ) {
  1797. if( m_bFirmwareUpgradeDone ){
  1798. #ifdef DEBUG
  1799. printf("Board Firmware Transfered!\n");
  1800. #endif
  1801. freeHexImage( m_pFirmwareImage );
  1802. unlink( _T( "firmware" ) );
  1803. m_pFirmwareImage = NULL;
  1804. };
  1805. };
  1806. };
  1807. bRetVal = 1;
  1808. m_nwChunkInterator ++;
  1809. if( m_nwChunkInterator > LGS_REAL_DETECTOR_ANGLES * LGS_BOARDSAMPLE_RATIO ){
  1810. m_nwChunkInterator = 0;
  1811. };
  1812. if( ( m_nwChunkInterator % 2 ) == 0 ) bRetVal = 0;
  1813. *pZero = m_nwChunkInterator == 0;
  1814. *pSun = false;
  1815. *pRange = 1024;
  1816. *pPower = true;
  1817. break;
  1818. };
  1819. };
  1820. return bRetVal;
  1821. };
  1822. bool CBoard::getSettings( BOARD_SETTINGS *S ){
  1823. if( S == NULL ) return false;
  1824. memcpy( S, &m_Settings, sizeof(BOARD_SETTINGS));
  1825. return true;
  1826. };