mt6701 uart_redirect

This commit is contained in:
2025-11-10 18:05:11 +08:00
commit ae9f390667
17 changed files with 1909 additions and 0 deletions

9
3rd/delay.c Normal file
View File

@@ -0,0 +1,9 @@
#include "Delay.h"
#include "ti_msp_dl_config.h"
void delay_ms(uint16_t ms)
{
while(ms--)
{
delay_cycles(CPUCLK_FREQ / 1000);
}
}

7
3rd/delay.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef delay_h
#define delay_h
#include <stdint.h>
void delay_ms(uint16_t ms);
#endif /* ti_msp_dl_config_h */

114
3rd/mt6701.c Normal file
View File

@@ -0,0 +1,114 @@
#include "MT6701.h"
#include "ti_msp_dl_config.h"
volatile int16_t angle;
volatile float angle_f;
/* Data sent to the Target */
uint8_t gTxPacket[I2C_TX_PACKET_SIZE] =
{
0x03
};
/* Data received from Target */
volatile uint8_t gRxPacket[I2C_RX_PACKET_SIZE];
/* I2C clock configuration */
DL_I2C_ClockConfig gI2CclockConfig;
/* Frequency of selected I2C clock*/
volatile uint32_t gClockSelFreq;
/* Cycles to delay after controller transfer initiated */
volatile uint32_t gDelayCycles;
/* I2C Target address */
#define I2C_TARGET_ADDRESS (0x06)
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 = (3 * (gI2CclockConfig.divideRatio + 1)) *
(CPUCLK_FREQ / gClockSelFreq);
/*
* 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);
/* 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)
{
/* LED will remain high if there is an error */
__BKPT(0);
}
/* 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);
/* 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);
}
}
void MT6701_get_angle_degree(void)
{
MT6701_iic_read_angel();
angle = ((int16_t)gRxPacket[0] << 6) | (gRxPacket[1] >> 2);
angle_f = (float)angle * 360 / 16384;
}

25
3rd/mt6701.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef MT6701_H
#define MT6701_H
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
#define I2C_TX_PACKET_SIZE (1)
/*
* Number of bytes to received from target.
* This example uses FIFO with polling, and the maximum FIFO size is 8.
* Refer to interrupt examples to handle larger packets
*/
#define I2C_RX_PACKET_SIZE (2)
void MT6701_iic_read_angel(void);
void MT6701_get_angle_degree(void);
#ifdef __cplusplus
}
#endif
#endif /* MT6701_H */

53
3rd/uart_redircet.c Normal file
View File

@@ -0,0 +1,53 @@
#include "ti_msp_dl_config.h"
#include <stdio.h>
#include <string.h>
/* ARM Compiler 6 半主机模式禁用声明 */
#if (__ARMCC_VERSION >= 6010050)
__asm(".global __use_no_semihosting\n\t");
__asm(".global __ARM_use_no_argv \n\t");
#else
#pragma import(__use_no_semihosting)
#endif
FILE __stdout;
/* 系统退出函数实现 */
void _sys_exit(int x)
{
x = x;
while (1)
; /* 死循环防止程序异常退出 */
}
/* ARM Compiler 6 printf重定向实现 */
int fputc(int c, FILE *stream)
{
/* 等待UART发送缓冲区就绪 */
while (DL_UART_Main_isBusy(UART_0_INST))
;
/* 发送字符 */
DL_UART_Main_transmitData(UART_0_INST, (uint8_t)c);
return c;
}
int fputs(const char *restrict s, FILE *restrict stream)
{
uint16_t i, len;
len = strlen(s);
for (i = 0; i < len; i++)
{
fputc(s[i], stream);
}
return len;
}
int puts(const char *_ptr)
{
int count = fputs(_ptr, stdout);
count += fputc('\n', stdout); /* 自动添加换行 */
return count;
}

7
3rd/uart_redircet.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef Uart_h
#define Uart_h
#include <stdint.h>
void UART_Console_write(const uint8_t *data, uint16_t size);
#endif /* ti_msp_dl_config_h */