]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - msp430-bsl/msp430-bsl.git/blob - source/peripherals.h
MSP-BSL v3.0
[msp430-bsl/msp430-bsl.git] / source / peripherals.h
1 /* --COPYRIGHT--,BSD\r
2  * Copyright (c) 2014, Texas Instruments Incorporated\r
3  * All rights reserved.\r
4  *\r
5  * Redistribution and use in source and binary forms, with or without\r
6  * modification, are permitted provided that the following conditions\r
7  * are met:\r
8  *\r
9  * *  Redistributions of source code must retain the above copyright\r
10  *    notice, this list of conditions and the following disclaimer.\r
11  *\r
12  * *  Redistributions in binary form must reproduce the above copyright\r
13  *    notice, this list of conditions and the following disclaimer in the\r
14  *    documentation and/or other materials provided with the distribution.\r
15  *\r
16  * *  Neither the name of Texas Instruments Incorporated nor the names of\r
17  *    its contributors may be used to endorse or promote products derived\r
18  *    from this software without specific prior written permission.\r
19  *\r
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;\r
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR\r
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
31  * --/COPYRIGHT--*/\r
32 /*\r
33  * ======== hal.c ========\r
34  *\r
35  */\r
36 #include <MSP430.h>\r
37 #include <stdint.h>\r
38 #include "descriptors.h"\r
39 \r
40 #ifndef _PERIPHERALS_H_\r
41 #define _PERIPHERALS_H_\r
42 \r
43 #ifdef __cplusplus\r
44 extern "C"\r
45 {\r
46 #endif\r
47 \r
48 // Maximal host system delay\r
49 #define MAX_SYS_DELAY   20  // ms\r
50 \r
51 // Size of Rx/Tx UART Buffers\r
52 // Value should be big enough to cover max system delay\r
53 #define RX_BUFFER_SIZE  1280             // max_baud * SYS_DELAY ms/1000/8bit\r
54 #define TX_BUFFER_SIZE  MAX_PACKET_SIZE  // Don't modyfy this value!!!\r
55 \r
56 \r
57 enum active_peripherals{None, UART, I2C, SPI};\r
58 extern enum active_peripherals active_peripheral;\r
59 extern uint8_t rxBuffer[RX_BUFFER_SIZE];\r
60 extern uint8_t txBuffer[TX_BUFFER_SIZE];\r
61 extern uint8_t *txBuffer_read_ptr;\r
62 extern uint8_t *txBuffer_write_ptr;\r
63 \r
64 uint8_t BaudrateSelect(uint32_t lBaudrate);\r
65 \r
66 extern uint32_t current_baudrate;\r
67 \r
68 void disable_peripherals();\r
69 \r
70 void startup_led_sequence();  // Blink the LEDs\r
71 #define LED_POUT  P1OUT\r
72 #define LED1_PIN  BIT0\r
73 #define LED2_PIN  BIT1\r
74 #define LED1_ON   {LED_POUT |= LED1_PIN;}\r
75 #define LED1_OFF  {LED_POUT &= ~LED1_PIN;}\r
76 #define LED1_TOGGLE  {LED_POUT ^= LED1_PIN;}\r
77 #define LED2_ON   {LED_POUT |= LED2_PIN;}\r
78 #define LED2_OFF  {LED_POUT &= ~LED2_PIN;}\r
79 #define LED2_TOGGLE  {LED_POUT ^= LED2_PIN;}\r
80 \r
81 #define LED_BLINK_DELAY  5000000\r
82 #define LED_BLINK_COUNT  3\r
83 \r
84 /*** BSL entry sequence ******************************************************/\r
85 \r
86 #define ENTRY_SEQ_PDIR  PJDIR\r
87 #define ENTRY_SEQ_POUT  PJOUT\r
88 #define RESET_PIN   BIT1\r
89 #define TEST_PIN    BIT2\r
90 #define TCK_PIN     BIT3\r
91 \r
92 #define RESET_HIGH  {ENTRY_SEQ_POUT |= RESET_PIN;}\r
93 #define RESET_LOW   {ENTRY_SEQ_POUT &= ~RESET_PIN;}\r
94 #define TEST_HIGH   {ENTRY_SEQ_POUT |= TEST_PIN;}\r
95 #define TEST_LOW    {ENTRY_SEQ_POUT &= ~TEST_PIN;}\r
96 #define TCK_HIGH    {ENTRY_SEQ_POUT |= TCK_PIN;}\r
97 #define TCK_LOW     {ENTRY_SEQ_POUT &= ~TCK_PIN;}\r
98 \r
99 #define INVOKE_DELAY 10000\r
100 \r
101 // Timing for Pin Toggling during BSL Entry Sequence\r
102 // TEST PIN reset time = 10us (must be less than 15us)\r
103 #define BSL_ENTRY_SEQUENCE_TIME  50\r
104 \r
105 void BSL_invoke_sequence();  // Toggle RST/TST/TCK pins for BSL invoke\r
106 void BSL_reset_sequence();  // Reset sequence to leave BSL (JTAG20 workaround)\r
107 \r
108 \r
109 /*** UART ********************************************************************/\r
110 uint8_t InitUart(uint32_t lBaudrate);\r
111 void uartControl();\r
112 \r
113 /*** I2C *********************************************************************/\r
114 #define BSL_SLAVE_ADDR  0x48\r
115 \r
116 #define I2C_PORT_SEL  P4SEL\r
117 #define I2C_PORT_OUT  P4OUT\r
118 #define I2C_PORT_REN  P4REN\r
119 #define I2C_PORT_DIR  P4DIR\r
120 \r
121 #define SDA_PIN  BIT5\r
122 #define SCL_PIN  BIT0\r
123 #define SCL_CLOCK_DIV(X)  (USB_MCLK_FREQ/X)\r
124 \r
125 #define BSL_NO_RESPONSE_REQUIRED          0xAE\r
126 #define BSL_ERROR_HEADER_INCORRECT        0x51\r
127 #define BSL_ERROR_INCORRECT_RESPONSE_CRC  0xA0\r
128 #define BSL_ERROR_OK                      0x00\r
129 \r
130 #define I2C_HEADER         0x80\r
131 #define I2C_HEADER_LENGTH  1     // length in byte\r
132 #define I2C_LENGTH_LENGTH  2     // length in byte\r
133 #define I2C_CRC_LENGTH     2     // length in byte\r
134 \r
135 // Init I2C module B0 and according port settings\r
136 int8_t InitI2C(unsigned char eeprom_i2c_address, uint32_t bitrate);\r
137 int16_t i2cSendMessage(unsigned char* I2cMessage, int messageLength);\r
138 int16_t i2cReceiveMessage(unsigned char* I2cMessage);\r
139 void i2cBslEntrySequence(void);\r
140 void i2cStopSending(void);\r
141 unsigned int UART_FSM(unsigned char *dataBuffer);\r
142 \r
143 void i2cControl(void);\r
144 \r
145 \r
146 /*** SPI *********************************************************************/\r
147 #define SPI_CRC_LENGTH  2  // length in byte\r
148 \r
149 enum spiStates {idle, TXheader, TXlengthLow, TXlengthHigh, TXreceive, RXheader, RXlengthLow, RXlengthHigh, RXreceive};\r
150 \r
151 // Future enhancement for SPI\r
152 \r
153 uint8_t spiInit(uint32_t lBaudrate);\r
154 void spiControl();\r
155 \r
156 #ifdef __cplusplus\r
157 }\r
158 #endif\r
159 \r
160 #endif