/** * @brief Initializes and Configures the IO_Expander Touch Panel Functionality * and configures all STM32F429I_DISCO necessary * hardware (GPIOs, APB clocks ..). * @param None * @retval IOE_OK if all initializations done correctly. Other value if error. */ uint8_t IOE_Config(void) { /* Configure the needed pins */ IOE_GPIO_Config(); /* I2C initialization */ IOE_I2C_Config(); /* Read IO Expander ID */ if(IOE_IsOperational()) { return IOE_NOT_OPERATIONAL; } /* Generate IO Expander Software reset */ IOE_Reset(); /* IO Expander configuration */ /* Touch Panel controller and ADC configuration */ IOE_FnctCmd(IOE_ADC_FCT, ENABLE); IOE_TP_Config(); /* Configuration is OK */ return IOE_OK; }
/** * @brief Initializes and Configures the IO_Expanders Functionalities * (Touch Screen ..) and configures all STM32100E-EVAL necessary * hardware (GPIOs, APB clocks ..). * @param None * @retval IOE_OK if all initializations done correctly. Other value if error. */ uint8_t IOE_Config(void) { /* Configure the needed pins */ IOE_GPIO_Config(); /* Configure I2C peripheral */ IOE_I2C_Config(); /* Read IO Expander 1 ID */ if(IOE_IsOperational(IOE_1_ADDR)) { return IOE1_NOT_OPERATIONAL; } /* Generate IOExpander Software reset */ IOE_Reset(IOE_1_ADDR); /* ---------------------- IO Expander configuration --------------------- */ /* Enable the GPIO, Touch Screen and ADC functionalities */ IOE_FnctCmd(IOE_1_ADDR, IOE_TS_FCT | IOE_ADC_FCT | IOE_IO_FCT | IOE_TEMPSENS_FCT , ENABLE); /* Touch Screen controller configuration */ IOE_TS_Config(); /* Temperature Sensor configuration */ IOE_TempSens_Config(); /* ------------------------------------------------------------------------ */ /* Configuration is OK */ return IOE_OK; }
/** * @brief Initializes and Configures the two IO_Expanders Functionalities * (IOs, Touch Screen ..) and configures all STM3210C-EVAL necessary * hardware (GPIOs, APB clocks ..). * @param None * @retval IOE_OK if all initializations done correctly. Other value if error. */ uint8_t IOE_Config(void) { /* Configure the needed pins */ IOE_GPIO_Config(); /* Configure the I2C peripheral */ IOE_I2C_Config(); /* Read IO Expander 1 ID */ if(IOE_IsOperational(IOE_1_ADDR)) { return IOE1_NOT_OPERATIONAL; } if(IOE_IsOperational(IOE_2_ADDR)) { return IOE2_NOT_OPERATIONAL; } /* Generate IOExpander Software reset */ IOE_Reset(IOE_1_ADDR); IOE_Reset(IOE_2_ADDR); /* ---------------------- IO Expander 1 configuration --------------------- */ /* Enable the GPIO, Touch Screen and ADC functionalities */ IOE_FnctCmd(IOE_1_ADDR, IOE_IO_FCT | IOE_TS_FCT | IOE_ADC_FCT, ENABLE); /* Configure the VBAT pin in output mode pin*/ IOE_IOPinConfig(IOE_1_ADDR, VBAT_DIV_PIN , Direction_OUT); /* ENABLE the alternate function for IN1 pin */ IOE_IOAFConfig(IOE_1_ADDR, VBAT_DIV_PIN, ENABLE); /* Apply the default state for the out pins */ IOE_WriteIOPin(VBAT_DIV_PIN, BitReset); /* Configure the MEMS interrupt pins in Input mode */ IOE_IOPinConfig(IOE_2_ADDR, (uint32_t)(MEMS_INT1_PIN | MEMS_INT2_PIN), Direction_IN); /* ENABLE the alternate function for the Joystick pins */ IOE_IOAFConfig(IOE_2_ADDR, (uint32_t)(MEMS_INT1_PIN | MEMS_INT2_PIN), ENABLE); /* Configure the IOs to detect Falling and Rising Edges */ IOE_IOEdgeConfig(IOE_2_ADDR, (uint32_t)(MEMS_INT1_PIN | MEMS_INT2_PIN), (uint32_t)(EDGE_FALLING | EDGE_RISING)); /* Touch Screen controller configuration */ IOE_TS_Config(); /* ------------------------------------------------------------------------ */ /* ---------------------- IO Expander 2 configuration --------------------- */ /* Enable the GPIO, Temperature Sensor and ADC functionalities */ IOE_FnctCmd(IOE_2_ADDR, IOE_IO_FCT | IOE_TEMPSENS_FCT | IOE_ADC_FCT, ENABLE); /* Configure the Audio Codec Reset pin in output mode pin*/ IOE_IOPinConfig(IOE_2_ADDR, (uint32_t)(AUDIO_RESET_PIN), Direction_OUT); IOE_IOPinConfig(IOE_2_ADDR, (uint32_t)(MII_INT_PIN), Direction_IN); /* ENABLE the alternate function for IN1 pin */ IOE_IOAFConfig(IOE_2_ADDR, (uint32_t)(AUDIO_RESET_PIN | MII_INT_PIN), ENABLE); /* Apply the default state for the out pins */ IOE_WriteIOPin(AUDIO_RESET_PIN, BitReset); IOE_WriteIOPin(MII_INT_PIN, BitReset); /* Configure the Joystick pins in Input mode */ IOE_IOPinConfig(IOE_2_ADDR, JOY_IO_PINS , Direction_IN); /* ENABLE the alternate function for the Joystick pins */ IOE_IOAFConfig(IOE_2_ADDR, JOY_IO_PINS, ENABLE); /* Configure the IOs to detect Falling and Rising Edges */ IOE_IOEdgeConfig(IOE_2_ADDR, JOY_IO_PINS, (uint8_t)(EDGE_FALLING | EDGE_RISING)); /* Temperature Sensor module configuration */ IOE_TempSens_Config(); /* ------------------------------------------------------------------------ */ /* Configuration is OK */ return IOE_OK; }
/** * @brief Reads a buffer of 4 bytes from IO_Expander registers. * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR * or IOE_2_ADDR. * @param RegisterAddr: The target register adress (between 00x and 0x24) * @retval : The value of the read register (0xAA if Timout occured) */ uint32_t I2C_ReadDataBuffer(u8 DeviceAddr, uint32_t RegisterAddr) { u8 Buffer[4] , idx = 2; /* Initialize the buffer */ Buffer[0] = 0; Buffer[1] = 0; Buffer[2] = 0; Buffer[3] = 0; /* Disable the I2C1 peripheral */ I2C_Cmd(I2C1, DISABLE); /* Reset all I2C2 registers */ I2C_SoftwareResetCmd(I2C1, ENABLE); I2C_SoftwareResetCmd(I2C1, DISABLE); /* Configure the I2C peripheral */ IOE_I2C_Config(); /* Enable the I2C peripheral */ I2C_GenerateSTART(I2C1, ENABLE); TimeOut = TIMEOUT_MAX; /* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Send device address for write */ I2C_Send7bitAddress(I2C1, DeviceAddr, I2C_Direction_Transmitter); /* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Clear EV6 by setting again the PE bit */ I2C_Cmd(I2C1, ENABLE); /* Send the device's internal address to write to */ I2C_SendData(I2C1, RegisterAddr); /* Test on EV8 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Send STRAT condition a second time */ I2C_GenerateSTART(I2C1, ENABLE); /* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Send EEPROM address for read */ I2C_Send7bitAddress(I2C1, DeviceAddr, I2C_Direction_Receiver); /* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* While there is data to be read */ while(idx) { if(idx == 1) { /* Disable Acknowledgement */ I2C_AcknowledgeConfig(I2C1, DISABLE); /* Send STOP Condition */ I2C_GenerateSTOP(I2C1, ENABLE); } /* Test on EV7 and clear it */ if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) { /* Read a byte from the EEPROM */ Buffer[idx-1] = I2C_ReceiveData(I2C1); /* Decrement the read bytes counter */ idx--; } } /* Enable Acknowledgement to be ready for another reception */ I2C_AcknowledgeConfig(I2C1, ENABLE); /* return a pointer to the buffer */ return *(uint32_t *)Buffer; }
/** * @brief Reads a register of the audio Codec through I2C. * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR * or IOE_2_ADDR. * @param RegisterAddr: The target register adress (between 00x and 0x24) * @retval The value of the read register (0xAA if Timout occured) */ uint8_t I2C_ReadDeviceRegister(uint8_t DeviceAddr, uint8_t RegisterAddr) { uint32_t tmp = 0; /* Disable the IOE_I2C peripheral */ I2C_Cmd(IOE_I2C, DISABLE); /* Reset all I2C2 registers */ I2C_SoftwareResetCmd(IOE_I2C, ENABLE); I2C_SoftwareResetCmd(IOE_I2C, DISABLE); /* Configure the I2C peripheral */ IOE_I2C_Config(); /* Enable the I2C peripheral */ I2C_GenerateSTART(IOE_I2C, ENABLE); TimeOut = TIMEOUT_MAX; /* Test on EV5 and clear it */ while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_MODE_SELECT)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Disable Acknowledgement */ I2C_AcknowledgeConfig(IOE_I2C, DISABLE); /* Transmit the slave address and enable writing operation */ I2C_Send7bitAddress(IOE_I2C, DeviceAddr, I2C_Direction_Transmitter); TimeOut = TIMEOUT_MAX; /* Test on EV6 and clear it */ while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Transmit the first address for r/w operations */ I2C_SendData(IOE_I2C, RegisterAddr); /* Test on EV8 and clear it */ while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Regenerate a start condition */ I2C_GenerateSTART(IOE_I2C, ENABLE); /* Test on EV5 and clear it */ while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_MODE_SELECT)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Transmit the slave address and enable writing operation */ I2C_Send7bitAddress(IOE_I2C, DeviceAddr, I2C_Direction_Receiver); /* Test on EV6 and clear it */ while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Test on EV7 and clear it */ while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* End the configuration sequence */ I2C_GenerateSTOP(IOE_I2C, ENABLE); /* Load the register value */ tmp = I2C_ReceiveData(IOE_I2C); /* Enable Acknowledgement */ I2C_AcknowledgeConfig(IOE_I2C, ENABLE); /* Return the read value */ return tmp; }
/** * @brief Writes a value in a register of the IOE through I2C. * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR * or IOE_2_ADDR. * @param RegisterAddr: The target register adress * @param RegisterValue: The target register value to be written * @retval IOE_OK: if all operations are OK. Other value if error. */ uint8_t I2C_WriteDeviceRegister(uint8_t DeviceAddr, uint8_t RegisterAddr, uint8_t RegisterValue) { uint32_t read_verif = 0; /* Reset all I2C2 registers */ I2C_SoftwareResetCmd(IOE_I2C, ENABLE); I2C_SoftwareResetCmd(IOE_I2C, DISABLE); TimeOut = TIMEOUT_MAX; /* Enable the IOE_I2C peripheral */ I2C_Cmd(IOE_I2C, ENABLE); /* Configure the I2C peripheral */ IOE_I2C_Config(); /* Begin the config sequence */ I2C_GenerateSTART(IOE_I2C, ENABLE); /* Test on EV5 and clear it */ while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_MODE_SELECT)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Transmit the slave address and enable writing operation */ I2C_Send7bitAddress(IOE_I2C, DeviceAddr, I2C_Direction_Transmitter); /* Test on EV6 and clear it */ while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Transmit the first address for r/w operations */ I2C_SendData(IOE_I2C, RegisterAddr); TimeOut = TIMEOUT_MAX; /* Test on EV8 and clear it */ while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Prepare the register value to be sent */ I2C_SendData(IOE_I2C, RegisterValue); /* Test on EV8 and clear it */ while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* End the configuration sequence */ I2C_GenerateSTOP(IOE_I2C, ENABLE); #ifdef VERIFY_WRITTENDATA /* Verify (if needed) that the loaded data is correct */ /* Read the just written register*/ read_verif = I2C_ReadDeviceRegister(DeviceAddr, RegisterAddr); /* Load the register and verify its value */ if (read_verif != RegisterValue) { /* Control data wrongly tranfered */ read_verif = IOE_FAILURE; } else { /* Control data correctly transfered */ read_verif = 0; } #endif /* Return the verifying value: 0 (Passed) or 1 (Failed) */ return read_verif; }
/** * @brief Reads a buffer of 2 bytes from the device registers. * @param DeviceAddr: The address of the device, could be : IOE_1_ADDR * or IOE_2_ADDR. * @param RegisterAddr: The target register adress (between 00x and 0x24) * @retval A pointer to the buffer containing the two returned bytes (in halfword). */ uint16_t I2C_ReadDataBuffer(uint8_t DeviceAddr, uint32_t RegisterAddr) { uint8_t Buffer[2] = {0x00, 0x00}; /* Disable the I2C1 peripheral */ I2C_Cmd(I2C1, DISABLE); /* Reset all I2C2 registers */ I2C_SoftwareResetCmd(I2C1, ENABLE); I2C_SoftwareResetCmd(I2C1, DISABLE); /* Configure the I2C peripheral */ IOE_I2C_Config(); /* Enable the I2C peripheral */ I2C_GenerateSTART(I2C1, ENABLE); /* Test on EV5 and clear it */ TimeOut = TIMEOUT_MAX; while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Send device address for write */ I2C_Send7bitAddress(I2C1, DeviceAddr, I2C_Direction_Transmitter); /* Test on EV6 and clear it */ TimeOut = TIMEOUT_MAX; while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Clear EV6 by setting again the PE bit */ I2C_Cmd(I2C1, ENABLE); /* Send the device's internal address to write to */ I2C_SendData(I2C1, RegisterAddr); /* Test on EV8 and clear it */ TimeOut = TIMEOUT_MAX; while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Send STRAT condition a second time */ I2C_GenerateSTART(I2C1, ENABLE); /* Test on EV5 and clear it */ TimeOut = TIMEOUT_MAX; while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Send IOExpander address for read */ I2C_Send7bitAddress(I2C1, DeviceAddr, I2C_Direction_Receiver); /* Test on EV6 and clear it */ TimeOut = TIMEOUT_MAX; while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Test on EV7 and clear it */ TimeOut = TIMEOUT_MAX; while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Read the first byte from the IOExpander */ Buffer[1] = I2C_ReceiveData(I2C1); /* Disable Acknowledgement */ I2C_AcknowledgeConfig(I2C1, DISABLE); /* Send STOP Condition */ I2C_GenerateSTOP(I2C1, ENABLE); /* Test on EV7 and clear it */ TimeOut = TIMEOUT_MAX; while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Read the second byte from the IOExpander */ Buffer[0] = I2C_ReceiveData(I2C1); /* Enable Acknowledgement to be ready for another reception */ I2C_AcknowledgeConfig(I2C1, ENABLE); /* return a pointer to the buffer */ return *(uint16_t *)Buffer; }