Lab Assignment (in C): GPIO

Objective

To grow your skills in the following:

  • Manipulating a microcontroller's registers in order to access and control physical pins
  • Use implemented driver to sense input signals and control LEDs

Assignment

Test your knowledge by doing the following:

Part 0. Basic GPIO Driver to blink an onboard LED

 void led_task(void *pvParameters) {
     //Choose one of the onboard LEDS by looking into schematics and write code for the below

    1) Set the PIN register bits for the LED
    2) Set the DIR register bit for the LED

    while(1) {
        3) Use CLR register to turn the LED ON (led may be active low)
        vTaskDelay(500);
 
        4) Use SET register to turn the LED OFF
        vTaskDelay(500);
    }
}

int main(void) {
  	//Create FreeRTOS LED task
    xTaskCreate(led_task, “led”, 1024/sizeof(void*), NULL, PRIORITY_LOW, NULL);

    vTaskStartScheduler();
    return 0;
}

Code Block 1: Simple LED Task

Part 1. GPIO Driver application

Part 1A. Implement the GPIO_0 Driver

Using the following template

  1. Implement ALL methods.
  2. All methods must work as expected as described in the comments above their method name.
#pragma once

#include <stdint.h>
#include <stdbool.h>

#include "lpc40xx.h"

/// Should alter the hardware registers to set the pin as input
void gpio0__set_as_input(uint8_t pin_num);

/// Should alter the hardware registers to set the pin as output
void gpio0__set_as_output(uint8_t pin_num);

/// Should alter the hardware registers to set the pin as high
void gpio0__set_high(uint8_t pin_num);

/// Should alter the hardware registers to set the pin as low
void gpio0__set_low(uint8_t pin_num);
    
/**
 * Should alter the hardware registers to set the pin as low
 *
 * @param {bool} high - true => set pin high, false => set pin low
 */
void gpio0__set(uint8_t pin_num, bool high);
    
/**
 * Should return the state of the pin (input or output, doesn't matter)
 *
 * @return {bool} level of pin high => true, low => false
 */
bool gpio0__get_level(uint8_t pin_num);

Code Block 2: GPIO Driver Template

Part 1B. Use Driver for an application

The application is to use an internal AND external switch to control an on board and external LED, respectively.

void led_task(void *task_param)
{
    /* Get Parameter */
    uint32_t param = (uint32_t)(task_param);
    /* Define Constants Here */

    /* Define Local Variables and Objects */
    
    /* Initialization Code */

    while(1) {
        /* Insert Loop Code */
    }
}

void switch_task(void * task_param)
{
    /* Get Parameter */
    uint32_t param = (uint32_t)(task_param);
    /* Define Constants Here */

    /* Define Local Variables and Objects */
    
    /* Initialization Code */

    while(1) {
        /* Insert Loop Code */
    }
}

Code Block 2: FreeRTOS Task Templates

Requirements:

You MUST NOT use any pre-existing library available in the drivers directory for this assignment. 

You MAY USE LPC17xx.h or LPC40xx.h as it is not a library but a list of registers mapped to the appropriate locations. 

  1. You MUST use the two task templates above, you MUST pass a parameter to these tasks to inform them which switch or led they are reading/controlling. Refer to the below link to understand how to pass a parameter https://www.freertos.org/a00125.html
  2. The task above must do the following:
    1. switch_task:  Will read from the internal OR external GPIO, depending on the input parameter, connected to the switches. If a button is RELEASED(not when pressed, but when it released), this task will set a global variable.
    2. led_task: Will loop and toggle the state of your designated LED, specified by the input parameter, and clear that global variable flag.
  3. Create another switch_task, but give it a different parameter such that it monitors a different switch
  4. Likewise, create another led_task, but give it a different parameter such that it manipulates a different LED
    • In total, you will need to create 4 tasks, two switch_task tasks, one for reading external switch and another for reading the internal switch two led_task tasks, one for toggling the internal and another for toggling the external LED.
    • Pass in a parameter to change the behavior of each task. See code block 2.
  5. Connect your own external LED and Switch to any of the pins on Port 0 and get the same functionality working as above.

Upload only relevant .c files into canvas. A good example is: main.c, lab_gpio_0.c. See Canvas for rubric and grade breakdown.

Extra Credit
Add a flashy easter egg feature to your assignment, with your new found LED and switch powers! The extra credit is subject to the instructor's, ISA's and TA's discretion about what is worth the extra credit. Be creative. Ex: Flash board LEDs from left to right or left to right when button pressed

Extra Credit
Be able to handle multiple ports (port 1 and port 2) within a single gpio driver file

  
Back to top