I got a weird problem last night. Here is a simple program that uses pin PA7 to drive a LED:
#include <stdint.h>
#include <stdbool.h>
#include "driverlib/sysctl.h"
#include "inc/tm4c123gh6pm.h"
void PortA_Init(void){
volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000001;
delay = SYSCTL_RCGC2_R;
GPIO_PORTA_LOCK_R = 0x4C4F434B; // 2) unlock PortA
GPIO_PORTA_CR_R = 0x80; // allow changes to PA7
GPIO_PORTA_AMSEL_R = 0x00; // 3) disable analog function
GPIO_PORTA_PCTL_R = 0x00000000; // 4) GPIO clear bit PCTL
GPIO_PORTA_DIR_R |= 0x80; // 5) PA7 output
GPIO_PORTA_AFSEL_R = 0x00; // 6) no alternate function
GPIO_PORTA_DEN_R |= 0x80; // 7) enable digital pins PA7
}
int main(void) {
PortA_Init();
while(1) {
GPIO_PORTA_DATA_R |= 0x80;
SysCtlDelay(500000);
GPIO_PORTA_DATA_R &= ~0x80;
SysCtlDelay(500000);
}
return 0;
}
When debugging in simulator (and real hardware), it is stuck in hardfault_handler section of startup.s
. I Googled for a while and found out that I can check address 0xe000ed2s for additional bits:
From what I understand, 0x08 means that I have a NOCP error, according to this tutorial. However, I'm NOT using the FPU so I don't get where the error comes from:
I did some further tests and found something interesting. You can see that I'm using some header files under /driverlib
. I also added the driverlib.lib
file into the project, as shown in the pic below. However, this hardfault error is gone whenever I remove the #include "driverlib/sysctl.h
line (as well as the SysCtlDelay()
calls) from the source code. But I don't know why and how to fix. I still want to use this header file for further development.
Thanks in advance~~