Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dummy ADC und DAC #230

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Modelica_DeviceDrivers/EmbeddedTargets/STM32F4/Blocks/ADC.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
within Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Blocks;

model ADC
import Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Functions;
import Modelica_DeviceDrivers.EmbeddedTargets.FTM32F4.Types;
import Modelica.SIunits;
outer Microcontroller mcu;
/* constant Types.ADCChannel channel annotation(
Dialog(enable = true, tab = "General", group = "Constants"));*/
Modelica.Blocks.Interfaces.IntegerOutput pb0 annotation(
Placement(visible = true, transformation(origin = {112, 56}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {112, 56}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Blocks.Interfaces.IntegerOutput pb1 annotation(
Placement(visible = true, transformation(origin = {112, -44}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {112, -44}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
protected
Functions.Analog.InitADC hadc = Functions.Analog.InitADC(mcu.hal);
equation
pb0 = Functions.Analog.read(hadc, Types.ADCChannel.CHANNEL1);
pb1 = Functions.Analog.read(hadc, Types.ADCChannel.CHANNEL2);
annotation(
Icon(graphics = {Text(extent = {{-95, -95}, {95, 95}}, textString = "Analog %channel", fontName = "Arial")}));
annotation(
defaultComponentName = "adc",
Documentation(info = "<html>
<h1>ADC</h1>

<p>The AVR ADC (analog-to-digital converter) takes as parameters a selection of which voltage reference input to use (<em>voltageReferenceSelect</em>), what voltage this reference voltage has (<em>voltageReference</em>), the <em>analogPort</em> (which analog input to convert to a digital value) and which prescaler to use (<em>prescaler</em>, might be automatically chosen if the used AVR platform had known constants for this). The output is continuously read (at each time step), producing a voltage between 0 and
<em>voltageReference</em>.</p>
</html>"),
Icon(graphics = {Text(extent = {{-95, -95}, {95, 95}}, textString = "ADC %analogPort\n0..%voltageReference [V]", fontName = "Arial")}));
end ADC;
19 changes: 19 additions & 0 deletions Modelica_DeviceDrivers/EmbeddedTargets/STM32F4/Blocks/DAC.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
within Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Blocks;

block DAC
import Modelica_DeviceDrivers.EmbeddedTargets.STM32F4;
import Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Functions;
import Modelica_DeviceDrivers.EmbeddedTargets.FTM32F4.Types;
import Modelica.SIunits;
outer Microcontroller mcu;
extends Modelica.Blocks.Icons.Block;
// TODO implement STM432F4 specific code

Modelica.Blocks.Interfaces.IntegerInput pa4 "Connector of Analog DAC signal)" annotation (
Placement(transformation(extent={{-140,-20},{-100,20}})));
protected
Functions.Analog.InitDAC hdac = Functions.Analog.InitDAC(mcu.hal);
algorithm
Functions.Analog.write(hdac, pa4);
annotation(Icon(graphics={ Text(extent = {{-95, -95}, {95, 95}}, textString = "Analog", fontName = "Arial")}));
end DAC;
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ DigitalWriteBoolean
SynchronizeRealtime
Led
ExtInt
ADC
DAC
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
within Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Examples.STM32F4_Discovery;
model Analog
extends .Modelica.Icons.Example;
import Modelica_DeviceDrivers.EmbeddedTargets.STM32F4;
inner Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Blocks.Microcontroller mcu(desiredPeriod = 0.002, platform = Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Types.Platform.STM32F4DISC)
annotation(Placement(visible = true, transformation(origin={-70,70}, extent={{-10,-10},
{10,10}}, rotation = 0)));
// STM32F4.Functions.HAL.Init HALinit = STM32F4.Functions.HAL.Init();
Blocks.Led led(led = Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Types.LED.LED3) annotation(Placement(visible = true, transformation(origin={-36,38}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Blocks.Sources.BooleanExpression boolExp(y=mod(time, 1) >= 0.5)
annotation (Placement(visible=true, transformation(
origin={-68,38},
extent={{-12,-10},{12,10}},
rotation=0)));
Blocks.SynchronizeRealtime synchronizeRealtime(ahbPre = Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Types.AHBPre.DIV_1, apb1Pre = Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Types.APBPre.DIV_4, apb2Pre = Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Types.APBPre.DIV_2, clock = Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Types.Clock.HSE_PLL, overdrive = false, pllM = 8, pllN = 336, pllP = Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Types.PLLP.DIV_2, pllQ = 7, preFlash = true, pwrRegVoltage = Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Types.PWRRegulatorVoltage.SCALE1) annotation (
Placement(visible = true, transformation(origin={-36,70}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Blocks.ADC adc annotation(
Placement(visible = true, transformation(origin = {6, 58}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Blocks.DAC dac1 annotation(
Placement(visible = true, transformation(origin = {52, 64}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
connect(adc.pb0, dac1.pa4) annotation(
Line(points = {{18, 64}, {40, 64}}, color = {255, 127, 0}));
connect(boolExp.y, led.u) annotation(
Line(points = {{-54.8, 38}, {-48, 38}}, color = {255, 0, 255}));
/* synchronizeRealtime1.actualInterval is not legal in experiment annotation*/
annotation ( Experiment(Interval = 0.002), Documentation(info = "<html>
<h1>Blink</h1>
<p>Blink is a very simple STM model, which simply toggles the built-in LED on the board on and off at the given frequency (this version blinks at a frequency given by the model using single precision floating point; it is also possible to simply flip the LED bit in each time step which gives a more accurate result). Use this model to see if your Modelica tool can export code for STM32F4 MCUs.</p>
<p>STM digital pin 12 on port D corresponds to digital pin D12 on the STM32F4-Discovery. The user LED's are accessed via convinient LED Block. If desired, you can connect an external LED to this PIN, with a suitable resistor in-between (perhaps 220&#8486;). Connect the other PIN on the LED to ground.</p>

</html>"));
end Analog;
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Blink
BlinkGPIO
BlinkGPIO_EXTI
BlinkSpringMass
Analog
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
within Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Functions.Analog;

class InitADC
extends ExternalObject;
import Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Functions.HAL;
function constructor "Initialize device"
import Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Types;
extends .Modelica.Icons.Function;
input HAL.Init handle "handle";
output InitADC hadc;
external "C" hadc = MDD_stm32f4_adc_init(handle)
annotation (Include="#include \"MDDSTM32F4Analog.h\"");
end constructor;

function destructor
extends .Modelica.Icons.Function;
input InitADC hadc;
external "C" MDD_stm32f4_adc_close(hadc)
annotation (Include="#include \"MDDSTM32F4Analog.h\"");
end destructor;
end InitADC;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
within Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Functions.Analog;

class InitDAC
extends ExternalObject;
import Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Functions.HAL;
function constructor "Initialize device"
import Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Types;
extends .Modelica.Icons.Function;
input HAL.Init handle "handle";
output InitDAC hdac;
external "C" hdac = MDD_stm32f4_dac_init(handle)
annotation (Include="#include \"MDDSTM32F4Analog.h\"");
end constructor;

function destructor
extends .Modelica.Icons.Function;
input InitDAC hdac;
external "C" MDD_stm32f4_dac_close(hdac)
annotation (Include="#include \"MDDSTM32F4Analog.h\"");
end destructor;
end InitDAC;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
within Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Functions;

encapsulated package Analog
extends .Modelica.Icons.Package;
end Analog;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
InitADC
InitDAC
read
write
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
within Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Functions.Analog;
function read
extends .Modelica.Icons.Function;
input InitADC hadc;
input .Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Types.ADCChannel ch;
output Integer v;
external "C" v = MDD_stm32f4_analog_read(hadc,ch)
annotation (Include="#include \"MDDSTM32F4Analog.h\"");
annotation(__ModelicaAssociation_Impure=true);
end read;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
within Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Functions.Analog;

function write
extends .Modelica.Icons.Function;
input InitDAC hdac;
input Integer value;
external "C" MDD_stm32f4_analog_write(hdac, value)
annotation (Include="#include \"MDDSTM32F4Analog.h\"");
end write;
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
within Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Functions.Digital;
impure function read
function read
extends .Modelica.Icons.Function;
input InitRead port;
input .Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Types.Pin pin "Must correspond to the initialized port";
output Boolean b;
external "C" b = MDD_stm32f4_digital_pin_read(port,pin)
annotation (Include="#include \"MDDSTM32F4Digital.h\"");
annotation(__ModelicaAssociation_Impure=true);
end read;
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ RealTimeSynchronization
Timers
Utilities
HAL
Analog
6 changes: 4 additions & 2 deletions Modelica_DeviceDrivers/EmbeddedTargets/STM32F4/Types.mo
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ type PWRRegulatorVoltage = enumeration(
SCALE3 "Max fKCLK frequency 120 MHz"
) "Power Regulator Voltage Scale";



type ADCChannel = enumeration(
CHANNEL1 "corresponds to pin PB0",
CHANNEL2 "corresponds to pin PB1"
) "ADC channel 8 and 9";
end Types;


Expand Down
160 changes: 160 additions & 0 deletions Modelica_DeviceDrivers/Resources/Include/MDDSTM32F4Analog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#ifndef MDDARMANALOG__H
#define MDDARMANALOG__H


#include <main.h>

ADC_HandleTypeDef AdcHandle;
DAC_HandleTypeDef DacHandle;

/* Variable used to get converted value */
__IO uint16_t uhADCxConvertedValue[2] = {0,0};
__IO uint16_t uhDACxConvertedValue = 0;

static inline void* MDD_stm32f4_adc_close(void* handle)
{
}
static inline void* MDD_stm32f4_dac_close(void* handle)
{
}

static inline void* MDD_stm32f4_adc_init(void* handle)
{
ADC_ChannelConfTypeDef sConfig;

/*##-1- Configure the ADC peripheral #######################################*/
int rc = 0;
AdcHandle.Instance = ADCx;

AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
AdcHandle.Init.ScanConvMode = ENABLE;
AdcHandle.Init.ContinuousConvMode = ENABLE;
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
AdcHandle.Init.NbrOfDiscConversion = 0;
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.NbrOfConversion = 2;
AdcHandle.Init.DMAContinuousRequests = ENABLE;
AdcHandle.Init.EOCSelection = DISABLE;

if(HAL_ADC_Init(&AdcHandle) != HAL_OK)
{
/* Initialization Error */
rc = 1;
return (void*)rc;
}

/*##-2- Configure ADC regular channel ######################################*/
/* Note: Considering IT occurring after each number of size of */
/* "uhADCxConvertedValue" ADC conversions (IT by DMA end */
/* of transfer), select sampling time and ADC clock with sufficient */
/* duration to not create an overhead situation in IRQHandler. */
sConfig.Channel = ADCx_CHANNEL;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES;
sConfig.Offset = 0;

if(HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK)
{
/* Channel Configuration Error */
rc = 1;
return (void*)rc;
}

sConfig.Rank = 2;
sConfig.Channel = ADC_CHANNEL_9;

if(HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK)
{
/* Channel Configuration Error */
rc = 1;
return (void*)rc;
}
/*##-3- Start the conversion process and enable interrupt ##################*/
/* Note: Considering IT occurring after each number of ADC conversions */
/* (IT by DMA end of transfer), select sampling time and ADC clock */
/* with sufficient duration to not create an overhead situation in */
/* IRQHandler. */
if(HAL_ADC_Start_DMA(&AdcHandle, (uint32_t*)&uhADCxConvertedValue, 2) != HAL_OK)
{
/* Start Conversation Error */
rc = 1;
return (void*)rc;
}
return (void*) rc;

}

static inline void* MDD_stm32f4_dac_init(void* handle)
{
DAC_ChannelConfTypeDef sConfig;
DacHandle.Instance = DAC;
int rc = 0;

static TIM_HandleTypeDef htim;
TIM_MasterConfigTypeDef sMasterConfig;

/*##-1- Configure the TIM peripheral #######################################*/
/* Time base configuration */
htim.Instance = TIM6;

htim.Init.Period = 0x7FF;
htim.Init.Prescaler = 0;
htim.Init.ClockDivision = 0;
htim.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_Base_Init(&htim);

/* TIM6 TRGO selection */
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

HAL_TIMEx_MasterConfigSynchronization(&htim, &sMasterConfig);


if(HAL_DAC_Init(&DacHandle) != HAL_OK)
{
/* Initialization Error */
rc = 1;
return (void*)rc;
}

/*##-1- DAC channel1 Configuration #########################################*/
sConfig.DAC_Trigger = DAC_TRIGGER_T6_TRGO;
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;

if(HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DACx_CHANNEL1) != HAL_OK)
{
/* Channel configuration Error */
rc = 1;
return (void*)rc;
}

/*##-2- Enable DAC Channel1 and associated DMA #############################*/
if(HAL_DAC_Start_DMA(&DacHandle, DACx_CHANNEL1, (uint32_t*)&uhDACxConvertedValue, 1, DAC_ALIGN_12B_R) != HAL_OK)
{
/* Start DMA Error */
rc = 1;
return (void*)rc;
}

/*##-2- Enable TIM peripheral counter ######################################*/
HAL_TIM_Base_Start(&htim);

return (void*) rc;

}

static inline int MDD_stm32f4_analog_read(void* handle, int channel)
{
return uhADCxConvertedValue[channel - 1];
}

static inline void MDD_stm32f4_analog_write(void* handle, int value)
{
uhDACxConvertedValue = (uint16_t) value;
}


#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
loadFile("../../../../../../../../package.mo");getErrorString();
getSourceFile(Modelica_DeviceDrivers);
translateModel(Modelica_DeviceDrivers.EmbeddedTargets.STM32F4.Examples.STM32F4_Discovery.Analog, fileNamePrefix="ADC_DAC_PID");getErrorString();

Loading