Jump to content
43oh

Connecting to MSP430G2553 serial using Ubuntu


Recommended Posts

I've adapted some serial code from this forum to RX and TX over serial. It works on Windows, but I can't get it to work on Ubuntu. I have probably done something really silly, as I'm not used to using serial on Linux. I've tried with minicom and picocom. I can see the stuff being sent from the device, but the device isn't picking up what I'm sending to it. Any ideas? Permissions? CR/LF? HW-CTRL?

 

Edit: I found the reason. I should have been looking for /r and not /n... Works now.

 

// Based on Nathan Zimmerman's code at http://www.43oh.com/forum/viewtopic.php?f=10&p=15345#p15345
// Adapted and added to by Lars Roland
// For USCI (MSP430G2553 etc)

//#include 
#include 
#include 
#include "uart.h"

#define interrupt(x) void __attribute__((interrupt (x)))

unsigned char rx_buffer[102];
int len;

void uartInit(void)
{

   BCSCTL1 = CALBC1_1MHZ;            // Set DCO to 1MHz
   DCOCTL = CALDCO_1MHZ;   

   len = 0;

   ////////////////USCI setup////////////////

   P1SEL = BIT1 + BIT2;            // Set P1.1 to RXD and P1.2 to TXD
   P1SEL2 = BIT1 + BIT2;            //

   //P1DIR |= BIT0 | BIT6;	// Set P1.0 and P1.6 as output
   //P1OUT |= BIT0; // Toggle P1.0
   //P1OUT |= BIT6; // Toggle P1.6

   UCA0CTL1 |= UCSSEL_2;         // Have USCI use SMCLK AKA 1MHz main CLK
   UCA0BR0 = 104;                // Baud: 9600, N= CLK/Baud, N= 10^6 / 9600
   UCA0BR1 = 0;                  // Set upper half of baud select to 0 
   UCA0MCTL = UCBRS_1;           // Modulation UCBRSx = 1
   UCA0CTL1 &= ~UCSWRST;         // Start USCI

   IE2 |= UCA0RXIE;              //Enable RX interrupt

   _BIS_SR(GIE);                 // Interrupt

} 

void putchar(unsigned char c)
{
   UCA0TXBUF = c; // write c to TX buffer
   while(!(IFG2&UCA0TXIFG));
   //P1OUT ^= BIT6; // Toggle P1.6
}

void putstring(unsigned char *tx_message)
{
   unsigned int i = 0;
   while (tx_message[i] != '\0') {
   	putchar(tx_message[i++]);
   	if (i > 100) {
   	    break;
   	}
   }
}


int getString(unsigned char *message)
{
   for (int i = 0; i < len; i++) {
   	message[i] = rx_buffer[i];
   }
   int length = len;
   len = 0;
   return length;
}

interrupt(USCIAB0RX_VECTOR) USCI0RX_ISR(void)
{
  P1OUT ^= BIT0; // Toggle P1.0
  unsigned char ch = UCA0RXBUF;
  UCB0TXBUF = ch;
  if (ch == '\r') {
  	// Ignore
  }
  else if (ch == '\n') {
  	rx_buffer[len++] = '\r';
  	rx_buffer[len++] = '\n';
  	rx_buffer[len++] = '\0';
  	__bic_SR_register_on_exit(CPUOFF);
  }
  else if (len < 100) 
  {
  	rx_buffer[len++] = ch;
  }
}

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...