2021-01-08 08:21:38 +00:00
|
|
|
/*
|
|
|
|
* adb.c
|
|
|
|
* quack
|
|
|
|
*
|
|
|
|
* Created by Michel DEPEIGE on 7/01/2020.
|
|
|
|
* Copyright (c) 2020 Michel DEPEIGE.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2021-08-18 21:13:40 +00:00
|
|
|
* it under the terms of the Apache License, Version 2.0 (the "License");
|
|
|
|
* You may obtain a copy of the License at:
|
2021-01-08 08:21:38 +00:00
|
|
|
*
|
2021-08-18 21:13:40 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2021-01-08 08:21:38 +00:00
|
|
|
*
|
2021-08-18 21:13:40 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2021-01-08 08:21:38 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "driver/gpio.h"
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
2021-08-21 08:52:54 +00:00
|
|
|
#include "freertos/queue.h"
|
2023-12-28 20:40:18 +00:00
|
|
|
#include "freertos/semphr.h"
|
2021-01-08 08:21:38 +00:00
|
|
|
#include "freertos/task.h"
|
2021-04-01 18:58:23 +00:00
|
|
|
#include "esp_log.h"
|
2021-01-08 08:21:38 +00:00
|
|
|
#include "esp_system.h"
|
2023-12-28 20:40:18 +00:00
|
|
|
#include "esp_chip_info.h"
|
|
|
|
#include "esp_private/periph_ctrl.h"
|
2021-03-24 09:26:20 +00:00
|
|
|
#include "driver/rmt.h"
|
2021-08-09 20:48:08 +00:00
|
|
|
#include "soc/periph_defs.h"
|
|
|
|
#include "soc/rmt_reg.h"
|
2021-01-08 08:21:38 +00:00
|
|
|
|
|
|
|
#include "adb.h"
|
2021-03-22 23:42:26 +00:00
|
|
|
#include "led.h"
|
2021-01-08 08:21:38 +00:00
|
|
|
#include "gpio.h"
|
|
|
|
|
2021-05-09 18:45:22 +00:00
|
|
|
/* defines */
|
|
|
|
#define TAG "ADB"
|
|
|
|
|
2021-03-24 09:26:20 +00:00
|
|
|
/* globals */
|
2021-04-01 18:58:23 +00:00
|
|
|
rmt_config_t adb_rmt_rx = RMT_DEFAULT_CONFIG_RX(GPIO_ADB, RMT_RX_CHANNEL);
|
2021-08-18 21:13:40 +00:00
|
|
|
extern TaskHandle_t t_adb2hid;
|
2021-03-22 23:42:26 +00:00
|
|
|
extern TaskHandle_t t_green, t_blue, t_yellow, t_red;
|
2021-08-21 08:52:54 +00:00
|
|
|
extern TaskHandle_t t_click;
|
|
|
|
extern QueueHandle_t q_qx, q_qy;
|
2021-03-22 23:42:26 +00:00
|
|
|
|
2021-03-21 21:01:43 +00:00
|
|
|
/* static defines */
|
2021-05-03 22:45:23 +00:00
|
|
|
static void adb_handle_button(bool action);
|
2021-08-09 20:48:08 +00:00
|
|
|
static void adb_rmt_reset(void);
|
2021-05-08 09:30:07 +00:00
|
|
|
static bool adb_rx_isone(rmt_item32_t cell);
|
|
|
|
static bool adb_rx_isstop(rmt_item32_t cell);
|
|
|
|
static bool adb_rx_iszero(rmt_item32_t cell);
|
2021-05-08 19:05:58 +00:00
|
|
|
static uint16_t IRAM_ATTR adb_rx_mouse();
|
2021-05-08 09:30:07 +00:00
|
|
|
static void adb_rx_setup(void);
|
|
|
|
static void adb_tx_as(void);
|
2021-03-21 21:01:43 +00:00
|
|
|
static void adb_tx_one(void);
|
2021-05-03 22:45:23 +00:00
|
|
|
static void adb_tx_setup(void);
|
2024-06-05 22:10:01 +00:00
|
|
|
static void adb_tx_stop(void);
|
2021-03-21 21:01:43 +00:00
|
|
|
static void adb_tx_zero(void);
|
|
|
|
|
|
|
|
/* functions */
|
2021-05-03 22:45:23 +00:00
|
|
|
static void adb_handle_button(bool action) {
|
|
|
|
static bool status = ADB_B_UP; // Keep state. Click is active low
|
|
|
|
|
|
|
|
if (status == ADB_B_UP && action == ADB_B_UP)
|
|
|
|
return ;
|
|
|
|
if (status == ADB_B_DOWN && action == ADB_B_DOWN)
|
|
|
|
return ;
|
|
|
|
|
|
|
|
/* release button */
|
|
|
|
if (status == ADB_B_DOWN && action == ADB_B_UP) {
|
|
|
|
xTaskNotify(t_click, 0, eSetValueWithOverwrite);
|
|
|
|
status = ADB_B_UP;
|
2021-05-09 18:45:22 +00:00
|
|
|
ESP_LOGD(TAG, "button released");
|
2021-05-03 22:45:23 +00:00
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* press button */
|
|
|
|
if (status == ADB_B_UP && action == ADB_B_DOWN) {
|
|
|
|
xTaskNotify(t_click, 1, eSetValueWithOverwrite);
|
|
|
|
status = ADB_B_DOWN;
|
2021-05-09 18:45:22 +00:00
|
|
|
ESP_LOGD(TAG, "button pressed");
|
2021-05-03 22:45:23 +00:00
|
|
|
return ;
|
|
|
|
}
|
|
|
|
}
|
2021-01-08 08:21:38 +00:00
|
|
|
|
2021-03-21 21:01:43 +00:00
|
|
|
void adb_init(void) {
|
2021-03-22 23:42:26 +00:00
|
|
|
/* initialise */
|
|
|
|
gpio_set_level(GPIO_ADB, 1);
|
|
|
|
adb_tx_reset();
|
|
|
|
|
2021-08-09 20:48:08 +00:00
|
|
|
/* avoid console flood when installing/uninstalling RMT driver */
|
2021-04-01 18:58:23 +00:00
|
|
|
esp_log_level_set("intr_alloc", ESP_LOG_INFO);
|
|
|
|
|
|
|
|
/* init RMT RX driver with default values for ADB */
|
2021-08-05 16:41:04 +00:00
|
|
|
adb_rmt_rx.mem_block_num = 4;
|
2021-04-01 18:58:23 +00:00
|
|
|
adb_rmt_rx.rx_config.filter_en = true;
|
|
|
|
adb_rmt_rx.rx_config.filter_ticks_thresh = 10;
|
|
|
|
adb_rmt_rx.rx_config.idle_threshold = 100;
|
|
|
|
rmt_config(&adb_rmt_rx);
|
2021-03-24 09:26:20 +00:00
|
|
|
|
2021-01-08 08:21:38 +00:00
|
|
|
/* If jumper is set, switch to ADB host mode */
|
2021-08-17 19:50:04 +00:00
|
|
|
if (adb_is_host())
|
2021-04-20 22:33:42 +00:00
|
|
|
xTaskCreatePinnedToCore(adb_task_host, "ADB_HOST", 6 * 1024, NULL, tskADB_PRIORITY, NULL, 1);
|
2021-01-08 08:21:38 +00:00
|
|
|
else
|
2021-08-22 14:23:25 +00:00
|
|
|
xTaskCreatePinnedToCore(adb_task_idle, "ADB_IDLE", 6 * 1024, NULL, tskADB_PRIORITY, NULL, 1);
|
2021-01-08 08:21:38 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 19:50:04 +00:00
|
|
|
inline bool adb_is_host(void) {
|
|
|
|
if (gpio_get_level(GPIO_ADBSRC) == 0)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:45:22 +00:00
|
|
|
/*
|
|
|
|
* Probe ADB mouse. Also switch the mouse to a better mode if avaible
|
|
|
|
*
|
|
|
|
* 1. check if there is someting at address $3, if not: retry
|
|
|
|
* 2. try to switch to CLASSIC2 protocol at temporary address $9
|
|
|
|
* 3. if CLASSIC2 is successfull, move device back to $3
|
|
|
|
*/
|
|
|
|
|
|
|
|
void adb_probe(void) {
|
|
|
|
uint16_t register3;
|
|
|
|
|
2024-06-04 08:04:47 +00:00
|
|
|
ESP_LOGI(TAG, "Probing for mouse…");
|
2021-05-09 18:45:22 +00:00
|
|
|
xTaskNotify(t_yellow, LED_SLOW, eSetValueWithOverwrite);
|
|
|
|
|
|
|
|
/* for some reason, RMT misses the first exchange sometimes. Flush the device should give it time */
|
|
|
|
adb_tx_cmd(ADB_MOUSE|ADB_FLUSH);
|
|
|
|
adb_rx_mouse();
|
|
|
|
vTaskDelay(12 / portTICK_PERIOD_MS);
|
|
|
|
|
|
|
|
while (true) {
|
2024-06-05 22:10:01 +00:00
|
|
|
|
2021-05-09 18:45:22 +00:00
|
|
|
adb_tx_cmd(ADB_MOUSE|ADB_TALK|ADB_REG3);
|
|
|
|
register3 = adb_rx_mouse();
|
2024-06-04 08:04:47 +00:00
|
|
|
ESP_LOGD("ADB", "Device $3 register3: %04x", register3);
|
2021-05-09 18:45:22 +00:00
|
|
|
|
2021-10-30 13:06:51 +00:00
|
|
|
if (register3 && (register3 & ADB_H_ALL) == ADB_H_ERR)
|
|
|
|
ESP_LOGE(TAG, "Mouse failed self init test");
|
|
|
|
|
2024-06-04 08:04:47 +00:00
|
|
|
/*
|
|
|
|
* try to unglue composite devices (Kensington)
|
|
|
|
* the idea is to move detected devices to $9, and check $3 again
|
|
|
|
* if there is something again at $3, it may be a composite device, or the user
|
|
|
|
* plugged two devices.
|
|
|
|
* if there is nothing anymore, move back $9 to $3
|
|
|
|
*
|
|
|
|
* we will handle that further down the line by checking the handler
|
|
|
|
*/
|
|
|
|
if (register3 & ADB_H_ALL) {
|
|
|
|
ESP_LOGI(TAG, "\tMoving $3 to $9 to check for composite devices");
|
|
|
|
adb_tx_listen(ADB_MOUSE|ADB_LISTEN|ADB_REG3, (ADB_TMP<<4)|ADB_H_MOVE);
|
|
|
|
vTaskDelay(7 / portTICK_PERIOD_MS);
|
|
|
|
ESP_LOGI(TAG, "\tChecking $3 again…");
|
|
|
|
adb_tx_cmd(ADB_MOUSE|ADB_TALK|ADB_REG3);
|
|
|
|
register3 = adb_rx_mouse();
|
|
|
|
if (register3) {
|
|
|
|
adb_tx_cmd(ADB_TMP|ADB_TALK|ADB_REG3);
|
|
|
|
register3 = adb_rx_mouse();
|
|
|
|
ESP_LOGI(TAG, "\t… something was there, $9 handler: $%02x", register3 & ADB_H_ALL);
|
|
|
|
xTaskNotify(t_red, LED_ONCE, eSetValueWithOverwrite);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ESP_LOGI(TAG, "\t… nothing, moving $9 back to $3");
|
|
|
|
adb_tx_listen(ADB_TMP|ADB_LISTEN|ADB_REG3, (ADB_MOUSE<<4)|ADB_H_MOVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore register3 from $3 for handler detection */
|
|
|
|
vTaskDelay(7 / portTICK_PERIOD_MS);
|
|
|
|
adb_tx_cmd(ADB_MOUSE|ADB_TALK|ADB_REG3);
|
|
|
|
register3 = adb_rx_mouse();
|
|
|
|
}
|
|
|
|
|
2021-10-30 13:06:51 +00:00
|
|
|
/* Accept all known handlers */
|
|
|
|
if (((register3 & ADB_H_ALL) == ADB_H_C100) || ((register3 & ADB_H_ALL) == ADB_H_C200) ||
|
2024-06-04 08:04:47 +00:00
|
|
|
((register3 & ADB_H_ALL) == ADB_H_MTRC) || ((register3 & ADB_H_ALL) == ADB_H_KSGT)) {
|
|
|
|
ESP_LOGI(TAG, "… detected mouse at $3");
|
|
|
|
|
2021-05-09 18:45:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
vTaskDelay(500 / portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try to switch to 200cpi mode. (0x6000) == Exceptional event + Service request enable */
|
|
|
|
vTaskDelay(7 / portTICK_PERIOD_MS);
|
|
|
|
if ((register3 & ADB_H_ALL) == ADB_H_C100) {
|
|
|
|
adb_tx_listen(ADB_MOUSE|ADB_LISTEN|ADB_REG3, 0x6000|(ADB_MOUSE<<4)|ADB_H_C200);
|
|
|
|
vTaskDelay(7 / portTICK_PERIOD_MS);
|
|
|
|
adb_tx_cmd(ADB_MOUSE|ADB_TALK|ADB_REG3);
|
|
|
|
register3 = adb_rx_mouse();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (register3 & ADB_H_ALL) {
|
|
|
|
case ADB_H_C100:
|
|
|
|
ESP_LOGD(TAG, "Mouse running at 100cpi");
|
|
|
|
break;
|
|
|
|
case ADB_H_C200:
|
|
|
|
ESP_LOGD(TAG, "Mouse running at 200cpi");
|
|
|
|
break;
|
2021-10-30 13:06:51 +00:00
|
|
|
case ADB_H_MTRC:
|
|
|
|
ESP_LOGD(TAG, "MacTRAC running at default cpi");
|
|
|
|
break;
|
2024-06-04 08:04:47 +00:00
|
|
|
case ADB_H_KSGT:
|
|
|
|
ESP_LOGD(TAG, "Kensington detected, switching to standard device");
|
|
|
|
adb_tx_listen(ADB_MOUSE|ADB_LISTEN|ADB_REG3, (ADB_KML1<<4)|ADB_H_MOVE);
|
|
|
|
adb_tx_listen(ADB_TMP|ADB_LISTEN|ADB_REG3, (ADB_MOUSE<<4)|ADB_H_MOVE);
|
|
|
|
adb_tx_listen(ADB_MOUSE|ADB_LISTEN|ADB_REG3, 0x6000|(ADB_MOUSE<<4)|ADB_H_C200);
|
|
|
|
ESP_LOGD(TAG, "Kensington running at 200cpi");
|
|
|
|
break;
|
2021-05-09 18:45:22 +00:00
|
|
|
default:
|
|
|
|
ESP_LOGE(TAG, "Mouse running with unknow handler: %x", register3 & ADB_H_ALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
xTaskNotify(t_yellow, LED_OFF, eSetValueWithOverwrite);
|
|
|
|
}
|
|
|
|
|
2021-08-09 20:48:08 +00:00
|
|
|
/*
|
|
|
|
* sometimes the RMT device get stuck with a full RX buffer. On the Quack Mouse adapter,
|
|
|
|
* it prevends reading from the ADB if the receive buffer has been filled
|
|
|
|
*
|
|
|
|
* Somewhat a hack, but resetting the module sometimes help.
|
|
|
|
*/
|
|
|
|
|
2021-08-09 21:46:57 +00:00
|
|
|
static void adb_rmt_reset() {
|
2021-08-09 20:48:08 +00:00
|
|
|
periph_module_reset(PERIPH_RMT_MODULE);
|
|
|
|
rmt_config(&adb_rmt_rx);
|
|
|
|
xTaskNotify(t_red, LED_ONCE, eSetValueWithOverwrite);
|
|
|
|
|
|
|
|
gpio_set_level(GPIO_ADB, 1);
|
|
|
|
adb_tx_reset();
|
|
|
|
}
|
|
|
|
|
2021-01-08 08:21:38 +00:00
|
|
|
void adb_task_host(void *pvParameters) {
|
2021-05-03 22:45:23 +00:00
|
|
|
/* Classic Apple Mouse Protocol is 16 bits long */
|
2021-03-22 23:42:26 +00:00
|
|
|
uint16_t data;
|
2024-06-05 22:10:01 +00:00
|
|
|
uint8_t last = 0;
|
|
|
|
int8_t move = 0;
|
|
|
|
uint8_t state = ADB_S_PROBE;
|
2021-03-21 21:01:43 +00:00
|
|
|
|
2021-04-20 22:33:42 +00:00
|
|
|
/* put green led to steady if BT is disabled. Otherwise BT init will do it */
|
2021-03-22 23:42:26 +00:00
|
|
|
if (gpio_get_level(GPIO_BTOFF) == 0)
|
2021-05-09 18:45:22 +00:00
|
|
|
xTaskNotify(t_green, LED_ON, eSetValueWithOverwrite);
|
|
|
|
ESP_LOGI(TAG, "host started on core %d", xPortGetCoreID());
|
2021-01-08 08:21:38 +00:00
|
|
|
|
2021-03-22 23:42:26 +00:00
|
|
|
/* poll the mouse like a maniac. It will answer only if there is user input */
|
2021-08-09 20:48:08 +00:00
|
|
|
ESP_ERROR_CHECK(rmt_driver_install(RMT_RX_CHANNEL, 256, 0));
|
2021-04-01 18:58:23 +00:00
|
|
|
|
2021-04-20 22:33:42 +00:00
|
|
|
while (true) {
|
2021-05-08 19:05:58 +00:00
|
|
|
/* Should give us a polling rate between 80-90 Hz */
|
|
|
|
vTaskDelay(7 / portTICK_PERIOD_MS);
|
2021-05-09 18:45:22 +00:00
|
|
|
|
|
|
|
if (state == ADB_S_PROBE) {
|
|
|
|
adb_probe();
|
|
|
|
state = ADM_S_POLL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-22 23:42:26 +00:00
|
|
|
adb_tx_cmd(ADB_MOUSE|ADB_TALK|ADB_REG0);
|
2021-04-01 18:58:23 +00:00
|
|
|
data = adb_rx_mouse();
|
2021-05-03 22:45:23 +00:00
|
|
|
|
|
|
|
if (data) {
|
2021-05-09 18:45:22 +00:00
|
|
|
last = 0;
|
|
|
|
|
2021-08-18 21:13:40 +00:00
|
|
|
/* split data for quad and (maybe) bluetooth */
|
|
|
|
if (gpio_get_level(GPIO_BTOFF) == 1 && t_adb2hid != NULL)
|
|
|
|
xTaskNotify(t_adb2hid, data, eSetValueWithOverwrite);
|
|
|
|
|
2021-05-03 22:45:23 +00:00
|
|
|
/* click is active low */
|
|
|
|
if (! (data & ADB_CMP_B1) || (! (data & ADB_CMP_B2)))
|
|
|
|
adb_handle_button(ADB_B_DOWN);
|
|
|
|
else
|
|
|
|
adb_handle_button(ADB_B_UP);
|
|
|
|
|
2021-05-08 09:30:07 +00:00
|
|
|
/* cast negative signed 7 bits to signed 8 bits */
|
2021-05-09 18:45:22 +00:00
|
|
|
move = (data & ADB_CMP_MX) >> 0;
|
2021-05-03 22:45:23 +00:00
|
|
|
if (move & 0x40) {
|
|
|
|
move &= ~0x40;
|
|
|
|
move |= 0x80;
|
|
|
|
move += 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (move)
|
2021-08-21 08:52:54 +00:00
|
|
|
xQueueSendToBack(q_qx, &move, 0);
|
2021-05-03 22:45:23 +00:00
|
|
|
|
2021-05-09 18:45:22 +00:00
|
|
|
move = (data & ADB_CMP_MY) >> 8;
|
2021-05-03 22:45:23 +00:00
|
|
|
if (move & 0x40) {
|
|
|
|
move &= ~0x40;
|
|
|
|
move |= 0x80;
|
|
|
|
move += 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (move)
|
2021-08-21 08:52:54 +00:00
|
|
|
xQueueSendToBack(q_qy, &move, 0);
|
2021-05-03 22:45:23 +00:00
|
|
|
}
|
2021-05-09 18:45:22 +00:00
|
|
|
else {
|
|
|
|
last++;
|
|
|
|
if (last == 0xff) {
|
|
|
|
adb_tx_cmd(ADB_MOUSE|ADB_TALK|ADB_REG3);
|
|
|
|
data = adb_rx_mouse();
|
|
|
|
if (!data) {
|
|
|
|
if (state == ADB_S_KEEP)
|
|
|
|
state = ADB_S_PROBE;
|
|
|
|
else
|
|
|
|
state = ADB_S_KEEP;
|
|
|
|
}
|
2024-06-04 08:04:47 +00:00
|
|
|
ESP_LOGD("ADB", "Check mouse presence %04x", data);
|
2021-05-09 18:45:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-08 08:21:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 18:42:44 +00:00
|
|
|
/*
|
|
|
|
* this do nothing since device mode doesn't work
|
2021-08-22 14:23:25 +00:00
|
|
|
* put level converter on input mode with a pull up to avoid oscillations
|
|
|
|
* if plugged into an ADB Bus by mistake.
|
2021-08-11 18:42:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void adb_task_idle(void *pvParameters) {
|
|
|
|
ESP_LOGI(TAG, "idle started on core %d", xPortGetCoreID());
|
|
|
|
|
|
|
|
/* RMT is not installed via rmt_driver_install() so no need to uninstall */
|
2021-08-22 14:23:25 +00:00
|
|
|
adb_rx_setup();
|
|
|
|
ESP_ERROR_CHECK(gpio_pullup_en(GPIO_ADB));
|
2021-08-11 18:42:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We sould exit here but for some reason core 1 panic with
|
|
|
|
* IllegalInstruction if we do... Dunno what's going on here either.
|
|
|
|
* let's suspend the task instead
|
|
|
|
*/
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "idle suspending itself on core %d", xPortGetCoreID());
|
2021-04-20 22:33:42 +00:00
|
|
|
vTaskSuspend(NULL);
|
2021-01-08 08:21:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 22:45:23 +00:00
|
|
|
/*
|
|
|
|
* some ADB mouses are using cells as short as 24µs+49µs, try to get that...
|
|
|
|
* spec allow ±30% variance
|
|
|
|
*
|
|
|
|
* units are in µs
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool adb_rx_isone(rmt_item32_t cell) {
|
|
|
|
if (cell.level0 == 0 && (cell.duration0 >= 22 && cell.duration0 <= 44) &&
|
|
|
|
cell.level1 == 1 && (cell.duration1 >= 46 && cell.duration1 <= 86))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool adb_rx_isstop(rmt_item32_t cell) {
|
|
|
|
/* high part of the well is lengh 0 because of RMT timeout (100µs+) */
|
|
|
|
if (cell.level0 == 0 && (cell.duration0 >= 46 && cell.duration0 <= 86) &&
|
|
|
|
cell.level1 == 1 && cell.duration1 == 0)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool adb_rx_iszero(rmt_item32_t cell) {
|
|
|
|
if (cell.level0 == 0 && (cell.duration0 >= 46 && cell.duration0 <= 86) &&
|
|
|
|
cell.level1 == 1 && (cell.duration1 >= 22 && cell.duration1 <= 44))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-08 19:05:58 +00:00
|
|
|
static uint16_t IRAM_ATTR adb_rx_mouse() {
|
2021-08-09 20:48:08 +00:00
|
|
|
uint32_t rmt_status;
|
2021-05-03 22:45:23 +00:00
|
|
|
uint16_t data = 0;
|
2021-04-01 18:58:23 +00:00
|
|
|
RingbufHandle_t rb = NULL;
|
|
|
|
rmt_item32_t* items = NULL;
|
|
|
|
size_t rx_size = 0;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
rmt_get_ringbuf_handle(RMT_RX_CHANNEL, &rb);
|
2021-05-09 18:45:22 +00:00
|
|
|
configASSERT(rb != NULL);
|
2021-04-01 18:58:23 +00:00
|
|
|
rmt_rx_start(RMT_RX_CHANNEL, true);
|
2021-05-08 19:05:58 +00:00
|
|
|
items = (rmt_item32_t*)xRingbufferReceive(rb, &rx_size, pdMS_TO_TICKS(8));
|
2021-08-09 21:46:57 +00:00
|
|
|
rmt_get_status(RMT_RX_CHANNEL, &rmt_status);
|
2021-08-09 20:48:08 +00:00
|
|
|
if (((rmt_status >> RMT_STATE_CH0_S) & RMT_STATE_CH0_V) == 4)
|
|
|
|
adb_rmt_reset();
|
2021-04-01 18:58:23 +00:00
|
|
|
rmt_rx_stop(RMT_RX_CHANNEL);
|
|
|
|
|
2021-05-03 22:45:23 +00:00
|
|
|
if (items == NULL)
|
2021-04-01 18:58:23 +00:00
|
|
|
return 0;
|
2021-05-03 22:45:23 +00:00
|
|
|
|
|
|
|
/*
|
2021-05-08 09:30:07 +00:00
|
|
|
* Mouse response size in bits is events / sizeof(rmt_item32_t) (4)
|
2021-05-03 22:45:23 +00:00
|
|
|
* start bit + 16 data bits + stop bit = 72 bytes in RMT buffer (18 bits received)
|
2021-05-08 09:30:07 +00:00
|
|
|
*
|
|
|
|
* Check start / stop bits and size.
|
|
|
|
* On real Macintoshes, ADB Manager does something similar
|
2021-05-03 22:45:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
switch (rx_size) {
|
|
|
|
case 0:
|
2021-08-09 20:48:08 +00:00
|
|
|
/* RMT giving back a buffer with a rx_size of 0… */
|
2021-05-09 18:45:22 +00:00
|
|
|
case 4:
|
2021-08-05 16:41:04 +00:00
|
|
|
/* single glitch or service request (keyboard), timeout/ignore */
|
|
|
|
vRingbufferReturnItem(rb, (void*) items);
|
2021-05-09 18:45:22 +00:00
|
|
|
return 0;
|
2021-05-03 22:45:23 +00:00
|
|
|
case 72:
|
|
|
|
xTaskNotify(t_yellow, LED_ONCE, eSetValueWithOverwrite);
|
|
|
|
break;
|
|
|
|
default:
|
2021-05-09 18:45:22 +00:00
|
|
|
ESP_LOGD(TAG, "wrong size of %i bit(s)", rx_size / sizeof(rmt_item32_t));
|
2021-05-03 22:45:23 +00:00
|
|
|
xTaskNotify(t_red, LED_ONCE, eSetValueWithOverwrite);
|
2021-03-22 23:42:26 +00:00
|
|
|
}
|
2021-05-03 22:45:23 +00:00
|
|
|
|
|
|
|
/* check start and stop bits */
|
|
|
|
if (! adb_rx_isone(*(items+0)) && (! adb_rx_isstop(*(items+(rx_size / sizeof(rmt_item32_t) - 1))))) {
|
|
|
|
xTaskNotify(t_red, LED_ONCE, eSetValueWithOverwrite);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rebuild our data with RMT buffer */
|
|
|
|
for (i = 1; i < ((rx_size / sizeof(rmt_item32_t)) - 1); i++) {
|
|
|
|
data <<= 1;
|
|
|
|
|
|
|
|
/* check that every data is either one or zero */
|
|
|
|
if ((! adb_rx_isone(*(items+i))) && (! adb_rx_iszero(*(items+i)))) {
|
|
|
|
xTaskNotify(t_red, LED_ONCE, eSetValueWithOverwrite);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (adb_rx_isone(*(items+i)))
|
|
|
|
data |= 1;
|
|
|
|
}
|
2021-04-01 18:58:23 +00:00
|
|
|
vRingbufferReturnItem(rb, (void*) items);
|
2021-03-22 23:42:26 +00:00
|
|
|
|
2021-05-03 22:45:23 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void adb_rx_setup() {
|
|
|
|
gpio_set_level(GPIO_DIR, 0);
|
|
|
|
gpio_set_direction(GPIO_ADB, GPIO_MODE_INPUT);
|
|
|
|
gpio_pullup_dis(GPIO_ADB);
|
|
|
|
gpio_pulldown_dis(GPIO_ADB);
|
2021-03-22 23:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void adb_tx_as() {
|
2024-06-05 22:10:01 +00:00
|
|
|
/*
|
|
|
|
* send attention (800 µs low) + sync (65 µs high).
|
|
|
|
* AN591 mentions 70 µs which is a mistake, make some devices angry
|
|
|
|
*/
|
|
|
|
|
2021-03-21 21:01:43 +00:00
|
|
|
gpio_set_level(GPIO_ADB, 0);
|
2023-12-28 20:40:18 +00:00
|
|
|
esp_rom_delay_us(800-1);
|
2021-03-21 21:01:43 +00:00
|
|
|
gpio_set_level(GPIO_ADB, 1);
|
2024-06-05 22:10:01 +00:00
|
|
|
esp_rom_delay_us(65-1);
|
2021-03-21 21:01:43 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 18:45:22 +00:00
|
|
|
void IRAM_ATTR adb_tx_cmd(unsigned char cmd) {
|
2021-05-03 22:45:23 +00:00
|
|
|
adb_tx_setup();
|
2021-03-22 23:42:26 +00:00
|
|
|
adb_tx_as();
|
2021-03-21 21:01:43 +00:00
|
|
|
|
2021-05-09 18:45:22 +00:00
|
|
|
/* send command byte (unrolled loop) */
|
2021-03-21 21:01:43 +00:00
|
|
|
cmd & 0x80 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
cmd & 0x40 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
cmd & 0x20 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
cmd & 0x10 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
cmd & 0x08 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
cmd & 0x04 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
cmd & 0x02 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
cmd & 0x01 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
|
|
|
|
/* stop bit */
|
2024-06-05 22:10:01 +00:00
|
|
|
adb_tx_stop();
|
2021-05-03 22:45:23 +00:00
|
|
|
adb_rx_setup();
|
2021-03-21 21:01:43 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 18:45:22 +00:00
|
|
|
void IRAM_ATTR adb_tx_data(uint16_t data) {
|
|
|
|
adb_tx_setup();
|
|
|
|
adb_tx_one();
|
|
|
|
|
|
|
|
/* send data 2 bytes (unrolled loop) */
|
|
|
|
data & 0x8000 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
data & 0x4000 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
data & 0x2000 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
data & 0x1000 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
data & 0x800 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
data & 0x400 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
data & 0x200 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
data & 0x100 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
data & 0x80 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
data & 0x40 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
data & 0x20 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
data & 0x10 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
data & 0x08 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
data & 0x04 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
data & 0x02 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
data & 0x01 ? adb_tx_one() : adb_tx_zero();
|
|
|
|
|
|
|
|
/* stop bit */
|
2024-06-05 22:10:01 +00:00
|
|
|
adb_tx_stop();
|
2021-05-09 18:45:22 +00:00
|
|
|
adb_rx_setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void adb_tx_listen(unsigned char cmd, uint16_t data) {
|
|
|
|
adb_tx_cmd(cmd);
|
|
|
|
|
2024-06-05 22:10:01 +00:00
|
|
|
/*
|
|
|
|
* Stop to start is between 160-240µS. Go for around 160 + time for GPIO setup
|
|
|
|
* values from AN591 Datasheet minus the estimated call to esp_rom_delay_us
|
|
|
|
*/
|
|
|
|
|
2023-12-28 20:40:18 +00:00
|
|
|
esp_rom_delay_us(160);
|
2021-05-09 18:45:22 +00:00
|
|
|
adb_tx_data(data);
|
|
|
|
}
|
|
|
|
|
2021-03-21 21:01:43 +00:00
|
|
|
static inline void adb_tx_one() {
|
|
|
|
gpio_set_level(GPIO_ADB, 0);
|
2023-12-28 20:40:18 +00:00
|
|
|
esp_rom_delay_us(ADB_1_LOW - 1);
|
2021-03-21 21:01:43 +00:00
|
|
|
gpio_set_level(GPIO_ADB, 1);
|
2023-12-28 20:40:18 +00:00
|
|
|
esp_rom_delay_us(ADB_1_HIGH - 1);
|
2021-03-21 21:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void adb_tx_reset() {
|
|
|
|
/*
|
|
|
|
* ADB spec says reset signal low for 3ms ±30%
|
|
|
|
* Note that the ADB Desktop Bus Mouse G5431 uses 4ms when plugged
|
2021-03-22 23:42:26 +00:00
|
|
|
* ADB mouse init in <70ms, but lets wait 500ms to be sure
|
2021-03-21 21:01:43 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-08 08:21:38 +00:00
|
|
|
gpio_set_level(GPIO_ADB, 0);
|
2023-12-28 20:40:18 +00:00
|
|
|
esp_rom_delay_us(ADB_RESET);
|
2021-01-08 08:21:38 +00:00
|
|
|
gpio_set_level(GPIO_ADB, 1);
|
2023-12-28 20:40:18 +00:00
|
|
|
esp_rom_delay_us(500);
|
2021-01-08 08:21:38 +00:00
|
|
|
}
|
2021-03-21 21:01:43 +00:00
|
|
|
|
2021-05-03 22:45:23 +00:00
|
|
|
static inline void adb_tx_setup() {
|
|
|
|
gpio_set_direction(GPIO_ADB, GPIO_MODE_OUTPUT);
|
|
|
|
gpio_set_level(GPIO_DIR, 1);
|
|
|
|
}
|
|
|
|
|
2024-06-05 22:10:01 +00:00
|
|
|
static inline void adb_tx_stop() {
|
|
|
|
gpio_set_level(GPIO_ADB, 0);
|
|
|
|
esp_rom_delay_us(ADB_S_LOW - 1);
|
|
|
|
gpio_set_level(GPIO_ADB, 1);
|
|
|
|
esp_rom_delay_us(ADB_S_HIGH - 1);
|
|
|
|
}
|
|
|
|
|
2021-03-21 21:01:43 +00:00
|
|
|
static inline void adb_tx_zero() {
|
|
|
|
gpio_set_level(GPIO_ADB, 0);
|
2023-12-28 20:40:18 +00:00
|
|
|
esp_rom_delay_us(ADB_0_LOW - 1);
|
2021-03-21 21:01:43 +00:00
|
|
|
gpio_set_level(GPIO_ADB, 1);
|
2023-12-28 20:40:18 +00:00
|
|
|
esp_rom_delay_us(ADB_0_HIGH - 1);
|
2021-03-21 21:01:43 +00:00
|
|
|
}
|