From d7a26b4af6ff2b834790b0bf4d90d6de21944ba6 Mon Sep 17 00:00:00 2001 From: Michal Horn Date: Thu, 16 Jul 2015 09:41:00 +0200 Subject: [PATCH] Separate GPIO HAL interface from the code The implementation of the functions is moved to the source file, leaving only commented function prototypes of the interface functions in the header and hiding the internal functions in the source file. Move GPIO HAL functions definitions from the header file fo the source file Move functions comments from the source files to the header files --- rpp/include/hal/gpio.h | 130 +++++++++++++++++------------------- rpp/include/hal/port_gpio.h | 15 +++++ rpp/include/hal/port_spi.h | 7 ++ rpp/src/hal/gpio.c | 123 ++++++++++++++++++++++------------ rpp/src/hal/port_gpio.c | 14 ---- rpp/src/hal/port_spi.c | 12 ---- 6 files changed, 162 insertions(+), 139 deletions(-) diff --git a/rpp/include/hal/gpio.h b/rpp/include/hal/gpio.h index 5bc603f..fd82c65 100644 --- a/rpp/include/hal/gpio.h +++ b/rpp/include/hal/gpio.h @@ -35,52 +35,20 @@ extern gioPORT_t *port_id_map[MAX_PORT_CNT]; extern pin_map_element_t pin_map[MAX_PIN_CNT]; -/** - * Get pin descriptor assigned to pin name. - * @param[in] pin_name Pointer to string - the name of the pin. - * @param[in] len Length of the name, if terminated by '/0', then len=-1 - * @return Pin descriptor or NULL if not found - */ -static inline uint32_t *hal_gpio_pin_get_dsc(const char *pin_name, int len) -{ - uint32_t i; - const char *pin_name_ptr; - char pin_name_term[32]; - - if (len != -1) { // pin name not terminated by '\0' - strncpy(pin_name_term, pin_name, len); - pin_name_term[len] = '\0'; - pin_name_ptr = pin_name_term; - } - else pin_name_ptr = pin_name; - - for (i = 0; i < MAX_PIN_CNT; i++) { - if (strcmp(pin_name_ptr, pin_map[i].pin_name) == 0) - return &pin_map[i].pin_desc; - } - return NULL; -} /** * Get port base assigned to port number * @param[in] port_num Port number <0;4> * @return Pointer to port registers */ -static inline gioPORT_t *hal_gpio_get_port_base(uint32_t port_num) -{ - return port_id_map[port_num]; -} +gioPORT_t *hal_gpio_get_port_base(uint32_t port_num); /** * Get port number assigned to pin in its descriptor * @param[in] pin descriptor * @return Index of port */ -static inline uint32_t hal_gpio_pin_get_port_num(uint32_t pin_dsc) -{ - - return (pin_dsc & ~PORT_CONF_MASK) >> PORT_SHIFT; -} +uint32_t hal_gpio_pin_get_port_num(uint32_t pin_dsc); /** * Get port base from pin descriptor @@ -88,72 +56,99 @@ static inline uint32_t hal_gpio_pin_get_port_num(uint32_t pin_dsc) * @param[in] pin_dcs Pin descriptor * @return Pointer to port registers */ -static inline gioPORT_t *hal_gpio_pin_get_port_base(uint32_t pin_dsc) -{ - return hal_gpio_get_port_base(hal_gpio_pin_get_port_num(pin_dsc)); -} +gioPORT_t *hal_gpio_pin_get_port_base(uint32_t pin_dsc); + +/** + * Get pin descriptor assigned to pin name. + * @param[in] pin_name Pointer to string - the name of the pin. + * @param[in] len Length of the name, if terminated by '/0', then len=-1 + * @return Pin descriptor or NULL if not found + */ +uint32_t *hal_gpio_pin_get_dsc(const char *pin_name, int len); /** * Get value from GPIO pin * @param[in] pin_dsc pin descriptor * @return value read from specified gpio pin */ -static inline uint32_t hal_gpio_pin_get_value(uint32_t pin_dsc) -{ - return ((hal_gpio_pin_get_port_base(pin_dsc)->DIN) >> (pin_dsc & 0x1f)) & 1; -} +uint32_t hal_gpio_pin_get_value(uint32_t pin_dsc); /** * Set value to gpio pin * @param[in] pin_dsc pin descriptor * @param[in] value value to be assigned to the pin */ -static inline void hal_gpio_pin_set_value(uint32_t pin_dsc, uint32_t value) -{ - if (value) - hal_gpio_pin_get_port_base(pin_dsc)->DSET = 1 << (pin_dsc & 0x1f); - else - hal_gpio_pin_get_port_base(pin_dsc)->DCLR = 1 << (pin_dsc & 0x1f); -} +void hal_gpio_pin_set_value(uint32_t pin_dsc, uint32_t value); /** * Set pin direction to input * @param[in] pin_dsc pin descriptor * @return always 0 */ -static inline int hal_gpio_pin_direction_input(uint32_t pin_dsc) -{ - hal_gpio_pin_get_port_base(pin_dsc)->DIR &= ~(1 << (pin_dsc & 0x1f)); - return 0; -} +int hal_gpio_pin_direction_input(uint32_t pin_dsc); /** * Set pin direction to output * @param[in] pin_dsc pin descriptor * @return always 0 */ -static inline int hal_gpio_pin_direction_output(uint32_t pin_dsc, uint32_t value) -{ - hal_gpio_pin_set_value(pin_dsc, value); - hal_gpio_pin_get_port_base(pin_dsc)->DIR |= (1 << (pin_dsc & 0x1f)); - return 0; -} +int hal_gpio_pin_direction_output(uint32_t pin_dsc, uint32_t value); /** * Get pin direction * @param[in] pin_dsc pin descriptor * @return 1 - output, 0 - input */ -static inline int hal_gpio_pin_get_direction(uint32_t pin_dsc) -{ - return (hal_gpio_pin_get_port_base(pin_dsc)->DIR >> (pin_dsc & 0x1f)) & 1; -} - +int hal_gpio_pin_get_direction(uint32_t pin_dsc); +/** + * Set pin as pull down or pull up and pull resistor enabled or disabled. + * @param[in] pin_dsc Descriptor of the pin + * @param[in] Mode on which pin will be configured to. + * PORT_CONF_MODE_PU - pull up + * PORT_CONF_MODE_PD - pull down + * must be | with + * PORT_CONF_MODE_PEN - pull resistor enable + * PORT_CONF_MODE_PDIS - pull resistor disable + * @return always 0 + */ uint32_t hal_gpio_pin_conf_mode(uint32_t pin_dsc, uint32_t mode); +/** + * Configure pin to be open drain or not + * @param[in] pin_dsc Descriptor of the pin + * @param[in] Mode on which pin will be configured to. + * PORT_CONF_OD_OFF - open-drain disabled + * PORT_CONF_OD_ON - open drain enabled + * + * @return always 0 + */ uint32_t hal_gpio_pin_conf_od(uint32_t pin_dsc, uint32_t od); +/** + * Configure pin + * @param[in] pin_dsc Descriptor of the pin + * @param[in] Mode on which pin will be configured to. + * PORT_CONF_OD_OFF - open-drain disabled + * PORT_CONF_OD_ON - open drain enabled + * + * PORT_CONF_MODE_PU - pull up + * PORT_CONF_MODE_PD - pull down + * + * PORT_CONF_MODE_PEN - pull resistor enable + * PORT_CONF_MODE_PDIS - pull resistor disable + * + * PORT_CONF_DIR_IN - direction input + * PORT_CONF_DIR_OUT - direction output + * + * PORT_CONF_INIT_LOW - init value 0 + * PORT_CONF_INIT_HIGH - init value 1 + * + * PORT_CONF_FNC_GPIO - port function GPIO + * PORT_CONF_FNC_FNCX - port alternate function X + * + * @return always 0 + */ uint32_t hal_gpio_pin_conf_set(uint32_t pin_dsc, uint32_t conf); /** @@ -161,10 +156,7 @@ uint32_t hal_gpio_pin_conf_set(uint32_t pin_dsc, uint32_t conf); * @param[in] pin_dsc pin descriptor * @return always 0; */ -static inline uint32_t hal_gpio_pin_conf(uint32_t pin_dsc) -{ - return hal_gpio_pin_conf_set(pin_dsc, pin_dsc); -} +uint32_t hal_gpio_pin_conf(uint32_t pin_dsc); #endif //_HAL_GPIO_H_ diff --git a/rpp/include/hal/port_gpio.h b/rpp/include/hal/port_gpio.h index f2d38d7..a3d5277 100644 --- a/rpp/include/hal/port_gpio.h +++ b/rpp/include/hal/port_gpio.h @@ -14,7 +14,22 @@ //#include "sys_common.h" #include "hal/hal.h" +/** + * Get values of all pins of given port. + * @param[in] config Pointer to array of pin descriptors + * @param[in] num_val Number of pins assigned to the port + * @param[out] values Stored values of all pins of the port. 1st bit -> pin0, 2nd bit -> pin1... + * @return always 0 + */ uint32_t hal_gio_port_get_val(uint32_t *config, uint32_t num_val, uint32_t *values); + +/** + * Set values to all pins of given port. + * @param[in] config Pointer to array of pin descriptors + * @param[in] num_val Number of pins assigned to the port + * @param[in] values Stored values of all pins of the port. 1st bit -> pin0, 2nd bit -> pin1... + * @return always 0 + */ uint32_t hal_gio_port_set_val(uint32_t *config, uint32_t num_val, const uint32_t *values); #endif /* PORT_GPIO_H_ */ diff --git a/rpp/include/hal/port_spi.h b/rpp/include/hal/port_spi.h index ad56907..f08c4e1 100644 --- a/rpp/include/hal/port_spi.h +++ b/rpp/include/hal/port_spi.h @@ -13,6 +13,13 @@ //#include "sys_common.h" #include "hal/hal.h" +/** + * Transfer command through the spi + * @param[in] config Address of the SPI + * @param[in] num_bytes Number of bytes to be trasfered + * @param[in] commands SPI command to be sent + * @return spi response + */ uint32_t hal_spi_port_transfer_command(uint32_t *config, uint32_t num_bytes, const uint32_t *commands); diff --git a/rpp/src/hal/gpio.c b/rpp/src/hal/gpio.c index 5a7ec12..0912b8a 100644 --- a/rpp/src/hal/gpio.c +++ b/rpp/src/hal/gpio.c @@ -21,17 +21,78 @@ #include "hal/hal.h" -/** - * Set pin as pull down or pull up and pull resistor enabled or disabled. - * @param[in] pin_dsc Descriptor of the pin - * @param[in] Mode on which pin will be configured to. - * PORT_CONF_MODE_PU - pull up - * PORT_CONF_MODE_PD - pull down - * must be | with - * PORT_CONF_MODE_PEN - pull resistor enable - * PORT_CONF_MODE_PDIS - pull resistor disable - * @return always 0 - */ +gioPORT_t *hal_gpio_get_port_base(uint32_t port_num) +{ + return port_id_map[port_num]; +} + +uint32_t hal_gpio_pin_get_port_num(uint32_t pin_dsc) +{ + + return (pin_dsc & ~PORT_CONF_MASK) >> PORT_SHIFT; +} + +gioPORT_t *hal_gpio_pin_get_port_base(uint32_t pin_dsc) +{ + return hal_gpio_get_port_base(hal_gpio_pin_get_port_num(pin_dsc)); +} + + +uint32_t *hal_gpio_pin_get_dsc(const char *pin_name, int len) +{ + uint32_t i; + const char *pin_name_ptr; + char pin_name_term[32]; + + if (len != -1) { // pin name not terminated by '\0' + strncpy(pin_name_term, pin_name, len); + pin_name_term[len] = '\0'; + pin_name_ptr = pin_name_term; + } + else pin_name_ptr = pin_name; + + for (i = 0; i < MAX_PIN_CNT; i++) { + if (strcmp(pin_name_ptr, pin_map[i].pin_name) == 0) + return &pin_map[i].pin_desc; + } + return NULL; +} + + +uint32_t hal_gpio_pin_get_value(uint32_t pin_dsc) +{ + return ((hal_gpio_pin_get_port_base(pin_dsc)->DIN) >> (pin_dsc & 0x1f)) & 1; +} + + +void hal_gpio_pin_set_value(uint32_t pin_dsc, uint32_t value) +{ + if (value) + hal_gpio_pin_get_port_base(pin_dsc)->DSET = 1 << (pin_dsc & 0x1f); + else + hal_gpio_pin_get_port_base(pin_dsc)->DCLR = 1 << (pin_dsc & 0x1f); +} + + +int hal_gpio_pin_direction_input(uint32_t pin_dsc) +{ + hal_gpio_pin_get_port_base(pin_dsc)->DIR &= ~(1 << (pin_dsc & 0x1f)); + return 0; +} + +int hal_gpio_pin_direction_output(uint32_t pin_dsc, uint32_t value) +{ + hal_gpio_pin_set_value(pin_dsc, value); + hal_gpio_pin_get_port_base(pin_dsc)->DIR |= (1 << (pin_dsc & 0x1f)); + return 0; +} + +int hal_gpio_pin_get_direction(uint32_t pin_dsc) +{ + return (hal_gpio_pin_get_port_base(pin_dsc)->DIR >> (pin_dsc & 0x1f)) & 1; +} + + uint32_t hal_gpio_pin_conf_mode(uint32_t pin_dsc, uint32_t mode) { gioPORT_t *gioPort = hal_gpio_pin_get_port_base(pin_dsc); @@ -47,15 +108,7 @@ uint32_t hal_gpio_pin_conf_mode(uint32_t pin_dsc, uint32_t mode) return 0; } -/** - * Configure pin to be open drain or not - * @param[in] pin_dsc Descriptor of the pin - * @param[in] Mode on which pin will be configured to. - * PORT_CONF_OD_OFF - open-drain disabled - * PORT_CONF_OD_ON - open drain enabled - * - * @return always 0 - */ + uint32_t hal_gpio_pin_conf_od(uint32_t pin_dsc, uint32_t od) { gioPORT_t *gioPort = hal_gpio_pin_get_port_base(pin_dsc); @@ -67,30 +120,7 @@ uint32_t hal_gpio_pin_conf_od(uint32_t pin_dsc, uint32_t od) return 0; } -/** - * Configure pin - * @param[in] pin_dsc Descriptor of the pin - * @param[in] Mode on which pin will be configured to. - * PORT_CONF_OD_OFF - open-drain disabled - * PORT_CONF_OD_ON - open drain enabled - * - * PORT_CONF_MODE_PU - pull up - * PORT_CONF_MODE_PD - pull down - * - * PORT_CONF_MODE_PEN - pull resistor enable - * PORT_CONF_MODE_PDIS - pull resistor disable - * - * PORT_CONF_DIR_IN - direction input - * PORT_CONF_DIR_OUT - direction output - * - * PORT_CONF_INIT_LOW - init value 0 - * PORT_CONF_INIT_HIGH - init value 1 - * - * PORT_CONF_FNC_GPIO - port function GPIO - * PORT_CONF_FNC_FNCX - port alternate function X - * - * @return always 0 - */ + uint32_t hal_gpio_pin_conf_set(uint32_t pin_dsc, uint32_t conf) { pin_dsc &= ~PORT_CONF_MASK; @@ -105,3 +135,8 @@ uint32_t hal_gpio_pin_conf_set(uint32_t pin_dsc, uint32_t conf) return 0; } + +uint32_t hal_gpio_pin_conf(uint32_t pin_dsc) +{ + return hal_gpio_pin_conf_set(pin_dsc, pin_dsc); +} diff --git a/rpp/src/hal/port_gpio.c b/rpp/src/hal/port_gpio.c index 79e990e..71efad2 100644 --- a/rpp/src/hal/port_gpio.c +++ b/rpp/src/hal/port_gpio.c @@ -16,13 +16,6 @@ #include "hal/hal.h" -/** - * Get values of all pins of given port. - * @param[in] config Pointer to array of pin descriptors - * @param[in] num_val Number of pins assigned to the port - * @param[out] values Stored values of all pins of the port. 1st bit -> pin0, 2nd bit -> pin1... - * @return always 0 - */ uint32_t hal_gio_port_get_val(uint32_t *config, uint32_t num_val, uint32_t *values) { uint32_t i; @@ -33,13 +26,6 @@ uint32_t hal_gio_port_get_val(uint32_t *config, uint32_t num_val, uint32_t *valu return 0; } -/** - * Set values to all pins of given port. - * @param[in] config Pointer to array of pin descriptors - * @param[in] num_val Number of pins assigned to the port - * @param[in] values Stored values of all pins of the port. 1st bit -> pin0, 2nd bit -> pin1... - * @return always 0 - */ uint32_t hal_gio_port_set_val(uint32_t *config, uint32_t num_val, const uint32_t *values) { uint32_t i; diff --git a/rpp/src/hal/port_spi.c b/rpp/src/hal/port_spi.c index 21fbe63..f1cb47e 100644 --- a/rpp/src/hal/port_spi.c +++ b/rpp/src/hal/port_spi.c @@ -14,10 +14,6 @@ * This file contains getter and setter functions for SPI port type. */ -//#include "hal_port_spi.h" -// Cannot include upper layer -//#include "drv_spi.h" -//#include "cmdproc.h" #include "hal/spi.h" #define PORT_BUF 4 @@ -26,14 +22,6 @@ uint8_t spi_port_buf_tx[PORT_BUF]; /** Buffer for spi response */ uint8_t spi_port_buf_rx[PORT_BUF]; - -/** - * Transfer command through the spi - * @param[in] config Address of the SPI - * @param[in] num_bytes Number of bytes to be trasfered - * @param[in] commands SPI command to be sent - * @return spi response - */ uint32_t hal_spi_port_transfer_command(uint32_t *config, uint32_t num_bytes, const uint32_t *commands) { spi_drv_t *ifc; -- 2.39.2