Sound4Calc/src/sound4calc.c

148 lines
3.7 KiB
C
Raw Normal View History

2017-02-25 11:29:24 +01:00
#include "sound4calc.h"
#include <mpu.h> // from Gint
2017-02-25 17:22:24 +01:00
#include <timer.h> //from Gint
2017-01-12 22:41:56 +01:00
2017-02-25 22:23:56 +01:00
static struct {
int freq;
int length_ms;
} seq[] = {
2017-02-26 13:55:45 +01:00
{ 440, 500 },
2017-02-25 22:23:56 +01:00
{ 392, 500 },
{ 349, 500 },
{ -1, -1 }
};
2017-02-25 21:30:59 +01:00
2017-02-26 15:01:53 +01:00
char wave_signal[25] = {0}; // simple waveform
2017-02-26 14:42:22 +01:00
unsigned short duration = 2000; // equivalent as 2000 ms duration sound
unsigned int freq = 440;
2017-02-26 14:16:21 +01:00
2017-02-25 22:23:56 +01:00
void CallSequence()
2017-01-11 16:25:08 +01:00
{
2017-02-26 13:55:45 +01:00
static int length_end = 0;
static int note = -1 ;
2017-02-25 21:30:59 +01:00
2017-02-26 13:55:45 +01:00
if(length_end<=0)
2017-02-25 21:30:59 +01:00
{
2017-02-26 13:55:45 +01:00
note++;
2017-02-25 21:30:59 +01:00
2017-02-26 13:55:45 +01:00
//Stop the timer
if(seq[note].freq < 0)
{
timer_stop(TIMER_USER);
return;
}
2017-02-25 21:30:59 +01:00
2017-02-26 13:55:45 +01:00
// update of the number of turn
length_end = seq[note].freq * seq[note].length_ms / 1000;
2017-02-25 21:30:59 +01:00
2017-02-26 13:55:45 +01:00
timer_start(TIMER_USER, 2 * seq[note].freq, Clock_Hz, CallSequence, 0);
}
PlayNote();
length_end--;
2016-05-14 22:19:51 +02:00
}
2017-02-26 13:55:45 +01:00
void CallNote()
{
unsigned int wavelength = strlen(wave_signal);
2017-02-26 15:01:53 +01:00
2017-02-26 13:55:45 +01:00
timer_start(TIMER_USER, freq * wavelength, Clock_Hz, PlayNote, freq * duration / 1000);
}
2017-02-25 22:23:56 +01:00
2017-02-26 15:07:48 +01:00
void StopTimer()
{
timer_stop(TIMER_USER);
}
2017-02-25 17:22:24 +01:00
/*
PlayNote()
allow to make different waveforms with '-' and '_'
*/
void PlayNote()
{
2017-02-26 13:55:45 +01:00
static int x = 0;
2017-02-25 22:23:56 +01:00
2017-02-26 13:55:45 +01:00
PutPinState(wave_signal[x]=='-');
x = x + 1;
if(!(wave_signal[x])) x = 0;
2017-02-25 17:22:24 +01:00
}
2017-02-25 21:30:59 +01:00
/*
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)
{
2017-02-26 13:55:45 +01:00
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);
}
2017-02-25 21:30:59 +01:00
}
2017-02-25 17:22:24 +01:00
/*
InitPorts();
2017-02-25 21:30:59 +01:00
Ports initialisation
Open all need ports to use SwitchPinState()
2017-02-25 17:22:24 +01:00
*/
2017-02-25 11:29:24 +01:00
void InitPorts()
2016-05-14 22:19:51 +02:00
{
2017-02-26 13:55:45 +01:00
if(isSH3())
{
2017-02-24 21:57:01 +01:00
// initialisation of pin
2017-02-25 21:30:59 +01:00
// *(volatile unsigned char*)SH7337_SCPDR |= 0x01;
2017-02-24 21:57:01 +01:00
// SCIF2 clock on (STBCR3.MSTP31)
2017-02-26 13:55:45 +01:00
*(volatile unsigned char*)SH7337_STBCR3 &= ~0x02;
2017-02-24 21:57:01 +01:00
// switch off SCSMR_2.TE and SCSMR_2.RE
2017-02-26 13:55:45 +01:00
*(volatile unsigned short*)SH7337_SCSCR2 &= ~0x0030;
2017-02-24 21:57:01 +01:00
// SCIF2 clock off (STBCR3.MSTP31)
2017-02-26 13:55:45 +01:00
*(volatile unsigned char*)SH7337_STBCR3 |= 0x02;
2017-02-24 21:57:01 +01:00
// set bit 6 of port G to output mode
2017-02-26 13:55:45 +01:00
*(volatile unsigned short*)SH7337_PGCR = ( *(volatile unsigned short*)SH7337_PGCR & ~0x3000 ) | 0x1000;
2017-02-24 21:57:01 +01:00
// set bit 5 and 6 of port G
2017-02-26 13:55:45 +01:00
*(volatile unsigned char*)SH7337_PGDR |= 0x60;
2017-02-24 21:57:01 +01:00
// set port SC bit 0 to output
2017-02-26 13:55:45 +01:00
*(volatile unsigned short*)SH7337_SCPCR = ( *(volatile unsigned short*)SH7337_SCPCR & ~0x0003 ) | 0x0001;
}
else
{
2017-02-24 21:57:01 +01:00
// initialisation of pin
2017-02-25 21:30:59 +01:00
// *(volatile unsigned char*)SH7305_PJDR |= 0x04;
2017-02-24 21:57:01 +01:00
// SCIF2 clock on (MSTPCR0.MSTP007)
2017-02-26 13:55:45 +01:00
*(volatile unsigned int*)SH7305_MSTPCR0 &= ~0x00000080;
2017-02-24 21:57:01 +01:00
// switch off SCSMR_2.TE and SCSMR_2.RE
2017-02-26 13:55:45 +01:00
*(volatile unsigned short*)SH7305_SCSCR &= ~0x0030;
2017-02-24 21:57:01 +01:00
// SCIF2 clock off (MSTPCR0.MSTP007)
2017-02-26 13:55:45 +01:00
*(volatile unsigned int*)SH7305_MSTPCR0 |= 0x00000080;
2017-02-24 21:57:01 +01:00
// set bit 3 of port U to output mode
2017-02-26 13:55:45 +01:00
*(volatile unsigned short*)SH7305_PUCR = ( *(volatile unsigned short*)SH7305_PUCR & ~0x00C0 ) | 0x0040;
2017-02-24 21:57:01 +01:00
// set bit 4 and 5 of port U
2017-02-26 13:55:45 +01:00
*(volatile unsigned char*)SH7305_PUDR |= 0x0C;
2017-02-25 21:30:59 +01:00
// set port J bit 2 to output
2017-02-26 13:55:45 +01:00
*(unsigned short*)0xA4050110 = ( *(unsigned short*)0xA4050110 & ~0x0030 ) | 0x0010;
2017-02-25 21:30:59 +01:00
// set port J bit 3 to input
2017-02-26 13:55:45 +01:00
*(unsigned short*)0xA4050110 = ( *(unsigned short*)0xA4050110 & ~0x00C0 ) | 0x0080;
2017-02-24 21:57:01 +01:00
2017-02-25 21:30:59 +01:00
/*
2017-02-24 21:57:01 +01:00
// 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;
2017-02-25 21:30:59 +01:00
*/
2017-02-26 13:55:45 +01:00
}
2017-01-12 22:41:56 +01:00
}