Skip to content

Commit

Permalink
v0.2: added GPIO_ex
Browse files Browse the repository at this point in the history
  • Loading branch information
SMFSW committed Apr 18, 2017
1 parent 3344757 commit ad54b37
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 9 deletions.
41 changes: 41 additions & 0 deletions GPIO_ex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*!\file GPIO_ex.c
** \author SMFSW
** \version v0.2
** \date 2017
** \copyright MIT (c) 2017, SMFSW
** \brief Simple extension for GPIOs
**/
#include <string.h>

#include "GPIO_ex.h"

#define MAX_PINS_PORT 16


int str_GPIO_name(char * name, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
const char *port, prt[][7] = { "GPIOA", "GPIOB", "GPIOC", "GPIOD", "GPIOF", "GPIO?" };
char pin;

if (!name) { return -1; } // pointer for storage is not defined

// Find port comparing address
if (GPIOx == GPIOA) { port = prt[0]; }
else if (GPIOx == GPIOB) { port = prt[1]; }
else if (GPIOx == GPIOC) { port = prt[2]; }
else if (GPIOx == GPIOD) { port = prt[3]; }
else if (GPIOx == GPIOF) { port = prt[4]; }
else { port = prt[5]; }

// Find pin shifting values to get pin index
for (pin = 0 ; pin < MAX_PINS_PORT ; pin++)
{
if (1U << pin == GPIO_Pin)
{
sprintf(name, "%s%i", port, pin);
return 0; // Match, return 0
}
}
sprintf(name, "%s%s", port, "xx");
return -1; // No match, return -1
}
72 changes: 72 additions & 0 deletions GPIO_ex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*!\file GPIO_ex.h
** \author SMFSW
** \version v0.2
** \date 2017
** \copyright MIT (c) 2017, SMFSW
** \brief Simple extension for GPIOs
**/
/****************************************************************/
#ifndef __GPIO_EX_H
#define __GPIO_EX_H
/****************************************************************/
#include <string.h>

#include "sarmfsw.h"
#include CMSIS_INC

/*!\enum enActOut
** \brief Enum des pilotages possibles de sorties logiques
**/
typedef enum enActOut{
Reset = 0, //!< Reset Output
Set, //!< Set Output
Toggle //!< Toggle Output
} ActOut;


int str_GPIO_name(char * name, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);


/*!\brief Write GPIO
** \param[in] GPIOx - port to write to
** \param[in] GPIO_Pin - pin to write to
** \param[in] Act - type of write
** \return Nothing
**/
__INLINE void __attribute__((always_inline)) write_GPIO(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, ActOut Act)
{
if (Act > Toggle) { return; }
else
{
if (Act == Reset) { HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_RESET); }
if (Act == Set) { HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_SET); }
if (Act == Toggle) { HAL_GPIO_TogglePin(GPIOx, GPIO_Pin); }
#if defined(VERBOSE)
char port[10] = "";
str_GPIO_name(port, GPIOx, GPIO_Pin);
printf("Written %s to %u at: %lums\n", port, HAL_GPIO_ReadPin(GPIOx, GPIO_Pin), HAL_GetTick());
#endif
}
}

/*!\brief Read GPIO
** \param[in] GPIOx - port to read from
** \param[in] GPIO_Pin - pin to read from
** \return Nothing
**/
__INLINE GPIO_PinState __attribute__((always_inline)) read_GPIO(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
#if defined(VERBOSE)
GPIO_PinState pin = HAL_GPIO_ReadPin(GPIOx, GPIO_Pin);
char port[10] = "";
str_GPIO_name(port, GPIOx, GPIO_Pin);
printf("Read %s is %u at: %lums\n", port, pin, HAL_GetTick());
return pin;
#else
return HAL_GPIO_ReadPin(GPIOx, GPIO_Pin);
#endif
}

/****************************************************************/
#endif /* __GPIO_EX_H */
/****************************************************************/
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified PWM.c
100644 → 100755
Empty file.
Empty file modified PWM.h
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
10 changes: 1 addition & 9 deletions stdream_rdir.c
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!\file stream_redirect.c
** \author SMFSW
** \version v0.1
** \version v0.2
** \date 2017
** \copyright MIT (c) 2017, SMFSW
** \brief Stream redirection
Expand Down Expand Up @@ -41,7 +41,6 @@ __STATIC_INLINE void __attribute__((always_inline)) uart_prt(char* ptr, int len)

int uart_printf(char *string, ...)
{
#if defined(IS_DEBUG)
va_list args;

va_start(args, string);
Expand All @@ -51,23 +50,16 @@ int uart_printf(char *string, ...)
buf_stream[0] = '\0'; // Erase string

return 0;
#else
return -1;
#endif
}


int uart_vprintf(char *string, va_list args)
{
#if defined(IS_DEBUG)
vsprintf(buf_stream, string, args);
uart_prt(buf_stream, strlen(buf_stream));
buf_stream[0] = '\0'; // Erase string

return 0;
#else
return -1;
#endif
}


Expand Down
Empty file modified stdream_rdir.h
100644 → 100755
Empty file.

0 comments on commit ad54b37

Please sign in to comment.