Joel Pallent / Mbed 2 deprecated Muscle_Controlled_Servo

Dependencies:   mbed

Fork of Muscle_Controlled_Servo by Ben Gordon

SPI.cpp

Committer:
BenRJG
Date:
2018-04-03
Revision:
4:bcef9164776e
Parent:
3:d6e142b6ead1
Child:
5:11489c0bd020

File content as of revision 4:bcef9164776e:

#include "SPI.h"

SPI spi(PA_7, PA_6, PA_5);      // Ordered as: mosi, miso, sclk could use forth parameter ssel
                                // However using multi SPI devices within FPGA with a seperate chip select
SPI spi_cmd(PA_7, PA_6, PA_5);  // NB another instance call spi_cmd for 8 bit SPI dataframe see later line 37
                                // For each device NB PA_7 PA_6 PA_5 are D11 D12 D13 respectively
                                
//DigitalOut LED_cs(PC_6);        // Chip Select for Basic Outputs to illuminate Onboard FPGA DEO nano LEDs CN7 pin 1
//DigitalOut LCD_cs(PB_15);       // Chip Select for the LCD via FPGA CN7 pin 3
//DigitalOut ADC_cs(PB_9);        // Chip Select for the ADC via FPGA CN7 pin 4

BusOut cs(PB_9,PB_15,PC_6);     // Chip Select => 111(7) = Nothing, 110(6) = General, 101(5) = LCD, 011(3) = ADC

//NBB the following line for F429ZI !!!!
DigitalIn DO_NOT_USE(PB_12);    // MAKE PB_12 (D19) an INPUT do NOT make an OUTPUT under any circumstances !!!!! ************* !!!!!!!!!!!
                                // This Pin is connected to the 5VDC from the FPGA card and an INPUT is 5V Tolerant

int adval_d;            //A to D value read back
float adval_f;
int err;                //error variable used for debugging, trapping etc.,

char adval[32];
//Ticker ticktock;

void spi_write_data(short destination, unsigned int data)
{
    cs = destination;             //Select the device by seting chip select LOW
    spi_cmd.write(0);
    spi.write(data);
    cs = CS_RESET;             //De-Select the device by seting chip select HIGH
}

int spi_read_data(short destination)
{
    cs = destination;             //Select the device by seting chip select LOW
    unsigned int read = spi.write(0);
    cs = CS_RESET;             //De-Select the device by seting chip select HIGH
    return read;
}

int spi_read_write_data(short destination, unsigned int data)
{
    cs = destination;             //Select the device by seting chip select LOW
    spi_cmd.write(0);
    unsigned int read = spi.write(data);
    cs = CS_RESET;             //De-Select the device by seting chip select HIGH
    return read;
}



void SPI_INIT (void)
{
    cs = CS_RESET;                     // Chip must be deselected, Chip Select is active LOW
    
    spi.format(16,0);           // Setup the DATA frame SPI for 16 bit wide word, Clock Polarity 0 and Clock Phase 0 (0)
    spi_cmd.format(8,0);        // Setup the COMMAND SPI as 8 Bit wide word, Clock Polarity 0 and Clock Phase 0 (0)
    spi.frequency(1000000);     // 1MHz clock rate
    spi_cmd.frequency(1000000); // 1MHz clock rate
    
    adval_d = 0;            //A to D value read back
    adval_f =0.0f;
    err = 0;                //error variable used for debugging, trapping etc.,
    
// Preload some arrays
//    char hello_world[]="Hello World";
    char splash_screen1[]="Martin Simpson";
    char splash_screen2[]="Plymouth UNI";
    char DVM[]="Voltage=";
// Start up sequences
    lcd_cls();
    lcd_locate(1,1);
    lcd_display(splash_screen1);    //Credit line 1
    lcd_locate(2,2);
    lcd_display(splash_screen2);    //Credit line 2
    wait(2);
    lcd_cls();  
    lcd_locate(1,0);
    lcd_display(DVM);   //Type Voltage display
    lcd_locate(1,13);
    lcd_display("V");   //Units display
}

void SPI_TEST(void)
{
    adval_d = read_adc();
    
    adval_f = 3.3f*((float)adval_d/4095);//Convert 12 bit to a float and scale
    sprintf(adval,"%.3f",adval_f);       //Store in an array string
    lcd_locate(1,8);                     //and display on LCD
    lcd_display(adval);                  //
    
    err = bar_graph(adval_d/255);       // 16*256 =4096 12 bit ADC!
    if (err < 0){printf("Display Overload\r\n");}
    
    read_switches();
    //LED Chaser display KIT lives on!
    for (uint32_t i=1;i<=128;i*=2)
    {
        spi_write_data(CS_GENERAL,i);             //Send data through spi
        wait_ms(20);
    }
    for (uint32_t i=128;i>=1;i/=2)
    {
        spi_write_data(CS_GENERAL,i);             //Send data through spi
        wait_ms(20);
    }
}
//////////////////////////////////////////////////////////
int lcd_cls(void){
    spi_write_data(CS_LCD,0x0001);
    wait_us(CD);    //Clear Screen
    return 0;
}

int lcd_locate(uint8_t line, uint8_t column){
    uint8_t line_addr;
    uint8_t column_addr;
    switch(line){
        case 1: line_addr=0x80; break;
        case 2: line_addr=0xC0; break;
        default: return -1; //return code !=0 is error
        }
    if(column<16){column_addr=column;}
    else{return -1;}
    spi_write_data(CS_LCD,(line_addr+column_addr));
    wait_us(CD); //DDRAM location Second line is 0x00C0 first line starts at 0x0080
    return 0;
}

int lcd_display(char* str){
    
    if (strlen(str)>16){return -1;} //return code !=0 is error
    
    uint8_t command_data=1;
    uint32_t wait_time;
 
    switch(command_data){
        case 0: wait_time=DD; break;
        case 1: wait_time=CD; break;
        default: return -1;
        }

    for (int i=0; i<strlen(str);i++){
        spi_write_data(CS_LCD,((command_data<<8)+str[i]));
        wait_us(wait_time);
    }
    return 0;
}

int bar_graph(uint8_t level){
    if (level>16){return -1;} //return code !=0 is error
    spi_write_data(CS_LCD,0x00C0);
    wait_us(CD); //DDRAM location Second line is 0x00C0 first line starts at 0x0080
    for (int i=1; i<=level ;i++)
    {
        if(level>0){cs = CS_LCD;spi_cmd.write(0);spi.write(0x01FF);cs = CS_RESET;wait_us(DD);}   // BLACK SPACE
        else{spi_write_data(CS_LCD,0x0120);wait_us(DD);}          // WHITE SPACE
    }
    for (int i=level; i<=16 ;i++)
    {
        spi_write_data(CS_LCD,0x0120);wait_us(DD); // SPACE
    }
    return 0; // return code ==0 is OK
}
///////////////////////////////////////////////////////////////////////////////////////////////
int read_adc(void){
    int adval_d;
    float adval_f;
    adval_d = spi_read_data(CS_ADC);
    adval_f = 3.3f*((float)adval_d/4095);
    printf("%d %.3fV\r\n",adval_d,adval_f);
    return adval_d;    
}
////////////////////////////////////////////////////////////////////
int read_switches(void){
    int sw_val;
    sw_val = spi_read_data(CS_GENERAL)&0x0F; // Just want lower 4bit nibble
    if (sw_val&(1<<0)){printf("Switch 0 :");}
    if (sw_val&(1<<1)){printf("Switch 1 :");}
    if (sw_val&(1<<2)){printf("Switch 2 :");}
    if (sw_val&(1<<3)){printf("Switch 3 :");}
    if (sw_val>0){printf("\r\n");}
    return sw_val;    
}