Hello, my name is Karol and I'm a brazilian computer engineering student. I'm not fluent, so I'm sorry about my english.
Following the Valvano's book as support, I decided to use SysTick to get accurate delays. I used an LED connected to PORT A2 to view the delay.
I chose to use the internal oscillator (16Mhz) with its frequency divided by 4 for my clock, it results in a 4 MHz frequency. Knowing this, my register NVIC_ST_CURRENT_R goes from value 4000000 to 0 so that they spend 1 second and the flag in bit 16 of the register NVIC_ST_CTRL_R become one.
Then comes the problem: during debug, as there are locking in breakpoints, I see my LED turning on and off. But in the Run Mode it is just on or off, as if the delay was not working. I could not detect the error in the code or logic. A help it would be possible? Here is my code:
#include <inttypes.h>
#include <stdint.h>
#include "tm4c123gh6pm.h" //Header file to registers address masks
/*
* Karoline Tyemi Fujimoto
* Using SysTick and Clock Control to get a precise delay
* The page numbers are datasheet references
*/
void setClock();
void setPortA();
void initSysTick();
void delay(unsigned long sec, unsigned long freq);
int main(void) {
unsigned long portA = 0;
setClock();
setPortA();
initSysTick();
while(1) {
portA = GPIO_PORTA_DATA_R&0x04;
GPIO_PORTA_DATA_R = (!portA) << 2;
delay(1,4000000);
}
return 0;
}
void setClock() {
SYSCTL_RCC_R = SYSCTL_RCC_MOSCDIS; //Disable main oscillator (external)
SYSCTL_RCC_R |= SYSCTL_RCC_OSCSRC_INT4; //Select the internal 16Mhz oscillator / 4 = 4Mhz
SYSCTL_RCC_R |= SYSCTL_RCC_BYPASS; //Select OSC like clock source
SYSCTL_RCC_R |= SYSCTL_RCC_PWRDN; //Turn off PLL
}
void setPortA() {
//System Control GPIO Run Mode Gating Control - Enable the GPIO PORTA's clock - Pag. 340
SYSCTL_RCGCGPIO_R |= 0x01;
//GPIO PORTA Lock Register - Unlock PA0 - Pag. 684
GPIO_PORTA_LOCK_R = 0x4C4F434B;
//GPIO PORTA Commit Register - Allow changes in PA2 - Pag. 685
GPIO_PORTA_CR_R = 0x04;
//GPIO PORTA Analog Mode Register - Turn off Analog Mode - Pag. 687
GPIO_PORTA_AMSEL_R = 0;
//GPIO PORTA Port Control - Set GPIO Funcion (AFSEL Complement) - Pag. 688
GPIO_PORTA_PCTL_R = 0;
//GPIO PORTA Data Direction - Set Pins 2 to output - Pag. 26
GPIO_PORTA_DIR_R |= 0x04;
//GPIO PORTF Alternate Function Select - Set the default pin operation - Pag. 602
GPIO_PORTA_AFSEL_R = 0;
//General Purpose PORTF Digital Enable - Pag. 682
GPIO_PORTA_DEN_R |= 0x04;
}
void initSysTick() {
//SysTick Control and Status Register - Enable SysTick with PIOSC/4 - Pag. 138
NVIC_ST_CTRL_R = 0x00;
}
void delay(unsigned long sec, unsigned long freq) {
unsigned long i, j = 0;
//SysTick Set Reload - Reload when have been passed 1 sec - Pag. 140
NVIC_ST_RELOAD_R = freq - 1;
//
for(i = 0;i < sec;i++) {
//SysTick Current - Reset the count register - Pag. 141
NVIC_ST_CURRENT_R = 0;
//Verify SysTick Control Register Flag - Pag. 138
while((NVIC_ST_CTRL_R&0x00010000)==0){
j = NVIC_ST_CURRENT_R;
}
}
}