Sille Van Landschoot / Mbed 2 deprecated mbed_slave_full

Dependencies:   mbed

main.cpp

Committer:
dwini
Date:
2014-10-14
Revision:
4:f537311ddc53
Parent:
3:01b322df3731
Child:
5:b73ea174e997

File content as of revision 4:f537311ddc53:

#include "mbed.h"
#include "memory.h"

DigitalOut aLED(LED1);
Serial pc(USBTX, USBRX); // tx, rx
I2CSlave slave(p28, p27);

const int SLAVE_ADDRESS = 0x84;
const int I2C_FREQUENCY = 100000;
const int I2C_BUFFER_SIZE = 5;

enum COMMAND { PUSH, PULL, CLEAR, PRINT };

#define intToByte(pbytebuff,intval) (*((int*)(pbytebuff)) = intval)
#define byteToInt(pbytebuff,pintval) (*(pintval) = *((int*)(pbytebuff)))

int main() {
    pc.baud(115200);
    pc.printf("Size of integer is %d bytes\r\n", sizeof(int));
    
    // Alive LED
    int cAlive = 0;
    
    // Configure I2C
    slave.frequency(I2C_FREQUENCY);
    pc.printf("Slave is working @ %dHz\r\n", I2C_FREQUENCY);
    slave.address(SLAVE_ADDRESS);
    pc.printf("Slave is working @ SLAVE_ADDRESS = 0x%x\r\n", SLAVE_ADDRESS);

    // Setup memory
    Memory memory;
    pc.printf("Size of memory buffer is %d elements\r\n", Memory::MEMORY_SIZE);
    memory.print();
    pc.printf("Awaiting commands from master ...\r\n");

    // I2C buffer
    char buffer[I2C_BUFFER_SIZE];
    
    // Internal address pointer
    int pointer = 0;
    
    while (1) {
        int i = slave.receive();
        switch (i) {
            case I2CSlave::ReadAddressed:
            {
                int value = memory.get(pointer);
                buffer[0] = pointer;
                intToByte(buffer+1, value);
                if (!slave.write(buffer, I2C_BUFFER_SIZE)) {
                    pc.printf("Retrieving and sending to master %d@%d\r\n", value, pointer);
                } else {
                    pc.printf("Failed to send to master %d@%d\r\n", value, pointer);
                }
                break;
            }
            case I2CSlave::WriteAddressed:
            {
                if (!slave.read(buffer, I2C_BUFFER_SIZE)) {
                    // Check command byte
                    switch (buffer[0]) {
                        case PUSH:
                        {
                            int address = buffer[1];
                            int value;
                            byteToInt(buffer+2, &value);
                            pc.printf("Storing %d@%d\r\n", value, address);
                            memory.set(address, value);
                            break;
                        }
                        
                        case PRINT:
                        {
                            memory.print();
                            break;
                        }
                            
                        case PULL:
                        {
                            pc.printf("Setting pointer to %d\r\n", buffer[1]);
                            if (buffer[1] < Memory::MEMORY_SIZE) {
                                pointer = buffer[1];
                            } else {
                                pc.printf("Address out of boundary\r\n");    
                            }
                            break;
                        }
                        
                        default:
                        {
                            pc.printf("Unknown command byte\r\n");
                        }
                    }
                } else {
                    pc.printf("Received WriteAddressed] without data\r\n");
                }
                break;
            }
        }
        
        // Clear buffer
        for (int i = 0; i < I2C_BUFFER_SIZE; i++) {
            buffer[i] = 0;   
        }
        
        // Alive LED
        cAlive = (cAlive + 1) % 100000;
        if (!cAlive) {
            aLED = !aLED;
        }
    }
}