Snippets

Brandon Nielsen MSP430F5529 LaunchPad 'Project0.1'

Updated by Brandon Nielsen

File Makefile Added

  • Ignore whitespace
  • Hide word diff
+#
+# Makefile for msp430
+#
+# 'make' builds everything
+# 'make clean' deletes everything except source files and Makefile
+# 'make install' builds everything, and programs the MSP430 using MSPFLASHER
+# You need to set TARGET, MCU and SOURCES for your project.
+# TARGET is the name of the executable file to be produced
+# $(TARGET).elf $(TARGET).hex and $(TARGET).txt nad $(TARGET).map are all generated.
+# The TXT file is used for BSL loading, the ELF can be used for JTAG use
+#
+TARGET     = project0
+MCU        = msp430f5529
+# MSP430Flasher name
+MSPFLASHER = MSP430Flasher
+# List all the source files here
+# eg if you have a source file foo.c then list it here
+SOURCES = main.c delay.c
+# Include are located in the Include directory
+INCLUDES = -IInclude
+# Add or subtract whatever MSPGCC flags you want. There are plenty more
+#######################################################################################
+CFLAGS   = -mmcu=$(MCU) -g -Os -Wall -Wunused $(INCLUDES)
+ASFLAGS  = -mmcu=$(MCU) -x assembler-with-cpp -Wa,-gstabs
+LDFLAGS  = -mmcu=$(MCU) -Wl,-Map=$(TARGET).map
+########################################################################################
+CC       = msp430-elf-gcc
+LD       = msp430-elf-ld
+AR       = msp430-elf-ar
+AS       = msp430-elf-gcc
+NM       = msp430-elf-nm
+OBJCOPY  = msp430-elf-objcopy
+RANLIB   = msp430-elf-ranlib
+STRIP    = msp430-elf-strip
+SIZE     = msp430-elf-size
+READELF  = msp430-elf-readelf
+MAKETXT  = srec_cat
+CP       = cp -p
+RM       = rm -f
+MV       = mv
+########################################################################################
+# the file which will include dependencies
+DEPEND = $(SOURCES:.c=.d)
+# all the object files
+OBJECTS = $(SOURCES:.c=.o)
+all: $(TARGET).elf $(TARGET).hex $(TARGET).txt
+$(TARGET).elf: $(OBJECTS)
+	echo "Linking $@"
+	$(CC) $(OBJECTS) $(LDFLAGS) $(LIBS) -o $@
+	echo
+	echo ">>>> Size of Firmware <<<<"
+	$(SIZE) $(TARGET).elf
+	echo
+%.hex: %.elf
+	$(OBJCOPY) -O ihex $< $@
+%.txt: %.hex
+	$(MAKETXT) -O $@ -TITXT $< -I
+	unix2dos $(TARGET).txt
+#  The above line is required for the DOS based TI BSL tool to be able to read the txt file generated from linux/unix systems.
+%.o: %.c
+	echo "Compiling $<"
+	$(CC) -c $(CFLAGS) -o $@ $<
+# rule for making assembler source listing, to see the code
+%.lst: %.c
+	$(CC) -c $(ASFLAGS) -Wa,-anlhd $< > $@
+# include the dependencies unless we're going to clean, then forget about them.
+ifneq ($(MAKECMDGOALS), clean)
+-include $(DEPEND)
+endif
+# dependencies file
+# includes also considered, since some of these are our own
+# (otherwise use -MM instead of -M)
+%.d: %.c
+	echo "Generating dependencies $@ from $<"
+	$(CC) -M ${CFLAGS} $< >$@
+.SILENT:
+.PHONY:	clean
+clean:
+	-$(RM) $(OBJECTS)
+	-$(RM) $(TARGET).*
+	-$(RM) $(SOURCES:.c=.lst)
+	-$(RM) $(DEPEND)
+install: $(TARGET).txt
+	  $(MSPFLASHER) -n $(MCU) -w "$(TARGET).txt" -v -z [VCC]

File delay.c Added

  • Ignore whitespace
  • Hide word diff
+#include "delay.h"
+
+//Tell GCC to not optimize out the delay loop
+#pragma GCC push_options
+#pragma GCC optimize ("0")
+void delay(uint16_t delayCounts) {
+	uint16_t i = 0;
+
+	//Delay between LED toggles. This for-loop will run until the condition is met.
+	//In this case, it will loop delayCounts number of times
+	for(i=0; i < delayCounts; i++);
+}
+#pragma GCC pop_options

File delay.h Added

  • Ignore whitespace
  • Hide word diff
+#ifndef DELAY_H
+#define DELAY_H
+
+#include <stdint.h>
+
+void delay(uint16_t);
+
+#endif
Created by Brandon Nielsen

File main.c Added

  • Ignore whitespace
  • Hide word diff
+#include <msp430.h>
+#include <stdbool.h>
+#include "delay.h"
+
+int main(void) {
+	WDTCTL = WDTPW + WDTHOLD;
+	// Stop watchdog timer. This line of code is needed at the beginning of most MSP430 projects.
+	// This line of code turns off the watchdog timer, which can reset the device after a certain period of time.
+	P1DIR |= 0x01;
+	// P1DIR is a register that configures the direction (DIR) of a port pin as an output or an input.
+	// To set a specific pin as output or input, we write a '1' or '0' on the appropriate bit of the register.
+	// P1DIR = <PIN7><PIN6><PIN5><PIN4><PIN3><PIN2><PIN1><PIN0>
+	// Since we want to blink the on-board red LED, we want to set the direction of Port 1, Pin 0 (P1.0) as an output
+	// We do that by writing a 1 on the PIN0 bit of the P1DIR register
+	// P1DIR = <PIN7><PIN6><PIN5><PIN4><PIN3><PIN2><PIN1><PIN0>
+	// P1DIR = 0000 0001
+	// P1DIR = 0x01 <--this is the hexadecimal conversion of 0000 0001
+
+	while(true)
+	// Conditional is always true, so we loop forever
+	{
+		P1OUT ^= 0x01;
+		// Toggle P1.0 using exclusive-OR operation (^=)
+		// P1OUT is another register which holds the status of the LED.
+		// '1' specifies that it's ON or HIGH, while '0' specifies that it's OFF or LOW
+		// Since our LED is tied to P1.0, we will toggle the 0 bit of the P1OUT register
+		delay(20000);
+	}
+
+	return 0;
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.