xref: /btstack/port/archive/msp-exp430f5438-cc2564b/firmware/hal_usb.c (revision 1664436fd643daf66517dc309e6cc72448e8a86d)
1 /**
2  * @file  hal_usb.c
3  *
4  * Copyright 2008 Texas Instruments, Inc.
5 ***************************************************************************/
6 #include  <msp430x54x.h>
7 #include "hal_usb.h"
8 
9 
10 #ifdef USE_IRQ_RX
11 static char halUsbReceiveBuffer[255];
12 static unsigned char bufferSize=0;
13 #endif
14 
15 /************************************************************************
16  * @brief  Initializes the serial communications peripheral and GPIO ports
17  *         to communicate with the TUSB3410.
18  *
19  * @param  none
20  *
21  * @return none
22  **************************************************************************/
halUsbInit(void)23 void halUsbInit(void)
24 {
25 
26 #ifdef USE_IRQ_RX
27   volatile unsigned char i;
28   for (i = 0;i < 255; i++){
29     halUsbReceiveBuffer[i]='\0';
30   }
31   bufferSize = 0;
32 #endif
33 
34   USB_PORT_SEL |= USB_PIN_RXD + USB_PIN_TXD;
35   USB_PORT_DIR |= USB_PIN_TXD;
36   USB_PORT_DIR &= ~USB_PIN_RXD;
37 
38   UCA1CTL1 |= UCSWRST;                          //Reset State
39   UCA1CTL0 = UCMODE_0;
40 
41   UCA1CTL0 &= ~UC7BIT;                      // 8bit char
42   UCA1CTL1 |= UCSSEL_2;
43   UCA1BR0 = 16;                             // 8Mhz/57600=138
44   UCA1BR1 = 1;
45   UCA1MCTL = 0xE;
46   UCA1CTL1 &= ~UCSWRST;
47   // UCA1IE |= UCRXIE;
48   // __bis_SR_register(GIE);                   // Enable Interrupts
49 }
50 
51 /************************************************************************
52  * @brief  Disables the serial communications peripheral and clears the GPIO
53  *         settings used to communicate with the TUSB3410.
54  *
55  * @param  none
56  *
57  * @return none
58  **************************************************************************/
halUsbShutDown(void)59 void halUsbShutDown(void)
60 {
61   UCA1IE &= ~UCRXIE;
62   UCA1CTL1 = UCSWRST;                          //Reset State
63   USB_PORT_SEL &= ~( USB_PIN_RXD + USB_PIN_TXD );
64   USB_PORT_DIR |= USB_PIN_TXD;
65   USB_PORT_DIR |= USB_PIN_RXD;
66   USB_PORT_OUT &= ~(USB_PIN_TXD + USB_PIN_RXD);
67 }
68 
69 /************************************************************************
70  * @brief  Sends a character over UART to the TUSB3410.
71  *
72  * @param  character The character to be sent.
73  *
74  * @return none
75  **************************************************************************/
halUsbSendChar(char character)76 void halUsbSendChar(char character)
77 {
78   while (!(UCA1IFG & UCTXIFG));
79   UCA1TXBUF = character;
80 }
81 
halUsbRecvChar(void)82 char halUsbRecvChar(void){
83     while (!(UCA1IFG & UCRXIFG));
84     return UCA1RXBUF;
85 }
86 
87 /************************************************************************
88  * @brief  Sends a string of characters to the TUSB3410
89  *
90  * @param  string[] The array of characters to be transmit to the TUSB3410.
91  *
92  * @param  length   The length of the string.
93  *
94  * @return none
95  **************************************************************************/
halUsbSendString(char string[],unsigned char length)96 void halUsbSendString(char string[], unsigned char length)
97 {
98   volatile unsigned char i;
99   for (i=0; i < length; i++)
100     halUsbSendChar(string[i]);
101 }
102 
103 #ifdef USE_IRQ_RX
104 /************************************************************************/
USCI_A1_ISR(void)105 interrupt(USCI_A1_VECTOR) USCI_A1_ISR (void)
106 {
107   halUsbReceiveBuffer[bufferSize++] = UCA1RXBUF;
108    __bic_SR_register_on_exit(LPM3_bits);
109 }
110 #endif
111 
112 // provide putchar used by printf
putchar(int c)113 int putchar(int c){
114     halUsbSendChar(c);
115     return 1;
116 }