Sound4Calc/src/sound4calc.c

148 lines
3.7 KiB
C

#include "sound4calc.h"
#include <mpu.h> // from Gint
#include <timer.h> //from Gint
static struct {
int freq;
int length_ms;
} seq[] = {
{ 440, 500 },
{ 392, 500 },
{ 349, 500 },
{ -1, -1 }
};
char wave_signal[25] = {0}; // simple waveform
unsigned short duration = 2000; // equivalent as 2000 ms duration sound
unsigned int freq = 440;
void CallSequence()
{
static int length_end = 0;
static int note = -1 ;
if(length_end<=0)
{
note++;
//Stop the timer
if(seq[note].freq < 0)
{
timer_stop(TIMER_USER);
return;
}
// update of the number of turn
length_end = seq[note].freq * seq[note].length_ms / 1000;
timer_start(TIMER_USER, 2 * seq[note].freq, Clock_Hz, CallSequence, 0);
}
PlayNote();
length_end--;
}
void CallNote()
{
unsigned int wavelength = strlen(wave_signal);
timer_start(TIMER_USER, freq * wavelength, Clock_Hz, PlayNote, freq * duration / 1000);
}
void StopTimer()
{
timer_stop(TIMER_USER);
}
/*
PlayNote()
allow to make different waveforms with '-' and '_'
*/
void PlayNote()
{
static int x = 0;
PutPinState(wave_signal[x]=='-');
x = x + 1;
if(!(wave_signal[x])) x = 0;
}
/*
PutPinState()
put the pin at the level wanted
SH4 :
state 0 : xxxx.01xx
state 1 : xxxx.10xx
SH3 :
state 0 : xxxx.xxx0
state 1 : xxxx.xxx1
*/
void PutPinState(char level)
{
level = !!level;
if(isSH3())
{
*(volatile unsigned char*)SH7337_SCPDR = (*(volatile unsigned char*)SH7337_SCPDR & 0xFE) | level ;
}
else
{
*(volatile unsigned char*)SH7305_PJDR = (*(volatile unsigned char*)SH7305_PJDR & 0xF3) | (0x4 << level);
}
}
/*
InitPorts();
Ports initialisation
Open all need ports to use SwitchPinState()
*/
void InitPorts()
{
if(isSH3())
{
// initialisation of pin
// *(volatile unsigned char*)SH7337_SCPDR |= 0x01;
// SCIF2 clock on (STBCR3.MSTP31)
*(volatile unsigned char*)SH7337_STBCR3 &= ~0x02;
// switch off SCSMR_2.TE and SCSMR_2.RE
*(volatile unsigned short*)SH7337_SCSCR2 &= ~0x0030;
// SCIF2 clock off (STBCR3.MSTP31)
*(volatile unsigned char*)SH7337_STBCR3 |= 0x02;
// set bit 6 of port G to output mode
*(volatile unsigned short*)SH7337_PGCR = ( *(volatile unsigned short*)SH7337_PGCR & ~0x3000 ) | 0x1000;
// set bit 5 and 6 of port G
*(volatile unsigned char*)SH7337_PGDR |= 0x60;
// set port SC bit 0 to output
*(volatile unsigned short*)SH7337_SCPCR = ( *(volatile unsigned short*)SH7337_SCPCR & ~0x0003 ) | 0x0001;
}
else
{
// initialisation of pin
// *(volatile unsigned char*)SH7305_PJDR |= 0x04;
// SCIF2 clock on (MSTPCR0.MSTP007)
*(volatile unsigned int*)SH7305_MSTPCR0 &= ~0x00000080;
// switch off SCSMR_2.TE and SCSMR_2.RE
*(volatile unsigned short*)SH7305_SCSCR &= ~0x0030;
// SCIF2 clock off (MSTPCR0.MSTP007)
*(volatile unsigned int*)SH7305_MSTPCR0 |= 0x00000080;
// set bit 3 of port U to output mode
*(volatile unsigned short*)SH7305_PUCR = ( *(volatile unsigned short*)SH7305_PUCR & ~0x00C0 ) | 0x0040;
// set bit 4 and 5 of port U
*(volatile unsigned char*)SH7305_PUDR |= 0x0C;
// set port J bit 2 to output
*(unsigned short*)0xA4050110 = ( *(unsigned short*)0xA4050110 & ~0x0030 ) | 0x0010;
// set port J bit 3 to input
*(unsigned short*)0xA4050110 = ( *(unsigned short*)0xA4050110 & ~0x00C0 ) | 0x0080;
/*
// set port J bit 2 to output mode
*(volatile unsigned short*)SH7305_PJCR = ( *(volatile unsigned short*)SH7305_PJCR & ~0x0030 ) | 0x0010;
// set port J bit 3 to output mode
*(volatile unsigned short*)SH7305_PJCR = ( *(volatile unsigned short*)SH7305_PJCR & ~0x00C0 ) | 0x0040;
*/
}
}