Skip to content

Commit

Permalink
v0.5: GPIO_in addition and Doxygen
Browse files Browse the repository at this point in the history
  • Loading branch information
SMFSW committed May 8, 2017
1 parent ea92c01 commit efb5989
Show file tree
Hide file tree
Showing 13 changed files with 2,693 additions and 68 deletions.
2,443 changes: 2,443 additions & 0 deletions Doxyfile

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions FctERR.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/*!\file FctERR.c
** \author SMFSW
** \version v0.4
** \version v0.5
** \date 2017
** \copyright MIT (c) 2017, SMFSW
** \brief errors to SMFSW FctERR code
**/
/****************************************************************/
/****************************************************************/

#include "FctERR.h"


FctERR HALERRtoFCTERR(HAL_StatusTypeDef Status)
FctERR HALERRtoFCTERR(HAL_StatusTypeDef st)
{
if (Status == HAL_OK) return ERR_OK;
else if (Status == HAL_ERROR) return ERR_FAULT;
else if (Status == HAL_BUSY) return ERR_BUSY;
else if (Status == HAL_TIMEOUT) return ERR_TIMEOUT;
else return ERR_FAULT;
if (st == HAL_OK) return ERR_OK;
else if (st == HAL_ERROR) return ERR_FAULT;
else if (st == HAL_BUSY) return ERR_BUSY;
else if (st == HAL_TIMEOUT) return ERR_TIMEOUT;
else return ERR_FAULT;
}
20 changes: 15 additions & 5 deletions FctERR.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!\file FctERR.h
** \author SMFSW
** \version v0.4
** \version v0.5
** \date 2017
** \copyright MIT (c) 2017, SMFSW
** \brief errors to SMFSW FctERR declarations
Expand All @@ -14,15 +14,18 @@
/****************************************************************/


// *****************************************************************************
// Section: Types
// *****************************************************************************
/*!\enum EnumFctERR
** \brief Enum of high level functions return state
**/
typedef enum __attribute__((__packed__)) EnumFctERR{
typedef enum PACK__ EnumFctERR{
ERR_OK = 0U, //!< OK
ERR_SPEED = 1U, //!< This device does not work in the active speed mode.
ERR_RANGE = 2U, //!< Parameter out of range.
ERR_VALUE = 3U, //!< Parameter of incorrect value.
ERR_OVERFLOW = 4U, //!< Timer overflow.
ERR_OVERFLOW = 4U, //!< Overflow.
ERR_MATH = 5U, //!< Overflow during evaluation.
ERR_ENABLED = 6U, //!< Device is enabled.
ERR_DISABLED = 7U, //!< Device is disabled.
Expand Down Expand Up @@ -52,10 +55,17 @@ typedef enum __attribute__((__packed__)) EnumFctERR{
ERR_NOTIMPLEM = 31U, //!< Function not implemented error
ERR_MEMORY = 32U, //!< Memory error
ERR_INSTANCE = 33U //!< Instance error
}FctERR;
} FctERR;


FctERR HALERRtoFCTERR(HAL_StatusTypeDef Status);
// *****************************************************************************
// Section: Interface Routines
// *****************************************************************************
/*!\brief Convert HAL_StatusTypeDef to FctERR
** \param[in] st - HAL_StatusTypeDef status
** \return FctERR status
**/
FctERR HALERRtoFCTERR(HAL_StatusTypeDef st);


/****************************************************************/
Expand Down
45 changes: 40 additions & 5 deletions GPIO_ex.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/*!\file GPIO_ex.c
** \author SMFSW
** \version v0.3
** \version v0.5
** \date 2017
** \copyright MIT (c) 2017, SMFSW
** \brief Simple extension for GPIOs
**/
/****************************************************************/
/****************************************************************/

#include <string.h>
#include <assert.h>

Expand All @@ -13,13 +16,45 @@
#define MAX_PINS_PORT 16


int str_GPIO_name(char * name, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
void GPIO_in_init(GPIO_in * in, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, uint16_t filter)
{
/* Check the parameters */
assert_param(IS_GPIO_PIN(GPIO_Pin));

in->cfg.GPIOx = GPIOx;
in->cfg.GPIO_Pin = GPIO_Pin;
in->cfg.filt = filter;
}


void GPIO_in_handler(GPIO_in * in)
{
if (in->in == in->mem) { in->edge = NoEdge; }
else if (in->in > in->mem) { in->edge = Rising; }
else { in->edge = Falling; }

in->mem = in->in;
if (!HAL_GPIO_ReadPin(in->cfg.GPIOx, in->cfg.GPIO_Pin))
{
if (TPSSUP_MS(in->hIn, in->cfg.filt))
{
if (!in->done) { in->done = in->in = true; }
}
}
else
{
in->done = in->in = false;
in->hIn = HAL_GetTick();
}
}


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

/* Check the parameters */
assert_param(name);
assert_param(IS_GPIO_PIN(GPIO_Pin));

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

Expand Down Expand Up @@ -54,9 +89,9 @@ int str_GPIO_name(char * name, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
if (1U << pin == GPIO_Pin)
{
sprintf(name, "%s%i", port, pin);
return 0; // Match, return 0
return ERR_OK; // Match
}
}
sprintf(name, "%s%s", port, "xx");
return -1; // No match, return -1
return ERR_VALUE; // No match
}
93 changes: 81 additions & 12 deletions GPIO_ex.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,93 @@
/*!\file GPIO_ex.h
** \author SMFSW
** \version v0.2
** \version v0.5
** \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
#include "FctERR.h"
/****************************************************************/


// *****************************************************************************
// Section: Types
// *****************************************************************************
/*!\enum ActOut
** \brief Logic output possible actions enumeration
**/
typedef enum enActOut{
typedef enum ActOut{
Reset = 0, //!< Reset Output
Set, //!< Set Output
Toggle //!< Toggle Output
} ActOut;
} eActOut;


/*!\struct GPIO_in
** \brief GPIO input structure
**/
typedef struct GPIO_in {
bool in; //!< Input value
eEdge edge; //!< Input edge
/*pvt*/
bool mem; //!< Memo value
bool done; //!< State change done
uint32_t hIn; //!< Filter time
struct {
GPIO_TypeDef * GPIOx; //!< HAL GPIO instance
uint16_t GPIO_Pin; //!< HAL GPIO pin
uint16_t filt; //!< Filter time (ms)
} cfg;
} GPIO_in;


// *****************************************************************************
// Section: Interface Routines
// *****************************************************************************
/*!\brief Initialize GPIO_in instance
** \param[in,out] in - input instance to initialize
** \param[in] GPIOx - port to write to
** \param[in] GPIO_Pin - pin to write to
** \param[in] filter - input filtering time
** \return Nothing
**/
void GPIO_in_init(GPIO_in * in, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, uint16_t filter);

/*!\brief Handles GPIO_in read and treatment
** \param[in,out] in - input instance to handle
** \return Nothing
**/
void GPIO_in_handler(GPIO_in * in);

/*!\brief Get GPIO_in input value
** \param[in] in - input instance
** \return Input value
**/
__INLINE bool INLINE__ get_GPIO_in(GPIO_in * in) { return in->in; }

/*!\brief Get GPIO_in input edge
** \param[in] in - input instance
** \return Input edge
**/
__INLINE bool INLINE__ get_GPIO_in_edge(GPIO_in * in) { return in->edge; }


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

/*!\brief Get name from Port, Pin
** \param[in,out] name - pointer to string for name
** \param[in] GPIOx - port to write to
** \param[in] GPIO_Pin - pin to write to
** \return Error code
**/
FctERR str_GPIO_name(char * name, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);


/*!\brief Write GPIO
Expand All @@ -33,8 +96,11 @@ int str_GPIO_name(char * name, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
** \param[in] Act - type of write
** \return Nothing
**/
__INLINE void __attribute__((always_inline)) write_GPIO(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, ActOut Act)
__INLINE void INLINE__ write_GPIO(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, eActOut Act)
{
/* Check the parameters */
assert_param(IS_GPIO_PIN(GPIO_Pin));

if (Act > Toggle) { return; }
else
{
Expand All @@ -44,23 +110,26 @@ __INLINE void __attribute__((always_inline)) write_GPIO(GPIO_TypeDef* GPIOx, uin
#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());
printf("Written %s to %u (%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
** \return Pin state
**/
__INLINE GPIO_PinState __attribute__((always_inline)) read_GPIO(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
__INLINE GPIO_PinState INLINE__ read_GPIO(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
/* Check the parameters */
assert_param(IS_GPIO_PIN(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());
printf("Read %s is %u (%lums)\n", port, pin, HAL_GetTick());
return pin;
#else
return HAL_GPIO_ReadPin(GPIOx, GPIO_Pin);
Expand Down
Binary file added HARMcksL_v0_5.pdf
Binary file not shown.
30 changes: 19 additions & 11 deletions PWM.c
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
/*!\file PWM.c
** \author SMFSW
** \version v0.1
** \version v0.5
** \date 2017
** \copyright MIT (c) 2017, SMFSW
** \brief Simple PWM handling
**/
/****************************************************************/
/****************************************************************/

#include "PWM.h"

#if defined(HAL_TIM_MODULE_ENABLED)


HAL_StatusTypeDef set_PWM_Freq(TIM_HandleTypeDef * pTim, uint32_t Freq)
HAL_StatusTypeDef set_PWM_Freq(TIM_HandleTypeDef * pTim, uint32_t freq)
{
const uint32_t coreCLK = HAL_RCC_GetHCLKFreq();
uint32_t per, i;

/* Check the parameters */
assert_param(IS_TIM_INSTANCE(pTim->Instance));

if (Freq > coreCLK / 100) { return HAL_ERROR; }
if (freq > coreCLK / 100) { return HAL_ERROR; }

// TODO: find prescaler & period with i++ instead of shifts
for (i = 1 ; i < (uint16_t) -1 ; i <<= 1)
{
per = (coreCLK / (Freq * (i + 1))) - 1;
per = (coreCLK / (freq * (i + 1))) - 1;
if (per <= (uint16_t) -1) { break; } // If in 16b range
if (i == 1 << 15) { return HAL_ERROR; } // If nothing has been found (last iteration)
}
Expand All @@ -40,26 +42,32 @@ HAL_StatusTypeDef set_PWM_Freq(TIM_HandleTypeDef * pTim, uint32_t Freq)
}

/*** PWM DRIVING ***/
__STATIC_INLINE HAL_StatusTypeDef set_PWM_Duty(TIM_HandleTypeDef * pTim, uint32_t Chan, uint16_t CCR_val)
/*!\brief Low level TIM module PWM duty cycle write
** \param[in,out] pTim - pointer to TIM instance for PWM generation
** \param[in] chan - Channel to write
** \param[in] CCR_val - Scaled duty cycle for CCR register
** \return HAL Status
**/
__STATIC_INLINE HAL_StatusTypeDef INLINE__ write_CCR(TIM_HandleTypeDef * pTim, uint32_t chan, uint16_t CCR_val)
{
__IO uint32_t * pCCR; // __IO means volatile for R/W

/* Check the parameters */
assert_param(IS_TIM_INSTANCE(pTim->Instance));
assert_param(IS_TIM_CCX_INSTANCE(pTim->Instance, Chan));
assert_param(IS_TIM_CCX_INSTANCE(pTim->Instance, chan));

if (Chan <= TIM_CHANNEL_4) { pCCR = &pTim->Instance->CCR1 + (Chan / 4); }
else if (Chan <= TIM_CHANNEL_6) { pCCR = &pTim->Instance->CCR5 + (Chan / 4) - 4; }
if (chan <= TIM_CHANNEL_4) { pCCR = &pTim->Instance->CCR1 + (chan / 4); }
else if (chan <= TIM_CHANNEL_6) { pCCR = &pTim->Instance->CCR5 + (chan / 4) - 4; }
else { return HAL_ERROR; }

*pCCR = CCR_val;
return HAL_OK;
}

HAL_StatusTypeDef set_PWM_Duty_Scaled(TIM_HandleTypeDef * pTim, uint32_t Chan, uint16_t Duty, uint16_t Scale)
HAL_StatusTypeDef set_PWM_Duty_Scaled(TIM_HandleTypeDef * pTim, uint32_t chan, uint16_t duty, uint16_t scale)
{
float tmp = ((float) min(Scale, Duty) / (float) Scale) * pTim->Instance->ARR;
return set_PWM_Duty(pTim, Chan, (uint16_t) tmp);
float tmp = ((float) min(scale, duty) / (float) scale) * pTim->Instance->ARR;
return write_CCR(pTim, chan, (uint16_t) tmp);
}


Expand Down
Loading

0 comments on commit efb5989

Please sign in to comment.