xref: /btstack/port/archive/ez430-rf2560/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   UCA3CTL1 |= UCSWRST;                          //Reset State
39   UCA3CTL0 = UCMODE_0;
40 
41   UCA3CTL0 &= ~UC7BIT;                      // 8bit char
42   UCA3CTL1 |= UCSSEL_2;
43 
44   // 115200 on 16 Mhz
45   // UCA3BR0 = 16;                             // 8Mhz/57600=138
46   // UCA3BR1 = 1;
47   //  UCA3MCTL = 0xE;
48 
49   // 9600 on 16 Mhz - from family user guide
50   UCA3BR1 =  0x06;
51   UCA3BR0 =  0x82;
52   UCA3MCTL = 0xC;
53 
54   UCA3CTL1 &= ~UCSWRST;
55   // UCA3IE |= UCRXIE;
56   // __bis_SR_register(GIE);                   // Enable Interrupts
57 }
58 
59 /************************************************************************
60  * @brief  Disables the serial communications peripheral and clears the GPIO
61  *         settings used to communicate with the TUSB3410.
62  *
63  * @param  none
64  *
65  * @return none
66  **************************************************************************/
halUsbShutDown(void)67 void halUsbShutDown(void)
68 {
69   UCA3IE &= ~UCRXIE;
70   UCA3CTL1 = UCSWRST;                          //Reset State
71   USB_PORT_SEL &= ~( USB_PIN_RXD + USB_PIN_TXD );
72   USB_PORT_DIR |= USB_PIN_TXD;
73   USB_PORT_DIR |= USB_PIN_RXD;
74   USB_PORT_OUT &= ~(USB_PIN_TXD + USB_PIN_RXD);
75 }
76 
77 /************************************************************************
78  * @brief  Sends a character over UART to the TUSB3410.
79  *
80  * @param  character The character to be sent.
81  *
82  * @return none
83  **************************************************************************/
halUsbSendChar(char character)84 void halUsbSendChar(char character)
85 {
86   while (!(UCA3IFG & UCTXIFG));
87   UCA3TXBUF = character;
88 }
89 
halUsbRecvChar(void)90 char halUsbRecvChar(void){
91     while (!(UCA3IFG & UCRXIFG));
92     return UCA3RXBUF;
93 }
94 
95 /************************************************************************
96  * @brief  Sends a string of characters to the TUSB3410
97  *
98  * @param  string[] The array of characters to be transmit to the TUSB3410.
99  *
100  * @param  length   The length of the string.
101  *
102  * @return none
103  **************************************************************************/
halUsbSendString(char string[],unsigned char length)104 void halUsbSendString(char string[], unsigned char length)
105 {
106   volatile unsigned char i;
107   for (i=0; i < length; i++)
108     halUsbSendChar(string[i]);
109 }
110 
111 #ifdef USE_IRQ_RX
112 /************************************************************************/
USCI_A1_ISR(void)113 interrupt(USCI_A1_VECTOR) USCI_A1_ISR (void)
114 {
115   halUsbReceiveBuffer[bufferSize++] = UCA3RXBUF;
116    __bic_SR_register_on_exit(LPM3_bits);
117 }
118 #endif
119 
120 // provide putchar used by printf
putchar(int c)121 int putchar(int c){
122     halUsbSendChar(c);
123     return 1;
124 }