Cosmetics
This commit is contained in:
@@ -1,31 +1,15 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file Project/ee_drivers/i2c.c
|
||||
* @author FrSky Application Team
|
||||
* @version V 0.2
|
||||
* @date 12-JULY-2012
|
||||
* @brief This file provides a set of functions needed to manage the
|
||||
* communication between I2C peripheral and I2C M24CXX EEPROM.
|
||||
CAT5137 added,share the I2C.
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include "board_taranis.h"
|
||||
|
||||
#define EE_CMD_WRITE (0)
|
||||
#define EE_CMD_READ (1)
|
||||
|
||||
#define SCL_H do{I2C_EE_GPIO->BSRRL = I2C_EE_SCL;}while(0)
|
||||
#define SCL_L do{I2C_EE_GPIO->BSRRH = I2C_EE_SCL;}while(0)
|
||||
#define SDA_H do{I2C_EE_GPIO->BSRRL = I2C_EE_SDA;}while(0)
|
||||
#define SDA_L do{I2C_EE_GPIO->BSRRH = I2C_EE_SDA;}while(0)
|
||||
#define SCL_read (I2C_EE_GPIO->IDR & I2C_EE_SCL)
|
||||
#define SDA_read (I2C_EE_GPIO->IDR & I2C_EE_SDA)
|
||||
#define WP_H do{I2C_EE_WP_GPIO->BSRRL = I2C_EE_WP;}while(0)
|
||||
#define WP_L do{I2C_EE_WP_GPIO->BSRRH = I2C_EE_WP;}while(0)
|
||||
#define SCL_H do{I2C_GPIO->BSRRL = I2C_GPIO_PIN_SCL;}while(0)
|
||||
#define SCL_L do{I2C_GPIO->BSRRH = I2C_GPIO_PIN_SCL;}while(0)
|
||||
#define SDA_H do{I2C_GPIO->BSRRL = I2C_GPIO_PIN_SDA;}while(0)
|
||||
#define SDA_L do{I2C_GPIO->BSRRH = I2C_GPIO_PIN_SDA;}while(0)
|
||||
#define SCL_read (I2C_GPIO->IDR & I2C_GPIO_PIN_SCL)
|
||||
#define SDA_read (I2C_GPIO->IDR & I2C_GPIO_PIN_SDA)
|
||||
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
void I2C_EE_ByteWrite(uint8_t* pBuffer, uint16_t WriteAddr);
|
||||
void I2C_EE_PageWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t NumByteToWrite);
|
||||
void I2C_EE_WaitEepromStandbyState(void);
|
||||
|
||||
@@ -34,36 +18,6 @@ uint8_t I2C_read_volume(void);
|
||||
|
||||
#define I2C_delay() delay_01us(100);
|
||||
|
||||
/**
|
||||
* @brief Configure the used I/O ports pin
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void I2C_GPIO_Configuration(void)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
|
||||
GPIOB->BSRRH =I2C_EE_WP; //PB9
|
||||
GPIO_InitStructure.GPIO_Pin = I2C_EE_WP;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
||||
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
||||
GPIO_Init(I2C_EE_WP_GPIO, &GPIO_InitStructure);
|
||||
|
||||
/* Configure I2C_EE pins: SCL and SDA */
|
||||
GPIO_InitStructure.GPIO_Pin = I2C_EE_SCL | I2C_EE_SDA;//PE0,PE1
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
||||
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
||||
GPIO_Init(I2C_EE_GPIO, &GPIO_InitStructure);
|
||||
|
||||
//Set Idle levels
|
||||
SDA_H;
|
||||
SCL_H;
|
||||
}
|
||||
|
||||
short I2C_START(void)
|
||||
{
|
||||
SDA_H;
|
||||
@@ -181,37 +135,33 @@ char I2C_READ(void)
|
||||
|
||||
void eepromInit(void)
|
||||
{
|
||||
/* GPIO Periph clock enable */
|
||||
RCC_AHB1PeriphClockCmd(I2C_EE_GPIO_CLK, ENABLE);
|
||||
RCC_AHB1PeriphClockCmd(I2C_RCC_AHB1Periph_GPIO, ENABLE);
|
||||
RCC_APB1PeriphClockCmd(I2C_RCC_APB1Periph_I2C, ENABLE);
|
||||
|
||||
/* GPIO configuration */
|
||||
I2C_GPIO_Configuration();
|
||||
}
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
GPIO_InitStructure.GPIO_Pin = I2C_GPIO_PIN_SCL | I2C_GPIO_PIN_SDA;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
||||
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
||||
GPIO_Init(I2C_GPIO, &GPIO_InitStructure);
|
||||
|
||||
/**
|
||||
* @brief Writes one byte to the I2C EEPROM.
|
||||
* @param pBuffer : pointer to the buffer containing the data to be
|
||||
* written to the EEPROM.
|
||||
* @param WriteAddr : EEPROM's internal address to write to.
|
||||
* @retval None
|
||||
*/
|
||||
void I2C_EE_ByteWrite(uint8_t* pBuffer, uint16_t WriteAddr)
|
||||
{
|
||||
I2C_START();
|
||||
I2C_SEND_DATA(I2C_EEPROM_ADDRESS|EE_CMD_WRITE);
|
||||
I2C_WAIT_ACK();
|
||||
#ifdef EE_M24C08
|
||||
I2C_SEND_DATA(WriteAddr);
|
||||
I2C_WAIT_ACK();
|
||||
#else
|
||||
I2C_SEND_DATA((uint8_t)((WriteAddr&0xFF00)>>8) );
|
||||
I2C_WAIT_ACK();
|
||||
I2C_SEND_DATA((uint8_t)(WriteAddr&0xFF));
|
||||
I2C_WAIT_ACK();
|
||||
#if 0
|
||||
GPIO_PinAFConfig(I2C_GPIO, I2C_GPIO_PIN_SCL, I2C_GPIO_AF);
|
||||
GPIO_PinAFConfig(I2C_GPIO, I2C_GPIO_PIN_SDA, I2C_GPIO_AF);
|
||||
|
||||
I2C_InitTypeDef I2C_InitStructure;
|
||||
I2C_InitStructure.I2C_OwnAddress1 = 0x00; // MPU6050 7-bit adress = 0x68, 8-bit adress = 0xD0;
|
||||
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
|
||||
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
|
||||
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
|
||||
|
||||
I2C_Init(I2C, &I2C_InitStructure);
|
||||
I2C_Cmd(I2C, ENABLE);
|
||||
#endif
|
||||
I2C_SEND_DATA(*pBuffer);
|
||||
I2C_WAIT_ACK();
|
||||
I2C_STOP();
|
||||
// Set Idle levels
|
||||
SDA_H;
|
||||
SCL_H;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -225,29 +175,24 @@ void I2C_EE_ByteWrite(uint8_t* pBuffer, uint16_t WriteAddr)
|
||||
void eepromReadBlock(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead)
|
||||
{
|
||||
I2C_START();
|
||||
I2C_SEND_DATA(I2C_EEPROM_ADDRESS|EE_CMD_WRITE);
|
||||
I2C_SEND_DATA(I2C_ADDRESS_EEPROM|EE_CMD_WRITE);
|
||||
I2C_WAIT_ACK();
|
||||
#ifdef EE_M24C08
|
||||
I2C_SEND_DATA(ReadAddr);
|
||||
I2C_WAIT_ACK();
|
||||
#else
|
||||
I2C_SEND_DATA((uint8_t)((ReadAddr & 0xFF00) >> 8));
|
||||
I2C_WAIT_ACK();
|
||||
I2C_SEND_DATA((uint8_t)(ReadAddr & 0x00FF));
|
||||
I2C_WAIT_ACK();
|
||||
#endif
|
||||
|
||||
I2C_START();
|
||||
I2C_SEND_DATA(I2C_EEPROM_ADDRESS|EE_CMD_READ);
|
||||
I2C_SEND_DATA(I2C_ADDRESS_EEPROM|EE_CMD_READ);
|
||||
I2C_WAIT_ACK();
|
||||
while (NumByteToRead) {
|
||||
if (NumByteToRead == 1) {
|
||||
*pBuffer =I2C_READ();
|
||||
*pBuffer = I2C_READ();
|
||||
I2C_NO_ACK();
|
||||
I2C_STOP();
|
||||
}
|
||||
else {
|
||||
*pBuffer =I2C_READ();
|
||||
*pBuffer = I2C_READ();
|
||||
I2C_ACK();
|
||||
pBuffer++;
|
||||
}
|
||||
@@ -278,7 +223,6 @@ void eepromWriteBlock(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWr
|
||||
/* If NumByteToWrite < I2C_FLASH_PAGESIZE */
|
||||
if (NumOfPage == 0) {
|
||||
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
|
||||
|
||||
I2C_EE_WaitEepromStandbyState();
|
||||
}
|
||||
/* If NumByteToWrite > I2C_FLASH_PAGESIZE */
|
||||
@@ -355,18 +299,13 @@ void eepromWriteBlock(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWr
|
||||
void I2C_EE_PageWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t NumByteToWrite)
|
||||
{
|
||||
I2C_START();
|
||||
I2C_SEND_DATA(I2C_EEPROM_ADDRESS|EE_CMD_WRITE);
|
||||
I2C_SEND_DATA(I2C_ADDRESS_EEPROM|EE_CMD_WRITE);
|
||||
I2C_WAIT_ACK();
|
||||
|
||||
#ifdef EE_M24C08
|
||||
I2C_SEND_DATA(WriteAddr);
|
||||
I2C_WAIT_ACK();
|
||||
#else
|
||||
I2C_SEND_DATA((uint8_t)((WriteAddr & 0xFF00) >> 8));
|
||||
I2C_WAIT_ACK();
|
||||
I2C_SEND_DATA((uint8_t)(WriteAddr & 0x00FF));
|
||||
I2C_WAIT_ACK();
|
||||
#endif /* EE_M24C08 */
|
||||
|
||||
/* While there is data to be written */
|
||||
while (NumByteToWrite--) {
|
||||
@@ -386,7 +325,7 @@ void I2C_EE_WaitEepromStandbyState(void)
|
||||
{
|
||||
do {
|
||||
I2C_START();
|
||||
I2C_SEND_DATA(I2C_EEPROM_ADDRESS|EE_CMD_WRITE);
|
||||
I2C_SEND_DATA(I2C_ADDRESS_EEPROM|EE_CMD_WRITE);
|
||||
} while (0 == I2C_WAIT_ACK());
|
||||
|
||||
I2C_STOP();
|
||||
@@ -394,11 +333,12 @@ void I2C_EE_WaitEepromStandbyState(void)
|
||||
|
||||
void setVolume(uint8_t volume)
|
||||
{
|
||||
if (volume > VOLUME_LEVEL_MAX)
|
||||
if (volume > VOLUME_LEVEL_MAX) {
|
||||
volume = VOLUME_LEVEL_MAX;
|
||||
}
|
||||
|
||||
I2C_START();
|
||||
I2C_SEND_DATA(I2C_CAT5137_ADDRESS|EE_CMD_WRITE);
|
||||
I2C_SEND_DATA(I2C_ADDRESS_CAT5137|EE_CMD_WRITE);
|
||||
I2C_WAIT_ACK();
|
||||
I2C_SEND_DATA(0);
|
||||
I2C_WAIT_ACK();
|
||||
@@ -411,12 +351,12 @@ uint8_t I2C_read_volume()
|
||||
{
|
||||
uint8_t volume ;
|
||||
I2C_START();
|
||||
I2C_SEND_DATA(I2C_CAT5137_ADDRESS|EE_CMD_WRITE);
|
||||
I2C_SEND_DATA(I2C_ADDRESS_CAT5137|EE_CMD_WRITE);
|
||||
I2C_WAIT_ACK();
|
||||
I2C_SEND_DATA(0);
|
||||
I2C_WAIT_ACK();
|
||||
I2C_START();
|
||||
I2C_SEND_DATA(I2C_CAT5137_ADDRESS|EE_CMD_READ);
|
||||
I2C_SEND_DATA(I2C_ADDRESS_CAT5137|EE_CMD_READ);
|
||||
I2C_WAIT_ACK();
|
||||
volume = I2C_READ();
|
||||
I2C_NO_ACK();
|
||||
|
||||
Reference in New Issue
Block a user