Skip to content

Commit

Permalink
Respond to I2C read request with an info structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
keirf committed Oct 8, 2019
1 parent 32e05d2 commit d42322c
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 13 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ dist: all
endif

BAUD=921600
DEV=/dev/ttyUSB1

flash: all
sudo ~/stm32flash/stm32flash -b $(BAUD) \
-vw src/$(PROJ).hex /dev/ttyUSB0
-vw src/$(PROJ).hex $(DEV)

start:
sudo ~/stm32flash/stm32flash -b $(BAUD) -g 0 /dev/ttyUSB0
sudo ~/stm32flash/stm32flash -b $(BAUD) -g 0 $(DEV)

serial:
sudo miniterm.py /dev/ttyUSB0 115200
sudo miniterm.py $(DEV) 115200
2 changes: 1 addition & 1 deletion inc/decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#include <stdarg.h>
#include <stddef.h>

#include "intrinsics.h"
#include "util.h"
#include "stm32f10x_regs.h"
#include "stm32f10x.h"
#include "intrinsics.h"

#include "config.h"
#include "cancellation.h"
Expand Down
10 changes: 9 additions & 1 deletion inc/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ int strncmp(const char *s1, const char *s2, size_t n);
char *strcpy(char *dest, const char *src);
char *strrchr(const char *s, int c);
int tolower(int c);
int isspace(int c);

long int strtol(const char *nptr, char **endptr, int base);

uint16_t crc16_ccitt(const void *buf, size_t len, uint16_t crc);

Expand Down Expand Up @@ -114,7 +117,12 @@ void lcd_init(void);
void lcd_process(void);
extern struct display lcd_display;
extern bool_t ff_osd_i2c_protocol;
extern uint8_t ff_osd_buttons;
extern uint8_t i2c_buttons_rx; /* Gotek -> FF_OSD */
extern struct __packed i2c_osd_info {
uint8_t protocol_ver;
uint8_t fw_major, fw_minor;
uint8_t buttons;
} i2c_osd_info;

/* Build info. */
extern const char fw_ver[];
Expand Down
30 changes: 23 additions & 7 deletions src/lcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ static uint16_t t_cons, t_prod;
static bool_t lcd_inc;
static uint8_t lcd_ddraddr;
struct display lcd_display;
uint8_t ff_osd_buttons;
uint8_t i2c_buttons_rx;
struct i2c_osd_info i2c_osd_info;

/* I2C Error ISR: As slave with clock stretc we can only receive:
/* I2C Error ISR: As slave with clock stretch we can only receive:
* Bus error (BERR): Peripheral automatically recovers
* Arbitration lost (ARLO): Peripheral automatically recovers */
* Acknowledge Failure (AF): Peripheral automatically recovers */
static void IRQ_i2c_error(void)
{
/* Clear I2C errors. Nothing else needs to be done. */
Expand All @@ -60,12 +61,15 @@ static void IRQ_i2c_error(void)

static void IRQ_i2c_event(void)
{
static uint8_t rp;
uint16_t sr1 = i2c->sr1;

if (sr1 & I2C_SR1_ADDR) {
/* Read SR2 clears SR1_ADDR. */
(void)i2c->sr2;
t_ring[MASK(t_ring, t_prod++)] = d_prod;
uint16_t sr2 = i2c->sr2;
if (!(sr2 & I2C_SR2_TRA))
t_ring[MASK(t_ring, t_prod++)] = d_prod;
rp = 0;
}

if (sr1 & I2C_SR1_STOPF) {
Expand All @@ -74,9 +78,15 @@ static void IRQ_i2c_event(void)
}

if (sr1 & I2C_SR1_RXNE) {
/* Read DR clear SR1_RXNE. */
/* Read DR clears SR1_RXNE. */
d_ring[MASK(d_ring, d_prod++)] = i2c->dr;
}

if (sr1 & I2C_SR1_TXE) {
/* Write DR clears SR1_TXE. */
uint8_t *info = (uint8_t *)&i2c_osd_info;
i2c->dr = (rp < sizeof(i2c_osd_info)) ? info[rp++] : 0;
}
}

static void process_cmd(uint8_t cmd)
Expand Down Expand Up @@ -190,7 +200,7 @@ static void ff_osd_process(void)
} else {
switch (x & 0xf0) {
case OSD_BUTTONS:
ff_osd_buttons = x & 0x0f;
i2c_buttons_rx = x & 0x0f;
break;
case OSD_ROWS:
/* 0-3 */
Expand Down Expand Up @@ -256,6 +266,12 @@ void lcd_process(void)

void lcd_init(void)
{
char *p;

i2c_osd_info.protocol_ver = 0;
i2c_osd_info.fw_major = strtol(fw_ver, &p, 10);
i2c_osd_info.fw_minor = strtol(p+1, NULL, 10);

rcc->apb1enr |= RCC_APB1ENR_I2C1EN;

gpio_configure_pin(gpiob, SCL, AFO_opendrain(_2MHz));
Expand Down
8 changes: 7 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ struct gotek_button {
} gl, gr, gs;

static bool_t gotek_active;

static void emulate_gotek_button(
uint8_t keycode, struct gotek_button *button, int pin)
{
Expand All @@ -509,13 +510,18 @@ static void emulate_gotek_button(

static void emulate_gotek_buttons(void)
{
uint8_t b = 0;
if (config_active)
gotek_active = FALSE;
else if (!gotek_active && !keys)
gotek_active = TRUE; /* only after keys are released */
emulate_gotek_button(K_LEFT, &gl, 3);
emulate_gotek_button(K_RIGHT, &gr, 4);
emulate_gotek_button(K_SELECT, &gs, 5);
if (gl.pressed) b |= B_LEFT;
if (gr.pressed) b |= B_RIGHT;
if (gs.pressed) b |= B_SELECT;
*(volatile uint8_t *)&i2c_osd_info.buttons = b;
}

static struct display no_display;
Expand Down Expand Up @@ -783,7 +789,7 @@ int main(void)
if (keys & K_MENU) b |= B_SELECT;
}
/* Fold in button presses remoted via I2C. */
b |= ff_osd_buttons;
b |= i2c_buttons_rx;
/* Pass button presses to config subsystem for processing. */
config_process(b & ~B_PROCESSED);
}
Expand Down
62 changes: 62 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,68 @@ int tolower(int c)
return c;
}

int isspace(int c)
{
return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r')
|| (c == '\f') || (c == '\v');
}

long int strtol(const char *nptr, char **endptr, int base)
{
long int val = 0;
const char *p = nptr;
bool_t is_neg = FALSE;
int c;

/* Optional whitespace prefix. */
while (isspace(*p))
p++;
c = tolower(*p);

/* Optional sign prefix: +, -. */
if ((c == '+') || (c == '-')) {
is_neg = (c == '-');
c = tolower(*++p);
}

/* Optional base prefix: 0, 0x. */
if (c == '0') {
if (base == 0)
base = 8;
c = tolower(*++p);
if (c == 'x') {
if (base == 0)
base = 16;
if (base != 16)
goto out;
c = tolower(*++p);
}
}

if (base == 0)
base = 10;

/* Digits. */
for (;;) {
/* Convert c to a digit [0123456789abcdefghijklmnopqrstuvwxyz]. */
if ((c >= '0') && (c <= '9'))
c -= '0';
else if ((c >= 'a') && (c <= 'z'))
c -= 'a' - 10;
else
break;
if (c >= base)
break;
val = (val * base) + c;
c = tolower(*++p);
}

out:
if (endptr)
*endptr = (char *)p;
return is_neg ? -val : val;
}

const static uint16_t crc16_ccitt_table[] = {
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
Expand Down

0 comments on commit d42322c

Please sign in to comment.