Jump to content
Sign in to follow this  
Zander

MSP430G2231 complie error

Recommended Posts

I recently damaged my arudino trying to bridge an H-bridge project. So as a back up I went over to the MSP430 launch pad. I have had it for maybe 1 or 2 years but have not done anything with it since how complex it appears to write a code for the MSP430.

 

Then I heard of the energia and decided to take the following code from the arduino then to MSP430

 

 

int stby = P1_3;
int fwd = P1_4;
int rev = P1_5;
const int analogInPin = P1_0;
float voltage_forward = P1_2;
float voltage_reverse = P1_6;             //Controls the  speed level


// the setup routine runs once when you press reset:
void setup() 
{
  // initialize serial communication at 9600 bits per second:
 /* Serial.begin(9600);*/
  pinMode(voltage_forward, OUTPUT);         
  pinMode(voltage_reverse, OUTPUT);
  pinMode(fwd, OUTPUT);
  pinMode(rev, OUTPUT);
  pinMode(stby, OUTPUT);
}


// the loop routine runs over and over again forever:  
void loop() 
{


  float input_speed;               // the input speed
  float temp;                      //temp variable
  boolean sign;    


  input_speed= analogRead(analogInPin) * (5.0 / 1023.0);                 //Read the input from 0-5V
  temp=2*input_speed-5;                                         //Make range from -5V to 5V instead of 0V to 5V
  temp=(255.0 / 5.0) *temp;


  if(temp>127.5) 
  {
    analogWrite(voltage_forward,temp);
    digitalWrite(fwd,HIGH);
    digitalWrite(rev,LOW);
    digitalWrite(stby,LOW);
  }
  if(-127.4<=temp<=127.4) 
  {


    digitalWrite(voltage_reverse,LOW);
    delay(10);
    digitalWrite(voltage_forward,LOW);
    delay(10);
    digitalWrite(fwd,LOW);
    digitalWrite(stby,HIGH);
    digitalWrite(rev,LOW);
  }




  if(temp<-127.5) 
  {
    /*temp=-temp;*/
    analogWrite(voltage_reverse,temp); 
    digitalWrite(fwd,LOW);
    digitalWrite(stby,LOW);
    digitalWrite(rev,HIGH);
  }
  temp=temp * (5.0 / 255.0);






  //Calculate the value of voltage applied to the motor




  /*******    Print the values to the Serial Monitor    *******/
  /*
                        Serial.print(F("Potiemeter reading=  "));                   //PRINT ERROR AS A VOLTAGE
   Serial.print(input_speed);
   Serial.print(F("\tVoltage applied=  "));             //PRINT ERROR AS A VOLTAGE
   Serial.print(temp);
   Serial.print(F("\n"));
   delay(100);                           // delay in between reads for stability 
   */


}
 

I know there are still some bugs in it but what I wanted to check if it would compile and what I got was this error message:

 

 

c:/users/progra~1/docume~1/energi~1/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3/../../../../msp430/bin/ld.exe: Project_Ported_to_MSP.cpp.elf section `.text' will not fit in region `rom'
c:/users/progra~1/docume~1/energi~1/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3/../../../../msp430/bin/ld.exe: section .vectors loaded at [0000ffe0,0000ffff] overlaps section .text loaded at [0000f800,00010169]
c:/users/progra~1/docume~1/energi~1/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3/../../../../msp430/bin/ld.exe: region `rom' overflowed by 476 bytes
c:/users/progra~1/docume~1/energi~1/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3\libcrt0.a(_copy_data.o): In function `__do_copy_data':
/root/mspgcc-20120406/BUILD/gcc/gcc/../../../gcc-4.6.3/gcc/config/msp430/crt0.S:195: relocation truncated to fit: R_MSP430_16_BYTE against symbol `__data_load_start' defined in *ABS* section in Project_Ported_to_MSP.cpp.elf
c:/users/progra~1/docume~1/energi~1/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3/crt0ivtbl16.o: In function `__isr_14':
(.text+0x2): relocation truncated to fit: R_MSP430_16 against symbol `_unexpected_' defined in .text.crt0 section in c:/users/progra~1/docume~1/energi~1/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3\libcrt0.a(_unexpected_.o)
core.a(wiring_digital.c.o): In function `pinMode':
C:\Users\Programing\Documents\energia-0101E0009\hardware\msp430\cores\msp430/wiring_digital.c:38: relocation truncated to fit: R_MSP430_16_BYTE against symbol `digital_pin_to_bit_mask' defined in .rodata.digital_pin_to_bit_mask section in core.a(wiring_digital.c.o)
C:\Users\Programing\Documents\energia-0101E0009\hardware\msp430\cores\msp430/wiring_digital.c:39: relocation truncated to fit: R_MSP430_16_BYTE against symbol `digital_pin_to_port' defined in .rodata.digital_pin_to_port section in core.a(wiring_digital.c.o)
C:\Users\Programing\Documents\energia-0101E0009\hardware\msp430\cores\msp430/wiring_digital.c:47: relocation truncated to fit: R_MSP430_16_BYTE against symbol `port_to_dir' defined in .rodata.port_to_dir section in core.a(wiring_digital.c.o)
C:\Users\Programing\Documents\energia-0101E0009\hardware\msp430\cores\msp430/wiring_digital.c:48: relocation truncated to fit: R_MSP430_16_BYTE against symbol `port_to_ren' defined in .rodata.port_to_ren section in core.a(wiring_digital.c.o)
C:\Users\Programing\Documents\energia-0101E0009\hardware\msp430\cores\msp430/wiring_digital.c:49: relocation truncated to fit: R_MSP430_16_BYTE against symbol `port_to_output' defined in .rodata.port_to_output section in core.a(wiring_digital.c.o)
core.a(wiring_digital.c.o).debug_info+0x59c): relocation truncated to fit: R_MSP430_16_BYTE against symbol `digital_pin_to_port' defined in .rodata.digital_pin_to_port section in core.a(wiring_digital.c.o)
core.a(wiring_digital.c.o).debug_info+0x5b1): relocation truncated to fit: R_MSP430_16_BYTE against symbol `digital_pin_to_bit_mask' defined in .rodata.digital_pin_to_bit_mask section in core.a(wiring_digital.c.o)
core.a(wiring_digital.c.o).debug_info+0x625): relocation truncated to fit: R_MSP430_16_BYTE against symbol `port_to_output' defined in .rodata.port_to_output section in core.a(wiring_digital.c.o)
core.a(wiring_digital.c.o).debug_info+0x63a): additional relocation overflows omitted from the output
collect2: ld returned 1 exit status

I just have a funny feeling that I can't fit the code on to this chip because of the size of the flash memory.

 

Just a final note is that I have heard that the chip only has one counter, I have the external clock soldered on so is there a way to use that in addition to this so I also suspect this.

 

Share this post


Link to post
Share on other sites

I'm not an energia expert (or user) but if I am reading this correctly, the errors are mostly all related to your code being about 476 bytes too big. The g2231, the lovely thing, is quite small at this point. It only has 2kb of flash space.

 

I believe that there are no coding differences, as far as Energia is concerned, between the g2231 and the g2452 (same family, but bigger and includes capsense). If you just want to check to see if it compiles properly, choose the g2452 instead. It has 8kb of flash space, so size shouldn't be an issue for your sketch.

 

As for the counter, I believe you might be confusing Timer and Clock Source. They are not the same thing, but the Timer can use the external clock source (the crystal). Someone more familiar with Energia might want to chime in about that.

Share this post


Link to post
Share on other sites

I think I'll add another line to my signature: never use floating points in embedded systems.

Your calculations can always be done in fixed point, saving you a heap of code.

There is also a bug in your code.

if(-127.4<=temp<=127.4)
// will first evaluate -127.4<=temp, which becomes a boolean (either 0 or 1)
// then it will evaluate this boolean with <=127.4.
// since both 0 and 1 are smaller than 127.4 the result of the comparison is always true.

// this is what you want
if((-127.4<=temp) && (temp<=127.4))

Now the floats, lets' get them away!

float voltage_forward = P1_2;
float voltage_reverse = P1_6;             //Controls the  speed level

change this to int type. The pin enumeration itself is an int, even when you want to write sometin g float-like to it.

 

  input_speed= analogRead(analogInPin) * (5.0 / 1023.0);                 //Read the input from 0-5V
  temp=2*input_speed-5;                                         //Make range from -5V to 5V instead of 0V to 5V
  temp=(255.0 / 5.0) *temp;

Firstly, the MSP430 can read voltages up to 2.5 volts. If you want to measure 5V, first divide the voltage using a resistor/voltage divider.

Now I'm going to write these calculations into a single formula. I use output for temp (since it's the used result) and input for analogRead(analogInPin).

output = (255.0 / 5.0) * (2* (input * (5.0 / 1023.0))  - 5)               all in one formula
output = (255.0 / 5.0) * (2* input * (5.0 / 1023.0)  - 5)                 strip useless braces
output = (255.0 / 5.0) * (input * (10.0 / 1023.0)  - 5)                   group the 2* and the *5.0 to *10.0
output = (255.0 / 5.0) * (input * (10.0 / 1023.0))  - ((255.0 / 5.0) *5)  move the -5 constant te separate term
output = (255.0 / 5.0) * (input * (10.0 / 1023.0))  - 255.0               resolve the constant term
output = (255.0 / 5.0) * input * (10.0 / 1023.0)  - 255.0                 remove useless braces again
output = 255.0 * input * 10.0 / (5.0 * 1023.0)  - 255.0                   move the divisors to the end
output = input * 2550.0 / (5.0 * 1023.0)  - 255.0                         group the 255.0* and *10.0
output = input * 2550.0 / 5115.0 - 255.0                                  group the 5.0* and *1023.0
output = input * 0.498,533,724,340 - 255.0                                resolve the fraction

Now there is a single multiplication and a single subtraction in floating point. On this point we start approximating. 0.498.... is very close to 0.5 or divide by 2. In integer world dividing by any power of 2 (where 2 is 21) this can be replaced by a shift (on unsigned integers only).

output = (input >> 1) - 255

Since this calcuation does not require floating point, let's replace them all with unsigned integers.

This will save you a few hundred bytes of code space.

 

But how good is it? Let's calculate the worst case scenario and see how much we're off.

So, we take for input the value 1023.

We resolve the original formula

255.0 = (255.0 / 5.0) * (2* (1023.0 * (5.0 / 1023.0))  - 5)

Now we resolve the integer formula

256 = (1023 >> 1) - 255

Ohh... there's a problem! The value 256 does not fit in the range of the analogWrite parameter.

So how do we solve this? Since you scale a even number of values (0 to 1023, 1024 values) to a range which is both positive and negative, this issue cannot be solved in a clean way; the number of values below zero and above zero can never be equal.

Let's look at the rest of the code:

if(temp>127.5) 
  {
    analogWrite(voltage_forward, temp);
  }
  if((-127.4<=temp)&&(temp<=127.4))
  {
    digitalWrite(voltage_reverse, LOW);
    digitalWrite(voltage_forward, LOW);
  }

  if(temp<-127.5) 
{
    analogWrite(voltage_reverse, -temp); 
}

So what you're saying is that for a read value in the first quarter (0..255 : -255.0..-127.5) the reverse voltage should be actuated , the mid half should disable the driver and the last quarter (768..1023 : 127.5..255) the forward voltage should be actuated. Let's rewrite this code with less calculations:

unsigned int temp = analogRead(analogInPin);
if(temp>=768) 
  {
    analogWrite(voltage_forward, (temp >> 1) - 256);
  }
  if((256<=temp)&&(temp<768))
  {
    digitalWrite(voltage_reverse, LOW);
    digitalWrite(voltage_forward, LOW);
  }

  if(temp<256) 
{
    analogWrite(voltage_reverse, 255 - (temp >> 1)); 
}

Oh, this might be somewhat quick. Let me explain

If ever the read value is between 0 and 255 inclusive, also written as [0,256) which means 0 inlcusive until 256 exclusive, the voltage_reverse is the input divided by 2 rounded down and subtracted from 255. So 0 would become 255 and 255 would become 128.

If ever the read value is in [768, 1024) the voltage_forward is the input divided by 2 transposed to 0. So 768 would become 128 and 1023 would become 255.

If ever the read value is in [256, 768), this is exactly all other values, both voltages are written low.

 

You see I don't even need to multiply or divide. I only need to "scale" the read value to half of it, which can be done using shift. Then I can do integer subtractions to get the right value.

Share this post


Link to post
Share on other sites
/*
  
 */
int analogInPin = P1_0;
int voltage_forward = P1_2;
int voltage_reverse = P1_6;             //Controls the  speed level


// the setup routine runs once when you press reset:
void setup() 
{
  // initialize serial communication at 9600 bits per second:
 /* Serial.begin(9600);*/
  pinMode(voltage_forward, OUTPUT);         
  pinMode(voltage_reverse, OUTPUT);
 }


// the loop routine runs over and over again forever:  
void loop() 
{


 /*int input_speed;               // the input speed
 int temp;                      //temp variable   


  input_speed= analogRead(analogInPin) * (5.0 / 1023.0);                 //Read the input from 0-5V
  temp=(input_speed >> 1) - 255;                                        //Make range from -5V to 5V instead of 0V to 5V
  */


  unsigned int temp = analogRead(analogInPin);
  if(temp>=768) 
  {
    analogWrite(voltage_forward, (temp>> 1) - 256);
  
  }
  if((256<=temp) && (temp<=768) )
  {


    digitalWrite(voltage_reverse,LOW);
    digitalWrite(voltage_forward,LOW);
    
  }




  if(temp<256) 
  {
    
    analogWrite(voltage_reverse, 255 - (temp>>1)); 
   
  }
}

I made some changes but I still have too much code. Because it compiles when I use a different  board...

 

If I switchback to the arduino board in the disable mode section I still see pulses.

Share this post


Link to post
Share on other sites

Change this part

  input_speed= analogRead(analogInPin) * (5.0 / 1023.0);                 //Read the input from 0-5V   temp=(input_speed >> 1) - 255;

to

temp=analogRead(analogPin);

Share this post


Link to post
Share on other sites

There is almost no change.

 

What's happening is I'm reading off a 10k potentiometer in voltage division configuration, high side is 5 volts low side is ground.

 

P1.6 is just 2 volts  under no load condition and only turns on or off when the potentiometer gets to a certain point. And the output is not a square way but what appears to be a capacitor quickly charging and then slowly discharging.

 

As for P1.2 even with the RXD jumper taken out nothing at all occurs I do see stray square ways but this appears to be serial communication.

 

Got it working.

 

Fix in bold.

 

 

int analogInPin = A4;
int voltage_forward = P1_2;
int voltage_reverse = P1_6;   //Controls the  speed level
int temp;
// the setup routine runs once when you press reset:
void setup() 
{
  // initialize serial communication at 9600 bits per second:
  /* Serial.begin(9600);*/
  pinMode(voltage_forward, OUTPUT);         
  pinMode(voltage_reverse, OUTPUT);
  analogReference(DEFAULT);
}


// the loop routine runs over and over again forever:  
void loop() 
{


  /*int input_speed;               // the input speed
   int temp;                      //temp variable   
   
   input_speed= analogRead(analogInPin) * (5.0 / 1023.0);                 //Read the input from 0-5V
   temp=(input_speed >> 1) - 255;  */  //Make range from -5V to 5V instead of 0V to 5V




   temp = analogRead(analogInPin);
  if(temp>=768) 
  {
    analogWrite(voltage_forward, (temp>> 1) - 256);


  }
  if((256<=temp) && (temp<=768) )
  {


    digitalWrite(voltage_reverse,LOW);
    digitalWrite(voltage_forward,LOW);


  }




  if(temp<256) 
  {


    analogWrite(voltage_reverse, 255 - (temp>>1)); 


  }






}

Share this post


Link to post
Share on other sites

Ohhh.... I hadn't thought about the jumpers. P1.6 is connected to an LED, remove that jumper too.

Just to be sure, remove both the TxD and RxD jumpers. On the Launchpad 1.5 the pins are diagonally opposed, so pin 1 and 4 of the jumer block are connected to the same device.

Share this post


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.

Sign in to follow this  

×
×
  • Create New...