Skip to content

Commit 77ba514

Browse files
author
Mark Qvist
committed
MP1 protocol receive implemented
1 parent 6b496c3 commit 77ba514

File tree

6 files changed

+150
-8
lines changed

6 files changed

+150
-8
lines changed

Modem/Modem_user.mk

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Modem_USER_CSRC = \
1212
$(Modem_SRC_PATH)/main.c \
1313
$(Modem_HW_PATH)/hardware.c \
1414
$(Modem_HW_PATH)/afsk.c \
15+
$(Modem_HW_PATH)/protocol/mp1.c \
1516
bertos/net/ax25.c \
1617
bertos/algo/crc_ccitt.c \
1718
#

Modem/main.c

+17-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#include <cpu/irq.h>
33
#include <cfg/debug.h>
44

5-
#include "afsk.h"
5+
#include "afsk.h" // Header for AFSK modem
6+
#include "protocol/mp1.h" // Header for MP.1 protocol
67

78
#include <drv/ser.h>
89
#include <drv/timer.h>
@@ -13,6 +14,8 @@
1314
static Afsk afsk;
1415
static Serial ser;
1516

17+
static MP1 mp1;
18+
1619
#define ADC_CH 0
1720

1821

@@ -32,6 +35,10 @@ static void message_callback(struct AX25Msg *msg)
3235
}
3336
///////////////////////////////////////////////
3437

38+
static void mp1Callback(struct MP1Packet *packet) {
39+
kfile_printf(&ser.fd, "\nMP1 Packet Received:\n");
40+
kfile_printf(&ser.fd, "%.*s\r\n", packet->dataLength, packet->data);
41+
}
3542

3643
static void init(void)
3744
{
@@ -41,6 +48,8 @@ static void init(void)
4148

4249
afsk_init(&afsk, ADC_CH, 0);
4350
ax25_init(&ax25, &afsk.fd, message_callback);
51+
mp1Init(&mp1, &afsk.fd, mp1Callback);
52+
4453

4554
ser_init(&ser, SER_UART0);
4655
ser_setbaudrate(&ser, 115200);
@@ -53,11 +62,13 @@ int main(void)
5362

5463
while (1)
5564
{
56-
// Raw output, no protocol
57-
if (!fifo_isempty(&afsk.rxFifo)) {
58-
char c = fifo_pop(&afsk.rxFifo);
59-
kprintf("%c", c);
60-
}
65+
// Raw read, no protocol
66+
// if (!fifo_isempty(&afsk.rxFifo)) {
67+
// char c = fifo_pop(&afsk.rxFifo);
68+
// kprintf("%c", c);
69+
// }
70+
71+
mp1Poll(&mp1);
6172

6273
// Use AX.25 to send test data
6374
if (timer_clock() - start > ms_to_ticks(4000L))

Modem/protocol/mp1.c

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include "mp1.h"
2+
#include <string.h>
3+
//#include <ctype.h>
4+
5+
static void mp1Decode(MP1 *mp1) {
6+
MP1Packet packet; // A decoded packet struct
7+
uint8_t *buffer = mp1->buffer; // Get the buffer from the protocol context
8+
9+
packet.dataLength = mp1->packetLength;
10+
packet.data = buffer;
11+
12+
if (mp1->callback) mp1->callback(&packet);
13+
}
14+
15+
void mp1Poll(MP1 *mp1) {
16+
int byte;
17+
18+
// Read bytes from the modem until we reach EOF
19+
while ((byte = kfile_getc(mp1->modem)) != EOF) {
20+
if (!mp1->escape && byte == HDLC_FLAG) {
21+
// We are not in an escape sequence and we
22+
// found a HDLC_FLAG. This can mean two things:
23+
if (mp1->packetLength >= MP1_MIN_FRAME_LENGTH) {
24+
// We already have more data than the minimum
25+
// frame length, which means the flag signifies
26+
// the end of the packet. Pass control to the
27+
// decoder.
28+
mp1Decode(mp1);
29+
}
30+
// If the above is not the case, this must be the
31+
// beginning of a frame
32+
mp1->reading = true;
33+
mp1->packetLength = 0;
34+
// We have indicated that we are reading,
35+
// and reset the length counter. Now we'll
36+
// continue to the next byte.
37+
continue;
38+
}
39+
40+
if (!mp1->escape && byte == HDLC_RESET) {
41+
// Not good, we got a reset. The transmitting
42+
// party may have encountered an error. We'll
43+
// stop receiving this packet immediately.
44+
mp1->reading = false;
45+
continue;
46+
}
47+
48+
if (!mp1->escape && byte == AX25_ESC) {
49+
// We found an escape character. We'll set
50+
// the escape seqeunce indicator so we don't
51+
// interpret the next byte as a reset or flag
52+
mp1->escape = true;
53+
continue;
54+
}
55+
56+
// Now let's get to the actual reading of the data
57+
if (mp1->reading) {
58+
if (mp1->packetLength < MP1_MAX_FRAME_LENGTH) {
59+
// If the length of the current incoming frame is
60+
// still less than our max length, put the incoming
61+
// byte in the buffer.
62+
mp1->buffer[mp1->packetLength++] = byte;
63+
} else {
64+
// If not, we have a problem: The buffer has overrun
65+
// We need to stop receiving, and the packet will be
66+
// dropped :(
67+
mp1->reading = false;
68+
}
69+
}
70+
// We need to set the escape sequence indicator back
71+
// to false after each byte.
72+
mp1->escape = false;
73+
}
74+
75+
if (kfile_error(mp1->modem)) {
76+
// If there was an error from the modem, we'll be rude
77+
// and just reset it. No error handling is done for now.
78+
kfile_clearerr(mp1->modem);
79+
}
80+
}
81+
82+
void mp1Init(MP1 *mp1, KFile *modem, mp1_callback_t callback) {
83+
// Allocate memory for our protocol "object"
84+
memset(mp1, 0, sizeof(*mp1));
85+
mp1->modem = modem;
86+
mp1->callback = callback;
87+
}

Modem/protocol/mp1.h

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef PROTOCOL_MP1
2+
#define PROTOCOL_MP1
3+
4+
#include <cfg/compiler.h>
5+
#include <io/kfile.h>
6+
7+
// Frame sizing
8+
#define MP1_MIN_FRAME_LENGTH 1
9+
#define MP1_MAX_FRAME_LENGTH 300
10+
11+
// We need to know some basic HDLC flag bytes
12+
#define HDLC_FLAG 0x7E
13+
#define HDLC_RESET 0x7F
14+
#define AX25_ESC 0x1B
15+
16+
// Just a forward declaration that this struct exists
17+
struct MP1Packet;
18+
19+
// The type of a callback function for passing
20+
// back a decoded packet
21+
typedef void (*mp1_callback_t)(struct MP1Packet *packet);
22+
23+
// Struct for a protocol context
24+
typedef struct MP1 {
25+
uint8_t buffer[MP1_MAX_FRAME_LENGTH]; // A buffer for incoming packets
26+
KFile *modem; // KFile access to the modem
27+
size_t packetLength; // Counter for received packet length
28+
mp1_callback_t callback; // The function to call when a packet has been received
29+
bool reading; // True when we have seen a HDLC flag
30+
bool escape; // We need to know if we are in an escape sequence
31+
} MP1;
32+
33+
// A struct encapsulating a network packet
34+
typedef struct MP1Packet {
35+
const uint8_t *data; // Pointer to the actual data in the packet
36+
size_t dataLength; // The length of the received data
37+
} MP1Packet;
38+
39+
void mp1Init(MP1 *mp1, KFile *modem, mp1_callback_t callback);
40+
void mp1Poll(MP1 *mp1);
41+
void mp1Send(MP1 *mp1, const void *_buffer, size_t length);
42+
43+
#endif

buildrev.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#define VERS_BUILD 126
1+
#define VERS_BUILD 136
22
#define VERS_HOST "vixen"

flash

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/bash
2-
avrdude -p m328p -c arduino -P /dev/ttyACM$1 -b 115200 -F -U flash:w:images/Modem.hex
2+
avrdude -p m328p -c arduino -P /dev/tty$1 -b 115200 -F -U flash:w:images/Modem.hex

0 commit comments

Comments
 (0)