From a2567961d365810d87348f623a4ebb8c0d3762e1 Mon Sep 17 00:00:00 2001 From: Sven Schwermer Date: Tue, 20 Apr 2021 09:24:32 +0200 Subject: [PATCH 1/3] pal: linux: Refactor I2C abstraction Properly handle errors and format source code. Signed-off-by: Sven Schwermer --- pal/linux/pal_i2c.c | 311 ++++++++++++++------------------------------ 1 file changed, 98 insertions(+), 213 deletions(-) diff --git a/pal/linux/pal_i2c.c b/pal/linux/pal_i2c.c index 4d6d5add..220971d7 100644 --- a/pal/linux/pal_i2c.c +++ b/pal/linux/pal_i2c.c @@ -34,8 +34,9 @@ * @{ */ -#include +#include #include +#include #include #include @@ -45,275 +46,159 @@ #if IFX_I2C_LOG_HAL == 1 #define LOG_HAL IFX_I2C_LOG #else -#include +#include #define LOG_HAL(...) //printf(__VA_ARGS__) #endif -// Slave address not initialization -#define IFXI2C_SLAVE_ADDRESS_INIT 0xFFFF -#define PAL_I2C_MASTER_MAX_BITRATE 100 -#define WAIT_500_MS (500) /// @cond hidden -void i2c_master_end_of_transmit_callback(void); -void i2c_master_end_of_receive_callback(void); -void invoke_upper_layer_callback (const pal_i2c_t* p_pal_i2c_ctx, optiga_lib_status_t event); -uint16_t usb_i2c_poll_operation_result(pal_i2c_t* p_i2c_context); - -/* Varibale to indicate the re-entrant count of the i2c bus acquire function*/ -static volatile uint32_t g_entry_count = 0; - -/* Pointer to the current pal i2c context*/ -static pal_i2c_t * gp_pal_i2c_current_ctx; - -//lint --e{715} suppress the unused p_i2c_context variable lint error , since this is kept for future enhancements -static pal_status_t pal_i2c_acquire(const void * p_i2c_context) +static pal_status_t pal_i2c_acquire(const pal_i2c_t *p_i2c_context) { - if (0 == g_entry_count) - { - g_entry_count++; - if (1 == g_entry_count) - { - return PAL_STATUS_SUCCESS; - } - } - return PAL_STATUS_FAILURE; + pal_linux_t *pal_linux = (pal_linux_t *) p_i2c_context->p_i2c_hw_config; + if (0 == lockf(pal_linux->i2c_handle, F_TLOCK, 0)) + return PAL_STATUS_SUCCESS; + else + return PAL_STATUS_FAILURE; } -// I2C release bus function -//lint --e{715} suppress the unused p_i2c_context variable lint, since this is kept for future enhancements -static void pal_i2c_release(const void* p_i2c_context) +static void pal_i2c_release(const pal_i2c_t *p_i2c_context) { - g_entry_count = 0; + pal_linux_t *pal_linux = (pal_linux_t *) p_i2c_context->p_i2c_hw_config; + lockf(pal_linux->i2c_handle, F_ULOCK, 0); } + /// @endcond -void invoke_upper_layer_callback (const pal_i2c_t * p_pal_i2c_ctx, optiga_lib_status_t event) +static void invoke_upper_layer_callback(const pal_i2c_t *p_pal_i2c_ctx, pal_status_t status) { - upper_layer_callback_t upper_layer_handler; //lint --e{611} suppress "void* function pointer is type casted to upper_layer_callback_t type" - upper_layer_handler = (upper_layer_callback_t )p_pal_i2c_ctx->upper_layer_event_handler; - - upper_layer_handler(p_pal_i2c_ctx->p_upper_layer_ctx , event); - - //Release I2C Bus - pal_i2c_release(p_pal_i2c_ctx->p_upper_layer_ctx ); -} - -/// @cond hidden -// I2C driver callback function when the transmit is completed successfully -void i2c_master_end_of_transmit_callback(void) -{ - invoke_upper_layer_callback(gp_pal_i2c_current_ctx, PAL_I2C_EVENT_SUCCESS); -} + upper_layer_callback_t upper_layer_handler = (upper_layer_callback_t) p_pal_i2c_ctx->upper_layer_event_handler; + optiga_lib_status_t event; + switch (status) + { + case PAL_STATUS_SUCCESS: + event = PAL_I2C_EVENT_SUCCESS; + break; + case PAL_STATUS_I2C_BUSY: + event = PAL_I2C_EVENT_BUSY; + break; + default: + event = PAL_I2C_EVENT_ERROR; + break; + } -// I2C driver callback function when the receive is completed successfully -void i2c_master_end_of_receive_callback(void) -{ - invoke_upper_layer_callback(gp_pal_i2c_current_ctx, PAL_I2C_EVENT_SUCCESS); + if (upper_layer_handler) + upper_layer_handler(p_pal_i2c_ctx->p_upper_layer_ctx, event); } -// I2C error callback function -void i2c_master_error_detected_callback(void) +static void hexdump(const uint8_t *data, int length, const char *dir) { - //I2C_MASTER_t *p_i2c_master; - // - //p_i2c_master = gp_pal_i2c_current_ctx->p_i2c_hw_config; - //if (I2C_MASTER_IsTxBusy(p_i2c_master)) - //{ - // //lint --e{534} suppress "Return value is not required to be checked" - // I2C_MASTER_AbortTransmit(p_i2c_master); - // while (I2C_MASTER_IsTxBusy(p_i2c_master)){} - //} - - //if (I2C_MASTER_IsRxBusy(p_i2c_master)) - //{ - // //lint --e{534} suppress "Return value is not required to be checked" - // I2C_MASTER_AbortReceive(p_i2c_master); - // while (I2C_MASTER_IsRxBusy(p_i2c_master)){} - //} - - invoke_upper_layer_callback(gp_pal_i2c_current_ctx, PAL_I2C_EVENT_ERROR); -} + LOG_HAL("[IFX-HAL]: I2C %s (%d): ", dir, length); + for (int i = 0; i < length; i++) + { + LOG_HAL("%02X ", data[i]); + } -void i2c_master_nack_received_callback(void) -{ - i2c_master_error_detected_callback(); + LOG_HAL("\n"); } -void i2c_master_arbitration_lost_callback(void) +pal_status_t pal_i2c_init(const pal_i2c_t* p_i2c_context) { - i2c_master_error_detected_callback(); -} + pal_linux_t *pal_linux = (pal_linux_t *) p_i2c_context->p_i2c_hw_config; + pal_linux->i2c_handle = open(pal_linux->i2c_if, O_RDWR); + if (pal_linux->i2c_handle == -1) + { + LOG_HAL("open returned an error = %d (%s)\n", errno, strerror(errno)); + return PAL_STATUS_FAILURE; + } -/// @endcond + // Assign the slave address + int ret = ioctl(pal_linux->i2c_handle, I2C_SLAVE, p_i2c_context->slave_address); + if (ret == -1) + { + LOG_HAL("ioctl returned an error = %d (%s)\n", errno, strerror(errno)); + return PAL_STATUS_FAILURE; + } -pal_status_t pal_i2c_init(const pal_i2c_t* p_i2c_context) -{ - int32_t ret = PAL_I2C_EVENT_ERROR; - pal_linux_t *pal_linux; - do - { - pal_linux = (pal_linux_t*) p_i2c_context->p_i2c_hw_config; - pal_linux->i2c_handle = open(pal_linux->i2c_if, O_RDWR); - LOG_HAL("IFX OPTIGA TRUST X Logs \n"); - - // Assign the slave address - ret = ioctl(pal_linux->i2c_handle, I2C_SLAVE, p_i2c_context->slave_address); - if(PAL_STATUS_SUCCESS != ret) - { - LOG_HAL((uint32_t)pal_linux->i2c_handle, "ioctl returned an error = ", ret); - break; - } - - //start_transceive_thread(); - }while(0); - return ret; + return PAL_STATUS_SUCCESS; } - pal_status_t pal_i2c_deinit(const pal_i2c_t* p_i2c_context) { - LOG_HAL("pal_i2c_deinit\n. "); - + pal_linux_t *pal_linux = (pal_linux_t *) p_i2c_context->p_i2c_hw_config; + if (pal_linux->i2c_handle >= 0) + { + int ret = close(pal_linux->i2c_handle); + if (ret == -1) + { + LOG_HAL("close returned an error = %d (%s)\n", errno, strerror(errno)); + return PAL_STATUS_FAILURE; + } + pal_linux->i2c_handle = -1; + } return PAL_STATUS_SUCCESS; } - -pal_status_t pal_i2c_write(const pal_i2c_t* p_i2c_context,uint8_t* p_data , uint16_t length) +pal_status_t pal_i2c_write(const pal_i2c_t *p_i2c_context, uint8_t *p_data, uint16_t length) { - pal_status_t status = PAL_STATUS_FAILURE; - int32_t i2c_write_status; - pal_linux_t *pal_linux; - - pal_linux = (pal_linux_t*) p_i2c_context->p_i2c_hw_config; - LOG_HAL("[IFX-HAL]: I2C TX (%d): ", length); -#if 1 - for (int i = 0; i < length; i++) - { - LOG_HAL("%02X ", p_data[i]); - } -#endif - LOG_HAL("\n"); - if(PAL_STATUS_SUCCESS == pal_i2c_acquire(p_i2c_context)) - { - gp_pal_i2c_current_ctx = (pal_i2c_t *)p_i2c_context; + pal_status_t status; + pal_linux_t *pal_linux = (pal_linux_t *) p_i2c_context->p_i2c_hw_config; - //Invoke the low level i2c master driver API to write to the bus + hexdump(p_data, length, "TX"); - i2c_write_status = write(pal_linux->i2c_handle, p_data, length); - if (0 > i2c_write_status) - { - //If I2C Master fails to invoke the write operation, invoke upper layer event handler with error. - - //lint --e{611} suppress "void* function pointer is type casted to upper_layer_callback_t type" - ((upper_layer_callback_t )(p_i2c_context->upper_layer_event_handler)) - (p_i2c_context->p_upper_layer_ctx , PAL_I2C_EVENT_ERROR); - - //Release I2C Bus - pal_i2c_release((void *)p_i2c_context); - } + if (PAL_STATUS_SUCCESS == pal_i2c_acquire(p_i2c_context)) + { + int ret = write(pal_linux->i2c_handle, p_data, length); + if (-1 == ret) + status = PAL_STATUS_FAILURE; else - { - i2c_master_end_of_transmit_callback(); status = PAL_STATUS_SUCCESS; - //transmission_completed = true; - } + + pal_i2c_release(p_i2c_context); } else - { status = PAL_STATUS_I2C_BUSY; - //lint --e{611} suppress "void* function pointer is type casted to upper_layer_callback_t type" - ((upper_layer_callback_t )(p_i2c_context->upper_layer_event_handler)) - (p_i2c_context->p_upper_layer_ctx , PAL_I2C_EVENT_BUSY); - } + + invoke_upper_layer_callback(p_i2c_context, status); return status; } - -pal_status_t pal_i2c_read(const pal_i2c_t* p_i2c_context , uint8_t* p_data , uint16_t length) +pal_status_t pal_i2c_read(const pal_i2c_t *p_i2c_context, uint8_t *p_data, uint16_t length) { - int32_t i2c_read_status = PAL_STATUS_FAILURE; - pal_linux_t *pal_linux; - + pal_status_t status; + pal_linux_t *pal_linux = (pal_linux_t *) p_i2c_context->p_i2c_hw_config; - pal_linux = (pal_linux_t*) p_i2c_context->p_i2c_hw_config; - //Acquire the I2C bus before read/write if (PAL_STATUS_SUCCESS == pal_i2c_acquire(p_i2c_context)) - { - gp_pal_i2c_current_ctx = (pal_i2c_t *)p_i2c_context; - i2c_read_status = read(pal_linux->i2c_handle,p_data, length); - if (0 > i2c_read_status) - { - LOG_HAL("[IFX-HAL]: libusb_interrupt_transfer ERROR %d\n.", i2c_read_status); - //lint --e{611} suppress "void* function pointer is type casted to upper_layer_callback_t type" - ((upper_layer_callback_t )(p_i2c_context->upper_layer_event_handler)) - (p_i2c_context->p_upper_layer_ctx , PAL_I2C_EVENT_ERROR); - //Release I2C Bus - pal_i2c_release((void *)p_i2c_context); - return i2c_read_status; - } - else + { + int ret = read(pal_linux->i2c_handle, p_data, length); + if (-1 == ret) + status = PAL_STATUS_FAILURE; + else { - LOG_HAL("[IFX-HAL]: I2C RX (%d)\n", length); -#if 1 - for (int i = 0; i < length; i++) - { - LOG_HAL("%02X ", p_data[i]); - } -#endif - - i2c_master_end_of_receive_callback(); - i2c_read_status = PAL_STATUS_SUCCESS; - //reception_started = true; + status = PAL_STATUS_SUCCESS; + hexdump(p_data, length, "RX"); } + + pal_i2c_release(p_i2c_context); } else - { - i2c_read_status = PAL_STATUS_I2C_BUSY; - //lint --e{611} suppress "void* function pointer is type casted to upper_layer_callback_t type" - ((upper_layer_callback_t )(p_i2c_context->upper_layer_event_handler)) - (p_i2c_context->p_upper_layer_ctx , PAL_I2C_EVENT_BUSY); - } - return i2c_read_status; -} + status = PAL_STATUS_I2C_BUSY; - + invoke_upper_layer_callback(p_i2c_context, status); + + return status; +} pal_status_t pal_i2c_set_bitrate(const pal_i2c_t* p_i2c_context , uint16_t bitrate) { - pal_status_t return_status = PAL_STATUS_FAILURE; - optiga_lib_status_t event = PAL_I2C_EVENT_ERROR; - LOG_HAL("pal_i2c_set_bitrate\n. "); - //Acquire the I2C bus before setting the bitrate - if (PAL_STATUS_SUCCESS == pal_i2c_acquire(p_i2c_context)) - { - // If the user provided bitrate is greater than the I2C master hardware maximum supported value, - // set the I2C master to its maximum supported value. - if (bitrate > PAL_I2C_MASTER_MAX_BITRATE) - { - bitrate = PAL_I2C_MASTER_MAX_BITRATE; - } - return_status = PAL_STATUS_SUCCESS; - event = PAL_I2C_EVENT_SUCCESS; - } - else - { - return_status = PAL_STATUS_I2C_BUSY; - event = PAL_I2C_EVENT_BUSY; - } - if (0 != p_i2c_context->upper_layer_event_handler) - { - //lint --e{611} suppress "void* function pointer is type casted to upper_layer_callback_t type" - ((upper_layer_callback_t)(p_i2c_context->upper_layer_event_handler))(p_i2c_context->p_upper_layer_ctx , event); - } - //Release I2C Bus - pal_i2c_release((void *)p_i2c_context); - return return_status; + // Linux' userspace i2c-dev does not allow changing the I2C clock rate + pal_status_t status = PAL_STATUS_SUCCESS; + invoke_upper_layer_callback(p_i2c_context, status); + return status; } /** From 7f85c21b14bc580ca5687b13e325a073870ed1d9 Mon Sep 17 00:00:00 2001 From: Sven Schwermer Date: Tue, 20 Apr 2021 14:34:19 +0200 Subject: [PATCH 2/3] pal: linux: Refactor GPIO abstraction Signed-off-by: Sven Schwermer --- pal/linux/pal_gpio.c | 232 +++++++++++++++++++++--------------------- pal/linux/pal_linux.h | 11 +- 2 files changed, 119 insertions(+), 124 deletions(-) diff --git a/pal/linux/pal_gpio.c b/pal/linux/pal_gpio.c index be8c31e1..80b0aa1d 100644 --- a/pal/linux/pal_gpio.c +++ b/pal/linux/pal_gpio.c @@ -45,128 +45,142 @@ #include "optiga/pal/pal_ifx_i2c_config.h" #include "pal_linux.h" -#define IN 0 -#define OUT 1 +#define PAL_GPIO_LOG(...) fprintf(stderr, __VA_ARGS__) -#define LOW 0 -#define HIGH 1 +static const char *IN = "in"; +static const char *OUT = "out"; +static const char *LOW = "0"; +static const char *HIGH = "1"; - -static int -GPIOExport(int pin) +static pal_status_t pal_gpio_export_unexport(const char *op, int pin) { -#define BUFFER_MAX 4 - char buffer[BUFFER_MAX]; - ssize_t bytes_written; - int fd; - - fd = open("/sys/class/gpio/export", O_WRONLY); - if (-1 == fd) { - fprintf(stderr, "Failed to open export for writing!\n"); - return(-1); + char buffer[32]; + int ret = snprintf(buffer, sizeof(buffer), "/sys/class/gpio/%s", op); + if (ret < 0 || ret == sizeof(buffer)) + { + PAL_GPIO_LOG("Failed to format GPIO %s file\n", op); + return PAL_STATUS_FAILURE; + } + + int fd = open(buffer, O_WRONLY); + if (fd == -1) + { + PAL_GPIO_LOG("Failed to open GPIO %s file for writing: %s\n", op, strerror(errno)); + return PAL_STATUS_FAILURE; + } + + ret = snprintf(buffer, sizeof(buffer), "%d", pin); + if (ret < 0 || ret == sizeof(buffer)) + { + PAL_GPIO_LOG("Failed to format string for GPIO %s\n", op); + close(fd); + return PAL_STATUS_FAILURE; + } + + ssize_t written = write(fd, buffer, ret); + if (written == -1) + { + PAL_GPIO_LOG("Failed to %s GPIO #%d: %s\n", op, pin, strerror(errno)); + close(fd); + return PAL_STATUS_FAILURE; } - bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin); - write(fd, buffer, bytes_written); close(fd); - usleep(100000); - return(0); + return PAL_STATUS_SUCCESS; } -static int -GPIOUnexport(int pin) +static pal_status_t pal_gpio_export(int pin) { - char buffer[BUFFER_MAX]; - ssize_t bytes_written; - int fd; - - fd = open("/sys/class/gpio/unexport", O_WRONLY); - if (-1 == fd) { - fprintf(stderr, "Failed to open unexport for writing!\n"); - return(-1); + char path[48]; + int ret = snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin); + if (ret < 0 || ret == sizeof(path)) + { + PAL_GPIO_LOG("Failed to format GPIO value file\n"); + return PAL_STATUS_FAILURE; } - bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin); - write(fd, buffer, bytes_written); - close(fd); - usleep(100000); - return(0); + // test if already exported + if (access(path, F_OK) == 0) + return PAL_STATUS_SUCCESS; + + return pal_gpio_export_unexport("export", pin); } -static int -GPIODirection(int pin, int dir) +static pal_status_t pal_gpio_unexport(int pin) { - static const char s_directions_str[] = "in\0out"; + return pal_gpio_export_unexport("unexport", pin); +} -#define DIRECTION_MAX 34 - char path[DIRECTION_MAX]; - int fd; +static pal_status_t pal_gpio_direction(int pin, const char *dir) +{ + char path[48]; + int ret = snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", pin); + if (ret <= 0 || ret == sizeof(path)) + { + PAL_GPIO_LOG("Failed to format GPIO direction file\n"); + return PAL_STATUS_FAILURE; + } - snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/direction", pin); - fd = open(path, O_WRONLY); - if (-1 == fd) { - fprintf(stderr, "Failed to open gpio direction for writing!\n"); - return(-1); + int fd = open(path, O_WRONLY); + if (fd == -1) + { + PAL_GPIO_LOG("Failed to open GPIO #%d direction file for writing: %s\n", pin, strerror(errno)); + return PAL_STATUS_FAILURE; } - if (-1 == write(fd, &s_directions_str[IN == dir ? 0 : 3], IN == dir ? 2 : 3)) { - fprintf(stderr, "Failed to set direction!\n"); - return(-1); + ssize_t written = write(fd, dir, strnlen(dir, 3)); + if (written == -1) + { + PAL_GPIO_LOG("Failed to write GPIO #%d direction file: %s\n", pin, strerror(errno)); + close(fd); + return PAL_STATUS_FAILURE; } close(fd); - usleep(100000); - return(0); + return PAL_STATUS_SUCCESS; } -static int -GPIOWrite(pal_linux_gpio_t* pin, int value) +static pal_status_t pal_gpio_write(pal_linux_gpio_t *pin, const char *value) { - if (write(pin->fd, value == LOW ? "0" : "1", 1) != 1) + ssize_t written = write(pin->fd, value, 1); + if (written == -1) { - // can't use printf, because it may execute in signal handler - int errsv = errno; - char err_msg[100]; - sprintf(err_msg, "Failed to write value! Erro code = %d, fd = %d\n", errsv, pin->fd); - write(STDERR_FILENO, err_msg, strlen(err_msg)); - return(-1); + PAL_GPIO_LOG("Failed to write GPIO pin #%d: %s\n", pin->pin_nr, strerror(errno)); + return PAL_STATUS_FAILURE; } - return(0); + return PAL_STATUS_SUCCESS; } -#define GPIO_VALUE_FMT_STR "/sys/class/gpio/gpio%d/value" - //lint --e{714,715} suppress "This function is used for to support multiple platforms " -pal_status_t pal_gpio_init(const pal_gpio_t * p_gpio_context) +pal_status_t pal_gpio_init(const pal_gpio_t *p_gpio_context) { -#define VALUE_MAX 30 - char path[VALUE_MAX] = {0}; - - if (p_gpio_context->p_gpio_hw != NULL) + pal_linux_gpio_t *gpio = (pal_linux_gpio_t *) p_gpio_context->p_gpio_hw; + if (gpio != NULL) { - pal_linux_gpio_t* gpio = p_gpio_context->p_gpio_hw; - int res_pin = gpio->pin_nr; - - /* - * Enable GPIO pins - */ - if (-1 == GPIOExport(res_pin)) - return(1); - - /* - * Set GPIO directions - */ - if (-1 == GPIODirection(res_pin, OUT)) - return(2); - - snprintf(path, VALUE_MAX, GPIO_VALUE_FMT_STR, res_pin); - int fd = open(path, O_WRONLY); - if (fd < 0) { - fprintf(stderr, "Failed to open gpio value for writing!\n"); - return(2); + int pin = gpio->pin_nr; + + pal_status_t status = pal_gpio_export(pin); + if (status != PAL_STATUS_SUCCESS) + return status; + + status = pal_gpio_direction(pin, OUT); + if (status != PAL_STATUS_SUCCESS) + return status; + + char path[48]; + int ret = snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin); + if (ret < 0 || ret == sizeof(path)) + { + PAL_GPIO_LOG("Failed to format GPIO value file\n"); + return PAL_STATUS_FAILURE; } - // store fd + int fd = open(path, O_WRONLY); + if (fd == -1) + { + PAL_GPIO_LOG("Failed to open GPIO value file for writing: %s\n", strerror(errno)); + return PAL_STATUS_I2C_BUSY; + } gpio->fd = fd; } @@ -174,43 +188,33 @@ pal_status_t pal_gpio_init(const pal_gpio_t * p_gpio_context) } //lint --e{714,715} suppress "This function is used for to support multiple platforms " -pal_status_t pal_gpio_deinit(const pal_gpio_t * p_gpio_context) +pal_status_t pal_gpio_deinit(const pal_gpio_t *p_gpio_context) { - if (p_gpio_context->p_gpio_hw != NULL) + pal_linux_gpio_t *gpio = (pal_linux_gpio_t *) p_gpio_context->p_gpio_hw; + if (gpio != NULL) { - pal_linux_gpio_t* gpio = (pal_linux_gpio_t*)(p_gpio_context->p_gpio_hw); - /* - * Disable GPIO pins - */ - if (-1 == GPIOUnexport(gpio->pin_nr)) - return(1); + pal_status_t status = pal_gpio_unexport(gpio->pin_nr); + if (status != PAL_STATUS_SUCCESS) + return status; close(gpio->fd); } - + return PAL_STATUS_SUCCESS; } -void pal_gpio_set_high(const pal_gpio_t * p_gpio_context) +void pal_gpio_set_high(const pal_gpio_t *p_gpio_context) { - if ((p_gpio_context != NULL) && (p_gpio_context->p_gpio_hw != NULL)) - { - /* - * Write GPIO value - */ - GPIOWrite((pal_linux_gpio_t*)(p_gpio_context->p_gpio_hw), HIGH ); - } + pal_linux_gpio_t *gpio = (pal_linux_gpio_t *) p_gpio_context->p_gpio_hw; + if (gpio != NULL) + pal_gpio_write(gpio, HIGH); } -void pal_gpio_set_low(const pal_gpio_t* p_gpio_context) +void pal_gpio_set_low(const pal_gpio_t *p_gpio_context) { - if ((p_gpio_context != NULL) && (p_gpio_context->p_gpio_hw != NULL)) - { - /* - * Write GPIO value - */ - GPIOWrite((pal_linux_gpio_t*)(p_gpio_context->p_gpio_hw), LOW); - } + pal_linux_gpio_t *gpio = (pal_linux_gpio_t *) p_gpio_context->p_gpio_hw; + if (gpio != NULL) + pal_gpio_write(gpio, LOW); } /** diff --git a/pal/linux/pal_linux.h b/pal/linux/pal_linux.h index 456e0012..141c02bf 100644 --- a/pal/linux/pal_linux.h +++ b/pal/linux/pal_linux.h @@ -37,15 +37,6 @@ #ifndef _PAL_LINUX_H_ #define _PAL_LINUX_H_ -#include "optiga/pal/pal.h" - -#define false 0 -#define true 1 - -#define HIGH 1 -#define LOW 0 -typedef uint16_t gpio_pin_t; - /** @brief PAL I2C context structure */ typedef struct pal_linux { @@ -58,7 +49,7 @@ typedef struct pal_linux } pal_linux_t; typedef struct pal_linux_gpio { - gpio_pin_t pin_nr; + int pin_nr; int fd; } pal_linux_gpio_t; From e2fa32f11d78424d5b52f3deb73736555b2b65b2 Mon Sep 17 00:00:00 2001 From: Sven Schwermer Date: Wed, 21 Apr 2021 08:15:24 +0200 Subject: [PATCH 3/3] pal: linux: Call PAL init functions Signed-off-by: Sven Schwermer --- pal/linux/pal.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pal/linux/pal.c b/pal/linux/pal.c index 15c5c963..d45f3621 100644 --- a/pal/linux/pal.c +++ b/pal/linux/pal.c @@ -34,7 +34,11 @@ * @{ */ +#include "optiga/optiga_lib_config.h" #include "optiga/pal/pal.h" +#include "optiga/pal/pal_gpio.h" +#include "optiga/pal/pal_ifx_i2c_config.h" +#include "optiga/pal/pal_os_timer.h" /** * @brief Initializes the PAL layer @@ -53,6 +57,24 @@ */ pal_status_t pal_init(void) { + pal_status_t status; + +#if OPTIGA_COMMS_DEFAULT_RESET_TYPE == 0 + status = pal_gpio_init(&optiga_vdd_0); + if (status != PAL_STATUS_SUCCESS) + return status; +#endif + +#if OPTIGA_COMMS_DEFAULT_RESET_TYPE == 0 || OPTIGA_COMMS_DEFAULT_RESET_TYPE == 2 + status = pal_gpio_init(&optiga_reset_0); + if (status != PAL_STATUS_SUCCESS) + return status; +#endif + + status = pal_timer_init(); + if (status != PAL_STATUS_SUCCESS) + return status; + return PAL_STATUS_SUCCESS; } @@ -73,6 +95,16 @@ pal_status_t pal_init(void) */ pal_status_t pal_deinit(void) { + pal_timer_deinit(); + +#if OPTIGA_COMMS_DEFAULT_RESET_TYPE == 0 || OPTIGA_COMMS_DEFAULT_RESET_TYPE == 2 + pal_gpio_deinit(&optiga_reset_0); +#endif + +#if OPTIGA_COMMS_DEFAULT_RESET_TYPE == 0 + pal_gpio_deinit(&optiga_vdd_0); +#endif + return PAL_STATUS_SUCCESS; }