Show Posts
Pages: [1] 2 3
1  Using Arduino / Networking, Protocols, and Devices / Re: Bug on I2C (onReceive) on: September 20, 2013, 12:42:32 pm
Ok, thanks for the tip Nick!  smiley

2  Using Arduino / Programming Questions / sizeof(array) does not return the real size under some circumstances on: September 20, 2013, 12:41:18 pm
This bug is present in 1.0.5, need to test it on 1.5.3.

This code will return the correct size:

Code:
void setup()
{
  Serial.begin(115200);
  byte b=10;
  byte data[]={0x1,b,0x2};
  b=sizeof(data);
  Serial.print(b);
}
void loop(){}



While this other wont:

Code:
void setup()
{
  Serial.begin(115200);
  byte b=10;
  byte data[]={0x1,b,0x2};
  crap(data);
}
void loop(){}

void crap(byte pep[])
{
  byte b=sizeof(pep);
  Serial.print(b);
}

So, if you send a variable to a sub inside an array, it wont be counted as data, even though the array bytes can be correctly retrieved inside the sub.
3  Using Arduino / Networking, Protocols, and Devices / Bug on I2C (onReceive) on: September 10, 2013, 09:54:30 am
Well, to make it more clear, i will post two codes that do the same thing, one works and the other doesnt due to this bug.

Working code:

Code:
#include <Wire.h>
char buffer[258];
#define LED 13
byte poop=0;
 #define THIS_ADDRESS 0x9
 #define OTHER_ADDRESS 0x8


 void setup() {
   pinMode(LED, OUTPUT);
   digitalWrite(LED, LOW);
   Wire.begin(THIS_ADDRESS);
   Wire.onReceive(receiveEvent);
   Serial.begin(115200);
 }

 void loop() {
   if (poop==1)
   {
     reply();
   }
 }


void reply()
{
  Wire.beginTransmission(OTHER_ADDRESS);
    Wire.write(0xFE);
    Wire.endTransmission();
    poop=0;
}

 void receiveEvent(int howMany){
   int counter=0;
  while(0 < Wire.available())
  {
    buffer[counter]=Wire.read();
    counter++;
  }
  
  switch (buffer[0]) {
    
    case 0xFF: //master is checking if slave is alive
    poop=1;
    break;
  }
 }

In this example, i change a global variable name inside the "receiveEvent()" function. Then, outside of this function, i reply with the code inside "reply()". This works perfectly.

Now:

Code:
#include <Wire.h>
char buffer[258];
#define LED 13
 #define THIS_ADDRESS 0x9
 #define OTHER_ADDRESS 0x8


 void setup() {
   pinMode(LED, OUTPUT);
   digitalWrite(LED, LOW);
   Wire.begin(THIS_ADDRESS);
   Wire.onReceive(receiveEvent);
   Serial.begin(115200);
 }

 void loop() {
 }




 void receiveEvent(int howMany){
   int counter=0;
  while(0 < Wire.available())
  {
    buffer[counter]=Wire.read();
    counter++;
  }
  
  switch (buffer[0]) {
    
    case 0xFF: //master is checking if slave is alive
   [b] Wire.beginTransmission(OTHER_ADDRESS);
    Wire.write(0xFE);
    Wire.endTransmission();[/b]
    break;
  }
 }

If i try to reply directly inside "receiveEvent()", the I2C bus will crash and he whole MCU will stop working. It wont be able to do the "Wire.endTransmission()".

This can easily be replicated by using the above indicated code.

Working code for the master is here:

Code:

#include <Wire.h>
byte buffer[258];


 #define THIS_ADDRESS 0x8
 #define OTHER_ADDRESS 0x9


 void setup() {
  
   Wire.begin(THIS_ADDRESS);
   Wire.onReceive(receiveEvent);
   Serial.begin(115200);
   Serial.println("Ready!");
   Serial.print("Checking if slave is alive...");
   Wire.beginTransmission(OTHER_ADDRESS);
   Wire.write(0xFF);
   Wire.endTransmission();
 }

 void loop() {
   delay(100);
 }

 void receiveEvent(int howMany){
   int counter=0;
  while(0 < Wire.available())
  {
    buffer[counter]=Wire.read();
    counter++;
  }
  switch (buffer[0]) {
    
    case 0xFE: //master is checking if slave is alive
    Serial.println("Present!");
    break;
  }
 }


BR

4  Using Arduino / Programming Questions / Re: SdFat writes weird stuff to binary on SD on: January 27, 2013, 03:40:56 pm
yes, i didnt pretty much explain myself here about the time thing, my bad  smiley-roll-sweat

5  Using Arduino / Programming Questions / Re: SdFat writes weird stuff to binary on SD on: January 27, 2013, 03:09:56 pm
it reads 512kb of data in blocks of 512bytes, but since the buffer is 512bytes, it will just loop until the data stream is done. i believe that 59 seconds is a really really fast operation time, but if you think something could be improved, please let me know.

I measure the time by a inittime=millis() at the beginning, and endtime=(millis()-inittime)/1000) at the end.

The full code for the project is at this point 2347 lines, so i just extracted the SD routines into the sketch that i have posted earlier.

BR
6  Using Arduino / Programming Questions / Re: SdFat writes weird stuff to binary on SD on: January 27, 2013, 09:44:27 am
Ok, just changed the loop for a myFile.write(SDbuffer, SDcounter); and it speeded up a 80secs operation to become a 59secs operation.


BIG thanks for your help PaulS! smiley


BR
7  Using Arduino / Programming Questions / Re: SdFat writes weird stuff to binary on SD on: January 27, 2013, 09:34:43 am
It was a hardware fault. I just fixed it, and now both codes posted work perfectly with the fix you mention aswell.

Anyway, i am interested on what you mention in writing a byte at a time.

I would really appreciate if you could let me know the correct syntax to write the entire buffer at a time.


BR
8  Using Arduino / Programming Questions / Re: SdFat writes weird stuff to binary on SD on: January 26, 2013, 07:31:42 pm
original code:

Code:
byte SDbuffer[512];//Buffer to be saved on SD
word SDcounter=0;//counter to know how many bytes will be written to the SD
#include <SD.h>
File myFile;
byte c;

void error_P(const char* str) {   
  PgmPrint("error: ");   
  SerialPrintln_P(str);   
  if (card.errorCode())
    {     
      PgmPrint("SD error: ");     
      Serial.print(card.errorCode(), HEX);     
      Serial.print(',');     
      Serial.println(card.errorData(), HEX);   
    }   
  while(1);
}

void setup()
{
Serial.begin(115200);
Serial2.begin(9600);
pinMode(53, OUTPUT);
  Serial.println("Initializing SD card...");
  if (!SD.begin(4))
    {
      Serial.println("initialization failed!");
      Serial.println("Please check that SD card is inserted and formatted as FAT16");
      fail=1;
       while (fail){}
    }
 myFile = SD.open("log.bin", O_CREAT | O_WRITE);//We do it the fast way
Serial.println("starting to log...");
}

void loop()
{

  while (SDcounter<513)
  {
   while (Serial2.available() < 1)
   {}
   c = Serial2.read();
   SDbuffer[SDcounter]=c;
  SDcounter++;
  }
  int sdloop=0;
  myFile= SD.open("log.bin", FILE_READ);
if (!myFile){
    // if the file didn't open, print an error:
    Serial.println("Error writing to SD card, please check it is not write protected");
    return;
  }

    while (sdloop < SDcounter){
    myFile.write(SDbuffer[sdloop]);
   sdloop++;
    }
    SDcounter=0;
myFile.close();
}

new code:

Code:
byte SDbuffer[512];//Buffer to be saved on SD
word SDcounter=0;//counter to know how many bytes will be written to the SD
#include <SdFat.h>
#include <SdFatUtil.h>
SdFat SD;
Sd2Card card;
SdVolume volume;
SdFile root;
const uint8_t SD_CHIP_SELECT = 53;
SdFile myFile;
byte SDbufferFlag;
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))

byte c;

void error_P(const char* str) {   
  PgmPrint("error: ");   
  SerialPrintln_P(str);   
  if (card.errorCode())
    {     
      PgmPrint("SD error: ");     
      Serial.print(card.errorCode(), HEX);     
      Serial.print(',');     
      Serial.println(card.errorData(), HEX);   
    }   
  while(1);
}

void setup()
{
Serial.begin(115200);
Serial2.begin(9600);
  Serial.print("Initializing SD card...");
  //SD card INIT
  if (!card.init(SPI_HALF_SPEED)) error("card.init failed");
  // initialize a FAT volume  
  if (!volume.init(&card)) error("volume.init failed");  
  // open the root directory  
  if (!root.openRoot(&volume)) error("openRoot failed");
  Serial.println("Done!");
}

void loop()
{

  while (SDcounter<513)
  {
   while (Serial2.available() < 1)
   {}
   c = Serial2.read();
   SDbuffer[SDcounter]=c;
  SDcounter++;
  }
  int sdloop=0;
  if (!myFile.open(&root,"log.bin", O_RDWR | O_CREAT)) {
    // if the file didn't open, print an error:
    Serial.println("Error writing to SD card, please check it is not write protected");
    return;
  }

    while (sdloop < SDcounter){
    myFile.write(SDbuffer[sdloop]);
   sdloop++;
    }
    SDcounter=0;
myFile.close();
}

BR
9  Using Arduino / Programming Questions / SdFat writes weird stuff to binary on SD on: January 26, 2013, 01:37:04 pm
I had a code that ran with standard SD library fine, which would write all binary bytes read on serial to a file on sd. The sketch for the function was:

Code:
   
//SDcounter is the number of bytes stored in buffer
byte SDbuffer[512];//Buffer to be saved on SD
int sdloop=0;
    while (sdloop < SDcounter){
    myFile.write(SDbuffer[sdloop]);
   sdloop++;
    }

Then i upgraded to SdFat and tried with the same function, but it writes the file with the correct size, but the data in it is completely wrong.

And if i try this:

Code:
myFile.write(SDbuffer, SDcounter);

it will print a file full of "0x00, 0x33, 0x00, 0x33.......".

Could someone please give a hand to let me know what am i doing wrong?

BR
10  Products / Arduino Due / Re: Disabling UART on Due? on: January 13, 2013, 07:38:30 pm
You are totally right stimmer, just checked it with the LA and it works perfectly. I must have done something wrong while trying it.

Big thanks for the help!

BR
11  Products / Arduino Due / Re: Disabling UART on Due? on: January 13, 2013, 03:51:25 pm
Thanks for your reply Markus.

You mean one like this one?: http://www.nxp.com/documents/application_note/AN00093.pdf

BR
12  Products / Arduino Due / Re: Disabling UART on Due? on: January 12, 2013, 02:48:22 pm
Thanks for the tip stimmer!

I changed it from USART2 to USART1, but still getting exactly the very same thing on the LA.

The weird thing is that it does the "LOW/HIGH" thing before serial printing "123456" for the first time, while that is supposed to go after printing it.


BR
13  Products / Arduino Due / Re: Disabling UART on Due? on: January 12, 2013, 12:47:43 pm
Now, this is weird...

This code:

Code:
#define IN    17 //RX2
#define OUT   16 //TX2

void setup(){
Serial2.begin(115200);
Serial.begin(115200);
Serial2.print("123456");
delay(1000);
USART2->US_CR = US_CR_RXDIS;
USART2->US_CR = US_CR_TXDIS;
pinMode(OUT, OUTPUT);
pinMode(IN, INPUT);
delay(1000);
  digitalWrite(OUT, HIGH);
  delay(200);
  digitalWrite(OUT, LOW);
  delay(200);
  digitalWrite(OUT, HIGH);
  delay(200);
  digitalWrite(OUT, LOW);
  delay(1400);
USART2->US_CR = US_CR_TXEN;
USART2->US_CR = US_CR_RXEN;
  Serial2.print("123456");
  delay(20);
  Serial2.print("123456");
  Serial.print("Done");
}
void loop(){}

Gives the following  response:



Uploaded with ImageShack.us

The red square represents the first (one time) "Serial2.print("123456");".

Any clues of what might be happening here?

BR
14  Products / Arduino Due / Re: Disabling UART on Due? on: January 12, 2013, 12:21:50 pm
I have found what i was looking for:

Code:
USART2->US_CR = US_CR_TXEN;//Enables USART2 TX
USART2->US_CR = US_CR_RXEN;//Enables USART2 RX
USART2->US_CR = US_CR_TXDIS;//Disables USART2 TX
USART2->US_CR = US_CR_RXDIS;//Disables USART2 RX

But now i find that if i have set the pins as digital previously like this:

Code:
pinMode(16, OUTPUT);
pinMode(17, INPUT);

the USART2 wont start, and will stay in LOW status, and therefor, not read anything.

Any clues on how to solve that?

I already tried the

Code:
g_APinDescription[pin].pPort -> PIO_PER = g_APinDescription[pin].ulPin;  (pin is digital io)
g_APinDescription[pin].pPort -> PIO_PDR = g_APinDescription[pin].ulPin;  (pin is peripheral)

and it didnt do the job.

BR
15  Using Arduino / Storage / Re: Read a specific offset from an fat16 SD card file? on: January 10, 2013, 01:44:00 pm
Worked 100%

Thanks!!!
Pages: [1] 2 3