How to Read and Show ADC value of STM32F4 using HAL Library - c

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.

Related

I'm trying to receive UART interrupts

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.

On-board LED, doesn't turn on/off

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

Debian I2C driver for OLED screen not working. [ssd1306]

I have some driver code that I am testing out for use with an SSD1306 driven OLED screen which is 128x32 (same as the OLED adafruit model). I need this to run in debian (I am using Linario-4.4.9)
I have followed the Debian guides on how to start creating a file handler for the device, this can be seen as follows below. The only thing in oled.h is the device adress (0x3C) and the proto types. I followed the initialization approach taken on the adafruit github (as I tried their code out first on an Ardunio to ensure the screen does in fact work). I believe I may be doing something wrong but I'm not entirely sure what I am doing wrong. I have also listed my init process below.
#include <errno.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <linux/i2c-dev.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include "oled.h"
int oled;
int lcd_driver_init(void)
{
///< Begin the init proc.
int dloc = open("/dev/i2c-1", O_RDWR);
if (dloc < 0 )
{
fprintf(stderr, "Error opening i2c device\n");
return -1;
}
if(ioctl(dloc, I2C_SLAVE, SCR_ADDR) < 0)
{
fprintf(stderr, "Error in ioctl. Errno :%i\n",errno);
return -2;
}
oled = dloc;
fprintf(stderr, "init success, device open and local\n");
return EXIT_SUCCESS;
}
int oled_command( uint8_t cmd)
{
char command[2]= {0};
command[1] = cmd;
int check = (write(oled, command, 2));
return check;
}
void oled_cmd_start()
{
int check = (write(oled, 0x00, sizeof(uint8_t)));
if(check<0)
fprintf(stderr, "Errno set:: %i\n", errno);
return;
}
void oled_data_start()
{
uint8_t _data_start_[1] ={ 0x40 };
int check = (write(oled, _data_start_, sizeof(uint8_t)));
if(check<0)
fprintf(stderr, "Errno set oled_data_start:: %i\n", errno);
return;
}
int oled_data (uint8_t xmit)
{
int check = (write(oled, &xmit, (sizeof(uint8_t))));
if(check<0)
fprintf(stderr, "Errno set oled_data:: %i\n", errno);
return check;
}
INIT PROCESS
void sendcommand(unsigned char payload)
{
oled_data(0x00); //Control Byte - Command
oled_data(payload); //payload
}
void lcd_init(void)
{
sendcommand(0xAE);//--Set Display off
sendcommand(0x00);//--set low column address
sendcommand(0x10);//--set high column address
sendcommand(0x81);//--set contrast control register
sendcommand(0x7f);
sendcommand(0xa1);//--set segment re-map 95 to 0
sendcommand(0xA6);//--set normal display
sendcommand(0xa8);//--set multiplex ratio(1 to 16)
sendcommand(0x1f);//--duty 1/32
sendcommand(0xd3);//--set display offset
sendcommand(0x00);//--not offset
sendcommand(0xd5);//--set display clock divide ratio/oscillator frequency
sendcommand(0xf0);//--set divide ratio
sendcommand(0xd9);//--set pre-charge period
sendcommand(0x22);
sendcommand(0xda);//--set com pins hardware configuration
sendcommand(0x02);//disable left/right remap and set for sequential
sendcommand(0xdb);//--set vcomh
sendcommand(0x49);//--0.83*vref
sendcommand(0x8d);//--set DC-DC enable
sendcommand(0x14);//
sendcommand(0xAF);//--turn on oled panel
sendcommand(0xA4);//--Entire Display ON
}
Following this, I send alternating 0xFF to try and make stripes on the screen. The only thing that shows up is random pixels. Nothing coherent.
I have connected a logic analyzer to sniff the I2C lines, and it appears that when I have the LA connected, the I2C lines no longer function and ERRNO returns an IO fault (#5).
There doesn't ever seem to be an issue opening up the device to get the file pointer however.
I do get ERRNO as timeout sometimes, but I have read that this is just an issue with I2C devices using the protocal as write expects a quicker response than I2C might give.
I am also compiling with -std=c99 -O0 to ensure all of the inline functions are there as well as ensuring that loop variables are available.
If anyone can point me in the right direction of can point out some flaw in my approach it would be much appreciated. Thank you.
EDIT
I've checked the device tree and the i2c device is correctly enabled. However none of the i2c_freq speeds seem to be enabled. Could this be causing the timeouts and the garbage data transfer?
I have connected a logic analyzer to sniff the I2C lines, and it appears that when I have the LA connected, the I2C lines no longer function and ERRNO returns an IO fault (#5).
logic analyzer is just a device for measurement. It converts the captured data into timing diagrams, decodes protocol which you have set. So it will not be responsible for any I2C read write error (until your grounding and h/w connections are correct).
For timeout issue either you can try by decreasing the i2c clock-frequency or ioctl I2C_TIMEOUT.
It turns out the SOM has an internal regulator for the I2C lines to be 1V8 where as the SSD1306 chip is running at 3V3 causing information to be mishandled. This behavior wasn't documented on the SOM.
Applying a level shifting chip to the design allowed for proper communication.
If someone has this same problem, check your schematics for voltage mismatch levels.

fgets function doesn't work

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.

How to print results with signs when calculation is done on array in C language

I am trying to do SPI transfer in a raspberry pi. Raspberry pi comes with an SPI interface which send and recieves the data through it GPIO(General purpose input output pins). The spi driver information is here. I am doing loopback (connect MOSI and MISO pins). I had describe in my code what data I am sending, This data I will recieve due to loopback. I want a calculation on the recieved data. These data are stored in the form of array. After the calculation I need to print the results with proper sign, but I am not getting it.Here is my complete code.
/************************All header should come here************************************************************************ ********************************/
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
/***************************All declaration should come here***************************************************************************************************/
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
static const char *device = "/dev/spidev0.0";//selecting spi device in the raspberry pi
static uint8_t mode;//mode of spi transfer
static uint8_t bits =8;//no. of bits to be transfer, Pi supports 8 bit transfer
static uint32_t speed = 32000000;//spi clock speed,32 MHz is the maximum supported clock frequency
static uint16_t delay;
/*****************************Function definition should come here**********************************************************************************************/
/*function for transfer*/
static void transfer(int fd)//declaring a function for the transfer, this should be call in main program for transfer
{
int ret;// a return variable
uint8_t tx[]={4,5,6,5,4,3,5,4};// Array initialization
uint8_t rx[ARRAY_SIZE(tx)]={0,};//recieving array should be same as transferred array
struct spi_ioc_transfer tr= { // standard structure for spi driver usage
.tx_buf=(unsigned long)tx,//transmitting buffer
.rx_buf= (unsigned long)rx,//recieving buffer
.len = ARRAY_SIZE(tx),//length of trasnmitting buffer
.delay_usecs=delay,
.speed_hz = speed,
.bits_per_word= bits,
};
ret = ioctl (fd, SPI_IOC_MESSAGE(1), &tr);//comm. is done by ioctl command, This initializes the transfer
for (ret=0;ret< ARRAY_SIZE(tx);ret++){//recieving the data in rx buffer and rx array size is same as tx array size
if (!(ret%4))//meant for printing process
puts("");
printf("%d\t %X\n",ret, rx[ret]);//formatting
}//herer return is used as an index for the recieving array
printf("\n");
float x=((rx[1]+rx[3])-(rx[0]+rx[2]))/(rx[0]+rx[1]+rx[2]+rx[3]);
//x[1]=((rx[5]+rx[7])-(rx[4])+rx[5])/(rx[4]+rx[5]+rx[6]+rx[7]);
printf("%f\n",x);
}//end of function for the transfer
/************************Main Program****************************************************************************************************************************/
int main(int argc, char *argv[]){// start of main
int ret=0;//initializing return
int fd;// a file handle to handle the device as a file
fd=open (device,O_RDWR);//fd handles this device with read and write permission
ret=ioctl(fd, SPI_IOC_WR_MODE,&mode);
ret=ioctl(fd, SPI_IOC_RD_MODE,&mode);
ret=ioctl(fd, SPI_IOC_WR_BITS_PER_WORD,&bits);
ret=ioctl(fd, SPI_IOC_RD_BITS_PER_WORD,&bits);
ret=ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ,&speed);
ret=ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ,&speed);
transfer(fd);//transfer of data begins
close(fd);//close the file handle, it has opened earlier
return ret;
}
I Just need my output with proper sign after calculation on the data.
Do you honestly think that how the data came into printf() matters? It really doesn't, printf() has no idea that you're using SPI, of course.
If all you want is the plus/minus to appear (instead of just the minus for negative values), reading the manual page for printf() tells us:
+
A sign (+ or -) should always be placed before a number produced by a signed conversion. By default a sign is used only for negative numbers. A + overrides a space if both are used.
So, you should use:
printf("%+d\t%+d\n", x[0], x[1]);

Resources