Skip to content

Instantly share code, notes, and snippets.

@bradfa
Forked from 0/msp430-elf.md
Last active February 24, 2024 00:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradfa/bc3f0f60677cfaf497c83f6758d71564 to your computer and use it in GitHub Desktop.
Save bradfa/bc3f0f60677cfaf497c83f6758d71564 to your computer and use it in GitHub Desktop.
Building a GCC cross-compiler for the MSP430.

msp430-elf

The following are instructions for building a GCC cross-compiler for the MSP430. They are based in part on Peter Bigot's post to mspgcc-users and the gist this was forked from.

Setup

export PREFIX=/usr/local/msp430
export PATH="${PREFIX}/bin:${PATH}"

Make sure $PREFIX exists, is empty, and is writable by you. You can use any directory, it doesn't have to be /usr/local/msp430/.

Software

binutils

Get the latest binutils (currently 2.30) and extract it.

tar xf binutils-2.30.tar.bz2
cd binutils-2.30
./configure --prefix="${PREFIX}" --target=msp430-elf
make
make install
cd ..

gcc (host)

The latest gcc release as of this writing is 7.3.0. In order to build GCC you'll also need GMP, MPC, and MPFR libs installed with their header files. Install all these and build GCC for the host.

GCC 7 needs a little patching to fix up some internal compiler errors related to MSP430, use the below patch as directed.

sudo apt install libgmp-dev libmpc-dev libmpfr-dev
tar xf gcc-7.3.0.tar.xz
cd gcc-7.3.0
patch -Np1 -i gcc7-implement-complex-partial-integers.patch
mkdir gcc-build
cd gcc-build
../configure --prefix="${PREFIX}" --target=msp430-elf \
  --with-newlib --enable-languages=c,c++
make all-host
make install-host
cd ..

newlib

Get the latest newlib (currently 3.0.0) and extract it. Or just grab the git repo.

cd newlib-cygwin
./configure --prefix="${PREFIX}" --target=msp430-elf
make
make install
cd ..

gcc (target)

cd gcc-7.3.0/gcc-build
make all-target
make install-target
cd ../..

Support files

TI provides the "support files" necessary for building code for the MSP430: headers and linker scripts. These are distributed as part of MSP430-GCC-OPENSOURCE and can be found on the download page (at least for version 3_02_03_00) as msp430-gcc-support-files.zip.

cd msp430-gcc-support-files
cp *.h "${PREFIX}/msp430-elf/include"
cp *.ld "${PREFIX}/msp430-elf/lib"
cd ..

Test

Now let's make sure it works with the customary blinkenlights. We assume that mspdebug is already installed. You may need to tweak the details for your specific chip model.

Put the following in test.c:

#include <msp430.h>

void __attribute__((interrupt(TIMERA1_VECTOR))) blink(void) {
	TACCTL1 &= ~CCIFG;

	P1OUT ^= BIT0 | BIT6;
}

void main(void) {
	WDTCTL = WDTPW | WDTHOLD;

	P1DIR |= BIT0 | BIT6;
	P1OUT ^= BIT0;

	BCSCTL3 |= LFXT1S_2;
	BCSCTL1 |= DIVA_1;

	TACCR0 = 1200;
	TACTL = TASSEL_1 | MC_1;
	TACCTL1 = CCIE | OUTMOD_3;

	__bis_SR_register(LPM3_bits | GIE);
}

Build and run it with:

msp430-elf-gcc -mmcu=msp430g2211 -specs=nosys.specs -o test.elf test.c
mspdebug rf2500 'prog test.elf'

Extras

gdb

Get the latest gdb (currently 7.9) and extract it.

cd gdb-7.9
./configure --prefix="${PREFIX}" --target=msp430-elf
make
make install
cd ..

In mspdebug, run gdb 1234. In msp430-elf-gdb, run target remote :1234.

From 31d8554ebb6afeb2d8f235cf3d3c262236aa5e32 Mon Sep 17 00:00:00 2001
From: Jozef Lawrynowicz <jozefl.gcc@gmail.com>
Date: Fri, 12 Jan 2018 13:23:40 +0000
Subject: [PATCH] Add support for Complex Partial Integers - CPSImode
2018-01-XX Jozef Lawrynowicz <jozefl.gcc@gmail.com>
gcc/
PR target/79242
* machmode.def: Define a complex mode for PARTIAL_INT.
* genmodes.c (complex_class): Return MODE_COMPLEX_INT for
MODE_PARTIAL_INT.
* doc/rtl.texi: Document CSPImode.
* config/msp430/msp430.c (msp430_hard_regno_nregs): Add CPSImode
handling.
(msp430_hard_regno_nregs_with_padding): Likewise.
gcc/testsuite/
PR target/79242
* gcc.target/msp430/pr79242.c: New test.
---
gcc/config/msp430/msp430.c | 4 ++++
gcc/doc/rtl.texi | 5 +++--
gcc/genmodes.c | 1 +
gcc/machmode.def | 1 +
gcc/testsuite/gcc.target/msp430/pr79242.c | 11 +++++++++++
5 files changed, 20 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/gcc.target/msp430/pr79242.c
diff --git a/gcc/config/msp430/msp430.c b/gcc/config/msp430/msp430.c
index 710a97b..c1f0d5b 100644
--- a/gcc/config/msp430/msp430.c
+++ b/gcc/config/msp430/msp430.c
@@ -905,6 +905,8 @@ msp430_hard_regno_nregs (int regno ATTRIBUTE_UNUSED,
{
if (mode == PSImode && msp430x)
return 1;
+ if (mode == CPSImode && msp430x)
+ return 2;
return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1)
/ UNITS_PER_WORD);
}
@@ -927,6 +929,8 @@ msp430_hard_regno_nregs_with_padding (int regno ATTRIBUTE_UNUSED,
{
if (mode == PSImode)
return 2;
+ if (mode == CPSImode)
+ return 4;
return msp430_hard_regno_nregs (regno, mode);
}
diff --git a/gcc/doc/rtl.texi b/gcc/doc/rtl.texi
index b02e5a1..ebe2a63 100644
--- a/gcc/doc/rtl.texi
+++ b/gcc/doc/rtl.texi
@@ -1291,10 +1291,11 @@ point values. The floating point values are in @code{QFmode},
@findex CDImode
@findex CTImode
@findex COImode
-@item CQImode, CHImode, CSImode, CDImode, CTImode, COImode
+@findex CPSImode
+@item CQImode, CHImode, CSImode, CDImode, CTImode, COImode, CPSImode
These modes stand for a complex number represented as a pair of integer
values. The integer values are in @code{QImode}, @code{HImode},
-@code{SImode}, @code{DImode}, @code{TImode}, and @code{OImode},
+@code{SImode}, @code{DImode}, @code{TImode}, @code{OImode}, and @code{PSImode},
respectively.
@findex BND32mode
diff --git a/gcc/genmodes.c b/gcc/genmodes.c
index e56c08b..2af6556 100644
--- a/gcc/genmodes.c
+++ b/gcc/genmodes.c
@@ -116,6 +116,7 @@ complex_class (enum mode_class c)
switch (c)
{
case MODE_INT: return MODE_COMPLEX_INT;
+ case MODE_PARTIAL_INT: return MODE_COMPLEX_INT;
case MODE_FLOAT: return MODE_COMPLEX_FLOAT;
default:
error ("no complex class for class %s", mode_class_names[c]);
diff --git a/gcc/machmode.def b/gcc/machmode.def
index afe6851..6c84488 100644
--- a/gcc/machmode.def
+++ b/gcc/machmode.def
@@ -243,6 +243,7 @@ UACCUM_MODE (UTA, 16, 64, 64); /* 64.64 */
/* Complex modes. */
COMPLEX_MODES (INT);
+COMPLEX_MODES (PARTIAL_INT);
COMPLEX_MODES (FLOAT);
/* Decimal floating point modes. */
diff --git a/gcc/testsuite/gcc.target/msp430/pr79242.c b/gcc/testsuite/gcc.target/msp430/pr79242.c
new file mode 100644
index 0000000..d7ff8d3
--- /dev/null
+++ b/gcc/testsuite/gcc.target/msp430/pr79242.c
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-skip-if "" { "*-*-*" } { "-mcpu=msp430" "-msmall" } { "" } } */
+/* { dg-options "-mcpu=msp430x" } */
+
+typedef _Complex __int20 C;
+
+C
+foo (C x, C y)
+{
+ return x + y;
+}
--
2.7.4
@bradfa
Copy link
Author

bradfa commented Jan 30, 2018

So in TI's official GCC 6 releases, they patch GCC to go find out information about each different MSP430 part in a CSV file instead of having GCC know how to deal with things compiled into itself. This CSV file has information like CPU_TYPE and MPY_TYPE and errata (CPU_Bugs in the code). How should this be handled? Putting it in a file outside of GCC itself seems somewhat reasonable but that's hard to deal with, too...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment