i2c eeprom

This commit is contained in:
Eugene 2023-09-12 16:09:57 +03:00
parent 49d32751ef
commit bd1ce624a4
10 changed files with 404 additions and 37 deletions

Binary file not shown.

View File

@ -16,10 +16,11 @@
#include "f28x_project.h"
#include "init_perif.h"
#include"frm_uart.h"
#include "spi_init.h"
#include "i2c_init.h"
#include "BL25CM1A.h"
#include "GD25Q16ETIGR.h"
#include "ZD24C02A.h"
//
// Main
//
@ -31,7 +32,22 @@ volatile uint16_t counter2=0 ;
uint16_t sendNow = 0;
uint16_t TestADR = 0;
uint16_t NByte = 16;
uint16_t WriteI2C = 0;
uint16_t Adr = 0;
Uint16 MainTimerTimeouts = 0;
void MainTimerBaseTimeoutInc(void)
{
MainTimerTimeouts++;
}
uint16_t ArrayForTests[MAX_BUFFER_SIZE] = {0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0};
uint16_t ArrayMax[256];
void main(void)
{
@ -47,12 +63,38 @@ void main(void)
{
asm (" NOP");
// if(sendNow)
// {
if(sendNow)
{
// Bl25cm1a_write();
// GD25Q16ETIGR_write();
// sendNow = 0;
// }
if(WriteI2C)
{
for(Adr = 0; Adr < 255; Adr+=15)
{
ArrayForTests[0] = Adr;
ZD24C02A_write(NByte, ArrayForTests);
MainTimerTimeouts=0;
while(MainTimerTimeouts<1000);
while(!GpioDataRegs.GPADAT.bit.GPIO1);
}
}
else
{
ArrayMax[0] = Adr;
NByte = 254;
ZD24C02A_read(NByte, ArrayMax);
}
sendNow = 0;
}
if(TestADR)
{
ZD24C02A_test(ArrayForTests);
TestADR = 0;
}
if(FMSTR_is_enable()) {
if(counter < 100) counter++;

View File

@ -4,5 +4,31 @@
* Created on: 8 ñåíò. 2023 ã.
* Author: seklyuts
*/
#include "f28x_project.h"
#include "i2c_init.h"
#include "ZD24C02A.h"
volatile uint16_t SlaveAdr = I2C_SLAVE_ADDRESS;
void ZD24C02A_write(uint16_t byteCount, uint16_t * Array)
{
I2CWrite(SlaveAdr, byteCount, true, Array);
}
void ZD24C02A_read(uint16_t byteCount, uint16_t * Array)
{
I2CWrite(SlaveAdr, 1, false, Array);
I2CRead(SlaveAdr, byteCount, true, Array);
// I2CWriteRead(I2C_SLAVE_ADDRESS, byteCount, true, Array);
// I2CWriteReadOnes(I2C_SLAVE_ADDRESS);
}
void ZD24C02A_test(uint16_t * Array)
{
uint16_t i=0;
for(i=0;i<=255;i+=15)
{
I2CWrite(SlaveAdr, 1, true, Array);
}
}

View File

@ -8,6 +8,12 @@
#ifndef SRC_ZD24C02A_H_
#define SRC_ZD24C02A_H_
#define I2C_SLAVE_ADDRESS 0x51U
#define I2C_OWN_ADDRESS 0x30U
void ZD24C02A_write(uint16_t byteCount, uint16_t * Array);
void ZD24C02A_read(uint16_t byteCount, uint16_t * Array);
void ZD24C02A_test(uint16_t * Array);
#endif /* SRC_ZD24C02A_H_ */

View File

@ -22,6 +22,10 @@ void GpioInit(void)
GpioCtrlRegs.GPAGMUX2.bit.GPIO18 = 0;
GpioCtrlRegs.GPADIR.bit.GPIO18 = 0;
GpioCtrlRegs.GPAGMUX2.bit.GPIO19 = 0;
GpioCtrlRegs.GPAGMUX2.bit.GPIO19 = 0;
GpioCtrlRegs.GPADIR.bit.GPIO19 = 0;
EDIS;
}

View File

@ -5,18 +5,14 @@
* Author: seklyuts
*/
#include "f28x_project.h"
#include "i2c_init.h"
//
// Function to configure I2CA as Master Transmitter.
//
#define I2C_SLAVE_ADDRESS 0x6AU
#define I2C_OWN_ADDRESS 0x30U
#define MAX_BUFFER_SIZE 0x10
#define I2C_NUMBYTES 0x2U
uint16_t I2C_TXdata[MAX_BUFFER_SIZE];
uint16_t I2C_RXdata[MAX_BUFFER_SIZE];
//
// I2C GPIO pins
@ -24,6 +20,17 @@ uint16_t I2C_RXdata[MAX_BUFFER_SIZE];
#define GPIO_PIN_SDAA 0U // GPIO number for I2C SDAA
#define GPIO_PIN_SCLA 1U // GPIO number for I2C SCLA
#define TIME_OVER 1000
Uint16 TimerTimeouts = 0, ErrI2c = 0, ErrI2c1 = 0, ErrI2c2 = 0, ErrI2c3 = 0, Addr=0, Addr1[255];
uint16_t RXdata;
void TimerBaseTimeoutInc(void)
{
TimerTimeouts++;
}
void I2CMasterGpioInit(void)
{
//
@ -47,7 +54,7 @@ void I2CMasterInit(uint16_t I2C_OwnAddress, uint16_t I2CSlave_Address)
// I2C configuration. Use a 400kHz I2CCLK with a 50% duty cycle.
//
//I2C_initMaster(base, DEVICE_SYSCLK_FREQ, 400000, I2C_DUTYCYCLE_50);
I2caRegs.I2CPSC.all = 0xB; // Prescaler - need 7-12 Mhz on module clk
I2caRegs.I2CPSC.all = 23; // Prescaler - need 7-12 Mhz on module clk
I2caRegs.I2CCLKL = 0x7; // NOTE: must be non zero
I2caRegs.I2CCLKH = 0x8; // NOTE: must be non zero
@ -61,7 +68,7 @@ void I2CMasterInit(uint16_t I2C_OwnAddress, uint16_t I2CSlave_Address)
//
// Set data count
//
I2caRegs.I2CCNT = I2C_NUMBYTES;
// I2caRegs.I2CCNT = I2C_NUMBYTES;
//
// Set the bit count to 8 bits per data byte
@ -97,10 +104,11 @@ void I2CMasterInit(uint16_t I2C_OwnAddress, uint16_t I2CSlave_Address)
}
uint16_t j = 0;
//
// Function to send data over I2C.
//
void I2CWrite(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition)
void I2CWrite(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition, uint16_t * I2C_TXdata)
{
//
@ -132,13 +140,16 @@ void I2CWrite(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition)
//
//transmit the bytes
//
for(index=0; index < I2C_NUMBYTES; index++)
for(index=0; index < byteCount; index++)
{
I2caRegs.I2CDXR.all= I2C_TXdata[index];
//
//wait till byte is sent
//
while(I2caRegs.I2CSTR.bit.BYTESENT != 0x1);
TimerTimeouts = 0;
while((I2caRegs.I2CSTR.bit.BYTESENT != 0x1)&&(TimerTimeouts < TIME_OVER));
if(TimerTimeouts >= TIME_OVER) ErrI2c++;
//
//clear the byte sent
//
@ -151,8 +162,9 @@ void I2CWrite(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition)
if(sendStopCondition)
{
I2caRegs.I2CMDR.bit.STP = 0x1;
while(I2caRegs.I2CMDR.bit.STP != 0x0);
TimerTimeouts = 0;
while((I2caRegs.I2CMDR.bit.STP != 0x0)&&(TimerTimeouts < TIME_OVER));
if(TimerTimeouts >= TIME_OVER) ErrI2c1++;
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
}
}
@ -160,7 +172,7 @@ void I2CWrite(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition)
//
// Function to read data over I2C. Returns the number of bytes read
//
uint16_t I2CRead(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition)
uint16_t I2CRead(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition, uint16_t * I2C_RXdata)
{
//
// Configure slave address
@ -176,26 +188,31 @@ uint16_t I2CRead(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition)
//
//Set Data Count
//
//I2caRegs.I2CCNT = byteCount;
I2caRegs.I2CCNT = byteCount;
//
// send Start condition
//
uint16_t count = 0;
if(count == (byteCount-1)) I2caRegs.I2CMDR.bit.NACKMOD = 0x1;
I2caRegs.I2CMDR.bit.STT = 0x1;
uint16_t count = 0;
//
// Read the received data into RX buffer
//
while(count < I2C_NUMBYTES)
TimerTimeouts = 0;
while((count < byteCount)&&(TimerTimeouts < TIME_OVER))
{
if(I2caRegs.I2CSTR.bit.RRDY ==0x1)
{
I2C_RXdata[count] = I2caRegs.I2CDRR.all;
RXdata = I2C_RXdata[count] = I2caRegs.I2CDRR.all;
count++;
}
}
if(TimerTimeouts >= TIME_OVER) ErrI2c2 += (byteCount - count);
I2caRegs.I2CMDR.bit.NACKMOD = 0x0;
//
// Send STOP condition
@ -203,12 +220,269 @@ uint16_t I2CRead(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition)
if(sendStopCondition)
{
I2caRegs.I2CMDR.bit.STP = 0x1;
while(I2caRegs.I2CMDR.bit.STP != 0x0);
TimerTimeouts = 0;
while((I2caRegs.I2CMDR.bit.STP != 0x0)&&(TimerTimeouts < TIME_OVER));
if(TimerTimeouts >= TIME_OVER) ErrI2c3++;
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
}
return count;
}
uint16_t I2CWriteRead(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition, uint16_t * I2C_RXdata)
{
//
// Locals
//
//
// Configure slave address
//
I2caRegs.I2CSAR.all = slaveAddr; // Slave address
//
// Configure I2C as Master Transmitter
//
I2caRegs.I2CMDR.bit.MST = 0x1;
I2caRegs.I2CMDR.bit.TRX = 0x1;
//
//Set Data Count
//
I2caRegs.I2CCNT = 1;
//
// send Start condition
//
I2caRegs.I2CMDR.bit.STT = 0x1;
//
//transmit the bytes
//
I2caRegs.I2CDXR.all= I2C_RXdata[0];
//
//wait till byte is sent
//
TimerTimeouts = 0;
while((I2caRegs.I2CSTR.bit.BYTESENT != 0x1)&&(TimerTimeouts < TIME_OVER));
if(TimerTimeouts >= TIME_OVER) ErrI2c++;
else Addr = slaveAddr;
//
//clear the byte sent
//
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
//
// Configure slave address
//
I2caRegs.I2CSAR.all = slaveAddr;
//
// Configure I2C in Master Receiver mode
//
I2caRegs.I2CMDR.bit.MST = 0x1;
I2caRegs.I2CMDR.bit.TRX = 0x0;
//
//Set Data Count
//
I2caRegs.I2CCNT = byteCount;
//
// send Start condition
//
I2caRegs.I2CMDR.bit.STT = 0x1;
uint16_t count = 0;
//
// Read the received data into RX buffer
//
TimerTimeouts = 0;
while((count < byteCount)&&(TimerTimeouts < TIME_OVER))
{
if(count == (byteCount-1)) I2caRegs.I2CMDR.bit.NACKMOD = 0x1;
if(I2caRegs.I2CSTR.bit.RRDY ==0x1)
{
I2C_RXdata[count] = I2caRegs.I2CDRR.all;
count++;
}
}
if(TimerTimeouts >= TIME_OVER) ErrI2c2 += (byteCount - count);
I2caRegs.I2CMDR.bit.NACKMOD = 0x0;
//
// Send STOP condition
//
if(sendStopCondition)
{
I2caRegs.I2CMDR.bit.STP = 0x1;
TimerTimeouts = 0;
while((I2caRegs.I2CMDR.bit.STP != 0x0)&&(TimerTimeouts < TIME_OVER));
if(TimerTimeouts >= TIME_OVER) ErrI2c3++;
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
}
return count;
}
void I2CWriteReadOnes(uint16_t slaveAddr)
{
//
// Locals
//
//
// Configure slave address
//
I2caRegs.I2CSAR.all = slaveAddr; // Slave address
//
// Configure I2C as Master Transmitter
//
I2caRegs.I2CMDR.bit.MST = 0x1;
I2caRegs.I2CMDR.bit.TRX = 0x1;
//
//Set Data Count
//
I2caRegs.I2CCNT = 1;
//
// send Start condition
//
I2caRegs.I2CMDR.bit.STT = 0x1;
//
//transmit the bytes
//
I2caRegs.I2CDXR.all= 0x80;
//
//wait till byte is sent
//
TimerTimeouts = 0;
while((I2caRegs.I2CSTR.bit.BYTESENT != 0x1)&&(TimerTimeouts < TIME_OVER));
if(TimerTimeouts >= TIME_OVER) ErrI2c++;
else Addr = slaveAddr;
//
//clear the byte sent
//
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
//
// Configure slave address
//
I2caRegs.I2CSAR.all = slaveAddr;
//
// Configure I2C in Master Receiver mode
//
I2caRegs.I2CMDR.bit.MST = 0x1;
I2caRegs.I2CMDR.bit.TRX = 0x0;
//
//Set Data Count
//
I2caRegs.I2CCNT = 0;
//
// send Start condition
//
I2caRegs.I2CMDR.bit.STT = 0x1;
//
// Read the received data into RX buffer
//
TimerTimeouts = 0;
I2caRegs.I2CMDR.bit.NACKMOD = 0x1;
while((I2caRegs.I2CSTR.bit.RRDY != 0x1)&&(TimerTimeouts < TIME_OVER));
if(TimerTimeouts >= TIME_OVER) ErrI2c2++;
else RXdata = I2caRegs.I2CDRR.all;
I2caRegs.I2CMDR.bit.NACKMOD = 0x0;
//
// Send STOP condition
//
I2caRegs.I2CMDR.bit.STP = 0x1;
TimerTimeouts = 0;
while((I2caRegs.I2CMDR.bit.STP != 0x0)&&(TimerTimeouts < TIME_OVER));
if(TimerTimeouts >= TIME_OVER) ErrI2c3++;
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
}
void I2CWriteOnse(uint16_t slaveAddr)
{
//
// Locals
//
uint16_t index = 0;
//
// Configure slave address
//
I2caRegs.I2CSAR.all = slaveAddr; // Slave address
//
// Configure I2C as Master Transmitter
//
I2caRegs.I2CMDR.bit.MST = 0x1;
I2caRegs.I2CMDR.bit.TRX = 0x1;
//
//Set Data Count
//
I2caRegs.I2CCNT = 2;
//
// send Start condition
//
I2caRegs.I2CMDR.bit.STT = 0x1;
//
//transmit the bytes
//
I2caRegs.I2CDXR.all= 0x80;
//
//wait till byte is sent
//
TimerTimeouts = 0;
while((I2caRegs.I2CSTR.bit.BYTESENT != 0x1)&&(TimerTimeouts < TIME_OVER));
if(TimerTimeouts >= TIME_OVER) ErrI2c++;
else Addr = slaveAddr;
//
//clear the byte sent
//
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
I2caRegs.I2CDXR.all= RXdata;
//
//wait till byte is sent
//
TimerTimeouts = 0;
while((I2caRegs.I2CSTR.bit.BYTESENT != 0x1)&&(TimerTimeouts < TIME_OVER));
if(TimerTimeouts >= TIME_OVER) ErrI2c++;
else Addr = slaveAddr;
//
//clear the byte sent
//
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
//
// Send STOP condition if specified
//
I2caRegs.I2CMDR.bit.STP = 0x1;
TimerTimeouts = 0;
while((I2caRegs.I2CMDR.bit.STP != 0x0)&&(TimerTimeouts < TIME_OVER));
if(TimerTimeouts >= TIME_OVER) ErrI2c1++;
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
}

View File

@ -8,12 +8,21 @@
#ifndef SRC_I2C_INIT_H_
#define SRC_I2C_INIT_H_
#define MAX_BUFFER_SIZE 0x10
//
// Function Prototypes
//
void I2CMasterInit(uint16_t I2CSlave_OwnAddress, uint16_t I2CSlave_Address);
void I2CWrite(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition);
uint16_t I2CRead(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition);
void I2CWrite(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition, uint16_t * I2C_TXdata);
uint16_t I2CRead(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition, uint16_t * I2C_RXdata);
uint16_t I2CWriteRead(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition, uint16_t * I2C_RXdata);
void I2CMasterGpioInit(void);
void TimerBaseTimeoutInc(void);
void I2CWriteReadOnes(uint16_t slaveAddr);
void I2CWriteOnse(uint16_t slaveAddr);
#endif /* SRC_I2C_INIT_H_ */

View File

@ -13,7 +13,9 @@
#include "gpio_init.h"
#include "spi_init.h"
#include "i2c_init.h"
#include "BL25CM1A.h"
#include "GD25Q16ETIGR.h"
#include "ZD24C02A.h"
void InitPerif(void)
{
@ -65,7 +67,7 @@ void InitPerif(void)
SdfmInitInterruptEn();
SdfmInit(SDFM1);
SpiInit();
// I2CMasterInit();
I2CMasterInit(I2C_OWN_ADDRESS,I2C_SLAVE_ADDRESS);
//
// Enable global Interrupts and higher priority real-time debug events:
//

View File

@ -64,6 +64,7 @@ void PWMAllInit(void)
PwmBrake100 = PERIOD_BRAKE;
PwmMotor100 = PERIOD_MOTOR;
PWMInit(1, PwmMotor100, INDEPENDED);
PWMInit(2, PwmMotor100, COMPLIMENTARY);
PWMInit(3, PwmMotor100, COMPLIMENTARY);
PWMInit(4, PwmMotor100, COMPLIMENTARY);
@ -88,12 +89,12 @@ void PWMGpioInit(void)
void PWMInitEnable(void)
{
CpuSysRegs.PCLKCR2.bit.EPWM1=1;
CpuSysRegs.PCLKCR2.bit.EPWM2=1;
CpuSysRegs.PCLKCR2.bit.EPWM3=1;
CpuSysRegs.PCLKCR2.bit.EPWM4=1;
CpuSysRegs.PCLKCR2.bit.EPWM5=1;
CpuSysRegs.PCLKCR2.bit.EPWM6=1;
CpuSysRegs.PCLKCR2.bit.EPWM11=1;
}
void PWMInitInterruptEn(void)
@ -102,7 +103,7 @@ void PWMInitInterruptEn(void)
// ISR functions found within this file.
//
EALLOW; // This is needed to write to EALLOW protected registers
// PieVectTable.EPWM1_INT = &epwm1_isr;
PieVectTable.EPWM1_INT = &epwm1_isr;
PieVectTable.EPWM2_INT = &epwm2_isr;
PieVectTable.EPWM3_INT = &epwm3_isr;
PieVectTable.EPWM4_INT = &epwm4_isr;
@ -118,7 +119,7 @@ void PWMInitInterruptEn(void)
//
// Enable EPWM INTn in the PIE: Group 3 interrupt 1-3
//
// PieCtrlRegs.PIEIER3.bit.INTx1 = 1;
PieCtrlRegs.PIEIER3.bit.INTx1 = 1;
PieCtrlRegs.PIEIER3.bit.INTx2 = 1;
PieCtrlRegs.PIEIER3.bit.INTx3 = 1;
PieCtrlRegs.PIEIER3.bit.INTx4 = 1;

View File

@ -9,13 +9,13 @@
#include "pwm_init.h"
#include"frm_uart.h"
#include "gpio_init.h"
#include "i2c_init.h"
#include "timer_base.h"
volatile Uint16 AutoChange = 0;
volatile Uint16 PWM_out = 0;
volatile Uint16 PWM_motor = PERIOD_2;
Uint16 Fault = 0, Fault_fix = 0;
Uint16 Fault = 0, Fault_fix = 0, Ready = 0;
//
// epwm1_isr - EPWM1 ISR
//
@ -29,6 +29,8 @@ __interrupt void epwm1_isr(void)
// Clear INT flag for this timer
//
EPwm1Regs.ETCLR.bit.INT = 1;
TimerBaseTimeoutInc();
MainTimerBaseTimeoutInc();
//
// Acknowledge this interrupt to receive more interrupts from group 3
@ -99,6 +101,7 @@ __interrupt void epwm4_isr(void)
__interrupt void epwm5_isr(void)
{
Ready = GpioDataRegs.GPADAT.bit.GPIO19;
Fault = !GpioDataRegs.GPADAT.bit.GPIO18;
if(Fault || Fault_fix)
{