E2PRO2 / Mbed OS e2pro2-rfid-house
Revision:
5:df18de5caf9f
Parent:
4:320ce84c8f43
Child:
6:8ac65a5956a1
--- a/main.cpp	Fri Jun 22 09:59:36 2018 +0000
+++ b/main.cpp	Fri May 03 11:14:06 2019 +0000
@@ -1,86 +1,171 @@
-
+/*
+ * File       : main.cpp
+ * License    : MIT License
+ * Version    : 0.91
+ * Author     : Janus Bo Andersen (JA67494)
+ * Last edit  : Janus
+ * Created on : 27 Apr 2019
+ * Last change: 03 May 2019
+ * Description: Demo program for Mifare RC-522 RFID reader/writer chip. Will:
+ *            : Communicate with RFID readers, transmit card numbers to terminal
+ *            : and toggle a digital pin(s) upon reading a card.
+ * Structure  : Uses SPI protocol to communicate with the RFIDs.
+ *            : Uses U(S)ART to communicate with connected computer/terminal.
+ * Adapted for: STM32 Nucleo L432KC and NXP KL25Z (macro settings).
+ * Changelog  : 27 Apr 2019: Created demo program
+              : 02 May 2019: Update pin mappings for FRDM-KL25Z Board
+              : 02 May 2019: Added set and reset for a latched circuit
+              : 03 May 2019: Upgrade to RTOS 5.
+              : 03 May 2019: Run two RFIDs simultaneosuly (changed library setup
+              :              by decr. SPI freq. and adding reset managers).
+ 
+ Resources:
+ https://github.com/armmbed/mbed-os
+ https://os.mbed.com/docs/mbed-os/v5.9/tutorials/migrating.html
+ 
+ */
 
-//Test of cheap 13.56 Mhz RFID-RC522 module from eBay
-//This code is based on Martin Olejar's MFRC522 library. Minimal changes
-//Adapted for Nucleo STM32 F401RE. Should work on other Nucleos too
+/*Connect as follows:
+
+Power supply MUST BE 3.3V and GND to the respective pins.
 
-//Connect as follows:
-//RFID pins        ->  Nucleo header CN5 (Arduino-compatible header)
-//----------------------------------------
-//RFID IRQ=pin5    ->   Not used. Leave open
-//RFID MISO=pin4   ->   Nucleo SPI_MISO=PA_6=D12
-//RFID MOSI=pin3   ->   Nucleo SPI_MOSI=PA_7=D11
-//RFID SCK=pin2    ->   Nucleo SPI_SCK =PA_5=D13
-//RFID SDA=pin1    ->   Nucleo SPI_CS  =PB_6=D10
-//RFID RST=pin7    ->   Nucleo         =PA_9=D8
-//3.3V and Gnd to the respective pins
+  RFID pins          ->   Board pinName   L432KC       FRDM-KL25Z
+  ---------------------------------------------------------------------
+  
+  =======================  BUS   ======================================
+  RFID MISO = pin4   ->   SPI_MISO        PA_6=D12     PTD3
+  RFID MOSI = pin3   ->   SPI_MOSI        PA_7=D11     PTD2
+  RFID SCK  = pin2   ->   SPI_SCK         PA_5=D13     PTD1
+  ======================= RFID 1 ====================================== 
+  RFID SDA  = pin1   ->   SPI_CS          PB_6=D10     PTD0
+  RFID RST  = pin7   ->                   PA_9=D8      PTA13
+  RFID IRQ  = pin5   ->                   -------- OPEN --------
+  ======================= RFID 2 ======================================
+  RFID SDA  = pin1   ->   SPI_CS          PA_12=D2     PTD5     
+  RFID RST  = pin7   ->                   PB_1=D6      [PTA13] <- might not be good
+  RFID IRQ  = pin5   ->                   -------- OPEN --------
+  =====================================================================
+
+Connecting the "HOUSE"
+  Location      Circuit latch function    L432KC       FRDM-KL25Z
+  ---------------------------------------------------------------------
+  Front door    Set                       A1           A1
+  Front door    Reset                     A2           A2
+  Room 1 door   Set                       A3           A3
+  Room 1 door   Reset                     A4           A4
+  =====================================================================
+  
+  Mapping:
+  Front door = rfid1
+  Room 1 door = rfid2
+  
+*/     
 
 #include "mbed.h"
 #include "MFRC522.h"
 
-DigitalOut LedGreen(LED2);
+#define COMPILING_FOR_KL25Z 0 //Set this to 1 to compile with KL25Z pins
 
-//Serial connection to PC for output
-Serial pc(USBTX, USBRX);
+#if COMPILING_FOR_KL25Z
+  // FRDM KL25Z pins
+  
+  //USB communication
+  #define SERIAL_TX PTE0  //UART0 USB
+  #define SERIAL_RX PTE1  //UART0 USB
+  
+  //BUS
+  #define SPI_MISO  PTD3
+  #define SPI_MOSI  PTD2
+  #define SPI_SCK   PTD1
+  
+  //RFID 1
+  #define SPI_CS1   PTD0  //Chip select can be any digital pin
+  #define MF_RESET1 PTA13
+  
+  //RFID 2
+  #define SPI_CS2   PTD5
+  #define MF_RESET2 PTA13
 
+  //House circuit connections
+  #define FRONTDOOR_SET A1
+  #define FRONTDOOR_RST A2
+  #define ROOM1DOOR_SET A3
+  #define ROOM1DOOR_RST A4
 
-//MFRC522    RfChip   (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);
-MFRC522    RfChip   (PTD2, PTD3, PTD1, PTD0, PTC4);
-//MFRC522    RfChip2   (PTD2, PTD3, PTD1, PTC3, PTC2); //Adding extra sensors if needed
-//MFRC522    RfChip3   (PTD2, PTD3, PTD1, PTA2, PTB23);
+#else
+  // STM32 L432KC pins
+  
+  //USB communication
+  #define SERIAL_TX USBTX  //UART0 USB
+  #define SERIAL_RX USBRX  //UART0 USB
+  
+  //BUS
+  #define SPI_MISO  D12
+  #define SPI_MOSI  D11
+  #define SPI_SCK   D13
+  
+  //RFID 1
+  #define SPI_CS1   D10
+  #define MF_RESET1  D8
+  
+  //RFID 2
+  #define SPI_CS2    D2
+  #define MF_RESET2  D6
 
-int main(void)
-{
-    pc.printf("starting...\n");
+  //House circuit connections
+  #define FRONTDOOR_SET A1
+  #define FRONTDOOR_RST A2
+  #define ROOMDOOR_SET  A3
+  #define ROOMDOOR_RST  A4
+
+#endif
+
+DigitalOut LedGreen(LED1); //Blinks when a card is read
 
-    // Init. RC522 Chip
-    RfChip.PCD_Init();
-//    RfChip2.PCD_Init();
-//    RfChip3.PCD_Init();
+//Serial connection to terminal for output
+Serial pc(SERIAL_TX, SERIAL_RX);
+
+//MFRC522 readers
+MFRC522 rfid1(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS1, MF_RESET1);
+MFRC522 rfid2(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS2, MF_RESET2);
+
+int main(void) {
+    pc.printf("Starting the Team 2 Pro 2 House...\n\r");
+
+    // Init. RC522 chips
+    rfid1.PCD_Init();
+    rfid2.PCD_Init();
 
     while (true) {
         LedGreen = 1;
 
-        if ( RfChip.PICC_IsNewCardPresent()) {
-            if (RfChip.PICC_ReadCardSerial()) {
+        if (rfid1.PICC_IsNewCardPresent()) {
+            if (rfid1.PICC_ReadCardSerial()) {
 
                 LedGreen = 0;
 
-                pc.printf("Card Reader 1: ");
-                for (uint8_t i = 0; i < RfChip.uid.size; i++) {
-                    pc.printf(" %X02", RfChip.uid.uidByte[i]);
+                pc.printf("Front door: ");
+                for (uint8_t i = 0; i < rfid1.uid.size; i++) {
+                    pc.printf(" %02X", rfid1.uid.uidByte[i]);
                 }
                 pc.printf("\n\r");
                 wait_ms(200);
             }
-        }
-        
-
-/*      if ( RfChip2.PICC_IsNewCardPresent()) {
-            if (RfChip2.PICC_ReadCardSerial()) {
-
-                        LedGreen = 0;
-
-                        pc.printf("Card Reader 2: ");
-                        for (uint8_t i = 0; i < RfChip2.uid.size; i++) {
-                            pc.printf(" %X02", RfChip2.uid.uidByte[i]);
-                        }
-                        pc.printf("\n\r");
-                    }
-                }
+        } //end rfid1
+       
 
-                if ( RfChip3.PICC_IsNewCardPresent()) {
-                    if (RfChip3.PICC_ReadCardSerial()) {
-
-                        LedGreen = 0;
-
-                        pc.printf("Card Reader 3: ");
-                        for (uint8_t i = 0; i < RfChip3.uid.size; i++) {
-                            pc.printf(" %X02", RfChip3.uid.uidByte[i]);
-                        }
-                        pc.printf("\n\r");
-                    }
-                }*/
-
-    }
-}
+        if ( rfid2.PICC_IsNewCardPresent()) {
+            if (rfid2.PICC_ReadCardSerial()) {
+    
+                LedGreen = 0;
+    
+                pc.printf("Room 1: ");
+                for (uint8_t i = 0; i < rfid2.uid.size; i++) {
+                    pc.printf(" %02X", rfid2.uid.uidByte[i]);
+                }
+                pc.printf("\n\r");
+                wait_ms(200);
+            }
+        } //end rfid2
+    } //end while
+} //end main
\ No newline at end of file