mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-01-31 09:32:57 +00:00
platform: add ChibiOS
This commit is contained in:
parent
4b7165eeb7
commit
065fc9a3d9
175
platform/chibios/btstack_run_loop_chibios.c
Normal file
175
platform/chibios/btstack_run_loop_chibios.c
Normal file
@ -0,0 +1,175 @@
|
||||
/*
|
||||
* Copyright (C) 2022 BlueKitchen GmbH
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holders nor the names of
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* 4. Any redistribution, use, or modification is done solely for
|
||||
* personal benefit and not for any commercial purpose or for
|
||||
* monetary gain.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
|
||||
* GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* Please inquire about commercial licensing options at
|
||||
* contact@bluekitchen-gmbh.com
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* btstack_run_loop_chibios.c
|
||||
*
|
||||
* Run loop on dedicated thread on FreeRTOS
|
||||
* The run loop is triggered from other task/ISRs either via Event Groups
|
||||
* or Task Notifications if HAVE_FREERTOS_TASK_NOTIFICATIONS is defined
|
||||
*/
|
||||
|
||||
#define BTSTACK_FILE__ "btstack_run_loop_chibios.c"
|
||||
|
||||
#include <stddef.h> // NULL
|
||||
|
||||
#include "btstack_run_loop_chibios.h"
|
||||
|
||||
#include "btstack_linked_list.h"
|
||||
#include "btstack_debug.h"
|
||||
#include "btstack_util.h"
|
||||
#include "hal_time_ms.h"
|
||||
|
||||
#include "ch.h"
|
||||
|
||||
|
||||
// main event
|
||||
#define EVT_TRIGGER EVENT_MASK(0)
|
||||
|
||||
// checks, assert that these are enabled
|
||||
// CH_CFG_USE_EVENTS
|
||||
// CH_CFG_USE_EVENTS_TIMEOUT
|
||||
|
||||
static thread_t * btstack_thread;
|
||||
static mutex_t btstack_run_loop_callbacks_mutex;
|
||||
|
||||
// the run loop
|
||||
static bool run_loop_exit_requested;
|
||||
|
||||
static uint32_t btstack_run_loop_chibios_get_time_ms(void){
|
||||
return (uint32_t) chTimeI2MS(chVTGetSystemTime());
|
||||
}
|
||||
|
||||
// set timer
|
||||
static void btstack_run_loop_chibios_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){
|
||||
ts->timeout = btstack_run_loop_chibios_get_time_ms() + timeout_in_ms + 1;
|
||||
}
|
||||
|
||||
static void btstack_run_loop_chibios_trigger_from_thread(void){
|
||||
chEvtSignal(btstack_thread, EVT_TRIGGER);
|
||||
}
|
||||
|
||||
static void btstack_run_loop_chibios_poll_data_sources_from_irq(void){
|
||||
chSysLockFromISR();
|
||||
chEvtSignalI(btstack_thread, EVT_TRIGGER);
|
||||
chSysUnlockFromISR();
|
||||
}
|
||||
|
||||
static void btstack_run_loop_chibios_trigger_exit_internal(void){
|
||||
run_loop_exit_requested = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute run_loop
|
||||
*/
|
||||
static void btstack_run_loop_chibios_execute(void) {
|
||||
log_debug("RL: execute");
|
||||
|
||||
run_loop_exit_requested = false;
|
||||
|
||||
while (true) {
|
||||
|
||||
// process data sources
|
||||
btstack_run_loop_base_poll_data_sources();
|
||||
|
||||
// execute callbacks - protect list with mutex
|
||||
while (1){
|
||||
chMtxLock(&btstack_run_loop_callbacks_mutex);
|
||||
btstack_context_callback_registration_t * callback_registration = (btstack_context_callback_registration_t *) btstack_linked_list_pop(&btstack_run_loop_base_callbacks);
|
||||
chMtxUnlock(&btstack_run_loop_callbacks_mutex);
|
||||
if (callback_registration == NULL){
|
||||
break;
|
||||
}
|
||||
(*callback_registration->callback)(callback_registration->context);
|
||||
}
|
||||
|
||||
// process timers
|
||||
uint32_t now = btstack_run_loop_chibios_get_time_ms();
|
||||
btstack_run_loop_base_process_timers(now);
|
||||
|
||||
// exit triggered by btstack_run_loop_trigger_exit (main thread or other thread)
|
||||
if (run_loop_exit_requested) break;
|
||||
|
||||
// wait for timeout or event notification
|
||||
int32_t timeout_next_timer_ms = btstack_run_loop_base_get_time_until_timeout(now);
|
||||
|
||||
if (timeout_next_timer_ms > 0){
|
||||
chEvtWaitOneTimeout(EVT_TRIGGER, chTimeMS2I(timeout_next_timer_ms));
|
||||
} else {
|
||||
chEvtWaitOne(EVT_TRIGGER);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void btstack_run_loop_chibios_execute_on_main_thread(btstack_context_callback_registration_t * callback_registration){
|
||||
// protect list with mutex
|
||||
chMtxLock(&btstack_run_loop_callbacks_mutex);
|
||||
btstack_run_loop_base_add_callback(callback_registration);
|
||||
chMtxUnlock(&btstack_run_loop_callbacks_mutex);
|
||||
btstack_run_loop_chibios_trigger_from_thread();
|
||||
}
|
||||
|
||||
static void btstack_run_loop_chibios_init(void){
|
||||
btstack_run_loop_base_init();
|
||||
chMtxObjectInit(&btstack_run_loop_callbacks_mutex);
|
||||
btstack_thread = chThdGetSelfX();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init
|
||||
*/
|
||||
|
||||
static const btstack_run_loop_t btstack_run_loop_chibios = {
|
||||
&btstack_run_loop_chibios_init,
|
||||
&btstack_run_loop_base_add_data_source,
|
||||
&btstack_run_loop_base_remove_data_source,
|
||||
&btstack_run_loop_base_enable_data_source_callbacks,
|
||||
&btstack_run_loop_base_disable_data_source_callbacks,
|
||||
&btstack_run_loop_chibios_set_timer,
|
||||
&btstack_run_loop_base_add_timer,
|
||||
&btstack_run_loop_base_remove_timer,
|
||||
&btstack_run_loop_chibios_execute,
|
||||
&btstack_run_loop_base_dump_timer,
|
||||
&btstack_run_loop_chibios_get_time_ms,
|
||||
&btstack_run_loop_chibios_poll_data_sources_from_irq,
|
||||
&btstack_run_loop_chibios_execute_on_main_thread,
|
||||
&btstack_run_loop_chibios_trigger_exit_internal,
|
||||
};
|
||||
|
||||
const btstack_run_loop_t * btstack_run_loop_chibios_get_instance(void){
|
||||
return &btstack_run_loop_chibios;
|
||||
}
|
66
platform/chibios/btstack_run_loop_chibios.h
Normal file
66
platform/chibios/btstack_run_loop_chibios.h
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (C) 2022 BlueKitchen GmbH
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holders nor the names of
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* 4. Any redistribution, use, or modification is done solely for
|
||||
* personal benefit and not for any commercial purpose or for
|
||||
* monetary gain.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
|
||||
* GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* Please inquire about commercial licensing options at
|
||||
* contact@bluekitchen-gmbh.com
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* btstack_run_loop_chibios.h
|
||||
*
|
||||
* Functions relevant for BTstack ChibiOS port
|
||||
*/
|
||||
|
||||
#ifndef BTSTACK_RUN_LOOP_CHIBIOS_H
|
||||
#define BTSTACK_RUN_LOOP_CHIBIOS_H
|
||||
|
||||
#include "btstack_config.h"
|
||||
#include "btstack_run_loop.h"
|
||||
|
||||
#if defined __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Get ChibiOS implmeentation of btstack_run_loop_t for use with btstack_run_loop_init
|
||||
*/
|
||||
const btstack_run_loop_t * btstack_run_loop_chibios_get_instance(void);
|
||||
|
||||
|
||||
/* API_END */
|
||||
|
||||
#if defined __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // BTSTACK_RUN_LOOP_CHIBIOS_H
|
132
platform/chibios/hal_flash_bank_chibios.c
Normal file
132
platform/chibios/hal_flash_bank_chibios.c
Normal file
@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright (C) 2022 BlueKitchen GmbH
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holders nor the names of
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
|
||||
* GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* hal_flash_bank_chbios.c
|
||||
*
|
||||
* HAL abstraction for Flash memory using hal_flash and hal_efl.h on ELFD1
|
||||
*/
|
||||
|
||||
#define BTSTACK_FILE__ "hal_flash_bank_chibios.c"
|
||||
|
||||
#include "hal_flash_bank.h"
|
||||
#include "btstack_debug.h"
|
||||
#include "btstack_util.h"
|
||||
|
||||
#include "hal_flash_bank_chibios.h"
|
||||
#include "hal.h"
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <string.h> // memcpy
|
||||
|
||||
uint32_t hal_flash_bank_chibios_get_size(void * context){
|
||||
hal_flash_bank_chbios_t * self = (hal_flash_bank_chbios_t *) context;
|
||||
return self->sector_size;
|
||||
}
|
||||
|
||||
uint32_t hal_flash_bank_chibios_get_alignment(void * context){
|
||||
UNUSED(context);
|
||||
return 4;
|
||||
}
|
||||
|
||||
void hal_flash_bank_chibios_erase(void * context, int bank){
|
||||
hal_flash_bank_chbios_t * self = (hal_flash_bank_chbios_t *) context;
|
||||
if (bank > 1) return;
|
||||
flashStartEraseSector(&EFLD1, self->sectors[bank]);
|
||||
// poll until done
|
||||
bool done = false;
|
||||
while (!done){
|
||||
uint32_t retry_delay_ms;
|
||||
flash_error_t result = flashQueryErase(&EFLD1, &retry_delay_ms);
|
||||
switch (result){
|
||||
case FLASH_BUSY_ERASING:
|
||||
chThdSleepMilliseconds( retry_delay_ms );
|
||||
break;
|
||||
case FLASH_NO_ERROR:
|
||||
done = true;
|
||||
break;
|
||||
default:
|
||||
btstack_assert(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void hal_flash_bank_chibios_read(void * context, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){
|
||||
hal_flash_bank_chbios_t * self = (hal_flash_bank_chbios_t *) context;
|
||||
|
||||
if (bank > 1) return;
|
||||
if (offset > self->sector_size) return;
|
||||
if ((offset + size) > self->sector_size) return;
|
||||
|
||||
flashRead(&EFLD1, self->banks[bank] + offset, size, buffer);
|
||||
}
|
||||
|
||||
void hal_flash_bank_chibios_write(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size){
|
||||
hal_flash_bank_chbios_t * self = (hal_flash_bank_chbios_t *) context;
|
||||
|
||||
if (bank > 1) return;
|
||||
if (offset > self->sector_size) return;
|
||||
if ((offset + size) > self->sector_size) return;
|
||||
|
||||
flashProgram(&EFLD1, self->banks[bank] + offset, size, data);
|
||||
}
|
||||
|
||||
static const hal_flash_bank_t hal_flash_bank_chibios = {
|
||||
&hal_flash_bank_chibios_get_size,
|
||||
&hal_flash_bank_chibios_get_alignment,
|
||||
&hal_flash_bank_chibios_erase,
|
||||
&hal_flash_bank_chibios_read,
|
||||
&hal_flash_bank_chibios_write
|
||||
};
|
||||
|
||||
const hal_flash_bank_t * hal_flash_bank_chibios_init_instance(hal_flash_bank_chbios_t * context,
|
||||
uint16_t bank_0_sector, uint16_t bank_1_sector){
|
||||
|
||||
const flash_descriptor_t * descriptor = flashGetDescriptor(&EFLD1);
|
||||
btstack_assert(bank_0_sector < descriptor->sectors_count);
|
||||
btstack_assert(bank_1_sector < descriptor->sectors_count);
|
||||
|
||||
// get bank size and offsets from chibios flash descriptor
|
||||
uint32_t bank_0_offset = descriptor->sectors[bank_0_sector].offset;
|
||||
uint32_t bank_1_offset = descriptor->sectors[bank_1_sector].offset;
|
||||
uint32_t sector_size = btstack_min(descriptor->sectors[bank_0_sector].size, descriptor->sectors[bank_1_sector].size);
|
||||
|
||||
context->sector_size = sector_size;
|
||||
context->sectors[0] = bank_0_sector;
|
||||
context->sectors[1] = bank_1_sector;
|
||||
context->banks[0] = bank_0_offset;
|
||||
context->banks[1] = bank_1_offset;
|
||||
log_info("Bank size %" PRIu32 " bytes", sector_size);
|
||||
log_info("Bank 0 uses sector %u, offset %" PRIx32, bank_0_sector, bank_0_offset);
|
||||
log_info("Bank 1 uses sector %u, offset %" PRIx32, bank_1_sector, bank_1_offset);
|
||||
return &hal_flash_bank_chibios;
|
||||
}
|
69
platform/chibios/hal_flash_bank_chibios.h
Normal file
69
platform/chibios/hal_flash_bank_chibios.h
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (C) 2022 BlueKitchen GmbH
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holders nor the names of
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
|
||||
* GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* hal_flash_bank_chibios.h
|
||||
*
|
||||
* HAL abstraction for Flash memory that can be written anywhere
|
||||
* after being erased
|
||||
*/
|
||||
|
||||
#ifndef HAL_FLASH_BANK_CHIBIOS_H
|
||||
#define HAL_FLASH_BANK_CHIBIOS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "hal_flash_bank.h"
|
||||
|
||||
#if defined __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
uint32_t sector_size;
|
||||
uint16_t sectors[2];
|
||||
uintptr_t banks[2];
|
||||
} hal_flash_bank_chbios_t;
|
||||
|
||||
/**
|
||||
* Configure HAL Flash Implementation
|
||||
*
|
||||
* @param context of hal_flash_bank_chbios_t
|
||||
* @param bank_0_sector
|
||||
* @param bank_1_sector
|
||||
* @return
|
||||
*/
|
||||
const hal_flash_bank_t * hal_flash_bank_chibios_init_instance(hal_flash_bank_chbios_t * context,
|
||||
uint16_t bank_0_sector, uint16_t bank_1_sector);
|
||||
|
||||
#if defined __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
141
platform/chibios/hal_uart_dma_chibios.c
Normal file
141
platform/chibios/hal_uart_dma_chibios.c
Normal file
@ -0,0 +1,141 @@
|
||||
/*
|
||||
* Copyright (C) 2022 BlueKitchen GmbH
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holders nor the names of
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
|
||||
* GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* hal_uart_dma_chibios.c
|
||||
*
|
||||
* hal_uart_dma implementation on top of ChibiOS/HAL
|
||||
*/
|
||||
|
||||
#define BTSTACK_FILE__ "hal_uart_dma_chibios.c"
|
||||
|
||||
|
||||
#include "btstack_config.h"
|
||||
|
||||
#ifdef HAL_CONTROLLER_UART
|
||||
|
||||
#include "btstack_debug.h"
|
||||
#include "btstack_util.h"
|
||||
|
||||
#include "hal_uart_dma.h"
|
||||
#include "hal.h"
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <string.h> // memcpy
|
||||
|
||||
|
||||
static void dummy_handler(void);
|
||||
static void tx_complete(UARTDriver *uartp);
|
||||
static void rx_complete(UARTDriver *uartp);
|
||||
|
||||
// handlers
|
||||
static void (*rx_done_handler)(void) = &dummy_handler;
|
||||
static void (*tx_done_handler)(void) = &dummy_handler;
|
||||
|
||||
static UARTConfig uartConf = {
|
||||
NULL, /* UART transmission buffer callback. */
|
||||
&tx_complete, /* UART physical end of transmission callback. */
|
||||
&rx_complete, /* UART Receiver receiver filled callback. */
|
||||
NULL, /* UART caracter received callback. */
|
||||
NULL, /* UART received error callback. */
|
||||
NULL, /* Receiver timeout callback */
|
||||
115200, /* UART baudrate. */
|
||||
0, /* CR 1 */
|
||||
0, /* CR 2 */
|
||||
(1<<8) | (1<<9) , /* CR 3 - enable RTS/CTS */
|
||||
};
|
||||
|
||||
static void dummy_handler(void){};
|
||||
|
||||
// reset Bluetooth using n_shutdown
|
||||
static void bluetooth_power_cycle(void){
|
||||
#ifdef HAL_CONTROLLER_RESET_PIN
|
||||
log_info("Bluetooth power cycle");
|
||||
palClearPad( HAL_CONTROLLER_RESET_PORT, HAL_CONTROLLER_RESET_PIN);
|
||||
chThdSleepMilliseconds( 250 );
|
||||
palSetPad( HAL_CONTROLLER_RESET_PORT, HAL_CONTROLLER_RESET_PIN);
|
||||
chThdSleepMilliseconds( 500 );
|
||||
#else
|
||||
log_info("Bluetooth power cycle skipped, HAL_CONTROLLER_RESET_PIN not defined");
|
||||
#endif
|
||||
}
|
||||
|
||||
static void rx_complete(UARTDriver *uartp){
|
||||
if (uartp == &HAL_CONTROLLER_UART){
|
||||
(*rx_done_handler)();
|
||||
}
|
||||
}
|
||||
|
||||
static void tx_complete(UARTDriver *uartp){
|
||||
UNUSED(uartp);
|
||||
if (uartp == &HAL_CONTROLLER_UART){
|
||||
(*tx_done_handler)();
|
||||
}
|
||||
}
|
||||
|
||||
void hal_uart_dma_init(void){
|
||||
bluetooth_power_cycle();
|
||||
uartStart(&HAL_CONTROLLER_UART, &uartConf);
|
||||
}
|
||||
|
||||
void hal_uart_dma_set_block_received( void (*the_block_handler)(void)){
|
||||
rx_done_handler = the_block_handler;
|
||||
}
|
||||
|
||||
void hal_uart_dma_set_block_sent( void (*the_block_handler)(void)){
|
||||
tx_done_handler = the_block_handler;
|
||||
}
|
||||
|
||||
void hal_uart_dma_set_sleep(uint8_t sleep){
|
||||
UNUSED(sleep);
|
||||
}
|
||||
|
||||
void hal_uart_dma_set_csr_irq_handler( void (*the_irq_handler)(void)){
|
||||
UNUSED(the_irq_handler);
|
||||
}
|
||||
|
||||
int hal_uart_dma_set_baud(uint32_t baud){
|
||||
// driver supports 'hot restart'
|
||||
uartConf.speed = baud;
|
||||
uartStart(&HAL_CONTROLLER_UART, &uartConf);
|
||||
log_info("UART baud %" PRIu32, baud);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void hal_uart_dma_send_block(const uint8_t *data, uint16_t size){
|
||||
uartStartSend(&HAL_CONTROLLER_UART, size, data);
|
||||
}
|
||||
|
||||
void hal_uart_dma_receive_block(uint8_t *data, uint16_t size){
|
||||
uartStartReceive(&HAL_CONTROLLER_UART, size, data);
|
||||
}
|
||||
|
||||
#endif
|
119
platform/chibios/retarget_chibios.c
Normal file
119
platform/chibios/retarget_chibios.c
Normal file
@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright (C) 2022 BlueKitchen GmbH
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holders nor the names of
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
|
||||
* GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* retarget_chibios.c
|
||||
*
|
||||
* retarget printf and friends for ChibiOS/HAL
|
||||
*/
|
||||
|
||||
#define BTSTACK_FILE__ "hal_uart_dma_chibios.c"
|
||||
|
||||
#include "btstack_config.h"
|
||||
#include "btstack_debug.h"
|
||||
#include "btstack_util.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
// retarget printf and friends
|
||||
|
||||
#ifndef ENABLE_SEGGER_RTT
|
||||
|
||||
#include "hal.h"
|
||||
|
||||
/**
|
||||
* Use USART_CONSOLE as a console.
|
||||
* This is a syscall for newlib
|
||||
* @param file
|
||||
* @param ptr
|
||||
* @param len
|
||||
* @return
|
||||
*/
|
||||
|
||||
int _write(int file, char *ptr, int len);
|
||||
int _write(int file, char *ptr, int len){
|
||||
UNUSED(file);
|
||||
#ifdef HAL_DEBUG_SERIAL
|
||||
sdWrite(&HAL_DEBUG_SERIAL, (const uint8_t *) ptr, len);
|
||||
#endif
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
int _read(int file, char * ptr, int len){
|
||||
UNUSED(file);
|
||||
UNUSED(ptr);
|
||||
UNUSED(len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _lseek(int file){
|
||||
UNUSED(file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _close(int file){
|
||||
UNUSED(file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _isatty(int file){
|
||||
UNUSED(file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _fstat(int file){
|
||||
UNUSED(file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void _exit(int err){
|
||||
UNUSED(err);
|
||||
while(1);
|
||||
}
|
||||
|
||||
int _kill(int pid){
|
||||
UNUSED(pid);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int _getpid(void){
|
||||
return -1;
|
||||
}
|
||||
|
||||
void * _sbrk(intptr_t increment){
|
||||
UNUSED(increment);
|
||||
// btstack_assert(false);
|
||||
return (void*) -1;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user