//Libraries from template
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
//Libraries I added
#include <string.h>
#include <stdlib.h>
#include <stdint.h> //AI: correction
//structs
//2. bit-field struct
// colon operator defines bit count
// fruit names chosen for humor
// 2+3+10+1 = 16 bits
// uint16 = unsigned int, 16 bit
typedef struct
{
uint16_t banana : 2;
uint16_t mango : 3;
uint16_t guayaba : 10;
uint16_t papaya : 1;
} BitOfFruit_Struct;
//functions
//1. pass by reference
void doubleValue(int* num)
{
*num *= 2; //take pointer and multiply value by 2
}
//5. volatile MMIO
//assume 32 bit instead of 86 bit
volatile uint32_t MMIO = 0x01234567;
uint32_t read_MMIO(void)
{
return MMIO;
} //AI: correction missing brackets
//AI: volatile forces reads from memory
// compiler can't cache value in register
// cannot provide atomicity, thread safety, locking
//6. static inline function
//takes in string as input
//output adds some praise
//static = only visible in source file
//inline = sub function body at call
//compiler may emit if address taken or
// compiler decides against inlining
static inline char* exclaim(char* str) //AI: String --> char
{
return strcat(str,"!");
}
//main
void app_main() {
//implement #1: pass by reference
//AI: change double -> doubleValue, corrected pointer handling and brackets
int first = 123; //initialize first number
printf("%d\n", first); //print initial number
doubleValue(&first); //'&' since dealing with pointers
printf("%d\n",first); //print number after doubling it
//implement #2: bit field struct
//assign fruit counts
BitOfFruit_Struct fruits= //AI: correction add =
{
.banana = 3, //AI: semi-colons -> commas
.mango = 7, //AI: corrected legal values
.guayaba = 12,
.papaya = 1
};
//print fruit outputs
//this could have applications for status values etc
//I chose fruit for learning purposes
printf("Bananas: %u\n", fruits.banana);
printf("Mango: %u\n", fruits.mango);
printf("Guayaba: %u\n", fruits.guayaba);
printf("Papaya: %u\n", fruits.papaya);
//implement #3: stack vs heap arrays
int sArr[100]; //stack array
int *hArr = malloc(100*sizeof(int)); //allocate memory for heap array
printf("Stack array: %p\n", (void *)sArr); //print stack array address
printf("Heap array: %p\n", (void *)hArr); //print heap array address
free(hArr); //free heap for security
//implement #4: undefined behavior
//int mistake; //not initializing to a value
//printf("UB: %d\n",mistake);
int safe = 0; //initialize to a value
printf("Safe: %d\n",safe);
//implement #5: volatile MMIO
uint32_t readme = read_MMIO();
printf("0x%08lx\n", (unsigned long)readme);
//implement #6: static inline function
//AI: change from strings to char arrays
char intro[32] = "RTS is great";
printf("%s\n",intro);
char* newIntro = exclaim(intro);
printf("%s\n",newIntro);
}