Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add nrf52 uart module support lib
Browse files Browse the repository at this point in the history
jiaxin96 committed Jun 19, 2021
1 parent 67fe370 commit 0cfbe71
Showing 10 changed files with 2,525 additions and 0 deletions.
42 changes: 42 additions & 0 deletions keyboards/yandrstudio/biu_nrf52_ble_lib/biu_ble_common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

#include "biu_ble_common.h"

__attribute__((weak)) bool bluetooth_init(void) { return true; }

__attribute__((weak)) void bluetooth_task(void) {}

__attribute__((weak)) bool bluetooth_is_connected(void) { return true; }

__attribute__((weak)) void bluetooth_unpair_all(void) {}

__attribute__((weak)) void bluetooth_unpair_one(uint8_t device_id) {}

__attribute__((weak)) void bluetooth_pair(void) {}

__attribute__((weak)) void bluetooth_switch_one(uint8_t device_id) {}

__attribute__((weak)) void bluetooth_send_keyboard(report_keyboard_t *report) {}

#ifdef EXTRAKEY_ENABLE
__attribute__((weak)) void bluetooth_send_extra(uint8_t report_id, uint16_t keycode) {}
#endif


#ifdef MOUSE_ENABLE
__attribute__((weak)) void bluetooth_send_mouse(report_mouse_t *report) {}
#endif

#ifdef NKRO_ENABLE
__attribute__((weak)) void bluetooth_send_keyboard_nkro(report_keyboard_t *report) {}
#endif

#ifdef JOYSTICK_ENABLE
__attribute__((weak)) void bluetooth_send_joystick(joystick_report_t *report) {}
#endif

__attribute__((weak)) void bluetooth_send_battery_level(void) { }





40 changes: 40 additions & 0 deletions keyboards/yandrstudio/biu_nrf52_ble_lib/biu_ble_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include "report.h"
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

bool bluetooth_init(void);
void bluetooth_task(void);
bool bluetooth_is_connected(void);
void bluetooth_unpair_all(void);
void bluetooth_unpair_one(uint8_t device_id);
void bluetooth_pair(void);
void bluetooth_switch_one(uint8_t device_id);

void bluetooth_send_keyboard(report_keyboard_t *report);

#ifdef EXTRAKEY_ENABLE
void bluetooth_send_extra(uint8_t report_id, uint16_t keycode);
#endif

#ifdef MOUSE_ENABLE
void bluetooth_send_mouse(report_mouse_t *report);
#endif

#ifdef NKRO_ENABLE
void bluetooth_send_keyboard_nkro(report_keyboard_t *report);
#endif

#ifdef JOYSTICK_ENABLE
void bluetooth_send_joystick(joystick_report_t *report);
#endif

void bluetooth_send_battery_level(void);

#ifdef __cplusplus
}
#endif
613 changes: 613 additions & 0 deletions keyboards/yandrstudio/biu_nrf52_ble_lib/biu_nrf52.cpp

Large diffs are not rendered by default.

297 changes: 297 additions & 0 deletions keyboards/yandrstudio/biu_nrf52_ble_lib/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
/*
* (c) 2015 flabberast <s3+flabbergast@sdfeu.org>
*
* Based on the following work:
* - Guillaume Duc's raw hid example (MIT License)
* https://github.com/guiduc/usb-hid-chibios-example
* - PJRC Teensy examples (MIT License)
* https://www.pjrc.com/teensy/usb_keyboard.html
* - hasu's TMK keyboard code (GPL v2 and some code Modified BSD)
* https://github.com/tmk/tmk_keyboard/
* - ChibiOS demo code (Apache 2.0 License)
* http://www.chibios.org
*
* Since some GPL'd code is used, this work is licensed under
* GPL v2 or later.
*/

#include <ch.h>
#include <hal.h>

#include "usb_main.h"

/* TMK includes */
#include "report.h"
#include "host.h"
#include "host_driver.h"
#include "keyboard.h"
#include "action.h"
#include "action_util.h"
#include "mousekey.h"
#include "led.h"
#include "sendchar.h"
#include "debug.h"
#include "print.h"


#ifdef BIU_BLE5_ENABLE
#include "biu_ble_common.h"
#include "outputselect.h"
#endif


#ifndef EARLY_INIT_PERFORM_BOOTLOADER_JUMP
// Change this to be TRUE once we've migrated keyboards to the new init system
// Remember to change docs/platformdev_chibios_earlyinit.md as well.
# define EARLY_INIT_PERFORM_BOOTLOADER_JUMP FALSE
#endif

#ifdef SLEEP_LED_ENABLE
# include "sleep_led.h"
#endif
#ifdef SERIAL_LINK_ENABLE
# include "serial_link/system/serial_link.h"
#endif
#ifdef VISUALIZER_ENABLE
# include "visualizer/visualizer.h"
#endif
#ifdef MIDI_ENABLE
# include "qmk_midi.h"
#endif
#ifdef STM32_EEPROM_ENABLE
# include "eeprom_stm32.h"
#endif
#ifdef EEPROM_DRIVER
# include "eeprom_driver.h"
#endif
#include "suspend.h"
#include "wait.h"

/* -------------------------
* TMK host driver defs
* -------------------------
*/

// /* declarations */
// uint8_t keyboard_leds(void);
// void send_keyboard(report_keyboard_t *report);
// void send_mouse(report_mouse_t *report);
// void send_system(uint16_t data);
// void send_consumer(uint16_t data);

/* host struct */
host_driver_t chibios_driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer};

#ifdef VIRTSER_ENABLE
void virtser_task(void);
#endif

#ifdef RAW_ENABLE
void raw_hid_task(void);
#endif

#ifdef CONSOLE_ENABLE
void console_task(void);
#endif
#ifdef MIDI_ENABLE
void midi_ep_task(void);
#endif

#ifdef BIU_BLE5_ENABLE
void bluetooth_task(void);
#endif

/* TESTING
* Amber LED blinker thread, times are in milliseconds.
*/
/* set this variable to non-zero anywhere to blink once */
// static THD_WORKING_AREA(waThread1, 128);
// static THD_FUNCTION(Thread1, arg) {

// (void)arg;
// chRegSetThreadName("blinker");
// while (true) {
// systime_t time;

// time = USB_DRIVER.state == USB_ACTIVE ? 250 : 500;
// palClearLine(LINE_CAPS_LOCK);
// chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
// palSetLine(LINE_CAPS_LOCK);
// chSysPolledDelayX(MS2RTC(STM32_HCLK, time));
// }
// }

/* Early initialisation
*/
__attribute__((weak)) void early_hardware_init_pre(void) {
#if EARLY_INIT_PERFORM_BOOTLOADER_JUMP
void enter_bootloader_mode_if_requested(void);
enter_bootloader_mode_if_requested();
#endif // EARLY_INIT_PERFORM_BOOTLOADER_JUMP
}

__attribute__((weak)) void early_hardware_init_post(void) {}

__attribute__((weak)) void board_init(void) {}

// This overrides what's normally in ChibiOS board definitions
void __early_init(void) {
early_hardware_init_pre();

// This is the renamed equivalent of __early_init in the board.c file
void __chibios_override___early_init(void);
__chibios_override___early_init();

early_hardware_init_post();
}

// This overrides what's normally in ChibiOS board definitions
void boardInit(void) {
// This is the renamed equivalent of boardInit in the board.c file
void __chibios_override_boardInit(void);
__chibios_override_boardInit();

board_init();
}

/* Main thread
*/

int cut_usb_st = 0;

int main(void) __attribute__((weak));
int main(void) {
/* ChibiOS/RT init */
halInit();
chSysInit();

#ifdef STM32_EEPROM_ENABLE
EEPROM_Init();
#endif
#ifdef EEPROM_DRIVER
eeprom_driver_init();
#endif

// TESTING
// chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

keyboard_setup();


/* Init USB */
usb_event_queue_init();
init_usb_driver(&USB_DRIVER);

#ifdef MIDI_ENABLE
setup_midi();
#endif

#ifdef SERIAL_LINK_ENABLE
init_serial_link();
#endif

#ifdef VISUALIZER_ENABLE
visualizer_init();
#endif

host_driver_t *driver = NULL;

/* Wait until the USB or serial link is active */
while (true) {
#if defined(WAIT_FOR_USB) || defined(SERIAL_LINK_ENABLE)
if (USB_DRIVER.state == USB_ACTIVE) {
driver = &chibios_driver;
break;
}
#else
driver = &chibios_driver;
break;
#endif
#ifdef SERIAL_LINK_ENABLE
if (is_serial_link_connected()) {
driver = get_serial_link_driver();
break;
}
serial_link_update();
#endif
wait_ms(50);
}

/* Do need to wait here!
* Otherwise the next print might start a transfer on console EP
* before the USB is completely ready, which sometimes causes
* HardFaults.
*/
wait_ms(50);

/* init TMK modules */
keyboard_init();
host_set_driver(driver);


#ifdef SLEEP_LED_ENABLE
sleep_led_init();
#endif

print("Keyboard start.\n");

/* Main loop */
while (true) {
usb_event_queue_task();

#if !defined(NO_USB_STARTUP_CHECK)
if (USB_DRIVER.state == USB_SUSPENDED) {
print("[s]");
# ifdef VISUALIZER_ENABLE
visualizer_suspend();
# endif
while (USB_DRIVER.state == USB_SUSPENDED) {
/* Do this in the suspended state */
# ifdef SERIAL_LINK_ENABLE
serial_link_update();
# endif
suspend_power_down(); // on AVR this deep sleeps for 15ms
/* Remote wakeup */
if (suspend_wakeup_condition()) {
usbWakeupHost(&USB_DRIVER);
restart_usb_driver(&USB_DRIVER);
}
}
/* Woken up */
// variables has been already cleared by the wakeup hook
send_keyboard_report();
# ifdef MOUSEKEY_ENABLE
mousekey_send();
# endif /* MOUSEKEY_ENABLE */

# ifdef VISUALIZER_ENABLE
visualizer_resume();
# endif
}
#endif

keyboard_task();
#ifdef CONSOLE_ENABLE
console_task();
#endif
#ifdef MIDI_ENABLE
midi_ep_task();
#endif
#ifdef VIRTSER_ENABLE
virtser_task();
#endif
#ifdef RAW_ENABLE
raw_hid_task();
#endif
#ifdef BIU_BLE5_ENABLE
if (where_to_send() == OUTPUT_BLUETOOTH) {
bluetooth_task();
}
#endif


// Run housekeeping
housekeeping_task_kb();
housekeeping_task_user();
}
}
81 changes: 81 additions & 0 deletions keyboards/yandrstudio/biu_nrf52_ble_lib/outputselect.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Copyright 2017 Priyadi Iman Nurcahyo
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 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
*/

#include "outputselect.h"
#include "biu_ble_common.h"
#if defined(PROTOCOL_CHIBIOS)
# include "usb_main.h"
#endif

#include "uart.h"
#include "print.h"


bool biu_ble5_is_connected() {
return bluetooth_is_connected();
}



uint8_t desired_output = OUTPUT_DEFAULT;

/** \brief Set Output
*
* FIXME: Needs doc
*/
void set_output(uint8_t output) {
set_output_user(output);
desired_output = output;
}

/** \brief Set Output User
*
* FIXME: Needs doc
*/
__attribute__((weak)) void set_output_user(uint8_t output) {}

static bool is_usb_configured(void) {
#if defined(PROTOCOL_CHIBIOS)
return USB_DRIVER.state == USB_ACTIVE;
#endif
}

/** \brief Auto Detect Output
*
* FIXME: Needs doc
*/
uint8_t auto_detect_output(void) {
if (is_usb_configured()) {
return OUTPUT_USB;
}

#ifdef BIU_BLE5_ENABLE
if (biu_ble5_is_connected()) {
return OUTPUT_BLUETOOTH;
}
#endif

return OUTPUT_NONE;
}

/** \brief Where To Send
*
* FIXME: Needs doc
*/
uint8_t where_to_send(void) {
if (desired_output == OUTPUT_AUTO) {
return auto_detect_output();
}
return desired_output;
}
43 changes: 43 additions & 0 deletions keyboards/yandrstudio/biu_nrf52_ble_lib/outputselect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2017 Priyadi Iman Nurcahyo
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 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <stdint.h>
#include <stdbool.h>
enum outputs {
OUTPUT_AUTO,
OUTPUT_NONE,
OUTPUT_USB,
OUTPUT_BLUETOOTH
};

#ifndef OUTPUT_DEFAULT
# define OUTPUT_DEFAULT OUTPUT_BLUETOOTH
#endif


#ifdef __cplusplus
extern "C" {
#endif

void set_output(uint8_t output);
void set_output_user(uint8_t output);
uint8_t auto_detect_output(void);
uint8_t where_to_send(void);
bool biu_ble5_is_connected(void);

#ifdef __cplusplus
}
#endif
67 changes: 67 additions & 0 deletions keyboards/yandrstudio/biu_nrf52_ble_lib/ring_buffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once
#include <stdint.h>
// A simple ringbuffer holding Size elements of type T
template <typename T, uint8_t Size>
class RingBuffer {
protected:
T buf_[Size];
uint8_t head_{0}, tail_{0};
public:
inline uint8_t nextPosition(uint8_t position) {
return (position + 1) % Size;
}

inline uint8_t prevPosition(uint8_t position) {
if (position == 0) {
return Size - 1;
}
return position - 1;
}

inline bool enqueue(const T &item) {
static_assert(Size > 1, "RingBuffer size must be > 1");
uint8_t next = nextPosition(head_);
if (next == tail_) {
// Full
return false;
}

buf_[head_] = item;
head_ = next;
return true;
}

inline bool get(T &dest, bool commit = true) {
auto tail = tail_;
if (tail == head_) {
// No more data
return false;
}

dest = buf_[tail];
tail = nextPosition(tail);

if (commit) {
tail_ = tail;
}
return true;
}

inline bool empty() const { return head_ == tail_; }

inline uint8_t size() const {
int diff = head_ - tail_;
if (diff >= 0) {
return diff;
}
return Size + diff;
}

inline T& front() {
return buf_[tail_];
}

inline bool peek(T &item) {
return get(item, false);
}
};
84 changes: 84 additions & 0 deletions keyboards/yandrstudio/biu_nrf52_ble_lib/uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* Copyright 2021
*
* 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 3 of the License, 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. If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

#include <stdint.h>

#include <hal.h>

#ifndef SERIAL_DRIVER
# define SERIAL_DRIVER SD1
#endif

#ifndef SD1_TX_PIN
# define SD1_TX_PIN A9
#endif

#ifndef SD1_TX_PAL_MODE
# define SD1_TX_PAL_MODE 7
#endif

#ifndef SD1_RX_PIN
# define SD1_RX_PIN A10
#endif

#ifndef SD1_RX_PAL_MODE
# define SD1_RX_PAL_MODE 7
#endif

#ifndef SD1_CTS_PIN
# define SD1_CTS_PIN A11
#endif

#ifndef SD1_CTS_PAL_MODE
# define SD1_CTS_PAL_MODE 7
#endif

#ifndef SD1_RTS_PIN
# define SD1_RTS_PIN A12
#endif

#ifndef SD1_RTS_PAL_MODE
# define SD1_RTS_PAL_MODE 7
#endif

#ifndef SD1_CR1
# define SD1_CR1 0
#endif

#ifndef SD1_CR2
# define SD1_CR2 0
#endif

#ifndef SD1_CR3
# define SD1_CR3 0
#endif
#ifdef __cplusplus
extern "C" {
#endif

void uart_init(uint32_t baud);

void uart_putchar(uint8_t c);

uint8_t uart_getchar(void);

bool uart_available(void);

#ifdef __cplusplus
}
#endif
1,146 changes: 1,146 additions & 0 deletions keyboards/yandrstudio/biu_nrf52_ble_lib/usb_main.c

Large diffs are not rendered by default.

112 changes: 112 additions & 0 deletions keyboards/yandrstudio/biu_nrf52_ble_lib/usb_main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* (c) 2015 flabberast <s3+flabbergast@sdfeu.org>
*
* Based on the following work:
* - Guillaume Duc's raw hid example (MIT License)
* https://github.com/guiduc/usb-hid-chibios-example
* - PJRC Teensy examples (MIT License)
* https://www.pjrc.com/teensy/usb_keyboard.html
* - hasu's TMK keyboard code (GPL v2 and some code Modified BSD)
* https://github.com/tmk/tmk_keyboard/
* - ChibiOS demo code (Apache 2.0 License)
* http://www.chibios.org
*
* Since some GPL'd code is used, this work is licensed under
* GPL v2 or later.
*/

#pragma once

// TESTING
// extern uint8_t blinkLed;

#include <ch.h>
#include <hal.h>

#include "host_driver.h"


/* declarations */
uint8_t keyboard_leds(void);
void send_keyboard(report_keyboard_t *report);
void send_mouse(report_mouse_t *report);
void send_system(uint16_t data);
void send_consumer(uint16_t data);


/* -------------------------
* General USB driver header
* -------------------------
*/

/* The USB driver to use */
#define USB_DRIVER USBD1

/* Initialize the USB driver and bus */
void init_usb_driver(USBDriver *usbp);

/* Restart the USB driver and bus */
void restart_usb_driver(USBDriver *usbp);

/* ---------------
* USB Event queue
* ---------------
*/

/* Initialisation of the FIFO */
void usb_event_queue_init(void);

/* Task to dequeue and execute any handlers for the USB events on the main thread */
void usb_event_queue_task(void);

/* ---------------
* Keyboard header
* ---------------
*/

/* extern report_keyboard_t keyboard_report_sent; */

/* keyboard IN request callback handler */
void kbd_in_cb(USBDriver *usbp, usbep_t ep);

/* start-of-frame handler */
void kbd_sof_cb(USBDriver *usbp);

#ifdef NKRO_ENABLE
/* nkro IN callback hander */
void nkro_in_cb(USBDriver *usbp, usbep_t ep);
#endif /* NKRO_ENABLE */

/* ------------
* Mouse header
* ------------
*/

#ifdef MOUSE_ENABLE

/* mouse IN request callback handler */
void mouse_in_cb(USBDriver *usbp, usbep_t ep);
#endif /* MOUSE_ENABLE */

/* ---------------
* Shared EP header
* ---------------
*/

/* shared IN request callback handler */
void shared_in_cb(USBDriver *usbp, usbep_t ep);

/* --------------
* Console header
* --------------
*/

#ifdef CONSOLE_ENABLE

/* Putchar over the USB console */
int8_t sendchar(uint8_t c);

/* Flush output (send everything immediately) */
void console_flush_output(void);

#endif /* CONSOLE_ENABLE */

0 comments on commit 0cfbe71

Please sign in to comment.