I have written some simple code (with the help of STM32CubeMx) for STM32F407VGTx to create a file in SDcard. Although it works perfectly with 407, it is not working with STM32F401ReTx. To be clear, I have generated code with CubeMx specifically for 401(did not use the code for 407).
Even for 407, I am able to work with only Version 21 of CubeMx and firmware version 16. I have added the code below. Any ideas?
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_SDIO_SD_Init();
MX_FATFS_Init();
/* USER CODE BEGIN 2 */
if(f_mount(&myFatFS,SD_Path,1)==FR_OK)
{
HAL_Delay(1000);
f_open(&myFil,"new document.txt\0",FA_CREATE_ALWAYS | FA_WRITE);
f_close(&myFil);
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Problem is probably heap size f407 has more initial heap size than f401. Try to increase heap size in linker file.
Related
I want to receive UART interrupts, but shouldn't the basic structure of the interrupts be made like that?
Sometimes it doesn't work and sometimes it prints out printf ("test\r\n") but sometimes it doesn't print out the received data, but I don't know what the cause is Can you tell me the solution?
I tried to pull the HAL_UART_Receive_IT door out while, but it didn't work
I don't think it's a connection problem since I'm sending a message
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
When I entered this code, there was an error
Error[Li006]: duplicate definitions for "USART1_IRQHandler";
The code below is the code that is currently experiencing a problem
uint8_t received_msg[10];
char check_msg[10];
//UART read
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
if(huart -> Instance == USART1){
printf("test\r\n");
HAL_UART_Receive_IT(&huart1, (uint8_t*)received_msg, sizeof(received_msg));
}
}
/* USER CODE END 0 */
/**
* #brief The application entry point.
* #retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_I2C1_Init();
MX_TIM6_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim6);
calibrate(0.01, 4);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1) {
rcvStat = HAL_UART_Receive_IT(&huart1, received_msg, sizeof(received_msg));
if(rcvStat != HAL_OK){
printf("error\r\n");
}
sprintf(check_msg, "%s", (char*)received_msg);
printf("%s\r\n", check_msg);
printf("%d, %d\r\n", timer, flag);
HAL_Delay(timer);
printf("---------------------\r\n");
memset(received_msg, 0, strlen(received_msg));
memset(check_msg, 0, strlen(check_msg));
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Call the RX interrupt once before the while loop and then after you have recieved the data in the ISR. Remove it from the inside of the while.
It seems like in the update loop you are re-issuing the RX_IT call, you should call this once and wait for data to arrive (ISR fires) and then re-issue at that point inside of ISR. Otherwise with a message size greater than one byte, you could re-issue the IT function after it recieved a few bytes and miss some bytes since it requests 10 bytes before the interrupt fires after being issued once.
Your ISR should primarily only be there for moving the RX'd data from one spot, to another for processing.
Printf is a pig and will most likely cause you to miss messages coming in if they are coming in real-time and not a one-off message.
A suggestion, would be to implement some type of circular FIFO queue in the ISR which stuffs the data in a FIFO buffer and that FIFO is accessed in your control loop for parsing, or action. That way, you are reciving each symbol and quickly exiting the interrupt and all data should be there for processing. I have had great success with this method in the past.
The main aim is to get the IP address of the stm32 board using the ethernet port but I am not there yet. In code a temporary breakpoint occurred at HAL_Init();
I am having trouble in finding what's wrong with code also had difficulty in knowing the working of the api function in the code. Need help ,Thank you.
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "cmsis_os.h"
#include "lwip.h"
#include "stdio.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include <lwip/netdb.h>
#include "lwip/netif.h"
#include "netif/etharp.h"
#include "ethernetif.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
osThreadId defaultTaskHandle;
struct netif gnetif;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
void StartDefaultTask(void const * argument);
//static void udp_client_connect(void);
static void MX_LWIP_Process(void);
extern struct netif gnetif;
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* #brief The application entry point.
* #retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
//initialize the lwip stack
MX_LWIP_Init();
/* USER CODE BEGIN 2 */
// udp_client_connect();
/* USER CODE END 2 */
/* USER CODE BEGIN RTOS_MUTEX */
/* add mutexes, ... */
/* USER CODE END RTOS_MUTEX */
/* USER CODE BEGIN RTOS_SEMAPHORES */
/* add semaphores, ... */
/* USER CODE END RTOS_SEMAPHORES */
/* USER CODE BEGIN RTOS_TIMERS */
/* start timers, add new ones, ... */
/* USER CODE END RTOS_TIMERS */
/* USER CODE BEGIN RTOS_QUEUES */
/* add queues, ... */
/* USER CODE END RTOS_QUEUES */
/* Create the thread(s) */
/* definition and creation of defaultTask */
osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */
/* Start scheduler */
// osKernelStart();
/* We should never get here as control is now taken by the scheduler */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
MX_LWIP_Process();
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
static void MX_LWIP_Process(void)
{
ethernetif_input(&gnetif);
ethernetif_set_link(&gnetif);
if (netif_is_link_up(&gnetif) && !netif_is_up(&gnetif))
{
netif_set_up(&gnetif);
dhcp_start(&gnetif);
}
}
I have a code generated by STMCubeMX where I use the portal PA0 like ADC Input. I'm trying to read this input using HAL Library of STM in C and transmit the value to USB port using CDC. See the main, where i try to get the data and show this:
#include "main.h"
#include "usb_device.h"
#include "usbd_cdc_if.h"
ADC_HandleTypeDef hadc1;
void SystemClock_Config(void);
static void MX_ADC1_Init(void);
int main(void)
{
uint8_t buffer[8];
HAL_Init();
SystemClock_Config();
MX_USB_DEVICE_Init();
MX_ADC1_Init();
HAL_ADC_Start(&hadc1);
while (1)
{
HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_14);
HAL_ADC_PollForConversion(&hadc1, 1000);
sprintf((char*)buffer,"%d\n",(int)HAL_ADC_GetValue(&hadc1));
//HAL_Delay(500);
CDC_Transmit_FS(buffer,8);
}
}
Debugging the code I saw that function "HAL_ADC_PollForConversion(&hadc1, 1000)" never returns "HAL_OK".
In the terminal, a single value appears.
UPDATE
Well, for the code works I have to put the "Start" of ADC inside of infinit loop and a call of "Stop" on the final of loop.
Ps: The Adc is running on continuous conversion mode.
int main(void)
{
uint8_t buffer[8];
HAL_Init();
SystemClock_Config();
MX_USB_DEVICE_Init();
MX_ADC1_Init();
while (1)
{
HAL_ADC_Start(&hadc1);
HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_14);
HAL_ADC_PollForConversion(&hadc1, 1000);
sprintf((char*)buffer,"%d\n",(int)HAL_ADC_GetValue(&hadc1));
//HAL_Delay(500);
CDC_Transmit_FS(buffer,8);
HAL_ADC_Stop(&hadc1);
}
}
As an addition to the answer in the comments:
The call to HAL_ADC_PollForConversion(&hadc1, 1000); explicitly stops the conversion, even if continuous conversion is activated.
If you you want/must wait for each conversion to get finished, you must restart the ADC with HAL_ADC_Start(&hadc1); after waiting (and reading) for the result.
I just started working with STM32F407VGT Discovery kit and I for the start I wanted to turn on/off LED's on-board. I am using CubeMX to generate initialization code and SystemWorkBench Eclipse to compile and also ST-LINK STM32 for programming my board. In CubeMX I initialized PD12-PD15 ports as output. I used function HAL_GPIO_WritePin to set my LED's ON. After compilation and programming to device, nothing happens... Please give me some advice what I did wrong.
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Problem solved! It seems that ST-LINK is not uploading hex correctly, I used AC6 to upload and everythig seem to work fine
When I write the following code. Putty prints the text but I can't type anything below ..
For example :
First he does a printf --> who are you ?
Then I do a fgets where I normally most type something , that doensn't work.
At last he does another printf where he say ; good to meet you ...
int main(void)
{
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
char name[10];
printf("Who are you? \n");
fgets(name,10,stdin);
printf("Good to meet you, %s.\n",name);
return(0);
}
can anyone help me ?
thanks !
You have overwritten the implemnetation of fputs in your source code. So your linker doesn't need to lookup the library implementation of that function. With your implementation of fputc you have connected the stdio-output-path to your UART transmitter.
You need to define the corresponding input-path. This could be done by implementing a fgetc function. In that can the library function fgets can call your function, polling the UART receiver.
The default implementation of stdio in embedded environment is highly vendor dependent. It can be connected to
the JTAG debugger
the microcontroller specific debug/console ports
nothing.
Therefore you are responsible to make the right implemantation of that connection. This can be more complecated than the fputc implementation because you have to check, if there is data received at the UART at the time of call.