Skip to content

Commit

Permalink
Changed arrangement in ngham_packets.h structs (be careful\!), added …
Browse files Browse the repository at this point in the history
…NGHam SPP python example, started on NGHam SPP C-code, updated documentation.
  • Loading branch information
skagmo committed Feb 8, 2015
1 parent cb44d94 commit 8cd8e9c
Show file tree
Hide file tree
Showing 36 changed files with 496 additions and 84 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
ngham_platform.c
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,30 @@ Content:

| File/folder | Description |
|:----------- |:----------- |
| documentation/ | More documentation. |
| fec-3.0.1/ | FEC library by Phil Karn for Reed Solomon. (http://www.ka9q.net/code/fec/) |
| other/ | More documentation. |
| platform/ | User provided - MUST BE CUSTOMIZED. |
| ccsds_scrambler.h | Pre-generated array from scrambling polynomial. |
| crc_ccitt.h | CRC-code used in NGHam. |
| ngham.h | The main NGHam encoder/decoder functions. |
| ngham_packets.h | Declarations of the TX/RX packet structs etc. |
| ngham_platform.h | User provided code - MUST BE CUSTOMIZED. |
| crc_ccitt.h | CRC-code used in NGHam. |
| ccsds_scrambler.h | Pre-generated array from scrambling polynomial. |
| ngham_paths.h | User provided - MUST BE CUSTOMIZED. |
| ngham_spp.h | Serial port protocol for communication with the host. |
| ngham_spp_test.py | Python code for NGHAM SPP communication. |


Not finished:

| File/folder | Description |
|:----------- |:----------- |
| ngham_extensions.h | An extension of the payload field in the NGHam radio protocol (enabled by a flag in the NGHam header). |
| ngham_spp.h | Serial port protocol for communication with the host. |

Usage:
Usage (for NGHam RF protocol):

1. Provide your own sync word correlator and GMSK (de)modulator.
2. The demodulator should start outputting data only after sync word is detected.
3. ngham_platform.h/c must be customized for the platform you are using.
3. Build all files in the root except "ngham_spp.c". From the folder "fec-3.0.1", build "decode_rs_char.c", "encode_rs_char.c" and "init_rs_char.c".
3. "ngham_paths.h" and the code in "platform/" must be customized.

```
// Initialize Reed Solomon arrays - only do this once!
Expand Down
Binary file added documentation/illustrations/ngham_block_v3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added documentation/illustrations/ngham_block_v3.vsd
Binary file not shown.
File renamed without changes.
Binary file added documentation/illustrations/ngham_spp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added documentation/illustrations/ngham_spp.vsd
Binary file not shown.
Binary file not shown.
Binary file added documentation/ngham_protocol_description.pdf
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@ Multple types of data can be fitted inside a NGHam packet, by starting with a ne

Extension data types:
0x00 Binary data of unknown type
0x01 Address
0x01 Ham address - everything after an adress belongs to the transmitting station
0x02 Statistics
0x03 Statistics for timing reference
0x04 Position
0x05 Voice
0x06 JPEG image
0x07 Repeated ham adress (originates from another station)

** Address **
Size is 6-17 B - eg. LA3JPA to broadcast will be 10 B, LA1K to broadcast is 8B, LA1K123Y to LA1K100Y is 17B
* 2-7B source: 4 bit LEN (1-8), 4 bit SSID, LEN x 6 bit (chars), fill to n*8 - LA3JPA-3
* 1-7B destination: 4 bit LEN (0-8, 0=broadcast), 4 bit SSID, LEN x 6 bit (chars), fill to n*8 - LA3JPA-3
Size is 6-17 B - eg. LA3JPA to broadcast will be 8B, LA1K to broadcast is 6B, LA1K123Y to LA1K100Y is 16B
* 2-7B mycall: 4 bit LEN (1-8), 4 bit SSID, LEN x 6 bit (chars), fill to n*8 - LA3JPA-3
* 1B sequence number
* 1B extended fields, packet type:
* 1B fields, packet type:
1b destcall_en - if this is not a broadcast packet, insert destination callsign
1b hopping_field_en
1b timeslot_field_en
1b connected_field_en
* 1B hopping: 1b reserved, 1b add_path_trace, 1b fill_in_hop_finished, 1b fill_in_hop_left, 2b hops_finished, 2b hops_left
If path_trace_en is set, fill_in_hops_finished+hops_finished callsigns follow (maximum 4):
2-7B: 4 bit LEN (1-8), 4 bit SSID, LEN x 6 bit (chars), fill to n*8 - LA3JPA-3
* 1-7B destcall: 4 bit LEN (1-8), 4 bit SSID, LEN x 6 bit (chars), fill to n*8 - LA3JPA-3
* 1B hopping: 2b reserved, 1b fill_in_hop_finished, 1b fill_in_hop_total, 2b hops_finished, 2b hops_total (not including fill-in)
* 2B timeslot information
* 1B connected mode information: 0=cts, 1=rts, 2=ack, 3=nack


** Position **
If address is enabled, use broadcast. Address size will therefore be 11 B.
Wait one full timeslot period to generate adress book, then picks random free timeslot
Expand All @@ -47,3 +46,9 @@ Takes less than 140 ms to transmit at 4800 baud. Will fit in 60 B payload size w
* Comment
* Icon
( * 2B timeslot information: 12b first timeslot, 4 b timeslot modulo (transmits every 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024) )


Repeated packet:
Address
Repeated address
Position
11 changes: 7 additions & 4 deletions ngham.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@

#include "ngham.h"

#include "fec-3.0.1/char.h" // Reed Solomon code from Phil Karn
#include "fec-3.0.1/rs-common.h"
#include "fec-3.0.1/fec.h" // RS code from Phil Karn
#include "fec-3.0.1/char.h" // RS code specific to 8-bit symbol size
#include "fec-3.0.1/rs-common.h" // RS control block struct
#include <stddef.h> // For NULL etc.
#include <string.h> // For memcpy
#include "ccsds_scrambler.h" // Pre-generated array from scrambling polynomial
#include "ngham_packets.h" // Structs for TX and RX packets
#include "crc_ccitt.h"
#include "ngham_platform.h" // CRC-code

#include "ngham_paths.h"
#include PATH_NGHAM_PLATFORM

// There are seven different sizes.
// Each size has a correlation tag for size, a total size, a maximum payload size and a parity data size.
Expand Down Expand Up @@ -141,7 +144,7 @@ void ngham_decode(uint8_t d){
static uint32_t size_tag;

switch (decoder_state){

case NGH_STATE_SIZE_TAG:
size_tag = 0;
ngham_action_reception_started();
Expand Down
1 change: 1 addition & 0 deletions ngham_packets.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ void rx_pkt_init(rx_pkt_t *p){
void tx_pkt_init(tx_pkt_t *p){
p->pl_len = 0;
p->ngham_flags = 0;
p->priority = PKT_PRIORITY_NORMAL;
}
26 changes: 18 additions & 8 deletions ngham_packets.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,33 @@
// If the following flag is set in a packet, NGHam extensions are used and first byte is type
#define NGHAM_FLAG_TYPE_EXTENSION 0x01

typedef struct{
#define SPP_PL_MAX 512

typedef struct __attribute__ ((packed)){
uint32_t timestamp; // Time stamp of sync word detection
uint16_t pl_len;
uint8_t pl[PKT_PL_SIZE];
uint8_t ngham_flags;
uint8_t rssi; // In dBm + 200
uint8_t noise; // Same as above
uint8_t rssi; // In dBm + 200
uint8_t errors; // Recovered symbols
uint32_t timestamp; // Time stamp of sync word detection
uint8_t ngham_flags;
uint8_t pl[PKT_PL_SIZE];
}rx_pkt_t;

typedef struct{
typedef struct __attribute__ ((packed)){
uint16_t pl_len;
uint8_t pl[PKT_PL_SIZE];
uint8_t ngham_flags;
uint8_t priority;
uint8_t ngham_flags;
uint8_t pl[PKT_PL_SIZE];
}tx_pkt_t;

// NGHam SPP header
typedef struct __attribute__ ((packed)){
uint8_t start;
uint16_t crc;
uint8_t pl_type;
uint8_t pl_len;
}ngh_spphdr_t;

void rx_pkt_init(rx_pkt_t *p);
void tx_pkt_init(tx_pkt_t *p);

Expand Down
7 changes: 7 additions & 0 deletions ngham_paths.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Path to main NGHAM platform specific code
#define PATH_NGHAM_PLATFORM "c:/lagring/prosjekter/owl_vhf/firmware/ngham_owl_platform.h"
// #define PATH_NGHAM_PLATFORM "platform/platform.h"

// Path to NGHAM serial port protocol platform code
#define PATH_NGHAM_PLATFORM_SPP "c:/lagring/prosjekter/owl_vhf/firmware/ngham_owl_platform_spp.h"
// #define PATH_NGHAM_PLATFORM_SPP "platform/platform_spp.h"
55 changes: 0 additions & 55 deletions ngham_platform.h

This file was deleted.

103 changes: 103 additions & 0 deletions ngham_spp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//**************************************************************//
// NGHam protocol - Jon Petter Skagmo, LA3JPA, 2014. //
// Licensed under LGPL. //
//**************************************************************//

#include "ngham_packets.h"
#include "crc_ccitt.h"

#include "ngham_paths.h"
#include PATH_NGHAM_PLATFORM_SPP

#define SPP_START 0x24 // Packet start byte definition

#define SPP_STATE_START 0x00 // States
#define SPP_STATE_HEADER 0x01
#define SPP_STATE_PAYLOAD 0x02

#define SPP_TYPE_RF 0x00 // Packet types
#define SPP_TYPE_LOCAL 0x01
#define SPP_TYPE_CMD 0x02

void ngham_pack_byte(port_ctx_t* ctx, uint8_t c){
switch(ctx->state){
case SPP_STATE_START:
if (c == SPP_START){
ctx->state = SPP_STATE_HEADER; // Start found; go to next state
ctx->d_ip = 1; // Starts at next first as SPP_START is already received
}
break;

case SPP_STATE_HEADER:
((uint8_t*)&ctx->ngh_spp)[ctx->d_ip++] = c; // Fill header
if (ctx->d_ip >= sizeof(ngh_spphdr_t)){
ctx->d_ip = 0;
ctx->state = SPP_STATE_PAYLOAD;
}
break;

case SPP_STATE_PAYLOAD:
if (ctx->d_ip < PORT_BUF_SIZE) ctx->d[ctx->d_ip++] = c; // Fill ctx->d with payload
if (ctx->d_ip >= ctx->ngh_spp.pl_len){
ctx->state = SPP_STATE_START;

// Calc CRC of type, length and payload
int j;
uint16_t crc = 0xffff;
for (j=3; j<5; j++) crc = crc_ccitt_byte(((uint8_t*)&ctx->ngh_spp)[j], crc);
for (j=0; j<ctx->d_ip; j++) crc = crc_ccitt_byte(ctx->d[j], crc);
crc ^= 0xffff;
if (crc == ctx->ngh_spp.crc){
switch(ctx->ngh_spp.pl_type){
case SPP_TYPE_RF:
{
// Data to be sent
tx_pkt_t p;
tx_pkt_init(&p);
p.ngham_flags = ctx->d[0]; // First byte in NGH SPP RF type is flags
p.pl_len = ctx->d_ip-1;
for (j=0; j<p.pl_len; j++) p.pl[j] = ctx->d[j+1];
PACKER_CALL(&p);

// TODO: Generate response!
}
break;
case SPP_TYPE_CMD:
{
// Prepare command and response
ngh_spphdr_t hdr;
uint8_t r[sizeof(hdr)+SPP_PL_MAX];
hdr.start = SPP_START;
hdr.pl_type = SPP_TYPE_CMD;
hdr.pl_len = 0;
cmd(ctx->d, ctx->ngh_spp.pl_len, &r[sizeof(hdr)], &hdr.pl_len); // Run command
for (j=3; j<5; j++) r[j] = ((uint8_t*)&hdr)[j]; // Copy length and type into header
hdr.crc = crc_ccitt(&r[3], hdr.pl_len+2);
for (j=0; j<3; j++) r[j] = ((uint8_t*)&hdr)[j]; // Copy CRC and stat into header
port_output(ctx, r, sizeof(hdr)+hdr.pl_len); // Send to port
}
break;
}
}
}
break;
}
}

void ngham_unpack(rx_pkt_t* p){
int j;
ngh_spphdr_t hdr;
hdr.start = SPP_START;
hdr.crc = 0xffff;
hdr.pl_type = SPP_TYPE_RF;
hdr.pl_len = p->pl_len+4;

// Calc CRC
for (j=3; j<5; j++) hdr.crc = crc_ccitt_byte(((uint8_t*)&hdr)[j], hdr.crc);
for (j=0; j<hdr.pl_len; j++) hdr.crc = crc_ccitt_byte(((uint8_t*)&p->noise)[j], hdr.crc); // SPP-protocol must match with rx_pkt_t struct
hdr.crc ^= 0xffff;

// Copy remaining and send to port
UNPACKER_CALL(PACKER_NGHAM, (uint8_t*)&hdr, sizeof(ngh_spphdr_t));
UNPACKER_CALL(PACKER_NGHAM, (uint8_t*)&p->noise, hdr.pl_len);
}
7 changes: 7 additions & 0 deletions ngham_spp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@
// Licensed under LGPL. //
//**************************************************************//

#include <stdint.h>

#include "ngham_paths.h"
#include PATH_NGHAM_PLATFORM_SPP

void ngham_pack_byte(port_ctx_t* ctx, uint8_t c);
void ngham_unpack(rx_pkt_t* p);
Loading

0 comments on commit 8cd8e9c

Please sign in to comment.