Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6978f1cccc |
@@ -1,56 +1,101 @@
|
|||||||
RadioSonde Version 0.8.5
|
RadioSonde Version 0.9.1
|
||||||
============================
|
============================
|
||||||
<img src="http://xavier.debert.free.fr/RS/TTGO.jpg" width="50%"><img src="http://xavier.debert.free.fr/RS/TTGO2.jpg" width="50%">
|
<img src="http://xavier.debert.free.fr/RS/TTGO.jpg" width="50%"><img src="http://xavier.debert.free.fr/RS/TTGO2.jpg" width="50%">
|
||||||
<img src="http://xavier.debert.free.fr/RS/TTGO3.jpg" width="50%"><img src="http://xavier.debert.free.fr/RS/TTGO4.jpg" width="50%"><img src="http://xavier.debert.free.fr/RS/TTGO5.jpg" width="50%">
|
<img src="http://xavier.debert.free.fr/RS/TTGO3.jpg" width="50%"><img src="http://xavier.debert.free.fr/RS/TTGO4.jpg" width="50%"><img src="http://xavier.debert.free.fr/RS/TTGO5.jpg" width="50%"><img src="http://xavier.debert.free.fr/RS/TTGO6.jpg" width="50%">
|
||||||
|
<img src="http://xavier.debert.free.fr/RS/TTGO7.jpg" width="50%"><img src="http://xavier.debert.free.fr/RS/TTGO8.jpg" width="50%">
|
||||||
<img src="http://xavier.debert.free.fr/RS/Web4.png" width="20%"><img src="http://xavier.debert.free.fr/RS/Web.png" width="20%">
|
<img src="http://xavier.debert.free.fr/RS/Web4.png" width="20%"><img src="http://xavier.debert.free.fr/RS/Web.png" width="20%">
|
||||||
<img src="http://xavier.debert.free.fr/RS/Web3.png" width="20%"><img src="http://xavier.debert.free.fr/RS/Web2.png" width="20%">
|
<img src="http://xavier.debert.free.fr/RS/Web3.png" width="20%"><img src="http://xavier.debert.free.fr/RS/Web2.png" width="20%">
|
||||||
<img src="http://xavier.debert.free.fr/RS/Web5.png" width="20%">
|
<img src="http://xavier.debert.free.fr/RS/Web5.png" width="20%"><img src="http://xavier.debert.free.fr/RS/Web6.png" width="20%">
|
||||||
|
<img src="http://xavier.debert.free.fr/RS/Web7.png" width="20%">
|
||||||
|
|
||||||
|
|
||||||
Projet basé sur le travail de DL9RDZ
|
Projet basé sur le travail de DL9RDZ
|
||||||
====================================
|
====================================
|
||||||
|
|
||||||
Pour TTGO LORA 32 esp32 pico D4
|
Pour TTGO LORA 32 esp32 pico D4 <br>
|
||||||
Décodage de RadioSonde RS41 and DFM06/09 et M10
|
Décodage de RadioSonde RS41/92 and DFM06/09/17 et M10+/20
|
||||||
|
|
||||||
Attention à la version de votre TTGO!
|
Attention à la version de votre TTGO! <br>
|
||||||
vous devez modifier dans config.txt, le port de l'écran OLED
|
vous devez modifier dans config.txt, le port de l'écran OLED <br>
|
||||||
- TTGO v1: SDA=4 SCL=15, RST=16
|
- TTGO v1: SDA=4 SCL=15, RST=16 <br>
|
||||||
- TTGO v2: SDA=21 SCL=22, RST=16
|
- TTGO v2: SDA=21 SCL=22, RST=16
|
||||||
|
|
||||||
## Version en production 0.8.5 et devel 0.8.7
|
## Version en production 0.9.1 devel 0.9.2
|
||||||
|
|
||||||
|
## 0.9.1
|
||||||
|
Corection RS41 <br>
|
||||||
|
Correction DFM 06/09 <br>
|
||||||
|
Add DFM17 <br>
|
||||||
|
Correction for all trame recived for M10 and M20 1000ms to 1512ms,<br>
|
||||||
|
Correction formulaire QRG, and end RS no save
|
||||||
|
|
||||||
|
## 0.9.0
|
||||||
|
Add M20
|
||||||
|
|
||||||
|
## 0.8.8
|
||||||
|
|
||||||
|
Add M10+ <br>
|
||||||
|
Add Temps restant avant impacte au sol si 99: 0. 0 soit le balon de la sonde n a pas encore éclaté, <br>
|
||||||
|
ou les informations ne sont pas disponible actuellement <br>
|
||||||
|
Add Test Buzzer au démarrage "Arche Perdu" Lol pour des chasseurs de sonde!<br>
|
||||||
|
Compatible Lilygo esp32 GPS inboard pin 34 Rx, 12 Tx
|
||||||
|
|
||||||
|
## 0.8.7
|
||||||
|
|
||||||
|
correction bug Buzzer Off->On->Off <br>
|
||||||
|
Add GainLNA RX SX1278FSK on Web config paramètre <br>
|
||||||
|
Add update OTA Os + DataWeb <br>
|
||||||
|
correction bugs sondmap.html <br>
|
||||||
|
correction text upgrade Os et DataWeb <br>
|
||||||
|
correction texte boussole S et N <br>
|
||||||
|
correction bugs distance 4928Km si lat et lon =0 erroné <br>
|
||||||
|
correction bugs fonction Vbat <br>
|
||||||
|
Add Telemetry width export data.csv <br>
|
||||||
|
Suppression µSD incompatible avec pin SX1278FSK et SPI <br>
|
||||||
|
Add transfert Telemetry To µSD on put SD automatic
|
||||||
|
|
||||||
## 0.8.0
|
|
||||||
travail de refonte et réécriture du code
|
|
||||||
## 0.8.1
|
|
||||||
modification de la partie Web
|
|
||||||
## 0.8.5
|
## 0.8.5
|
||||||
Evolution majeur du système
|
|
||||||
affichage du pourcentage de la batterie en mode scanning
|
Evolution majeur du système <br>
|
||||||
création d'une fenetre Batterie, Boussole
|
affichage du pourcentage de la batterie en mode scanning <br>
|
||||||
suppresion lib et code TFT
|
création d'une fenetre Batterie, Boussole <br>
|
||||||
création Azimute, elevation correction de Bugs majeur , mineur
|
suppresion lib et code TFT <br>
|
||||||
Ajout fonction Smetre, Buzzer, QTH, Gps on off ...
|
création Azimute, elevation correction de Bugs majeur , mineur <br>
|
||||||
mise à jour OTA
|
Ajout fonction Smetre, Buzzer, QTH, Gps on off ... <br>
|
||||||
|
mise à jour OTA <br>
|
||||||
trop de modification pour toutes les expliciter!
|
trop de modification pour toutes les expliciter!
|
||||||
|
|
||||||
|
## 0.8.1
|
||||||
|
|
||||||
|
modification de la partie Web
|
||||||
|
|
||||||
|
## 0.8.0
|
||||||
|
|
||||||
|
travail de refonte et réécriture du code
|
||||||
|
|
||||||
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
## Les Boutons optionnel à ajouter(souder)
|
## Les Boutons optionnel à ajouter(souder)
|
||||||
sur les GPIO 1002 et 1004
|
sur les GPIO 1002 et 1004 <br>
|
||||||
attention:
|
attention:
|
||||||
|
|
||||||
+5V--[ SW ]---GPIO----[ R1 ]---/ R1=10 ou 12KOhms
|
+3.3V--[ SW ]---GPIO----[ R1 ]---/ R1=10 ou 12KOhms
|
||||||
|
|
||||||
- appuie court <1.5 seconds
|
- appuie court <1.5 seconds <br>
|
||||||
- appuie double court 0.5 seconds
|
- appuie double court 0.5 seconds <br>
|
||||||
- appuie moyen 2-4 seconds
|
- appuie moyen 2-4 seconds <br>
|
||||||
- appuie long >5 seconds
|
- appuie long >5 seconds
|
||||||
|
|
||||||
|
## Buzzer optionnel à ajouter(souder)
|
||||||
|
sur les GPIO 25 ou 12 suivant le modèl
|
||||||
|
|
||||||
|
GPIO --[ BUZZER ]---/
|
||||||
|
|
||||||
## Wifi configuration
|
## Wifi configuration
|
||||||
|
|
||||||
Au démarrage, si aucune connexion possible au wifi paramètré, il monte un Wifi AP
|
Au démarrage, si aucune connexion possible au wifi paramètré, il monte un Wifi AP<br>
|
||||||
le SSID et mot de passe par défaut est: Radiosonde
|
le SSID et mot de passe par défaut est: <b>Radiosonde</b> <br>
|
||||||
en mode AP, il doit être en 192.168.4.1,
|
en mode AP, il doit être en 192.168.4.1, <br>
|
||||||
mais vous avez aussi la possibilité de mettre http://radiosonde.local dans n'importe quel Wifi
|
mais vous avez aussi la possibilité de mettre http://radiosonde.local dans n'importe quel Wifi
|
||||||
connecté.
|
connecté.
|
||||||
|
|
||||||
@@ -89,3 +134,142 @@ voir Setup.md pour l'installation!
|
|||||||
Xavier
|
Xavier
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
RadioSonde Version 0.9.1
|
||||||
|
============================
|
||||||
|
<img src="http://xavier.debert.free.fr/RS/TTGO.jpg" width="50%"><img src="http://xavier.debert.free.fr/RS/TTGO2.jpg" width="50%">
|
||||||
|
<img src="http://xavier.debert.free.fr/RS/TTGO3.jpg" width="50%"><img src="http://xavier.debert.free.fr/RS/TTGO4.jpg" width="50%"><img src="http://xavier.debert.free.fr/RS/TTGO5.jpg" width="50%"><img src="http://xavier.debert.free.fr/RS/TTGO6.jpg" width="50%">
|
||||||
|
<img src="http://xavier.debert.free.fr/RS/TTGO7.jpg" width="50%"><img src="http://xavier.debert.free.fr/RS/TTGO8.jpg" width="50%">
|
||||||
|
<img src="http://xavier.debert.free.fr/RS/Web4.png" width="20%"><img src="http://xavier.debert.free.fr/RS/Web.png" width="20%">
|
||||||
|
<img src="http://xavier.debert.free.fr/RS/Web3.png" width="20%"><img src="http://xavier.debert.free.fr/RS/Web2.png" width="20%">
|
||||||
|
<img src="http://xavier.debert.free.fr/RS/Web5.png" width="20%"><img src="http://xavier.debert.free.fr/RS/Web6.png" width="20%">
|
||||||
|
<img src="http://xavier.debert.free.fr/RS/Web7.png" width="20%">
|
||||||
|
|
||||||
|
|
||||||
|
Projet basé sur le travail de DL9RDZ
|
||||||
|
====================================
|
||||||
|
|
||||||
|
Pour TTGO LORA 32 esp32 pico D4 <br>
|
||||||
|
Décodage de RadioSonde RS41/92 and DFM06/09/17 et M10+/20
|
||||||
|
|
||||||
|
Attention à la version de votre TTGO! <br>
|
||||||
|
vous devez modifier dans config.txt, le port de l'écran OLED <br>
|
||||||
|
- TTGO v1: SDA=4 SCL=15, RST=16 <br>
|
||||||
|
- TTGO v2: SDA=21 SCL=22, RST=16
|
||||||
|
|
||||||
|
## Version en production 0.9.1 devel 0.9.2
|
||||||
|
|
||||||
|
## 0.9.1
|
||||||
|
Corection RS41 <br>
|
||||||
|
Correction DFM 06/09 <br>
|
||||||
|
Add DFM17 <br>
|
||||||
|
Correction for all trame recived for M10 and M20 1000ms to 1512ms,<br>
|
||||||
|
Correction formulaire QRG, and end RS no save
|
||||||
|
|
||||||
|
## 0.9.0
|
||||||
|
Add M20
|
||||||
|
|
||||||
|
## 0.8.8
|
||||||
|
|
||||||
|
Add M10+ <br>
|
||||||
|
Add Temps restant avant impacte au sol si 99: 0. 0 soit le balon de la sonde n a pas encore éclaté, <br>
|
||||||
|
ou les informations ne sont pas disponible actuellement <br>
|
||||||
|
Add Test Buzzer au démarrage "Arche Perdu" Lol pour des chasseurs de sonde!<br>
|
||||||
|
Compatible Lilygo esp32 GPS inboard pin 34 Rx, 12 Tx
|
||||||
|
|
||||||
|
## 0.8.7
|
||||||
|
|
||||||
|
correction bug Buzzer Off->On->Off <br>
|
||||||
|
Add GainLNA RX SX1278FSK on Web config paramètre <br>
|
||||||
|
Add update OTA Os + DataWeb <br>
|
||||||
|
correction bugs sondmap.html <br>
|
||||||
|
correction text upgrade Os et DataWeb <br>
|
||||||
|
correction texte boussole S et N <br>
|
||||||
|
correction bugs distance 4928Km si lat et lon =0 erroné <br>
|
||||||
|
correction bugs fonction Vbat <br>
|
||||||
|
Add Telemetry width export data.csv <br>
|
||||||
|
Suppression µSD incompatible avec pin SX1278FSK et SPI <br>
|
||||||
|
Add transfert Telemetry To µSD on put SD automatic
|
||||||
|
|
||||||
|
## 0.8.5
|
||||||
|
|
||||||
|
Evolution majeur du système <br>
|
||||||
|
affichage du pourcentage de la batterie en mode scanning <br>
|
||||||
|
création d'une fenetre Batterie, Boussole <br>
|
||||||
|
suppresion lib et code TFT <br>
|
||||||
|
création Azimute, elevation correction de Bugs majeur , mineur <br>
|
||||||
|
Ajout fonction Smetre, Buzzer, QTH, Gps on off ... <br>
|
||||||
|
mise à jour OTA <br>
|
||||||
|
trop de modification pour toutes les expliciter!
|
||||||
|
|
||||||
|
## 0.8.1
|
||||||
|
|
||||||
|
modification de la partie Web
|
||||||
|
|
||||||
|
## 0.8.0
|
||||||
|
|
||||||
|
travail de refonte et réécriture du code
|
||||||
|
|
||||||
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
## Les Boutons optionnel à ajouter(souder)
|
||||||
|
sur les GPIO 1002 et 1004 <br>
|
||||||
|
attention:
|
||||||
|
|
||||||
|
+3.3V--[ SW ]---GPIO----[ R1 ]---/ R1=10 ou 12KOhms
|
||||||
|
|
||||||
|
- appuie court <1.5 seconds <br>
|
||||||
|
- appuie double court 0.5 seconds <br>
|
||||||
|
- appuie moyen 2-4 seconds <br>
|
||||||
|
- appuie long >5 seconds
|
||||||
|
|
||||||
|
## Buzzer optionnel à ajouter(souder)
|
||||||
|
sur les GPIO 25 ou 12 suivant le modèl
|
||||||
|
|
||||||
|
GPIO --[ BUZZER ]---/
|
||||||
|
|
||||||
|
## Wifi configuration
|
||||||
|
|
||||||
|
Au démarrage, si aucune connexion possible au wifi paramètré, il monte un Wifi AP<br>
|
||||||
|
le SSID et mot de passe par défaut est: <b>Radiosonde</b> <br>
|
||||||
|
en mode AP, il doit être en 192.168.4.1, <br>
|
||||||
|
mais vous avez aussi la possibilité de mettre http://radiosonde.local dans n'importe quel Wifi
|
||||||
|
connecté.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Mode Scanne
|
||||||
|
|
||||||
|
Le fichier qrg.txt contient la liste par défaut des cannaux.
|
||||||
|
pour y configurer les noms, fréquences et mode [M=M10, 6=DFM06, 9=DFM09 et 4=RS41]
|
||||||
|
|
||||||
|
|
||||||
|
## Mode Réception
|
||||||
|
|
||||||
|
En réception, une seul fréquence est décodé, les infos de la sonde (ID, GPS, RSSI, ...)
|
||||||
|
seront affichées dans plusieur fenetre à choisir ( 0 à 6) à configurer dans la page Web
|
||||||
|
rubrique config.
|
||||||
|
In receiving mode, a single frequency will be decoded, and sonde info (ID, GPS
|
||||||
|
coordinates, RSSI) will be displayed. The bar above the IP address indicates,
|
||||||
|
for the last 18 frames, if reception was successfull (|) or failed (.)
|
||||||
|
A DOUBLE press will switch to scanning mode.
|
||||||
|
A SHORT press will switch to the next channel in channels.txt
|
||||||
|
|
||||||
|
## Mode Analyseur
|
||||||
|
|
||||||
|
Le mode analyseur de spectre (400..406 MHz) est affiché (chaque ligne == 50 kHz)
|
||||||
|
Pour les cartes TTGO sans bouton configurable, il y a un nouveau paramètre dans config.txt:
|
||||||
|
- spectrum=10 // 0=off / 1-99 nombre de seconds pour afficher l'analyseur
|
||||||
|
- timer=1 // 0=off / 1= afficher le compte à rebours du spectre dans l'affichage du spectre
|
||||||
|
- marker=1 // 0=off / 1= afficher la fréquence dans l'affichage du spectre
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
voir Setup.md pour l'installation!
|
||||||
|
|
||||||
|
73
|
||||||
|
Xavier
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,6 @@
|
|||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
|
|
||||||
#include <SPIFFS.h>
|
#include <SPIFFS.h>
|
||||||
//#include <U8x8lib.h>
|
|
||||||
//#include <U8g2lib.h>
|
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <Update.h>
|
#include <Update.h>
|
||||||
#include <ESPmDNS.h>
|
#include <ESPmDNS.h>
|
||||||
@@ -22,6 +20,8 @@
|
|||||||
#include "geteph.h"
|
#include "geteph.h"
|
||||||
#include "rs92gps.h"
|
#include "rs92gps.h"
|
||||||
|
|
||||||
|
#include "spiffs_sd.h"
|
||||||
|
|
||||||
|
|
||||||
int e;
|
int e;
|
||||||
enum MainState { ST_DECODER, ST_SPECTRUM, ST_WIFISCAN, ST_UPDATE, ST_TOUCHCALIB };
|
enum MainState { ST_DECODER, ST_SPECTRUM, ST_WIFISCAN, ST_UPDATE, ST_TOUCHCALIB };
|
||||||
@@ -41,6 +41,8 @@ String updateBinM = "/RS/radiosonde/update.ino.bin";
|
|||||||
String updateBinD = "/RS/radiosondedevel/update.ino.bin";
|
String updateBinD = "/RS/radiosondedevel/update.ino.bin";
|
||||||
String *updateBin = &updateBinM;
|
String *updateBin = &updateBinM;
|
||||||
|
|
||||||
|
String updateDataWeb = "/RS/dataweb/data.tar";
|
||||||
|
|
||||||
#define LOCALUDPPORT 9002
|
#define LOCALUDPPORT 9002
|
||||||
|
|
||||||
boolean connected = false;
|
boolean connected = false;
|
||||||
@@ -153,9 +155,10 @@ void setupChannelList() {
|
|||||||
sonde.clearSonde();
|
sonde.clearSonde();
|
||||||
Serial.println("Reading channel config:");
|
Serial.println("Reading channel config:");
|
||||||
while (file.available()) {
|
while (file.available()) {
|
||||||
String line = readLine(file); //file.readStringUntil('\n');
|
//String line = readLine(file); //file.readStringUntil('\n');
|
||||||
|
String line = file.readStringUntil('\n');
|
||||||
String sitename;
|
String sitename;
|
||||||
if (!file.available()) break;
|
//if (!file.available()) break;
|
||||||
if (line[0] == '#') continue;
|
if (line[0] == '#') continue;
|
||||||
char *space = strchr(line.c_str(), ' ');
|
char *space = strchr(line.c_str(), ' ');
|
||||||
if (!space) continue;
|
if (!space) continue;
|
||||||
@@ -173,9 +176,15 @@ void setupChannelList() {
|
|||||||
else if (space[1] == '6') {
|
else if (space[1] == '6') {
|
||||||
type = STYPE_DFM06;
|
type = STYPE_DFM06;
|
||||||
}
|
}
|
||||||
|
else if (space[1] == 'D') {
|
||||||
|
type = STYPE_DFM;
|
||||||
|
}
|
||||||
else if (space[1] == 'M') {
|
else if (space[1] == 'M') {
|
||||||
type = STYPE_M10;
|
type = STYPE_M10;
|
||||||
}
|
}
|
||||||
|
else if (space[1] == '2') {
|
||||||
|
type = STYPE_M20;
|
||||||
|
}
|
||||||
else continue;
|
else continue;
|
||||||
int active = space[3] == '+' ? 1 : 0;
|
int active = space[3] == '+' ? 1 : 0;
|
||||||
if (space[4] == ' ') {
|
if (space[4] == ' ') {
|
||||||
@@ -194,25 +203,55 @@ void setupChannelList() {
|
|||||||
|
|
||||||
const char *createQRGForm() {
|
const char *createQRGForm() {
|
||||||
char *ptr = message;
|
char *ptr = message;
|
||||||
strcpy(ptr, "<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\"></head><body><form action=\"qrg.html\" method=\"post\"><table><tr><th>ID</th><th>Active</th><th>Freq</th><th>Launchsite</th><th>Mode</th></tr>");
|
strcpy(ptr, "<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">");
|
||||||
|
strcat(ptr, "<script type=\"text/javascript\">"
|
||||||
|
"let stypes=new Map();"
|
||||||
|
"stypes.set('4', 'RS41');"
|
||||||
|
"stypes.set('R', 'RS92');"
|
||||||
|
"stypes.set('9', 'DFM9');"
|
||||||
|
"stypes.set('6', 'DFM6');"
|
||||||
|
"stypes.set('D', 'DFM All');"
|
||||||
|
"stypes.set('M', 'M10');"
|
||||||
|
"stypes.set('2', 'M20');"
|
||||||
|
"function prep() {"
|
||||||
|
" var stlist=document.querySelectorAll(\"input.stype\");"
|
||||||
|
" for(txt of stlist){"
|
||||||
|
" var val=txt.getAttribute('value'); var nam=txt.getAttribute('name'); "
|
||||||
|
" var sel=document.createElement('select');"
|
||||||
|
" sel.setAttribute('name',nam);"
|
||||||
|
" for(stype of stypes) { "
|
||||||
|
" var opt=document.createElement('option');"
|
||||||
|
" opt.value=stype[0];"
|
||||||
|
" opt.innerHTML=stype[1];"
|
||||||
|
" if(stype[0]==val) { opt.setAttribute('selected','selected'); }"
|
||||||
|
" sel.appendChild(opt);"
|
||||||
|
" } txt.replaceWith(sel); } } "
|
||||||
|
" window.onload = prep; "
|
||||||
|
"</script>");
|
||||||
|
|
||||||
|
strcat(ptr, "</head><body><form action=\"qrg.html\" method=\"post\"><table><tr><th>ID</th><th>Active</th><th>Freq</th><th>Launchsite</th><th>Mode</th></tr>");
|
||||||
for (int i = 0; i < sonde.config.maxsonde; i++) {
|
for (int i = 0; i < sonde.config.maxsonde; i++) {
|
||||||
String s = sondeTypeSelect(i >= sonde.nSonde ? 2 : sonde.sondeList[i].type);
|
//String s = sondeTypeSelect(i >= sonde.nSonde ? 2 : sonde.sondeList[i].type);
|
||||||
String site = sonde.sondeList[i].launchsite;
|
String site = sonde.sondeList[i].launchsite;
|
||||||
sprintf(ptr + strlen(ptr), "<tr><td>%d</td><td><input name=\"A%d\" type=\"checkbox\" %s/></td>"
|
sprintf(ptr + strlen(ptr), "<tr><td>%d</td><td><input name=\"A%d\" type=\"checkbox\" %s/></td>"
|
||||||
"<td><input name=\"F%d\" type=\"text\" value=\"%3.3f\"></td>"
|
"<td><input name=\"F%d\" type=\"text\" value=\"%3.3f\"></td>"
|
||||||
"<td><input name=\"S%d\" type=\"text\" value=\"%s\"></td>"
|
"<td><input name=\"S%d\" type=\"text\" value=\"%s\"></td>"
|
||||||
"<td><select name=\"T%d\">%s</select></td>",
|
//"<td><select name=\"T%d\">%s</select></td>",
|
||||||
|
"<td><input class='stype' name='T%d' value='%c'>",
|
||||||
i + 1,
|
i + 1,
|
||||||
i + 1, (i < sonde.nSonde && sonde.sondeList[i].active) ? "checked" : "",
|
i + 1, (i < sonde.nSonde && sonde.sondeList[i].active) ? "checked" : "",
|
||||||
i + 1, i >= sonde.nSonde ? 400.000 : sonde.sondeList[i].freq,
|
i + 1, i >= sonde.nSonde ? 400.000 : sonde.sondeList[i].freq,
|
||||||
i + 1, i >= sonde.nSonde ? " " : sonde.sondeList[i].launchsite,
|
i + 1, i >= sonde.nSonde ? " " : sonde.sondeList[i].launchsite,
|
||||||
i + 1, s.c_str());
|
i + 1, i >= sonde.nSonde ? 2 : sondeTypeChar[sonde.sondeList[i].type] );
|
||||||
|
//i + 1, s.c_str());
|
||||||
}
|
}
|
||||||
strcat(ptr, "</table><input type=\"submit\" value=\"Update\"/></form></body></html>");
|
strcat(ptr, "</table><input type=\"submit\" value=\"Update\"/></form></body></html>");
|
||||||
Serial.printf("QRG form: size=%d bytes\n", strlen(message));
|
Serial.printf("QRG form: size=%d bytes\n", strlen(message));
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const char *handleQRGPost(AsyncWebServerRequest *request) {
|
const char *handleQRGPost(AsyncWebServerRequest *request) {
|
||||||
char label[10];
|
char label[10];
|
||||||
// parameters: a_i, f_1, t_i (active/frequency/type)
|
// parameters: a_i, f_1, t_i (active/frequency/type)
|
||||||
@@ -249,7 +288,7 @@ const char *handleQRGPost(AsyncWebServerRequest *request) {
|
|||||||
const char *tstr = tstring.c_str();
|
const char *tstr = tstring.c_str();
|
||||||
const char *sstr = sstring.c_str();
|
const char *sstr = sstring.c_str();
|
||||||
Serial.printf("Processing a=%s, f=%s, t=%s, site=%s\n", active ? "YES" : "NO", fstr, tstr, sstr);
|
Serial.printf("Processing a=%s, f=%s, t=%s, site=%s\n", active ? "YES" : "NO", fstr, tstr, sstr);
|
||||||
char typech = (tstr[2] == '4' ? '4' : tstr[2] == '9' ? 'R' : tstr[0] == 'M' ? 'M' : tstr[3]); // a bit ugly
|
char typech = tstr[0];
|
||||||
file.printf("%3.3f %c %c %s\n", atof(fstr), typech, active ? '+' : '-', sstr);
|
file.printf("%3.3f %c %c %s\n", atof(fstr), typech, active ? '+' : '-', sstr);
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
@@ -299,7 +338,6 @@ void setupWifiList() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const char *createWIFIForm() {
|
const char *createWIFIForm() {
|
||||||
char *ptr = message;
|
char *ptr = message;
|
||||||
char tmp[4];
|
char tmp[4];
|
||||||
@@ -363,10 +401,11 @@ void addSondeStatus(char *ptr, int i)
|
|||||||
strcat(ptr, "<table>");
|
strcat(ptr, "<table>");
|
||||||
sprintf(ptr + strlen(ptr), "<tr><td id=\"sfreq\">%3.3f MHz, Type: %s</td><tr><td>ID: %s", s->freq, sondeTypeStr[s->type],
|
sprintf(ptr + strlen(ptr), "<tr><td id=\"sfreq\">%3.3f MHz, Type: %s</td><tr><td>ID: %s", s->freq, sondeTypeStr[s->type],
|
||||||
s->validID ? s->id : "<?""?>");
|
s->validID ? s->id : "<?""?>");
|
||||||
if (s->validID && (s->type == STYPE_DFM06 || s->type == STYPE_DFM09 || s->type == STYPE_M10)) {
|
if (s->validID && (s->type == STYPE_DFM || s->type == STYPE_DFM06 || s->type == STYPE_DFM09 || s->type == STYPE_M10 || s->type == STYPE_M20)) {
|
||||||
sprintf(ptr + strlen(ptr), " (ser: %s)", s->ser);
|
sprintf(ptr + strlen(ptr), " (ser: %s)", s->ser);
|
||||||
}
|
}
|
||||||
sprintf(ptr + strlen(ptr), "</td></tr><tr><td>QTH: %.6f,%.6f h=%.0fm</td></tr>\n", s->lat, s->lon, s->alt);
|
sprintf(ptr + strlen(ptr), "</td></tr><tr><td>QTHSonde: %.6f,%.6f h=%.0fm</td></tr>\n", s->lat, s->lon, s->alt);
|
||||||
|
sprintf(ptr + strlen(ptr), "</td></tr><tr><td>QTHOm: %.6f,%.6f h=%.0fm</td></tr>\n", sonde.config.gps_Lat, sonde.config.gps_Lon, sonde.config.gps_Alt);
|
||||||
const time_t t = s->time;
|
const time_t t = s->time;
|
||||||
ts = *gmtime(&t);
|
ts = *gmtime(&t);
|
||||||
sprintf(ptr + strlen(ptr), "<tr><td>Frame# %d, Sats=%d, %04d-%02d-%02d %02d:%02d:%02d</td></tr>",
|
sprintf(ptr + strlen(ptr), "<tr><td>Frame# %d, Sats=%d, %04d-%02d-%02d %02d:%02d:%02d</td></tr>",
|
||||||
@@ -375,9 +414,11 @@ void addSondeStatus(char *ptr, int i)
|
|||||||
sprintf(ptr + strlen(ptr), "<tr><td>Burst-KT=%d Launch-KT=%d Countdown=%d (vor %ds)</td></tr>\n",
|
sprintf(ptr + strlen(ptr), "<tr><td>Burst-KT=%d Launch-KT=%d Countdown=%d (vor %ds)</td></tr>\n",
|
||||||
s->burstKT, s->launchKT, s->countKT, ((uint16_t)s->frame - s->crefKT));
|
s->burstKT, s->launchKT, s->countKT, ((uint16_t)s->frame - s->crefKT));
|
||||||
}
|
}
|
||||||
sprintf(ptr + strlen(ptr), "<tr><td><a target=\"_empty\" href=\"geo:%.6f,%.6f\">GEO-App</a> - ", s->lat, s->lon);
|
sprintf(ptr + strlen(ptr), "<tr><td>");
|
||||||
|
sprintf(ptr + strlen(ptr), "<a target=\"_empty\" href=\"https://www.google.fr/maps/dir//%.6f;%.6f\">G-MAPS</a> - ", s->lat, s->lon);
|
||||||
sprintf(ptr + strlen(ptr), "<a target=\"_empty\" href=\"https://tracker.sondehub.org/?sondehub=1#!mt=osm&mz=8&qm=6_hours&mc=%.6f,%.6f&q=RS_%s\">Sondehub</a> - ", s->lat, s->lon, s->id);
|
sprintf(ptr + strlen(ptr), "<a target=\"_empty\" href=\"https://tracker.sondehub.org/?sondehub=1#!mt=osm&mz=8&qm=6_hours&mc=%.6f,%.6f&q=RS_%s\">Sondehub</a> - ", s->lat, s->lon, s->id);
|
||||||
sprintf(ptr + strlen(ptr), "<a target=\"_empty\" href=\"https://www.openstreetmap.org/?mlat=%.6f&mlon=%.6f&zoom=14\">OSM</a></td></tr>", s->lat, s->lon);
|
sprintf(ptr + strlen(ptr), "<a target=\"_empty\" href=\"https://www.openstreetmap.org/?mlat=%.6f&mlon=%.6f&zoom=14\">OSM</a> - ", s->lat, s->lon);
|
||||||
|
sprintf(ptr + strlen(ptr), "<a target=\"_empty\" href=\"https://www.google.com/maps/search/?api=1&query=%.6f,%.6f\">Google</a></td></tr>", s->lat, s->lon);
|
||||||
strcat(ptr, "</table><p/>\n");
|
strcat(ptr, "</table><p/>\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -435,6 +476,7 @@ struct st_configitems config_list[] = {
|
|||||||
{"noisefloor", "Sepctrum noisefloor", 0, &sonde.config.noisefloor},
|
{"noisefloor", "Sepctrum noisefloor", 0, &sonde.config.noisefloor},
|
||||||
/* decoder settings */
|
/* decoder settings */
|
||||||
{"", "Receiver configuration", -5, NULL},
|
{"", "Receiver configuration", -5, NULL},
|
||||||
|
{"gainLNA", "Gain SX1278 default 0 or -32 only negatif", 0, &sonde.config.gainLNA},
|
||||||
{"showafc", "Show AFC value", 0, &sonde.config.showafc},
|
{"showafc", "Show AFC value", 0, &sonde.config.showafc},
|
||||||
{"freqofs", "RX frequency offset (Hz)", 0, &sonde.config.freqofs},
|
{"freqofs", "RX frequency offset (Hz)", 0, &sonde.config.freqofs},
|
||||||
{"rs41.agcbw", "RS41 AGC bandwidth", 0, &sonde.config.rs41.agcbw},
|
{"rs41.agcbw", "RS41 AGC bandwidth", 0, &sonde.config.rs41.agcbw},
|
||||||
@@ -485,11 +527,12 @@ struct st_configitems config_list[] = {
|
|||||||
{"mdnsname", "mDNS name", 14, &sonde.config.mdnsname},
|
{"mdnsname", "mDNS name", 14, &sonde.config.mdnsname},
|
||||||
{"vbatmax", "VBat Max", 5, sonde.config.vbatmax},
|
{"vbatmax", "VBat Max", 5, sonde.config.vbatmax},
|
||||||
{"vbatmin", "VBat Min", 5, sonde.config.vbatmin},
|
{"vbatmin", "VBat Min", 5, sonde.config.vbatmin},
|
||||||
{"sdOn", "SDCard (0=Disabled, 1 Enable)", 0, &sonde.config.sdOn},
|
{"telemetryOn", "Telemetry (0=Disabled, 1 Enable)", 0, &sonde.config.telemetryOn},
|
||||||
{"buzzerOn", "Buzzer (0=Disabled, 1 Enable)", 0, &sonde.config.buzzerOn},
|
{"buzzerOn", "Buzzer (0=Disabled, 1 Enable)", 0, &sonde.config.buzzerOn},
|
||||||
{"buzzerPort", "Buzzer Port (12)", 0, &sonde.config.buzzerPort},
|
{"buzzerPort", "Buzzer Port (12)", 0, &sonde.config.buzzerPort},
|
||||||
{"buzzerFreq", "Buzzer Frequency (700)", 0, &sonde.config.buzzerFreq},
|
{"buzzerFreq", "Buzzer Frequency (700)", 0, &sonde.config.buzzerFreq},
|
||||||
{"dbsmetre", "dB = 0 / Smetre =1)", 0, &sonde.config.dbsmetre},
|
{"dbsmetre", "dB = 0 / Smetre =1)", 0, &sonde.config.dbsmetre},
|
||||||
|
{"degdec", "Lat,Lon Decimal =0 ou Degres =1)", 0, &sonde.config.degdec},
|
||||||
};
|
};
|
||||||
const static int N_CONFIG = (sizeof(config_list) / sizeof(struct st_configitems));
|
const static int N_CONFIG = (sizeof(config_list) / sizeof(struct st_configitems));
|
||||||
|
|
||||||
@@ -880,7 +923,9 @@ void SetupAsyncServer() {
|
|||||||
server.on("/index.html", HTTP_GET, [](AsyncWebServerRequest * request) {
|
server.on("/index.html", HTTP_GET, [](AsyncWebServerRequest * request) {
|
||||||
request->send(SPIFFS, "/index.html", String(), false, processor);
|
request->send(SPIFFS, "/index.html", String(), false, processor);
|
||||||
});
|
});
|
||||||
|
server.on("/download", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||||
|
request->send(SPIFFS, "/data.csv", "text/plain", true);
|
||||||
|
});
|
||||||
server.on("/test.html", HTTP_GET, [](AsyncWebServerRequest * request) {
|
server.on("/test.html", HTTP_GET, [](AsyncWebServerRequest * request) {
|
||||||
request->send(SPIFFS, "/test.html", String(), false, processor);
|
request->send(SPIFFS, "/test.html", String(), false, processor);
|
||||||
});
|
});
|
||||||
@@ -1087,7 +1132,11 @@ char buffer[85];
|
|||||||
MicroNMEA nmea(buffer, sizeof(buffer));
|
MicroNMEA nmea(buffer, sizeof(buffer));
|
||||||
|
|
||||||
int lastCourse = 0;
|
int lastCourse = 0;
|
||||||
void unkHandler(const MicroNMEA& nmea) {
|
|
||||||
|
/// Arrg. MicroNMEA changes type definition... so lets auto-infer type
|
||||||
|
template<typename T>
|
||||||
|
//void unkHandler(const MicroNMEA& nmea) {
|
||||||
|
void unkHandler(T nmea) {
|
||||||
if (strcmp(nmea.getMessageID(), "VTG") == 0) {
|
if (strcmp(nmea.getMessageID(), "VTG") == 0) {
|
||||||
const char *s = nmea.getSentence();
|
const char *s = nmea.getSentence();
|
||||||
while (*s && *s != ',') s++;
|
while (*s && *s != ',') s++;
|
||||||
@@ -1097,6 +1146,7 @@ void unkHandler(const MicroNMEA& nmea) {
|
|||||||
Serial.printf("Course update: %d\n", lastCourse);
|
Serial.printf("Course update: %d\n", lastCourse);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void gpsTask(void *parameter) {
|
void gpsTask(void *parameter) {
|
||||||
nmea.setUnknownSentenceHandler(unkHandler);
|
nmea.setUnknownSentenceHandler(unkHandler);
|
||||||
|
|
||||||
@@ -1120,10 +1170,31 @@ void gpsTask(void *parameter) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define UBX_SYNCH_1 0xB5
|
||||||
|
#define UBX_SYNCH_2 0x62
|
||||||
|
uint8_t ubx_set9k6[]={UBX_SYNCH_1, UBX_SYNCH_2, 0x06, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0xC0, 0x08, 0x00, 0x00, 0x80, 0x25, 0x00, 0x00, 0x03, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8D, 0x8F};
|
||||||
|
uint8_t ubx_factorydef[]={UBX_SYNCH_1, UBX_SYNCH_2, 0x06, 0x09, 13, 0, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0x17, 0x8A };
|
||||||
|
|
||||||
void initGPS() {
|
void initGPS() {
|
||||||
if (sonde.config.gps_rxd < 0) return; // GPS disabled
|
if (sonde.config.gps_rxd < 0) return; // GPS disabled
|
||||||
Serial2.begin(9600, SERIAL_8N1, sonde.config.gps_rxd, sonde.config.gps_txd);
|
Serial2.begin(9600, SERIAL_8N1, sonde.config.gps_rxd, sonde.config.gps_txd);
|
||||||
|
if (sonde.config.gps_txd >= 0) { // TX enable, thus try setting baud to 9600 and do a factory reset
|
||||||
|
Serial.println("Trying to reset GPS...");
|
||||||
|
Serial2.begin(115200, SERIAL_8N1, sonde.config.gps_rxd, sonde.config.gps_txd);
|
||||||
|
Serial2.write(ubx_set9k6, sizeof(ubx_set9k6));
|
||||||
|
delay(100);
|
||||||
|
Serial2.begin(38400, SERIAL_8N1, sonde.config.gps_rxd, sonde.config.gps_txd);
|
||||||
|
Serial2.write(ubx_set9k6, sizeof(ubx_set9k6));
|
||||||
|
delay(100);
|
||||||
|
Serial2.begin(19200, SERIAL_8N1, sonde.config.gps_rxd, sonde.config.gps_txd);
|
||||||
|
Serial2.write(ubx_set9k6, sizeof(ubx_set9k6));
|
||||||
|
delay(100);
|
||||||
|
Serial2.begin(9600, SERIAL_8N1, sonde.config.gps_rxd, sonde.config.gps_txd);
|
||||||
|
Serial2.write(ubx_factorydef, sizeof(ubx_factorydef));
|
||||||
|
delay(1000);
|
||||||
|
} else {
|
||||||
|
Serial2.begin(9600, SERIAL_8N1, sonde.config.gps_rxd, sonde.config.gps_txd);
|
||||||
|
}
|
||||||
xTaskCreate( gpsTask, "gpsTask",
|
xTaskCreate( gpsTask, "gpsTask",
|
||||||
5000, /* stack size */
|
5000, /* stack size */
|
||||||
NULL, /* paramter */
|
NULL, /* paramter */
|
||||||
@@ -1441,6 +1512,7 @@ void setup()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Serial.println("Reading initial configuration");
|
Serial.println("Reading initial configuration");
|
||||||
setupConfigData(); // configuration must be read first due to OLED ports!!!
|
setupConfigData(); // configuration must be read first due to OLED ports!!!
|
||||||
|
|
||||||
@@ -1524,6 +1596,41 @@ void setup()
|
|||||||
delay(100);
|
delay(100);
|
||||||
Serial.println("Showing welcome display");
|
Serial.println("Showing welcome display");
|
||||||
disp.rdis->welcome();
|
disp.rdis->welcome();
|
||||||
|
|
||||||
|
//Music
|
||||||
|
if(sonde.config.buzzerOn==1){
|
||||||
|
ledcSetup(0, sonde.config.buzzerFreq, 8);
|
||||||
|
ledcAttachPin(sonde.config.buzzerPort, 0);
|
||||||
|
|
||||||
|
// fréquence associée à chaque note
|
||||||
|
// do, do#, ré, ré#, mi, fa, fa#, sol, sol#, la, la#, si
|
||||||
|
const float note[12] = {65.41, 69.30, 73.42, 77.78, 82.41, 87.31, 92.50, 98.00, 103.83, 110.00, 116.54, 123.47
|
||||||
|
};
|
||||||
|
|
||||||
|
const int nombreDeNotes = 32;
|
||||||
|
const int tempo = 150; // plus c'est petit, plus c'est rapide
|
||||||
|
|
||||||
|
const int melodie[][3] = { {4, 2, 2}, {5, 2, 1}, {7, 2, 3}, {0, 3, 6},
|
||||||
|
{2, 2, 2}, {4, 2, 1},{5, 2, 8},
|
||||||
|
{7, 2, 2}, {9, 2, 1}, {11, 2, 3}, {5, 3, 6},
|
||||||
|
{9, 2, 2}, {11, 2, 1}, {0, 3, 3}, {2, 3, 3}, {4, 3, 3},
|
||||||
|
{4, 2, 2}, {5, 2, 1}, {7, 2, 3}, {0, 3, 6},
|
||||||
|
{2, 3, 2}, {4, 3, 1},{5, 3, 8},
|
||||||
|
{7, 2, 2}, {7, 2, 1}, {4, 3, 3}, {2, 3, 2},
|
||||||
|
{7, 2, 1}, {5, 3, 3}, {4, 3, 2}, {2, 3, 1},{0, 3, 8}
|
||||||
|
};
|
||||||
|
int frequence;
|
||||||
|
|
||||||
|
for ( int i = 0; i < nombreDeNotes ; i++ ) {
|
||||||
|
frequence = round(note[melodie[i][0]] * 2.0 * (melodie[i][1] - 1));
|
||||||
|
ledcSetup(0, frequence, 12);
|
||||||
|
ledcWrite(0, 2048); // rapport cyclique 50%
|
||||||
|
delay(tempo * melodie[i][2] - 50);
|
||||||
|
ledcWrite(0, 0); // rapport cyclique 0% (silence, pour séparer les notes adjacentes)
|
||||||
|
delay(50);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
delay(3000);
|
delay(3000);
|
||||||
Serial.println("Clearing display");
|
Serial.println("Clearing display");
|
||||||
sonde.clearDisplay();
|
sonde.clearDisplay();
|
||||||
@@ -1589,7 +1696,7 @@ void setup()
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
//sx1278.setLNAGain(-48);
|
//sx1278.setLNAGain(-48);
|
||||||
sx1278.setLNAGain(0);
|
sx1278.setLNAGain(sonde.config.gainLNA);
|
||||||
|
|
||||||
int gain = sx1278.getLNAGain();
|
int gain = sx1278.getLNAGain();
|
||||||
Serial.print("RX LNA Gain is ");
|
Serial.print("RX LNA Gain is ");
|
||||||
@@ -1612,10 +1719,10 @@ void setup()
|
|||||||
setupChannelList();
|
setupChannelList();
|
||||||
#if 0
|
#if 0
|
||||||
sonde.clearSonde();
|
sonde.clearSonde();
|
||||||
sonde.addSonde(402.700, STYPE_RS41);
|
sonde.addSonde(402.800, STYPE_RS41);
|
||||||
sonde.addSonde(405.700, STYPE_RS41);
|
sonde.addSonde(402.000, STYPE_M10);
|
||||||
sonde.addSonde(405.900, STYPE_RS41);
|
sonde.addSonde(404.800, STYPE_RS41);
|
||||||
sonde.addSonde(403.450, STYPE_DFM09);
|
sonde.addSonde(403.000, STYPE_DFM09);
|
||||||
Serial.println("No channel config file, using defaults!");
|
Serial.println("No channel config file, using defaults!");
|
||||||
Serial.println();
|
Serial.println();
|
||||||
#endif
|
#endif
|
||||||
@@ -1628,11 +1735,33 @@ void setup()
|
|||||||
1, /* priority */
|
1, /* priority */
|
||||||
NULL); /* task handle*/
|
NULL); /* task handle*/
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
sonde.setup();
|
sonde.setup();
|
||||||
initGPS();
|
initGPS();
|
||||||
|
|
||||||
WiFi.onEvent(WiFiEvent);
|
WiFi.onEvent(WiFiEvent);
|
||||||
getKeyPress(); // clear key buffer
|
|
||||||
|
// init fichie data.csv
|
||||||
|
File file = SPIFFS.open("/data.csv", "w");
|
||||||
|
|
||||||
|
// Read all the lines of the reply from server and print them to Serial
|
||||||
|
|
||||||
|
if (!file) {
|
||||||
|
Serial.println("There was an error opening the file for writing");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
String telemetry = "RadioSonde N; Ser ; Type ; Freq ; Lat ; Lon; Alt; QTHLat; QTHLon; QTHAlt; VSpeed ; HSpeed ; Dir ; DESIGNATION ; RSSI; SMETRE; STAT; AFC; ValidPos\n";
|
||||||
|
if (file.print(telemetry+"\n")) {
|
||||||
|
Serial.println("File was written");
|
||||||
|
} else {
|
||||||
|
Serial.println("File write failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void enterMode(int mode) {
|
void enterMode(int mode) {
|
||||||
@@ -2213,7 +2342,8 @@ void execOTA() {
|
|||||||
bool isValidContentType = false;
|
bool isValidContentType = false;
|
||||||
sonde.clearDisplay();
|
sonde.clearDisplay();
|
||||||
disp.rdis->setFont(FONT_SMALL);
|
disp.rdis->setFont(FONT_SMALL);
|
||||||
disp.rdis->drawString(0, 0, "RadioSonde Upgrade");
|
disp.rdis->drawString(0, 0, "RadioSonde");
|
||||||
|
disp.rdis->drawString(0, 1, "Upgrade Os");
|
||||||
String dispHost = updateHost.substring(0, 14);
|
String dispHost = updateHost.substring(0, 14);
|
||||||
//disp.rdis->drawString(2, 0, dispHost.c_str());
|
//disp.rdis->drawString(2, 0, dispHost.c_str());
|
||||||
|
|
||||||
@@ -2222,7 +2352,7 @@ void execOTA() {
|
|||||||
if (client.connect(updateHost.c_str(), updatePort)) {
|
if (client.connect(updateHost.c_str(), updatePort)) {
|
||||||
// Connection succeeded, fecthing the bin
|
// Connection succeeded, fecthing the bin
|
||||||
Serial.println("Fetching bin: " + String(*updateBin));
|
Serial.println("Fetching bin: " + String(*updateBin));
|
||||||
disp.rdis->drawString(0, 1, "Fetching update");
|
disp.rdis->drawString(0, 2, "Fetching update");
|
||||||
|
|
||||||
// Get the contents of the bin file
|
// Get the contents of the bin file
|
||||||
client.print(String("GET ") + *updateBin + " HTTP/1.1\r\n" +
|
client.print(String("GET ") + *updateBin + " HTTP/1.1\r\n" +
|
||||||
@@ -2263,6 +2393,8 @@ void execOTA() {
|
|||||||
{{BIN FILE CONTENTS}}
|
{{BIN FILE CONTENTS}}
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
while (client.available()) {
|
while (client.available()) {
|
||||||
// read line till /n
|
// read line till /n
|
||||||
String line = client.readStringUntil('\n');
|
String line = client.readStringUntil('\n');
|
||||||
@@ -2341,6 +2473,15 @@ void execOTA() {
|
|||||||
// execOTA();
|
// execOTA();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
//ICI Update Data
|
||||||
|
|
||||||
|
// Update DATAWeb!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
//Data data.tar
|
||||||
|
updatedataweb(updateHost,updatePort,updateDataWeb,"index.html");
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
if (Update.end()) {
|
if (Update.end()) {
|
||||||
Serial.println("OTA done!");
|
Serial.println("OTA done!");
|
||||||
if (Update.isFinished()) {
|
if (Update.isFinished()) {
|
||||||
@@ -2365,15 +2506,78 @@ void execOTA() {
|
|||||||
Serial.println("There was no content in the response");
|
Serial.println("There was no content in the response");
|
||||||
client.flush();
|
client.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Back to some normal state
|
// Back to some normal state
|
||||||
enterMode(ST_DECODER);
|
enterMode(ST_DECODER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void updatedataweb(String host, int port, String dataweb, String fichier){
|
||||||
|
|
||||||
|
//Data1 index.html
|
||||||
|
sonde.clearDisplay();
|
||||||
|
disp.rdis->setFont(FONT_SMALL);
|
||||||
|
disp.rdis->drawString(0, 0, "RadioSonde");
|
||||||
|
disp.rdis->drawString(0, 1, "Upgrade Data");
|
||||||
|
String *data = &dataweb;
|
||||||
|
String dispHost = host.substring(0, 14);
|
||||||
|
Serial.println("Connecting to: " + host);
|
||||||
|
// Connect to Update host
|
||||||
|
if (client.connect(host.c_str(), port)) {
|
||||||
|
// Connection succeeded, fecthing the dataweb
|
||||||
|
Serial.println("Fetching data: " + String(*data));
|
||||||
|
disp.rdis->drawString(0, 3, "Fetching data");
|
||||||
|
char buf[16];
|
||||||
|
snprintf(buf, 16, "%s", fichier);
|
||||||
|
disp.rdis->drawString(0, 4, buf);
|
||||||
|
// Get the contents of the bin file
|
||||||
|
client.print(String("GET ") + *data + " HTTP/1.1\r\n" +
|
||||||
|
"Host: " + host + "\r\n" +
|
||||||
|
"Cache-Control: no-cache\r\n" +
|
||||||
|
"Connection: close\r\n\r\n");
|
||||||
|
|
||||||
|
unsigned long timeout = millis();
|
||||||
|
while (client.available() == 0) {
|
||||||
|
if (millis() - timeout > 5000) {
|
||||||
|
Serial.println("Client Timeout !");
|
||||||
|
client.stop();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = SPIFFS.open("/"+fichier, FILE_WRITE);
|
||||||
|
int cpt=0;
|
||||||
|
// Read all the lines of the reply from server and print them to Serial
|
||||||
|
while(client.available()){
|
||||||
|
String line = client.readStringUntil('\r');
|
||||||
|
line.trim();
|
||||||
|
Serial.print(line);
|
||||||
|
cpt += 1;
|
||||||
|
if (!file) {
|
||||||
|
Serial.println("There was an error opening the file for writing");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (cpt>10){
|
||||||
|
if (file.print(line+"\n")) {
|
||||||
|
Serial.println("File was written");
|
||||||
|
} else {
|
||||||
|
Serial.println("File write failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
client.flush();
|
||||||
|
//Untar data.tar
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if(sonde.config.buzzerOn==1){
|
if(sonde.config.buzzerOn==1){
|
||||||
ledcSetup(0, sonde.config.buzzerFreq, 8);
|
ledcSetup(0, sonde.config.buzzerFreq, 8);
|
||||||
ledcAttachPin(sonde.config.buzzerPort, 0);
|
ledcAttachPin(sonde.config.buzzerPort, 0);
|
||||||
|
} else {
|
||||||
|
ledcSetup(0, 0, 8);
|
||||||
|
ledcAttachPin(sonde.config.buzzerPort, 0);
|
||||||
}
|
}
|
||||||
Serial.printf("\nRunning main loop in state %d. free heap: %d;\n", mainState, ESP.getFreeHeap());
|
Serial.printf("\nRunning main loop in state %d. free heap: %d;\n", mainState, ESP.getFreeHeap());
|
||||||
Serial.printf("currentDisp:%d lastDisp:%d\n", currentDisplay, lastDisplay);
|
Serial.printf("currentDisp:%d lastDisp:%d\n", currentDisplay, lastDisplay);
|
||||||
@@ -2405,8 +2609,7 @@ void loop() {
|
|||||||
sonde.clearDisplay();
|
sonde.clearDisplay();
|
||||||
sonde.updateDisplay();
|
sonde.updateDisplay();
|
||||||
lastDisplay = currentDisplay;
|
lastDisplay = currentDisplay;
|
||||||
|
|
||||||
}
|
}
|
||||||
Serial.printf("Unused stack: %d\n", uxTaskGetStackHighWaterMark(0));
|
Serial.printf("Unused stack: %d\n", uxTaskGetStackHighWaterMark(0));
|
||||||
|
//transfert_sd();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
# Hardware depending settings
|
# Hardware depending settings
|
||||||
#-------------------------------#
|
#-------------------------------#
|
||||||
# pin: 255=disabled; x=button x+128=touch button
|
# pin: 255=disabled; x=button x+128=touch button
|
||||||
button_pin=2
|
#button_pin=2
|
||||||
button2_pin=4
|
#button2_pin=4
|
||||||
# No specification in config file: try autodetection (gpio4 pin level at startup)
|
# No specification in config file: try autodetection (gpio4 pin level at startup)
|
||||||
#button_pin=0
|
#button_pin=0
|
||||||
#button2_pin=255
|
#button2_pin=255
|
||||||
@@ -52,11 +52,12 @@ display=0,1,2,3,4,5,6
|
|||||||
norx_timeout=20
|
norx_timeout=20
|
||||||
vbatmax=1.86
|
vbatmax=1.86
|
||||||
vbatmin=1.64
|
vbatmin=1.64
|
||||||
sdOn=0
|
telemetryOn=0
|
||||||
buzzerOn=0
|
buzzerOn=0
|
||||||
buzzerPort=12
|
buzzerPort=12
|
||||||
buzzerFreq=700
|
buzzerFreq=700
|
||||||
dbsmetre=1
|
dbsmetre=1
|
||||||
|
gainLNA=0
|
||||||
#-------------------------------#
|
#-------------------------------#
|
||||||
# Spectrum display settings
|
# Spectrum display settings
|
||||||
#-------------------------------#
|
#-------------------------------#
|
||||||
@@ -68,8 +69,8 @@ marker=1
|
|||||||
#-------------------------------#
|
#-------------------------------#
|
||||||
# APRS settings
|
# APRS settings
|
||||||
#-------------------------------#
|
#-------------------------------#
|
||||||
call=NOCALL
|
call=FR2013
|
||||||
passcode=123456
|
passcode=14003
|
||||||
#-------------------------------#
|
#-------------------------------#
|
||||||
# Sonde specific settings: bandwidth
|
# Sonde specific settings: bandwidth
|
||||||
# valid values: 3100, 3900, 5200, 6300, 7800, 10400, 12500,
|
# valid values: 3100, 3900, 5200, 6300, 7800, 10400, 12500,
|
||||||
@@ -89,8 +90,8 @@ dfm.rxbw=10400
|
|||||||
# local use only, do not feed to public services
|
# local use only, do not feed to public services
|
||||||
# data not sanitized / quality checked, outliers not filtered out
|
# data not sanitized / quality checked, outliers not filtered out
|
||||||
axudp.active=1
|
axudp.active=1
|
||||||
axudp.host=monurl.fr
|
axudp.host=fra1od.fr.to
|
||||||
axudp.port=14580
|
axudp.port=20350
|
||||||
axudp.symbol=/O
|
axudp.symbol=/O
|
||||||
axudp.highrate=1
|
axudp.highrate=1
|
||||||
axudp.idformat=0
|
axudp.idformat=0
|
||||||
@@ -98,9 +99,9 @@ axudp.idformat=0
|
|||||||
# maybe some time in the future
|
# maybe some time in the future
|
||||||
#-------------------------------#
|
#-------------------------------#
|
||||||
# currently simply not implemented, no need to put anything here anyway
|
# currently simply not implemented, no need to put anything here anyway
|
||||||
tcp.active=0
|
tcp.active=1
|
||||||
tcp.host=radiosondy.info
|
tcp.host=fra1od.fr.to
|
||||||
tcp.port=14590
|
tcp.port=14580
|
||||||
tcp.symbol=/O
|
tcp.symbol=/O
|
||||||
tcp.highrate=20
|
tcp.highrate=20
|
||||||
tcp.idformat=0
|
tcp.idformat=0
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
<button class="tablinks fa fa-gears" onclick="selTab(event,'Config'); w3_close()"> Config</button><br>
|
<button class="tablinks fa fa-gears" onclick="selTab(event,'Config'); w3_close()"> Config</button><br>
|
||||||
<button class="tablinks fa fa-sliders" onclick="selTab(event,'Control'); w3_close()"> Control</button><br>
|
<button class="tablinks fa fa-sliders" onclick="selTab(event,'Control'); w3_close()"> Control</button><br>
|
||||||
<button class="tablinks fa fa-download" onclick="selTab(event,'Update'); w3_close()"> Update</button><br>
|
<button class="tablinks fa fa-download" onclick="selTab(event,'Update'); w3_close()"> Update</button><br>
|
||||||
|
<button class="tablinks fa fa-file" onclick="selTab(event,'Telemetry'); w3_close()"> Telemetry</button><br>
|
||||||
<button class="tablinks fa fa-support" onclick="selTab(event,'About'); w3_close()"> About</button><br><br><br><br>
|
<button class="tablinks fa fa-support" onclick="selTab(event,'About'); w3_close()"> About</button><br><br><br><br>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
@@ -48,17 +49,7 @@
|
|||||||
<div id="SondeMap" class="tabcontent" data-src="sondemap.html">
|
<div id="SondeMap" class="tabcontent" data-src="sondemap.html">
|
||||||
<iframe src="sondemap.html" style="border:none;" width="98%%" height="98%%"></iframe>
|
<iframe src="sondemap.html" style="border:none;" width="98%%" height="98%%"></iframe>
|
||||||
</div>
|
</div>
|
||||||
<!--
|
|
||||||
<div id="SondeMap" class="tabcontent" data-src="http://wx.dl2mf.de/#?">
|
|
||||||
<iframe src="http://wx.dl2mf.de/" style="border:none;" width="98%%" height="98%%"></iframe>
|
|
||||||
</div>
|
|
||||||
<div id="SondeMap2" class="tabcontent" data-src="https://radiosondy.info/">
|
|
||||||
<iframe src="https://radiosondy.info/" style="border:none;" width="98%%" height="98%%"></iframe>
|
|
||||||
</div>
|
|
||||||
<div id="SondeMap3" class="tabcontent" data-src="https://tracker.sondehub.org/?sondehub=1#!mt=osm&mz=8&qm=6_hours&mc=43.76665,6.24011&f=none&q=RS_*;*chase">
|
|
||||||
<iframe src="https://tracker.sondehub.org/?sondehub=1#!mt=osm&mz=8&qm=6_hours&mc=43.76665,6.24011&f=none&q=RS_*;*chase" style="border:none;" width="98%%" height="98%%"></iframe>
|
|
||||||
</div>
|
|
||||||
-->
|
|
||||||
<div id="Config" class="tabcontent">
|
<div id="Config" class="tabcontent">
|
||||||
<h3>Configuration</h3>
|
<h3>Configuration</h3>
|
||||||
<iframe src="config.html" style="border:none;" width="100%%" height="100%%"></iframe>
|
<iframe src="config.html" style="border:none;" width="100%%" height="100%%"></iframe>
|
||||||
@@ -73,7 +64,10 @@
|
|||||||
<h3>Update</h3>
|
<h3>Update</h3>
|
||||||
<iframe src="update.html" style="border:none;" width="100%%" height="100%%"></iframe>
|
<iframe src="update.html" style="border:none;" width="100%%" height="100%%"></iframe>
|
||||||
</div>
|
</div>
|
||||||
|
<div id="Telemetry" class="tabcontent">
|
||||||
|
<h3>Telemetry</h3>
|
||||||
|
<button onclick="javascript:window.open('/download','_self');">Telemetry</button>
|
||||||
|
</div>
|
||||||
<div id="About" class="tabcontent">
|
<div id="About" class="tabcontent">
|
||||||
<h3>About</h3>
|
<h3>About</h3>
|
||||||
%VERSION_NAME%<br>
|
%VERSION_NAME%<br>
|
||||||
@@ -83,7 +77,7 @@
|
|||||||
<br>
|
<br>
|
||||||
Autodetect info: %AUTODETECT_INFO%<br>
|
Autodetect info: %AUTODETECT_INFO%<br>
|
||||||
<br>
|
<br>
|
||||||
CopyLeft 2020 Modifier par <a href="http://openpmr.cla.fr">FRS2013</a><br>
|
CopyLeft 2020 Modifier par <a href="http://openpmr.cla.fr">FRS2013</a> & Vigor<br>
|
||||||
Licence GNU GPL <a href="https://www.gnu.org/licenses/gpl-2.0.txt">https://www.gnu.org/licenses/gpl-2.0.txt</a>
|
Licence GNU GPL <a href="https://www.gnu.org/licenses/gpl-2.0.txt">https://www.gnu.org/licenses/gpl-2.0.txt</a>
|
||||||
for details
|
for details
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
Radiosonde
|
Radiosonde
|
||||||
Radiosonde
|
Radiosonde
|
||||||
VotreSSID
|
Terreandro
|
||||||
votrepass
|
terrexavier
|
||||||
|
TERREWIFI
|
||||||
|
terrexavier
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# Frequency in Mhz (format nnn.nnn)
|
# Frequency in Mhz (format nnn.nnn)
|
||||||
# Type (4=RS41, R=RS92, 6=DFM normal, DFM-06, 9=DFM inverted, DFM-09, M=M10)
|
# Type (4=RS41, R=RS92, 6=DFM normal, DFM-06, 9=DFM inverted, DFM-09, D=DFM, M=M10, 2=M20)
|
||||||
#
|
#
|
||||||
400.000 9 + Test(FR)
|
400.000 9 + Test(FR)
|
||||||
401.500 4 - Santander(ES)
|
401.500 4 - Santander(ES)
|
||||||
@@ -20,5 +20,5 @@
|
|||||||
405.900 4 - Bergen_2
|
405.900 4 - Bergen_2
|
||||||
405.100 4 - Meppen_2
|
405.100 4 - Meppen_2
|
||||||
405.300 4 - Essen
|
405.300 4 - Essen
|
||||||
405.500 4 - Essen_2
|
405.500 2 - Essen_2
|
||||||
# end
|
# end
|
||||||
|
|||||||
@@ -55,7 +55,7 @@
|
|||||||
# B battery(T-Beam 1.0) S=status V=Batt.Volt C=charge current D=discharge current
|
# B battery(T-Beam 1.0) S=status V=Batt.Volt C=charge current D=discharge current
|
||||||
# U=USB volt I=USB current T=IC temp
|
# U=USB volt I=USB current T=IC temp
|
||||||
# AA Azimut degres
|
# AA Azimut degres
|
||||||
#
|
# Z duree vol H:m.s
|
||||||
# fonts=x,y can be used to select font (x=small, y=large) for all items below
|
# fonts=x,y can be used to select font (x=small, y=large) for all items below
|
||||||
# for SSD1306, x and y can be used to select one of those fonts:
|
# for SSD1306, x and y can be used to select one of those fonts:
|
||||||
# (y should be a 1x2 font (1,5,6,7), x a small font)
|
# (y should be a 1x2 font (1,5,6,7), x a small font)
|
||||||
@@ -127,7 +127,8 @@ timeaction=#,#,0
|
|||||||
0,0=t
|
0,0=t
|
||||||
0,5=f MHz
|
0,5=f MHz
|
||||||
1,0=is
|
1,0=is
|
||||||
1,8=c
|
#1,8=c
|
||||||
|
1,8=z
|
||||||
2,0=L
|
2,0=L
|
||||||
2,10=a
|
2,10=a
|
||||||
3,10=h
|
3,10=h
|
||||||
@@ -247,3 +248,4 @@ fonts=0,1
|
|||||||
6,0=xPower:
|
6,0=xPower:
|
||||||
6,7=bPmW
|
6,7=bPmW
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">
|
<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<button class="tablinks" onclick="javascript:window.open('http://wx.dl2mf.de/','_self')"> Sonde Map</button><br><br>
|
<button class="tablinks" onclick="javascript:window.open('http://wx.dl2mf.de/','_blank')"> Sonde Map</button><br><br>
|
||||||
<button class="tablinks" onclick="javascript:window.open('https://radiosondy.info/','_self')"> RadioSony</button><br><br>
|
<button class="tablinks" onclick="javascript:window.open('http://radiosondy.info/','_self')"> RadioSondy</button><br><br>
|
||||||
<button class="tablinks" onclick="javascript:window.open('https://tracker.sondehub.org/?sondehub=1#!mt=osm&mz=8&qm=6_hours&mc=43.76665,6.24011&f=none&q=RS_*;*chase','_self')"> SondeHub</button><br><br>
|
<button class="tablinks" onclick="javascript:window.open('http://tracker.sondehub.org/','_blank')"> SondeHub</button><br><br>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -1,4 +1,2 @@
|
|||||||
const char *version_name = "RadioSonde";
|
const char *version_name = "RadioSonde";
|
||||||
const char *version_id = "0.8.5";
|
const char *version_id = "0.9.1";
|
||||||
const int SPIFFS_MAJOR=2;
|
|
||||||
const int SPIFFS_MINOR=4;
|
|
||||||
|
|||||||
BIN
Binary file not shown.
|
Before Width: | Height: | Size: 21 KiB |
@@ -1,89 +0,0 @@
|
|||||||
# default language
|
|
||||||
language: generic
|
|
||||||
|
|
||||||
env:
|
|
||||||
global:
|
|
||||||
- CLI_VERSION=latest
|
|
||||||
|
|
||||||
matrix:
|
|
||||||
include:
|
|
||||||
# compile example sketches for the following boards
|
|
||||||
- env:
|
|
||||||
- BOARD='arduino:avr:mega:cpu=atmega2560'
|
|
||||||
install:
|
|
||||||
- arduino-cli core install arduino:avr
|
|
||||||
|
|
||||||
- env:
|
|
||||||
- BOARD='arduino:samd:mkrzero'
|
|
||||||
install:
|
|
||||||
- arduino-cli core install arduino:samd
|
|
||||||
|
|
||||||
- env:
|
|
||||||
- BOARD='arduino:megaavr:uno2018:mode=on'
|
|
||||||
install:
|
|
||||||
- arduino-cli core install arduino:megaavr
|
|
||||||
|
|
||||||
- env:
|
|
||||||
- BOARD='arduino:sam:arduino_due_x'
|
|
||||||
install:
|
|
||||||
- arduino-cli core install arduino:sam
|
|
||||||
|
|
||||||
# check all code files for compliance with the Arduino code formatting style
|
|
||||||
- env:
|
|
||||||
- NAME='Code Formatting Check'
|
|
||||||
language: minimal
|
|
||||||
before_install:
|
|
||||||
# install Artistic Style code formatter tool: http://astyle.sourceforge.net
|
|
||||||
- |
|
|
||||||
mkdir "${HOME}/astyle";
|
|
||||||
wget --no-verbose --output-document="${HOME}/astyle/astyle.tar.gz" "https://iweb.dl.sourceforge.net/project/astyle/astyle/astyle%203.1/astyle_3.1_linux.tar.gz";
|
|
||||||
tar --extract --file="${HOME}/astyle/astyle.tar.gz" --directory="${HOME}/astyle";
|
|
||||||
cd "${HOME}/astyle/astyle/build/gcc";
|
|
||||||
make;
|
|
||||||
export PATH="$PWD/bin:$PATH";
|
|
||||||
cd "$TRAVIS_BUILD_DIR"
|
|
||||||
# download Arduino's Artistic Style configuration file
|
|
||||||
- wget --directory-prefix="${HOME}/astyle" https://raw.githubusercontent.com/arduino/Arduino/master/build/shared/examples_formatter.conf
|
|
||||||
script:
|
|
||||||
# check code formatting
|
|
||||||
- find . -regextype posix-extended -path './.git' -prune -or \( -iregex '.*\.((ino)|(h)|(hpp)|(hh)|(hxx)|(h\+\+)|(cpp)|(cc)|(cxx)|(c\+\+)|(cp)|(c)|(ipp)|(ii)|(ixx)|(inl)|(tpp)|(txx)|(tpl))$' -and -type f \) -print0 | xargs -0 -L1 bash -c 'if ! diff "$0" <(astyle --options=${HOME}/astyle/examples_formatter.conf --dry-run < "$0"); then echo "Non-compliant code formatting in $0"; false; fi'
|
|
||||||
|
|
||||||
# check all files for commonly misspelled words
|
|
||||||
- env:
|
|
||||||
- NAME='Spell Check'
|
|
||||||
language: python
|
|
||||||
python: 3.6
|
|
||||||
before_install:
|
|
||||||
# https://github.com/codespell-project/codespell
|
|
||||||
- pip install codespell
|
|
||||||
script:
|
|
||||||
# codespell will ignore any words in extras/codespell-ignore-words-list.txt, which may be used to fix false positives
|
|
||||||
- codespell --skip="${TRAVIS_BUILD_DIR}/.git" --ignore-words="${TRAVIS_BUILD_DIR}/extras/codespell-ignore-words-list.txt" "${TRAVIS_BUILD_DIR}"
|
|
||||||
|
|
||||||
# default phases shared by the compilation tests
|
|
||||||
before_install:
|
|
||||||
- wget http://downloads.arduino.cc/arduino-cli/arduino-cli-$CLI_VERSION-linux64.tar.bz2
|
|
||||||
- tar xf arduino-cli-$CLI_VERSION-linux64.tar.bz2
|
|
||||||
- mkdir -p "$HOME/bin"
|
|
||||||
- mv arduino-cli "$HOME/bin"
|
|
||||||
- export PATH="$PATH:$HOME/bin"
|
|
||||||
- arduino-cli core update-index
|
|
||||||
- buildExampleSketch() { arduino-cli compile --verbose --warnings all --fqbn $BOARD "$PWD/examples/$1"; }
|
|
||||||
- mkdir -p "$HOME/Arduino/libraries"
|
|
||||||
- ln -s "$PWD" "$HOME/Arduino/libraries/."
|
|
||||||
|
|
||||||
script:
|
|
||||||
- buildExampleSketch CardInfo
|
|
||||||
- buildExampleSketch Datalogger
|
|
||||||
- buildExampleSketch DumpFile
|
|
||||||
- buildExampleSketch Files
|
|
||||||
- buildExampleSketch listfiles
|
|
||||||
- buildExampleSketch ReadWrite
|
|
||||||
|
|
||||||
notifications:
|
|
||||||
webhooks:
|
|
||||||
# use TravisBuddy to comment on any pull request that results in a failed CI build
|
|
||||||
urls:
|
|
||||||
- https://www.travisbuddy.com/
|
|
||||||
on_success: never
|
|
||||||
on_failure: always
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
= SD Library for Arduino =
|
|
||||||
|
|
||||||
image:https://travis-ci.org/arduino-libraries/SD.svg?branch=master[Build Status, link=https://travis-ci.org/arduino-libraries/SD]
|
|
||||||
|
|
||||||
The SD library allows for reading from and writing to SD cards.
|
|
||||||
|
|
||||||
For more information about this library please visit us at
|
|
||||||
http://www.arduino.cc/en/Reference/SD
|
|
||||||
|
|
||||||
== License ==
|
|
||||||
|
|
||||||
Copyright (C) 2009 by William Greiman
|
|
||||||
Copyright (c) 2010 SparkFun Electronics
|
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
@@ -1,116 +0,0 @@
|
|||||||
/*
|
|
||||||
SD card test
|
|
||||||
|
|
||||||
This example shows how use the utility libraries on which the'
|
|
||||||
SD library is based in order to get info about your SD card.
|
|
||||||
Very useful for testing a card when you're not sure whether its working or not.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
SD card attached to SPI bus as follows:
|
|
||||||
** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
|
|
||||||
** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
|
|
||||||
** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
|
|
||||||
** CS - depends on your SD card shield or module.
|
|
||||||
Pin 4 used here for consistency with other Arduino examples
|
|
||||||
|
|
||||||
|
|
||||||
created 28 Mar 2011
|
|
||||||
by Limor Fried
|
|
||||||
modified 9 Apr 2012
|
|
||||||
by Tom Igoe
|
|
||||||
*/
|
|
||||||
// include the SD library:
|
|
||||||
#include <SPI.h>
|
|
||||||
#include <SD.h>
|
|
||||||
|
|
||||||
// set up variables using the SD utility library functions:
|
|
||||||
Sd2Card card;
|
|
||||||
SdVolume volume;
|
|
||||||
SdFile root;
|
|
||||||
|
|
||||||
// change this to match your SD shield or module;
|
|
||||||
// Arduino Ethernet shield: pin 4
|
|
||||||
// Adafruit SD shields and modules: pin 10
|
|
||||||
// Sparkfun SD shield: pin 8
|
|
||||||
// MKRZero SD: SDCARD_SS_PIN
|
|
||||||
const int chipSelect = 4;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// Open serial communications and wait for port to open:
|
|
||||||
Serial.begin(9600);
|
|
||||||
while (!Serial) {
|
|
||||||
; // wait for serial port to connect. Needed for native USB port only
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Serial.print("\nInitializing SD card...");
|
|
||||||
|
|
||||||
// we'll use the initialization code from the utility libraries
|
|
||||||
// since we're just testing if the card is working!
|
|
||||||
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
|
|
||||||
Serial.println("initialization failed. Things to check:");
|
|
||||||
Serial.println("* is a card inserted?");
|
|
||||||
Serial.println("* is your wiring correct?");
|
|
||||||
Serial.println("* did you change the chipSelect pin to match your shield or module?");
|
|
||||||
while (1);
|
|
||||||
} else {
|
|
||||||
Serial.println("Wiring is correct and a card is present.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// print the type of card
|
|
||||||
Serial.println();
|
|
||||||
Serial.print("Card type: ");
|
|
||||||
switch (card.type()) {
|
|
||||||
case SD_CARD_TYPE_SD1:
|
|
||||||
Serial.println("SD1");
|
|
||||||
break;
|
|
||||||
case SD_CARD_TYPE_SD2:
|
|
||||||
Serial.println("SD2");
|
|
||||||
break;
|
|
||||||
case SD_CARD_TYPE_SDHC:
|
|
||||||
Serial.println("SDHC");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
Serial.println("Unknown");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
|
|
||||||
if (!volume.init(card)) {
|
|
||||||
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
|
|
||||||
while (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.print("Clusters: ");
|
|
||||||
Serial.println(volume.clusterCount());
|
|
||||||
Serial.print("Blocks x Cluster: ");
|
|
||||||
Serial.println(volume.blocksPerCluster());
|
|
||||||
|
|
||||||
Serial.print("Total Blocks: ");
|
|
||||||
Serial.println(volume.blocksPerCluster() * volume.clusterCount());
|
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
// print the type and size of the first FAT-type volume
|
|
||||||
uint32_t volumesize;
|
|
||||||
Serial.print("Volume type is: FAT");
|
|
||||||
Serial.println(volume.fatType(), DEC);
|
|
||||||
|
|
||||||
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
|
|
||||||
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
|
|
||||||
volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB)
|
|
||||||
Serial.print("Volume size (Kb): ");
|
|
||||||
Serial.println(volumesize);
|
|
||||||
Serial.print("Volume size (Mb): ");
|
|
||||||
volumesize /= 1024;
|
|
||||||
Serial.println(volumesize);
|
|
||||||
Serial.print("Volume size (Gb): ");
|
|
||||||
Serial.println((float)volumesize / 1024.0);
|
|
||||||
|
|
||||||
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
|
|
||||||
root.openRoot(volume);
|
|
||||||
|
|
||||||
// list all files in the card with date and size
|
|
||||||
root.ls(LS_R | LS_DATE | LS_SIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop(void) {
|
|
||||||
}
|
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
/*
|
|
||||||
SD card datalogger
|
|
||||||
|
|
||||||
This example shows how to log data from three analog sensors
|
|
||||||
to an SD card using the SD library.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
analog sensors on analog ins 0, 1, and 2
|
|
||||||
SD card attached to SPI bus as follows:
|
|
||||||
** MOSI - pin 11
|
|
||||||
** MISO - pin 12
|
|
||||||
** CLK - pin 13
|
|
||||||
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
|
|
||||||
|
|
||||||
created 24 Nov 2010
|
|
||||||
modified 9 Apr 2012
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <SPI.h>
|
|
||||||
#include <SD.h>
|
|
||||||
|
|
||||||
const int chipSelect = 4;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// Open serial communications and wait for port to open:
|
|
||||||
Serial.begin(9600);
|
|
||||||
while (!Serial) {
|
|
||||||
; // wait for serial port to connect. Needed for native USB port only
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Serial.print("Initializing SD card...");
|
|
||||||
|
|
||||||
// see if the card is present and can be initialized:
|
|
||||||
if (!SD.begin(chipSelect)) {
|
|
||||||
Serial.println("Card failed, or not present");
|
|
||||||
// don't do anything more:
|
|
||||||
while (1);
|
|
||||||
}
|
|
||||||
Serial.println("card initialized.");
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// make a string for assembling the data to log:
|
|
||||||
String dataString = "";
|
|
||||||
|
|
||||||
// read three sensors and append to the string:
|
|
||||||
for (int analogPin = 0; analogPin < 3; analogPin++) {
|
|
||||||
int sensor = analogRead(analogPin);
|
|
||||||
dataString += String(sensor);
|
|
||||||
if (analogPin < 2) {
|
|
||||||
dataString += ",";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// open the file. note that only one file can be open at a time,
|
|
||||||
// so you have to close this one before opening another.
|
|
||||||
File dataFile = SD.open("datalog.txt", FILE_WRITE);
|
|
||||||
|
|
||||||
// if the file is available, write to it:
|
|
||||||
if (dataFile) {
|
|
||||||
dataFile.println(dataString);
|
|
||||||
dataFile.close();
|
|
||||||
// print to the serial port too:
|
|
||||||
Serial.println(dataString);
|
|
||||||
}
|
|
||||||
// if the file isn't open, pop up an error:
|
|
||||||
else {
|
|
||||||
Serial.println("error opening datalog.txt");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
/*
|
|
||||||
SD card file dump
|
|
||||||
|
|
||||||
This example shows how to read a file from the SD card using the
|
|
||||||
SD library and send it over the serial port.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
SD card attached to SPI bus as follows:
|
|
||||||
** MOSI - pin 11
|
|
||||||
** MISO - pin 12
|
|
||||||
** CLK - pin 13
|
|
||||||
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
|
|
||||||
|
|
||||||
created 22 December 2010
|
|
||||||
by Limor Fried
|
|
||||||
modified 9 Apr 2012
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <SPI.h>
|
|
||||||
#include <SD.h>
|
|
||||||
|
|
||||||
const int chipSelect = 4;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// Open serial communications and wait for port to open:
|
|
||||||
Serial.begin(9600);
|
|
||||||
while (!Serial) {
|
|
||||||
; // wait for serial port to connect. Needed for native USB port only
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Serial.print("Initializing SD card...");
|
|
||||||
|
|
||||||
// see if the card is present and can be initialized:
|
|
||||||
if (!SD.begin(chipSelect)) {
|
|
||||||
Serial.println("Card failed, or not present");
|
|
||||||
// don't do anything more:
|
|
||||||
while (1);
|
|
||||||
}
|
|
||||||
Serial.println("card initialized.");
|
|
||||||
|
|
||||||
// open the file. note that only one file can be open at a time,
|
|
||||||
// so you have to close this one before opening another.
|
|
||||||
File dataFile = SD.open("datalog.txt");
|
|
||||||
|
|
||||||
// if the file is available, write to it:
|
|
||||||
if (dataFile) {
|
|
||||||
while (dataFile.available()) {
|
|
||||||
Serial.write(dataFile.read());
|
|
||||||
}
|
|
||||||
dataFile.close();
|
|
||||||
}
|
|
||||||
// if the file isn't open, pop up an error:
|
|
||||||
else {
|
|
||||||
Serial.println("error opening datalog.txt");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
/*
|
|
||||||
SD card basic file example
|
|
||||||
|
|
||||||
This example shows how to create and destroy an SD card file
|
|
||||||
The circuit:
|
|
||||||
SD card attached to SPI bus as follows:
|
|
||||||
** MOSI - pin 11
|
|
||||||
** MISO - pin 12
|
|
||||||
** CLK - pin 13
|
|
||||||
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
|
|
||||||
|
|
||||||
created Nov 2010
|
|
||||||
by David A. Mellis
|
|
||||||
modified 9 Apr 2012
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
*/
|
|
||||||
#include <SPI.h>
|
|
||||||
#include <SD.h>
|
|
||||||
|
|
||||||
File myFile;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// Open serial communications and wait for port to open:
|
|
||||||
Serial.begin(9600);
|
|
||||||
while (!Serial) {
|
|
||||||
; // wait for serial port to connect. Needed for native USB port only
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Serial.print("Initializing SD card...");
|
|
||||||
|
|
||||||
if (!SD.begin(4)) {
|
|
||||||
Serial.println("initialization failed!");
|
|
||||||
while (1);
|
|
||||||
}
|
|
||||||
Serial.println("initialization done.");
|
|
||||||
|
|
||||||
if (SD.exists("example.txt")) {
|
|
||||||
Serial.println("example.txt exists.");
|
|
||||||
} else {
|
|
||||||
Serial.println("example.txt doesn't exist.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// open a new file and immediately close it:
|
|
||||||
Serial.println("Creating example.txt...");
|
|
||||||
myFile = SD.open("example.txt", FILE_WRITE);
|
|
||||||
myFile.close();
|
|
||||||
|
|
||||||
// Check to see if the file exists:
|
|
||||||
if (SD.exists("example.txt")) {
|
|
||||||
Serial.println("example.txt exists.");
|
|
||||||
} else {
|
|
||||||
Serial.println("example.txt doesn't exist.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// delete the file:
|
|
||||||
Serial.println("Removing example.txt...");
|
|
||||||
SD.remove("example.txt");
|
|
||||||
|
|
||||||
if (SD.exists("example.txt")) {
|
|
||||||
Serial.println("example.txt exists.");
|
|
||||||
} else {
|
|
||||||
Serial.println("example.txt doesn't exist.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// nothing happens after setup finishes.
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
/*
|
|
||||||
Non-blocking Write
|
|
||||||
|
|
||||||
This example demonstrates how to perform non-blocking writes
|
|
||||||
to a file on a SD card. The file will contain the current millis()
|
|
||||||
value every 10ms. If the SD card is busy, the data will be buffered
|
|
||||||
in order to not block the sketch.
|
|
||||||
|
|
||||||
NOTE: myFile.availableForWrite() will automatically sync the
|
|
||||||
file contents as needed. You may lose some unsynced data
|
|
||||||
still if myFile.sync() or myFile.close() is not called.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- Arduino MKR Zero board
|
|
||||||
- micro SD card attached
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <SD.h>
|
|
||||||
|
|
||||||
// file name to use for writing
|
|
||||||
const char filename[] = "demo.txt";
|
|
||||||
|
|
||||||
// File object to represent file
|
|
||||||
File txtFile;
|
|
||||||
|
|
||||||
// string to buffer output
|
|
||||||
String buffer;
|
|
||||||
|
|
||||||
unsigned long lastMillis = 0;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
while (!Serial);
|
|
||||||
|
|
||||||
// reserve 1kB for String used as a buffer
|
|
||||||
buffer.reserve(1024);
|
|
||||||
|
|
||||||
// set LED pin to output, used to blink when writing
|
|
||||||
pinMode(LED_BUILTIN, OUTPUT);
|
|
||||||
|
|
||||||
// init the SD card
|
|
||||||
if (!SD.begin()) {
|
|
||||||
Serial.println("Card failed, or not present");
|
|
||||||
// don't do anything more:
|
|
||||||
while (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If you want to start from an empty file,
|
|
||||||
// uncomment the next line:
|
|
||||||
// SD.remove(filename);
|
|
||||||
|
|
||||||
// try to open the file for writing
|
|
||||||
txtFile = SD.open(filename, FILE_WRITE);
|
|
||||||
if (!txtFile) {
|
|
||||||
Serial.print("error opening ");
|
|
||||||
Serial.println(filename);
|
|
||||||
while (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// add some new lines to start
|
|
||||||
txtFile.println();
|
|
||||||
txtFile.println("Hello World!");
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// check if it's been over 10 ms since the last line added
|
|
||||||
unsigned long now = millis();
|
|
||||||
if ((now - lastMillis) >= 10) {
|
|
||||||
// add a new line to the buffer
|
|
||||||
buffer += "Hello ";
|
|
||||||
buffer += now;
|
|
||||||
buffer += "\r\n";
|
|
||||||
|
|
||||||
lastMillis = now;
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if the SD card is available to write data without blocking
|
|
||||||
// and if the buffered data is enough for the full chunk size
|
|
||||||
unsigned int chunkSize = txtFile.availableForWrite();
|
|
||||||
if (chunkSize && buffer.length() >= chunkSize) {
|
|
||||||
// write to file and blink LED
|
|
||||||
digitalWrite(LED_BUILTIN, HIGH);
|
|
||||||
txtFile.write(buffer.c_str(), chunkSize);
|
|
||||||
digitalWrite(LED_BUILTIN, LOW);
|
|
||||||
|
|
||||||
// remove written data from buffer
|
|
||||||
buffer.remove(0, chunkSize);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
/*
|
|
||||||
SD card read/write
|
|
||||||
|
|
||||||
This example shows how to read and write data to and from an SD card file
|
|
||||||
The circuit:
|
|
||||||
SD card attached to SPI bus as follows:
|
|
||||||
** MOSI - pin 11
|
|
||||||
** MISO - pin 12
|
|
||||||
** CLK - pin 13
|
|
||||||
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
|
|
||||||
|
|
||||||
created Nov 2010
|
|
||||||
by David A. Mellis
|
|
||||||
modified 9 Apr 2012
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <SPI.h>
|
|
||||||
#include <SD.h>
|
|
||||||
|
|
||||||
File myFile;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// Open serial communications and wait for port to open:
|
|
||||||
Serial.begin(9600);
|
|
||||||
while (!Serial) {
|
|
||||||
; // wait for serial port to connect. Needed for native USB port only
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Serial.print("Initializing SD card...");
|
|
||||||
|
|
||||||
if (!SD.begin(4)) {
|
|
||||||
Serial.println("initialization failed!");
|
|
||||||
while (1);
|
|
||||||
}
|
|
||||||
Serial.println("initialization done.");
|
|
||||||
|
|
||||||
// open the file. note that only one file can be open at a time,
|
|
||||||
// so you have to close this one before opening another.
|
|
||||||
myFile = SD.open("test.txt", FILE_WRITE);
|
|
||||||
|
|
||||||
// if the file opened okay, write to it:
|
|
||||||
if (myFile) {
|
|
||||||
Serial.print("Writing to test.txt...");
|
|
||||||
myFile.println("testing 1, 2, 3.");
|
|
||||||
// close the file:
|
|
||||||
myFile.close();
|
|
||||||
Serial.println("done.");
|
|
||||||
} else {
|
|
||||||
// if the file didn't open, print an error:
|
|
||||||
Serial.println("error opening test.txt");
|
|
||||||
}
|
|
||||||
|
|
||||||
// re-open the file for reading:
|
|
||||||
myFile = SD.open("test.txt");
|
|
||||||
if (myFile) {
|
|
||||||
Serial.println("test.txt:");
|
|
||||||
|
|
||||||
// read from the file until there's nothing else in it:
|
|
||||||
while (myFile.available()) {
|
|
||||||
Serial.write(myFile.read());
|
|
||||||
}
|
|
||||||
// close the file:
|
|
||||||
myFile.close();
|
|
||||||
} else {
|
|
||||||
// if the file didn't open, print an error:
|
|
||||||
Serial.println("error opening test.txt");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// nothing happens after setup
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
/*
|
|
||||||
Listfiles
|
|
||||||
|
|
||||||
This example shows how print out the files in a
|
|
||||||
directory on a SD card
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
SD card attached to SPI bus as follows:
|
|
||||||
** MOSI - pin 11
|
|
||||||
** MISO - pin 12
|
|
||||||
** CLK - pin 13
|
|
||||||
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
|
|
||||||
|
|
||||||
created Nov 2010
|
|
||||||
by David A. Mellis
|
|
||||||
modified 9 Apr 2012
|
|
||||||
by Tom Igoe
|
|
||||||
modified 2 Feb 2014
|
|
||||||
by Scott Fitzgerald
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
*/
|
|
||||||
#include <SPI.h>
|
|
||||||
#include <SD.h>
|
|
||||||
|
|
||||||
File root;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// Open serial communications and wait for port to open:
|
|
||||||
Serial.begin(9600);
|
|
||||||
while (!Serial) {
|
|
||||||
; // wait for serial port to connect. Needed for native USB port only
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.print("Initializing SD card...");
|
|
||||||
|
|
||||||
if (!SD.begin(4)) {
|
|
||||||
Serial.println("initialization failed!");
|
|
||||||
while (1);
|
|
||||||
}
|
|
||||||
Serial.println("initialization done.");
|
|
||||||
|
|
||||||
root = SD.open("/");
|
|
||||||
|
|
||||||
printDirectory(root, 0);
|
|
||||||
|
|
||||||
Serial.println("done!");
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// nothing happens after setup finishes.
|
|
||||||
}
|
|
||||||
|
|
||||||
void printDirectory(File dir, int numTabs) {
|
|
||||||
while (true) {
|
|
||||||
|
|
||||||
File entry = dir.openNextFile();
|
|
||||||
if (! entry) {
|
|
||||||
// no more files
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
for (uint8_t i = 0; i < numTabs; i++) {
|
|
||||||
Serial.print('\t');
|
|
||||||
}
|
|
||||||
Serial.print(entry.name());
|
|
||||||
if (entry.isDirectory()) {
|
|
||||||
Serial.println("/");
|
|
||||||
printDirectory(entry, numTabs + 1);
|
|
||||||
} else {
|
|
||||||
// files have sizes, directories do not
|
|
||||||
Serial.print("\t\t");
|
|
||||||
Serial.println(entry.size(), DEC);
|
|
||||||
}
|
|
||||||
entry.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
#######################################
|
|
||||||
# Syntax Coloring Map SD
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Datatypes (KEYWORD1)
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
SD KEYWORD1 SD
|
|
||||||
File KEYWORD1 SD
|
|
||||||
SDFile KEYWORD1 SD
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Methods and Functions (KEYWORD2)
|
|
||||||
#######################################
|
|
||||||
begin KEYWORD2
|
|
||||||
exists KEYWORD2
|
|
||||||
mkdir KEYWORD2
|
|
||||||
remove KEYWORD2
|
|
||||||
rmdir KEYWORD2
|
|
||||||
open KEYWORD2
|
|
||||||
close KEYWORD2
|
|
||||||
seek KEYWORD2
|
|
||||||
position KEYWORD2
|
|
||||||
size KEYWORD2
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Constants (LITERAL1)
|
|
||||||
#######################################
|
|
||||||
FILE_READ LITERAL1
|
|
||||||
FILE_WRITE LITERAL1
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
name=SD
|
|
||||||
version=1.2.4
|
|
||||||
author=Arduino, SparkFun
|
|
||||||
maintainer=Arduino <info@arduino.cc>
|
|
||||||
sentence=Enables reading and writing on SD cards.
|
|
||||||
paragraph=Once an SD memory card is connected to the SPI interface of the Arduino or Genuino board you can create files and read/write on them. You can also move through directories on the SD card.
|
|
||||||
category=Data Storage
|
|
||||||
url=http://www.arduino.cc/en/Reference/SD
|
|
||||||
architectures=*
|
|
||||||
@@ -1,168 +0,0 @@
|
|||||||
/*
|
|
||||||
|
|
||||||
SD - a slightly more friendly wrapper for sdfatlib
|
|
||||||
|
|
||||||
This library aims to expose a subset of SD card functionality
|
|
||||||
in the form of a higher level "wrapper" object.
|
|
||||||
|
|
||||||
License: GNU General Public License V3
|
|
||||||
(Because sdfatlib is licensed with this.)
|
|
||||||
|
|
||||||
(C) Copyright 2010 SparkFun Electronics
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <SD.h>
|
|
||||||
|
|
||||||
/* for debugging file open/close leaks
|
|
||||||
uint8_t nfilecount=0;
|
|
||||||
*/
|
|
||||||
|
|
||||||
File::File(SdFile f, const char *n) {
|
|
||||||
// oh man you are kidding me, new() doesn't exist? Ok we do it by hand!
|
|
||||||
_file = (SdFile *)malloc(sizeof(SdFile));
|
|
||||||
if (_file) {
|
|
||||||
memcpy(_file, &f, sizeof(SdFile));
|
|
||||||
|
|
||||||
strncpy(_name, n, 12);
|
|
||||||
_name[12] = 0;
|
|
||||||
|
|
||||||
/* for debugging file open/close leaks
|
|
||||||
nfilecount++;
|
|
||||||
Serial.print("Created \"");
|
|
||||||
Serial.print(n);
|
|
||||||
Serial.print("\": ");
|
|
||||||
Serial.println(nfilecount, DEC);
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
File::File(void) {
|
|
||||||
_file = 0;
|
|
||||||
_name[0] = 0;
|
|
||||||
//Serial.print("Created empty file object");
|
|
||||||
}
|
|
||||||
|
|
||||||
// returns a pointer to the file name
|
|
||||||
char *File::name(void) {
|
|
||||||
return _name;
|
|
||||||
}
|
|
||||||
|
|
||||||
// a directory is a special type of file
|
|
||||||
boolean File::isDirectory(void) {
|
|
||||||
return (_file && _file->isDir());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
size_t File::write(uint8_t val) {
|
|
||||||
return write(&val, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t File::write(const uint8_t *buf, size_t size) {
|
|
||||||
size_t t;
|
|
||||||
if (!_file) {
|
|
||||||
setWriteError();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
_file->clearWriteError();
|
|
||||||
t = _file->write(buf, size);
|
|
||||||
if (_file->getWriteError()) {
|
|
||||||
setWriteError();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
int File::availableForWrite() {
|
|
||||||
if (_file) {
|
|
||||||
return _file->availableForWrite();
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int File::peek() {
|
|
||||||
if (! _file) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int c = _file->read();
|
|
||||||
if (c != -1) {
|
|
||||||
_file->seekCur(-1);
|
|
||||||
}
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
int File::read() {
|
|
||||||
if (_file) {
|
|
||||||
return _file->read();
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// buffered read for more efficient, high speed reading
|
|
||||||
int File::read(void *buf, uint16_t nbyte) {
|
|
||||||
if (_file) {
|
|
||||||
return _file->read(buf, nbyte);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int File::available() {
|
|
||||||
if (! _file) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t n = size() - position();
|
|
||||||
|
|
||||||
return n > 0X7FFF ? 0X7FFF : n;
|
|
||||||
}
|
|
||||||
|
|
||||||
void File::flush() {
|
|
||||||
if (_file) {
|
|
||||||
_file->sync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean File::seek(uint32_t pos) {
|
|
||||||
if (! _file) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return _file->seekSet(pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t File::position() {
|
|
||||||
if (! _file) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return _file->curPosition();
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t File::size() {
|
|
||||||
if (! _file) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return _file->fileSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
void File::close() {
|
|
||||||
if (_file) {
|
|
||||||
_file->close();
|
|
||||||
free(_file);
|
|
||||||
_file = 0;
|
|
||||||
|
|
||||||
/* for debugging file open/close leaks
|
|
||||||
nfilecount--;
|
|
||||||
Serial.print("Deleted ");
|
|
||||||
Serial.println(nfilecount, DEC);
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
File::operator bool() {
|
|
||||||
if (_file) {
|
|
||||||
return _file->isOpen();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
|
|
||||||
** SD - a slightly more friendly wrapper for sdfatlib **
|
|
||||||
|
|
||||||
This library aims to expose a subset of SD card functionality in the
|
|
||||||
form of a higher level "wrapper" object.
|
|
||||||
|
|
||||||
License: GNU General Public License V3
|
|
||||||
(Because sdfatlib is licensed with this.)
|
|
||||||
|
|
||||||
(C) Copyright 2010 SparkFun Electronics
|
|
||||||
|
|
||||||
Now better than ever with optimization, multiple file support, directory handling, etc - ladyada!
|
|
||||||
|
|
||||||
@@ -1,637 +0,0 @@
|
|||||||
/*
|
|
||||||
|
|
||||||
SD - a slightly more friendly wrapper for sdfatlib
|
|
||||||
|
|
||||||
This library aims to expose a subset of SD card functionality
|
|
||||||
in the form of a higher level "wrapper" object.
|
|
||||||
|
|
||||||
License: GNU General Public License V3
|
|
||||||
(Because sdfatlib is licensed with this.)
|
|
||||||
|
|
||||||
(C) Copyright 2010 SparkFun Electronics
|
|
||||||
|
|
||||||
|
|
||||||
This library provides four key benefits:
|
|
||||||
|
|
||||||
Including `SD.h` automatically creates a global
|
|
||||||
`SD` object which can be interacted with in a similar
|
|
||||||
manner to other standard global objects like `Serial` and `Ethernet`.
|
|
||||||
|
|
||||||
Boilerplate initialisation code is contained in one method named
|
|
||||||
`begin` and no further objects need to be created in order to access
|
|
||||||
the SD card.
|
|
||||||
|
|
||||||
Calls to `open` can supply a full path name including parent
|
|
||||||
directories which simplifies interacting with files in subdirectories.
|
|
||||||
|
|
||||||
Utility methods are provided to determine whether a file exists
|
|
||||||
and to create a directory hierarchy.
|
|
||||||
|
|
||||||
|
|
||||||
Note however that not all functionality provided by the underlying
|
|
||||||
sdfatlib library is exposed.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
Implementation Notes
|
|
||||||
|
|
||||||
In order to handle multi-directory path traversal, functionality that
|
|
||||||
requires this ability is implemented as callback functions.
|
|
||||||
|
|
||||||
Individual methods call the `walkPath` function which performs the actual
|
|
||||||
directory traversal (swapping between two different directory/file handles
|
|
||||||
along the way) and at each level calls the supplied callback function.
|
|
||||||
|
|
||||||
Some types of functionality will take an action at each level (e.g. exists
|
|
||||||
or make directory) which others will only take an action at the bottom
|
|
||||||
level (e.g. open).
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "SD.h"
|
|
||||||
|
|
||||||
namespace SDLib {
|
|
||||||
|
|
||||||
// Used by `getNextPathComponent`
|
|
||||||
#define MAX_COMPONENT_LEN 12 // What is max length?
|
|
||||||
#define PATH_COMPONENT_BUFFER_LEN MAX_COMPONENT_LEN+1
|
|
||||||
|
|
||||||
bool getNextPathComponent(const char *path, unsigned int *p_offset,
|
|
||||||
char *buffer) {
|
|
||||||
/*
|
|
||||||
|
|
||||||
Parse individual path components from a path.
|
|
||||||
|
|
||||||
e.g. after repeated calls '/foo/bar/baz' will be split
|
|
||||||
into 'foo', 'bar', 'baz'.
|
|
||||||
|
|
||||||
This is similar to `strtok()` but copies the component into the
|
|
||||||
supplied buffer rather than modifying the original string.
|
|
||||||
|
|
||||||
|
|
||||||
`buffer` needs to be PATH_COMPONENT_BUFFER_LEN in size.
|
|
||||||
|
|
||||||
`p_offset` needs to point to an integer of the offset at
|
|
||||||
which the previous path component finished.
|
|
||||||
|
|
||||||
Returns `true` if more components remain.
|
|
||||||
|
|
||||||
Returns `false` if this is the last component.
|
|
||||||
(This means path ended with 'foo' or 'foo/'.)
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
// TODO: Have buffer local to this function, so we know it's the
|
|
||||||
// correct length?
|
|
||||||
|
|
||||||
int bufferOffset = 0;
|
|
||||||
|
|
||||||
int offset = *p_offset;
|
|
||||||
|
|
||||||
// Skip root or other separator
|
|
||||||
if (path[offset] == '/') {
|
|
||||||
offset++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy the next next path segment
|
|
||||||
while (bufferOffset < MAX_COMPONENT_LEN
|
|
||||||
&& (path[offset] != '/')
|
|
||||||
&& (path[offset] != '\0')) {
|
|
||||||
buffer[bufferOffset++] = path[offset++];
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer[bufferOffset] = '\0';
|
|
||||||
|
|
||||||
// Skip trailing separator so we can determine if this
|
|
||||||
// is the last component in the path or not.
|
|
||||||
if (path[offset] == '/') {
|
|
||||||
offset++;
|
|
||||||
}
|
|
||||||
|
|
||||||
*p_offset = offset;
|
|
||||||
|
|
||||||
return (path[offset] != '\0');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
boolean walkPath(const char *filepath, SdFile& parentDir,
|
|
||||||
boolean(*callback)(SdFile& parentDir,
|
|
||||||
const char *filePathComponent,
|
|
||||||
boolean isLastComponent,
|
|
||||||
void *object),
|
|
||||||
void *object = NULL) {
|
|
||||||
/*
|
|
||||||
|
|
||||||
When given a file path (and parent directory--normally root),
|
|
||||||
this function traverses the directories in the path and at each
|
|
||||||
level calls the supplied callback function while also providing
|
|
||||||
the supplied object for context if required.
|
|
||||||
|
|
||||||
e.g. given the path '/foo/bar/baz'
|
|
||||||
the callback would be called at the equivalent of
|
|
||||||
'/foo', '/foo/bar' and '/foo/bar/baz'.
|
|
||||||
|
|
||||||
The implementation swaps between two different directory/file
|
|
||||||
handles as it traverses the directories and does not use recursion
|
|
||||||
in an attempt to use memory efficiently.
|
|
||||||
|
|
||||||
If a callback wishes to stop the directory traversal it should
|
|
||||||
return false--in this case the function will stop the traversal,
|
|
||||||
tidy up and return false.
|
|
||||||
|
|
||||||
If a directory path doesn't exist at some point this function will
|
|
||||||
also return false and not subsequently call the callback.
|
|
||||||
|
|
||||||
If a directory path specified is complete, valid and the callback
|
|
||||||
did not indicate the traversal should be interrupted then this
|
|
||||||
function will return true.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
SdFile subfile1;
|
|
||||||
SdFile subfile2;
|
|
||||||
|
|
||||||
char buffer[PATH_COMPONENT_BUFFER_LEN];
|
|
||||||
|
|
||||||
unsigned int offset = 0;
|
|
||||||
|
|
||||||
SdFile *p_parent;
|
|
||||||
SdFile *p_child;
|
|
||||||
|
|
||||||
SdFile *p_tmp_sdfile;
|
|
||||||
|
|
||||||
p_child = &subfile1;
|
|
||||||
|
|
||||||
p_parent = &parentDir;
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
|
|
||||||
boolean moreComponents = getNextPathComponent(filepath, &offset, buffer);
|
|
||||||
|
|
||||||
boolean shouldContinue = callback((*p_parent), buffer, !moreComponents, object);
|
|
||||||
|
|
||||||
if (!shouldContinue) {
|
|
||||||
// TODO: Don't repeat this code?
|
|
||||||
// If it's one we've created then we
|
|
||||||
// don't need the parent handle anymore.
|
|
||||||
if (p_parent != &parentDir) {
|
|
||||||
(*p_parent).close();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!moreComponents) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean exists = (*p_child).open(*p_parent, buffer, O_RDONLY);
|
|
||||||
|
|
||||||
// If it's one we've created then we
|
|
||||||
// don't need the parent handle anymore.
|
|
||||||
if (p_parent != &parentDir) {
|
|
||||||
(*p_parent).close();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle case when it doesn't exist and we can't continue...
|
|
||||||
if (exists) {
|
|
||||||
// We alternate between two file handles as we go down
|
|
||||||
// the path.
|
|
||||||
if (p_parent == &parentDir) {
|
|
||||||
p_parent = &subfile2;
|
|
||||||
}
|
|
||||||
|
|
||||||
p_tmp_sdfile = p_parent;
|
|
||||||
p_parent = p_child;
|
|
||||||
p_child = p_tmp_sdfile;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (p_parent != &parentDir) {
|
|
||||||
(*p_parent).close(); // TODO: Return/ handle different?
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
The callbacks used to implement various functionality follow.
|
|
||||||
|
|
||||||
Each callback is supplied with a parent directory handle,
|
|
||||||
character string with the name of the current file path component,
|
|
||||||
a flag indicating if this component is the last in the path and
|
|
||||||
a pointer to an arbitrary object used for context.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
boolean callback_pathExists(SdFile& parentDir, const char *filePathComponent,
|
|
||||||
boolean /* isLastComponent */, void * /* object */) {
|
|
||||||
/*
|
|
||||||
|
|
||||||
Callback used to determine if a file/directory exists in parent
|
|
||||||
directory.
|
|
||||||
|
|
||||||
Returns true if file path exists.
|
|
||||||
|
|
||||||
*/
|
|
||||||
SdFile child;
|
|
||||||
|
|
||||||
boolean exists = child.open(parentDir, filePathComponent, O_RDONLY);
|
|
||||||
|
|
||||||
if (exists) {
|
|
||||||
child.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
return exists;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
boolean callback_makeDirPath(SdFile& parentDir, const char *filePathComponent,
|
|
||||||
boolean isLastComponent, void *object) {
|
|
||||||
/*
|
|
||||||
|
|
||||||
Callback used to create a directory in the parent directory if
|
|
||||||
it does not already exist.
|
|
||||||
|
|
||||||
Returns true if a directory was created or it already existed.
|
|
||||||
|
|
||||||
*/
|
|
||||||
boolean result = false;
|
|
||||||
SdFile child;
|
|
||||||
|
|
||||||
result = callback_pathExists(parentDir, filePathComponent, isLastComponent, object);
|
|
||||||
if (!result) {
|
|
||||||
result = child.makeDir(parentDir, filePathComponent);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
|
|
||||||
boolean isLastComponent, void *object) {
|
|
||||||
|
|
||||||
Callback used to open a file specified by a filepath that may
|
|
||||||
specify one or more directories above it.
|
|
||||||
|
|
||||||
Expects the context object to be an instance of `SDClass` and
|
|
||||||
will use the `file` property of the instance to open the requested
|
|
||||||
file/directory with the associated file open mode property.
|
|
||||||
|
|
||||||
Always returns true if the directory traversal hasn't reached the
|
|
||||||
bottom of the directory hierarchy.
|
|
||||||
|
|
||||||
Returns false once the file has been opened--to prevent the traversal
|
|
||||||
from descending further. (This may be unnecessary.)
|
|
||||||
|
|
||||||
if (isLastComponent) {
|
|
||||||
SDClass *p_SD = static_cast<SDClass*>(object);
|
|
||||||
p_SD->file.open(parentDir, filePathComponent, p_SD->fileOpenMode);
|
|
||||||
if (p_SD->fileOpenMode == FILE_WRITE) {
|
|
||||||
p_SD->file.seekSet(p_SD->file.fileSize());
|
|
||||||
}
|
|
||||||
// TODO: Return file open result?
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
boolean callback_remove(SdFile& parentDir, const char *filePathComponent,
|
|
||||||
boolean isLastComponent, void * /* object */) {
|
|
||||||
if (isLastComponent) {
|
|
||||||
return SdFile::remove(parentDir, filePathComponent);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean callback_rmdir(SdFile& parentDir, const char *filePathComponent,
|
|
||||||
boolean isLastComponent, void * /* object */) {
|
|
||||||
if (isLastComponent) {
|
|
||||||
SdFile f;
|
|
||||||
if (!f.open(parentDir, filePathComponent, O_READ)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return f.rmDir();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Implementation of class used to create `SDCard` object. */
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
boolean SDClass::begin(uint8_t csPin) {
|
|
||||||
if (root.isOpen()) {
|
|
||||||
root.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
Performs the initialisation required by the sdfatlib library.
|
|
||||||
|
|
||||||
Return true if initialization succeeds, false otherwise.
|
|
||||||
|
|
||||||
*/
|
|
||||||
return card.init(SPI_HALF_SPEED, csPin) &&
|
|
||||||
volume.init(card) &&
|
|
||||||
root.openRoot(volume);
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean SDClass::begin(uint32_t clock, uint8_t csPin) {
|
|
||||||
if (root.isOpen()) {
|
|
||||||
root.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
return card.init(SPI_HALF_SPEED, csPin) &&
|
|
||||||
card.setSpiClock(clock) &&
|
|
||||||
volume.init(card) &&
|
|
||||||
root.openRoot(volume);
|
|
||||||
}
|
|
||||||
|
|
||||||
//call this when a card is removed. It will allow you to insert and initialise a new card.
|
|
||||||
void SDClass::end() {
|
|
||||||
root.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
// this little helper is used to traverse paths
|
|
||||||
SdFile SDClass::getParentDir(const char *filepath, int *index) {
|
|
||||||
// get parent directory
|
|
||||||
SdFile d1;
|
|
||||||
SdFile d2;
|
|
||||||
|
|
||||||
d1.openRoot(volume); // start with the mostparent, root!
|
|
||||||
|
|
||||||
// we'll use the pointers to swap between the two objects
|
|
||||||
SdFile *parent = &d1;
|
|
||||||
SdFile *subdir = &d2;
|
|
||||||
|
|
||||||
const char *origpath = filepath;
|
|
||||||
|
|
||||||
while (strchr(filepath, '/')) {
|
|
||||||
|
|
||||||
// get rid of leading /'s
|
|
||||||
if (filepath[0] == '/') {
|
|
||||||
filepath++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! strchr(filepath, '/')) {
|
|
||||||
// it was in the root directory, so leave now
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// extract just the name of the next subdirectory
|
|
||||||
uint8_t idx = strchr(filepath, '/') - filepath;
|
|
||||||
if (idx > 12) {
|
|
||||||
idx = 12; // don't let them specify long names
|
|
||||||
}
|
|
||||||
char subdirname[13];
|
|
||||||
strncpy(subdirname, filepath, idx);
|
|
||||||
subdirname[idx] = 0;
|
|
||||||
|
|
||||||
// close the subdir (we reuse them) if open
|
|
||||||
subdir->close();
|
|
||||||
if (! subdir->open(parent, subdirname, O_READ)) {
|
|
||||||
// failed to open one of the subdirectories
|
|
||||||
return SdFile();
|
|
||||||
}
|
|
||||||
// move forward to the next subdirectory
|
|
||||||
filepath += idx;
|
|
||||||
|
|
||||||
// we reuse the objects, close it.
|
|
||||||
parent->close();
|
|
||||||
|
|
||||||
// swap the pointers
|
|
||||||
SdFile *t = parent;
|
|
||||||
parent = subdir;
|
|
||||||
subdir = t;
|
|
||||||
}
|
|
||||||
|
|
||||||
*index = (int)(filepath - origpath);
|
|
||||||
// parent is now the parent directory of the file!
|
|
||||||
return *parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
File SDClass::open(const char *filepath, uint8_t mode) {
|
|
||||||
/*
|
|
||||||
|
|
||||||
Open the supplied file path for reading or writing.
|
|
||||||
|
|
||||||
The file content can be accessed via the `file` property of
|
|
||||||
the `SDClass` object--this property is currently
|
|
||||||
a standard `SdFile` object from `sdfatlib`.
|
|
||||||
|
|
||||||
Defaults to read only.
|
|
||||||
|
|
||||||
If `write` is true, default action (when `append` is true) is to
|
|
||||||
append data to the end of the file.
|
|
||||||
|
|
||||||
If `append` is false then the file will be truncated first.
|
|
||||||
|
|
||||||
If the file does not exist and it is opened for writing the file
|
|
||||||
will be created.
|
|
||||||
|
|
||||||
An attempt to open a file for reading that does not exist is an
|
|
||||||
error.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
int pathidx;
|
|
||||||
|
|
||||||
// do the interactive search
|
|
||||||
SdFile parentdir = getParentDir(filepath, &pathidx);
|
|
||||||
// no more subdirs!
|
|
||||||
|
|
||||||
filepath += pathidx;
|
|
||||||
|
|
||||||
if (! filepath[0]) {
|
|
||||||
// it was the directory itself!
|
|
||||||
return File(parentdir, "/");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open the file itself
|
|
||||||
SdFile file;
|
|
||||||
|
|
||||||
// failed to open a subdir!
|
|
||||||
if (!parentdir.isOpen()) {
|
|
||||||
return File();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! file.open(parentdir, filepath, mode)) {
|
|
||||||
return File();
|
|
||||||
}
|
|
||||||
// close the parent
|
|
||||||
parentdir.close();
|
|
||||||
|
|
||||||
if ((mode & (O_APPEND | O_WRITE)) == (O_APPEND | O_WRITE)) {
|
|
||||||
file.seekSet(file.fileSize());
|
|
||||||
}
|
|
||||||
return File(file, filepath);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
File SDClass::open(char *filepath, uint8_t mode) {
|
|
||||||
//
|
|
||||||
|
|
||||||
Open the supplied file path for reading or writing.
|
|
||||||
|
|
||||||
The file content can be accessed via the `file` property of
|
|
||||||
the `SDClass` object--this property is currently
|
|
||||||
a standard `SdFile` object from `sdfatlib`.
|
|
||||||
|
|
||||||
Defaults to read only.
|
|
||||||
|
|
||||||
If `write` is true, default action (when `append` is true) is to
|
|
||||||
append data to the end of the file.
|
|
||||||
|
|
||||||
If `append` is false then the file will be truncated first.
|
|
||||||
|
|
||||||
If the file does not exist and it is opened for writing the file
|
|
||||||
will be created.
|
|
||||||
|
|
||||||
An attempt to open a file for reading that does not exist is an
|
|
||||||
error.
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
// TODO: Allow for read&write? (Possibly not, as it requires seek.)
|
|
||||||
|
|
||||||
fileOpenMode = mode;
|
|
||||||
walkPath(filepath, root, callback_openPath, this);
|
|
||||||
|
|
||||||
return File();
|
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
//boolean SDClass::close() {
|
|
||||||
// /*
|
|
||||||
//
|
|
||||||
// Closes the file opened by the `open` method.
|
|
||||||
//
|
|
||||||
// */
|
|
||||||
// file.close();
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
boolean SDClass::exists(const char *filepath) {
|
|
||||||
/*
|
|
||||||
|
|
||||||
Returns true if the supplied file path exists.
|
|
||||||
|
|
||||||
*/
|
|
||||||
return walkPath(filepath, root, callback_pathExists);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//boolean SDClass::exists(char *filepath, SdFile& parentDir) {
|
|
||||||
// /*
|
|
||||||
//
|
|
||||||
// Returns true if the supplied file path rooted at `parentDir`
|
|
||||||
// exists.
|
|
||||||
//
|
|
||||||
// */
|
|
||||||
// return walkPath(filepath, parentDir, callback_pathExists);
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
boolean SDClass::mkdir(const char *filepath) {
|
|
||||||
/*
|
|
||||||
|
|
||||||
Makes a single directory or a hierarchy of directories.
|
|
||||||
|
|
||||||
A rough equivalent to `mkdir -p`.
|
|
||||||
|
|
||||||
*/
|
|
||||||
return walkPath(filepath, root, callback_makeDirPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean SDClass::rmdir(const char *filepath) {
|
|
||||||
/*
|
|
||||||
|
|
||||||
Remove a single directory or a hierarchy of directories.
|
|
||||||
|
|
||||||
A rough equivalent to `rm -rf`.
|
|
||||||
|
|
||||||
*/
|
|
||||||
return walkPath(filepath, root, callback_rmdir);
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean SDClass::remove(const char *filepath) {
|
|
||||||
return walkPath(filepath, root, callback_remove);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// allows you to recurse into a directory
|
|
||||||
File File::openNextFile(uint8_t mode) {
|
|
||||||
dir_t p;
|
|
||||||
|
|
||||||
//Serial.print("\t\treading dir...");
|
|
||||||
while (_file->readDir(&p) > 0) {
|
|
||||||
|
|
||||||
// done if past last used entry
|
|
||||||
if (p.name[0] == DIR_NAME_FREE) {
|
|
||||||
//Serial.println("end");
|
|
||||||
return File();
|
|
||||||
}
|
|
||||||
|
|
||||||
// skip deleted entry and entries for . and ..
|
|
||||||
if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') {
|
|
||||||
//Serial.println("dots");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// only list subdirectories and files
|
|
||||||
if (!DIR_IS_FILE_OR_SUBDIR(&p)) {
|
|
||||||
//Serial.println("notafile");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// print file name with possible blank fill
|
|
||||||
SdFile f;
|
|
||||||
char name[13];
|
|
||||||
_file->dirName(p, name);
|
|
||||||
//Serial.print("try to open file ");
|
|
||||||
//Serial.println(name);
|
|
||||||
|
|
||||||
if (f.open(_file, name, mode)) {
|
|
||||||
//Serial.println("OK!");
|
|
||||||
return File(f, name);
|
|
||||||
} else {
|
|
||||||
//Serial.println("ugh");
|
|
||||||
return File();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Serial.println("nothing");
|
|
||||||
return File();
|
|
||||||
}
|
|
||||||
|
|
||||||
void File::rewindDirectory(void) {
|
|
||||||
if (isDirectory()) {
|
|
||||||
_file->rewind();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SDClass SD;
|
|
||||||
|
|
||||||
};
|
|
||||||
@@ -1,138 +0,0 @@
|
|||||||
/*
|
|
||||||
|
|
||||||
SD - a slightly more friendly wrapper for sdfatlib
|
|
||||||
|
|
||||||
This library aims to expose a subset of SD card functionality
|
|
||||||
in the form of a higher level "wrapper" object.
|
|
||||||
|
|
||||||
License: GNU General Public License V3
|
|
||||||
(Because sdfatlib is licensed with this.)
|
|
||||||
|
|
||||||
(C) Copyright 2010 SparkFun Electronics
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef __SD_H__
|
|
||||||
#define __SD_H__
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
#include "utility/SdFat.h"
|
|
||||||
#include "utility/SdFatUtil.h"
|
|
||||||
|
|
||||||
#define FILE_READ O_READ
|
|
||||||
#define FILE_WRITE (O_READ | O_WRITE | O_CREAT | O_APPEND)
|
|
||||||
|
|
||||||
namespace SDLib {
|
|
||||||
|
|
||||||
class File : public Stream {
|
|
||||||
private:
|
|
||||||
char _name[13]; // our name
|
|
||||||
SdFile *_file; // underlying file pointer
|
|
||||||
|
|
||||||
public:
|
|
||||||
File(SdFile f, const char *name); // wraps an underlying SdFile
|
|
||||||
File(void); // 'empty' constructor
|
|
||||||
virtual size_t write(uint8_t);
|
|
||||||
virtual size_t write(const uint8_t *buf, size_t size);
|
|
||||||
virtual int availableForWrite();
|
|
||||||
virtual int read();
|
|
||||||
virtual int peek();
|
|
||||||
virtual int available();
|
|
||||||
virtual void flush();
|
|
||||||
int read(void *buf, uint16_t nbyte);
|
|
||||||
boolean seek(uint32_t pos);
|
|
||||||
uint32_t position();
|
|
||||||
uint32_t size();
|
|
||||||
void close();
|
|
||||||
operator bool();
|
|
||||||
char * name();
|
|
||||||
|
|
||||||
boolean isDirectory(void);
|
|
||||||
File openNextFile(uint8_t mode = O_RDONLY);
|
|
||||||
void rewindDirectory(void);
|
|
||||||
|
|
||||||
using Print::write;
|
|
||||||
};
|
|
||||||
|
|
||||||
class SDClass {
|
|
||||||
|
|
||||||
private:
|
|
||||||
// These are required for initialisation and use of sdfatlib
|
|
||||||
Sd2Card card;
|
|
||||||
SdVolume volume;
|
|
||||||
SdFile root;
|
|
||||||
|
|
||||||
// my quick&dirty iterator, should be replaced
|
|
||||||
SdFile getParentDir(const char *filepath, int *indx);
|
|
||||||
public:
|
|
||||||
// This needs to be called to set up the connection to the SD card
|
|
||||||
// before other methods are used.
|
|
||||||
boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN);
|
|
||||||
boolean begin(uint32_t clock, uint8_t csPin);
|
|
||||||
|
|
||||||
//call this when a card is removed. It will allow you to insert and initialise a new card.
|
|
||||||
void end();
|
|
||||||
|
|
||||||
// Open the specified file/directory with the supplied mode (e.g. read or
|
|
||||||
// write, etc). Returns a File object for interacting with the file.
|
|
||||||
// Note that currently only one file can be open at a time.
|
|
||||||
File open(const char *filename, uint8_t mode = FILE_READ);
|
|
||||||
File open(const String &filename, uint8_t mode = FILE_READ) {
|
|
||||||
return open(filename.c_str(), mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Methods to determine if the requested file path exists.
|
|
||||||
boolean exists(const char *filepath);
|
|
||||||
boolean exists(const String &filepath) {
|
|
||||||
return exists(filepath.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the requested directory heirarchy--if intermediate directories
|
|
||||||
// do not exist they will be created.
|
|
||||||
boolean mkdir(const char *filepath);
|
|
||||||
boolean mkdir(const String &filepath) {
|
|
||||||
return mkdir(filepath.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete the file.
|
|
||||||
boolean remove(const char *filepath);
|
|
||||||
boolean remove(const String &filepath) {
|
|
||||||
return remove(filepath.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean rmdir(const char *filepath);
|
|
||||||
boolean rmdir(const String &filepath) {
|
|
||||||
return rmdir(filepath.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
// This is used to determine the mode used to open a file
|
|
||||||
// it's here because it's the easiest place to pass the
|
|
||||||
// information through the directory walking function. But
|
|
||||||
// it's probably not the best place for it.
|
|
||||||
// It shouldn't be set directly--it is set via the parameters to `open`.
|
|
||||||
int fileOpenMode;
|
|
||||||
|
|
||||||
friend class File;
|
|
||||||
friend boolean callback_openPath(SdFile&, const char *, boolean, void *);
|
|
||||||
};
|
|
||||||
|
|
||||||
extern SDClass SD;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
// We enclose File and SD classes in namespace SDLib to avoid conflicts
|
|
||||||
// with others legacy libraries that redefines File class.
|
|
||||||
|
|
||||||
// This ensure compatibility with sketches that uses only SD library
|
|
||||||
using namespace SDLib;
|
|
||||||
|
|
||||||
// This allows sketches to use SDLib::File with other libraries (in the
|
|
||||||
// sketch you must use SDFile instead of File to disambiguate)
|
|
||||||
typedef SDLib::File SDFile;
|
|
||||||
typedef SDLib::SDClass SDFileSystemClass;
|
|
||||||
#define SDFileSystem SDLib::SD
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,418 +0,0 @@
|
|||||||
/* Arduino SdFat Library
|
|
||||||
Copyright (C) 2009 by William Greiman
|
|
||||||
|
|
||||||
This file is part of the Arduino SdFat Library
|
|
||||||
|
|
||||||
This Library is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This Library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with the Arduino SdFat Library. If not, see
|
|
||||||
<http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
#ifndef FatStructs_h
|
|
||||||
#define FatStructs_h
|
|
||||||
/**
|
|
||||||
\file
|
|
||||||
FAT file structures
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
mostly from Microsoft document fatgen103.doc
|
|
||||||
http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx
|
|
||||||
*/
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** Value for byte 510 of boot block or MBR */
|
|
||||||
uint8_t const BOOTSIG0 = 0X55;
|
|
||||||
/** Value for byte 511 of boot block or MBR */
|
|
||||||
uint8_t const BOOTSIG1 = 0XAA;
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
\struct partitionTable
|
|
||||||
\brief MBR partition table entry
|
|
||||||
|
|
||||||
A partition table entry for a MBR formatted storage device.
|
|
||||||
The MBR partition table has four entries.
|
|
||||||
*/
|
|
||||||
struct partitionTable {
|
|
||||||
/**
|
|
||||||
Boot Indicator . Indicates whether the volume is the active
|
|
||||||
partition. Legal values include: 0X00. Do not use for booting.
|
|
||||||
0X80 Active partition.
|
|
||||||
*/
|
|
||||||
uint8_t boot;
|
|
||||||
/**
|
|
||||||
Head part of Cylinder-head-sector address of the first block in
|
|
||||||
the partition. Legal values are 0-255. Only used in old PC BIOS.
|
|
||||||
*/
|
|
||||||
uint8_t beginHead;
|
|
||||||
/**
|
|
||||||
Sector part of Cylinder-head-sector address of the first block in
|
|
||||||
the partition. Legal values are 1-63. Only used in old PC BIOS.
|
|
||||||
*/
|
|
||||||
unsigned beginSector : 6;
|
|
||||||
/** High bits cylinder for first block in partition. */
|
|
||||||
unsigned beginCylinderHigh : 2;
|
|
||||||
/**
|
|
||||||
Combine beginCylinderLow with beginCylinderHigh. Legal values
|
|
||||||
are 0-1023. Only used in old PC BIOS.
|
|
||||||
*/
|
|
||||||
uint8_t beginCylinderLow;
|
|
||||||
/**
|
|
||||||
Partition type. See defines that begin with PART_TYPE_ for
|
|
||||||
some Microsoft partition types.
|
|
||||||
*/
|
|
||||||
uint8_t type;
|
|
||||||
/**
|
|
||||||
head part of cylinder-head-sector address of the last sector in the
|
|
||||||
partition. Legal values are 0-255. Only used in old PC BIOS.
|
|
||||||
*/
|
|
||||||
uint8_t endHead;
|
|
||||||
/**
|
|
||||||
Sector part of cylinder-head-sector address of the last sector in
|
|
||||||
the partition. Legal values are 1-63. Only used in old PC BIOS.
|
|
||||||
*/
|
|
||||||
unsigned endSector : 6;
|
|
||||||
/** High bits of end cylinder */
|
|
||||||
unsigned endCylinderHigh : 2;
|
|
||||||
/**
|
|
||||||
Combine endCylinderLow with endCylinderHigh. Legal values
|
|
||||||
are 0-1023. Only used in old PC BIOS.
|
|
||||||
*/
|
|
||||||
uint8_t endCylinderLow;
|
|
||||||
/** Logical block address of the first block in the partition. */
|
|
||||||
uint32_t firstSector;
|
|
||||||
/** Length of the partition, in blocks. */
|
|
||||||
uint32_t totalSectors;
|
|
||||||
} __attribute__((packed));
|
|
||||||
/** Type name for partitionTable */
|
|
||||||
typedef struct partitionTable part_t;
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
\struct masterBootRecord
|
|
||||||
|
|
||||||
\brief Master Boot Record
|
|
||||||
|
|
||||||
The first block of a storage device that is formatted with a MBR.
|
|
||||||
*/
|
|
||||||
struct masterBootRecord {
|
|
||||||
/** Code Area for master boot program. */
|
|
||||||
uint8_t codeArea[440];
|
|
||||||
/** Optional WindowsNT disk signature. May contain more boot code. */
|
|
||||||
uint32_t diskSignature;
|
|
||||||
/** Usually zero but may be more boot code. */
|
|
||||||
uint16_t usuallyZero;
|
|
||||||
/** Partition tables. */
|
|
||||||
part_t part[4];
|
|
||||||
/** First MBR signature byte. Must be 0X55 */
|
|
||||||
uint8_t mbrSig0;
|
|
||||||
/** Second MBR signature byte. Must be 0XAA */
|
|
||||||
uint8_t mbrSig1;
|
|
||||||
} __attribute__((packed));
|
|
||||||
/** Type name for masterBootRecord */
|
|
||||||
typedef struct masterBootRecord mbr_t;
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
\struct biosParmBlock
|
|
||||||
|
|
||||||
\brief BIOS parameter block
|
|
||||||
|
|
||||||
The BIOS parameter block describes the physical layout of a FAT volume.
|
|
||||||
*/
|
|
||||||
struct biosParmBlock {
|
|
||||||
/**
|
|
||||||
Count of bytes per sector. This value may take on only the
|
|
||||||
following values: 512, 1024, 2048 or 4096
|
|
||||||
*/
|
|
||||||
uint16_t bytesPerSector;
|
|
||||||
/**
|
|
||||||
Number of sectors per allocation unit. This value must be a
|
|
||||||
power of 2 that is greater than 0. The legal values are
|
|
||||||
1, 2, 4, 8, 16, 32, 64, and 128.
|
|
||||||
*/
|
|
||||||
uint8_t sectorsPerCluster;
|
|
||||||
/**
|
|
||||||
Number of sectors before the first FAT.
|
|
||||||
This value must not be zero.
|
|
||||||
*/
|
|
||||||
uint16_t reservedSectorCount;
|
|
||||||
/** The count of FAT data structures on the volume. This field should
|
|
||||||
always contain the value 2 for any FAT volume of any type.
|
|
||||||
*/
|
|
||||||
uint8_t fatCount;
|
|
||||||
/**
|
|
||||||
For FAT12 and FAT16 volumes, this field contains the count of
|
|
||||||
32-byte directory entries in the root directory. For FAT32 volumes,
|
|
||||||
this field must be set to 0. For FAT12 and FAT16 volumes, this
|
|
||||||
value should always specify a count that when multiplied by 32
|
|
||||||
results in a multiple of bytesPerSector. FAT16 volumes should
|
|
||||||
use the value 512.
|
|
||||||
*/
|
|
||||||
uint16_t rootDirEntryCount;
|
|
||||||
/**
|
|
||||||
This field is the old 16-bit total count of sectors on the volume.
|
|
||||||
This count includes the count of all sectors in all four regions
|
|
||||||
of the volume. This field can be 0; if it is 0, then totalSectors32
|
|
||||||
must be non-zero. For FAT32 volumes, this field must be 0. For
|
|
||||||
FAT12 and FAT16 volumes, this field contains the sector count, and
|
|
||||||
totalSectors32 is 0 if the total sector count fits
|
|
||||||
(is less than 0x10000).
|
|
||||||
*/
|
|
||||||
uint16_t totalSectors16;
|
|
||||||
/**
|
|
||||||
This dates back to the old MS-DOS 1.x media determination and is
|
|
||||||
no longer usually used for anything. 0xF8 is the standard value
|
|
||||||
for fixed (non-removable) media. For removable media, 0xF0 is
|
|
||||||
frequently used. Legal values are 0xF0 or 0xF8-0xFF.
|
|
||||||
*/
|
|
||||||
uint8_t mediaType;
|
|
||||||
/**
|
|
||||||
Count of sectors occupied by one FAT on FAT12/FAT16 volumes.
|
|
||||||
On FAT32 volumes this field must be 0, and sectorsPerFat32
|
|
||||||
contains the FAT size count.
|
|
||||||
*/
|
|
||||||
uint16_t sectorsPerFat16;
|
|
||||||
/** Sectors per track for interrupt 0x13. Not used otherwise. */
|
|
||||||
uint16_t sectorsPerTrtack;
|
|
||||||
/** Number of heads for interrupt 0x13. Not used otherwise. */
|
|
||||||
uint16_t headCount;
|
|
||||||
/**
|
|
||||||
Count of hidden sectors preceding the partition that contains this
|
|
||||||
FAT volume. This field is generally only relevant for media
|
|
||||||
visible on interrupt 0x13.
|
|
||||||
*/
|
|
||||||
uint32_t hidddenSectors;
|
|
||||||
/**
|
|
||||||
This field is the new 32-bit total count of sectors on the volume.
|
|
||||||
This count includes the count of all sectors in all four regions
|
|
||||||
of the volume. This field can be 0; if it is 0, then
|
|
||||||
totalSectors16 must be non-zero.
|
|
||||||
*/
|
|
||||||
uint32_t totalSectors32;
|
|
||||||
/**
|
|
||||||
Count of sectors occupied by one FAT on FAT32 volumes.
|
|
||||||
*/
|
|
||||||
uint32_t sectorsPerFat32;
|
|
||||||
/**
|
|
||||||
This field is only defined for FAT32 media and does not exist on
|
|
||||||
FAT12 and FAT16 media.
|
|
||||||
Bits 0-3 -- Zero-based number of active FAT.
|
|
||||||
Only valid if mirroring is disabled.
|
|
||||||
Bits 4-6 -- Reserved.
|
|
||||||
Bit 7 -- 0 means the FAT is mirrored at runtime into all FATs.
|
|
||||||
-- 1 means only one FAT is active; it is the one referenced in bits 0-3.
|
|
||||||
Bits 8-15 -- Reserved.
|
|
||||||
*/
|
|
||||||
uint16_t fat32Flags;
|
|
||||||
/**
|
|
||||||
FAT32 version. High byte is major revision number.
|
|
||||||
Low byte is minor revision number. Only 0.0 define.
|
|
||||||
*/
|
|
||||||
uint16_t fat32Version;
|
|
||||||
/**
|
|
||||||
Cluster number of the first cluster of the root directory for FAT32.
|
|
||||||
This usually 2 but not required to be 2.
|
|
||||||
*/
|
|
||||||
uint32_t fat32RootCluster;
|
|
||||||
/**
|
|
||||||
Sector number of FSINFO structure in the reserved area of the
|
|
||||||
FAT32 volume. Usually 1.
|
|
||||||
*/
|
|
||||||
uint16_t fat32FSInfo;
|
|
||||||
/**
|
|
||||||
If non-zero, indicates the sector number in the reserved area
|
|
||||||
of the volume of a copy of the boot record. Usually 6.
|
|
||||||
No value other than 6 is recommended.
|
|
||||||
*/
|
|
||||||
uint16_t fat32BackBootBlock;
|
|
||||||
/**
|
|
||||||
Reserved for future expansion. Code that formats FAT32 volumes
|
|
||||||
should always set all of the bytes of this field to 0.
|
|
||||||
*/
|
|
||||||
uint8_t fat32Reserved[12];
|
|
||||||
} __attribute__((packed));
|
|
||||||
/** Type name for biosParmBlock */
|
|
||||||
typedef struct biosParmBlock bpb_t;
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
\struct fat32BootSector
|
|
||||||
|
|
||||||
\brief Boot sector for a FAT16 or FAT32 volume.
|
|
||||||
|
|
||||||
*/
|
|
||||||
struct fat32BootSector {
|
|
||||||
/** X86 jmp to boot program */
|
|
||||||
uint8_t jmpToBootCode[3];
|
|
||||||
/** informational only - don't depend on it */
|
|
||||||
char oemName[8];
|
|
||||||
/** BIOS Parameter Block */
|
|
||||||
bpb_t bpb;
|
|
||||||
/** for int0x13 use value 0X80 for hard drive */
|
|
||||||
uint8_t driveNumber;
|
|
||||||
/** used by Windows NT - should be zero for FAT */
|
|
||||||
uint8_t reserved1;
|
|
||||||
/** 0X29 if next three fields are valid */
|
|
||||||
uint8_t bootSignature;
|
|
||||||
/** usually generated by combining date and time */
|
|
||||||
uint32_t volumeSerialNumber;
|
|
||||||
/** should match volume label in root dir */
|
|
||||||
char volumeLabel[11];
|
|
||||||
/** informational only - don't depend on it */
|
|
||||||
char fileSystemType[8];
|
|
||||||
/** X86 boot code */
|
|
||||||
uint8_t bootCode[420];
|
|
||||||
/** must be 0X55 */
|
|
||||||
uint8_t bootSectorSig0;
|
|
||||||
/** must be 0XAA */
|
|
||||||
uint8_t bootSectorSig1;
|
|
||||||
} __attribute__((packed));
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// End Of Chain values for FAT entries
|
|
||||||
/** FAT16 end of chain value used by Microsoft. */
|
|
||||||
uint16_t const FAT16EOC = 0XFFFF;
|
|
||||||
/** Minimum value for FAT16 EOC. Use to test for EOC. */
|
|
||||||
uint16_t const FAT16EOC_MIN = 0XFFF8;
|
|
||||||
/** FAT32 end of chain value used by Microsoft. */
|
|
||||||
uint32_t const FAT32EOC = 0X0FFFFFFF;
|
|
||||||
/** Minimum value for FAT32 EOC. Use to test for EOC. */
|
|
||||||
uint32_t const FAT32EOC_MIN = 0X0FFFFFF8;
|
|
||||||
/** Mask a for FAT32 entry. Entries are 28 bits. */
|
|
||||||
uint32_t const FAT32MASK = 0X0FFFFFFF;
|
|
||||||
|
|
||||||
/** Type name for fat32BootSector */
|
|
||||||
typedef struct fat32BootSector fbs_t;
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
\struct directoryEntry
|
|
||||||
\brief FAT short directory entry
|
|
||||||
|
|
||||||
Short means short 8.3 name, not the entry size.
|
|
||||||
|
|
||||||
Date Format. A FAT directory entry date stamp is a 16-bit field that is
|
|
||||||
basically a date relative to the MS-DOS epoch of 01/01/1980. Here is the
|
|
||||||
format (bit 0 is the LSB of the 16-bit word, bit 15 is the MSB of the
|
|
||||||
16-bit word):
|
|
||||||
|
|
||||||
Bits 9-15: Count of years from 1980, valid value range 0-127
|
|
||||||
inclusive (1980-2107).
|
|
||||||
|
|
||||||
Bits 5-8: Month of year, 1 = January, valid value range 1-12 inclusive.
|
|
||||||
|
|
||||||
Bits 0-4: Day of month, valid value range 1-31 inclusive.
|
|
||||||
|
|
||||||
Time Format. A FAT directory entry time stamp is a 16-bit field that has
|
|
||||||
a granularity of 2 seconds. Here is the format (bit 0 is the LSB of the
|
|
||||||
16-bit word, bit 15 is the MSB of the 16-bit word).
|
|
||||||
|
|
||||||
Bits 11-15: Hours, valid value range 0-23 inclusive.
|
|
||||||
|
|
||||||
Bits 5-10: Minutes, valid value range 0-59 inclusive.
|
|
||||||
|
|
||||||
Bits 0-4: 2-second count, valid value range 0-29 inclusive (0 - 58 seconds).
|
|
||||||
|
|
||||||
The valid time range is from Midnight 00:00:00 to 23:59:58.
|
|
||||||
*/
|
|
||||||
struct directoryEntry {
|
|
||||||
/**
|
|
||||||
Short 8.3 name.
|
|
||||||
The first eight bytes contain the file name with blank fill.
|
|
||||||
The last three bytes contain the file extension with blank fill.
|
|
||||||
*/
|
|
||||||
uint8_t name[11];
|
|
||||||
/** Entry attributes.
|
|
||||||
|
|
||||||
The upper two bits of the attribute byte are reserved and should
|
|
||||||
always be set to 0 when a file is created and never modified or
|
|
||||||
looked at after that. See defines that begin with DIR_ATT_.
|
|
||||||
*/
|
|
||||||
uint8_t attributes;
|
|
||||||
/**
|
|
||||||
Reserved for use by Windows NT. Set value to 0 when a file is
|
|
||||||
created and never modify or look at it after that.
|
|
||||||
*/
|
|
||||||
uint8_t reservedNT;
|
|
||||||
/**
|
|
||||||
The granularity of the seconds part of creationTime is 2 seconds
|
|
||||||
so this field is a count of tenths of a second and its valid
|
|
||||||
value range is 0-199 inclusive. (WHG note - seems to be hundredths)
|
|
||||||
*/
|
|
||||||
uint8_t creationTimeTenths;
|
|
||||||
/** Time file was created. */
|
|
||||||
uint16_t creationTime;
|
|
||||||
/** Date file was created. */
|
|
||||||
uint16_t creationDate;
|
|
||||||
/**
|
|
||||||
Last access date. Note that there is no last access time, only
|
|
||||||
a date. This is the date of last read or write. In the case of
|
|
||||||
a write, this should be set to the same date as lastWriteDate.
|
|
||||||
*/
|
|
||||||
uint16_t lastAccessDate;
|
|
||||||
/**
|
|
||||||
High word of this entry's first cluster number (always 0 for a
|
|
||||||
FAT12 or FAT16 volume).
|
|
||||||
*/
|
|
||||||
uint16_t firstClusterHigh;
|
|
||||||
/** Time of last write. File creation is considered a write. */
|
|
||||||
uint16_t lastWriteTime;
|
|
||||||
/** Date of last write. File creation is considered a write. */
|
|
||||||
uint16_t lastWriteDate;
|
|
||||||
/** Low word of this entry's first cluster number. */
|
|
||||||
uint16_t firstClusterLow;
|
|
||||||
/** 32-bit unsigned holding this file's size in bytes. */
|
|
||||||
uint32_t fileSize;
|
|
||||||
} __attribute__((packed));
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// Definitions for directory entries
|
|
||||||
//
|
|
||||||
/** Type name for directoryEntry */
|
|
||||||
typedef struct directoryEntry dir_t;
|
|
||||||
/** escape for name[0] = 0XE5 */
|
|
||||||
uint8_t const DIR_NAME_0XE5 = 0X05;
|
|
||||||
/** name[0] value for entry that is free after being "deleted" */
|
|
||||||
uint8_t const DIR_NAME_DELETED = 0XE5;
|
|
||||||
/** name[0] value for entry that is free and no allocated entries follow */
|
|
||||||
uint8_t const DIR_NAME_FREE = 0X00;
|
|
||||||
/** file is read-only */
|
|
||||||
uint8_t const DIR_ATT_READ_ONLY = 0X01;
|
|
||||||
/** File should hidden in directory listings */
|
|
||||||
uint8_t const DIR_ATT_HIDDEN = 0X02;
|
|
||||||
/** Entry is for a system file */
|
|
||||||
uint8_t const DIR_ATT_SYSTEM = 0X04;
|
|
||||||
/** Directory entry contains the volume label */
|
|
||||||
uint8_t const DIR_ATT_VOLUME_ID = 0X08;
|
|
||||||
/** Entry is for a directory */
|
|
||||||
uint8_t const DIR_ATT_DIRECTORY = 0X10;
|
|
||||||
/** Old DOS archive bit for backup support */
|
|
||||||
uint8_t const DIR_ATT_ARCHIVE = 0X20;
|
|
||||||
/** Test value for long name entry. Test is
|
|
||||||
(d->attributes & DIR_ATT_LONG_NAME_MASK) == DIR_ATT_LONG_NAME. */
|
|
||||||
uint8_t const DIR_ATT_LONG_NAME = 0X0F;
|
|
||||||
/** Test mask for long name entry */
|
|
||||||
uint8_t const DIR_ATT_LONG_NAME_MASK = 0X3F;
|
|
||||||
/** defined attribute bits */
|
|
||||||
uint8_t const DIR_ATT_DEFINED_BITS = 0X3F;
|
|
||||||
/** Directory entry is part of a long name */
|
|
||||||
static inline uint8_t DIR_IS_LONG_NAME(const dir_t* dir) {
|
|
||||||
return (dir->attributes & DIR_ATT_LONG_NAME_MASK) == DIR_ATT_LONG_NAME;
|
|
||||||
}
|
|
||||||
/** Mask for file/subdirectory tests */
|
|
||||||
uint8_t const DIR_ATT_FILE_TYPE_MASK = (DIR_ATT_VOLUME_ID | DIR_ATT_DIRECTORY);
|
|
||||||
/** Directory entry is for a file */
|
|
||||||
static inline uint8_t DIR_IS_FILE(const dir_t* dir) {
|
|
||||||
return (dir->attributes & DIR_ATT_FILE_TYPE_MASK) == 0;
|
|
||||||
}
|
|
||||||
/** Directory entry is for a subdirectory */
|
|
||||||
static inline uint8_t DIR_IS_SUBDIR(const dir_t* dir) {
|
|
||||||
return (dir->attributes & DIR_ATT_FILE_TYPE_MASK) == DIR_ATT_DIRECTORY;
|
|
||||||
}
|
|
||||||
/** Directory entry is for a file or subdirectory */
|
|
||||||
static inline uint8_t DIR_IS_FILE_OR_SUBDIR(const dir_t* dir) {
|
|
||||||
return (dir->attributes & DIR_ATT_VOLUME_ID) == 0;
|
|
||||||
}
|
|
||||||
#endif // FatStructs_h
|
|
||||||
@@ -1,777 +0,0 @@
|
|||||||
/* Arduino Sd2Card Library
|
|
||||||
Copyright (C) 2009 by William Greiman
|
|
||||||
|
|
||||||
This file is part of the Arduino Sd2Card Library
|
|
||||||
|
|
||||||
This Library is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This Library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with the Arduino Sd2Card Library. If not, see
|
|
||||||
<http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
#define USE_SPI_LIB
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include "Sd2Card.h"
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#ifndef SOFTWARE_SPI
|
|
||||||
#ifdef USE_SPI_LIB
|
|
||||||
|
|
||||||
#ifndef SDCARD_SPI
|
|
||||||
#define SDCARD_SPI SPI
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <SPI.h>
|
|
||||||
static SPISettings settings;
|
|
||||||
#endif
|
|
||||||
// functions for hardware SPI
|
|
||||||
/** Send a byte to the card */
|
|
||||||
static void spiSend(uint8_t b) {
|
|
||||||
#ifndef USE_SPI_LIB
|
|
||||||
SPDR = b;
|
|
||||||
while (!(SPSR & (1 << SPIF)))
|
|
||||||
;
|
|
||||||
#else
|
|
||||||
SDCARD_SPI.transfer(b);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
/** Receive a byte from the card */
|
|
||||||
static uint8_t spiRec(void) {
|
|
||||||
#ifndef USE_SPI_LIB
|
|
||||||
spiSend(0XFF);
|
|
||||||
return SPDR;
|
|
||||||
#else
|
|
||||||
return SDCARD_SPI.transfer(0xFF);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
#else // SOFTWARE_SPI
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** nop to tune soft SPI timing */
|
|
||||||
#define nop asm volatile ("nop\n\t")
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** Soft SPI receive */
|
|
||||||
uint8_t spiRec(void) {
|
|
||||||
uint8_t data = 0;
|
|
||||||
// no interrupts during byte receive - about 8 us
|
|
||||||
cli();
|
|
||||||
// output pin high - like sending 0XFF
|
|
||||||
fastDigitalWrite(SPI_MOSI_PIN, HIGH);
|
|
||||||
|
|
||||||
for (uint8_t i = 0; i < 8; i++) {
|
|
||||||
fastDigitalWrite(SPI_SCK_PIN, HIGH);
|
|
||||||
|
|
||||||
// adjust so SCK is nice
|
|
||||||
nop;
|
|
||||||
nop;
|
|
||||||
|
|
||||||
data <<= 1;
|
|
||||||
|
|
||||||
if (fastDigitalRead(SPI_MISO_PIN)) {
|
|
||||||
data |= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
fastDigitalWrite(SPI_SCK_PIN, LOW);
|
|
||||||
}
|
|
||||||
// enable interrupts
|
|
||||||
sei();
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** Soft SPI send */
|
|
||||||
void spiSend(uint8_t data) {
|
|
||||||
// no interrupts during byte send - about 8 us
|
|
||||||
cli();
|
|
||||||
for (uint8_t i = 0; i < 8; i++) {
|
|
||||||
fastDigitalWrite(SPI_SCK_PIN, LOW);
|
|
||||||
|
|
||||||
fastDigitalWrite(SPI_MOSI_PIN, data & 0X80);
|
|
||||||
|
|
||||||
data <<= 1;
|
|
||||||
|
|
||||||
fastDigitalWrite(SPI_SCK_PIN, HIGH);
|
|
||||||
}
|
|
||||||
// hold SCK high for a few ns
|
|
||||||
nop;
|
|
||||||
nop;
|
|
||||||
nop;
|
|
||||||
nop;
|
|
||||||
|
|
||||||
fastDigitalWrite(SPI_SCK_PIN, LOW);
|
|
||||||
// enable interrupts
|
|
||||||
sei();
|
|
||||||
}
|
|
||||||
#endif // SOFTWARE_SPI
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// send command and return error code. Return zero for OK
|
|
||||||
uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) {
|
|
||||||
// end read if in partialBlockRead mode
|
|
||||||
readEnd();
|
|
||||||
|
|
||||||
// select card
|
|
||||||
chipSelectLow();
|
|
||||||
|
|
||||||
// wait up to 300 ms if busy
|
|
||||||
waitNotBusy(300);
|
|
||||||
|
|
||||||
// send command
|
|
||||||
spiSend(cmd | 0x40);
|
|
||||||
|
|
||||||
// send argument
|
|
||||||
for (int8_t s = 24; s >= 0; s -= 8) {
|
|
||||||
spiSend(arg >> s);
|
|
||||||
}
|
|
||||||
|
|
||||||
// send CRC
|
|
||||||
uint8_t crc = 0XFF;
|
|
||||||
if (cmd == CMD0) {
|
|
||||||
crc = 0X95; // correct crc for CMD0 with arg 0
|
|
||||||
}
|
|
||||||
if (cmd == CMD8) {
|
|
||||||
crc = 0X87; // correct crc for CMD8 with arg 0X1AA
|
|
||||||
}
|
|
||||||
spiSend(crc);
|
|
||||||
|
|
||||||
// wait for response
|
|
||||||
for (uint8_t i = 0; ((status_ = spiRec()) & 0X80) && i != 0XFF; i++)
|
|
||||||
;
|
|
||||||
return status_;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
Determine the size of an SD flash memory card.
|
|
||||||
|
|
||||||
\return The number of 512 byte data blocks in the card
|
|
||||||
or zero if an error occurs.
|
|
||||||
*/
|
|
||||||
uint32_t Sd2Card::cardSize(void) {
|
|
||||||
csd_t csd;
|
|
||||||
if (!readCSD(&csd)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (csd.v1.csd_ver == 0) {
|
|
||||||
uint8_t read_bl_len = csd.v1.read_bl_len;
|
|
||||||
uint16_t c_size = (csd.v1.c_size_high << 10)
|
|
||||||
| (csd.v1.c_size_mid << 2) | csd.v1.c_size_low;
|
|
||||||
uint8_t c_size_mult = (csd.v1.c_size_mult_high << 1)
|
|
||||||
| csd.v1.c_size_mult_low;
|
|
||||||
return (uint32_t)(c_size + 1) << (c_size_mult + read_bl_len - 7);
|
|
||||||
} else if (csd.v2.csd_ver == 1) {
|
|
||||||
uint32_t c_size = ((uint32_t)csd.v2.c_size_high << 16)
|
|
||||||
| (csd.v2.c_size_mid << 8) | csd.v2.c_size_low;
|
|
||||||
return (c_size + 1) << 10;
|
|
||||||
} else {
|
|
||||||
error(SD_CARD_ERROR_BAD_CSD);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
static uint8_t chip_select_asserted = 0;
|
|
||||||
|
|
||||||
void Sd2Card::chipSelectHigh(void) {
|
|
||||||
digitalWrite(chipSelectPin_, HIGH);
|
|
||||||
#ifdef USE_SPI_LIB
|
|
||||||
if (chip_select_asserted) {
|
|
||||||
chip_select_asserted = 0;
|
|
||||||
SDCARD_SPI.endTransaction();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
void Sd2Card::chipSelectLow(void) {
|
|
||||||
#ifdef USE_SPI_LIB
|
|
||||||
if (!chip_select_asserted) {
|
|
||||||
chip_select_asserted = 1;
|
|
||||||
SDCARD_SPI.beginTransaction(settings);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
digitalWrite(chipSelectPin_, LOW);
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** Erase a range of blocks.
|
|
||||||
|
|
||||||
\param[in] firstBlock The address of the first block in the range.
|
|
||||||
\param[in] lastBlock The address of the last block in the range.
|
|
||||||
|
|
||||||
\note This function requests the SD card to do a flash erase for a
|
|
||||||
range of blocks. The data on the card after an erase operation is
|
|
||||||
either 0 or 1, depends on the card vendor. The card must support
|
|
||||||
single block erase.
|
|
||||||
|
|
||||||
\return The value one, true, is returned for success and
|
|
||||||
the value zero, false, is returned for failure.
|
|
||||||
*/
|
|
||||||
uint8_t Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) {
|
|
||||||
if (!eraseSingleBlockEnable()) {
|
|
||||||
error(SD_CARD_ERROR_ERASE_SINGLE_BLOCK);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (type_ != SD_CARD_TYPE_SDHC) {
|
|
||||||
firstBlock <<= 9;
|
|
||||||
lastBlock <<= 9;
|
|
||||||
}
|
|
||||||
if (cardCommand(CMD32, firstBlock)
|
|
||||||
|| cardCommand(CMD33, lastBlock)
|
|
||||||
|| cardCommand(CMD38, 0)) {
|
|
||||||
error(SD_CARD_ERROR_ERASE);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (!waitNotBusy(SD_ERASE_TIMEOUT)) {
|
|
||||||
error(SD_CARD_ERROR_ERASE_TIMEOUT);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
chipSelectHigh();
|
|
||||||
return true;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
chipSelectHigh();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** Determine if card supports single block erase.
|
|
||||||
|
|
||||||
\return The value one, true, is returned if single block erase is supported.
|
|
||||||
The value zero, false, is returned if single block erase is not supported.
|
|
||||||
*/
|
|
||||||
uint8_t Sd2Card::eraseSingleBlockEnable(void) {
|
|
||||||
csd_t csd;
|
|
||||||
return readCSD(&csd) ? csd.v1.erase_blk_en : 0;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
Initialize an SD flash memory card.
|
|
||||||
|
|
||||||
\param[in] sckRateID SPI clock rate selector. See setSckRate().
|
|
||||||
\param[in] chipSelectPin SD chip select pin number.
|
|
||||||
|
|
||||||
\return The value one, true, is returned for success and
|
|
||||||
the value zero, false, is returned for failure. The reason for failure
|
|
||||||
can be determined by calling errorCode() and errorData().
|
|
||||||
*/
|
|
||||||
uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
|
|
||||||
errorCode_ = inBlock_ = partialBlockRead_ = type_ = 0;
|
|
||||||
chipSelectPin_ = chipSelectPin;
|
|
||||||
// 16-bit init start time allows over a minute
|
|
||||||
unsigned int t0 = millis();
|
|
||||||
uint32_t arg;
|
|
||||||
|
|
||||||
// set pin modes
|
|
||||||
pinMode(chipSelectPin_, OUTPUT);
|
|
||||||
digitalWrite(chipSelectPin_, HIGH);
|
|
||||||
#ifndef USE_SPI_LIB
|
|
||||||
pinMode(SPI_MISO_PIN, INPUT);
|
|
||||||
pinMode(SPI_MOSI_PIN, OUTPUT);
|
|
||||||
pinMode(SPI_SCK_PIN, OUTPUT);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef SOFTWARE_SPI
|
|
||||||
#ifndef USE_SPI_LIB
|
|
||||||
// SS must be in output mode even it is not chip select
|
|
||||||
pinMode(SS_PIN, OUTPUT);
|
|
||||||
digitalWrite(SS_PIN, HIGH); // disable any SPI device using hardware SS pin
|
|
||||||
// Enable SPI, Master, clock rate f_osc/128
|
|
||||||
SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1) | (1 << SPR0);
|
|
||||||
// clear double speed
|
|
||||||
SPSR &= ~(1 << SPI2X);
|
|
||||||
#else // USE_SPI_LIB
|
|
||||||
SDCARD_SPI.begin();
|
|
||||||
settings = SPISettings(250000, MSBFIRST, SPI_MODE0);
|
|
||||||
#endif // USE_SPI_LIB
|
|
||||||
#endif // SOFTWARE_SPI
|
|
||||||
|
|
||||||
// must supply min of 74 clock cycles with CS high.
|
|
||||||
#ifdef USE_SPI_LIB
|
|
||||||
SDCARD_SPI.beginTransaction(settings);
|
|
||||||
#endif
|
|
||||||
for (uint8_t i = 0; i < 10; i++) {
|
|
||||||
spiSend(0XFF);
|
|
||||||
}
|
|
||||||
#ifdef USE_SPI_LIB
|
|
||||||
SDCARD_SPI.endTransaction();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
chipSelectLow();
|
|
||||||
|
|
||||||
// command to go idle in SPI mode
|
|
||||||
while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) {
|
|
||||||
unsigned int d = millis() - t0;
|
|
||||||
if (d > SD_INIT_TIMEOUT) {
|
|
||||||
error(SD_CARD_ERROR_CMD0);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// check SD version
|
|
||||||
if ((cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) {
|
|
||||||
type(SD_CARD_TYPE_SD1);
|
|
||||||
} else {
|
|
||||||
// only need last byte of r7 response
|
|
||||||
for (uint8_t i = 0; i < 4; i++) {
|
|
||||||
status_ = spiRec();
|
|
||||||
}
|
|
||||||
if (status_ != 0XAA) {
|
|
||||||
error(SD_CARD_ERROR_CMD8);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
type(SD_CARD_TYPE_SD2);
|
|
||||||
}
|
|
||||||
// initialize card and send host supports SDHC if SD2
|
|
||||||
arg = type() == SD_CARD_TYPE_SD2 ? 0X40000000 : 0;
|
|
||||||
|
|
||||||
while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) {
|
|
||||||
// check for timeout
|
|
||||||
unsigned int d = millis() - t0;
|
|
||||||
if (d > SD_INIT_TIMEOUT) {
|
|
||||||
error(SD_CARD_ERROR_ACMD41);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// if SD2 read OCR register to check for SDHC card
|
|
||||||
if (type() == SD_CARD_TYPE_SD2) {
|
|
||||||
if (cardCommand(CMD58, 0)) {
|
|
||||||
error(SD_CARD_ERROR_CMD58);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if ((spiRec() & 0XC0) == 0XC0) {
|
|
||||||
type(SD_CARD_TYPE_SDHC);
|
|
||||||
}
|
|
||||||
// discard rest of ocr - contains allowed voltage range
|
|
||||||
for (uint8_t i = 0; i < 3; i++) {
|
|
||||||
spiRec();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
chipSelectHigh();
|
|
||||||
|
|
||||||
#ifndef SOFTWARE_SPI
|
|
||||||
return setSckRate(sckRateID);
|
|
||||||
#else // SOFTWARE_SPI
|
|
||||||
return true;
|
|
||||||
#endif // SOFTWARE_SPI
|
|
||||||
|
|
||||||
fail:
|
|
||||||
chipSelectHigh();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
Enable or disable partial block reads.
|
|
||||||
|
|
||||||
Enabling partial block reads improves performance by allowing a block
|
|
||||||
to be read over the SPI bus as several sub-blocks. Errors may occur
|
|
||||||
if the time between reads is too long since the SD card may timeout.
|
|
||||||
The SPI SS line will be held low until the entire block is read or
|
|
||||||
readEnd() is called.
|
|
||||||
|
|
||||||
Use this for applications like the Adafruit Wave Shield.
|
|
||||||
|
|
||||||
\param[in] value The value TRUE (non-zero) or FALSE (zero).)
|
|
||||||
*/
|
|
||||||
void Sd2Card::partialBlockRead(uint8_t value) {
|
|
||||||
readEnd();
|
|
||||||
partialBlockRead_ = value;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
Read a 512 byte block from an SD card device.
|
|
||||||
|
|
||||||
\param[in] block Logical block to be read.
|
|
||||||
\param[out] dst Pointer to the location that will receive the data.
|
|
||||||
|
|
||||||
\return The value one, true, is returned for success and
|
|
||||||
the value zero, false, is returned for failure.
|
|
||||||
*/
|
|
||||||
uint8_t Sd2Card::readBlock(uint32_t block, uint8_t* dst) {
|
|
||||||
return readData(block, 0, 512, dst);
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
Read part of a 512 byte block from an SD card.
|
|
||||||
|
|
||||||
\param[in] block Logical block to be read.
|
|
||||||
\param[in] offset Number of bytes to skip at start of block
|
|
||||||
\param[out] dst Pointer to the location that will receive the data.
|
|
||||||
\param[in] count Number of bytes to read
|
|
||||||
\return The value one, true, is returned for success and
|
|
||||||
the value zero, false, is returned for failure.
|
|
||||||
*/
|
|
||||||
uint8_t Sd2Card::readData(uint32_t block,
|
|
||||||
uint16_t offset, uint16_t count, uint8_t* dst) {
|
|
||||||
if (count == 0) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if ((count + offset) > 512) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (!inBlock_ || block != block_ || offset < offset_) {
|
|
||||||
block_ = block;
|
|
||||||
// use address if not SDHC card
|
|
||||||
if (type() != SD_CARD_TYPE_SDHC) {
|
|
||||||
block <<= 9;
|
|
||||||
}
|
|
||||||
if (cardCommand(CMD17, block)) {
|
|
||||||
error(SD_CARD_ERROR_CMD17);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (!waitStartBlock()) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
offset_ = 0;
|
|
||||||
inBlock_ = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef OPTIMIZE_HARDWARE_SPI
|
|
||||||
// start first spi transfer
|
|
||||||
SPDR = 0XFF;
|
|
||||||
|
|
||||||
// skip data before offset
|
|
||||||
for (; offset_ < offset; offset_++) {
|
|
||||||
while (!(SPSR & (1 << SPIF)))
|
|
||||||
;
|
|
||||||
SPDR = 0XFF;
|
|
||||||
}
|
|
||||||
// transfer data
|
|
||||||
n = count - 1;
|
|
||||||
for (uint16_t i = 0; i < n; i++) {
|
|
||||||
while (!(SPSR & (1 << SPIF)))
|
|
||||||
;
|
|
||||||
dst[i] = SPDR;
|
|
||||||
SPDR = 0XFF;
|
|
||||||
}
|
|
||||||
// wait for last byte
|
|
||||||
while (!(SPSR & (1 << SPIF)))
|
|
||||||
;
|
|
||||||
dst[n] = SPDR;
|
|
||||||
|
|
||||||
#else // OPTIMIZE_HARDWARE_SPI
|
|
||||||
|
|
||||||
// skip data before offset
|
|
||||||
for (; offset_ < offset; offset_++) {
|
|
||||||
spiRec();
|
|
||||||
}
|
|
||||||
// transfer data
|
|
||||||
for (uint16_t i = 0; i < count; i++) {
|
|
||||||
dst[i] = spiRec();
|
|
||||||
}
|
|
||||||
#endif // OPTIMIZE_HARDWARE_SPI
|
|
||||||
|
|
||||||
offset_ += count;
|
|
||||||
if (!partialBlockRead_ || offset_ >= 512) {
|
|
||||||
// read rest of data, checksum and set chip select high
|
|
||||||
readEnd();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
chipSelectHigh();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** Skip remaining data in a block when in partial block read mode. */
|
|
||||||
void Sd2Card::readEnd(void) {
|
|
||||||
if (inBlock_) {
|
|
||||||
// skip data and crc
|
|
||||||
#ifdef OPTIMIZE_HARDWARE_SPI
|
|
||||||
// optimize skip for hardware
|
|
||||||
SPDR = 0XFF;
|
|
||||||
while (offset_++ < 513) {
|
|
||||||
while (!(SPSR & (1 << SPIF)))
|
|
||||||
;
|
|
||||||
SPDR = 0XFF;
|
|
||||||
}
|
|
||||||
// wait for last crc byte
|
|
||||||
while (!(SPSR & (1 << SPIF)))
|
|
||||||
;
|
|
||||||
#else // OPTIMIZE_HARDWARE_SPI
|
|
||||||
while (offset_++ < 514) {
|
|
||||||
spiRec();
|
|
||||||
}
|
|
||||||
#endif // OPTIMIZE_HARDWARE_SPI
|
|
||||||
chipSelectHigh();
|
|
||||||
inBlock_ = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** read CID or CSR register */
|
|
||||||
uint8_t Sd2Card::readRegister(uint8_t cmd, void* buf) {
|
|
||||||
uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
|
|
||||||
if (cardCommand(cmd, 0)) {
|
|
||||||
error(SD_CARD_ERROR_READ_REG);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (!waitStartBlock()) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
// transfer data
|
|
||||||
for (uint16_t i = 0; i < 16; i++) {
|
|
||||||
dst[i] = spiRec();
|
|
||||||
}
|
|
||||||
spiRec(); // get first crc byte
|
|
||||||
spiRec(); // get second crc byte
|
|
||||||
chipSelectHigh();
|
|
||||||
return true;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
chipSelectHigh();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
Set the SPI clock rate.
|
|
||||||
|
|
||||||
\param[in] sckRateID A value in the range [0, 6].
|
|
||||||
|
|
||||||
The SPI clock will be set to F_CPU/pow(2, 1 + sckRateID). The maximum
|
|
||||||
SPI rate is F_CPU/2 for \a sckRateID = 0 and the minimum rate is F_CPU/128
|
|
||||||
for \a scsRateID = 6.
|
|
||||||
|
|
||||||
\return The value one, true, is returned for success and the value zero,
|
|
||||||
false, is returned for an invalid value of \a sckRateID.
|
|
||||||
*/
|
|
||||||
uint8_t Sd2Card::setSckRate(uint8_t sckRateID) {
|
|
||||||
if (sckRateID > 6) {
|
|
||||||
error(SD_CARD_ERROR_SCK_RATE);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#ifndef USE_SPI_LIB
|
|
||||||
// see avr processor datasheet for SPI register bit definitions
|
|
||||||
if ((sckRateID & 1) || sckRateID == 6) {
|
|
||||||
SPSR &= ~(1 << SPI2X);
|
|
||||||
} else {
|
|
||||||
SPSR |= (1 << SPI2X);
|
|
||||||
}
|
|
||||||
SPCR &= ~((1 << SPR1) | (1 << SPR0));
|
|
||||||
SPCR |= (sckRateID & 4 ? (1 << SPR1) : 0)
|
|
||||||
| (sckRateID & 2 ? (1 << SPR0) : 0);
|
|
||||||
#else // USE_SPI_LIB
|
|
||||||
switch (sckRateID) {
|
|
||||||
case 0: settings = SPISettings(25000000, MSBFIRST, SPI_MODE0); break;
|
|
||||||
case 1: settings = SPISettings(4000000, MSBFIRST, SPI_MODE0); break;
|
|
||||||
case 2: settings = SPISettings(2000000, MSBFIRST, SPI_MODE0); break;
|
|
||||||
case 3: settings = SPISettings(1000000, MSBFIRST, SPI_MODE0); break;
|
|
||||||
case 4: settings = SPISettings(500000, MSBFIRST, SPI_MODE0); break;
|
|
||||||
case 5: settings = SPISettings(250000, MSBFIRST, SPI_MODE0); break;
|
|
||||||
default: settings = SPISettings(125000, MSBFIRST, SPI_MODE0);
|
|
||||||
}
|
|
||||||
#endif // USE_SPI_LIB
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
#ifdef USE_SPI_LIB
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// set the SPI clock frequency
|
|
||||||
uint8_t Sd2Card::setSpiClock(uint32_t clock) {
|
|
||||||
settings = SPISettings(clock, MSBFIRST, SPI_MODE0);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// wait for card to go not busy
|
|
||||||
uint8_t Sd2Card::waitNotBusy(unsigned int timeoutMillis) {
|
|
||||||
unsigned int t0 = millis();
|
|
||||||
unsigned int d;
|
|
||||||
do {
|
|
||||||
if (spiRec() == 0XFF) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
d = millis() - t0;
|
|
||||||
} while (d < timeoutMillis);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** Wait for start block token */
|
|
||||||
uint8_t Sd2Card::waitStartBlock(void) {
|
|
||||||
unsigned int t0 = millis();
|
|
||||||
while ((status_ = spiRec()) == 0XFF) {
|
|
||||||
unsigned int d = millis() - t0;
|
|
||||||
if (d > SD_READ_TIMEOUT) {
|
|
||||||
error(SD_CARD_ERROR_READ_TIMEOUT);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (status_ != DATA_START_BLOCK) {
|
|
||||||
error(SD_CARD_ERROR_READ);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
chipSelectHigh();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
Writes a 512 byte block to an SD card.
|
|
||||||
|
|
||||||
\param[in] blockNumber Logical block to be written.
|
|
||||||
\param[in] src Pointer to the location of the data to be written.
|
|
||||||
\param[in] blocking If the write should be blocking.
|
|
||||||
\return The value one, true, is returned for success and
|
|
||||||
the value zero, false, is returned for failure.
|
|
||||||
*/
|
|
||||||
uint8_t Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src, uint8_t blocking) {
|
|
||||||
#if SD_PROTECT_BLOCK_ZERO
|
|
||||||
// don't allow write to first block
|
|
||||||
if (blockNumber == 0) {
|
|
||||||
error(SD_CARD_ERROR_WRITE_BLOCK_ZERO);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
#endif // SD_PROTECT_BLOCK_ZERO
|
|
||||||
|
|
||||||
// use address if not SDHC card
|
|
||||||
if (type() != SD_CARD_TYPE_SDHC) {
|
|
||||||
blockNumber <<= 9;
|
|
||||||
}
|
|
||||||
if (cardCommand(CMD24, blockNumber)) {
|
|
||||||
error(SD_CARD_ERROR_CMD24);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (!writeData(DATA_START_BLOCK, src)) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (blocking) {
|
|
||||||
// wait for flash programming to complete
|
|
||||||
if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
|
|
||||||
error(SD_CARD_ERROR_WRITE_TIMEOUT);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
// response is r2 so get and check two bytes for nonzero
|
|
||||||
if (cardCommand(CMD13, 0) || spiRec()) {
|
|
||||||
error(SD_CARD_ERROR_WRITE_PROGRAMMING);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
chipSelectHigh();
|
|
||||||
return true;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
chipSelectHigh();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** Write one data block in a multiple block write sequence */
|
|
||||||
uint8_t Sd2Card::writeData(const uint8_t* src) {
|
|
||||||
// wait for previous write to finish
|
|
||||||
if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
|
|
||||||
error(SD_CARD_ERROR_WRITE_MULTIPLE);
|
|
||||||
chipSelectHigh();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return writeData(WRITE_MULTIPLE_TOKEN, src);
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// send one block of data for write block or write multiple blocks
|
|
||||||
uint8_t Sd2Card::writeData(uint8_t token, const uint8_t* src) {
|
|
||||||
#ifdef OPTIMIZE_HARDWARE_SPI
|
|
||||||
|
|
||||||
// send data - optimized loop
|
|
||||||
SPDR = token;
|
|
||||||
|
|
||||||
// send two byte per iteration
|
|
||||||
for (uint16_t i = 0; i < 512; i += 2) {
|
|
||||||
while (!(SPSR & (1 << SPIF)))
|
|
||||||
;
|
|
||||||
SPDR = src[i];
|
|
||||||
while (!(SPSR & (1 << SPIF)))
|
|
||||||
;
|
|
||||||
SPDR = src[i + 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
// wait for last data byte
|
|
||||||
while (!(SPSR & (1 << SPIF)))
|
|
||||||
;
|
|
||||||
|
|
||||||
#else // OPTIMIZE_HARDWARE_SPI
|
|
||||||
spiSend(token);
|
|
||||||
for (uint16_t i = 0; i < 512; i++) {
|
|
||||||
spiSend(src[i]);
|
|
||||||
}
|
|
||||||
#endif // OPTIMIZE_HARDWARE_SPI
|
|
||||||
spiSend(0xff); // dummy crc
|
|
||||||
spiSend(0xff); // dummy crc
|
|
||||||
|
|
||||||
status_ = spiRec();
|
|
||||||
if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) {
|
|
||||||
error(SD_CARD_ERROR_WRITE);
|
|
||||||
chipSelectHigh();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** Start a write multiple blocks sequence.
|
|
||||||
|
|
||||||
\param[in] blockNumber Address of first block in sequence.
|
|
||||||
\param[in] eraseCount The number of blocks to be pre-erased.
|
|
||||||
|
|
||||||
\note This function is used with writeData() and writeStop()
|
|
||||||
for optimized multiple block writes.
|
|
||||||
|
|
||||||
\return The value one, true, is returned for success and
|
|
||||||
the value zero, false, is returned for failure.
|
|
||||||
*/
|
|
||||||
uint8_t Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) {
|
|
||||||
#if SD_PROTECT_BLOCK_ZERO
|
|
||||||
// don't allow write to first block
|
|
||||||
if (blockNumber == 0) {
|
|
||||||
error(SD_CARD_ERROR_WRITE_BLOCK_ZERO);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
#endif // SD_PROTECT_BLOCK_ZERO
|
|
||||||
// send pre-erase count
|
|
||||||
if (cardAcmd(ACMD23, eraseCount)) {
|
|
||||||
error(SD_CARD_ERROR_ACMD23);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
// use address if not SDHC card
|
|
||||||
if (type() != SD_CARD_TYPE_SDHC) {
|
|
||||||
blockNumber <<= 9;
|
|
||||||
}
|
|
||||||
if (cardCommand(CMD25, blockNumber)) {
|
|
||||||
error(SD_CARD_ERROR_CMD25);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
chipSelectHigh();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** End a write multiple blocks sequence.
|
|
||||||
|
|
||||||
\return The value one, true, is returned for success and
|
|
||||||
the value zero, false, is returned for failure.
|
|
||||||
*/
|
|
||||||
uint8_t Sd2Card::writeStop(void) {
|
|
||||||
if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
spiSend(STOP_TRAN_TOKEN);
|
|
||||||
if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
chipSelectHigh();
|
|
||||||
return true;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
error(SD_CARD_ERROR_STOP_TRAN);
|
|
||||||
chipSelectHigh();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** Check if the SD card is busy
|
|
||||||
|
|
||||||
\return The value one, true, is returned when is busy and
|
|
||||||
the value zero, false, is returned for when is NOT busy.
|
|
||||||
*/
|
|
||||||
uint8_t Sd2Card::isBusy(void) {
|
|
||||||
chipSelectLow();
|
|
||||||
byte b = spiRec();
|
|
||||||
chipSelectHigh();
|
|
||||||
|
|
||||||
return (b != 0XFF);
|
|
||||||
}
|
|
||||||
@@ -1,273 +0,0 @@
|
|||||||
/* Arduino Sd2Card Library
|
|
||||||
Copyright (C) 2009 by William Greiman
|
|
||||||
|
|
||||||
This file is part of the Arduino Sd2Card Library
|
|
||||||
|
|
||||||
This Library is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This Library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with the Arduino Sd2Card Library. If not, see
|
|
||||||
<http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
#ifndef Sd2Card_h
|
|
||||||
#define Sd2Card_h
|
|
||||||
/**
|
|
||||||
\file
|
|
||||||
Sd2Card class
|
|
||||||
*/
|
|
||||||
#include "Sd2PinMap.h"
|
|
||||||
#include "SdInfo.h"
|
|
||||||
/** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */
|
|
||||||
uint8_t const SPI_FULL_SPEED = 0;
|
|
||||||
/** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */
|
|
||||||
uint8_t const SPI_HALF_SPEED = 1;
|
|
||||||
/** Set SCK rate to F_CPU/8. Sd2Card::setSckRate(). */
|
|
||||||
uint8_t const SPI_QUARTER_SPEED = 2;
|
|
||||||
/**
|
|
||||||
USE_SPI_LIB: if set, use the SPI library bundled with Arduino IDE, otherwise
|
|
||||||
run with a standalone driver for AVR.
|
|
||||||
*/
|
|
||||||
#define USE_SPI_LIB
|
|
||||||
/**
|
|
||||||
Define MEGA_SOFT_SPI non-zero to use software SPI on Mega Arduinos.
|
|
||||||
Pins used are SS 10, MOSI 11, MISO 12, and SCK 13.
|
|
||||||
|
|
||||||
MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used
|
|
||||||
on Mega Arduinos. Software SPI works well with GPS Shield V1.1
|
|
||||||
but many SD cards will fail with GPS Shield V1.0.
|
|
||||||
*/
|
|
||||||
#define MEGA_SOFT_SPI 0
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#if MEGA_SOFT_SPI && (defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__))
|
|
||||||
#define SOFTWARE_SPI
|
|
||||||
#endif // MEGA_SOFT_SPI
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// SPI pin definitions
|
|
||||||
//
|
|
||||||
#ifndef SOFTWARE_SPI
|
|
||||||
// hardware pin defs
|
|
||||||
|
|
||||||
// include pins_arduino.h or variant.h depending on architecture, via Arduino.h
|
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
SD Chip Select pin
|
|
||||||
|
|
||||||
Warning if this pin is redefined the hardware SS will pin will be enabled
|
|
||||||
as an output by init(). An avr processor will not function as an SPI
|
|
||||||
master unless SS is set to output mode.
|
|
||||||
*/
|
|
||||||
#ifndef SDCARD_SS_PIN
|
|
||||||
/** The default chip select pin for the SD card is SS. */
|
|
||||||
uint8_t const SD_CHIP_SELECT_PIN = SS;
|
|
||||||
#else
|
|
||||||
uint8_t const SD_CHIP_SELECT_PIN = SDCARD_SS_PIN;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// The following three pins must not be redefined for hardware SPI,
|
|
||||||
// so ensure that they are taken from pins_arduino.h or variant.h, depending on architecture.
|
|
||||||
#ifndef SDCARD_MOSI_PIN
|
|
||||||
/** SPI Master Out Slave In pin */
|
|
||||||
uint8_t const SPI_MOSI_PIN = MOSI;
|
|
||||||
/** SPI Master In Slave Out pin */
|
|
||||||
uint8_t const SPI_MISO_PIN = MISO;
|
|
||||||
/** SPI Clock pin */
|
|
||||||
uint8_t const SPI_SCK_PIN = SCK;
|
|
||||||
#else
|
|
||||||
uint8_t const SPI_MOSI_PIN = SDCARD_MOSI_PIN;
|
|
||||||
uint8_t const SPI_MISO_PIN = SDCARD_MISO_PIN;
|
|
||||||
uint8_t const SPI_SCK_PIN = SDCARD_SCK_PIN;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/** optimize loops for hardware SPI */
|
|
||||||
#ifndef USE_SPI_LIB
|
|
||||||
#define OPTIMIZE_HARDWARE_SPI
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#else // SOFTWARE_SPI
|
|
||||||
// define software SPI pins so Mega can use unmodified GPS Shield
|
|
||||||
/** SPI chip select pin */
|
|
||||||
uint8_t const SD_CHIP_SELECT_PIN = 10;
|
|
||||||
/** SPI Master Out Slave In pin */
|
|
||||||
uint8_t const SPI_MOSI_PIN = 11;
|
|
||||||
/** SPI Master In Slave Out pin */
|
|
||||||
uint8_t const SPI_MISO_PIN = 12;
|
|
||||||
/** SPI Clock pin */
|
|
||||||
uint8_t const SPI_SCK_PIN = 13;
|
|
||||||
#endif // SOFTWARE_SPI
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** Protect block zero from write if nonzero */
|
|
||||||
#define SD_PROTECT_BLOCK_ZERO 1
|
|
||||||
/** init timeout ms */
|
|
||||||
unsigned int const SD_INIT_TIMEOUT = 2000;
|
|
||||||
/** erase timeout ms */
|
|
||||||
unsigned int const SD_ERASE_TIMEOUT = 10000;
|
|
||||||
/** read timeout ms */
|
|
||||||
unsigned int const SD_READ_TIMEOUT = 300;
|
|
||||||
/** write time out ms */
|
|
||||||
unsigned int const SD_WRITE_TIMEOUT = 600;
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// SD card errors
|
|
||||||
/** timeout error for command CMD0 */
|
|
||||||
uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
|
|
||||||
/** CMD8 was not accepted - not a valid SD card*/
|
|
||||||
uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
|
|
||||||
/** card returned an error response for CMD17 (read block) */
|
|
||||||
uint8_t const SD_CARD_ERROR_CMD17 = 0X3;
|
|
||||||
/** card returned an error response for CMD24 (write block) */
|
|
||||||
uint8_t const SD_CARD_ERROR_CMD24 = 0X4;
|
|
||||||
/** WRITE_MULTIPLE_BLOCKS command failed */
|
|
||||||
uint8_t const SD_CARD_ERROR_CMD25 = 0X05;
|
|
||||||
/** card returned an error response for CMD58 (read OCR) */
|
|
||||||
uint8_t const SD_CARD_ERROR_CMD58 = 0X06;
|
|
||||||
/** SET_WR_BLK_ERASE_COUNT failed */
|
|
||||||
uint8_t const SD_CARD_ERROR_ACMD23 = 0X07;
|
|
||||||
/** card's ACMD41 initialization process timeout */
|
|
||||||
uint8_t const SD_CARD_ERROR_ACMD41 = 0X08;
|
|
||||||
/** card returned a bad CSR version field */
|
|
||||||
uint8_t const SD_CARD_ERROR_BAD_CSD = 0X09;
|
|
||||||
/** erase block group command failed */
|
|
||||||
uint8_t const SD_CARD_ERROR_ERASE = 0X0A;
|
|
||||||
/** card not capable of single block erase */
|
|
||||||
uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0X0B;
|
|
||||||
/** Erase sequence timed out */
|
|
||||||
uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0X0C;
|
|
||||||
/** card returned an error token instead of read data */
|
|
||||||
uint8_t const SD_CARD_ERROR_READ = 0X0D;
|
|
||||||
/** read CID or CSD failed */
|
|
||||||
uint8_t const SD_CARD_ERROR_READ_REG = 0X0E;
|
|
||||||
/** timeout while waiting for start of read data */
|
|
||||||
uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X0F;
|
|
||||||
/** card did not accept STOP_TRAN_TOKEN */
|
|
||||||
uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X10;
|
|
||||||
/** card returned an error token as a response to a write operation */
|
|
||||||
uint8_t const SD_CARD_ERROR_WRITE = 0X11;
|
|
||||||
/** attempt to write protected block zero */
|
|
||||||
uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X12;
|
|
||||||
/** card did not go ready for a multiple block write */
|
|
||||||
uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X13;
|
|
||||||
/** card returned an error to a CMD13 status check after a write */
|
|
||||||
uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X14;
|
|
||||||
/** timeout occurred during write programming */
|
|
||||||
uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X15;
|
|
||||||
/** incorrect rate selected */
|
|
||||||
uint8_t const SD_CARD_ERROR_SCK_RATE = 0X16;
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// card types
|
|
||||||
/** Standard capacity V1 SD card */
|
|
||||||
uint8_t const SD_CARD_TYPE_SD1 = 1;
|
|
||||||
/** Standard capacity V2 SD card */
|
|
||||||
uint8_t const SD_CARD_TYPE_SD2 = 2;
|
|
||||||
/** High Capacity SD card */
|
|
||||||
uint8_t const SD_CARD_TYPE_SDHC = 3;
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
\class Sd2Card
|
|
||||||
\brief Raw access to SD and SDHC flash memory cards.
|
|
||||||
*/
|
|
||||||
class Sd2Card {
|
|
||||||
public:
|
|
||||||
/** Construct an instance of Sd2Card. */
|
|
||||||
Sd2Card(void) : errorCode_(0), inBlock_(0), partialBlockRead_(0), type_(0) {}
|
|
||||||
uint32_t cardSize(void);
|
|
||||||
uint8_t erase(uint32_t firstBlock, uint32_t lastBlock);
|
|
||||||
uint8_t eraseSingleBlockEnable(void);
|
|
||||||
/**
|
|
||||||
\return error code for last error. See Sd2Card.h for a list of error codes.
|
|
||||||
*/
|
|
||||||
uint8_t errorCode(void) const {
|
|
||||||
return errorCode_;
|
|
||||||
}
|
|
||||||
/** \return error data for last error. */
|
|
||||||
uint8_t errorData(void) const {
|
|
||||||
return status_;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
Initialize an SD flash memory card with default clock rate and chip
|
|
||||||
select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
|
|
||||||
*/
|
|
||||||
uint8_t init(void) {
|
|
||||||
return init(SPI_FULL_SPEED, SD_CHIP_SELECT_PIN);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
Initialize an SD flash memory card with the selected SPI clock rate
|
|
||||||
and the default SD chip select pin.
|
|
||||||
See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
|
|
||||||
*/
|
|
||||||
uint8_t init(uint8_t sckRateID) {
|
|
||||||
return init(sckRateID, SD_CHIP_SELECT_PIN);
|
|
||||||
}
|
|
||||||
uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin);
|
|
||||||
void partialBlockRead(uint8_t value);
|
|
||||||
/** Returns the current value, true or false, for partial block read. */
|
|
||||||
uint8_t partialBlockRead(void) const {
|
|
||||||
return partialBlockRead_;
|
|
||||||
}
|
|
||||||
uint8_t readBlock(uint32_t block, uint8_t* dst);
|
|
||||||
uint8_t readData(uint32_t block,
|
|
||||||
uint16_t offset, uint16_t count, uint8_t* dst);
|
|
||||||
/**
|
|
||||||
Read a cards CID register. The CID contains card identification
|
|
||||||
information such as Manufacturer ID, Product name, Product serial
|
|
||||||
number and Manufacturing date. */
|
|
||||||
uint8_t readCID(cid_t* cid) {
|
|
||||||
return readRegister(CMD10, cid);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
Read a cards CSD register. The CSD contains Card-Specific Data that
|
|
||||||
provides information regarding access to the card's contents. */
|
|
||||||
uint8_t readCSD(csd_t* csd) {
|
|
||||||
return readRegister(CMD9, csd);
|
|
||||||
}
|
|
||||||
void readEnd(void);
|
|
||||||
uint8_t setSckRate(uint8_t sckRateID);
|
|
||||||
#ifdef USE_SPI_LIB
|
|
||||||
uint8_t setSpiClock(uint32_t clock);
|
|
||||||
#endif
|
|
||||||
/** Return the card type: SD V1, SD V2 or SDHC */
|
|
||||||
uint8_t type(void) const {
|
|
||||||
return type_;
|
|
||||||
}
|
|
||||||
uint8_t writeBlock(uint32_t blockNumber, const uint8_t* src, uint8_t blocking = 1);
|
|
||||||
uint8_t writeData(const uint8_t* src);
|
|
||||||
uint8_t writeStart(uint32_t blockNumber, uint32_t eraseCount);
|
|
||||||
uint8_t writeStop(void);
|
|
||||||
uint8_t isBusy(void);
|
|
||||||
private:
|
|
||||||
uint32_t block_;
|
|
||||||
uint8_t chipSelectPin_;
|
|
||||||
uint8_t errorCode_;
|
|
||||||
uint8_t inBlock_;
|
|
||||||
uint16_t offset_;
|
|
||||||
uint8_t partialBlockRead_;
|
|
||||||
uint8_t status_;
|
|
||||||
uint8_t type_;
|
|
||||||
// private functions
|
|
||||||
uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
|
|
||||||
cardCommand(CMD55, 0);
|
|
||||||
return cardCommand(cmd, arg);
|
|
||||||
}
|
|
||||||
uint8_t cardCommand(uint8_t cmd, uint32_t arg);
|
|
||||||
void error(uint8_t code) {
|
|
||||||
errorCode_ = code;
|
|
||||||
}
|
|
||||||
uint8_t readRegister(uint8_t cmd, void* buf);
|
|
||||||
uint8_t sendWriteCommand(uint32_t blockNumber, uint32_t eraseCount);
|
|
||||||
void chipSelectHigh(void);
|
|
||||||
void chipSelectLow(void);
|
|
||||||
void type(uint8_t value) {
|
|
||||||
type_ = value;
|
|
||||||
}
|
|
||||||
uint8_t waitNotBusy(unsigned int timeoutMillis);
|
|
||||||
uint8_t writeData(uint8_t token, const uint8_t* src);
|
|
||||||
uint8_t waitStartBlock(void);
|
|
||||||
};
|
|
||||||
#endif // Sd2Card_h
|
|
||||||
@@ -1,525 +0,0 @@
|
|||||||
/* Arduino SdFat Library
|
|
||||||
Copyright (C) 2010 by William Greiman
|
|
||||||
|
|
||||||
This file is part of the Arduino SdFat Library
|
|
||||||
|
|
||||||
This Library is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This Library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with the Arduino SdFat Library. If not, see
|
|
||||||
<http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
#if defined(__arm__) // Arduino Due Board follows
|
|
||||||
|
|
||||||
#ifndef Sd2PinMap_h
|
|
||||||
#define Sd2PinMap_h
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
uint8_t const SS_PIN = SS;
|
|
||||||
uint8_t const MOSI_PIN = MOSI;
|
|
||||||
uint8_t const MISO_PIN = MISO;
|
|
||||||
uint8_t const SCK_PIN = SCK;
|
|
||||||
|
|
||||||
#endif // Sd2PinMap_h
|
|
||||||
|
|
||||||
#elif defined(__AVR_ATmega4809__) // Arduino UNO WiFI Rev2 follows
|
|
||||||
|
|
||||||
#ifndef Sd2PinMap_h
|
|
||||||
#define Sd2PinMap_h
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
uint8_t const SS_PIN = SS;
|
|
||||||
uint8_t const MOSI_PIN = MOSI;
|
|
||||||
uint8_t const MISO_PIN = MISO;
|
|
||||||
uint8_t const SCK_PIN = SCK;
|
|
||||||
|
|
||||||
#endif // Sd2PinMap_h
|
|
||||||
|
|
||||||
#elif defined(__AVR__) // Other AVR based Boards follows
|
|
||||||
|
|
||||||
// Warning this file was generated by a program.
|
|
||||||
#ifndef Sd2PinMap_h
|
|
||||||
#define Sd2PinMap_h
|
|
||||||
#include <avr/io.h>
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** struct for mapping digital pins */
|
|
||||||
struct pin_map_t {
|
|
||||||
volatile uint8_t* ddr;
|
|
||||||
volatile uint8_t* pin;
|
|
||||||
volatile uint8_t* port;
|
|
||||||
uint8_t bit;
|
|
||||||
};
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
|
||||||
// Mega
|
|
||||||
|
|
||||||
// Two Wire (aka I2C) ports
|
|
||||||
uint8_t const SDA_PIN = 20;
|
|
||||||
uint8_t const SCL_PIN = 21;
|
|
||||||
|
|
||||||
// SPI port
|
|
||||||
uint8_t const SS_PIN = 53;
|
|
||||||
uint8_t const MOSI_PIN = 51;
|
|
||||||
uint8_t const MISO_PIN = 50;
|
|
||||||
uint8_t const SCK_PIN = 52;
|
|
||||||
|
|
||||||
static const pin_map_t digitalPinMap[] = {
|
|
||||||
{&DDRE, &PINE, &PORTE, 0}, // E0 0
|
|
||||||
{&DDRE, &PINE, &PORTE, 1}, // E1 1
|
|
||||||
{&DDRE, &PINE, &PORTE, 4}, // E4 2
|
|
||||||
{&DDRE, &PINE, &PORTE, 5}, // E5 3
|
|
||||||
{&DDRG, &PING, &PORTG, 5}, // G5 4
|
|
||||||
{&DDRE, &PINE, &PORTE, 3}, // E3 5
|
|
||||||
{&DDRH, &PINH, &PORTH, 3}, // H3 6
|
|
||||||
{&DDRH, &PINH, &PORTH, 4}, // H4 7
|
|
||||||
{&DDRH, &PINH, &PORTH, 5}, // H5 8
|
|
||||||
{&DDRH, &PINH, &PORTH, 6}, // H6 9
|
|
||||||
{&DDRB, &PINB, &PORTB, 4}, // B4 10
|
|
||||||
{&DDRB, &PINB, &PORTB, 5}, // B5 11
|
|
||||||
{&DDRB, &PINB, &PORTB, 6}, // B6 12
|
|
||||||
{&DDRB, &PINB, &PORTB, 7}, // B7 13
|
|
||||||
{&DDRJ, &PINJ, &PORTJ, 1}, // J1 14
|
|
||||||
{&DDRJ, &PINJ, &PORTJ, 0}, // J0 15
|
|
||||||
{&DDRH, &PINH, &PORTH, 1}, // H1 16
|
|
||||||
{&DDRH, &PINH, &PORTH, 0}, // H0 17
|
|
||||||
{&DDRD, &PIND, &PORTD, 3}, // D3 18
|
|
||||||
{&DDRD, &PIND, &PORTD, 2}, // D2 19
|
|
||||||
{&DDRD, &PIND, &PORTD, 1}, // D1 20
|
|
||||||
{&DDRD, &PIND, &PORTD, 0}, // D0 21
|
|
||||||
{&DDRA, &PINA, &PORTA, 0}, // A0 22
|
|
||||||
{&DDRA, &PINA, &PORTA, 1}, // A1 23
|
|
||||||
{&DDRA, &PINA, &PORTA, 2}, // A2 24
|
|
||||||
{&DDRA, &PINA, &PORTA, 3}, // A3 25
|
|
||||||
{&DDRA, &PINA, &PORTA, 4}, // A4 26
|
|
||||||
{&DDRA, &PINA, &PORTA, 5}, // A5 27
|
|
||||||
{&DDRA, &PINA, &PORTA, 6}, // A6 28
|
|
||||||
{&DDRA, &PINA, &PORTA, 7}, // A7 29
|
|
||||||
{&DDRC, &PINC, &PORTC, 7}, // C7 30
|
|
||||||
{&DDRC, &PINC, &PORTC, 6}, // C6 31
|
|
||||||
{&DDRC, &PINC, &PORTC, 5}, // C5 32
|
|
||||||
{&DDRC, &PINC, &PORTC, 4}, // C4 33
|
|
||||||
{&DDRC, &PINC, &PORTC, 3}, // C3 34
|
|
||||||
{&DDRC, &PINC, &PORTC, 2}, // C2 35
|
|
||||||
{&DDRC, &PINC, &PORTC, 1}, // C1 36
|
|
||||||
{&DDRC, &PINC, &PORTC, 0}, // C0 37
|
|
||||||
{&DDRD, &PIND, &PORTD, 7}, // D7 38
|
|
||||||
{&DDRG, &PING, &PORTG, 2}, // G2 39
|
|
||||||
{&DDRG, &PING, &PORTG, 1}, // G1 40
|
|
||||||
{&DDRG, &PING, &PORTG, 0}, // G0 41
|
|
||||||
{&DDRL, &PINL, &PORTL, 7}, // L7 42
|
|
||||||
{&DDRL, &PINL, &PORTL, 6}, // L6 43
|
|
||||||
{&DDRL, &PINL, &PORTL, 5}, // L5 44
|
|
||||||
{&DDRL, &PINL, &PORTL, 4}, // L4 45
|
|
||||||
{&DDRL, &PINL, &PORTL, 3}, // L3 46
|
|
||||||
{&DDRL, &PINL, &PORTL, 2}, // L2 47
|
|
||||||
{&DDRL, &PINL, &PORTL, 1}, // L1 48
|
|
||||||
{&DDRL, &PINL, &PORTL, 0}, // L0 49
|
|
||||||
{&DDRB, &PINB, &PORTB, 3}, // B3 50
|
|
||||||
{&DDRB, &PINB, &PORTB, 2}, // B2 51
|
|
||||||
{&DDRB, &PINB, &PORTB, 1}, // B1 52
|
|
||||||
{&DDRB, &PINB, &PORTB, 0}, // B0 53
|
|
||||||
{&DDRF, &PINF, &PORTF, 0}, // F0 54
|
|
||||||
{&DDRF, &PINF, &PORTF, 1}, // F1 55
|
|
||||||
{&DDRF, &PINF, &PORTF, 2}, // F2 56
|
|
||||||
{&DDRF, &PINF, &PORTF, 3}, // F3 57
|
|
||||||
{&DDRF, &PINF, &PORTF, 4}, // F4 58
|
|
||||||
{&DDRF, &PINF, &PORTF, 5}, // F5 59
|
|
||||||
{&DDRF, &PINF, &PORTF, 6}, // F6 60
|
|
||||||
{&DDRF, &PINF, &PORTF, 7}, // F7 61
|
|
||||||
{&DDRK, &PINK, &PORTK, 0}, // K0 62
|
|
||||||
{&DDRK, &PINK, &PORTK, 1}, // K1 63
|
|
||||||
{&DDRK, &PINK, &PORTK, 2}, // K2 64
|
|
||||||
{&DDRK, &PINK, &PORTK, 3}, // K3 65
|
|
||||||
{&DDRK, &PINK, &PORTK, 4}, // K4 66
|
|
||||||
{&DDRK, &PINK, &PORTK, 5}, // K5 67
|
|
||||||
{&DDRK, &PINK, &PORTK, 6}, // K6 68
|
|
||||||
{&DDRK, &PINK, &PORTK, 7} // K7 69
|
|
||||||
};
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#elif (defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__)) && defined(CORE_MICRODUINO)
|
|
||||||
// Microduino Core+
|
|
||||||
|
|
||||||
// Two Wire (aka I2C) ports
|
|
||||||
uint8_t const SDA_PIN = 20;
|
|
||||||
uint8_t const SCL_PIN = 21;
|
|
||||||
|
|
||||||
// SPI port
|
|
||||||
uint8_t const SS_PIN = 10;
|
|
||||||
uint8_t const MOSI_PIN = 11;
|
|
||||||
uint8_t const MISO_PIN = 12;
|
|
||||||
uint8_t const SCK_PIN = 13;
|
|
||||||
|
|
||||||
static const pin_map_t digitalPinMap[] = {
|
|
||||||
{&DDRD, &PIND, &PORTD, 0}, // D0 PD0
|
|
||||||
{&DDRD, &PIND, &PORTD, 1}, // D1 PD1
|
|
||||||
{&DDRD, &PIND, &PORTD, 2}, // D2 PD2
|
|
||||||
{&DDRD, &PIND, &PORTD, 3}, // D3 PD3
|
|
||||||
{&DDRB, &PINB, &PORTB, 0}, // D4 PB0
|
|
||||||
{&DDRB, &PINB, &PORTB, 1}, // D5 PB1
|
|
||||||
{&DDRB, &PINB, &PORTB, 2}, // D6 PB2
|
|
||||||
{&DDRB, &PINB, &PORTB, 3}, // D7 PB3
|
|
||||||
{&DDRD, &PIND, &PORTD, 6}, // D8 PD6
|
|
||||||
{&DDRD, &PIND, &PORTD, 5}, // D9 PD5
|
|
||||||
{&DDRB, &PINB, &PORTB, 4}, // D10 PB4
|
|
||||||
{&DDRB, &PINB, &PORTB, 5}, // D11 PB5
|
|
||||||
{&DDRB, &PINB, &PORTB, 6}, // D12 PB6
|
|
||||||
{&DDRB, &PINB, &PORTB, 7}, // D13 PB7
|
|
||||||
{&DDRC, &PINC, &PORTC, 7}, // D14 PC7
|
|
||||||
{&DDRC, &PINC, &PORTC, 6}, // D15 PC6
|
|
||||||
{&DDRC, &PINC, &PORTC, 5}, // D16 PC5
|
|
||||||
{&DDRC, &PINC, &PORTC, 4}, // D17 PC4
|
|
||||||
{&DDRC, &PINC, &PORTC, 3}, // D18 PC3
|
|
||||||
{&DDRC, &PINC, &PORTC, 2}, // D19 PC2
|
|
||||||
{&DDRC, &PINC, &PORTC, 1}, // D20 PC1
|
|
||||||
{&DDRC, &PINC, &PORTC, 0}, // D21 PC0
|
|
||||||
{&DDRD, &PIND, &PORTD, 4}, // D22 PD4
|
|
||||||
{&DDRD, &PIND, &PORTD, 7}, // D23 PD7
|
|
||||||
{&DDRA, &PINA, &PORTA, 7}, // D24 PA7
|
|
||||||
{&DDRA, &PINA, &PORTA, 6}, // D25 PA6
|
|
||||||
{&DDRA, &PINA, &PORTA, 5}, // D26 PA5
|
|
||||||
{&DDRA, &PINA, &PORTA, 4}, // D27 PA4
|
|
||||||
{&DDRA, &PINA, &PORTA, 3}, // D28 PA3
|
|
||||||
{&DDRA, &PINA, &PORTA, 2}, // D29 PA2
|
|
||||||
{&DDRA, &PINA, &PORTA, 1}, // D30 PA1
|
|
||||||
{&DDRA, &PINA, &PORTA, 0} // D31 PA0
|
|
||||||
};
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#elif defined(__AVR_ATmega128RFA1__) && defined(CORE_MICRODUINO)
|
|
||||||
// Microduino Core RF
|
|
||||||
|
|
||||||
// Two Wire (aka I2C) ports
|
|
||||||
uint8_t const SDA_PIN = 18;
|
|
||||||
uint8_t const SCL_PIN = 19;
|
|
||||||
|
|
||||||
// SPI port
|
|
||||||
uint8_t const SS_PIN = 10;
|
|
||||||
uint8_t const MOSI_PIN = 11;
|
|
||||||
uint8_t const MISO_PIN = 12;
|
|
||||||
uint8_t const SCK_PIN = 13;
|
|
||||||
|
|
||||||
static const pin_map_t digitalPinMap[] = {
|
|
||||||
{&DDRD, &PINE, &PORTE, 0}, // D0 PE0
|
|
||||||
{&DDRD, &PINE, &PORTE, 1}, // D1 PE1
|
|
||||||
{&DDRD, &PIND, &PORTD, 2}, // D2 PD2
|
|
||||||
{&DDRD, &PIND, &PORTD, 3}, // D3 PD3
|
|
||||||
{&DDRB, &PINE, &PORTE, 3}, // D4 PE3
|
|
||||||
{&DDRB, &PINE, &PORTE, 4}, // D5 PE4
|
|
||||||
{&DDRB, &PINE, &PORTE, 5}, // D6 PE5
|
|
||||||
{&DDRB, &PINB, &PORTB, 7}, // D7 PB7
|
|
||||||
{&DDRD, &PINB, &PORTB, 6}, // D8 PB6
|
|
||||||
{&DDRD, &PINB, &PORTB, 5}, // D9 PB5
|
|
||||||
{&DDRB, &PINB, &PORTB, 4}, // D10 PB4
|
|
||||||
{&DDRB, &PINB, &PORTB, 2}, // D11 PB2
|
|
||||||
{&DDRB, &PINB, &PORTB, 3}, // D12 PB3
|
|
||||||
{&DDRB, &PINB, &PORTB, 1}, // D13 PB1
|
|
||||||
{&DDRF, &PINF, &PORTF, 7}, // D14 PF7
|
|
||||||
{&DDRF, &PINF, &PORTF, 6}, // D15 PF6
|
|
||||||
{&DDRF, &PINF, &PORTF, 5}, // D16 PF5
|
|
||||||
{&DDRF, &PINF, &PORTF, 4}, // D17 PF4
|
|
||||||
{&DDRD, &PIND, &PORTD, 1}, // D18 PD1
|
|
||||||
{&DDRD, &PIND, &PORTD, 0}, // D19 PD0
|
|
||||||
{&DDRF, &PINF, &PORTF, 3}, // D20 PF3
|
|
||||||
{&DDRF, &PINF, &PORTF, 2}, // D21 PF2
|
|
||||||
};
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#elif defined(__AVR_ATmega32U4__) && defined(CORE_MICRODUINO)
|
|
||||||
// Microduino Core USB
|
|
||||||
|
|
||||||
// Two Wire (aka I2C) ports
|
|
||||||
uint8_t const SDA_PIN = 18;
|
|
||||||
uint8_t const SCL_PIN = 19;
|
|
||||||
|
|
||||||
// SPI port
|
|
||||||
uint8_t const SS_PIN = 10;
|
|
||||||
uint8_t const MOSI_PIN = 11;
|
|
||||||
uint8_t const MISO_PIN = 12;
|
|
||||||
uint8_t const SCK_PIN = 13;
|
|
||||||
|
|
||||||
static const pin_map_t digitalPinMap[] = {
|
|
||||||
{&DDRD, &PIND, &PORTD, 2}, // D0 - PD2
|
|
||||||
{&DDRD, &PIND, &PORTD, 3}, // D1 - PD3
|
|
||||||
{&DDRE, &PINE, &PORTE, 6}, // D2 - PE6
|
|
||||||
{&DDRD, &PIND, &PORTD, 6}, // D3 - PD6
|
|
||||||
{&DDRD, &PIND, &PORTD, 7}, // D4 - PD7
|
|
||||||
{&DDRC, &PINC, &PORTC, 6}, // D5 - PC6
|
|
||||||
{&DDRC, &PINC, &PORTC, 7}, // D6 - PC7
|
|
||||||
{&DDRE, &PINE, &PORTE, 7}, // D7 - PE7
|
|
||||||
{&DDRB, &PINB, &PORTB, 6}, // D8 - PB6
|
|
||||||
{&DDRB, &PINB, &PORTB, 5}, // D9 - PB5
|
|
||||||
{&DDRB, &PINB, &PORTB, 0}, // D10 - PB0
|
|
||||||
{&DDRB, &PINB, &PORTB, 2}, // D11 - MOSI - PB2
|
|
||||||
{&DDRB, &PINB, &PORTB, 3}, // D12 -MISO - PB3
|
|
||||||
{&DDRB, &PINB, &PORTB, 1}, // D13 -SCK - PB1
|
|
||||||
{&DDRF, &PINF, &PORTF, 7}, // D14 - A0 - PF7
|
|
||||||
{&DDRF, &PINF, &PORTF, 6}, // D15 - A1 - PF6
|
|
||||||
{&DDRF, &PINF, &PORTF, 5}, // D16 - A2 - PF5
|
|
||||||
{&DDRF, &PINF, &PORTF, 4}, // D17 - A3 - PF4
|
|
||||||
{&DDRD, &PIND, &PORTD, 1}, // D18 - PD1
|
|
||||||
{&DDRD, &PIND, &PORTD, 0}, // D19 - PD0
|
|
||||||
{&DDRF, &PINF, &PORTF, 1}, // D20 - A6 - PF1
|
|
||||||
{&DDRF, &PINF, &PORTF, 0}, // D21 - A7 - PF0
|
|
||||||
};
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
|
|
||||||
// Sanguino
|
|
||||||
|
|
||||||
// Two Wire (aka I2C) ports
|
|
||||||
uint8_t const SDA_PIN = 17;
|
|
||||||
uint8_t const SCL_PIN = 18;
|
|
||||||
|
|
||||||
// SPI port
|
|
||||||
uint8_t const SS_PIN = 4;
|
|
||||||
uint8_t const MOSI_PIN = 5;
|
|
||||||
uint8_t const MISO_PIN = 6;
|
|
||||||
uint8_t const SCK_PIN = 7;
|
|
||||||
|
|
||||||
static const pin_map_t digitalPinMap[] = {
|
|
||||||
{&DDRB, &PINB, &PORTB, 0}, // B0 0
|
|
||||||
{&DDRB, &PINB, &PORTB, 1}, // B1 1
|
|
||||||
{&DDRB, &PINB, &PORTB, 2}, // B2 2
|
|
||||||
{&DDRB, &PINB, &PORTB, 3}, // B3 3
|
|
||||||
{&DDRB, &PINB, &PORTB, 4}, // B4 4
|
|
||||||
{&DDRB, &PINB, &PORTB, 5}, // B5 5
|
|
||||||
{&DDRB, &PINB, &PORTB, 6}, // B6 6
|
|
||||||
{&DDRB, &PINB, &PORTB, 7}, // B7 7
|
|
||||||
{&DDRD, &PIND, &PORTD, 0}, // D0 8
|
|
||||||
{&DDRD, &PIND, &PORTD, 1}, // D1 9
|
|
||||||
{&DDRD, &PIND, &PORTD, 2}, // D2 10
|
|
||||||
{&DDRD, &PIND, &PORTD, 3}, // D3 11
|
|
||||||
{&DDRD, &PIND, &PORTD, 4}, // D4 12
|
|
||||||
{&DDRD, &PIND, &PORTD, 5}, // D5 13
|
|
||||||
{&DDRD, &PIND, &PORTD, 6}, // D6 14
|
|
||||||
{&DDRD, &PIND, &PORTD, 7}, // D7 15
|
|
||||||
{&DDRC, &PINC, &PORTC, 0}, // C0 16
|
|
||||||
{&DDRC, &PINC, &PORTC, 1}, // C1 17
|
|
||||||
{&DDRC, &PINC, &PORTC, 2}, // C2 18
|
|
||||||
{&DDRC, &PINC, &PORTC, 3}, // C3 19
|
|
||||||
{&DDRC, &PINC, &PORTC, 4}, // C4 20
|
|
||||||
{&DDRC, &PINC, &PORTC, 5}, // C5 21
|
|
||||||
{&DDRC, &PINC, &PORTC, 6}, // C6 22
|
|
||||||
{&DDRC, &PINC, &PORTC, 7}, // C7 23
|
|
||||||
{&DDRA, &PINA, &PORTA, 7}, // A7 24
|
|
||||||
{&DDRA, &PINA, &PORTA, 6}, // A6 25
|
|
||||||
{&DDRA, &PINA, &PORTA, 5}, // A5 26
|
|
||||||
{&DDRA, &PINA, &PORTA, 4}, // A4 27
|
|
||||||
{&DDRA, &PINA, &PORTA, 3}, // A3 28
|
|
||||||
{&DDRA, &PINA, &PORTA, 2}, // A2 29
|
|
||||||
{&DDRA, &PINA, &PORTA, 1}, // A1 30
|
|
||||||
{&DDRA, &PINA, &PORTA, 0} // A0 31
|
|
||||||
};
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#elif defined(__AVR_ATmega32U4__)
|
|
||||||
// Leonardo
|
|
||||||
|
|
||||||
// Two Wire (aka I2C) ports
|
|
||||||
uint8_t const SDA_PIN = 2;
|
|
||||||
uint8_t const SCL_PIN = 3;
|
|
||||||
|
|
||||||
// SPI port
|
|
||||||
uint8_t const SS_PIN = 17;
|
|
||||||
uint8_t const MOSI_PIN = 16;
|
|
||||||
uint8_t const MISO_PIN = 14;
|
|
||||||
uint8_t const SCK_PIN = 15;
|
|
||||||
|
|
||||||
static const pin_map_t digitalPinMap[] = {
|
|
||||||
{&DDRD, &PIND, &PORTD, 2}, // D2 0
|
|
||||||
{&DDRD, &PIND, &PORTD, 3}, // D3 1
|
|
||||||
{&DDRD, &PIND, &PORTD, 1}, // D1 2
|
|
||||||
{&DDRD, &PIND, &PORTD, 0}, // D0 3
|
|
||||||
{&DDRD, &PIND, &PORTD, 4}, // D4 4
|
|
||||||
{&DDRC, &PINC, &PORTC, 6}, // C6 5
|
|
||||||
{&DDRD, &PIND, &PORTD, 7}, // D7 6
|
|
||||||
{&DDRE, &PINE, &PORTE, 6}, // E6 7
|
|
||||||
{&DDRB, &PINB, &PORTB, 4}, // B4 8
|
|
||||||
{&DDRB, &PINB, &PORTB, 5}, // B5 9
|
|
||||||
{&DDRB, &PINB, &PORTB, 6}, // B6 10
|
|
||||||
{&DDRB, &PINB, &PORTB, 7}, // B7 11
|
|
||||||
{&DDRD, &PIND, &PORTD, 6}, // D6 12
|
|
||||||
{&DDRC, &PINC, &PORTC, 7}, // C7 13
|
|
||||||
{&DDRB, &PINB, &PORTB, 3}, // B3 14
|
|
||||||
{&DDRB, &PINB, &PORTB, 1}, // B1 15
|
|
||||||
{&DDRB, &PINB, &PORTB, 2}, // B2 16
|
|
||||||
{&DDRB, &PINB, &PORTB, 0}, // B0 17
|
|
||||||
{&DDRF, &PINF, &PORTF, 7}, // F7 18
|
|
||||||
{&DDRF, &PINF, &PORTF, 6}, // F6 19
|
|
||||||
{&DDRF, &PINF, &PORTF, 5}, // F5 20
|
|
||||||
{&DDRF, &PINF, &PORTF, 4}, // F4 21
|
|
||||||
{&DDRF, &PINF, &PORTF, 1}, // F1 22
|
|
||||||
{&DDRF, &PINF, &PORTF, 0}, // F0 23
|
|
||||||
};
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
|
|
||||||
// Teensy++ 1.0 & 2.0
|
|
||||||
|
|
||||||
// Two Wire (aka I2C) ports
|
|
||||||
uint8_t const SDA_PIN = 1;
|
|
||||||
uint8_t const SCL_PIN = 0;
|
|
||||||
|
|
||||||
// SPI port
|
|
||||||
uint8_t const SS_PIN = 20;
|
|
||||||
uint8_t const MOSI_PIN = 22;
|
|
||||||
uint8_t const MISO_PIN = 23;
|
|
||||||
uint8_t const SCK_PIN = 21;
|
|
||||||
|
|
||||||
static const pin_map_t digitalPinMap[] = {
|
|
||||||
{&DDRD, &PIND, &PORTD, 0}, // D0 0
|
|
||||||
{&DDRD, &PIND, &PORTD, 1}, // D1 1
|
|
||||||
{&DDRD, &PIND, &PORTD, 2}, // D2 2
|
|
||||||
{&DDRD, &PIND, &PORTD, 3}, // D3 3
|
|
||||||
{&DDRD, &PIND, &PORTD, 4}, // D4 4
|
|
||||||
{&DDRD, &PIND, &PORTD, 5}, // D5 5
|
|
||||||
{&DDRD, &PIND, &PORTD, 6}, // D6 6
|
|
||||||
{&DDRD, &PIND, &PORTD, 7}, // D7 7
|
|
||||||
{&DDRE, &PINE, &PORTE, 0}, // E0 8
|
|
||||||
{&DDRE, &PINE, &PORTE, 1}, // E1 9
|
|
||||||
{&DDRC, &PINC, &PORTC, 0}, // C0 10
|
|
||||||
{&DDRC, &PINC, &PORTC, 1}, // C1 11
|
|
||||||
{&DDRC, &PINC, &PORTC, 2}, // C2 12
|
|
||||||
{&DDRC, &PINC, &PORTC, 3}, // C3 13
|
|
||||||
{&DDRC, &PINC, &PORTC, 4}, // C4 14
|
|
||||||
{&DDRC, &PINC, &PORTC, 5}, // C5 15
|
|
||||||
{&DDRC, &PINC, &PORTC, 6}, // C6 16
|
|
||||||
{&DDRC, &PINC, &PORTC, 7}, // C7 17
|
|
||||||
{&DDRE, &PINE, &PORTE, 6}, // E6 18
|
|
||||||
{&DDRE, &PINE, &PORTE, 7}, // E7 19
|
|
||||||
{&DDRB, &PINB, &PORTB, 0}, // B0 20
|
|
||||||
{&DDRB, &PINB, &PORTB, 1}, // B1 21
|
|
||||||
{&DDRB, &PINB, &PORTB, 2}, // B2 22
|
|
||||||
{&DDRB, &PINB, &PORTB, 3}, // B3 23
|
|
||||||
{&DDRB, &PINB, &PORTB, 4}, // B4 24
|
|
||||||
{&DDRB, &PINB, &PORTB, 5}, // B5 25
|
|
||||||
{&DDRB, &PINB, &PORTB, 6}, // B6 26
|
|
||||||
{&DDRB, &PINB, &PORTB, 7}, // B7 27
|
|
||||||
{&DDRA, &PINA, &PORTA, 0}, // A0 28
|
|
||||||
{&DDRA, &PINA, &PORTA, 1}, // A1 29
|
|
||||||
{&DDRA, &PINA, &PORTA, 2}, // A2 30
|
|
||||||
{&DDRA, &PINA, &PORTA, 3}, // A3 31
|
|
||||||
{&DDRA, &PINA, &PORTA, 4}, // A4 32
|
|
||||||
{&DDRA, &PINA, &PORTA, 5}, // A5 33
|
|
||||||
{&DDRA, &PINA, &PORTA, 6}, // A6 34
|
|
||||||
{&DDRA, &PINA, &PORTA, 7}, // A7 35
|
|
||||||
{&DDRE, &PINE, &PORTE, 4}, // E4 36
|
|
||||||
{&DDRE, &PINE, &PORTE, 5}, // E5 37
|
|
||||||
{&DDRF, &PINF, &PORTF, 0}, // F0 38
|
|
||||||
{&DDRF, &PINF, &PORTF, 1}, // F1 39
|
|
||||||
{&DDRF, &PINF, &PORTF, 2}, // F2 40
|
|
||||||
{&DDRF, &PINF, &PORTF, 3}, // F3 41
|
|
||||||
{&DDRF, &PINF, &PORTF, 4}, // F4 42
|
|
||||||
{&DDRF, &PINF, &PORTF, 5}, // F5 43
|
|
||||||
{&DDRF, &PINF, &PORTF, 6}, // F6 44
|
|
||||||
{&DDRF, &PINF, &PORTF, 7} // F7 45
|
|
||||||
};
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#else // defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
|
||||||
// 168 and 328 Arduinos
|
|
||||||
|
|
||||||
// Two Wire (aka I2C) ports
|
|
||||||
uint8_t const SDA_PIN = 18;
|
|
||||||
uint8_t const SCL_PIN = 19;
|
|
||||||
|
|
||||||
// SPI port
|
|
||||||
uint8_t const SS_PIN = 10;
|
|
||||||
uint8_t const MOSI_PIN = 11;
|
|
||||||
uint8_t const MISO_PIN = 12;
|
|
||||||
uint8_t const SCK_PIN = 13;
|
|
||||||
|
|
||||||
static const pin_map_t digitalPinMap[] = {
|
|
||||||
{&DDRD, &PIND, &PORTD, 0}, // D0 0
|
|
||||||
{&DDRD, &PIND, &PORTD, 1}, // D1 1
|
|
||||||
{&DDRD, &PIND, &PORTD, 2}, // D2 2
|
|
||||||
{&DDRD, &PIND, &PORTD, 3}, // D3 3
|
|
||||||
{&DDRD, &PIND, &PORTD, 4}, // D4 4
|
|
||||||
{&DDRD, &PIND, &PORTD, 5}, // D5 5
|
|
||||||
{&DDRD, &PIND, &PORTD, 6}, // D6 6
|
|
||||||
{&DDRD, &PIND, &PORTD, 7}, // D7 7
|
|
||||||
{&DDRB, &PINB, &PORTB, 0}, // B0 8
|
|
||||||
{&DDRB, &PINB, &PORTB, 1}, // B1 9
|
|
||||||
{&DDRB, &PINB, &PORTB, 2}, // B2 10
|
|
||||||
{&DDRB, &PINB, &PORTB, 3}, // B3 11
|
|
||||||
{&DDRB, &PINB, &PORTB, 4}, // B4 12
|
|
||||||
{&DDRB, &PINB, &PORTB, 5}, // B5 13
|
|
||||||
{&DDRC, &PINC, &PORTC, 0}, // C0 14
|
|
||||||
{&DDRC, &PINC, &PORTC, 1}, // C1 15
|
|
||||||
{&DDRC, &PINC, &PORTC, 2}, // C2 16
|
|
||||||
{&DDRC, &PINC, &PORTC, 3}, // C3 17
|
|
||||||
{&DDRC, &PINC, &PORTC, 4}, // C4 18
|
|
||||||
{&DDRC, &PINC, &PORTC, 5} // C5 19
|
|
||||||
};
|
|
||||||
#endif // defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
static const uint8_t digitalPinCount = sizeof(digitalPinMap) / sizeof(pin_map_t);
|
|
||||||
|
|
||||||
uint8_t badPinNumber(void)
|
|
||||||
__attribute__((error("Pin number is too large or not a constant")));
|
|
||||||
|
|
||||||
static inline __attribute__((always_inline))
|
|
||||||
uint8_t getPinMode(uint8_t pin) {
|
|
||||||
if (__builtin_constant_p(pin) && pin < digitalPinCount) {
|
|
||||||
return (*digitalPinMap[pin].ddr >> digitalPinMap[pin].bit) & 1;
|
|
||||||
} else {
|
|
||||||
return badPinNumber();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
static inline __attribute__((always_inline))
|
|
||||||
void setPinMode(uint8_t pin, uint8_t mode) {
|
|
||||||
if (__builtin_constant_p(pin) && pin < digitalPinCount) {
|
|
||||||
if (mode) {
|
|
||||||
*digitalPinMap[pin].ddr |= 1 << digitalPinMap[pin].bit;
|
|
||||||
} else {
|
|
||||||
*digitalPinMap[pin].ddr &= ~(1 << digitalPinMap[pin].bit);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
badPinNumber();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
static inline __attribute__((always_inline))
|
|
||||||
uint8_t fastDigitalRead(uint8_t pin) {
|
|
||||||
if (__builtin_constant_p(pin) && pin < digitalPinCount) {
|
|
||||||
return (*digitalPinMap[pin].pin >> digitalPinMap[pin].bit) & 1;
|
|
||||||
} else {
|
|
||||||
return badPinNumber();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
static inline __attribute__((always_inline))
|
|
||||||
void fastDigitalWrite(uint8_t pin, uint8_t value) {
|
|
||||||
if (__builtin_constant_p(pin) && pin < digitalPinCount) {
|
|
||||||
if (value) {
|
|
||||||
*digitalPinMap[pin].port |= 1 << digitalPinMap[pin].bit;
|
|
||||||
} else {
|
|
||||||
*digitalPinMap[pin].port &= ~(1 << digitalPinMap[pin].bit);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
badPinNumber();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif // Sd2PinMap_h
|
|
||||||
|
|
||||||
#elif defined (__CPU_ARC__)
|
|
||||||
|
|
||||||
#if defined (__ARDUINO_ARC__)
|
|
||||||
// Two Wire (aka I2C) ports
|
|
||||||
uint8_t const SDA_PIN = 18;
|
|
||||||
uint8_t const SCL_PIN = 19;
|
|
||||||
|
|
||||||
// SPI port
|
|
||||||
uint8_t const SS_PIN = 10;
|
|
||||||
uint8_t const MOSI_PIN = 11;
|
|
||||||
uint8_t const MISO_PIN = 12;
|
|
||||||
uint8_t const SCK_PIN = 13;
|
|
||||||
|
|
||||||
#endif // Arduino ARC
|
|
||||||
|
|
||||||
#else
|
|
||||||
#error Architecture or board not supported.
|
|
||||||
#endif
|
|
||||||
@@ -1,641 +0,0 @@
|
|||||||
/* Arduino SdFat Library
|
|
||||||
Copyright (C) 2009 by William Greiman
|
|
||||||
|
|
||||||
This file is part of the Arduino SdFat Library
|
|
||||||
|
|
||||||
This Library is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This Library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with the Arduino SdFat Library. If not, see
|
|
||||||
<http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
#ifndef SdFat_h
|
|
||||||
#define SdFat_h
|
|
||||||
/**
|
|
||||||
\file
|
|
||||||
SdFile and SdVolume classes
|
|
||||||
*/
|
|
||||||
#if defined (__AVR__) || defined (__CPU_ARC__)
|
|
||||||
#include <avr/pgmspace.h>
|
|
||||||
#endif
|
|
||||||
#include "Sd2Card.h"
|
|
||||||
#include "FatStructs.h"
|
|
||||||
#include <Print.h>
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
Allow use of deprecated functions if non-zero
|
|
||||||
*/
|
|
||||||
#define ALLOW_DEPRECATED_FUNCTIONS 1
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// forward declaration since SdVolume is used in SdFile
|
|
||||||
class SdVolume;
|
|
||||||
//==============================================================================
|
|
||||||
// SdFile class
|
|
||||||
|
|
||||||
#ifdef O_RDONLY //ARDUINO_ARCH_MBED
|
|
||||||
#undef O_READ
|
|
||||||
#undef O_RDONLY
|
|
||||||
#undef O_WRITE
|
|
||||||
#undef O_WRONLY
|
|
||||||
#undef O_RDWR
|
|
||||||
#undef O_ACCMODE
|
|
||||||
#undef O_APPEND
|
|
||||||
#undef O_SYNC
|
|
||||||
#undef O_CREAT
|
|
||||||
#undef O_EXCL
|
|
||||||
#undef O_TRUNC
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// flags for ls()
|
|
||||||
/** ls() flag to print modify date */
|
|
||||||
uint8_t const LS_DATE = 1;
|
|
||||||
/** ls() flag to print file size */
|
|
||||||
uint8_t const LS_SIZE = 2;
|
|
||||||
/** ls() flag for recursive list of subdirectories */
|
|
||||||
uint8_t const LS_R = 4;
|
|
||||||
|
|
||||||
// use the gnu style oflag in open()
|
|
||||||
/** open() oflag for reading */
|
|
||||||
uint8_t const O_READ = 0X01;
|
|
||||||
/** open() oflag - same as O_READ */
|
|
||||||
uint8_t const O_RDONLY = O_READ;
|
|
||||||
/** open() oflag for write */
|
|
||||||
uint8_t const O_WRITE = 0X02;
|
|
||||||
/** open() oflag - same as O_WRITE */
|
|
||||||
uint8_t const O_WRONLY = O_WRITE;
|
|
||||||
/** open() oflag for reading and writing */
|
|
||||||
uint8_t const O_RDWR = (O_READ | O_WRITE);
|
|
||||||
/** open() oflag mask for access modes */
|
|
||||||
uint8_t const O_ACCMODE = (O_READ | O_WRITE);
|
|
||||||
/** The file offset shall be set to the end of the file prior to each write. */
|
|
||||||
uint8_t const O_APPEND = 0X04;
|
|
||||||
/** synchronous writes - call sync() after each write */
|
|
||||||
uint8_t const O_SYNC = 0X08;
|
|
||||||
/** create the file if nonexistent */
|
|
||||||
uint8_t const O_CREAT = 0X10;
|
|
||||||
/** If O_CREAT and O_EXCL are set, open() shall fail if the file exists */
|
|
||||||
uint8_t const O_EXCL = 0X20;
|
|
||||||
/** truncate the file to zero length */
|
|
||||||
uint8_t const O_TRUNC = 0X40;
|
|
||||||
|
|
||||||
// flags for timestamp
|
|
||||||
/** set the file's last access date */
|
|
||||||
uint8_t const T_ACCESS = 1;
|
|
||||||
/** set the file's creation date and time */
|
|
||||||
uint8_t const T_CREATE = 2;
|
|
||||||
/** Set the file's write date and time */
|
|
||||||
uint8_t const T_WRITE = 4;
|
|
||||||
// values for type_
|
|
||||||
/** This SdFile has not been opened. */
|
|
||||||
uint8_t const FAT_FILE_TYPE_CLOSED = 0;
|
|
||||||
/** SdFile for a file */
|
|
||||||
uint8_t const FAT_FILE_TYPE_NORMAL = 1;
|
|
||||||
/** SdFile for a FAT16 root directory */
|
|
||||||
uint8_t const FAT_FILE_TYPE_ROOT16 = 2;
|
|
||||||
/** SdFile for a FAT32 root directory */
|
|
||||||
uint8_t const FAT_FILE_TYPE_ROOT32 = 3;
|
|
||||||
/** SdFile for a subdirectory */
|
|
||||||
uint8_t const FAT_FILE_TYPE_SUBDIR = 4;
|
|
||||||
/** Test value for directory type */
|
|
||||||
uint8_t const FAT_FILE_TYPE_MIN_DIR = FAT_FILE_TYPE_ROOT16;
|
|
||||||
|
|
||||||
/** date field for FAT directory entry */
|
|
||||||
static inline uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day) {
|
|
||||||
return (year - 1980) << 9 | month << 5 | day;
|
|
||||||
}
|
|
||||||
/** year part of FAT directory date field */
|
|
||||||
static inline uint16_t FAT_YEAR(uint16_t fatDate) {
|
|
||||||
return 1980 + (fatDate >> 9);
|
|
||||||
}
|
|
||||||
/** month part of FAT directory date field */
|
|
||||||
static inline uint8_t FAT_MONTH(uint16_t fatDate) {
|
|
||||||
return (fatDate >> 5) & 0XF;
|
|
||||||
}
|
|
||||||
/** day part of FAT directory date field */
|
|
||||||
static inline uint8_t FAT_DAY(uint16_t fatDate) {
|
|
||||||
return fatDate & 0X1F;
|
|
||||||
}
|
|
||||||
/** time field for FAT directory entry */
|
|
||||||
static inline uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second) {
|
|
||||||
return hour << 11 | minute << 5 | second >> 1;
|
|
||||||
}
|
|
||||||
/** hour part of FAT directory time field */
|
|
||||||
static inline uint8_t FAT_HOUR(uint16_t fatTime) {
|
|
||||||
return fatTime >> 11;
|
|
||||||
}
|
|
||||||
/** minute part of FAT directory time field */
|
|
||||||
static inline uint8_t FAT_MINUTE(uint16_t fatTime) {
|
|
||||||
return (fatTime >> 5) & 0X3F;
|
|
||||||
}
|
|
||||||
/** second part of FAT directory time field */
|
|
||||||
static inline uint8_t FAT_SECOND(uint16_t fatTime) {
|
|
||||||
return 2 * (fatTime & 0X1F);
|
|
||||||
}
|
|
||||||
/** Default date for file timestamps is 1 Jan 2000 */
|
|
||||||
uint16_t const FAT_DEFAULT_DATE = ((2000 - 1980) << 9) | (1 << 5) | 1;
|
|
||||||
/** Default time for file timestamp is 1 am */
|
|
||||||
uint16_t const FAT_DEFAULT_TIME = (1 << 11);
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
\class SdFile
|
|
||||||
\brief Access FAT16 and FAT32 files on SD and SDHC cards.
|
|
||||||
*/
|
|
||||||
class SdFile : public Print {
|
|
||||||
public:
|
|
||||||
/** Create an instance of SdFile. */
|
|
||||||
SdFile(void) : type_(FAT_FILE_TYPE_CLOSED) {}
|
|
||||||
/**
|
|
||||||
writeError is set to true if an error occurs during a write().
|
|
||||||
Set writeError to false before calling print() and/or write() and check
|
|
||||||
for true after calls to print() and/or write().
|
|
||||||
*/
|
|
||||||
//bool writeError;
|
|
||||||
/**
|
|
||||||
Cancel unbuffered reads for this file.
|
|
||||||
See setUnbufferedRead()
|
|
||||||
*/
|
|
||||||
void clearUnbufferedRead(void) {
|
|
||||||
flags_ &= ~F_FILE_UNBUFFERED_READ;
|
|
||||||
}
|
|
||||||
uint8_t close(void);
|
|
||||||
uint8_t contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
|
|
||||||
uint8_t createContiguous(SdFile* dirFile,
|
|
||||||
const char* fileName, uint32_t size);
|
|
||||||
/** \return The current cluster number for a file or directory. */
|
|
||||||
uint32_t curCluster(void) const {
|
|
||||||
return curCluster_;
|
|
||||||
}
|
|
||||||
/** \return The current position for a file or directory. */
|
|
||||||
uint32_t curPosition(void) const {
|
|
||||||
return curPosition_;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
Set the date/time callback function
|
|
||||||
|
|
||||||
\param[in] dateTime The user's call back function. The callback
|
|
||||||
function is of the form:
|
|
||||||
|
|
||||||
\code
|
|
||||||
void dateTime(uint16_t* date, uint16_t* time) {
|
|
||||||
uint16_t year;
|
|
||||||
uint8_t month, day, hour, minute, second;
|
|
||||||
|
|
||||||
// User gets date and time from GPS or real-time clock here
|
|
||||||
|
|
||||||
// return date using FAT_DATE macro to format fields
|
|
||||||
* *date = FAT_DATE(year, month, day);
|
|
||||||
|
|
||||||
// return time using FAT_TIME macro to format fields
|
|
||||||
* *time = FAT_TIME(hour, minute, second);
|
|
||||||
}
|
|
||||||
\endcode
|
|
||||||
|
|
||||||
Sets the function that is called when a file is created or when
|
|
||||||
a file's directory entry is modified by sync(). All timestamps,
|
|
||||||
access, creation, and modify, are set when a file is created.
|
|
||||||
sync() maintains the last access date and last modify date/time.
|
|
||||||
|
|
||||||
See the timestamp() function.
|
|
||||||
*/
|
|
||||||
static void dateTimeCallback(
|
|
||||||
void (*dateTime)(uint16_t* date, uint16_t* time)) {
|
|
||||||
dateTime_ = dateTime;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
Cancel the date/time callback function.
|
|
||||||
*/
|
|
||||||
static void dateTimeCallbackCancel(void) {
|
|
||||||
// use explicit zero since NULL is not defined for Sanguino
|
|
||||||
dateTime_ = 0;
|
|
||||||
}
|
|
||||||
/** \return Address of the block that contains this file's directory. */
|
|
||||||
uint32_t dirBlock(void) const {
|
|
||||||
return dirBlock_;
|
|
||||||
}
|
|
||||||
uint8_t dirEntry(dir_t* dir);
|
|
||||||
/** \return Index of this file's directory in the block dirBlock. */
|
|
||||||
uint8_t dirIndex(void) const {
|
|
||||||
return dirIndex_;
|
|
||||||
}
|
|
||||||
static void dirName(const dir_t& dir, char* name);
|
|
||||||
/** \return The total number of bytes in a file or directory. */
|
|
||||||
uint32_t fileSize(void) const {
|
|
||||||
return fileSize_;
|
|
||||||
}
|
|
||||||
/** \return The first cluster number for a file or directory. */
|
|
||||||
uint32_t firstCluster(void) const {
|
|
||||||
return firstCluster_;
|
|
||||||
}
|
|
||||||
/** \return True if this is a SdFile for a directory else false. */
|
|
||||||
uint8_t isDir(void) const {
|
|
||||||
return type_ >= FAT_FILE_TYPE_MIN_DIR;
|
|
||||||
}
|
|
||||||
/** \return True if this is a SdFile for a file else false. */
|
|
||||||
uint8_t isFile(void) const {
|
|
||||||
return type_ == FAT_FILE_TYPE_NORMAL;
|
|
||||||
}
|
|
||||||
/** \return True if this is a SdFile for an open file/directory else false. */
|
|
||||||
uint8_t isOpen(void) const {
|
|
||||||
return type_ != FAT_FILE_TYPE_CLOSED;
|
|
||||||
}
|
|
||||||
/** \return True if this is a SdFile for a subdirectory else false. */
|
|
||||||
uint8_t isSubDir(void) const {
|
|
||||||
return type_ == FAT_FILE_TYPE_SUBDIR;
|
|
||||||
}
|
|
||||||
/** \return True if this is a SdFile for the root directory. */
|
|
||||||
uint8_t isRoot(void) const {
|
|
||||||
return type_ == FAT_FILE_TYPE_ROOT16 || type_ == FAT_FILE_TYPE_ROOT32;
|
|
||||||
}
|
|
||||||
void ls(uint8_t flags = 0, uint8_t indent = 0);
|
|
||||||
uint8_t makeDir(SdFile* dir, const char* dirName);
|
|
||||||
uint8_t open(SdFile* dirFile, uint16_t index, uint8_t oflag);
|
|
||||||
uint8_t open(SdFile* dirFile, const char* fileName, uint8_t oflag);
|
|
||||||
|
|
||||||
uint8_t openRoot(SdVolume* vol);
|
|
||||||
static void printDirName(const dir_t& dir, uint8_t width);
|
|
||||||
static void printFatDate(uint16_t fatDate);
|
|
||||||
static void printFatTime(uint16_t fatTime);
|
|
||||||
static void printTwoDigits(uint8_t v);
|
|
||||||
/**
|
|
||||||
Read the next byte from a file.
|
|
||||||
|
|
||||||
\return For success read returns the next byte in the file as an int.
|
|
||||||
If an error occurs or end of file is reached -1 is returned.
|
|
||||||
*/
|
|
||||||
int16_t read(void) {
|
|
||||||
uint8_t b;
|
|
||||||
return read(&b, 1) == 1 ? b : -1;
|
|
||||||
}
|
|
||||||
int16_t read(void* buf, uint16_t nbyte);
|
|
||||||
int8_t readDir(dir_t* dir);
|
|
||||||
static uint8_t remove(SdFile* dirFile, const char* fileName);
|
|
||||||
uint8_t remove(void);
|
|
||||||
/** Set the file's current position to zero. */
|
|
||||||
void rewind(void) {
|
|
||||||
curPosition_ = curCluster_ = 0;
|
|
||||||
}
|
|
||||||
uint8_t rmDir(void);
|
|
||||||
uint8_t rmRfStar(void);
|
|
||||||
/** Set the files position to current position + \a pos. See seekSet(). */
|
|
||||||
uint8_t seekCur(uint32_t pos) {
|
|
||||||
return seekSet(curPosition_ + pos);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
Set the files current position to end of file. Useful to position
|
|
||||||
a file for append. See seekSet().
|
|
||||||
*/
|
|
||||||
uint8_t seekEnd(void) {
|
|
||||||
return seekSet(fileSize_);
|
|
||||||
}
|
|
||||||
uint8_t seekSet(uint32_t pos);
|
|
||||||
/**
|
|
||||||
Use unbuffered reads to access this file. Used with Wave
|
|
||||||
Shield ISR. Used with Sd2Card::partialBlockRead() in WaveRP.
|
|
||||||
|
|
||||||
Not recommended for normal applications.
|
|
||||||
*/
|
|
||||||
void setUnbufferedRead(void) {
|
|
||||||
if (isFile()) {
|
|
||||||
flags_ |= F_FILE_UNBUFFERED_READ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
uint8_t timestamp(uint8_t flag, uint16_t year, uint8_t month, uint8_t day,
|
|
||||||
uint8_t hour, uint8_t minute, uint8_t second);
|
|
||||||
uint8_t sync(uint8_t blocking = 1);
|
|
||||||
/** Type of this SdFile. You should use isFile() or isDir() instead of type()
|
|
||||||
if possible.
|
|
||||||
|
|
||||||
\return The file or directory type.
|
|
||||||
*/
|
|
||||||
uint8_t type(void) const {
|
|
||||||
return type_;
|
|
||||||
}
|
|
||||||
uint8_t truncate(uint32_t size);
|
|
||||||
/** \return Unbuffered read flag. */
|
|
||||||
uint8_t unbufferedRead(void) const {
|
|
||||||
return flags_ & F_FILE_UNBUFFERED_READ;
|
|
||||||
}
|
|
||||||
/** \return SdVolume that contains this file. */
|
|
||||||
SdVolume* volume(void) const {
|
|
||||||
return vol_;
|
|
||||||
}
|
|
||||||
size_t write(uint8_t b);
|
|
||||||
size_t write(const void* buf, uint16_t nbyte);
|
|
||||||
size_t write(const char* str);
|
|
||||||
#ifdef __AVR__
|
|
||||||
void write_P(PGM_P str);
|
|
||||||
void writeln_P(PGM_P str);
|
|
||||||
#endif
|
|
||||||
int availableForWrite(void);
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#if ALLOW_DEPRECATED_FUNCTIONS
|
|
||||||
// Deprecated functions - suppress cpplint warnings with NOLINT comment
|
|
||||||
/** \deprecated Use:
|
|
||||||
uint8_t SdFile::contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
|
|
||||||
*/
|
|
||||||
uint8_t contiguousRange(uint32_t& bgnBlock, uint32_t& endBlock) { // NOLINT
|
|
||||||
return contiguousRange(&bgnBlock, &endBlock);
|
|
||||||
}
|
|
||||||
/** \deprecated Use:
|
|
||||||
uint8_t SdFile::createContiguous(SdFile* dirFile,
|
|
||||||
const char* fileName, uint32_t size)
|
|
||||||
*/
|
|
||||||
uint8_t createContiguous(SdFile& dirFile, // NOLINT
|
|
||||||
const char* fileName, uint32_t size) {
|
|
||||||
return createContiguous(&dirFile, fileName, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
\deprecated Use:
|
|
||||||
static void SdFile::dateTimeCallback(
|
|
||||||
void (*dateTime)(uint16_t* date, uint16_t* time));
|
|
||||||
*/
|
|
||||||
static void dateTimeCallback(
|
|
||||||
void (*dateTime)(uint16_t& date, uint16_t& time)) { // NOLINT
|
|
||||||
oldDateTime_ = dateTime;
|
|
||||||
dateTime_ = dateTime ? oldToNew : 0;
|
|
||||||
}
|
|
||||||
/** \deprecated Use: uint8_t SdFile::dirEntry(dir_t* dir); */
|
|
||||||
uint8_t dirEntry(dir_t& dir) {
|
|
||||||
return dirEntry(&dir); // NOLINT
|
|
||||||
}
|
|
||||||
/** \deprecated Use:
|
|
||||||
uint8_t SdFile::makeDir(SdFile* dir, const char* dirName);
|
|
||||||
*/
|
|
||||||
uint8_t makeDir(SdFile& dir, const char* dirName) { // NOLINT
|
|
||||||
return makeDir(&dir, dirName);
|
|
||||||
}
|
|
||||||
/** \deprecated Use:
|
|
||||||
uint8_t SdFile::open(SdFile* dirFile, const char* fileName, uint8_t oflag);
|
|
||||||
*/
|
|
||||||
uint8_t open(SdFile& dirFile, // NOLINT
|
|
||||||
const char* fileName, uint8_t oflag) {
|
|
||||||
return open(&dirFile, fileName, oflag);
|
|
||||||
}
|
|
||||||
/** \deprecated Do not use in new apps */
|
|
||||||
uint8_t open(SdFile& dirFile, const char* fileName) { // NOLINT
|
|
||||||
return open(dirFile, fileName, O_RDWR);
|
|
||||||
}
|
|
||||||
/** \deprecated Use:
|
|
||||||
uint8_t SdFile::open(SdFile* dirFile, uint16_t index, uint8_t oflag);
|
|
||||||
*/
|
|
||||||
uint8_t open(SdFile& dirFile, uint16_t index, uint8_t oflag) { // NOLINT
|
|
||||||
return open(&dirFile, index, oflag);
|
|
||||||
}
|
|
||||||
/** \deprecated Use: uint8_t SdFile::openRoot(SdVolume* vol); */
|
|
||||||
uint8_t openRoot(SdVolume& vol) {
|
|
||||||
return openRoot(&vol); // NOLINT
|
|
||||||
}
|
|
||||||
|
|
||||||
/** \deprecated Use: int8_t SdFile::readDir(dir_t* dir); */
|
|
||||||
int8_t readDir(dir_t& dir) {
|
|
||||||
return readDir(&dir); // NOLINT
|
|
||||||
}
|
|
||||||
/** \deprecated Use:
|
|
||||||
static uint8_t SdFile::remove(SdFile* dirFile, const char* fileName);
|
|
||||||
*/
|
|
||||||
static uint8_t remove(SdFile& dirFile, const char* fileName) { // NOLINT
|
|
||||||
return remove(&dirFile, fileName);
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// rest are private
|
|
||||||
private:
|
|
||||||
static void (*oldDateTime_)(uint16_t& date, uint16_t& time); // NOLINT
|
|
||||||
static void oldToNew(uint16_t* date, uint16_t* time) {
|
|
||||||
uint16_t d;
|
|
||||||
uint16_t t;
|
|
||||||
oldDateTime_(d, t);
|
|
||||||
*date = d;
|
|
||||||
*time = t;
|
|
||||||
}
|
|
||||||
#endif // ALLOW_DEPRECATED_FUNCTIONS
|
|
||||||
private:
|
|
||||||
// bits defined in flags_
|
|
||||||
// should be 0XF
|
|
||||||
static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC);
|
|
||||||
// available bits
|
|
||||||
static uint8_t const F_FILE_NON_BLOCKING_WRITE = 0X10;
|
|
||||||
// a new cluster was added to the file
|
|
||||||
static uint8_t const F_FILE_CLUSTER_ADDED = 0X20;
|
|
||||||
// use unbuffered SD read
|
|
||||||
static uint8_t const F_FILE_UNBUFFERED_READ = 0X40;
|
|
||||||
// sync of directory entry required
|
|
||||||
static uint8_t const F_FILE_DIR_DIRTY = 0X80;
|
|
||||||
|
|
||||||
// make sure F_OFLAG is ok
|
|
||||||
#if ((F_FILE_NON_BLOCKING_WRITE | F_FILE_CLUSTER_ADDED | F_FILE_UNBUFFERED_READ | F_FILE_DIR_DIRTY) & F_OFLAG)
|
|
||||||
#error flags_ bits conflict
|
|
||||||
#endif // flags_ bits
|
|
||||||
|
|
||||||
// private data
|
|
||||||
uint8_t flags_; // See above for definition of flags_ bits
|
|
||||||
uint8_t type_; // type of file see above for values
|
|
||||||
uint32_t curCluster_; // cluster for current file position
|
|
||||||
uint32_t curPosition_; // current file position in bytes from beginning
|
|
||||||
uint32_t dirBlock_; // SD block that contains directory entry for file
|
|
||||||
uint8_t dirIndex_; // index of entry in dirBlock 0 <= dirIndex_ <= 0XF
|
|
||||||
uint32_t fileSize_; // file size in bytes
|
|
||||||
uint32_t firstCluster_; // first cluster of file
|
|
||||||
SdVolume* vol_; // volume where file is located
|
|
||||||
|
|
||||||
// private functions
|
|
||||||
uint8_t addCluster(void);
|
|
||||||
uint8_t addDirCluster(void);
|
|
||||||
dir_t* cacheDirEntry(uint8_t action);
|
|
||||||
static void (*dateTime_)(uint16_t* date, uint16_t* time);
|
|
||||||
static uint8_t make83Name(const char* str, uint8_t* name);
|
|
||||||
uint8_t openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
|
|
||||||
dir_t* readDirCache(void);
|
|
||||||
};
|
|
||||||
//==============================================================================
|
|
||||||
// SdVolume class
|
|
||||||
/**
|
|
||||||
\brief Cache for an SD data block
|
|
||||||
*/
|
|
||||||
union cache_t {
|
|
||||||
/** Used to access cached file data blocks. */
|
|
||||||
uint8_t data[512];
|
|
||||||
/** Used to access cached FAT16 entries. */
|
|
||||||
uint16_t fat16[256];
|
|
||||||
/** Used to access cached FAT32 entries. */
|
|
||||||
uint32_t fat32[128];
|
|
||||||
/** Used to access cached directory entries. */
|
|
||||||
dir_t dir[16];
|
|
||||||
/** Used to access a cached MasterBoot Record. */
|
|
||||||
mbr_t mbr;
|
|
||||||
/** Used to access to a cached FAT boot sector. */
|
|
||||||
fbs_t fbs;
|
|
||||||
};
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
\class SdVolume
|
|
||||||
\brief Access FAT16 and FAT32 volumes on SD and SDHC cards.
|
|
||||||
*/
|
|
||||||
class SdVolume {
|
|
||||||
public:
|
|
||||||
/** Create an instance of SdVolume */
|
|
||||||
SdVolume(void) : allocSearchStart_(2), fatType_(0) {}
|
|
||||||
/** Clear the cache and returns a pointer to the cache. Used by the WaveRP
|
|
||||||
recorder to do raw write to the SD card. Not for normal apps.
|
|
||||||
*/
|
|
||||||
static uint8_t* cacheClear(void) {
|
|
||||||
cacheFlush();
|
|
||||||
cacheBlockNumber_ = 0XFFFFFFFF;
|
|
||||||
return cacheBuffer_.data;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
Initialize a FAT volume. Try partition one first then try super
|
|
||||||
floppy format.
|
|
||||||
|
|
||||||
\param[in] dev The Sd2Card where the volume is located.
|
|
||||||
|
|
||||||
\return The value one, true, is returned for success and
|
|
||||||
the value zero, false, is returned for failure. Reasons for
|
|
||||||
failure include not finding a valid partition, not finding a valid
|
|
||||||
FAT file system or an I/O error.
|
|
||||||
*/
|
|
||||||
uint8_t init(Sd2Card* dev) {
|
|
||||||
return init(dev, 1) ? true : init(dev, 0);
|
|
||||||
}
|
|
||||||
uint8_t init(Sd2Card* dev, uint8_t part);
|
|
||||||
|
|
||||||
// inline functions that return volume info
|
|
||||||
/** \return The volume's cluster size in blocks. */
|
|
||||||
uint8_t blocksPerCluster(void) const {
|
|
||||||
return blocksPerCluster_;
|
|
||||||
}
|
|
||||||
/** \return The number of blocks in one FAT. */
|
|
||||||
uint32_t blocksPerFat(void) const {
|
|
||||||
return blocksPerFat_;
|
|
||||||
}
|
|
||||||
/** \return The total number of clusters in the volume. */
|
|
||||||
uint32_t clusterCount(void) const {
|
|
||||||
return clusterCount_;
|
|
||||||
}
|
|
||||||
/** \return The shift count required to multiply by blocksPerCluster. */
|
|
||||||
uint8_t clusterSizeShift(void) const {
|
|
||||||
return clusterSizeShift_;
|
|
||||||
}
|
|
||||||
/** \return The logical block number for the start of file data. */
|
|
||||||
uint32_t dataStartBlock(void) const {
|
|
||||||
return dataStartBlock_;
|
|
||||||
}
|
|
||||||
/** \return The number of FAT structures on the volume. */
|
|
||||||
uint8_t fatCount(void) const {
|
|
||||||
return fatCount_;
|
|
||||||
}
|
|
||||||
/** \return The logical block number for the start of the first FAT. */
|
|
||||||
uint32_t fatStartBlock(void) const {
|
|
||||||
return fatStartBlock_;
|
|
||||||
}
|
|
||||||
/** \return The FAT type of the volume. Values are 12, 16 or 32. */
|
|
||||||
uint8_t fatType(void) const {
|
|
||||||
return fatType_;
|
|
||||||
}
|
|
||||||
/** \return The number of entries in the root directory for FAT16 volumes. */
|
|
||||||
uint32_t rootDirEntryCount(void) const {
|
|
||||||
return rootDirEntryCount_;
|
|
||||||
}
|
|
||||||
/** \return The logical block number for the start of the root directory
|
|
||||||
on FAT16 volumes or the first cluster number on FAT32 volumes. */
|
|
||||||
uint32_t rootDirStart(void) const {
|
|
||||||
return rootDirStart_;
|
|
||||||
}
|
|
||||||
/** return a pointer to the Sd2Card object for this volume */
|
|
||||||
static Sd2Card* sdCard(void) {
|
|
||||||
return sdCard_;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#if ALLOW_DEPRECATED_FUNCTIONS
|
|
||||||
// Deprecated functions - suppress cpplint warnings with NOLINT comment
|
|
||||||
/** \deprecated Use: uint8_t SdVolume::init(Sd2Card* dev); */
|
|
||||||
uint8_t init(Sd2Card& dev) {
|
|
||||||
return init(&dev); // NOLINT
|
|
||||||
}
|
|
||||||
|
|
||||||
/** \deprecated Use: uint8_t SdVolume::init(Sd2Card* dev, uint8_t vol); */
|
|
||||||
uint8_t init(Sd2Card& dev, uint8_t part) { // NOLINT
|
|
||||||
return init(&dev, part);
|
|
||||||
}
|
|
||||||
#endif // ALLOW_DEPRECATED_FUNCTIONS
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
private:
|
|
||||||
// Allow SdFile access to SdVolume private data.
|
|
||||||
friend class SdFile;
|
|
||||||
|
|
||||||
// value for action argument in cacheRawBlock to indicate read from cache
|
|
||||||
static uint8_t const CACHE_FOR_READ = 0;
|
|
||||||
// value for action argument in cacheRawBlock to indicate cache dirty
|
|
||||||
static uint8_t const CACHE_FOR_WRITE = 1;
|
|
||||||
|
|
||||||
static cache_t cacheBuffer_; // 512 byte cache for device blocks
|
|
||||||
static uint32_t cacheBlockNumber_; // Logical number of block in the cache
|
|
||||||
static Sd2Card* sdCard_; // Sd2Card object for cache
|
|
||||||
static uint8_t cacheDirty_; // cacheFlush() will write block if true
|
|
||||||
static uint32_t cacheMirrorBlock_; // block number for mirror FAT
|
|
||||||
//
|
|
||||||
uint32_t allocSearchStart_; // start cluster for alloc search
|
|
||||||
uint8_t blocksPerCluster_; // cluster size in blocks
|
|
||||||
uint32_t blocksPerFat_; // FAT size in blocks
|
|
||||||
uint32_t clusterCount_; // clusters in one FAT
|
|
||||||
uint8_t clusterSizeShift_; // shift to convert cluster count to block count
|
|
||||||
uint32_t dataStartBlock_; // first data block number
|
|
||||||
uint8_t fatCount_; // number of FATs on volume
|
|
||||||
uint32_t fatStartBlock_; // start block for first FAT
|
|
||||||
uint8_t fatType_; // volume type (12, 16, OR 32)
|
|
||||||
uint16_t rootDirEntryCount_; // number of entries in FAT16 root dir
|
|
||||||
uint32_t rootDirStart_; // root start block for FAT16, cluster for FAT32
|
|
||||||
//----------------------------------------------------------------------------
|
|
||||||
uint8_t allocContiguous(uint32_t count, uint32_t* curCluster);
|
|
||||||
uint8_t blockOfCluster(uint32_t position) const {
|
|
||||||
return (position >> 9) & (blocksPerCluster_ - 1);
|
|
||||||
}
|
|
||||||
uint32_t clusterStartBlock(uint32_t cluster) const {
|
|
||||||
return dataStartBlock_ + ((cluster - 2) << clusterSizeShift_);
|
|
||||||
}
|
|
||||||
uint32_t blockNumber(uint32_t cluster, uint32_t position) const {
|
|
||||||
return clusterStartBlock(cluster) + blockOfCluster(position);
|
|
||||||
}
|
|
||||||
static uint8_t cacheFlush(uint8_t blocking = 1);
|
|
||||||
static uint8_t cacheMirrorBlockFlush(uint8_t blocking);
|
|
||||||
static uint8_t cacheRawBlock(uint32_t blockNumber, uint8_t action);
|
|
||||||
static void cacheSetDirty(void) {
|
|
||||||
cacheDirty_ |= CACHE_FOR_WRITE;
|
|
||||||
}
|
|
||||||
static uint8_t cacheZeroBlock(uint32_t blockNumber);
|
|
||||||
uint8_t chainSize(uint32_t beginCluster, uint32_t* size) const;
|
|
||||||
uint8_t fatGet(uint32_t cluster, uint32_t* value) const;
|
|
||||||
uint8_t fatPut(uint32_t cluster, uint32_t value);
|
|
||||||
uint8_t fatPutEOC(uint32_t cluster) {
|
|
||||||
return fatPut(cluster, 0x0FFFFFFF);
|
|
||||||
}
|
|
||||||
uint8_t freeChain(uint32_t cluster);
|
|
||||||
uint8_t isEOC(uint32_t cluster) const {
|
|
||||||
return cluster >= (fatType_ == 16 ? FAT16EOC_MIN : FAT32EOC_MIN);
|
|
||||||
}
|
|
||||||
uint8_t readBlock(uint32_t block, uint8_t* dst) {
|
|
||||||
return sdCard_->readBlock(block, dst);
|
|
||||||
}
|
|
||||||
uint8_t readData(uint32_t block, uint16_t offset,
|
|
||||||
uint16_t count, uint8_t* dst) {
|
|
||||||
return sdCard_->readData(block, offset, count, dst);
|
|
||||||
}
|
|
||||||
uint8_t writeBlock(uint32_t block, const uint8_t* dst, uint8_t blocking = 1) {
|
|
||||||
return sdCard_->writeBlock(block, dst, blocking);
|
|
||||||
}
|
|
||||||
uint8_t isBusy(void) {
|
|
||||||
return sdCard_->isBusy();
|
|
||||||
}
|
|
||||||
uint8_t isCacheMirrorBlockDirty(void) {
|
|
||||||
return (cacheMirrorBlock_ != 0);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
#endif // SdFat_h
|
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
/* Arduino SdFat Library
|
|
||||||
Copyright (C) 2008 by William Greiman
|
|
||||||
|
|
||||||
This file is part of the Arduino SdFat Library
|
|
||||||
|
|
||||||
This Library is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This Library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with the Arduino SdFat Library. If not, see
|
|
||||||
<http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
#ifndef SdFatUtil_h
|
|
||||||
#define SdFatUtil_h
|
|
||||||
/**
|
|
||||||
\file
|
|
||||||
Useful utility functions.
|
|
||||||
*/
|
|
||||||
#include <Arduino.h>
|
|
||||||
#ifdef __AVR__
|
|
||||||
#include <avr/pgmspace.h>
|
|
||||||
/** Store and print a string in flash memory.*/
|
|
||||||
#define PgmPrint(x) SerialPrint_P(PSTR(x))
|
|
||||||
/** Store and print a string in flash memory followed by a CR/LF.*/
|
|
||||||
#define PgmPrintln(x) SerialPrintln_P(PSTR(x))
|
|
||||||
/** Defined so doxygen works for function definitions. */
|
|
||||||
#endif
|
|
||||||
#define NOINLINE __attribute__((noinline,unused))
|
|
||||||
#define UNUSEDOK __attribute__((unused))
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** Return the number of bytes currently free in RAM. */
|
|
||||||
static UNUSEDOK int FreeRam(void) {
|
|
||||||
extern int __bss_end;
|
|
||||||
extern int* __brkval;
|
|
||||||
int free_memory;
|
|
||||||
if (reinterpret_cast<int>(__brkval) == 0) {
|
|
||||||
// if no heap use from end of bss section
|
|
||||||
free_memory = reinterpret_cast<int>(&free_memory)
|
|
||||||
- reinterpret_cast<int>(&__bss_end);
|
|
||||||
} else {
|
|
||||||
// use from top of stack to heap
|
|
||||||
free_memory = reinterpret_cast<int>(&free_memory)
|
|
||||||
- reinterpret_cast<int>(__brkval);
|
|
||||||
}
|
|
||||||
return free_memory;
|
|
||||||
}
|
|
||||||
#ifdef __AVR__
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
%Print a string in flash memory to the serial port.
|
|
||||||
|
|
||||||
\param[in] str Pointer to string stored in flash memory.
|
|
||||||
*/
|
|
||||||
static NOINLINE void SerialPrint_P(PGM_P str) {
|
|
||||||
for (uint8_t c; (c = pgm_read_byte(str)); str++) {
|
|
||||||
Serial.write(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
%Print a string in flash memory followed by a CR/LF.
|
|
||||||
|
|
||||||
\param[in] str Pointer to string stored in flash memory.
|
|
||||||
*/
|
|
||||||
static NOINLINE void SerialPrintln_P(PGM_P str) {
|
|
||||||
SerialPrint_P(str);
|
|
||||||
Serial.println();
|
|
||||||
}
|
|
||||||
#endif // __AVR__
|
|
||||||
#endif // #define SdFatUtil_h
|
|
||||||
@@ -1,202 +0,0 @@
|
|||||||
/* Arduino SdFat Library
|
|
||||||
Copyright (C) 2009 by William Greiman
|
|
||||||
|
|
||||||
This file is part of the Arduino SdFat Library
|
|
||||||
|
|
||||||
This Library is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This Library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with the Arduino SdFat Library. If not, see
|
|
||||||
<http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
\mainpage Arduino SdFat Library
|
|
||||||
<CENTER>Copyright © 2009 by William Greiman
|
|
||||||
</CENTER>
|
|
||||||
|
|
||||||
\section Intro Introduction
|
|
||||||
The Arduino SdFat Library is a minimal implementation of FAT16 and FAT32
|
|
||||||
file systems on SD flash memory cards. Standard SD and high capacity
|
|
||||||
SDHC cards are supported.
|
|
||||||
|
|
||||||
The SdFat only supports short 8.3 names.
|
|
||||||
|
|
||||||
The main classes in SdFat are Sd2Card, SdVolume, and SdFile.
|
|
||||||
|
|
||||||
The Sd2Card class supports access to standard SD cards and SDHC cards. Most
|
|
||||||
applications will only need to call the Sd2Card::init() member function.
|
|
||||||
|
|
||||||
The SdVolume class supports FAT16 and FAT32 partitions. Most applications
|
|
||||||
will only need to call the SdVolume::init() member function.
|
|
||||||
|
|
||||||
The SdFile class provides file access functions such as open(), read(),
|
|
||||||
remove(), write(), close() and sync(). This class supports access to the root
|
|
||||||
directory and subdirectories.
|
|
||||||
|
|
||||||
A number of example are provided in the SdFat/examples folder. These were
|
|
||||||
developed to test SdFat and illustrate its use.
|
|
||||||
|
|
||||||
SdFat was developed for high speed data recording. SdFat was used to implement
|
|
||||||
an audio record/play class, WaveRP, for the Adafruit Wave Shield. This
|
|
||||||
application uses special Sd2Card calls to write to contiguous files in raw mode.
|
|
||||||
These functions reduce write latency so that audio can be recorded with the
|
|
||||||
small amount of RAM in the Arduino.
|
|
||||||
|
|
||||||
\section SDcard SD\SDHC Cards
|
|
||||||
|
|
||||||
Arduinos access SD cards using the cards SPI protocol. PCs, Macs, and
|
|
||||||
most consumer devices use the 4-bit parallel SD protocol. A card that
|
|
||||||
functions well on A PC or Mac may not work well on the Arduino.
|
|
||||||
|
|
||||||
Most cards have good SPI read performance but cards vary widely in SPI
|
|
||||||
write performance. Write performance is limited by how efficiently the
|
|
||||||
card manages internal erase/remapping operations. The Arduino cannot
|
|
||||||
optimize writes to reduce erase operations because of its limit RAM.
|
|
||||||
|
|
||||||
SanDisk cards generally have good write performance. They seem to have
|
|
||||||
more internal RAM buffering than other cards and therefore can limit
|
|
||||||
the number of flash erase operations that the Arduino forces due to its
|
|
||||||
limited RAM.
|
|
||||||
|
|
||||||
\section Hardware Hardware Configuration
|
|
||||||
|
|
||||||
SdFat was developed using an
|
|
||||||
<A HREF = "http://www.adafruit.com/"> Adafruit Industries</A>
|
|
||||||
<A HREF = "http://www.ladyada.net/make/waveshield/"> Wave Shield</A>.
|
|
||||||
|
|
||||||
The hardware interface to the SD card should not use a resistor based level
|
|
||||||
shifter. SdFat sets the SPI bus frequency to 8 MHz which results in signal
|
|
||||||
rise times that are too slow for the edge detectors in many newer SD card
|
|
||||||
controllers when resistor voltage dividers are used.
|
|
||||||
|
|
||||||
The 5 to 3.3 V level shifter for 5 V Arduinos should be IC based like the
|
|
||||||
74HC4050N based circuit shown in the file SdLevel.png. The Adafruit Wave Shield
|
|
||||||
uses a 74AHC125N. Gravitech sells SD and MicroSD Card Adapters based on the
|
|
||||||
74LCX245.
|
|
||||||
|
|
||||||
If you are using a resistor based level shifter and are having problems try
|
|
||||||
setting the SPI bus frequency to 4 MHz. This can be done by using
|
|
||||||
card.init(SPI_HALF_SPEED) to initialize the SD card.
|
|
||||||
|
|
||||||
\section comment Bugs and Comments
|
|
||||||
|
|
||||||
If you wish to report bugs or have comments, send email to fat16lib@sbcglobal.net.
|
|
||||||
|
|
||||||
\section SdFatClass SdFat Usage
|
|
||||||
|
|
||||||
SdFat uses a slightly restricted form of short names.
|
|
||||||
Only printable ASCII characters are supported. No characters with code point
|
|
||||||
values greater than 127 are allowed. Space is not allowed even though space
|
|
||||||
was allowed in the API of early versions of DOS.
|
|
||||||
|
|
||||||
Short names are limited to 8 characters followed by an optional period (.)
|
|
||||||
and extension of up to 3 characters. The characters may be any combination
|
|
||||||
of letters and digits. The following special characters are also allowed:
|
|
||||||
|
|
||||||
$ % ' - _ @ ~ ` ! ( ) { } ^ # &
|
|
||||||
|
|
||||||
Short names are always converted to upper case and their original case
|
|
||||||
value is lost.
|
|
||||||
|
|
||||||
\note
|
|
||||||
The Arduino Print class uses character
|
|
||||||
at a time writes so it was necessary to use a \link SdFile::sync() sync() \endlink
|
|
||||||
function to control when data is written to the SD card.
|
|
||||||
|
|
||||||
\par
|
|
||||||
An application which writes to a file using \link Print::print() print()\endlink,
|
|
||||||
\link Print::println() println() \endlink
|
|
||||||
or \link SdFile::write write() \endlink must call \link SdFile::sync() sync() \endlink
|
|
||||||
at the appropriate time to force data and directory information to be written
|
|
||||||
to the SD Card. Data and directory information are also written to the SD card
|
|
||||||
when \link SdFile::close() close() \endlink is called.
|
|
||||||
|
|
||||||
\par
|
|
||||||
Applications must use care calling \link SdFile::sync() sync() \endlink
|
|
||||||
since 2048 bytes of I/O is required to update file and
|
|
||||||
directory information. This includes writing the current data block, reading
|
|
||||||
the block that contains the directory entry for update, writing the directory
|
|
||||||
block back and reading back the current data block.
|
|
||||||
|
|
||||||
It is possible to open a file with two or more instances of SdFile. A file may
|
|
||||||
be corrupted if data is written to the file by more than one instance of SdFile.
|
|
||||||
|
|
||||||
\section HowTo How to format SD Cards as FAT Volumes
|
|
||||||
|
|
||||||
You should use a freshly formatted SD card for best performance. FAT
|
|
||||||
file systems become slower if many files have been created and deleted.
|
|
||||||
This is because the directory entry for a deleted file is marked as deleted,
|
|
||||||
but is not deleted. When a new file is created, these entries must be scanned
|
|
||||||
before creating the file, a flaw in the FAT design. Also files can become
|
|
||||||
fragmented which causes reads and writes to be slower.
|
|
||||||
|
|
||||||
Microsoft operating systems support removable media formatted with a
|
|
||||||
Master Boot Record, MBR, or formatted as a super floppy with a FAT Boot Sector
|
|
||||||
in block zero.
|
|
||||||
|
|
||||||
Microsoft operating systems expect MBR formatted removable media
|
|
||||||
to have only one partition. The first partition should be used.
|
|
||||||
|
|
||||||
Microsoft operating systems do not support partitioning SD flash cards.
|
|
||||||
If you erase an SD card with a program like KillDisk, Most versions of
|
|
||||||
Windows will format the card as a super floppy.
|
|
||||||
|
|
||||||
The best way to restore an SD card's format is to use SDFormatter
|
|
||||||
which can be downloaded from:
|
|
||||||
|
|
||||||
http://www.sdcard.org/consumers/formatter/
|
|
||||||
|
|
||||||
SDFormatter aligns flash erase boundaries with file
|
|
||||||
system structures which reduces write latency and file system overhead.
|
|
||||||
|
|
||||||
SDFormatter does not have an option for FAT type so it may format
|
|
||||||
small cards as FAT12.
|
|
||||||
|
|
||||||
After the MBR is restored by SDFormatter you may need to reformat small
|
|
||||||
cards that have been formatted FAT12 to force the volume type to be FAT16.
|
|
||||||
|
|
||||||
If you reformat the SD card with an OS utility, choose a cluster size that
|
|
||||||
will result in:
|
|
||||||
|
|
||||||
4084 < CountOfClusters && CountOfClusters < 65525
|
|
||||||
|
|
||||||
The volume will then be FAT16.
|
|
||||||
|
|
||||||
If you are formatting an SD card on OS X or Linux, be sure to use the first
|
|
||||||
partition. Format this partition with a cluster count in above range.
|
|
||||||
|
|
||||||
\section References References
|
|
||||||
|
|
||||||
Adafruit Industries:
|
|
||||||
|
|
||||||
http://www.adafruit.com/
|
|
||||||
|
|
||||||
http://www.ladyada.net/make/waveshield/
|
|
||||||
|
|
||||||
The Arduino site:
|
|
||||||
|
|
||||||
http://www.arduino.cc/
|
|
||||||
|
|
||||||
For more information about FAT file systems see:
|
|
||||||
|
|
||||||
http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx
|
|
||||||
|
|
||||||
For information about using SD cards as SPI devices see:
|
|
||||||
|
|
||||||
http://www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
|
|
||||||
|
|
||||||
The ATmega328 datasheet:
|
|
||||||
|
|
||||||
http://www.atmel.com/dyn/resources/prod_documents/doc8161.pdf
|
|
||||||
|
|
||||||
|
|
||||||
*/
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,232 +0,0 @@
|
|||||||
/* Arduino Sd2Card Library
|
|
||||||
Copyright (C) 2009 by William Greiman
|
|
||||||
|
|
||||||
This file is part of the Arduino Sd2Card Library
|
|
||||||
|
|
||||||
This Library is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This Library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with the Arduino Sd2Card Library. If not, see
|
|
||||||
<http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
#ifndef SdInfo_h
|
|
||||||
#define SdInfo_h
|
|
||||||
#include <stdint.h>
|
|
||||||
// Based on the document:
|
|
||||||
//
|
|
||||||
// SD Specifications
|
|
||||||
// Part 1
|
|
||||||
// Physical Layer
|
|
||||||
// Simplified Specification
|
|
||||||
// Version 2.00
|
|
||||||
// September 25, 2006
|
|
||||||
//
|
|
||||||
// www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// SD card commands
|
|
||||||
/** GO_IDLE_STATE - init card in spi mode if CS low */
|
|
||||||
uint8_t const CMD0 = 0X00;
|
|
||||||
/** SEND_IF_COND - verify SD Memory Card interface operating condition.*/
|
|
||||||
uint8_t const CMD8 = 0X08;
|
|
||||||
/** SEND_CSD - read the Card Specific Data (CSD register) */
|
|
||||||
uint8_t const CMD9 = 0X09;
|
|
||||||
/** SEND_CID - read the card identification information (CID register) */
|
|
||||||
uint8_t const CMD10 = 0X0A;
|
|
||||||
/** SEND_STATUS - read the card status register */
|
|
||||||
uint8_t const CMD13 = 0X0D;
|
|
||||||
/** READ_BLOCK - read a single data block from the card */
|
|
||||||
uint8_t const CMD17 = 0X11;
|
|
||||||
/** WRITE_BLOCK - write a single data block to the card */
|
|
||||||
uint8_t const CMD24 = 0X18;
|
|
||||||
/** WRITE_MULTIPLE_BLOCK - write blocks of data until a STOP_TRANSMISSION */
|
|
||||||
uint8_t const CMD25 = 0X19;
|
|
||||||
/** ERASE_WR_BLK_START - sets the address of the first block to be erased */
|
|
||||||
uint8_t const CMD32 = 0X20;
|
|
||||||
/** ERASE_WR_BLK_END - sets the address of the last block of the continuous
|
|
||||||
range to be erased*/
|
|
||||||
uint8_t const CMD33 = 0X21;
|
|
||||||
/** ERASE - erase all previously selected blocks */
|
|
||||||
uint8_t const CMD38 = 0X26;
|
|
||||||
/** APP_CMD - escape for application specific command */
|
|
||||||
uint8_t const CMD55 = 0X37;
|
|
||||||
/** READ_OCR - read the OCR register of a card */
|
|
||||||
uint8_t const CMD58 = 0X3A;
|
|
||||||
/** SET_WR_BLK_ERASE_COUNT - Set the number of write blocks to be
|
|
||||||
pre-erased before writing */
|
|
||||||
uint8_t const ACMD23 = 0X17;
|
|
||||||
/** SD_SEND_OP_COMD - Sends host capacity support information and
|
|
||||||
activates the card's initialization process */
|
|
||||||
uint8_t const ACMD41 = 0X29;
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/** status for card in the ready state */
|
|
||||||
uint8_t const R1_READY_STATE = 0X00;
|
|
||||||
/** status for card in the idle state */
|
|
||||||
uint8_t const R1_IDLE_STATE = 0X01;
|
|
||||||
/** status bit for illegal command */
|
|
||||||
uint8_t const R1_ILLEGAL_COMMAND = 0X04;
|
|
||||||
/** start data token for read or write single block*/
|
|
||||||
uint8_t const DATA_START_BLOCK = 0XFE;
|
|
||||||
/** stop token for write multiple blocks*/
|
|
||||||
uint8_t const STOP_TRAN_TOKEN = 0XFD;
|
|
||||||
/** start data token for write multiple blocks*/
|
|
||||||
uint8_t const WRITE_MULTIPLE_TOKEN = 0XFC;
|
|
||||||
/** mask for data response tokens after a write block operation */
|
|
||||||
uint8_t const DATA_RES_MASK = 0X1F;
|
|
||||||
/** write data accepted token */
|
|
||||||
uint8_t const DATA_RES_ACCEPTED = 0X05;
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
typedef struct CID {
|
|
||||||
// byte 0
|
|
||||||
uint8_t mid; // Manufacturer ID
|
|
||||||
// byte 1-2
|
|
||||||
char oid[2]; // OEM/Application ID
|
|
||||||
// byte 3-7
|
|
||||||
char pnm[5]; // Product name
|
|
||||||
// byte 8
|
|
||||||
unsigned prv_m : 4; // Product revision n.m
|
|
||||||
unsigned prv_n : 4;
|
|
||||||
// byte 9-12
|
|
||||||
uint32_t psn; // Product serial number
|
|
||||||
// byte 13
|
|
||||||
unsigned mdt_year_high : 4; // Manufacturing date
|
|
||||||
unsigned reserved : 4;
|
|
||||||
// byte 14
|
|
||||||
unsigned mdt_month : 4;
|
|
||||||
unsigned mdt_year_low : 4;
|
|
||||||
// byte 15
|
|
||||||
unsigned always1 : 1;
|
|
||||||
unsigned crc : 7;
|
|
||||||
} cid_t;
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// CSD for version 1.00 cards
|
|
||||||
typedef struct CSDV1 {
|
|
||||||
// byte 0
|
|
||||||
unsigned reserved1 : 6;
|
|
||||||
unsigned csd_ver : 2;
|
|
||||||
// byte 1
|
|
||||||
uint8_t taac;
|
|
||||||
// byte 2
|
|
||||||
uint8_t nsac;
|
|
||||||
// byte 3
|
|
||||||
uint8_t tran_speed;
|
|
||||||
// byte 4
|
|
||||||
uint8_t ccc_high;
|
|
||||||
// byte 5
|
|
||||||
unsigned read_bl_len : 4;
|
|
||||||
unsigned ccc_low : 4;
|
|
||||||
// byte 6
|
|
||||||
unsigned c_size_high : 2;
|
|
||||||
unsigned reserved2 : 2;
|
|
||||||
unsigned dsr_imp : 1;
|
|
||||||
unsigned read_blk_misalign : 1;
|
|
||||||
unsigned write_blk_misalign : 1;
|
|
||||||
unsigned read_bl_partial : 1;
|
|
||||||
// byte 7
|
|
||||||
uint8_t c_size_mid;
|
|
||||||
// byte 8
|
|
||||||
unsigned vdd_r_curr_max : 3;
|
|
||||||
unsigned vdd_r_curr_min : 3;
|
|
||||||
unsigned c_size_low : 2;
|
|
||||||
// byte 9
|
|
||||||
unsigned c_size_mult_high : 2;
|
|
||||||
unsigned vdd_w_cur_max : 3;
|
|
||||||
unsigned vdd_w_curr_min : 3;
|
|
||||||
// byte 10
|
|
||||||
unsigned sector_size_high : 6;
|
|
||||||
unsigned erase_blk_en : 1;
|
|
||||||
unsigned c_size_mult_low : 1;
|
|
||||||
// byte 11
|
|
||||||
unsigned wp_grp_size : 7;
|
|
||||||
unsigned sector_size_low : 1;
|
|
||||||
// byte 12
|
|
||||||
unsigned write_bl_len_high : 2;
|
|
||||||
unsigned r2w_factor : 3;
|
|
||||||
unsigned reserved3 : 2;
|
|
||||||
unsigned wp_grp_enable : 1;
|
|
||||||
// byte 13
|
|
||||||
unsigned reserved4 : 5;
|
|
||||||
unsigned write_partial : 1;
|
|
||||||
unsigned write_bl_len_low : 2;
|
|
||||||
// byte 14
|
|
||||||
unsigned reserved5: 2;
|
|
||||||
unsigned file_format : 2;
|
|
||||||
unsigned tmp_write_protect : 1;
|
|
||||||
unsigned perm_write_protect : 1;
|
|
||||||
unsigned copy : 1;
|
|
||||||
unsigned file_format_grp : 1;
|
|
||||||
// byte 15
|
|
||||||
unsigned always1 : 1;
|
|
||||||
unsigned crc : 7;
|
|
||||||
} csd1_t;
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// CSD for version 2.00 cards
|
|
||||||
typedef struct CSDV2 {
|
|
||||||
// byte 0
|
|
||||||
unsigned reserved1 : 6;
|
|
||||||
unsigned csd_ver : 2;
|
|
||||||
// byte 1
|
|
||||||
uint8_t taac;
|
|
||||||
// byte 2
|
|
||||||
uint8_t nsac;
|
|
||||||
// byte 3
|
|
||||||
uint8_t tran_speed;
|
|
||||||
// byte 4
|
|
||||||
uint8_t ccc_high;
|
|
||||||
// byte 5
|
|
||||||
unsigned read_bl_len : 4;
|
|
||||||
unsigned ccc_low : 4;
|
|
||||||
// byte 6
|
|
||||||
unsigned reserved2 : 4;
|
|
||||||
unsigned dsr_imp : 1;
|
|
||||||
unsigned read_blk_misalign : 1;
|
|
||||||
unsigned write_blk_misalign : 1;
|
|
||||||
unsigned read_bl_partial : 1;
|
|
||||||
// byte 7
|
|
||||||
unsigned reserved3 : 2;
|
|
||||||
unsigned c_size_high : 6;
|
|
||||||
// byte 8
|
|
||||||
uint8_t c_size_mid;
|
|
||||||
// byte 9
|
|
||||||
uint8_t c_size_low;
|
|
||||||
// byte 10
|
|
||||||
unsigned sector_size_high : 6;
|
|
||||||
unsigned erase_blk_en : 1;
|
|
||||||
unsigned reserved4 : 1;
|
|
||||||
// byte 11
|
|
||||||
unsigned wp_grp_size : 7;
|
|
||||||
unsigned sector_size_low : 1;
|
|
||||||
// byte 12
|
|
||||||
unsigned write_bl_len_high : 2;
|
|
||||||
unsigned r2w_factor : 3;
|
|
||||||
unsigned reserved5 : 2;
|
|
||||||
unsigned wp_grp_enable : 1;
|
|
||||||
// byte 13
|
|
||||||
unsigned reserved6 : 5;
|
|
||||||
unsigned write_partial : 1;
|
|
||||||
unsigned write_bl_len_low : 2;
|
|
||||||
// byte 14
|
|
||||||
unsigned reserved7: 2;
|
|
||||||
unsigned file_format : 2;
|
|
||||||
unsigned tmp_write_protect : 1;
|
|
||||||
unsigned perm_write_protect : 1;
|
|
||||||
unsigned copy : 1;
|
|
||||||
unsigned file_format_grp : 1;
|
|
||||||
// byte 15
|
|
||||||
unsigned always1 : 1;
|
|
||||||
unsigned crc : 7;
|
|
||||||
} csd2_t;
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// union of old and new style CSD register
|
|
||||||
union csd_t {
|
|
||||||
csd1_t v1;
|
|
||||||
csd2_t v2;
|
|
||||||
};
|
|
||||||
#endif // SdInfo_h
|
|
||||||
@@ -1,351 +0,0 @@
|
|||||||
/* Arduino SdFat Library
|
|
||||||
Copyright (C) 2009 by William Greiman
|
|
||||||
|
|
||||||
This file is part of the Arduino SdFat Library
|
|
||||||
|
|
||||||
This Library is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This Library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with the Arduino SdFat Library. If not, see
|
|
||||||
<http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
#include "SdFat.h"
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// raw block cache
|
|
||||||
// init cacheBlockNumber_to invalid SD block number
|
|
||||||
uint32_t SdVolume::cacheBlockNumber_ = 0XFFFFFFFF;
|
|
||||||
cache_t SdVolume::cacheBuffer_; // 512 byte cache for Sd2Card
|
|
||||||
Sd2Card* SdVolume::sdCard_; // pointer to SD card object
|
|
||||||
uint8_t SdVolume::cacheDirty_ = 0; // cacheFlush() will write block if true
|
|
||||||
uint32_t SdVolume::cacheMirrorBlock_ = 0; // mirror block for second FAT
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// find a contiguous group of clusters
|
|
||||||
uint8_t SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
|
|
||||||
// start of group
|
|
||||||
uint32_t bgnCluster;
|
|
||||||
|
|
||||||
// flag to save place to start next search
|
|
||||||
uint8_t setStart;
|
|
||||||
|
|
||||||
// set search start cluster
|
|
||||||
if (*curCluster) {
|
|
||||||
// try to make file contiguous
|
|
||||||
bgnCluster = *curCluster + 1;
|
|
||||||
|
|
||||||
// don't save new start location
|
|
||||||
setStart = false;
|
|
||||||
} else {
|
|
||||||
// start at likely place for free cluster
|
|
||||||
bgnCluster = allocSearchStart_;
|
|
||||||
|
|
||||||
// save next search start if one cluster
|
|
||||||
setStart = 1 == count;
|
|
||||||
}
|
|
||||||
// end of group
|
|
||||||
uint32_t endCluster = bgnCluster;
|
|
||||||
|
|
||||||
// last cluster of FAT
|
|
||||||
uint32_t fatEnd = clusterCount_ + 1;
|
|
||||||
|
|
||||||
// search the FAT for free clusters
|
|
||||||
for (uint32_t n = 0;; n++, endCluster++) {
|
|
||||||
// can't find space checked all clusters
|
|
||||||
if (n >= clusterCount_) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// past end - start from beginning of FAT
|
|
||||||
if (endCluster > fatEnd) {
|
|
||||||
bgnCluster = endCluster = 2;
|
|
||||||
}
|
|
||||||
uint32_t f;
|
|
||||||
if (!fatGet(endCluster, &f)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (f != 0) {
|
|
||||||
// cluster in use try next cluster as bgnCluster
|
|
||||||
bgnCluster = endCluster + 1;
|
|
||||||
} else if ((endCluster - bgnCluster + 1) == count) {
|
|
||||||
// done - found space
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// mark end of chain
|
|
||||||
if (!fatPutEOC(endCluster)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// link clusters
|
|
||||||
while (endCluster > bgnCluster) {
|
|
||||||
if (!fatPut(endCluster - 1, endCluster)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
endCluster--;
|
|
||||||
}
|
|
||||||
if (*curCluster != 0) {
|
|
||||||
// connect chains
|
|
||||||
if (!fatPut(*curCluster, bgnCluster)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// return first cluster number to caller
|
|
||||||
*curCluster = bgnCluster;
|
|
||||||
|
|
||||||
// remember possible next free cluster
|
|
||||||
if (setStart) {
|
|
||||||
allocSearchStart_ = bgnCluster + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
uint8_t SdVolume::cacheFlush(uint8_t blocking) {
|
|
||||||
if (cacheDirty_) {
|
|
||||||
if (!sdCard_->writeBlock(cacheBlockNumber_, cacheBuffer_.data, blocking)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!blocking) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// mirror FAT tables
|
|
||||||
if (!cacheMirrorBlockFlush(blocking)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
cacheDirty_ = 0;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
uint8_t SdVolume::cacheMirrorBlockFlush(uint8_t blocking) {
|
|
||||||
if (cacheMirrorBlock_) {
|
|
||||||
if (!sdCard_->writeBlock(cacheMirrorBlock_, cacheBuffer_.data, blocking)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
cacheMirrorBlock_ = 0;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
uint8_t SdVolume::cacheRawBlock(uint32_t blockNumber, uint8_t action) {
|
|
||||||
if (cacheBlockNumber_ != blockNumber) {
|
|
||||||
if (!cacheFlush()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!sdCard_->readBlock(blockNumber, cacheBuffer_.data)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
cacheBlockNumber_ = blockNumber;
|
|
||||||
}
|
|
||||||
cacheDirty_ |= action;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// cache a zero block for blockNumber
|
|
||||||
uint8_t SdVolume::cacheZeroBlock(uint32_t blockNumber) {
|
|
||||||
if (!cacheFlush()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// loop take less flash than memset(cacheBuffer_.data, 0, 512);
|
|
||||||
for (uint16_t i = 0; i < 512; i++) {
|
|
||||||
cacheBuffer_.data[i] = 0;
|
|
||||||
}
|
|
||||||
cacheBlockNumber_ = blockNumber;
|
|
||||||
cacheSetDirty();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// return the size in bytes of a cluster chain
|
|
||||||
uint8_t SdVolume::chainSize(uint32_t cluster, uint32_t* size) const {
|
|
||||||
uint32_t s = 0;
|
|
||||||
do {
|
|
||||||
if (!fatGet(cluster, &cluster)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
s += 512UL << clusterSizeShift_;
|
|
||||||
} while (!isEOC(cluster));
|
|
||||||
*size = s;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// Fetch a FAT entry
|
|
||||||
uint8_t SdVolume::fatGet(uint32_t cluster, uint32_t* value) const {
|
|
||||||
if (cluster > (clusterCount_ + 1)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
uint32_t lba = fatStartBlock_;
|
|
||||||
lba += fatType_ == 16 ? cluster >> 8 : cluster >> 7;
|
|
||||||
if (lba != cacheBlockNumber_) {
|
|
||||||
if (!cacheRawBlock(lba, CACHE_FOR_READ)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (fatType_ == 16) {
|
|
||||||
*value = cacheBuffer_.fat16[cluster & 0XFF];
|
|
||||||
} else {
|
|
||||||
*value = cacheBuffer_.fat32[cluster & 0X7F] & FAT32MASK;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// Store a FAT entry
|
|
||||||
uint8_t SdVolume::fatPut(uint32_t cluster, uint32_t value) {
|
|
||||||
// error if reserved cluster
|
|
||||||
if (cluster < 2) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// error if not in FAT
|
|
||||||
if (cluster > (clusterCount_ + 1)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// calculate block address for entry
|
|
||||||
uint32_t lba = fatStartBlock_;
|
|
||||||
lba += fatType_ == 16 ? cluster >> 8 : cluster >> 7;
|
|
||||||
|
|
||||||
if (lba != cacheBlockNumber_) {
|
|
||||||
if (!cacheRawBlock(lba, CACHE_FOR_READ)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// store entry
|
|
||||||
if (fatType_ == 16) {
|
|
||||||
cacheBuffer_.fat16[cluster & 0XFF] = value;
|
|
||||||
} else {
|
|
||||||
cacheBuffer_.fat32[cluster & 0X7F] = value;
|
|
||||||
}
|
|
||||||
cacheSetDirty();
|
|
||||||
|
|
||||||
// mirror second FAT
|
|
||||||
if (fatCount_ > 1) {
|
|
||||||
cacheMirrorBlock_ = lba + blocksPerFat_;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
// free a cluster chain
|
|
||||||
uint8_t SdVolume::freeChain(uint32_t cluster) {
|
|
||||||
// clear free cluster location
|
|
||||||
allocSearchStart_ = 2;
|
|
||||||
|
|
||||||
do {
|
|
||||||
uint32_t next;
|
|
||||||
if (!fatGet(cluster, &next)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// free cluster
|
|
||||||
if (!fatPut(cluster, 0)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
cluster = next;
|
|
||||||
} while (!isEOC(cluster));
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
/**
|
|
||||||
Initialize a FAT volume.
|
|
||||||
|
|
||||||
\param[in] dev The SD card where the volume is located.
|
|
||||||
|
|
||||||
\param[in] part The partition to be used. Legal values for \a part are
|
|
||||||
1-4 to use the corresponding partition on a device formatted with
|
|
||||||
a MBR, Master Boot Record, or zero if the device is formatted as
|
|
||||||
a super floppy with the FAT boot sector in block zero.
|
|
||||||
|
|
||||||
\return The value one, true, is returned for success and
|
|
||||||
the value zero, false, is returned for failure. Reasons for
|
|
||||||
failure include not finding a valid partition, not finding a valid
|
|
||||||
FAT file system in the specified partition or an I/O error.
|
|
||||||
*/
|
|
||||||
uint8_t SdVolume::init(Sd2Card* dev, uint8_t part) {
|
|
||||||
uint32_t volumeStartBlock = 0;
|
|
||||||
sdCard_ = dev;
|
|
||||||
// if part == 0 assume super floppy with FAT boot sector in block zero
|
|
||||||
// if part > 0 assume mbr volume with partition table
|
|
||||||
if (part) {
|
|
||||||
if (part > 4) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
part_t* p = &cacheBuffer_.mbr.part[part - 1];
|
|
||||||
if ((p->boot & 0X7F) != 0 ||
|
|
||||||
p->totalSectors < 100 ||
|
|
||||||
p->firstSector == 0) {
|
|
||||||
// not a valid partition
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
volumeStartBlock = p->firstSector;
|
|
||||||
}
|
|
||||||
if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
bpb_t* bpb = &cacheBuffer_.fbs.bpb;
|
|
||||||
if (bpb->bytesPerSector != 512 ||
|
|
||||||
bpb->fatCount == 0 ||
|
|
||||||
bpb->reservedSectorCount == 0 ||
|
|
||||||
bpb->sectorsPerCluster == 0) {
|
|
||||||
// not valid FAT volume
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
fatCount_ = bpb->fatCount;
|
|
||||||
blocksPerCluster_ = bpb->sectorsPerCluster;
|
|
||||||
|
|
||||||
// determine shift that is same as multiply by blocksPerCluster_
|
|
||||||
clusterSizeShift_ = 0;
|
|
||||||
while (blocksPerCluster_ != (1 << clusterSizeShift_)) {
|
|
||||||
// error if not power of 2
|
|
||||||
if (clusterSizeShift_++ > 7) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
blocksPerFat_ = bpb->sectorsPerFat16 ?
|
|
||||||
bpb->sectorsPerFat16 : bpb->sectorsPerFat32;
|
|
||||||
|
|
||||||
fatStartBlock_ = volumeStartBlock + bpb->reservedSectorCount;
|
|
||||||
|
|
||||||
// count for FAT16 zero for FAT32
|
|
||||||
rootDirEntryCount_ = bpb->rootDirEntryCount;
|
|
||||||
|
|
||||||
// directory start for FAT16 dataStart for FAT32
|
|
||||||
rootDirStart_ = fatStartBlock_ + bpb->fatCount * blocksPerFat_;
|
|
||||||
|
|
||||||
// data start for FAT16 and FAT32
|
|
||||||
dataStartBlock_ = rootDirStart_ + ((32 * bpb->rootDirEntryCount + 511) / 512);
|
|
||||||
|
|
||||||
// total blocks for FAT16 or FAT32
|
|
||||||
uint32_t totalBlocks = bpb->totalSectors16 ?
|
|
||||||
bpb->totalSectors16 : bpb->totalSectors32;
|
|
||||||
// total data blocks
|
|
||||||
clusterCount_ = totalBlocks - (dataStartBlock_ - volumeStartBlock);
|
|
||||||
|
|
||||||
// divide by cluster size to get cluster count
|
|
||||||
clusterCount_ >>= clusterSizeShift_;
|
|
||||||
|
|
||||||
// FAT type is determined by cluster count
|
|
||||||
if (clusterCount_ < 4085) {
|
|
||||||
fatType_ = 12;
|
|
||||||
} else if (clusterCount_ < 65525) {
|
|
||||||
fatType_ = 16;
|
|
||||||
} else {
|
|
||||||
rootDirStart_ = bpb->fat32RootCluster;
|
|
||||||
fatType_ = 32;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
@@ -194,7 +194,7 @@ uint8_t SX1278FSK::setFSK()
|
|||||||
{
|
{
|
||||||
uint8_t state = 2;
|
uint8_t state = 2;
|
||||||
byte st0;
|
byte st0;
|
||||||
byte config1;
|
//byte config1;
|
||||||
|
|
||||||
#if (SX1278FSK_debug_mode > 1)
|
#if (SX1278FSK_debug_mode > 1)
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|||||||
+320
-10
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
/* DFM decoder functions */
|
/* DFM decoder functions */
|
||||||
#include "DFM.h"
|
#include "DFM.h"
|
||||||
#include "SX1278FSK.h"
|
#include "SX1278FSK.h"
|
||||||
@@ -12,11 +11,26 @@
|
|||||||
#define DFM_DBG(x)
|
#define DFM_DBG(x)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int DFM::setup(float frequency, int inv)
|
#define DFM_FRAMELEN 33
|
||||||
|
|
||||||
|
// single data structure, search restarts after decoder change
|
||||||
|
static struct st_dfmstat {
|
||||||
|
int idcnt0;
|
||||||
|
int idcnt1;
|
||||||
|
int lastfrid;
|
||||||
|
int lastfrcnt;
|
||||||
|
uint8_t start[50];
|
||||||
|
uint16_t dat[50*2];
|
||||||
|
uint8_t cnt[50*2];
|
||||||
|
uint8_t nameregok;
|
||||||
|
uint8_t nameregtop;
|
||||||
|
} dfmstate;
|
||||||
|
|
||||||
|
int DFM::setup(float frequency, int type)
|
||||||
{
|
{
|
||||||
inverse = inv;
|
stype = type;
|
||||||
#if DFM_DEBUG
|
#if DFM_DEBUG
|
||||||
Serial.printf("Setup sx1278 for DFM sonde (inv=%d)\n",inv);
|
Serial.printf("Setup sx1278 for DFM sonde (type=%d)\n", stype);
|
||||||
#endif
|
#endif
|
||||||
if(sx1278.ON()!=0) {
|
if(sx1278.ON()!=0) {
|
||||||
DFM_DBG(Serial.println("Setting SX1278 power on FAILED"));
|
DFM_DBG(Serial.println("Setting SX1278 power on FAILED"));
|
||||||
@@ -44,6 +58,10 @@ int DFM::setup(float frequency, int inv)
|
|||||||
DFM_DBG(Serial.printf("Setting RX bandwidth to %d Hz FAILED", sonde.config.dfm.rxbw));
|
DFM_DBG(Serial.printf("Setting RX bandwidth to %d Hz FAILED", sonde.config.dfm.rxbw));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(type == STYPE_DFM09 || type == STYPE_DFM06) {
|
||||||
|
// packet mode, old version, misses some frames because chip enables rx too late after
|
||||||
|
// one frame was recevied. TODO: check if this can be fixed by changing parameters
|
||||||
// Enable auto-AFC, auto-AGC, RX Trigger by preamble
|
// Enable auto-AFC, auto-AGC, RX Trigger by preamble
|
||||||
if(sx1278.setRxConf(0x1E)!=0) {
|
if(sx1278.setRxConf(0x1E)!=0) {
|
||||||
DFM_DBG(Serial.println("Setting RX Config FAILED"));
|
DFM_DBG(Serial.println("Setting RX Config FAILED"));
|
||||||
@@ -52,12 +70,13 @@ int DFM::setup(float frequency, int inv)
|
|||||||
// Set autostart_RX to 01, preamble 0, SYNC detect==on, syncsize=3 (==4 byte
|
// Set autostart_RX to 01, preamble 0, SYNC detect==on, syncsize=3 (==4 byte
|
||||||
//char header[] = "0110.0101 0110.0110 1010.0101 1010.1010";
|
//char header[] = "0110.0101 0110.0110 1010.0101 1010.1010";
|
||||||
|
|
||||||
const char *SYNC=inverse?"\x9A\x99\x5A\x55":"\x65\x66\xA5\xAA";
|
const char *SYNC=(stype==STYPE_DFM09)?"\x9A\x99\x5A\x55":"\x65\x66\xA5\xAA";
|
||||||
if(sx1278.setSyncConf(0x53, 4, (const uint8_t *)SYNC)!=0) {
|
if(sx1278.setSyncConf(0x53, 4, (const uint8_t *)SYNC)!=0) {
|
||||||
DFM_DBG(Serial.println("Setting SYNC Config FAILED"));
|
DFM_DBG(Serial.println("Setting SYNC Config FAILED"));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if(sx1278.setPreambleDetect(0xA8)!=0) {
|
//if(sx1278.setPreambleDetect(0xA8)!=0) {
|
||||||
|
if(sx1278.setPreambleDetect(0xAA)!=0) {
|
||||||
DFM_DBG(Serial.println("Setting PreambleDetect FAILED"));
|
DFM_DBG(Serial.println("Setting PreambleDetect FAILED"));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -68,11 +87,41 @@ int DFM::setup(float frequency, int inv)
|
|||||||
DFM_DBG(Serial.println("Setting Packet config FAILED"));
|
DFM_DBG(Serial.println("Setting Packet config FAILED"));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
sx1278.setPayloadLength(33); // Expect 33 bytes (7+13+13 bytes)
|
||||||
|
} else {
|
||||||
|
// continuous mode
|
||||||
|
// Enable auto-AFC, auto-AGC, RX Trigger by preamble ????
|
||||||
|
if(sx1278.setRxConf(0x1E)!=0) {
|
||||||
|
DFM_DBG(Serial.println("Setting RX Config FAILED"));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// working with continuous RX
|
||||||
|
const char *SYNC="\xAA\xAA";
|
||||||
|
if(sx1278.setSyncConf(0x70, 2, (const uint8_t *)SYNC)!=0) {
|
||||||
|
DFM_DBG(Serial.println("Setting SYNC Config FAILED"));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if(sx1278.setPreambleDetect(0xA8)!=0) {
|
||||||
|
//if(sx1278.setPreambleDetect(0x9F)!=0) {
|
||||||
|
DFM_DBG(Serial.println("Setting PreambleDetect FAILED"));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if(sx1278.setPacketConfig(0x08, 0x40)!=0) {
|
||||||
|
DFM_DBG(Serial.println("Setting Packet config FAILED"));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
sx1278.setPayloadLength(0); // infinite for now...
|
||||||
|
}
|
||||||
Serial.print("DFM: setting RX frequency to ");
|
Serial.print("DFM: setting RX frequency to ");
|
||||||
Serial.println(frequency);
|
Serial.println(frequency);
|
||||||
|
|
||||||
int retval = sx1278.setFrequency(frequency);
|
int retval = sx1278.setFrequency(frequency);
|
||||||
sx1278.clearIRQFlags();
|
sx1278.clearIRQFlags();
|
||||||
|
|
||||||
|
// Do this only once in setup in continous mode
|
||||||
|
sx1278.writeRegister(REG_OP_MODE, FSK_RX_MODE);
|
||||||
|
|
||||||
|
memset((void *)&dfmstate, 0, sizeof(dfmstate));
|
||||||
DFM_DBG(Serial.println("Setting SX1278 config for DFM finished\n"); Serial.println());
|
DFM_DBG(Serial.println("Setting SX1278 config for DFM finished\n"); Serial.println());
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
@@ -163,8 +212,147 @@ void DFM::printRaw(const char *label, int len, int ret, const uint8_t *data)
|
|||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char* typestr[16]={
|
||||||
|
"", "", "", "", "", "", // 00..05
|
||||||
|
"DFM6", // 06 => DFM6
|
||||||
|
"PS15", // 07 => PS15 (untested)
|
||||||
|
"", "",
|
||||||
|
"DFM9", // 0A => DFM9
|
||||||
|
"DF17", // 0B => DFM17?
|
||||||
|
"DF9P", // 0C => DFM9P or DFM17 test
|
||||||
|
"DF17", // 0D => DFM17
|
||||||
|
"", ""
|
||||||
|
};
|
||||||
|
|
||||||
|
#define DFMIDTHRESHOLD 2
|
||||||
|
/* inspired by oe5dxl's finddnmae in sondeudp.c of dxlaprs */
|
||||||
|
void DFM::finddfname(uint8_t *b)
|
||||||
|
{
|
||||||
|
uint8_t st;
|
||||||
|
uint32_t thres;
|
||||||
|
uint32_t i;
|
||||||
|
uint8_t ix;
|
||||||
|
uint16_t d;
|
||||||
|
|
||||||
|
st = b[0]; /* frame start byte */
|
||||||
|
ix = b[3]; /* hi/lo part of ser; (LSB due to our bitsToBytes...) */
|
||||||
|
d = (b[1]<<8) + b[2]; /* data byte */
|
||||||
|
/* find highest channel number single frame serial,
|
||||||
|
(2 frame serial will make a single serial too) */
|
||||||
|
if(dfmstate.idcnt0 < DFMIDTHRESHOLD && dfmstate.idcnt1 < DFMIDTHRESHOLD) {
|
||||||
|
uint32_t v = (st<<20) | (d<<4) | ix;
|
||||||
|
if ( st > (dfmstate.lastfrid>>20) ) {
|
||||||
|
dfmstate.lastfrid = v;
|
||||||
|
Serial.print(" MAXCH:"); Serial.print(st);
|
||||||
|
dfmstate.lastfrcnt = 0;
|
||||||
|
} else if ( st == (dfmstate.lastfrid>>20) ) {
|
||||||
|
/* same id found */
|
||||||
|
if (v == dfmstate.lastfrid) {
|
||||||
|
++dfmstate.lastfrcnt;
|
||||||
|
thres = DFMIDTHRESHOLD * 2;
|
||||||
|
/* may be a 2 frame serial so increase safety level */
|
||||||
|
if (ix <= 1) thres *= 2;
|
||||||
|
/* may be not a dfm6 so increase safety level */
|
||||||
|
if ( (st>>4) != 6) thres *= 2;
|
||||||
|
if (dfmstate.lastfrcnt >= thres) {
|
||||||
|
/* id found */
|
||||||
|
if (dfmstate.lastfrcnt == thres) {
|
||||||
|
uint32_t id = ((st&0x0F)<<20) | (d<<4) | ix;
|
||||||
|
uint32_t chkid = id;
|
||||||
|
int i;
|
||||||
|
/* check validity */
|
||||||
|
for(i=0; i<6; i++) {
|
||||||
|
if((chkid&0x0f)>9) { break; /* not ok */ }
|
||||||
|
chkid >>= 4;
|
||||||
|
}
|
||||||
|
if(i==6) {
|
||||||
|
snprintf(sonde.si()->id, 10, "D%x ", id);
|
||||||
|
sonde.si()->validID = true;
|
||||||
|
strncpy(sonde.si()->typestr, typestr[ (st>>4)&0x0F ], 5);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
dfmstate.lastfrcnt = 0;
|
||||||
|
Serial.print(" NOT NUMERIC SERIAL");
|
||||||
|
}
|
||||||
|
//anonym->idtime = osic_time();
|
||||||
|
} else {
|
||||||
|
Serial.print(" MAXCHCNT/SECLVL:");
|
||||||
|
Serial.print(dfmstate.lastfrcnt);
|
||||||
|
Serial.print("/");
|
||||||
|
Serial.print(thres);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
dfmstate.lastfrid = v; /* not stable ser */
|
||||||
|
dfmstate.lastfrcnt = 0UL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} /*find highest channel number single frame serial */
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i<dfmstate.nameregtop && dfmstate.start[i]!=st) i++;
|
||||||
|
Serial.printf(" %02x:i=%d,top=%d", st, i, dfmstate.nameregtop);
|
||||||
|
if (i<dfmstate.nameregtop) {
|
||||||
|
if (ix<=1UL && (dfmstate.cnt[2*i+ix]==0 || dfmstate.dat[2*i+ix]==d)) {
|
||||||
|
dfmstate.dat[2*i+ix] = d;
|
||||||
|
if(dfmstate.cnt[2*i+ix] < 255) dfmstate.cnt[2*i+ix]++;
|
||||||
|
Serial.print(" ID:");
|
||||||
|
Serial.print(st, HEX);
|
||||||
|
Serial.print("[");
|
||||||
|
Serial.print(ix);
|
||||||
|
Serial.print("] CNT:");
|
||||||
|
Serial.print(dfmstate.cnt[2*i]);
|
||||||
|
Serial.print(",");
|
||||||
|
Serial.print(dfmstate.cnt[2*i+1]);
|
||||||
|
Serial.print(",st=");
|
||||||
|
Serial.print(st);
|
||||||
|
Serial.print(",lastfrid=");
|
||||||
|
Serial.print(dfmstate.lastfrid>>20);
|
||||||
|
if( (dfmstate.cnt[2*i]>DFMIDTHRESHOLD && dfmstate.cnt[2*i+1]>DFMIDTHRESHOLD) ||
|
||||||
|
(dfmstate.cnt[2*i]>0 && dfmstate.cnt[2*i+1]>0 && st == (dfmstate.lastfrid>>20) && (st>>4)>6) ) {
|
||||||
|
if(dfmstate.idcnt0 <= 1) {
|
||||||
|
dfmstate.idcnt0 = dfmstate.cnt[2*i];
|
||||||
|
dfmstate.idcnt1 = dfmstate.cnt[2*i+1];
|
||||||
|
dfmstate.nameregok = i;
|
||||||
|
// generate id.....
|
||||||
|
snprintf(sonde.si()->id, 10, "D%d", ((dfmstate.dat[2*i]<<16)|dfmstate.dat[2*i+1])%100000000);
|
||||||
|
Serial.print("\nNEW AUTOID:");
|
||||||
|
Serial.println(sonde.si()->id);
|
||||||
|
sonde.si()->validID = true;
|
||||||
|
strncpy(sonde.si()->typestr, typestr[ (st>>4)&0x0F ], 5);
|
||||||
|
}
|
||||||
|
if(dfmstate.nameregok==i) {
|
||||||
|
Serial.print(" ID OK");
|
||||||
|
// idtime = .... /* TODO */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* data changed so not ser */
|
||||||
|
dfmstate.cnt[2*i] = 0;
|
||||||
|
dfmstate.cnt[2*i+1] = 0;
|
||||||
|
if(dfmstate.nameregok == i) { /* found id wrong */
|
||||||
|
dfmstate.idcnt0 = 0;
|
||||||
|
dfmstate.idcnt1 = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (ix<=1) { /* add new entry for possible ID */
|
||||||
|
dfmstate.start[dfmstate.nameregtop] = st;
|
||||||
|
dfmstate.cnt[2*dfmstate.nameregtop] = 0;
|
||||||
|
dfmstate.cnt[2*dfmstate.nameregtop+1] = 0;
|
||||||
|
dfmstate.cnt[2*dfmstate.nameregtop+ix] = 1;
|
||||||
|
dfmstate.dat[2*dfmstate.nameregtop+ix] = d;
|
||||||
|
if(dfmstate.nameregtop<49) dfmstate.nameregtop++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void DFM::decodeCFG(uint8_t *cfg)
|
void DFM::decodeCFG(uint8_t *cfg)
|
||||||
{
|
{
|
||||||
|
#if 1
|
||||||
|
// new ID
|
||||||
|
finddfname(cfg);
|
||||||
|
// new aprs ID (dxlaprs, autorx) is now "D" + serial (8 digits) by consensus
|
||||||
|
memcpy(sonde.si()->ser, sonde.si()->id+1, 9);
|
||||||
|
#else
|
||||||
|
// old ID
|
||||||
static int lowid, highid, idgood=0, type=0;
|
static int lowid, highid, idgood=0, type=0;
|
||||||
if((cfg[0]>>4)==0x06 && type==0) { // DFM-6 ID
|
if((cfg[0]>>4)==0x06 && type==0) { // DFM-6 ID
|
||||||
lowid = ((cfg[0]&0x0F)<<20) | (cfg[1]<<12) | (cfg[2]<<4) | (cfg[3]&0x0f);
|
lowid = ((cfg[0]&0x0F)<<20) | (cfg[1]<<12) | (cfg[2]<<4) | (cfg[3]&0x0f);
|
||||||
@@ -190,6 +378,7 @@ void DFM::decodeCFG(uint8_t *cfg)
|
|||||||
sonde.si()->validID = true;
|
sonde.si()->validID = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static int bitCount(int x) {
|
static int bitCount(int x) {
|
||||||
@@ -288,20 +477,140 @@ void DFM::bitsToBytes(uint8_t *bits, uint8_t *bytes, int len)
|
|||||||
bytes[(i-1)/8] &= 0x0F;
|
bytes[(i-1)/8] &= 0x0F;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int haveNewFrame = 0;
|
||||||
|
|
||||||
|
int DFM::processDFMdata(uint8_t dt) {
|
||||||
|
static uint8_t data[1024];
|
||||||
|
static uint32_t rxdata = 0;
|
||||||
|
static uint8_t rxbitc = 0;
|
||||||
|
static uint8_t rxbyte = 0;
|
||||||
|
static uint8_t rxsearching = 1;
|
||||||
|
static uint8_t rxp;
|
||||||
|
static int rssi=0, fei=0, afc=0;
|
||||||
|
static uint8_t invers = 0;
|
||||||
|
|
||||||
|
for(int i=0; i<8; i++) {
|
||||||
|
uint8_t d = (dt&0x80)?1:0;
|
||||||
|
dt <<= 1;
|
||||||
|
rxdata = (rxdata<<1) | d;
|
||||||
|
if( (rxbitc&1)==0 ) {
|
||||||
|
// "bit1"
|
||||||
|
rxbyte = (rxbyte<<1) | d;
|
||||||
|
} else {
|
||||||
|
// "bit2" ==> 01 or 10 => 1, otherweise => 0
|
||||||
|
// not here: (10=>1, 01=>0)!!! rxbyte = rxbyte ^ d;
|
||||||
|
}
|
||||||
|
//
|
||||||
|
if(rxsearching) {
|
||||||
|
if( rxdata == 0x6566A5AA || rxdata == 0x9A995A55 ) {
|
||||||
|
rxsearching = false;
|
||||||
|
rxbitc = 0;
|
||||||
|
rxp = 0;
|
||||||
|
rxbyte = 0;
|
||||||
|
rssi=sx1278.getRSSI();
|
||||||
|
fei=sx1278.getFEI();
|
||||||
|
afc=sx1278.getAFC();
|
||||||
|
sonde.si()->rssi = rssi;
|
||||||
|
sonde.si()->afc = afc;
|
||||||
|
invers = (rxdata == 0x6566A5AA)?1:0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
rxbitc = (rxbitc+1)%16; // 16;
|
||||||
|
if(rxbitc == 0) { // got 8 data bit
|
||||||
|
if(invers) rxbyte=~rxbyte;
|
||||||
|
data[rxp++] = rxbyte&0xff; // (rxbyte>>1)&0xff;
|
||||||
|
if(rxp>=DFM_FRAMELEN) {
|
||||||
|
rxsearching = true;
|
||||||
|
//Serial.println("Got a DFM frame!");
|
||||||
|
Serial.print("[RSSI="); Serial.print(rssi);
|
||||||
|
Serial.print(" FEI="); Serial.print(fei);
|
||||||
|
Serial.print(" AFC="); Serial.print(afc); Serial.print("] ");
|
||||||
|
decodeFrameDFM(data);
|
||||||
|
haveNewFrame = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int DFM::receive() {
|
int DFM::receive() {
|
||||||
|
if( stype == STYPE_DFM ) {
|
||||||
|
return receiveNew();
|
||||||
|
} else {
|
||||||
|
return receiveOld();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int DFM::receiveNew() {
|
||||||
|
int rxframes = 4;
|
||||||
|
// tentative continuous RX version...
|
||||||
|
unsigned long t0 = millis();
|
||||||
|
while( ( millis() - t0 ) < 1000 ) {
|
||||||
|
uint8_t value = sx1278.readRegister(REG_IRQ_FLAGS2);
|
||||||
|
if ( bitRead(value, 7) ) {
|
||||||
|
Serial.println("FIFO full");
|
||||||
|
}
|
||||||
|
if ( bitRead(value, 4) ) {
|
||||||
|
Serial.println("FIFO overflow");
|
||||||
|
// new: (maybe clear only overflow??) TODO
|
||||||
|
sx1278.clearIRQFlags();
|
||||||
|
}
|
||||||
|
if ( bitRead(value, 2) == 1 ) {
|
||||||
|
Serial.println("FIFO: payload ready()");
|
||||||
|
// does not make much sence? (from m10): TODO
|
||||||
|
// ??????? sx1278.clearIRQFlags();
|
||||||
|
}
|
||||||
|
if(bitRead(value, 6) == 0) { // while FIFO not empty
|
||||||
|
byte data = sx1278.readRegister(REG_FIFO);
|
||||||
|
processDFMdata(data);
|
||||||
|
value = sx1278.readRegister(REG_IRQ_FLAGS2);
|
||||||
|
} else {
|
||||||
|
#if 0
|
||||||
|
if(headerDetected) {
|
||||||
|
t0 = millis(); // restart timer... don't time out if header detected...
|
||||||
|
headerDetected = 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if(haveNewFrame) {
|
||||||
|
//Serial.printf("DFM::receive(): new frame complete after %ldms\n", millis()-t0);
|
||||||
|
haveNewFrame = 0;
|
||||||
|
rxframes--;
|
||||||
|
if(rxframes==0) return RX_OK;
|
||||||
|
}
|
||||||
|
delay(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return RX_TIMEOUT;
|
||||||
|
}
|
||||||
|
|
||||||
|
int DFM::receiveOld() {
|
||||||
byte data[1000]; // pending data from previous mode may write more than 33 bytes. TODO.
|
byte data[1000]; // pending data from previous mode may write more than 33 bytes. TODO.
|
||||||
for(int i=0; i<2; i++) {
|
for(int i=0; i<2; i++) {
|
||||||
sx1278.setPayloadLength(33); // Expect 33 bytes (7+13+13 bytes)
|
sx1278.setPayloadLength(33); // Expect 33 bytes (7+13+13 bytes)
|
||||||
|
|
||||||
sx1278.writeRegister(REG_OP_MODE, FSK_RX_MODE);
|
sx1278.writeRegister(REG_OP_MODE, FSK_RX_MODE);
|
||||||
|
//int t = millis();
|
||||||
int e = sx1278.receivePacketTimeout(1000, data);
|
int e = sx1278.receivePacketTimeout(1000, data);
|
||||||
|
//Serial.printf("rxPTO done after %d ms", (int)(millis()-t));
|
||||||
if(e) { return RX_TIMEOUT; } //if timeout... return 1
|
if(e) { return RX_TIMEOUT; } //if timeout... return 1
|
||||||
|
|
||||||
Serial.printf("inverse is %d\b", inverse);
|
if(!(stype==STYPE_DFM09)) { for(int i=0; i<33; i++) { data[i]^=0xFF; } }
|
||||||
if(!inverse) { for(int i=0; i<33; i++) { data[i]^=0xFF; } }
|
decodeFrameDFM(data);
|
||||||
|
}
|
||||||
|
return RX_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
int DFM::decodeFrameDFM(uint8_t *data) {
|
||||||
deinterleave(data, 7, hamming_conf);
|
deinterleave(data, 7, hamming_conf);
|
||||||
deinterleave(data+7, 13, hamming_dat1);
|
deinterleave(data+7, 13, hamming_dat1);
|
||||||
deinterleave(data+20, 13, hamming_dat2);
|
deinterleave(data+20, 13, hamming_dat2);
|
||||||
|
#if 0
|
||||||
|
Serial.print("RAWCFG:");
|
||||||
|
for(int i=0; i<7*8; i++) {
|
||||||
|
Serial.print(hamming_conf[i]?"1":"0");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
int ret0 = hamming(hamming_conf, 7, block_conf);
|
int ret0 = hamming(hamming_conf, 7, block_conf);
|
||||||
int ret1 = hamming(hamming_dat1, 13, block_dat1);
|
int ret1 = hamming(hamming_dat1, 13, block_dat1);
|
||||||
@@ -318,7 +627,7 @@ int DFM::receive() {
|
|||||||
decodeCFG(byte_conf);
|
decodeCFG(byte_conf);
|
||||||
decodeDAT(byte_dat1);
|
decodeDAT(byte_dat1);
|
||||||
decodeDAT(byte_dat2);
|
decodeDAT(byte_dat2);
|
||||||
}
|
Serial.println("");
|
||||||
return RX_OK;
|
return RX_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -345,3 +654,4 @@ int DFM::waitRXcomplete() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DFM dfm = DFM();
|
DFM dfm = DFM();
|
||||||
|
|
||||||
|
|||||||
@@ -23,16 +23,23 @@
|
|||||||
class DFM
|
class DFM
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
int inverse=0;
|
int stype;
|
||||||
|
char *stypename=NULL;
|
||||||
|
|
||||||
void deinterleave(uint8_t *str, int L, uint8_t *block);
|
void deinterleave(uint8_t *str, int L, uint8_t *block);
|
||||||
uint32_t bits2val(const uint8_t *bits, int len);
|
uint32_t bits2val(const uint8_t *bits, int len);
|
||||||
int check(uint8_t code[8]);
|
int check(uint8_t code[8]);
|
||||||
int hamming(uint8_t *ham, int L, uint8_t *sym);
|
int hamming(uint8_t *ham, int L, uint8_t *sym);
|
||||||
void printRaw(const char *prefix, int len, int ret, const uint8_t* data);
|
void printRaw(const char *prefix, int len, int ret, const uint8_t* data);
|
||||||
|
void finddfname(uint8_t *cfg);
|
||||||
void decodeCFG(uint8_t *cfg);
|
void decodeCFG(uint8_t *cfg);
|
||||||
void decodeDAT(uint8_t *dat);
|
void decodeDAT(uint8_t *dat);
|
||||||
void bitsToBytes(uint8_t *bits, uint8_t *bytes, int len);
|
void bitsToBytes(uint8_t *bits, uint8_t *bytes, int len);
|
||||||
|
int processDFMdata(uint8_t dt);
|
||||||
|
int decodeFrameDFM(uint8_t *data);
|
||||||
|
int receiveOld();
|
||||||
|
int receiveNew();
|
||||||
|
|
||||||
|
|
||||||
#define B 8
|
#define B 8
|
||||||
#define S 4
|
#define S 4
|
||||||
@@ -55,7 +62,7 @@ private:
|
|||||||
public:
|
public:
|
||||||
DFM();
|
DFM();
|
||||||
// main decoder API
|
// main decoder API
|
||||||
int setup(float frequency, int inverse);
|
int setup(float frequency, int type);
|
||||||
int receive();
|
int receive();
|
||||||
int waitRXcomplete();
|
int waitRXcomplete();
|
||||||
|
|
||||||
|
|||||||
+141
-19
@@ -7,16 +7,17 @@
|
|||||||
#include "Sonde.h"
|
#include "Sonde.h"
|
||||||
#include "math.h"
|
#include "math.h"
|
||||||
|
|
||||||
|
|
||||||
int readLine(Stream &stream, char *buffer, int maxlen);
|
int readLine(Stream &stream, char *buffer, int maxlen);
|
||||||
|
|
||||||
extern const char *version_name;
|
extern const char *version_name;
|
||||||
extern const char *version_id;
|
extern const char *version_id;
|
||||||
|
|
||||||
#include <../fonts/FreeMono9pt7b.h>
|
#include <fonts/FreeMono9pt7b.h>
|
||||||
#include <../fonts/FreeMono12pt7b.h>
|
#include <fonts/FreeMono12pt7b.h>
|
||||||
#include <../fonts/FreeSans9pt7b.h>
|
#include <fonts/FreeSans9pt7b.h>
|
||||||
#include <../fonts/FreeSans12pt7b.h>
|
#include <fonts/FreeSans12pt7b.h>
|
||||||
#include <../fonts/Picopixel.h>
|
#include <fonts/Picopixel.h>
|
||||||
|
|
||||||
extern Sonde sonde;
|
extern Sonde sonde;
|
||||||
|
|
||||||
@@ -29,7 +30,8 @@ extern SemaphoreHandle_t axpSemaphore;
|
|||||||
|
|
||||||
SPIClass spiDisp(HSPI);
|
SPIClass spiDisp(HSPI);
|
||||||
|
|
||||||
const char *sondeTypeStr[NSondeTypes] = { "DFM6", "DFM9", "RS41", "RS92", "M10 " };
|
const char *sondeTypeStr[NSondeTypes] = { "DFM6", "DFM9", "RS41", "RS92", "M10 ", "M20 ", "DFM " };
|
||||||
|
const char sondeTypeChar[NSondeTypes] = { '6', '9', '4', 'R', 'M', '2', 'D' };
|
||||||
|
|
||||||
byte myIP_tiles[8*11];
|
byte myIP_tiles[8*11];
|
||||||
static uint8_t ap_tile[8]={0x00,0x04,0x22,0x92, 0x92, 0x22, 0x04, 0x00};
|
static uint8_t ap_tile[8]={0x00,0x04,0x22,0x92, 0x92, 0x22, 0x04, 0x00};
|
||||||
@@ -267,12 +269,12 @@ void U8x8Display::drawTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y
|
|||||||
void U8x8Display::welcome() {
|
void U8x8Display::welcome() {
|
||||||
u8x8->clear();
|
u8x8->clear();
|
||||||
setFont(FONT_LARGE);
|
setFont(FONT_LARGE);
|
||||||
drawString(8 - strlen(version_name) / 2, 1, version_name);
|
drawString(0, 1, version_name);
|
||||||
//drawString(8 - strlen(version_id) / 2, 2, version_id);
|
drawString(11, 1, version_id);
|
||||||
setFont(FONT_SMALL);
|
setFont(FONT_SMALL);
|
||||||
drawString(0, 4, "RS41/92,DFM,M10");
|
drawString(0, 4, "RS41/92,DFM,M1&2");
|
||||||
drawString(0, 6, "by FRS2013 ");
|
drawString(0, 6, "by Vigor & Xav ");
|
||||||
drawString(11, 6, version_id);
|
//drawString(11, 6, version_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
static String previp;
|
static String previp;
|
||||||
@@ -513,6 +515,11 @@ void Display::parseDispElement(char *text, DispEntry *de)
|
|||||||
de->extra = strdup(text+1);
|
de->extra = strdup(text+1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'z':
|
||||||
|
// Duree vol
|
||||||
|
de->func = disp.drawDurVol;
|
||||||
|
de->extra = strdup(text+1);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
Serial.printf("Unknown element: %c\n", type);
|
Serial.printf("Unknown element: %c\n", type);
|
||||||
break;
|
break;
|
||||||
@@ -794,18 +801,45 @@ void Display::drawLat(DispEntry *de) {
|
|||||||
drawString(de,"00.0000");
|
drawString(de,"00.0000");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
//Format Degres, minutes, secondes ou decimal 1=degres 0=decimal
|
||||||
|
if (sonde.config.degdec==1)
|
||||||
|
{
|
||||||
|
float decimal=sonde.si()->lat;
|
||||||
|
int degres=int(decimal);
|
||||||
|
float minutes=(decimal - degres)*60;
|
||||||
|
float secondes=(minutes-int(minutes))*60;
|
||||||
|
snprintf(buf, 16, "%2d %2.0f'%2.0f", degres, minutes, secondes);
|
||||||
|
drawString(de,buf);
|
||||||
|
rdis->drawTile(2, 2, 1, deg_tile);
|
||||||
|
} else
|
||||||
|
{
|
||||||
snprintf(buf, 16, "%2.5f", sonde.si()->lat);
|
snprintf(buf, 16, "%2.5f", sonde.si()->lat);
|
||||||
drawString(de,buf);
|
drawString(de,buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
void Display::drawLon(DispEntry *de) {
|
void Display::drawLon(DispEntry *de) {
|
||||||
rdis->setFont(de->fmt);
|
rdis->setFont(de->fmt);
|
||||||
if(!sonde.si()->validPos) {
|
if(!sonde.si()->validPos) {
|
||||||
drawString(de,"00.0000");
|
drawString(de,"00.0000");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
//Format Degres, minutes, secondes ou decimal 1=degres 0=decimal
|
||||||
|
if (sonde.config.degdec==1)
|
||||||
|
{
|
||||||
|
float decimal=sonde.si()->lon;
|
||||||
|
int degres=int(decimal);
|
||||||
|
float minutes=(decimal - degres)*60;
|
||||||
|
float secondes=(minutes-int(minutes))*60;
|
||||||
|
snprintf(buf, 16, "%2d %2.0f'%2.0f", degres, minutes, secondes);
|
||||||
|
drawString(de,buf);
|
||||||
|
rdis->drawTile(2, 4, 1, deg_tile);
|
||||||
|
} else
|
||||||
|
{
|
||||||
snprintf(buf, 16, "%2.5f", sonde.si()->lon);
|
snprintf(buf, 16, "%2.5f", sonde.si()->lon);
|
||||||
drawString(de,buf);
|
drawString(de,buf);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
void Display::drawAlt(DispEntry *de) {
|
void Display::drawAlt(DispEntry *de) {
|
||||||
rdis->setFont(de->fmt);
|
rdis->setFont(de->fmt);
|
||||||
if(!sonde.si()->validPos) {
|
if(!sonde.si()->validPos) {
|
||||||
@@ -825,7 +859,7 @@ void Display::drawHS(DispEntry *de) {
|
|||||||
}
|
}
|
||||||
boolean is_ms = (de->extra && de->extra[0]=='m')?true:false; // m/s or km/h
|
boolean is_ms = (de->extra && de->extra[0]=='m')?true:false; // m/s or km/h
|
||||||
float hs = sonde.si()->hs;
|
float hs = sonde.si()->hs;
|
||||||
if(is_ms) hs = hs / 3.6;
|
if(is_ms) hs = hs * 3.6;
|
||||||
boolean has_extra = (de->extra && de->extra[1]!=0)? true: false;
|
boolean has_extra = (de->extra && de->extra[1]!=0)? true: false;
|
||||||
snprintf(buf, 16, hs>99?" %3.0f":" %2.1f", hs);
|
snprintf(buf, 16, hs>99?" %3.0f":" %2.1f", hs);
|
||||||
if(has_extra) { strcat(buf, de->extra+1); }
|
if(has_extra) { strcat(buf, de->extra+1); }
|
||||||
@@ -934,6 +968,12 @@ void Display::drawVBat(DispEntry *de) {
|
|||||||
snprintf(buf, 16, "%3.0f%s", sonde.si()->vbat, "%");
|
snprintf(buf, 16, "%3.0f%s", sonde.si()->vbat, "%");
|
||||||
drawString(de, buf);
|
drawString(de, buf);
|
||||||
}
|
}
|
||||||
|
void Display::drawDurVol(DispEntry *de) {
|
||||||
|
rdis->setFont(de->fmt);
|
||||||
|
Serial.printf("\nDurVol:%2d:%2d.%2d", sonde.si()->durvolheure,sonde.si()->durvolminute,sonde.si()->durvolseconde);
|
||||||
|
snprintf(buf, 16, "%2d:%2d.%2d", sonde.si()->durvolheure,sonde.si()->durvolminute,sonde.si()->durvolseconde);
|
||||||
|
drawString(de,buf);
|
||||||
|
}
|
||||||
void Display::drawAFC(DispEntry *de) {
|
void Display::drawAFC(DispEntry *de) {
|
||||||
if(!sonde.config.showafc) return;
|
if(!sonde.config.showafc) return;
|
||||||
rdis->setFont(de->fmt);
|
rdis->setFont(de->fmt);
|
||||||
@@ -980,7 +1020,45 @@ void Display::drawSite(DispEntry *de) {
|
|||||||
if(de->extra[0]) strcat(buf, de->extra+1);
|
if(de->extra[0]) strcat(buf, de->extra+1);
|
||||||
drawString(de, buf);
|
drawString(de, buf);
|
||||||
}
|
}
|
||||||
void Display::drawTelemetry(DispEntry *de) {
|
void Telemetry() {
|
||||||
|
if (sonde.config.telemetryOn==1) {
|
||||||
|
File file = SPIFFS.open("/data.csv", "a");
|
||||||
|
|
||||||
|
if (!file) {
|
||||||
|
Serial.println("There was an error opening the file for writing");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
int dbmetre=sonde.si()->rssi/2;
|
||||||
|
String smetre ="";
|
||||||
|
if (dbmetre>138) smetre="S0";
|
||||||
|
if ((dbmetre<137)&&(dbmetre>122)) smetre="S1";
|
||||||
|
if ((dbmetre<121)&&(dbmetre>116)) smetre="S2";
|
||||||
|
if ((dbmetre<115)&&(dbmetre>108)) smetre="S3";
|
||||||
|
if ((dbmetre<109)&&(dbmetre>104)) smetre="S4";
|
||||||
|
if ((dbmetre<103)&&(dbmetre>98)) smetre="S5";
|
||||||
|
if ((dbmetre<97)&&(dbmetre>92)) smetre="S6";
|
||||||
|
if ((dbmetre<91)&&(dbmetre>86)) smetre="S7";
|
||||||
|
if ((dbmetre<85)&&(dbmetre>80)) smetre="S8";
|
||||||
|
if ((dbmetre<79)&&(dbmetre>74)) smetre="S9";
|
||||||
|
if ((dbmetre<73)&&(dbmetre>64)) smetre="S9+10";
|
||||||
|
if ((dbmetre<63)&&(dbmetre>54)) smetre="S9+20";
|
||||||
|
if ((dbmetre<53)&&(dbmetre>44)) smetre="S9+30";
|
||||||
|
if ((dbmetre<43)&&(dbmetre>34)) smetre="S9+40";
|
||||||
|
if ((dbmetre<33)&&(dbmetre>24)) smetre="S9+50";
|
||||||
|
if ((dbmetre<23)&&(dbmetre>14)) smetre="S9+60";
|
||||||
|
if ((dbmetre<13)&&(dbmetre>0)) smetre="S9+70";
|
||||||
|
|
||||||
|
String telemetry = telemetry + sonde.si()->id + ";" + sonde.si()->ser + ";" + sondeTypeStr[sonde.si()->type] + ";" + sonde.si()->freq + "Mhz;" + sonde.si()->lat + "; "+ sonde.si()->lon + ";" + sonde.si()->alt + "m;" + sonde.config.gps_lat + ";" + sonde.config.gps_lon +";" + sonde.config.gps_alt + ";" + sonde.si()->vs + "m/s;" + sonde.si()->hs + "m/s;" + ";" + sonde.si()->dir + "°;" + sonde.si()->launchsite + ";" + sonde.si()->rssi/2 + ";" + smetre + ";" + *sonde.si()->rxStat + ";" + sonde.si()->afc*0.001 + ";" + sonde.si()->validPos + "\n";
|
||||||
|
if (file.print(telemetry+"\n")) {
|
||||||
|
Serial.println("File was written");
|
||||||
|
} else {
|
||||||
|
Serial.println("File write failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Display::drawKilltimer(DispEntry *de) {
|
void Display::drawKilltimer(DispEntry *de) {
|
||||||
@@ -1050,6 +1128,10 @@ static int tmpc=0;
|
|||||||
gpsCourse = lastCourse;
|
gpsCourse = lastCourse;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
sonde.config.gps_Lat=gpsLat;
|
||||||
|
sonde.config.gps_Lon=gpsLon;
|
||||||
|
sonde.config.gps_Alt=gpsAlt;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// distance
|
// distance
|
||||||
@@ -1090,23 +1172,57 @@ static int tmpc=0;
|
|||||||
Serial.printf("\nGPS data: valid%d GPS at %f,%f (alt=%d,cog=%d); sonde at dist=%d, dir=%d rel.bear=%d\n",gpsValid?1:0,
|
Serial.printf("\nGPS data: valid%d GPS at %f,%f (alt=%d,cog=%d); sonde at dist=%d, dir=%d rel.bear=%d\n",gpsValid?1:0,
|
||||||
gpsLat, gpsLon, gpsAlt, gpsCourse, gpsDist, gpsDir, gpsBear);
|
gpsLat, gpsLon, gpsAlt, gpsCourse, gpsDist, gpsDir, gpsBear);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
void Display::calcVbat() {
|
||||||
//VBat %
|
//VBat %
|
||||||
float vbat,r,Dmax, Dact;
|
float vbat,r=0,Dmax, Dact;
|
||||||
float ADC_PIN = 35;
|
|
||||||
float vbatmax=atof(sonde.config.vbatmax), vbatmin=atof(sonde.config.vbatmin);
|
float vbatmax=atof(sonde.config.vbatmax), vbatmin=atof(sonde.config.vbatmin);
|
||||||
|
int ADC_PIN = 35;
|
||||||
|
if (axp192_found==true)
|
||||||
|
{
|
||||||
|
ADC_PIN = 0;
|
||||||
|
}
|
||||||
r = analogRead(ADC_PIN);
|
r = analogRead(ADC_PIN);
|
||||||
vbat = r * (3.30 / 4094.00); //convert the value to a true voltage.
|
vbat = r * (3.30 / 4094.00); //convert the value to a true voltage.
|
||||||
Serial.printf("\nVbatMAX= %1.2fV",vbatmax);
|
//Serial.printf("\nVbatMAX= %1.2fV",vbatmax);
|
||||||
Serial.printf("\nVbatMIN= %1.2fV",vbatmin);
|
//Serial.printf("\nVbatMIN= %1.2fV",vbatmin);
|
||||||
Dmax = vbatmax - vbatmin;
|
Dmax = vbatmax - vbatmin;
|
||||||
Dact = vbat - vbatmin;
|
Dact = vbat - vbatmin;
|
||||||
vbat = (Dact / Dmax)*100;
|
vbat = (Dact / Dmax)*100;
|
||||||
Serial.printf("\nVbat:%3.0f\%\n",vbat);
|
Serial.printf("\nVbat:%3.0f\%\n",vbat);
|
||||||
sonde.si()->vbat=vbat;
|
sonde.si()->vbat=vbat;
|
||||||
}
|
}
|
||||||
|
void Display::calcDurVol() {
|
||||||
|
|
||||||
|
float alt = sonde.si()->alt;
|
||||||
|
float hs = sonde.si()->hs, durvol=0;
|
||||||
|
int heure=0, minute=0, seconde=0;
|
||||||
|
if (hs <0)
|
||||||
|
{
|
||||||
|
durvol=(alt/hs)*-1;
|
||||||
|
if (durvol>0)
|
||||||
|
{
|
||||||
|
Serial.printf("\nDurVol=%2.2f",durvol);
|
||||||
|
if ((durvol/60)>59)
|
||||||
|
{
|
||||||
|
heure=int(durvol/3600);
|
||||||
|
minute=int((heure-(durvol/3600))*3600);
|
||||||
|
}else {
|
||||||
|
minute=int(durvol/60);
|
||||||
|
seconde=int((minute-(durvol/60))*60);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
heure=0;
|
||||||
|
}
|
||||||
|
sonde.si()->durvolheure=heure;
|
||||||
|
sonde.si()->durvolminute=minute;
|
||||||
|
sonde.si()->durvolseconde=seconde;
|
||||||
|
//Serial.printf("\nHeure=:%2d:%2d.%2d\n",heure,minute,seconde);
|
||||||
|
}
|
||||||
void Display::drawGPS(DispEntry *de) {
|
void Display::drawGPS(DispEntry *de) {
|
||||||
//if(sonde.config.gps_rxd<0) return;
|
|
||||||
rdis->setFont(de->fmt);
|
rdis->setFont(de->fmt);
|
||||||
switch(de->extra[0]) {
|
switch(de->extra[0]) {
|
||||||
case 'W':
|
case 'W':
|
||||||
@@ -1144,7 +1260,7 @@ void Display::drawGPS(DispEntry *de) {
|
|||||||
if( (sonde.si()->validPos&0x03)!=0x03 ) {
|
if( (sonde.si()->validPos&0x03)!=0x03 ) {
|
||||||
snprintf(buf, 16, "no pos ");
|
snprintf(buf, 16, "no pos ");
|
||||||
if(de->extra && *de->extra=='5') buf[5]=0;
|
if(de->extra && *de->extra=='5') buf[5]=0;
|
||||||
} else if((!disp.gpsValid)||(disp.gpsLat!=0)&&(disp.gpsLon!=0)) {
|
} else if((!disp.gpsValid)&&(disp.gpsLat!=0)) {
|
||||||
snprintf(buf, 16, "no gps ");
|
snprintf(buf, 16, "no gps ");
|
||||||
if(de->extra && *de->extra=='5') buf[5]=0;
|
if(de->extra && *de->extra=='5') buf[5]=0;
|
||||||
} else {
|
} else {
|
||||||
@@ -1476,6 +1592,7 @@ void Display::drawGPS(DispEntry *de) {
|
|||||||
drawString(de, buf);
|
drawString(de, buf);
|
||||||
rdis->drawTile(de->x+3, de->y, 1, deg_tile);
|
rdis->drawTile(de->x+3, de->y, 1, deg_tile);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1527,6 +1644,7 @@ void Display::updateDisplayPos() {
|
|||||||
if(di->func != disp.drawLat && di->func != disp.drawLon) continue;
|
if(di->func != disp.drawLat && di->func != disp.drawLon) continue;
|
||||||
di->func(di);
|
di->func(di);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
void Display::updateDisplayPos2() {
|
void Display::updateDisplayPos2() {
|
||||||
for(DispEntry *di=layout->de; di->func != NULL; di++) {
|
for(DispEntry *di=layout->de; di->func != NULL; di++) {
|
||||||
@@ -1582,9 +1700,13 @@ void Display::updateDisplayIP() {
|
|||||||
|
|
||||||
void Display::updateDisplay() {
|
void Display::updateDisplay() {
|
||||||
calcGPS();
|
calcGPS();
|
||||||
|
calcVbat();
|
||||||
|
calcDurVol();
|
||||||
|
Telemetry();
|
||||||
for(DispEntry *di=layout->de; di->func != NULL; di++) {
|
for(DispEntry *di=layout->de; di->func != NULL; di++) {
|
||||||
di->func(di);
|
di->func(di);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Display disp = Display();
|
Display disp = Display();
|
||||||
@@ -85,6 +85,8 @@ private:
|
|||||||
static void circ(int x, int y);
|
static void circ(int x, int y);
|
||||||
static int countEntries(File f);
|
static int countEntries(File f);
|
||||||
void calcGPS();
|
void calcGPS();
|
||||||
|
void calcVbat();
|
||||||
|
void calcDurVol();
|
||||||
boolean gpsValid;
|
boolean gpsValid;
|
||||||
float gpsLat, gpsLon;
|
float gpsLat, gpsLon;
|
||||||
int gpsAlt;
|
int gpsAlt;
|
||||||
@@ -123,6 +125,7 @@ public:
|
|||||||
static void drawLon(DispEntry *de);
|
static void drawLon(DispEntry *de);
|
||||||
static void drawAz(DispEntry *de);
|
static void drawAz(DispEntry *de);
|
||||||
static void drawVBat(DispEntry *de);
|
static void drawVBat(DispEntry *de);
|
||||||
|
static void drawDurVol(DispEntry *de);
|
||||||
static void drawAlt(DispEntry *de);
|
static void drawAlt(DispEntry *de);
|
||||||
static void drawHS(DispEntry *de);
|
static void drawHS(DispEntry *de);
|
||||||
static void drawVS(DispEntry *de);
|
static void drawVS(DispEntry *de);
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ int M10::setup(float frequency)
|
|||||||
M10_DBG(Serial.println("Setting FSJ mode FAILED"));
|
M10_DBG(Serial.println("Setting FSJ mode FAILED"));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if(sx1278.setBitrate(9600)!=0) {
|
if(sx1278.setBitrate(9616)!=0) {
|
||||||
M10_DBG(Serial.println("Setting bitrate 9600bit/s FAILED"));
|
M10_DBG(Serial.println("Setting bitrate 9600bit/s FAILED"));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -80,13 +80,14 @@ int M10::setup(float frequency)
|
|||||||
//const char *SYNC="\x99\x9A";
|
//const char *SYNC="\x99\x9A";
|
||||||
#if 1
|
#if 1
|
||||||
// version 1, working with continuous RX
|
// version 1, working with continuous RX
|
||||||
const char *SYNC="\x66\x65";
|
//const char *SYNC="\x66\x65";
|
||||||
if(sx1278.setSyncConf(0x70, 2, (const uint8_t *)SYNC)!=0) {
|
|
||||||
|
const char *SYNC="\x99\x99\x4C\x99";
|
||||||
|
if(sx1278.setSyncConf(0x70, 4, (const uint8_t *)SYNC)!=0) {
|
||||||
M10_DBG(Serial.println("Setting SYNC Config FAILED"));
|
M10_DBG(Serial.println("Setting SYNC Config FAILED"));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
//if(sx1278.setPreambleDetect(0xA8)!=0) {
|
if((sx1278.setPreambleDetect(0x9F)!=0)&&((sx1278.setPreambleDetect(0xAF)!=0))) {
|
||||||
if(sx1278.setPreambleDetect(0x9F)!=0) {
|
|
||||||
M10_DBG(Serial.println("Setting PreambleDetect FAILED"));
|
M10_DBG(Serial.println("Setting PreambleDetect FAILED"));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -258,8 +259,8 @@ int M10::decodeframeM10(uint8_t *data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Serial.println(crcok?"CRC OK":"CRC NOT OK");
|
Serial.println(crcok?"CRC OK":"CRC NOT OK");
|
||||||
|
//M10 0x9F M10Plus 0xAF
|
||||||
if(data[1]==0x9F && data[2]==0x20) {
|
if((data[1]==0x9F && data[2]==0x20) || (data[1]==0xAF && data[2]==0x20)) {
|
||||||
Serial.println("Decoding...");
|
Serial.println("Decoding...");
|
||||||
// Its a M10
|
// Its a M10
|
||||||
// getid...
|
// getid...
|
||||||
@@ -381,7 +382,7 @@ void M10::processM10data(uint8_t dt)
|
|||||||
int M10::receive() {
|
int M10::receive() {
|
||||||
unsigned long t0 = millis();
|
unsigned long t0 = millis();
|
||||||
Serial.printf("M10::receive() start at %ld\n",t0);
|
Serial.printf("M10::receive() start at %ld\n",t0);
|
||||||
while( millis() - t0 < 1000 ) {
|
while( millis() - t0 < 1512 ) {
|
||||||
uint8_t value = sx1278.readRegister(REG_IRQ_FLAGS2);
|
uint8_t value = sx1278.readRegister(REG_IRQ_FLAGS2);
|
||||||
if ( bitRead(value, 7) ) {
|
if ( bitRead(value, 7) ) {
|
||||||
Serial.println("FIFO full");
|
Serial.println("FIFO full");
|
||||||
|
|||||||
Executable
+535
@@ -0,0 +1,535 @@
|
|||||||
|
|
||||||
|
/* M20 decoder functions */
|
||||||
|
#include "M20.h"
|
||||||
|
#include "SX1278FSK.h"
|
||||||
|
#include "rsc.h"
|
||||||
|
#include "Sonde.h"
|
||||||
|
#include <SPIFFS.h>
|
||||||
|
|
||||||
|
// well...
|
||||||
|
//#include "rs92gps.h"
|
||||||
|
|
||||||
|
#define M20_DEBUG 1
|
||||||
|
|
||||||
|
#if M20_DEBUG
|
||||||
|
#define M20_DBG(x) x
|
||||||
|
#else
|
||||||
|
#define M20_DBG(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static byte data1[512];
|
||||||
|
static byte *dataptr=data1;
|
||||||
|
|
||||||
|
static uint8_t rxbitc;
|
||||||
|
static uint16_t rxbyte;
|
||||||
|
static int rxp=0;
|
||||||
|
static int haveNewFrame = 0;
|
||||||
|
static int lastFrame = 0;
|
||||||
|
static int headerDetected = 0;
|
||||||
|
|
||||||
|
int M20::setup(float frequency)
|
||||||
|
{
|
||||||
|
#if M20_DEBUG
|
||||||
|
Serial.println("Setup sx1278 for M20 sonde");
|
||||||
|
#endif
|
||||||
|
//if(!initialized) {
|
||||||
|
//Gencrctab();
|
||||||
|
//initrsc();
|
||||||
|
// not here for now.... get_eph("/brdc.19n");
|
||||||
|
// initialized = true;
|
||||||
|
//}
|
||||||
|
|
||||||
|
if(sx1278.ON()!=0) {
|
||||||
|
M20_DBG(Serial.println("Setting SX1278 power on FAILED"));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if(sx1278.setFSK()!=0) {
|
||||||
|
M20_DBG(Serial.println("Setting FSJ mode FAILED"));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if(sx1278.setBitrate(9600)!=0) {
|
||||||
|
M20_DBG(Serial.println("Setting bitrate 9600bit/s FAILED"));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#if M20_DEBUG
|
||||||
|
float br = sx1278.getBitrate();
|
||||||
|
Serial.print("Exact bitrate is ");
|
||||||
|
Serial.println(br);
|
||||||
|
#endif
|
||||||
|
if(sx1278.setAFCBandwidth(sonde.config.rs92.rxbw)!=0) {
|
||||||
|
M20_DBG(Serial.printf("Setting AFC bandwidth %d Hz FAILED", sonde.config.rs92.rxbw));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if(sx1278.setRxBandwidth(sonde.config.rs92.rxbw)!=0) {
|
||||||
|
M20_DBG(Serial.printf("Setting RX bandwidth to %d Hz FAILED", sonde.config.rs92.rxbw));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable auto-AFC, auto-AGC, RX Trigger by preamble
|
||||||
|
if(sx1278.setRxConf(0x1E)!=0) {
|
||||||
|
M20_DBG(Serial.println("Setting RX Config FAILED"));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// Set autostart_RX to 01, preamble 0, SYNC detect==on, syncsize=3 (==4 byte
|
||||||
|
//char header[] = "0110.0101 0110.0110 1010.0101 1010.1010";
|
||||||
|
|
||||||
|
//const char *SYNC="\x10\xB6\xCA\x11\x22\x96\x12\xF8";
|
||||||
|
//const char *SYNC="\x08\x6D\x53\x88\x44\x69\x48\x1F";
|
||||||
|
// was 0x57
|
||||||
|
//const char *SYNC="\x99\x9A";
|
||||||
|
#if 1
|
||||||
|
// version 1, working with continuous RX
|
||||||
|
//const char *SYNC="\x66\x65";
|
||||||
|
const char *SYNC="\x99\x99\x4C\x99";
|
||||||
|
if(sx1278.setSyncConf(0x70, 4, (const uint8_t *)SYNC)!=0) {
|
||||||
|
M20_DBG(Serial.println("Setting SYNC Config FAILED"));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
//if(sx1278.setPreambleDetect(0xA8)!=0) {
|
||||||
|
if(sx1278.setPreambleDetect(0x20)!=0) {
|
||||||
|
M20_DBG(Serial.println("Setting PreambleDetect FAILED"));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#if 0
|
||||||
|
// version 2, with per-packet rx start, untested
|
||||||
|
// header is 2a 10 65, i.e. with lsb first
|
||||||
|
// 0 0101 0100 1 0 0000 1000 1 0 1010 0110 1
|
||||||
|
// 10 10011001 10011010 01 10 10101010 01101010 01 10 01100110 10010110 01
|
||||||
|
// preamble 0x6A 0x66 0x6A
|
||||||
|
// i.e. preamble detector on (0x80), preamble detector size 1 (0x00), preample chip errors??? (0x0A)
|
||||||
|
// after 2a2a2a2a2a1065
|
||||||
|
if(sx1278.setPreambleDetect(0xA8)!=0) {
|
||||||
|
M20_DBG(Serial.println("Setting PreambleDetect FAILED"));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// sync config: ato restart (01), preamble polarity AA (0), sync on (1), resevered (0), syncsize 2+1 (010) => 0x52
|
||||||
|
const char *SYNC="\x6A\x66\x69";
|
||||||
|
if(sx1278.setSyncConf(0x52, 3, (const uint8_t *)SYNC)!=0) {
|
||||||
|
M20_DBG(Serial.println("Setting SYNC Config FAILED"));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// payload length is ((240 - 7)*10 +6)/8 = 292
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Packet config 1: fixed len, no mancecer, no crc, no address filter
|
||||||
|
// Packet config 2: packet mode, no home ctrl, no beackn, msb(packetlen)=0)
|
||||||
|
if(sx1278.setPacketConfig(0x08, 0x40)!=0) {
|
||||||
|
M20_DBG(Serial.println("Setting Packet config FAILED"));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.print("M20: setting RX frequency to ");
|
||||||
|
Serial.println(frequency);
|
||||||
|
int res = sx1278.setFrequency(frequency);
|
||||||
|
// enable RX
|
||||||
|
sx1278.setPayloadLength(0); // infinite for now...
|
||||||
|
//sx1278.setPayloadLength(292);
|
||||||
|
sx1278.writeRegister(REG_OP_MODE, FSK_RX_MODE);
|
||||||
|
|
||||||
|
#if M20_DEBUG
|
||||||
|
M20_DBG(Serial.println("Setting SX1278 config for M20 finished\n"); Serial.println());
|
||||||
|
#endif
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
int M20::setFrequency(float frequency) {
|
||||||
|
Serial.print("M20: setting RX frequency to ");
|
||||||
|
Serial.println(frequency);
|
||||||
|
int res = sx1278.setFrequency(frequency);
|
||||||
|
// enable RX
|
||||||
|
sx1278.setPayloadLength(0); // infinite for now...
|
||||||
|
|
||||||
|
sx1278.writeRegister(REG_OP_MODE, FSK_RX_MODE);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
uint32_t M20::bits2val(const uint8_t *bits, int len) {
|
||||||
|
uint32_t val = 0;
|
||||||
|
for (int j = 0; j < len; j++) {
|
||||||
|
val |= (bits[j] << (len-1-j));
|
||||||
|
}
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
M20::M20() {
|
||||||
|
}
|
||||||
|
|
||||||
|
#define M20_FRAMELEN 70
|
||||||
|
#define M20_CRCPOS 68
|
||||||
|
|
||||||
|
void M20::printRaw(uint8_t *data, int len)
|
||||||
|
{
|
||||||
|
char buf[3];
|
||||||
|
int i;
|
||||||
|
for(i=0; i<len; i++) {
|
||||||
|
snprintf(buf, 3, "%02X ", data[i]);
|
||||||
|
Serial.print(buf);
|
||||||
|
}
|
||||||
|
Serial.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
static int update_checkM20(int c, uint8_t b) {
|
||||||
|
int c0, c1, t, t6, t7, s;
|
||||||
|
c1 = c & 0xFF;
|
||||||
|
// B
|
||||||
|
b = (b >> 1) | ((b & 1) << 7);
|
||||||
|
b ^= (b >> 2) & 0xFF;
|
||||||
|
// A1
|
||||||
|
t6 = ( c & 1) ^ ((c >> 2) & 1) ^ ((c >> 4) & 1);
|
||||||
|
t7 = ((c >> 1) & 1) ^ ((c >> 3) & 1) ^ ((c >> 5) & 1);
|
||||||
|
t = (c & 0x3F) | (t6 << 6) | (t7 << 7);
|
||||||
|
// A2
|
||||||
|
s = (c >> 7) & 0xFF;
|
||||||
|
s ^= (s >> 2) & 0xFF;
|
||||||
|
c0 = b ^ t ^ s;
|
||||||
|
return ((c1 << 8) | c0) & 0xFFFF;
|
||||||
|
}
|
||||||
|
static bool checkM20crc(uint8_t *msg) {
|
||||||
|
int i, cs, cs1;
|
||||||
|
cs = 0;
|
||||||
|
for (i = 0; i < M20_CRCPOS; i++) {
|
||||||
|
cs = update_checkM20(cs, msg[i]);
|
||||||
|
}
|
||||||
|
cs = cs & 0xFFFF;
|
||||||
|
cs1 = (msg[M20_CRCPOS] << 8) | msg[M20_CRCPOS+1];
|
||||||
|
return (cs1 == cs);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef uint32_t SET256[8];
|
||||||
|
static SET256 sondeudp_VARSET = {0x03BBBBF0UL,0x80600000UL,0x06A001A0UL,
|
||||||
|
0x0000001CUL,0x00000000UL,0x00000000UL,0x00000000UL,
|
||||||
|
0x00000000UL};
|
||||||
|
// VARSET=SET256{4..9,11..13,15..17,19..21,23..25,53..54,63,69,71,72,85,87,89,90,98..100};
|
||||||
|
|
||||||
|
static uint8_t fixcnt[M20_FRAMELEN];
|
||||||
|
static uint8_t fixbytes[M20_FRAMELEN];
|
||||||
|
|
||||||
|
static int32_t getint32(uint8_t *data) {
|
||||||
|
return (int32_t)( data[3]|(data[2]<<8)|(data[1]<<16)|(data[0]<<24) );
|
||||||
|
}
|
||||||
|
|
||||||
|
static int32_t getint24(uint8_t *data) {
|
||||||
|
return (int32_t)(data[2]|(data[1]<<8)|(data[0]<<16) );
|
||||||
|
}
|
||||||
|
|
||||||
|
static int16_t getint16(uint8_t *data) {
|
||||||
|
return (int16_t)(data[1]|((uint16_t)data[0]<<8));
|
||||||
|
}
|
||||||
|
|
||||||
|
static int16_t getint16_r(uint8_t *data) {
|
||||||
|
return (int16_t)(((uint16_t)data[1]<<8) |data[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char dez(uint8_t nr) {
|
||||||
|
nr = nr%10;
|
||||||
|
return '0'+nr;
|
||||||
|
}
|
||||||
|
static char hex(uint8_t nr) {
|
||||||
|
nr = nr&0x0f;
|
||||||
|
if(nr<10) return '0'+nr;
|
||||||
|
else return 'A'+nr-10;
|
||||||
|
}
|
||||||
|
const static float DEGMUL = 1.0/0xB60B60;
|
||||||
|
#define VMUL 0.005
|
||||||
|
#define VMUL_M20 0.01
|
||||||
|
#ifndef PI
|
||||||
|
#define PI (3.1415926535897932384626433832795)
|
||||||
|
#endif
|
||||||
|
#define RAD (PI/180)
|
||||||
|
|
||||||
|
|
||||||
|
// ret: 1=frame ok; 2=frame with errors; 0=ignored frame (m20dop-alternativ)
|
||||||
|
int M20::decodeframeM20(uint8_t *data) {
|
||||||
|
int repairstep = 16;
|
||||||
|
int repl = 0;
|
||||||
|
bool crcok;
|
||||||
|
// error correction, inspired by oe5dxl's sondeudp
|
||||||
|
do {
|
||||||
|
crcok = checkM20crc(data);
|
||||||
|
if(crcok) break;
|
||||||
|
repl = 0;
|
||||||
|
for(int i=0; i<M20_CRCPOS; i++) {
|
||||||
|
if( ((sondeudp_VARSET[i/32]&(1<<(i%32))) != 1) && (fixcnt[i]>=repairstep) ) {
|
||||||
|
repl++;
|
||||||
|
data[i] = fixbytes[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
repairstep >>= 1;
|
||||||
|
} while(repairstep>0);
|
||||||
|
if(crcok) {
|
||||||
|
for(int i=0; i<M20_CRCPOS; i++) {
|
||||||
|
if(fixbytes[i]==data[i] &&fixcnt[i]<255) fixcnt[i]++;
|
||||||
|
else { fixcnt[i]=0; fixbytes[i]=data[i]; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Serial.println(crcok?"CRC OK":"CRC NOT OK");
|
||||||
|
//M20 0x20
|
||||||
|
if(data[1]==0x20) {
|
||||||
|
Serial.println("Decoding...");
|
||||||
|
// Its a M20
|
||||||
|
// getid...
|
||||||
|
char ids[11]={'M','E','0','0','0','0','0','0','0','0','0'};
|
||||||
|
|
||||||
|
ids[0] = 'M';
|
||||||
|
ids[1] = 'E';
|
||||||
|
uint32_t id = getint16(data+18);
|
||||||
|
ids[2] = hex(id/16);
|
||||||
|
ids[3] = hex(id);
|
||||||
|
//
|
||||||
|
id = getint16_r(data+19)/4;
|
||||||
|
ids[4] = (char)((id/10000)%10+48);
|
||||||
|
ids[5] = (char)((id/1000)%10+48);
|
||||||
|
ids[6] = (char)((id/100)%10+48);
|
||||||
|
ids[7] = (char)((id/10)%10+48);
|
||||||
|
ids[8] = (char)(id%10+48);
|
||||||
|
strncpy(sonde.si()->ser, ids, 10);
|
||||||
|
sonde.si()->validID = true;
|
||||||
|
//Serial.printf("ID is %s [%02x %02x %d]\n", ids, data[95], data[93], id);
|
||||||
|
// ID printed on sonde is ...-.-abbbb, with a=id>>13, bbbb=id&0x1fff in decimal
|
||||||
|
// position data
|
||||||
|
// 0x1C 4 byte
|
||||||
|
sonde.si()->lat = getint32(data+28) * 1e-6;
|
||||||
|
//0x20 4 byte
|
||||||
|
sonde.si()->lon = getint32(data+32) * 1e-6;
|
||||||
|
//0x08 3 byte
|
||||||
|
sonde.si()->alt = getint24(data+8) * VMUL_M20;
|
||||||
|
//0x0B 2 byte
|
||||||
|
//VMUL_M20 specific
|
||||||
|
float ve = getint16(data+11)*VMUL_M20;
|
||||||
|
//0x0D 2 byte
|
||||||
|
float vn = getint16(data+13)*VMUL_M20;
|
||||||
|
//0x18 2 byte
|
||||||
|
sonde.si()->vs = getint16(data+24) * VMUL_M20;
|
||||||
|
sonde.si()->hs = sqrt(ve*ve+vn*vn);
|
||||||
|
float dir = atan2(vn, ve)*(1.0/RAD);
|
||||||
|
if(dir<0) dir+=360;
|
||||||
|
sonde.si()->dir = dir;
|
||||||
|
sonde.si()->validPos = 0x3f;
|
||||||
|
|
||||||
|
//0x0F 3 byte
|
||||||
|
uint32_t tow = getint24(data+15);
|
||||||
|
uint16_t week = getint16(data+26);
|
||||||
|
sonde.si()->time = (tow+week*604800+315964800)-18;
|
||||||
|
|
||||||
|
sonde.si()->validTime = true;
|
||||||
|
} else {
|
||||||
|
Serial.printf("data is %02x %02x %02x\n", data[0], data[1], data[2]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return crcok?1:2;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t rxdata;
|
||||||
|
static bool rxsearching=true;
|
||||||
|
|
||||||
|
// search for
|
||||||
|
// //101001100110011010011010011001100110100110101010100110101001
|
||||||
|
// //1010011001100110100110100110 0110.0110 1001.1010 1010.1001 1010.1001 => 0x669AA9A9
|
||||||
|
void M20::processM20data(uint8_t dt)
|
||||||
|
{
|
||||||
|
for(int i=0; i<8; i++) {
|
||||||
|
uint8_t d = (dt&0x80)?1:0;
|
||||||
|
dt <<= 1;
|
||||||
|
rxdata = (rxdata<<1) | d;
|
||||||
|
//uint8_t value = ((rxdata>>1)^rxdata)&0x01;
|
||||||
|
//if((rxbitc&1)==1) { rxbyte = (rxbyte>>1) + ((value)<<8); } // mancester decoded data
|
||||||
|
//rxbyte = (rxbyte>>1) | (d<<8);
|
||||||
|
if( (rxbitc&1)==0 ) {
|
||||||
|
// "bit1"
|
||||||
|
rxbyte = (rxbyte<<1) | d;
|
||||||
|
} else {
|
||||||
|
// "bit2" ==> 01 or 10 => 1, otherweise => 0
|
||||||
|
rxbyte = rxbyte ^ d;
|
||||||
|
}
|
||||||
|
//
|
||||||
|
if(rxsearching) {
|
||||||
|
if( rxdata == 0xcccca64c || rxdata == 0x333359b3 ) {
|
||||||
|
rxsearching = false;
|
||||||
|
rxbitc = 0;
|
||||||
|
rxp = 0;
|
||||||
|
#if 1
|
||||||
|
int rssi=sx1278.getRSSI();
|
||||||
|
int fei=sx1278.getFEI();
|
||||||
|
int afc=sx1278.getAFC();
|
||||||
|
Serial.print("Test: RSSI="); Serial.print(rssi);
|
||||||
|
Serial.print(" FEI="); Serial.print(fei);
|
||||||
|
Serial.print(" AFC="); Serial.println(afc);
|
||||||
|
sonde.si()->rssi = rssi;
|
||||||
|
sonde.si()->afc = afc;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
rxbitc = (rxbitc+1)%16; // 16;
|
||||||
|
if(rxbitc == 0) { // got 8 data bit
|
||||||
|
//Serial.printf("%03x ",rxbyte);
|
||||||
|
dataptr[rxp++] = rxbyte&0xff; // (rxbyte>>1)&0xff;
|
||||||
|
#if 0
|
||||||
|
if(rxp==7 && dataptr[6] != 0x65) {
|
||||||
|
Serial.printf("wrong start: %02x\n",dataptr[6]);
|
||||||
|
rxsearching = true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if(rxp>=M20_FRAMELEN) {
|
||||||
|
rxsearching = true;
|
||||||
|
haveNewFrame = decodeframeM20(dataptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int M20::receive() {
|
||||||
|
unsigned long t0 = millis();
|
||||||
|
Serial.printf("M20::receive() start at %ld\n",t0);
|
||||||
|
while( millis() - t0 < 1512 ) {
|
||||||
|
uint8_t value = sx1278.readRegister(REG_IRQ_FLAGS2);
|
||||||
|
if ( bitRead(value, 7) ) {
|
||||||
|
Serial.println("FIFO full");
|
||||||
|
}
|
||||||
|
if ( bitRead(value, 4) ) {
|
||||||
|
Serial.println("FIFO overflow");
|
||||||
|
}
|
||||||
|
if ( bitRead(value, 2) == 1 ) {
|
||||||
|
Serial.println("FIFO: ready()");
|
||||||
|
sx1278.clearIRQFlags();
|
||||||
|
}
|
||||||
|
if(bitRead(value, 6) == 0) { // while FIFO not empty
|
||||||
|
byte data = sx1278.readRegister(REG_FIFO);
|
||||||
|
//Serial.printf("%02x",data);
|
||||||
|
processM20data(data);
|
||||||
|
value = sx1278.readRegister(REG_IRQ_FLAGS2);
|
||||||
|
} else {
|
||||||
|
if(headerDetected) {
|
||||||
|
t0 = millis(); // restart timer... don't time out if header detected...
|
||||||
|
headerDetected = 0;
|
||||||
|
}
|
||||||
|
if(haveNewFrame) {
|
||||||
|
Serial.printf("M20::receive(): new frame complete after %ldms\n", millis()-t0);
|
||||||
|
printRaw(dataptr, M20_FRAMELEN);
|
||||||
|
int retval = haveNewFrame==1 ? RX_OK: RX_ERROR;
|
||||||
|
haveNewFrame = 0;
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
delay(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Serial.printf("M20::receive() timed out\n");
|
||||||
|
return RX_TIMEOUT; // TODO RX_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define M20MAXLEN (240)
|
||||||
|
int M20::waitRXcomplete() {
|
||||||
|
// called after complete...
|
||||||
|
#if 0
|
||||||
|
Serial.printf("decoding frame %d\n", lastFrame);
|
||||||
|
print_frame(lastFrame==1?data1:data2, 240);
|
||||||
|
SondeInfo *si = sonde.sondeList+rxtask.receiveSonde;
|
||||||
|
si->lat = gpx.lat;
|
||||||
|
si->lon = gpx.lon;
|
||||||
|
si->alt = gpx.alt;
|
||||||
|
si->vs = gpx.vU;
|
||||||
|
si->hs = gpx.vH;
|
||||||
|
si->dir = gpx.vD;
|
||||||
|
si->validPos = 0x3f;
|
||||||
|
memcpy(si->id, gpx.id, 9);
|
||||||
|
si->validID = true;
|
||||||
|
|
||||||
|
int res=0;
|
||||||
|
uint32_t t0 = millis();
|
||||||
|
while( rxtask.receiveResult == 0xFFFF && millis()-t0 < 2000) { delay(20); }
|
||||||
|
|
||||||
|
if( rxtask.receiveResult<0 || rxtask.receiveResult==RX_TIMEOUT) {
|
||||||
|
res = RX_TIMEOUT;
|
||||||
|
} else if ( rxtask.receiveResult==0) {
|
||||||
|
res = RX_OK;
|
||||||
|
} else {
|
||||||
|
res = RX_ERROR;
|
||||||
|
}
|
||||||
|
rxtask.receiveResult = 0xFFFF;
|
||||||
|
Serial.printf("M20::waitRXcomplete returning %d (%s)\n", res, RXstr[res]);
|
||||||
|
return res;
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
int oldwaitRXcomplete() {
|
||||||
|
Serial.println("M20: receive frame...\n");
|
||||||
|
sx1278receiveData = true;
|
||||||
|
delay(6000); // done in other task....
|
||||||
|
//sx1278receiveData = false;
|
||||||
|
#if 0
|
||||||
|
//sx1278.setPayloadLength(518-8); // Expect 320-8 bytes or 518-8 bytes (8 byte header)
|
||||||
|
//sx1278.setPayloadLength(0); // infinite for now...
|
||||||
|
|
||||||
|
////// test code for continuous reception
|
||||||
|
// sx1278.receive(); /// active FSK RX mode -- already done above...
|
||||||
|
uint8_t value = sx1278.readRegister(REG_IRQ_FLAGS2);
|
||||||
|
unsigned long previous = millis();
|
||||||
|
|
||||||
|
byte ready=0;
|
||||||
|
uint32_t wait = 8000;
|
||||||
|
// while not yet done or FIFO not yet empty
|
||||||
|
// bit 6: FIFO Empty
|
||||||
|
// bit 2 payload ready
|
||||||
|
int by=0;
|
||||||
|
while( (!ready || bitRead(value,6)==0) && (millis() - previous < wait) )
|
||||||
|
{
|
||||||
|
if( bitRead(value, 7) ) { Serial.println("FIFO full"); }
|
||||||
|
if( bitRead(value, 4) ) { Serial.println("FIFO overflow"); }
|
||||||
|
if( bitRead(value,2)==1 ) ready=1;
|
||||||
|
if( bitRead(value, 6) == 0 ) { // FIFO not empty
|
||||||
|
byte data = sx1278.readRegister(REG_FIFO);
|
||||||
|
process8N1data(data);
|
||||||
|
by++;
|
||||||
|
#if 0
|
||||||
|
if(di==1) {
|
||||||
|
int rssi=getRSSI();
|
||||||
|
int fei=getFEI();
|
||||||
|
int afc=getAFC();
|
||||||
|
Serial.print("Test: RSSI="); Serial.println(rssi);
|
||||||
|
Serial.print("Test: FEI="); Serial.println(fei);
|
||||||
|
Serial.print("Test: AFC="); Serial.println(afc);
|
||||||
|
sonde.si()->rssi = rssi;
|
||||||
|
sonde.si()->afc = afc;
|
||||||
|
}
|
||||||
|
if(di>520) {
|
||||||
|
// TODO
|
||||||
|
Serial.println("TOO MUCH DATA");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
previous = millis(); // reset timeout after receiving data
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
value = sx1278.readRegister(REG_IRQ_FLAGS2);
|
||||||
|
}
|
||||||
|
Serial.printf("processed %d bytes before end/timeout\n", by);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/////
|
||||||
|
#if 0
|
||||||
|
int e = sx1278.receivePacketTimeout(1000, data+8);
|
||||||
|
if(e) { Serial.println("TIMEOUT"); return RX_TIMEOUT; } //if timeout... return 1
|
||||||
|
|
||||||
|
printRaw(data, M20MAXLEN);
|
||||||
|
//for(int i=0; i<M20MAXLEN; i++) { data[i] = reverse(data[i]); }
|
||||||
|
//printRaw(data, MAXLEN);
|
||||||
|
//for(int i=0; i<M20MAXLEN; i++) { data[i] = data[i] ^ scramble[i&0x3F]; }
|
||||||
|
//printRaw(data, MAXLEN);
|
||||||
|
//int res = decode41(data, M20MAXLEN);
|
||||||
|
#endif
|
||||||
|
int res=0;
|
||||||
|
return res==0 ? RX_OK : RX_ERROR;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
M20 m20 = M20();
|
||||||
|
|
||||||
Executable
+96
@@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
* M20.h
|
||||||
|
* Functions for decoding Meteomodem M20 sondes with SX127x chips
|
||||||
|
* Copyright (C) 2019 Hansi Reiser, dl9rdz
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: GPL-2.0+
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef M20_h
|
||||||
|
#define M20_h
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <Arduino.h>
|
||||||
|
#ifndef inttypes_h
|
||||||
|
#include <inttypes.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
struct CONTEXTR9 {
|
||||||
|
char calibdata[512];
|
||||||
|
uint32_t calibok;
|
||||||
|
char mesok;
|
||||||
|
char posok;
|
||||||
|
char framesent;
|
||||||
|
double lat;
|
||||||
|
double lon;
|
||||||
|
double heig;
|
||||||
|
double speed;
|
||||||
|
double dir;
|
||||||
|
double climb;
|
||||||
|
double lastlat;
|
||||||
|
double laslong;
|
||||||
|
double lastalt;
|
||||||
|
double lastspeed;
|
||||||
|
double lastdir;
|
||||||
|
double lastclb;
|
||||||
|
float hrmsc;
|
||||||
|
float vrmsc;
|
||||||
|
double hp;
|
||||||
|
double hyg;
|
||||||
|
double temp;
|
||||||
|
double ozontemp;
|
||||||
|
double ozon;
|
||||||
|
uint32_t goodsats;
|
||||||
|
uint32_t timems;
|
||||||
|
uint32_t framenum;
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Main class */
|
||||||
|
class M20
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
void printRaw(uint8_t *data, int len);
|
||||||
|
void processM20data(uint8_t data);
|
||||||
|
int decodeframeM20(uint8_t *data);
|
||||||
|
#if 0
|
||||||
|
void stobyte92(uint8_t byte);
|
||||||
|
void dogps(const uint8_t *sf, int sf_len,
|
||||||
|
struct CONTEXTR9 * cont, uint32_t * timems,
|
||||||
|
uint32_t * gpstime);
|
||||||
|
uint32_t bits2val(const uint8_t *bits, int len);
|
||||||
|
int bitsToBytes(uint8_t *bits, uint8_t *bytes, int len);
|
||||||
|
int decode92(byte *data, int maxlen);
|
||||||
|
|
||||||
|
uint8_t hamming_conf[ 7*8]; // 7*8=56
|
||||||
|
uint8_t hamming_dat1[13*8]; // 13*8=104
|
||||||
|
uint8_t hamming_dat2[13*8];
|
||||||
|
|
||||||
|
uint8_t block_conf[ 7*4]; // 7*4=28
|
||||||
|
uint8_t block_dat1[13*4]; // 13*4=52
|
||||||
|
uint8_t block_dat2[13*4];
|
||||||
|
|
||||||
|
uint8_t H[4][8] = // extended Hamming(8,4) particy check matrix
|
||||||
|
{{ 0, 1, 1, 1, 1, 0, 0, 0},
|
||||||
|
{ 1, 0, 1, 1, 0, 1, 0, 0},
|
||||||
|
{ 1, 1, 0, 1, 0, 0, 1, 0},
|
||||||
|
{ 1, 1, 1, 0, 0, 0, 0, 1}};
|
||||||
|
uint8_t He[8] = { 0x7, 0xB, 0xD, 0xE, 0x8, 0x4, 0x2, 0x1}; // Spalten von H:
|
||||||
|
// 1-bit-error-Syndrome
|
||||||
|
boolean initialized = false;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
public:
|
||||||
|
M20();
|
||||||
|
int setup(float frequency);
|
||||||
|
int receive();
|
||||||
|
int waitRXcomplete();
|
||||||
|
|
||||||
|
//int use_ecc = 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern M20 m20;
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -356,7 +356,8 @@ static void posrs41(const byte b[], uint32_t b_len, uint32_t p)
|
|||||||
if (dir<0.0) dir = 360.0+dir;
|
if (dir<0.0) dir = 360.0+dir;
|
||||||
sonde.si()->dir = dir;
|
sonde.si()->dir = dir;
|
||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
sonde.si()->hs = sqrt((float)(vn*vn+ve*ve))*3.6f;
|
//sonde.si()->hs = sqrt((float)(vn*vn+ve*ve))*3.6f;
|
||||||
|
sonde.si()->hs = sqrt((float)(vn*vn+ve*ve));
|
||||||
Serial.print(sonde.si()->hs);
|
Serial.print(sonde.si()->hs);
|
||||||
Serial.print("km/h ");
|
Serial.print("km/h ");
|
||||||
Serial.print(dir);
|
Serial.print(dir);
|
||||||
|
|||||||
@@ -6,18 +6,11 @@
|
|||||||
#include "RS92.h"
|
#include "RS92.h"
|
||||||
#include "DFM.h"
|
#include "DFM.h"
|
||||||
#include "M10.h"
|
#include "M10.h"
|
||||||
|
#include "M20.h"
|
||||||
#include "SX1278FSK.h"
|
#include "SX1278FSK.h"
|
||||||
#include "Display.h"
|
#include "Display.h"
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
|
|
||||||
#define SDCARD
|
|
||||||
|
|
||||||
#ifdef SDCARD
|
|
||||||
#include "sd.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int snOn=0;
|
|
||||||
|
|
||||||
extern SX1278FSK sx1278;
|
extern SX1278FSK sx1278;
|
||||||
|
|
||||||
RXTask rxtask = { -1, -1, -1, 0xFFFF, 0 };
|
RXTask rxtask = { -1, -1, -1, 0xFFFF, 0 };
|
||||||
@@ -87,6 +80,9 @@ void Sonde::defaultConfig() {
|
|||||||
strcpy(config.gps_lat,"43.591");
|
strcpy(config.gps_lat,"43.591");
|
||||||
strcpy(config.gps_lon,"7.100");
|
strcpy(config.gps_lon,"7.100");
|
||||||
config.gps_alt=123;
|
config.gps_alt=123;
|
||||||
|
config.gps_Lat=0;
|
||||||
|
config.gps_Lon=0;
|
||||||
|
config.gps_Alt=0;
|
||||||
config.oled_rst = 16;
|
config.oled_rst = 16;
|
||||||
config.disptype = 0;
|
config.disptype = 0;
|
||||||
config.oled_orient = 1;
|
config.oled_orient = 1;
|
||||||
@@ -158,16 +154,18 @@ void Sonde::defaultConfig() {
|
|||||||
}
|
}
|
||||||
//
|
//
|
||||||
config.noisefloor = -125;
|
config.noisefloor = -125;
|
||||||
|
config.gainLNA=0;
|
||||||
strcpy(config.call,"NOCALL");
|
strcpy(config.call,"NOCALL");
|
||||||
strcpy(config.passcode, "---");
|
strcpy(config.passcode, "---");
|
||||||
strcpy(config.mdnsname, "radiosonde");
|
strcpy(config.mdnsname, "radiosonde");
|
||||||
strcpy(config.vbatmax,"1.84");
|
strcpy(config.vbatmax,"1.84");
|
||||||
strcpy(config.vbatmin,"1.64");
|
strcpy(config.vbatmin,"1.64");
|
||||||
config.sdOn=0;
|
config.telemetryOn=0;
|
||||||
config.buzzerOn=0;
|
config.buzzerOn=0;
|
||||||
config.buzzerFreq=700;
|
config.buzzerFreq=700;
|
||||||
config.buzzerPort=12;
|
config.buzzerPort=12;
|
||||||
config.dbsmetre=0;
|
config.dbsmetre=0;
|
||||||
|
config.degdec=0;
|
||||||
config.maxsonde=15;
|
config.maxsonde=15;
|
||||||
config.debug=0;
|
config.debug=0;
|
||||||
config.wifi=1;
|
config.wifi=1;
|
||||||
@@ -215,6 +213,8 @@ void Sonde::setConfig(const char *cfg) {
|
|||||||
if(strcmp(cfg,"noisefloor")==0) {
|
if(strcmp(cfg,"noisefloor")==0) {
|
||||||
config.noisefloor = atoi(val);
|
config.noisefloor = atoi(val);
|
||||||
if(config.noisefloor==0) config.noisefloor=-130;
|
if(config.noisefloor==0) config.noisefloor=-130;
|
||||||
|
} else if(strcmp(cfg,"gainLNA")==0) {
|
||||||
|
config.gainLNA = atoi(val);
|
||||||
} else if(strcmp(cfg,"call")==0) {
|
} else if(strcmp(cfg,"call")==0) {
|
||||||
strncpy(config.call, val, 9);
|
strncpy(config.call, val, 9);
|
||||||
config.call[9]=0;
|
config.call[9]=0;
|
||||||
@@ -269,8 +269,8 @@ void Sonde::setConfig(const char *cfg) {
|
|||||||
strncpy(config.vbatmax,val,5);
|
strncpy(config.vbatmax,val,5);
|
||||||
} else if(strcmp(cfg,"vbatmin")==0) {
|
} else if(strcmp(cfg,"vbatmin")==0) {
|
||||||
strncpy(config.vbatmin,val,5);
|
strncpy(config.vbatmin,val,5);
|
||||||
} else if(strcmp(cfg,"sdOn")==0) {
|
} else if(strcmp(cfg,"telemetryOn")==0) {
|
||||||
config.sdOn=atoi(val);
|
config.telemetryOn=atoi(val);
|
||||||
} else if(strcmp(cfg,"buzzerOn")==0) {
|
} else if(strcmp(cfg,"buzzerOn")==0) {
|
||||||
config.buzzerOn=atoi(val);
|
config.buzzerOn=atoi(val);
|
||||||
} else if(strcmp(cfg,"buzzerPort")==0) {
|
} else if(strcmp(cfg,"buzzerPort")==0) {
|
||||||
@@ -279,6 +279,8 @@ void Sonde::setConfig(const char *cfg) {
|
|||||||
config.buzzerFreq=atoi(val);
|
config.buzzerFreq=atoi(val);
|
||||||
} else if(strcmp(cfg,"dbsmetre")==0) {
|
} else if(strcmp(cfg,"dbsmetre")==0) {
|
||||||
config.dbsmetre=atoi(val);
|
config.dbsmetre=atoi(val);
|
||||||
|
} else if(strcmp(cfg,"degdec")==0) {
|
||||||
|
config.degdec=atoi(val);
|
||||||
} else if(strcmp(cfg,"display")==0) {
|
} else if(strcmp(cfg,"display")==0) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
char *ptr;
|
char *ptr;
|
||||||
@@ -427,6 +429,7 @@ void Sonde::setup() {
|
|||||||
break;
|
break;
|
||||||
case STYPE_DFM06:
|
case STYPE_DFM06:
|
||||||
case STYPE_DFM09:
|
case STYPE_DFM09:
|
||||||
|
case STYPE_DFM:
|
||||||
dfm.setup( sondeList[rxtask.currentSonde].freq * 1000000, sondeList[rxtask.currentSonde].type==STYPE_DFM06?0:1 );
|
dfm.setup( sondeList[rxtask.currentSonde].freq * 1000000, sondeList[rxtask.currentSonde].type==STYPE_DFM06?0:1 );
|
||||||
break;
|
break;
|
||||||
case STYPE_RS92:
|
case STYPE_RS92:
|
||||||
@@ -435,6 +438,9 @@ void Sonde::setup() {
|
|||||||
case STYPE_M10:
|
case STYPE_M10:
|
||||||
m10.setup( sondeList[rxtask.currentSonde].freq * 1000000);
|
m10.setup( sondeList[rxtask.currentSonde].freq * 1000000);
|
||||||
break;
|
break;
|
||||||
|
case STYPE_M20:
|
||||||
|
m20.setup( sondeList[rxtask.currentSonde].freq * 1000000);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
// debug
|
// debug
|
||||||
float afcbw = sx1278.getAFCBandwidth();
|
float afcbw = sx1278.getAFCBandwidth();
|
||||||
@@ -457,6 +463,10 @@ void Sonde::receive() {
|
|||||||
case STYPE_M10:
|
case STYPE_M10:
|
||||||
res = m10.receive();
|
res = m10.receive();
|
||||||
break;
|
break;
|
||||||
|
case STYPE_M20:
|
||||||
|
res = m20.receive();
|
||||||
|
break;
|
||||||
|
case STYPE_DFM:
|
||||||
case STYPE_DFM06:
|
case STYPE_DFM06:
|
||||||
case STYPE_DFM09:
|
case STYPE_DFM09:
|
||||||
res = dfm.receive();
|
res = dfm.receive();
|
||||||
@@ -466,7 +476,9 @@ void Sonde::receive() {
|
|||||||
// state information for RX_TIMER / NORX_TIMER events
|
// state information for RX_TIMER / NORX_TIMER events
|
||||||
if(res==0) { // RX OK
|
if(res==0) { // RX OK
|
||||||
flashLed(700);
|
flashLed(700);
|
||||||
if(sonde.config.buzzerOn==1) buzzerLed(500);
|
if(sonde.config.buzzerOn==1) {
|
||||||
|
buzzerLed(500);
|
||||||
|
}
|
||||||
if(si->lastState != 1) {
|
if(si->lastState != 1) {
|
||||||
si->rxStart = millis();
|
si->rxStart = millis();
|
||||||
si->lastState = 1;
|
si->lastState = 1;
|
||||||
@@ -511,11 +523,7 @@ void Sonde::receive() {
|
|||||||
Serial.printf("receive Result is %04x\n", res);
|
Serial.printf("receive Result is %04x\n", res);
|
||||||
// let waitRXcomplete resume...
|
// let waitRXcomplete resume...
|
||||||
rxtask.receiveResult = res;
|
rxtask.receiveResult = res;
|
||||||
#ifdef SDCARD
|
|
||||||
if (sonde.config.sdOn==1){
|
|
||||||
sdwrite();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// return (action<<8) | (rxresult)
|
// return (action<<8) | (rxresult)
|
||||||
@@ -551,6 +559,10 @@ rxloop:
|
|||||||
case STYPE_M10:
|
case STYPE_M10:
|
||||||
m10.waitRXcomplete();
|
m10.waitRXcomplete();
|
||||||
break;
|
break;
|
||||||
|
case STYPE_M20:
|
||||||
|
m20.waitRXcomplete();
|
||||||
|
break;
|
||||||
|
case STYPE_DFM:
|
||||||
case STYPE_DFM06:
|
case STYPE_DFM06:
|
||||||
case STYPE_DFM09:
|
case STYPE_DFM09:
|
||||||
dfm.waitRXcomplete();
|
dfm.waitRXcomplete();
|
||||||
|
|||||||
@@ -46,9 +46,10 @@ extern const char *RXstr[];
|
|||||||
// 01000000 => goto sonde -1
|
// 01000000 => goto sonde -1
|
||||||
// 01000001 => goto sonde +1
|
// 01000001 => goto sonde +1
|
||||||
|
|
||||||
#define NSondeTypes 5
|
#define NSondeTypes 7
|
||||||
enum SondeType { STYPE_DFM06, STYPE_DFM09, STYPE_RS41, STYPE_RS92, STYPE_M10 };
|
enum SondeType { STYPE_DFM06, STYPE_DFM09, STYPE_RS41, STYPE_RS92, STYPE_M10, STYPE_M20, STYPE_DFM };
|
||||||
extern const char *sondeTypeStr[NSondeTypes];
|
extern const char *sondeTypeStr[NSondeTypes];
|
||||||
|
extern const char sondeTypeChar[NSondeTypes];
|
||||||
|
|
||||||
typedef struct st_sondeinfo {
|
typedef struct st_sondeinfo {
|
||||||
// receiver configuration
|
// receiver configuration
|
||||||
@@ -56,6 +57,7 @@ typedef struct st_sondeinfo {
|
|||||||
SondeType type;
|
SondeType type;
|
||||||
float freq;
|
float freq;
|
||||||
// decoded ID
|
// decoded ID
|
||||||
|
char typestr[5]; // decoded type (use type if *typestr==0)
|
||||||
char id[10];
|
char id[10];
|
||||||
char ser[12];
|
char ser[12];
|
||||||
bool validID;
|
bool validID;
|
||||||
@@ -65,9 +67,12 @@ typedef struct st_sondeinfo {
|
|||||||
float lon; // longitude
|
float lon; // longitude
|
||||||
float az; // azimut
|
float az; // azimut
|
||||||
float vbat; // vbat %
|
float vbat; // vbat %
|
||||||
|
int durvolheure; // duree vol heure
|
||||||
|
int durvolminute; // duree vol minute
|
||||||
|
int durvolseconde; // duree vol seconde
|
||||||
float alt; // altitude
|
float alt; // altitude
|
||||||
float vs; // vertical speed
|
float vs; // vertical speed in m/s
|
||||||
float hs; // horizontal speed
|
float hs; // horizontal speed in m/s
|
||||||
float dir; // 0..360
|
float dir; // 0..360
|
||||||
uint8_t sats; // number of sats
|
uint8_t sats; // number of sats
|
||||||
uint8_t validPos; // bit pattern for validity of above 7 fields; 0x80: position is old
|
uint8_t validPos; // bit pattern for validity of above 7 fields; 0x80: position is old
|
||||||
@@ -166,6 +171,7 @@ typedef struct st_rdzconfig {
|
|||||||
char gps_lat[20]; // QTH no gps latitude
|
char gps_lat[20]; // QTH no gps latitude
|
||||||
char gps_lon[20]; // QTH no gps longitude
|
char gps_lon[20]; // QTH no gps longitude
|
||||||
int gps_alt; // QTH no gps altitude
|
int gps_alt; // QTH no gps altitude
|
||||||
|
float gps_Lat,gps_Lon, gps_Alt; // QTH gps OM lat, lon, alt
|
||||||
// software configuration
|
// software configuration
|
||||||
int debug; // show port and config options after reboot
|
int debug; // show port and config options after reboot
|
||||||
int wifi; // connect to known WLAN 0=skip
|
int wifi; // connect to known WLAN 0=skip
|
||||||
@@ -178,14 +184,16 @@ typedef struct st_rdzconfig {
|
|||||||
int maxsonde; // number of max sonde in scan (range=1-99)
|
int maxsonde; // number of max sonde in scan (range=1-99)
|
||||||
int norx_timeout; // Time after which rx mode switches to scan mode (without rx signal)
|
int norx_timeout; // Time after which rx mode switches to scan mode (without rx signal)
|
||||||
int noisefloor; // for spectrum display
|
int noisefloor; // for spectrum display
|
||||||
char mdnsname[15]; // mDNS-Name, defaults to rdzsonde
|
int gainLNA;
|
||||||
|
char mdnsname[15]; // mDNS-Name, defaults to radiosonde
|
||||||
char vbatmax[5]; // Vbat maxi when bat charged
|
char vbatmax[5]; // Vbat maxi when bat charged
|
||||||
char vbatmin[5]; // Vbat minimum discharged
|
char vbatmin[5]; // Vbat minimum discharged
|
||||||
int sdOn; // Active SDCard
|
int telemetryOn; // Active Save information telemetry
|
||||||
int buzzerPort; // Buzzer port
|
int buzzerPort; // Buzzer port
|
||||||
int buzzerFreq; // Buzzer Frequency
|
int buzzerFreq; // Buzzer Frequency
|
||||||
int buzzerOn; // Buzzer On
|
int buzzerOn; // Buzzer On
|
||||||
int dbsmetre; // Db or Smetre display
|
int dbsmetre; // Db or Smetre display
|
||||||
|
int degdec; // Degres or Decimal 0=decimal 1=degres
|
||||||
// receiver configuration
|
// receiver configuration
|
||||||
int showafc; // show afc value in rx screen
|
int showafc; // show afc value in rx screen
|
||||||
int freqofs; // frequency offset (tuner config = rx frequency + freqofs) in Hz
|
int freqofs; // frequency offset (tuner config = rx frequency + freqofs) in Hz
|
||||||
|
|||||||
@@ -317,7 +317,7 @@ char *aprs_senddata(SondeInfo *s, const char *usercall, const char *sym) {
|
|||||||
char comm[100];
|
char comm[100];
|
||||||
snprintf(comm, 100, "Clb=%.1fm/s %.3fMHz Type=%s", s->vs, s->freq, sondeTypeStr[s->type]);
|
snprintf(comm, 100, "Clb=%.1fm/s %.3fMHz Type=%s", s->vs, s->freq, sondeTypeStr[s->type]);
|
||||||
strcat(b, comm);
|
strcat(b, comm);
|
||||||
if(s->type==STYPE_M10||s->type==STYPE_DFM06||s->type==STYPE_DFM09) {
|
if(s->type==STYPE_M20||s->type==STYPE_M10||s->type==STYPE_DFM||s->type==STYPE_DFM06||s->type==STYPE_DFM09) {
|
||||||
snprintf(comm, 100, " ser=%s", s->ser);
|
snprintf(comm, 100, " ser=%s", s->ser);
|
||||||
strcat(b, comm);
|
strcat(b, comm);
|
||||||
}
|
}
|
||||||
|
|||||||
Executable
+124
@@ -0,0 +1,124 @@
|
|||||||
|
#include "FS.h"
|
||||||
|
#include <SPIFFS.h>
|
||||||
|
#include <WiFi.h>
|
||||||
|
//Write data web
|
||||||
|
|
||||||
|
//Fonction
|
||||||
|
void writedataweb(){
|
||||||
|
|
||||||
|
|
||||||
|
String updateHost = "xavier.debert.free.fr";
|
||||||
|
String updateDataWeb = "/RS/dataweb/index.html.txt";
|
||||||
|
String *updateData = &updateDataWeb;
|
||||||
|
String dispHost = updateHost.substring(0, 14);
|
||||||
|
//disp.rdis->drawString(2, 0, dispHost.c_str());
|
||||||
|
|
||||||
|
Serial.println("Connecting to: " + updateHost);
|
||||||
|
// Connect to Update host
|
||||||
|
if (client.connect(updateHost.c_str(), updatePort)) {
|
||||||
|
// Connection succeeded, fecthing the bin
|
||||||
|
Serial.println("Fetching index.html: " + String(*updateData));
|
||||||
|
|
||||||
|
|
||||||
|
// Get the contents of the bin file
|
||||||
|
client.print(String("GET ") + *updateData + " HTTP/1.1\r\n" +
|
||||||
|
"Host: " + updateHost + "\r\n" +
|
||||||
|
"Cache-Control: no-cache\r\n" +
|
||||||
|
"Connection: close\r\n\r\n");
|
||||||
|
|
||||||
|
// Check what is being sent
|
||||||
|
// Serial.print(String("GET ") + bin + " HTTP/1.1\r\n" +
|
||||||
|
// "Host: " + host + "\r\n" +
|
||||||
|
// "Cache-Control: no-cache\r\n" +
|
||||||
|
// "Connection: close\r\n\r\n");
|
||||||
|
|
||||||
|
unsigned long timeout = millis();
|
||||||
|
while (client.available() == 0) {
|
||||||
|
if (millis() - timeout > 5000) {
|
||||||
|
Serial.println("Client Timeout !");
|
||||||
|
client.stop();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Once the response is available,
|
||||||
|
// check stuff
|
||||||
|
|
||||||
|
/*
|
||||||
|
Response Structure
|
||||||
|
HTTP/1.1 200 OK
|
||||||
|
x-amz-id-2: NVKxnU1aIQMmpGKhSwpCBh8y2JPbak18QLIfE+OiUDOos+7UftZKjtCFqrwsGOZRN5Zee0jpTd0=
|
||||||
|
x-amz-request-id: 2D56B47560B764EC
|
||||||
|
Date: Wed, 14 Jun 2017 03:33:59 GMT
|
||||||
|
Last-Modified: Fri, 02 Jun 2017 14:50:11 GMT
|
||||||
|
ETag: "d2afebbaaebc38cd669ce36727152af9"
|
||||||
|
Accept-Ranges: bytes
|
||||||
|
Content-Type: application/octet-stream
|
||||||
|
Content-Length: 357280
|
||||||
|
Server: AmazonS3
|
||||||
|
|
||||||
|
{{BIN FILE CONTENTS}}
|
||||||
|
|
||||||
|
*/
|
||||||
|
while (client.available()) {
|
||||||
|
// read line till /n
|
||||||
|
String line = client.readStringUntil('\n');
|
||||||
|
// remove space, to check if the line is end of headers
|
||||||
|
line.trim();
|
||||||
|
|
||||||
|
// if the the line is empty,
|
||||||
|
// this is end of headers
|
||||||
|
// break the while and feed the
|
||||||
|
// remaining `client` to the
|
||||||
|
// Update.writeStream();
|
||||||
|
if (!line.length()) {
|
||||||
|
//headers ended
|
||||||
|
break; // and get the OTA started
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the HTTP Response is 200
|
||||||
|
// else break and Exit Update
|
||||||
|
if (line.startsWith("HTTP/1.1")) {
|
||||||
|
if (line.indexOf("200") < 0) {
|
||||||
|
Serial.println("Got a non 200 status code from server. error.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// extract headers here
|
||||||
|
// Start with content length
|
||||||
|
if (line.startsWith("Content-Length: ")) {
|
||||||
|
contentLength = atoi((getHeaderValue(line, "Content-Length: ")).c_str());
|
||||||
|
Serial.println("Got " + String(contentLength) + " bytes from server");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next, the content type
|
||||||
|
if (line.startsWith("Content-Type: ")) {
|
||||||
|
String contentType = getHeaderValue(line, "Content-Type: ");
|
||||||
|
Serial.println("Got " + contentType + " payload.");
|
||||||
|
if (contentType == "application/text") {
|
||||||
|
isValidContentType = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Connect to updateHost failed
|
||||||
|
// May be try?
|
||||||
|
// Probably a choppy network?
|
||||||
|
Serial.println("Connection to " + String(updateHost) + " failed. Please check your setup");
|
||||||
|
// retry??
|
||||||
|
// execOTA();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Serial.printf("\nDataWeb On\n");
|
||||||
|
//open file for appending new blank line to EOF.
|
||||||
|
File f = SPIFFS.open("/data.html", "w");
|
||||||
|
|
||||||
|
f.println("<html><body>TEST de DATAWeb1</body></html>");
|
||||||
|
|
||||||
|
f.close();
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -136,11 +136,15 @@ void geteph() {
|
|||||||
int len=0;
|
int len=0;
|
||||||
while(dclient.connected()) {
|
while(dclient.connected()) {
|
||||||
while(dclient.available()) {
|
while(dclient.available()) {
|
||||||
char c = dclient.read();
|
int c = dclient.read();
|
||||||
|
if(c==-1) {
|
||||||
|
Serial.println("dclient.read() returned -1 inspite of available() being true?!");
|
||||||
|
} else {
|
||||||
fh.write(c);
|
fh.write(c);
|
||||||
len++;
|
len++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Serial.printf("fetched %d bytes\n", len);
|
Serial.printf("fetched %d bytes\n", len);
|
||||||
fh.close();
|
fh.close();
|
||||||
snprintf(buf, 16, "Fetched %d B ",len);
|
snprintf(buf, 16, "Fetched %d B ",len);
|
||||||
|
|||||||
@@ -1,179 +0,0 @@
|
|||||||
#include "FS.h"
|
|
||||||
#include "SD_MMC.h"
|
|
||||||
//Write data RadioSonde on SD
|
|
||||||
|
|
||||||
//Fonction de gestion SD
|
|
||||||
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
|
|
||||||
Serial.printf("Listing directory: %s\n", dirname);
|
|
||||||
|
|
||||||
File root = fs.open(dirname);
|
|
||||||
if(!root){
|
|
||||||
Serial.println("Failed to open directory");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(!root.isDirectory()){
|
|
||||||
Serial.println("Not a directory");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
File file = root.openNextFile();
|
|
||||||
while(file){
|
|
||||||
if(file.isDirectory()){
|
|
||||||
Serial.print(" DIR : ");
|
|
||||||
Serial.println(file.name());
|
|
||||||
if(levels){
|
|
||||||
listDir(fs, file.name(), levels -1);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Serial.print(" FILE: ");
|
|
||||||
Serial.print(file.name());
|
|
||||||
Serial.print(" SIZE: ");
|
|
||||||
Serial.println(file.size());
|
|
||||||
}
|
|
||||||
file = root.openNextFile();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void createDir(fs::FS &fs, const char * path){
|
|
||||||
Serial.printf("Creating Dir: %s\n", path);
|
|
||||||
if(fs.mkdir(path)){
|
|
||||||
Serial.println("Dir created");
|
|
||||||
} else {
|
|
||||||
Serial.println("mkdir failed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void readFile(fs::FS &fs, const char * path){
|
|
||||||
Serial.printf("Reading file: %s\n", path);
|
|
||||||
|
|
||||||
File file = fs.open(path);
|
|
||||||
if(!file){
|
|
||||||
Serial.println("Failed to open file for reading");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.print("Read from file: ");
|
|
||||||
while(file.available()){
|
|
||||||
Serial.write(file.read());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void appendFile(fs::FS &fs, const char * path, const char * message){
|
|
||||||
Serial.printf("Appending to file: %s\n", path);
|
|
||||||
|
|
||||||
File file = fs.open(path, FILE_APPEND);
|
|
||||||
if(!file){
|
|
||||||
Serial.println("Failed to open file for appending");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(file.print(message)){
|
|
||||||
Serial.println("Message appended");
|
|
||||||
} else {
|
|
||||||
Serial.println("Append failed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void writeFile(fs::FS &fs, const char * path, const char * message){
|
|
||||||
Serial.printf("Writing file: %s\n", path);
|
|
||||||
|
|
||||||
File file = fs.open(path, FILE_WRITE);
|
|
||||||
if(!file){
|
|
||||||
Serial.println("Failed to open file for writing");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(file.print(message)){
|
|
||||||
Serial.println("File written");
|
|
||||||
} else {
|
|
||||||
Serial.println("Write failed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void sdwrite()
|
|
||||||
{
|
|
||||||
Serial.print("Detect SD\n");
|
|
||||||
if(SD_MMC.begin()){
|
|
||||||
|
|
||||||
|
|
||||||
uint8_t cardType = SD_MMC.cardType();
|
|
||||||
|
|
||||||
if(cardType != CARD_NONE){
|
|
||||||
|
|
||||||
|
|
||||||
Serial.print("SD_MMC Card Type: ");
|
|
||||||
if(cardType == CARD_MMC){
|
|
||||||
Serial.println("MMC");
|
|
||||||
} else if(cardType == CARD_SD){
|
|
||||||
Serial.println("SDSC");
|
|
||||||
} else if(cardType == CARD_SDHC){
|
|
||||||
Serial.println("SDHC");
|
|
||||||
} else {
|
|
||||||
Serial.println("UNKNOWN");
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024);
|
|
||||||
Serial.printf("SD_MMC Card Size: %lluMB\n", cardSize);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
Serial.printf("Total space: %lluMB\n", SD_MMC.totalBytes() / (1024 * 1024));
|
|
||||||
Serial.printf("Used space: %lluMB\n", SD_MMC.usedBytes() / (1024 * 1024));
|
|
||||||
*/
|
|
||||||
if (sonde.si()->validPos) {
|
|
||||||
char buff[20];
|
|
||||||
char chemin[40];
|
|
||||||
//String repertoire="/radiosonde/", name=sonde.si()->ser;
|
|
||||||
//repertoire.toCharArray(chemin, 40);
|
|
||||||
//if (!SD_MMC.exists(chemin)) {
|
|
||||||
createDir(SD_MMC, "/radiosonde/");
|
|
||||||
Serial.printf("\nWrite Rep\n");
|
|
||||||
//listDir(SD_MMC, chemin, 0);
|
|
||||||
//}
|
|
||||||
|
|
||||||
//if (name.length()>8) {
|
|
||||||
//name.concat(".csv");
|
|
||||||
//repertoire.concat(name);
|
|
||||||
//repertoire.toCharArray(chemin, 40);
|
|
||||||
writeFile(SD_MMC, "/radiosonde/sonde.csv", "");
|
|
||||||
Serial.printf("\nWrite file\n");
|
|
||||||
//if (!SD_MMC.exists(chemin)) {
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", "RadioSonde N; Ser ; Type ; Freq ; Alt ; VSpeed ; HSpeed ; Dir ; LaunchSite ; RSSI\n");
|
|
||||||
Serial.printf("\nWrite titre\n");
|
|
||||||
//}
|
|
||||||
Serial.printf("\nWrite Data\n");
|
|
||||||
snprintf(buff, 16, "%10c", sonde.si()->id);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
|
|
||||||
snprintf(buff, 16, "%12c", sonde.si()->ser);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", sondeTypeStr[sonde.si()->type]);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
|
|
||||||
snprintf(buff, 16, "%3.3fMhz", sonde.si()->freq);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
|
|
||||||
snprintf(buff, 16, sonde.si()->alt>=1000?" %5.0fm":" %3.1fm", sonde.si()->alt);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
|
|
||||||
snprintf(buff, 16, "%2.2fm/s", sonde.si()->vs);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
|
|
||||||
snprintf(buff, 16, "%2.2fm/s", sonde.si()->hs);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
|
|
||||||
snprintf(buff, 16, "%3.0f°", sonde.si()->dir);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
|
|
||||||
snprintf(buff, 16, "%18c", sonde.si()->launchsite);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", ";");
|
|
||||||
snprintf(buff, 16, "%d", sonde.si()->rssi);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", buff);
|
|
||||||
appendFile(SD_MMC, "/radiosonde/sonde.csv", "\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Executable
+36
@@ -0,0 +1,36 @@
|
|||||||
|
#include <FS.h>
|
||||||
|
#include <SPIFFS.h>
|
||||||
|
#include <SD_MMC.h> // (or SD_MMC.h)
|
||||||
|
|
||||||
|
|
||||||
|
void transfert_sd() {
|
||||||
|
|
||||||
|
if (!SD_MMC.begin()) {
|
||||||
|
Serial.println("Carte SD introuvable");
|
||||||
|
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
Serial.println("Carte SD détectée");
|
||||||
|
sonde.clearDisplay();
|
||||||
|
disp.rdis->drawString(0, 2, "Carte SD On");
|
||||||
|
disp.rdis->drawString(0, 4, "Transfert Data");
|
||||||
|
File sourceFile = SPIFFS.open("/data.csv", "r");
|
||||||
|
File destFile = SD_MMC.open("/data.csv","w");
|
||||||
|
|
||||||
|
static uint8_t tanpon[512];
|
||||||
|
while( sourceFile.read( tanpon, 512) ) {
|
||||||
|
destFile.write( tanpon, 512 );
|
||||||
|
}
|
||||||
|
destFile.close();
|
||||||
|
sourceFile.close();
|
||||||
|
delay(1000);
|
||||||
|
//sonde.updateDisplay();
|
||||||
|
sonde.clearDisplay();
|
||||||
|
disp.rdis->drawString(0, 2, "Sortir SD");
|
||||||
|
disp.rdis->drawString(0, 4, "Reboot 5s");
|
||||||
|
delay(5000);
|
||||||
|
ESP.restart();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user