Skip to content

Commit

Permalink
[TEST] Use while loops to await pin state change
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBowman committed Jul 10, 2023
1 parent 5106d7d commit 2d55e45
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 205 deletions.
83 changes: 83 additions & 0 deletions JR3.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#ifndef __JR3_HPP__
#define __JR3_HPP__

#include "FastIO.h"

template <PinName clockPin, PinName dataPin>
class JR3
{
public:
uint32_t readFrame();

private:
void awaitNextFrame();

static const unsigned int FRAME_SIZE = 20;

FastIn<clockPin> clock;
FastIn<dataPin> data;
};

template <PinName clockPin, PinName dataPin>
inline uint32_t JR3<clockPin, dataPin>::readFrame()
{
uint32_t frame = 0;

awaitNextFrame();

for (int i = FRAME_SIZE - 1; i >= 0; i--)
{
// await end of previous pulse
while (clock.read() == 1) {}

// await rising edge on clock signal
while (clock.read() == 0) {}

if (data.read())
{
frame |= (1 << i);
}
}

return frame;
}

template <PinName clockPin, PinName dataPin>
inline void JR3<clockPin, dataPin>::awaitNextFrame()
{
int clockState, dataState;

while (true)
{
// await next interval between frames
while (!(data.read() == 1 && clock.read() == 1)) {}

// await beginning of start pulse
while ((dataState = data.read()) == 1 && (clockState = clock.read()) == 1) {}

if (dataState != 0 || clockState != 1)
{
continue; // no start pulse, retry
}

// await rising edge of start pulse
while ((dataState = data.read()) == 0 && (clockState = clock.read()) == 1) {}

if (dataState != 1 || clockState != 1)
{
continue; // no start pulse, retry
}

// // await end of start pulse
// while (data.read() == 1 && (clockState = clock.read()) == 1) {}

// if (clockState != 0)
// {
// continue; // this is not the first bit, try again
// }

break; // start pulse completed, we can start processing a new frame
}
}

#endif // __JR3_HPP__
111 changes: 111 additions & 0 deletions JR3Port.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#ifndef __JR3_PORT_HPP__
#define __JR3_PORT_HPP__

#include "mbed.h"

// https://github.com/ARMmbed/mbed-os/blob/f9c0cd2a94fc1353946b321aad7f8bca2bfe1b40/targets/TARGET_NXP/TARGET_LPC176X/port_api.c
// https://os.mbed.com/users/igorsk/notebook/fast-gpio-with-c-templates/
// https://os.mbed.com/users/igorsk/code/FastIO//file/b8e40f2a0aac/FastIO.h/

class JR3Port
{
public:
JR3Port(PortName portName, PinName clockPin, PinName dataPin)
: port(portName, 0x00000003)
// : port(portName, (1UL << ((clockPin - P0_0) % 32)) | (1UL << ((dataPin - P0_0) % 32))),
// DATA_LOW_CLOCK_LOW(0),
// DATA_LOW_CLOCK_HIGH(1UL << ((clockPin - P0_0) % 32)),
// DATA_HIGH_CLOCK_LOW(1UL << ((dataPin - P0_0) % 32)),
// DATA_HIGH_CLOCK_HIGH((1UL << ((clockPin - P0_0) % 32)) | (1UL << ((dataPin - P0_0) % 32))),
// CLOCK_LOW()
{}

uint32_t readFrame();

private:
void awaitNextFrame();

PortIn port;

// const int DATA_LOW_CLOCK_LOW;
// const int DATA_LOW_CLOCK_HIGH;
// const int DATA_HIGH_CLOCK_LOW;
// const int DATA_HIGH_CLOCK_HIGH;

// const int CLOCK_LOW;
// const int CLOCK_HIGH;
// const int DATA_LOW;
// const int DATA_HIGH;

static const unsigned int FRAME_SIZE = 20;
};

inline uint32_t JR3Port::readFrame()
{
int pins;
uint32_t frame = 0;

awaitNextFrame();

for (int i = FRAME_SIZE - 1; i >= 0; i--)
{
// await end of previous pulse
while ((port.read() & 0x00000001) == 1) {}

// await rising edge on clock signal
while (((pins = port.read()) & 0x00000001) == 0) {}

if ((pins & 0x00000002) == 0x00000002)
{
frame |= (1U << i);
}
}

return frame;
}

inline void JR3Port::awaitNextFrame()
{
int pins;

while (true)
{
// await next interval between frames
// while (!(data.read() == 1 && clock.read() == 1)) {}
while (port.read() != 0x00000003) {}

// await beginning of start pulse
// while (data.read() == 1 && clock.read() == 1) {}
while ((pins = port.read()) == 0x00000003) {}

// if (data.read() != 0 || clock.read() != 1)
if (pins != 0x00000001)
{
continue; // no start pulse, retry
}

// await rising edge of start pulse
// while (data.read() == 0 && clock.read() == 1) {}
while ((pins = port.read()) == 0x00000001) {}

// if (data.read() != 1 || clock.read() != 1)
if (pins != 0x00000003)
{
continue; // no start pulse, retry
}

// // await end of start pulse
// // while (data.read() == 1 && clock.read() == 1) {}
// while ((pins = port.read()) == 0x00000003) {}

// // if (clock.read() != 0)
// if ((pins & 0x00000001) != 0)
// {
// continue; // this is not the first bit, try again
// }

break; // start pulse completed, we can start processing a new frame
}
}

#endif // __JR3_PORT_HPP__
128 changes: 128 additions & 0 deletions JR3PortRegister.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#ifndef __JR3_PORT_REGISTER_HPP__
#define __JR3_PORT_REGISTER_HPP__

#include "mbed.h"

// https://github.com/ARMmbed/mbed-os/blob/f9c0cd2a94fc1353946b321aad7f8bca2bfe1b40/targets/TARGET_NXP/TARGET_LPC176X/port_api.c
// https://os.mbed.com/users/igorsk/notebook/fast-gpio-with-c-templates/
// https://os.mbed.com/users/igorsk/code/FastIO//file/b8e40f2a0aac/FastIO.h/

template <PortName portName, PinName clockPin, PinName dataPin>
class JR3PortRegister
{
public:
JR3PortRegister()
// : port(portName, (1UL << ((clockPin - P0_0) % 32)) | (1UL << ((dataPin - P0_0) % 32))),
// DATA_LOW_CLOCK_LOW(0),
// DATA_LOW_CLOCK_HIGH(1UL << ((clockPin - P0_0) % 32)),
// DATA_HIGH_CLOCK_LOW(1UL << ((dataPin - P0_0) % 32)),
// DATA_HIGH_CLOCK_HIGH((1UL << ((clockPin - P0_0) % 32)) | (1UL << ((dataPin - P0_0) % 32))),
// CLOCK_LOW()
{
LPC_GPIO_TypeDef * port_reg = (LPC_GPIO_TypeDef *)(LPC_GPIO0_BASE + ((int)portName * 0x20));

gpio_set(port_pin(portName, 0));
gpio_set(port_pin(portName, 1));

port_reg->FIODIR &= ~0x00000003;

port_in = &port_reg->FIOPIN;

// for (i = 0; i < 32; i++) {
// if (obj->mask & (1 << i))
// {
// gpio_set(port_pin(portName, i));
// }
// }
}

uint32_t readFrame();

private:
void awaitNextFrame();

volatile uint32_t * port_in;

// const int DATA_LOW_CLOCK_LOW;
// const int DATA_LOW_CLOCK_HIGH;
// const int DATA_HIGH_CLOCK_LOW;
// const int DATA_HIGH_CLOCK_HIGH;

// const int CLOCK_LOW;
// const int CLOCK_HIGH;
// const int DATA_LOW;
// const int DATA_HIGH;

static const unsigned int FRAME_SIZE = 20;
};

template <PortName portName, PinName clockPin, PinName dataPin>
inline uint32_t JR3PortRegister<portName, clockPin, dataPin>::readFrame()
{
int pins;
uint32_t frame = 0;

awaitNextFrame();

for (int i = FRAME_SIZE - 1; i >= 0; i--)
{
while ((*port_in & 0x00000001) == 1) {}

// await rising edge on clock signal
while (((pins = (*port_in & 0x00000003)) & 0x00000001) == 0) {}

if ((pins & 0x00000002) == 0x00000002)
{
frame |= (1U << i);
}
}

return frame;
}

template <PortName portName, PinName clockPin, PinName dataPin>
inline void JR3PortRegister<portName, clockPin, dataPin>::awaitNextFrame()
{
int pins;

while (true)
{
// await next interval between frames
// while (!(data.read() == 1 && clock.read() == 1)) {}
while ((*port_in & 0x00000003) != 0x00000003) {}

// await beginning of start pulse
// while (data.read() == 1 && clock.read() == 1) {}
while ((pins = (*port_in & 0x00000003)) == 0x00000003) {}

// if (data.read() != 0 || clock.read() != 1)
if (pins != 0x00000001)
{
continue; // no start pulse, retry
}

// await rising edge of start pulse
// while (data.read() == 0 && clock.read() == 1) {}
while ((pins = (*port_in & 0x00000003)) == 0x00000001) {}

// if (data.read() != 1 || clock.read() != 1)
if (pins != 0x00000003)
{
continue; // no start pulse, retry
}

// // await end of start pulse
// // while (data.read() == 1 && clock.read() == 1) {} &= ~obj->mas &= ~obj->mas
// while ((pins = (*port_in & 0x00000003)) == 0x00000003) {}

// // if (clock.read() != 0)
// if ((pins & 0x00000001) != 0)
// {
// continue; // this is not the first bit, try again
// }

break; // start pulse completed, we can start processing a new frame
}
}

#endif // __JR3_PORT_REGISTER_HPP__
Loading

0 comments on commit 2d55e45

Please sign in to comment.