CS428 Embedded Systems Lab


Assignment 4: Peripheral Monitor

The goal of this assignment is to help you understand I2C and to allow you control over input/output ports. 

1. Get a lego kit from the lab

Return the components you have used for the previous assignment. For this assignment you will need the lego nxt controller, the charger for the controller battery, a usb cable, and the following sensors: light, mic, touch, motor. Follow the same procedure as for the previous assignments.

Please refer
to the Assignments page for information about forking the assignment repo. Create a file as4_readme.txt that will contain to any questions below and your remarks about your implementation.

2. I2C

You are given the following API: 

 

void I2CInit(void);

void I2CExit(void);

void I2CTransfer(void);

void I2CCtrl(enum power_t);

    


2.1 Make sure your main calls the given HardwareInit() function. Why is this necessary? 

2.2. Implement function I2CTransfer. This function initiates a send or receive transaction over I2C. It can be called as frequently as the program wants, however, a transfer will initiate only after TIMEOUT time units (e.g. 2ms) have elapsed from the previous transfer. The function alternates between send and receive transactions. Each send or receive transaction sends or receives the corresponding buffer (IoToAvr, IoFromAvr). The first transaction that is performed by I2CTransfer() has to be the special "hello" transaction. I2CTransfer() only intitiates a transaction using the functions DataTxInit() and DataRxInit(). Each of these functions performs the actual transaction initiation. The transaction will continue in an interrupt handler when after each byte is sent or received. Remember that besides the data, each transaction needs to use a checksum. Also, the interrupt handler will need to deal with possible errors. The implementation of I2CTransfer, DataTx/RxInit, and the handler are essentially a state machine that you will need to design and implement. 

2.3 Implelment function I2CCtrl(). This function sets the power parameter/mode in AVR and allows the controller to run in regular mode, sets the controller to reprogram mode, or powers the controller down.


3. Buttons

You are given the following API: 

 

void   ButtonInit(void);

void   ButtonExit(void);

enum button_t  ButtonRead(void);

    


3.1 Implelment this API. ButtonInit simply initializes the button variable in IoFromAvr. ButtonRead() returns the last button that was pressed when the last I2C transfer from AVR to ARM occured. 

3.2 Modify your firmware to display on the LCD the button pressed. 

3.3 Fill in led.c to turn on/off the led in the light sensor when buttons are pressed. 

3.4 Modify your firmware to go in reprogram mode when pressing the ENTER button and to shutdown when pressing the EXIT button.


4. Input Ports

You are given the following API: 


void InputInit(void);

void InputExit(void);

void InputGetSensorValue(UWORD *value, UBYTE port);

    


4.1 Implement InputInit(). This function remembers in a structure which sensor is connected to which input port. This configuration is static and you will need to modify this function whenever you change sensor connectivity. 

4.2 Implement InputGetSensorValue(). This function returns the value that was last read from AVR for the specified port. 

4.3 Modify your firmware to display the sensor names and the current value for each sensor.


5. Output Ports

You are given the following API: 

    

void      OutputInit(void);

void      OutputExit(void);

void      OutputSetSpeed (UBYTE MotorNr, SBYTE Speed);

    


5.1 Implement OutputInit(). This function merely initializes IoToAvr. 

5.2 Implement OutputSetSpeed(); This function sets in IoToAvr the correct values for each motor. 

5.3 Modify you firmware to display the speed set for each motor.


6. Extra credit

6.1 For each sensor provide functions that return meaningful values. E.g. for the touch sensor TRUE when pressed and FALSE otherwise (easy), for the light sensor values that correspond to light intensity and specific colors (via their light intensity in a specific environment), for the mic to recognize specific sounds (in noisy environments too).
6.2 Write simple functions that would move the lego forward/reverse and turn it left/right using two motors (to try this you will need to build a simple moving project -- go for it!).
6.3 Make the sensors "interract" e.g. the led to turn on/off on specific sounds or the motors to start/stop based on specific sensor input/commands. 

7. Submit

Submit all files required to run the assignment, including your documentation file as4_readme.txt with all your answers and remarks. Please refer to the policies for information about the submission process. Also, follow the procedure outlined in class for returning the components you used in this assignment. 

___________________________________________________________________________________________________________________________________
(c) Copyright University of Crete, Greece, Last Modified: 07-Feb-2022