VS1005 SPI MISO

Writing software that controls the system and peripherals such as displays, SD cards, Buttons, LEDs, Serial Ports etc.
Fridolin
Senior User
Posts: 20
Joined: Mon 2015-11-30 12:54

VS1005 SPI MISO

Post by Fridolin » Mon 2015-11-30 13:12

Hello,

I try to comunicate with an spi port expander and an spi lcd . Comunication to LCD works fine (single direction). My problem is to receive the data from ioexpander (the MISO pin). data is correct (on the MISO pin, see attached image) but not read in at DATA0


Macrofunctions

Code: Select all

#define SPI_MASTER_8BIT_CSHI PERIP(SPI0_CF) = SPI_CF_MASTER | SPI_CF_DLEN8 | SPI_CF_FSIDLE1 | 0b10000000 | 0b1100000000 //macro to set SPI to MASTER; 8BIT; FSYNC Idle => xCS high
#define SPI_MASTER_8BIT_CSLO PERIP(SPI0_CF) = SPI_CF_MASTER | SPI_CF_DLEN8 | SPI_CF_FSIDLE0 | 0b10000000 | 0b1100000000//macro to set SPI to MASTER; 8BIT; FSYNC not Idle => xCS low

INI SPI-Interface

Code: Select all

GpioSetAsPeripheral(0x10);	// set GPIO1_0 as peripheral (SPI.cs)
GpioSetAsPeripheral(0x11);	// set GPIO1_1 as peripheral (SPI.scl)
GpioSetAsPeripheral(0x12);	// set GPIO1_2 as peripheral (SPI.miso)
GpioSetAsPeripheral(0x13);	// set GPIO1_3 as peripheral (SPI.mosi)
	
PERIP(SYSTEMPD_SFENA) = 0;

GpioSetPin(0x0F,0);			// reset GPIO0_10	--> SPI.cd

SPI_MASTER_8BIT_CSHI;
		
PERIP(SPI_CC_CLKDIV) = 3;
PERIP(SPI0_CLKCF) = PERIP(SPI_CC_CLKDIV)<<2;

PERIP(SPI0_DATA) = 0; //Kickstart	

Cycle Polling: Write 2 Bytes, and receive one byte

Code: Select all

u_int16 data_tmp;
	
SPI_MASTER_8BIT_CSLO;
// 1st
PERIP(SPI0_DATA) = IOEXP_READ_OP; // op-code -> read

while (PERIP(SPI0_STATUS) & SPI_ST_TXRUNNING);

// 2nd
PERIP(SPI0_DATA) = reg; // address

while (PERIP(SPI0_STATUS) & SPI_ST_TXRUNNING);

// 3rd
PERIP(SPI0_DATA) = 0xff; // dummy

while(PERIP(SPI0_STATUS) & SPI_ST_TXRUNNING){};
			
data_tmp = PERIP(SPI0_DATA);

SPI_MASTER_8BIT_CSHI;


no data is saved in data_tmp

Thanks!


Attached Osci Image (D4: MISO D7:MOSI D3:CLK)
Attachments
scope_1.png
scope_1.png (19.77 KiB) Viewed 1233 times

User avatar
Henrik
VLSI Staff
Posts: 1041
Joined: Tue 2010-06-22 14:10

Re: VS1005 SPI MISO

Post by Henrik » Tue 2015-12-08 13:14

Hello!

To me the code and signals mostly look good. But there is one error here:

Code: Select all

/* This is incorrect! */
PERIP(SPI_CC_CLKDIV) = 3;
PERIP(SPI0_CLKCF) = PERIP(SPI_CC_CLKDIV)<<2;
There is no such register as SPI_CC_CLKDIV. That is just the bit shift multiplier for a field in SPI0_CLKCF. I think what you want to do is this:

Code: Select all

/* Try this one instead. */
PERIP(SPI0_CLKCF) = 3*SPI_CC_CLKDIV;
Also, this line doesn't do what you think:

Code: Select all

/* Dangerous */
PERIP(SYSTEMPD_SFENA) = 0;
It will clear a pseudo-random address.

I think what you wanted is this:

Code: Select all

/* Try this one instead. */
PERIP(SYSTEMPD) &= ~SYSTEMPD_SFENA;


If this doesn't help, I'd suggest the following:
If possible, could you also add the Chip Select to your monitored pins? Also, could you show a larger picture of the actual byte that you are trying to receive (so that I can try to verify clock polarities)?

Also, at the end of your code, could you tell what you get if you add this:
printf("GPIO1_MODE %04x\n", PERIP(GPIO1_MODE));

Kind regards,
- Henrik
Good signatures never die. They just fade away.

User avatar
Henrik
VLSI Staff
Posts: 1041
Joined: Tue 2010-06-22 14:10

Re: VS1005 SPI MISO

Post by Henrik » Tue 2015-12-08 13:35

Oh, and now I think I found the final mistake:

Code: Select all

/* These macros won't work */
#define SPI_MASTER_8BIT_CSHI PERIP(SPI0_CF) = SPI_CF_MASTER | SPI_CF_DLEN8 | SPI_CF_FSIDLE1 | 0b10000000 | 0b1100000000 //macro to set SPI to MASTER; 8BIT; FSYNC Idle => xCS high
#define SPI_MASTER_8BIT_CSLO PERIP(SPI0_CF) = SPI_CF_MASTER | SPI_CF_DLEN8 | SPI_CF_FSIDLE0 | 0b10000000 | 0b1100000000//macro to set SPI to MASTER; 8BIT; FSYNC not Idle => xCS low
They activate the RF and TX FIFOs, after which the registers work quite differently. Try this:

Code: Select all

/* Try this: */
#define SPI_MASTER_8BIT_CSHI PERIP(SPI0_CF) = SPI_CF_MASTER | SPI_CF_DLEN8 | SPI_CF_FSIDLE1  //macro to set SPI to MASTER; 8BIT; FSYNC Idle => xCS high
#define SPI_MASTER_8BIT_CSLO PERIP(SPI0_CF) = SPI_CF_MASTER | SPI_CF_DLEN8 | SPI_CF_FSIDLE0 //macro to set SPI to MASTER; 8BIT; FSYNC not Idle => xCS low
Kind regards,
- Henrik
Good signatures never die. They just fade away.

Fridolin
Senior User
Posts: 20
Joined: Mon 2015-11-30 12:54

Re: VS1005 SPI MISO

Post by Fridolin » Tue 2015-12-08 14:12

Hallo Henrik,

thanks for your support. I changed my code like u told, but unfortunately it dosn't help.

Console Output:
GPIO_MODE: 7F0F
SPI0_CF: 2F
SPI0_CLKCF: C

D6:XCS0
Attachments
scope_4.png
Complete IO-Expander READ Frame
scope_4.png (19.98 KiB) Viewed 1221 times
scope_3.png
First Byte
scope_3.png (19.26 KiB) Viewed 1221 times
Last edited by Fridolin on Tue 2015-12-08 14:27, edited 1 time in total.

User avatar
Henrik
VLSI Staff
Posts: 1041
Joined: Tue 2010-06-22 14:10

Re: VS1005 SPI MISO

Post by Henrik » Tue 2015-12-08 14:26

Fridolin wrote:Hallo Henrik,

thanks for your support. I changed my code like u told, but unfortunately it dosn't help.

Console Output:
GPIO_MODE: 7F0F
SPI0_CF: 2F
SPI0_CLKCF: C
Let's see now.
1) GPIO1_MODE = 0x7f0f means that XCS0, SCLK0, MISO0, and MOSI0, are all in peripheral mode = SPI mode. Good.
2) SPI0_CF = 0x2F = Master mode, 8 data bits, FSIDLE = 1. Good.
3) SPI0_CLKCF = 0xC = SPI_CC_CLKDIV = 3, polarity and phase normal. Good.

If you have also replaced the macros, then try these:
4) Double verify that you absolutely use the right MISO pin.
5) If you have the current VS1005 packaging version where you can see the pins of the side of the IC (date code 1407 or larger), then verify that the MISO signal actually comes to the VS1005 pin (so that there are no soldering issues).
6) Try a slower SPI clock (e.g. PERIP(SPI0_CLKCF) = 12*SPI_CC_CLKDIV;).

If all of that doesn't help, could you generate the following two images:
7) Transferring the command and reply so that also Chip Select shows.
8) Transferring the reply byte in enough detail that clock polarity shows.

Kind regards,
- Henrik
Good signatures never die. They just fade away.

Fridolin
Senior User
Posts: 20
Joined: Mon 2015-11-30 12:54

Re: VS1005 SPI MISO

Post by Fridolin » Tue 2015-12-08 14:41

images I added in my last post. I forgot to mention. I use the break out Board and connected

XCS0 pin 27
SPI_MISO pin 29
SPI_MOSI pin 30
SCLK0 pin 28

User avatar
Henrik
VLSI Staff
Posts: 1041
Joined: Tue 2010-06-22 14:10

Re: VS1005 SPI MISO

Post by Henrik » Tue 2015-12-08 14:55

Hello again,

the waveforms look good. Chip select is good. Your pins look good. However, for the last image, can I get the answer byte, I mean the FF/02 byte, so that I can see the clock polarity of the reply? The answer bits should be arriving at the falling clock edge, then VS1005 reads them at the next rising edge.

After that, the only thing I can think of is point (5) of my last message: if possible, check MISO directly from the VS1005g pin to verify there isn't a soldering issue.

Kind regards,
- Henrik
Good signatures never die. They just fade away.

User avatar
Henrik
VLSI Staff
Posts: 1041
Joined: Tue 2010-06-22 14:10

Re: VS1005 SPI MISO

Post by Henrik » Tue 2015-12-08 15:08

One addition,

what value do you get to data_tmp? Does the value change if you connect SPI0_MISO to GND or IOVDD?

Can you provide your complete, corrected code?

Kind regards,
- Henrik
Good signatures never die. They just fade away.

Fridolin
Senior User
Posts: 20
Joined: Mon 2015-11-30 12:54

Re: VS1005 SPI MISO

Post by Fridolin » Tue 2015-12-08 16:53

Hi,

thanks again. I will test your solution tomorrow and send you my results as soon as possible.

Fridolin
Senior User
Posts: 20
Joined: Mon 2015-11-30 12:54

Re: VS1005 SPI MISO

Post by Fridolin » Thu 2015-12-10 12:15

Hallo

my complete code

Code: Select all

#include <vo_stdio.h>
#include <volink.h>     // Linker directives like DLLENTRY
#include <apploader.h>  // RunLibraryFunction etc // Contains LoadLibrary() and DropLibrary()
#include <sysmemory.h>
#include <vo_gpio.h>
#include <clockspeed.h>
#include <vo_fat.h>

//#include "decodeAudio.h"
#include <libaudiodec.h>
#include <vsostasks.h>
#include <timers.h>
#include <string.h>

#include "fonts/font_6x8.h"		//width 6 height 8
#include "fonts/font_7x12.h"	//width 7 height 12
//#include "fonts/font_8x16.h"	//width 8 height 16

#include <stdlib.h>
#include <saturate.h> // Contains number saturation functions

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	Definitions
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//--------------- Global ----------
#define VERSION "0.1-beta"

//--------------- Audio ----------
#define BUFSIZE 128	//input buffer

//--------------- LCD ----------
#define DELETE 0
#define ADD	1
#define INVERS 2

#define XPIXEL 102
#define YPIXEL 64
#define XMAX XPIXEL-1
#define YMAX YPIXEL-1

//--------------- MP3 ----------
#define MAX_FILES 200

//--------------- IOEXP ----------
#define IOEXP_WRITE_OP 0b01000000
#define IOEXP_READ_OP 0b01000001

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	Global varibales (static)
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//-------------- LCD ----------
extern unsigned char lcd_buffer[XPIXEL][YPIXEL/8];

//--------------- Timer ----------
volatile int ms = 0;
volatile int ms_old = 0;
volatile u_int16 delay_ms = 0;

//--------------- MP3 ----------
AUDIO_DECODER *auDec = NULL;
u_int16 *auDecLib = NULL;


struct s_file{
	u_int16 filename[50];
}sd[MAX_FILES];


volatile u_int16 test = 0;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	Macrofuncitons
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//--------------- LCD ----------
#define lcd_send_command(data) lcd_send(data, 0)
#define lcd_send_data(data) lcd_send(data, 1)
#define lcd_clear_all() lcd_clear(0,0,XPIXEL-1,YPIXEL-1)
#define lcd_fill_all() lcd_fill(0,0,XPIXEL-1,YPIXEL-1)

//--------------- SD-Card  ----------
#define SPI_MASTER_8BIT_CSHI PERIP(SPI0_CF) = SPI_CF_MASTER | SPI_CF_DLEN8 | SPI_CF_FSIDLE1 //macro to set SPI to MASTER; 8BIT; FSYNC Idle => xCS high
#define SPI_MASTER_8BIT_CSLO PERIP(SPI0_CF) = SPI_CF_MASTER | SPI_CF_DLEN8 | SPI_CF_FSIDLE0 //macro to set SPI to MASTER; 8BIT; FSYNC not Idle => xCS low

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	Functions
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//--------------- Global ----------
void uc_ini( void );

//--------------- LCD ----------
void lcd_ini(void);
void lcd_send(u_int16 data, u_int16 cd);
void lcd_clear(u_int16 xs, u_int16 ys, u_int16 xe, u_int16 ye);
void lcd_fill(u_int16 xs, u_int16 ys, u_int16 xe, u_int16 ye);
void lcd_line(u_int16 xs, u_int16 ys, u_int16 xe, u_int16 ye);
void lcd_rect(u_int16 xs, u_int16 ys, u_int16 xe, u_int16 ye);
void lcd_char(u_int16 x, u_int16 y, u_int16 data, u_int16 *font, u_int16 linkmode);
void lcd_string(u_int16 x, u_int16 y, char *data, u_int16 *font, u_int16 linkmode );
void lcd_setcontrast(unsigned char contrast, unsigned char resistor);

//--------------- SPI ----------
u_int16 spi_byte(u_int16 data);

//--------------- UART ----------
void uart_putChar(u_int16 c);
void uart_putStr(u_int16 *s);

//--------------- SD-Card  ----------
//void ListFiles(char driveLetter);

//--------------- MP3  ----------
void MP3Thread(void);

//line-in
void LineIn_play(void);

//IO-Expander
void ioexp_ini();
u_int16 ioexp_read_reg(unsigned char reg);
void ioexp_write_reg(unsigned char reg, unsigned char value);


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	Main
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(char *parameters) {
			
	u_int16 i;	
	u_int16 line;	
	char str[5];	
										
	uc_ini();

	ms = 1000; // wait 1s
	while( ms != 0 ){};
				
	ms = 100; // wait 100ms
	while( ms != 0 ){};
	//StartTask(TASK_DECODER, MP3Thread); Yield(); //Start the MP3 thread and allow it to initialize
	
/*
	Forbid();
	if (auDec) {
		auDec->pause = 0; 
		auDec->cs.fastForward = 1;
	} else {printf("Decoder not active\n");}
	Permit();
*/

	ms = 100; // wait 100ms
	while( ms != 0 ){};
			
	lcd_clear_all(); //Clear display and set display buffer to 0

	lcd_string(0,2*font_6x8[5], "UP:", font_6x8, DELETE); //Printing next line small fonts, normal
	lcd_string(0,3*font_6x8[5], "OK:", font_6x8, DELETE); //Printing next line small fonts, normal
	lcd_string(0,4*font_6x8[5], "LEFT:", font_6x8, DELETE); //Printing next line small fonts, normal
	lcd_string(0,5*font_6x8[5], "RIGHT:", font_6x8, DELETE); //Printing next line small fonts, normal
	lcd_string(0,6*font_6x8[5], "DOWN:", font_6x8, DELETE); //Printing next line small fonts, normal

	while(1){
		u_int16 tmp_reg09;
		char str_[20];
		tmp_reg09 = ioexp_read_reg(0x09);//read out gpio register

		if(tmp_reg09 > 0 || test > 0) {
			lcd_string(5*font_6x8[4],7*font_6x8[5], "true", font_6x8, DELETE); //Printing next line small fonts, normal
		}

		sprintf(str_, "%i", tmp_reg09);
		
		lcd_string(0,7*font_6x8[5], str_, font_6x8, DELETE); //Printing next line small fonts, normal

		if( (tmp_reg09 && 0b00000001) == 0b00000001)
		{
			lcd_string(8*font_6x8[4],2*font_6x8[5], "1", font_6x8, DELETE); //Printing next line small fonts, normal
		} else {
			lcd_string(8*font_6x8[4],2*font_6x8[5], "0", font_6x8, DELETE); //Printing next line small fonts, normal
		}

		if( (tmp_reg09 && 0b00000010) == 0b00000010)
		{
			lcd_string(8*font_6x8[4],3*font_6x8[5], "1", font_6x8, DELETE); //Printing next line small fonts, normal
		} else {
			lcd_string(8*font_6x8[4],3*font_6x8[5], "0", font_6x8, DELETE); //Printing next line small fonts, normal
		}

		if( (tmp_reg09 && 0b00000100) == 0b00000100)
		{
			lcd_string(8*font_6x8[4],4*font_6x8[5], "1", font_6x8, DELETE); //Printing next line small fonts, normal
		} else {
			lcd_string(8*font_6x8[4],4*font_6x8[5], "0", font_6x8, DELETE); //Printing next line small fonts, normal
		}

		if( (tmp_reg09 && 0b00001000) == 0b00001000)
		{
			lcd_string(8*font_6x8[4],5*font_6x8[5], "1", font_6x8, DELETE); //Printing next line small fonts, normal
		} else {
			lcd_string(8*font_6x8[4],5*font_6x8[5], "0", font_6x8, DELETE); //Printing next line small fonts, normal
		}

		if( (tmp_reg09 && 0b00010000) == 0b00010000)
		{
			lcd_string(8*font_6x8[4],6*font_6x8[5], "1", font_6x8, DELETE); //Printing next line small fonts, normal
		} else {
			lcd_string(8*font_6x8[4],6*font_6x8[5], "0", font_6x8, DELETE); //Printing next line small fonts, normal
		}
		
		ms = 500; // wait 100ms
		while( ms != 0 ){};

	}	
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	Functions
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*--------------------------------------
Function:	ini_uc()
Input:		---
Output:	---
Description: initialize interfaces
--------------------------------------*/
void uc_ini( void )
{
	static char buff[20];
	// INI
	u_int16 regval_tmp;
	
	// LCD
	u_int16 counter_contrast = 0;

	// SD-Card
	u_int16 file_counter;
	
	//---------------------------------------
	//	Ini Timer 0
	//---------------------------------------
	uart_putStr("Config timer0...\n");
	
	PERIP(INT_ENABLE0_HP) |= INTF_TIMER0;// enable timer0 interrupt	(INTF_TIMER0==14)
	//PERIP(INT_ENABLE0_HP) &= ~INTF_TIMER0;// disable timer0 interrupt

	//PERIP(TIMER_CF) = 49;// do not change --> otherwise UART is to slow
	//PERIP(TIMER_T0H) = 91;		// 1sec timer
	//PERIP(TIMER_T0L) = 36224; // 1sec timer
	PERIP(TIMER_T0H) = 0;		// 1ms timer
	PERIP(TIMER_T0L) = 6000; 	// 1ms timer
	PERIP(TIMER_ENA) |= TIMER_ENA_T0; // start Timer0 (Timer 2 is used for UART)
	
	uart_putStr("...config timer0 finished\n\n");

	//---------------------------------------
	//	Ini SPI 0
	//---------------------------------------
	uart_putStr("Config spi interface...\n");
	GpioSetAsPeripheral(0x10);	// set GPIO1_0 as peripheral (SPI.cs)
	GpioSetAsPeripheral(0x11);	// set GPIO1_1 as peripheral (SPI.scl)
	GpioSetAsPeripheral(0x12);	// set GPIO1_2 as peripheral (SPI.miso)
	GpioSetAsPeripheral(0x13);	// set GPIO1_3 as peripheral (SPI.mosi)
	
	regval_tmp = PERIP(GPIO1_MODE);
	//uart_putStr("GPIO1_MODE: %u \n",regval_tmp);
	sprintf(buff,"GPIO1_MODE: %X \n",regval_tmp);
	uart_putStr(buff);
	
	//PERIP(SYSTEMPD) &= ~SYSTEMPD_SFENA;

	GpioSetPin(0x0F,0);			// reset GPIO0_10	--> SPI.cd

	SPI_MASTER_8BIT_CSHI;
		
	regval_tmp = PERIP(SPI0_CF);
	sprintf(buff,"SPI0_CF: %X \n", regval_tmp);
	uart_putStr(buff);
	
	//PERIP(SPI0_CLKCF) = 3*SPI_CC_CLKDIV;
	PERIP(SPI0_CLKCF) = 12*SPI_CC_CLKDIV;
	
	regval_tmp = PERIP(SPI0_CLKCF);
	sprintf(buff,"SPI0_CLKCF: %X \n",regval_tmp);
	uart_putStr(buff);

	PERIP(SPI0_DATA) = 0; //Kickstart	

	//PERIP(INT_ENABLE0_HP) |= INTF_SPI0; //Enable interrupt
	uart_putStr("...config spi interface finished\n\n");
	
	//PERIP(SPI0_FSYNC) = 0;	
     
	//---------------------------------------
	//	Ini AUDIO
	//---------------------------------------
	//fprintf(uart,"Config audio...\n");

	//fprintf(uart,"...config audio finished\n\n");

	//---------------------------------------
	//	Ini SD-Card
	//---------------------------------------
	uart_putStr("Config sd-card...\n");
	for( file_counter = 0; file_counter<26; file_counter++ ) {
		if( vo_pdevices[file_counter] && vo_pdevices[file_counter]->fs == vo_filesystems[0] ) {	//FAT
			//fprintf(uart,"Found FAT device:  %c %s\n", 'A'+	file_counter, vo_pdevices[file_counter]->Identify(vo_pdevices[file_counter],NULL,0));
		
			if( ('A' + file_counter) != 'S' ) { //not S --> internal Flash
				//ListFiles('A' + file_counter); // list files on detected FAT device --> output uart
			}
		}
	}

	//ListFiles('d'); // list files on file d (sd-card)
	
	uart_putStr("...config sd-card finished\n\n");

	//---------------------------------------
	//	Ini mp3 decoder
	//---------------------------------------
	uart_putStr("Config mp3 decoder...\n");

	if (!(auDecLib = LoadLibrary("audiodec"))) {
		uart_putStr("Couldn't open AUDIODEC library\n");
	}

	uart_putStr("...config mp3 decoder finished\n\n");

	
	//---------------------------------------
	//	Ini DSP
	//---------------------------------------
	uart_putStr("Config display...\n");
	
	lcd_ini();

	uart_putStr("...config display finished\n\n");


	//---------------------------------------
	//	Ini IOEXP --ini after DSP otherwise not working becausee need time to boot
	//---------------------------------------
	uart_putStr("Config IO-Exp...\n");
	
	ioexp_ini();

	uart_putStr("...config IO-Exp finished\n\n");
}

/*--------------------------------------
Function:	LineIn_play()
Input:		---
Output:	---
Description: Read stereo samples from stdaudioin into myBuf and write stereo samples from myBuf into stdaudioout.
--------------------------------------*/
void LineIn_play(void)
{
	// AUDIO
	static s_int16 myBuf[BUFSIZE];	// Remember to never allocate buffers from stack space. So, if you
										// allocate the space inside your function, never forget "static"!

		// Read stereo samples from stdaudioin into myBuf.
		// By default, stdaudioin comes from line in.
		// By default both input and output are 16-bit stereo at 48 kHz.
		fread(myBuf, sizeof(s_int16), BUFSIZE, stdaudioin);


		// Here you may process audio in 16-bit L/R stereo format.
#if 0
		// This example code shows how to add to left channel volume.
		// Replace "#if 0" with "#if 1" to activate.
		{
			int i;
			s_int16 *p = myBuf; // Buffer to first element in myBuf
			for (i=0; i<BUFSIZE; i+=2) {
				// NOTE: The 16-bit sample is first cast to 32 bits to avoid
				// overflow in multiplication (which would lead to catastrophic
				// distortion), then multiplied by 4 (increase volume by 12 dB),
				// then finally saturated (which leads to smooth distortion if
				// overflow occurs), then cut back to 16 bits.
				*p = Sat32To16((s_int32)*p * 4);
				p+=2; // Skip to next left sample
			}
		}
#endif
		// Write stereo samples from myBuf into stdaudioout.
		// By default, stdaudioout goes to line out.
		fwrite(myBuf, sizeof(s_int16), BUFSIZE, stdaudioout);
}

/*--------------------------------------
Function:	MP3Thread()
Input:		---
Output:	---
Description: decode mp3 files
--------------------------------------*/
void MP3Thread(void) {	
	//AUDIO_DECODER *dec = NULL;
	FILE *inFp = NULL;	
	const char *eStr = NULL;
	int eCode = 0;	
	int result = S_OK;
		
	/* Replace the file name you want to play here. Don't forget to begin
	the name with "S:" for "System Disk". */

	//fprintf(uart,"Opening %s...\n", filelist[0]);	
	inFp = fopen( "test.mp3", "rb");
	if (!inFp) {
		//fprintf(uart,"Couldn't open %s for reading.\n", filelist[0]);
		result = S_ERROR;
		goto finally;
	}
	
	/* We could give a hint of the the file format, but we'll leave it to
	the decoder to determine. This is OK if the file is seekable. */
	//fprintf(uart,"Starting the decoder...\n");
	auDec = CreateAudioDecoder(auDecLib, inFp, stdaudioout, NULL, auDecFGuess);
	if (!auDec) {
		//printf("Couldn't create decoder\n");
		result = S_ERROR;
		goto finally;
	}
	
	//fprintf(uart,"Decoding audio...\n");
	eCode = DecodeAudio(auDecLib, auDec, &eStr);
	//fprintf(uart,"Decoder returns %d, \"%s\"\n", eCode, eStr ? eStr : "(null)");
	Delay(100);
	
	finally:
	//printf("Clean-up.\n");
	
	if (auDec) {
		DeleteAudioDecoder(auDecLib, auDec);
		auDec = NULL;
	}
	if (inFp) {
		fclose(inFp);
		inFp = NULL;
	}
}

/*--------------------------------------
Function:	ListFiles()
Input:		driveLetter
Output:	---
Description: List Files found on driveLetter
--------------------------------------*/
/*
void ListFiles(char driveLetter)
{
	static char filename[256];
	char s[3];
	sprintf(s,"%c:", driveLetter);
	
	if( filename ) {
		FILE *f = fopen(s, "s"); // Get a file handle for searching (s)

		if(f) {
			if( FatFindFirst(f,"",filename,256) == S_OK ) {
				do {
					//fprintf(uart,"Found file: '%s' | ", filename);
					
					//fprintf(uart,"Short name: '%s' | ", f->extraInfo);
					
					//fprintf(uart,"Pos: '%i' | ", f->pos);
					
					//fprintf(uart,"Attr: %02x ", f->ungetc_buffer);
					// if( f->ungetc_buffer & __ATTR_DIRECTORY ) fprintf(uart," (DIR) ");
					//fprintf(uart," | ");
					
					//fprintf(uart,"Extension: %s ", &f->extraInfo[13] );

					//fprintf(uart,"\n");
				} while( FatFindNext(f,filename, 256) == S_OK );
			}

			fclose(f);
		}
	} else {
		//fprintf(uart,"Couldn't get a buffer for filename with malloc\n");
	}
}
*/

//##############################################################################################
//
//	Func: Timer0InterruptHandler(void)
//	Desc: TIMER 0 ISR
//
//##############################################################################################
#pragma interrupt y 0x2e
void Timer0InterruptHandler(void) {
  if( ms > 0) ms--;
  if( delay_ms > 0) delay_ms--;
}

#pragma interrupt y 0x23
void SpiInterruptHandler(void) {
	if(PERIP(SPI0_STATUS) & SPI_ST_RXFULL){
		if((PERIP(SPI0_DATA)&0xff) == 0x02){
			test = 1;
		}
	}else{
		//test = 0;
	}
}

#pragma interrupt y 0x2d
void UartReceiveInterruptHandler(void){
	//*uartWr++ = PERIP(UART_DATA);
	//if (uartWr >= &uartBuffer[sizeof(uartBuffer)]) {
	//	uartWr = uartBuffer;
	//}
}

/*--------------------------------------
Function:	spi_byte()
Input:		data
Output:		---
Description:Sends one byte of data and
			wait until transfer is
			completed
--------------------------------------*/
u_int16 spi_byte(u_int16 data)
{
	SPI_MASTER_8BIT_CSLO;

	PERIP(SPI0_DATA) = data; //send data

	while (PERIP(SPI0_STATUS) & SPI_ST_TXRUNNING);

	SPI_MASTER_8BIT_CSHI;
					
	return PERIP(SPI0_DATA);
}

/*--------------------------------------
Function:	uart_putChar()
Input:		c
Output:	---
Description: send one byte on serial interface
--------------------------------------*/
void uart_putChar(u_int16 c)
{
	while( PERIP(UART_STATUS) & UART_ST_TXFULL);
	PERIP( UART_DATA ) = c;
}

/*--------------------------------------
Function:	uart_putStr()
Input:		*s
Output:	---
Description: send str 
--------------------------------------*/
void uart_putStr(u_int16 *s)
{
    unsigned char si;
    si = 0;
    while(s[si] != '\0')             // send the string until it is empty
    {
        uart_putChar( s[si] );       // send char
        si++;                       
    }
}

unsigned char lcd_buffer[XPIXEL][YPIXEL/8];  //Buffer to store display data. The buffer organization is equal to
											 // the one of the display

/*--------------------------------------
Function:	lcd_ini()
Input:		---
Output:	---
Description: initializes and clears lcd
--------------------------------------*/
void lcd_ini(void)
{
	GpioSetPin(0x17,0);			// reset GPIO1_7	--> Set display reset to low -> LCD is off now

	delay_ms = 1;
	while( delay_ms != 0 ){};					// 1ms delay

	GpioSetPin(0x17,1);			// set GPIO1_7	--> Set display reset to high -> LCD is running now

	delay_ms = 1;
	while( delay_ms != 0 ){};					// 1ms delay
	
	//Initialize bottom view 3.3V (booster on) 8Bit SPI
	lcd_send_command(0x40); //Startline 0
	lcd_send_command(0xA1); //SEG reverse
	lcd_send_command(0xC0); //Set COM direction (COM0-COM63)
	lcd_send_command(0xA4); //Set all Pixel to on
	lcd_send_command(0xA6); //Display inverse off
	lcd_send_command(0xA2); //Set bias 1/9
	lcd_send_command(0x2F); //Booster, regulator, follower on
	//lcd_send_command(0x27); //Set contrast
	//lcd_send_command(0x81); //Set contrast
	//lcd_send_command(0x10); //Set contrast
	lcd_setcontrast(10,7);
	lcd_send_command(0xFA); //Temperature compensation
	lcd_send_command(0x90); //Temperature compensation
	lcd_send_command(0xAF); //Display on

	lcd_clear_all(); //Clear display and set display buffer to 0
}

/*--------------------------------------
Function:	ioexp_ini()
Input:		---
Output:	---
Description: initializes port expande
GP0 = Input (Button - UP)
GP1 = Input (Button - OK)
GP2 = Input (Button - left)
GP3 = Input (Button - right)
GP4 = Input (Button - down)
GP5 = Input (Button - esc)
GP6 = Output (Backlight LCD Green)
GP7 = Output (Backlight LCD Red)
--------------------------------------*/
void ioexp_ini()
{
	ioexp_write_reg(0x00,0b00111111); //address (IODIRA) - set io direction; 0=output | 1=input
	ioexp_write_reg(0x01,0b00111111); //address (IPOLA) - switch polarity; 
	ioexp_write_reg(0x06,0b00111111); //address (GPIOA) - set pullup resistor; 0=pullup disabled | 1=pullup enabled
}

/*--------------------------------------
Function:	ioexp_read_reg()
Input:		port
Output:	---
Description: read the gpport port
--------------------------------------*/
u_int16 ioexp_read_reg(unsigned char reg)
{
	u_int16 data_tmp;

	char buff[20];

	GpioSetPin(0x20,1);				// GPIO2_0 (HMI_CS_LCD/IOEXP) [ 0-->LCD (default); 1-->IOEXP ]
	
	SPI_MASTER_8BIT_CSLO;
	
	// write always three bytes (op-code, address, value)

	// 1st
	PERIP(SPI0_DATA) = IOEXP_READ_OP; // op-code -> read

	while (PERIP(SPI0_STATUS) & SPI_ST_TXRUNNING);

	// 2nd
	PERIP(SPI0_DATA) = reg; // address

	while (PERIP(SPI0_STATUS) & SPI_ST_TXRUNNING);
	
	// 3rd
	PERIP(SPI0_DATA) = 0xff; // dummy

	while(PERIP(SPI0_STATUS) & SPI_ST_TXRUNNING){};
	
	data_tmp = PERIP(SPI0_DATA);
	
	if( data_tmp > 0)
		GpioSetPin(0x0E,1);			// turn off LED (GPIO0_14)
	else 
		GpioSetPin(0x0E,0);			// turn in LED (GPIO0_14)


	SPI_MASTER_8BIT_CSHI;
		
	GpioSetPin(0x20,0);				// GPIO2_0 (HMI_CS_LCD/IOEXP) [ 0-->LCD (default); 1-->IOEXP ]

	return data_tmp;
}

/*--------------------------------------
Function:	ioexp_write_reg()
Input:		port, state
Output:	---
Description: sets the port to spezified state
--------------------------------------*/
void ioexp_write_reg(unsigned char reg, unsigned char value)
{		
	GpioSetPin(0x20,1);			// GPIO2_0 (HMI_CS_LCD/IOEXP) [ 0-->LCD (default); 1-->IOEXP ]

	SPI_MASTER_8BIT_CSLO;
	
	// write always three bytes (op-code, address, value)

	// 1st
	PERIP(SPI0_DATA) = IOEXP_WRITE_OP;  // op-code -> write

	while (PERIP(SPI0_STATUS) & SPI_ST_TXRUNNING);

	// 2nd
	PERIP(SPI0_DATA) = reg; // address

	while (PERIP(SPI0_STATUS) & SPI_ST_TXRUNNING);
	
	// 3rd
	PERIP(SPI0_DATA) = value; // value

	while (PERIP(SPI0_STATUS) & SPI_ST_TXRUNNING);
	
	
	SPI_MASTER_8BIT_CSHI;
	
	GpioSetPin(0x20,0);			// GPIO2_0 (HMI_CS_LCD/IOEXP) [ 0-->LCD (default); 1-->IOEXP ]
}

/*--------------------------------------
Function:	lcd_send()
Input:		data, cd
Output:		---
Description:send data, as command (cd=0)
			or simple data (cd=1)
--------------------------------------*/
void lcd_send(u_int16 data, u_int16 cd)
{		
	GpioSetPin(0x20,0);			// GPIO2_0 (HMI_CS_LCD/IOEXP) [ 0-->LCD (default); 1-->IOEXP ]
	if (cd == 0)
		GpioSetPin(0x21,0);		// GPIO2_1==0 --> Send command
	else
		GpioSetPin(0x21,1);		// GPIO2_1==1 --> Send data
	spi_byte(data);				// Send data
}

if I put MISO to vdd or gnd it dosn't change the data_tmp value (but on the osci i can see the changes...)

Thanks a lot!

Maybe it's just a small thing but I can't see :geek:


D3: CLK
D4: MISO
D5: CS_IO_EXP (if high --> io-expander is selected)
D6: XCS0
D7: MOSI
Attachments
scope_5.png
response frame included
scope_5.png (19.43 KiB) Viewed 1143 times

Post Reply