Browse Source

16bit timebase so the read in delay is atomic

Ondřej Hruška 4 years ago
parent
commit
0736081111
2 changed files with 19 additions and 4 deletions
  1. 18 3
      User/bootstrap.c
  2. 1 1
      User/bootstrap.h

+ 18 - 3
User/bootstrap.c View File

@@ -6,7 +6,7 @@
6 6
 #include "bootstrap.h"
7 7
 
8 8
 /** Global time base */
9
-volatile uint32_t time_ms;
9
+volatile uint16_t time_ms;
10 10
 
11 11
 /**
12 12
  * Putchar for printf
@@ -61,8 +61,23 @@ INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
61 61
 /** Delay ms */
62 62
 void Delay(uint16_t ms)
63 63
 {
64
-	uint32_t start = time_ms;
65
-	while ((time_ms - start) < ms);
64
+	uint16_t start = time_ms;
65
+	uint16_t t2;
66
+	while (1) {
67
+		t2 = time_ms;
68
+		if ((t2 - start) >= ms) {
69
+			break;
70
+		}
71
+	}
72
+}
73
+
74
+/** Delay N seconds */
75
+void Delay_s(uint16_t s)
76
+{
77
+	while (s != 0) {
78
+		Delay(1000);
79
+		s--;
80
+	}
66 81
 }
67 82
 
68 83
 /**

+ 1 - 1
User/bootstrap.h View File

@@ -6,7 +6,7 @@
6 6
 #define STM8S_STDINIT_H
7 7
 
8 8
 /** Global timebase */
9
-extern volatile uint32_t time_ms;
9
+extern volatile uint16_t time_ms;
10 10
 
11 11
 /** Uart IRQ handler */
12 12
 void UART1_RX_IRQHandler(void) INTERRUPT(18);