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:
       char byte_0 = spi1_exchange_byte(0xDE);
       char byte_1 = spi1_exchange_byte(0xAD);

    * 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


Back to top