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