Nextion - STM32 if Statement Does Not Work - stm32

Im working on a STM32 project using Nextion display. Im trying to change the state of the pin according to an array and state of the pin depends on three different bits in the array. I used four if statements back to back and even tho there are no errors, it does not work.
if(Rx_Data[1] == 0x05)
...
if(Rx_Data[2] == 0x08)
{
HAL_Delay(10);
if(Rx_Data[3] == 0x00)
{
//d++;
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
HAL_Delay(1000);
}
if(Rx_Data[3] == 0x01)
{
//d++;
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
HAL_Delay(1000);
}

Related

writing data on sd card

I use a simple code in stm32 for SD card.
The code is written in "int main(void)" section. I am trying to configure a SD card so that as soon as the STM32F750vbt6 turn on, a file is created in the SD card and a text is written in the file.
when I turn the STM32F750vbt6 on, this does not happen and nothing is written in the SD card but when the code reaches to the "while" section (I have written an LED blink code inside the while section) and I reset the micro controller using RST pin of the STM32F750vbt6, the device works properly and a text is written in the sd card.
How can I fix this? Why is there a need for resetting the device for the code to work?
this is my code :
const char wtext[] = "hi world";
int main(void)
{
FRESULT res; /* FatFs function common result code */
uint32_t byteswritten, bytesread; /* File write/read counts */
MPU_Config();
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SDMMC1_SD_Init();
MX_FATFS_Init();
if(retSD == 0)
{
if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) == FR_OK)
{
if(f_open(&MyFile, "file.txt", FA_CREATE_ALWAYS | FA_WRITE) ==FR_OK)
{
f_write(&MyFile, wtext, sizeof(wtext), (void*)&byteswritten);
f_close(&MyFile);
}
}
}
FATFS_UnLinkDriver(SDPath);
while (1)
{
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
HAL_Delay(2000);
HAL_GPIO_WritePin(LED1_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
HAL_Delay(2000);
}
}
The problem was that the device did not work from the ports. The data and CMD ports must be connected as pull-up

stm32 SPI receive only mode need a transmit?

sorry in advance, i am new at this kind of projects.
I tried to implement a connection to an ADC (LTC1609) via SPI. I just want to get the data from the ADC as quick as possible. The ADC has just a output-line, so i chose the "read only mode". But i also tried to use the HAL_SPI_TransmitReceive funktion because i remembered that the spi exchange starts with writeing in the transmit register (at least with the HCS12).
The ADC needs also some additional signals changes between pulling down the NSS and starting the spi connection so i put the NSS in software mode.
The Cofiguration looks like this:
static void MX_SPI1_Init(void)
{
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;
hspi1.Init.DataSize = SPI_DATASIZE_16BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
}
I write a funktion to read from ADC and store the data.
void LTC1609_ADU_Read(uint8_t *data)
{
uint8_t buffer_rx[2];
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0); // NSS low (Pin PA4) Start
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, 0); // ADU R/NC low (Pin PG6) Start conversion
delay_hns (1); // wait 1us (10 x 100ns)
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, 1); // ADU R/NC high (Pin PG6)
if(HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_8) == 1) // /BUSY is high? -> start SPI
{
if(HAL_SPI_Receive(&hspi1, buffer_rx,2,10)!= HAL_OK);
{
SPI_error = HAL_SPI_GetError(&hspi1); // get the SPI error
Error_Handler(); // stop in error
}
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 1); // Set ADU /CS high (Pin PA4) fin
*data = buffer_rx[1]; // store received data
}
In the while(1) i called it like this with a delay of 100 ms for testing:
LTC1609_ADU_Read(&buffer_A_rx);
Problem:
The uC (STM32L4R5ZIT6P) dont start the SPI clk to trigger out the data from ADC. Also the Clock polarity is somtimes low (like i want) and sometimes high [could also be a messuring problem, i messure it with a sheep logic analyser 24Mhz 8CH from amazon].
If i start the programm, it runs into the error handler and the errorcode is 0x20. I found this explaining:
HAL_SPI_ERROR_FLAG 0x00000020U /*!< Flag: RXNE,TXE, BSY */
Can anyone give me a tip on what could be the reason? Did iam doing something wrong or could the hardware be damaged? Thanks in advance!
screenshot signals without clk
One problem that I see in your code is that you check the BUSY pin immediately after setting R/C high and abort the transaction if BUSY is low.
According to the datasheet, BUSY line may be low for up to 3 us (t3).
Try waiting for the busy pin to go high, for example by adding a loop as shown below.
void LTC1609_ADU_Read(uint8_t *data)
{
uint8_t buffer_rx[2];
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0); // NSS low (Pin PA4) Start
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, 0); // ADU R/NC low (Pin PG6) Start conversion
delay_hns(1); // wait 1us (10 x 100ns)
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, 1); // ADU R/NC high (Pin PG6)
uint8_t retries = 0;
do {
delay_hns(1);
} while (HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_8) == 0 && ++retries < 5);
if (HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_8) == 1) // /BUSY is high? -> start SPI
{
if (HAL_SPI_Receive(&hspi1, buffer_rx, 2, 10) != HAL_OK)
{
SPI_error = HAL_SPI_GetError(&hspi1); // get the SPI error
Error_Handler(); // stop in error
}
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 1); // Set ADU /CS high (Pin PA4) fin
*data = buffer_rx[1]; // store received data
}
Also, there is a stray ; in your code, on this line:
if(HAL_SPI_Receive(&hspi1, buffer_rx,2,10)!= HAL_OK);

Switch LED on STM discovery board with external button

I am new to programming microcontrollers.
I have the STM32F072 discovery board and I want to use an external push button with it. I am using Visual Studio Code.
I connected the button to the 3V output and the other side to pin A10, which is configured like this:
/*Configure GPIO pin : PA10 */
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
Then, I wrote a function to read the state of the button
uint8_t read_button(void)
{
uint8_t button_state = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_10);
return button_state;
}
I am calling the function in the infinite while loop in main.c, where my idea was quite simply:
button_state = read_button();
if (button_state == 1)
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, GPIO_PIN_SET);
}
(Pin C6 is connected with with the red LED) After Building and upoading, the red LED is always on. The Button does nothing.
Can anyone give me a hint what am I doing wrong?
Thank you very much!
it's normal that the LED is always on, you never put it off in your code.
button_state = read_button();
if (button_state == 1)
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, GPIO_PIN_RESET);
}
With this, your LED will be on when you push your button and off otherwise.

STM32 TSC Slider_3 Ch_Capacitive Touch_STM32F072 Discovery_ Resolution and Calculation of Position in the range of 0...255

To understand capacitive touch / TSC library based application, I’ve been studying the polling based sample code given in AN5105 for STM32F072B-Disco evaluation board. After tweaking the code to customize for the application, I’ve a few questions as below which I am not able to understand and can’t find any explanation even after reviewing various application notes from ST. Your input would be much appreciated and would be very helpful for me to clarify the concepts of TSC.
Resolution and Calculation of touch Position on the slider in the range of 0...255:
The header file, tsl_conf.h, supplied by TSC, has the following parameters:
#define TSLPRM_LINROT_RESOLUTION (7) //Position resolution in number of bits (range=1..8)
#define TSLPRM_USE_3CH_LIN_H (1) //Half ended electrode design
MyLinRots[0].p_Data->Position structure is used to compare the position value in the range of 0...255, but I am not able to understand the correlation of the value and position of touch on the slider / channel. How does Resolution value 1..8 affect this calculation? Is there any formula based on resolution parameter to calculate position value based on which channel is touched on the slider?
In the example code given in AN5105, I am trying to get 4 LEDs to be evenly distributed in the entire slider over the range of 0...255 with the following code, but not able to understand the calculation of the values used to compare with MyLinRots[0].p_Data->Position structure :
if(MyLinRots[0].p_Data->StateId == TSL_STATEID_DETECT)
{
//TSLPRM_LINROT_RESOLUTION
if(MyLinRots[0].p_Data->Position >= 1 && MyLinRots[0].p_Data->Position < 60)
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD3_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD6_GPIO_Port, LD6_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
}
if(MyLinRots[0].p_Data->Position >= 60 && MyLinRots[0].p_Data->Position < 120)
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD6_GPIO_Port, LD6_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
}
if(MyLinRots[0].p_Data->Position >= 120 && MyLinRots[0].p_Data->Position < 180)
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD6_GPIO_Port, LD6_Pin, GPIO_PIN_SET);
}
if(MyLinRots[0].p_Data->Position >= 180 && MyLinRots[0].p_Data->Position < 255)
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD6_GPIO_Port, LD6_Pin, GPIO_PIN_RESET);
}
}
else //if(MyLinRots[0].p_Data->StateId == TSL_STATEID_RELEASE)
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD6_GPIO_Port, LD6_Pin, GPIO_PIN_RESET);
}
In another example, I re-configured the touch channels on the same STM32F072B-Disco board as 3 individual touch keys. If I keep TSLPRM_TKEY_Detect_IN_TH and TSLPRM_TKEY_Detect_OUT_TH parameters in tsl_conf.h header file as default of 110 & 120, the project compiles without any error.
// TouchKeys Detect state input threshold (range=0..255)
#define TSLPRM_TKEY_DETECT_IN_TH (110)
// TouchKeys Detect state output threshold (range=0..255)
#define TSLPRM_TKEY_DETECT_OUT_TH (120)
With these parameter values, I had to press very hard on the channels and often times, it was very difficult to detect touch, so I reconfigured detect threshold to lower value of 50.
#define TSLPRM_TKEY_DETECT_IN_TH (50)
With this change, I get compiling errors as below:
../Middlewares/ST/STM32_TouchSensing_Library/inc/tsl_check_config.h(162): error: #35: #error directive: "TSLPRM_TKEY_DETECT_OUT_TH is out of range (TSLPRM_TKEY_PROX_IN_TH+1 .. TSLPRM_TKEY_DETECT_IN_TH-1)."
User code is as follow:
//Private macro
#define TEST_TKEY(NB) ((MyTKeys[(NB)].p_Data->StateId == TSL_STATEID_DETECT) || (MyTKeys[(NB)].p_Data->StateId == TSL_STATEID_DEB_RELEASE_DETECT) || (MyTKeys[(NB)].p_Data->StateId == TSL_STATEID_TOUCH) || (MyTKeys[(NB)].p_Data->StateId == TSL_STATEID_DEB_DETECT))
while (1)
{
if (tsl_user_Exec() == TSL_STATUS_OK)
{
ProcessSensors(); // Execute sensors related tasks
}
}
void ProcessSensors (void)
{
if (TEST_TKEY(0))
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD3_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
}
if (TEST_TKEY(1))
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);
}
if (TEST_TKEY(2))
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD5_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(LD4_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
}
}
static void MX_TSC_Init(void)
{
/** Configure the TSC peripheral
htsc.Instance = TSC;
htsc.Init.CTPulseHighLength = TSC_CTPH_2CYCLES;
htsc.Init.CTPulseLowLength = TSC_CTPL_2CYCLES;
htsc.Init.SpreadSpectrum = DISABLE;
htsc.Init.SpreadSpectrumDeviation = 1;
htsc.Init.SpreadSpectrumPrescaler = TSC_SS_PRESC_DIV1;
htsc.Init.PulseGeneratorPrescaler = TSC_PG_PRESC_DIV4;
htsc.Init.MaxCountValue = TSC_MCV_8191;
htsc.Init.IODefaultMode = TSC_IODEF_OUT_PP_LOW;
htsc.Init.SynchroPinPolarity = TSC_SYNC_POLARITY_FALLING;
htsc.Init.AcquisitionMode = TSC_ACQ_MODE_NORMAL;
htsc.Init.MaxCountInterrupt = DISABLE;
htsc.Init.ChannelIOs = TSC_GROUP1_IO3|TSC_GROUP2_IO3|TSC_GROUP3_IO2;
htsc.Init.ShieldIOs = 0;
htsc.Init.SamplingIOs = TSC_GROUP1_IO4|TSC_GROUP2_IO4|TSC_GROUP3_IO3;
if (HAL_TSC_Init(&htsc) != HAL_OK)
{
Error_Handler();
}
}
I don't understand what is it that I am doing wrong and how can I configure 3 touch keys on this slider with adjustable detect threshold?
I've the project complied in Keil, which I can share to better understand the problem.
Your support with these questions is much appreciated and would be very helpful for me to understand and learn TSC and Touch application..
Thank you.

CubeMX - I2C DMA - help needed - ST32F1

HAL_I2C_Mem_Write_DMA / HAL_I2C_Mem_Read_DMA what is the problem ?
Hi I'm trying to run I2C in DMA mode with LIS35 (accelerometer). I wrote simple code as below but each time when I try to run or debug it I'm getting back return "LIS35_ERROR;" which means that LIS35_I2C_Init(void) function goes wrong.
Previously (I mean yesterday) I wrote two similarly projects:
First was based on HAL_I2C_Mem_Write / HAL_I2C_Mem_Read functions
- and all works properly (return LIS35_OK;)
Second was based on HAL_I2C_Mem_Write_IT / HAL_I2C_Mem_Read_IT functions
- and all works properly(return LIS35_OK;)
Environment:
STM32CubeMX - updated today,
Board - Nucleo-F103RB
CubeMX project added as attachment
I2C configuration al on pictures below
/* USER CODE BEGIN 4 */
char LIS35_I2C_Init(void)
{
uint8_t Sett_Lis35_cr2_boot = LIS35_REG_CR2_BOOT;
uint8_t RegVal, LIS35Settings;
HAL_StatusTypeDef state1, state2, state3;
volatile long int i;
//reset LIS35 settings
if(HAL_I2C_Mem_Write_DMA(&hi2c2, LIS35_Addr, LIS35_REG_CR2, 1, &Sett_Lis35_cr2_boot, 1) != HAL_OK) {
//led blink info
}
while (HAL_I2C_GetState(&hi2c2) != HAL_I2C_STATE_READY){}
//Write settings - activate all axis
LIS35Settings = LIS35_REG_CR1_XEN | LIS35_REG_CR1_YEN | LIS35_REG_CR1_ZEN | LIS35_REG_CR1_ACTIVE;
//WRITE CONFIGURATION TO LIS35 CHIP
if(HAL_I2C_Mem_Write_DMA(&hi2c2, LIS35_Addr, LIS35_REG_CR1, 1, &LIS35Settings, 1)!= HAL_OK) {
//led blink info
}
while (HAL_I2C_GetState(&hi2c2) != HAL_I2C_STATE_READY){}
//Read configuration - if OK, LIS35 is up and running
if(HAL_I2C_Mem_Read_DMA(&hi2c2, LIS35_Addr, LIS35_REG_CR1, 1, &RegVal, 1) != HAL_OK) {
//led blink info
}
while (HAL_I2C_GetState(&hi2c2) != HAL_I2C_STATE_READY){}
if (RegVal == LIS35Settings){
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
return LIS35_OK;
}
return LIS35_ERROR;
}
void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c){
static uint8_t cntTx;
cntTx++;
}
void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c){
static uint8_t cntRx;
cntRx++;
}
/* USER CODE END 4 */
Thanks in advance for any help :-)
Farnk