1 /*
2 * initMCU.c
3 *
4 * Provides initialization functions.
5 *
6 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
7 *
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the
19 * distribution.
20 *
21 * Neither the name of Texas Instruments Incorporated nor the names of
22 * its contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 */
39 #include <MSP430.h>
40 #include "USB_API/USB_Common/types.h"
41 #include "USB_API/USB_Common/device.h"
42 #include "F5xx_F6xx_Core_Lib/HAL_UCS.h"
43 #include "F5xx_F6xx_Core_Lib/HAL_PMM.h"
45 #include "USB_config/descriptors.h"
46 #include "USB_API/USB_Common/usb.h" // USB-specific functions
47 #include "USB_API/USB_CDC_API/UsbCdc.h"
49 #include "uart.h"
50 #include "i2c.h"
52 void Port_Mapping(void);
54 //----------------------------------------------------------------------------
55 VOID Init_Clock(VOID)
56 {
57 #if defined (__MSP430F563x_F663x)
58 while(BAKCTL & LOCKIO) // Unlock XT1 pins for operation
59 BAKCTL &= ~(LOCKIO); // enable XT1 pins
60 // Workaround for USB7
61 UCSCTL6 &= ~XT1OFF;
62 #endif
63 if (USB_PLL_XT == 2)
64 {
65 #if defined (__MSP430F552x) || defined (__MSP430F550x)
66 P5SEL |= 0x0C; // enable XT2 pins for F5529
67 #elif defined (__MSP430F563x_F663x)
68 P7SEL |= 0x0C;
69 #endif
70 // Use the REFO oscillator to source the FLL and ACLK
71 UCSCTL3 = (UCSCTL3 & ~(SELREF_7)) | (SELREF__REFOCLK);
72 UCSCTL4 = (UCSCTL4 & ~(SELA_7)) | (SELA__REFOCLK);
74 // MCLK will be driven by the FLL (not by XT2), referenced to the REFO
75 Init_FLL(USB_MCLK_FREQ/1000, USB_MCLK_FREQ/32768); // Start the FLL, at the freq indicated by the config constant USB_MCLK_FREQ
77 //XT2_Start(XT2DRIVE_3); // Start the "USB crystal"
78 }
79 else
80 {
81 #if defined (__MSP430F552x) || defined (__MSP430F550x)
82 P5SEL |= 0x10; // enable XT1 pins
83 #endif
84 // Use the REFO oscillator to source the FLL and ACLK
85 // UCSCTL3 = SELREF__REFOCLK;
86 // UCSCTL4 = (UCSCTL4 & ~(SELA_7)) | (SELA__REFOCLK);
88 P5SEL |= 0x30; // enable XT1 pins for F5509
89 LFXT_Start(XT1DRIVE_3);
90 // Use the LFXT1 oscillator to source the FLL and ACLK
91 UCSCTL3 = SELA__XT1CLK;
92 UCSCTL4 = (UCSCTL4 & ~(SELA_7)) | (SELA__XT1CLK);
95 // MCLK will be driven by the FLL (not by XT2), referenced to the REFO
96 Init_FLL(USB_MCLK_FREQ/1000, USB_MCLK_FREQ/32768); // set FLL (DCOCLK)
98 //XT1_Start(XT1DRIVE_3); // Start the "USB crystal"
99 }
100 }
102 //----------------------------------------------------------------------------
103 VOID Init_Ports(VOID)
104 {
105 // Drive all I/O's as output-low, making sure there's no shoot-through current. There
106 // should be no floating I/Os, to prevent unnecessary current draw during USB suspend.
107 PAOUT = 0x0000;
108 PASEL = 0x0000;
109 PAREN = 0xF0FF;
110 PADIR = 0x0F00;
112 PBOUT = 0x0000;
113 PBSEL = 0x0000;
114 PBREN = 0xFFFF;
116 PCOUT = 0x0000;
117 PCSEL = 0x0000;
118 PCREN = 0xFFFF;
120 #ifndef __MSP430F550x
121 PDOUT = 0x0000; // If using a device other than:
122 PDSEL = 0x0000; // F5510, F5529, F5638, or F6638
123 PDREN = 0xF0FF;
124 PDDIR = 0xFFFF; // you may need to comment out these lines
125 #endif
128 #if defined (__MSP430F563x_F663x)
129 P9OUT = 0x00;
130 P9SEL = 0x00;
131 P9REN = 0xFF;
132 #endif
134 PJDIR = (0xFFFF - RESET_PIN - TEST_PIN);
135 PJOUT = 0x0000;
137 P1DIR |= 0x03; // for LEDs
138 }
142 //----------------------------------------------------------------------------
143 VOID Init_StartUp(VOID)
144 {
145 WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
147 __disable_interrupt(); // Disable global interrupts
149 Init_Ports(); // Init ports (do first ports because clocks do change ports)
150 SetVCore(3); // USB core requires the VCore set to 1.8 volt, independ of CPU clock frequency
151 Init_Clock();
153 __enable_interrupt(); // enable global interrupts
155 Port_Mapping();
157 #ifdef UART_BASED
158 #ifdef UART0_INTFNUM
159 InitUart0(9600);
160 #endif
162 #ifdef UART1_INTFNUM
163 InitUart1(9600);
164 #endif
165 #endif
167 #ifdef I2C_BASED
168 #ifdef UART0_INTFNUM
169 InitI2C(BSL_SLAVE_ADDR, 400000);
170 #endif
171 #endif
173 }
175 //----------------------------------------------------------------------------
176 VOID ConfigUSB(VOID)
177 {
178 USB_init(); // Init USB
180 // Enable various USB event handling routines
182 USB_setEnabledEvents(kUSB_VbusOnEvent+kUSB_VbusOffEvent+kUSB_receiveCompletedEvent
183 +kUSB_dataReceivedEvent+kUSB_UsbSuspendEvent+kUSB_UsbResumeEvent+kUSB_UsbResetEvent);
186 // See if we're already attached physically to USB, and if so, connect to it
187 // Normally applications don't invoke the event handlers, but this is an exception.
189 if (USB_connectionInfo() & kUSB_vbusPresent)
190 {
191 if (USB_enable() == kUSB_succeed)
192 {
193 USB_reset();
194 USB_connect();
195 }
196 }
198 }
200 //----------------------------------------------------------------------------
201 void Port_Mapping(void)
202 {
203 // Disable Interrupts before altering Port Mapping registers
204 __disable_interrupt();
205 // Enable Write-access to modify port mapping registers
206 PMAPPWD = 0x02D52;
208 #ifdef PORT_MAP_RECFG
209 // Allow reconfiguration during runtime
210 PMAPCTL = PMAPRECFG;
211 #endif
213 #ifdef __MSP430F550x
214 //P4MAP0 = PM_UCA0RXD;
215 //P4MAP1 = PM_UCA0TXD;
216 #ifdef UART_BASED
217 P4MAP4 = PM_UCA0TXD;
218 P4MAP5 = PM_UCA0RXD;
219 #elif I2C_BASED
220 P4MAP4 = PM_UCB0SCL;
221 P4MAP5 = PM_UCB0SDA;
222 #else
223 #error Define a valid interface
224 #endif
225 //P4MAP7 = PM_MCLK;
226 #endif
227 #ifdef __MSP430F563x_F663x
228 P2MAP0 = PM_UCA0TXD;
229 P2MAP1 = PM_UCA0RXD;
230 #endif
232 // Disable Write-Access to modify port mapping registers
233 PMAPPWD = 0;
234 #ifdef PORT_MAP_EINT
235 __enable_interrupt(); // Re-enable all interrupts
236 #endif
237 }
238 //----------------------------------------------------------------------------
239 //End of file.
240 //----------------------------------------------------------------------------