diff --git a/Makefile b/Makefile index b80f19f..691ec00 100644 --- a/Makefile +++ b/Makefile @@ -21,8 +21,8 @@ # Description: Makefile used to build files for this program -# PROJECT: name of the output file -PROJECT = main +# MAIN: name of the output file +MAIN = main #DEV : your TM4C123GH6PM when connected to your system,usually will be /dev/ttyACM0 DEV = /dev/ttyACM0 # SRCS: all source files from src directory @@ -35,6 +35,8 @@ OBJS = $(addprefix $(OBJ),$(notdir $(SRCS:.c=.o))) #Flag points to the INC folder containing header files INC = -Iinc +MCU = TM4C123GH6PM + # LD_SCRIPT: linker script LD_SCRIPT = ld/$(MCU).ld @@ -49,40 +51,41 @@ MKDIR = @mkdir -p $(@D) #creates folders if not present #GCC FLAGS -CFLAGS = -ggdb -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -CFLAGS += -mfloat-abi=softfp -Os -MD -std=c99 -c +CFLAGS = -ggdb -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 +CFLAGS += -mfloat-abi=softfp -O1 -MD -std=c99 -c #LINKER FLAGS -LDFLAGS = -T $(LD_SCRIPT) -e Reset_Handler - +LDFLAGS = -T $(LD_SCRIPT) -e Reset_Handler # Rules to build bin -all: bin/$(PROJECT).bin +all: bin/$(MAIN).bin $(OBJ)%.o: src/%.c #turns .c source files into object files - $(MKDIR) + $(MKDIR) $(CC) -o $@ $^ $(INC) $(CFLAGS) $(OBJ)%.o: libs/%.c #turns .c source files into object files - $(MKDIR) + $(MKDIR) $(CC) -o $@ $^ $(INC) $(CFLAGS) - -bin/$(PROJECT).elf: $(OBJS) ##contains debug symbols for GNU GDB - $(MKDIR) + +bin/$(MAIN).elf: $(OBJS) ##contains debug symbols for GNU GDB + $(MKDIR) $(LD) -o $@ $^ $(LDFLAGS) -bin/$(PROJECT).bin: bin/$(PROJECT).elf #debug symbols for GNU GDB stripped by objcopy,finished binary ready for flashing +bin/$(MAIN).bin: bin/$(MAIN).elf #debug symbols for GNU GDB stripped by objcopy,finished binary ready for flashing $(OBJCOPY) -O binary $< $@ - #Flashes bin to TM4C -flash: - $(FLASHER) -S $(DEV) bin/$(PROJECT).bin +flash: all + $(FLASHER) -S $(DEV) bin/$(MAIN).bin #remove object and bin files clean: -$(RM) obj -$(RM) bin -.PHONY: all clean +re: clean all + + +.PHONY: all clean re diff --git a/README.md b/README.md index 46154fc..15e45bf 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,8 @@ - - - - -# TM4C Template for Linux - -**UPDATE** : Added new `startup.c` and `startup.h` files for the TM4C,and a linker script all written from scratch by me,and Open Source, -unlike the earlier TI BSD licensed `startup_gcc.c` file and associated linker script - +# TM4C Template for Linux +**UPDATE** : Added new `startup.c` and `startup.h` files for the TM4C, and a linker script all written from scratch by me, and Open Source, unlike the earlier TI BSD licensed `startup_gcc.c` file and associated linker script ## About - -This is a simple template for writing and building code for the TM4C123GH6PM Launchpad development board by TI on a Linux(specifically Ubuntu) system.This repository includes a `Makefile`,a linker script `TM4C123GH6PM.ld` for the TM4C board,the `Startup.c` file and required headers for the TM4C microcontroller and a basic blinky `main.c` file - - +This is a simple template for writing and building code for the TM4C123GH6PM Launchpad development board by TI on a Linux system. This repository includes a `Makefile`, a linker script `TM4C123GH6PM.ld` for the TM4C board, the `Startup.c` file and required headers for the TM4C microcontroller and a basic blinky `main.c` file. Directory structure after running `make` ``` @@ -21,13 +11,17 @@ Directory structure after running `make` │ ├── main.bin │ └── main.elf ├── inc +│ ├── gpio.h │ └── startup.h ├── ld │ └── TM4C123GH6PM.ld ├── libs +│ ├── gpio.c │ └── startup.c ├── Makefile ├── obj +│ ├── gpio.d +│ ├── gpio.o │ ├── main.d │ ├── main.o │ ├── startup.d @@ -36,17 +30,12 @@ Directory structure after running `make` └── src └── main.c ``` -Tested on Ubuntu 18.04 LTS - - +Tested on Ubuntu 18.04 LTS and Arch Linux with Linux kernel version 5.8.3-arch1-1. ## Toolchain setup and installation - - - You need these dependencies before anything else - + ``` sudo apt install flex bison libgmp3-dev libmpfr-dev libncurses5-dev \ @@ -54,18 +43,14 @@ libmpc-dev autoconf texinfo build-essential libftdi-dev python-yaml \ zlib1g-dev libusb-1.0-0-dev ``` - - You will also need to get the **GCC ARM compiler/linker** - - ```sudo apt install arm-none-eabi-gcc``` And a flasher like **lm4flash** - + ``` git clone https://github.com/utzig/lm4tools.git @@ -75,24 +60,21 @@ make sudo cp lm4flash /usr/bin/ ``` - ## Usage - - - - Clone this [tm4c-linux-template](https://github.com/shawn-dsilva/tm4c-linux-template) repository ```git clone https://github.com/shawn-dsilva/tm4c-linux-template.git``` - Create a file called `61.dialout.rules` in `/etc/udev/rules.d` with this line inside it `SUBSYSTEM=="usb", ATTRS{idVendor}=="1cbe", ATTRS{idProduct}=="00fd", MODE="0666"` -this is to allow any program to read or write to your TM4C Launchpad board,i.e you will not have to use `sudo` everytime + +This is to allow any program to read or write to your TM4C Launchpad board, i.e you will not have to use `sudo` everytime. - Header files(`.h`) to be placed in `inc` folder - C source files( `.c`) to be placed in `src` folder -- Library files for peripherals to go in the `libs` folder,these files are also C source files( `.c` ) +- Library files for peripherals to go in the `libs` folder, these files are also C source files( `.c` ) -- Set the `TARGET` variable in the Makefile to whatever you want the +- Set the `MAIN` variable in the Makefile to whatever you want the finished `.bin` to be named as. - Run `make` @@ -105,29 +87,25 @@ finished `.bin` to be named as. - Run `make clean` to delete all object and `.bin` files. - - - ## Debugging with OpenOCD and GDB - -- Download,Build and Install **OpenOCD** +- Download, Build and Install **OpenOCD** ``` git clone git://git.code.sf.net/p/openocd/code openocd.git cd openocd.git ./bootstrap -./configure --prefix=/usr --enable-maintainer-mode --enable-stlink +./configure --prefix=/usr --enable-maintainer-mode --enable-stlink --enable-ti-icdi make sudo make install ``` -- Install **GNU GDB** +- Install **GNU GDB** ```sudo apt install gdb-multiarch``` - Run this command to start OpenOCD with your board ``` -openocd -f /usr/share/openocd/scripts/board/ek-tm4c123gxl.cfg +openocd -f /usr/share/openocd/scripts/board/ek-tm4c123gxl.cfg ``` You should see this output @@ -136,7 +114,7 @@ Open On-Chip Debugger 0.10.0+dev-00554-g05e0d633 (2018-10-16-17:47) Licensed under GNU GPL v2 For bug reports, read http://openocd.org/doc/doxygen/bugs.html -Info : The selected transport took over low-level target control. +Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD adapter speed: 500 kHz Info : Listening on port 6666 for tcl connections @@ -161,7 +139,7 @@ you should see this UI >│49 if(PF4 == 0x0){ │ │50 Led = GPIO_PORTF_DATA_R; // read value in PORTF DATA register │ │51 Led = Led^0x02; // reverse value of LED │ - │52 GPIO_PORTF_DATA_R = Led; // write value to PORTF DATA register,toggle led │ + │52 GPIO_PORTF_DATA_R = Led; // write value to PORTF DATA register, toggle led │ │53 Delay(); │ │54 } │ │55 } │ @@ -180,7 +158,7 @@ you should see this UI │68 │ │69 │ └────────────────────────────────────────────────────────────────────────────────────────────────────┘ -remote Remote target In: main L49 PC: 0x2da +remote Remote target In: main L49 PC: 0x2da Type "apropos word" to search for commands related to "word". (gdb) target remote localhost:3333 Remote debugging using localhost:3333 @@ -193,7 +171,7 @@ Reading symbols from bin/main.elf...done. (gdb) target remote localhost:3333 Remote debugging using localhost:3333 0x000002da in main () at src/main.c:49 -(gdb) +(gdb) ``` - Now you can run various commands like `layout regs` to get the register layouts or `disass` to disassemble the code for example diff --git a/inc/gpio.h b/inc/gpio.h index 98dabd4..ded15c9 100644 --- a/inc/gpio.h +++ b/inc/gpio.h @@ -19,4 +19,5 @@ #define GPIO_PORTF_DR8R_R (*((volatile unsigned long *)0x40025508)) #define GPIO_LOCK_KEY 0x4C4F434B // Unlocks the GPIO_CR register #define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108)) + void PortF_Init(void); // prototype call diff --git a/libs/gpio.c b/libs/gpio.c index 692620c..82ef55b 100644 --- a/libs/gpio.c +++ b/libs/gpio.c @@ -1,5 +1,7 @@ -#include"gpio.h" -void PortF_Init(void){ volatile unsigned long delay; +#include "gpio.h" + +void PortF_Init(void){ + volatile unsigned long delay; SYSCTL_RCGC2_R |= 0x00000020; // 1) activate clock for Port F delay = SYSCTL_RCGC2_R; // allow time for clock to start GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock GPIO Port F diff --git a/src/main.c b/src/main.c index 98927ee..9d02f0f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,40 +1,22 @@ - //main.c //Toggles the Red LED of TM4C Launchpad when SW1(PF4) is pushed and held //PF4 is negative logic,i.e 0 is on and non-zero is off -#include"gpio.h" -#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC)) -#define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400)) -#define GPIO_PORTF_AFSEL_R (*((volatile unsigned long *)0x40025420)) -#define GPIO_PORTF_PUR_R (*((volatile unsigned long *)0x40025510)) -#define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C)) -#define GPIO_PORTF_LOCK_R (*((volatile unsigned long *)0x40025520)) -#define GPIO_PORTF_CR_R (*((volatile unsigned long *)0x40025524)) -#define GPIO_PORTF_AMSEL_R (*((volatile unsigned long *)0x40025528)) -#define GPIO_PORTF_PCTL_R (*((volatile unsigned long *)0x4002552C)) -#define PF4 (*((volatile unsigned long *)0x40025040)) -#define PF3 (*((volatile unsigned long *)0x40025020)) -#define PF2 (*((volatile unsigned long *)0x40025010)) -#define PF1 (*((volatile unsigned long *)0x40025008)) -#define PF0 (*((volatile unsigned long *)0x40025004)) -#define GPIO_PORTF_DR2R_R (*((volatile unsigned long *)0x40025500)) -#define GPIO_PORTF_DR4R_R (*((volatile unsigned long *)0x40025504)) -#define GPIO_PORTF_DR8R_R (*((volatile unsigned long *)0x40025508)) -#define GPIO_LOCK_KEY 0x4C4F434B // Unlocks the GPIO_CR register -#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108)) +#include "gpio.h" unsigned long Led; -void Delay(void){unsigned long volatile time; +void Delay(void) { + unsigned long volatile time; time = 800000; while(time){ time--; } } -int main(void){ + +int main(void) { PortF_Init(); while(1){ - //if(PF4 == 0x0){ + //if(PF4 == 0x0){ Led = 0x02; // reverse value of LED GPIO_PORTF_DATA_R = Led; // write value to PORTF DATA register,toggle led Delay(); @@ -47,7 +29,7 @@ int main(void){ /*Led = 0x0A; // reverse value of LED GPIO_PORTF_DATA_R = Led; // write value to PORTF DATA register,toggle led Delay();*/ - // } + // } } }