extern "C" int main(void) { rc522_pcd_select(RC522_PCD_1); mfrc522_init(); rc522_pcd_select(RC522_PCD_2); mfrc522_init(); enc28j60_init(mac_addr); dhcp_retry_time = RTC_GetCounter() + 1; // Check if timer started. uint8_t status = mfrc522_read(Status1Reg); uint32_t poll_time, dns_time; __enable_irq(); rc522_irq_prepare(); rc522_pcd_select(RC522_PCD_1); rc522_irq_prepare(); // Workers. while (1) { // If DHCPD expired or still not assigng request for new net settings. while (dhcp_status != DHCP_ASSIGNED) { dhcp_poll(); } // If cached tag did request, the even is stored to queue, send it to server // in main thread. tag_event_queue_processor(); if (ticks - poll_time > 600) { poll_time = ticks; tcp_poll(); } if (ticks - dns_time > 600) { dns_time = ticks; //dns_query("com"); } } }
void init_mfrc522(void) { pin_mode_output(MFRC522_NRSTPD); pin_high(MFRC522_NRSTPD); /* Slave select pin, high (deselected) initially. */ pin_mode_output(MFRC522_SS); pin_high(MFRC522_SS); pin_mode_output(MFRC522_MOSI); pin_low(MFRC522_MOSI); pin_mode_input(MFRC522_MISO); pin_mode_output(MFRC522_SCK); pin_low(MFRC522_SCK); spi_mode_master(); spi_enable(); mfrc522_init(); }
extern "C" void TIM2_IRQHandler() { open_node(); if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); uint8_t pcd = RC522_PCD_1; do { // Chose rfid device. rc522_pcd_select(pcd); uint8_t status = mfrc522_read(Status1Reg); __disable_irq(); // If timer is not running and interrupt timer flag is not active reinit device. if (!(status & TRunning)) { uint8_t need_reinit = 0; switch (pcd) { case RC522_PCD_1: if (EXTI_GetITStatus(EXTI_Line10) == RESET) need_reinit = 1; break; case RC522_PCD_2: if (EXTI_GetITStatus(EXTI_Line11) == RESET) need_reinit = 1; break; } if (need_reinit) { spi_hardware_failure_signal(); mfrc522_init(); __enable_irq(); rc522_irq_prepare(); } } __enable_irq(); } while (pcd++ < RC522_PCD_2); // Check enc28j60 chip and restart if needed. if (!enc28j60_revid || (enc28j60_revid != enc28j60_rcr(EREVID)) || (GPIO_ReadInputDataBit(ETH_GPIO, ETH_IRQ_PIN) == RESET && (EXTI_GetITStatus(EXTI_Line2) == RESET))) { enc28j60_init(mac_addr); } uint16_t phstat1 = enc28j60_read_phy(PHSTAT1); // Если ethernet провод вытаскивали, обновить DHCP. // Пока отключено, т.к. по непонятным причинам LLSTAT падает иногда // хотя коннект сохраняется, что вызывает провалы в доступности интерфейса на 3-10 секунд, // пока интерфейс не поднимится по DHCP заного, однако если согласно LLSTAT линк выключен // но мы не гасим интерфейс он продолжает нормально работать. if(!(phstat1 & PHSTAT1_LLSTAT)) { static uint16_t link_dhcp_time; // Avoid frequently link checks. if (ticks - link_dhcp_time > 5000) { link_dhcp_time = ticks; // Обновим адрес через 5 секунд // (после того, как линк появится) dhcp_status = DHCP_INIT; dhcp_retry_time = RTC_GetCounter() + 2; // Линка нет - опускаем интерфейс ip_addr = 0; ip_mask = 0; ip_gateway = 0; enc28j60_init(mac_addr); } } } }
/* main program */ void main(void) { //local var uint8_t curr_id[5]; uint16_t card_tipe; uint8_t str[MAX_LEN]; uint8_t status; //start up delay delay_ms(1000); //init usart usart_init(); #if defined(DEBUG_ON) printf("RC522 TEST\n"); #endif // init spi as master spi_init(); // init rc522 mfrc522_init(); // check reader attached??? rc522_check_reader(); #if defined(DEBUG_ON) printf("RC522 DETECTED\n"); #endif while (1) { // read id card if (rc522_read_card_id(curr_id, &card_tipe)) { printf("%.2X%.2X%.2X%.2X%.2X %.4X\n", curr_id[0], curr_id[1], curr_id[2], curr_id[3], curr_id[4], card_tipe); // select the tag printf("select = %.2X\n", mfrc522_select_tag(curr_id)); //login to block 4 printf("login = %d\n", mfrc522_auth(PICC_AUTHENT1A, 4, keyA_default, curr_id)); //read block 4 status = mfrc522_read_block(4, str); printf("read = %d %.2X %.2X %.2X\n", status, str[0], str[1], str[2]); // delay_ms(1000); // // //login to block 4 // printf("login = %d\n", mfrc522_auth(PICC_AUTHENT1A, 4, keyA_default, curr_id)); // // //write block 4 // str[0] = 1; // str[1] = 2; // str[2] = 3; // status = mfrc522_write_block(4, str); // // delay_ms(1000); // // //login to block 4 // printf("login = %d\n", mfrc522_auth(PICC_AUTHENT1A, 4, keyA_default, curr_id)); // // //read block 4 // status = mfrc522_read_block(4, str); // printf("read = %d %.2X %.2X %.2X\n", status, str[0], str[1], str[2]); } //halt the card mfrc522_halt(); //delay delay_ms(100); //eternal loop // for(;;); } }
int main() { DDRC &= ~(1<<PC0); //input for DPDT switch increase_day_count_eeprom(); //some more initialization of EEPROM /*** Students list ***/ /** These are the bytes that are read from the RFID tags of the students**/ uint8_t person_byte[MAX_PEOPLE][5] = { {0x23, 0x6D, 0xD6, 0x00, 0x98} , {0xF9, 0x46, 0x1D, 0x00, 0xA2}, {0xA3, 0x7E, 0x30, 0x02, 0xEF} }; //Some tags that we used to experiment //{0xF9, 0x46, 0x1D, 0x00, 0xA2} //{0x23, 0x6D, 0xD6, 0x00, 0x98} - recognized1 //{0xF9, 0x46, 0x1D, 0x00, 0xA2} - recognized2 (change any bit in any of these 2 to see effect of unrecognized person entering) //{0xA3, 0x7E, 0x30, 0x02, 0xEF} - Nimi - white //{0x5B, 0xA8, 0x2C, 0x00, 0xDF} - Adnan - blue // person name list .. these used to control people names etc int detected_person; int person_entry_list[MAX_PEOPLE] = {0, 0, 0}; char *person_name[MAX_LEN] = { (char *)"Sibat", (char *)"Ripon" , (char *)"Nimi" }; char *entered_msg = (char *)" entered"; char *left_msg = (char *)" left"; char msg_to_show[100]; int person_count = 0; // iterator and byte array to use later uint8_t byte, i; uint8_t str[MAX_LEN]; _delay_ms(50); // initialize the LCD LCDInit(LS_BLINK); LCDWriteStringXY(2,0,"RFID Reader"); // spi initialization spi_init(); _delay_ms(1000); LCDClear(); //init reader mfrc522_init(); // poll until reader is found while(1) { byte = mfrc522_read(VersionReg); if(byte == 0x92) { LCDClear(); LCDWriteStringXY(2,0,"Detected"); _delay_ms(1000); break; } else { LCDClear(); LCDWriteStringXY(0,0,"No reader found"); _delay_ms(800); LCDClear(); LCDWriteStringXY(0, 0, "Connect reader"); _delay_ms(1000); } } // ready to run _delay_ms(1500); LCDClear(); // initializing the RGB LEDs DDRA = 0xFF; PORTA = 0xFE; //Interrupt INT2 DDRB |= (1<<PB2); // Set PB2 as input (Using for interrupt INT2) PORTB |= (1<<PB2); // Enable PB2 pull-up resistor // setting up the timer codes // setting up the LED animation initials // TODO timer code ... START HERE ... TCCR1A = 0x00; TCCR1B = 0x01; TIMSK = 0x04; min = 0; sec = 0; hour = 0; sec_counter = 0; LED_animation_status = 0; LED_animation_on = 1; program_status_2_first_time = 1; program_status_3_first_time = 1; program_status_4_first_time = 1; // TODO timer code ... ENDS HERE ... // The loop starts here // program status : 1 means entrance period // : 2 means session period // : 3 means session ended program_status = 1; //Using interrupt 2 for reading history mode GICR &= ~(1<<INT2); // Disable INT2 MCUCSR |= (1<<ISC2); // Trigger INT2 on 1 = rising edge , 0 falling edge GIFR |= (1<<INTF2); // clear INTF2 GICR |= (1<<INT2); // Enable INT2 sei(); while(1) { spi_init(); mfrc522_init(); byte = mfrc522_read(ComIEnReg); mfrc522_write(ComIEnReg,byte|0x20); byte = mfrc522_read(DivIEnReg); mfrc522_write(DivIEnReg,byte|0x80); if(program_status == 1) { LCDClear(); LCDWriteStringXY(0, 0, "Show your card."); LCDWriteStringXY(0, 1, "#students:"); LCDWriteIntXY(12,1, person_count ,2 ); byte = mfrc522_request(PICC_REQALL,str); if(byte == CARD_FOUND) { byte = mfrc522_get_card_serial(str); if(byte == CARD_FOUND) { LED_animation_on = 0; // check for person 1 detected_person = -1; for(i = 0; i < MAX_PEOPLE; i++) { for(byte = 0; byte < 5; byte++) { /***The following piece of code was written to test and read the bytes off the tags and print them***/ //LCDClear(); //LCDWriteIntXY(0,0,byte,2); //LCDWriteIntXY(0,1,str[byte],14); //_delay_ms(3000); if(str[byte] != person_byte[i][byte]) { break; } } if(byte == 5) { detected_person = i; break; } } if(i == MAX_PEOPLE) { detected_person = -1; } // showing message on LCD upon decision of the person LCDClear(); if(detected_person == -1) { PORTA = 0x7E; LCDClear(); LCDWriteStringXY(0, 0, "Access denied!"); _delay_ms(2000); PORTA = 0xFE; LCDClear(); LCDWriteStringXY(0, 0, "Unrecognized!"); } else { PORTA = 0xFD; LCDClear(); LCDWriteStringXY(0, 0, "Access granted!"); _delay_ms(2000); if(person_entry_list[i] == 0) { person_entry_list[i] = 1; } else { person_entry_list[i] = 0; } strcpy(msg_to_show, person_name[detected_person]); if(person_entry_list[i] == 0) { /** Code to sound buzzer when a student is leaving **/ PORTA = 0x7D; _delay_ms(200); PORTA = 0xFD; // end of buzzer code //EEPROM WRITE if(write_enable_eeprom == 1){ eeprom_update_byte ((uint8_t*) (&NonVolatileIsPresent[curr_day][i]), 0); } person_count--; strcat(msg_to_show, left_msg); } else { /** Code to sound buzzer when a student is entering **/ PORTA = 0x7D; _delay_ms(200); PORTA = 0xFD; _delay_ms(100); PORTA = 0x7D; _delay_ms(200); PORTA = 0xFD; // end of buzzer code //EEPROM WRITE if(write_enable_eeprom == 1) { eeprom_write_byte ((uint8_t*) (&NonVolatileIsPresent[curr_day][i]), 1); eeprom_update_byte ((uint8_t*) (&NonVolatileHour[curr_day][i]), hour); eeprom_update_byte ((uint8_t*) (&NonVolatileMinute[curr_day][i]), min); eeprom_update_byte ((uint8_t*) (&NonVolatileSecond[curr_day][i]), sec); } person_count++; strcat(msg_to_show, entered_msg); } LCDClear(); LCDWriteStringXY(0, 0, msg_to_show); } _delay_ms(3000); PORTA = 0xFE; LCDClear(); LED_animation_on = 1; } else { LCDClear(); LCDWriteStringXY(0,1,"Error"); } } _delay_ms(200); } else if(program_status == 2) { LCDClear(); LCDWriteStringXY(0, 0, "In session now!"); LCDWriteStringXY(0, 1, "#students:"); LCDWriteIntXY(12,1, person_count ,2 ); byte = mfrc522_request(PICC_REQALL,str); if(byte == CARD_FOUND) { LED_animation_on = 0; PORTA = 0x7E; LCDClear(); LCDWriteStringXY(0, 0, "Warning!!"); _delay_ms(2000); LCDClear(); LCDWriteStringXY(0, 0, "Session running."); _delay_ms(2000); PORTA = 0xFE; LCDClear(); LCDWriteStringXY(0, 0, "Can't go out/in."); _delay_ms(2000); LCDClear(); PORTA = 0xFB; LED_animation_on = 1; } _delay_ms(200); } else if(program_status == 3) { LCDClear(); LCDWriteStringXY(0, 0, "Session ended!"); LCDWriteStringXY(0, 1, "#students:"); LCDWriteIntXY(12,1, person_count ,2 ); byte = mfrc522_request(PICC_REQALL,str); if(byte == CARD_FOUND) { byte = mfrc522_get_card_serial(str); if(byte == CARD_FOUND) { LED_animation_on = 0; // check for person 1 detected_person = -1; for(i = 0; i < MAX_PEOPLE; i++) { for(byte = 0; byte < 5; byte++) { if(str[byte] != person_byte[i][byte]) { break; } } if(byte == 5) { detected_person = i; break; } } // showing message on LCD upon decision of the person if(detected_person == -1) { PORTA = 0x7E; LCDClear(); LCDWriteStringXY(0, 0, "Unrecognized!"); _delay_ms(2000); PORTA = 0xFE; } else { PORTA = 0xFB; if(person_entry_list[detected_person] != 0){ LCDClear(); strcpy(msg_to_show, "Take care "); strcat(msg_to_show, person_name[detected_person]); LCDWriteStringXY(0, 0, msg_to_show); _delay_ms(1000); LCDClear(); strcpy(msg_to_show, person_name[detected_person]); strcat(msg_to_show, left_msg); LCDWriteStringXY(0, 0, msg_to_show); person_entry_list[detected_person] = 0; person_count--; //unnecessary sanity check if(person_count == 0) { break; } } } _delay_ms(3000); PORTA = 0xFD; LCDClear(); LED_animation_on = 1; } else { PORTA = 0x7E; LCDClear(); LCDWriteStringXY(0,1,"Error"); _delay_ms(2000); PORTA = 0xFE; LCDClear(); PORTA = 0xFD; } } _delay_ms(200); } else { /***This is the warning phase. When leaving period has ended, but some students were still stuck in the classroom, the buzzer buzzes off continuously suspecting that some students might be sick or in trouble. ***/ if(person_count != 0) { LED_animation_on = 0; PORTA = 0x7E; LCDClear(); LCDWriteIntXY(0,0, person_count ,2 ); LCDWriteStringXY(3, 0, "student could"); LCDWriteStringXY(0, 1, "not get out"); _delay_ms(1500); LCDClear(); LCDWriteStringXY(0, 0, "Take caution!"); _delay_ms(1500); PORTA = 0xFE; _delay_ms(400); } else { /**When all students have left, nothing to do. **/ LCDClear(); break; } } } LED_animation_on = 1; PORTA = 0xFB; temp_PORTA = PORTA; LCDClear(); LCDWriteStringXY(0, 0, "Everyone left."); while(1) { ; } cli(); }