You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
476 lines
14 KiB
476 lines
14 KiB
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file : main.c
|
|
* @brief : Main program body
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
|
|
* All rights reserved.</center></h2>
|
|
*
|
|
* This software component is licensed by ST under BSD 3-Clause license,
|
|
* the "License"; You may not use this file except in compliance with the
|
|
* License. You may obtain a copy of the License at:
|
|
* opensource.org/licenses/BSD-3-Clause
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
/* USER CODE END Header */
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "main.h"
|
|
|
|
/* Private includes ----------------------------------------------------------*/
|
|
/* USER CODE BEGIN Includes */
|
|
#include <stdio.h>
|
|
/* 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 ---------------------------------------------------------*/
|
|
I2C_HandleTypeDef hi2c1;
|
|
|
|
UART_HandleTypeDef huart1;
|
|
|
|
/* 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);
|
|
static void MX_I2C1_Init(void);
|
|
/* USER CODE BEGIN PFP */
|
|
|
|
/* USER CODE END PFP */
|
|
|
|
/* Private user code ---------------------------------------------------------*/
|
|
/* USER CODE BEGIN 0 */
|
|
|
|
#define SEESAW_REGISTER_LEN (2)
|
|
#define SEESAW_TEMPERATURE_BUFFER_LEN (4)
|
|
#define SEESAW_MOISTURE_BUFFER_LEN (2)
|
|
|
|
#define SEESAW_STATUS_BASE (0x00)
|
|
#define SEESAW_TOUCH_BASE (0x0F)
|
|
#define SEESAW_STATUS_TEMP (0x04)
|
|
#define SEESAW_TOUCH_CHANNEL_OFFSET (0x10)
|
|
|
|
typedef uint8_t seesaw_register_t[SEESAW_REGISTER_LEN];
|
|
|
|
typedef struct seesaw_board {
|
|
int temperature_i2c_timeout;
|
|
int temperature_write_read_timeout;
|
|
int moisture_i2c_timeout;
|
|
int moisture_write_read_timeout;
|
|
int address;
|
|
uint8_t temperature_buffer[SEESAW_TEMPERATURE_BUFFER_LEN];
|
|
uint8_t moisture_buffer[SEESAW_MOISTURE_BUFFER_LEN];
|
|
} seesaw_board_t;
|
|
|
|
const seesaw_register_t temperature_register = {SEESAW_STATUS_BASE, SEESAW_STATUS_TEMP};
|
|
const seesaw_register_t moisture_register = {SEESAW_TOUCH_BASE, SEESAW_TOUCH_CHANNEL_OFFSET};
|
|
|
|
HAL_StatusTypeDef i2c_write(uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout) {
|
|
//printf("I2C WRITE | address=0x%04x | size=0x%04x\n", DevAddress, Size);
|
|
return HAL_I2C_Master_Transmit(&hi2c1, DevAddress, pData, Size, Timeout);
|
|
}
|
|
|
|
HAL_StatusTypeDef i2c_read(uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout) {
|
|
//printf("I2C READ | address=0x%04x | size=0x%04x\n", DevAddress, Size);
|
|
return HAL_I2C_Master_Receive(&hi2c1, DevAddress, pData, Size, Timeout);
|
|
}
|
|
|
|
HAL_StatusTypeDef seesaw_read(
|
|
int address,
|
|
seesaw_register_t seesaw_register,
|
|
uint8_t * buffer,
|
|
int i2c_timeout,
|
|
int write_read_timeout
|
|
) {
|
|
HAL_StatusTypeDef i2c_status;
|
|
i2c_status = i2c_write(
|
|
address,
|
|
seesaw_register,
|
|
SEESAW_REGISTER_LEN,
|
|
i2c_timeout
|
|
);
|
|
if (i2c_status != HAL_OK) { return i2c_status; }
|
|
|
|
HAL_Delay(write_read_timeout);
|
|
|
|
i2c_status = i2c_read(
|
|
address,
|
|
buffer,
|
|
SEESAW_TEMPERATURE_BUFFER_LEN,
|
|
i2c_timeout
|
|
);
|
|
if (i2c_status != HAL_OK) { return i2c_status; }
|
|
|
|
return HAL_OK;
|
|
}
|
|
|
|
float getTemp(uint8_t * buf) {
|
|
int32_t ret = ((uint32_t)buf[0] << 24) | ((uint32_t)buf[1] << 16) |
|
|
((uint32_t)buf[2] << 8) | (uint32_t)buf[3];
|
|
return (1.0 / (1UL << 16)) * ret;
|
|
}
|
|
|
|
int getMoisture(uint8_t * buf) {
|
|
return ((uint16_t)buf[0] << 8) | buf[1];
|
|
}
|
|
/* 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();
|
|
MX_I2C1_Init();
|
|
/* USER CODE BEGIN 2 */
|
|
#ifdef __GNUC__
|
|
/* With GCC, small printf (option LD Linker->Libraries->Small printf
|
|
set to 'Yes') calls __io_putchar() */
|
|
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
|
|
#else
|
|
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
|
|
#endif /* __GNUC__ */
|
|
/* USER CODE END 2 */
|
|
|
|
/* Infinite loop */
|
|
/* USER CODE BEGIN WHILE */
|
|
|
|
/* I2C config */
|
|
const seesaw_board_t seesaw_board = {
|
|
.temperature_i2c_timeout = 1000,
|
|
.temperature_write_read_timeout = 100,
|
|
.moisture_i2c_timeout = 3000,
|
|
.moisture_write_read_timeout = 100,
|
|
.address = 0x36 << 1, // shift by one because it is a 7bit address
|
|
};
|
|
|
|
HAL_StatusTypeDef i2c_status;
|
|
|
|
printf("\n\n\nHoi, da bini wieder\n");
|
|
|
|
while (1)
|
|
{
|
|
|
|
i2c_status = seesaw_read(
|
|
seesaw_board.address,
|
|
temperature_register,
|
|
seesaw_board.temperature_buffer,
|
|
seesaw_board.temperature_i2c_timeout,
|
|
seesaw_board.temperature_write_read_timeout
|
|
);
|
|
if (i2c_status != HAL_OK) {
|
|
Error_Handler();
|
|
}
|
|
printf("Temperature: %2.2f degree celsius | ", getTemp(seesaw_board.temperature_buffer));
|
|
|
|
i2c_status = seesaw_read(
|
|
seesaw_board.address,
|
|
moisture_register,
|
|
seesaw_board.moisture_buffer,
|
|
seesaw_board.moisture_i2c_timeout,
|
|
seesaw_board.moisture_write_read_timeout
|
|
);
|
|
if (i2c_status != HAL_OK) {
|
|
Error_Handler();
|
|
}
|
|
printf("Moisture value: %d\n", getMoisture(seesaw_board.moisture_buffer));
|
|
|
|
/* USER CODE END WHILE */
|
|
|
|
/* USER CODE BEGIN 3 */
|
|
}
|
|
/* USER CODE END 3 */
|
|
}
|
|
|
|
/**
|
|
* @brief System Clock Configuration
|
|
* @retval None
|
|
*/
|
|
void SystemClock_Config(void)
|
|
{
|
|
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
|
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
|
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
|
|
|
|
/** Configure the main internal regulator output voltage
|
|
*/
|
|
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
|
|
/** Initializes the RCC Oscillators according to the specified parameters
|
|
* in the RCC_OscInitTypeDef structure.
|
|
*/
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_MSI;
|
|
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
|
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
|
|
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
|
RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
|
|
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_10;
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
|
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers
|
|
*/
|
|
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK4|RCC_CLOCKTYPE_HCLK2
|
|
|RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
|
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
|
|
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
|
|
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
|
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
|
|
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
|
RCC_ClkInitStruct.AHBCLK2Divider = RCC_SYSCLK_DIV1;
|
|
RCC_ClkInitStruct.AHBCLK4Divider = RCC_SYSCLK_DIV1;
|
|
|
|
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/** Initializes the peripherals clocks
|
|
*/
|
|
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SMPS|RCC_PERIPHCLK_USART1
|
|
|RCC_PERIPHCLK_I2C1;
|
|
PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
|
|
PeriphClkInitStruct.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
|
|
PeriphClkInitStruct.SmpsClockSelection = RCC_SMPSCLKSOURCE_HSI;
|
|
PeriphClkInitStruct.SmpsDivSelection = RCC_SMPSCLKDIV_RANGE0;
|
|
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN Smps */
|
|
|
|
/* USER CODE END Smps */
|
|
}
|
|
|
|
/**
|
|
* @brief I2C1 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_I2C1_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN I2C1_Init 0 */
|
|
|
|
/* USER CODE END I2C1_Init 0 */
|
|
|
|
/* USER CODE BEGIN I2C1_Init 1 */
|
|
|
|
/* USER CODE END I2C1_Init 1 */
|
|
hi2c1.Instance = I2C1;
|
|
hi2c1.Init.Timing = 0x00707CBB;
|
|
hi2c1.Init.OwnAddress1 = 0;
|
|
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
|
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
|
hi2c1.Init.OwnAddress2 = 0;
|
|
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
|
|
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
|
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
|
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/** Configure Analogue filter
|
|
*/
|
|
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/** Configure Digital filter
|
|
*/
|
|
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN I2C1_Init 2 */
|
|
|
|
/* USER CODE END I2C1_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief USART1 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_USART1_UART_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN USART1_Init 0 */
|
|
|
|
/* USER CODE END USART1_Init 0 */
|
|
|
|
/* USER CODE BEGIN USART1_Init 1 */
|
|
|
|
/* USER CODE END USART1_Init 1 */
|
|
huart1.Instance = USART1;
|
|
huart1.Init.BaudRate = 115200;
|
|
huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
|
huart1.Init.StopBits = UART_STOPBITS_1;
|
|
huart1.Init.Parity = UART_PARITY_NONE;
|
|
huart1.Init.Mode = UART_MODE_TX_RX;
|
|
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
|
huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
|
|
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
|
if (HAL_UART_Init(&huart1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN USART1_Init 2 */
|
|
|
|
/* USER CODE END USART1_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief GPIO Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_GPIO_Init(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
/* GPIO Ports Clock Enable */
|
|
__HAL_RCC_GPIOC_CLK_ENABLE();
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
__HAL_RCC_GPIOD_CLK_ENABLE();
|
|
|
|
/*Configure GPIO pin Output Level */
|
|
HAL_GPIO_WritePin(GPIOB, LD2_Pin|LD3_Pin|LD1_Pin, GPIO_PIN_RESET);
|
|
|
|
/*Configure GPIO pin : B1_Pin */
|
|
GPIO_InitStruct.Pin = B1_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
|
|
|
|
/*Configure GPIO pins : LD2_Pin LD3_Pin LD1_Pin */
|
|
GPIO_InitStruct.Pin = LD2_Pin|LD3_Pin|LD1_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
/*Configure GPIO pins : PA11 PA12 */
|
|
GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
GPIO_InitStruct.Alternate = GPIO_AF10_USB;
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/*Configure GPIO pins : B2_Pin B3_Pin */
|
|
GPIO_InitStruct.Pin = B2_Pin|B3_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
|
|
|
}
|
|
|
|
/* USER CODE BEGIN 4 */
|
|
/**
|
|
* @brief Retargets the C library printf function to the USART.
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
PUTCHAR_PROTOTYPE
|
|
{
|
|
/* Place your implementation of fputc here */
|
|
/* e.g. write a character to the USART1 and Loop until the end of transmission */
|
|
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
|
|
|
|
return ch;
|
|
}
|
|
/* USER CODE END 4 */
|
|
|
|
/**
|
|
* @brief This function is executed in case of error occurrence.
|
|
* @retval None
|
|
*/
|
|
void Error_Handler(void)
|
|
{
|
|
/* USER CODE BEGIN Error_Handler_Debug */
|
|
/* User can add his own implementation to report the HAL error return state */
|
|
__disable_irq();
|
|
while (1)
|
|
{
|
|
}
|
|
/* USER CODE END Error_Handler_Debug */
|
|
}
|
|
|
|
#ifdef USE_FULL_ASSERT
|
|
/**
|
|
* @brief Reports the name of the source file and the source line number
|
|
* where the assert_param error has occurred.
|
|
* @param file: pointer to the source file name
|
|
* @param line: assert_param error line source number
|
|
* @retval None
|
|
*/
|
|
void assert_failed(uint8_t *file, uint32_t line)
|
|
{
|
|
/* USER CODE BEGIN 6 */
|
|
/* User can add his own implementation to report the file name and line number,
|
|
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
|
/* USER CODE END 6 */
|
|
}
|
|
#endif /* USE_FULL_ASSERT */
|
|
|
|
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
|
|