Use the speaker to play music

Closes #1
This commit is contained in:
Lukas Fürderer 2020-11-15 20:14:26 +01:00
parent 40c87d0fcf
commit b37bb42f03
8 changed files with 381 additions and 33 deletions

12
Main.c
View File

@ -8,6 +8,8 @@
This file contains the main app function and init.
*/
#include "audio.h"
#include "Board_DAC.h"
#include "Welcome.h"
//25x25 Dot Qr Code, 25bits per line (x)
@ -71,6 +73,9 @@ int main (void)
*/
static void vMainInitApp(void)
{
// Init audio pin
DAC_Initialize();
// Init GLCD
GLCD_Initialize ();
GLCD_SetForegroundColor (GLCD_COLOR_BLACK);
@ -81,6 +86,13 @@ static void vMainInitApp(void)
// Init LED
LED_Initialize();
vStartAudio();
vPrintQr();
while (1)
{
}
}
/**

View File

@ -1,27 +1,32 @@
/*
* Auto generated Run-Time-Environment Component Configuration File
* *** Do not modify ! ***
*
* Project: 'Welcome'
* Target: 'MCB1760 mit LPC1768'
*/
#ifndef RTE_COMPONENTS_H
#define RTE_COMPONENTS_H
/*
* Define the Device Header File:
*/
#define CMSIS_device_header "LPC17xx.h"
#define RTE_DEVICE_STARTUP_LPC17XX /* Device Startup for NXP17XX */
#define RTE_Drivers_I2C0 /* Driver I2C0 */
#define RTE_Drivers_I2C1 /* Driver I2C1 */
#define RTE_Drivers_I2C2 /* Driver I2C2 */
#define RTE_Drivers_SPI0 /* Driver SPI0 */
#define RTE_Drivers_SPI1 /* Driver SPI1 */
#define RTE_Drivers_SPI2 /* Driver SPI2 */
#endif /* RTE_COMPONENTS_H */
/*
* Auto generated Run-Time-Environment Configuration File
* *** Do not modify ! ***
*
* Project: 'Welcome'
* Target: 'MCB1760 mit LPC1768'
*/
#ifndef RTE_COMPONENTS_H
#define RTE_COMPONENTS_H
/*
* Define the Device Header File:
*/
#define CMSIS_device_header "LPC17xx.h"
/* Keil::CMSIS Driver:I2C:2.4.0 */
#define RTE_Drivers_I2C0 /* Driver I2C0 */
#define RTE_Drivers_I2C1 /* Driver I2C1 */
#define RTE_Drivers_I2C2 /* Driver I2C2 */
/* Keil::CMSIS Driver:SPI:SPI:2.1.0 */
#define RTE_Drivers_SPI2 /* Driver SPI2 */
/* Keil::CMSIS Driver:SPI:SSP:2.7.0 */
#define RTE_Drivers_SPI0 /* Driver SPI0 */
#define RTE_Drivers_SPI1 /* Driver SPI1 */
/* Keil::Device:Startup:1.0.0 */
#define RTE_DEVICE_STARTUP_LPC17XX /* Device Startup for NXP17XX */
#endif /* RTE_COMPONENTS_H */

View File

@ -10,7 +10,7 @@
<aExt>*.s*; *.src; *.a*</aExt>
<oExt>*.obj; *.o</oExt>
<lExt>*.lib</lExt>
<tExt>*.txt; *.h; *.inc</tExt>
<tExt>*.txt; *.h; *.inc; *.md</tExt>
<pExt>*.plm</pExt>
<CppX>*.cpp</CppX>
<nMigrate>0</nMigrate>
@ -77,7 +77,7 @@
<tvExpOptDlg>0</tvExpOptDlg>
<IsCurrentTarget>1</IsCurrentTarget>
</OPTFL>
<CpuCode>0</CpuCode>
<CpuCode>8</CpuCode>
<DebugOpt>
<uSim>0</uSim>
<uTrg>1</uTrg>
@ -140,10 +140,59 @@
<SetRegEntry>
<Number>0</Number>
<Key>UL2CM3</Key>
<Name>-UAny -O206 -S8 -C0 -P00 -N00("ARM CoreSight SW-DP") -D00(2BA01477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD10000000 -FCFE0 -FN1 -FF0LPC_IAP_512.FLM -FS00 -FL080000 -FP0($$Device:LPC1768$Flash\LPC_IAP_512.FLM)</Name>
<Name>-UAny -O206 -S8 -C0 -P00 -N00("ARM CoreSight SW-DP") -D00(2BA01477) -L00(0) -TO65554 -TC10000000 -TT10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD10000000 -FCFE0 -FN1 -FF0LPC_IAP_512.FLM -FS00 -FL080000 -FP0($$Device:LPC1768$Flash\LPC_IAP_512.FLM)</Name>
</SetRegEntry>
</TargetDriverDllRegistry>
<Breakpoint/>
<Breakpoint>
<Bp>
<Number>0</Number>
<Type>0</Type>
<LineNumber>48</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>0</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>0</BreakIfRCount>
<Filename>.\audio.c</Filename>
<ExecCommand></ExecCommand>
<Expression></Expression>
</Bp>
<Bp>
<Number>1</Number>
<Type>0</Type>
<LineNumber>47</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>0</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>0</BreakIfRCount>
<Filename>.\audio.c</Filename>
<ExecCommand></ExecCommand>
<Expression></Expression>
</Bp>
<Bp>
<Number>2</Number>
<Type>0</Type>
<LineNumber>81</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>8200</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>1</BreakIfRCount>
<Filename>.\audio.c</Filename>
<ExecCommand></ExecCommand>
<Expression>\\Welcome\audio.c\81</Expression>
</Bp>
</Breakpoint>
<Tracepoint>
<THDelay>0</THDelay>
</Tracepoint>
@ -226,6 +275,54 @@
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>1</GroupNumber>
<FileNumber>3</FileNumber>
<FileType>1</FileType>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>.\audio.c</PathWithFileName>
<FilenameWithoutPath>audio.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>1</GroupNumber>
<FileNumber>4</FileNumber>
<FileType>5</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>.\audio.h</PathWithFileName>
<FilenameWithoutPath>audio.h</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>1</GroupNumber>
<FileNumber>5</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>.\melody.c</PathWithFileName>
<FilenameWithoutPath>melody.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>1</GroupNumber>
<FileNumber>6</FileNumber>
<FileType>5</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>.\melody.h</PathWithFileName>
<FilenameWithoutPath>melody.h</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
</Group>
<Group>

View File

@ -10,7 +10,7 @@
<TargetName>MCB1760 mit LPC1768</TargetName>
<ToolsetNumber>0x4</ToolsetNumber>
<ToolsetName>ARM-ADS</ToolsetName>
<pCCUsed>5060750::V5.06 update 6 (build 750)::ARMCC</pCCUsed>
<pCCUsed>5060960::V5.06 update 7 (build 960)::.\ARMCC</pCCUsed>
<uAC6>0</uAC6>
<TargetOption>
<TargetCommonOption>
@ -185,6 +185,7 @@
<uocXRam>0</uocXRam>
<RvdsVP>0</RvdsVP>
<RvdsMve>0</RvdsMve>
<RvdsCdeCp>0</RvdsCdeCp>
<hadIRAM2>1</hadIRAM2>
<hadIROM2>0</hadIROM2>
<StupSel>8</StupSel>
@ -351,7 +352,7 @@
<NoWarn>0</NoWarn>
<uSurpInc>0</uSurpInc>
<useXO>0</useXO>
<uClangAs>0</uClangAs>
<ClangAsOpt>4</ClangAsOpt>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
@ -392,6 +393,26 @@
<FileType>5</FileType>
<FilePath>.\Welcome.h</FilePath>
</File>
<File>
<FileName>audio.c</FileName>
<FileType>1</FileType>
<FilePath>.\audio.c</FilePath>
</File>
<File>
<FileName>audio.h</FileName>
<FileType>5</FileType>
<FilePath>.\audio.h</FilePath>
</File>
<File>
<FileName>melody.c</FileName>
<FileType>1</FileType>
<FilePath>.\melody.c</FilePath>
</File>
<File>
<FileName>melody.h</FileName>
<FileType>5</FileType>
<FilePath>.\melody.h</FilePath>
</File>
</Files>
</Group>
<Group>

106
audio.c Normal file
View File

@ -0,0 +1,106 @@
#include "audio.h"
#include "Board_DAC.h"
#include "LPC17xx.h"
#include "melody.h"
#define FREQ(x) ((x & 0xffff0000) >> 16)
#define WAVES(x) (x & 0xffff)
struct AudioState
{
bool highState;
uint16_t u16RemainingWaves;
uint32_t u32EndBreak;
const uint32_t *cpu32NextAction;
uint32_t u32StackPointer;
const uint32_t *cpu32Stack[10];
};
static struct AudioState sAudioState;
bool bPerformAudioStep()
{
bool bSuccess = true;
struct AudioState *psState = &sAudioState;
psState = psState;
sAudioState.highState = !sAudioState.highState;
if (sAudioState.highState)
{
DAC_SetValue(512);
if (sAudioState.u16RemainingWaves == 0)
{
LPC_TIM0->MR0 = sAudioState.u32EndBreak;
}
}
else
{
DAC_SetValue(0);
if (sAudioState.u16RemainingWaves > 0)
{
sAudioState.u16RemainingWaves--;
}
else
{
if (FREQ(*sAudioState.cpu32NextAction) == 0)
{
sAudioState.highState = true;
sAudioState.cpu32NextAction = cppu32JumpLabels[*sAudioState.cpu32NextAction];
bSuccess = false;
}
else if (FREQ(*sAudioState.cpu32NextAction) == 1)
{
sAudioState.highState = true;
sAudioState.cpu32Stack[sAudioState.u32StackPointer] = sAudioState.cpu32NextAction + 1;
sAudioState.cpu32NextAction = cppu32JumpLabels[WAVES(*sAudioState.cpu32NextAction)];
sAudioState.u32StackPointer++;
bSuccess = false;
}
else if (FREQ(*sAudioState.cpu32NextAction) == 2)
{
sAudioState.highState = true;
sAudioState.u32StackPointer--;
sAudioState.cpu32NextAction = sAudioState.cpu32Stack[sAudioState.u32StackPointer];
bSuccess = false;
}
else
{
uint32_t u32Frequency = FREQ(*sAudioState.cpu32NextAction);
LPC_TIM0->MR0 = u32Frequency * 2;
uint32_t u32Waves = WAVES(*sAudioState.cpu32NextAction);
sAudioState.u16RemainingWaves = u32Waves * 9 / 10 - 1;
sAudioState.u32EndBreak = u32Waves * u32Frequency * 2 / 5;
sAudioState.cpu32NextAction++;
}
}
}
return bSuccess;
}
void TIMER0_IRQHandler()
{
LPC_TIM0->IR = 0x1; // acknowledge interrupt
while (!bPerformAudioStep())
{
}
}
// Initializes the interrupt-driven audio output
void vStartAudio()
{
sAudioState.highState = true;
sAudioState.u16RemainingWaves = 0;
sAudioState.cpu32NextAction = cpu32EntryPoint;
sAudioState.u32StackPointer = 0;
// Init timerinterrupt 0 (20 ms)
LPC_SC->PCONP |= (1 << 2); // turn on
LPC_SC->PCLKSEL0 |= (1 << 2); // select clock
LPC_TIM0->MR0 = 100000 * 2 - 1;
LPC_TIM0->MCR = 0x3;
NVIC_SetPriority(TIMER0_IRQn, 1U); // priority
NVIC_EnableIRQ(TIMER0_IRQn);
LPC_TIM0->TCR = 1; // start
}

8
audio.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef AUDIO_H
#define AUDIO_H
#include <stdbool.h>
void vStartAudio(void);
#endif

90
melody.c Normal file
View File

@ -0,0 +1,90 @@
#include "melody.h"
// freq: wave duration as multiple of 40ns
// duration: total duration as multiple of 40ns
#define TONE(freq, duration) ((((uint32_t)freq & 0xffff) << 16) | (((uint32_t)duration / (uint32_t)freq) & 0xffff))
const uint32_t TACT = 2120 * 1000 * 25; // 2.12 seconds for one tact
const uint32_t GIS2 = 30098;
const uint32_t F = 35793;
const uint32_t DIS = 40177;
const uint32_t CIS = 45097;
const uint32_t C = 47778;
const uint32_t B = 53629;
const uint32_t GIS = 60197;
const uint32_t CALL = 0x10000;
const uint32_t RETURN = 0x20000;
const uint32_t refrain[];
const uint32_t quadrupel[];
const uint32_t *cpu32EntryPoint = refrain;
const uint32_t GOTO_REFRAIN = 0;
const uint32_t CALL_QUADRUPEL = CALL + 1;
const uint32_t *jumplabels[] = {
refrain,
quadrupel,
};
const uint32_t **cppu32JumpLabels = jumplabels;
const uint32_t quadrupel[] = {
TONE(GIS, TACT / 16),
TONE(B, TACT / 16),
TONE(CIS, TACT / 16),
TONE(B, TACT / 16),
RETURN,
};
const uint32_t refrain[] = {
CALL_QUADRUPEL,
TONE(F, TACT * 3 / 16),
TONE(F, TACT * 3 / 16),
TONE(DIS, TACT * 3 / 8),
CALL_QUADRUPEL,
TONE(DIS, TACT * 3 / 16),
TONE(DIS, TACT * 3 / 16),
TONE(CIS, TACT * 3 / 16),
TONE(C, TACT / 16),
TONE(B, TACT / 8),
CALL_QUADRUPEL,
TONE(CIS, TACT / 4),
TONE(DIS, TACT / 8),
TONE(C, TACT * 3 / 16),
TONE(B, TACT / 16),
TONE(GIS, TACT / 4),
TONE(GIS, TACT / 8),
TONE(DIS, TACT / 4),
TONE(CIS, TACT / 2),
CALL_QUADRUPEL,
TONE(F, TACT * 3 / 16),
TONE(F, TACT * 3 / 16),
TONE(DIS, TACT * 3 / 8),
CALL_QUADRUPEL,
TONE(GIS2,TACT / 4),
TONE(C, TACT / 8),
TONE(CIS, TACT * 3 / 16),
TONE(C, TACT / 16),
TONE(B, TACT / 8),
CALL_QUADRUPEL,
TONE(CIS, TACT / 4),
TONE(DIS, TACT / 8),
TONE(C, TACT * 3 / 16),
TONE(B, TACT / 16),
TONE(GIS, TACT / 4),
TONE(GIS, TACT / 8),
TONE(DIS, TACT / 4),
TONE(CIS, TACT / 2),
GOTO_REFRAIN,
};

9
melody.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef MELODY_H
#define MELODY_H
#include "stdint.h"
extern const uint32_t *cpu32EntryPoint;
extern const uint32_t **cppu32JumpLabels;
#endif