Skip to content

Commit

Permalink
[TEST] Watch clock edges
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBowman committed Jun 12, 2023
1 parent 57dabb9 commit 1fd91c3
Showing 1 changed file with 84 additions and 34 deletions.
118 changes: 84 additions & 34 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,32 @@
**/

#include "mbed.h"
#include "LPC17xx.h"

PinName sensorClockPin = p9;
PinName sensorDataPin = p10;

// DigitalOut led(LED1);

class SensorInterruptHandler
{
public:
SensorInterruptHandler(PinName clockPin, PinName dataPin, Thread & _thread)
: clock(clockPin),
data(dataPin),
clockIn(clockPin),
dataIn(dataPin),
thread(_thread),
isTransmittingFrame(false),
isStartPulse(false),
clockState(true),
dataState(true),
index(FRAME_LENGTH),
buffer(SIGNAL_BIT)
{
clock.fall(callback(this, &SensorInterruptHandler::onClockFallingEdge));
clock.rise(callback(this, &SensorInterruptHandler::onClockRisingEdge));

data.fall(callback(this, &SensorInterruptHandler::onDataFallingEdge));
data.rise(callback(this, &SensorInterruptHandler::onDataRisingEdge));
}
Expand All @@ -29,17 +38,34 @@ class SensorInterruptHandler
static const uint32_t SIGNAL_BIT = 0x10000000U; // don't use 0x80000000U as it is `osFlagsError`

private:
void onClockFallingEdge()
{
clockState = false;
}

void onClockRisingEdge()
{
clockState = true;

if (isTransmittingFrame)
{
buffer |= data << --index;
index--;

if (!dataState)
{
buffer = buffer | (1UL << index);
}
else
{
buffer = buffer | ((index % 2 == 0) << index);
}

if (index == 0)
{
isTransmittingFrame = false;
thread.flags_set(buffer);
index = FRAME_LENGTH;
// led = 0;
buffer = SIGNAL_BIT;
}
}
Expand All @@ -52,27 +78,46 @@ class SensorInterruptHandler

void onDataFallingEdge()
{
if (!isTransmittingFrame && clock == 1)
dataState = false;

if (!isTransmittingFrame && clockState)
{
isStartPulse = true;
}
}

void onDataRisingEdge()
{
if (!isTransmittingFrame && isStartPulse && clock == 1)
dataState = true;

if (!isTransmittingFrame && isStartPulse && clockState)
{
isStartPulse = false;
isTransmittingFrame = true;
// led = 1;
// buffer = SIGNAL_BIT;
}

// buffer |= (dataState ? 1 : 0) << --index;

// if (index == 0)
// {
// thread.flags_set(buffer);
// index = FRAME_LENGTH;
// buffer = SIGNAL_BIT;
// }
}

InterruptIn clock;
InterruptIn data;
DigitalIn clockIn;
DigitalIn dataIn;
Thread & thread;

volatile bool isTransmittingFrame;
volatile bool isStartPulse; // pulse high-low-high on DATA while DLCK is high
volatile bool clockState;
volatile bool dataState;
volatile int index;
volatile uint32_t buffer;
};
Expand All @@ -98,36 +143,38 @@ void sensorWorker(SensorInterruptHandler * handler)

int16_t data = static_cast<int16_t>(rawData);

switch (rawChannel)
{
case VOLTAGE_LEVEL:
printf("Voltage level: %d\n", data);
break;
case FX:
printf("Fx: %d\n", data);
break;
case FY:
printf("Fy: %d\n", data);
break;
case FZ:
printf("Fz: %d\n", data);
break;
case MX:
printf("Mx: %d\n", data);
break;
case MY:
printf("My: %d\n", data);
break;
case MZ:
printf("Mz: %d\n", data);
break;
case CALIBRATION:
printf("Calibration: %d\n", data);
break;
default:
printf("Unknown channel: %d\n", data);
break;
}
printf("0x%08x 0x%08x 0x%02x\n", rawData, data, rawChannel);

// switch (rawChannel)
// {
// case VOLTAGE_LEVEL:
// printf("Voltage level: %d\n", data);
// break;
// case FX:
// printf("Fx: %d\n", data);
// break;
// case FY:
// printf("Fy: %d\n", data);
// break;
// case FZ:
// printf("Fz: %d\n", data);
// break;
// case MX:
// printf("Mx: %d\n", data);
// break;
// case MY:
// printf("My: %d\n", data);
// break;
// case MZ:
// printf("Mz: %d\n", data);
// break;
// case CALIBRATION:
// printf("Calibration: %d\n", data);
// break;
// default:
// printf("Unknown channel: %d\n", data);
// break;
// }
}
}

Expand All @@ -139,7 +186,10 @@ int main()
sensorThread.start(callback(sensorWorker, &sensorInterrupt));

while (true)
{}
{
printf("loop\n");
ThisThread::sleep_for(1000);
}

sensorThread.join();
}

0 comments on commit 1fd91c3

Please sign in to comment.