From 143a2a159aec1db9acd2ccd0c6a550272896c73e Mon Sep 17 00:00:00 2001 From: casainho Date: Wed, 24 May 2017 16:05:32 +0100 Subject: [PATCH] Now ADC works for read throttle - pooling. The ADC code need to be inside main, inside adc.c file does not work :-( --- Flash_Debug-STM8.launch | 2 +- Makefile | 4 ++- StdPeriphLib/inc/stm8s.h | 2 ++ StdPeriphLib/src/stm8s_adc1.c | 33 --------------------- StdPeriphLib/src/stm8s_gpio.c | 7 ----- main.c | 55 ++++++++++++++++++++++++++++++----- 6 files changed, 54 insertions(+), 49 deletions(-) diff --git a/Flash_Debug-STM8.launch b/Flash_Debug-STM8.launch index f3485d49..dd1318ed 100644 --- a/Flash_Debug-STM8.launch +++ b/Flash_Debug-STM8.launch @@ -16,7 +16,7 @@ - + diff --git a/Makefile b/Makefile index a5f77058..ab92ec07 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,7 @@ EXTRASRCS = \ $(SDIR)/stm8s_exti.c \ $(SDIR)/stm8s_uart2.c \ $(SDIR)/stm8s_tim1.c \ + $(SDIR)/stm8s_adc1.c \ gpio.c \ uart.c \ hall_sensors.c \ @@ -48,7 +49,8 @@ EXTRASRCS = \ RELS = $(EXTRASRCS:.c=.rel) INCLUDES = -I$(IDIR) -I. -CFLAGS = -m$(PLATFORM) -I/usr/local/share/sdcc/include -I/usr/local/share/sdcc/lib/ --all-callee-saves --stack-auto --fverbose-asm --float-reent --no-peep +#CFLAGS = -m$(PLATFORM) -I/usr/local/share/sdcc/include -I/usr/local/share/sdcc/lib/ --nogcse --noinvariant --noinduction --noloopreverse --nolabelopt --nooverlay --no-peep --nolospre +CFLAGS = -m$(PLATFORM) -I/usr/local/share/sdcc/include -I/usr/local/share/sdcc/lib/ --std-c99 --nolospre IHX_FLAGS = --out-fmt-ihx --debug ELF_FLAGS = --out-fmt-elf --debug LIBS = -l$(PLATFORM) diff --git a/StdPeriphLib/inc/stm8s.h b/StdPeriphLib/inc/stm8s.h index a0fe3402..9ad4657c 100644 --- a/StdPeriphLib/inc/stm8s.h +++ b/StdPeriphLib/inc/stm8s.h @@ -77,6 +77,8 @@ /******************************************************************************/ /* Library configuration section */ /******************************************************************************/ +#define SDCC + /* Check the used compiler */ #if defined(__CSMC__) #define _COSMIC_ diff --git a/StdPeriphLib/src/stm8s_adc1.c b/StdPeriphLib/src/stm8s_adc1.c index d203a499..6e52a84c 100644 --- a/StdPeriphLib/src/stm8s_adc1.c +++ b/StdPeriphLib/src/stm8s_adc1.c @@ -87,16 +87,6 @@ void ADC1_DeInit(void) */ void ADC1_Init(ADC1_ConvMode_TypeDef ADC1_ConversionMode, ADC1_Channel_TypeDef ADC1_Channel, ADC1_PresSel_TypeDef ADC1_PrescalerSelection, ADC1_ExtTrig_TypeDef ADC1_ExtTrigger, FunctionalState ADC1_ExtTriggerState, ADC1_Align_TypeDef ADC1_Align, ADC1_SchmittTrigg_TypeDef ADC1_SchmittTriggerChannel, FunctionalState ADC1_SchmittTriggerState) { - /* Check the parameters */ - assert_param(IS_ADC1_CONVERSIONMODE_OK(ADC1_ConversionMode)); - assert_param(IS_ADC1_CHANNEL_OK(ADC1_Channel)); - assert_param(IS_ADC1_PRESSEL_OK(ADC1_PrescalerSelection)); - assert_param(IS_ADC1_EXTTRIG_OK(ADC1_ExtTrigger)); - assert_param(IS_FUNCTIONALSTATE_OK(((ADC1_ExtTriggerState)))); - assert_param(IS_ADC1_ALIGN_OK(ADC1_Align)); - assert_param(IS_ADC1_SCHMITTTRIG_OK(ADC1_SchmittTriggerChannel)); - assert_param(IS_FUNCTIONALSTATE_OK(ADC1_SchmittTriggerState)); - /*-----------------CR1 & CSR configuration --------------------*/ /* Configure the conversion mode and the channel to convert respectively according to ADC1_ConversionMode & ADC1_Channel values & ADC1_Align values */ @@ -189,10 +179,6 @@ void ADC1_DataBufferCmd(FunctionalState NewState) */ void ADC1_ITConfig(ADC1_IT_TypeDef ADC1_IT, FunctionalState NewState) { - /* Check the parameters */ - assert_param(IS_ADC1_IT_OK(ADC1_IT)); - assert_param(IS_FUNCTIONALSTATE_OK(NewState)); - if (NewState != DISABLE) { /* Enable the ADC1 interrupts */ @@ -213,9 +199,6 @@ void ADC1_ITConfig(ADC1_IT_TypeDef ADC1_IT, FunctionalState NewState) */ void ADC1_PrescalerConfig(ADC1_PresSel_TypeDef ADC1_Prescaler) { - /* Check the parameter */ - assert_param(IS_ADC1_PRESSEL_OK(ADC1_Prescaler)); - /* Clear the SPSEL bits */ ADC1->CR1 &= (uint8_t)(~ADC1_CR1_SPSEL); /* Select the prescaler division factor according to ADC1_PrescalerSelection values */ @@ -232,10 +215,6 @@ void ADC1_PrescalerConfig(ADC1_PresSel_TypeDef ADC1_Prescaler) */ void ADC1_SchmittTriggerConfig(ADC1_SchmittTrigg_TypeDef ADC1_SchmittTriggerChannel, FunctionalState NewState) { - /* Check the parameters */ - assert_param(IS_ADC1_SCHMITTTRIG_OK(ADC1_SchmittTriggerChannel)); - assert_param(IS_FUNCTIONALSTATE_OK(NewState)); - if (ADC1_SchmittTriggerChannel == ADC1_SCHMITTTRIG_ALL) { if (NewState != DISABLE) @@ -285,11 +264,6 @@ void ADC1_SchmittTriggerConfig(ADC1_SchmittTrigg_TypeDef ADC1_SchmittTriggerChan */ void ADC1_ConversionConfig(ADC1_ConvMode_TypeDef ADC1_ConversionMode, ADC1_Channel_TypeDef ADC1_Channel, ADC1_Align_TypeDef ADC1_Align) { - /* Check the parameters */ - assert_param(IS_ADC1_CONVERSIONMODE_OK(ADC1_ConversionMode)); - assert_param(IS_ADC1_CHANNEL_OK(ADC1_Channel)); - assert_param(IS_ADC1_ALIGN_OK(ADC1_Align)); - /* Clear the align bit */ ADC1->CR2 &= (uint8_t)(~ADC1_CR2_ALIGN); /* Configure the data alignment */ @@ -324,10 +298,6 @@ void ADC1_ConversionConfig(ADC1_ConvMode_TypeDef ADC1_ConversionMode, ADC1_Chann */ void ADC1_ExternalTriggerConfig(ADC1_ExtTrig_TypeDef ADC1_ExtTrigger, FunctionalState NewState) { - /* Check the parameters */ - assert_param(IS_ADC1_EXTTRIG_OK(ADC1_ExtTrigger)); - assert_param(IS_FUNCTIONALSTATE_OK(NewState)); - /* Clear the external trigger selection bits */ ADC1->CR2 &= (uint8_t)(~ADC1_CR2_EXTSEL); @@ -529,9 +499,6 @@ FlagStatus ADC1_GetFlagStatus(ADC1_Flag_TypeDef Flag) uint8_t flagstatus = 0; uint8_t temp = 0; - /* Check the parameters */ - assert_param(IS_ADC1_FLAG_OK(Flag)); - if ((Flag & 0x0F) == 0x01) { /* Get OVR flag status */ diff --git a/StdPeriphLib/src/stm8s_gpio.c b/StdPeriphLib/src/stm8s_gpio.c index 956ffb02..8d055cbc 100644 --- a/StdPeriphLib/src/stm8s_gpio.c +++ b/StdPeriphLib/src/stm8s_gpio.c @@ -70,13 +70,6 @@ void GPIO_DeInit(GPIO_TypeDef* GPIOx) void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin, GPIO_Mode_TypeDef GPIO_Mode) { - /*----------------------*/ - /* Check the parameters */ - /*----------------------*/ - - assert_param(IS_GPIO_MODE_OK(GPIO_Mode)); - assert_param(IS_GPIO_PIN_OK(GPIO_Pin)); - /* Reset corresponding bit to GPIO_Pin in CR2 register */ GPIOx->CR2 &= (uint8_t)(~(GPIO_Pin)); diff --git a/main.c b/main.c index 0eff6e6a..238a431f 100644 --- a/main.c +++ b/main.c @@ -16,6 +16,46 @@ #include "uart.h" #include "pwm.h" #include "interrupts.h" +#include "stm8s_adc1.h" + +//can't put ADC code file on external file like adc.c or the code will not work :-( +void adc_init (void) +{ + //init GPIO for the used ADC pins + GPIO_Init(THROTTLE__PORT, + THROTTLE__PIN, + GPIO_MODE_IN_FL_NO_IT); + + //de-Init ADC peripheral + ADC1_DeInit(); + + //init ADC2 peripheral + ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, + ADC1_CHANNEL_4, + ADC1_PRESSEL_FCPU_D2, + ADC1_EXTTRIG_TIM, + DISABLE, //disable external trigger + ADC1_ALIGN_RIGHT, + ADC1_SCHMITTTRIG_CHANNEL9, + DISABLE); +} + +uint16_t adc_read_throttle (void) +{ + ADC1_ConversionConfig(ADC1_CONVERSIONMODE_SINGLE, + ADC1_CHANNEL_4, + ADC1_ALIGN_RIGHT); + + //start Conversion + ADC1_StartConversion(); + + //block waiting for the end of conversion + while (!ADC1_GetFlagStatus(ADC1_FLAG_EOC)) ; + + return ADC1_GetConversionValue(); +} + + //With SDCC, interrupt service routine function prototypes must be placed in the file that contains main () //in order for an vector for the interrupt to be placed in the the interrupt vector space. It's acceptable @@ -144,7 +184,7 @@ int main() //set clock at the max 16MHz CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); -// gpio_init (); + gpio_init (); brake_init (); // hold here while brake is pressed -- this is a protection for development @@ -153,14 +193,15 @@ int main() uart_init (); hall_sensor_init (); pwm_init (); - enableInterrupts (); - - pwm_set_duty_cycle_channel1(1024/10); - pwm_set_duty_cycle_channel2(1024/3); - pwm_set_duty_cycle_channel3(1024/2); + adc_init (); + enableInterrupts(); while (1) { - ; + static uint16_t adc_value; + adc_value = adc_read_throttle (); + pwm_set_duty_cycle_channel1 (adc_value); + pwm_set_duty_cycle_channel2 (adc_value); + pwm_set_duty_cycle_channel3 (adc_value); } }