Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'master' of https://github.com/bubulindo/Arduino_STM32 i…
…nto bubulindo-master
  • Loading branch information
rogerclarkmelbourne committed Sep 26, 2016
2 parents 1641d24 + 0b34af3 commit 9147a46
Show file tree
Hide file tree
Showing 32 changed files with 1,313 additions and 40 deletions.
22 changes: 22 additions & 0 deletions STM32F1/cores/maple/HardwareTimer.cpp 100644 → 100755
Expand Up @@ -142,6 +142,28 @@ void HardwareTimer::refresh(void) {
timer_generate_update(this->dev);
}

/* CARLOS Changes to add encoder mode.*/

//direction of movement. (to be better described).
uint8 HardwareTimer::getDirection(){
return get_direction(this->dev);
}

//set if the encoder will count edges on one, which or both channels.
void HardwareTimer::setEdgeCounting(uint32 counting) {
(dev->regs).gen->SMCR = counting;//TIMER_SMCR_SMS_ENCODER3; //choose encoder 3, counting on

}

uint8 HardwareTimer::getEdgeCounting() {
return (dev->regs).gen->SMCR;
}



//set the polarity of counting... not sure how interesting this is..
void HardwareTimer::setPolarity(){}

/* -- Deprecated predefined instances -------------------------------------- */

HardwareTimer Timer1(1);
Expand Down
21 changes: 21 additions & 0 deletions STM32F1/cores/maple/HardwareTimer.h 100644 → 100755
Expand Up @@ -38,6 +38,10 @@
/** Timer mode. */
typedef timer_mode TimerMode;

//CARLOS
//defines for the ENCODER mode.


/**
* @brief Interface to one of the 16-bit timer peripherals.
*/
Expand Down Expand Up @@ -205,6 +209,23 @@ class HardwareTimer {
*/
void refresh(void);

//CARLOS.
/*
added these functions to make sense for the encoder mode.
*/
//direction of movement. (to be better described).
uint8 getDirection();

//set if the encoder will count edges on one, which or both channels.
void setEdgeCounting(uint32 counting);
uint8 getEdgeCounting(); //not sure if needed.

//set the polarity of counting... not sure how interesting this is..
void setPolarity();

//add the filtering definition for the input channel.


/* Escape hatch */

/**
Expand Down
8 changes: 4 additions & 4 deletions STM32F1/cores/maple/libmaple/adc.c 100644 → 100755
Expand Up @@ -44,7 +44,7 @@
*
* @param dev ADC peripheral to initialize
*/
void adc_init(const adc_dev *dev) {
void adc_init(adc_dev *dev) {
rcc_clk_enable(dev->clk_id);
rcc_reset_dev(dev->clk_id);
}
Expand All @@ -55,7 +55,7 @@ void adc_init(const adc_dev *dev) {
* @param event Event used to trigger the start of conversion.
* @see adc_extsel_event
*/
void adc_set_extsel(const adc_dev *dev, adc_extsel_event event) {
void adc_set_extsel(adc_dev *dev, adc_extsel_event event) {
uint32 cr2 = dev->regs->CR2;
cr2 &= ~ADC_CR2_EXTSEL;
cr2 |= event;
Expand All @@ -71,7 +71,7 @@ void adc_set_extsel(const adc_dev *dev, adc_extsel_event event) {
* @param smp_rate sample rate to set
* @see adc_smp_rate
*/
void adc_set_sample_rate(const adc_dev *dev, adc_smp_rate smp_rate) {
void adc_set_sample_rate(adc_dev *dev, adc_smp_rate smp_rate) {
uint32 adc_smpr1_val = 0, adc_smpr2_val = 0;
int i;

Expand All @@ -95,7 +95,7 @@ void adc_set_sample_rate(const adc_dev *dev, adc_smp_rate smp_rate) {
* @param channel channel to convert
* @return conversion result
*/
uint16 adc_read(const adc_dev *dev, uint8 channel) {
uint16 adc_read(adc_dev *dev, uint8 channel) {
adc_reg_map *regs = dev->regs;

adc_set_reg_seqlen(dev, 1);
Expand Down
115 changes: 108 additions & 7 deletions STM32F1/cores/maple/libmaple/adc_f1.c 100644 → 100755
Expand Up @@ -34,6 +34,7 @@

#include <libmaple/adc.h>
#include <libmaple/gpio.h>
#include <libmaple/nvic.h>//Added by bubulindo.

/*
* Devices
Expand All @@ -42,26 +43,126 @@
adc_dev adc1 = {
.regs = ADC1_BASE,
.clk_id = RCC_ADC1,
.handlers = {[3]=0}, //added by bubulindo. EOC, JEOC, AWD
.irq_num = NVIC_ADC_1_2,
};
/** ADC1 device. */
const adc_dev *ADC1 = &adc1;
adc_dev *ADC1 = &adc1;

adc_dev adc2 = {
.regs = ADC2_BASE,
.clk_id = RCC_ADC2,
};
/** ADC2 device. */
const adc_dev *ADC2 = &adc2;
adc_dev *ADC2 = &adc2;

#if defined(STM32_HIGH_DENSITY) || defined(STM32_XL_DENSITY)
adc_dev adc3 = {
.regs = ADC3_BASE,
.clk_id = RCC_ADC3,
.handlers = {[3]=0}, //added by bubulindo. EOC, JEOC, AWD
.irq_num = NVIC_ADC3,//added by bubulindo.
};
/** ADC3 device. */
const adc_dev *ADC3 = &adc3;
adc_dev *ADC3 = &adc3;
#endif


/*
adc irq routine.
Added by bubulindo.
*/
void __irq_adc() {
//get status
uint32 adc_sr = ADC1->regs->SR;
//End Of Conversion
if (adc_sr & (1U << ADC_SR_EOC_BIT)) {
ADC1->regs->SR &= ~(1<<ADC_SR_EOC_BIT);
void (*handler)(void) = ADC1->handlers[ADC_EOC];
if (handler) {
handler();
}
}
//Injected End Of Conversion
if (adc_sr & (1U << ADC_SR_JEOC_BIT)) {
ADC1->regs->SR &= ~(1<<ADC_SR_JEOC_BIT);
void (*handler)(void) = ADC1->handlers[ADC_JEOC];
if (handler) {
handler();
}
}
//Analog Watchdog
if (adc_sr & (1U << ADC_SR_AWD_BIT)) {
ADC1->regs->SR &= ~(1<<ADC_SR_AWD_BIT);
void (*handler)(void) = ADC1->handlers[ADC_AWD];
if (handler) {
handler();
}
}
};//end of adc irq


/*
ADC3 IRQ handler.
added by bubulindo
*/
#if defined(STM32_HIGH_DENSITY) || defined(STM32_XL_DENSITY)
void __irq_adc3() {
//get status
uint32 adc_sr = ADC3->regs->SR;
//End Of Conversion
if (adc_sr & (1U << ADC_SR_EOC_BIT)) {
ADC3->regs->SR &= ~(1<<ADC_SR_EOC_BIT);
void (*handler)(void) = ADC3->handlers[ADC_EOC];
if (handler) {
handler();
}
}
//Injected End Of Conversion
if (adc_sr & (1U << ADC_SR_JEOC_BIT)) {
ADC3->regs->SR &= ~(1<<ADC_SR_JEOC_BIT);
void (*handler)(void) = ADC3->handlers[ADC_JEOC];
if (handler) {
handler();
}
}
//Analog Watchdog
if (adc_sr & (1U << ADC_SR_AWD_BIT)) {
ADC3->regs->SR &= ~(1<<ADC_SR_AWD_BIT);
void (*handler)(void) = ADC3->handlers[ADC_AWD];
if (handler) {
handler();
}
}
};//end of ADC3 irq
#endif

/*
enable interrupts on the ADC:
use ADC_EOC, ADC_JEOC, ADC_AWD
This will set up the interrupt bit in the ADC as well as in the NVIC.
added by bubulindo
*/
void adc_enable_irq(adc_dev* dev, uint8 interrupt) {//ADC1 for now.
dev->regs->CR1 |= (1U<<(interrupt +ADC_CR1_EOCIE_BIT));
nvic_irq_enable(dev->irq_num);
}

/*
attach interrupt functionality for ADC
use ADC_EOC, ADC_JEOC, ADC_AWD
added by bubulindo
*/

void adc_attach_interrupt(adc_dev *dev,
uint8 interrupt,
voidFuncPtr handler) {
dev->handlers[interrupt] = handler;
adc_enable_irq(dev, interrupt);
//enable_irq(dev, interrupt); //I need to create this function. to enable NVIC
// nvic_irq_enable()
}

/*
* STM32F1 routines
*/
Expand All @@ -73,7 +174,7 @@ const adc_dev *ADC3 = &adc3;
*
* @param dev adc device
*/
void adc_calibrate(const adc_dev *dev) {
void adc_calibrate(adc_dev *dev) {
__io uint32 *rstcal_bit = bb_perip(&(dev->regs->CR2), 3);
__io uint32 *cal_bit = bb_perip(&(dev->regs->CR2), 2);

Expand All @@ -94,19 +195,19 @@ void adc_set_prescaler(adc_prescaler pre) {
rcc_set_prescaler(RCC_PRESCALER_ADC, (uint32)pre);
}

void adc_foreach(void (*fn)(const adc_dev*)) {
void adc_foreach(void (*fn)(adc_dev*)) {
fn(ADC1);
fn(ADC2);
#if defined(STM32_HIGH_DENSITY) || defined(STM32_XL_DENSITY)
fn(ADC3);
#endif
}

void adc_config_gpio(const adc_dev *ignored, gpio_dev *gdev, uint8 bit) {
void adc_config_gpio(adc_dev *ignored, gpio_dev *gdev, uint8 bit) {
gpio_set_mode(gdev, bit, GPIO_INPUT_ANALOG);
}

void adc_enable_single_swstart(const adc_dev *dev) {
void adc_enable_single_swstart(adc_dev *dev) {
adc_init(dev);
adc_set_extsel(dev, ADC_SWSTART);
adc_set_exttrig(dev, 1);
Expand Down
38 changes: 38 additions & 0 deletions STM32F1/cores/maple/libmaple/timer.c 100644 → 100755
Expand Up @@ -38,6 +38,8 @@
static void disable_channel(timer_dev *dev, uint8 channel);
static void pwm_mode(timer_dev *dev, uint8 channel);
static void output_compare_mode(timer_dev *dev, uint8 channel);
static void encoder_mode(timer_dev *dev, uint8 channel) ;//CARLOS


static inline void enable_irq(timer_dev *dev, timer_interrupt_id iid);

Expand Down Expand Up @@ -230,6 +232,10 @@ void timer_set_mode(timer_dev *dev, uint8 channel, timer_mode mode) {
case TIMER_OUTPUT_COMPARE:
output_compare_mode(dev, channel);
break;
//added by CARLOS.
case TIMER_ENCODER:
encoder_mode(dev, channel); //find a way to pass all the needed stuff on the 8bit var
break;
}
}

Expand Down Expand Up @@ -293,6 +299,13 @@ void timer_detach_interrupt(timer_dev *dev, uint8 interrupt) {
dev->handlers[interrupt] = NULL;
}

//CARLOS
uint8 get_direction(timer_dev *dev){
return *bb_perip(&(dev->regs).gen->CR1, TIMER_CR1_DIR_BIT);
}



/*
* Utilities
*/
Expand All @@ -313,6 +326,31 @@ static void output_compare_mode(timer_dev *dev, uint8 channel) {
timer_cc_enable(dev, channel);
}

//added by CARLOS.
static void encoder_mode(timer_dev *dev, uint8 channel) {

//prescaler.
//(dev->regs).gen->PSC = 1;

//map inputs.
(dev->regs).gen->CCMR1 = TIMER_CCMR1_CC1S_INPUT_TI1 | TIMER_CCMR1_CC2S_INPUT_TI2 | TIMER_CCMR1_IC2F | TIMER_CCMR1_IC1F ;

(dev->regs).gen->SMCR = TIMER_SMCR_SMS_ENCODER3; //choose encoder 3, counting on both edges.

//polarity
//(dev->regs).gen->CCER = TIMER_CCER_CC1P; //to invert the counting, only one of the inputs should be inverted.

//set the interval used by the encoder.
//timer_set_reload(dev, 1000);

// (dev->regs).gen->CR1 |=TIMER_CR1_UDIS_BIT;

//run timer
timer_resume(dev);
}



static void enable_adv_irq(timer_dev *dev, timer_interrupt_id id);
static void enable_bas_gen_irq(timer_dev *dev);

Expand Down
2 changes: 1 addition & 1 deletion STM32F1/cores/maple/wirish_analog.cpp 100644 → 100755
Expand Up @@ -38,7 +38,7 @@
* to INPUT_ANALOG. That's faster, but it does require some extra work
* on the user's part. Not too much, we think ;). */
uint16 analogRead(uint8 pin) {
const adc_dev *dev = PIN_MAP[pin].adc_device;
adc_dev *dev = PIN_MAP[pin].adc_device;
if (dev == NULL) {
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion STM32F1/cores/maple/wirish_types.h 100644 → 100755
Expand Up @@ -51,7 +51,7 @@
typedef struct stm32_pin_info {
gpio_dev *gpio_device; /**< Maple pin's GPIO device */
timer_dev *timer_device; /**< Pin's timer device, if any. */
const adc_dev *adc_device; /**< ADC device, if any. */
adc_dev *adc_device; /**< ADC device, if any. */
uint8 gpio_bit; /**< Pin's GPIO port bit. */
uint8 timer_channel; /**< Timer channel, or 0 if none. */
uint8 adc_channel; /**< Pin ADC channel, or ADCx if none. */
Expand Down

0 comments on commit 9147a46

Please sign in to comment.