MSP430F5529/UART console

From Teknologisk videncenter
< MSP430F5529
Revision as of 07:13, 8 December 2022 by Heth (talk | contribs) (Created page with "The following code is a simple implementation on console function inplementing STDIN/STDOUT and STDERR on '''uca1''' on MSP430F5529 <source lang=c line> #include "io430....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The following code is a simple implementation on console function inplementing STDIN/STDOUT and STDERR on uca1 on MSP430F5529


  1 #include "io430.h"
  2 #include <stddef.h>
  3 
  4 
  5 void uart_init( ) {
  6 
  7   P4SEL = BIT4+BIT5;                        // P3.4,5 = USCI_A1 TXD/RXD
  8   UCA1CTL1 |= UCSWRST;                      // **Put state machine in reset**
  9   UCA1CTL1 |= UCSSEL_1;                     // CLK = ACLK
 10   UCA1BR0 = 0x03;                           // 32kHz/9600=3.41 (see User's Guide)
 11   UCA1BR1 = 0x00;                           //
 12   UCA1MCTL = UCBRS_3+UCBRF_0;               // Modulation UCBRSx=3, UCBRFx=0
 13   UCA1CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
 14    
 15 }
 16 
 17 //cin - Character in
 18 // Poll USCI_A0 receive register until character received.
 19 // Parameters: none
 20 // Returns: unsigned char - character received
 21 unsigned char cin( void ) {
 22       while ( !(0x01 & UCA1IFG) );
 23   return(UCA1RXBUF);
 24 }
 25 //cin_nonblocked
 26 //If character received - return it
 27 //Else return 0x00 - No character received
 28  unsigned char cin_nonblocked( void ) {
 29   if ( 0x01 & UCA1IFG ) {
 30     return(UCA1RXBUF);
 31   } else {
 32     return(0x00);
 33   }
 34 }
 35 
 36 //cou - Character out
 37 // Transmit character poll USCI_A0 transmit register until ready.
 38 // Parameters: Character to transmit
 39 // Returns: none
 40 int cout( unsigned char c ) {
 41   while ( !(0x02 & UCA1IFG) ); //&& (count++ > 0) );
 42   UCA1TXBUF = c;
 43   if (c == '\n')
 44     cout('\r');
 45   return(c);
 46 }
 47 
 48 
 49 /*
 50   THE DLIB LOW-LEVEL I/O INTERFACE
 51         - See IAR C-compiler manual
 52         - See skeleton functions in IAR installation ./430/src/lib/dlib
 53 */
 54 #include <yfuns.h>
 55 // __read() - DLIB-low level file i/o - defaults to console STDIN
 56 size_t __read(int handle, unsigned char * buffer, size_t size) {
 57   int nChars = 0;
 58 
 59   /* This template only reads from "standard in", for all other file
 60    * handles it returns failure. */
 61   if (handle != _LLIO_STDIN) {
 62     return _LLIO_ERROR;
 63   }
 64 
 65   for (/* Empty */; size > 0; --size) {
 66     int c = cin();
 67         if (c < 0)
 68       break;
 69 
 70     *buffer++ = c;
 71     ++nChars;
 72   }
 73 
 74   return nChars;
 75 }
 76 
 77 
 78 
 79 
 80 // __write() - DLIB-low level file i/o - defaults to console STDOUT and STDERR
 81 size_t __write(int handle, const unsigned char * buffer, size_t size) {
 82 
 83   size_t nChars = 0;
 84   
 85   if (buffer == 0) {
 86     /*
 87      * This means that we should flush internal buffers.  Since we
 88      * don't we just return.  (Remember, "handle" == -1 means that all
 89      * handles should be flushed.)
 90      */
 91     return 0;
 92   }
 93 
 94   /* This template only writes to "standard out" and "standard err",
 95    * for all other file handles it returns failure. */
 96   if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR) {
 97     return _LLIO_ERROR;
 98   }
 99   for (/* Empty */; size != 0; --size) {
100     if (cout(*buffer++) < 0) {
101       return _LLIO_ERROR;
102     }
103     ++nChars;
104   }
105   return nChars;
106 }