216 lines
6.7 KiB
C
216 lines
6.7 KiB
C
/*
|
|
* The Clear BSD License
|
|
* Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
|
|
* Copyright 2016-2017 NXP
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
* are permitted (subject to the limitations in the disclaimer below) provided
|
|
* that the following conditions are met:
|
|
*
|
|
* o Redistributions of source code must retain the above copyright notice, this list
|
|
* of conditions and the following disclaimer.
|
|
*
|
|
* o Redistributions in binary form must reproduce the above copyright notice, this
|
|
* list of conditions and the following disclaimer in the documentation and/or
|
|
* other materials provided with the distribution.
|
|
*
|
|
* o Neither the name of the copyright holder nor the names of its
|
|
* contributors may be used to endorse or promote products derived from this
|
|
* software without specific prior written permission.
|
|
*
|
|
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
#include "fsl_device_registers.h"
|
|
#include "fsl_debug_console.h"
|
|
#include "board.h"
|
|
#include "AT24C02.h"
|
|
|
|
#include "fsl_common.h"
|
|
#include "pin_mux.h"
|
|
#include "clock_config.h"
|
|
#include "fsl_gpio.h"
|
|
#include "fsl_port.h"
|
|
/*******************************************************************************
|
|
* Definitions
|
|
******************************************************************************/
|
|
#define BOARD_LED_GPIO BOARD_LED_RED2_GPIO
|
|
#define BOARD_LED_GPIO_PIN BOARD_LED_RED2_GPIO_PIN
|
|
|
|
#define I2C_RELEASE_SDA_PORT PORTB
|
|
#define I2C_RELEASE_SCL_PORT PORTB
|
|
#define I2C_RELEASE_SDA_GPIO GPIOB
|
|
#define I2C_RELEASE_SDA_PIN 1U
|
|
#define I2C_RELEASE_SCL_GPIO GPIOB
|
|
#define I2C_RELEASE_SCL_PIN 0U
|
|
#define I2C_RELEASE_BUS_COUNT 100U
|
|
|
|
/*******************************************************************************
|
|
* Prototypes
|
|
******************************************************************************/
|
|
void BOARD_I2C_ReleaseBus(void);
|
|
|
|
/*******************************************************************************
|
|
* Code
|
|
******************************************************************************/
|
|
|
|
static void i2c_release_bus_delay(void)
|
|
{
|
|
uint32_t i = 0;
|
|
for (i = 0; i < I2C_RELEASE_BUS_COUNT; i++)
|
|
{
|
|
__NOP();
|
|
}
|
|
}
|
|
|
|
void BOARD_I2C_ReleaseBus(void)
|
|
{
|
|
uint8_t i = 0;
|
|
port_pin_config_t i2c_pin_config = {0};
|
|
gpio_pin_config_t pin_config;
|
|
|
|
/* Config pin mux as gpio */
|
|
i2c_pin_config.pullSelect = kPORT_PullUp;
|
|
i2c_pin_config.mux = kPORT_MuxAsGpio;
|
|
|
|
pin_config.pinDirection = kGPIO_DigitalOutput;
|
|
pin_config.outputLogic = 1U;
|
|
CLOCK_EnableClock(kCLOCK_PortB);
|
|
PORT_SetPinConfig(I2C_RELEASE_SCL_PORT, I2C_RELEASE_SCL_PIN, &i2c_pin_config);
|
|
PORT_SetPinConfig(I2C_RELEASE_SDA_PORT, I2C_RELEASE_SDA_PIN, &i2c_pin_config);
|
|
|
|
GPIO_PinInit(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, &pin_config);
|
|
GPIO_PinInit(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, &pin_config);
|
|
|
|
/* Drive SDA low first to simulate a start */
|
|
GPIO_PinWrite(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, 0U);
|
|
i2c_release_bus_delay();
|
|
|
|
/* Send 9 pulses on SCL and keep SDA high */
|
|
for (i = 0; i < 9; i++)
|
|
{
|
|
GPIO_PinWrite(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, 0U);
|
|
i2c_release_bus_delay();
|
|
|
|
GPIO_PinWrite(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, 1U);
|
|
i2c_release_bus_delay();
|
|
|
|
GPIO_PinWrite(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, 1U);
|
|
i2c_release_bus_delay();
|
|
i2c_release_bus_delay();
|
|
}
|
|
|
|
/* Send stop */
|
|
GPIO_PinWrite(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, 0U);
|
|
i2c_release_bus_delay();
|
|
|
|
GPIO_PinWrite(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, 0U);
|
|
i2c_release_bus_delay();
|
|
|
|
GPIO_PinWrite(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, 1U);
|
|
i2c_release_bus_delay();
|
|
|
|
GPIO_PinWrite(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, 1U);
|
|
i2c_release_bus_delay();
|
|
}
|
|
|
|
uint32_t AT24C02_GetI2cSrcClkFreq(void)
|
|
{
|
|
return CLOCK_GetFreq(kCLOCK_McgIrc48MClk);
|
|
}
|
|
|
|
/*!
|
|
* @brief delayForAT24C02DataFlashReady function
|
|
* The AT24C02 need a time to flash the data that has been written
|
|
*/
|
|
void delayForAT24C02DataFlashReady(void)
|
|
{
|
|
volatile uint32_t i = 50000;
|
|
while (i--)
|
|
{
|
|
;
|
|
}
|
|
}
|
|
|
|
/*!
|
|
* @brief Main function
|
|
*/
|
|
int main(void)
|
|
{
|
|
at24c02_handler_t handler;
|
|
at24c02_status_t status;
|
|
uint8_t write_data;
|
|
uint8_t read_data;
|
|
uint8_t eeprom_add;
|
|
uint16_t cnt;
|
|
|
|
/* Initializes board hardware. */
|
|
BOARD_InitPins();
|
|
BOARD_BootClockHSRUN();
|
|
BOARD_I2C_ReleaseBus();
|
|
BOARD_I2C_ConfigurePins();
|
|
BOARD_InitDebugConsole();
|
|
CLOCK_SetPllFllSelClock(3U); /* IRC48 MHz clock. */
|
|
CLOCK_SetLpi2c0Clock(1);
|
|
|
|
PRINTF("EEPROM Demo\r\n\r\n");
|
|
PRINTF("This demo will write data into on board EEPROM and then read it back.\r\n\r\n");
|
|
|
|
/* Configures the at24c02 handler */
|
|
handler.i2cBase = AT24C02_I2C_BASE;
|
|
handler.device_addr = AT24C02_I2C_ADDR;
|
|
handler.baudRate_kbps = AT24C02_I2C_BAUDRATE;
|
|
|
|
/* Initializes the i2c which to be used to operate the at24c02 */
|
|
AT24C02_I2CInit(&handler);
|
|
|
|
for (cnt = 0; cnt < 256; cnt++)
|
|
{
|
|
eeprom_add = cnt;
|
|
write_data = 255 - cnt;
|
|
|
|
status = AT24C02_ByteWrite(&handler, eeprom_add, write_data);
|
|
if (status == kStatus_AT24C02_I2CFail)
|
|
{
|
|
PRINTF("Write failure\r\n");
|
|
}
|
|
|
|
/* Wait for the data flashed ready */
|
|
delayForAT24C02DataFlashReady();
|
|
|
|
AT24C02_ACKPoll(&handler);
|
|
|
|
status = AT24C02_ByteRead(&handler, eeprom_add, &read_data);
|
|
if (status == kStatus_AT24C02_I2CFail)
|
|
{
|
|
PRINTF("Read failure\r\n");
|
|
}
|
|
|
|
if (read_data != write_data)
|
|
{
|
|
PRINTF("Read data is 0x%x instead of 0x%x\r\n", read_data, write_data);
|
|
}
|
|
else
|
|
{
|
|
PRINTF("Write data at 0x%x is 0x%x, read back data is 0x%x\r\n", eeprom_add, write_data, read_data);
|
|
}
|
|
}
|
|
|
|
PRINTF("\r\nEnd of the demo.\n\r");
|
|
|
|
while (1)
|
|
{
|
|
}
|
|
}
|