diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..662f6d5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +bin/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..4d5293e --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib/usart"] + path = lib/usart + url = git@git.chch.tech:avr/usart.git diff --git a/lib/usart/.gitignore b/lib/usart/.gitignore new file mode 100644 index 0000000..662f6d5 --- /dev/null +++ b/lib/usart/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +bin/ diff --git a/lib/usart/LICENSE b/lib/usart/LICENSE new file mode 100644 index 0000000..204b93d --- /dev/null +++ b/lib/usart/LICENSE @@ -0,0 +1,19 @@ +MIT License Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice (including the next +paragraph) shall be included in all copies or substantial portions of the +Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF +OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/lib/usart/README.md b/lib/usart/README.md new file mode 100644 index 0000000..56d1233 --- /dev/null +++ b/lib/usart/README.md @@ -0,0 +1,2 @@ +# usart + diff --git a/lib/usart/makefile b/lib/usart/makefile new file mode 100644 index 0000000..cfdf81e --- /dev/null +++ b/lib/usart/makefile @@ -0,0 +1,28 @@ +MCU=atmega32 +F_CPU=8000000 +PROG=dragon_jtag +PORT?=/dev/ttyUSB0 +CC=avr-g++ +OBJCOPY=avr-objcopy +CFLAGS=-Wall -g -mmcu=${MCU} -DF_CPU=${F_CPU} -I. +TARGET=main +SRCS= src/*.cpp test/main.cpp + +all: build flash + +build: + ${CC} ${CFLAGS} -o bin/${TARGET}.bin ${SRCS} + ${CC} ${CFLAGS} -o bin/${TARGET}.elf ${SRCS} + ${OBJCOPY} -j .text -j .data -O ihex bin/${TARGET}.bin bin/${TARGET}.hex + +flash: + avrdude -p ${MCU} -c ${PROG} -U flash:w:bin/main.hex + +clean: + rm -f bin/* + +term: + python3 term.py + +avarice: + avarice --program --file bin/main.elf --part atmega32 --dragon :4242 diff --git a/lib/usart/src/usart.cpp b/lib/usart/src/usart.cpp new file mode 100644 index 0000000..4758601 --- /dev/null +++ b/lib/usart/src/usart.cpp @@ -0,0 +1,65 @@ +#include +#include +#include "usart.h" + +namespace usart +{ + namespace { + static FILE mystdout; + } + + void init( unsigned long baud) { + fdev_setup_stream(&mystdout, put_printf, NULL, _FDEV_SETUP_WRITE); + stdout = &mystdout; + const unsigned int ubrr = F_CPU/8/baud-1; + + /*Set baud rate */ + + + #if defined __AVR_ATmega328P__ + UBRR0H = (unsigned char)(ubrr>>8); + UBRR0L = (unsigned char)ubrr; + UCSR0A |= (1<>8); + UBRRL = (unsigned char)ubrr; + UCSRA |= (1< + +namespace usart +{ + void init(unsigned long baud); + void put(char data ); + char get(); + int put_printf(char var, FILE *stream); +} // namespace usart + + + diff --git a/lib/usart/term.py b/lib/usart/term.py new file mode 100644 index 0000000..a6c0b3d --- /dev/null +++ b/lib/usart/term.py @@ -0,0 +1,13 @@ +import serial +from time import sleep + +with serial.Serial('/dev/ttyUSB0', 9600, timeout=1) as ser: + ser.write(b'a') + while True: + x = ser.read() # read one byte + if x != b'': + pass + print(x.decode('utf')) + #ser.write(b'a') + #print(ser.read()) + sleep(1) \ No newline at end of file diff --git a/lib/usart/test/main.cpp b/lib/usart/test/main.cpp new file mode 100644 index 0000000..8d72b75 --- /dev/null +++ b/lib/usart/test/main.cpp @@ -0,0 +1,21 @@ +#include +#include +#include "../src/usart.h" +#include + + + + + +int main (void) { + DDRD |= (1< MAX_LEN) + + { + n = MAX_LEN; + } + + // read the data received in FIFO + for (i=0; i + + +//MF522command wordu +#define PCD_IDLE 0x00 //NO action; cancel current command +#define PCD_AUTHENT 0x0E //verify key +#define PCD_RECEIVE 0x08 //receive data + +#define PCD_TRANSMIT 0x04 //send data +#define PCD_TRANSCEIVE 0x0C //receive and send data +#define PCD_RESETPHASE 0x0F //reset +#define PCD_CALCCRC 0x03 //CRC calculation + +//Mifare_One Card command word +#define PICC_REQIDL 0x26 // line-tracking area is dormant #define PICC_REQALL 0x52 //line-tracking area is interfered +#define PICC_ANTICOLL 0x93 //Anti collision +#define PICC_SElECTTAG 0x93 //choose cards +#define PICC_AUTHENT1A 0x60 //Verify A key +#define PICC_AUTHENT1B 0x61 //Verify B key +#define PICC_READ 0x30 // Reader Module +#define PICC_WRITE 0xA0 // letter block + +#define PICC_DECREMENT 0xC0 +#define PICC_INCREMENT 0xC1 +#define PICC_RESTORE 0xC2 //Transfer data to buffer +#define PICC_TRANSFER 0xB0 //Save buffer data +#define PICC_HALT 0x50 //Dormancy + + +//MF522 Error code returned when communication +#define MI_OK 0 +#define MI_NOTAGERR 1 +#define MI_ERR 2 + + +//------------------MFRC522 Register--------------- +//Page 0:Command and Status +#define Reserved00 0x00 +#define CommandReg 0x01 +#define CommIEnReg 0x02 +#define DivlEnReg 0x03 +#define CommIrqReg 0x04 +#define DivIrqReg 0x05 +#define ErrorReg 0x06 +#define Status1Reg 0x07 +#define Status2Reg 0x08 +#define FIFODataReg 0x09 +#define FIFOLevelReg 0x0A + +#define WaterLevelReg 0x0B +#define ControlReg 0x0C +#define BitFramingReg 0x0D +#define CollReg 0x0E +#define Reserved01 0x0F +//Page 1:Command +#define Reserved10 0x10 +#define ModeReg 0x11 +#define TxModeReg 0x12 +#define RxModeReg 0x13 +#define TxControlReg 0x14 +#define TxAutoReg 0x15 +#define TxSelReg 0x16 +#define RxSelReg 0x17 +#define RxThresholdReg 0x18 +#define DemodReg 0x19 + +#define Reserved11 0x1A +#define Reserved12 0x1B +#define MifareReg 0x1C +#define Reserved13 0x1D +#define Reserved14 0x1E +#define SerialSpeedReg 0x1F +//Page 2:CFG +#define Reserved20 0x20 +#define CRCResultRegM 0x21 +#define CRCResultRegL 0x22 +#define Reserved21 0x23 +#define ModWidthReg 0x24 +#define Reserved22 0x25 +#define RFCfgReg 0x26 +#define GsNReg 0x27 +#define CWGsPReg 0x28 +#define ModGsPReg 0x29 +#define TModeReg 0x2A +#define TPrescalerReg 0x2B +#define TReloadRegH 0x2C +#define TReloadRegL 0x2D +#define TCounterValueRegH 0x2E +#define TCounterValueRegL 0x2F +//Page 3:TestRegister +#define Reserved30 0x30 + +#define TestSel1Reg 0x31 +#define TestSel2Reg 0x32 +#define TestPinEnReg 0x33 +#define TestPinValueReg 0x34 +#define TestBusReg 0x35 +#define AutoTestReg 0x36 +#define VersionReg 0x37 +#define AnalogTestReg 0x38 +#define TestDAC1Reg 0x39 +#define TestDAC2Reg 0x3A +#define TestADCReg 0x3B +#define Reserved31 0x3C +#define Reserved32 0x3D +#define Reserved33 0x3E +#define Reserved34 0x3F + +#define uchar unsigned char +#define uint unsigned int + +#ifndef NRSTPD_PORT + #define NRSTPD_PORT PORTB + #define NRSTPD_DDR DDRB + #define NRSTPD_PIN PB1 +#endif + +#define MAX_LEN 16 + +void MFRC522_Init(void); +void MFRC522_Halt(void); +uchar MFRC522_ToCard(uchar command, uchar *sendData, uchar sendLen, uchar *backData, uint *backLen); +void Write_MFRC522(uchar addr, uchar val); +uchar Read_MFRC522(uchar addr); +uchar MFRC522_Auth(uchar authMode, uchar BlockAddr, uchar *Sectorkey, uchar *serNum); +uchar MFRC522_Write(uchar blockAddr, uchar *writeData); +uchar MFRC522_Read(uchar blockAddr, uchar *recvData); +uchar MFRC522_Request(uchar reqMode, uchar *TagType); +uchar MFRC522_Anticoll(uchar *serNum); +uchar MFRC522_SelectTag(uchar *serNum); + +#endif \ No newline at end of file diff --git a/src/spi.cpp b/src/spi.cpp new file mode 100644 index 0000000..801391f --- /dev/null +++ b/src/spi.cpp @@ -0,0 +1,21 @@ +#include "spi.h" + + +namespace spi +{ + void init() { + SPI_PORT |= (1< + + +#if defined __AVR_ATmega328P__ + #define SPI_DDR DDRB + #define SPI_PORT PORTB + #define SPI_SCK PB5 + #define SPI_MISO PB4 + #define SPI_MOSI PB3 + #define SPI_CS PB2 +#elif defined __AVR_ATmega32__ || defined __AVR_ATmega16__ + #define SPI_PORT PORTB + #define SPI_DDR DDRB + #define SPI_SCK PB7 + #define SPI_MOSI PB5 + #define SPI_CS PB4 +#endif + +namespace spi +{ + void init(); + uint8_t transfer(uint8_t data); + + inline void cs_low() {SPI_PORT &= ~(1< +#include "../lib/usart/src/usart.h" +#include "string.h" + +#define uchar unsigned char +#define uint unsigned int +#define MAX_LEN 16 +const int chipSelectPin = 10; +const int NRSTPD = 5; + +uchar serNum[5]; +uchar writeDate[16] ={'T', 'e', 'n', 'g', ' ', 'B', 'o', 0, 0, 0, 0, 0, 0, 0, 0,0}; + +uchar sectorKeyA[16][16] = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, + {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, + {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, + }; + uchar sectorNewKeyA[16][16] = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, + {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, + {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, + }; + +int main() { + usart::init(1000000); + printf("Hello"); + MFRC522_Init(); + while(1) { + uchar i; + uchar status; + uchar str[MAX_LEN]; + uchar RC_size; + uchar blockAddr; //Select the address of the operation 0~63 + + + // searching card, return card type + status = MFRC522_Request(PICC_REQIDL, str); + if (status == MI_OK) + { + } + + + status = MFRC522_Anticoll(str); + memcpy(serNum, str, 5); + if (status == MI_OK) + { + printf("The card's number is : %x%x%x%x%x%x\n\r", + serNum[0], + serNum[1], + serNum[2], + serNum[3], + serNum[4]); + } + + // select card, return card capacity + RC_size = MFRC522_SelectTag(serNum); + if (RC_size != 0) + {} + + // write data card + blockAddr = 7; // data block 7 + status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[blockAddr/4], serNum); // authentication + if (status == MI_OK) + + { + // write data + status = MFRC522_Write(blockAddr, sectorNewKeyA[blockAddr/4]); + printf("set the new card password, and can modify the data of the Sector: "); + printf("%i", blockAddr/4); + + // write data + blockAddr = blockAddr - 3 ; + status = MFRC522_Write(blockAddr, writeDate); + if(status == MI_OK) + { + printf("OK!\n\r"); + } + } + + // read card + blockAddr = 7; // data block 7 + status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, + + sectorNewKeyA[blockAddr/4], serNum); // authentication + if (status == MI_OK) + { + // read data + blockAddr = blockAddr - 3 ; + status = MFRC522_Read(blockAddr, str); + if (status == MI_OK) + { + printf("Read from the card ,the data is : \n\r"); + for (i=0; i<16; i++) + { + printf("%c", str[i]); + } + printf("\n\r"); + } + } + printf("\n\r"); + MFRC522_Halt(); // command card into sleeping mode + + } + +} +