test soft i2c write and read passed
This commit is contained in:
16
3rd/delay.c
16
3rd/delay.c
@@ -1,9 +1,13 @@
|
||||
#include "delay.h"
|
||||
#include "ti_msp_dl_config.h"
|
||||
void delay_ms(uint16_t ms)
|
||||
{
|
||||
while(ms--)
|
||||
{
|
||||
delay_cycles(CPUCLK_FREQ / 1000);
|
||||
}
|
||||
void delay_ms(uint16_t ms) {
|
||||
while (ms--) {
|
||||
delay_cycles(CPUCLK_FREQ / 1000);
|
||||
}
|
||||
}
|
||||
|
||||
void delay_us(uint16_t us) {
|
||||
while (us--) {
|
||||
delay_cycles(CPUCLK_FREQ / 1000000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,5 +3,6 @@
|
||||
#include <stdint.h>
|
||||
|
||||
void delay_ms(uint16_t ms);
|
||||
void delay_us(uint16_t us);
|
||||
|
||||
#endif /* ti_msp_dl_config_h */
|
||||
239
3rd/mt6701.c
239
3rd/mt6701.c
@@ -28,129 +28,134 @@ volatile uint32_t gClockSelFreq;
|
||||
volatile uint32_t gDelayCycles;
|
||||
|
||||
/* I2C Target address */
|
||||
#define I2C_TARGET_ADDRESS (0x06)
|
||||
|
||||
// void MT6701_iic_read_angel(void) {
|
||||
|
||||
// I2C_Start();
|
||||
// I2C_SendByte(I2C_TARGET_ADDRESS);
|
||||
// I2C_RecviveAck();
|
||||
// I2C_SendByte(0X03);
|
||||
// I2C_RecviveAck();
|
||||
|
||||
// I2C_Start();
|
||||
// I2C_SendByte(I2C_TARGET_ADDRESS | 0x01);
|
||||
// I2C_RecviveAck();
|
||||
// gRxPacket[0] = I2C_RecviveData();
|
||||
// I2C_RecviveAck();
|
||||
|
||||
// I2C_Start();
|
||||
// I2C_SendByte(I2C_TARGET_ADDRESS | 0x01);
|
||||
// I2C_RecviveAck();
|
||||
// gRxPacket[1] = I2C_RecviveData();
|
||||
// I2C_SendAck(1);
|
||||
// I2C_Stop();
|
||||
// }
|
||||
#define I2C_TARGET_ADDRESS (0x06 << 1)
|
||||
|
||||
void MT6701_iic_read_angel(void) {
|
||||
/* Get I2C clock source and clock divider to use for delay cycle
|
||||
calculation */
|
||||
DL_I2C_getClockConfig(I2C_1_INST, &gI2CclockConfig);
|
||||
switch (gI2CclockConfig.clockSel) {
|
||||
case DL_I2C_CLOCK_BUSCLK:
|
||||
gClockSelFreq = 32000000;
|
||||
break;
|
||||
case DL_I2C_CLOCK_MFCLK:
|
||||
gClockSelFreq = 4000000;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
/*
|
||||
* Calculate number of clock cycles to delay after controller transfer
|
||||
initiated
|
||||
* gDelayCycles = 3 I2C functional clock cycles
|
||||
* gDelayCycles = 3 * I2C clock divider * (CPU clock freq / I2C clock
|
||||
freq)
|
||||
*/
|
||||
gDelayCycles =
|
||||
(5 * (gI2CclockConfig.divideRatio + 1)) * (CPUCLK_FREQ / gClockSelFreq);
|
||||
if (DEBUG_ENABLED & DEBUG_MT_ENABLED & DEBUG_I2C) {
|
||||
printf("i2c before writing -------\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* Fill FIFO with data. This example will send a MAX of 8 bytes since it
|
||||
* doesn't handle the case where FIFO is full
|
||||
*/
|
||||
DL_I2C_fillControllerTXFIFO(I2C_1_INST, &gTxPacket[0], I2C_TX_PACKET_SIZE);
|
||||
I2C_Start();
|
||||
I2C_SendByte(I2C_TARGET_ADDRESS);
|
||||
I2C_RecviveAck();
|
||||
I2C_SendByte(0X03);
|
||||
I2C_RecviveAck();
|
||||
|
||||
/* Wait for I2C to be Idle */
|
||||
while (
|
||||
!(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_IDLE))
|
||||
;
|
||||
I2C_Start();
|
||||
I2C_SendByte(I2C_TARGET_ADDRESS | 0x01);
|
||||
I2C_RecviveAck();
|
||||
gRxPacket[0] = I2C_RecviveData();
|
||||
I2C_SendAck(1);
|
||||
|
||||
/* Send the packet to the controller.
|
||||
* This function will send Start + Stop automatically.
|
||||
*/
|
||||
DL_I2C_startControllerTransfer(I2C_1_INST, I2C_TARGET_ADDRESS,
|
||||
DL_I2C_CONTROLLER_DIRECTION_TX,
|
||||
I2C_TX_PACKET_SIZE);
|
||||
|
||||
if (DEBUG_ENABLED & DEBUG_MT_ENABLED & DEBUG_I2C) {
|
||||
printf("i2c writing done -------\n");
|
||||
}
|
||||
|
||||
/* Workaround for errata I2C_ERR_13 */
|
||||
delay_cycles(gDelayCycles);
|
||||
|
||||
/* Poll until the Controller writes all bytes */
|
||||
while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY)
|
||||
;
|
||||
|
||||
/* Trap if there was an error */
|
||||
if (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_ERROR) {
|
||||
gIsI2cError = true;
|
||||
|
||||
if (DEBUG_ENABLED & DEBUG_MT_ENABLED & DEBUG_I2C) {
|
||||
printf("i2c error ------------------------------------------\n");
|
||||
}
|
||||
/* LED will remain high if there is an error */
|
||||
__BKPT(0);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Wait for I2C to be Idle */
|
||||
while (
|
||||
!(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_IDLE))
|
||||
;
|
||||
|
||||
/* Add delay between transfers */
|
||||
delay_cycles(1000);
|
||||
|
||||
if (DEBUG_ENABLED & DEBUG_MT_ENABLED & DEBUG_I2C) {
|
||||
printf("i2c before reading -------\n");
|
||||
}
|
||||
|
||||
/* Send a read request to Target */
|
||||
DL_I2C_startControllerTransfer(I2C_1_INST, I2C_TARGET_ADDRESS,
|
||||
DL_I2C_CONTROLLER_DIRECTION_RX,
|
||||
I2C_RX_PACKET_SIZE);
|
||||
|
||||
/*
|
||||
* Receive all bytes from target. LED will remain high if not all bytes
|
||||
* are received
|
||||
*/
|
||||
for (uint8_t i = 0; i < I2C_RX_PACKET_SIZE; i++) {
|
||||
while (DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST))
|
||||
;
|
||||
gRxPacket[i] = DL_I2C_receiveControllerData(I2C_1_INST);
|
||||
}
|
||||
|
||||
if (DEBUG_ENABLED & DEBUG_MT_ENABLED & DEBUG_I2C) {
|
||||
printf("i2c reading done -------\n");
|
||||
}
|
||||
I2C_Start();
|
||||
I2C_SendByte(I2C_TARGET_ADDRESS | 0x01);
|
||||
I2C_RecviveAck();
|
||||
gRxPacket[1] = I2C_RecviveData();
|
||||
I2C_SendAck(1);
|
||||
I2C_Stop();
|
||||
}
|
||||
|
||||
// void MT6701_iic_read_angel(void) {
|
||||
// /* Get I2C clock source and clock divider to use for delay cycle
|
||||
// calculation */
|
||||
// DL_I2C_getClockConfig(I2C_1_INST, &gI2CclockConfig);
|
||||
// switch (gI2CclockConfig.clockSel) {
|
||||
// case DL_I2C_CLOCK_BUSCLK:
|
||||
// gClockSelFreq = 32000000;
|
||||
// break;
|
||||
// case DL_I2C_CLOCK_MFCLK:
|
||||
// gClockSelFreq = 4000000;
|
||||
// break;
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
// /*
|
||||
// * Calculate number of clock cycles to delay after controller transfer
|
||||
// initiated
|
||||
// * gDelayCycles = 3 I2C functional clock cycles
|
||||
// * gDelayCycles = 3 * I2C clock divider * (CPU clock freq / I2C clock
|
||||
// freq)
|
||||
// */
|
||||
// gDelayCycles =
|
||||
// (5 * (gI2CclockConfig.divideRatio + 1)) * (CPUCLK_FREQ /
|
||||
// gClockSelFreq);
|
||||
// if (DEBUG_ENABLED & DEBUG_MT_ENABLED & DEBUG_I2C) {
|
||||
// printf("i2c before writing -------\n");
|
||||
// }
|
||||
|
||||
// /*
|
||||
// * Fill FIFO with data. This example will send a MAX of 8 bytes since it
|
||||
// * doesn't handle the case where FIFO is full
|
||||
// */
|
||||
// DL_I2C_fillControllerTXFIFO(I2C_1_INST, &gTxPacket[0], I2C_TX_PACKET_SIZE);
|
||||
|
||||
// /* Wait for I2C to be Idle */
|
||||
// while (
|
||||
// !(DL_I2C_getControllerStatus(I2C_1_INST) &
|
||||
// DL_I2C_CONTROLLER_STATUS_IDLE))
|
||||
// ;
|
||||
|
||||
// /* Send the packet to the controller.
|
||||
// * This function will send Start + Stop automatically.
|
||||
// */
|
||||
// DL_I2C_startControllerTransfer(I2C_1_INST, I2C_TARGET_ADDRESS,
|
||||
// DL_I2C_CONTROLLER_DIRECTION_TX,
|
||||
// I2C_TX_PACKET_SIZE);
|
||||
|
||||
// if (DEBUG_ENABLED & DEBUG_MT_ENABLED & DEBUG_I2C) {
|
||||
// printf("i2c writing done -------\n");
|
||||
// }
|
||||
|
||||
// /* Workaround for errata I2C_ERR_13 */
|
||||
// delay_cycles(gDelayCycles);
|
||||
|
||||
// /* Poll until the Controller writes all bytes */
|
||||
// while (DL_I2C_getControllerStatus(I2C_1_INST) &
|
||||
// DL_I2C_CONTROLLER_STATUS_BUSY)
|
||||
// ;
|
||||
|
||||
// /* Trap if there was an error */
|
||||
// if (DL_I2C_getControllerStatus(I2C_1_INST) &
|
||||
// DL_I2C_CONTROLLER_STATUS_ERROR) {
|
||||
// gIsI2cError = true;
|
||||
|
||||
// if (DEBUG_ENABLED & DEBUG_MT_ENABLED & DEBUG_I2C) {
|
||||
// printf("i2c error ------------------------------------------\n");
|
||||
// }
|
||||
// /* LED will remain high if there is an error */
|
||||
// __BKPT(0);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// /* Wait for I2C to be Idle */
|
||||
// while (
|
||||
// !(DL_I2C_getControllerStatus(I2C_1_INST) &
|
||||
// DL_I2C_CONTROLLER_STATUS_IDLE))
|
||||
// ;
|
||||
|
||||
// /* Add delay between transfers */
|
||||
// delay_cycles(1000);
|
||||
|
||||
// if (DEBUG_ENABLED & DEBUG_MT_ENABLED & DEBUG_I2C) {
|
||||
// printf("i2c before reading -------\n");
|
||||
// }
|
||||
|
||||
// /* Send a read request to Target */
|
||||
// DL_I2C_startControllerTransfer(I2C_1_INST, I2C_TARGET_ADDRESS,
|
||||
// DL_I2C_CONTROLLER_DIRECTION_RX,
|
||||
// I2C_RX_PACKET_SIZE);
|
||||
|
||||
// /*
|
||||
// * Receive all bytes from target. LED will remain high if not all bytes
|
||||
// * are received
|
||||
// */
|
||||
// for (uint8_t i = 0; i < I2C_RX_PACKET_SIZE; i++) {
|
||||
// while (DL_I2C_isControllerRXFIFOEmpty(I2C_1_INST))
|
||||
// ;
|
||||
// gRxPacket[i] = DL_I2C_receiveControllerData(I2C_1_INST);
|
||||
// }
|
||||
|
||||
// if (DEBUG_ENABLED & DEBUG_MT_ENABLED & DEBUG_I2C) {
|
||||
// printf("i2c reading done -------\n");
|
||||
// }
|
||||
// }
|
||||
volatile float Last_ts = 0.0;
|
||||
volatile float last_angle = 0.0;
|
||||
|
||||
|
||||
@@ -1,20 +1,46 @@
|
||||
#include "soft_i2c.h"
|
||||
#include "delay.h"
|
||||
#include "ti_msp_dl_config.h"
|
||||
|
||||
#define DELAY 1
|
||||
#define SDA_PIN_SHIFT 9 // if pa.8 shift8 , if pa.9 shift 9
|
||||
void I2C_ENABLE_OUTPUT_SDA(void) {
|
||||
DL_GPIO_initDigitalOutput(SOFT_I2C_SDA_IOMUX);
|
||||
DL_GPIO_enableOutput(SOFT_I2C_PORT, SOFT_I2C_SDA_PIN);
|
||||
}
|
||||
void I2C_ENABLE_INPUT_SDA(void) {
|
||||
DL_GPIO_disableOutput(SOFT_I2C_PORT, SOFT_I2C_SDA_PIN);
|
||||
DL_GPIO_initDigitalInputFeatures(
|
||||
SOFT_I2C_SDA_IOMUX, DL_GPIO_INVERSION_DISABLE, DL_GPIO_RESISTOR_PULL_UP,
|
||||
DL_GPIO_HYSTERESIS_DISABLE, DL_GPIO_WAKEUP_DISABLE);
|
||||
}
|
||||
|
||||
void I2C_W_SCL(uint8_t BitValue) {
|
||||
DL_GPIO_writePinsVal(SOFT_I2C_PORT, SOFT_I2C_CLK_PIN, BitValue);
|
||||
if (BitValue) {
|
||||
DL_GPIO_setPins(SOFT_I2C_PORT, SOFT_I2C_CLK_PIN);
|
||||
|
||||
} else {
|
||||
DL_GPIO_clearPins(SOFT_I2C_PORT, SOFT_I2C_CLK_PIN);
|
||||
}
|
||||
delay_us(DELAY);
|
||||
}
|
||||
|
||||
void I2C_W_SDA(uint8_t BitValue) {
|
||||
DL_GPIO_writePinsVal(SOFT_I2C_PORT, SOFT_I2C_SDA_PIN, BitValue);
|
||||
if (BitValue) {
|
||||
DL_GPIO_setPins(SOFT_I2C_PORT, SOFT_I2C_SDA_PIN);
|
||||
|
||||
} else {
|
||||
DL_GPIO_clearPins(SOFT_I2C_PORT, SOFT_I2C_SDA_PIN);
|
||||
}
|
||||
delay_us(DELAY);
|
||||
}
|
||||
|
||||
// 读取时钟线数据
|
||||
uint8_t I2C_R_SDA(void) {
|
||||
uint8_t BitValue;
|
||||
uint32_t BitValue;
|
||||
BitValue = DL_GPIO_readPins(SOFT_I2C_PORT, SOFT_I2C_SDA_PIN);
|
||||
|
||||
return BitValue;
|
||||
return (uint8_t)(BitValue >> 9);
|
||||
}
|
||||
|
||||
void I2C_Start(void) {
|
||||
@@ -44,11 +70,13 @@ uint8_t I2C_RecviveData(void) {
|
||||
uint8_t i, Byte = 0x00;
|
||||
I2C_W_SDA(1);
|
||||
for (i = 0; i < 8; i++) {
|
||||
I2C_ENABLE_INPUT_SDA();
|
||||
I2C_W_SCL(1);
|
||||
if (I2C_R_SDA() == 1) {
|
||||
Byte |= (0x80 >> i);
|
||||
}
|
||||
I2C_W_SCL(0);
|
||||
I2C_ENABLE_OUTPUT_SDA();
|
||||
}
|
||||
return Byte;
|
||||
}
|
||||
@@ -64,9 +92,12 @@ uint8_t I2C_RecviveAck(void) {
|
||||
uint8_t AckBit;
|
||||
|
||||
I2C_W_SDA(1);
|
||||
I2C_ENABLE_INPUT_SDA();
|
||||
I2C_W_SCL(1);
|
||||
|
||||
AckBit = I2C_R_SDA();
|
||||
I2C_W_SCL(0);
|
||||
I2C_ENABLE_OUTPUT_SDA();
|
||||
|
||||
return AckBit;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user