Hi.
I am working on a code that reads addresses on an SRAM.
i have been able to read and compare address values by inputting their DEC challenge value and gotten response in BIN, HEX, DEC.
but i am struggling inputting the binary and hex substitute from the serial monitor to get the same set of output and want to help in identifying a way to input a HEX or BIN from the serial monitor to compare the address output using the right data type.
find below my code,
void setup() {
uint8_t inval = 0;
// setting the pins to access the SRAM
pinMode(CSS, OUTPUT);
digitalWrite(CSS, HIGH);
pinMode(CS, OUTPUT);
Serial.begin(9600);
delay(2500);
SPI.begin();
while (!Serial);
//for (i=0; i<=8192; i++) { // access all memory locations, 64 Kbit SRAM = 65536 / 8 = 8192 i=545
//Spi23K640Wr8(i, (uint8_t)i);
//Serial.print((uint16_t)value, HEX); //prints out all the address of the memory
Serial.println("Enter challenge");
while (!Serial.available());
byte value = Serial.readStringUntil('\n').toInt(); //convert serial monitor input to integer
inval = Spi23K640Rd8(i); //store the SRAM memory ouput
if (value == Spi23K640Rd8(545)) //assign the output to variabel value
{
Serial.println("challenge pair correct"); //if its thesame print
Serial.println("loading address......");
delay(3000);
Serial.print("Challenge Response pair address = ");
Serial.println(Spi23K640Rd8(545), BIN); //prints out specific address
Serial.println("Authenticate Chip");
}
else if (value != Spi23K640Rd8(545)) //if its now equal to value
{
delay(3000);
Serial.println("challenge pair wrong !!!INTRUDER ALERT");
}
}
thanks for your feedback.
the function "toHex()"/"toBin()" is not recognise. do I need to add a library, and will the Hex be inputted using '0x' or the exact hex value and on the binary side as well do I need to use '0b' before the main value
thanks for brushing up for me.
this way i can have different constants in array and challenge them.
but after running the code instead of the initial address location to print out the value i want it is printing maximum FF which make. find attached the whole code from the SPI transfer set up for the SRAM
/*
CS: pin 12
MOSI: pin 8
MISO: pin 10 SRAM pins connection to the microcontroller
SCK: pin 9
*/
#include <SPI.h> //SPI interface
//SRAM opcodes
#define RDSRAM 5 //00000101
#define WRSRAM 1 //00000001
#define READ 3 //00000011
#define WRITE 2 //00000010
//initializing variables
int CS = 12;
int CSS = 8;
//const int address_output = '92'; should the know address be a constant for comparsion and if so where should it be saved?
uint8_t i;
char buf [80];
byte code;
int val;
const int codeAddr = 545;
char s [80];
//SPI interface and memory address read
uint8_t Spi23K640Rd8(uint32_t address)
{
uint8_t read_byte;
digitalWrite(CS,LOW);
SPI.transfer(READ);
//SPI.transfer((uint8_t)(address >> 16) & 0xff);
SPI.transfer((uint8_t)(address >> 8) & 0xff);
SPI.transfer((uint8_t)address);
read_byte = SPI.transfer(0x00);
digitalWrite(CS,HIGH);
return read_byte;
}
// -----------------------------------------------------------------------------
void setup () {
Serial.begin (9600);
#ifndef MyHW
pinMode (CSS, OUTPUT);
digitalWrite (CSS, HIGH);
pinMode (CS, OUTPUT);
delay (2500);
SPI.begin ();
#endif
code = Spi23K640Rd8 (codeAddr);
Serial.println (code, HEX);
while (!Serial) ;
Serial.println ("Enter challenge");
}
// -----------------------------------------------------------------------------
void loop (void)
{
if (Serial.available ()) {
int n = Serial.readBytesUntil ('\n', buf, sizeof (buf)-1);
buf [n] = '\0';
sscanf (buf, "%x", &val);
sprintf (s, " buf %s, code 0x%x, val 0x%x", buf, code, val);
Serial.println (s);
if (code != val) {
Serial.println ("challenge pair wrong !!!INTRUDER ALERT");
}
else {
Serial.println ("challenge pair correct");
Serial.println ("loading address......");
Serial.print ("Challenge Response pair address = ");
Serial.println (codeAddr, BIN); //prints out specific address
Serial.println ("Authenticate Chip");
}
delay (3000);
Serial.println ("Enter challenge");
}
}
yes, its working,
but adopting your code made the printed-out location become FF and from my initial code location 545 outputs 5C when challenged which coincides with my data set
i assume you only need to read the device once. your code repeatedly reads it. don't know why this should matter except possibly that the initial read doesn't work
you could replace the used of "code" with your read
also why is there this line
this performs a read of (?) before the read that actually reads address 545
yes I only need to read it once. only a power state changes the value of the address but this address i am trying to read has stable bits
but the correct value of address is not being printed in this implementation.
disregard that line of code I was trying to store it for an array. its redundant.
did you mean I should change SPI.transfer (READ) to variable 'code' or the exact value?
i can't help because i don't have your hardware
you'll just have to go back to your original code and figure things out.
try integrating some of the stuff i did in your original code.
i already suggested just replacing the use of the "code" variable with your repeated reads, "Spi23K640Rd8(545)"