typedef enum colours_t { black, blue, green, cyan, red, white }; typedef enum colours_t { black, blue, green, cyan, red, white } my_colours;
Category: Memory
Malloc
Temporary memory example //CREATE TEMPORARY MEMORY BUFFER uint32_t *my_temporary_memory_buffer = malloc(1024 * sizeof(uint32_t)); //Allocate a block of size bytes of memory, returning a pointer to the beginning of the block. Use calloc() to do same but zero initialise the buffer. if (my_temporary_memory_buffer != NULL) { //Use my_temporary_memory_buffer as needed my_temporary_memory_buffer[0] = 1234; //RELEASE TEMPORARY MEMORY […]
Endian
Endianness defines the location of byte 0 within a larger data structure. Little-endian The least significant byte of a 16-bit value is sent or stored before the most significant byte Bits7:0 | Bits15:8 A 32bit value is stored in a byte array as: Byte[3] | Byte[2] | Byte[1] | Byte[0] Big-endian The more significant byte […]
Buffers
Move buffer down one place Move buffer up one place
Pointers
Basic pointer usage char my_buffer[20]; //Declare an array char *p_buffer; //Declare a pointer p_buffer = &my_buffer[0]; //Load pointer *p_buffer++ = 'H'; //Write to the index 0 in the array *p_buffer++ = 'e'; //Write to the index 1 in the array
Converting Variables
Convert bytes to float union { float f; uint32_t ui32; } converted_value; converted_value.ui32 = ((uint32_t)byte_value[3] << 24) | ((uint32_t)byte_value[2] << 16) | ((uint32_t)byte_value[1] << 8) | (uint32_t)byte_value[0]; Note, if this appears not to work (you get zero out but with a valid float value in) then try reversing the byte order. Example The following input […]
Rolling memory buffer
Example Of Non Irq Function Fills Buffers and IRQ Function Empties Buffers This implemenation is based on the function sending the data being an irq which sends bits of the data irq calls and the function filling the data buffers being non irq and ensuring the operation is irq safe. #define FILE_DATA_NUM_OF_BUFFERS 4 #define FILE_DATA_BUFFER_LENGTH 512 […]
Variables – stdint.h types
In C99 the available basic integer types (the ones without _t) were deemed insufficient, because their actual sizes may vary across different systems. The C99 standard includes definitions of several new integer types to enhance the portability of programs. The new types are especially useful in embedded environments. All of the new types are suffixed […]
Variables
Volatile – Variables Which Can Be Altered In Interrupts Ensure these variables are declared with the 'volatile' keyword so the compiler knows the variable can be changed in an interrupt. This can be a classic cause of very strange bugs where you can't understand why your release code seems to be getting locked up, say, on a while loop […]
Arrays
Multidimensional Arrays Array length See sizeof()