Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Build sa8x8-fw

on:
workflow_dispatch:
push:
paths:
- src/**
Expand All @@ -20,12 +21,14 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v7
with:
fetch-depth: 0 # tags are needed for the firmware version string

- name: Setup
uses: eWaterCycle/setup-apptainer@v2
with:
apptainer-version: 1.3.6
run: |
wget -q https://github.com/apptainer/apptainer/releases/download/v1.3.6/apptainer_1.3.6_amd64.deb
sudo apt-get install -y ./apptainer_1.3.6_amd64.deb

- name: Compile
run: |
Expand All @@ -35,7 +38,7 @@ jobs:
done

- name: Upload
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v7
with:
name: sa8x8-fw-srecs
path: |
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0 # tags are needed for the firmware version string

- name: Setup
uses: eWaterCycle/setup-apptainer@v2
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The following commands are available in this firmware:
| AT+POKE=_REG_,_VAL_ | Set transceiver register contents |
| AT+AMP=_0_/_1_ | Set power amplifier state |
| AT+AUDIO=_0_/_1_ | Set external audio amplifier state |
| AT+TOT=_SEC_ | Set transmit timeout, 0 disables |

## Supported Modules

Expand Down
62 changes: 47 additions & 15 deletions api/python/sa8x8.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ class AT1846S_FUNC(IntEnum):
FUNC_TX = 2


class SA8x8Error(Exception):
"""Module replied ERR, gave an unexpected reply or did not reply."""


class AT1846S:
def i2c_read():
raise NotImplemented("i2c_read not implemented")
def i2c_read(self, reg):
raise NotImplementedError("i2c_read not implemented")

def i2c_write():
raise NotImplemented("i2c_write not implemented")
def i2c_write(self, reg, val):
raise NotImplementedError("i2c_write not implemented")

def initialize(self):
self.i2c_write(0x30, 0x0001) # Soft reset
Expand Down Expand Up @@ -213,7 +217,7 @@ def mode(self, mode):
self.reload()

def frequency(self, freq):
val = int((freq / 1000) * 16)
val = (int(freq) * 16) // 1000 # Units of 1/16 kHz, exact integer math
fHi = (val >> 16) & 0xFFFF
fLo = val & 0xFFFF

Expand All @@ -235,27 +239,55 @@ def __init__(self, ser):
self.xcvr.i2c_read = lambda reg: self.peek(reg)
self.xcvr.i2c_write = lambda reg, val: self.poke(reg, val)

def command(self, cmd):
"""Send one AT command and return its reply line without CR/LF."""
self.ser.write(f'{cmd}\r\n'.encode('ascii'))
line = self.ser.readline()

# readline() returns a partial or empty line when the timeout expires
if not line.endswith(b'\n'):
raise SA8x8Error(f'{cmd}: no reply')

res = line.strip(b'\r\n').decode('ascii')
if res == 'ERR':
raise SA8x8Error(f'{cmd}: ERR')

return res

def expect_ok(self, cmd):
res = self.command(cmd)
if res != 'OK':
raise SA8x8Error(f'{cmd}: unexpected reply {res!r}')

@property
def version(self):
self.ser.write(b'AT+VERSION\r\n')
return self.ser.readline().strip(b'\r\n').decode('utf-8')
return self.command('AT+VERSION')

def peek(self, reg):
msg = f'AT+PEEK={reg}\r\n'
self.ser.write(msg.encode('utf-8'))
val = int(self.ser.readline().strip(b'\r\n').decode('utf-8'))
return val
res = self.command(f'AT+PEEK={reg}')
try:
return int(res)
except ValueError:
raise SA8x8Error(f'AT+PEEK={reg}: unexpected reply {res!r}') from None

def poke(self, reg, val):
msg = f'AT+POKE={reg},{val}\r\n'
self.ser.write(msg.encode('utf-8'))
res = self.ser.readline().strip(b'\r\n').decode('utf-8')
return True if res == b'OK' else False
self.expect_ok(f'AT+POKE={reg},{val}')
return True

def amp(self, enabled):
self.expect_ok(f'AT+AMP={int(bool(enabled))}')

def audio(self, enabled):
self.expect_ok(f'AT+AUDIO={int(bool(enabled))}')

def tot(self, seconds):
self.expect_ok(f'AT+TOT={int(seconds)}')


def main():
with serial.Serial() as ser:
ser.baudrate = 9600
ser.timeout = 1 # Seconds; a lost reply raises instead of hanging
ser.port = '/dev/ttyUSB0'
ser.open()

Expand Down
26 changes: 19 additions & 7 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#define TX (1U << 6)
#define RX (1U << 5)

#define TOT_DEFAULT 180U // Default transmit timeout in seconds, 0 disables

/*
* Bit-level access for uint8_t
*/
Expand Down Expand Up @@ -64,20 +66,28 @@ typedef struct {

/*
* A single ring buffer item
*
* The character is written by the UART receive interrupt and read by the
* main loop, so it is volatile to keep the compiler from caching it.
*/
struct item {
char c;
volatile char c;
struct item *next;
};

/*
* A generic ring buffer structure
*
* The put and get pointers are shared between the UART receive interrupt
* (producer) and the main loop (consumer). They are volatile so that the
* main loop rereads them on every iteration, even if the compiler inlines
* the ring functions (for example with LTO).
*/
struct ring {
uint16_t size;
struct item *buffer;
struct item *put;
struct item *get;
struct item *volatile put;
struct item *volatile get;
};

/*
Expand Down Expand Up @@ -106,7 +116,7 @@ extern struct ring rx;
char up(char c);
int eq(const char *s1, const char *s2, uint16_t len);
const char *i2a(uint16_t n);
uint16_t a2i(const char *s, uint8_t *pos);
bool a2i(const char *s, uint8_t *pos, uint16_t *n);

/*
* Platform specific
Expand All @@ -119,26 +129,28 @@ bool platform_peek(uint8_t reg, uint16_t *val);
bool platform_poke(uint8_t reg, uint16_t val);
void platform_amp(bool enabled);
void platform_audio(bool enabled);
extern const bool PLATFORM_TURBO;
bool platform_tot(uint16_t seconds);

/*
* UART peripheral control
*/
void uart_init(void);
int uart_puts(const char *s);
int uart_gets(char *s, int size);

/*
* I2C peripheral control
*/
void i2c_init(void);
void i2c_write(uint8_t addr, uint8_t reg, uint16_t val);
uint16_t i2c_read(uint8_t addr, uint8_t reg);
bool i2c_write(uint8_t addr, uint8_t reg, uint16_t val);
bool i2c_read(uint8_t addr, uint8_t reg, uint16_t *val);
extern const uint8_t I2C_ADDR_XCVR;

/*
* Time management
*/
void delay(uint16_t n);
extern volatile uint16_t uptime;

#endif

5 changes: 4 additions & 1 deletion src/r5f1026a/interrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ void INT_SRE0(void) {}
void INT_TM01H(void) {}
void INT_TM03H(void) {}
void INT_IICA0(void) {}
void INT_TM00(void) {}
void INT_TM00(void) {
uptime++;
TMIF00 = 0U;
}
void INT_TM01(void) {}
void INT_TM02(void) {}
void INT_TM03(void) {}
Expand Down
Loading