Instantly share code, notes, and snippets.
Last active
October 18, 2022 06:48
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save shiva-karthick/ac323cff663329d6432c9d76efe146bd to your computer and use it in GitHub Desktop.
EE2028 Assignment 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Includes ------------------------------------------------------------------*/ | |
#include "main.h" | |
#include "../../Drivers/BSP/B-L475E-IOT01/stm32l475e_iot01_accelero.h" | |
#include "../../Drivers/BSP/B-L475E-IOT01/stm32l475e_iot01_tsensor.h" | |
#include "../../Drivers/BSP/B-L475E-IOT01/stm32l475e_iot01_gyro.h" | |
#include "../../Drivers/BSP/B-L475E-IOT01/stm32l475e_iot01_magneto.h" | |
#include "../../Drivers/BSP/B-L475E-IOT01/stm32l475e_iot01_psensor.h" | |
#include "../../Drivers/BSP/B-L475E-IOT01/stm32l475e_iot01_hsensor.h" | |
#include "stdio.h" | |
#include "stdbool.h" | |
#include "string.h" | |
#include "math.h" | |
#define NORMAL 0 | |
#define PAUSE 1 | |
#define GYRO_THRESHOLD 20.0 | |
#define TEMP_THRESHOLD 38 | |
#define MAG_THRESHOLD 0.4 | |
#define HUM_THRESHOLD 30.0 | |
#define PRES_THRESHOLD 110000.0 | |
#define WARNING 1 | |
#define SAFE 0 | |
// Function Declarations | |
void select_mode(void); | |
static void MX_GPIO_Init(void); | |
static void printAcc(void); | |
static void printTemp(void); | |
static void printMagneto(void); | |
static void UART1_Init(void); | |
UART_HandleTypeDef huart1; | |
// Initialized Global Variables | |
int button_count = 0, mode = NORMAL, button_t2 = 0, button_t1 = 0; | |
HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { | |
button_t2 = (HAL_GetTick()); | |
if (GPIO_Pin == BUTTON_EXTI13_Pin) // Check for pin 13 | |
{ | |
select_mode(); | |
} | |
} | |
int main(void) { | |
initialise_monitor_handles(); // for semi-hosting support (printf) | |
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */ | |
HAL_Init(); | |
MX_GPIO_Init(); | |
/* UART initialization */ | |
// UART1_Init(); | |
/* Peripheral initializations using BSP functions */ | |
BSP_ACCELERO_Init(); | |
BSP_TSENSOR_Init(); | |
BSP_MAGNETO_Init(); | |
// local variables | |
uint32_t start_accelerometer = HAL_GetTick(); | |
uint32_t start_temperature = HAL_GetTick(); | |
uint32_t start_magnetometer = HAL_GetTick(); | |
uint32_t toggle_led = HAL_GetTick(); | |
while (1) { | |
if (mode == NORMAL) { // resume printing | |
if ((HAL_GetTick() - start_accelerometer) > 1000) { | |
// 1 second has passed already | |
printAcc(); | |
start_accelerometer = HAL_GetTick(); // reset the start_accelerometer | |
} | |
if ((HAL_GetTick() - start_temperature) > 1500) { | |
// 1.5 second has passed already | |
printTemp(); | |
start_temperature = HAL_GetTick(); // reset the start_temperature | |
} | |
if ((HAL_GetTick() - toggle_led) > 50) { | |
// 50 millisecond has passed already | |
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14); | |
toggle_led = HAL_GetTick(); // reset the toggle_led | |
} | |
if ((HAL_GetTick() - start_magnetometer) > 2000) { | |
// 2 second has passed | |
printMagneto(); | |
start_magnetometer = HAL_GetTick(); // reset the start_magnetometer | |
} | |
} else if (mode == PAUSE) { // pause printing | |
// do nothing | |
// pause printing | |
} | |
} | |
} | |
void select_mode(void) { | |
if (button_count == 0) { | |
// mode = NORMAL; | |
button_count = 1; | |
button_t1 = button_t2; | |
printf("Initial \n", button_count); | |
} else if (button_count >= 1) { | |
if ((button_t2 - button_t1) <= 1000) { | |
// Double Press | |
mode = PAUSE; | |
printf("Toggle Pause \n", button_count); | |
} | |
if ((button_t2 - button_t1) > 1000) { | |
mode = NORMAL; | |
button_count = 1; | |
button_t1 = button_t2; | |
printf("Toggle Normal \n", button_count); | |
} | |
} | |
} | |
static void MX_GPIO_Init(void) { | |
GPIO_InitTypeDef GPIO_InitStruct = { 0 }; | |
/* GPIO Ports Clock Enable */ | |
__HAL_RCC_GPIOB_CLK_ENABLE(); // Enable AHB2 Bus for GPIOB | |
HAL_GPIO_WritePin(GPIOB, LED2_Pin, GPIO_PIN_RESET); // Reset the LED2_Pin as 0 | |
/*Configure GPIO pin LED2_Pin */ | |
GPIO_InitStruct.Pin = LED2_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); | |
// Configuring Push Button // | |
/* GPIO Ports Clock Enable */ | |
__HAL_RCC_GPIOC_CLK_ENABLE(); // Enable AHB2 Bus for GPIOB | |
/*Configure GPIO pin LED2_Pin */ | |
GPIO_InitStruct.Pin = BUTTON_EXTI13_Pin; | |
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; | |
GPIO_InitStruct.Pull = GPIO_NOPULL; | |
// GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; | |
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); | |
// Enable NVIC EXTI line 13 | |
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); | |
} | |
static void printAcc(void) { | |
float accel_data[3]; // array of 3 values | |
int16_t accel_data_i16[3] = { 0 }; // array to store the x, y and z readings. | |
BSP_ACCELERO_AccGetXYZ(accel_data_i16); // read accelerometer after 1s | |
// the function above returns 16 bit integers which are 100 * acceleration_in_m/s2. Converting to float to print the actual acceleration. | |
accel_data[0] = (float) accel_data_i16[0] / 100.0f; | |
accel_data[1] = (float) accel_data_i16[1] / 100.0f; | |
accel_data[2] = (float) accel_data_i16[2] / 100.0f; | |
printf("Accel X : %f; Accel Y : %f; Accel Z : %f; \n", accel_data[0], | |
accel_data[1], accel_data[2]); | |
} | |
static void printMagneto(void) { | |
float magneto_data[3]; // array of 3 values | |
int16_t magneto_data_i16[3] = { 0 }; // array to store the x, y and z readings. | |
BSP_ACCELERO_AccGetXYZ(magneto_data_i16); // read accelerometer after 1s | |
// the function above returns 16 bit integers which are 100 * acceleration_in_m/s2. | |
// Converting to float to print the actual acceleration. | |
magneto_data[0] = (float) magneto_data_i16[0] / 100.0f; | |
magneto_data[1] = (float) magneto_data_i16[1] / 100.0f; | |
magneto_data[2] = (float) magneto_data_i16[2] / 100.0f; | |
printf("Magneto X : %f; Magneto Y : %f; Magneto Z : %f; \n", | |
magneto_data[0], magneto_data[1], magneto_data[2]); | |
} | |
static void printTemp(void) { | |
float temp_data = 0.0; | |
temp_data = BSP_TSENSOR_ReadTemp(); // read temperature sensor after 1.5s | |
printf("Temperature : %f\n", temp_data); | |
} | |
static void UART1_Init(void) { | |
/* Pin configuration for UART. BSP_COM_Init() can do this automatically */ | |
__HAL_RCC_GPIOB_CLK_ENABLE(); | |
GPIO_InitTypeDef GPIO_InitStruct = { 0 }; | |
GPIO_InitStruct.Alternate = GPIO_AF7_USART1; | |
GPIO_InitStruct.Pin = GPIO_PIN_7 | GPIO_PIN_6; | |
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; | |
GPIO_InitStruct.Pull = GPIO_NOPULL; | |
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; | |
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); | |
/* Configuring UART1 */ | |
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.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; | |
if (HAL_UART_Init(&huart1) != HAL_OK) { | |
while (1) | |
; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "main.h" | |
#include "../../Drivers/BSP/B-L475E-IOT01/stm32l475e_iot01_accelero.h" | |
#include "../../Drivers/BSP/B-L475E-IOT01/stm32l475e_iot01_tsensor.h" | |
#include "stdio.h" | |
#include "../../Drivers/BSP/B-L475E-IOT01/stm32l475e_iot01_hsensor.h" | |
extern void initialise_monitor_handles(void); // for semi-hosting support (printf) | |
static void MX_GPIO_Init(void); | |
static void showAcc(void); | |
static void showTemp(void); | |
void SystemClock_Config(void); | |
static void func1(void); | |
uint32_t T1, T2; | |
int flag = 0, mode = 0; | |
HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) | |
{ | |
if ((GPIO_Pin == BUTTON_EXTI13_Pin) && (flag == 0)) | |
{ | |
flag++; | |
T1 = uwTick; | |
printf("Button pressed once.\n"); | |
} | |
else if ((GPIO_Pin == BUTTON_EXTI13_Pin) && (flag > 0)) | |
{ | |
flag++; | |
T2 = uwTick; | |
printf("Button pressed twice.\n"); | |
} | |
//printf("Flag = %i\n", flag); | |
//printf("T1 = %i\n", T1); | |
} | |
int main(void) | |
{ | |
initialise_monitor_handles(); | |
HAL_Init(); | |
MX_GPIO_Init(); | |
BSP_ACCELERO_Init(); | |
BSP_TSENSOR_Init(); | |
BSP_HSENSOR_Init(); //Humidity Sensor Test | |
//uint32_t H1, H; | |
//H = uwTick; | |
while(1) | |
{ | |
//H1 = uwTick; | |
if ((flag == 1) && (uwTick - T1 > 1000)) | |
{ | |
printf("Printing activated.\n"); | |
flag = 0; | |
func1(); | |
} | |
if ((flag == 2) && (T2 - T1 < 1000)) | |
{ | |
printf("Printing paused.\n"); | |
flag = 0; | |
} | |
/*float hsensor; | |
hsensor = BSP_HSENSOR_ReadHumidity(); | |
if ((H1 - H) > 1000) | |
{ | |
printf("Humidity: %f\n",hsensor); | |
H = uwTick; | |
}*/ | |
} | |
} | |
static void MX_GPIO_Init(void) //For LED and PB | |
{ | |
GPIO_InitTypeDef GPIO_InitStruct = {0}; | |
//GPIO Ports Clock Enable | |
__HAL_RCC_GPIOB_CLK_ENABLE(); // For LED | |
__HAL_RCC_GPIOC_CLK_ENABLE(); // For PB | |
//Configure GPIO pin Output Level // Pin Initialization | |
HAL_GPIO_WritePin(GPIOB, LED2_Pin, GPIO_PIN_RESET); | |
//Configure GPIO pin LED2_Pin // Pin Configuration | |
GPIO_InitStruct.Pin = LED2_Pin; | |
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; | |
GPIO_InitStruct.Pull = GPIO_NOPULL; | |
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; //Have to implement as part of GPIO initialization | |
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); | |
// Configuration of BUTTON_EXTI13_Pin (G{IO-C Pin-13)as AF | |
GPIO_InitStruct.Pin = BUTTON_EXTI13_Pin; | |
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; | |
GPIO_InitStruct.Pull = GPIO_NOPULL; | |
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); | |
// Enable NVIC EXTI line 13 | |
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); | |
} | |
static void func1(void) | |
{ | |
uint32_t Acc, Acc1, Temp, Temp1, LED, LED1; | |
Acc = uwTick; //Initialize tick for Acc | |
Temp = uwTick; //Initialize tick for Temp | |
LED = uwTick; //Initialize tick for LED | |
do | |
{ | |
Acc1 = uwTick; | |
Temp1 = uwTick; | |
LED1 = uwTick; | |
if((Acc1 - Acc) > 1000) | |
{ | |
showAcc(); | |
Acc = uwTick; | |
} | |
if((Temp1 - Temp) > 1500) | |
{ | |
showTemp(); | |
Temp = uwTick; | |
} | |
if ((LED1 - LED) > 1000) | |
{ | |
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14); //Pin Read Write (Toggle 0 to 1) | |
LED = uwTick; | |
} | |
}while(flag == 0); | |
} | |
static void showAcc(void) | |
{ | |
float accel_data[3]; | |
int16_t accel_data_i16[3] = { 0 }; // array to store the x, y and z readings. | |
BSP_ACCELERO_AccGetXYZ(accel_data_i16); // read accelerometer | |
// the function above returns 16 bit integers which are 100 * acceleration_in_m/s2. Converting to float to print the actual acceleration. | |
accel_data[0] = (float)accel_data_i16[0] / 100.0f; | |
accel_data[1] = (float)accel_data_i16[1] / 100.0f; | |
accel_data[2] = (float)accel_data_i16[2] / 100.0f; | |
printf("\nAccel:\nX: %f; Y: %f; Z: %f", accel_data[0], accel_data[1], accel_data[2]); | |
} | |
static void showTemp(void) | |
{ | |
float temp_data; | |
temp_data = BSP_TSENSOR_ReadTemp(); // read temperature sensor | |
printf("\nTemperature : %f\n", temp_data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment