amiro-blt / Target / Source / ARMCM3_STM32 / bluetoothUart.c @ c9c97bb3
History | View | Annotate | Download (11.446 KB)
1 | 69661903 | Thomas Schöpping | /************************************************************************************//** |
---|---|---|---|
2 | * \file Source\ARMCM3_STM32\bluetoothUart.c
|
||
3 | * \brief Bootloader BLUETOOTH UART communication interface source file.
|
||
4 | * \ingroup Target_ARMCM3_STM32
|
||
5 | * \internal
|
||
6 | *----------------------------------------------------------------------------------------
|
||
7 | * C O P Y R I G H T
|
||
8 | *----------------------------------------------------------------------------------------
|
||
9 | * Copyright (c) 2011 by Feaser http://www.feaser.com All rights reserved
|
||
10 | *
|
||
11 | *----------------------------------------------------------------------------------------
|
||
12 | * L I C E N S E
|
||
13 | *----------------------------------------------------------------------------------------
|
||
14 | * This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
|
||
15 | * modify it under the terms of the GNU General Public License as published by the Free
|
||
16 | * Software Foundation, either version 3 of the License, or (at your option) any later
|
||
17 | * version.
|
||
18 | *
|
||
19 | * OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||
20 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||
21 | * PURPOSE. See the GNU General Public License for more details.
|
||
22 | *
|
||
23 | * You should have received a copy of the GNU General Public License along with OpenBLT.
|
||
24 | * If not, see <http://www.gnu.org/licenses/>.
|
||
25 | *
|
||
26 | * A special exception to the GPL is included to allow you to distribute a combined work
|
||
27 | * that includes OpenBLT without being obliged to provide the source code for any
|
||
28 | * proprietary components. The exception text is included at the bottom of the license
|
||
29 | * file <license.html>.
|
||
30 | *
|
||
31 | * \endinternal
|
||
32 | ****************************************************************************************/
|
||
33 | |||
34 | /****************************************************************************************
|
||
35 | * Include files
|
||
36 | ****************************************************************************************/
|
||
37 | #include "boot.h" /* bootloader generic header */ |
||
38 | |||
39 | |||
40 | #if (BOOT_COM_BLUETOOTH_UART_ENABLE > 0 || BOOT_GATE_BLUETOOTH_UART_ENABLE > 0) |
||
41 | /****************************************************************************************
|
||
42 | * Type definitions
|
||
43 | ****************************************************************************************/
|
||
44 | /** \brief BLUETOOTH UART register layout. */
|
||
45 | typedef struct |
||
46 | { |
||
47 | volatile blt_int16u SR; /**< status register */ |
||
48 | blt_int16u RESERVED0; |
||
49 | volatile blt_int16u DR; /**< data register */ |
||
50 | blt_int16u RESERVED1; |
||
51 | volatile blt_int16u BRR; /**< baudrate register */ |
||
52 | blt_int16u RESERVED2; |
||
53 | volatile blt_int16u CR1; /**< control register 1 */ |
||
54 | blt_int16u RESERVED3; |
||
55 | volatile blt_int16u CR2; /**< control register 2 */ |
||
56 | blt_int16u RESERVED4; |
||
57 | volatile blt_int16u CR3; /**< control register 3 */ |
||
58 | blt_int16u RESERVED5; |
||
59 | volatile blt_int16u GTPR; /**< guard time and prescale reg. */ |
||
60 | blt_int16u RESERVED6; |
||
61 | } tBluetoothUartRegs; /**< UART register layout type */
|
||
62 | |||
63 | |||
64 | /****************************************************************************************
|
||
65 | * Macro definitions
|
||
66 | ****************************************************************************************/
|
||
67 | #if (BOOT_COM_UART_ENABLE == 0 && BOOT_GATE_UART_ENABLE == 0) |
||
68 | /** \brief USART enable bit. */
|
||
69 | #define UART_BIT_UE ((blt_int16u)0x2000) |
||
70 | /** \brief Transmitter enable bit. */
|
||
71 | #define UART_BIT_TE ((blt_int16u)0x0008) |
||
72 | /** \brief Receiver enable bit. */
|
||
73 | #define UART_BIT_RE ((blt_int16u)0x0004) |
||
74 | /** \brief Transmit data reg. empty bit. */
|
||
75 | #define UART_BIT_TXE ((blt_int16u)0x0080) |
||
76 | /** \brief Read data reg. not empty bit. */
|
||
77 | #define UART_BIT_RXNE ((blt_int16u)0x0020) |
||
78 | #endif /* (BOOT_COM_UART_ENABLE == 0 && BOOT_GATE_UART_ENABLE == 0) */ |
||
79 | |||
80 | |||
81 | /****************************************************************************************
|
||
82 | * Register definitions
|
||
83 | ****************************************************************************************/
|
||
84 | #if (BOOT_COM_BLUETOOTH_UART_CHANNEL_INDEX == 0) |
||
85 | /** \brief Set UART base address to USART1. */
|
||
86 | #define BLUETOOTH_UARTx ((tBluetoothUartRegs *) (blt_int32u)0x40013800) |
||
87 | #elif (BOOT_COM_BLUETOOTH_UART_CHANNEL_INDEX == 1) |
||
88 | /** \brief Set UART base address to USART2. */
|
||
89 | #define BLUETOOTH_UARTx ((tBluetoothUartRegs *) (blt_int32u)0x40004400) |
||
90 | #else
|
||
91 | /** \brief Set UART base address to USART1 by default. */
|
||
92 | #define BLUETOOTH_UARTx ((tBluetoothUartRegs *) (blt_int32u)0x40013800) |
||
93 | #endif
|
||
94 | |||
95 | |||
96 | /****************************************************************************************
|
||
97 | * Function prototypes
|
||
98 | ****************************************************************************************/
|
||
99 | static blt_bool BluetoothUartReceiveByte(blt_int8u *data);
|
||
100 | static blt_bool BluetoothUartTransmitByte(blt_int8u data);
|
||
101 | |||
102 | /************************************************************************************//** |
||
103 | ** \brief Initializes the BLUETOOTH UART communication interface.
|
||
104 | ** \return none.
|
||
105 | **
|
||
106 | ****************************************************************************************/
|
||
107 | void BluetoothUartInit(void) |
||
108 | { |
||
109 | /* the current implementation supports USART1 and USART2. throw an assertion error in
|
||
110 | * case a different UART channel is configured.
|
||
111 | */
|
||
112 | ASSERT_CT((BOOT_COM_BLUETOOTH_UART_CHANNEL_INDEX == 0) || (BOOT_COM_BLUETOOTH_UART_CHANNEL_INDEX == 1)); |
||
113 | /* first reset the UART configuration. note that this already configures the UART
|
||
114 | * for 1 stopbit, 8 databits and no parity.
|
||
115 | */
|
||
116 | BLUETOOTH_UARTx->BRR = 0;
|
||
117 | BLUETOOTH_UARTx->CR1 = 0;
|
||
118 | BLUETOOTH_UARTx->CR2 = 0;
|
||
119 | BLUETOOTH_UARTx->CR3 = 0;
|
||
120 | BLUETOOTH_UARTx->GTPR = 0;
|
||
121 | /* configure the baudrate, knowing that PCLKx is configured to be half of
|
||
122 | * BOOT_CPU_SYSTEM_SPEED_KHZ.
|
||
123 | */
|
||
124 | BLUETOOTH_UARTx->BRR = ((BOOT_CPU_SYSTEM_SPEED_KHZ/2)*(blt_int32u)1000)/BOOT_COM_UART_BAUDRATE; |
||
125 | /* enable the UART including the transmitter and the receiver */
|
||
126 | BLUETOOTH_UARTx->CR1 |= (UART_BIT_UE | UART_BIT_TE | UART_BIT_RE); |
||
127 | } /*** end of BluetoothUartInit ***/
|
||
128 | |||
129 | |||
130 | /************************************************************************************//** |
||
131 | ** \brief Transmits a packet formatted for the communication interface.
|
||
132 | ** \param data Pointer to byte array with data that it to be transmitted.
|
||
133 | ** \param len Number of bytes that are to be transmitted.
|
||
134 | ** \return none.
|
||
135 | **
|
||
136 | ****************************************************************************************/
|
||
137 | void BluetoothUartTransmitPacket(blt_int8u *data, blt_int8u len)
|
||
138 | { |
||
139 | blt_int16u data_index; |
||
140 | blt_bool result; |
||
141 | |||
142 | /* verify validity of the len-paramenter */
|
||
143 | ASSERT_RT(len <= BOOT_COM_UART_TX_MAX_DATA); |
||
144 | |||
145 | /* first transmit the length of the packet */
|
||
146 | result = BluetoothUartTransmitByte(len); |
||
147 | ASSERT_RT(result == BLT_TRUE); |
||
148 | |||
149 | /* transmit all the packet bytes one-by-one */
|
||
150 | for (data_index = 0; data_index < len; data_index++) |
||
151 | { |
||
152 | /* keep the watchdog happy */
|
||
153 | CopService(); |
||
154 | /* write byte */
|
||
155 | result = BluetoothUartTransmitByte(data[data_index]); |
||
156 | ASSERT_RT(result == BLT_TRUE); |
||
157 | } |
||
158 | } /*** end of BluetoothUartTransmitPacket ***/
|
||
159 | |||
160 | |||
161 | /************************************************************************************//** |
||
162 | ** \brief Receives a communication interface packet if one is present.
|
||
163 | ** \param data Pointer to byte array where the data is to be stored.
|
||
164 | ** \return Length of message (if the message is invalid, the length will be 0).
|
||
165 | **
|
||
166 | ****************************************************************************************/
|
||
167 | blt_int8u BluetoothUartReceivePacket(blt_int8u *data) |
||
168 | { |
||
169 | static blt_int8u xcpCtoReqPacket[BOOT_COM_UART_RX_MAX_DATA+1]; /* one extra for length */ |
||
170 | static blt_int8u xcpCtoRxLength;
|
||
171 | static blt_int8u xcpUartDataLength;
|
||
172 | static blt_bool xcpCtoRxInProgress = BLT_FALSE;
|
||
173 | |||
174 | /* start of cto packet received? */
|
||
175 | if (xcpCtoRxInProgress == BLT_FALSE)
|
||
176 | { |
||
177 | /* store the message length when received */
|
||
178 | if (BluetoothUartReceiveByte(&xcpCtoReqPacket[0]) == BLT_TRUE) |
||
179 | { |
||
180 | /* save message length */
|
||
181 | xcpUartDataLength = xcpCtoReqPacket[0];
|
||
182 | if (xcpCtoReqPacket[0] > 0) |
||
183 | { |
||
184 | /* indicate that a cto packet is being received */
|
||
185 | xcpCtoRxInProgress = BLT_TRUE; |
||
186 | /* reset packet data count */
|
||
187 | xcpCtoRxLength = 0;
|
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | else
|
||
192 | { |
||
193 | /* store the next packet byte */
|
||
194 | if (BluetoothUartReceiveByte(&xcpCtoReqPacket[xcpCtoRxLength+1]) == BLT_TRUE) |
||
195 | { |
||
196 | /* increment the packet data count */
|
||
197 | xcpCtoRxLength++; |
||
198 | |||
199 | /* check to see if the entire packet was received */
|
||
200 | if (xcpCtoRxLength == xcpCtoReqPacket[0]) |
||
201 | { |
||
202 | /* copy the packet data */
|
||
203 | CpuMemCopy((blt_int32u)data, (blt_int32u)&xcpCtoReqPacket[1], xcpCtoRxLength);
|
||
204 | /* done with cto packet reception */
|
||
205 | xcpCtoRxInProgress = BLT_FALSE; |
||
206 | |||
207 | /* packet reception complete */
|
||
208 | // return BLT_TRUE;
|
||
209 | return xcpUartDataLength;
|
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | /* packet reception not yet complete */
|
||
214 | // return BLT_FALSE;
|
||
215 | return 0; |
||
216 | } /*** end of BluetoothUartReceivePacket ***/
|
||
217 | |||
218 | |||
219 | /************************************************************************************//** |
||
220 | ** \brief Receives a communication interface byte if one is present.
|
||
221 | ** \param data Pointer to byte where the data is to be stored.
|
||
222 | ** \return BLT_TRUE if a byte was received, BLT_FALSE otherwise.
|
||
223 | **
|
||
224 | ****************************************************************************************/
|
||
225 | static blt_bool BluetoothUartReceiveByte(blt_int8u *data)
|
||
226 | { |
||
227 | /* check if a new byte was received by means of the RDR-bit */
|
||
228 | if((BLUETOOTH_UARTx->SR & UART_BIT_RXNE) != 0) |
||
229 | { |
||
230 | /* store the received byte */
|
||
231 | data[0] = BLUETOOTH_UARTx->DR;
|
||
232 | /* inform caller of the newly received byte */
|
||
233 | return BLT_TRUE;
|
||
234 | } |
||
235 | /* inform caller that no new data was received */
|
||
236 | return BLT_FALSE;
|
||
237 | } /*** end of BluetoothUartReceiveByte ***/
|
||
238 | |||
239 | |||
240 | /************************************************************************************//** |
||
241 | ** \brief Transmits a communication interface byte.
|
||
242 | ** \param data Value of byte that is to be transmitted.
|
||
243 | ** \return BLT_TRUE if the byte was transmitted, BLT_FALSE otherwise.
|
||
244 | **
|
||
245 | ****************************************************************************************/
|
||
246 | static blt_bool BluetoothUartTransmitByte(blt_int8u data)
|
||
247 | { |
||
248 | /* check if tx holding register can accept new data */
|
||
249 | if ((BLUETOOTH_UARTx->SR & UART_BIT_TXE) == 0) |
||
250 | { |
||
251 | /* UART not ready. should not happen */
|
||
252 | return BLT_FALSE;
|
||
253 | } |
||
254 | /* write byte to transmit holding register */
|
||
255 | BLUETOOTH_UARTx->DR = data; |
||
256 | /* wait for tx holding register to be empty */
|
||
257 | while((BLUETOOTH_UARTx->SR & UART_BIT_TXE) == 0) |
||
258 | { |
||
259 | /* keep the watchdog happy */
|
||
260 | CopService(); |
||
261 | } |
||
262 | /* byte transmitted */
|
||
263 | return BLT_TRUE;
|
||
264 | } /*** end of BluetoothUartTransmitByte ***/
|
||
265 | |||
266 | #endif /* BOOT_COM_BLUETOOTH_UART_ENABLE > 0 || BOOT_GATE_BLUETOOTH_UART_ENABLE > 0 */ |
||
267 | |||
268 | /*********************************** end of bluetoothUart.c *************************************/
|