diff delay.c @ 14:891841f5f785

Fix delay() to be accurate as measured on the cro.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 14 Nov 2012 12:24:21 +1030
parents 58d76cf522ff
children a38003b97de6
line wrap: on
line diff
--- a/delay.c	Wed Feb 08 10:37:22 2012 +1030
+++ b/delay.c	Wed Nov 14 12:24:21 2012 +1030
@@ -2,13 +2,82 @@
 #include "stm32f10x.h"
 #include "delay.h"
 
-/* This is a bit rough and ready */
+/* Sleep for nCount usec
+ * TDS1012 on 2.5usec/div shows...
+ *   30usec = 29.60usec 
+ *   60usec = 59.20usec 
+ *
+ * XXX: not sure disable IRQ stuff is working as I see occasional (small) extra delays
+ */
 void
-delay(__IO uint32_t nCount) {
-    __IO uint32_t i;
-    
-    for(; nCount != 0; nCount--)
-	for (i = 0; i < 3900; i++)
-	    ;
+delay(uint32_t nCount) {
+    __disable_irq();
+    for(; nCount != 0; nCount--) {
+#ifdef SYSCLK_FREQ_72MHz
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+	__asm__("nop");
+#else
+#error Unknown clock frequency
+#endif
+    }
+    __enable_irq();
 }