AI广告全链路解析:从CTR预估到智能出价
2026/8/30 7:26:32
#include "derivative.h" /* include peripheral declarations */ #include "project.h" #include "mode_entry.h" #include "gpio.h" #include "uart.h" #include "spi.h" extern void xcptn_xmpl(void); void peri_clock_gating(void); /* Configure gating/enabling peri. clocks */ __attribute__ ((section(".text"))) int main(void) { int counter = 0; xcptn_xmpl (); /* Configure and Enable Interrupts */ peri_clock_gating(); /* Configure gating/enabling peri. clocks for modes*/ system160mhz(); /* sysclk=160MHz, dividers configured, mode trans*/ gpio_output(PA5,SIUL2_SRC_FULL_DIS,SIUL2_SSS_1); //PA5 SPI1_CS0 gpio_output(PA6,SIUL2_SRC_FULL_DIS,SIUL2_SSS_1); //PA6 SPI1_SCK gpio_output(PA7,SIUL2_SRC_FULL_DIS,SIUL2_SSS_1); //PA7 SPI1_MOSI gpio_input(PA8,SIUL2_NULL,44,SIUL2_SSS_1); //PA8 SPI1_MISO spi_init(SPI1,SPI_PCS0,SPI_POL_LOW,SPI_PHA_1,SPI_BR_16); uint8_t RecDataMaster[4]; uint8_t SenDataMaster[4]; uint16_t time1,time2; SenDataMaster[0] = 0x9F; SenDataMaster[1] = 0xFF; SenDataMaster[2] = 0xFF; SenDataMaster[3] = 0xFF; for(;;) { spi_swap_string(SPI1,&SenDataMaster,&RecDataMaster,4); counter++; } return 0; } void peri_clock_gating (void) { MC_ME.RUN_PC[0].R = 0x00000000; /* gate off clock for all RUN modes */ MC_ME.RUN_PC[1].R = 0x000000FE; /* enable peri clock for all RUN modes */ /* Note: RUN_PC0 is default cfg for PCTLs */ MC_ME.PCTL204.B.RUN_CFG = 0x00000001; //开启LIN_0时钟门 MC_ME.PCTL91.B.RUN_CFG = 0x00000001; //开启LIN_1时钟门 MC_ME.PCTL98.B.RUN_CFG = 0x00000001; //开启SPI_1时钟门 }spi.c文件
/** * @file spi.c * @brief spi驱动 * @details 适配MPC5744P * @author loopy * @date 2026-08-16 * @version V1.0 * @note 无 */ //头文件包含 #include "spi.h" #include "project.h" volatile struct SPI_tag *SPI[SPI_MAX] = {&SPI_0, &SPI_1, &SPI_2, &SPI_3}; /** * @brief spi初始化函数 * @param spi_num: SPI模块编号 * @param spi_pcs: 启用的片选引脚 * @param spi_pol: SCK的极性 * @param spi_pha: 相位 * @param spi_br: 分频系数 * @return 无 * @note 无 */ void spi_init(spi_num spi_num,uint8_t spi_pcs,spi_pol spi_pol,spi_pha spi_pha,uint8_t spi_br) { SPI[spi_num]->MCR.B.MSTR = 1; //使能主机模式 SPI[spi_num]->MCR.B.PCSIS = spi_pcs; //使能片选引脚 SPI[spi_num]->MCR.B.MDIS = 0; //SPI[spi_num]->MCR.B.MTFE = 1; //SPI[spi_num]->MCR.B.SMPL_PT = 0; SPI[spi_num]->MCR.B.HALT = 1; //停止模式下配置 SPI[spi_num]->MODE.CTAR[0].B.CPOL = spi_pol; SPI[spi_num]->MODE.CTAR[0].B.CPHA = spi_pha; SPI[spi_num]->MODE.CTAR[0].B.FMSZ = 7; //8字节传输 /* 时钟计算SCK baud rate = (fP /PBR) x [(1+DBR)/BR] * fP为外设时钟频率,在本工程中已配置为 fP = 40MHz * PBR默认值为2,DBR默认值为0,BR根据自己需求设置 */ SPI[spi_num]->MODE.CTAR[0].B.BR = spi_br; //分频系数 SPI[spi_num]->MCR.B.HALT = 0; //停止模式下配置 } /** * @brief spi发送接收字节函数 * @param spi_num: SPI模块编号 * @param byte: 传输的byte * @param spi_keepcs: 启用的片选引脚 * @return recdata: 读取返回值 * @note 当前默认片选脚0 */ uint8_t spi_swap_byte(spi_num spi_num,uint8_t byte,spi_keepcs spi_keepcs) { uint8_t recdata = 0; SPI[spi_num]->PUSHR.PUSHR.R = 0x08010000 | byte | (uint32_t)((uint32_t)spi_keepcs<<31); while (SPI[spi_num]->SR.B.TCF != 1){} while (SPI[spi_num]->SR.B.RFDF != 1){} recdata = SPI[spi_num]->POPR.R; SPI[spi_num]->SR.R = 0xDA0A0000; return recdata; } /** * @brief spi发送接收字符串函数 * @param spi_num: SPI模块编号 * @param *send_data: 发送的数组 * @param *read_data: 接收的数组 * @param len: 数组长度 * @return 无 * @note 无 */ void spi_swap_string(spi_num spi_num,uint8_t *send_data,uint8_t *read_data,uint8_t len) { uint8_t i = 0; for(i=0;i<len;i++) { if(i == len - 1) { read_data[i] = spi_swap_byte(spi_num,send_data[i],SPI_RETURN_PCS); } else { read_data[i] = spi_swap_byte(spi_num,send_data[i],SPI_KEEP_PCS); } } }/** * @file spi.h * @brief spi驱动 * @details 适配MPC5744P * @author loopy * @date 2026-08-16 * @version V1.0 * @note 无 */ #ifndef PERIPHERALS_SPI_H_ #define PERIPHERALS_SPI_H_ //头文件包含 #include "MPC5744P.h" //宏定义 #define SPI_PCS0 0b00000001 //片选0 #define SPI_PCS1 0b00000010 //片选1 #define SPI_PCS2 0b00000100 //片选2 #define SPI_PCS3 0b00001000 //片选3 #define SPI_PCS4 0b00010000 //片选4 #define SPI_PCS5 0b00100000 //片选5 #define SPI_PCS6 0b01000000 //片选6 #define SPI_PCS7 0b10000000 //片选7 //数据类型定义 typedef enum //SPI序号 { SPI0 = 0x0, SPI1 = 0x1, SPI2 = 0x2, SPI3 = 0x3, SPI_MAX = 0x4, }spi_num; typedef enum //SPI极性 { SPI_POL_LOW = 0x0, //SCK空闲状态为低电平 SPI_POL_HIGH = 0x1, //SCK空闲状态为高电平 }spi_pol; typedef enum //SPI相位 { SPI_PHA_0 = 0x0, //在 SCK第一个边沿捕获数据,在下一个边沿更新数据 SPI_PHA_1 = 0x1, //在 SCK第一个边沿更新数据,在下一个边沿捕获数据 }spi_pha; typedef enum //SPI 片选保持 { SPI_RETURN_PCS = 0x0, //一个字节传输完成片选恢复 SPI_KEEP_PCS = 0x1, //保持片选 }spi_keepcs; typedef enum //SPI BR系数 { SPI_BR_2 = 0x0, SPI_BR_4 = 0x1, SPI_BR_6 = 0x2, SPI_BR_8 = 0x3, SPI_BR_16 = 0x4, SPI_BR_32 = 0x5, SPI_BR_64 = 0x6, SPI_BR_128 = 0x7, SPI_BR_256 = 0x8, SPI_BR_512 = 0x9, SPI_BR_1024 = 0xA, SPI_BR_2048 = 0xB, SPI_BR_4096 = 0xC, SPI_BR_8192 = 0xD, SPI_BR_16384 = 0xE, SPI_BR_32768 = 0xF, }spi_br; //全局函数声明 /** * @brief spi初始化函数 * @param spi_num: SPI模块编号 * @param spi_pcs: 启用的片选引脚 * @param spi_pol: SCK的极性 * @param spi_pha: 相位 * @param spi_br: 分频系数 * @return 无 * @note 无 */ extern void spi_init(spi_num spi_num,uint8_t spi_pcs,spi_pol spi_pol,spi_pha spi_pha,uint8_t spi_br); /** * @brief spi发送接收函数 * @param spi_num: SPI模块编号 * @param byte: 传输的byte * @param spi_keepcs: 启用的片选引脚 * @return recdata: 读取返回值 * @note 无 */ extern uint8_t spi_swap_byte(spi_num spi_num,uint8_t byte,spi_keepcs spi_keepcs); /** * @brief spi发送接收字符串函数 * @param spi_num: SPI模块编号 * @param *send_data: 发送的数组 * @param *read_data: 接收的数组 * @param len: 数组长度 * @return 无 * @note 无 */ void spi_swap_string(spi_num spi_num,uint8_t *send_data,uint8_t *read_data,uint8_t len); #endif /* PERIPHERALS_SPI_H_ */与W25Q64发0x9F,波形如下: