mirror of
https://github.com/markqvist/OpenModem.git
synced 2024-10-01 03:15:46 -04:00
Indentation mess cleanup
This commit is contained in:
parent
c7ff5cc5f1
commit
80459cbbd3
226
Modem/hardware.c
226
Modem/hardware.c
@ -3,12 +3,12 @@
|
||||
//////////////////////////////////////////////////////
|
||||
|
||||
#include "hardware.h" // We need the header for this code
|
||||
#include "afsk.h" // We also need to know about the AFSK modem
|
||||
#include "afsk.h" // We also need to know about the AFSK modem
|
||||
|
||||
#include <cpu/irq.h> // Interrupt functions from BertOS
|
||||
#include <cpu/irq.h> // Interrupt functions from BertOS
|
||||
|
||||
#include <avr/io.h> // AVR IO functions from BertOS
|
||||
#include <avr/interrupt.h> // AVR interrupt functions from BertOS
|
||||
#include <avr/io.h> // AVR IO functions from BertOS
|
||||
#include <avr/interrupt.h> // AVR interrupt functions from BertOS
|
||||
|
||||
// A reference to our modem "object"
|
||||
static Afsk *modem;
|
||||
@ -25,80 +25,80 @@ static Afsk *modem;
|
||||
// it the way we need.
|
||||
void hw_afsk_adcInit(int ch, Afsk *_modem)
|
||||
{
|
||||
// Store a reference to our modem "object"
|
||||
modem = _modem;
|
||||
// Store a reference to our modem "object"
|
||||
modem = _modem;
|
||||
|
||||
// Also make sure that we are not trying to use
|
||||
// a pin that can't be used for analog input
|
||||
ASSERT(ch <= 5);
|
||||
// Also make sure that we are not trying to use
|
||||
// a pin that can't be used for analog input
|
||||
ASSERT(ch <= 5);
|
||||
|
||||
// We need a timer to control how often our sampling functions
|
||||
// should run. To do this we will need to change some registers.
|
||||
// First we do some configuration on the Timer/Counter Control
|
||||
// Register 1, aka Timer1.
|
||||
//
|
||||
// The following bits are set:
|
||||
// CS10: ClockSource 10, sets no prescaler on the clock,
|
||||
// meaning it will run at the same speed as the CPU, ie 16MHz
|
||||
// WGM13 and WGM12 together enables "Timer Mode 12", which
|
||||
// is Clear Timer on Compare, compare set to TOP, and the
|
||||
// source for the TOP value is ICR1 (Input Capture Register1).
|
||||
// TOP means that we specify a maximum value for the timer, and
|
||||
// once that value is reached, an interrupt will be triggered.
|
||||
// The timer will then start from zero again. As just noted,
|
||||
// the place we specify this value is in the ICR1 register.
|
||||
TCCR1A = 0;
|
||||
TCCR1B = BV(CS10) | BV(WGM13) | BV(WGM12);
|
||||
// We need a timer to control how often our sampling functions
|
||||
// should run. To do this we will need to change some registers.
|
||||
// First we do some configuration on the Timer/Counter Control
|
||||
// Register 1, aka Timer1.
|
||||
//
|
||||
// The following bits are set:
|
||||
// CS10: ClockSource 10, sets no prescaler on the clock,
|
||||
// meaning it will run at the same speed as the CPU, ie 16MHz
|
||||
// WGM13 and WGM12 together enables "Timer Mode 12", which
|
||||
// is Clear Timer on Compare, compare set to TOP, and the
|
||||
// source for the TOP value is ICR1 (Input Capture Register1).
|
||||
// TOP means that we specify a maximum value for the timer, and
|
||||
// once that value is reached, an interrupt will be triggered.
|
||||
// The timer will then start from zero again. As just noted,
|
||||
// the place we specify this value is in the ICR1 register.
|
||||
TCCR1A = 0;
|
||||
TCCR1B = BV(CS10) | BV(WGM13) | BV(WGM12);
|
||||
|
||||
// We now set the ICR1 register to what count value we want to
|
||||
// reset (and thus trigger the interrupt) at.
|
||||
// Since the timer is running at 16MHz, the counter will be
|
||||
// incremented 16 million times each second, and we want the
|
||||
// interrupt to trigger 9600 times each second. The formula for
|
||||
// calculating the value of ICR1 (the TOP value) is:
|
||||
// (CPUClock / Prescaler) / desired frequency - 1
|
||||
// So that's what well put in this register to set up our
|
||||
// 9.6KHz sampling rate. Note that we can also specify a clock
|
||||
// correction to this calculation. If you measure your processors
|
||||
// actual clock speed to 16.095MHz, define FREQUENCY_CORRECTION
|
||||
// as 9500, and the actual sampling (and this modulation and
|
||||
// demodulation) will be much closer to an actual 9600 Hz.
|
||||
// No crystals are perfect though, and will also drift with
|
||||
// temperature variations, but if you have a board with a
|
||||
// crystal that is way off frequency, this can help alot.
|
||||
ICR1 = (((CPU_FREQ+FREQUENCY_CORRECTION)) / 9600) - 1;
|
||||
// We now set the ICR1 register to what count value we want to
|
||||
// reset (and thus trigger the interrupt) at.
|
||||
// Since the timer is running at 16MHz, the counter will be
|
||||
// incremented 16 million times each second, and we want the
|
||||
// interrupt to trigger 9600 times each second. The formula for
|
||||
// calculating the value of ICR1 (the TOP value) is:
|
||||
// (CPUClock / Prescaler) / desired frequency - 1
|
||||
// So that's what well put in this register to set up our
|
||||
// 9.6KHz sampling rate. Note that we can also specify a clock
|
||||
// correction to this calculation. If you measure your processors
|
||||
// actual clock speed to 16.095MHz, define FREQUENCY_CORRECTION
|
||||
// as 9500, and the actual sampling (and this modulation and
|
||||
// demodulation) will be much closer to an actual 9600 Hz.
|
||||
// No crystals are perfect though, and will also drift with
|
||||
// temperature variations, but if you have a board with a
|
||||
// crystal that is way off frequency, this can help alot.
|
||||
ICR1 = (((CPU_FREQ+FREQUENCY_CORRECTION)) / 9600) - 1;
|
||||
|
||||
// Set reference to AVCC (5V), select pin
|
||||
// Set the ADMUX register. The first part (BV(REFS0)) sets
|
||||
// the reference voltage to VCC (5V), and the next selects
|
||||
// the ADC channel (basically what pin we are capturing on)
|
||||
ADMUX = BV(REFS0) | ch;
|
||||
// Set reference to AVCC (5V), select pin
|
||||
// Set the ADMUX register. The first part (BV(REFS0)) sets
|
||||
// the reference voltage to VCC (5V), and the next selects
|
||||
// the ADC channel (basically what pin we are capturing on)
|
||||
ADMUX = BV(REFS0) | ch;
|
||||
|
||||
DDRC &= ~BV(ch); // Set the selected channel (pin) to input
|
||||
PORTC &= ~BV(ch); // Initialize the selected pin to LOW
|
||||
DIDR0 |= BV(ch); // Disable the Digital Input Buffer on selected pin
|
||||
DDRC &= ~BV(ch); // Set the selected channel (pin) to input
|
||||
PORTC &= ~BV(ch); // Initialize the selected pin to LOW
|
||||
DIDR0 |= BV(ch); // Disable the Digital Input Buffer on selected pin
|
||||
|
||||
// Now a little more configuration to get the ADC working
|
||||
// the way we want
|
||||
ADCSRB = BV(ADTS2) | // Setting these three on (1-1-1) sets the ADC to
|
||||
BV(ADTS1) | // "Timer1 capture event". That means we can declare
|
||||
BV(ADTS0); // an ISR in the ADC Vector, that will then get called
|
||||
// everytime the ADC has a sample ready, which will
|
||||
// happen at the 9.6Khz sampling rate we set up earlier
|
||||
|
||||
ADCSRA = BV(ADEN) | // ADC Enable - Yes, we need to turn it on :)
|
||||
BV(ADSC) | // ADC Start Converting - Tell it to start doing conversions
|
||||
BV(ADATE) | // Enable autotriggering - Enables the autotrigger on complete
|
||||
BV(ADIE) | // ADC Interrupt enable - Enables an interrupt to be called
|
||||
BV(ADPS2); // Enable prescaler flag 2 (1-0-0 = division by 16 = 1MHz)
|
||||
// This sets the ADC to run at 1MHz. This is out of spec,
|
||||
// Since it's normal operating range is only up to 200KHz.
|
||||
// But don't worry, it's not dangerous! I promise it wont
|
||||
// blow up :) There is a downside to running at this speed
|
||||
// though, hence the "out of spec", which is that we get
|
||||
// a much lower resolution on the output. In this case,
|
||||
// it's not a problem though, since we don't need the full
|
||||
// 10-bit resolution, so we'll take fast and less precise!
|
||||
// Now a little more configuration to get the ADC working
|
||||
// the way we want
|
||||
ADCSRB = BV(ADTS2) | // Setting these three on (1-1-1) sets the ADC to
|
||||
BV(ADTS1) | // "Timer1 capture event". That means we can declare
|
||||
BV(ADTS0); // an ISR in the ADC Vector, that will then get called
|
||||
// everytime the ADC has a sample ready, which will
|
||||
// happen at the 9.6Khz sampling rate we set up earlier
|
||||
|
||||
ADCSRA = BV(ADEN) | // ADC Enable - Yes, we need to turn it on :)
|
||||
BV(ADSC) | // ADC Start Converting - Tell it to start doing conversions
|
||||
BV(ADATE)| // Enable autotriggering - Enables the autotrigger on complete
|
||||
BV(ADIE) | // ADC Interrupt enable - Enables an interrupt to be called
|
||||
BV(ADPS2); // Enable prescaler flag 2 (1-0-0 = division by 16 = 1MHz)
|
||||
// This sets the ADC to run at 1MHz. This is out of spec,
|
||||
// Since it's normal operating range is only up to 200KHz.
|
||||
// But don't worry, it's not dangerous! I promise it wont
|
||||
// blow up :) There is a downside to running at this speed
|
||||
// though, hence the "out of spec", which is that we get
|
||||
// a much lower resolution on the output. In this case,
|
||||
// it's not a problem though, since we don't need the full
|
||||
// 10-bit resolution, so we'll take fast and less precise!
|
||||
}
|
||||
|
||||
|
||||
@ -114,52 +114,52 @@ void hw_afsk_adcInit(int ch, Afsk *_modem)
|
||||
bool hw_ptt_on;
|
||||
bool hw_afsk_dac_isr;
|
||||
DECLARE_ISR(ADC_vect) {
|
||||
TIFR1 = BV(ICF1);
|
||||
TIFR1 = BV(ICF1);
|
||||
|
||||
// Call the routine for analysing the captured sample
|
||||
// Notice that we read the ADC sample, and then bitshift
|
||||
// by two places to the right, effectively eliminating
|
||||
// two bits of precision. But we didn't have those
|
||||
// anyway, because the ADC is running at high speed.
|
||||
// We then subtract 128 from the value, to get the
|
||||
// representation to match an AC waveform. We need to
|
||||
// do this because the AC waveform (from the audio input)
|
||||
// is biased by +2.5V, which is nessecary, since the ADC
|
||||
// can't read negative voltages. By doing this simple
|
||||
// math, we bring it back to an AC representation
|
||||
// we can do further calculations on.
|
||||
afsk_adc_isr(modem, ((int16_t)((ADC) >> 2) - 128));
|
||||
// Call the routine for analysing the captured sample
|
||||
// Notice that we read the ADC sample, and then bitshift
|
||||
// by two places to the right, effectively eliminating
|
||||
// two bits of precision. But we didn't have those
|
||||
// anyway, because the ADC is running at high speed.
|
||||
// We then subtract 128 from the value, to get the
|
||||
// representation to match an AC waveform. We need to
|
||||
// do this because the AC waveform (from the audio input)
|
||||
// is biased by +2.5V, which is nessecary, since the ADC
|
||||
// can't read negative voltages. By doing this simple
|
||||
// math, we bring it back to an AC representation
|
||||
// we can do further calculations on.
|
||||
afsk_adc_isr(modem, ((int16_t)((ADC) >> 2) - 128));
|
||||
|
||||
// We also need to check if we're supposed to spit
|
||||
// out some modulated data to the DAC.
|
||||
if (hw_afsk_dac_isr) {
|
||||
// If there is, it's easy to actually do so. We
|
||||
// calculate what the sample should be in the
|
||||
// DAC ISR, and apply the bitmask 11110000. This
|
||||
// simoultaneously spits out our 4-bit digital
|
||||
// sample to the four pins connected to our DAC
|
||||
// circuit, which then converts it to an analog
|
||||
// waveform. The reason for the " | BV(3)" is that
|
||||
// we also need to trigger another pin controlled
|
||||
// by the PORTD register. This is the PTT pin
|
||||
// which tells the radio to open it transmitter.
|
||||
PORTD = (afsk_dac_isr(modem) & 0xF0) | BV(3);
|
||||
} else {
|
||||
// If we're not supposed to transmit anything, we
|
||||
// keep quiet by continously sending 128, which
|
||||
// when converted to an AC waveform by the DAC,
|
||||
// equates to a steady, unchanging 0 volts.
|
||||
if (hw_ptt_on) {
|
||||
PORTD = 136;
|
||||
} else {
|
||||
PORTD = 128;
|
||||
}
|
||||
}
|
||||
|
||||
// We also need to check if we're supposed to spit
|
||||
// out some modulated data to the DAC.
|
||||
if (hw_afsk_dac_isr) {
|
||||
// If there is, it's easy to actually do so. We
|
||||
// calculate what the sample should be in the
|
||||
// DAC ISR, and apply the bitmask 11110000. This
|
||||
// simoultaneously spits out our 4-bit digital
|
||||
// sample to the four pins connected to our DAC
|
||||
// circuit, which then converts it to an analog
|
||||
// waveform. The reason for the " | BV(3)" is that
|
||||
// we also need to trigger another pin controlled
|
||||
// by the PORTD register. This is the PTT pin
|
||||
// which tells the radio to open it transmitter.
|
||||
PORTD = (afsk_dac_isr(modem) & 0xF0) | BV(3);
|
||||
} else {
|
||||
// If we're not supposed to transmit anything, we
|
||||
// keep quiet by continously sending 128, which
|
||||
// when converted to an AC waveform by the DAC,
|
||||
// equates to a steady, unchanging 0 volts.
|
||||
if (hw_ptt_on) {
|
||||
PORTD = 136;
|
||||
} else {
|
||||
PORTD = 128;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// * "finally" is probably the wrong description here.
|
||||
// (*) "finally" is probably the wrong description here.
|
||||
// "All the f'ing time" is probably more accurate :)
|
||||
// but it felt like it was a long way down here,
|
||||
// writing all the explanations. I think this is a
|
||||
|
@ -5,9 +5,9 @@
|
||||
#ifndef FSK_MODEM_HW
|
||||
#define FSK_MODEM_HW
|
||||
|
||||
#include "cfg/cfg_arch.h" // Architecture configuration
|
||||
#include "cfg/cfg_arch.h" // Architecture configuration
|
||||
|
||||
#include <avr/io.h> // AVR IO functions from BertOS
|
||||
#include <avr/io.h> // AVR IO functions from BertOS
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
// Definitions and some useful macros //
|
||||
|
282
Modem/main.c
282
Modem/main.c
@ -3,40 +3,40 @@
|
||||
// First things first, all the includes we need //
|
||||
//////////////////////////////////////////////////////
|
||||
|
||||
#include <cpu/irq.h> // Interrupt functionality from BertOS
|
||||
#include <cpu/irq.h> // Interrupt functionality from BertOS
|
||||
|
||||
#include <drv/ser.h> // Serial driver from BertOS
|
||||
#include <drv/timer.h> // Timer driver from BertOS
|
||||
#include <drv/ser.h> // Serial driver from BertOS
|
||||
#include <drv/timer.h> // Timer driver from BertOS
|
||||
|
||||
#include <stdio.h> // Standard input/output
|
||||
#include <string.h> // String operations
|
||||
#include <stdio.h> // Standard input/output
|
||||
#include <string.h> // String operations
|
||||
|
||||
#include "afsk.h" // Header for AFSK modem
|
||||
#include "protocol/mp1.h" // Header for MP.1 protocol
|
||||
#include "afsk.h" // Header for AFSK modem
|
||||
#include "protocol/mp1.h" // Header for MP.1 protocol
|
||||
|
||||
#if SERIAL_DEBUG
|
||||
#include "cfg/debug.h" // Debug configuration from BertOS
|
||||
#include "cfg/debug.h" // Debug configuration from BertOS
|
||||
#endif
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
// A few definitions //
|
||||
// A few definitions //
|
||||
//////////////////////////////////////////////////////
|
||||
|
||||
static Afsk afsk; // Declare a AFSK modem struct
|
||||
static MP1 mp1; // Declare a protocol struct
|
||||
static Serial ser; // Declare a serial interface struct
|
||||
static Afsk afsk; // Declare a AFSK modem struct
|
||||
static MP1 mp1; // Declare a protocol struct
|
||||
static Serial ser; // Declare a serial interface struct
|
||||
|
||||
#define ADC_CH 0 // Define which channel (pin) we want
|
||||
// for the ADC (this is A0 on arduino)
|
||||
#define ADC_CH 0 // Define which channel (pin) we want
|
||||
// for the ADC (this is A0 on arduino)
|
||||
|
||||
|
||||
static uint8_t serialBuffer[MP1_MAX_DATA_SIZE]; // This is a buffer for incoming serial data
|
||||
static uint8_t serialBuffer[MP1_MAX_DATA_SIZE]; // This is a buffer for incoming serial data
|
||||
|
||||
static int sbyte; // For holding byte read from serial port
|
||||
static size_t serialLen = 0; // Counter for counting length of data from serial
|
||||
static bool sertx = false; // Flag signifying whether it's time to send data
|
||||
// Received on the serial port.
|
||||
static int sbyte; // For holding byte read from serial port
|
||||
static size_t serialLen = 0; // Counter for counting length of data from serial
|
||||
static bool sertx = false; // Flag signifying whether it's time to send data
|
||||
// received on the serial port.
|
||||
#define SER_BUFFER_FULL (serialLen < MP1_MAX_DATA_SIZE-1)
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
@ -47,147 +47,147 @@ static bool sertx = false; // Flag signifying whether it's time to send da
|
||||
// so we can process each packet as they are decoded.
|
||||
// Right now it just prints the packet to the serial port.
|
||||
static void mp1Callback(struct MP1Packet *packet) {
|
||||
if (SERIAL_DEBUG) {
|
||||
kfile_printf(&ser.fd, "%.*s\n", packet->dataLength, packet->data);
|
||||
} else {
|
||||
for (unsigned long i = 0; i < packet->dataLength; i++) {
|
||||
kfile_putc(packet->data[i], &ser.fd);
|
||||
}
|
||||
}
|
||||
if (SERIAL_DEBUG) {
|
||||
kfile_printf(&ser.fd, "%.*s\n", packet->dataLength, packet->data);
|
||||
} else {
|
||||
for (unsigned long i = 0; i < packet->dataLength; i++) {
|
||||
kfile_putc(packet->data[i], &ser.fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Simple initialization function.
|
||||
static void init(void)
|
||||
{
|
||||
// Enable interrupts
|
||||
IRQ_ENABLE;
|
||||
// Enable interrupts
|
||||
IRQ_ENABLE;
|
||||
|
||||
// Initialize hardware timers
|
||||
timer_init();
|
||||
timer_init();
|
||||
|
||||
// Initialize serial comms on UART0,
|
||||
// which is the hardware serial on arduino
|
||||
ser_init(&ser, SER_UART0);
|
||||
ser_setbaudrate(&ser, 9600);
|
||||
// Initialize serial comms on UART0,
|
||||
// which is the hardware serial on arduino
|
||||
ser_init(&ser, SER_UART0);
|
||||
ser_setbaudrate(&ser, 9600);
|
||||
|
||||
// For some reason BertOS sets the serial
|
||||
// to 7 bit characters by default. We set
|
||||
// it to 8 instead.
|
||||
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
|
||||
// For some reason BertOS sets the serial
|
||||
// to 7 bit characters by default. We set
|
||||
// it to 8 instead.
|
||||
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
|
||||
|
||||
// Create a modem context
|
||||
afsk_init(&afsk, ADC_CH);
|
||||
// ... and a protocol context with the modem
|
||||
mp1Init(&mp1, &afsk.fd, mp1Callback);
|
||||
// Create a modem context
|
||||
afsk_init(&afsk, ADC_CH);
|
||||
// ... and a protocol context with the modem
|
||||
mp1Init(&mp1, &afsk.fd, mp1Callback);
|
||||
|
||||
// That's all!
|
||||
// That's all!
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Start by running the main initialization
|
||||
init();
|
||||
// Record the current tick count for time-keeping
|
||||
ticks_t start = timer_clock();
|
||||
#if MP1_USE_TX_QUEUE
|
||||
ticks_t frameQueued = 0;
|
||||
#endif
|
||||
|
||||
// Go into ye good ol' infinite loop
|
||||
while (1)
|
||||
{
|
||||
// First we instruct the protocol to check for
|
||||
// incoming data
|
||||
mp1Poll(&mp1);
|
||||
// Start by running the main initialization
|
||||
init();
|
||||
// Record the current tick count for time-keeping
|
||||
ticks_t start = timer_clock();
|
||||
#if MP1_USE_TX_QUEUE
|
||||
ticks_t frameQueued = 0;
|
||||
#endif
|
||||
|
||||
// Go into ye good ol' infinite loop
|
||||
while (1)
|
||||
{
|
||||
// First we instruct the protocol to check for
|
||||
// incoming data
|
||||
mp1Poll(&mp1);
|
||||
|
||||
|
||||
// If there was actually some data waiting for us
|
||||
// there, let's se what it tastes like :)
|
||||
if (!sertx && ser_available(&ser)) {
|
||||
// We then read a byte from the serial port.
|
||||
// Notice that we use "_nowait" since we can't
|
||||
// have this blocking execution until a byte
|
||||
// comes in.
|
||||
sbyte = ser_getchar_nowait(&ser);
|
||||
|
||||
// If there was actually some data waiting for us
|
||||
// there, let's se what it tastes like :)
|
||||
if (!sertx && ser_available(&ser)) {
|
||||
// We then read a byte from the serial port.
|
||||
// Notice that we use "_nowait" since we can't
|
||||
// have this blocking execution until a byte
|
||||
// comes in.
|
||||
sbyte = ser_getchar_nowait(&ser);
|
||||
|
||||
// If SERIAL_DEBUG is specified we'll handle
|
||||
// serial data as direct human input and only
|
||||
// transmit when we get a LF character
|
||||
#if SERIAL_DEBUG
|
||||
// If we have not yet surpassed the maximum frame length
|
||||
// and the byte is not a "transmit" (newline) character,
|
||||
// we should store it for transmission.
|
||||
if ((serialLen < MP1_MAX_DATA_SIZE) && (sbyte != 10)) {
|
||||
// Put the read byte into the buffer;
|
||||
serialBuffer[serialLen] = sbyte;
|
||||
// Increment the read length counter
|
||||
serialLen++;
|
||||
} else {
|
||||
// If one of the above conditions were actually the
|
||||
// case, it means we have to transmit, se we set
|
||||
// transmission flag to true.
|
||||
sertx = true;
|
||||
}
|
||||
#else
|
||||
// Otherwise we assume the modem is running
|
||||
// in automated mode, and we push out data
|
||||
// as it becomes available. We either transmit
|
||||
// immediately when the max frame length has
|
||||
// been reached, or when we get no input for
|
||||
// a certain amount of time.
|
||||
// If SERIAL_DEBUG is specified we'll handle
|
||||
// serial data as direct human input and only
|
||||
// transmit when we get a LF character
|
||||
#if SERIAL_DEBUG
|
||||
// If we have not yet surpassed the maximum frame length
|
||||
// and the byte is not a "transmit" (newline) character,
|
||||
// we should store it for transmission.
|
||||
if ((serialLen < MP1_MAX_DATA_SIZE) && (sbyte != 10)) {
|
||||
// Put the read byte into the buffer;
|
||||
serialBuffer[serialLen] = sbyte;
|
||||
// Increment the read length counter
|
||||
serialLen++;
|
||||
} else {
|
||||
// If one of the above conditions were actually the
|
||||
// case, it means we have to transmit, se we set
|
||||
// transmission flag to true.
|
||||
sertx = true;
|
||||
}
|
||||
#else
|
||||
// Otherwise we assume the modem is running
|
||||
// in automated mode, and we push out data
|
||||
// as it becomes available. We either transmit
|
||||
// immediately when the max frame length has
|
||||
// been reached, or when we get no input for
|
||||
// a certain amount of time.
|
||||
|
||||
if (serialLen < MP1_MAX_DATA_SIZE-1) {
|
||||
// Put the read byte into the buffer;
|
||||
serialBuffer[serialLen] = sbyte;
|
||||
// Increment the read length counter
|
||||
serialLen++;
|
||||
} else {
|
||||
// If max frame length has been reached
|
||||
// we need to transmit.
|
||||
serialBuffer[serialLen] = sbyte;
|
||||
serialLen++;
|
||||
sertx = true;
|
||||
}
|
||||
if (serialLen < MP1_MAX_DATA_SIZE-1) {
|
||||
// Put the read byte into the buffer;
|
||||
serialBuffer[serialLen] = sbyte;
|
||||
// Increment the read length counter
|
||||
serialLen++;
|
||||
} else {
|
||||
// If max frame length has been reached
|
||||
// we need to transmit.
|
||||
serialBuffer[serialLen] = sbyte;
|
||||
serialLen++;
|
||||
sertx = true;
|
||||
}
|
||||
|
||||
start = timer_clock();
|
||||
#endif
|
||||
} else {
|
||||
if (!SERIAL_DEBUG && serialLen > 0 && timer_clock() - start > ms_to_ticks(TX_MAXWAIT)) {
|
||||
sertx = true;
|
||||
}
|
||||
}
|
||||
start = timer_clock();
|
||||
#endif
|
||||
} else {
|
||||
if (!SERIAL_DEBUG && serialLen > 0 && timer_clock() - start > ms_to_ticks(TX_MAXWAIT)) {
|
||||
sertx = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Check whether we should send data in our serial buffer
|
||||
if (sertx) {
|
||||
#if MP1_USE_TX_QUEUE
|
||||
mp1QueueFrame(&mp1, serialBuffer, serialLen);
|
||||
frameQueued = timer_clock();
|
||||
sertx = false;
|
||||
serialLen = 0;
|
||||
#else
|
||||
// Wait until incoming packets are done
|
||||
if (!mp1CarrierSense(&mp1)) {
|
||||
// And then send the data
|
||||
mp1Send(&mp1, serialBuffer, serialLen);
|
||||
|
||||
// Reset the transmission flag and length counter
|
||||
sertx = false;
|
||||
serialLen = 0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
// Check whether we should send data in our serial buffer
|
||||
if (sertx) {
|
||||
#if MP1_USE_TX_QUEUE
|
||||
mp1QueueFrame(&mp1, serialBuffer, serialLen);
|
||||
frameQueued = timer_clock();
|
||||
sertx = false;
|
||||
serialLen = 0;
|
||||
#else
|
||||
// Wait until incoming packets are done
|
||||
if (!mp1CarrierSense(&mp1)) {
|
||||
// And then send the data
|
||||
mp1Send(&mp1, serialBuffer, serialLen);
|
||||
|
||||
// Reset the transmission flag and length counter
|
||||
sertx = false;
|
||||
serialLen = 0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if MP1_USE_TX_QUEUE
|
||||
// We first wait a little to see if more
|
||||
// frames are coming in.
|
||||
if (timer_clock() - frameQueued > ms_to_ticks(MP1_QUEUE_TX_WAIT)) {
|
||||
if (!ser_available(&ser) && !mp1CarrierSense(&mp1)) {
|
||||
// And if not, we send process the frame
|
||||
// queue if possible.
|
||||
mp1ProcessQueue(&mp1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
return 0;
|
||||
#if MP1_USE_TX_QUEUE
|
||||
// We first wait a little to see if more
|
||||
// frames are coming in.
|
||||
if (timer_clock() - frameQueued > ms_to_ticks(MP1_QUEUE_TX_WAIT)) {
|
||||
if (!ser_available(&ser) && !mp1CarrierSense(&mp1)) {
|
||||
// And if not, we send process the frame
|
||||
// queue if possible.
|
||||
mp1ProcessQueue(&mp1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
return 0;
|
||||
}
|
1556
Modem/protocol/mp1.c
1556
Modem/protocol/mp1.c
File diff suppressed because it is too large
Load Diff
@ -7,23 +7,23 @@
|
||||
// Options
|
||||
#define MP1_ENABLE_TCP_COMPATIBILITY false
|
||||
#if MP1_ENABLE_TCP_COMPATIBILITY
|
||||
#define MP1_ENABLE_COMPRESSION false
|
||||
#define MP1_ENABLE_CSMA true
|
||||
#define MP1_ENABLE_COMPRESSION false
|
||||
#define MP1_ENABLE_CSMA true
|
||||
#else
|
||||
#define MP1_ENABLE_COMPRESSION true
|
||||
#define MP1_ENABLE_CSMA false
|
||||
#define MP1_ENABLE_COMPRESSION true
|
||||
#define MP1_ENABLE_CSMA false
|
||||
#endif
|
||||
|
||||
// Frame sizing & checksum
|
||||
#define MP1_INTERLEAVE_SIZE 12
|
||||
#if MP1_ENABLE_COMPRESSION
|
||||
#define MP1_MAX_FRAME_LENGTH 22 * MP1_INTERLEAVE_SIZE
|
||||
#define MP1_USE_TX_QUEUE false
|
||||
#define MP1_MAX_FRAME_LENGTH 22 * MP1_INTERLEAVE_SIZE
|
||||
#define MP1_USE_TX_QUEUE false
|
||||
#else
|
||||
#define MP1_MAX_FRAME_LENGTH 25 * MP1_INTERLEAVE_SIZE
|
||||
#define MP1_USE_TX_QUEUE true
|
||||
#define MP1_TX_QUEUE_LENGTH 2
|
||||
#define MP1_QUEUE_TX_WAIT 16UL
|
||||
#define MP1_MAX_FRAME_LENGTH 25 * MP1_INTERLEAVE_SIZE
|
||||
#define MP1_USE_TX_QUEUE true
|
||||
#define MP1_TX_QUEUE_LENGTH 2
|
||||
#define MP1_QUEUE_TX_WAIT 16UL
|
||||
#endif
|
||||
#define MP1_HEADER_SIZE 1
|
||||
#define MP1_CHECKSUM_SIZE 1
|
||||
@ -34,10 +34,10 @@
|
||||
|
||||
// These two parameters are used for
|
||||
// P-persistent CSMA
|
||||
#define MP1_SETTLE_TIME 100UL // The minimum wait time before even considering sending
|
||||
#define MP1_SLOT_TIME 100UL // The time to wait if deciding not to send
|
||||
#define MP1_P_PERSISTENCE 85UL // The probability (between 0 and 255) for sending
|
||||
#define MP1_TXDELAY 0UL // Delay between turning on the transmitter and sending
|
||||
#define MP1_SETTLE_TIME 100UL // The minimum wait time before even considering sending
|
||||
#define MP1_SLOT_TIME 100UL // The time to wait if deciding not to send
|
||||
#define MP1_P_PERSISTENCE 85UL // The probability (between 0 and 255) for sending
|
||||
#define MP1_TXDELAY 0UL // Delay between turning on the transmitter and sending
|
||||
|
||||
// We need to know some basic HDLC flag bytes
|
||||
#define HDLC_FLAG 0x7E
|
||||
@ -48,9 +48,9 @@
|
||||
// to send as padding if we need to pad a
|
||||
// packet. Due to forward error correction,
|
||||
// packets must have an even number of bytes.
|
||||
#define MP1_PADDING 0x55
|
||||
#define MP1_HEADER_PADDED 0x01
|
||||
#define MP1_HEADER_COMPRESSION 0x02
|
||||
#define MP1_PADDING 0x55
|
||||
#define MP1_HEADER_PADDED 0x01
|
||||
#define MP1_HEADER_COMPRESSION 0x02
|
||||
|
||||
// Just a forward declaration that this struct exists
|
||||
struct MP1Packet;
|
||||
@ -61,35 +61,35 @@ typedef void (*mp1_callback_t)(struct MP1Packet *packet);
|
||||
|
||||
// Struct for a protocol context
|
||||
typedef struct MP1 {
|
||||
uint8_t buffer[MP1_MAX_FRAME_LENGTH+MP1_INTERLEAVE_SIZE]; // A buffer for incoming packets
|
||||
KFile *modem; // KFile access to the modem
|
||||
size_t packetLength; // Counter for received packet length
|
||||
size_t readLength; // This is the full read length, including parity bytes
|
||||
uint8_t calculatedParity; // Calculated parity for incoming data block
|
||||
mp1_callback_t callback; // The function to call when a packet has been received
|
||||
uint8_t checksum_in; // Rolling checksum for incoming packets
|
||||
uint8_t checksum_out; // Rolling checksum for outgoing packets
|
||||
bool reading; // True when we have seen a HDLC flag
|
||||
bool escape; // We need to know if we are in an escape sequence
|
||||
ticks_t settleTimer; // Timer used for carrier sense settling
|
||||
long correctionsMade; // A counter for how many corrections were made to a packet
|
||||
uint8_t interleaveCounter; // Keeps track of when we have received an entire interleaved block
|
||||
uint8_t interleaveOut[MP1_INTERLEAVE_SIZE]; // A buffer for interleaving bytes before they are sent
|
||||
uint8_t interleaveIn[MP1_INTERLEAVE_SIZE]; // A buffer for storing interleaved bytes before they are deinterleaved
|
||||
uint8_t randomSeed; // A seed for the pseudo-random number generator
|
||||
#if MP1_USE_TX_QUEUE
|
||||
bool queueProcessing; // For sending queued frames without preamble after first one
|
||||
size_t queueLength; // The length of the transmission queue
|
||||
size_t frameLengths[MP1_TX_QUEUE_LENGTH]; // The lengths of the frames in the queue
|
||||
uint8_t frameQueue[MP1_TX_QUEUE_LENGTH] // A buffer for a queued frame
|
||||
[MP1_MAX_DATA_SIZE];
|
||||
#endif
|
||||
uint8_t buffer[MP1_MAX_FRAME_LENGTH+MP1_INTERLEAVE_SIZE]; // A buffer for incoming packets
|
||||
KFile *modem; // KFile access to the modem
|
||||
size_t packetLength; // Counter for received packet length
|
||||
size_t readLength; // This is the full read length, including parity bytes
|
||||
uint8_t calculatedParity; // Calculated parity for incoming data block
|
||||
mp1_callback_t callback; // The function to call when a packet has been received
|
||||
uint8_t checksum_in; // Rolling checksum for incoming packets
|
||||
uint8_t checksum_out; // Rolling checksum for outgoing packets
|
||||
bool reading; // True when we have seen a HDLC flag
|
||||
bool escape; // We need to know if we are in an escape sequence
|
||||
ticks_t settleTimer; // Timer used for carrier sense settling
|
||||
long correctionsMade; // A counter for how many corrections were made to a packet
|
||||
uint8_t interleaveCounter; // Keeps track of when we have received an entire interleaved block
|
||||
uint8_t interleaveOut[MP1_INTERLEAVE_SIZE]; // A buffer for interleaving bytes before they are sent
|
||||
uint8_t interleaveIn[MP1_INTERLEAVE_SIZE]; // A buffer for storing interleaved bytes before they are deinterleaved
|
||||
uint8_t randomSeed; // A seed for the pseudo-random number generator
|
||||
#if MP1_USE_TX_QUEUE
|
||||
bool queueProcessing; // For sending queued frames without preamble after first one
|
||||
size_t queueLength; // The length of the transmission queue
|
||||
size_t frameLengths[MP1_TX_QUEUE_LENGTH]; // The lengths of the frames in the queue
|
||||
uint8_t frameQueue[MP1_TX_QUEUE_LENGTH] // A buffer for a queued frame
|
||||
[MP1_MAX_DATA_SIZE];
|
||||
#endif
|
||||
} MP1;
|
||||
|
||||
// A struct encapsulating a network packet
|
||||
typedef struct MP1Packet {
|
||||
const uint8_t *data; // Pointer to the actual data in the packet
|
||||
size_t dataLength; // The length of the received data
|
||||
const uint8_t *data; // Pointer to the actual data in the packet
|
||||
size_t dataLength; // The length of the received data
|
||||
} MP1Packet;
|
||||
|
||||
// Declarations of functions
|
||||
|
@ -1,2 +1,2 @@
|
||||
#define VERS_BUILD 1756
|
||||
#define VERS_BUILD 1758
|
||||
#define VERS_HOST "shard"
|
||||
|
Loading…
Reference in New Issue
Block a user