SJOne Board

Introduction

How to obtain the Board

About the Board

  • LPC1758 User Manual
  • File:2012SJOneBoardSchematic.pdf
  • 512K ROM, 64K(96K) RAM, 1M(2M) SPI Flash, and Micro-SD for storage.
    Arm Cortex-M4 variant contains 96K RAM, and 2Mb SPI flash
  • Built-in Nordic Wireless (Board-to-Board communication)
    Software Stack for Mesh Network
  • 4 Switches and 4 LEDs (both hard-wired)
  • Sensors :
    Temperature, 3-Axis Acceleration, IR (remote control), and Light
  • 2-Digit 7-Segment Display
  • RTC Crystal with Backup Battery
  • Socket for Xbee or Wifi Module (Uart2 or Uart3)
  • Many GPIOs with two SPI, Multiple UARTs, and I2C availability
  • Power from USB or External Power

Getting Started

Basic IO

GPIO 

#include "gpio.hpp"
#include "utilities.h"

int main(void)
{
	/* Use P1.20 as General Purpose Input/Output (GPIO) */
    GPIO pin20(P1_20);
    /* Use this pin as OUTPUT */
    pin20.setAsOutput(); 
	/* Turn on voltage to 3.3v */
    pin20.setHigh(); 
    /* Turn off voltage to 0v  */
    pin20.setLow();  
}

ADC Input

#include "adc0.h"
 
void adc_read(void)
{
    /*********************************************************
     * ADC is already initialized before main() is called.
     * There are 3 ADC pins labeled on the SJ-One board.
     * You can use one or more channels as illustrated below.
     */

    LPC_PINCON->PINSEL1 |= (1 << 20); // ADC-3 is on P0.26, select this as ADC0.3
    LPC_PINCON->PINSEL3 |= (3 << 28); // ADC-4 is on P1.30, select this as ADC0.4
    LPC_PINCON->PINSEL3 |= (3 << 30); // ADC-5 is on P1.31, select this as ADC0.5

    int adc3 = adc0_get_reading(3); // Read the value of ADC-3
    int adc4 = adc0_get_reading(4); // Read the value of ADC-4
    int adc5 = adc0_get_reading(5); // Read the value of ADC-5
}

PWM and Servo Control

#include "lpc_pwm.hpp"

/**
 * You can control up to 6 servos with hardware signals (and more with sw)
 * Each signal is mapped to from P2.0 to P2.5
 */
void motor_control()
{
    /* Use 1Khz PWM.  Each PWM shares the 1st frequency you set */
    PWM motor1(PWM::pwm1, 1000);
    PWM motor2(PWM::pwm2, 0);

    /* Set to 50% motor speed */
    motor1.set(50);
    motor2.set(50);
}
void servo_control()
{
    /* Use 50Hz PWM for servos.  Each PWM will be 50Hz */
    PWM servo1(PWM::pwm1, 50);
    PWM servo2(PWM::pwm2, 0);

    servo1.set(5.0);  ///< Set to left position
    servo2.set(10.0); ///< Set to right position
}

Serial Communication Libraries

UART Driver

#include "uart2.hpp"
#include "uart3.hpp"

void uart()
{
    Uart2& u2 = Uart2::getInstance();
    Uart3& u3 = Uart3::getInstance();

    u2.init(38400); /* Init baud rate */
    u3.init(19200); /* Init baud rate */

    u2.putline("Hello World\n");
    u3.putline("Hello World\n");

    /* Reference the documentation of Uart2 or Uart3 header file for more functions.
     * Most of the functionality is in a base class called uart_dev.cpp
     */
}

SPI Driver

To hook up your external SPI device(s), use SPI#1 connections because there is already a driver in SJ-One sample project for this SPI. See the connections below and the sample code:

#include "spi1.h"

void access_my_spi_device()
{
   // Send 0xDEAD over to SPI device and get 2 bytes back:
   chip_select_my_device(true);
   {
       char byte_0 = spi1_exchange_byte(0xDE);
       char byte_1 = spi1_exchange_byte(0xAD);
   }
   chip_select_my_device(false);

   /**
    * You can use any GPIO for CS (chip-select) signal.
    * This example assumes CS is done through a function: 
    *   chip_select_my_device(bool); 
    */
}

 

Figure 1. SJ One Board SPI

I2C Driver

I2C#2 is tied to on-board sensors and you should utilize I2C 2's connection to hook up external I2C devices. See the connections below and the sample code:

#include "I2C2.hpp"

void send_byte_to_my_i2c_device()
{
	const char my_dev_addr = 0xBA; // Your device address
	const char my_dev_reg = 0x01; // Write to 1st register of your device
	const char my_dev_data = 0xAB; // Write 0xAB to reg 0x01
	I2C2::getInstance().writeReg(my_dev_addr, my_dev_reg, my_dev_data);
}

 

Figure 2. SJ One Board I2C

 

Internal Component Libraries

LEDs, Switches & LED Display

There are on-board switches and LEDs you may use. Furthermore, you can interface your board to external LEDs or switches as well.

#include "io.hpp"

void led_sw()
{
    /* Check if button 1 is pressed */
	if (SW.getSwitch(1))   
	{
        /* Turn on LED # 1 */
		LE.on(1);          
        /* LED display will show "1" */
		LD.setNumber(1);   
	}
	else
	{
        /* Turn off all LEDs */
		LE.setAll(0); 
        /* Clear the display */
		LD.clear();   
	}
}

Sensors

This section provides examples of how to read data values from the sensors.

#include "io.hpp"

void sensors()
{
    /* Read light sensor */
    int light_value = LS.getRawValue();
	/* Read from accelerometer */
    int tilt_x = AS.getX();
    int tilt_y = AS.getY();
    int tilt_z = AS.getZ();
	/* Read from temperature sensor */
    int temperature_f = TS.getFarenheit();
}

 

 

 

 

 

 

External Components

ADC Ultrasonic Proximity Sensor

#include “adc0.h”

int main(void)
{
    int reading = 0;

    // Initialization :
    LPC_PINCON->PINSEL3 |=  (3 << 28); // ADC-4 is on P1.30, select this as ADC0.4

    while (1)
    {
        reading = adc0_get_reading(4); // Read current value of ADC-4
        printf("\nADC Reading: %d", reading);
        delay_ms(1000);
    }

    return 0;
}

Buzzer

A buzzer needs a PWM to operate, or you can operate it using a GPIO but it can waste your CPU cycles. Before you start with the code, attach a GPIO or PWM pin to a 1K resistor leading to the buzzer's red wire. The black wire should be connected to ground.

#include "gpio.hpp"
#include "utilities.h"

int main(void)
{
	/* Assume we attached our P1.20 to the buzzer */
	GPIO buzzer(P1_20);   /* Use P1.20 as General Purpose Input/Output (GPIO) */
	buzzer.setAsOutput(); /* Use this pin as OUTPUT */

	while (1)
	{
		/* Beep for one second */
		for (int i = 0; i < 1000; i += 2)
		{
			delay_ms(1);
			buzzer.setHigh();
			delay_ms(1);
			buzzer.setLow();
		}
	}
}

Motion Sensor

A typical motion sensor like a PIR motion sensor is very easy to attach. Power-up the sensor using 5v, and connect the ground. Then take a resistor, tie one end to 5v, and the other end to the motion's signal. Finally, tie the signal to one of your GPIOs.

#include "gpio.hpp"
#include "utilities.h"

int main(void)
{
	/* Assume we attached our P1.20 to out motion sensor's output pin */
	GPIO motion(P1_20);   /* Use P1.20 as General Purpose Input/Output (GPIO) */
	motion.setAsInput();
	motion.enablePullUp();

	while (1)
	{
		/* Get average of one second */
		int count = 0;
		for (int i = 0; i < 1000; i++)
		{
			/* If output is low, then motion has been detected, but it could be false positive */
			if (!motion.read())
			{
				count++;
			}
			delay_ms(1);
		}
		/* Test and calibrate yourself: */
		bool motionDetected = (count > 50);
	}
}

Debugging a crash

Debugging a Crash

When the crash happens, it will print some useful info upon next boot, and it will also tell you the last running task before the crash happened (if you are running FreeRTOS). The next clues can be determined by doing this:

  • Note down the address of PC and LR from the crash info printout
  • Open *.lst file from the _build folder, and search for last four digits of PC. That is where the crash happened.
  • LR is the "return address" of the previously called function. But I think if you see "0x123F", search for "1240" or "123C" because I think you have to subtract one from the LR value so that it lies on 4 byte boundary.

Data logging

One of the best ways to debugging a project is to log useful information. There is a logging facility available and it is best to use FreeRTOS that provides optimized operation. See "file_logger.h" for more details and parameters.

#include "file_logger.h"

void foo()
{
    LOG_INFO("Some info");
    LOG_WARN("Some warning");
    LOG_ERROR("Some error happened, value is: %u", 123);

    /* All the info is logged to a file (log.csv)
     *  You can open this file using terminal command "cat log.csv"
     *  You can also modify the destination to the SD card by defining
     *  logging filename to "1:log.csv" at file_logger.h
     */
}

FreeRTOS

FreeRTOS

void vTaskCode( void * pvParameters )
{
    /* Grab Parameter */
    uint32_t c = (uint32_t)(pvParameters);
    /* Define Constants Here */
    const uint32_t COUNT_INCREMENT = 20;
    /* Define Local Variables */
    uint32_t counter = 0;
    /* Initialization Code */
    initTIMER();
    /* Code Loop */
    while(1)
    {
        /* Insert Loop Code */
    }
    /* Only necessary if above loop has a condition */
    xTaskDelete(NULL);
}

int main(int argc, char const *argv[])
{
    //// You may need to change this value.
    const uint32_t STACK_SIZE = 128;
    xReturned = xTaskCreate(
                    vTaskCode,       /* Function that implements the task. */
                    "NAME",          /* Text name for the task. */
                    STACK_SIZE,      /* Stack size in words, not bytes. */
                    ( void * ) 1,    /* Parameter passed into the task. */
                    tskIDLE_PRIORITY,/* Priority at which the task is created. */
                    &xHandle );      /* Used to pass out the created task's handle. */

    /* Start Scheduler */
    vTaskStartScheduler();

    return 0;
}

C++ Scheduler Task

/**
 * Add this on top of main()
 * Read "scheduler.task" for more documentation
 * Read "examples.cpp" for more examples.
 */
class myTask : public scheduler_task
{
public:
	myTask (uint8_t priority) : scheduler_task("mytask", 512 * 8, priority)
	{
	}

	/* this function will be called in a loop by FreeRTOS */
	bool run(void *p)
	{
		puts("Hello");
		vTaskDelay(1000);
		return true; /* Returning false will suspend the task */
	}
};

int main(void)
{
	// ...
	/* Add your new task here */
	scheduler_add_task(new myTask(PRIORITY_LOW));
	// ...
	scheduler_start(false);
	// ...
}

Services

Timer Services

Getting system time using C libraries is an option, but you can also get time directly from "rtc.h". Other than this, there are two more timer services you can use as demonstrated in the code sample below.

#include "lpc_sys.h"
#include "soft_timer.hpp"

void timers()
{
	/* You can get the time since the board has been powered */
	uint64_t uptime_ms = sys_get_uptime_ms();
	uint64_t uptime_us = sys_get_uptime_us();

	/* There is also a polling timer available in C++, read "soft_timer.hpp" for more info. */
	SoftTimer myTimer(1000);

	if (myTimer.expired())
	{
		/* Do something */
	}
}

File I/O

You can read or write files on the SPI Flash or an SD card. You can open a limited amount of files using standard C libraries. First, at your sys_config.h file, please enable ENABLE_C_FILE_IO

#include "io.hpp"

void file_io()
{
	/* Option 1 : C library I/O (less efficient)
	 * 0: is for SPI Flash
	 * 1: is for SD Card
	 */
	FILE *fd = fopen("1:myfile.txt", "r");
	char line[128] = { 0 };
	if (fd)
	{
		fgets(line, sizeof(line) - 1, fd);
		fclose(fd);
	}

	/* Option 2 : Use "storage" object (more efficient)
	 * This option doesn't require 'ENABLE_C_FILE_IO'
	 */

	/* Write "hello" to "myfile.txt" */
	Storage::write("1:myfile.txt", "hello", 5, 0))

	/* Read the size of data array from myfile.txt
	 * Not using 0: or 1: will default to 0: (SPI Flash)
	 */
	char data[16] = { 0 };
	Storage::read("1:myfile.txt", data, sizeof(data) - 1, 0));

	/* Option 3 : Directly use ff.h API that will
	 * read/write SD card or SPI Flash
	 * Read documentation at : http://elm-chan.org/fsw/ff/00index_e.html
	 */
}

Command Line Interface

Add a Terminal Command

/* At terminal.cpp, add the following code at the taskEntry() function */
bool terminalTask::taskEntry()
{
	// ...
	CMD_HANDLER_FUNC(myCmdHandler);
	cp.addHandler(myCmdHandler,   "newcmd",  "This is help message. 'newcmd test' will display: 'OK'");
	// ...
	return true;
}

/* ----------------------------------------------
 * Your source file, such as "my_source.cpp"
 * We will add our command handler function here
 */
#include "command_handler.hpp"

CMD_HANDLER_FUNC(myCmdHandler)
{
	/* cmdParams is a str passed to you after user's command.
	 * If command was "newcmd test 123" then cmdParams will be "test 123".
	 *
	 * output is a CharDev class where the command came from, so
	 * we can use this to output a reply message.
	 * See "handlers.cpp" for more examples
	 */
	if (cmdParams == "test")
	{
		output.printf("OK!\n");
	}
	else
	{
		output.printf("ERROR for my command\n");
	}
	/* return false will display command's help to the user */
	return true; /* return true if command was successful */
}

Adding Additional Source Code

C++ File

Adding C++ code is simple. Simply add your *.hpp and *.cpp files and you are good to go! The file extensions must be *.hpp or *.cpp

C File

Adding C source code is a bit more hassle since compiling with mixed C/C++ requires some extra headers, so please use the following code as template for your header file. The *.c file doesn't require any extra stuff though.

#ifndef MY_NEW_FILE_H__
#define MY_NEW_FILE_H__

/* need the C++ header */
#ifdef __cplusplus
extern "C"
#endif



/* Now include your function headers */



#ifdef __cplusplus
}
#endif
#endif /* end MY_NEW_FILE_H__ */

Block Diagrams

Board Block Diagrams

The block diagrams below show the connectivity to various different chips on the PCB, and also show which GPIOs are available to you. The first diagram shows the pins used for on-board sensors or interfaces. The second diagram shows IOs you can use. 

Board Connections

 
SJ One Board Connections

Board IO

 
SJ One Board IO

Board Overlay

This board overlay can be compared against diagrams above to get an idea of where the IOs are located.