Dependencies:   mbed

Committer:
BenRJG
Date:
Sun Apr 01 15:54:46 2018 +0000
Revision:
2:b615682e3e4f
Parent:
1:acc66d3a1a1c
Child:
3:d6e142b6ead1
Created functions from main code to SPI_TEST and SPI_INIT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BenRJG 1:acc66d3a1a1c 1 #include "SPI.h"
BenRJG 1:acc66d3a1a1c 2
BenRJG 1:acc66d3a1a1c 3 SPI spi(PA_7, PA_6, PA_5); // Ordered as: mosi, miso, sclk could use forth parameter ssel
BenRJG 1:acc66d3a1a1c 4 // However using multi SPI devices within FPGA with a seperate chip select
BenRJG 1:acc66d3a1a1c 5 SPI spi_cmd(PA_7, PA_6, PA_5); // NB another instance call spi_cmd for 8 bit SPI dataframe see later line 37
BenRJG 1:acc66d3a1a1c 6 // For each device NB PA_7 PA_6 PA_5 are D11 D12 D13 respectively
BenRJG 1:acc66d3a1a1c 7 DigitalOut cs(PC_6); // Chip Select for Basic Outputs to illuminate Onboard FPGA DEO nano LEDs CN7 pin 1
BenRJG 1:acc66d3a1a1c 8 DigitalOut LCD_cs(PB_15); // Chip Select for the LCD via FPGA CN7 pin 3
BenRJG 1:acc66d3a1a1c 9 DigitalOut ADC_cs(PB_9); // Chip Select for the ADC via FPGA CN7 pin 4
BenRJG 1:acc66d3a1a1c 10
BenRJG 1:acc66d3a1a1c 11 //NBB the following line for F429ZI !!!!
BenRJG 1:acc66d3a1a1c 12 DigitalIn DO_NOT_USE(PB_12); // MAKE PB_12 (D19) an INPUT do NOT make an OUTPUT under any circumstances !!!!! ************* !!!!!!!!!!!
BenRJG 1:acc66d3a1a1c 13 // This Pin is connected to the 5VDC from the FPGA card and an INPUT is 5V Tolerant
BenRJG 1:acc66d3a1a1c 14
BenRJG 2:b615682e3e4f 15 int adval_d; //A to D value read back
BenRJG 2:b615682e3e4f 16 float adval_f;
BenRJG 2:b615682e3e4f 17 int err; //error variable used for debugging, trapping etc.,
BenRJG 2:b615682e3e4f 18
BenRJG 2:b615682e3e4f 19 char adval[32];
BenRJG 1:acc66d3a1a1c 20 //Ticker ticktock;
BenRJG 2:b615682e3e4f 21 void SPI_INIT (void)
BenRJG 2:b615682e3e4f 22 {
BenRJG 2:b615682e3e4f 23 cs = 1; // Chip must be deselected, Chip Select is active LOW
BenRJG 2:b615682e3e4f 24 LCD_cs = 1; // Chip must be deselected, Chip Select is active LOW
BenRJG 2:b615682e3e4f 25 ADC_cs = 1; // Chip must be deselected, Chip Select is active LOW
BenRJG 2:b615682e3e4f 26 spi.format(16,0); // Setup the DATA frame SPI for 16 bit wide word, Clock Polarity 0 and Clock Phase 0 (0)
BenRJG 2:b615682e3e4f 27 spi_cmd.format(8,0); // Setup the COMMAND SPI as 8 Bit wide word, Clock Polarity 0 and Clock Phase 0 (0)
BenRJG 2:b615682e3e4f 28 spi.frequency(1000000); // 1MHz clock rate
BenRJG 2:b615682e3e4f 29 spi_cmd.frequency(1000000); // 1MHz clock rate
BenRJG 2:b615682e3e4f 30
BenRJG 2:b615682e3e4f 31 adval_d = 0; //A to D value read back
BenRJG 2:b615682e3e4f 32 adval_f =0.0f;
BenRJG 2:b615682e3e4f 33 err = 0; //error variable used for debugging, trapping etc.,
BenRJG 2:b615682e3e4f 34
BenRJG 2:b615682e3e4f 35 // Preload some arrays
BenRJG 2:b615682e3e4f 36 // char hello_world[]="Hello World";
BenRJG 2:b615682e3e4f 37 char splash_screen1[]="Martin Simpson";
BenRJG 2:b615682e3e4f 38 char splash_screen2[]="Plymouth UNI";
BenRJG 2:b615682e3e4f 39 char DVM[]="Voltage=";
BenRJG 2:b615682e3e4f 40 // Start up sequences
BenRJG 2:b615682e3e4f 41 lcd_cls();
BenRJG 2:b615682e3e4f 42 lcd_locate(1,1);
BenRJG 2:b615682e3e4f 43 lcd_display(splash_screen1); //Credit line 1
BenRJG 2:b615682e3e4f 44 lcd_locate(2,2);
BenRJG 2:b615682e3e4f 45 lcd_display(splash_screen2); //Credit line 2
BenRJG 2:b615682e3e4f 46 wait(2);
BenRJG 2:b615682e3e4f 47 lcd_cls();
BenRJG 2:b615682e3e4f 48 pulse_bar_graph(); //Flashy bargraph clear screen
BenRJG 2:b615682e3e4f 49 lcd_locate(1,0);
BenRJG 2:b615682e3e4f 50 lcd_display(DVM); //Type Voltage display
BenRJG 2:b615682e3e4f 51 lcd_locate(1,13);
BenRJG 2:b615682e3e4f 52 lcd_display("V"); //Units display
BenRJG 2:b615682e3e4f 53 }
BenRJG 1:acc66d3a1a1c 54
BenRJG 2:b615682e3e4f 55 void SPI_TEST(void)
BenRJG 2:b615682e3e4f 56 {
BenRJG 2:b615682e3e4f 57 adval_d = read_adc();
BenRJG 2:b615682e3e4f 58
BenRJG 2:b615682e3e4f 59 adval_f = 3.3f*((float)adval_d/4095);//Convert 12 bit to a float and scale
BenRJG 2:b615682e3e4f 60 sprintf(adval,"%.3f",adval_f); //Store in an array string
BenRJG 2:b615682e3e4f 61 lcd_locate(1,8); //and display on LCD
BenRJG 2:b615682e3e4f 62 lcd_display(adval); //
BenRJG 2:b615682e3e4f 63
BenRJG 2:b615682e3e4f 64 err = bar_graph(adval_d/255); // 16*256 =4096 12 bit ADC!
BenRJG 2:b615682e3e4f 65 if (err < 0){printf("Display Overload\r\n");}
BenRJG 2:b615682e3e4f 66
BenRJG 2:b615682e3e4f 67 read_switches();
BenRJG 2:b615682e3e4f 68 //LED Chaser display KIT lives on!
BenRJG 2:b615682e3e4f 69 for (uint32_t i=1;i<=128;i*=2)
BenRJG 2:b615682e3e4f 70 {
BenRJG 2:b615682e3e4f 71 cs = 0; //Select the device by seting chip select LOW
BenRJG 2:b615682e3e4f 72 spi_cmd.write(0);
BenRJG 2:b615682e3e4f 73 spi.write(i);
BenRJG 2:b615682e3e4f 74 cs = 1; //De-Select the device by seting chip select HIGH
BenRJG 2:b615682e3e4f 75 wait_ms(20);
BenRJG 2:b615682e3e4f 76 }
BenRJG 2:b615682e3e4f 77 for (uint32_t i=128;i>=1;i/=2)
BenRJG 2:b615682e3e4f 78 {
BenRJG 2:b615682e3e4f 79 cs = 0; //Select the device by seting chip select LOW
BenRJG 2:b615682e3e4f 80 spi_cmd.write(0);
BenRJG 2:b615682e3e4f 81 spi.write(i);
BenRJG 2:b615682e3e4f 82 cs = 1; //De-Select the device by seting chip select HIGH
BenRJG 2:b615682e3e4f 83 wait_ms(20);
BenRJG 2:b615682e3e4f 84 }
BenRJG 2:b615682e3e4f 85 }
BenRJG 1:acc66d3a1a1c 86
BenRJG 1:acc66d3a1a1c 87 int lcd_cls(void){
BenRJG 1:acc66d3a1a1c 88 LCD_cs = 0;spi_cmd.write(0);spi.write(0x0001);LCD_cs = 1;wait_us(CD); //Clear Screen
BenRJG 1:acc66d3a1a1c 89 return 0;
BenRJG 1:acc66d3a1a1c 90 }
BenRJG 1:acc66d3a1a1c 91
BenRJG 1:acc66d3a1a1c 92 int lcd_locate(uint8_t line, uint8_t column){
BenRJG 1:acc66d3a1a1c 93 uint8_t line_addr;
BenRJG 1:acc66d3a1a1c 94 uint8_t column_addr;
BenRJG 1:acc66d3a1a1c 95 switch(line){
BenRJG 1:acc66d3a1a1c 96 case 1: line_addr=0x80; break;
BenRJG 1:acc66d3a1a1c 97 case 2: line_addr=0xC0; break;
BenRJG 1:acc66d3a1a1c 98 default: return -1; //return code !=0 is error
BenRJG 1:acc66d3a1a1c 99 }
BenRJG 1:acc66d3a1a1c 100 if(column<16){column_addr=column;}
BenRJG 1:acc66d3a1a1c 101 else{return -1;}
BenRJG 1:acc66d3a1a1c 102 LCD_cs = 0;
BenRJG 1:acc66d3a1a1c 103 spi_cmd.write(0);
BenRJG 1:acc66d3a1a1c 104 spi.write(line_addr+column_addr);
BenRJG 1:acc66d3a1a1c 105 LCD_cs = 1;
BenRJG 1:acc66d3a1a1c 106 wait_us(CD); //DDRAM location Second line is 0x00C0 first line starts at 0x0080
BenRJG 1:acc66d3a1a1c 107 return 0;
BenRJG 1:acc66d3a1a1c 108 }
BenRJG 1:acc66d3a1a1c 109
BenRJG 1:acc66d3a1a1c 110 int lcd_display(char* str){
BenRJG 1:acc66d3a1a1c 111
BenRJG 1:acc66d3a1a1c 112 if (strlen(str)>16){return -1;} //return code !=0 is error
BenRJG 1:acc66d3a1a1c 113
BenRJG 1:acc66d3a1a1c 114 uint8_t command_data=1;
BenRJG 1:acc66d3a1a1c 115 uint32_t wait_time;
BenRJG 1:acc66d3a1a1c 116
BenRJG 1:acc66d3a1a1c 117 switch(command_data){
BenRJG 1:acc66d3a1a1c 118 case 0: wait_time=DD; break;
BenRJG 1:acc66d3a1a1c 119 case 1: wait_time=CD; break;
BenRJG 1:acc66d3a1a1c 120 default: return -1;
BenRJG 1:acc66d3a1a1c 121 }
BenRJG 1:acc66d3a1a1c 122
BenRJG 1:acc66d3a1a1c 123 for (int i=0; i<strlen(str);i++){
BenRJG 1:acc66d3a1a1c 124 LCD_cs = 0;
BenRJG 1:acc66d3a1a1c 125 spi_cmd.write(0);
BenRJG 1:acc66d3a1a1c 126 spi.write((command_data<<8)+str[i]);
BenRJG 1:acc66d3a1a1c 127 LCD_cs = 1;
BenRJG 1:acc66d3a1a1c 128 wait_us(wait_time);
BenRJG 1:acc66d3a1a1c 129 }
BenRJG 1:acc66d3a1a1c 130 return 0;
BenRJG 1:acc66d3a1a1c 131 }
BenRJG 1:acc66d3a1a1c 132
BenRJG 1:acc66d3a1a1c 133 int bar_graph(uint8_t level){
BenRJG 1:acc66d3a1a1c 134 if (level>16){return -1;} //return code !=0 is error
BenRJG 1:acc66d3a1a1c 135 LCD_cs = 0;spi_cmd.write(0);spi.write(0x00C0);LCD_cs = 1;wait_us(CD); //DDRAM location Second line is 0x00C0 first line starts at 0x0080
BenRJG 1:acc66d3a1a1c 136 for (int i=1; i<=level ;i++)
BenRJG 1:acc66d3a1a1c 137 {
BenRJG 1:acc66d3a1a1c 138 if(level>0){LCD_cs = 0;spi_cmd.write(0);spi.write(0x01FF);LCD_cs = 1;wait_us(DD);} // BLACK SPACE
BenRJG 1:acc66d3a1a1c 139 else{LCD_cs = 0;spi_cmd.write(0);spi.write(0x0120);LCD_cs = 1;wait_us(DD);} // WHITE SPACE
BenRJG 1:acc66d3a1a1c 140 }
BenRJG 1:acc66d3a1a1c 141 for (int i=level; i<=16 ;i++)
BenRJG 1:acc66d3a1a1c 142 {
BenRJG 1:acc66d3a1a1c 143 LCD_cs = 0;spi_cmd.write(0);spi.write(0x0120);LCD_cs = 1;wait_us(DD); // SPACE
BenRJG 1:acc66d3a1a1c 144 }
BenRJG 1:acc66d3a1a1c 145 return 0; // return code ==0 is OK
BenRJG 1:acc66d3a1a1c 146 }
BenRJG 1:acc66d3a1a1c 147
BenRJG 1:acc66d3a1a1c 148 int read_adc(void){
BenRJG 1:acc66d3a1a1c 149 int adval_d;
BenRJG 1:acc66d3a1a1c 150 float adval_f;
BenRJG 1:acc66d3a1a1c 151 ADC_cs = 0;
BenRJG 1:acc66d3a1a1c 152 adval_d = spi.write(0x00);
BenRJG 1:acc66d3a1a1c 153 ADC_cs =1 ;
BenRJG 1:acc66d3a1a1c 154 adval_f = 3.3f*((float)adval_d/4095);
BenRJG 1:acc66d3a1a1c 155 printf("%d %.3fV\r\n",adval_d,adval_f);
BenRJG 1:acc66d3a1a1c 156 return adval_d;
BenRJG 1:acc66d3a1a1c 157 }
BenRJG 1:acc66d3a1a1c 158
BenRJG 1:acc66d3a1a1c 159 void pulse_bar_graph(void){
BenRJG 1:acc66d3a1a1c 160 for (uint8_t i=0;i<16;i++)
BenRJG 1:acc66d3a1a1c 161 {
BenRJG 1:acc66d3a1a1c 162 printf("%u\r\n",i);
BenRJG 1:acc66d3a1a1c 163 bar_graph(i);
BenRJG 1:acc66d3a1a1c 164 wait_ms(100);
BenRJG 1:acc66d3a1a1c 165 }
BenRJG 1:acc66d3a1a1c 166 for (int8_t i=15;i>=0;i--)
BenRJG 1:acc66d3a1a1c 167 {
BenRJG 1:acc66d3a1a1c 168 printf("%u\r\n",i);
BenRJG 1:acc66d3a1a1c 169 bar_graph(i);
BenRJG 1:acc66d3a1a1c 170 wait_ms(100);
BenRJG 1:acc66d3a1a1c 171 }
BenRJG 1:acc66d3a1a1c 172 }
BenRJG 1:acc66d3a1a1c 173
BenRJG 1:acc66d3a1a1c 174 int read_switches(void){
BenRJG 1:acc66d3a1a1c 175 int sw_val;
BenRJG 1:acc66d3a1a1c 176 cs = 0;
BenRJG 1:acc66d3a1a1c 177 spi_cmd.write(0);
BenRJG 1:acc66d3a1a1c 178 sw_val = spi.write(0x00)&0x0F; // Just want lower 4bit nibble
BenRJG 1:acc66d3a1a1c 179 cs = 1 ;
BenRJG 1:acc66d3a1a1c 180 if (sw_val&(1<<0)){printf("Switch 0 :");}
BenRJG 1:acc66d3a1a1c 181 if (sw_val&(1<<1)){printf("Switch 1 :");}
BenRJG 1:acc66d3a1a1c 182 if (sw_val&(1<<2)){printf("Switch 2 :");}
BenRJG 1:acc66d3a1a1c 183 if (sw_val&(1<<3)){printf("Switch 3 :");}
BenRJG 1:acc66d3a1a1c 184 if (sw_val>0){printf("\r\n");}
BenRJG 1:acc66d3a1a1c 185 return sw_val;
BenRJG 1:acc66d3a1a1c 186 }