From 03d2ba88e1c743dfefedb64f54c75f214baec8cf Mon Sep 17 00:00:00 2001 From: Andrey Perminov Date: Thu, 7 May 2020 16:04:31 -0700 Subject: [PATCH] Moved FPC BM-Lite SDK to a separate folder Moved FPC BM-Lite SDK to a reparate folder Checking result of execution of every command on BM-Lite Change-Id: Ib0e441fac2a0f276d4adead0e588ac656276d20e --- BMLite_example/Makefile | 7 +- BMLite_example/inc/raspberry_pi_hal.h | 32 ----- BMLite_example/src/main.c | 70 ++++------- BMLite_sdk/bmlite.mk | 13 ++ BMLite_sdk/inc/bmlite_hal.h | 116 ++++++++++++++++++ .../inc/bmlite_if.h | 0 .../inc/bmlite_if_callbacks.h | 0 .../inc/fpc_bep_types.h | 3 +- {BMLite_example => BMLite_sdk}/inc/fpc_crc.h | 0 .../inc/fpc_hcp_common.h | 0 {BMLite_example => BMLite_sdk}/inc/hcp_tiny.h | 0 BMLite_sdk/inc/platform.h | 78 ++++++++++++ .../src/bmlite_if.c | 10 +- {BMLite_example => BMLite_sdk}/src/fpc_crc.c | 0 {BMLite_example => BMLite_sdk}/src/hcp_tiny.c | 4 +- BMLite_sdk/src/platform.c | 78 ++++++++++++ .../inc/platform_rpi.h | 33 +++-- HAL_Driver/raspberry.mk | 15 ++- .../src/platform_linux.c | 50 +++++++- .../src/rpi_com.c | 8 +- .../src/rpi_spi.c | 62 ++++------ 21 files changed, 441 insertions(+), 138 deletions(-) delete mode 100644 BMLite_example/inc/raspberry_pi_hal.h create mode 100644 BMLite_sdk/bmlite.mk create mode 100755 BMLite_sdk/inc/bmlite_hal.h rename {BMLite_example => BMLite_sdk}/inc/bmlite_if.h (100%) rename {BMLite_example => BMLite_sdk}/inc/bmlite_if_callbacks.h (100%) rename {BMLite_example => BMLite_sdk}/inc/fpc_bep_types.h (98%) rename {BMLite_example => BMLite_sdk}/inc/fpc_crc.h (100%) rename {BMLite_example => BMLite_sdk}/inc/fpc_hcp_common.h (100%) rename {BMLite_example => BMLite_sdk}/inc/hcp_tiny.h (100%) create mode 100644 BMLite_sdk/inc/platform.h rename {BMLite_example => BMLite_sdk}/src/bmlite_if.c (95%) rename {BMLite_example => BMLite_sdk}/src/fpc_crc.c (100%) rename {BMLite_example => BMLite_sdk}/src/hcp_tiny.c (99%) create mode 100644 BMLite_sdk/src/platform.c rename BMLite_example/inc/platform.h => HAL_Driver/inc/platform_rpi.h (80%) rename {BMLite_example => HAL_Driver}/src/platform_linux.c (51%) rename BMLite_example/src/platform_com.c => HAL_Driver/src/rpi_com.c (92%) rename BMLite_example/src/platform_spi.c => HAL_Driver/src/rpi_spi.c (79%) diff --git a/BMLite_example/Makefile b/BMLite_example/Makefile index 0771bf3..aa241fe 100644 --- a/BMLite_example/Makefile +++ b/BMLite_example/Makefile @@ -27,6 +27,7 @@ OUT := out DEPTH := HCP_PATH := ../hcp RPIHAL_PATH := ../HAL_Driver +BMLITE_PATH := ../BMLite_sdk # Main target TARGET := $(OUT)/$(PRODUCT) @@ -46,7 +47,9 @@ CFLAGS +=\ -Wno-unused-result CFLAGS +=\ - -DBMLITE_USE_CALLBACK + -DBMLITE_USE_CALLBACK \ + -DDEBUG + # C source files C_SRCS = $(wildcard src/*.c) @@ -56,6 +59,8 @@ PATH_INC += inc C_INC = $(addprefix -I,$(PATH_INC)) +# Include BM-Lite SDK +include $(BMLITE_PATH)/bmlite.mk # Include HAL driver include $(RPIHAL_PATH)/raspberry.mk diff --git a/BMLite_example/inc/raspberry_pi_hal.h b/BMLite_example/inc/raspberry_pi_hal.h deleted file mode 100644 index f174e2b..0000000 --- a/BMLite_example/inc/raspberry_pi_hal.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2020 Fingerprint Cards AB - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * @file Raspberry_pi_hal.h - * @brief Raspberry Pi HAL. - */ - -#ifndef RASPBERRYPI_HAL_H -#define RASPBERRYPI_HAL_H - -/* -* Pin definitions for RPI 3 -*/ -#define BMLITE_RESET_PIN 0 -#define BMLITE_IRQ_PIN 22 -#define SPI_CHANNEL 0 - -#endif /* RASPBERRYPI_HAL_H */ diff --git a/BMLite_example/src/main.c b/BMLite_example/src/main.c index c2164ad..64ead62 100644 --- a/BMLite_example/src/main.c +++ b/BMLite_example/src/main.c @@ -32,15 +32,18 @@ #include #include "bmlite_if.h" +#include "hcp_tiny.h" #include "platform.h" +#include "bmlite_hal.h" +#include "platform_rpi.h" #define DATA_BUFFER_SIZE 102400 static uint8_t hcp_txrx_buffer[MTU]; static uint8_t hcp_data_buffer[DATA_BUFFER_SIZE]; static HCP_comm_t hcp_chain = { - .read = platform_spi_receive, - .write = platform_spi_send, + .read = platform_bmlite_receive, + .write = platform_bmlite_send, .pkt_buffer = hcp_data_buffer, .txrx_buffer = hcp_txrx_buffer, .pkt_size = 0, @@ -106,30 +109,33 @@ void save_to_pgm(FILE *f, uint8_t *image, int res_x, int res_y) int main (int argc, char **argv) { - char *port = NULL; - int baudrate = 921600; - int timeout = 5; int index; int c; - interface_t iface = COM_INTERFACE; + rpi_initparams_t rpi_params; + + rpi_params.iface = COM_INTERFACE; + rpi_params.hcp_comm = &hcp_chain; + rpi_params.baudrate = 921600; + rpi_params.timeout = 5; + rpi_params.port = NULL; opterr = 0; while ((c = getopt (argc, argv, "sb:p:t:")) != -1) { switch (c) { case 's': - iface = SPI_INTERFACE; - if(baudrate == 921600) - baudrate = 1000000; + rpi_params.iface = SPI_INTERFACE; + if(rpi_params.baudrate == 921600) + rpi_params.baudrate = 1000000; break; case 'b': - baudrate = atoi(optarg); + rpi_params.baudrate = atoi(optarg); break; case 'p': - port = optarg; + rpi_params.port = optarg; break; case 't': - timeout = atoi(optarg); + rpi_params.timeout = atoi(optarg); break; case '?': if (optopt == 'b') @@ -147,7 +153,7 @@ int main (int argc, char **argv) } } - if (iface == COM_INTERFACE && port == NULL) { + if (rpi_params.iface == COM_INTERFACE && rpi_params.port == NULL) { printf("port must be specified\n"); help(); exit(1); @@ -157,31 +163,9 @@ int main (int argc, char **argv) printf ("Non-option argument %s\n", argv[index]); } - switch (iface) { - case SPI_INTERFACE: - if(!platform_spi_init(baudrate)) { - printf("SPI initialization failed\n"); - exit(1); - } - break; - case COM_INTERFACE: - if (!platform_com_init(port, baudrate, timeout)) { - printf("Com initialization failed\n"); - exit(1); - } - break; - default: - printf("Interface not specified'n"); - help(); - exit(1); - } - - if (iface == COM_INTERFACE) { - hcp_chain.read = platform_com_receive; - hcp_chain.write = platform_com_send; - } else { - hcp_chain.read = platform_spi_receive; - hcp_chain.write = platform_spi_send; + if(platform_init(&rpi_params) != FPC_BEP_RESULT_OK) { + help(); + exit(1); } while(1) { @@ -190,13 +174,13 @@ int main (int argc, char **argv) uint16_t template_id; bool match; - // platform_clear_screen(); + rpi_clear_screen(); printf("BM-Lite Interface\n"); - if (iface == SPI_INTERFACE) - printf("SPI port: speed %d Hz\n", baudrate); + if (rpi_params.iface == SPI_INTERFACE) + printf("SPI port: speed %d Hz\n", rpi_params.baudrate); else - printf("Com port: %s [speed: %d]\n", port, baudrate); - printf("Timeout: %ds\n", timeout); + printf("Com port: %s [speed: %d]\n", rpi_params.port, rpi_params.baudrate); + printf("Timeout: %ds\n", rpi_params.timeout); printf("-------------------\n\n"); printf("Possible options:\n"); printf("a: Enroll finger\n"); diff --git a/BMLite_sdk/bmlite.mk b/BMLite_sdk/bmlite.mk new file mode 100644 index 0000000..781c687 --- /dev/null +++ b/BMLite_sdk/bmlite.mk @@ -0,0 +1,13 @@ +# Binary sources + +BMLITE_SDK = $(DEPTH)../BMLite_sdk + +VPATH += $(BMLITE_SDK) + +C_INC += -I$(BMLITE_SDK)/inc + +# Source Folders +VPATH += $(BMLITE_SDK)/src/ + +# C Sources +C_SRCS += $(notdir $(wildcard $(BMLITE_SDK)/src/*.c)) \ No newline at end of file diff --git a/BMLite_sdk/inc/bmlite_hal.h b/BMLite_sdk/inc/bmlite_hal.h new file mode 100755 index 0000000..b926941 --- /dev/null +++ b/BMLite_sdk/inc/bmlite_hal.h @@ -0,0 +1,116 @@ +/** + * @file bmlite_hal.h + * @brief BM-Lite HAL functions. + * + * All functions must be implemented in order to support BM-Lite on a Board + */ + +#ifndef BMLITE_H +#define BMLITE_H + +#include +#include +#include + +#include "fpc_bep_types.h" + +#ifdef __arm__ +typedef uint32_t hal_tick_t; +#else +typedef uint64_t hal_tick_t; +#endif + +/** + * @brief LED status. + * + * Different LED status. + */ +typedef enum { + BMLITE_LED_STATUS_READY = 0, + BMLITE_LED_STATUS_MATCH, + BMLITE_LED_STATUS_WAITTOUCH, + BMLITE_LED_STATUS_ENROLL, + BMLITE_LED_STATUS_DELETE_TEMPLATES, + BMLITE_LED_STATUS_ERROR, +} platform_led_status_t; + + +/* + * @brief Board initialization + * @param[in] params - pointer to additional parameters + */ + +fpc_bep_result_t hal_board_init(void *params); + +/* + * @brief Control BM-Lite Reset pin + * @param[in] True - Activate RESET + * False - Deactivate RESET + */ +void hal_bmlite_reset(bool state); + +/* + * @brief SPI write-read + * @param[in] Write buffer + * @param[in] Read buffer + * @param[in] Size + * @param[in] Leave CS asserted + * @return ::fpc_bep_result_t + */ +fpc_bep_result_t hal_bmlite_spi_write_read(uint8_t *write, uint8_t *read, size_t size, + bool leave_cs_asserted); + +/* + * @brief Check if BM-Lite IRQ pin is set + * @return ::bool + */ +bool hal_bmlite_get_status(void); + +/** + * @brief Initializes timebase. Starts system tick counter. + */ +void hal_timebase_init(void); + +/** + * @brief Reads the system tick counter. + * + * @return Tick count since hal_timebase_init() call. [ms] + */ +hal_tick_t hal_timebase_get_tick(void); + +/** + * @brief Busy wait. + * + * @param[in] ms Time to wait [ms]. + * 0 => return immediately + * 1 => wait at least 1ms etc. + */ +void hal_timebase_busy_wait(uint32_t ms); + +/** + * Optional functions for Buttons & Leds control + */ + +/** + * @brief Get button press time (msec) + * + * @return ::uint32_t + */ +uint32_t hal_get_button_press_time(void); + +/** + * @brief Check if button was pressed and released. + * + * @return Button press time in milli seconds. + */ +uint32_t hal_check_button_pressed(void); + +/** + * @brief Set LED(s) status + * @param[in] Status + * @param[in] Status modifier + */ +void hal_set_leds(platform_led_status_t status, uint16_t mode); + + +#endif /* BMLITE_H */ diff --git a/BMLite_example/inc/bmlite_if.h b/BMLite_sdk/inc/bmlite_if.h similarity index 100% rename from BMLite_example/inc/bmlite_if.h rename to BMLite_sdk/inc/bmlite_if.h diff --git a/BMLite_example/inc/bmlite_if_callbacks.h b/BMLite_sdk/inc/bmlite_if_callbacks.h similarity index 100% rename from BMLite_example/inc/bmlite_if_callbacks.h rename to BMLite_sdk/inc/bmlite_if_callbacks.h diff --git a/BMLite_example/inc/fpc_bep_types.h b/BMLite_sdk/inc/fpc_bep_types.h similarity index 98% rename from BMLite_example/inc/fpc_bep_types.h rename to BMLite_sdk/inc/fpc_bep_types.h index e9a187e..31f310d 100644 --- a/BMLite_example/inc/fpc_bep_types.h +++ b/BMLite_sdk/inc/fpc_bep_types.h @@ -17,8 +17,9 @@ #ifndef FPC_BEP_TYPES_H #define FPC_BEP_TYPES_H +#include #include - +#include /** * @file fpc_bep_types.h * @brief Biometric Embedded Platform types. diff --git a/BMLite_example/inc/fpc_crc.h b/BMLite_sdk/inc/fpc_crc.h similarity index 100% rename from BMLite_example/inc/fpc_crc.h rename to BMLite_sdk/inc/fpc_crc.h diff --git a/BMLite_example/inc/fpc_hcp_common.h b/BMLite_sdk/inc/fpc_hcp_common.h similarity index 100% rename from BMLite_example/inc/fpc_hcp_common.h rename to BMLite_sdk/inc/fpc_hcp_common.h diff --git a/BMLite_example/inc/hcp_tiny.h b/BMLite_sdk/inc/hcp_tiny.h similarity index 100% rename from BMLite_example/inc/hcp_tiny.h rename to BMLite_sdk/inc/hcp_tiny.h diff --git a/BMLite_sdk/inc/platform.h b/BMLite_sdk/inc/platform.h new file mode 100644 index 0000000..a3f2495 --- /dev/null +++ b/BMLite_sdk/inc/platform.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2020 Fingerprint Cards AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PLATFORM_H +#define PLATFORM_H + +/** + * @file platform.h + * @brief Platform specific function interface + */ + +#include +#include +#include + +#include "fpc_bep_types.h" + +/** + * @brief Initializes board + * + * @param[in] params - pointer to additional parameters. + */ +fpc_bep_result_t platform_init(void *params); + +/** + * @brief Does BM-Lite HW Reset + * + */ +void platform_bmlite_reset(void); + +/** + * @brief Sends data over communication port in blocking mode. + * + * @param[in] size Number of bytes to send. + * @param[in] data Data buffer to send. + * @param[in] timeout Timeout in ms. Use 0 for infinity. + * + * @return ::fpc_com_result_t + */ +fpc_bep_result_t platform_bmlite_send(uint16_t size, const uint8_t *data, uint32_t timeout, + void *session); + +/** + * @brief Receives data from communication port in blocking mode. + * + * @param[in] size Number of bytes to receive. + * @param[in, out] data Data buffer to fill. + * @param[in] timeout Timeout in ms. Use 0 for infinity. + * + * @return ::fpc_com_result_t + */ +fpc_bep_result_t platform_bmlite_receive(uint16_t size, uint8_t *data, uint32_t timeout, + void *session); + +/** + * @brief Stops execution if a debug interface is attached. + */ +void platform_halt_if_debug(void); + +/** + * @brief Performs a software reset. + */ +void platform_sw_reset(void) __attribute__((__noreturn__)); + +#endif /* PLATFORM_H */ diff --git a/BMLite_example/src/bmlite_if.c b/BMLite_sdk/src/bmlite_if.c similarity index 95% rename from BMLite_example/src/bmlite_if.c rename to BMLite_sdk/src/bmlite_if.c index 9e8560f..c20ea70 100644 --- a/BMLite_example/src/bmlite_if.c +++ b/BMLite_sdk/src/bmlite_if.c @@ -1,14 +1,16 @@ #include "hcp_tiny.h" #include "bmlite_if.h" +#include "bmlite_hal.h" #include "platform.h" #include #include "bmlite_if_callbacks.h" #define MAX_CAPTURE_ATTEMPTS 15 +#define MAX_SINGLE_CAPTURE_ATTEMPTS 3 #define CAPTURE_TIMEOUT 3000 -#define exit_if_err(c) { bep_result = c; if(bep_result) goto exit; } +#define exit_if_err(c) { bep_result = c; if(bep_result || chain->bep_result) goto exit; } #define assert(c) { fpc_bep_result_t res = c; if(res) return res; } @@ -78,7 +80,11 @@ fpc_bep_result_t bep_capture(HCP_comm_t *chain, uint16_t timeout) bmlite_on_start_capture(); chain->phy_rx_timeout = timeout; - bep_result = bmlite_send_cmd_arg(chain, CMD_CAPTURE, ARG_NONE, ARG_TIMEOUT, &timeout, sizeof(timeout)); + for(int i=0; i< MAX_SINGLE_CAPTURE_ATTEMPTS; i++) { + bep_result = bmlite_send_cmd_arg(chain, CMD_CAPTURE, ARG_NONE, ARG_TIMEOUT, &timeout, sizeof(timeout)); + if( !(bep_result || chain->bep_result)) + break; + } chain->phy_rx_timeout = prev_timeout; bmlite_on_finish_capture(); diff --git a/BMLite_example/src/fpc_crc.c b/BMLite_sdk/src/fpc_crc.c similarity index 100% rename from BMLite_example/src/fpc_crc.c rename to BMLite_sdk/src/fpc_crc.c diff --git a/BMLite_example/src/hcp_tiny.c b/BMLite_sdk/src/hcp_tiny.c similarity index 99% rename from BMLite_example/src/hcp_tiny.c rename to BMLite_sdk/src/hcp_tiny.c index 47db667..43bdab7 100644 --- a/BMLite_example/src/hcp_tiny.c +++ b/BMLite_sdk/src/hcp_tiny.c @@ -7,8 +7,6 @@ #include "bmlite_if_callbacks.h" -#define DEBUG - #ifdef DEBUG #include #include @@ -87,7 +85,7 @@ fpc_bep_result_t bmlite_get_arg(HCP_comm_t *hcp_comm, uint16_t arg_type) uint8_t *buffer = hcp_comm->pkt_buffer; uint16_t args_nr = ((_HCP_cmd_t *)(buffer))->args_nr; uint8_t *pdata = (uint8_t *)&((_HCP_cmd_t *)(buffer))->args; - while (i < args_nr && (pdata - buffer) <= hcp_comm->pkt_size) { + while (i < args_nr && (uint32_t)(pdata - buffer) <= hcp_comm->pkt_size) { _CMD_arg_t *parg = (_CMD_arg_t *)pdata; if(parg->arg == arg_type) { hcp_comm->arg.size = parg->size; diff --git a/BMLite_sdk/src/platform.c b/BMLite_sdk/src/platform.c new file mode 100644 index 0000000..5d28d6a --- /dev/null +++ b/BMLite_sdk/src/platform.c @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2020 Fingerprint Cards AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file platform.c + * @brief Platform specific functions + */ + + +#include "fpc_bep_types.h" +#include "platform.h" +#include "bmlite_hal.h" + +fpc_bep_result_t platform_init(void *params) +{ + fpc_bep_result_t result; + result = hal_board_init(params); + if(result == FPC_BEP_RESULT_OK) { + hal_timebase_init(); + platform_bmlite_reset(); + } + return result; +} + +void platform_bmlite_reset(void) +{ + hal_bmlite_reset(true); + hal_timebase_busy_wait(100); + hal_bmlite_reset(false); + hal_timebase_busy_wait(100); +} + +fpc_bep_result_t platform_bmlite_send(uint16_t size, const uint8_t *data, uint32_t timeout, + void *session) +{ + uint8_t buff[size]; + + return hal_bmlite_spi_write_read((uint8_t *)data, buff, size, false); +} + +fpc_bep_result_t platform_bmlite_receive(uint16_t size, uint8_t *data, uint32_t timeout, + void *session) +{ + volatile uint32_t start_time = hal_timebase_get_tick(); + volatile uint32_t curr_time = start_time; + // Wait for BM_Lite Ready for timeout or indefinitely if timeout is 0 + while (!hal_bmlite_get_status() && + (!timeout || (curr_time = hal_timebase_get_tick()) - start_time < timeout)) { + if(hal_check_button_pressed()) { + return FPC_BEP_RESULT_TIMEOUT; + } + } + if(timeout && curr_time - start_time >= timeout) { + return FPC_BEP_RESULT_TIMEOUT; + } + + uint8_t buff[size]; + return hal_bmlite_spi_write_read(buff, data, size, false); +} + +__attribute__((weak)) uint32_t hal_check_button_pressed() +{ + return 0; +} + diff --git a/BMLite_example/inc/platform.h b/HAL_Driver/inc/platform_rpi.h similarity index 80% rename from BMLite_example/inc/platform.h rename to HAL_Driver/inc/platform_rpi.h index 29a32ef..e8be771 100644 --- a/BMLite_example/inc/platform.h +++ b/HAL_Driver/inc/platform_rpi.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef PLATFORM_H -#define PLATFORM_H +#ifndef PLATFORM_RPI_H +#define PLATFORM_RPI_H /** * @file platform.h @@ -26,12 +26,27 @@ #include #include "fpc_bep_types.h" +#include "hcp_tiny.h" typedef enum { COM_INTERFACE = 0, SPI_INTERFACE } interface_t; +typedef struct { + interface_t iface; + char *port; + uint32_t baudrate; + uint32_t timeout; + HCP_comm_t *hcp_comm; +} rpi_initparams_t; + +/* +* Pin definitions for RPI 3 +*/ +#define BMLITE_RESET_PIN 0 +#define BMLITE_IRQ_PIN 22 +#define SPI_CHANNEL 0 /** * @brief Initializes COM Physical layer. @@ -40,7 +55,7 @@ typedef enum { * @param[in] baudrate Baudrate. * @param[in] timeout Timeout in ms. Use 0 for infinity. */ -bool platform_com_init(char *port, int baudrate, int timeout); +bool rpi_com_init(char *port, int baudrate, int timeout); /** * @brief Sends data over communication port in blocking mode. @@ -51,7 +66,7 @@ bool platform_com_init(char *port, int baudrate, int timeout); * * @return ::fpc_bep_result_t */ -fpc_bep_result_t platform_com_send(uint16_t size, const uint8_t *data, uint32_t timeout, +fpc_bep_result_t rpi_com_send(uint16_t size, const uint8_t *data, uint32_t timeout, void *session); /** @@ -63,7 +78,7 @@ fpc_bep_result_t platform_com_send(uint16_t size, const uint8_t *data, uint32_t * * @return ::fpc_bep_result_t */ -fpc_bep_result_t platform_com_receive(uint16_t size, uint8_t *data, uint32_t timeout, +fpc_bep_result_t rpi_com_receive(uint16_t size, uint8_t *data, uint32_t timeout, void *session); /** @@ -71,7 +86,7 @@ fpc_bep_result_t platform_com_receive(uint16_t size, uint8_t *data, uint32_t tim * * @param[in] speed_hz Baudrate. */ -bool platform_spi_init(uint32_t speed_hz); +bool rpi_spi_init(uint32_t speed_hz); /** * @brief Sends data over communication port in blocking mode. @@ -102,12 +117,12 @@ fpc_bep_result_t platform_spi_receive(uint16_t size, uint8_t *data, uint32_t tim * * @return time in us. */ -uint64_t platform_get_time(void); +// uint64_t platform_get_time(void); /** * @brief Clear console screen */ -void platform_clear_screen(void); +void rpi_clear_screen(void); /** * @brief Busy wait. @@ -119,4 +134,4 @@ void platform_clear_screen(void); void hal_timebase_busy_wait(uint32_t ms); -#endif /* PLATFORM_H */ +#endif /* PLATFORM_RPI_H */ diff --git a/HAL_Driver/raspberry.mk b/HAL_Driver/raspberry.mk index 2574081..90be377 100644 --- a/HAL_Driver/raspberry.mk +++ b/HAL_Driver/raspberry.mk @@ -1,6 +1,15 @@ # Binary sources -VPATH += $(DEPTH)../HAL_Driver -C_INC += -I$(DEPTH)../HAL_Driver/inc +NHAL = $(DEPTH)../HAL_Driver -LDFLAGS += -lwiringPi -L$(DEPTH)../HAL_Driver/lib/ +VPATH += $(NHAL) + +C_INC += -I$(NHAL)/inc + +LDFLAGS += -lwiringPi -L$(NHAL)/lib/ + +# Source Folders +VPATH += $(NHAL)/src/ + +# C Sources +C_SRCS += $(notdir $(wildcard $(NHAL)/src/*.c)) \ No newline at end of file diff --git a/BMLite_example/src/platform_linux.c b/HAL_Driver/src/platform_linux.c similarity index 51% rename from BMLite_example/src/platform_linux.c rename to HAL_Driver/src/platform_linux.c index 6227ced..f8e2d5c 100644 --- a/BMLite_example/src/platform_linux.c +++ b/HAL_Driver/src/platform_linux.c @@ -30,9 +30,11 @@ #include #include +#include "bmlite_hal.h" +#include "platform_rpi.h" #include "platform.h" -uint64_t platform_get_time(void) +hal_tick_t hal_timebase_get_tick(void) { struct timeval current_time; uint64_t time_in_ms; @@ -44,12 +46,50 @@ uint64_t platform_get_time(void) return time_in_ms; } -void platform_clear_screen(void) +void hal_timebase_busy_wait(uint32_t ms) +{ + usleep(ms * 1000); +} + +void rpi_clear_screen(void) { system("clear"); } -void hal_timebase_busy_wait(uint32_t ms) +void hal_timebase_init() { - usleep(ms * 1000); -} \ No newline at end of file +} + +fpc_bep_result_t hal_board_init(void *params) +{ + rpi_initparams_t *p = (rpi_initparams_t *)params; + switch (p->iface) { + case SPI_INTERFACE: + if(!rpi_spi_init(p->baudrate)) { + printf("SPI initialization failed\n"); + return FPC_BEP_RESULT_INTERNAL_ERROR; + } + break; + case COM_INTERFACE: + if (!rpi_com_init(p->port, p->baudrate, p->timeout)) { + printf("Com initialization failed\n"); + return FPC_BEP_RESULT_INTERNAL_ERROR; + } + break; + default: + printf("Interface not specified'n"); + return FPC_BEP_RESULT_INTERNAL_ERROR; + } + + if (p->iface == COM_INTERFACE) { + p->hcp_comm->read = rpi_com_receive; + p->hcp_comm->write = rpi_com_send; + } else { + p->hcp_comm->read = platform_bmlite_receive; + p->hcp_comm->write = platform_bmlite_send; + } + + p->hcp_comm->phy_rx_timeout = p->timeout*1000; + + return FPC_BEP_RESULT_OK; +} diff --git a/BMLite_example/src/platform_com.c b/HAL_Driver/src/rpi_com.c similarity index 92% rename from BMLite_example/src/platform_com.c rename to HAL_Driver/src/rpi_com.c index 51618b9..fce0160 100644 --- a/BMLite_example/src/platform_com.c +++ b/HAL_Driver/src/rpi_com.c @@ -29,7 +29,7 @@ #include #include -#include "platform.h" +#include "platform_rpi.h" static int fd = -1; @@ -63,7 +63,7 @@ static int set_interface_attribs(int fd, int speed, int timeout) return 0; } -bool platform_com_init(char *port, int baudrate, int timeout) +bool rpi_com_init(char *port, int baudrate, int timeout) { int baud; @@ -84,7 +84,7 @@ bool platform_com_init(char *port, int baudrate, int timeout) return true; } -fpc_bep_result_t platform_com_send(uint16_t size, const uint8_t *data, uint32_t timeout, +fpc_bep_result_t rpi_com_send(uint16_t size, const uint8_t *data, uint32_t timeout, void *session) { fpc_bep_result_t res = FPC_BEP_RESULT_OK; @@ -104,7 +104,7 @@ fpc_bep_result_t platform_com_send(uint16_t size, const uint8_t *data, uint32_t return res; } -fpc_bep_result_t platform_com_receive(uint16_t size, uint8_t *data, uint32_t timeout, +fpc_bep_result_t rpi_com_receive(uint16_t size, uint8_t *data, uint32_t timeout, void *session) { fpc_bep_result_t res = FPC_BEP_RESULT_OK; diff --git a/BMLite_example/src/platform_spi.c b/HAL_Driver/src/rpi_spi.c similarity index 79% rename from BMLite_example/src/platform_spi.c rename to HAL_Driver/src/rpi_spi.c index 235054e..173bd8a 100644 --- a/BMLite_example/src/platform_spi.c +++ b/HAL_Driver/src/rpi_spi.c @@ -39,7 +39,7 @@ #include "wiringPi.h" #include "wiringPiSPI.h" -#include "raspberry_pi_hal.h" +#include "platform_rpi.h" #define SPI_BUF_MIN_SIZE 40000 // This path is used to determine the SPI buffer size. @@ -52,7 +52,7 @@ uint32_t speed_hz_int; void fpc_sensor_spi_reset(bool state); -void raspberryPi_init() +static void raspberryPi_init() { /* Start wiringPi functions. */ wiringPiSetup(); @@ -66,15 +66,7 @@ void raspberryPi_init() } -void platform_spi_sensor_reset(void) -{ - fpc_sensor_spi_reset(true); - usleep(100); - fpc_sensor_spi_reset(false); - usleep(10); -} - -void fpc_sensor_spi_reset(bool state) +void hal_bmlite_reset(bool state) { /* The reset pin is controlled by WiringPis digitalWrite function*/ if (state) { @@ -90,7 +82,7 @@ bool hal_bmlite_get_status(void) } -bool platform_spi_init(uint32_t speed_hz) +bool rpi_spi_init(uint32_t speed_hz) { raspberryPi_init(); @@ -143,7 +135,7 @@ bool platform_spi_init(uint32_t speed_hz) return true; } -static fpc_bep_result_t platform_spi_write_read(const uint8_t *write, uint8_t *read, size_t size, +fpc_bep_result_t hal_bmlite_spi_write_read(const uint8_t *write, uint8_t *read, size_t size, bool leave_cs_asserted) { /* @@ -179,28 +171,28 @@ static fpc_bep_result_t platform_spi_write_read(const uint8_t *write, uint8_t *r } -fpc_bep_result_t platform_spi_send(uint16_t size, const uint8_t *data, uint32_t timeout, - void *session) -{ - uint8_t buff[size]; +// fpc_bep_result_t platform_spi_send(uint16_t size, const uint8_t *data, uint32_t timeout, +// void *session) +// { +// uint8_t buff[size]; - return platform_spi_write_read(data, buff, size, false); -} +// return platform_spi_write_read(data, buff, size, false); +// } -fpc_bep_result_t platform_spi_receive(uint16_t size, uint8_t *data, uint32_t timeout, - void *session) -{ - volatile uint64_t start_time = platform_get_time(); - volatile uint64_t curr_time = start_time; - // Wait for BM_Lite Ready for timeout or indefinitely if timeout is 0 - while (!hal_bmlite_get_status() && - (!timeout || (curr_time = platform_get_time()) - start_time < timeout)) { - //usleep(1); - } - if(timeout && curr_time - start_time >= timeout) { - return FPC_BEP_RESULT_TIMEOUT; - } +// fpc_bep_result_t platform_spi_receive(uint16_t size, uint8_t *data, uint32_t timeout, +// void *session) +// { +// volatile uint64_t start_time = platform_get_time(); +// volatile uint64_t curr_time = start_time; +// // Wait for BM_Lite Ready for timeout or indefinitely if timeout is 0 +// while (!hal_bmlite_get_status() && +// (!timeout || (curr_time = platform_get_time()) - start_time < timeout)) { +// //usleep(1); +// } +// if(timeout && curr_time - start_time >= timeout) { +// return FPC_BEP_RESULT_TIMEOUT; +// } - uint8_t buff[size]; - return platform_spi_write_read(buff, data, size, false); -} +// uint8_t buff[size]; +// return platform_spi_write_read(buff, data, size, false); +// }