fix(Lab 2):
Added: - Buffer SIZE and clearing - Enabled GP15 pull-up resistor Modified: - Sleep time according to Lab instruction - Variables to unsigned char
This commit is contained in:
parent
4b8b8041ab
commit
620596cd6f
|
@ -269,4 +269,5 @@ gen
|
||||||
|
|
||||||
# Android studio 3.1+ serialized cache file
|
# Android studio 3.1+ serialized cache file
|
||||||
|
|
||||||
*/build/*
|
*/build/*
|
||||||
|
*/*.cmake
|
|
@ -1,7 +1,8 @@
|
||||||
/**
|
/**
|
||||||
* Lab 2
|
* Lab 2
|
||||||
* Note that this program would print out garbage for the first few characters
|
* This is configured with GPIO15 as a pseudo button with a pull up resistor
|
||||||
* received. It will work as intended after a few characters are received.
|
* Since the button is active low, the program will send '1' when the button is not pressed (high)
|
||||||
|
* and send 'A' to 'Z' when the button is pressed (low)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -9,12 +10,13 @@
|
||||||
|
|
||||||
#define UART_ID uart0
|
#define UART_ID uart0
|
||||||
#define BAUD_RATE 115200 // Baud rate of UART
|
#define BAUD_RATE 115200 // Baud rate of UART
|
||||||
|
#define UART_BUF_SIZE 64 // Buffer size for UART
|
||||||
|
|
||||||
#define UART_TX_PIN 16 // GPIO for UART TX pin
|
#define UART_TX_PIN 16 // GPIO for UART TX pin
|
||||||
#define UART_RX_PIN 17 // GPIO for UART RX pin
|
#define UART_RX_PIN 17 // GPIO for UART RX pin
|
||||||
#define GPIO_PIN 15 // GPIO for pseudo button
|
#define PSEUDO_BTN 15 // GPIO for pseudo button
|
||||||
|
|
||||||
#define SLEEP_TIME 100 // For testing can use a smaller value
|
#define SLEEP_TIME 1000 // For testing can use a smaller value
|
||||||
// 1000ms is based on the lab requirements
|
// 1000ms is based on the lab requirements
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -22,24 +24,32 @@
|
||||||
* @param uart_input Pointer to a character to be sent through UART0 (Space optimisation)
|
* @param uart_input Pointer to a character to be sent through UART0 (Space optimisation)
|
||||||
* @return void Prints the character in uart_receive()
|
* @return void Prints the character in uart_receive()
|
||||||
*/
|
*/
|
||||||
void uart_transmit(char *uart_input) {
|
static void uart_transmit(char *uart_input) {
|
||||||
if (gpio_get(GPIO_PIN)) { // Active low
|
if (gpio_get(PSEUDO_BTN)) { // If GPIO15 is high
|
||||||
uart_putc(UART_ID, '1');
|
uart_putc(UART_ID, '1');
|
||||||
} else { // Active high
|
} else { // If GPIO15 is low
|
||||||
uart_putc(UART_ID, *uart_input);
|
uart_putc(UART_ID, *uart_input);
|
||||||
*uart_input = (*uart_input - 'A' + 1) % 26 + 'A'; // Next character
|
*uart_input = (*uart_input - 'A' + 1) % 26 + 'A'; // Next character and wrap around back to A
|
||||||
// Wrap around back to A
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear buffer (read until buffer size is UART_BUF_SIZE)
|
||||||
|
* This is to prevent the program from printing out garbage
|
||||||
|
* for the first few characters received or during pseudo button press
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
static void uart_buf_clr() {
|
||||||
|
while (uart_is_readable(UART_ID) && uart_getc(UART_ID) != UART_BUF_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Receives a character through UART0 and prints it out
|
* Receives a character through UART0 and prints it out
|
||||||
* Ideally, the buffer should be cleared after each read
|
|
||||||
* NOTE: Flipping bit 5 effectively converts a character to lowercase
|
* NOTE: Flipping bit 5 effectively converts a character to lowercase
|
||||||
* @param uart_output Pointer to a location to store the received character (Space optimisation)
|
* @param uart_output Pointer to a location to store the received character (Space optimisation)
|
||||||
* @return void Character is read and printed
|
* @return void Character is read and printed
|
||||||
*/
|
*/
|
||||||
void uart_receive(char *uart_output) {
|
static void uart_receive(char *uart_output) {
|
||||||
if (uart_is_readable(UART_ID)) {
|
if (uart_is_readable(UART_ID)) {
|
||||||
*uart_output = uart_getc(UART_ID);
|
*uart_output = uart_getc(UART_ID);
|
||||||
|
|
||||||
|
@ -50,6 +60,8 @@ void uart_receive(char *uart_output) {
|
||||||
*uart_output = '2'; // If '1' is received, print '2'
|
*uart_output = '2'; // If '1' is received, print '2'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uart_buf_clr(); // Clear buffer
|
||||||
|
|
||||||
printf("%c\n", *uart_output);
|
printf("%c\n", *uart_output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,22 +70,25 @@ void uart_receive(char *uart_output) {
|
||||||
* Initializes UART0 and GPIO pins
|
* Initializes UART0 and GPIO pins
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
void pin_init() {
|
static void pin_init() {
|
||||||
// UART init
|
// UART init
|
||||||
uart_init(UART_ID, BAUD_RATE);
|
uart_init(UART_ID, BAUD_RATE);
|
||||||
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
|
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
|
||||||
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
|
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
|
||||||
|
|
||||||
// Pseudo button init
|
// Pseudo button init
|
||||||
gpio_set_dir(GPIO_PIN, GPIO_IN);
|
gpio_set_dir(PSEUDO_BTN, GPIO_IN);
|
||||||
|
|
||||||
|
// Enable pull up resistor
|
||||||
|
gpio_set_pulls(PSEUDO_BTN, true, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
stdio_init_all();
|
stdio_init_all();
|
||||||
pin_init();
|
pin_init();
|
||||||
|
|
||||||
char uart_input = 'A'; // Initial value of A
|
u_char uart_input = 'A'; // Initial value of A
|
||||||
char uart_output; // To store received character
|
u_char uart_output; // To store received character
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
uart_transmit(&uart_input); // Pass by reference to change
|
uart_transmit(&uart_input); // Pass by reference to change
|
||||||
|
@ -82,7 +97,7 @@ int main() {
|
||||||
uart_receive(&uart_output); // Pass by reference to change
|
uart_receive(&uart_output); // Pass by reference to change
|
||||||
// value of uart_output during reception
|
// value of uart_output during reception
|
||||||
|
|
||||||
sleep_ms(SLEEP_TIME); // Sleep based on SLEEP_TIME
|
sleep_ms(SLEEP_TIME); // Sleep based on SLEEP_TIME
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue