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.

218 lines
6.4 KiB

5 years ago
  1. /* --COPYRIGHT--,BSD
  2. * Copyright (c) 2013, Texas Instruments Incorporated
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of Texas Instruments Incorporated nor the names of
  17. * its contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. * --/COPYRIGHT--*/
  32. //*****************************************************************************
  33. //
  34. //! TouchProGUI.c
  35. //
  36. //! TouchProGUI enables interrupt-driven UART communication
  37. //! to the Touch Pro GUI platform tool. The protocol is defined in
  38. //! SLAU486 (The TouchPro GUI User's Guide).
  39. //! The baud rate is fixed at 9600B.
  40. //
  41. //! Resources: UCA0 in UART Mode, USCIAB0TX_VECTOR
  42. //! - UCA0 BR and MOD settings need to be placed in the header file for the
  43. //! to provide 9600B with a given input clock.
  44. //
  45. //! \author Texas Instruments
  46. //! \author MSP430 Strategic Applications
  47. //
  48. //! \version 1.0 - W. Schnoor, 1/7/14: Initial Release
  49. //
  50. //*****************************************************************************
  51. #include "TouchProGUI.h"
  52. uint8_t g_ui8TouchProGUITXBuffer[TOUCHPROGUI_BUFFER_LENGTH] = { TOUCHPROGUI_SYNC_UPPER, TOUCHPROGUI_SYNC_LOWER };
  53. uint8_t g_ui8TouchProGUITXLength = 0;
  54. //*****************************************************************************
  55. //
  56. //! TouchProGUI_init()
  57. //! This API will configure the USCI UCA0 in UART mode based upon the
  58. //! definitions UART_BR_LOWER, UART_BR_UPPER, and UART_MOD.
  59. //
  60. //! /param none.
  61. //! /return none.
  62. //
  63. //*****************************************************************************
  64. void TouchProGUI_init(void)
  65. {
  66. //
  67. // Configure Pin for UART Transmit Functionality
  68. //
  69. P1SEL |= BIT2;
  70. P1SEL2 |= BIT2;
  71. //
  72. // Configure USCIA0 UART based upon header file definitions
  73. //
  74. UCA0CTL1 |= UCSSEL_2;
  75. UCA0BR0 = UART_BR_LOWER;
  76. UCA0BR1 = UART_BR_UPPER;
  77. UCA0MCTL = UART_MOD;
  78. //
  79. // Release State Machine Reset
  80. //
  81. UCA0CTL1 &= ~UCSWRST;
  82. }
  83. //*****************************************************************************
  84. //
  85. //! TouchProGUI_sendData(uint16_t *ui16DataBuffer, uint8_t ui8Length)
  86. //! This API will send a set of data to TouchPro GUI.
  87. //
  88. //! /param ui16DataBuffer is a pointer to the data array to send.
  89. //! /param ui8Length specifies how many words in the ui16DataBuffer array
  90. //! that there are to be sent.
  91. //! /return none.
  92. //
  93. //*****************************************************************************
  94. void TouchProGUI_sendData(uint16_t *ui16DataBuffer, uint8_t ui8Length)
  95. {
  96. uint8_t *pTxBuffer;
  97. uint8_t ui8CurrentChannel;
  98. uint8_t ui8Checksum;
  99. //
  100. // Check to make sure the user is not trying to send more channels
  101. // than the buffer or TouchPro GUI supprt
  102. //
  103. if (ui8Length > TOUCHPROGUI_MAX_CHANNELS)
  104. {
  105. ui8Length = TOUCHPROGUI_MAX_CHANNELS;
  106. }
  107. //
  108. // Start buffer pointer at position 2 (since positions 0 and 1
  109. // are utilized for the sync word and are not to be altered).
  110. // NOTE: The check sum includes sync word.
  111. //
  112. pTxBuffer = &g_ui8TouchProGUITXBuffer[2];
  113. ui8Checksum = TOUCHPROGUI_SYNC_UPPER + TOUCHPROGUI_SYNC_LOWER;
  114. //
  115. // Calculate packet length (without sync word) in bytes
  116. // = (Length * 3) + 1
  117. //
  118. g_ui8TouchProGUITXLength = ui8Length * 3 + 1;
  119. *(pTxBuffer++) = g_ui8TouchProGUITXLength;
  120. ui8Checksum += g_ui8TouchProGUITXLength;
  121. //
  122. // Fill buffer with each channel's data
  123. //
  124. for (ui8CurrentChannel = 0; ui8CurrentChannel < ui8Length; ui8CurrentChannel++)
  125. {
  126. //
  127. // Channel Number
  128. //
  129. *pTxBuffer = (uint8_t)ui8CurrentChannel;
  130. ui8Checksum += *(pTxBuffer++);
  131. //
  132. // Channel Data Upper Byte
  133. //
  134. *pTxBuffer = (uint8_t)(ui16DataBuffer[ui8CurrentChannel] >> 8);
  135. ui8Checksum += *(pTxBuffer++);
  136. //
  137. // Channel Data Lower Byte
  138. //
  139. *pTxBuffer = (uint8_t)ui16DataBuffer[ui8CurrentChannel];
  140. ui8Checksum += *(pTxBuffer++);
  141. }
  142. //
  143. // Add checksum to buffer
  144. //
  145. *pTxBuffer = ui8Checksum;
  146. //
  147. // Increase packet length by three to include the sync word (0x55AA)
  148. // and length byte
  149. //
  150. g_ui8TouchProGUITXLength += 3;
  151. //
  152. // Start USCI_A0 transmit interrupt to
  153. // send the data via the USCI_A0 peripheral
  154. //
  155. IE2 |= UCA0TXIE;
  156. //
  157. // Go to LPM0 for completion of byte transmission
  158. //
  159. LPM0;
  160. //
  161. // If revived from LPMx, ensure packet got sent
  162. // before continuing
  163. //
  164. if (g_ui8TouchProGUITXLength != 0)
  165. {
  166. LPM0;
  167. }
  168. else
  169. {
  170. return;
  171. }
  172. }
  173. //*****************************************************************************
  174. //
  175. //! USCIAB0TX_VECTOR
  176. //
  177. //*****************************************************************************
  178. #pragma vector=USCIAB0TX_VECTOR
  179. __interrupt void USCI_ISR(void)
  180. {
  181. static uint8_t g_ui8CurrentByteIndex = 0;
  182. //
  183. // Send the next byte in the transmit buffer
  184. //
  185. UCA0TXBUF = g_ui8TouchProGUITXBuffer[g_ui8CurrentByteIndex++];
  186. //
  187. // If sending the last byte, clear interrupt enable, reset byte
  188. // counters, and wake back to application from LPMx.
  189. //
  190. if (g_ui8CurrentByteIndex == g_ui8TouchProGUITXLength)
  191. {
  192. g_ui8CurrentByteIndex = 0;
  193. g_ui8TouchProGUITXLength = 0;
  194. IE2 &= ~UCA0TXIE;
  195. LPM3_EXIT;
  196. }
  197. }