Skip to content

Commit

Permalink
Now ADC works for read throttle - pooling. The ADC code need to be in…
Browse files Browse the repository at this point in the history
…side main, inside adc.c file does not work :-(
  • Loading branch information
casainho committed May 24, 2017
1 parent cddb150 commit 143a2a1
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Flash_Debug-STM8.launch
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<stringAttribute key="org.eclipse.cdt.launch.DEBUGGER_STOP_AT_MAIN_SYMBOL" value="main"/>
<booleanAttribute key="org.eclipse.cdt.launch.ENABLE_REGISTER_BOOKKEEPING" value="false"/>
<booleanAttribute key="org.eclipse.cdt.launch.ENABLE_VARIABLE_BOOKKEEPING" value="false"/>
<stringAttribute key="org.eclipse.cdt.launch.FORMAT" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&lt;contentList&gt;&lt;content id=&quot;temp1-apply_duty_cycle-(cast)&quot; val=&quot;int&quot;/&gt;&lt;content id=&quot;temp1-apply_duty_cycle-(format)&quot; val=&quot;0&quot;/&gt;&lt;/contentList&gt;"/>
<stringAttribute key="org.eclipse.cdt.launch.FORMAT" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&lt;contentList&gt;&lt;content id=&quot;temp1-apply_duty_cycle-(format)&quot; val=&quot;0&quot;/&gt;&lt;content id=&quot;temp1-apply_duty_cycle-(cast)&quot; val=&quot;int&quot;/&gt;&lt;/contentList&gt;"/>
<stringAttribute key="org.eclipse.cdt.launch.GLOBAL_VARIABLES" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;globalVariableList/&gt;&#10;"/>
<stringAttribute key="org.eclipse.cdt.launch.MEMORY_BLOCKS" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#10;&lt;memoryBlockExpressionList&gt;&#10;&lt;memoryBlockExpressionItem&gt;&#10;&lt;expression text=&quot;0x40000000&quot;/&gt;&#10;&lt;/memoryBlockExpressionItem&gt;&#10;&lt;memoryBlockExpressionItem&gt;&#10;&lt;expression text=&quot;0x40000424&quot;/&gt;&#10;&lt;/memoryBlockExpressionItem&gt;&#10;&lt;/memoryBlockExpressionList&gt;&#10;"/>
<stringAttribute key="org.eclipse.cdt.launch.PROGRAM_NAME" value="/home/cas/OpenSource-EBike-firmware/BMSBattery_S_controllers_firmware/bin/main.elf"/>
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions StdPeriphLib/inc/stm8s.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
/******************************************************************************/
/* Library configuration section */
/******************************************************************************/
#define SDCC

/* Check the used compiler */
#if defined(__CSMC__)
#define _COSMIC_
Expand Down
33 changes: 0 additions & 33 deletions StdPeriphLib/src/stm8s_adc1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand All @@ -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)
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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 */
Expand Down
7 changes: 0 additions & 7 deletions StdPeriphLib/src/stm8s_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
55 changes: 48 additions & 7 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}

0 comments on commit 143a2a1

Please sign in to comment.