/*********************************************************************************************************************** * 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) 2011, 2018 Renesas Electronics Corporation. All rights reserved. ***********************************************************************************************************************/ /*********************************************************************************************************************** * File Name : r_main.c * Version : CodeGenerator for RL78/G14 V2.05.02.05 [30 May 2018] * Device(s) : R5F104PJ * Tool-Chain : CCRL * Description : This file implements main function. * Creation Date: 2019/01/06 ***********************************************************************************************************************/ /*********************************************************************************************************************** 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_timer.h" /* Start user code for include. Do not edit comment generated here */ #include #include #include #include /* 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 */ /* 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 put_message0(uint8_t *); void uart0_dump(unsigned char); void uart0_dump_b(unsigned char); void UART_POSE_KEY(void); void TMR0_wait(unsigned int); void TMR0_M_wait(unsigned int); void Program_main(void); void SPI_CAN_sample(void); void SPI_CAN_io(unsigned char, unsigned char , unsigned char ); void CAN_CS_H(void); void CAN_CS_L(void); void SPI_CHK_SSR03(char); #define PI_NORMAL 0 #define PI_FAILER 1 #define PI_RETURN_H 2 #define PI_RETURN_L 3 #define PI_MODE_EXECL 4 #define PI_MODE_NOEXECL 5 #define PI_TIM_CAN_CS_0 1 #define PI_TIM_CAN_CS_1 1 #define PI_TIM_CAN_CS_W1 1 #define PI_CANSPI_CMD_RESET 0b11000000 #define PI_CANSPI_CMD_READ 0b00000011 #define PI_CANSPI_CMD_WRITE 0b00000010 #define PI_CANSPI_CMD_RDSTAT 0b10100000 #define PI_CANSPI_CMD_RXSTAT 0b10110000 #define PI_CANSPI_CMD_BITMOD 0b00000101 unsigned char G_arv_char ; /* 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 */ while (1U) { Program_main(); } /* 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(); R_SAU0_Create(); R_UART0_Start(); R_CSI11_Create(); CAN_CS_H(); put_message0((uint8_t *)"RL78/G14 (Tx0) Connected OK >"); R_CSI11_Start(); /* End user code. Do not edit comment generated here */ } /* Start user code for adding. Do not edit comment generated here */ /*********************************************************************************************************************** * Function Name: Program_main * Description : This function is the user's main program . * If you do not write the main program here, it will disappear when you generate it . * Arguments : * Return Value : None ***********************************************************************************************************************/ void Program_main(void) { SPI_CAN_sample(); UART_POSE_KEY(); } /*********************************************************************************************************************** * Function Name: SPI_CAN_sample * Description : This function is a sample program that sends and receives data by CAN communication . * Arguments : * Return Value : None ***********************************************************************************************************************/ void SPI_CAN_sample(void) { // **************************************************** // 1.This sample is written to MCP2515 by SPI and read from MCP2515 // **************************************************** TMR0_M_wait(100); SPI_CAN_io((unsigned char)PI_CANSPI_CMD_RESET, (unsigned char)0b00000000, (unsigned char)0b00000000 ); TMR0_M_wait(10); SPI_CAN_io((unsigned char)PI_CANSPI_CMD_WRITE, (unsigned char)0b00000001, (unsigned char)0b00000000 ); TMR0_M_wait(10); SPI_CAN_io((unsigned char)PI_CANSPI_CMD_WRITE, (unsigned char)0b00000010, (unsigned char)0b00001111 ); TMR0_M_wait(10); SPI_CAN_io((unsigned char)PI_CANSPI_CMD_WRITE, (unsigned char)0b00000100, (unsigned char)0b10101010 ); TMR0_M_wait(10); SPI_CAN_io((unsigned char)PI_CANSPI_CMD_WRITE, (unsigned char)0b00001000, (unsigned char)0b11110000 ); TMR0_M_wait(10); SPI_CAN_io((unsigned char)PI_CANSPI_CMD_WRITE, (unsigned char)0b00010000, (unsigned char)0b11111111 ); TMR0_M_wait(10); SPI_CAN_io((unsigned char)PI_CANSPI_CMD_READ, (unsigned char)0b00000001, (unsigned char)0b00000000 ); uart0_dump_b((unsigned char)G_arv_char); G_arv_char = 0x00; TMR0_M_wait(10); SPI_CAN_io((unsigned char)PI_CANSPI_CMD_READ, (unsigned char)0b00000010, (unsigned char)0b00000000 ); uart0_dump_b((unsigned char)G_arv_char); G_arv_char = 0x00; TMR0_M_wait(10); SPI_CAN_io((unsigned char)PI_CANSPI_CMD_READ, (unsigned char)0b00000100, (unsigned char)0b00000000 ); uart0_dump_b((unsigned char)G_arv_char); G_arv_char = 0x00; TMR0_M_wait(10); SPI_CAN_io((unsigned char)PI_CANSPI_CMD_READ, (unsigned char)0b00001000, (unsigned char)0b00000000 ); uart0_dump_b((unsigned char)G_arv_char); G_arv_char = 0x00; TMR0_M_wait(10); SPI_CAN_io((unsigned char)PI_CANSPI_CMD_READ, (unsigned char)0b00010000, (unsigned char)0b00000000 ); uart0_dump_b((unsigned char)G_arv_char); G_arv_char = 0x00; TMR0_M_wait(10); } /*********************************************************************************************************************** * Function Name: SPI_CAN_io * Description : This function read from MCP2515 and writes to MCP2515 with the SPI interface . * Arguments : * Return Value : None * Caution !!! : 1.The SSR03 register must be dummy read !!!!! * Therefore, if you omit the deb_reg variable, the program operation may become unstable !!!!!! * 2.To change the variable returned from the function to the argument of the function, * please carefully read the memory area of this MPU and rewrite the program. * Memory Aria handling is special, so you can not change it easily !!!!! ***********************************************************************************************************************/ void SPI_CAN_io(unsigned char in_inst, unsigned char in_addr, unsigned char in_data ) { uint8_t tx_buf ; uint16_t tx_num=1; uint8_t rx_buf; uint16_t deb_reg; // ****************************** // 1.Output CS sinnal // ****************************** CAN_CS_L(); TMR0_wait((unsigned int)PI_TIM_CAN_CS_0); // ****************************** // 2.Output Instruction // ****************************** tx_buf =(uint8_t)in_inst; deb_reg = SSR03; R_CSI11_Send_Receive(&tx_buf, tx_num, &rx_buf); SPI_CHK_SSR03(PI_MODE_EXECL); // ****************************** // 3.Output 8-Bit Adress // ****************************** tx_buf = (uint8_t)in_addr; deb_reg = SSR03; R_CSI11_Send_Receive(&tx_buf, tx_num, &rx_buf); SPI_CHK_SSR03(PI_MODE_EXECL); // ****************************** // 4.Output 8-Bit data // ****************************** tx_buf = (uint8_t)in_data; deb_reg = SSR03; R_CSI11_Send_Receive(&tx_buf, tx_num, &rx_buf); SPI_CHK_SSR03(PI_MODE_EXECL); deb_reg = SSR03; G_arv_char = (unsigned char)rx_buf; // ****************************** // 5.Output CS signal // ****************************** TMR0_M_wait((unsigned int)PI_TIM_CAN_CS_W1); CAN_CS_H(); TMR0_wait((unsigned int)10); } /*********************************************************************************************************************** * Function Name: UART_POSE_KEY * Description : This function uses the UART pause function . * Arguments : * Return Value : None ***********************************************************************************************************************/ void UART_POSE_KEY(void) { uint8_t rtx_buf = 0x00; while((SSR01 & 0x0040)== 0){ //Check TSF01 (Recive) SSRmn(m:unit number[0,1]/n:channel number[0,1,2,3]) R_UART0_Receive(&rtx_buf,(uint16_t)1); } } /*********************************************************************************************************************** * Function Name: SPI_CHK_SSR03 * Description : * Arguments : * Return Value : * important subjects : * 1.Dummy read of SDR03 register can not be omitted !!!!!!!! * This dummy read clears the data accumulated in the SDR03 register and prevents overrun !!!!!! * It affects OVF02 of SSR03 register !!!!!! ***********************************************************************************************************************/ void SPI_CHK_SSR03(char in_mode) { uint16_t ssr03_data; uint16_t sdr03_data; // Do not omit this dummy variable !!!!!!! if(in_mode == PI_MODE_NOEXECL ){ sdr03_data = SDR03; return; } while(1){ if(((unsigned char)SSR03 & (unsigned char)0b01000000) == 0 ){ sdr03_data = SDR03; // Do not skip this dummy read !!!!!!!! return; } } } /*********************************************************************************************************************** * Function Name: TMR0_M_wait * Description : This function waits for the specified time using the TIMER0 function . * Arguments : Input in_loop ( n *[uS]) * Return Value : None ***********************************************************************************************************************/ void TMR0_M_wait(unsigned int in_lp) { for ( ; in_lp > 0 ; in_lp-- ) { TMR0_wait((unsigned int)1000); } } /*********************************************************************************************************************** * Function Name: TMR0_wait * Description : This function waits for the specified time using the TIMER0 function . * Arguments : Input in_loop ( n *[uS]) * Return Value : None ***********************************************************************************************************************/ void TMR0_wait(unsigned int in_lp) { R_TAU0_Channel0_Start(); for ( ; in_lp > 0 ; in_lp-- ) { while ( TMIF00 == 0 ) { NOP(); } TMIF00 = 0; } R_TAU0_Channel0_Stop(); } /*********************************************************************************************************************** * Function Name: uart0_dump * Description : This function converts the character in hex code and outputs it to UART0 * Arguments : Input char * Return Value : None ***********************************************************************************************************************/ void uart0_dump(unsigned char in_c) { int in_i; char out_line[10]; in_i = (int)in_c; // sprintf(out_line,"[%x(%c)]",in_i,in_i); sprintf(out_line,"[%2.2x]",in_i); put_message0((uint8_t *)out_line); } /*********************************************************************************************************************** * Function Name: uart0_dump_b * Description : This function converts the character in binary code and outputs it to UART0 * Arguments : Input char * Return Value : None ***********************************************************************************************************************/ void uart0_dump_b(unsigned char in_c) { unsigned char i; unsigned char b[9]; char out_line[2]; unsigned char lp; for (i = 0; in_c > 0; i++) { b[i] = in_c % 2; in_c = in_c / 2; } put_message0((uint8_t *)"("); for (lp = 0; lp < ( 8 - i ) ; lp++) { put_message0((uint8_t *)"0"); } while (i > 0){ sprintf(out_line,"%1.1d", b[--i]); put_message0((uint8_t *)out_line); } put_message0((uint8_t *)")"); } /*********************************************************************************************************************** * Function Name: put_message0 * Description : This function is a sample program that outputs a message to UART0. * Arguments : None * Return Value : None ***********************************************************************************************************************/ void put_message0(uint8_t *message) { while(*message != 0x00){ while((SSR00 & 0x0040)!= 0); //Check TSF02 (Send) SSRmn(m:unit number[0,1]/n:channel number[0,1,2,3]) R_UART0_Send((uint8_t *)message,(uint16_t)1); message++; } } /*********************************************************************************************************************** * Function Name: CAN_CS_H * Description : This function set CS signal (P4[1]) connected to SPI_CAN to H * Arguments : * Return Value : None ***********************************************************************************************************************/ void CAN_CS_H(void) { P4 |= 0b00000010; } /*********************************************************************************************************************** * Function Name: CAN_CS_L * Description : This function set CS signal (P4[1]) connected to SPI_CAN to L * Arguments : * Return Value : None ***********************************************************************************************************************/ void CAN_CS_L(void) { P4 &= 0b11111101; } /* End user code. Do not edit comment generated here */