1 Abstract
After learning S32K3 PWM and have written some MCAL codes, this is the first MCAL article record starts with the combination of K344 EMIOS+ICU+TRIMUX+LCU, which can involve comprehensive configurations such as PORT, DIO, EMIOS, interrupt ICU, TRIGMUX, LCU, etc. The board platform is still based on NXP official S32K344EVB, RTD400, and the function is: use two channels of EMIOS0 and one channel of EMIOS1. After one channel of EMIOS 0 outputs PWM, it connects LCU through TRIGUMX to generate a set of complementary PWM, and the other channel can realize hardware interrupt control of PWM duty cycle through SW5 PT26 button. One channel of EMIOS1 is directly connected to the PTA29 onboard red light, and the brightness is gradually changed by changing the PWM duty cycle, and then the breathing light effect is realized by directly changing the on and off cycle. At the same time, when the PWM turns off the red light, the onboard green light is turned on through DIO, and when the PWM turns on the red light, the DIO turns off the onboard green light.
Text description is always less intuitive than graphic description, so here is the picture:
Fig 1
2. Function realization
This article is based on porting the MCAL code to the S32DS demo. Then, on the S32DS platform, MCAL related modules are configured through EB, and then compiled and downloaded for simulation through S32DS. Of course, if you like the command line mode, you can directly use the xmd file configured by EB, and then compile with VScode. The process is also very simple. This article will not go into details about the command line method.
2.1 Hardware and software platform
Board:S32K344EVB,also can use other K3 boards.
IDE:S32DS3.5
RTD: K344 RTD 400
MCAL tool: EBtresos Studio 29.0
2.2 Software control process
Before talking about the specific MCAL configuration, here is the software flow chart of the functions in this article:
Fig 2
Here you can see that the default situation is the one configured through MCAL, and then the PWM frequency is also modified in the code, and the PWM duty cycle is modified by keystrokes and loop delays.
2.3 Resource Allocation Overview
The hardware resources and functions used in this article are listed as follows:
Fig 3
The configuration of emios related buses is as follows:
Fig 4
It should be noted here that for the master bus and bus mode in MCL emios, it is necessary to select the appropriate PWM mode and counter bus in the PWM module, otherwise either the configuration will be wrong or the correct PWM waveform cannot be generated.
From the official S32K3 RM, you can check the clock channel and bus type:
Fig 5
For example, if EMIOS_CH23 is selected in PWM0, then this bus corresponds to bus A, and this clock can be used for all channels, so PWM0 is CH12 and can use Bus A. CH22 corresponds to bus F, and this clock can also be used for all channels, so it is no problem to select Bus F for PWM1 CH4. CH0 corresponds to bus B, and this clock corresponds to channels 0-7, so it can also be used for PWM2 is CH2. When selecting the counter bus clock source for your EMIOS channel, you must consider the channel coverage of the counter bus.
In addition to the selection of the counter bus, there is also the PWM mode selection, which is easier to handle. For clock counting up, select OPWMB, and for clock counting up and down, select OPWMCB center-aligned PWM.
In actual use, the mode selection is usually determined based on one's own PWM requirements, and then based on the following RM table:
Fig 6
You can find the channel types that the corresponding mode can support, and then select the corresponding channel, counter bus, etc. according to the channel type in Figure 5. With these basic knowledge, we can directly enter the EB configuration.
2.4 EB configuration
Here we list all the modules used in EB tresos related to this article, and focus on the modules that require specific configuration.
Fig 7
2.4.1 Dio module
The DioPort interface needs to be configured. The main purpose is to configure PTA30, onboard green light, select DioPort Id=1, Dio Channel Id=14. The rules for DioPortId and Dio Channel Id are as follows:
Channel = DioChannelId + DioPortId∗16
For S32K3X4 derivatives
– Port AL=0
– Port AH=1
– Port BL=2
– Port BH=3
– Port CL=4
– Port CH=5
– Port DL=6
– Port DH=7
– Port EL=8
– Port EH=9
– Port FL=10
– Port FH=11
– Port GL=12
– Port GH=13
PTA30=>30=DioChannelId(14)+DioPortId*16
2.4.2 Icu module
First, configure IcuSiul2, the goal is to enable the input interrupt of onboard SW5, PTB26. PTB26 corresponds to EIRQ[13],
Then, the corresponding interrupt situation is as follows:
Fig 8
Fig 9
(1) Icu->IcuSiul2->IcuSiul2Channels:13
(2) Icu->IcuChannel configuration is:
Fig 10
Select IcuChannelRef as the previously configured IcuSiul2Channels, and add the interrupt notification function:
User_EdgeDetect, note that this function is the name of the user interrupt processing function that needs to be added in the code.
(3)Icu->IcuHwInterruptConfigList-> ICU Peripheral ISR Name: SIUL2_0_IRQ_CH_13,IcuIsrEnable enable
2.4.3 Mcl module
This module is mainly used to configure emios counting clock, trgmux, and LCU configuration.
(1)Mcl->Trgmux Logic Instance->Hardware Instance: TRGMUX_IP_HW_INST_0
(2) Mcl->Trgmux Logic Group:
Fig 11
The main purpose is to connect PWM1 Emios0_ch4 to LCU0_IN0 through Trigmux.
(3)Mcl->LCU Configuration
Here is the configuration of the LCU module. The main function is to configure the logic input and logic output. There is one input IN0 and two outputs OUT0 and OUT1.
Fig 12
Fig 13
Fig 14
OUTPUT0 value is 0XAAAA=43690,
OUTPUT1 value is 0X5555=21845
The purpose of this is to generate a pair of complementary PWMs from the input PWM.
Fig 15
(4)Mcl->Emios Common
Add two Emios, EMIOS_0 and EMIOS_1, which means two EMIOS are used.
EMIOS0 is configured with two master bus channels: CH_22 and CH_0, and has different mode types, counting up and down and counting up.
EMIOS1 is configured with one master bus channel: CH_23, counting up
Corresponding to Figure 4.
Fig 16
Fig 17
Note that the channel here is not the actual PWM output channel, but the counter bus channel of the PWM channel, which has the ability to provide clocks.
2.4.4 Mcu module
This module is the basis for the entire MCU to configure the clock. When using the default setting of the original RTD PWM demo, there is only one point that needs attention
Mcu->McuClockSettingConfig_0->McuClockReferencePoint->McuClockReferencePoint_0->core clock 48MHZ.
This clock is the source of the EMIOS clock. With the clock source, it is not difficult to calculate the actual PWM frequency according to the set period. For example, if a 1Khz PWM is required, you can configure period=48M/1K=48000
2.4.5 Platform module
Platform->Interrupt Controller->IntCtrlConfig0, enable SIUL_1_IRQn, and add Handler as:
SIUL2_EXT_IRQ_8_15_ISR
Note that this SIUL2_EXT_IRQ_8_15_ISR is not written randomly, but must correspond to the one in Siul2_Icu_Ip_Irq.c, otherwise an error will be reported.
Fig18
Different interrupts have different interrupt service functions. You need to find the function name defined in the code and fill it into EB.
EB configuration is as follows:
Fig 19
2.4.6 Port module
The configuration of 8 pins is as follows:
Fig 20
As you can see, there are 3 main EMIOS PWMs, one input interrupt, one output GPIO, and two output LCU complementary PWMs.
2.4.7 Pwm module
Mcl configures the counter clock channel of EMIOS. The PWM channels to be output need to be configured in the Pwm module and linked to the MCU clock source and the emios counter bus source in Mcl.
(1) Pwm->PwmEmios
Add two groups for the corresponding EMIOS modules. For example, this article uses EMIOS0 and EMIOS1, so two need to be added:
Fig 21
For PwmEmios_1, there is one channel, and the configuration is as follows:
Fig 22
PwmEmiosBusRef: /Mcl/Mcl/MclConfig/EmiosCommon_1/EmiosMclMasterBus_0
Fig 23
As you can see, the busRef of PwmEmios_1 here is Emios_ch_23 in Mcl, that is, BusA.
That is to say, the bus reference clock used by EMIOS1_CH12 comes from EMIOS1_CH23, that is, Bus A.
There are two channels configured in PwmEmios_0, and the configuration is as follows:
Fig 24
Fig 25
The bus reference clock used by EMIOS0_CH4 comes from EMIOS0_CH22, which is Bus F.
Another EMIOS0 channel:
Fig 26
Fig 27
The bus reference clock used by EMIOS0_CH2 comes from EMIOS0_CH0, that is, Bus B, so select Bus BCDE.
At this point, we can clearly understand the relationship between the real PWM output channel and the internal MCL Emios counter bus channel.
(2)Pwm->PwmEmios
With the specific information of PWM configured above, we can directly configure the PWM channels. There are three channels in total:
PWM0, PWM1, PWM2, which are also the flags needed in the code.
Fig 28
2.5 main code
#include "Pwm.h"
#include "Mcu.h"
#include "Port.h"
#include "Mcl.h"
#include "Platform.h"
#include "Dio.h"
#include "Icu.h"
//#include "check_example.h"
#define NUM_BLINK_LED (uint32)10U
#define DELAY_TIMER (uint32)5000000U
#define MCL_EMIOS_1_CH_23 (uint16)279U
#define MCL_EMIOS_0_CH_22 (uint16)22U
Mcl_LcuSyncOutputValueType PWM_OutputList[2];
volatile uint8 UserCountIrqCH0;
void TestDelay(uint32 delay);
void TestDelay(uint32 delay)
{
static volatile uint32 DelayTimer = 0;
while(DelayTimer<delay)
{
DelayTimer++;
}
DelayTimer=0;
}
void User_EdgeDetect(void)
{
/* increment IRQ counter */
UserCountIrqCH0++;
if(UserCountIrqCH0 % 2 == 0)
{
Pwm_SetDutyCycle(PwmChannel_2, 0X6000);
}
else
{
Pwm_SetDutyCycle(PwmChannel_2, 0X2000);
}
}
int main(void)
{
uint8 num_blink = 0U, i = 0;
uint16 duty_cnt = 0;
UserCountIrqCH0 = 0U;
/* Initialize the Mcu driver */
Mcu_Init(&Mcu_Config_VS_0);
/* Initialize the clock tree */
Mcu_InitClock(McuClockSettingConfig_0);
/* Apply a mode configuration */
Mcu_SetMode(McuModeSettingConf_0);
Platform_Init(NULL_PTR);
/* Initialize all pins using the Port driver */
Port_Init(&Port_Config_VS_0);
/* Initialize Mcl driver */
Mcl_Init(&Mcl_Config_VS_0);
/* Initialize the Icu driver */
Icu_Init(NULL_PTR);
Icu_EnableEdgeDetection(IcuChannel_0);
Icu_EnableNotification(IcuChannel_0);
/* Initialize Pwm driver , after that Led on*/
Pwm_Init(&Pwm_Config_VS_0);
/* PTA29 duty cycle is 50% */
Pwm_SetDutyCycle(PwmChannel_0, 0X4000);
// PTB16,pwm1 , emios0_ch4, use trigmux LCU output 2 Complementarity PWM
Mcl_LcuSyncOutputValueType lcuEnable[2U];
lcuEnable[0].LogicOutputId = 0;
lcuEnable[0].Value = 1U;
lcuEnable[1].LogicOutputId = 1;
lcuEnable[1].Value = 1U;
Mcl_SetLcuSyncOutputEnable(lcuEnable, 2U);
TestDelay(DELAY_TIMER);
/* Set new period for all channels used external counter bus */
Mcl_Emios_SetCounterBusPeriod(MCL_EMIOS_1_CH_23, 4800, FALSE); // pwmchannel_0 10Khz
Mcl_Emios_SetCounterBusPeriod(MCL_EMIOS_0_CH_22, 1200, FALSE);// for PwmChannel_1, 20Khz
// PWM0: 10kHZ
//PWM1: 20KHZ
//PWM2:1KHZ
// PTA29 10kHZ
/* PTA29 duty cycle is 50% */
Pwm_SetDutyCycle(PwmChannel_0, 0X4000);
/* Setup new duty cycle to the pin*/
Pwm_SetDutyCycle(PwmChannel_1, 0x4000);
for(i=0; i <= 10; i++)
{
duty_cnt = i * 0x800;
Pwm_SetDutyCycle(PwmChannel_0, duty_cnt);
TestDelay(DELAY_TIMER);
}
/* Using duty cycle 0% and 100% to Blink LED */
while(1)
{
/* pwm1 when duty cycle is 75% */
Pwm_SetDutyCycle(PwmChannel_1, 0X6000);
//Led off
Pwm_SetDutyCycle(PwmChannel_0, 0X0000); //red off
Dio_WriteChannel(DioConf_DioChannel_Digital_ledgreenPTA30, STD_HIGH); //green on
TestDelay(DELAY_TIMER);
/* pwm 1 when duty cycle is 25% */
Pwm_SetDutyCycle(PwmChannel_1, 0X2000);
//Led ON
Dio_WriteChannel(DioConf_DioChannel_Digital_ledgreenPTA30, STD_LOW); //Green OFF
Pwm_SetDutyCycle(PwmChannel_0, 0X8000); //RED ON
TestDelay(DELAY_TIMER);
num_blink++;
}
/* De-Initialize Pwm driver */
Pwm_DeInit();
//Exit_Example(TRUE);
return 0U;
}
3. Test Result
After power-on, the onboard red light flashes, then gradually turns bright from off, and flashes alternately with the green light.
Test PWM1 PTB16: EMIOS0_CH4, the waveform is 20Khz after stabilization, and the duty cycle changes alternately between 25% and 75%. PWM2 PTB14: EMIOS0_CH2, after stabilization, the frequency is 1KHZ, and as the onboard SW5 is pressed, the duty cycle changes alternately between 25% and 75%.
Test PTD3, PTD2, it can be seen that it is a pair of complementary waveforms, and the frequency is the same as PTB16, and the duty cycle change rule is also the same. It can be seen that the key interrupt, 3 main PWM, and 2 LCU PWM in this article are already working.
Fig 29
Fig 30
查看全文