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.

368 lines
14 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-13
***********************************************************************************************************************/
/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include "r_cg_macrodriver.h"
#include "r_cg_cgc.h"
#include "r_cg_port.h"
#include "r_cg_serial.h"
#include "r_cg_adc.h"
#include "r_cg_wdt.h"
/* Start user code for include. Do not edit comment generated here */
#include "common.h"
#include "anaout.h"
#include "dipSwitch.h"
#include <string.h>
#include <ctype.h>
#include <stddef.h>
#include <r_cg_port.h>
#define CMD_MAX 529
volatile uint8_t uart_rx_done = 0;
volatile uint8_t uart_rx_index = 0;
volatile uint8_t uart_rx_buffer[UART_RX_BUF_SIZE] = {0};
volatile uint16_t uart_rx_length = 0;
// (1~32)
static uint8_t g_fixed_addr = 1;
/* 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') ? (unsigned char)(h - 'A' + 10) : (unsigned char)(h - '0');
lo = (l >= 'A') ? (unsigned char)(l - 'A' + 10) : (unsigned char)(l - '0');
return (unsigned char)((hi << 4) | lo);
}
// Prefix: "X<AA>C_<CCC>:"
static int parse_x_prefix(const char *s, int len, uint8_t *addr, uint8_t *ch, int *payload_pos)
{
if (len < 9) return 0;
if (toupper((unsigned char)s[0]) != 'X') return 0;
if (!isdigit((unsigned char)s[1]) || !isdigit((unsigned char)s[2])) return -1;
if (toupper((unsigned char)s[3]) != 'C') return -1;
if (s[4] != '_') return -1;
if (!isdigit((unsigned char)s[5]) || !isdigit((unsigned char)s[6]) || !isdigit((unsigned char)s[7])) return -1;
if (s[8] != ':') return -1;
*addr = (uint8_t)((s[1] - '0') * 10 + (s[2] - '0'));
*ch = (uint8_t)((s[5] - '0') * 100 + (s[6] - '0') * 10 + (s[7] - '0'));
*payload_pos = 9;
return 1;
}
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;
static 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;
}
static void cmd_unknown(const unsigned char *d, unsigned int len)
{
uart1_send_string("Unknown cmd\r\n");
delay(100000);
}
static void process_cmd(ProtocolType protocol, uint8_t id,
const unsigned char *data, unsigned int len)
{
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); break;
}
}
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';
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';
// Prefix
{
uint8_t addr = 0;
uint8_t ch = 0;
int payload_pos = 0;
int r = parse_x_prefix(line, idx, &addr, &ch, &payload_pos);
if (r == -1) {
uart1_send_string("Err:X_prefix\r\n");
goto clear;
}
if (r == 1) {
if (addr < 1 || addr > 32) { uart1_send_string("Err:addr_range\r\n"); goto clear; }
if (ch < 1 || ch > 20) { uart1_send_string("Err:ch_range\r\n"); goto clear; }
if (addr != g_fixed_addr) {
uart1_send_string("Err:addr_mismatch\r\n");
goto clear;
}
ANAOUT_Select(ch);
{
int rem = idx - payload_pos;
if (rem <= 0) {
uart1_send_string("<ACK>XC\r\n");
goto clear;
}
for (i = 0; i < rem; i++) line[i] = line[payload_pos + i];
line[rem] = '\0';
idx = rem;
pos = 2;
}
}
}
if (idx < 7) { uart1_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) { uart1_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++;
}
if (line[pos] == 'a' || line[pos] == 'A') {
if (proto == PROTOCOL_OWIW) proto = PROTOCOL_OWIA;
else if (proto == PROTOCOL_I2CW) proto = PROTOCOL_I2CA;
pos++;
}
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) { uart1_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')) {
uart1_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) { uart1_send_string("Err:len_range\r\n"); goto clear; }
if (proto == PROTOCOL_I2CA || proto == PROTOCOL_OWIA) {
if (byte_len != 0 && byte_len != 3) {
uart1_send_string("Err:len_a_mode\r\n"); goto clear;
}
if (byte_len == 0) {
if (pos != idx) { uart1_send_string("Err:read_no_payload\r\n"); goto clear; }
} else {
if ((int)(pos + (int)byte_len*2) > idx) { uart1_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) { uart1_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) { uart1_send_string("Err:payload0\r\n"); goto clear; }
if ((int)(pos + (int)byte_len*2) > idx) { uart1_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) { uart1_send_string("Err:len_trail\r\n"); goto clear; }
}
else if (proto == PROTOCOL_OWIR || proto == PROTOCOL_I2CR)
{
if (byte_len == 0) { uart1_send_string("Err:read_len_nonzero\r\n"); goto clear; }
if (pos != idx) { uart1_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_UART1_Receive((uint8_t *)&uart_rx_buffer[uart_rx_index], 1);
}
}
}
/* End user code. Do not edit comment generated here */
/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
/* Start user code for global. Do not edit comment generated here */
/* 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(); // UART0
R_UART1_Create(); // UART1
R_IICA0_Create(); // I2C
R_UART0_Start(); // RS485
R_UART1_Start(); // PC
R_UART1_Receive((uint8_t *)&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_PORT_Create();
rs485_init();
R_ADC_Create();
R_ADC_Set_OperationOn();
DipSwitch_Init();
g_fixed_addr = DipSwitch_ReadAddr_1to32();
ANAOUT_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 */