-
-
-
File not uploaded, so I copied the code here.
Please find here the example firmware//================================================================================== // Copyright (c) 2014 Giovanni Varasano. // v4 - last update 06/06/2014 // ================================================================================== var fs = require("fs"); var ST = { IDLE: 0, FIX:1, RUN:2 }; tmrId=0; file_ok=0, gps_ok = 0; gps_path=""; gps_date= "", gps_time=""; gps_msec=0; gps_lat=0, gps_lon=0, gps_speed =0; gps_course=0; ir_red=0.0,ir_nir=0.0,irr_edg=0.0,ir_ndre=0.0,ir_ndvi=0.0; //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- var WORK = LED2; var FIX = LED3; var STOP = LED4; // --------------------------------------------------------- // --------------------------------------------------------- btn_state = false; var btn_id = setWatch(function(e) { btn_state = !btn_state; } ,B6,{ repeat : true, edge : "both", debounce : 350}); //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- function onInit() { setTimeout(function(){ file_ok=0, gps_ok = 0; Serial6.setup(19200, {rx:C7,tx:C6}); Serial6.print("$JBAUD,115200\r\n"); setTimeout(function(){ Serial6.setup(115200, {rx:C7,tx:C6}); Serial4.setup(38400, {rx:C11,tx:C10}); tracking("RMC"); tracking("GGA"); tmrId = go_ahead(); }, 3000 ); }, 2000 ); } //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- function go_ahead() { state=ST.IDLE return setInterval( function() { switch(state) { case ST.IDLE: file_ok=0;STOP.set();FIX.reset();WORK.reset(); if (btn_state ) { STOP.reset(); state=ST.FIX; } break; case ST.FIX: if (!btn_state ) { state=ST.IDLE } FIX.set(); if(file_ok === 1) {FIX.reset();state=ST.RUN;work=WORK.startFlashing(250);} break; case ST.RUN: if (!btn_state ) {state=ST.IDLE; WORK.stopFlashing(work);} save_data(); break; } },50); } //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- function save_data() { if ( gps_ok ) { var s_data = gps_date+","+gps_time+"."+gps_msec+","+gps_speed+","+gps_course+","; s_data += utm.toString()+","+ir_red+","+ir_nir+","+irr_edg+","+ir_ndre+","+ir_ndvi+"\r\n"; fs.appendFileSync(gps_path,s_data); gps_ok=0; } } //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- function LatLng(lat,lng,alt) { this.lat = lat; this.lng = lng; this.alt = alt; this.toUTMRef = LatLngToUTMRef; this.toString = LatLngToString; } //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- function LatLngToString() { return "(" + this.lat + ", " + this.lng + ")"; } //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- function UTMRef(easting, northing, altitude, latZone, lngZone) { this.easting = Number(easting).toFixed(3); this.northing = Number(northing).toFixed(3); this.altitude = Number(altitude).toFixed(3); this.latZone = latZone; this.lngZone = lngZone; this.toString = UTMRefToString; } //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- function UTMRefToString() { return this.easting + "," + this.northing+ ","+this.altitude+","+this.latZone+this.lngZone+""; } //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- function RefEll(maj, min) { this.maj = maj; this.min = min; this.ecc = ((maj * maj) - (min * min)) / (maj * maj); } //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- function LatLngToUTMRef() { var wgs84 = new RefEll(6378137, 6356752.314); var UTM_F0 = 0.9996; var a = wgs84.maj; var eSquared = wgs84.ecc; var longitude = this.lng; var latitude = this.lat; var altitude = this.alt; var latitudeRad = latitude * (Math.PI / 180.0); var longitudeRad = longitude * (Math.PI / 180.0); var longitudeZone = Math.floor((Number(longitude) + 180.0) / 6.0) + 1; if (latitude >= 56.0 && latitude < 64.0 && longitude >= 3.0 && longitude < 12.0) { longitudeZone = 32; } if (latitude >= 72.0 && latitude < 84.0) { if (longitude >= 0.0 && longitude < 9.0) { longitudeZone = 31; } else if (longitude >= 9.0 && longitude < 21.0) { longitudeZone = 33; } else if (longitude >= 21.0 && longitude < 33.0) { longitudeZone = 35; } else if (longitude >= 33.0 && longitude < 42.0) { longitudeZone = 37; } } var longitudeOrigin = (longitudeZone - 1) * 6 - 180 + 3; var longitudeOriginRad = longitudeOrigin * (Math.PI / 180.0); var UTMZone = getUTMLatitudeZoneLetter(latitude); ePrimeSquared = (eSquared) / (1 - eSquared); var n = a / Math.sqrt(1 - eSquared * Math.sin(latitudeRad) * Math.sin(latitudeRad)); var t = Math.tan(latitudeRad) * Math.tan(latitudeRad); var c = ePrimeSquared * Math.cos(latitudeRad) * Math.cos(latitudeRad); var A = Math.cos(latitudeRad) * (longitudeRad - longitudeOriginRad); var M = a * ( (1 - eSquared / 4 - 3 * eSquared * eSquared / 64 - 5 * eSquared * eSquared * eSquared / 256) * latitudeRad - (3 * eSquared / 8 + 3 * eSquared * eSquared / 32 + 45 * eSquared * eSquared * eSquared / 1024) * Math.sin(2 * latitudeRad) + (15 * eSquared * eSquared / 256 + 45 * eSquared * eSquared * eSquared / 1024) * Math.sin(4 * latitudeRad) - (35 * eSquared * eSquared * eSquared / 3072)* Math.sin(6 * latitudeRad)); var UTMEasting = (UTM_F0 * n * (A + (1 - t + c) * Math.pow(A, 3.0) / 6 + (5 - 18 * t + t * t + 72 * c - 58 * ePrimeSquared) * Math.pow(A, 5.0) / 120) + 500000.0); var UTMNorthing = (UTM_F0 * (M + n * Math.tan(latitudeRad) * (A * A / 2 + (5 - t + (9 * c) + (4 * c * c)) * Math.pow(A, 4.0) / 24 + (61 - (58 * t) + (t * t) + (600 * c) - (330 * ePrimeSquared)) * Math.pow(A, 6.0) / 720)) ); if (latitude < 0) { UTMNorthing += 10000000.0; } return new UTMRef(UTMEasting, UTMNorthing, altitude, UTMZone, longitudeZone); } //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- function getUTMLatitudeZoneLetter(latitude) { if ((84 >= latitude) && (latitude >= 72)) return "X"; else if (( 72 > latitude) && (latitude >= 64)) return "W"; else if (( 64 > latitude) && (latitude >= 56)) return "V"; else if (( 56 > latitude) && (latitude >= 48)) return "U"; else if (( 48 > latitude) && (latitude >= 40)) return "T"; else if (( 40 > latitude) && (latitude >= 32)) return "S"; else if (( 32 > latitude) && (latitude >= 24)) return "R"; else if (( 24 > latitude) && (latitude >= 16)) return "Q"; else if (( 16 > latitude) && (latitude >= 8)) return "P"; else if (( 8 > latitude) && (latitude >= 0)) return "N"; else if (( 0 > latitude) && (latitude >= -8)) return "M"; else if (( -8 > latitude) && (latitude >= -16)) return "L"; else if ((-16 > latitude) && (latitude >= -24)) return "K"; else if ((-24 > latitude) && (latitude >= -32)) return "J"; else if ((-32 > latitude) && (latitude >= -40)) return "H"; else if ((-40 > latitude) && (latitude >= -48)) return "G"; else if ((-48 > latitude) && (latitude >= -56)) return "F"; else if ((-56 > latitude) && (latitude >= -64)) return "E"; else if ((-64 > latitude) && (latitude >= -72)) return "D"; else if ((-72 > latitude) && (latitude >= -80)) return "C"; else return 'Z'; } // ----------------------------------------------------------- // -------------------------------------------------- var Checksum = function(sentence, checksum) { var q, c1, c2, i; i = 1; c1 = sentence.charCodeAt(i); for( i = 2; i < sentence.length; ++i) { c1 = c1 ^ sentence.charCodeAt(i); } c2 = parseInt(checksum, 16); return (c1 === c2); }; // ----------------------------------------------------------- // -------------------------------------------------- var m_hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']; // ----------------------------------------------------------- // -------------------------------------------------- var toHexString = function(v) { var lsn, msn; msn = (v >> 4) & 0x0f; lsn = (v >> 0) & 0x0f; return m_hex[msn] + m_hex[lsn]; }; // ----------------------------------------------------------- // -------------------------------------------------- var computeChecksum = function(sentence) { var c1; var i; i = 1; c1 = sentence.charCodeAt(i); for( i = 2; i < sentence.length; ++i) { c1 = c1 ^ sentence.charCodeAt(i); } return '*' + toHexString(c1); }; // ----------------------------------------------------------- // ----------------------------------------------------------- function handleGPSLine(line ,callback) { var c = line.split("*"); if ( Checksum(c[0],c[1]) ) { var tag = line.substr(3,3); var d = line.split(","); if (tag=="GGA") { var dlat = d[2].indexOf("."); var dlon = d[4].indexOf("."); callback({ tag : tag, time : d[1].substr(0,2)+":"+d[1].substr(2,2)+":"+d[1].substr(4,2), msec : (parseInt(d[1].substr(7,2),10)*10), lat : ((parseInt(d[2].substr(0,dlat-2),10)+parseFloat(d[2].substr(dlat-2))/60)*(d[3]=="S"?-1:1)).toFixed(9), lon : ((parseInt(d[4].substr(0,dlon-2),10)+parseFloat(d[4].substr(dlon-2))/60)*(d[5]=="W"?-1:1)).toFixed(9), fix : parseInt(d[6],10), alt : parseFloat(d[9]) }); } if (tag=="RMC") { callback({ tag : tag, time : d[1].substr(0,2)+":"+d[1].substr(2,2)+":"+d[1].substr(4,2), valid : d[2], speed : parseFloat(d[7])*1.852, course : parseFloat(d[8]), date : d[9].substr(0,2)+"/"+d[9].substr(2,2)+"/"+d[9].substr(4,2), path : d[9].substr(4,2)+d[9].substr(2,2)+d[9].substr(0,2)+"_"+d[1].substr(0,2)+d[1].substr(2,2)+".txt" }); } } } // ----------------------------------------------------------- // -------------------------------------------------- function fix_gps_data(data) { if ( data.fix > 0 ) { gps_ok = 1; gps_time=data.time; gps_msec=data.msec; gps_lat=data.lat; gps_lon=data.lon; var ll = new LatLng(data.lat,data.lon,data.alt); utm = ll.toUTMRef(); } else { gps_ok = 0; } } // ----------------------------------------------------------- // -------------------------------------------------- function get_gps_data(data) { if(data.tag == "GGA" ) { fix_gps_data(data); } if(data.tag == "RMC" ) { fix_data(data); } } // ----------------------------------------------------------- // ----------------------------------------------------------- function fix_data(data) { if ( data.valid=="A" ) { if ( file_ok === 0) { file_ok=1; gps_date = data.date; gps_path = data.path; var head = "# File: "+gps_path+" created on "+data.date+" "+data.time+"\r\n"; fs.appendFileSync(gps_path,head); } gps_speed = data.speed; gps_course = data.course; } } // ----------------------------------------------------------- // -------------------------------------------------- function fix_irs_data(data) { ir_red = data.red; ir_nir = data.nir; irr_edg = data.red_edg; ir_ndre = data.ndre; ir_ndvi = data.ndvi; } // --------------------------------------------------- // // -------------------------------------------------- function handleIRSLine(line ,callback) { var d = line.split(","); callback({ red : parseFloat(d[0]), nir : parseFloat(d[1]), red_edg : parseFloat(d[2]), ndre : parseFloat(d[3]), ndvi : parseFloat(d[4]) }); } // ----------------------------------------------------------- // -------------------------------------------------- Serial6.onData(function (e) { if (e.data=="\n") { var line = gpsLine; gpsLine=""; handleGPSLine(line ,function(data) { get_gps_data(data); } ); } else gpsLine+=e.data; } ); // ----------------------------------------------------------- // -------------------------------------------------- Serial4.onData(function (e) { if (e.data=="\n") { var line = irsLine; irsLine=""; handleIRSLine(line ,function(data) { fix_irs_data(data); } ); } else irsLine+=e.data; } ); // -------------------------------------- // -------------------------------------- var tracking = function(cmd ) { var str = "$J"; if (cmd =="GGA" ) { str += "ASC,GP"+cmd+",10"; } else if ( cmd == "RMC" ) { str += "ASC,GP"+cmd+",1"; } else { } if (cmd == "OFF") { str += cmd; } str += "\r\n"; Serial6.print(str); }; // ---------------------------------------------- // ---------------------------------------------- Pin.prototype.startFlashing = function(period) { var on = false; var pin = this; return setInterval(function() { on = !on; digitalWrite(pin, on); }, period); }; // ---------------------------------------------- // ---------------------------------------------- Pin.prototype.stopFlashing = function(stop ) { var pin = this; clearInterval(stop);digitalWrite(pin,0); }; onInit();
-
-
-
You can use a moving average with an interval of 8 or 16 samples .
I used the following code to get stable readings from a 10-bit encoder.
Using the dithered noise resolution is in fact improved to 1 1bits.setInterval(function() { enc_deg = sma16(degree(C1).toFixed(1)); // console.log("Encoder Angle[°] = "+enc_deg.toFixed(1)); }, 100 ); function degree( pin ) { var ratio = 4.0; var offset = 0.5; var factor = 0.73333333333; var phys = analogRead(pin) * 3.3; // *E.getAnalogVRef(); var meas = 360.0 * ( phys / factor - offset ) / ratio ; console.log("Input[V] = "+phys.toFixed(3)+" Angle[°] = "+meas.toFixed(1)); return meas; } // ------------------------------------ // simple parametric moving averager // ------------------------------------ function sma(period) { var nums = []; return function(num) { nums.push(num); if (nums.length > period) nums.splice(0,1); var sum = 0; var n = period; function sum_num (n) { sum+=Number(n); }; nums.forEach(sum_num); if (nums.length < period) n = nums.length; return(sum/n); } } var sma16 = sma(16);
Input[V] = 0.494 Angle[°] = 15.6 Next reading = 15.6, SMA = 15.6
Input[V] = 0.496 Angle[°] = 15.9 Next reading = 15.9, SMA = 15.7
Input[V] = 0.495 Angle[°] = 15.8 Next reading = 15.8, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.8 Next reading = 15.8, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.7 Next reading = 15.7, SMA = 15.7
Input[V] = 0.498 Angle[°] = 16.1 Next reading = 16.1, SMA = 15.8
Input[V] = 0.497 Angle[°] = 16.0 Next reading = 16.0, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.7 Next reading = 15.7, SMA = 15.8
Input[V] = 0.496 Angle[°] = 15.9 Next reading = 15.9, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.7 Next reading = 15.7, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.7 Next reading = 15.7, SMA = 15.8
Input[V] = 0.494 Angle[°] = 15.6 Next reading = 15.6, SMA = 15.8
Input[V] = 0.494 Angle[°] = 15.6 Next reading = 15.6, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.8 Next reading = 15.8, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.7 -
I understood the problem.
In order to have a local filesystem stored on a SD card and being able to use the "require " method on the needed files an initial delay must be setup inside the onInit() function.
This is needed not when the code is executed from the terminal, but for the code saved in flash after the execution of the save() function.
On my STM32F4Discovery board 1 second was not enough so I setup for a 2s delay.
After having down this, all worked pretty fine. non garbage on the LCD even restarting
It would be however interesting to know why such a delay is needed.My code was the following :
var fs ; var lcd ; // ============== LCD ====================== var lcd; var l = false; // var ON =1, OFF = 0; var tmo_id1; var root; // LCD background LED function LCD_bckgnd(pin,status ) { digitalWrite(pin,status); } // var lcd_on = LCD_bckgnd(ON,E7); var lcd_off = LCD_bckgnd(OFF,E7); /* var DELAY(func,interval) = */ function onInit() { // delay a while …(2s) setTimeout(function(){ fs = require('fs'); root = fs.readdir(); console.log("onInit(): OK."); console.log("SD card File System:",root ); console.log("--------------------------------"); var files = fs.readdir(root); (function(){ for(var i=0;i<files.length;i++){ console.log(" -->",files[i]); } })(); lcd = require("HD44780").connect(C5,C4,C0,C1,C2,C3); console.log("--------------------------------"); LCD_bckgnd(E7,true); lcd.clear(); lcd.setCursor(0,0); lcd.print(""); // bug fist char lost lcd.print("Board OK ..."); lcd.setCursor(0,1); lcd.print("flashing LED1..."); },2000); tmo_id1 = setInterval( function() { l = !l; LED1.write(l);} , 500); function logo(){ lcd.setCursor(0,1); lcd.print("G.Varasano @2014"); } setTimeout(logo,5000); } // clearInterval(tmo_id1); onInit();
-
-
-
I think to have done nothing strange after all.
I simply wanted to enable the SD card on the STm32F4 Discovery board.
I wrote USE_FILESYSTEM=1 in the Makefile
I added the pin definitions for the board in the .py file as for the Espruino board.
After that I compiled the ESPRUINO source code in order to get a new elf and/or bin file.
Using the same board on two different environment I got now two different results :
a) EEPC using Windows7 plus WEB IDE but PC not linked to the Internet .
Everything works fine.b) Mac osX WEB IDE ( PC linked to the Internet )
Same board, same firmware, the program hangs up.To me is now evident that the behavior depends on how the require machinery works.
I must investigate detailed, but I would appreciate if some explanation, even short, could be given by anyone.
One could build hundreds of Espruino boards, but it is important to have a complete knowledge about it-s inner workings, otherwise the simplicity of programming with respect traditional Embedded C programming will be vain if a situation can arise when the control -
-
-
Thanks a lot for your firm reply, considering that I am working on an unsupported board.
I know that I changed a lot of things and I know that I must learn Javascript to a deeper level of detail before using it.
I am just starting to learn it.
It is powerful but I must keep my "Eyes wide shut" before using it.
As you suggest I'll download the Chrome from the app store, even the one I used worked till this morning. This stuff is really new to me.
I must just relax and take it easy before using Espruino in a production environment, even it is a really good design for the STFxx embedded systems. -
-
Strange behavior.
On MAc OSX (WEB IDE Linked to Ethernet ) since this morning I am not able to have a serial communication.
But this may be a failure of the Mac.
On the Windows side, ( WEB IDE not linked to Ethernet )no problems with communications using either the USB cable plugged or unplugged.
The behavior is the same.
SD card inserted, otherwise DISK_ERR is issued back.
GOOD LCD display except col 0, row 0 position is shifted left when code is downloaded from Web IDE.
Now after first and following resets ( with USB cable plugged )
"oard OK… "
2nd row goodafter several resets with USB cable unplugged the LCD begins to be be filled with garbages.
Downloading the code from WEB IDE is always good .
Javascript is straightforward in developing applications because it avoids the frequent compilation, debug , recompilation cycles usually done with C programming.
Anyway it seems that is a little deficient with respect debugging.
Is there a suggested way to perform debug in a better way for a JS beginner ? -
I am going to check.
Only 4 bits are used and connected on what appear to be right pins. (C5,C4, C0,C1,C2,C3).
I made a checklist of already used wires.
The remaining 4 wires are not used at all .
I'll begin to work out all the warnings and then let you know.Maybe try executing the code after a timeout on onInit?
No the code was executed before a SetInterval()
Yes I put the flashing of the led inside the onInit() and this seems to cause problems to the serial communication on Mac OsX as soon as the USB cable is plugged in but not on Windows 7 ( strange… but I must check what is happening since this morning on the Mac )If the USB cable is linked led doesn't flash
If the USB is uncabled , the LED flashes.
At the end of the trial the downloaded code was this :var ON=1, OFF=0; function LCD_bckgnd(status,pin) { digitalWrite(pin,status); } function onInit() { var l=false; LCD_bckgnd(ON,E7); lcd.clear(); lcd.setCursor(0,0); lcd.print(" Board OK …"); lcd.setCursor(0,1); lcd.print("Led1 blinking."); setInterval( function() { l=!l; LED1.write(l);},500); } var lcd.c = require("HD44780").connect(C5,C4,C0,C1,C2,C3); onInit();
What does it happens with using a timeout on onInit() ?
Which kind of things should be logically done inside on onInit() ? -
I understand now that the SD card is better to be used for data storage instead of modules downloading.
I opted for he SD card in case I had to use the board on plants without Ethernet connections.
I checked again.
It was a side effect of an unknown reason or perhaps to my "headache" , the infamous MISO pin on the SD card which did not make a good contact from time to time.Now after having done the same operations the serial link is operative.
Just to let you now, anyway I had a different behavior when executing the LCD code.The LCD is working fine when the code is downloaded from the WEB IDE.
When the code is executed from onInit() and flash, the LCD shows some garbage instead of the string
The situation after having pressed the reset button two times is a shift of the garbage in the first row as you can see from the attached screenshots.
May it be that some garbage has been introduced after recompilation, but there are a lot of warnings in the C code, that should all be fixed.
All are quite referred to unsigned to signed int conversion warnings.
May it be that the LCD garbage is related to the inclusion of the SD card code, some pointer not properly used or something else, because I did a same test for the LCD and it worked fine except for the shift problem, that is to say even before including the SD card code I noticed that the LCD sometimes loosed a column at the beginning of the string.
Anyway I never considered this a problem , till now, because I consider it due to the setCursor function.
I could try to fix all compilation warnings and then let you know.P.S.
I recompiled under Ubuntu Linux using the GCC ARM suite compiled for this distribution.
The recompilation of the GCC code gave no warnings at all so I assumed it was fine. -
I erased the flash and then connected again the board to the Web IDE.
Now the communication has resumed in a proper way.My question is :
Was this problem simply a side effect or is it not possible to use a local filesystem from which "requiring modules" and have the serial communication over the console USB terminal working ? -
I am using an STM32F4discovery board, but I think this time the problem doesn't depend on the board.
I put the HD44780.js file on a SD card into the node_modules directory in order to load the file from a local filesystem( the SD card )
Then wrote a simple javascript to display a greetings message on LCD and flash a led ever 1 s .
I saved the script on the flash by using save().
This works fine as soon as the board starts standalone or linked to a PC with the serial USB cable.
Anyway I am no longer able to get any communication over the serial USB channel and I have no way to access the board interactive shell either by using a terminal emulator or the Web IDE.Is there a reason for this behavior related to the way the "require" machinery works ?
-
Thanks a lot for your help.
Now the SPI works perfectly well, even I must check what you changed in order to have it working ( MISO pin ? ) and I immediately was able to get data from the accelerometer, but most important from the SD card.
It is to say that my last trouble was on the microSD card MISO pin.
I soldered by hand .5 mm wires on the rear of the small SMD card connector and by change the only poor contact was on the Card response (MISO) pin…
( I can say that Murphy law is really true )Espruino is really a good design.
I can imagine what could be done when it will be ported to the powerful STm32F429 architecture. -
Really an hard to be logically fixed BUG.
The code is written properly.
I tested introducing inside the interrupt routine additional checks for the OVR or BSY flags but nothing changed.
I then thought about introducing a delay between reads in order to give the sensor enough time to compute results from a CS to the next , but nothing changed.
Interrupts are received in a proper way ( I measured 5 of them ) for the 1 write + 2 reads, but if ZERO is assigned instead of the first dummy 0x255 it "is likely" that this content is got from another SPIBuffer instead of the one addressed by the SPI port in use.
But looking at the code this cannot be…Mah ???
-
STM32F4 DISCOVERY and Version 1v51
Data is sent and received but ZERO is got from the Javascript side.function onInit() { SPI1.setup({ mode:0, sck: A5, miso: A6, mosi: A7, cs: E3 } ); SPI1.send([0x20,0b01000111], E3); getAcc(); } var avrx=0.0, avry=0.0; function getAcc(){ var accx = SPI1.send([0xA9,0], E3)[1]; var accy = SPI1.send([0xAB,0], E3)[1]; if (accx>127) accx-=256; if (accy>127) accy-=256; avrx = 0.1*accx + 0.9*avrx; avry = 0.1*accy + 0.9*avry; console.log("X= "+accx+" Y= "+accy); } onInit();
-
I looked at the C code of the two versions 1v41 and 1v51.
Everything seems oK.
However, you now manage the SPI port using an interrupt and putting the data into a buffer before returning to the Javascript caller code.
May it be that
either
the interrupt is not generated ? ( Even NVIC structure seems programmed OK )
or
data is put in a wrong way or position into the 4 byte buffers ?
( conversion from unsigned char to int. !!! )
May it be that something the Head and Tail pointers disagree and some garbage is returned back… ?
Another difference with respect the 1v41 version is that now before sending data you do not flush the receiving SPI shift register to disregard eventual garbage.I was not able to see any errors, but it is evident that something must now go wrong .
The ultimate solution is to DEBUG at C code level, the differences among the versions 1v41 and 1v51 being now known.
I did a check executing a same program from ( downloading from the Web IDE ) or from FLASH using the USB console.
I obtained that the version executed after downloading from Flash onInit() runs twice as much as it would.
I set a led flashing at 1 Hz.
It flashed at 1 Hz when executing after a reset() and at 2Hz when executing after onInit()
The same happens for characters received fron an external part and sampled every 100 ms in a state machine loop.
IS there a reason for this behaviour ?
Thanks in advance for the reply.
Please find attached below the profiling.