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.

127 lines
5.7 KiB

5 years ago
  1. /*******************************************************************************
  2. *
  3. * uart.c - c file for UART communication using MSP430 TimerA
  4. * - peripheral
  5. *
  6. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * Neither the name of Texas Instruments Incorporated nor the names of
  21. * its contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. *
  37. ******************************************************************************/
  38. #include "uart.h"
  39. unsigned char timerA_UART_mode = 0;
  40. unsigned int txData;
  41. //------------------------------------------------------------------------------
  42. // Function configures Timer_A for full-duplex UART operation
  43. //------------------------------------------------------------------------------
  44. void TimerA_UART_init(void)
  45. {
  46. DCOCTL = 0x00; // Set DCOCLK to 1MHz
  47. BCSCTL1 = CALBC1_1MHZ;
  48. DCOCTL = CALDCO_1MHZ;
  49. BCSCTL2 &= ~DIVS_3; // SMCLK = 1MHz
  50. P1SEL |= UART_TXD + UART_RXD; // Timer function for TXD/RXD pins
  51. // P1SEL |= UART_TXD ;
  52. P1DIR |= UART_TXD; // TXD
  53. P1DIR &= ~UART_RXD;
  54. TACCTL0 = OUT; // Set TXD Idle as Mark = '1'
  55. // TACCTL1 = SCS + CM1 + CAP + CCIE; // Sync, Neg Edge, Capture, Int
  56. TACTL |= TACLR; // SMCLK, start in continuous mode
  57. TACTL = TASSEL_2 + MC_2; // SMCLK, start in continuous mode
  58. timerA_UART_mode = 1;
  59. }
  60. //------------------------------------------------------------------------------
  61. // Function unconfigures Timer_A for full-duplex UART operation
  62. //------------------------------------------------------------------------------
  63. void TimerA_UART_shutdown(void)
  64. {
  65. timerA_UART_mode = 0;
  66. P1SEL &= ~(UART_TXD + UART_RXD); // Timer function for TXD/RXD pins
  67. // P1SEL &= ~(UART_TXD ); // Timer function for TXD/RXD pins
  68. TACCTL1 &= ~CCIE; // Sync, Neg Edge, Capture, Int
  69. TACTL &= ~MC_3; // Clear TA modes --> Stop Timer Module
  70. P1OUT &= ~UART_TXD;
  71. }
  72. //------------------------------------------------------------------------------
  73. // Outputs one byte using the Timer_A UART
  74. //------------------------------------------------------------------------------
  75. void TimerA_UART_tx(unsigned char byte)
  76. {
  77. while (TACCTL0 & CCIE); // Ensure last char got TX'd
  78. TACCR0 = TAR; // Current state of TA counter
  79. TACCR0 += UART_TBIT; // One bit time till first bit
  80. txData = byte; // Load global variable
  81. txData |= 0x100; // Add mark stop bit to TXData
  82. txData <<= 1; // Add space start bit
  83. TACCTL0 = OUTMOD0 + CCIE; // Set TXD on EQU2 (idle), Int
  84. __bis_SR_register( LPM0_bits + GIE);
  85. }
  86. //------------------------------------------------------------------------------
  87. // Prints a string over using the Timer_A UART
  88. //------------------------------------------------------------------------------
  89. void TimerA_UART_print(char *string)
  90. {
  91. while (*string) {
  92. TimerA_UART_tx(*string++);
  93. }
  94. }
  95. //------------------------------------------------------------------------------
  96. // Timer_A UART - Transmit Interrupt Handler
  97. //------------------------------------------------------------------------------
  98. #pragma vector = TIMER0_A0_VECTOR
  99. __interrupt void Timer_A0_ISR(void)
  100. {
  101. static unsigned char txBitCnt = 10;
  102. if (!timerA_UART_mode)
  103. __bic_SR_register_on_exit(LPM3_bits+GIE);
  104. else
  105. {
  106. TACCR0 += UART_TBIT; // Add Offset to CCRx
  107. if (--txBitCnt == 0) // All bits TXed?
  108. {
  109. TACCTL0 &= ~CCIE; // All bits TXed, disable interrupt
  110. txBitCnt = 10;
  111. __bic_SR_register_on_exit(LPM0_bits+GIE);
  112. }
  113. else {
  114. if (txData & 0x01) {
  115. TACCTL0 &= ~OUTMOD2; // TX Mark '1'
  116. }
  117. else {
  118. TACCTL0 |= OUTMOD2; // TX Space '0'
  119. }
  120. txData >>= 1;
  121. }
  122. }
  123. }