一、AURIX TM TC275 lite 套件硬件介绍
AURIX TM TC275 lite 套件配备了基于 32 位单芯片 AURIX TM TriCore TM的微控制器 Aurix TM TC275。它可以与一系列开发工具一起使用,包括 AURIX TM Development Studio(Tool overview - Infineon Developer Center)、英飞凌免费的基于 Eclipse 的 IDE(Eclipse Downloads | The Eclipse Foundation),或来自 Hightec/PLS/Infineon 的基于 Eclipse 的“FreeEntryToolchain”。AURIX TM TC275 lite 套件拥有丰富的外设模块。
二、项目设计思路
1、任务的选择
选择任务二,设计一个通过旋转板卡上的电位计,改变呼吸灯闪烁速率的呼吸灯,同时将ADC采集的数据通过串口,发送到电脑上显示。
2、项目思路
首先是选择开发工具AURIX TM Development Studio,可以编写需要完成任务的代码,也可以调用丰富的库和观察Terminal窗口返回的ADC值。
然后根据硬禾学堂在B站提供的学习视频,选择自己需要调用的库“ADC_Single_Channel_1_KIT_TC275_LK”、“ASCLIN_UART_1_KIT_TC275_LK”、“GTM_TOM_PWM_1_KIT_TC275_LK”、“STM_Interrupt_1_KIT_TC275_LK”。建立一个TC275的工程,一共包含三个主函数,使用到CPU0即可,主要是用到自己编写一个名为“Control_LedsPin.c”的文件,文件包含两个函数,一个是初始化函数“void Init_Leds(void)”,内容是对调用的各个库初始化,另一个是“void Control_Leds(void)”,内容是根据读取的电位计ADC值,来控制Led的呼吸频率,使用的是LED1。如下面的流程图所示。
然后使用FT2231芯片进行直接烧录程序时,需要更改库中的引脚,将P15.2/P15.3更改成P14.0/P14.1即可。
三、各功能对应的主要代码片段及说明
1、ADC函数子模块,参考的是“AURIX_code_examples”中的“ADC_Single_Channel_1_KIT_TC275_LK”例程;
/*********************************************************************************************************************/
/*-----------------------------------------------------Includes------------------------------------------------------*/
/*********************************************************************************************************************/
#include "IfxVadc_Adc.h"
#include "ADC_Single_Channel.h"
#include "ADC_Single_Channel.h"
#include "stdint.h"
/*********************************************************************************************************************/
/*------------------------------------------------------Macros-------------------------------------------------------*/
/*********************************************************************************************************************/
#define VADC_GROUP IfxVadc_GroupId_0 /* Use the ADC group 0 */
#define CHANNEL_ID 0 /* Use the Channel 0 */
#define CHANNEL_RESULT_REGISTER 5 /* Use the Result Register 5 */
/*********************************************************************************************************************/
/*-------------------------------------------------Global variables--------------------------------------------------*/
/*********************************************************************************************************************/
ApplicationVadcBackgroundScan g_vadcBackgroundScan;
/*********************************************************************************************************************/
/*---------------------------------------------Function Implementations----------------------------------------------*/
/*********************************************************************************************************************/
/* The VADC module and group are initialized */
void vadcBackgroundScanInit (void)
{
/* VADC module configuration */
/* Create VADC configuration */
IfxVadc_Adc_Config adcConfig;
/* Initialize the VADC configuration with default values */
IfxVadc_Adc_initModuleConfig(&adcConfig, &MODULE_VADC);
/* Initialize the VADC module using the VADC configuration */
IfxVadc_Adc_initModule(&g_vadcBackgroundScan.vadc, &adcConfig);
/* VADC group configuration */
/* Create group configuration */
IfxVadc_Adc_GroupConfig adcGroupConfig;
/* Initialize the group configuration with default values */
IfxVadc_Adc_initGroupConfig(&adcGroupConfig, &g_vadcBackgroundScan.vadc);
/* Define which ADC group is going to be used */
adcGroupConfig.groupId = VADC_GROUP;
adcGroupConfig.master = VADC_GROUP;
/* Enable background scan source */
adcGroupConfig.arbiter.requestSlotBackgroundScanEnabled = TRUE;
/* Enable background auto scan mode */
adcGroupConfig.backgroundScanRequest.autoBackgroundScanEnabled = TRUE;
/* Enable the gate in "always" mode (no edge detection) */
adcGroupConfig.backgroundScanRequest.triggerConfig.gatingMode = IfxVadc_GatingMode_always;
/* Initialize the group using the group configuration */
IfxVadc_Adc_initGroup(&g_vadcBackgroundScan.adcGroup, &adcGroupConfig);
}
/* The input channels to be used are setup and the VADC is set into run mode */
void vadcBackgroundScanRun (void)
{
/* Initialize the channel configuration of application handle g_vadcBackgroundScan with default values */
IfxVadc_Adc_initChannelConfig(&g_vadcBackgroundScan.adcChannelConfig, &g_vadcBackgroundScan.adcGroup);
g_vadcBackgroundScan.adcChannelConfig.channelId = (IfxVadc_ChannelId) CHANNEL_ID;
g_vadcBackgroundScan.adcChannelConfig.resultRegister = (IfxVadc_ChannelResult) CHANNEL_RESULT_REGISTER;
g_vadcBackgroundScan.adcChannelConfig.backgroundChannel = TRUE;
/* Initialize the channel of application handle g_VadcBackgroundScan using the channel configuration */
IfxVadc_Adc_initChannel(&g_vadcBackgroundScan.adcChannel, &g_vadcBackgroundScan.adcChannelConfig);
/* Enable background scan for the channel */
IfxVadc_Adc_setBackgroundScan(&g_vadcBackgroundScan.vadc, &g_vadcBackgroundScan.adcGroup,
(1 << (IfxVadc_ChannelId) CHANNEL_ID), (1 << (IfxVadc_ChannelId) CHANNEL_ID));
/* Start background scan conversion */
IfxVadc_Adc_startBackgroundScan(&g_vadcBackgroundScan.vadc);
}
#define RETRY_MAX 0xf000
uint32_t Adc_GetValue (void)
{
Ifx_VADC_RES conversionResult;
uint16_t retry = 0;
do
{
conversionResult = IfxVadc_Adc_getResult(&g_vadcBackgroundScan.adcChannel);
if (retry++ > RETRY_MAX)
{
return 0xffffffff;
}
}while (!conversionResult.B.VF);
return conversionResult.B.RESULT;
}
2、UART函数子模块,参考的是“AURIX_code_examples”中的“ASCLIN_UART_1_KIT_TC275_LK”例程;
/*********************************************************************************************************************/
/*-----------------------------------------------------Includes------------------------------------------------------*/
/*********************************************************************************************************************/
#include "IfxAsclin_Asc.h"
#include "IfxCpu_Irq.h"
#include "Ifx_Types.h"
/*********************************************************************************************************************/
/*------------------------------------------------------Macros-------------------------------------------------------*/
/*********************************************************************************************************************/
#define UART_BAUDRATE 115200 /* UART baud rate in bit/s */
#define UART_PIN_RX IfxAsclin0_RXA_P14_1_IN /* UART receive port pin */
#define UART_PIN_TX IfxAsclin0_TX_P14_0_OUT /* UART transmit port pin */
/* Definition of the interrupt priorities */
#define INTPRIO_ASCLIN0_RX 18
#define INTPRIO_ASCLIN0_TX 19
#define UART_RX_BUFFER_SIZE 64 /* Definition of the receive buffer size */
#define UART_TX_BUFFER_SIZE 64 /* Definition of the transmit buffer size */
#define SIZE 13 /* Size of the string */
/*********************************************************************************************************************/
/*-------------------------------------------------Global variables--------------------------------------------------*/
/*********************************************************************************************************************/
/* Declaration of the ASC handle */
static IfxAsclin_Asc g_ascHandle;
/* Declaration of the FIFOs parameters */
static uint8 g_ascTxBuffer[UART_TX_BUFFER_SIZE + sizeof(Ifx_Fifo) + 8];
static uint8 g_ascRxBuffer[UART_RX_BUFFER_SIZE + sizeof(Ifx_Fifo) + 8];
/* Definition of txData and rxData */
uint8 g_txData[] = "Hello World!";
uint8 g_rxData[SIZE] = {''};
/* Size of the message */
Ifx_SizeT g_count = sizeof(g_txData);
/*********************************************************************************************************************/
/*---------------------------------------------Function Implementations----------------------------------------------*/
/*********************************************************************************************************************/
/* Adding of the interrupt service routines */
IFX_INTERRUPT(asclin0TxISR, 0, INTPRIO_ASCLIN0_TX);
void asclin0TxISR(void)
{
IfxAsclin_Asc_isrTransmit(&g_ascHandle);
}
IFX_INTERRUPT(asclin0RxISR, 0, INTPRIO_ASCLIN0_RX);
void asclin0RxISR(void)
{
IfxAsclin_Asc_isrReceive(&g_ascHandle);
}
/* This function initializes the ASCLIN UART module */
void init_ASCLIN_UART(void)
{
/* Initialize an instance of IfxAsclin_Asc_Config with default values */
IfxAsclin_Asc_Config ascConfig;
IfxAsclin_Asc_initModuleConfig(&ascConfig, &MODULE_ASCLIN0);
/* Set the desired baud rate */
ascConfig.baudrate.baudrate = UART_BAUDRATE;
/* ISR priorities and interrupt target */
ascConfig.interrupt.txPriority = INTPRIO_ASCLIN0_TX;
ascConfig.interrupt.rxPriority = INTPRIO_ASCLIN0_RX;
ascConfig.interrupt.typeOfService = IfxCpu_Irq_getTos(IfxCpu_getCoreIndex());
/* FIFO configuration */
ascConfig.txBuffer = &g_ascTxBuffer;
ascConfig.txBufferSize = UART_TX_BUFFER_SIZE;
ascConfig.rxBuffer = &g_ascRxBuffer;
ascConfig.rxBufferSize = UART_RX_BUFFER_SIZE;
/* Pin configuration */
const IfxAsclin_Asc_Pins pins =
{
NULL_PTR, IfxPort_InputMode_pullUp, /* CTS pin not used */
&UART_PIN_RX, IfxPort_InputMode_pullUp, /* RX pin */
NULL_PTR, IfxPort_OutputMode_pushPull, /* RTS pin not used */
&UART_PIN_TX, IfxPort_OutputMode_pushPull, /* TX pin */
IfxPort_PadDriver_cmosAutomotiveSpeed1
};
ascConfig.pins = &pins;
IfxAsclin_Asc_initModule(&g_ascHandle, &ascConfig); /* Initialize module with above parameters */
}
/* This function sends and receives the string "Hello World!" */
//void send_receive_ASCLIN_UART_message(void)
//{
// IfxAsclin_Asc_write(&g_ascHandle, g_txData, &g_count, TIME_INFINITE); /* Transmit data via TX */
// IfxAsclin_Asc_read(&g_ascHandle, g_rxData, &g_count, TIME_INFINITE); /* Receive data via RX */
//}
void Uart_SendStr(char* str) {
Ifx_SizeT len = (Ifx_SizeT)strlen(str);
IfxAsclin_Asc_write(&g_ascHandle, str, &len, TIME_INFINITE); /* Transmit data via TX */
}
3、PWM函数子模块,参考的是“AURIX_code_examples”中的“GTM_TOM_PWM_1_KIT_TC275_LK”例程;
/*********************************************************************************************************************/
/*-----------------------------------------------------Includes------------------------------------------------------*/
/*********************************************************************************************************************/
#include "GTM_TOM_PWM.h"
#include "Ifx_Types.h"
#include "IfxGtm_Tom_Pwm.h"
/*********************************************************************************************************************/
/*------------------------------------------------------Macros-------------------------------------------------------*/
/*********************************************************************************************************************/
#define ISR_PRIORITY_TOM 20 /* Interrupt priority number */
#define LED IfxGtm_TOM1_4_TOUT14_P00_5_OUT /* LED which will be driven by the PWM */
#define PWM_PERIOD 50000 /* PWM period for the TOM */
#define FADE_STEP PWM_PERIOD / 100 /* PWM duty cycle for the TOM */
/*********************************************************************************************************************/
/*-------------------------------------------------Global variables--------------------------------------------------*/
/*********************************************************************************************************************/
IfxGtm_Tom_Pwm_Config g_tomConfig; /* Timer configuration structure */
IfxGtm_Tom_Pwm_Driver g_tomDriver; /* Timer Driver structure */
uint32 g_fadeValue = 0; /* Fade value, starting from 0 */
sint8 g_fadeDir = 1; /* Fade direction variable */
/*********************************************************************************************************************/
/*-----------------------------------------------Function Prototypes-------------------------------------------------*/
/*********************************************************************************************************************/
void setDutyCycle (uint32 dutyCycle); /* Function to set the duty cycle of the PWM */
/*********************************************************************************************************************/
/*--------------------------------------------Function Implementations-----------------------------------------------*/
/*********************************************************************************************************************/
/* This function initializes the TOM */
void initGtmTomPwm (void)
{
IfxGtm_enable(&MODULE_GTM); /* Enable GTM */
IfxGtm_Cmu_enableClocks(&MODULE_GTM, IFXGTM_CMU_CLKEN_FXCLK); /* Enable the FXU clock */
/* Initialize the configuration structure with default parameters */
IfxGtm_Tom_Pwm_initConfig(&g_tomConfig, &MODULE_GTM);
g_tomConfig.tom = LED.tom; /* Select the TOM depending on the LED */
g_tomConfig.tomChannel = LED.channel; /* Select the channel depending on the LED */
g_tomConfig.period = PWM_PERIOD; /* Set the timer period */
g_tomConfig.pin.outputPin = &LED; /* Set the LED port pin as output */
g_tomConfig.synchronousUpdateEnabled = TRUE; /* Enable synchronous update */
IfxGtm_Tom_Pwm_init(&g_tomDriver, &g_tomConfig); /* Initialize the GTM TOM */
IfxGtm_Tom_Pwm_start(&g_tomDriver, TRUE); /* Start the PWM */
}
/* This function creates the fade effect for the LED */
void fadeLED (void)
{
if ((g_fadeValue + FADE_STEP) >= PWM_PERIOD)
{
g_fadeDir = -1; /* Set the direction of the fade */
}
else if ((g_fadeValue - FADE_STEP) <= 0)
{
g_fadeDir = 1; /* Set the direction of the fade */
}
g_fadeValue += g_fadeDir * FADE_STEP; /* Calculation of the new duty cycle */
setDutyCycle(g_fadeValue); /* Set the duty cycle of the PWM */
}
/* This function sets the duty cycle of the PWM */
void SetDutyCycle (uint32 dutyCycle)
{
g_tomConfig.dutyCycle = dutyCycle* PWM_PERIOD / 100; /* Change the value of the duty cycle */
IfxGtm_Tom_Pwm_init(&g_tomDriver, &g_tomConfig); /* Re-initialize the PWM */
}
4、STM时钟函数子模块,参考的是“AURIX_code_examples”中的“STM_Interrupt_1_KIT_TC275_LK”例程;
/*********************************************************************************************************************/
/*-----------------------------------------------------Includes------------------------------------------------------*/
/*********************************************************************************************************************/
#include "STM_Interrupt.h"
#include "Bsp.h"
#include "IfxPort.h"
#include "IfxStm.h"
#include "stdint.h"
/*********************************************************************************************************************/
/*------------------------------------------------------Macros-------------------------------------------------------*/
/*********************************************************************************************************************/
#define ISR_PRIORITY_STM 40 /* Priority for interrupt ISR */
#define LED &MODULE_P00,5 /* LED toggled in Interrupt Service Routine (ISR) */
#define STM &MODULE_STM0 /* STM0 is used in this example */
/*********************************************************************************************************************/
/*-------------------------------------------------Global variables--------------------------------------------------*/
/*********************************************************************************************************************/
IfxStm_CompareConfig g_STMConf; /* STM configuration structure */
/*********************************************************************************************************************/
/*---------------------------------------------Function Implementations----------------------------------------------*/
/*********************************************************************************************************************/
/* Macro to define Interrupt Service Routine.
* This macro makes following definitions:
* 1) Define linker section as .intvec_tc<vector number>_<interrupt priority>.
* 2) define compiler specific attribute for the interrupt functions.
* 3) define the Interrupt service routine as ISR function.
*
* IFX_INTERRUPT(isr, vectabNum, priority)
* - isr: Name of the ISR function.
* - vectabNum: Vector table number.
* - priority: Interrupt priority. Refer Usage of Interrupt Macro for more details.
*/
/* Function to initialize the STM */
void initSTM (void)
{
IfxStm_initCompareConfig(&g_STMConf); /* Initialize the configuration structure with default values */
// g_STMConf.triggerPriority = ISR_PRIORITY_STM; /* Set the priority of the interrupt */
// g_STMConf.typeOfService = IfxSrc_Tos_cpu0; /* Set the service provider for the interrupts */
// g_STMConf.ticks = g_ticksFor500ms; /* Set the number of ticks after which the timer triggers an
// * interrupt for the first time */
IfxStm_initCompare(STM, &g_STMConf); /* Initialize the STM with the user configuration */
}
void Delay_ms (uint16_t ms)
{
uint32 tick = IfxStm_getTicksFromMilliseconds(&MODULE_STM0, ms);
IfxStm_waitTicks(&MODULE_STM0, tick);
}
5、电位计控制频率函数子模块,对其中一些做了初始化和闪烁频率控制;
#include "stdint.h"
#include "GTM_TOM_PWM.h"
#include "ADC_SINGLE_CHANNEL.h"
#include "math.h"
#include "STM_Interrupt.h"
#include "stdio.h"
#include "ASCLIN_UART.h"
uint32_t Adc_val;
uint8_t Delay;
uint16_t send_count = 0;
char Str[32];
void Init_Leds (void)
{
init_ASCLIN_UART();
Uart_SendStr("Hello World\r\n");
vadcBackgroundScanInit();
vadcBackgroundScanRun();
initSTM();
initGtmTomPwm();
}
void Control_Leds (void)
{
for (uint8_t i = 0; i < 100; i++)
{
SetDutyCycle(i);
Adc_val = Adc_GetValue();
Delay = 2.5 * pow(2, Adc_val / 1024.0);
Delay_ms(Delay);
send_count += Delay;
if (send_count >= 500)
{
send_count -= 500;
sprintf(Str, "Adc_val:%d\r\n", (int) Adc_val);
Uart_SendStr(Str);
}
}
for (uint8_t i = 100; i > 0; i--)
{
SetDutyCycle(i);
Delay = 2.5 * pow(2, Adc_val / 1024.0);
Delay_ms(Delay);
send_count += Delay;
if (send_count >= 500)
{
send_count -= 500;
sprintf(Str, "Adc_val:%d\r\n", (int) Adc_val);
Uart_SendStr(Str);
}
}
}
四、AURIX TM TC275 lite 套件任务完成情况
设计一个呼吸灯,通过旋转板卡上的电位计,改变呼吸灯闪烁速率,同时将ADC采集的数据通过串口/CAN,发送到另一台设备上显示,在这里,另一台设备使用到的是本地的笔记本电脑。
1、设计一个呼吸灯
2、通过串口/CAN,发送到本地电脑上显示
五、对本活动的心得体会
本人在暑假期间参加了第17届智能车比赛,参加组别是无线充电组,最终获得赛区二等奖,然后我主要是队伍里面负责硬件这一部分,还包括一些机械结构问题,如轮胎处理,车重减重,齿轮润滑等,大部分时间在画PCB板子和调整LCC匹配参数,然后我们同样使用的是英飞凌TC系列的芯片,TC264,双核,主频也是200MHz,我也接触了一部分TC264调参的任务,比较有趣,所以此次完成TC275板卡的任务二,还是有一个比较大的成长的,特别是对一些库和例程的参考和学习。祝硬禾学堂越来越好!