128 lines
3.2 KiB
C
128 lines
3.2 KiB
C
/*
|
|
* app.c
|
|
*
|
|
* Created on: Apr 3, 2021
|
|
* Author: Think
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
#include "app.h"
|
|
#include "stm32f4xx_hal.h"
|
|
#include "CS43L22.h"
|
|
|
|
#define HEARTBEAT 1000
|
|
#define BUFFER_SIZE_SINUS 16000
|
|
|
|
int16_t sinusTable[BUFFER_SIZE_SINUS] = { 0 };
|
|
int i = 0;
|
|
|
|
|
|
|
|
extern I2C_HandleTypeDef hi2c1;
|
|
extern I2S_HandleTypeDef hi2s2;
|
|
extern I2S_HandleTypeDef hi2s3;
|
|
CS43L22 cs43l22;
|
|
|
|
GPIO_InitTypeDef heartBeatLed = {.Pin = GPIO_PIN_13, .Mode = GPIO_MODE_OUTPUT_PP, .Speed = GPIO_SPEED_LOW};
|
|
uint32_t ts_blink = 0;
|
|
uint8_t chipID = 0, revID = 0;
|
|
uint16_t data = 0;
|
|
|
|
void initSinusTable(void)
|
|
{
|
|
//Pour avoir un sinus partant d'une fréquence de 1Hz, il faut AUDIOFREQ_16K cases
|
|
for(int i = 0; i < BUFFER_SIZE_SINUS; i++)
|
|
{
|
|
sinusTable[i] = 4000*sin((1.0/(double)BUFFER_SIZE_SINUS)*(double)i*2.0*M_PI);
|
|
}
|
|
}
|
|
|
|
void dacRegDump()
|
|
{
|
|
uint8_t regDump[CS43L22_RESGISTER_COUNT] = {0};
|
|
if(!CS43L22_RegisterDump(&cs43l22, regDump))
|
|
printf("Failed to dump registers\r\n");
|
|
else
|
|
{
|
|
printf("CS43L22 Register dump :\r\n");
|
|
for(int i = 0; i < CS43L22_RESGISTER_COUNT / 3; i++)
|
|
{
|
|
printf("%#X = %#X, %#X = %#X, %#X = %#X\r\n", CS43L22_GetRegisterArray()[i*3], regDump[i*3]
|
|
, CS43L22_GetRegisterArray()[i*3 + 1], regDump[i*3 + 1]
|
|
, CS43L22_GetRegisterArray()[i*3 + 2], regDump[i*3 + 2]);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void setup(void)
|
|
{
|
|
HAL_GPIO_Init(GPIOD, &heartBeatLed);
|
|
|
|
if(!CS43L22_Init(&cs43l22, &hi2c1))
|
|
printf("Failed to init DAC\r\n");
|
|
|
|
dacRegDump();
|
|
|
|
//We set the auto clocking
|
|
if(!CS43L22_WriteRegister(&cs43l22, CLOCKING_CTL, 0b10000001))
|
|
printf("Fail CLOCKING_CTL\r\n");
|
|
|
|
//...
|
|
if(!CS43L22_WriteRegister(&cs43l22, INTERFACE_CTL_1, 0b00000111))
|
|
printf("Fail INTERFACE_CTL_1\r\n");
|
|
|
|
if(!CS43L22_WriteRegister(&cs43l22, POWER_CTL_2, 0b10101111))
|
|
printf("Fail POWER_CTL_2\r\n");
|
|
|
|
if(!CS43L22_WriteRegister(&cs43l22, MISC_CTL, 0b00000010))
|
|
printf("Fail MISC_CTL\r\n");
|
|
|
|
/*if(!CS43L22_WriteRegister(&cs43l22, PASSTHROUGH_A_SELECT, 0b10001000))
|
|
printf("Fail PASSTHROUGH_A_SELECT\r\n");
|
|
|
|
if(!CS43L22_WriteRegister(&cs43l22, PASSTHROUGH_B_SELECT, 0b10001000))
|
|
printf("Fail PASSTHROUGH_B_SELECT\r\n");*/
|
|
|
|
//We set the frequency
|
|
/*if(!CS43L22_WriteRegister(&cs43l22, BEEP_FREQ_ON_TIME, 0b00010000))
|
|
printf("Fail BEEP_FREQ_ON_TIME\r\n");
|
|
|
|
//We set the volume
|
|
if(!CS43L22_WriteRegister(&cs43l22, BEEP_VOL_OFF_TIME, 0b00000110))
|
|
printf("Fail BEEP_VOL_OFF_TIME\r\n");
|
|
|
|
//We activate the beep to continuous
|
|
if(!CS43L22_WriteRegister(&cs43l22, BEEP_TONE_CFG, 0b11100000))
|
|
printf("Fail BEEP_TONE_CFG\r\n");*/
|
|
|
|
//We enable the DAC
|
|
if(!CS43L22_WriteRegister(&cs43l22, POWER_CTL_1, 0b10011110))
|
|
printf("Fail POWER_CTL_1\r\n");
|
|
|
|
initSinusTable();
|
|
}
|
|
|
|
void loop(void)
|
|
{
|
|
if(HAL_GetTick() - ts_blink > HEARTBEAT)
|
|
{
|
|
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_13);
|
|
|
|
if(!CS43L22_GetDeviceID(&cs43l22, &chipID, &revID))
|
|
printf("Failed to retrieve CS43L22 ID\r\n");
|
|
//else
|
|
//printf("Device id : %u, revID : %u\r\n", chipID, revID);
|
|
|
|
ts_blink = HAL_GetTick();
|
|
}
|
|
|
|
//HAL_I2S_Transmit(&hi2s2, &data, 1, HAL_MAX_DELAY);
|
|
HAL_I2S_Transmit(&hi2s3, &sinusTable[i++], 2, HAL_MAX_DELAY);
|
|
if(i == BUFFER_SIZE_SINUS)i=0;
|
|
HAL_Delay(3);
|
|
}
|
|
|