Full text
Final Deg ee P ojec
Bachelo 's deg ee in Indus ial Technology
Enginee ing
Music Qua e based on a PSoC
REPORT
Au ho : An onio Ma ínez Ga cía
Di ec o : Manuel Mo eno Eguilaz
Submission: Janua y 2017
Escola Tècnica Supe io
d’Enginye ia Indus ial de Ba celona
Music qua e based on a PSoC Page 1
Abs ac
This epo de ails he mig a ion p ocess o he Qua e code om a Mic ochip PIC24
mic ocon olle , which was p e iously mig a ed by Pau Mendie a om a Mic ochip PIC18
mic ocon olle , o a CY8CKIT-042-BLE PSoC 4 mic ocon olle manu ac u ed by Cyp ess
Semiconduc o . I also explains how se e al imp o emen s, bo h in sound quali y and
p og am unc ionali y, ha e been implemen ed in o he inal p og am once he mig a ion was
comple ed.
The o iginal Qua e code, de eloped by Vic o Timo ee , syn hesized 4 di e en oices
( wo gui a s, a iolin and a bass) coo dina ed by a conduc o ask, each o which played
om i s own music shee , which was s o ed in ROM. The sound samples we e gene a ed
using ampli ude modula ion: he cha ac e is ic wa e o m o he ins umen (also s o ed in
ROM) and i s ampli ude en elope (gene a ed h ough so wa e) we e mul iplied, and la e
expo ed h ough an 8-bi PWM unning a 78 kHz. These could la e be played on speake s
wi h he help o an RC il e .
The p ojec was ca ied ou by con inuously es ing he so wa e on he ac ual
mic ocon olle . Mo eo e , da a was ex ac ed om hese es s and compa ed wi h he
simula ion o Pe e Domenech’s PIC18 code. A g ea use o he example p ojec s p o ided
by Cyp ess Semiconduc o , which demons a e he ea u es o he PSoC 4 h ough simple
applica ions, has also been made. I was also essen ial o analyze di e en signals wi h an
oscilloscope a he labo a o y.
The p og am ob ained a e comple ing he p ojec expo s he audio signal h ough a 16-bi
PWM unning a 92 kHz and includes an ex a oice (a second iolin), mo e accu a e
cha ac e is ic wa e o ms and imp o ed ampli ude en elopes, as well as added unc ionali y,
such as an ex a oc a e and a wide no e equency ange.
The epo i s p esen s he basic ope a ion o he o iginal code, which was ca e ully s udied
a he beginning o he p ojec . Then, i explains how he OS (F eeRTOS) and he p og am
we e mig a ed, along wi h he ex ensi e oubleshoo ing p ocess ha ollowed. Mo eo e , i
de ails he imp o emen s ha we e made once he code wo ked co ec ly. Finally,
sugges ions a e made o u u e imp o emen s.
Page 2 Repo
Con en s
ABSTRACT _______________________________________________________ 1
CONTENTS _______________________________________________________ 2
1. GLOSSARY ___________________________________________________ 5
2. PREFACE ____________________________________________________ 6
2.1. P io wo ks ela ed o his p ojec ..............................................................................6
2.2. Mo i a ion ..................................................................................................................7
2.3. P e ious equi emen s ..............................................................................................7
3. INTRODUCTION _______________________________________________ 8
3.1. Objec i es .................................................................................................................8
3.2. Scope o he p ojec ..................................................................................................8
4. BASIC QUARTET OPERATION __________________________________ 10
4.1. The Conduc o ........................................................................................................10
4.2. The Ins umen s ......................................................................................................10
4.3. The Syn hesize ......................................................................................................11
4.4. Pulse Wid h Modula ion ..........................................................................................13
5. SOFTWARE MIGRATION _______________________________________ 14
5.1. F eeRTOS mig a ion...............................................................................................14
5.2. F eeRTOS con igu a ion .........................................................................................14
5.2.1. Co-ope a i e schedule .................................................................................................... 14
5.3. In oducing he TCPWM block ................................................................................15
5.4. Qua e code mig a ion ...........................................................................................16
5.4.1. Changes made o he ampli ude modula ion .................................................................. 16
5.4.2. Changes made o he PWM ............................................................................................ 17
6. QUARTET TROUBLESHOOTING ________________________________ 18
6.1. Adjus ing emp_dac ................................................................................................19
6.2. Inc easing clock speed ...........................................................................................20
6.3. Ex ac ing da a om he PSoC 4 ............................................................................21
6.4. Compa ison be ween PSoC 4 and PIC18 ..............................................................23
6.4.1. Tes ing condi ions ............................................................................................................ 23
6.4.2. S2. ................................................................................................................................... 24
6.4.3. Temp1 .............................................................................................................................. 25
6.4.4. S2. ................................................................................................................................... 25
6.4.5. Temp2 .............................................................................................................................. 26
6.4.6. Temp_dac ........................................................................................................................ 27
Music qua e based on a PSoC Page 3
6.4.7. Compa ison conclusions ................................................................................................. 27
6.5. Tempo al analysis .................................................................................................. 28
6.5.1. PWM equency e i ica ion ............................................................................................ 28
6.5.2. In e up equency .......................................................................................................... 28
6.5.3. Time implemen a ion...................................................................................................... 29
6.5.4. In e up equency e i ica ion ........................................................................................ 30
6.5.5. Tickless idle ..................................................................................................................... 32
6.6. Final co ec ions ..................................................................................................... 33
6.6.1. PWM block a ia ion ....................................................................................................... 33
6.6.2. Sound quali y co ec ion .................................................................................................. 34
7. QUARTET IMPROVEMENTS ___________________________________ 35
7.1. 16-bi PWM ............................................................................................................ 35
7.2. New ampli ude en elopes ...................................................................................... 36
7.3. Wa e o m ables wi h 128 da a poin s ................................................................... 39
7.3.1. Da a in e pola ion ............................................................................................................ 39
7.3.2. Da a eading me hod ....................................................................................................... 40
7.3.3. P og am modi ica ions .................................................................................................... 40
7.4. Wa e o m ables wi h 256 da a poin s ................................................................... 41
7.4.1. In e pola ed da a .............................................................................................................. 41
7.4.2. Code modi ica ions .......................................................................................................... 43
7.5. Wa e o m ables wi h 512 da a poin s ................................................................... 43
7.6. B oadening he no e equency ange ................................................................... 44
7.6.1. Du a ion modi ica ions ..................................................................................................... 44
7.6.2. Change in o he in ege ype ........................................................................................... 44
7.6.3. Command e-s uc u e .................................................................................................... 44
7.7. Adding an ex a oc a e .......................................................................................... 45
7.8. Adding an ex a oice ............................................................................................ 46
7.8.1. No elis and sound channel ............................................................................................. 46
7.8.2. Syn hesize modi ica ions ................................................................................................ 46
7.8.3. F eeRTOS ela ed modi ica ions .................................................................................... 47
8. FINAL QUARTET ANALYSIS ___________________________________ 50
8.1. Resou ce usage (4 oices) .................................................................................... 50
8.2. Tempo al analysis (4 oices) ................................................................................. 50
8.2.1. PWM equency ............................................................................................................... 50
8.2.2. In e up equency .......................................................................................................... 51
8.2.3. Tempo ............................................................................................................................. 51
8.2.4. Compu a ional e o o syn hesizing a sound sample .................................................... 52
8.2.5. Compu a ional e o o adjus ing emp_dac ................................................................... 53
8.2.6. Command p ocessing ..................................................................................................... 54
8.3. Resou ce usage (5 oices) .................................................................................... 55
8.4. Tempo al analysis (5 oices) ................................................................................. 55
Page 4 Repo
8.4.1. In e up du a ion .............................................................................................................. 55
9. FUTURE IMPROVEMENTS _____________________________________ 56
9.1. Blue oo h .................................................................................................................56
9.2. U ilizing ha dwa e blocks ........................................................................................56
9.3. Adding new ins umen s .........................................................................................56
9.4. Mul iple sound ou pu s ............................................................................................57
9.5. Widening he ange o no e du a ions .....................................................................57
10. PLANNING __________________________________________________ 58
11. BUDGET ____________________________________________________ 59
12. ENVIRONMENTAL IMPACT _____________________________________ 60
CONCLUSION ____________________________________________________ 61
BIBLIOGRAPHY __________________________________________________ 62
Music qua e based on a PSoC Page 5
1. Glossa y
BLE: Blue oo h Low Ene gy
BPM: Bea s Pe Minu e
CAN: Con olle A ea Ne wo k
CPU: Cen al P ocessing Uni
DAC: Digi al o Analog Con e e
IDE: In eg a ed De eloping En i onmen
ISR: In e up Se ice Rou ine
LED: Ligh -Emi ing Diode
OS: Ope a ing Sys em
PIC: Pe iphe al In e ace Con olle
PSoC: P og ammable Sys em on a Chip
PWM: Pulse Wid h Modula ion/Modula o
RC: Resis o -Capaci o
RF: Radio F equency
ROM: Read-Only Memo y
RTOS: Real Time Ope a ing Sys em
SCB: Se ial Communica ion Block
SRAM: S a ic Random Access Memo y
TCPWM: Time Coun e Pulse Wid h Modula o
UART: Uni e sal Asynch onous Recei e -T ansmi e
UDB: Uni e sal Digi al Block
USB: Uni e sal Se ial Bus
Page 6 Repo
2. P e ace
2.1. P io wo ks ela ed o his p ojec
The o igin o his p ojec goes back o Vic o Timo ee ’s usage example o he OSA RTOS
[1]. Timo ee designed a p og am, which he named “Qua e ”, ha was able o syn hesize a
4-channel melody and expo i using an 8-bi PWM unning a 78 kHz so ha , wi h he help
o an RC il e , i could be played on speake s o headphones. The o iginal code was w i en
o a PIC16 mic ocon olle and used he OSA RTOS as an ope a ing sys em.
In 2015 Pe e Domenech, a s uden a ETSEIB (Escola Tècnica Supe io d’Enginye ia
Indus ial de Ba celona), succeeded in mig a ing Timo ee ’s code so ha i could be
execu ed using a PIC18 mic ocon olle [2]. He chose his mic ocon olle in pa icula
because i is widely a ailable a he Depa men o Elec onics a said uni e si y.
Two yea s la e ano he s uden , Pau Mendie a, mig a ed Pe e Domenech’s code so ha a
PIC24 could un i [3]. This mean a change om an 8-bi CPU o a 16-bi CPU and,
he e o e, he expec ed o imp o e he sound quali y by inc easing he PWM esolu ion. In
he end, howe e , only a 9-bi PWM unning a 31.25 kHz was achie ed and, hus, he audio
quali y did no imp o e signi ican ly. He also made he necessa y changes so ha he code
could un on F eeRTOS [4], a mo e uni e sal ope a ing sys em which is easie o mig a e.
O he ela ed p ojec s include Juan Gallos a’s RF Music Fes i al: o ques a basada en
mic ocon olado es PIC18 y RF [5] and Joan Cal e ’s CAN Music Fes i al: O ques a
Basada en Mic ocon olado s PIC18 i un bus CAN [6]. The o me designed a musical
o ches a whe e each ins umen was indi idually syn hesized by ou di e en PIC18
mic ocon olle s and he conduc o sen commands o no es o each ins umen h ough RF.
The la e pu sued he same objec i e, bu using a CAN bus ins ead o RF as a means o
communica ion.
Music qua e based on a PSoC Page 7
2.2. Mo i a ion
The easons ha jus i ied he c ea ion o his p ojec a e he ollowing:
Lea n how o p og am a mic ocon olle in C language.
Lea n how o use a PSoC and ge amilia ized wi h i s speci ic IDE.
Unde s and how a RTOS, mo e speci ically F eeRTOS, wo ks.
Apply he knowledge and skills lea ned in an enginee ing deg ee o a mo e p ac ical
case.
Upg ade an exis ing code so ha i p oduces an imp o ed esul .
Lea n a me hod o syn hesizing music.
Deepen he pe sonal knowledge in he ields o elec onics, p og amming and
music.
2.3. P e ious equi emen s
Since he o iginal code is w i en in C language, i is essen ial o ha e a ce ain deg ee o
unde s anding o his p og amming language be o e he ac ual p ojec can begin. In his
case, an in oduc o y u o ial was consul ed [7]. Knowing he basics o digi al elec onics is
also necessa y.
Mo eo e , one has o amilia ize hemsel es wi h he PSoC speci ic IDE, PSoC C ea o 4.1
[8], in o de o de elop he code, ans e i o he p ocesso and manage he chip’s
con igu able ha dwa e blocks. Wi h ega d o hese componen s, some o hei da ashee s
ha e o be s udied ca e ully, namely he PWM [9], TCPWM [10], SCB [11], Clock [12],
In e up [13] and Pins [14] componen s.
In addi ion, i is impo an o s udy he pin dis ibu ion on he ki [15] whene e cable
connec ions ha e o be made and o unde s and he undamen al beha io and
cha ac e is ics o he F eeRTOS ope a ing sys em [16].
Page 14 Repo
5. So wa e mig a ion
5.1. F eeRTOS mig a ion
The mig a ion p ocess began by es ing an example p ojec p o ided by Cyp ess
Semiconduc o in which F eeRTOS was speci ically implemen ed o he PSoC 4 BLE [17].
The p ojec was buil using PSoC C ea o 4.1 [8].
This example basically de ec s when he SW2 swi ch o he CapSense Slide a e p essed
and sends a message h ough one UART acco dingly. Once es ed co ec ly i was possible
o mo e on o he con igu a ion o he OS.
5.2. F eeRTOS con igu a ion
Fo he Qua e code o wo k co ec ly i is necessa y o change some pa ame e s inside he
F eeRTOS con igu a ion. This is done by edi ing he F eeRTOSCon ig.h heade ile. Apa
om disabling p e-emp ion (see sec ion 5.2.1), a i s all o he op ions we e le a hei
de aul se ing.
5.2.1. Co-ope a i e schedule
F eeRTOS allows he p e-emp ion o asks, which means ha du ing a ick in e up ( he
OS’s own so wa e-gene a ed in e up ) a highe p io i y ask can o ce he unning ask ou
o unning s a e, eplacing i .
In case o he Qua e code, a con ex swi ch (changing which ask is unning) only happens
when he ins umen s en e he blocked s a e o wai o he conduc o o gi e a semapho e
o hem. The e o e, he co-ope a i e ask schedule was chosen, which is equi alen o
disabling p e-emp ion.
Music qua e based on a PSoC Page 15
5.3. In oducing he TCPWM block
The nex logical s ep in he mig a ion p ocess is o es he PSoC 4’s TCPWM block and
unde s and how i wo ks. In o de o do his, a TCPWM example p o ided by Cyp ess
Semiconduc o was consul ed [18].
In his example p ojec , he b igh ness o a LED is con olled using he TCPWM block. Once
i was success ully es ed in sepa a e, all necessa y code and con igu a ion was hen mo ed
o he F eeRTOS example o check i bo h p ojec s we e compa ible. Mo e speci ically, i
was necessa y o:
- Replace all p e iously exis ing blocks in he TopDesign.cysch ile wi h he TCPWM
block (see Fig. 5.2).
- Enable he in e up componen linked o he TCPWM block (TC_ISR). This is done
using he ollowing command in he main() unc ion:
TC_ISR_S a Ex(In e up Handle );
- S a he TCPWM componen and con igu e i s pa ame e s. This is also done in he
main() unc ion, e en hough he la e can also be done by di ec ly edi ing he
block’s p ope ies:
PWM_S a ();
PWM_W i ePe iod(65300u);
- Edi he ISR unc ion so ha i handles he TC_ISR in e up (see Fig. 5.1).
- Remo e unnecessa y code om he i s example.
Figu e 5.1: ISR unc ion ha handles he TCPWM in e up . Sou ce: [18]
Page 16 Repo
Figu e 5.2: TCPWM block, loca ed in he TopDesign.cysch ile, which con ols he b igh ness o a g een
LED. Sou ce: [18]
A e comple ing hese s eps, he TCPWM example wo ked as expec ed inside he
F eeRTOS p ojec . Now ha he ope a ing sys em’s unc ions we e a ailable and he
TCPWM block was implemen ed, all in he same p ojec , i was possible o p oceed o he
mig a ion o he ac ual Qua e code.
5.4. Qua e code mig a ion
The i s s ep in he code mig a ion was o copy all Qua e -speci ic iles (sinus.h,
bach1067.h and elochka.h) in o he p ojec ’s olde . A e doing so, Pau Mendie a’s code [3]
had o be implemen ed in o he p ojec ’s main.c ile. Ne e heless, some modi ica ions we e
s ill necessa y.
5.4.1. Changes made o he ampli ude modula ion
As explained in he p e ious chap e , he inal s ep in he syn hesizing p ocess is o mul iply
wo signals, which was o iginally done using assembly language o mul iply wo a iables.
Since mig a ing his piece o code would no be i ial ( he assembly language would be
d as ically di e en ) and he PSoC 4’s p ocesso is mo e powe ul han he PIC24’s, his
ope a ion was implemen ed as a simple p oduc be ween wo a iables in C language,
which is mo e cos ly (compu a ionally).
Music qua e based on a PSoC Page 17
5.4.2. Changes made o he PWM
All o he code ela ed o he sound syn hesizing had o be in oduced in o he unc ion ha
handles he in e up s while espec ing he s uc u e seen in sec ion 5.3 (see Fig. 5.1). The
main di e ence is ha he in e up has o be clea ed a he s a o he unc ion and ha he
pulse wid h is modula ed wi h he PWM_W i eCompa e( emp_dac) command, whe e
emp_dac is he a iable ha holds he inal syn hesized sample o all 4 oices.
I is also impo an o i he pe iod o he PWM so ha i s equency ma ches he desi ed
alue. In o de o achie e a PWM equency o 78 kHz while using a 24 MHz clock, he
pe iod has o be 300 (see calcula ion below).
Page 18 Repo
6. Qua e oubleshoo ing
The aim o he mig a ion p ocess desc ibed in he p e ious chap e was o achie e he
same acous ic esul wi h he PSoC 4 as he one ob ained wi h he PIC mic ocon olle s.
The mig a ed p og am did p oduce sound, bu i was signi ican ly di e en han wha was
expec ed. The main issues we e:
- The sound was highly sa u a ed.
- The no es played by he p og am we e o a much highe equency han hey should
be.
- The melody could only be dis inguished when playing one ins umen a a ime. I
wo o mo e ins umen s we e played simul aneously hey ell ou o synch oniza ion
and he sound became dis o ed.
- The empo was a slowe han he o iginal.
- The sound had an o e all poo quali y.
In his chap e , he p ocess o de ec ing and sol ing he mis akes ha caused hese aul s
will be explained in de ail. This has been he mos challenging and ime-consuming pa o
he p ojec . Hence, o simpli y i , se e al cha ac e is ics o he esul ing sound (lis ed below)
ha e been e alua ed o e e y possible ix made o he p ojec , in acco dance wi h he
legend p esen ed in able 6.1.
- Cla i y: A highly sa u a ed audio signal has a loud c acking sound added o i
(simila o whi e noise). This makes i bo he some o lis en o and some imes e en
makes i di icul o iden i y he melody ha is being played. High cla i y will imply li le
sa u a ion and ice e sa.
- Accu acy o no e equency: Musical no es ha e a cha ac e is ic equency.
The e o e, a syn hesize ha wo ks co ec ly should gene a e an audio signal wi h
he equency o he desi ed no e.
- Quali y: This is a mo e subjec i e pa ame e . Highe quali y he e will imply a mo e
de ailed and ealis ic sound.
- Co ec in e ac ion be ween ins umen s: T oubleshoo ing is made easie by
analyzing a single oice, bu i is also impo an o check ha he sum o all 4 oices
plays co ec ly.
Music qua e based on a PSoC Page 19
Symbol
Meaning
+
Sligh imp o emen
++
Signi ican imp o emen
+++
C i ical imp o emen
/
No di e ence
-
Sligh wo sening
--
Signi ican wo sening
---
C i ical wo sening
?
No es ed
Table 6.1: Legend o he symbols used o desc ibe he imp o emen /wo sening o he audio esul
E alua ion ables a e always ela i e o he bes con igu a ion ha has been ound a a
pa icula poin . Ob iously, changes ha ha e a posi i e e ec on he esul a e kep and
hose ha do no a e disca ded.
6.1. Adjus ing emp_dac
A i s , i was hough ha emp_dac, he a iable ha holds he inal syn hesized sample,
was no p ope ly adjus ed o he ange o alues accep ed by he PWM. Since he PWM
pe iod was se o 300, any emp_dac alue ou side he ange o 0-300 would p oduce
unexpec ed esul s.
In he o iginal code, once all o he 4 oices we e added he esul was di ided by 4 and,
la e , an o se o 128 was added. This las ope a ion was done because he wa e o m
ables include nega i e alues. Howe e , since he mul iplica ion ope a ion used in his case
is sligh ly di e en , he alues p oduced by he mig a ed code we e o a highe o de , and
hus he a iable had o be di ided by a highe numbe .
Se e al combina ions o di iso and o se we e es ed and e alua ed empi ically. Wi h a
single ac i e oice, he bes esul was achie ed by di iding by 512 (shi ing 9 bi s o he
igh ) and adding an o se o 220. The sound became sligh ly less sa u a ed, bu mos o
he issues pe sis ed (see able 6.2). I was he e o e easonable o assume ha he e was a
mo e se ious p oblem wi h he mig a ed code ha emained unde ec ed and was
Page 20 Repo
esponsible o he poo quali y o he esul .
Cla i y
+
Accu acy
/
Quali y
/
Co ec in e ac ion
?
Table 6.2: Sound e alua ion a e adjus ing he emp_dac a iable
6.2. Inc easing clock speed
Be o e s a ing a mo e de ailed analysis o he mig a ed code’s beha io , i was decided ha
i would be in e es ing o inc ease he equency o he clock ha con ols he TCPWM
block. Un il now, i had been unning a he same equency as he ex e nal clock
(EXTCLK), 24 MHz.
Howe e , he TCPWM block allows clocks o ope a e up o a maximum o 48 MHz [10].
Choosing his clock speed means ha , i he same PWM equency is kep , he PWM pe iod
should now be 600, hus doubling i s esolu ion. In o he wo ds, he emp_dac a iable
would be able o hold double he amoun o di e en alues han i p e iously could. This
would be equi alen o a 9-bi esolu ion.
A e es ing his modi ica ion (see eco ding #1 o he annex), he quali y inc eased sligh ly
bu so did he sa u a ion (see able 6.3). S ill, o e all his e sion o he mig a ed code
sounded be e .
Cla i y
-
Accu acy
/
Quali y
+
Co ec in e ac ion
?
Table 6.3: Sound e alua ion a e inc easing he clock speed o 48 MHz
Music qua e based on a PSoC Page 21
6.3. Ex ac ing da a om he PSoC 4
Once he PWM esolu ion and he adjus men o he emp_dac a iable we e disca ded
om being esponsible o he majo sound issues, a mo e de ailed and comp ehensi e
analysis o he code’s beha io was deemed necessa y.
Up o his poin all da a had been ex ac ed om he PSoC using b eakpoin s in PSoC
C ea o 4.1. This p ocedu e was enough o check he s a e o a iables a ce ain poin s o
execu ion, bu i made sa ing la ge amoun s o da a almos impossible (sa ing da a om
each in e up , o example). The e o e, i was necessa y o ind ano he way o ex ac
in o ma ion.
This was done by ollowing an online u o ial ha demons a ed how o implemen he p in
C unc ion in o he PSoC (by de aul he unc ion compiles wi hou e o s, bu i does no hing
when called) [19].
Fi s , a UART (SCB mode) block was placed in o he TopDesign.cysch ile wi h i s s anda d
con igu a ion. Then, i was assigned o he co ec pins, P1.5 and P1.4 (see Fig. 6.1) and,
a e wa ds, a cable connec ion was made om P1.5 o P12.6 and om P1.4 o P12.7 (see
Fig. 6.2).
Figu e 6.1: Pin assignmen o he UART block. Sc eensho aken om PSoC C ea o 4.1.
Page 22 Repo
Figu e 6.2: PSoC 4 a e making he cable connec ions o he UART block (g een cables). Sou ce: own.
The ollowing s eps consis ed in inc easing he heap size, which is done in he “Sys em” ab
in he .cydw ile, and adding he _w i e() unc ion o he main.c ile (see Fig. 6.3). Mo eo e ,
he UART block has o be s a ed wi h he UART_S a () command.
Figu e 6.3: Addi ional code necessa y o he implemen a ion o he p in unc ion. Sou ce: [19].
Finally, he p og am PuTTY [20] was ins alled in o de o ecei e he in o ma ion ha he
PSoC would be ansmi ing h ough USB.
Music qua e based on a PSoC Page 23
6.4. Compa ison be ween PSoC 4 and PIC18
In o de o disco e wha had gone w ong in he mig a ion p ocess, a comp ehensi e
compa ison o he PSoC and PIC18’s beha io was done. The PSoC’s da a was ex ac ed
using he UART block (as seen in sec ion 6.3), while he PIC18 was analyzed by simula ing
Pe e Domenech’s code [2] wi h he use o MPLAB IDE [21].
6.4.1. Tes ing condi ions
Bo h sys ems we e es ed in he ollowing condi ions:
- Only one oice ( he iolin) was playing.
- The only no e played was b0 (in Qua e no a ion) wi h a base scale o 0 and a
du a ion o 4.
- The a iables whose alues we e s o ed o e e y in e up we e:
S2. : Wo ks as an index o selec ing alues om he ables in sinus.h.
emp1: Holds he alue ead om he ables in sinus.h.
S2. : Inc eases by one e e y 64 in e up s and is used o gene a e he
ampli ude en elope.
emp2: Holds he alue o he ampli ude en elope.
emp_dac: Holds he alue a e he ampli ude modula ion and be o e i is
adjus ed.
Page 30 Repo
Some modi ica ions o he code in main.c we e also necessa y:
- The Time block has o be s a ed in main():
Time _S a ();
- The in e up associa ed wi h he ime has o be clea ed a he s a o he in e up
handle unc ion ( eplacing he PWM in e up ):
Time _Clea In e up (Time _INTR_MASK_TC);
The modi ied p ojec was es ed wi h posi i e esul s (see eco ding #2 o he annex): he
no e accu acy imp o ed signi ican ly, e en hough i was no ye co ec , and so did he
sound quali y (see able 6.4). S ill, he p og am did no sound as in ended.
Cla i y
/
Accu acy
++
Quali y
++
Co ec in e ac ion
?
Table 6.4: Sound e alua ion a e changing he in e up equency.
6.5.4. In e up equency e i ica ion
Jus as wi h he PWM, an oscilloscope was used o measu e he equency o in e up s. In
his case, howe e , an ou pu pin had o be con igu ed (see Fig. 6.12). I was ini ialized o 0
and was se o 1 (Ou pu PinSW_W i e(1u)) a he s a o he in e up handle unc ion and
back o 0 (Ou pu PinSW_W i e(0u)) a he end o i .
Figu e 6.12: So wa e ou pu pin in he TopDesign.cysch ile. Sou ce: Own.
Music qua e based on a PSoC Page 31
Figu e 6.13: Oscilloscope cap u e o he in e up signal. Sou ce: own.
In Fig. 6.13 he high le el ep esen s he amoun o ime spen inside an in e up . This
esul is qui e su p ising: No only is he in e up equency w ong (54 kHz), bu also he e is
a second, unexpec ed in e up ha happens almos immedia ely a e he i s .
A e u he es ing, i was concluded ha he OS was esponsible o his, because, i i was
disabled (by ne e s a ing he ask schedule ), he in e up beha io was pe ec ly co ec
(see Fig. 6.14).
Figu e 6.14: Oscilloscope cap u e o he in e up signal while F eeRTOS was disabled. Sou ce: own.
Page 32 Repo
6.5.5. Tickless idle
A e lea ning ha he OS was somehow in e e ing wi h he in e up s, i seemed
easonable o examine he F eeRTOS con igu a ion ile ca e ully. E e y hing seemed o be
in o de , excep o his line:
#de ine con igUSE_TICKLESS_IDLE 2
The F eeRTOS e e ence manual [23] was consul ed so as o lea n mo e abou i .
Appa en ly, i he ickless idle is ac i e (se o 1 o 2), and only he Idle ask ( he ask ha
uns when no o he asks a e able o) is unning, hen he mic ocon olle en e s a low
powe s a e. This means ha he ick in e up , du ing which he OS can selec a new ask o
en e he unning s a e, is s opped. This could explain he wei d beha io expe ienced
be o e.
Tes ing (see able 6.5) e ealed ha he in e ac ion be ween ins umen s was pe ec ly
co ec (see eco ding #3 o he annex): all o hem we e audible and synch onized. Sound
quali y also seemed o imp o e. As a downside, he e was mo e sa u a ion han be o e.
Cla i y
-
Accu acy
+
Quali y
++
Co ec in e ac ion
+++
Table 6.5: Sound e alua ion a e disabling he ickless idle.
The case o no e accu acy bea s special men ion. The oo o his issue lies in a mac o
called ‘Hz’ loca ed in elochka.h:
#de ine Hz *64L*256/10000
This mac o’s pu pose is o ansla e a equency measu ed in He z in o he numbe o
poin s in he sinus.h ables ha ha e o be skipped du ing each in e up . In o he wo ds: he
p og am is designed o be cohe en wi h he no e equencies only when he in e up
equency is 10 kHz. This is why he no es played a e now co ec .
Music qua e based on a PSoC Page 33
6.6. Final co ec ions
6.6.1. PWM block a ia ion
The sa u a ion no ed in he p e ious sec ion became wo ying, as he e was no way o
adjus ing he emp_dac a iable ha comple ely sol ed i . The e o e, a di e en block ha
could also wo k as a PWM was es ed. The TCPWM block ha was being used p e iously
was eplaced by he PWM block [9] (see Fig. 6.15).
Figu e 6.15: TopDesign.cysch ile o he main mig a ed p ojec a e eplacing he TCPWM block wi h a
PWM block. Sou ce: Adap a ion o [18] and [22].
The main disad an adge o his new block is ha he maximum clock equency i allows is
12 MHz. This means ha he PWM esolu ion will be lowe (a ound 7-8 bi s) because he
pe iod will necessa ily be lowe oo.
Page 34 Repo
Cla i y
+++
Accu acy
/
Quali y
---
Co ec in e ac ion
/
Table 6.6: Sound e alua ion a e swi ching he PWM block.
A e e alua ing he esul ing sound (see able 6.6), he sa u a ion disappea ed (see
eco ding #4 o he annex). Howe e , he quali y go signi ican ly wo se.
6.6.2. Sound quali y co ec ion
Fu he analysis o he code e ealed ha he sinus.h ables we e no being ead co ec ly
now. Mo e speci ically, he nega i e alues we e being con e ed in o posi i e ones. To
sol e his, all o he ables in sinus.h, along wi h he emp1 a iable, we e changed in o he
in ege ype (in he o iginal code hey belonged o he cha ac e ype).
Cla i y
/
Accu acy
/
Quali y
+++
Co ec in e ac ion
/
Table 6.7: Sound e alua ion a e changing he sinus.h ables in o he in ege ype.
A e e i ying ha he ables we e now being ead co ec ly, he sound was e-e alua ed
(see able 6.7). I seemed ha , wi h he inc ease in quali y achie ed, he sound esul was
inally equi alen o ha o he p e ious p ojec s (see eco ding #5 o he annex). The e o e,
he mig a ion p ocess had inished.
Music qua e based on a PSoC Page 35
7. Qua e imp o emen s
This chap e will be de o ed o explaining he imp o emen s made o he mig a ed Qua e
p og am. Mos o hem a e designed o imp o e sound quali y, bu some add ex a
unc ionali y o he p og am as well.
7.1. 16-bi PWM
The mos e ec i e way o imp o e sound quali y was o inc ease he PWM bi esolu ion. In
o de o do his, an exis ing p ojec by Ganesh Raaja was consul ed [24]. This p ojec
consis ed in achie ing a high esolu ion PWM by combining lowe esolu ion PWM blocks
(see Fig. 7.1).
Figu e 7.1: Implemen a ion o he 16-bi PWM. Sou ce: Adap a ion o [24].
The PWM1 block has wo 8-bi ou pu s wi h a di e ence in du y cycle o one clock. The
ou pu is selec ed by he ha dwa e cmp_sel signal, which is he ou pu signal o he PWM2
block. A 24 MHz clock is used o he PWM1 block (because i was he maximum equency
i allowed), while he PWM2 clock is he e minal coun ou pu om PWM1.
Page 36 Repo
The e a e also se e al unc ions ha need o be added o he code in main.c. The i s wo
a e he unc ions ha s a and s op he en i e 16-bi PWM (see Fig. 7.2).
Figu e 7.2: Func ions ha s a ( op) and s op (bo om) he 16-bi PWM. Sou ce: [24].
I is also necessa y o implemen he unc ion ha w i es he compa e alue (pulse wid h) o
he 16-bi PWM (see Fig. 7.3).
Figu e 7.3: Func ion ha w i es he compa e alue o he 16-bi PWM. Sou ce: [24].
The sound ha esul ed om in oducing he high esolu ion PWM imp o ed g ea ly when
compa ed o he p e ious e sion o he p og am (see eco ding #6 o he annex).
7.2. New ampli ude en elopes
The nex upg ade consis ed in changing he ampli ude en elopes along wi h he way hey
we e gene a ed. Ins ead o being c ea ed using simple ope a ions du ing in e up s, hey will
be s o ed in ROM, ha ing al eady been calcula ed p e iously. This will allow less linea
en elopes ha will gi e he sound a mo e na u al e olu ion.
Fig. 7.4, 7.5 and 7.6 show he o iginal and new ampli ude en elopes o e e y ins umen .
Music qua e based on a PSoC Page 37
Figu e 7.4: Compa ison be ween he o iginal and new ampli ude en elopes o he bass. Elabo a ed om
Vic o Timo ee ’s code.
Figu e 7.5: Compa ison be ween he o iginal and new ampli ude en elopes o he iolin. Elabo a ed om
Vic o Timo ee ’s code.
Figu e 7.6: Compa ison be ween he o iginal and new ampli ude en elopes o he gui a . Elabo a ed om
Vic o Timo ee ’s code.
Page 38 Repo
These new en elopes we e s o ed in a ays inside he sinus.h ile. Las ly, he code which
p e iously gene a ed he en elope had o be eplaced (see Fig. 7.7).
Figu e 7.7: Compa ison be ween he o iginal (le ) and new ( igh ) code inside he syn hesize ha deals
wi h he bass’s en elope. Adap ed om Vic o Timo ee ’s code.
The change in sound was sub le, bu when lis ening ca e ully i was e i ied ha he
ins umen s sounded mo e na u al (see eco ding #7 o he annex). In ac , he e ec can be
obse ed in he ac ual eco dings (see Fig. 7.8). The o iginal wa e o ms in he audio
eco ding we e mo e linea and a i icial, whe eas he new ones p esen mo e a iabili y.
Figu e 7.8: Compa ison be ween he 16-bi PWM eco ding wi h he o iginal en elopes ( op) and wi h he
new ones (bo om). Sou ce: own.
Music qua e based on a PSoC Page 39
7.3. Wa e o m ables wi h 128 da a poin s
Ano he way o imp o ing he sound quali y o he p og am is o inc ease he numbe o
da a poin s ha make up one pe iod o he cha ac e is ic wa e o m o e e y ins umen .
Mo e speci ically, he o iginal amoun o 64 alues will be doubled. The esul will be mo e
ealis ic sounding ins umen s.
7.3.1. Da a in e pola ion
The i s s ep in his p ocess is o in e pola e he o iginal da a poin s o c ea e a con inuous
unc ion. The o iginal alues we e p e iously ampli ied (mul iplied by 4) so ha he e was
oom o c ea e in e media e poin s. This was done o di e en pieces o he en i e se o
poin s, as inding a unc ion ha desc ibed he en i e wa e o m would be oo complica ed.
Fig. 7.9, 7.10 and 7.11 compa e he o iginal and new wa e o ms o e e y ins umen .
Figu e 7.9: Compa ison be ween he o iginal bass wa e o m and he new one wi h double he amoun o
da a poin s. Elabo a ed om Vic o Timo ee ’s da a in sinus.h.
Figu e 7.10: Compa ison be ween he o iginal iolin wa e o m and he new one wi h double he amoun
o da a poin s. Elabo a ed om Vic o Timo ee ’s da a in sinus.h.
Page 46 Repo
7.8. Adding an ex a oice
The las imp o emen made o he Qua e p og am consis ed in adding ano he oice. The
idea was o in oduce ano he iolin ha played an oc a e lowe han he o iginal one, hus
c ea ing a ha monized e ec be ween bo h (see eco ding #10 o he annex).
7.8.1. No elis and sound channel
The i s s ep was o copy he o iginal iolin no elis , ename i and se he oc a e o 2
ins ead o 3. Also, he swi ch o his new oice was c ea ed:
#de ine pin_ENABLE_VIOLIN2 1 // Swi ch iolin2 channel ON/OFF
Fu he mo e, a new sound channel had o be de ined:
TSound S5; // Fo channel 5 ( iolin2)
7.8.2. Syn hesize modi ica ions
Addi ional code had o be in oduced in o he in e up handle unc ion so ha he new oice
was co ec ly syn hesized (see Fig. 7.18 and 7.19).
Figu e 7.18: F agmen o code ha syn hesizes he sound sample o iolin2. Adap ed om Vic o
Timo ee ’s code.
Figu e 7.19: F agmen o code ha con ols he e olu ion o he indexes o he en elope ables. Adap ed
om Vic o Timo ee ’s code.
Music qua e based on a PSoC Page 47
7.8.3. F eeRTOS ela ed modi ica ions
The nex s eps, which we e mo e ela ed o he OS, consis ed in:
1. C ea ing a lag ha indica es ha iolin2 is playing (see Fig. 7.20). I was assigned o
he nex a ailable bi (0001 0000).
Figu e 7.20: De ini ion o lags o all 5 oices. Adap ed om Pau Mendie a’s code.
2. C ea ing a ask handle and a bina y semapho e o he new iolin2 ask (see Fig.
7.21).
Figu e 7.21: De ini ion o all ask handles and bina y semapho es o all 5 oices. Adap ed om Pau
Mendie a’s code.
3. De ining he new ask in he Task_De s.h ile (see Fig. 7.22).
Figu e 7.22: De ini ion o all asks in Task_De s.h. Adap ed om Pau Mendie a’s code.
Page 48 Repo
4. C ea ing he new ask and bina y semapho e in he main() unc ion (see Fig. 7.23).
The code o he new ask can be seen in Fig. 7.24.
Figu e 7.23: C ea ion o all asks and bina y semapho es. Adap ed om Pau Mendie a’s code.
Figu e 7.24: Violin2 ask. Adap ed om Pau Mendie a’s code.
5. Edi ing he conduc o ask so ha i gi es he semapho e o he iolin2 ask as well
(see Fig. 7.25).
Music qua e based on a PSoC Page 49
Figu e 7.25: Conduc o ask a e adding he ex a oice. Adap ed om Pau Mendie a’s code.
6. Re-adjus ing he emp_dac a iable.
Page 50 Repo
8. Final Qua e Analysis
A de ailed analysis o he inal imp o ed Qua e p og am, wi h and wi hou he ex a oice,
will be p esen ed in his chap e .
8.1. Resou ce usage (4 oices)
SRAM
8.984 KB (54.8%)
Flash
16.720 KB (12.8%)
S ack
1280 by es
Heap
1024 by es
UDB
50%
Table 8.1: Resou ces used by he inal imp o ed 4- oice Qua e p og am.
Table 8.1 shows he amoun o PSoC’s esou ces consumed by he inal p og am. The
SRAM and lash alues show ha he e is enough space o add mo e ins umen s o
imp o emen s. UDB, which measu es he amoun o digi al blocks used, shows ha
addi ional blocks could be included.
8.2. Tempo al analysis (4 oices)
8.2.1. PWM equency
Figu e 8.1: Oscilloscope cap u e o he inal PWM signal. Sou ce: own.
Music qua e based on a PSoC Page 51
The PWM equency, as shown in Fig. 8.1, has inc eased o 91.85 kHz because o he
in oduc ion o he high esolu ion PWM. Since i is highe han he o iginal (78 kHz), he e is
no p oblem wi h i .
8.2.2. In e up equency
Figu e 8.2: Oscilloscope cap u e o he inal in e up signal. Sou ce: own.
The in e up equency is app oxima ely 40 kHz (see Fig. 8.2), as was expec ed. The ime
spen in high le el is he du a ion o an in e up . The es o i is a ailable o he OS o ca y
ou i s own ope a ions, which in his case is mo e han enough.
8.2.3. Tempo
Figu e 8.3: Oscilloscope cap u e ha shows he ime be ween wo consecu i e eigh h no es. Sou ce:
own.
Page 52 Repo
The peaks shown in Fig. 8.3 ep esen he p ocessing o wo consecu i e eigh h no es.
The e o e, using he ime be ween hese wo no es (162.2 ms), he empo o he song can
be calcula ed:
8.2.4. Compu a ional e o o syn hesizing a sound sample
Figu e 8.4: Oscilloscope cap u e ha shows he amoun o ime equi ed o syn hesize a sound sample
o a single oice. Sou ce: own.
Fig. 8.4 shows ha i akes he p ocesso 1.8 µs o syn hesize one sound sample o he
bass. Assuming i akes mo e o less he same o syn hesize e e y oice, he p ocesso
would need a ound 7.2 µs o gene a e he sample o all 4 oices, which accoun s o 60%
o he o al in e up ime (see Fig. 8.2 o he o al in e up ime).
Music qua e based on a PSoC Page 53
8.2.5. Compu a ional e o o adjus ing emp_dac
Figu e 8.5: Oscilloscope cap u e ha shows he amoun o ime equi ed o adjus he emp_dac a iable.
Sou ce: own.
I is in e es ing o s udy he amoun o ime equi ed o adjus he emp_dac a iable
because in he inal p og am his is done using a ma hema ical di ision. This was no
possible in he PIC mic ocon olle s due o he high compu a ional cos o he ope a ion, so
ins ead i was done using he bina y igh shi ope a o (>>), which is equi alen o a di ision
by a powe o 2. The ma hema ical di ision, howe e , gi es mo e eedom when adjus ing
emp_dac, and hus allows mos o he PWM esolu ion o be u ilized.
Fig 8.5 e eals ha he ime spen making his adjus men is a ound 4.5 µs, 37% o he o al
in e up ime. The e o e, i clea ly is a cos ly ope a ion, e en hough in his case his is no a
p oblem due o how much ime is a ailable (in e up s ake only abou 50% o he a ailable
ime, as seen in Fig 8.2).
Page 54 Repo
8.2.6. Command p ocessing
Figu e 8.6: Oscilloscope cap u e showing he amoun o ime du ing which he ins umen s a e wai ing
o a command (high ol age). Sou ce: own.
The ime spen in low ol age in Fig 8.6 ep esen s he momen when he ins umen s a e
ecei ing a command om he conduc o . The es o i is spen wai ing o he command.
This shows how as commands a e p ocessed.
Music qua e based on a PSoC Page 55
8.3. Resou ce usage (5 oices)
SRAM
9.008 KB (55.0%)
Flash
17.624 KB (13.4%)
S ack
1280 by es
Heap
1024 by es
UDB
50%
Table 8.2: Resou ces used by he inal imp o ed 5- oice Qua e p og am.
As i can be seen in able 8.2, he addi ion o an ex a oice ba ely has an impac on he
esou ces consumed by he p og am.
8.4. Tempo al analysis (5 oices)
8.4.1. In e up du a ion
Figu e 8.7: Oscilloscope cap u e showing he in e up signal ( op, CH1) and he PWM signal (down,
CH2). Sou ce: own.
Fig. 8.7 shows ha an en i e in e up now las s 13.53 µs, which is 1.37 µs mo e han he 4-
oice e sion. The e is s ill enough ime o he OS o ca y ou i s asks.
Page 62 Repo
Bibliog aphy
[1] Vic o Timo ee , OSA RTOS, [h p://www.picosa.na od. u/].
[2] Pe e Domenech, Aplicaciones musicales del sis ema ope a i o en iempo eal OSA
RTOS, Ba celona 2015.
[3] Pau Mendie a, Audio applica ion based on F eeRTOS Ope a ing Sys em, Ba celona
2017.
[4] Real Time Enginee s L d., F eeRTOS O icial Websi e,
[h p://www. ee os.o g/RTOS.h ml]
[5] Juan Gallos a, RF Music Fes i al: o ques a basada en mic ocon olado es PIC18 y
RF, Ba celona 2015.
[6] Joan Cal e , CAN Music Fes i al: o ques a basada en mic ocon olado es PIC18 y
bus CAN, Ba celona 2016.
[7] Tu o ials Poin , C p og amming, [h ps://www. u o ialspoin .com/cp og amming/,
consul ed on Augus 2017].
[8] Cyp ess Semiconduc o Co po a ion, O icial PSoC C ea o Websi e,
[h p://www.cyp ess.com/p oduc s/psoc-c ea o -in eg a ed-design-en i onmen -ide]
[9] Cyp ess Semiconduc o Co po a ion, Pulse Wid h Modula o (PWM) 3.30 Componen
Da ashee , 2016.
[10] Cyp ess Semiconduc o Co po a ion, PSoC 4 Time Coun e Pulse Wid h Modula o
(TCPWM) 2.10 Componen Da ashee , 2016.
[11] Cyp ess Semiconduc o Co po a ion, PSoC 4 Se ial Communica ion Block (SCB) 4.0
Componen Da ashee , 2017.
[12] Cyp ess Semiconduc o Co po a ion, Clock 2.20 Componen Da ashee , 2017.
[13] Cyp ess Semiconduc o Co po a ion, In e up 1.70 Componen Da ashee , 2017.
[14] Cyp ess Semiconduc o Co po a ion, Pins 2.20 Componen Da ashee , 2017.
[15] Cyp ess Semiconduc o Co po a ion, CY8CKIT-042-BLE Quick S a Guide, 2015.
[16] Real Time Enginee s L d., Mas e ing he F eeRTOS™ Real Time Ke nel, 2016.
[17] Cyp ess Semiconduc o Co po a ion, F ee RTOS wi h PSoC 4 BLE,
[h ps://cyp ess.hacks e .io/42177/ ee- os-wi h-psoc-4-ble-82a61e, consul ed on
Augus 2017]
[18] Cyp ess Semiconduc o Co po a ion, TCPWM (PWM mode) example p ojec 2.0,
Music qua e based on a PSoC Page 63
2016.
[19] Alan Hawse, Implemen ing PSoC P in ,
[h ps://io expe .com/2017/05/10/implemen ing-psoc-p in /, consul ed on Oc obe
2017]
[20] Simon Ta ham, PuTTY, [h p://www.pu y.o g/, consul ed on Oc obe 2017]
[21] MICROCHIP TECHNOLOGY INC. MPLAB IDE.
[h p://www.mic ochip.com/pagehandle /en-us/ amily/mplabx/].
[22] Cyp ess Semiconduc o Co po a ion, TCPWM (Time /Coun e mode) example p ojec
3.0, 2016.
[23] Real Time Enginee s L d., The F eeRTOS™ Re e ence Manual, 2016.
[24] Ganesh Raaja, HIGH RESOLUTION HIGH FREQUENCY PWM IN PSOC4 BLE,
[h p://www.cyp ess.com/blog/psoc-hacke -blog/high- esolu ion-high- equency-pwm-
psoc4-ble, consul ed on No embe 2017].
[25] Cyp ess Semiconduc o Co po a ion, AN69133 - PSoC® 3 / PSoC 5LP Easy
Wa e o m Gene a ion wi h he Wa eDAC8 Componen ,
[h p://www.cyp ess.com/documen a ion/applica ion-no es/an69133-psoc-3-psoc-5lp-
easy-wa e o m-gene a ion-wa edac8-componen , consul ed on Janua y 2018].
[26] Cyp ess Semiconduc o Co po a ion, AN62582 - AM Modula ion and Demodula ion,
[h p://www.cyp ess.com/documen a ion/applica ion-no es/an62582-am-modula ion-
and-demodula ion, consul ed on Janua y 2018].
[27] Vic o K emin, Analog Mul iplica ion wi h PSoC, 2010.
[28] Cyp ess Semiconduc o Co po a ion, High Resolu ion DAC in PSoC 3/5,
[h ps://communi y.cyp ess.com/docs/DOC-12231, consul ed on Janua y 2018].
[29] Cyp ess Semiconduc o Co po a ion, RoHS, G een and En i onmen al In o ma ion,
[h p://www.cyp ess.com/suppo / ohs, consul ed on Janua y 2018].