Example program for Port access. Blink the LED using the registers.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Ladon
Date:
Mon Jul 22 13:44:32 2019 +0000
Parent:
1:65c259961c26
Commit message:
Replaced #defines with reinterpret_cast.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 65c259961c26 -r f72c56822d58 main.cpp
--- a/main.cpp	Mon Jul 22 12:32:29 2019 +0000
+++ b/main.cpp	Mon Jul 22 13:44:32 2019 +0000
@@ -27,52 +27,57 @@
 //      #define ODR ((volatile unsigned short*) (GPIOA + 0x14)
 // -
 
-#define MODER   ((volatile unsigned int*)   0x48000000)
+// #define MODER   ((volatile unsigned int*)   0x48000000U)
+volatile unsigned int*   moder  = reinterpret_cast<volatile unsigned int*>(  0x48000000U);
 // 32 bits.
 // We 'll set pins 11,10 set to 0,1.
 // -
 
-#define ODR     ((volatile unsigned short*) 0x48000014)
+// #define ODR     ((volatile unsigned short*) 0x48000014U)
+volatile unsigned short* odr    = reinterpret_cast<volatile unsigned short*>(0x48000014U);
 // 16 bits.
 // We 'll write at pin 5.
 // -
 
 // More FUN (dwarf fortress reference).
 // -
-#define OTYPER      ((volatile unsigned short*) (0x48000004))
-#define PUPDR       ((volatile unsigned int*)   (0x4800000C))
-#define BSRR        ((volatile unsigned int*)   (0x48000018))
-#define LCKR        ((volatile unsigned int*)   (0x4800001C))
-#define AFRL        ((volatile unsigned int*)   (0x48000020))
+// #define OTYPER      ((volatile unsigned short*) (0x48000004U))
+// #define PUPDR       ((volatile unsigned int*)   (0x4800000CU))
+// #define BSRR        ((volatile unsigned int*)   (0x48000018U))
+// #define LCKR        ((volatile unsigned int*)   (0x4800001CU))
+// #define AFRL        ((volatile unsigned int*)   (0x48000020U))
+volatile unsigned short* otyper = reinterpret_cast<volatile unsigned short*>(0x48000004U);
+volatile unsigned int*   pupdr  = reinterpret_cast<volatile unsigned int*>(  0x4800000CU);
+volatile unsigned int*   bsrr   = reinterpret_cast<volatile unsigned int*>(  0x48000018U);
+volatile unsigned int*   lckr   = reinterpret_cast<volatile unsigned int*>(  0x4800001CU);
+volatile unsigned int*   afrl   = reinterpret_cast<volatile unsigned int*>(  0x48000020U);
 
 void set_bit_as_output ()
 {
     // Set to output.
     // -
-    unsigned int moder = *MODER;
-    moder |= 0x00000400U;
-    moder &= 0xFFFFF7FFU;
+    unsigned int tmp = *moder;
+    tmp |= 0x00000400U;
+    tmp &= 0xFFFFF7FFU;
     // ^ Write 0,1 to bits 11,10.
     // -
-    *MODER = moder;
+    *moder = tmp;
 }
 
 void toggle_bit ()
 {
 ///*
-    static unsigned short state = 0;
-    state ^= 0x20;
-    *ODR = state;
+    *odr ^= 0x20;
 //*/
 /*
     static bool state = false;
     if (state)
     {
-        *BSRR = 0x00200000U;
+        *bsrr = 0x00200000U;
     }
     else
     {
-        *BSRR = 0x00000020U;
+        *bsrr = 0x00000020U;
     }    
     state = !state;
 */
@@ -85,12 +90,12 @@
     {
         toggle_bit();
 /*
-        printf("MODER  = 0x%X\n",  *MODER);
-        printf("OTYPER = 0x%X\n",  *OTYPER);
-        printf("PUPDR  = 0x%X\n",  *PUPDR);
-        printf("ODR    = 0x%hX\n", *ODR);
-        printf("LCKR   = 0x%X\n",  *LCKR);
-        printf("AFRL   = 0x%X\n",  *AFRL);
+        printf("MODER  = 0x%X\n",  *moder);
+        printf("OTYPER = 0x%X\n",  *otyper);
+        printf("PUPDR  = 0x%X\n",  *pupdr);
+        printf("ODR    = 0x%hX\n", *odr);
+        printf("LCKR   = 0x%X\n",  *lckr);
+        printf("AFRL   = 0x%X\n",  *afrl);
         printf("\n");
 */
         wait(0.65);