2019-10-18 19:06:28 +00:00
|
|
|
/*
|
|
|
|
* WARNING: be careful changing this code, it is very timing dependent
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "serial.h"
|
2023-07-09 10:03:50 +00:00
|
|
|
#include "gpio.h"
|
2019-10-18 19:06:28 +00:00
|
|
|
#include "wait.h"
|
2022-04-19 10:56:16 +00:00
|
|
|
#include "synchronization_util.h"
|
2019-10-18 19:06:28 +00:00
|
|
|
|
2020-12-11 02:45:24 +00:00
|
|
|
#include <hal.h>
|
2019-10-18 19:06:28 +00:00
|
|
|
|
|
|
|
// TODO: resolve/remove build warnings
|
2023-09-25 03:06:42 +00:00
|
|
|
#if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT) && defined(PROTOCOL_CHIBIOS) && defined(WS2812_BITBANG)
|
2019-10-18 19:06:28 +00:00
|
|
|
# warning "RGBLED_SPLIT not supported with bitbang WS2812 driver"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// default wait implementation cannot be called within interrupt
|
|
|
|
// this method seems to be more accurate than GPT timers
|
|
|
|
#if PORT_SUPPORTS_RT == FALSE
|
|
|
|
# error "chSysPolledDelayX method not supported on this platform"
|
|
|
|
#else
|
|
|
|
# undef wait_us
|
2022-07-11 13:17:05 +00:00
|
|
|
// Force usage of polled waiting - in case WAIT_US_TIMER is activated
|
|
|
|
# define wait_us(us) chSysPolledDelayX(US2RTC(REALTIME_COUNTER_CLOCK, us))
|
2019-10-18 19:06:28 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SELECT_SOFT_SERIAL_SPEED
|
|
|
|
# define SELECT_SOFT_SERIAL_SPEED 1
|
|
|
|
// TODO: correct speeds...
|
|
|
|
// 0: about 189kbps (Experimental only)
|
|
|
|
// 1: about 137kbps (default)
|
|
|
|
// 2: about 75kbps
|
|
|
|
// 3: about 39kbps
|
|
|
|
// 4: about 26kbps
|
|
|
|
// 5: about 20kbps
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Serial pulse period in microseconds. At the moment, going lower than 12 causes communication failure
|
|
|
|
#if SELECT_SOFT_SERIAL_SPEED == 0
|
|
|
|
# define SERIAL_DELAY 12
|
|
|
|
#elif SELECT_SOFT_SERIAL_SPEED == 1
|
|
|
|
# define SERIAL_DELAY 16
|
|
|
|
#elif SELECT_SOFT_SERIAL_SPEED == 2
|
|
|
|
# define SERIAL_DELAY 24
|
|
|
|
#elif SELECT_SOFT_SERIAL_SPEED == 3
|
|
|
|
# define SERIAL_DELAY 32
|
|
|
|
#elif SELECT_SOFT_SERIAL_SPEED == 4
|
|
|
|
# define SERIAL_DELAY 48
|
|
|
|
#elif SELECT_SOFT_SERIAL_SPEED == 5
|
|
|
|
# define SERIAL_DELAY 64
|
|
|
|
#else
|
|
|
|
# error invalid SELECT_SOFT_SERIAL_SPEED value
|
|
|
|
#endif
|
|
|
|
|
2022-02-12 18:29:31 +00:00
|
|
|
inline static void serial_delay(void) {
|
|
|
|
wait_us(SERIAL_DELAY);
|
|
|
|
}
|
|
|
|
inline static void serial_delay_half(void) {
|
|
|
|
wait_us(SERIAL_DELAY / 2);
|
|
|
|
}
|
|
|
|
inline static void serial_delay_blip(void) {
|
|
|
|
wait_us(1);
|
|
|
|
}
|
|
|
|
inline static void serial_output(void) {
|
2024-02-18 06:08:27 +00:00
|
|
|
gpio_set_pin_output(SOFT_SERIAL_PIN);
|
2022-02-12 18:29:31 +00:00
|
|
|
}
|
|
|
|
inline static void serial_input(void) {
|
2024-02-18 06:08:27 +00:00
|
|
|
gpio_set_pin_input_high(SOFT_SERIAL_PIN);
|
2022-02-12 18:29:31 +00:00
|
|
|
}
|
|
|
|
inline static bool serial_read_pin(void) {
|
2024-02-18 06:08:27 +00:00
|
|
|
return !!gpio_read_pin(SOFT_SERIAL_PIN);
|
2022-02-12 18:29:31 +00:00
|
|
|
}
|
|
|
|
inline static void serial_low(void) {
|
2024-02-18 06:08:27 +00:00
|
|
|
gpio_write_pin_low(SOFT_SERIAL_PIN);
|
2022-02-12 18:29:31 +00:00
|
|
|
}
|
|
|
|
inline static void serial_high(void) {
|
2024-02-18 06:08:27 +00:00
|
|
|
gpio_write_pin_high(SOFT_SERIAL_PIN);
|
2022-02-12 18:29:31 +00:00
|
|
|
}
|
2019-10-18 19:06:28 +00:00
|
|
|
|
|
|
|
void interrupt_handler(void *arg);
|
|
|
|
|
|
|
|
// Use thread + palWaitLineTimeout instead of palSetLineCallback
|
2024-02-18 06:08:27 +00:00
|
|
|
// - Methods like gpio_set_pin_output and palEnableLineEvent/palDisableLineEvent
|
2019-10-18 19:06:28 +00:00
|
|
|
// cause the interrupt to lock up, which would limit to only receiving data...
|
|
|
|
static THD_WORKING_AREA(waThread1, 128);
|
|
|
|
static THD_FUNCTION(Thread1, arg) {
|
|
|
|
(void)arg;
|
|
|
|
chRegSetThreadName("blinker");
|
|
|
|
while (true) {
|
|
|
|
palWaitLineTimeout(SOFT_SERIAL_PIN, TIME_INFINITE);
|
|
|
|
interrupt_handler(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 23:10:06 +00:00
|
|
|
void soft_serial_initiator_init(void) {
|
2019-10-18 19:06:28 +00:00
|
|
|
serial_output();
|
|
|
|
serial_high();
|
|
|
|
}
|
|
|
|
|
2021-06-17 23:10:06 +00:00
|
|
|
void soft_serial_target_init(void) {
|
2019-10-18 19:06:28 +00:00
|
|
|
serial_input();
|
|
|
|
|
|
|
|
palEnablePadEvent(PAL_PORT(SOFT_SERIAL_PIN), PAL_PAD(SOFT_SERIAL_PIN), PAL_EVENT_MODE_FALLING_EDGE);
|
|
|
|
chThdCreateStatic(waThread1, sizeof(waThread1), HIGHPRIO, Thread1, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used by the master to synchronize timing with the slave.
|
|
|
|
static void __attribute__((noinline)) sync_recv(void) {
|
|
|
|
serial_input();
|
|
|
|
// This shouldn't hang if the slave disconnects because the
|
|
|
|
// serial line will float to high if the slave does disconnect.
|
|
|
|
while (!serial_read_pin()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
serial_delay();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used by the slave to send a synchronization signal to the master.
|
|
|
|
static void __attribute__((noinline)) sync_send(void) {
|
|
|
|
serial_output();
|
|
|
|
|
|
|
|
serial_low();
|
|
|
|
serial_delay();
|
|
|
|
|
|
|
|
serial_high();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads a byte from the serial line
|
|
|
|
static uint8_t __attribute__((noinline)) serial_read_byte(void) {
|
|
|
|
uint8_t byte = 0;
|
|
|
|
serial_input();
|
|
|
|
for (uint8_t i = 0; i < 8; ++i) {
|
|
|
|
byte = (byte << 1) | serial_read_pin();
|
|
|
|
serial_delay();
|
|
|
|
}
|
|
|
|
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sends a byte with MSB ordering
|
|
|
|
static void __attribute__((noinline)) serial_write_byte(uint8_t data) {
|
|
|
|
uint8_t b = 8;
|
|
|
|
serial_output();
|
|
|
|
while (b--) {
|
|
|
|
if (data & (1 << b)) {
|
|
|
|
serial_high();
|
|
|
|
} else {
|
|
|
|
serial_low();
|
|
|
|
}
|
|
|
|
serial_delay();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// interrupt handle to be used by the slave device
|
|
|
|
void interrupt_handler(void *arg) {
|
2022-06-20 22:24:53 +00:00
|
|
|
split_shared_memory_lock_autounlock();
|
2019-10-18 19:06:28 +00:00
|
|
|
chSysLockFromISR();
|
|
|
|
|
|
|
|
sync_send();
|
|
|
|
|
|
|
|
// read mid pulses
|
|
|
|
serial_delay_blip();
|
|
|
|
|
|
|
|
uint8_t checksum_computed = 0;
|
|
|
|
int sstd_index = 0;
|
|
|
|
|
|
|
|
sstd_index = serial_read_byte();
|
|
|
|
sync_send();
|
|
|
|
|
2021-06-17 23:10:06 +00:00
|
|
|
split_transaction_desc_t *trans = &split_transaction_table[sstd_index];
|
2019-10-18 19:06:28 +00:00
|
|
|
for (int i = 0; i < trans->initiator2target_buffer_size; ++i) {
|
2021-06-17 23:10:06 +00:00
|
|
|
split_trans_initiator2target_buffer(trans)[i] = serial_read_byte();
|
2019-10-18 19:06:28 +00:00
|
|
|
sync_send();
|
2021-06-17 23:10:06 +00:00
|
|
|
checksum_computed += split_trans_initiator2target_buffer(trans)[i];
|
2019-10-18 19:06:28 +00:00
|
|
|
}
|
|
|
|
checksum_computed ^= 7;
|
2022-03-23 16:38:51 +00:00
|
|
|
|
|
|
|
serial_read_byte();
|
2019-10-18 19:06:28 +00:00
|
|
|
sync_send();
|
|
|
|
|
|
|
|
// wait for the sync to finish sending
|
|
|
|
serial_delay();
|
|
|
|
|
2021-06-17 23:10:06 +00:00
|
|
|
// Allow any slave processing to occur
|
|
|
|
if (trans->slave_callback) {
|
|
|
|
trans->slave_callback(trans->initiator2target_buffer_size, split_trans_initiator2target_buffer(trans), trans->target2initiator_buffer_size, split_trans_target2initiator_buffer(trans));
|
|
|
|
}
|
|
|
|
|
2019-10-18 19:06:28 +00:00
|
|
|
uint8_t checksum = 0;
|
|
|
|
for (int i = 0; i < trans->target2initiator_buffer_size; ++i) {
|
2021-06-17 23:10:06 +00:00
|
|
|
serial_write_byte(split_trans_target2initiator_buffer(trans)[i]);
|
2019-10-18 19:06:28 +00:00
|
|
|
sync_send();
|
|
|
|
serial_delay_half();
|
2021-06-17 23:10:06 +00:00
|
|
|
checksum += split_trans_target2initiator_buffer(trans)[i];
|
2019-10-18 19:06:28 +00:00
|
|
|
}
|
|
|
|
serial_write_byte(checksum ^ 7);
|
|
|
|
sync_send();
|
|
|
|
|
|
|
|
// wait for the sync to finish sending
|
|
|
|
serial_delay();
|
|
|
|
|
|
|
|
// end transaction
|
|
|
|
serial_input();
|
|
|
|
|
|
|
|
// TODO: remove extra delay between transactions
|
|
|
|
serial_delay();
|
|
|
|
|
|
|
|
chSysUnlockFromISR();
|
|
|
|
}
|
|
|
|
|
2022-04-19 10:56:16 +00:00
|
|
|
static inline bool initiate_transaction(uint8_t sstd_index) {
|
2022-01-27 05:13:27 +00:00
|
|
|
if (sstd_index > NUM_TOTAL_TRANSACTIONS) return false;
|
2022-04-19 10:56:16 +00:00
|
|
|
|
2022-06-20 22:24:53 +00:00
|
|
|
split_shared_memory_lock_autounlock();
|
|
|
|
|
2021-06-17 23:10:06 +00:00
|
|
|
split_transaction_desc_t *trans = &split_transaction_table[sstd_index];
|
2019-10-18 19:06:28 +00:00
|
|
|
|
|
|
|
// TODO: remove extra delay between transactions
|
|
|
|
serial_delay();
|
|
|
|
|
|
|
|
// this code is very time dependent, so we need to disable interrupts
|
|
|
|
chSysLock();
|
|
|
|
|
|
|
|
// signal to the slave that we want to start a transaction
|
|
|
|
serial_output();
|
|
|
|
serial_low();
|
|
|
|
serial_delay_blip();
|
|
|
|
|
|
|
|
// wait for the slaves response
|
|
|
|
serial_input();
|
|
|
|
serial_high();
|
|
|
|
serial_delay();
|
|
|
|
|
|
|
|
// check if the slave is present
|
|
|
|
if (serial_read_pin()) {
|
|
|
|
// slave failed to pull the line low, assume not present
|
2022-06-17 22:04:17 +00:00
|
|
|
serial_dprintf("serial::NO_RESPONSE\n");
|
2019-10-18 19:06:28 +00:00
|
|
|
chSysUnlock();
|
2022-01-27 05:13:27 +00:00
|
|
|
return false;
|
2019-10-18 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 10:56:16 +00:00
|
|
|
// if the slave is present synchronize with it
|
2019-10-18 19:06:28 +00:00
|
|
|
uint8_t checksum = 0;
|
|
|
|
// send data to the slave
|
2022-02-12 18:29:31 +00:00
|
|
|
serial_write_byte(sstd_index); // first chunk is transaction id
|
2019-10-18 19:06:28 +00:00
|
|
|
sync_recv();
|
2021-06-17 23:10:06 +00:00
|
|
|
|
2019-10-18 19:06:28 +00:00
|
|
|
for (int i = 0; i < trans->initiator2target_buffer_size; ++i) {
|
2021-06-17 23:10:06 +00:00
|
|
|
serial_write_byte(split_trans_initiator2target_buffer(trans)[i]);
|
2019-10-18 19:06:28 +00:00
|
|
|
sync_recv();
|
2021-06-17 23:10:06 +00:00
|
|
|
checksum += split_trans_initiator2target_buffer(trans)[i];
|
2019-10-18 19:06:28 +00:00
|
|
|
}
|
|
|
|
serial_write_byte(checksum ^ 7);
|
|
|
|
sync_recv();
|
|
|
|
|
|
|
|
serial_delay();
|
2022-02-12 18:29:31 +00:00
|
|
|
serial_delay(); // read mid pulses
|
2019-10-18 19:06:28 +00:00
|
|
|
|
|
|
|
// receive data from the slave
|
|
|
|
uint8_t checksum_computed = 0;
|
|
|
|
for (int i = 0; i < trans->target2initiator_buffer_size; ++i) {
|
2021-06-17 23:10:06 +00:00
|
|
|
split_trans_target2initiator_buffer(trans)[i] = serial_read_byte();
|
2019-10-18 19:06:28 +00:00
|
|
|
sync_recv();
|
2021-06-17 23:10:06 +00:00
|
|
|
checksum_computed += split_trans_target2initiator_buffer(trans)[i];
|
2019-10-18 19:06:28 +00:00
|
|
|
}
|
|
|
|
checksum_computed ^= 7;
|
|
|
|
uint8_t checksum_received = serial_read_byte();
|
|
|
|
|
|
|
|
sync_recv();
|
|
|
|
serial_delay();
|
|
|
|
|
|
|
|
if ((checksum_computed) != (checksum_received)) {
|
2022-06-17 22:04:17 +00:00
|
|
|
serial_dprintf("serial::FAIL[%u,%u,%u]\n", checksum_computed, checksum_received, sstd_index);
|
2019-10-18 19:06:28 +00:00
|
|
|
serial_output();
|
|
|
|
serial_high();
|
|
|
|
|
|
|
|
chSysUnlock();
|
2022-01-27 05:13:27 +00:00
|
|
|
return false;
|
2019-10-18 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// always, release the line when not in use
|
|
|
|
serial_high();
|
|
|
|
serial_output();
|
|
|
|
|
|
|
|
chSysUnlock();
|
2022-01-27 05:13:27 +00:00
|
|
|
return true;
|
2019-10-18 19:06:28 +00:00
|
|
|
}
|
2022-04-19 10:56:16 +00:00
|
|
|
|
|
|
|
/////////
|
|
|
|
// start transaction by initiator
|
|
|
|
//
|
|
|
|
// bool soft_serial_transaction(int sstd_index)
|
|
|
|
//
|
|
|
|
// this code is very time dependent, so we need to disable interrupts
|
|
|
|
bool soft_serial_transaction(int sstd_index) {
|
2022-06-20 22:24:53 +00:00
|
|
|
return initiate_transaction((uint8_t)sstd_index);
|
2022-04-19 10:56:16 +00:00
|
|
|
}
|