2 * Copyright (c) 2012, Alexander I. Mykyta
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 /*==================================================================================================
29 * Alex M. 11/15/2010 born
31 *=================================================================================================*/
34 * \addtogroup MOD_UART
40 * \brief Code for \ref MOD_UART "UART IO"
41 * \author Alex Mykyta (amykyta3@gmail.com)
50 #include "uart_io_internal.h"
54 #if (UIO_USE_INTERRUPTS == 1)
55 char rxbuf
[UIO_RXBUF_SIZE
];
57 char txbuf
[UIO_TXBUF_SIZE
];
64 ///\addtogroup UART_FUNCTIONS
67 //==================================================================================================
68 // Hardware Abstraction Layer
69 //==================================================================================================
71 * \brief Initializes the UART controller
74 * \attention The initialization routine does \e not setup the IO ports!
77 #if defined(__MSP430_HAS_USCI__) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
78 UIO_CTL1
|= UCSWRST
; // soft reset
80 UIO_CTL1
= (UIO_CLK_SRC
*64)+UCSWRST
;
81 UIO_BR0
= UIO_BR0_DEFAULT
;
82 UIO_BR1
= UIO_BR1_DEFAULT
;
83 UIO_MCTL
= UIO_MCTL_DEFAULT
;
87 #if (UIO_USE_INTERRUPTS == 1)
88 fifo_init(&RXFIFO
,rxbuf
,UIO_RXBUF_SIZE
);
89 fifo_init(&TXFIFO
,txbuf
,UIO_TXBUF_SIZE
);
91 #if (UIO_ISR_SPLIT == 0)
98 #elif defined(__MSP430_HAS_UCA__) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
99 #error "Hardware Abstraction for UCA not written yet!"
103 //--------------------------------------------------------------------------------------------------
105 * \brief Gets a character from the inuart_put stream
107 * \retval uint8_t Character
109 uint16_t uart_getc_nonblocking(void){
110 #if defined(__MSP430_HAS_USCI__) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111 #if (UIO_USE_INTERRUPTS == 1)
113 if(fifo_rdcount(&RXFIFO
) == 0){ // no char received
116 fifo_read(&RXFIFO
,&chr
,1);
120 if((UIO_IFG
& UCRXIFG
) == 0){ // no char received
127 #elif defined(__MSP430_HAS_UCA__) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
128 #error "Hardware Abstraction for UCA not written yet!"
132 //--------------------------------------------------------------------------------------------------
134 * \brief Gets a character from the inuart_put stream
135 * \details If a character is not immediately available, function will block until it receives one.
137 * \retval uint8_t Character
139 uint8_t uart_getc(void){
140 #if defined(__MSP430_HAS_USCI__) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
141 #if (UIO_USE_INTERRUPTS == 1)
143 while(fifo_rdcount(&RXFIFO
) == 0); // wait until char recieved
144 fifo_read(&RXFIFO
,&chr
,1);
147 while((UIO_IFG
& UCRXIFG
) == 0); // wait until char recieved
151 #elif defined(__MSP430_HAS_UCA__) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
152 #error "Hardware Abstraction for UCA not written yet!"
156 //--------------------------------------------------------------------------------------------------
158 * \brief Returns the number of characters immediately available for inuart_put
160 * \retval uint16_t Characters available
162 uint16_t incount(void){
163 #if defined(__MSP430_HAS_USCI__) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
164 #if (UIO_USE_INTERRUPTS == 1)
165 return(fifo_rdcount(&RXFIFO
));
167 if((UIO_IFG
& UCRXIFG
) != 0){
175 #elif defined(__MSP430_HAS_UCA__) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
176 #error "Hardware Abstraction for UCA not written yet!"
180 //--------------------------------------------------------------------------------------------------
182 * \brief Sends a character
183 * \param [in] c Character to be sent
186 void uart_putc(uint8_t c
){
187 #if defined(__MSP430_HAS_USCI__) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
188 #if (UIO_USE_INTERRUPTS == 1)
190 while(fifo_wrcount(&TXFIFO
) == 0); // wait until fifo has room for another
191 fifo_write(&TXFIFO
,&c
,1);
195 fifo_read(&TXFIFO
,&chr
,1);
197 #if (UIO_ISR_SPLIT == 0)
200 UIO_IE
|= UIO_UCATXIE
;
205 while((UIO_IFG
& UCTXIFG
) == 0); // wait until txbuf is empty
209 #elif defined(__MSP430_HAS_UCA__) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
210 #error "Hardware Abstraction for UCA not written yet!"
214 //--------------------------------------------------------------------------------------------------
217 #if defined(__MSP430_HAS_USCI__) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
218 #if (UIO_USE_INTERRUPTS == 1)
219 #if (UIO_ISR_SPLIT == 0)
220 // RX/TX Interrupt Service Routine
221 #pragma vector=UIO_ISR_VECTOR
222 __interrupt
void UIO_rxtxISR(void) {
224 uint16_t iv
= UIO_IV
;
229 fifo_write(&RXFIFO
,&chr
,1);
230 }else if(iv
== 0x04){
231 // Transmit Buffer Empty
232 if(fifo_read(&TXFIFO
,&chr
,1) == RES_OK
){
236 UIO_IE
&= ~UCTXIE
; // disable tx interrupt
241 // RX Interrupt Service Routine
242 #pragma vector=UIO_RXISR_VECTOR
243 __interrupt
void UIO_rxISR(void) {
246 fifo_write(&RXFIFO
,&chr
,1);
249 // TX Interrupt Service Routine
250 #pragma vector=UIO_TXISR_VECTOR
251 __interrupt
void UIO_txISR(void) {
253 if(fifo_read(&TXFIFO
,&chr
,1) == RES_OK
){
257 UIO_IE
&= ~UIO_UCATXIE
; // disable tx interrupt
263 #elif defined(__MSP430_HAS_UCA__) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
264 #error "Hardware Abstraction for UCA not written yet!"
269 //==================================================================================================
271 //==================================================================================================
273 * \brief Sends a string to the serial port
274 * \param [in] s Pointer to string to be sent
277 void uart_puts(char *s
) {
279 //if(*s == '\n') What the heck is this for?
285 //--------------------------------------------------------------------------------------------------
287 * \brief Reads in a string until a newline character ( \c \n)is received
288 * \details If an entire is not immediately available, function will block until it receives a
290 * \param [in] line Pointer to line buffer to be used. Buffer /e MUST be large enough to receive
292 * \return Pointer to the received string
294 char* uart_getline(char *line
){
298 // until we read a newline
299 while ((k
= uart_getc()) != '\n') {
303 // newline dropped and Null-terminating character added
306 // return original pointer
310 //--------------------------------------------------------------------------------------------------
312 * \brief Prints a HEX formatted string
313 * \param [in] value Value to be printed
314 * \param [in] places Number of digits to print. Use \c 0 to automatically determine how many digits.
317 void uart_putx(uint16_t value
, uint16_t places
){
322 // size places to fit number
325 while(((value
& bitmask
) == 0) && (places
> 0)){
327 bitmask
= bitmask
>> 4;
333 for(i
= ((places
-1)<<2); i
>= 0; i
-= 4) {
334 j
= (value
>> i
) & 0xf;
338 uart_putc('A' - 10 + j
);
342 //--------------------------------------------------------------------------------------------------
344 * \brief Prints an unsigned decimal formatted string
345 * \param [in] value Value to be printed
348 void uart_putd(uint16_t value
){
355 str
[n
]=(value
% 10) + '0';
365 //--------------------------------------------------------------------------------------------------
367 * \brief Prints an unsigned decimal formatted string (32 bit version)
368 * \param [in] value Value to be printed
371 void uart_putd32(uint32_t value
){
378 str
[n
]=(value
% 10) + '0';
387 //--------------------------------------------------------------------------------------------------
389 * \brief Prints a signed decimal formatted string
390 * \param [in] value Value to be printed
393 void uart_putsd(int16_t value
){
403 uvalue
= (uint16_t) value
;
407 str
[n
]=(uvalue
% 10) + '0';
409 } while(uvalue
!= 0);