Last active
August 6, 2019 17:17
-
-
Save thejpster/b002700236d4bcae20e71bc39c8f15ad to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* An example of some bad C code. | |
* | |
* Copyright (c) Jonathan 'theJPster' Pallant 2019 | |
* | |
* This example is under the Blue Oak Model Licence: https://blueoakcouncil.org/license/1.0.0 | |
*/ | |
// =========================================================================== | |
// System Includes | |
// =========================================================================== | |
#include <stdbool.h> | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <string.h> | |
// =========================================================================== | |
// Macros | |
// =========================================================================== | |
/** | |
* How big our buffer is. | |
*/ | |
#define NUM_CHARS 16 | |
/** | |
* Some imaginary hardware UART register. | |
*/ | |
#define UART_FIFO ((uint32_t*) 0x40004010U) | |
// =========================================================================== | |
// External Function Declarations | |
// =========================================================================== | |
/** | |
* Normally this would be in a header file. | |
*/ | |
extern void uart_write(const char* fmt, ...); | |
// =========================================================================== | |
// Private Variable Definitions | |
// =========================================================================== | |
/** | |
* The buffer we store our UART characters in. | |
*/ | |
static char character_buffer[NUM_CHARS]; | |
/** | |
* The number of characters we have in our UART buffer. | |
*/ | |
static size_t num_chars_in_buffer; | |
// =========================================================================== | |
// Private Function Declarations | |
// =========================================================================== | |
static bool has_characters(void); | |
static char get_character(void); | |
// =========================================================================== | |
// Public Functions | |
// =========================================================================== | |
/** | |
* Called when our startup code has finished. | |
* | |
* Never exits. Simply prints out the characters received on the UART. | |
*/ | |
int main(void) { | |
char *c = malloc(16); | |
sprintf(c, "%s%s%s%s%s", "test", "test", "test", "test", "test"); | |
uart_write("%s", c); | |
while(true) { | |
if (has_characters()) { | |
char x = get_character(); | |
uart_write("I got character %u (which is '%c')!", x); | |
} | |
} | |
} | |
/** | |
* Called whenever a character is available in the hardware UART FIFO. Stuffs | |
* the character into our buffer so the main loop can read it. | |
*/ | |
void UART_Interrupt(void) { | |
if (num_chars_in_buffer < NUM_CHARS) { | |
character_buffer[num_chars_in_buffer] = *UART_FIFO; | |
num_chars_in_buffer++; | |
} | |
} | |
// =========================================================================== | |
// Private Functions | |
// =========================================================================== | |
/** | |
* Return true if there is something in the buffer, false otherwise. | |
*/ | |
static bool has_characters(void) { | |
if (num_chars_in_buffer > 0) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* Get a character from the buffer. Returns 'null' character if buffer is empty. | |
*/ | |
static char get_character(void) { | |
char c = '\0'; | |
if (num_chars_in_buffer > 0) { | |
c = character_buffer[0]; | |
memmove( | |
&character_buffer[1], | |
&character_buffer[0], | |
num_chars_in_buffer - 1 | |
); | |
} | |
return c; | |
} | |
// =========================================================================== | |
// End of File | |
// =========================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment