Skip to content

Instantly share code, notes, and snippets.

@TareObjects
Created January 11, 2016 11:34
Show Gist options
  • Select an option

  • Save TareObjects/6f0d62d73c5efbb24d29 to your computer and use it in GitHub Desktop.

Select an option

Save TareObjects/6f0d62d73c5efbb24d29 to your computer and use it in GitHub Desktop.
#define USE_US_TIMER 1
extern "C" {
#include <spi.h>
#include <spi_register.h>
#include "user_interface.h"
}
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
static WiFiUDP wifiUdp;
static const char *kRemoteIpadr = "192.168.0.70";
static const int kRmoteUdpPort = 5431;
#define MaxBuffer 256
#define ShouldSend0 0
#define ShouldSend1 1
#define ShouldNotSend 2
int shouldSend = ShouldNotSend;
int selector = 0;
unsigned short *buf[2];
static unsigned short buf1[MaxBuffer];
static unsigned short buf2[MaxBuffer];
int counter[2];
ETSTimer Timer;
int seq = 0;
int temp = 0;
void fetchFunction(void *temp) {
buf[selector][counter[selector]++] = check(0);
if (counter[selector] >= MaxBuffer) {
buf[selector][0] = counter[selector];
shouldSend = selector;
buf[selector][1] = seq++;
if (seq > 500) seq = 0;
selector = 1 - selector;
counter[selector] = 1; // start from 1. [0] is counter
}
}
void setup() {
Serial.begin(115200);
Serial.print("\n");
system_timer_reinit(); // to use os_timer_arm_us
//
// wifi setup
//
static const int kLocalPort = 7000;
WiFi.begin("ssid", "password");
while( WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print('.');
}
Serial.println("connected");
wifiUdp.begin(kLocalPort);
//
// buffer clean up
//
buf[0] = buf1;
buf[1] = buf2;
counter[0] = counter[1] = 1;
selector = 0;
//
// adc setup
//
spi_init(HSPI);
Serial.println(" end.");
// fetch every 20uSec
os_timer_setfn(&Timer, fetchFunction, NULL);
os_timer_arm_us(&Timer, 20, true);
}
void loop() {
if (shouldSend != ShouldNotSend) {
wifiUdp.beginPacket(kRemoteIpadr, kRmoteUdpPort);
char *conv = (char *)buf[shouldSend];
wifiUdp.write(conv, counter[shouldSend]*2);
wifiUdp.endPacket();
Serial.print("should send = ");
Serial.print(shouldSend);
Serial.print(", counter = ");
Serial.print(counter[shouldSend]);
Serial.print(", size = ");
Serial.println(sizeof(uint16));
shouldSend = ShouldNotSend;
}
}
uint32 check(int channel) {
// start bit (1 bit) : 1
// SGL/DIFF bit (1 bit) : SGL:1
// select the input channel (3 bit) : CH0:0, CH1:1
uint8 cmd = channel == 0 ? 0b1101 : 0b1111;
const uint32 COMMAND_LENGTH = 4;
const uint32 RESPONSE_LENGTH = 12;
uint32 retval = spi_transaction(HSPI, 0, 0, 0, 0, COMMAND_LENGTH, cmd, RESPONSE_LENGTH, 0);
retval = retval & 0x3FF; // mask to 10-bit value
return retval;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment