Subversion Repositories Code-Repo

Rev

Rev 197 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

#include <xc.h>
#include <string.h>
#include "main.h"
#include "TIMER.h"
#include "INTERRUPT.h"
#include "UART.h"
#include "I2C.h"

// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = ON       // PLL Enable (4x PLL disabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
// </editor-fold>

UART_DATA uart_data;
I2C_DATA i2c_data;

void Error(char ID) {
    while(1) {
        for (char i = 0; i < ID; i++) {
            LED_LAT = 1;
            __delay_ms(120);
            LED_LAT = 0;
            __delay_ms(150);
        }
        __delay_ms(1000);
    }
}

void Startup_Check(void) {
    char buffer[20];
    char result, length;

    BLE_RESET_LAT = 0;
    __delay_ms(3000);
    BLE_RESET_LAT = 1;
    __delay_ms(200);

    // Check BLE Module
    length = UART_Read(buffer);
    if (memcmp(buffer, "\r\nBR-LE4.0-S2\r\n", 15)) {
        Error(1);
    }
    UART_Write("AT\r", 3);
    __delay_ms(10);
    length = UART_Read(buffer);
    if (memcmp(buffer, "\r\nOK\r\n", 6)) {
        Error(1);
    }

    // Check Battery Gauge
    I2C_Master_Restart(ADDRESS_LIPO, 0x0C, 2);
    do {
        result = I2C_Get_Status();
    } while (!result);
    if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
        Error(2);
    }
    length = I2C_Read_Buffer(buffer);
    if ((buffer[0] != 0x97) || (buffer[1] != 0x00) || (length != 2)) {
        Error(2);
    }
    
    // Check Gyroscope
    I2C_Master_Restart(ADDRESS_GYRO, 0x0F, 1);
    do {
        result = I2C_Get_Status();
    } while (!result);
    if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
        Error(3);
    }
    length = I2C_Read_Buffer(buffer);
    if ((buffer[0] != 0xD4) || (length != 1)) {
        Error(3);
    }

    // Check Accelerometer
    I2C_Master_Restart(ADDRESS_ACCL, 0x20, 1);
    do {
        result = I2C_Get_Status();
    } while (!result);
    if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
        Error(4);
    }
    length = I2C_Read_Buffer(buffer);
    if ((buffer[0] != 0x07) || (length != 1)) {
        Error(4);
    }

    // Check Magnometer
    I2C_Master_Restart(ADDRESS_MAGN, 0x0A, 1);
    do {
        result = I2C_Get_Status();
    } while (!result);
    if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
        Error(4);
    }
    length = I2C_Read_Buffer(buffer);
    if ((buffer[0] != 0x48) || (length != 1)) {
        Error(4);
    }
}

int main() {

    OSCCON = 0x70; // PLL disabled, 8MHz
    ANSELA = 0;
    ANSELB = 0;
    ANSELC = 0;

    LED_TRIS = 0;
    LED_LAT = 0;

    BLE_RESET_TRIS = 0;
    BLE_RESET_LAT = 0;

    BLE_SLEEP_TRIS = 0;
    BLE_SLEEP_LAT = 0;

    BLE_MODE_TRIS = 0;
    BLE_MODE_LAT = 0;

    TIMER_1_Init();
    UART_Init(&uart_data);
    I2C_Init(&i2c_data);
    INTERRUPT_Init();

    I2C_Configure_Master(I2C_100KHZ);
    INTERRUPT_Enable();
    
    Startup_Check();

    TIMER_1_Start();

    while(1);
}