Commit d8ebfa0f authored by Mikaël BRIDAY's avatar Mikaël BRIDAY

add pinAlt function

parent 6ee7fb53
......@@ -88,3 +88,44 @@ unsigned char digitalRead(GPIO_TypeDef *port,
return 0;
}
unsigned char pinAlt(GPIO_TypeDef *port,
uint32_t numBit,
uint32_t AFId)
{
if(!IS_GPIO_ALL_INSTANCE(port)) return 0xFF;
if(numBit > 15) return 0xFF;
if(AFId > 15) return 0xFF;
//clock
clockForGpio(port);
//set MODER to 10
uint32_t mask2Bits = (3U << (numBit*2));
port->MODER &= ~mask2Bits;
port->MODER |= (2U<<(numBit*2));
//set alternate function
uint32_t shift;
int AFRReg;
if(numBit < 8U) //AFRL
{
shift = numBit<<2;
AFRReg = 0;
} else { //AFRH
shift = (numBit-8U)<<2;
AFRReg = 1;
}
port->AFR[AFRReg] &= ~(0xfU<<shift);
port->AFR[AFRReg] |= (AFId<<shift);
return 0;
}
unsigned char pinAnalog(GPIO_TypeDef *port, uint32_t numBit)
{
uint32_t mask2Bits; //mask for 2bit fields
if(!IS_GPIO_ALL_INSTANCE(port)) return 0xFF;
if(numBit > 15) return 0xFF;
//clock
clockForGpio(port);
port->MODER |= (3U<<(numBit*2)); //analog config
mask2Bits = (3U << (numBit*2));
port->PUPDR &= ~mask2Bits; //remove pull-up/down
return 0;
}
......@@ -3,6 +3,10 @@
#include "stm32f3xx.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
//mode
#define DISABLE 0
#define OUTPUT 1
......@@ -10,32 +14,59 @@
#define INPUT_PULLUP 3
#define INPUT_PULLDOWN 4
//configure a pin
// - port is GPIOA, GPIOB, ...
// - numBit is the pin number (0 to 15)
// - mode is in DISABLE, OUTPUT, INPUT, ...)
/* configure a pin
* - port is GPIOA, GPIOB, ...
* - numBit is the pin number (0 to 15)
* - mode is in DISABLE, OUTPUT, INPUT, ...)
*/
int pinMode(GPIO_TypeDef *port,
unsigned char numBit,
unsigned char mode);
//set a pin, previously configured as OUTPUT
// high state if 'value' is different from 0
// low state if 'value' is 0.
/* set a pin, previously configured as OUTPUT
* high state if 'value' is different from 0
* low state if 'value' is 0.
*/
void digitalWrite(GPIO_TypeDef *port,
unsigned char numBit,
unsigned int value);
//toggle output state of a pin
// 0 => 1
// 1 => 0
/* toggle output state of a pin
* 0 => 1
* 1 => 0
*/
void digitalToggle(GPIO_TypeDef *port,
unsigned char numBit);
//read the state of a pin, previously configured as input.
//returns
// - 0xFF in case of error (bad arguments)
// - 0 or 1, depending on pin state.
/* read the state of a pin, previously configured as input.
* returns
* - 0xFF in case of error (bad arguments)
* - 0 or 1, depending on pin state.
*/
unsigned char digitalRead(GPIO_TypeDef *port,
unsigned char numBit);
/* Activate the alternate function in parameter for the
* pin.
* \param AFId the alternate function id, between 0 and 15
* \return
* - 0xFF in case of error (bad arguments)
*/
unsigned char pinAlt(GPIO_TypeDef *port,
uint32_t numBit,
uint32_t AFId);
/* Activate the analog mode for a pin
* Warning: it does not configure the ADC!
* \return
* - 0xFF in case of error (bad arguments)
* - 0 in case of success.
*/
unsigned char pinAnalog(GPIO_TypeDef *port, uint32_t numBit);
#ifdef __cplusplus
}
#endif /* __cpusplus */
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment