enum

  typedef enum colours_t { black, blue, green, cyan, red, white }; typedef enum colours_t { black, blue, green, cyan, red, white } my_colours;      

Read More

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 […]

Read More

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 […]

Read More

Buffers

Move buffer up one place //Move the buffer up 1 for (count = (MY_BUFFER_SIZE-1); count > 0; count–) { my_buffer[count] = my_buffer[(count – 1)]; } my_buffer[0] = some_new_value_being_added;      

Read More

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  

Read More

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 […]

Read More

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 […]

Read More

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 […]

Read More

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 […]

Read More

Arrays

  Multidimensional Arrays BYTE MyArray[20][10]; //2 Dimensional array [NumberOfParents][NumberOfElementsPerParent] BYTE MyArray[3][2] = { {0x01, 0x02}, { 0x11, 0x12}, { 0x21, 0x22} };    

Read More