我为RA6M5编写程序,在程序中使用了sin(x)函数,但是无论我怎么努力都无法使用
情况一、
使用sin函数得到'sin' has unknown return type; cast the call to its declared return type的错误。但是程序可以顺利编译和执行。
相关代码如下:
/*
* Copyright (c) 2020 - 2024 Renesas Electronics Corporation and/or its affiliates
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "common_data.h"
#include "fsp_common_api.h"
#include "hal_data.h"
#include "r_icu.h"
#include "app_print.h"
#include <stdbool.h>
#include <math.h>
#include <stdint.h>
#define APP_ERR_TRAP() __asm("BKPT #0\n")
#define PI 3.1415926
void R_BSP_WarmStart(bsp_warm_start_event_t event);
extern bsp_leds_t g_bsp_leds;
volatile bool g_sw1_press = false;
volatile bool g_sw2_press = false;
volatile bool g_dac_sw = false;
/*******************************************************************************************************************//**
* [url=home.php?mod=space&uid=159083]@brief[/url] Blinky example application
*
* Blinks all leds at a rate of 1 second using the software delay function provided by the BSP.
*
**********************************************************************************************************************/
void hal_entry (void)
{
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
fsp_err_t err = FSP_SUCCESS;
double cycle = 0;
double dac_value = 0;
uint16_t freq = 0;
uint16_t output = 0;
/* Define the units to be used with the software delay function */
const bsp_delay_units_t bsp_delay_units = BSP_DELAY_UNITS_MILLISECONDS;
const double cyclePien = (2 * PI)/1000;
/* Set the blink frequency (must be <= bsp_delay_units */
const uint32_t freq_in_hz = 2;
/* Calculate the delay in terms of bsp_delay_units */
const uint32_t delay = bsp_delay_units / freq_in_hz;
/* LED type structure */
bsp_leds_t leds = g_bsp_leds;
/* If this board has no LEDs then trap here */
if (0 == leds.led_count)
{
while (1)
{
; // There are no LEDs on this board
}
}
/* Holds level to set for pins */
bsp_io_level_t pin_level = BSP_IO_LEVEL_LOW;
err = R_SCI_UART_Open(&g_uart7_ctrl, &g_uart7_cfg);
if(FSP_SUCCESS != err)
{
APP_ERR_TRAP();
}
/* Initialize External IRQ driver*/
err = R_ICU_ExternalIrqOpen(&g_external_irq9_ctrl, &g_external_irq9_cfg);
if(FSP_SUCCESS != err)
{
APP_ERR_TRAP();
}
err = R_ICU_ExternalIrqEnable(&g_external_irq9_ctrl);
if(FSP_SUCCESS != err)
{
APP_ERR_TRAP();
}
/* Initialize External IRQ driver*/
err = R_ICU_ExternalIrqOpen(&g_external_irq10_ctrl, &g_external_irq10_cfg);
if(FSP_SUCCESS != err)
{
APP_ERR_TRAP();
}
err = R_ICU_ExternalIrqEnable(&g_external_irq10_ctrl);
if(FSP_SUCCESS != err)
{
APP_ERR_TRAP();
}
err = R_DAC_Open (&g_dac0_ctrl, &g_dac0_cfg);
if(FSP_SUCCESS != err)
{
APP_ERR_TRAP();
}
while (1)
{
if(g_sw1_press)
{
freq = (uint16_t) ( freq++ )%10;
g_sw1_press = false;
APP_ERR_PRINT("Freg Add\r\n");
}
if(g_sw2_press)
{
g_dac_sw = !g_dac_sw;
if(g_dac_sw)
{
APP_ERR_PRINT("OUT ON\r\n");
}
else
{
APP_ERR_PRINT("OUT OFF\r\n");
}
g_sw2_press = false;
}
cycle = cycle + cyclePien/(freq+1);
if (cycle >= (2 * PI))
{
cycle = 0;
}
dac_value = (sin(cycle) + 1) * 2047;
if(!g_dac_sw)
{
dac_value = 0;
}
output = (uint16_t) dac_value;
err = R_DAC_Write (&g_dac0_ctrl, output);
err = R_DAC_Start (&g_dac0_ctrl); R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MICROSECONDS);
/* Delay */
R_BSP_SoftwareDelay(delay, bsp_delay_units);
}
}
但是,cyclePien的数值是正确的。(下面会说)
程序无法正确执行。
情况二、
使用keil进行编译,则非常离谱,cyclePien的变量居然是错误的,而且无论怎么定义,都不可以。cycle始终为0,cycle==0;
相关代码如下:
/*
* Copyright (c) 2020 - 2024 Renesas Electronics Corporation and/or its affiliates
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "hal_data.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdarg.h>
#include <string.h>
#define APP_ERR_TRAP() __asm("BKPT #0\n")
#define PI 3.1415926
void R_BSP_WarmStart(bsp_warm_start_event_t event);
extern bsp_leds_t g_bsp_leds;
volatile bool g_sw1_press = false;
volatile bool g_sw2_press = false;
volatile bool g_dac_sw = false;
/* Macro definition */
#define CARRIAGE_ASCII (13u) /* Carriage return */
#define ZERO_ASCII (48u) /* ASCII value of zero */
#define NINE_ASCII (57u) /* ASCII value for nine */
#define DATA_LENGTH (4u) /* Expected Input Data length */
#define UART_ERROR_EVENTS (UART_EVENT_BREAK_DETECT | UART_EVENT_ERR_OVERFLOW | UART_EVENT_ERR_FRAMING | \
UART_EVENT_ERR_PARITY) /* UART Error event bits mapped in registers */
#define RESET_VALUE (0x00)
/* Flag for user callback */
static volatile uint8_t g_uart_event = RESET_VALUE;
static uint8_t g_temp_buffer[DATA_LENGTH] = {RESET_VALUE};
/* Counter to update g_temp_buffer index */
static volatile uint8_t g_counter_var = RESET_VALUE;
/* Flag to check whether data is received or not */
static volatile uint8_t g_data_received_flag = false;
int APP_PRINT( const char * sFormat, ... );
int v_uart( const char * sFormat, va_list * pParamList);
/*******************************************************************************************************************//**
* @brief Blinky example application
*
* Blinks all leds at a rate of 1 second using the software delay function provided by the BSP.
*
**********************************************************************************************************************/
void hal_entry (void)
{
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
fsp_err_t err = FSP_SUCCESS;
float cycle = 0.0;
float dac_value = 0.0;
uint16_t freq = 0;
uint16_t output = 0;
const float cyclePien = (2 * PI)/1000;
//const float cyclePien = 0.0062831852f;
/* Define the units to be used with the software delay function */
const bsp_delay_units_t bsp_delay_units = BSP_DELAY_UNITS_MILLISECONDS;
/* Set the blink frequency (must be <= bsp_delay_units */
const uint32_t freq_in_hz = 2;
/* Calculate the delay in terms of bsp_delay_units */
const uint32_t delay = bsp_delay_units / freq_in_hz;
/* LED type structure */
bsp_leds_t leds = g_bsp_leds;
/* If this board has no LEDs then trap here */
if (0 == leds.led_count)
{
while (1)
{
; // There are no LEDs on this board
}
}
/* Holds level to set for pins */
bsp_io_level_t pin_level = BSP_IO_LEVEL_LOW;
err = R_SCI_UART_Open(&g_uart7_ctrl, &g_uart7_cfg);
if(FSP_SUCCESS != err)
{
APP_ERR_TRAP();
}
/* Initialize External IRQ driver*/
err = R_ICU_ExternalIrqOpen(&g_external_irq9_ctrl, &g_external_irq9_cfg);
if(FSP_SUCCESS != err)
{
APP_ERR_TRAP();
}
err = R_ICU_ExternalIrqEnable(&g_external_irq9_ctrl);
if(FSP_SUCCESS != err)
{
APP_ERR_TRAP();
}
/* Initialize External IRQ driver*/
err = R_ICU_ExternalIrqOpen(&g_external_irq10_ctrl, &g_external_irq10_cfg);
if(FSP_SUCCESS != err)
{
APP_ERR_TRAP();
}
err = R_ICU_ExternalIrqEnable(&g_external_irq10_ctrl);
if(FSP_SUCCESS != err)
{
APP_ERR_TRAP();
}
err = R_DAC_Open (&g_dac0_ctrl, &g_dac0_cfg);
if(FSP_SUCCESS != err)
{
APP_ERR_TRAP();
}
while (1)
{
/* Enable access to the PFS registers. If using r_ioport module then register protection is automatically
* handled. This code uses BSP IO functions to show how it is used.
*/
// R_BSP_PinAccessEnable();
// /* Update all board LEDs */
// for (uint32_t i = 0; i < leds.led_count; i++)
// {
// /* Get pin to toggle */
// uint32_t pin = leds.p_leds[i];
// /* Write to this pin */
// R_BSP_PinWrite((bsp_io_port_pin_t) pin, pin_level);
// }
// /* Protect PFS registers */
// R_BSP_PinAccessDisable();
// /* Toggle level for next write */
// if (BSP_IO_LEVEL_LOW == pin_level)
// {
// pin_level = BSP_IO_LEVEL_HIGH;
// }
// else
// {
// pin_level = BSP_IO_LEVEL_LOW;
// }
if(g_sw1_press)
{
freq = (uint16_t)(( freq++ )%10);
g_sw1_press = false;
APP_PRINT("Freg Add\r\n");
}
if(g_sw2_press)
{
g_dac_sw = !g_dac_sw;
if(g_dac_sw)
{
APP_PRINT("OUT ON\r\n");
}
else
{
APP_PRINT("OUT OFF\r\n");
}
g_sw2_press = false;
}
cycle += cyclePien/(freq+1);
if (cycle >= (2 * PI))
{
cycle = 0;
}
dac_value = (sin(cycle) + 1) * 2047;
if(!g_dac_sw)
{
dac_value = 0;
}
output = (uint16_t) dac_value;
err = R_DAC_Write (&g_dac0_ctrl, output);
err = R_DAC_Start (&g_dac0_ctrl); R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MICROSECONDS);
/* Delay */
R_BSP_SoftwareDelay(1, bsp_delay_units);
}
}
而我即时修改了,相关的配置也不可以。无论怎么修改都不正确!
太过邪门,请大家解惑!!!
|