quack/main/adb.c

183 lines
5.1 KiB
C
Raw Normal View History

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
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING); if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
*/
#include <stdio.h>
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#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"
#include "esp_spi_flash.h"
2021-03-24 09:26:20 +00:00
#include "driver/rmt.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-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-03-22 23:42:26 +00:00
extern TaskHandle_t t_green, t_blue, t_yellow, t_red;
/* static defines */
2021-03-22 23:42:26 +00:00
static uint16_t adb_rx_mouse();
static bool adb_rx_tlt(void);
static void adb_tx_as(void);
static void adb_tx_one(void);
static void adb_tx_zero(void);
/* functions */
2021-01-08 08:21:38 +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-04-01 18:58:23 +00:00
/* avoir console flood when installing/uninstalling RMT driver */
esp_log_level_set("intr_alloc", ESP_LOG_INFO);
/* init RMT RX driver with default values for ADB */
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 */
if (gpio_get_level(GPIO_ADBSRC) == 0)
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-04-20 22:33:42 +00:00
xTaskCreatePinnedToCore(adb_task_mouse, "ADB_MOUSE", 6 * 1024, NULL, tskADB_PRIORITY, NULL, 1);
2021-01-08 08:21:38 +00:00
}
void adb_task_host(void *pvParameters) {
2021-03-22 23:42:26 +00:00
uint16_t data;
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)
xTaskNotify(t_green, LED_ON, eSetValueWithOverwrite);
2021-04-01 18:58:23 +00:00
ESP_LOGI("ADB", "ADB host started");
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-04-01 18:58:23 +00:00
ESP_ERROR_CHECK(rmt_driver_install(RMT_RX_CHANNEL, 200, 0));
2021-04-20 22:33:42 +00:00
while (true) {
2021-03-22 23:42:26 +00:00
vTaskDelay(20 / portTICK_PERIOD_MS);
adb_tx_cmd(ADB_MOUSE|ADB_TALK|ADB_REG0);
2021-04-01 18:58:23 +00:00
data = adb_rx_mouse();
2021-01-08 08:21:38 +00:00
}
}
void adb_task_mouse(void *pvParameters) {
2021-04-20 22:33:42 +00:00
ESP_LOGI("ADB", "ADB mouse started");
vTaskSuspend(NULL);
2021-01-08 08:21:38 +00:00
}
2021-04-01 18:58:23 +00:00
int dur( uint32_t level, uint32_t duration ) {
if ( level == 0 ) { return duration; }
else { return -1.0 * duration; }
2021-03-22 23:42:26 +00:00
}
2021-04-01 18:58:23 +00:00
static uint16_t adb_rx_mouse() {
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);
assert(rb != NULL);
rmt_rx_start(RMT_RX_CHANNEL, true);
items = (rmt_item32_t*) xRingbufferReceive(rb, &rx_size, pdMS_TO_TICKS(10));
printf( ">>> %i\n", rx_size);
rmt_rx_stop(RMT_RX_CHANNEL);
if (rx_size > (1 * sizeof(rmt_item32_t)))
xTaskNotify(t_yellow, LED_ONCE, eSetValueWithOverwrite);
else
return 0;
for (i = 0; i < (rx_size / sizeof(rmt_item32_t)); i++) {
printf("%d:%dus %d:%dus\n", (items+i)->level0, (items+i)->duration0, (items+i)->level1, (items+i)->duration1);
2021-03-22 23:42:26 +00:00
}
2021-04-01 18:58:23 +00:00
vRingbufferReturnItem(rb, (void*) items);
2021-03-22 23:42:26 +00:00
2021-04-01 18:58:23 +00:00
return 0;
2021-03-22 23:42:26 +00:00
}
static inline void adb_tx_as() {
/* send attention (800 µs low) + sync (70 µs high) */
gpio_set_level(GPIO_ADB, 0);
2021-03-22 23:42:26 +00:00
ets_delay_us(800-1);
gpio_set_level(GPIO_ADB, 1);
2021-03-22 23:42:26 +00:00
ets_delay_us(80-1);
}
void adb_tx_cmd(unsigned char cmd) {
2021-03-22 23:42:26 +00:00
gpio_set_direction(GPIO_ADB, GPIO_MODE_OUTPUT);
2021-04-01 18:58:23 +00:00
2021-03-22 23:42:26 +00:00
adb_tx_as();
/* send actual byte (unrolled loop) */
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 */
adb_tx_zero();
gpio_set_direction(GPIO_ADB, GPIO_MODE_INPUT);
2021-04-01 18:58:23 +00:00
gpio_set_pull_mode(GPIO_ADB, GPIO_PULLUP_ONLY);
}
static inline void adb_tx_one() {
/* values from AN591 Datasheet minus the estimated call to ets_delay_us */
gpio_set_level(GPIO_ADB, 0);
2021-04-01 18:58:23 +00:00
ets_delay_us(ADB_1_LOW - 1);
gpio_set_level(GPIO_ADB, 1);
2021-04-01 18:58:23 +00:00
ets_delay_us(ADB_1_HIGH - 1);
}
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-01-08 08:21:38 +00:00
gpio_set_level(GPIO_ADB, 0);
2021-04-01 18:58:23 +00:00
ets_delay_us(ADB_RESET);
2021-01-08 08:21:38 +00:00
gpio_set_level(GPIO_ADB, 1);
2021-03-22 23:42:26 +00:00
ets_delay_us(500);
2021-01-08 08:21:38 +00:00
}
static inline void adb_tx_zero() {
/* values from AN591 Datasheet minus the estimated call to ets_delay_us */
gpio_set_level(GPIO_ADB, 0);
2021-04-01 18:58:23 +00:00
ets_delay_us(ADB_0_LOW - 1);
gpio_set_level(GPIO_ADB, 1);
2021-04-01 18:58:23 +00:00
ets_delay_us(ADB_0_HIGH - 1);
}