新手入门的系列文档【原创】
原有设计从SPI/I2C 启动,如何通过GPIO来选择从USB启动。
上电启动后,判断引脚状态,是否选择破环原有启动镜像。
#ifdef UART_BOOT
CyBool_t boot_value = 0;
uint8_t release_count = 0;
for(uint8_t i = 0;i < 30;i++){
if(CyU3PGpioSimpleGetValue (BOOTPIN, &boot_value) == CY_U3P_SUCCESS){
if(boot_value == CyFalse){
if(release_count++ >= RELEASE_COUNT){
bootfromusb();
}
}else{
release_count = 0;
}
}
CyU3PBusyWait(100);
}
#endif
破环之前EEPROM上存储的镜像。
#ifdef UART_BOOT
void CyI2cInit(void){
CyU3PI2cConfig_t i2cConfig;
CyU3PReturnStatus_t apiRetStatus;
apiRetStatus = CyU3PI2cInit ();
if (apiRetStatus != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "I2C initialization failed!\n");
CyFxAppErrorHandler (apiRetStatus);
}
/* Set I2C Configuration */
i2cConfig.bitRate = 100000; /* 100 KHz */
i2cConfig.isDma = CyFalse;
i2cConfig.busTimeout = 0xffffffffU;
i2cConfig.dmaTimeout = 0xffff;
apiRetStatus = CyU3PI2cSetConfig (&i2cConfig, NULL);
if (CY_U3P_SUCCESS != apiRetStatus)
{
CyU3PDebugPrint (4, "I2C configuration failed!\n");
CyFxAppErrorHandler (apiRetStatus);
}
}
static void I2CAccessDelay(CyU3PReturnStatus_t status){
/* Add a 10us delay if the I2C operation that preceded this call was successful. */
if (status == CY_U3P_SUCCESS)
CyU3PBusyWait(10);
}
/*I2C write stream function*/
CyU3PReturnStatus_t masterWrite_register(uint8_t slaveAddr, uint16_t regAddr, uint8_t* regData,uint16_t byteCount){
CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
CyU3PI2cPreamble_t preamble;
for(uint8_t i = 0;i < byteCount;i++){
preamble.buffer[0] = slaveAddr & I2C_SLAVEADDR_MASK; /* Mask out the transfer type bit. */
preamble.buffer[1] = (uint8_t)(regAddr >> 7);
preamble.buffer[2] = (uint8_t)(regAddr);
preamble.length = 3;
preamble.ctrlMask = 0x0000; /* Send start bit after third byte of preamble. */
apiRetStatus = CyU3PI2cTransmitBytes(&preamble, ®Data[i], 1, 0);
I2CAccessDelay(apiRetStatus);
}
return apiRetStatus;
}
void bootfromusb(){
uint8_t temp[I2C_COUNT];
masterWrite_register(I2C_DEVICE_ADDR,0x00,temp,I2C_COUNT);
CyU3PDeviceReset(CyFalse);
}
#endif
|