/** * @brief Writes a Byte to a given register into the audio codec through the control interface (I2C) * @param RegisterAddr: The address (location) of the register to be written. * @param RegisterValue: the Byte value to be written into destination register. * @retval 0 if correct communication, else wrong communication */ static int32_t Codec_WriteRegister(uint8_t RegisterAddr, uint8_t RegisterValue) { uint32_t result = 0; /*!< While the bus is busy */ while(I2C_GetFlagStatus(CODEC_I2C, I2C_FLAG_BUSY)); /* Start the config sequence */ I2C_GenerateSTART(CODEC_I2C, ENABLE); /* Test on EV5 and clear it */ while (!I2C_CheckEvent(CODEC_I2C, I2C_EVENT_MASTER_MODE_SELECT)); /* Transmit the slave address and enable writing operation */ I2C_Send7bitAddress(CODEC_I2C, CODEC_ADDRESS, I2C_Direction_Transmitter); /* Test on EV6 and clear it */ while (!I2C_CheckEvent(CODEC_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); /* Transmit the first address for write operation */ I2C_SendData(CODEC_I2C, RegisterAddr); /* Test on EV8 and clear it */ while (!I2C_CheckEvent(CODEC_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTING)); /* Prepare the register value to be sent */ I2C_SendData(CODEC_I2C, RegisterValue); /*!< Wait till all data have been physically transferred on the bus */ while(!I2C_GetFlagStatus(CODEC_I2C, I2C_FLAG_BTF)); /* End the configuration sequence */ I2C_GenerateSTOP(CODEC_I2C, ENABLE); #ifdef VERIFY_WRITTENDATA /* Verify that the data has been correctly written */ result = (Codec_ReadRegister(RegisterAddr) == RegisterValue)? 0:1; #endif /* VERIFY_WRITTENDATA */ /* Return the verifying value: 0 (Passed) or 1 (Failed) */ return result; }
void Codec_WriteRegister(uint8_t RegisterAddr, uint8_t RegisterValue) //dodane ale zmodyfikowane, nie zwraca info o bledzie { /*!< While the bus is busy */ CODECTimeout = CODEC_LONG_TIMEOUT; while(I2C_GetFlagStatus(CODEC_I2C, I2C_FLAG_BUSY)) { if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback(); } /* Start the config sequence */ I2C_GenerateSTART(CODEC_I2C, ENABLE); /* Test on EV5 and clear it */ CODECTimeout = CODEC_FLAG_TIMEOUT; while (!I2C_CheckEvent(CODEC_I2C, I2C_EVENT_MASTER_MODE_SELECT)) { if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback(); } /* Transmit the slave address and enable writing operation */ I2C_Send7bitAddress(CODEC_I2C, CODEC_I2C_ADDRESS, I2C_Direction_Transmitter); /* Test on EV6 and clear it */ CODECTimeout = CODEC_FLAG_TIMEOUT; while (!I2C_CheckEvent(CODEC_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) { if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback(); } /* Transmit the first address for write operation */ I2C_SendData(CODEC_I2C, RegisterAddr); /* Test on EV8 and clear it */ CODECTimeout = CODEC_FLAG_TIMEOUT; while (!I2C_CheckEvent(CODEC_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTING)) { if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback(); } /* Prepare the register value to be sent */ I2C_SendData(CODEC_I2C, RegisterValue); /*!< Wait till all data have been physically transferred on the bus */ CODECTimeout = CODEC_LONG_TIMEOUT; while(!I2C_GetFlagStatus(CODEC_I2C, I2C_FLAG_BTF)) { if((CODECTimeout--) == 0) Codec_TIMEOUT_UserCallback(); } /* End the configuration sequence */ I2C_GenerateSTOP(CODEC_I2C, ENABLE); #ifdef VERIFY_WRITTENDATA /* Verify that the data has been correctly written */ result = (Codec_ReadRegister(RegisterAddr) == RegisterValue)? 0:1; #endif /* VERIFY_WRITTENDATA */ /* Return the verifying value: 0 (Passed) or 1 (Failed) */ }
/** * @brief Main program. * @param None * @retval None */ int main(void) { // int c; /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ SystemInit(); RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(SystemCoreClock / SYSTICK_FREQUENCY_HZ); #ifdef TRACEUART TRACEUART_Configuration(); #endif //TRACEUART UART_Configuration(); LED_Init(); /* Output a message on Hyperterminal using printf function */ printf("\n\r\n\r"); printf("*** LATTICE FW LOADER V1.00 Build by phalt on ("__DATE__ "-" __TIME__ ")\n\r"); printf("*** LATTICE FW LOADER V1.00 Rebooting ...\n\r"); /* * wait for a keystroke (or a button press if you want.) */ // printf("\n\rPress Return to start, any other key To Enter the Console ...\n\r"); // c = awaitkey(DEFAULT_BOOT_DELAY); if(1) //(((c != '\r') && (c != '\n') && (c != '\0'))) { Delay(500); GPIO_ResetBits(BLINK_PORT, (0xF << LED1)); // traceprintf("\rtrace start OK\n\r"); // Codec_GPIO_Init(); // Delay(1); // /* Reset the Codec Registers */ // Codec_Reset(); // Delay(1); // /* Initialize the Control interface of the Audio Codec */ // Codec_CtrlInterface_Init(); // Delay(1); if(1) { GPIO_SetBits(BLINK_PORT, (0xF << LED1)); // WavePlayBack(AUDIOFREQ, 1); WavePlayerInit(AUDIOFREQ); GPIO_ResetBits(BLINK_PORT, (0xF << LED1)); } while(1) //eugene loop { // Codec_CtrlInterface_Init(); GPIO_SetBits(BLINK_PORT, (0x1 << LED1)); // Codec_Write(); Codec_ReadRegister(0x00); //test read of codec GPIO_ResetBits(BLINK_PORT, (0x1 << LED1)); Delay(1000); } if(1) //test { init_builtin_cmds(); serial_term(); } } printf("*** JUMP to Application now ...\n\r"); run_app(0x08000000); /* Infinite loop */ while (1); }