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.
 
 

441 lines
16 KiB

/***********************************************************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products.
* No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
* applicable laws, including copyright laws.
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY
* LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT,
* INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR
* ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability
* of this software. By using this software, you agree to the additional terms and conditions found by accessing the
* following link:
* http://www.renesas.com/disclaimer
*
* Copyright (C) 2012, 2021 Renesas Electronics Corporation. All rights reserved.
***********************************************************************************************************************/
/***********************************************************************************************************************
* File Name : r_main.c
* Version : CodeGenerator for RL78/F14 V2.03.07.02 [08 Nov 2021]
* Device(s) : R5F10PPJ
* Tool-Chain : CCRL
* Description : This file implements main function.
* Creation Date: 2026-01-12
***********************************************************************************************************************/
/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include "r_cg_macrodriver.h"
#include "r_cg_cgc.h"
#include "r_cg_serial.h"
#include "r_cg_wdt.h"
/* Start user code for include. Do not edit comment generated here */
#include "common.h"
#include "r_cg_macrodriver.h"
#include <string.h>
#include <ctype.h>
#include <stddef.h>
#include <r_cg_port.h>
#define UART_RX_BUF_SIZE 96
#define CMD_MAX 529
// pin10~13 �� P1.0~P1.3 ����
#define X0C_PORT P1
#define X0C_PORTMODE PM1
#define X0C_CH0_MASK _01_Pn0_OUTPUT_1 // P1.0 = 1
#define X0C_CH1_MASK _02_Pn1_OUTPUT_1 // P1.1 = 1
#define X0C_CH2_MASK _04_Pn2_OUTPUT_1 // P1.2 = 1
#define X0C_CH3_MASK _08_Pn3_OUTPUT_1 // P1.3 = 1
#define X0C_MASK (X0C_CH0_MASK|X0C_CH1_MASK|X0C_CH2_MASK|X0C_CH3_MASK)
extern volatile uint8_t uart_rx_done;
extern volatile uint8_t uart_rx_index;
extern volatile uint8_t uart_rx_buffer[UART_RX_BUF_SIZE];
extern volatile uint16_t uart_rx_length;
static uint8_t uart0_rx_buffer[11];
const unsigned char *d;
unsigned int len;
/* End user code. Do not edit comment generated here */
#include "r_cg_userdefine.h"
/***********************************************************************************************************************
Pragma directive
***********************************************************************************************************************/
/* Start user code for pragma. Do not edit comment generated here */
static unsigned char hex2byte(char h, char l)
{
unsigned char hi, lo;
if (h >= 'a' && h <= 'f') h -= 32;
if (l >= 'a' && l <= 'f') l -= 32;
hi = (h >= 'A') ? h - 'A' + 10 : h - '0';
lo = (l >= 'A') ? l - 'A' + 10 : l - '0';
return (hi << 4) | lo;
}
// === X0C GPIO: P1.0~P1.3 �� pin10~13���� �����Ѵٰ� ���� ===
static void x0c_init(void){
// �������� ����
X0C_PORTMODE &= (uint8_t)~(uint8_t)X0C_MASK;
// ���� LOW
X0C_PORT &= (uint8_t)~(uint8_t)X0C_MASK;
}
static void x0c_all_low(void){
X0C_PORT &= (uint8_t)~(uint8_t)X0C_MASK;
}
static void x0c_select(uint8_t ch){
// break-before-make
X0C_PORT &= (uint8_t)~(uint8_t)X0C_MASK;
__nop(); __nop(); __nop(); __nop();
switch(ch){
case 0: X0C_PORT = (uint8_t)((X0C_PORT & (uint8_t)~(uint8_t)X0C_MASK) | X0C_CH0_MASK); break;
case 1: X0C_PORT = (uint8_t)((X0C_PORT & (uint8_t)~(uint8_t)X0C_MASK) | X0C_CH1_MASK); break;
case 2: X0C_PORT = (uint8_t)((X0C_PORT & (uint8_t)~(uint8_t)X0C_MASK) | X0C_CH2_MASK); break;
case 3: X0C_PORT = (uint8_t)((X0C_PORT & (uint8_t)~(uint8_t)X0C_MASK) | X0C_CH3_MASK); break;
default: /* 255=all low �� */ break;
}
}
typedef void (*CmdHandler)(const unsigned char *data, unsigned int len);
void cmd_unknown(const unsigned char *d, unsigned int len)
{
uart_send_string("Unknownaaaaa cmd\r\n");
delay(100000);
}
typedef enum {
PROTOCOL_I2CT,
PROTOCOL_OWIT,
PROTOCOL_I2CW,
PROTOCOL_I2CR,
PROTOCOL_OWIW,
PROTOCOL_OWIR,
PROTOCOL_I2CA,
PROTOCOL_OWIA,
PROTOCOL_OWID,
PROTOCOL_OWIC,
PROTOCOL_I2CC,
PROTOCOL_UNKNOWN
} ProtocolType;
ProtocolType detect_protocol(char header1,char header2)
{
if (header1 == 'I' && header2 == 'W' ) return PROTOCOL_I2CW;
if (header1 == 'I' && header2 == 'R') return PROTOCOL_I2CR;
if (header1 == 'O' && header2 == 'W' ) return PROTOCOL_OWIW;
if (header1 == 'O' && header2 == 'R' ) return PROTOCOL_OWIR;
return PROTOCOL_UNKNOWN;
}
typedef struct {
unsigned char code;
CmdHandler fn;
} CmdMap;
void process_cmd(ProtocolType protocol, uint8_t id/*unsigned char code*/,
const unsigned char *data, unsigned int len)
{
const CmdMap *map = NULL;
int map_size = 0;
int i;
switch (protocol) {
case PROTOCOL_I2CT:
I2C_T_Command_Mode_receiveData(data,(uint8_t)len, id);
break;
case PROTOCOL_OWIT:
OWI_T_CommandMode(data,(uint8_t)len, id);
break;
case PROTOCOL_I2CW:
I2C_Command_Mode_receiveData(data,(uint8_t)len,id);
break;
case PROTOCOL_I2CR:
I2C_Command_Mode_Send((uint8_t)len, id);
break;
case PROTOCOL_OWIW:
OWI_CommandMode(data,(uint8_t)len,id);
break;
case PROTOCOL_OWIR:
OWI_ReadBytesAndPrint(len,id);
break;
case PROTOCOL_I2CA:
I2C_A_Command_Mode_receiveData(data,(uint8_t)len,id);
break;
case PROTOCOL_OWIA:
OWI_A_CommandMode(data,(uint8_t)len,id);
break;
case PROTOCOL_OWID:
OWI_disable();
break;
case PROTOCOL_OWIC:
OWI_Diagnostic(id);
break;
case PROTOCOL_I2CC:
I2C_Diagnostic(id);
break;
default:
cmd_unknown(data, len);
return;
}
}
/* End user code. Do not edit comment generated here */
/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
/* Start user code for global. Do not edit comment generated here */
void handle_uart_command_line(void)
{
char line[UART_RX_BUF_SIZE];
while (1)
{
if (uart_rx_done)
{
int i;
int idx = 0;
int pos = 2;
int rx_len;
ProtocolType proto;
uint8_t id;
unsigned int byte_len;
uint8_t cmd[CMD_MAX];
unsigned int k = 0;
uart_rx_done = 0;
rx_len = (int)uart_rx_length;
if (rx_len < 0) rx_len = 0;
if (rx_len > (int)UART_RX_BUF_SIZE - 1) rx_len = (int)UART_RX_BUF_SIZE - 1;
uart_rx_buffer[rx_len] = '\0';
// �� �� ������ (CR ����, LF���� ����)
for (i = 0; i < rx_len && i < (int)sizeof(line) - 1; i++) {
char c = (char)uart_rx_buffer[i];
if (c == '\r') continue;
if (c == '\n') break;
line[idx++] = c;
}
line[idx] = '\0';
/* ============================
* X0C_ �����Ƚ� ��ó��
* ����: X0C_011:..., X0C_021:..., X0C_031:..., X0C_041:...
* (�ɼ�) X0C_000: -> ���� LOW
* ============================ */
if (idx >= 8 &&
(line[0]=='X' || line[0]=='x') &&
(line[1]=='0' || line[1]=='0') &&
(line[2]=='0' || line[1]=='0') &&
(line[3]=='C' || line[2]=='c') &&
line[4]=='_')
{
int ch = -1;
// ������ ���� �˻�: 'X0C_' '0' [1~4|0] '1' ':' (�Ǵ� 000:)
if (line[5]=='0' && line[8]==':') {
if (line[5]=='0' && line[6]=='0' && line[7]=='0') ch = 0;
else if (line[5]=='0' && line[6]=='0' && line[7]=='1') ch = 1;
else if (line[5]=='2' && line[6]=='1') ch = 1;
else if (line[5]=='3' && line[6]=='1') ch = 2; // pin12
else if (line[5]=='4' && line[6]=='1') ch = 3; // pin13
else if (line[5]=='0' && line[6]=='0') ch = 255; // 000: -> ���� LOW
}
if (ch == -1) {
uart_send_string("Err:X0C_code\r\n");
goto clear;
}
// ���� LOW
X0C_PORT &= (uint8_t)~(uint8_t)X0C_MASK;
__nop(); __nop(); __nop(); __nop();
// ���õ� ä�θ� HIGH
if (ch != 255) {
switch(ch){
case 0: X0C_PORT = (uint8_t)((X0C_PORT & (uint8_t)~(uint8_t)X0C_MASK) | X0C_CH0_MASK); break;
case 1: X0C_PORT = (uint8_t)((X0C_PORT & (uint8_t)~(uint8_t)X0C_MASK) | X0C_CH1_MASK); break;
case 2: X0C_PORT = (uint8_t)((X0C_PORT & (uint8_t)~(uint8_t)X0C_MASK) | X0C_CH2_MASK); break;
case 3: X0C_PORT = (uint8_t)((X0C_PORT & (uint8_t)~(uint8_t)X0C_MASK) | X0C_CH3_MASK); break;
}
delay_us(7000);
}
// �ݷ� �� ���ɸ� ���⵵�� ���۸� ������ ����
{
int start = 8; // "X0C_0x1:" ���� ���� ���
int rem = idx - start; // ���� ����
if (rem <= 0) {
// X0C_�� �ִ� ����: �ܼ� ACK �� ����
uart_send_string("<ACK>X0C\r\n");
goto clear;
}
for (i = 0; i < rem; i++) line[i] = line[start + i];
line[rem] = '\0';
idx = rem;
pos = 2; // �������� ���ؼ� ���� �ʱ�ȭ (OW/IR/IW/OR�� 2���� ���� ��ġ)
}
}
// (X0C ó�� ��) �ּ� ���� ���˻�
if (idx < 7) { uart_send_string("Err:short\r\n"); goto clear; }
{
char h0 = (char)toupper((unsigned char)line[0]);
char h1 = (char)toupper((unsigned char)line[1]);
proto = detect_protocol(h0, h1);
}
if (proto == PROTOCOL_UNKNOWN) { uart_send_string("Err:ID\r\n"); goto clear; }
if (line[pos] == 't' || line[pos] == 'T') {
if (proto == PROTOCOL_OWIW) proto = PROTOCOL_OWIT;
else if (proto == PROTOCOL_I2CW) proto = PROTOCOL_I2CT;
pos++; // 't'
}
if (line[pos] == 'a' || line[pos] == 'A') {
if (proto == PROTOCOL_OWIW) proto = PROTOCOL_OWIA;
else if (proto == PROTOCOL_I2CW) proto = PROTOCOL_I2CA;
pos++; // 'a'
}
if (line[pos] == 'd' || line[pos] == 'D') {
if (proto == PROTOCOL_OWIW) proto = PROTOCOL_OWID;
pos++;
}
if (line[pos] == 'c' || line[pos] == 'C') {
if (proto == PROTOCOL_OWIW) proto = PROTOCOL_OWIC;
else if (proto == PROTOCOL_I2CW) proto = PROTOCOL_I2CC;
pos++;
}
if (line[pos] == '_' || line[pos] == ':') pos++;
if (pos + 1 >= idx) { uart_send_string("Err:id_short\r\n"); goto clear; }
id = hex2byte(line[pos], line[pos+1]);
pos += 2;
if (pos + 2 >= idx ||
!(line[pos] >= '0' && line[pos] <= '9') ||
!(line[pos+1] >= '0' && line[pos+1] <= '9') ||
!(line[pos+2] >= '0' && line[pos+2] <= '9')) {
uart_send_string("Err:len_dec\r\n"); goto clear;
}
byte_len = (unsigned int)(100*(line[pos]-'0') + 10*(line[pos+1]-'0') + (line[pos+2]-'0'));
pos += 3;
if (byte_len > CMD_MAX) { uart_send_string("Err:len_range\r\n"); goto clear; }
if (proto == PROTOCOL_I2CA || proto == PROTOCOL_OWIA) {
if (byte_len != 0 && byte_len != 3) {
uart_send_string("Err:len_a_mode\r\n"); goto clear;
}
if (byte_len == 0) {
if (pos != idx) { uart_send_string("Err:read_no_payload\r\n"); goto clear; }
} else {
if ((int)(pos + (int)byte_len*2) > idx) { uart_send_string("Err:len_mismatch\r\n"); goto clear; }
for (k = 0; k < byte_len; k++) {
cmd[k] = hex2byte(line[pos + 2*k], line[pos + 2*k + 1]);
}
pos += byte_len*2;
if (pos != idx) { uart_send_string("Err:len_trail\r\n"); goto clear; }
}
}
else if (proto == PROTOCOL_OWIT || proto == PROTOCOL_I2CT ||
proto == PROTOCOL_OWIW || proto == PROTOCOL_I2CW)
{
if (byte_len == 0) { uart_send_string("Err:payload0\r\n"); goto clear; }
if ((int)(pos + (int)byte_len*2) > idx) { uart_send_string("Err:len_mismatch\r\n"); goto clear; }
for (k = 0; k < byte_len; k++) {
cmd[k] = hex2byte(line[pos + 2*k], line[pos + 2*k + 1]);
}
pos += byte_len*2;
if (pos != idx) { uart_send_string("Err:len_trail\r\n"); goto clear; }
}
else if (proto == PROTOCOL_OWIR || proto == PROTOCOL_I2CR)
{
if (byte_len == 0) { uart_send_string("Err:read_len_nonzero\r\n"); goto clear; }
if (pos != idx) { uart_send_string("Err:read_no_payload\r\n"); goto clear; }
}
process_cmd(proto, id, cmd, byte_len);
clear:
idx = 0;
uart_rx_index = 0;
uart_rx_length = 0;
R_UART0_Receive((uint8_t *)&uart_rx_buffer[uart_rx_index], 1);
}
}
}
/* End user code. Do not edit comment generated here */
void R_MAIN_UserInit(void);
/***********************************************************************************************************************
* Function Name: main
* Description : This function implements main function.
* Arguments : None
* Return Value : None
***********************************************************************************************************************/
void main(void)
{
R_MAIN_UserInit();
/* Start user code. Do not edit comment generated here */
R_UART0_Create();
R_IICA0_Create();
R_UART0_Start();
R_UART0_Receive(&uart_rx_buffer[uart_rx_index], 1);
handle_uart_command_line();
delay(100000);
while (1U)
{
;
}
/* End user code. Do not edit comment generated here */
}
/***********************************************************************************************************************
* Function Name: R_MAIN_UserInit
* Description : This function adds user code before implementing main function.
* Arguments : None
* Return Value : None
***********************************************************************************************************************/
void R_MAIN_UserInit(void)
{
/* Start user code. Do not edit comment generated here */
EI();
R_ADC_Create();
R_ADC_Set_OperationOn();
x0c_init();
/* End user code. Do not edit comment generated here */
}
/* Start user code for adding. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */