【先楫HPM6750EVK2测评】硬件spi驱动ad9833波形发生器芯片
[复制链接]
本帖最后由 dql2016 于 2023-5-14 22:21 编辑
例程提供了SPI主机轮询的参考代码,根据例程修改,能够十分简单的将1SPI接口应用起来,这里驱动常见的SPI接口DDS芯片AD9833代码如下:
/*
* Copyright (c) 2021 HPMicro
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/
#include "board.h"
#include "hpm_debug_console.h"
#include "hpm_spi_drv.h"
#include "hpm_clock_drv.h"
#define FMCLK 25000000 // Master Clock On AD9833
enum WaveType{SIN, SQR, TRI}; // Wave Selection Enum
uint16_t FRQLW = 0; // MSB of Frequency Tuning Word
uint16_t FRQHW = 0; // LSB of Frequency Tuning Word
uint32_t phaseVal=0; // Phase Tuning Value
uint8_t WKNOWN=0; // Flag Variable
// -------------------------------- Functions --------------------------------
// ------------------------------------------------ Software SPI Function
void writeSPI(uint16_t word)
{
hpm_stat_t stat = spi_write_data(BOARD_APP_SPI_BASE, 2, &word, 2);
if (stat == status_success)
{
printf("SPI-Master transfer ends.\n");
}
else
{
printf("SPI-Master transfer error[%d]!\n", stat);
}
}
// ------------------------------------------------ Sets Output Wave Type
void AD9833_SetWave(uint16_t Wave)
{
switch(Wave){
case 0:
writeSPI(0x2000); // Value for Sinusoidal Wave
WKNOWN=0;
break;
case 1:
writeSPI(0x2028); // Value for Square Wave
WKNOWN=1;
break;
case 2:
writeSPI(0x2002); // Value for Triangle Wave
WKNOWN=2;
break;
default:
break;
}
}
// ------------------------------------------------ Sets Wave Frequency & Phase (In Degree) In PHASE0 & FREQ0 Registers
void AD9833_SetWaveData(float Frequency,float Phase)
{
// ---------- Tuning Word for Phase ( 0 - 360 Degree )
if(Phase<0)Phase=0; // Changing Phase Value to Positive
if(Phase>360)Phase=360; // Maximum value For Phase (In Degree)
phaseVal = ((int)(Phase*(4096/360)))|0XC000; // 4096/360 = 11.37 change per Degree for Register And using 0xC000 which is Phase 0 Register Address
// ---------- Tuning word for Frequency
long freq=0;
freq=(int)(((Frequency*pow(2,28))/FMCLK)+1); // Tuning Word
FRQHW=(int)((freq & 0xFFFC000) >> 14); // FREQ MSB
FRQLW=(int)(freq & 0x3FFF); // FREQ LSB
FRQLW |= 0x4000;
FRQHW |= 0x4000;
// ------------------------------------------------ Writing DATA
writeSPI(0x2100); // enable 16bit words and set reset bit
writeSPI(FRQLW);
writeSPI(FRQHW);
writeSPI(phaseVal);
writeSPI(0x2000); // clear reset bit
AD9833_SetWave(WKNOWN);
return;
}
// ------------------------------------------------ Initializing AD9833
void AD9833_Init(uint16_t WaveType,float FRQ,float Phase)
{
AD9833_SetWave(WaveType); // Type Of Wave
AD9833_SetWaveData(FRQ,Phase); // Frequency & Phase Set
return;
}
int main(void)
{
uint8_t wbuff[10] = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9};
uint8_t rbuff[10] = {0};
spi_timing_config_t timing_config = {0};
spi_format_config_t format_config = {0};
spi_control_config_t control_config = {0};
hpm_stat_t stat;
/* bsp initialization */
board_init();
board_init_spi_clock(BOARD_APP_SPI_BASE);
board_init_spi_pins(BOARD_APP_SPI_BASE);
printf("SPI-Master Polling Transfer Example\n");
/* set SPI sclk frequency for master */
spi_master_get_default_timing_config(&timing_config);
timing_config.master_config.clk_src_freq_in_hz = clock_get_frequency(BOARD_APP_SPI_CLK_NAME);;
timing_config.master_config.sclk_freq_in_hz = BOARD_APP_SPI_SCLK_FREQ;
spi_master_timing_init(BOARD_APP_SPI_BASE, &timing_config);
printf("SPI-Master transfer timing is configured.\n");
printf("SPI-Master transfer source clock frequency: %dHz\n", timing_config.master_config.clk_src_freq_in_hz);
printf("SPI-Master transfer sclk frequency: %dHz\n", timing_config.master_config.sclk_freq_in_hz);
/* set SPI format config for master */
spi_master_get_default_format_config(&format_config);
format_config.common_config.data_len_in_bits = BOARD_APP_SPI_DATA_LEN_IN_BITS;
format_config.common_config.mode = spi_master_mode;
format_config.common_config.cpol = spi_sclk_high_idle;
format_config.common_config.cpha = spi_sclk_sampling_even_clk_edges;
spi_format_init(BOARD_APP_SPI_BASE, &format_config);
printf("SPI-Master transfer format is configured.\n");
/* set SPI control config for master */
spi_master_get_default_control_config(&control_config);
control_config.master_config.cmd_enable = false; /* cmd phase control for master */
control_config.master_config.addr_enable = false; /* address phase control for master */
control_config.common_config.trans_mode = spi_trans_write_read_together;
printf("SPI-Master transfer starts.\n");
AD9833_Init(SIN,10000,0);
while(1)
{
}
return 0;
}
|