mirror of
https://github.com/qmk/qmk_firmware.git
synced 2026-10-02 14:58:59 +02:00
* bla personal ap2-c18 keymap. * Bidirectional, asynchronous message-based communication with Shine. - Requires a matching Shine version. - Protocol is resiliant to loosing bytes during communication, chips won't lock waiting for bytes that aren't coming. - Chips resynchronize in event of loosing a byte using a AA0D header. Regressions: - Key masking/locking doesn't work right now. (did it work before?) - Not all user keymaps build against it. * Clang-format + code to ease reducing speed of LED UART. - Did clang-format --style=file -i on multiple files according to coding_conventions_c.md - Added separate serial speed for IAP boot and Led communication, it's possible that reducing this to 9600 helped someone with faulty HW. With this code they can do it with simple replacing of a value. * Main chip can set/clear foreground using a mask mechanism. - Some preparations for selective colouring. * Selective mask works - tested on capslock. - Migrated personal keymaps to new status API. * Clear the foreground colors to show profile when it's modified. - Show example of achieving selective caps-lock painting + foreground painting for layers. - annepro2LedMaskSetRow is implemented, but still requires testing. * Implement the QMK side of led blinking to indicate the command was received. - This stupidly blinks the key when user presses one of the bluetooth commands to let the user know that the command was received and forwarded to the BT chip. - TODO: Row/col key positions are hardcoded and not taken from the keymap. * Reduce memory footprint. Applying code review suggestions. Moved msgId to globals - preparing for transmission without copying payload when no retries are necessary. Added empty readme.md files - required by QMK lint. Co-authored-by: Tomasz bla Fortuna <bla@thera.be>
119 lines
2.9 KiB
C
119 lines
2.9 KiB
C
/*
|
|
* (c) 2021 by Tomasz bla Fortuna
|
|
* License: GPLv2
|
|
*
|
|
* This file is shared with the Shine firmware. Keep it in sync (and in the
|
|
* shine's clang formatting).
|
|
*
|
|
* Implementation of a robust serial protocol which can handle single dropped
|
|
* characters during transit without locking.
|
|
*
|
|
* At 115200, transmitting the shortest message takes 0.043ms, at 9600 - 0.52ms.
|
|
*
|
|
*/
|
|
|
|
#include "protocol.h"
|
|
#include "board.h"
|
|
#include "ch.h"
|
|
#include "hal.h"
|
|
|
|
/* UART communication protocol state */
|
|
protocol_t proto;
|
|
|
|
void protoInit(protocol_t *proto, void (*callback)(const message_t *)) {
|
|
proto->previousId = 0;
|
|
proto->callback = callback;
|
|
proto->state = STATE_SYNC_1;
|
|
proto->errors = 0;
|
|
}
|
|
|
|
static uint8_t msgId = 0;
|
|
void protoTx(uint8_t cmd, const unsigned char *buf, int payloadSize,
|
|
int retries) {
|
|
chDbgCheck(payloadSize <= MAX_PAYLOAD_SIZE);
|
|
|
|
const uint8_t header[5] = {
|
|
0x7A, 0x1D, cmd, ++msgId, payloadSize,
|
|
};
|
|
|
|
/* We don't implement ACKs, yet some messages should not be lost. */
|
|
for (int i = 0; i < retries; i++) {
|
|
sdWrite(&PROTOCOL_SD, header, sizeof(header));
|
|
if (payloadSize)
|
|
sdWrite(&PROTOCOL_SD, buf, payloadSize);
|
|
}
|
|
}
|
|
|
|
static inline void messageReceived(protocol_t *proto) {
|
|
if (proto->buffer.msgId != proto->previousId) {
|
|
/* It's not a resend / duplicate */
|
|
proto->callback(&proto->buffer);
|
|
proto->previousId = proto->buffer.msgId;
|
|
}
|
|
proto->state = STATE_SYNC_1;
|
|
}
|
|
|
|
void protoConsume(protocol_t *proto, uint8_t byte) {
|
|
switch (proto->state) {
|
|
case STATE_SYNC_1:
|
|
if (byte == 0x7A) {
|
|
proto->state = STATE_SYNC_2;
|
|
} else {
|
|
proto->errors++;
|
|
}
|
|
return;
|
|
|
|
case STATE_SYNC_2:
|
|
if (byte == 0x1D) {
|
|
proto->state = STATE_CMD;
|
|
} else {
|
|
proto->state = STATE_SYNC_1;
|
|
proto->errors++;
|
|
}
|
|
return;
|
|
|
|
case STATE_CMD:
|
|
proto->buffer.command = byte;
|
|
proto->state = STATE_ID;
|
|
return;
|
|
|
|
case STATE_ID:
|
|
proto->buffer.msgId = byte;
|
|
proto->state = STATE_PAYLOAD_SIZE;
|
|
return;
|
|
|
|
case STATE_PAYLOAD_SIZE:
|
|
proto->buffer.payloadSize = byte;
|
|
if (proto->buffer.payloadSize > MAX_PAYLOAD_SIZE) {
|
|
proto->buffer.payloadSize = MAX_PAYLOAD_SIZE;
|
|
proto->errors++;
|
|
}
|
|
proto->payloadPosition = 0;
|
|
if (proto->buffer.payloadSize == 0) {
|
|
/* No payload - whole message received */
|
|
messageReceived(proto);
|
|
} else {
|
|
proto->state = STATE_PAYLOAD;
|
|
}
|
|
return;
|
|
|
|
case STATE_PAYLOAD:
|
|
/* NOTE: This could be read with sdReadTimeout probably, but that breaks
|
|
* abstraction */
|
|
proto->buffer.payload[proto->payloadPosition] = byte;
|
|
proto->payloadPosition++;
|
|
if (proto->payloadPosition == proto->buffer.payloadSize) {
|
|
/* Payload read - message received */
|
|
messageReceived(proto);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
void protoSilence(protocol_t *proto) {
|
|
if (proto->state != STATE_SYNC_1) {
|
|
proto->state = STATE_SYNC_1;
|
|
proto->errors++;
|
|
}
|
|
}
|