diff options
Diffstat (limited to 'users/drashna/drashna.c')
-rw-r--r-- | users/drashna/drashna.c | 95 |
1 files changed, 51 insertions, 44 deletions
diff --git a/users/drashna/drashna.c b/users/drashna/drashna.c index 6e8d4ac9bd..91f3a06b7c 100644 --- a/users/drashna/drashna.c +++ b/users/drashna/drashna.c @@ -2,9 +2,6 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include "drashna.h" -#ifdef __AVR__ -# include <avr/wdt.h> -#endif userspace_config_t userspace_config; @@ -76,49 +73,59 @@ void tap_code16_nomods(uint16_t kc) { set_mods(temp_mod); } -/** - * @brief Run shutdown routine and soft reboot firmware. - * - */ - -#ifdef HAPTIC_ENABLE -# include "haptic.h" -#endif +#ifdef I2C_SCANNER_ENABLE +# include "i2c_master.h" +# include "debug.h" -#ifdef AUDIO_ENABLE -# ifndef GOODBYE_SONG -# define GOODBYE_SONG SONG(GOODBYE_SOUND) +# ifndef I2C_SCANNER_TIMEOUT +# define I2C_SCANNER_TIMEOUT 50 # endif -float reset_song[][2] = GOODBYE_SONG; -#endif -void software_reset(void) { - clear_keyboard(); -#if defined(MIDI_ENABLE) && defined(MIDI_BASIC) - process_midi_all_notes_off(); -#endif -#ifdef AUDIO_ENABLE -# ifndef NO_MUSIC_MODE - music_all_notes_off(); -# endif - uint16_t timer_start = timer_read(); - PLAY_SONG(reset_song); - shutdown_user(); - while (timer_elapsed(timer_start) < 250) wait_ms(1); - stop_all_notes(); -#else - shutdown_user(); - wait_ms(250); -#endif -#ifdef HAPTIC_ENABLE - haptic_shutdown(); -#endif +i2c_status_t i2c_start_bodge(uint8_t address, uint16_t timeout) { + i2c_start(address); -#if defined(PROTOCOL_LUFA) - wdt_enable(WDTO_250MS); -#elif defined(PROTOCOL_CHIBIOS) -# if defined(MCU_STM32) || defined(MCU_KINETIS) - NVIC_SystemReset(); -# endif -#endif + // except on ChibiOS where the only way is do do "something" + uint8_t data = 0; + return i2c_readReg(address, 0, &data, sizeof(data), I2C_SCANNER_TIMEOUT); } + +# define i2c_start i2c_start_bodge + +void do_scan(void) { + uint8_t nDevices = 0; + + dprintf("Scanning...\n"); + + for (uint8_t address = 1; address < 127; address++) { + // The i2c_scanner uses the return value of + // i2c_start to see if a device did acknowledge to the address. + i2c_status_t error = i2c_start(address << 1, I2C_SCANNER_TIMEOUT); + if (error == I2C_STATUS_SUCCESS) { + i2c_stop(); + xprintf(" I2C device found at address 0x%02X\n", I2C_SCANNER_TIMEOUT); + nDevices++; + } else { + // dprintf(" Unknown error (%u) at address 0x%02X\n", error, address); + } + } + + if (nDevices == 0) + xprintf("No I2C devices found\n"); + else + xprintf("done\n"); +} + +uint16_t scan_timer = 0; + +void matrix_scan_i2c(void) { + if (timer_elapsed(scan_timer) > 5000) { + do_scan(); + scan_timer = timer_read(); + } +} + +void keyboard_post_init_i2c(void) { + i2c_init(); + scan_timer = timer_read(); +} +#endif |