You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
8.9 KiB

2 weeks ago
#include "i2c.h"
#include "delay.h"
#include "uart.h"
#include <string.h>
uint8_t g_i2c_last_command[3] = {0};
uint8_t g_i2c_command_valid = 0;
/**
* : disable
* : I2C (disable)
*
* :
*
* : (void)
*
* :
* 1)
* - dis {0x01, 0x02}
*
* 2) I2C
* - R_IICA0_Master_Send()
* -
* - UART로 "I2C Send Failed"
*
* :
* - SLAVE_ADDR는 I2C
* - sizeof(dis)
* - 100ms
*/
void disable(void){
static uint8_t dis[] = {0x01, 0x02};
//static uint8_t tx[] = {0xT0, 0x00, 0x00};
// I2C 마스터 송신
if (R_IICA0_Master_Send(SLAVE_ADDR << 1, dis, sizeof(dis), 100) != MD_OK)
{
1 week ago
HOST_PRINT("I2C Send Failed\r\n");
2 weeks ago
return;
}
}
/**
* : I2C_EnablePower
* : I2C .
*
* :
*
* : (void)
*
* :
* 1) P7 HIGH
* - _02_Pn1_OUTPUT_1 : P7 1 HIGH
*
* :
* - I2C
* - HIGH
*/
void I2C_EnablePower(void) {
P7 = _02_Pn1_OUTPUT_1;
}
/**
* : I2C_DisablePower
* : I2C .
*
* :
*
* : (void)
*
* :
* 1) P7 LOW
* - _00_Pn1_OUTPUT_0 : P7 1 LOW
*
* :
* - I2C
* - LOW
*/
void I2C_DisablePower(void) {
P7 = _00_Pn1_OUTPUT_0;
}
/**
* : I2C_Diagnostic
* : I2C UART로
*
* :
* - id : I2C
*
* : (void)
*
* :
* 1) CMD_LIST
* - 10
* - 3: { , 1, 2}
*
* 2) CMD_LIST I2C
* - R_IICA0_Master_Send()
* - R_IICA0_Master_Receive()
* - (rx[1], rx[2]) UART (line)
* - CMD
* - / delay로
*
* 3) UART
* - (line) UART로
* - CRLF
*
* :
* - RAM_BYTES는 I2C
* - UART CMD별 2 HEX로
* - I2C
*/
void I2C_Diagnostic(uint8_t id )
{
uint8_t CMD_LIST[10][3] = {
{0x2E, 0x01, 0x00}, // BR
{0x2E, 0x00, 0x00}, // BR_AZ
{0x2E, 0x02, 0x00}, // T_RAW
{0x2E, 0x03, 0x00}, // Y_data
{0x2E, 0x21, 0x00}, // BR_AOUT
{0x2E, 0x40, 0x00},
{0x2E, 0x05, 0x00},
{0x2E, 0x07, 0x00},
{0x2E, 0x19, 0x00},
{0x2E, 0x0B, 0x00}
};
char line[128];
size_t n = 0;
uint8_t rx[RAM_BYTES] = {0};
int j;
// 2) CMD_LIST 순서대로 I2C 읽기
for (j = 0; j < 10; j++) {
if (R_IICA0_Master_Send((id << 1), CMD_LIST[j], 3, 100) != MD_OK) {
1 week ago
HOST_PRINT("I2C Send Failed\r\n");
2 weeks ago
return;
}
delay(10000);
if (R_IICA0_Master_Receive((id << 1), rx, RAM_BYTES, 100) != MD_OK) {
1 week ago
HOST_PRINT("I2C Receive Failed\r\n");
2 weeks ago
return;
}
delay(10000);
n += sprintf(&line[n], "%02X%02X", rx[1], rx[2]);
// 마지막 CMD 뒤에는 쉼표 붙이지 않음
if (j < 9) {
line[n++] = ',';
}
}
// 마지막에 CRLF
line[n++] = '\r';
line[n++] = '\n';
line[n] = '\0';
1 week ago
HOST_PRINT(line);
2 weeks ago
delay(10000);
}
/**
* : I2C_T_Command_Mode_receiveData
* : I2C (tx_data) , UART로
*
* :
* - tx_data : I2C로
* - tx_len : ()
* - id : I2C
*
* : (void)
*
* :
* 1) I2C
* - R_IICA0_Create() I2C
* - I2C (I2C_EnablePower())
* - delay(1)
*
* 2) I2C
* - R_IICA0_Master_Send() tx_data
* - UART로 "I2C Send Failed"
*
* 3) UART
* - "51" UART로
*
* :
* - (read)
* - UART "51" /
*/
void I2C_T_Command_Mode_receiveData(const uint8_t *tx_data, uint8_t tx_len,uint8_t id )
{
char uart_buf[16];
int j;
uint8_t rx[3] = {0};
R_IICA0_Create();
I2C_EnablePower();
delay(1000000);
if (R_IICA0_Master_Send((id << 1), tx_data, tx_len, 100) != MD_OK)
{
1 week ago
HOST_PRINT("I2C Send Failed\r\n");
2 weeks ago
return;
}
1 week ago
HOST_PRINT("51\r\n");
2 weeks ago
}
/**
* : I2C_Command_Mode_receiveData
* : I2C (tx_data) , UART로
*
* :
* - tx_data : I2C로
* - tx_len : ()
* - id : I2C
*
* : (void)
*
* :
* 1) I2C
* - R_IICA0_Master_Send() (id) tx_data
* - UART로 "I2C Send Failed"
*
* 2) UART
* - "51" UART로
* -
*
* :
* -
* - I2C
*/
void I2C_Command_Mode_receiveData(const uint8_t *tx_data, uint8_t tx_len,uint8_t id )
{
char uart_buf[16];
int j;
uint8_t rx[3] = {0};
if (R_IICA0_Master_Send((id << 1), tx_data, tx_len, 100) != MD_OK)
{
1 week ago
HOST_PRINT("I2C Send Failed\r\n");
2 weeks ago
return;
}
1 week ago
HOST_PRINT("51\r\n");
2 weeks ago
}
/**
* : I2C_Command_Mode_Send
* : I2C , UART로
*
* :
* - tx_len : ()
* - id : I2C
*
* : (void)
*
* :
* 1) I2C
* - R_IICA0_Master_Receive() (id) tx_len
* - UART로 "I2C Receive Failed"
*
* 2) UART
* - "%02X "
* - 2 "%02X%02X "
* - 1 "%02X"
* - ("\r\n")
*
* 3) delay
* - delay를 UART
*
* :
* - I2C
* -
*/
void I2C_Command_Mode_Send(uint8_t tx_len, uint8_t id)
{
char uart_buf[16];
char tmp_buf[8];
int i,j;
uint8_t rx[600];
uint8_t va0, va1;
uint8_t status = 0x00;
int tries = 0;
1 week ago
/* 버퍼 초기화 (기존 로직/출력 형식 유지) */
2 weeks ago
memset(uart_buf, 0, sizeof(uart_buf));
memset(tmp_buf, 0, sizeof(tmp_buf));
memset(rx, 0x00, sizeof(rx)); // 가드 패턴(0x00으로 해도 무방)
if (R_IICA0_Master_Receive((id << 1), rx, (uint8_t)(tx_len), 1000) != MD_OK) {
1 week ago
HOST_PRINT("I2C Receive Failed\r\n");
2 weeks ago
return;
}
delay(1000000);
sprintf(uart_buf, "%02X ", rx[0]);
strcpy(tmp_buf, uart_buf);
1 week ago
HOST_PRINT(uart_buf);
2 weeks ago
delay(10000);
for (i = 1; i < (tx_len); i += 2) {
va0 = rx[i];
if (i + 1 < (tx_len)) {
va1 = rx[i + 1];
delay(10000);
sprintf(uart_buf, "%02X%02X ", va0, va1);
1 week ago
HOST_PRINT(uart_buf);
2 weeks ago
delay(10000);
} else {
delay(10000);
sprintf(uart_buf, "%02X", va0);
strcpy(tmp_buf, uart_buf);
1 week ago
HOST_PRINT(uart_buf);
2 weeks ago
delay(10000);
}
}
1 week ago
HOST_PRINT("\r\n");
2 weeks ago
}