Production Test Program (PTP) for the LPC4088 Experiment Base Board

Dependencies:   EALib I2S LM75B SDFileSystem mbed

main.cpp

Committer:
embeddedartists
Date:
2014-08-25
Revision:
0:0d5190d379d3
Child:
1:47680ec5d783

File content as of revision 0:0d5190d379d3:

/******************************************************************************
 * Includes
 *****************************************************************************/
#include "mbed.h"

#include "MCIFileSystem.h"
#include "SDFileSystem.h"
#include "LM75B.h"
#include "MMA7455.h"
#include "WM8731.h"
#include "TextLCD.h"
#include "DmTftHX8353C.h"
#include "BubbleDemo.h"

/******************************************************************************
 * Typedefs and defines
 *****************************************************************************/

#define BUTTON_PRESSED   0
#define BUTTON_RELEASED  1

#define LED_ON   0
#define LED_OFF  1

#define handleError(__a, __b)  printf("Error: %s\n", (__b)); mbed_die()

#define waitForButtonClick()  do {while(button.read() == BUTTON_RELEASED); while(button.read() == BUTTON_PRESSED);} while(false)

/******************************************************************************
 * Local variables
 *****************************************************************************/

//MCIFileSystem mcifs("mci");//, P4_16);
SDFileSystem spifs(p5, p6, p7, p8, "spi"); // mosi, miso, sclk, cs

DigitalOut myled(LED1);
DigitalIn  button(p23);

SPI shiftreg(p5, p6, p7);  // mosi, miso, sclk,
DigitalOut shiftregCS(p30);

DigitalOut ledRed(p25);
DigitalOut ledGreen(p28);
DigitalOut ledBlue(p26);

/******************************************************************************
 * Local functions
 *****************************************************************************/

static void test_micro_sd_card_mci()
{
  FILE* fp = fopen("/mci/message.txt", "r");
  if (fp != NULL) {
    char buf[20];
    int num = fread(buf, 1, sizeof(buf), fp);
    if (num >= 10) {
      buf[10] = '\0';
      if (strcmp(buf, "eatest2014") == 0) {
        printf("MCI SD Card works!\n");
        fclose(fp);
        return;
      }
      handleError(STATE_ERR_MCIFS, "Invalid data read from /mci/message.txt\n");
    }
    handleError(STATE_ERR_MCIFS, "Failed to read >= 10 bytes from /mci/message.txt\n");
  }
  handleError(STATE_ERR_MCIFS, "Failed to open /mci/message.txt\n");
}  

static void test_micro_sd_card_spi()
{
  FILE* fp = fopen("/spi/message.txt", "r");
  if (fp != NULL) {
    char buf[20];
    int num = fread(buf, 1, sizeof(buf), fp);
    if (num >= 10) {
      buf[10] = '\0';
      if (strcmp(buf, "eatest2014") == 0) {
        printf("SPI SD Card works!\n");
        fclose(fp);
        return;
      }
      handleError(STATE_ERR_SPIFS, "Invalid data read from /spi/message.txt\n");
    }
    handleError(STATE_ERR_SPIFS, "Failed to read >= 10 bytes from /spi/message.txt\n");
  }
  handleError(STATE_ERR_SPIFS, "Failed to open /spi/message.txt\n");
}  

static void test_acc()
{
  MMA7455 sensor(P0_27, P0_28);
  
  //Try to initialize the accelerometer
  if (!sensor.setMode(MMA7455::ModeMeasurement)) {
     handleError(STATE_ERR_ACC, "Unable to set mode for MMA7455!\n");
  }
  
  printf("Put on flat surface and press button to calibrate!\n");
  while (button.read() != BUTTON_PRESSED) {}
  while (button.read() == BUTTON_PRESSED) {}
    
  if (!sensor.calibrate()) {
     handleError(STATE_ERR_ACC, "Failed to calibrate MMA7455!\n");
  }
  
  printf("Printing values for the next 5 seconds...\n");
  int x=0, y=0, z=0;
  
  Timer t;
  t.start();
  while (t.read() < 6) {
    if (!sensor.read(x, y, z)) {
      handleError(STATE_ERR_ACC, "Failed to read accelerometer data!\n");
    }
    printf("ACC: x,y,z = {%5d, %5d, %5d}\n", x, y, z);
  }
  printf("Done with ACC tests!\n");
}

static void test_lm75()
{
  //Create an LM75B object at 0x92/0x93 (ADDRESS_1)
  LM75B sensor(P0_27, P0_28, LM75B::ADDRESS_1);
  
    //Try to open the LM75B
    if (sensor.open()) {
        printf("LM75 Device detected!\n");
 
        int i = 10;
        while (i--) {
            //Print the current temperature
            printf("Temp = %.3f\n", (float)sensor);
 
            //Sleep for 0.5 seconds
            wait(0.5);
        }
    } else {
        handleError(STATE_ERR_LM75, "LM75 Device not detected!\n");
    }  
}

static void test_trimpot()
{
  AnalogIn trimpot(p15);

  printf("Reading trimpot for 5 seconds...\n");  

  for (int i = 0; i < 50; i++) {
    printf("Trimpot = %.3f\n", trimpot.read());
    wait(0.1);
  }  
}

static void outputShiftReg(uint8_t val)
{
    shiftregCS = 0;  
    shiftreg.write(val);
    shiftregCS = 1;
}
static void test_shiftreg()
{
   outputShiftReg(0x1);
   wait(0.1);
   outputShiftReg(0x2);
   wait(0.1);
   outputShiftReg(0x4);
   wait(0.1);
   outputShiftReg(0x8);
   wait(0.1);
   outputShiftReg(0x10);
   wait(0.1);
   outputShiftReg(0x20);
   wait(0.1);
   outputShiftReg(0x40);
   wait(0.1);
   outputShiftReg(0x80);
   wait(0.1);
   outputShiftReg(0x40);
   wait(0.1);
   outputShiftReg(0x20);
   wait(0.1);
   outputShiftReg(0x10);
   wait(0.1);
   outputShiftReg(0x8);
   wait(0.1);
   outputShiftReg(0x4);
   wait(0.1);
   outputShiftReg(0x2);
   wait(0.1);
   outputShiftReg(0x1);
   wait(0.1);
   outputShiftReg(0x0);
}
    

static void test_joystick()
{
  DigitalIn up(p32);//p39);
  DigitalIn down(p38);//p32);
  DigitalIn left(p39);//p38);
  DigitalIn right(p37);
  DigitalIn center(p31);

  printf("Reading joystick for 10 seconds or all directions...\n");  
  uint16_t mask = 0;

  for (int i = 0; i < 100; i++) {
    bool line = false;
    if (up.read() == 0) { printf("UP ");        line = true; mask |= 0x01; }
    if (down.read() == 0) { printf("DOWN ");    line = true; mask |= 0x02; }
    if (left.read() == 0) { printf("LEFT ");    line = true; mask |= 0x04; }
    if (right.read() == 0) { printf("RIGHT ");  line = true; mask |= 0x08; }
    if (center.read() == 0) { printf("CENTER ");line = true; mask |= 0x10; }
    if (line) {
      printf("\n");
    }
    if (mask == 0x1F) {
        printf("All directions tested. Done!\n");
        break;
    }
    wait(0.1);
  }  
}

static void test_rgb()
{
  printf("All off. Press button to continue\n");
  ledRed = LED_OFF;
  ledGreen = LED_OFF;
  ledBlue = LED_OFF;
  waitForButtonClick();

  printf("RED on. Press button to continue\n");
  ledRed = LED_ON;
  ledGreen = LED_OFF;
  ledBlue = LED_OFF;
  waitForButtonClick();
  
  printf("GREEN on. Press button to continue\n");
  ledRed = LED_OFF;
  ledGreen = LED_ON;
  ledBlue = LED_OFF;
  waitForButtonClick();
  
  printf("BLUE on. Press button to continue\n");
  ledRed = LED_OFF;
  ledGreen = LED_OFF;
  ledBlue = LED_ON;
  waitForButtonClick();
  
  printf("All on. Press button to continue\n");
  ledRed = LED_OFF;
  ledGreen = LED_OFF;
  ledBlue = LED_OFF;
  waitForButtonClick();
}

static void test_audio()
{
    WM8731 audio(P0_27, P0_28);
    if (!audio.writeCmd(WM8731::REG_R15_RESET, 0x0000)) {
        handleError(STATE_ERR_AUDIO, "Failed to send command to audio codec\n");
    }
}

static void test_TextLCD()
{
    SPI spi_lcd(p5, p6, p7); // MOSI, MISO, SCLK
    TextLCD_SPI lcd(&spi_lcd, p30, TextLCD::LCD16x2); // SPI bus, CS pin, LCD Type ok
    
    lcd.cls();
    lcd.locate(0, 0);
    lcd.putc('E');
    lcd.putc('m');
    lcd.putc('b');
    lcd.putc('e');
    lcd.putc('d');
    lcd.putc('d');
    lcd.putc('e');
    lcd.putc('d');
    lcd.putc(' ');
    lcd.putc('A');
    lcd.putc('r');
    lcd.putc('t');
    lcd.putc('i');
    lcd.putc('s');
    lcd.putc('t');
    lcd.putc('s');
    lcd.putc('!');
    lcd.setCursor(TextLCD::CurOff_BlkOn);
}

static void test_DisplayModule()
{
    // Test of DM-TFT18-101 display (http://www.displaymodule.com/collections/featured-modules/products/dm-tft18-101)
    DmTftHX8353C tft(p5, p7, p30, p17, p16); // mosi, clk, cs, dc, rst
    tft.init();
    
    BubbleDemo bubbleDemo(&tft, tft.width(), tft.height());
    bubbleDemo.run(750, 20);
}

int main() {
    printf("hello! " __DATE__ " at " __TIME__ "\n");
    
    shiftregCS = 1;    
    button.mode(PullUp);
    
    ledRed = LED_OFF;
    ledGreen = LED_OFF;
    ledBlue = LED_OFF;
      
    outputShiftReg(0x0);
    
     //startup flash behaviour
     for(int i=0; i<10; i++)
     {
       ledRed = LED_ON;
       wait(0.05);
       ledRed = LED_OFF;
       wait(0.05);
     }
     wait(1.0);
     for(int i=0; i<10; i++)
     {
       ledGreen = LED_ON;
       wait(0.05);
       ledGreen = LED_OFF;
       wait(0.05);
     }
     wait(1.0);
     for(int i=0; i<10; i++)
     {
       ledBlue = LED_ON;
       wait(0.05);
       ledBlue = LED_OFF;
       wait(0.05);
     }
     wait(1.0);    

    //test_micro_sd_card_mci();
//    test_micro_sd_card_spi();
    test_acc();
    test_lm75();
    test_trimpot();
    test_joystick();
    test_rgb();
    test_audio();
    //test_TextLCD();
    //test_DisplayModule();
    waitForButtonClick();

    while(1) {
        test_shiftreg();
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}