瀏覽代碼

Merge branch 'new/systick-interrupt/7'

DricomDragon 5 年之前
父節點
當前提交
cadbbd13a8
共有 3 個文件被更改,包括 45 次插入42 次删除
  1. 10 38
      prog/sources/setup-loop.cpp
  2. 25 4
      prog/sources/time.cpp
  3. 10 0
      prog/sources/time.h

+ 10 - 38
prog/sources/setup-loop.cpp

@@ -38,46 +38,18 @@ void setup (USER_MODE) {
 	}
 
 	// Init LCD
-	printString(MODE_ "Welcome Jovian");
+	printString(MODE_ "Wake up in ");
+	printUnsigned(MODE_ millis(MODE));
+	printString(MODE_ "ms");
+
+	gotoLineColumn(MODE_ 1, 0);
 }
 
 void loop (USER_MODE) {
+	busyWaitUntil(MODE_ counter * 1000);
+	counter++;
 
-	// Shift bits
-	for (unsigned int i(0); i < 5; i++) {
-		antiBlinkBuffer[i] <<= 1;
-
-		if (digitalRead(BUTTON[i]))
-			antiBlinkBuffer[i] |= 0x01;
-
-		// Command leds with buttons
-		if (antiBlinkBuffer[i] == bufferEmpty) {
-			buttonReleased[i] = false;
-		}
-		else if (antiBlinkBuffer[i] == bufferFull) {
-			buttonReleased[i] = true;
-			actionDone[i] = false;
-		}
-
-		if (!buttonReleased[i] && !actionDone[i]) {
-			digitalToggle(LED[i]);
-			actionDone[i] = true;
-			counter ++;
-			
-			// Prevent persistent digit
-			if (counter >= 20) {
-				counter = 0;
-				gotoLineColumn(MODE_ 2, 12);
-				printChar(MODE_ ' ');
-			}
-
-			// Show action on LCD
-			gotoLineColumn(MODE_ 1, 13 - i);
-			printChar(MODE_ '1');
-
-			gotoLineColumn(MODE_ 2, 13 - counter / 10);
-			printUnsigned(MODE_ counter);
-		}
-	}
+	gotoLineColumn(MODE_ 1, 0);
+	printString(MODE_ "Date:");
+	printUnsigned(MODE_ millis(MODE));
 }
-

+ 25 - 4
prog/sources/time.cpp

@@ -1,5 +1,7 @@
 #include "all-headers.h"
 
+static volatile uint32_t gUptime;
+
 static void startSystick (BOOT_MODE) {
 	SYST_RVR = CPU_MHZ * 1000 - 1 ; // Underflow every ms
 	SYST_CVR = 0 ;
@@ -8,6 +10,12 @@ static void startSystick (BOOT_MODE) {
 
 MACRO_BOOT_ROUTINE (startSystick);
 
+static void activateSystickInterrupt (INIT_MODE) {
+	SYST_CSR |= SYST_CSR_TICKINT;
+}
+
+MACRO_INIT_ROUTINE (activateSystickInterrupt);
+
 void busyWaitDuring_initMode (INIT_MODE_ const uint32_t inDelayMS) {
 	const uint32_t COUNTFLAG_MASK = 1 << 16 ;
 	for (uint32_t i=0 ; i<inDelayMS ; i++) {
@@ -16,8 +24,21 @@ void busyWaitDuring_initMode (INIT_MODE_ const uint32_t inDelayMS) {
 }
 
 void busyWaitDuring (USER_MODE_ const uint32_t inDelayMS) {
-	const uint32_t COUNTFLAG_MASK = 1 << 16 ;
-	for (uint32_t i = 0; i < inDelayMS; i++) {
-		while ((SYST_CSR & COUNTFLAG_MASK) == 0) {} // Busy wait, polling COUNTFLAG
-	}
+	busyWaitUntil(MODE_ gUptime + inDelayMS);
+}
+
+void busyWaitUntil (USER_MODE_ const uint32_t inDeadLineMS) {
+	while (inDeadLineMS > gUptime) {}
+}
+
+void systickInterruptServiceRoutine (SECTION_MODE) {
+	gUptime += 1;
+}
+
+uint32_t millis(ANY_MODE) {
+	return gUptime;
+}
+
+uint32_t systick(ANY_MODE) {
+	return SYST_CVR;
 }

+ 10 - 0
prog/sources/time.h

@@ -6,3 +6,13 @@ void busyWaitDuring_initMode (INIT_MODE_ const uint32_t inDelayMS);
 
 void busyWaitDuring (USER_MODE_ const uint32_t inDelayMS);
 
+void busyWaitUntil (USER_MODE_ const uint32_t inDeadLineMS);
+
+// Prevent default SysTick interruption code generation
+//$interrupt-section SysTick
+
+void systickInterruptServiceRoutine (SECTION_MODE) asm ("interrupt.section.SysTick");
+
+uint32_t millis(ANY_MODE);
+
+uint32_t systick(ANY_MODE);