2727#include <freertos/ringbuf.h>
2828#include <freertos/semphr.h>
2929#include <freertos/task.h>
30- #include " esp_log.h"
31- #include " esp_system.h"
32- #include " esp_chip_info.h"
33- #include " esp_private/periph_ctrl.h"
34- #include " soc/periph_defs.h"
35- #include "soc/rmt_reg.h"
30+ #include < esp_log.h>
31+ #include < esp_system.h>
32+ #include < esp_chip_info.h>
33+ #include < esp_private/periph_ctrl.h>
34+ #include < soc/rmt_reg.h>
35+ #include <hal/rmt_types.h>
3636
3737#include "adb.h"
3838#include "led.h"
4242/*
4343 * THEORY OF OPERATION *
4444 *
45- * Quack scheduler is running at 250 Hz which give 4 ms running "slots"
45+ * Quack scheduler is running at 250 Hz which give 4 ms time "slots"
4646 *
4747 * TRANSMITTING *
4848 * It's done by toggling the GPIO in an active loop, which is less than ideal because
4949 * the RTOS may be scheduling stuff.
5050 *
5151 * For polling it's ok because the ADB command (TALK) can be send under 4ms so the task
5252 * shouldn't be rescheduled. For the initial setup it can be a little longer (up to 12 ms)
53- * so there is risks of being rescheduled here. On can avoid most of it by putting the
54- * transmitting task into high priority and only running others tasks as low priority.
53+ * so there is risks of being rescheduled here. One can avoid most of it by putting the
54+ * transmitting task into as priority and only running others tasks as low priority.
5555 *
5656 * RECEIVING *
5757 * since we can be scheduled anytime during reception, reception is done using the RMT
5858 * (Remote Control Transceiver) transceiver of the ESP32.
5959 *
60- * Before ESP-IDF v5.0, it was donne using the RMT driver but this has been deprecated
61- * a subset of it for this specific project has been reimplemented within the phy.* files
60+ * Before ESP-IDF v5.0, it was donne using the RMT driver but this has been deprecated.
61+ * A subset of it for this specific project has been reimplemented within the phy.* files
6262 * using the RMT HAL.
6363 *
64+ * The driver grab the ADB pulses for about 8ms, then pass then into a ringbuf,
65+ * which are then decoded in adb_rx_mouse(). the 8ms is a compromise to be able to poll
66+ * at a decent rate because the peripheral can't know when the transmission happens.
67+ * this limit us to about 20 bits
68+ *
6469 * Overall, in order to not push our luck this realistically limit the mouse protocol to
6570 * the simpler ones (classic 100 and 200 cpi)
6671 */
6772
6873/* defines */
69- #define TAG "ADB "
74+ #define TAG "ADB_MGR "
7075
7176/* globals */
7277extern TaskHandle_t t_adb2hid ;
@@ -77,9 +82,9 @@ extern QueueHandle_t q_qx, q_qy;
7782/* static defines */
7883static void adb_handle_button (bool action );
7984static void adb_phy_reset (void );
80- static bool adb_rx_isone (phy_item32_t cell );
81- static bool adb_rx_isstop (phy_item32_t cell );
82- static bool adb_rx_iszero (phy_item32_t cell );
85+ static bool adb_rx_isone (rmt_symbol_word_t cell );
86+ static bool adb_rx_isstop (rmt_symbol_word_t cell );
87+ static bool adb_rx_iszero (rmt_symbol_word_t cell );
8388static uint16_t adb_rx_mouse (void );
8489static void adb_rx_setup (void );
8590static void adb_scan_macaj (void );
@@ -163,10 +168,12 @@ void adb_probe(void) {
163168
164169 adb_tx_cmd (ADB_MOUSE |ADB_TALK |ADB_REG3 );
165170 register3 = adb_rx_mouse ();
166- ESP_LOGD ("ADB" , "Device $3 register3: %04x" , register3 );
171+ ESP_LOGD (TAG , "$3 register3: %04x" , register3 );
167172
168- if (register3 && (register3 & ADB_H_ALL ) == ADB_H_ERR )
173+ if (register3 && (register3 & ADB_H_ALL ) == ADB_H_ERR ) {
169174 ESP_LOGE (TAG , "Mouse failed self init test" );
175+ xTaskNotify (t_red , LED_ONCE , eSetValueWithOverwrite );
176+ }
170177
171178 /*
172179 * try to unglue composite devices (Kensington)
@@ -381,7 +388,7 @@ void adb_task_host(void *pvParameters) {
381388 else
382389 state = ADB_S_KEEP ;
383390 }
384- ESP_LOGD ("ADB" , "Check mouse presence %04x" , data );
391+ ESP_LOGD (TAG , "Checking $3: %04x" , data );
385392 }
386393 }
387394 }
@@ -417,22 +424,22 @@ void adb_task_idle(void *pvParameters) {
417424 * units are in µs
418425 */
419426
420- static bool adb_rx_isone (phy_item32_t cell ) {
427+ static bool adb_rx_isone (rmt_symbol_word_t cell ) {
421428 if (cell .level0 == 0 && (cell .duration0 >= 22 && cell .duration0 <= 44 ) &&
422429 cell .level1 == 1 && (cell .duration1 >= 46 && cell .duration1 <= 86 ))
423430 return true;
424431 return false;
425432}
426433
427- static bool adb_rx_isstop (phy_item32_t cell ) {
428- /* high part of the well is lengh 0 because of RMT timeout (100µs+) */
434+ static bool adb_rx_isstop (rmt_symbol_word_t cell ) {
435+ /* high part of the end cell is lengh 0 because of RMT timeout (100µs+) */
429436 if (cell .level0 == 0 && (cell .duration0 >= 46 && cell .duration0 <= 86 ) &&
430437 cell .level1 == 1 && cell .duration1 == 0 )
431438 return true;
432439 return false;
433440}
434441
435- static bool adb_rx_iszero (phy_item32_t cell ) {
442+ static bool adb_rx_iszero (rmt_symbol_word_t cell ) {
436443 if (cell .level0 == 0 && (cell .duration0 >= 46 && cell .duration0 <= 86 ) &&
437444 cell .level1 == 1 && (cell .duration1 >= 22 && cell .duration1 <= 44 ))
438445 return true;
@@ -443,14 +450,14 @@ static uint16_t IRAM_ATTR adb_rx_mouse() {
443450 uint32_t phy_status ;
444451 uint16_t data = 0 ;
445452 RingbufHandle_t rb = NULL ;
446- phy_item32_t * items = NULL ;
453+ rmt_symbol_word_t * items = NULL ;
447454 size_t rx_size = 0 ;
448455 size_t i ;
449456
450457 phy_get_ringbuf_handle (& rb );
451458 configASSERT (rb != NULL );
452459 phy_rx_start (true);
453- items = (phy_item32_t * )xRingbufferReceive (rb , & rx_size , pdMS_TO_TICKS (8 ));
460+ items = (rmt_symbol_word_t * )xRingbufferReceive (rb , & rx_size , pdMS_TO_TICKS (8 ));
454461
455462 phy_get_status (& phy_status );
456463 if (((phy_status >> RMT_STATE_CH0_S ) & RMT_STATE_CH0_V ) == 4 )
@@ -461,7 +468,7 @@ static uint16_t IRAM_ATTR adb_rx_mouse() {
461468 return 0 ;
462469
463470 /*
464- * Mouse response size in bits is events / sizeof(phy_item32_t ) (4)
471+ * Mouse response size in bits is events / sizeof(rmt_symbol_word_t ) (4)
465472 * start bit + 16 data bits + stop bit = 72 bytes in RMT buffer (18 bits received)
466473 *
467474 * Check start / stop bits and size.
@@ -479,18 +486,18 @@ static uint16_t IRAM_ATTR adb_rx_mouse() {
479486 xTaskNotify (t_yellow , LED_ONCE , eSetValueWithOverwrite );
480487 break ;
481488 default :
482- ESP_LOGD (TAG , "wrong size of %i bit(s)" , rx_size / sizeof (phy_item32_t ));
489+ ESP_LOGD (TAG , "wrong size of %i bit(s)" , rx_size / sizeof (rmt_symbol_word_t ));
483490 xTaskNotify (t_red , LED_ONCE , eSetValueWithOverwrite );
484491 }
485492
486493 /* check start and stop bits */
487- if (! adb_rx_isone (* (items + 0 )) && (! adb_rx_isstop (* (items + (rx_size / sizeof (phy_item32_t ) - 1 ))))) {
494+ if (! adb_rx_isone (* (items + 0 )) && (! adb_rx_isstop (* (items + (rx_size / sizeof (rmt_symbol_word_t ) - 1 ))))) {
488495 xTaskNotify (t_red , LED_ONCE , eSetValueWithOverwrite );
489496 return 0 ;
490497 }
491498
492499 /* rebuild our data with RMT buffer */
493- for (i = 1 ; i < ((rx_size / sizeof (phy_item32_t )) - 1 ); i ++ ) {
500+ for (i = 1 ; i < ((rx_size / sizeof (rmt_symbol_word_t )) - 1 ); i ++ ) {
494501 data <<= 1 ;
495502
496503 /* check that every data is either one or zero */
0 commit comments