2015-06-30 06:49 AM
Hi!
I'm trying to communicate with ahttp://www.cirrus.com/en/pubs/proDatasheet/CS4344-45-48_F2.pdf
DAC via I²S but I can't get it to work. I followed the steps on the STM32's HAL but I guess I made one or several mistakes as I don't get any output on any of the I²S pins. Here is my code :
#include ''mbed.h''
I2S_InitTypeDef CS4344_conf;
I2S_HandleTypeDef CS4344;
GPIO_InitTypeDef SDIN;
GPIO_InitTypeDef SCLK;
GPIO_InitTypeDef LRCLK;
GPIO_InitTypeDef MCLK;
uint16_t buf[2] = {0x1245, 0x6842};
int
main() {
CS4344_conf.Mode = I2S_MODE_MASTER_TX;
CS4344_conf.Standard = I2S_STANDARD_PHILIPS;
CS4344_conf.DataFormat = I2S_DATAFORMAT_16B;
CS4344_conf.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
CS4344_conf.AudioFreq = I2S_AUDIOFREQ_96K;
CS4344_conf.CPOL = I2S_CPOL_LOW;
CS4344_conf.ClockSource = I2S_CLOCK_PLL;
CS4344_conf.FullDuplexMode = I2S_FULLDUPLEXMODE_DISABLE;
CS4Instance = SPI3;
CS4Init = CS4344_conf;
HAL_I2S_MspInit (&CS4344);
//-------- Enable the SPIx interface clock.
__HAL_RCC_SPI3_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_I2S_ENABLE(&CS4344);
//-------- I2S pins configuration:
SDIN.Pin = GPIO_PIN_5;
SDIN.Mode = GPIO_MODE_AF_PP;
SDIN.Pull = GPIO_PULLUP;
SDIN.Alternate = GPIO_AF6_SPI3;
SCLK.Pin = GPIO_PIN_3;
SCLK.Mode = GPIO_MODE_AF_PP;
SCLK.Pull = GPIO_PULLUP;
SCLK.Alternate = GPIO_AF6_SPI3;
LRCLK.Pin = GPIO_PIN_15;
LRCLK.Mode = GPIO_MODE_AF_PP;
LRCLK.Pull = GPIO_PULLUP;
LRCLK.Alternate = GPIO_AF6_SPI3;
MCLK.Pin = GPIO_PIN_10;
MCLK.Mode = GPIO_MODE_AF_PP;
MCLK.Pull = GPIO_PULLUP;
MCLK.Alternate = GPIO_AF6_SPI3;
HAL_GPIO_Init (GPIOB, &SDIN);
HAL_GPIO_Init (GPIOB, &SCLK);
HAL_GPIO_Init (GPIOB, &MCLK);
HAL_GPIO_Init (GPIOA, &LRCLK);
// ------------- Program the Mode, Standard, Data Format, MCLK Output, Audio frequency and Polarity using HAL_I2S_Init() function.
//HAL_I2S_MspInit (&CS4344);
HAL_I2S_Init (&CS4344);
while
(1) {
HAL_I2S_Transmit (&CS4344, buf, 2, 1000);
}
}
What am I doing wrong? all the other example codes on the internet are using a different HAL (the functions don't match to the ones in the reference...)
2015-06-30 07:42 AM
I also made a second code using bits of things I found online :
#include ''mbed.h''
#define M_PI 3,1415926535897932384626433832795
const
uint16_t wt01[255] = { 2047, 2097, 2147, 2198, 2248, 2298, 2348, 2398, 2447, 2497, 2546, 2595, 2643, 2691, 2739, 2786, 2833, 2879, 2925, 2970, 3015, \
3059, 3103, 3145, 3188, 3229, 3270, 3310, 3349, 3388, 3426, 3462, 3498, 3534, 3568, 3601, 3633, 3665, 3695, 3725, 3753, 3780, \
3807, 3832, 3856, 3879, 3901, 3922, 3941, 3960, 3977, 3993, 4008, 4022, 4035, 4046, 4056, 4065, 4073, 4079, 4085, 4089, 4092, \
4093, 4093, 4093, 4090, 4087, 4082, 4076, 4069, 4061, 4051, 4041, 4029, 4015, 4001, 3985, 3969, 3951, 3932, 3911, 3890, 3868, \
3844, 3819, 3793, 3767, 3739, 3710, 3680, 3649, 3617, 3584, 3551, 3516, 3481, 3444, 3407, 3369, 3330, 3290, 3250, 3209, 3167, \
3124, 3081, 3037, 2993, 2948, 2902, 2856, 2809, 2762, 2715, 2667, 2619, 2570, 2521, 2472, 2423, 2373, 2323, 2273, 2223, 2173, \
2122, 2072, 2021, 1971, 1920, 1870, 1820, 1770, 1720, 1670, 1621, 1572, 1523, 1474, 1426, 1378, 1331, 1284, 1237, 1191, 1145, \
1100, 1056, 1012, 969, 926, 884, 843, 803, 763, 724, 686, 649, 612, 577, 542, 509, 476, 444, 413, 383, 354, 326, 300, 274, 249, \
225, 203, 182, 161, 142, 124, 108, 92, 78, 64, 52, 42, 32, 24, 17, 11, 6, 3, 0, 0, 0, 1, 4, 8, 14, 20, 28, 37, 47, 58, 71, 85, \
100, 116, 133, 152, 171, 192, 214, 237, 261, 286, 313, 340, 368, 398, 428, 460, 492, 525, 559, 595, 631, 667, 705, 744, 783, \
823, 864, 905, 948, 990, 1034, 1078, 1123, 1168, 1214, 1260, 1307, 1354, 1402, 1450, 1498, 1547, 1596, 1646, 1695, 1745, 1795, \
1845, 1895, 1946, 1996 };
I2S_HandleTypeDef hi2s2;
/* I2S2 init function */
void
MX_GPIO_Init(
void
) {
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__GPIOH_CLK_ENABLE();
__GPIOC_CLK_ENABLE();
__GPIOB_CLK_ENABLE();
__GPIOA_CLK_ENABLE();
/*Configure GPIO pin : PC9 */
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
static
void
SystemClock_Config(
void
)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
}
void
MX_I2S2_Init(
void
)
{
hi2s2.Instance = SPI2;
hi2s2.Init.Mode = I2S_MODE_MASTER_RX;
hi2s2.Init.Standard = I2S_STANDARD_PHILLIPS;
hi2s2.Init.DataFormat = I2S_DATAFORMAT_16B;
hi2s2.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
hi2s2.Init.AudioFreq = I2S_AUDIOFREQ_44K;
hi2s2.Init.CPOL = I2S_CPOL_LOW;
hi2s2.Init.ClockSource = I2S_CLOCK_PLL;
hi2s2.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_ENABLE;
HAL_I2S_Init(&hi2s2);
}
void
HAL_I2S_MspInit(I2S_HandleTypeDef* hi2s)
{
GPIO_InitTypeDef GPIO_InitStruct;
if
(hi2s->Instance==SPI2)
{
/* Peripheral clock enable */
__SPI2_CLK_ENABLE();
/**I2S2 GPIO Configuration
PC2 ------> I2S2_ext_SD
PC3 ------> I2S2_SD
PA6 ------> I2S2_MCK
PB10 ------> I2S2_CK
PB12 ------> I2S2_WS
*/
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
GPIO_InitStruct.Alternate = GPIO_AF6_I2S2ext;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
GPIO_InitStruct.Alternate = GPIO_AF6_SPI2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
}
/* USER CODE BEGIN 0 */
#define BufferSize 255
uint16_t InputBuffer[BufferSize];
uint16_t OutputBuffer[BufferSize];
/* USER CODE END 0 */
int
main(
void
)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2S2_Init();
/* USER CODE BEGIN 2 */
uint16_t i;
for
(i=0;i<BufferSize;i++)
{
InputBuffer[i]=0xFFFF;
}
for
(i=0;i<BufferSize;i++){
OutputBuffer[i]=wt01[i];
}
// HAL_I2S_Receive_DMA(&hi2s2, InputBuffer, 20000);
while
(__HAL_I2S_GET_FLAG(&hi2s2, I2S_FLAG_TXE) != 1);
HAL_I2SEx_TransmitReceive(&hi2s2, OutputBuffer, InputBuffer, BufferSize, 1000);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while
(1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
while
(__HAL_I2S_GET_FLAG(&hi2s2, I2S_FLAG_TXE) != 1);
for
(i=0;i<BufferSize;i++)
{
OutputBuffer[i]=InputBuffer[i];
}
HAL_I2SEx_TransmitReceive(&hi2s2, OutputBuffer, InputBuffer, BufferSize , 1000);
}
/* USER CODE END 3 */
}
But it's not working either...