Инициализация i2c + рефакторинг PWM

This commit is contained in:
Eugene 2023-09-08 12:18:50 +03:00
parent 9a236d1418
commit 2a4d4cfce3
11 changed files with 257 additions and 33 deletions

Binary file not shown.

View File

@ -17,6 +17,7 @@
#include "init_perif.h" #include "init_perif.h"
#include"frm_uart.h" #include"frm_uart.h"
#include "spi_init.h" #include "spi_init.h"
#include "BL25CM1A.h" #include "BL25CM1A.h"
#include "GD25Q16ETIGR.h" #include "GD25Q16ETIGR.h"
// //

View File

@ -9,14 +9,14 @@
void GpioInit(void) void GpioInit(void)
{ {
EALLOW; EALLOW;
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0; GpioCtrlRegs.GPAMUX2.bit.GPIO20 = 0;
GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 0; GpioCtrlRegs.GPAMUX2.bit.GPIO21 = 0;
GpioCtrlRegs.GPAGMUX1.bit.GPIO0 = 0; GpioCtrlRegs.GPAGMUX2.bit.GPIO20 = 0;
GpioCtrlRegs.GPAGMUX1.bit.GPIO1 = 0; GpioCtrlRegs.GPAGMUX2.bit.GPIO21 = 0;
GpioCtrlRegs.GPADIR.bit.GPIO0 = 1; GpioCtrlRegs.GPADIR.bit.GPIO20 = 1;
GpioCtrlRegs.GPADIR.bit.GPIO1 = 1; GpioCtrlRegs.GPADIR.bit.GPIO21 = 1;
GpioDataRegs.GPADAT.bit.GPIO0 = 0; GpioDataRegs.GPADAT.bit.GPIO20 = 0;
GpioDataRegs.GPADAT.bit.GPIO1 = 0; GpioDataRegs.GPADAT.bit.GPIO21 = 0;
GpioCtrlRegs.GPAGMUX2.bit.GPIO18 = 0; GpioCtrlRegs.GPAGMUX2.bit.GPIO18 = 0;
GpioCtrlRegs.GPAGMUX2.bit.GPIO18 = 0; GpioCtrlRegs.GPAGMUX2.bit.GPIO18 = 0;
@ -25,13 +25,13 @@ void GpioInit(void)
EDIS; EDIS;
} }
void Gpio0out(uint16_t out_bit) void Gpio20out(uint16_t out_bit)
{ {
GpioDataRegs.GPADAT.bit.GPIO0 = out_bit; GpioDataRegs.GPADAT.bit.GPIO20 = out_bit;
} }
void Gpio1out(uint16_t out_bit) void Gpio21out(uint16_t out_bit)
{ {
GpioDataRegs.GPADAT.bit.GPIO1 = out_bit; GpioDataRegs.GPADAT.bit.GPIO21 = out_bit;
} }

View File

@ -10,7 +10,7 @@
void GpioInit(void); void GpioInit(void);
void Gpio0out(uint16_t out_bit); void Gpio20out(uint16_t out_bit);
void Gpio1out(uint16_t out_bit); void Gpio21out(uint16_t out_bit);
#endif /* SRC_GPIO_INIT_H_ */ #endif /* SRC_GPIO_INIT_H_ */

View File

@ -4,5 +4,211 @@
* Created on: 5 ñåíò. 2023 ã. * Created on: 5 ñåíò. 2023 ã.
* Author: seklyuts * Author: seklyuts
*/ */
#include "f28x_project.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
//
#define GPIO_PIN_SDAA 26U // GPIO number for I2C SDAA
#define GPIO_PIN_SCLA 27U // GPIO number for I2C SCLA
void I2CMasterGpioInit(void)
{
//
//Configure I2C pins
//
GPIO_SetupPinMux(GPIO_PIN_SDAA, GPIO_MUX_CPU1, 11);
GPIO_SetupPinOptions(GPIO_PIN_SDAA, GPIO_OUTPUT, GPIO_PULLUP);
GPIO_SetupPinMux(GPIO_PIN_SCLA, GPIO_MUX_CPU1, 11);
GPIO_SetupPinOptions(GPIO_PIN_SCLA, GPIO_OUTPUT, GPIO_PULLUP);
}
void I2CMasterInit(uint16_t I2C_OwnAddress, uint16_t I2CSlave_Address)
{
EALLOW;
//
// Must put I2C into reset before configuring it
//
I2caRegs.I2CMDR.all &= ~(0x20U);
//
// 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.I2CCLKL = 0x7; // NOTE: must be non zero
I2caRegs.I2CCLKH = 0x8; // NOTE: must be non zero
//
// Configure Master as a Transmitter
//
I2caRegs.I2CMDR.bit.MST = 0x1;
I2caRegs.I2CMDR.bit.TRX = 0x1;
//
// Set data count
//
I2caRegs.I2CCNT = I2C_NUMBYTES;
//
// Set the bit count to 8 bits per data byte
//
I2caRegs.I2CMDR.bit.BC = 0x0U;
//
// Configure slave and own address
//
I2caRegs.I2COAR.all = I2C_OwnAddress; // Own address
I2caRegs.I2CSAR.all = I2CSlave_Address; // Slave address
//
// Set emulation mode to FREE
//
I2caRegs.I2CMDR.bit.FREE = 0x1;
//
//Clear all status
//
I2caRegs.I2CSTR.all = 0xFFFF;
//
// Enable I2C Interrupts- RRDY
//
I2caRegs.I2CIER.all = 0x08;
//
// Take I2C out of reset
//
I2caRegs.I2CMDR.all |= 0x0020;
}
//
// Function to send data over I2C.
//
void I2CWrite(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition)
{
//
// 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 = byteCount;
//
// send Start condition
//
I2caRegs.I2CMDR.bit.STT = 0x1;
//
//transmit the bytes
//
for(index=0; index < I2C_NUMBYTES; index++)
{
I2caRegs.I2CDXR.all= I2C_TXdata[index];
//
//wait till byte is sent
//
while(I2caRegs.I2CSTR.bit.BYTESENT != 0x1);
//
//clear the byte sent
//
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
}
//
// Send STOP condition if specified
//
if(sendStopCondition)
{
I2caRegs.I2CMDR.bit.STP = 0x1;
while(I2caRegs.I2CMDR.bit.STP != 0x0);
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
}
}
//
// Function to read data over I2C. Returns the number of bytes read
//
uint16_t I2CRead(uint16_t slaveAddr, uint16_t byteCount, bool sendStopCondition)
{
//
// 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
//
while(count < I2C_NUMBYTES)
{
if(I2caRegs.I2CSTR.bit.RRDY ==0x1)
{
I2C_RXdata[count] = I2caRegs.I2CDRR.all;
count++;
}
}
//
// Send STOP condition
//
if(sendStopCondition)
{
I2caRegs.I2CMDR.bit.STP = 0x1;
while(I2caRegs.I2CMDR.bit.STP != 0x0);
I2caRegs.I2CSTR.bit.BYTESENT = 0x1;
}
return count;
}

View File

@ -8,6 +8,12 @@
#ifndef SRC_I2C_INIT_H_ #ifndef SRC_I2C_INIT_H_
#define SRC_I2C_INIT_H_ #define SRC_I2C_INIT_H_
//
// 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 I2CMasterGpioInit(void);
#endif /* SRC_I2C_INIT_H_ */ #endif /* SRC_I2C_INIT_H_ */

View File

@ -12,6 +12,7 @@
#include "f2838x_sdfm_drivers.h" #include "f2838x_sdfm_drivers.h"
#include "gpio_init.h" #include "gpio_init.h"
#include "spi_init.h" #include "spi_init.h"
#include "i2c_init.h"
void InitPerif(void) void InitPerif(void)
@ -25,6 +26,7 @@ void InitPerif(void)
SdfmInitEnable(); SdfmInitEnable();
SpiaGpioInit(); SpiaGpioInit();
I2CMasterGpioInit();
// Clear all interrupts and initialize PIE vector table: // Clear all interrupts and initialize PIE vector table:
@ -63,6 +65,7 @@ void InitPerif(void)
SdfmInitInterruptEn(); SdfmInitInterruptEn();
SdfmInit(SDFM1); SdfmInit(SDFM1);
SpiInit(); SpiInit();
// I2CMasterInit();
// //
// Enable global Interrupts and higher priority real-time debug events: // Enable global Interrupts and higher priority real-time debug events:
// //

View File

@ -64,11 +64,11 @@ void PWMAllInit(void)
PwmBrake100 = PERIOD_BRAKE; PwmBrake100 = PERIOD_BRAKE;
PwmMotor100 = PERIOD_MOTOR; PwmMotor100 = PERIOD_MOTOR;
PWMInit(2, PERIOD_MOTOR, COMPLIMENTARY); PWMInit(2, PwmMotor100, COMPLIMENTARY);
PWMInit(3, PERIOD_MOTOR, COMPLIMENTARY); PWMInit(3, PwmMotor100, COMPLIMENTARY);
PWMInit(4, PERIOD_MOTOR, COMPLIMENTARY); PWMInit(4, PwmMotor100, COMPLIMENTARY);
PWMInit(5, PERIOD_BRAKE, INDEPENDED); PWMInit(5, PwmBrake100, INDEPENDED);
PWMInit(6, PERIOD_BRAKE, INDEPENDED); PWMInit(6, PwmBrake100, INDEPENDED);
// PWMInit(11, PERIOD, INDEPENDED); // PWMInit(11, PERIOD, INDEPENDED);
EALLOW; EALLOW;

View File

@ -8,13 +8,21 @@
#ifndef SRC_PWM_INIT_H_ #ifndef SRC_PWM_INIT_H_
#define SRC_PWM_INIT_H_ #define SRC_PWM_INIT_H_
#define SYS_PWM_FREQUENCY 100000000.0 //Hz
#define PERIOD_BRAKE 1000
#define PERIOD_MOTOR 5000 #define FREQUENCY_BRAKE 20000.0 //Hz
#define EPWM_DB 150 #define FREQUENCY_MOTOR 10000.0 //Hz
#define PERIOD_2 PERIOD_MOTOR/2 #define EPWM_DB_mkS 3.0 //mkS
#define PWM_MAX PERIOD_MOTOR - EPWM_DB
#define PWM_MIN EPWM_DB #define PERIOD_BRAKE (SYS_PWM_FREQUENCY/2/FREQUENCY_BRAKE) //Tic
#define PERIOD_MOTOR (SYS_PWM_FREQUENCY/2/FREQUENCY_MOTOR) //Tic
#define EPWM_DB (EPWM_DB_mkS*SYS_PWM_FREQUENCY/2/1000000)
#define PERIOD_2 (PERIOD_MOTOR/2)
#define PWM_MAX (PERIOD_MOTOR - EPWM_DB)
#define PWM_MIN EPWM_DB
#define INDEPENDED 1 #define INDEPENDED 1
#define COMPLIMENTARY 0 #define COMPLIMENTARY 0

View File

@ -41,7 +41,7 @@ __interrupt void epwm1_isr(void)
// //
__interrupt void epwm2_isr(void) __interrupt void epwm2_isr(void)
{ {
Gpio0out(1); Gpio20out(1);
// if(AutoChange) pwm_AutoChange(2); // if(AutoChange) pwm_AutoChange(2);
// else // else
EPwm2Regs.CMPA.bit.CMPA = PERIOD_MOTOR - PWM_motor; EPwm2Regs.CMPA.bit.CMPA = PERIOD_MOTOR - PWM_motor;
@ -51,7 +51,7 @@ __interrupt void epwm2_isr(void)
// Clear INT flag for this timer // Clear INT flag for this timer
// //
EPwm2Regs.ETCLR.bit.INT = 1; EPwm2Regs.ETCLR.bit.INT = 1;
Gpio0out(0); Gpio20out(0);
// //
// Acknowledge this interrupt to receive more interrupts from group 3 // Acknowledge this interrupt to receive more interrupts from group 3
// //

View File

@ -33,7 +33,7 @@ int16_t Filter4_Result[MAX_SAMPLES];
#pragma DATA_SECTION(Filter2_Result,"Filter2_RegsFile"); #pragma DATA_SECTION(Filter2_Result,"Filter2_RegsFile");
#pragma DATA_SECTION(Filter3_Result,"Filter3_RegsFile"); #pragma DATA_SECTION(Filter3_Result,"Filter3_RegsFile");
#pragma DATA_SECTION(Filter4_Result,"Filter4_RegsFile"); #pragma DATA_SECTION(Filter4_Result,"Filter4_RegsFile");
int16_t ADC_Volt = 0; int16_t ADC_ampere = 0;
int16_t sdfmOffset = 0; int16_t sdfmOffset = 0;
uint16_t startInitCurrent = 0; uint16_t startInitCurrent = 0;
uint16_t initDone = 0; uint16_t initDone = 0;
@ -153,7 +153,7 @@ __interrupt void Sdfm1_ISR(void)
uint16_t i = 0; uint16_t i = 0;
int32_t OffsetCount = 0; int32_t OffsetCount = 0;
Gpio1out(1); Gpio21out(1);
// //
// Wait for result from all the filters (SDIFLG) // Wait for result from all the filters (SDIFLG)
// //
@ -180,12 +180,12 @@ __interrupt void Sdfm1_ISR(void)
} }
} }
Filter1_Result[loopCounter1++] = SDFM1_READ_FILTER1_DATA_16BIT; Filter1_Result[loopCounter1++] = SDFM1_READ_FILTER1_DATA_16BIT;
ADC_Volt = Filter1_Result[loopCounter1-1] - sdfmOffset; ADC_ampere = Filter1_Result[loopCounter1-1] - sdfmOffset;
} }
if(IntFlags & 0x100) if(IntFlags & 0x100)
{ {
ADC_Volt = 0; ADC_ampere = 0;
EALLOW; EALLOW;
Sdfm1Regs.SDCPARM1.bit.MFIE = 0; Sdfm1Regs.SDCPARM1.bit.MFIE = 0;
EDIS; EDIS;
@ -206,7 +206,7 @@ __interrupt void Sdfm1_ISR(void)
// Acknowledge this __interrupt to receive more __interrupts from group 5 // Acknowledge this __interrupt to receive more __interrupts from group 5
// //
PieCtrlRegs.PIEACK.all = PIEACK_GROUP5; PieCtrlRegs.PIEACK.all = PIEACK_GROUP5;
Gpio1out(0); Gpio21out(0);
} }
//Sdfm_clearFlagRegister //Sdfm_clearFlagRegister