Skip to content

Commit

Permalink
New update changes to MCU1
Browse files Browse the repository at this point in the history
  • Loading branch information
niekiran committed Apr 9, 2019
1 parent 02d5adf commit 2cee859
Show file tree
Hide file tree
Showing 619 changed files with 135,149 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Wire Slave Receiver
//Uno, Ethernet A4 (SDA), A5 (SCL)
#include <Wire.h>

#define MY_ADDR 0x68

int LED = 13;
char rx_buffer[32] ;
uint32_t cnt =0;
uint8_t message[50];
void setup() {

Serial.begin(9600);
// Define the LED pin as Output
pinMode (LED, OUTPUT);

// Start the I2C Bus as Slave on address 0X69
Wire.begin(MY_ADDR);

// Attach a function to trigger when something is received.
Wire.onReceive(receiveEvent);

sprintf(message,"Slave is ready : Address 0x%x",MY_ADDR);
Serial.println((char*)message );
Serial.println("Waiting for data from master");
}

void loop(void)
{

}

void receiveEvent(int bytes)
{
while( Wire.available() )
{
rx_buffer[cnt++] = Wire.read();
}
rx_buffer[cnt] = '\0';
cnt=0;
Serial.print("Received:");
Serial.println((char*)rx_buffer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Wire Slave Transmitter and receiver
//Uno, Ethernet A4 (SDA), A5 (SCL)
#include <Wire.h>

// Include the required Wire library for I2C<br>#include <Wire.h>
int LED = 13;
uint8_t active_command = 0xff,led_status=0;
char name_msg[32] = "Welcome to FastBit EBA\n";

uint16_t device_id = 0xFF45;

#define SLAVE_ADDR 0x68

uint8_t get_len_of_data(void)
{
return (uint8_t)strlen(name_msg);
}
void setup() {
// Define the LED pin as Output
pinMode (LED, OUTPUT);

// Start the I2C Bus as Slave on address 9
Wire.begin(SLAVE_ADDR);

// Attach a function to trigger when something is received.
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);


}


//write
void receiveEvent(int bytes) {
active_command = Wire.read(); // read one character from the I2C
}

//read
void requestEvent() {

if(active_command == 0X51)
{
uint8_t len = get_len_of_data();
Wire.write(&len,1);
active_command = 0xff;
}


if(active_command == 0x52)
{
// Wire.write(strlen(name));
Wire.write(name_msg,get_len_of_data());
// Wire.write((uint8_t*)&name_msg[32],18);
active_command = 0xff;
}
//Wire.write("hello "); // respond with message of 6 bytes
// as expected by master
}
void loop() {


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Wire Master Transmitter and Receiver
//Uno, Ethernet A4 (SDA), A5 (SCL)
#include <Wire.h>

// Include the required Wire library for I2C<br>#include <Wire.h>
int LED = 13;

uint8_t rcv_buf[32];

int data_len=0;
#define SLAVE_ADDR 0x69

void setup() {
Serial.begin(9600);

// Define the LED pin as Output
pinMode (LED, OUTPUT);

// join i2c bus (address optional for master)
Wire.begin();



}


void loop() {

Serial.println("Arduino Master");
Serial.println("Send character \"s\" to begin");
Serial.println("-----------------------------");

while(!Serial.available());
char in_read=Serial.read();

while(in_read != 's');

Serial.println("Starting..");

Wire.beginTransmission(SLAVE_ADDR);

Wire.write(0X51); //Send this command to read the length
Wire.endTransmission();


Wire.requestFrom(SLAVE_ADDR,1); // Request the transmitted two bytes from the two registers

if(Wire.available()) { //
data_len = Wire.read(); // Reads the data
}
Serial.print("Data Length:");
Serial.println(String(data_len,DEC));

Wire.beginTransmission(SLAVE_ADDR);

Wire.write(0X52); //Send this command to ask data
Wire.endTransmission();

Wire.requestFrom(SLAVE_ADDR,data_len);

uint32_t i=0;
for( i =0; i <= data_len ; i++)
{
if(Wire.available()) { //
rcv_buf[i] = Wire.read(); // Reads the data
}
}
rcv_buf[i] = '\0';

Serial.print("Data:");
Serial.println((char*)rcv_buf);
Serial.println("*********************END*********************");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Wire Master Transmitter and Receiver
//Uno, Ethernet A4 (SDA), A5 (SCL)
#include <Wire.h>

// Include the required Wire library for I2C<br>#include <Wire.h>
int LED = 13;

uint8_t rcv_buf[512];

uint32_t data_len=0,w_ptr = 0;
#define SLAVE_ADDR 0x68

void setup() {
Serial.begin(9600);

// Define the LED pin as Output
pinMode (LED, OUTPUT);

// join i2c bus (address optional for master)
Wire.begin();



}


void loop() {

uint32_t rem_len=0,last_read=0;
Serial.println("Arduino Master");
Serial.println("Send character \"s\" to begin");
Serial.println("-----------------------------");

while(!Serial.available());
char in_read=Serial.read();

while(in_read != 's');

Serial.println("Starting..");

Wire.beginTransmission(SLAVE_ADDR);

Wire.write(0X51); //Send this command to read the length
Wire.endTransmission();


Wire.requestFrom(SLAVE_ADDR,4); // Request the transmitted two bytes from the two registers

for(uint32_t len = 0 ; len < 4; len++)
{
if(Wire.available()) { //
uint32_t data = (uint32_t)Wire.read();
data_len |= ( data << ( 8 * len) ); // Reads the data
}
}
//data_len = 0x2f8;
Serial.print("Data Length:");
Serial.println(data_len);

Wire.beginTransmission(SLAVE_ADDR);

Wire.write(0X52); //Send this command to ask data
Wire.endTransmission();

rem_len = data_len;
while(rem_len > 0)
{
if(rem_len <= 32)
{
Wire.requestFrom(SLAVE_ADDR,rem_len);
last_read = rem_len;
rem_len = 0;
}else
{
Wire.requestFrom(SLAVE_ADDR,32);
last_read = 32;
rem_len -= 32;
}
uint32_t i=0;
for( i =0; i <= last_read ; i++)
{
if(Wire.available()) { //
rcv_buf[w_ptr++] = Wire.read(); // Reads the data
}
}
}

rcv_buf[w_ptr] = '\0';
w_ptr = 0;
Serial.print("Data:");
Serial.println((char*)rcv_buf);
Serial.println("*********************END*********************");
}
115 changes: 115 additions & 0 deletions Resources/Arduino/spi/001SPISlaveRxString/001SPISlaveRxString.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* SPI Slave Demo
*
* SPI pin numbers:
* SCK 13 // Serial Clock.
* MISO 12 // Master In Slave Out.
* MOSI 11 // Master Out Slave In.
* SS 10 // Slave Select . Arduino SPI pins respond only if SS pulled low by the master
*
*/
#include <SPI.h>
#include<stdint.h>
#define SPI_SCK 13
#define SPI_MISO 12
#define SPI_MOSI 11
#define SPI_SS 10

char dataBuff[500];
//Initialize SPI slave.
void SPI_SlaveInit(void)
{
#if 0
// Initialize SPI pins.
pinMode(SPI_SCK, INPUT);
pinMode(SPI_MOSI, INPUT);
pinMode(SPI_MISO, OUTPUT);
pinMode(SPI_SS, INPUT);

// Enable SPI as slave.
SPCR = (1 << SPE);
#endif
// Initialize SPI pins.
pinMode(SCK, INPUT);
pinMode(MOSI, INPUT);
pinMode(MISO, OUTPUT);
pinMode(SS, INPUT);
//make SPI as slave

// Enable SPI as slave.
SPCR = (1 << SPE);
}

//This function returns SPDR Contents
uint8_t SPI_SlaveReceive(void)
{
/* Wait for reception complete */
while(!(SPSR & (1<<SPIF)));
/* Return Data Register */
return SPDR;
}


//sends one byte of data
void SPI_SlaveTransmit(char data)
{
/* Start transmission */
SPDR = data;

/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)));
}


// The setup() function runs right after reset.
void setup()
{
// Initialize serial communication
Serial.begin(9600);

// Initialize SPI Slave.
SPI_SlaveInit();

Serial.println("Slave Initialized");
}
uint16_t dataLen = 0;
uint32_t i = 0;
// The loop function runs continuously after setup().
void loop()
{



Serial.println("Slave waiting for ss to go low");
while(digitalRead(SS) );

// Serial.println("start");

//1. read the length
// dataLen = (uint16_t)( SPI_SlaveReceive() | (SPI_SlaveReceive() << 8) );
//Serial.println(String(dataLen,HEX));
i = 0;
dataLen = SPI_SlaveReceive();
for(i = 0 ; i < dataLen ; i++ )
{
dataBuff[i] = SPI_SlaveReceive();
}


// Serial.println(String(i,HEX));
dataBuff[i] = '\0';

Serial.println("Rcvd:");
Serial.println(dataBuff);
Serial.print("Length:");
Serial.println(dataLen);




}




Binary file not shown.
Loading

0 comments on commit 2cee859

Please sign in to comment.