Просмотр исходного кода

First commit, a cute SM130 story

Pi3rrot 11 лет назад
Родитель
Сommit
d3091b935d
5 измененных файлов с 157 добавлено и 0 удалено
  1. 0 0
      lib/.holder
  2. 117 0
      lib/SM130/SM130.cpp
  3. 19 0
      lib/SM130/SM130.h
  4. 3 0
      make.sh
  5. 18 0
      src/RFID-Mifare.pde

+ 0 - 0
lib/.holder


+ 117 - 0
lib/SM130/SM130.cpp

@@ -0,0 +1,117 @@
+#include <Arduino.h>
+#include <SoftwareSerial.h>
+#include "SM130.h"
+
+SoftwareSerial rfid(7, 8);
+
+SM130::SM130() {
+	rfid.begin(19200);
+}
+
+bool SM130::send_command(uint8_t cmd, const uint8_t *cmd_args, const uint8_t cmd_args_length, uint8_t *ret, uint8_t *length) {
+	uint8_t length_cmd = 1 + cmd_args_length;	//(1) + les arguments
+	uint8_t crc = length_cmd + cmd;			//taille totale
+
+	Serial.print("<command 0x");			//Print de la commande
+	Serial.print(cmd, HEX);
+	Serial.print("> Sent !");
+	Serial.println();
+
+	rfid.write((uint8_t)0xFF);			//Le header qui fait 0xFF
+	rfid.write((uint8_t)0x00);			//Reserved byte toujours à 0x00
+	rfid.write((uint8_t)length_cmd);		//Lenght of the payload data
+	rfid.write((uint8_t)cmd);			//byte of the instruction command
+	for (int i = 0; i < cmd_args_length; i++) {	//Datas de taille N bytes dans une boucle
+		rfid.write((uint8_t)cmd_args[i]);
+		crc += cmd_args[i];			// on incrémente le CRC
+	}
+	rfid.write((uint8_t)crc);			//on write le CRC
+
+	return read_response(ret, length, NULL);
+}
+
+bool SM130::read_response(uint8_t *ret, uint8_t *length, uint8_t *cmd) {
+	uint8_t crc = 0;
+	uint8_t ret_header, reserved, ret_length, ret_cmd, ret_crc;
+	
+	Serial.println("<read_response>");
+	
+	//Header byte
+	ret_header = rfid_read_sync();			//on recup le 1er byte de réponse qui doit etre 0xFF toujours
+	if(ret_header != 0xFF) {			//et on le test ;)
+		Serial.print("Bad header: 0x");
+		Serial.print(ret_header, HEX);
+		Serial.print(". Must be 0xFF !");
+		goto error;
+	}
+	//Reserved Byte						//On incremente pas CRC du header byte d'apres la doc
+	reserved = rfid_read_sync();			//et on continue !
+	if(reserved != 0x00) {				//on test le byte reserved qui doit être à 0x00
+		Serial.print("Bad reserved = 0x");
+		Serial.print(ret_header, HEX);
+		Serial.print(". Must be 0x00 !");
+		goto error;
+	}
+	crc = reserved;					//on oublie pas le $ù%#[[ de CRC
+
+	//Length byte
+	ret_length = rfid_read_sync();
+	Serial.print("Length = 0x");
+	Serial.print(ret_length, HEX);
+	Serial.println();
+	crc += ret_length;
+
+	//Command byte
+	ret_cmd = rfid_read_sync();			//On est censé récupérer la même commande que le send_command
+	if(cmd) {				//On test
+		*cmd = ret_cmd;
+	}
+	ret_length--;
+	Serial.print("command = 0x");
+	Serial.print(ret_cmd, HEX);
+	Serial.println();
+	crc += ret_cmd;
+
+	//Lecture de la réponse complête
+	Serial.print("response = ");
+	int i;
+	while (i < ret_length) {
+		ret[i] = rfid_read_sync();
+		crc += ret[i];
+
+		Serial.print(ret[i], HEX);
+		Serial.print (" ");
+		i++;
+	}
+	
+	Serial.println();
+
+	//On check le CRC
+	ret_crc = rfid_read_sync();
+	if (ret_crc != crc) {
+		Serial.print("Bad CRC 0x");
+		Serial.print(ret_crc, HEX);
+		Serial.print(" ! Should be : 0x");
+		Serial.print(crc, HEX);
+		Serial.println();
+		return false;
+	}
+
+	*length = ret_length;
+	Serial.println("</response>");
+	return true;
+
+error:
+	while(rfid.available())
+		rfid.read();
+
+	Serial.println("</response>");
+	return false;
+	
+
+}
+
+uint8_t SM130::rfid_read_sync() {
+	while(!rfid.available());
+	return rfid.read();
+}

+ 19 - 0
lib/SM130/SM130.h

@@ -0,0 +1,19 @@
+#ifndef SM130_h
+#define SM130_h
+
+#include <Arduino.h>
+
+class SM130 {
+
+	public:
+		SM130();
+		bool send_command(uint8_t cmd, const uint8_t *cmd_args, const uint8_t cmd_args_length, uint8_t *ret, uint8_t *length);
+		bool read_response(uint8_t *ret, uint8_t *length, uint8_t *cmd);
+		uint8_t rfid_read_sync();
+
+	private:
+
+};
+
+
+#endif

+ 3 - 0
make.sh

@@ -0,0 +1,3 @@
+ino build -m atmega328
+ino upload -m atmega328 -p /dev/ttyUSB0
+ino serial -p /dev/ttyUSB0

+ 18 - 0
src/RFID-Mifare.pde

@@ -0,0 +1,18 @@
+#include "SM130.h"
+
+SM130 SM130;
+
+void setup() {
+	Serial.begin(9600);
+
+	uint8_t ret[10];
+	uint8_t len = 10;
+	SM130.send_command(0x80, NULL,0 ,ret, &len);		//RESET
+//	delay(100);
+//	SM130 send_command(0x81, NULL, ret, &len);		//Read Firmware Version
+}
+
+
+void loop() {
+
+}