AA32 RADIO FM DUT GEII TOURS
Si4735.cpp@0:f5a073ecafa6, 2019-12-16 (annotated)
- Committer:
- jlpadiolleau
- Date:
- Mon Dec 16 07:37:25 2019 +0000
- Revision:
- 0:f5a073ecafa6
Si4735 GEII TOURS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jlpadiolleau | 0:f5a073ecafa6 | 1 | //Modifié par VG: attention il faut diminuer la vitesse du bus SPI sur la UnoRev3 (sinon gros problème de lecture) |
jlpadiolleau | 0:f5a073ecafa6 | 2 | //Voir ligne dans la méthode begin SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0); |
jlpadiolleau | 0:f5a073ecafa6 | 3 | //j'ai ajouté 1<<SPR0 pour mettre l'horloge / 16 (Fmax du Si7435=2,5MHz !!!) |
jlpadiolleau | 0:f5a073ecafa6 | 4 | |
jlpadiolleau | 0:f5a073ecafa6 | 5 | |
jlpadiolleau | 0:f5a073ecafa6 | 6 | /* Arduino Si4735 Library |
jlpadiolleau | 0:f5a073ecafa6 | 7 | * Written by Ryan Owens for SparkFun Electronics 5/17/11 |
jlpadiolleau | 0:f5a073ecafa6 | 8 | * Altered by Wagner Sartori Junior 09/13/11 |
jlpadiolleau | 0:f5a073ecafa6 | 9 | * Actively Being Developed by Jon Carrier |
jlpadiolleau | 0:f5a073ecafa6 | 10 | * |
jlpadiolleau | 0:f5a073ecafa6 | 11 | * This library is for use with the SparkFun Si4735 Shield |
jlpadiolleau | 0:f5a073ecafa6 | 12 | * Released under the 'Buy Me a Beer' license |
jlpadiolleau | 0:f5a073ecafa6 | 13 | * (If we ever meet, you buy me a beer) |
jlpadiolleau | 0:f5a073ecafa6 | 14 | * |
jlpadiolleau | 0:f5a073ecafa6 | 15 | * See the header file for better function documentation. |
jlpadiolleau | 0:f5a073ecafa6 | 16 | * |
jlpadiolleau | 0:f5a073ecafa6 | 17 | * See the example sketches to learn how to use the library in your code. |
jlpadiolleau | 0:f5a073ecafa6 | 18 | */ |
jlpadiolleau | 0:f5a073ecafa6 | 19 | |
jlpadiolleau | 0:f5a073ecafa6 | 20 | /* |
jlpadiolleau | 0:f5a073ecafa6 | 21 | |
jlpadiolleau | 0:f5a073ecafa6 | 22 | SPCR |
jlpadiolleau | 0:f5a073ecafa6 | 23 | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | |
jlpadiolleau | 0:f5a073ecafa6 | 24 | | SPIE | SPE | DORD | MSTR | CPOL | CPHA | SPR1 | SPR0 | |
jlpadiolleau | 0:f5a073ecafa6 | 25 | |
jlpadiolleau | 0:f5a073ecafa6 | 26 | SPIE - Enables the SPI interrupt when 1 |
jlpadiolleau | 0:f5a073ecafa6 | 27 | SPE - Enables the SPI when 1 |
jlpadiolleau | 0:f5a073ecafa6 | 28 | DORD - Sends data least Significant Bit First when 1, most Significant Bit first when 0 |
jlpadiolleau | 0:f5a073ecafa6 | 29 | MSTR - Sets the Arduino in master mode when 1, slave mode when 0 |
jlpadiolleau | 0:f5a073ecafa6 | 30 | CPOL - Sets the data clock to be idle when high if set to 1, idle when low if set to 0 |
jlpadiolleau | 0:f5a073ecafa6 | 31 | CPHA - Samples data on the falling edge of the data clock when 1, rising edge when 0 |
jlpadiolleau | 0:f5a073ecafa6 | 32 | SPR1 and SPR0 - Sets the SPI speed, 00 is fastest (4MHz) 11 is slowest (250KHz) |
jlpadiolleau | 0:f5a073ecafa6 | 33 | |
jlpadiolleau | 0:f5a073ecafa6 | 34 | */ |
jlpadiolleau | 0:f5a073ecafa6 | 35 | #include "Si4735.h" |
jlpadiolleau | 0:f5a073ecafa6 | 36 | |
jlpadiolleau | 0:f5a073ecafa6 | 37 | |
jlpadiolleau | 0:f5a073ecafa6 | 38 | //This is just a constructor. |
jlpadiolleau | 0:f5a073ecafa6 | 39 | //Default values are assigned to various private variables |
jlpadiolleau | 0:f5a073ecafa6 | 40 | Si4735::Si4735() |
jlpadiolleau | 0:f5a073ecafa6 | 41 | { |
jlpadiolleau | 0:f5a073ecafa6 | 42 | //busspi = new SPI(D11,D12,D13);//mosi,miso,sck |
jlpadiolleau | 0:f5a073ecafa6 | 43 | SPISS = new DigitalOut(D10); |
jlpadiolleau | 0:f5a073ecafa6 | 44 | GPO2 = new DigitalInOut(D2);//D2 |
jlpadiolleau | 0:f5a073ecafa6 | 45 | GPO1 = new DigitalInOut(D12);//D12 |
jlpadiolleau | 0:f5a073ecafa6 | 46 | POWER_PIN = new DigitalOut(D8);//D8 |
jlpadiolleau | 0:f5a073ecafa6 | 47 | RADIO_RESET_PIN = new DigitalOut(D9);//D9 |
jlpadiolleau | 0:f5a073ecafa6 | 48 | |
jlpadiolleau | 0:f5a073ecafa6 | 49 | _mode = FM;//FM par défaut |
jlpadiolleau | 0:f5a073ecafa6 | 50 | _locale = EU;//europe de l'ouest |
jlpadiolleau | 0:f5a073ecafa6 | 51 | _volume = 63;//volume à fond |
jlpadiolleau | 0:f5a073ecafa6 | 52 | } |
jlpadiolleau | 0:f5a073ecafa6 | 53 | |
jlpadiolleau | 0:f5a073ecafa6 | 54 | void Si4735::begin(char mode) |
jlpadiolleau | 0:f5a073ecafa6 | 55 | { |
jlpadiolleau | 0:f5a073ecafa6 | 56 | _mode = mode;//FM par défaut |
jlpadiolleau | 0:f5a073ecafa6 | 57 | //Etat RESET de l'ordinogramme |
jlpadiolleau | 0:f5a073ecafa6 | 58 | //On configure les broches en sorties, positionne GPO1 et GPO2 à 1 pendant le reset du Si4735 |
jlpadiolleau | 0:f5a073ecafa6 | 59 | //pour se mettre en mode de communication SPI |
jlpadiolleau | 0:f5a073ecafa6 | 60 | GPO2->output();//On force pour l'instant GPO2 en sortie |
jlpadiolleau | 0:f5a073ecafa6 | 61 | GPO1->output();//On force pour l'instant GPO1 en sortie |
jlpadiolleau | 0:f5a073ecafa6 | 62 | |
jlpadiolleau | 0:f5a073ecafa6 | 63 | //On active la séquence reset du Si4735 |
jlpadiolleau | 0:f5a073ecafa6 | 64 | RADIO_RESET_PIN->write(0); |
jlpadiolleau | 0:f5a073ecafa6 | 65 | POWER_PIN->write(0); |
jlpadiolleau | 0:f5a073ecafa6 | 66 | //wait_ms(20); |
jlpadiolleau | 0:f5a073ecafa6 | 67 | //On met un niveau 1 sur les GPO (mode SPI) |
jlpadiolleau | 0:f5a073ecafa6 | 68 | GPO1->write(1); |
jlpadiolleau | 0:f5a073ecafa6 | 69 | GPO2->write(1); |
jlpadiolleau | 0:f5a073ecafa6 | 70 | wait_ms(20); |
jlpadiolleau | 0:f5a073ecafa6 | 71 | //On alimente le Si4735 |
jlpadiolleau | 0:f5a073ecafa6 | 72 | POWER_PIN->write(1); |
jlpadiolleau | 0:f5a073ecafa6 | 73 | wait_ms(20); |
jlpadiolleau | 0:f5a073ecafa6 | 74 | //front montant sur la broche reset du Si4735 |
jlpadiolleau | 0:f5a073ecafa6 | 75 | RADIO_RESET_PIN->write(1); |
jlpadiolleau | 0:f5a073ecafa6 | 76 | wait_ms(200);//attente pour la prise en compte du mode choisi |
jlpadiolleau | 0:f5a073ecafa6 | 77 | |
jlpadiolleau | 0:f5a073ecafa6 | 78 | GPO1->input();//On remet GPO1 en entrée pour le bus SPI |
jlpadiolleau | 0:f5a073ecafa6 | 79 | //broche INT_PIN en entrée |
jlpadiolleau | 0:f5a073ecafa6 | 80 | GPO2->input();//On remet GPO2 en entrée |
jlpadiolleau | 0:f5a073ecafa6 | 81 | //SS au niveau haut |
jlpadiolleau | 0:f5a073ecafa6 | 82 | SPISS->write(1); |
jlpadiolleau | 0:f5a073ecafa6 | 83 | delete GPO1;//on détruit GPO1 pour laisser la broche D12 au bus SPI |
jlpadiolleau | 0:f5a073ecafa6 | 84 | |
jlpadiolleau | 0:f5a073ecafa6 | 85 | wait(0.2); |
jlpadiolleau | 0:f5a073ecafa6 | 86 | //Configure le SPI, Maitre, 1MHz Mode 0 à priori pour le Si4735 |
jlpadiolleau | 0:f5a073ecafa6 | 87 | //2,5 MHz max pour l'horloge bus SPI |
jlpadiolleau | 0:f5a073ecafa6 | 88 | busspi = new SPI(D11,D12,D13);//mosi,miso,sck |
jlpadiolleau | 0:f5a073ecafa6 | 89 | busspi->format(8,0);//8 bits de données et mode 0 |
jlpadiolleau | 0:f5a073ecafa6 | 90 | busspi->frequency(1000000);//1 MHz |
jlpadiolleau | 0:f5a073ecafa6 | 91 | wait(0.2); |
jlpadiolleau | 0:f5a073ecafa6 | 92 | |
jlpadiolleau | 0:f5a073ecafa6 | 93 | //Emission de la commande POWER_UP (0x01) |
jlpadiolleau | 0:f5a073ecafa6 | 94 | sprintf(command, "%c%c%c", 0x01, 0x50, 0x05);//Quartz 32768Hz externe validé et sortie GPO2, sortie analogique validé |
jlpadiolleau | 0:f5a073ecafa6 | 95 | sendCommand(command, 3); |
jlpadiolleau | 0:f5a073ecafa6 | 96 | wait_ms(200);//Pas vu dans la doc ??? |
jlpadiolleau | 0:f5a073ecafa6 | 97 | |
jlpadiolleau | 0:f5a073ecafa6 | 98 | //Configure GPO lines to maximize stability |
jlpadiolleau | 0:f5a073ecafa6 | 99 | sprintf(command, "%c%c", 0x80,0x06);//GPO1 et GPO2 en sortie, GPO3 en haute impédance |
jlpadiolleau | 0:f5a073ecafa6 | 100 | sendCommand(command, 2); |
jlpadiolleau | 0:f5a073ecafa6 | 101 | wait_ms(10); |
jlpadiolleau | 0:f5a073ecafa6 | 102 | sprintf(command, "%c%c", 0x81,0x04);//toutes les sorties à zéro sauf GPO1 (pourquoi MISO à 1 ?) |
jlpadiolleau | 0:f5a073ecafa6 | 103 | sendCommand(command, 2); |
jlpadiolleau | 0:f5a073ecafa6 | 104 | wait_ms(10); |
jlpadiolleau | 0:f5a073ecafa6 | 105 | |
jlpadiolleau | 0:f5a073ecafa6 | 106 | //Configure le volume à la valeur courante |
jlpadiolleau | 0:f5a073ecafa6 | 107 | setVolume(_volume); |
jlpadiolleau | 0:f5a073ecafa6 | 108 | |
jlpadiolleau | 0:f5a073ecafa6 | 109 | //son sur les sorties audio |
jlpadiolleau | 0:f5a073ecafa6 | 110 | unmute(); |
jlpadiolleau | 0:f5a073ecafa6 | 111 | |
jlpadiolleau | 0:f5a073ecafa6 | 112 | setLocale(EU);//désaccentuation Europe = 50µs (filtrage) |
jlpadiolleau | 0:f5a073ecafa6 | 113 | /* |
jlpadiolleau | 0:f5a073ecafa6 | 114 | //Enable RDS |
jlpadiolleau | 0:f5a073ecafa6 | 115 | setProperty(0x1500, 0x0001);//positionne RDSINT quand la pile FIFO a des données RDS |
jlpadiolleau | 0:f5a073ecafa6 | 116 | setProperty(0x1501, 0x0004);//Minimum 4 groupes RDS stocké dans la FIFO (A,B,C et D) |
jlpadiolleau | 0:f5a073ecafa6 | 117 | //setProperty(0x1502, 0xEF01);//Validation RDS et config de la gestion des erreurs sur chaque bloc |
jlpadiolleau | 0:f5a073ecafa6 | 118 | //Only store good blocks and ones that have been corrected |
jlpadiolleau | 0:f5a073ecafa6 | 119 | setProperty(0x1502, 0xAA01); |
jlpadiolleau | 0:f5a073ecafa6 | 120 | //Only store good blocks |
jlpadiolleau | 0:f5a073ecafa6 | 121 | //setProperty(0x1502, 0x0001);//Validation RDS et aucune erreur sur les blocs |
jlpadiolleau | 0:f5a073ecafa6 | 122 | */ |
jlpadiolleau | 0:f5a073ecafa6 | 123 | seekThresholds(2,14);//on ajuste le seuil pour rechercher plus de stations SNR=2, RSSI=14 |
jlpadiolleau | 0:f5a073ecafa6 | 124 | } |
jlpadiolleau | 0:f5a073ecafa6 | 125 | |
jlpadiolleau | 0:f5a073ecafa6 | 126 | |
jlpadiolleau | 0:f5a073ecafa6 | 127 | void Si4735::tuneFrequency(uint16_t frequency) |
jlpadiolleau | 0:f5a073ecafa6 | 128 | { |
jlpadiolleau | 0:f5a073ecafa6 | 129 | //Split the desired frequency into two character for use in the |
jlpadiolleau | 0:f5a073ecafa6 | 130 | //set frequency command. |
jlpadiolleau | 0:f5a073ecafa6 | 131 | uint8_t highByte = frequency >> 8; |
jlpadiolleau | 0:f5a073ecafa6 | 132 | uint8_t lowByte = frequency & 0x00FF; |
jlpadiolleau | 0:f5a073ecafa6 | 133 | |
jlpadiolleau | 0:f5a073ecafa6 | 134 | //set the new frequency. Mode FM uniquement |
jlpadiolleau | 0:f5a073ecafa6 | 135 | sprintf(command, "%c%c%c%c", 0x20, 0x00, highByte, lowByte); |
jlpadiolleau | 0:f5a073ecafa6 | 136 | sendCommand(command, 4); |
jlpadiolleau | 0:f5a073ecafa6 | 137 | wait_ms(100); |
jlpadiolleau | 0:f5a073ecafa6 | 138 | } |
jlpadiolleau | 0:f5a073ecafa6 | 139 | #if defined(USE_SI4735_REV) |
jlpadiolleau | 0:f5a073ecafa6 | 140 | void Si4735::getREV(char*FW,char*CMP,char *REV) |
jlpadiolleau | 0:f5a073ecafa6 | 141 | { |
jlpadiolleau | 0:f5a073ecafa6 | 142 | //FW = Firmware and it is a 2 character array |
jlpadiolleau | 0:f5a073ecafa6 | 143 | //CMP = Component Revision and it is a 2 character array |
jlpadiolleau | 0:f5a073ecafa6 | 144 | //REV = Chip Revision and it is a single character |
jlpadiolleau | 0:f5a073ecafa6 | 145 | char response [16]={0}; |
jlpadiolleau | 0:f5a073ecafa6 | 146 | sprintf(command, "%c", 0x10); |
jlpadiolleau | 0:f5a073ecafa6 | 147 | |
jlpadiolleau | 0:f5a073ecafa6 | 148 | //Send the command |
jlpadiolleau | 0:f5a073ecafa6 | 149 | sendCommand(command, 1); |
jlpadiolleau | 0:f5a073ecafa6 | 150 | |
jlpadiolleau | 0:f5a073ecafa6 | 151 | //Now read the response |
jlpadiolleau | 0:f5a073ecafa6 | 152 | getResponse(response); |
jlpadiolleau | 0:f5a073ecafa6 | 153 | |
jlpadiolleau | 0:f5a073ecafa6 | 154 | FW[0]=response[2]; |
jlpadiolleau | 0:f5a073ecafa6 | 155 | FW[1]=response[3]; |
jlpadiolleau | 0:f5a073ecafa6 | 156 | FW[2]='\0'; |
jlpadiolleau | 0:f5a073ecafa6 | 157 | CMP[0]=response[6]; |
jlpadiolleau | 0:f5a073ecafa6 | 158 | CMP[1]=response[7]; |
jlpadiolleau | 0:f5a073ecafa6 | 159 | CMP[2]='\0'; |
jlpadiolleau | 0:f5a073ecafa6 | 160 | *REV=response[8]; |
jlpadiolleau | 0:f5a073ecafa6 | 161 | } |
jlpadiolleau | 0:f5a073ecafa6 | 162 | #endif //USE_SI4735_REV |
jlpadiolleau | 0:f5a073ecafa6 | 163 | |
jlpadiolleau | 0:f5a073ecafa6 | 164 | uint16_t Si4735::getFrequency() |
jlpadiolleau | 0:f5a073ecafa6 | 165 | { |
jlpadiolleau | 0:f5a073ecafa6 | 166 | char response [16]={0}; |
jlpadiolleau | 0:f5a073ecafa6 | 167 | uint16_t frequency=0; |
jlpadiolleau | 0:f5a073ecafa6 | 168 | uint8_t highByte=0; |
jlpadiolleau | 0:f5a073ecafa6 | 169 | uint8_t lowByte=0; |
jlpadiolleau | 0:f5a073ecafa6 | 170 | |
jlpadiolleau | 0:f5a073ecafa6 | 171 | //The FM_TUNE_STATUS command |
jlpadiolleau | 0:f5a073ecafa6 | 172 | sprintf(command, "%c%c", 0x22, 0x00); |
jlpadiolleau | 0:f5a073ecafa6 | 173 | //Send the command |
jlpadiolleau | 0:f5a073ecafa6 | 174 | sendCommand(command, 2); |
jlpadiolleau | 0:f5a073ecafa6 | 175 | |
jlpadiolleau | 0:f5a073ecafa6 | 176 | //Now read the response |
jlpadiolleau | 0:f5a073ecafa6 | 177 | getResponse(response); |
jlpadiolleau | 0:f5a073ecafa6 | 178 | |
jlpadiolleau | 0:f5a073ecafa6 | 179 | lowByte=response[3]; |
jlpadiolleau | 0:f5a073ecafa6 | 180 | highByte=response[2]; |
jlpadiolleau | 0:f5a073ecafa6 | 181 | |
jlpadiolleau | 0:f5a073ecafa6 | 182 | frequency = (highByte<<8)+lowByte; |
jlpadiolleau | 0:f5a073ecafa6 | 183 | return frequency; |
jlpadiolleau | 0:f5a073ecafa6 | 184 | } |
jlpadiolleau | 0:f5a073ecafa6 | 185 | |
jlpadiolleau | 0:f5a073ecafa6 | 186 | void Si4735::seekUp(void) |
jlpadiolleau | 0:f5a073ecafa6 | 187 | { |
jlpadiolleau | 0:f5a073ecafa6 | 188 | //Use the current mode selection to seek up. |
jlpadiolleau | 0:f5a073ecafa6 | 189 | sprintf(command, "%c%c", 0x21, 0x0C); |
jlpadiolleau | 0:f5a073ecafa6 | 190 | sendCommand(command, 2); |
jlpadiolleau | 0:f5a073ecafa6 | 191 | wait_ms(1); |
jlpadiolleau | 0:f5a073ecafa6 | 192 | } |
jlpadiolleau | 0:f5a073ecafa6 | 193 | |
jlpadiolleau | 0:f5a073ecafa6 | 194 | void Si4735::seekDown(void) |
jlpadiolleau | 0:f5a073ecafa6 | 195 | { |
jlpadiolleau | 0:f5a073ecafa6 | 196 | //Use the current mode selection to seek down. |
jlpadiolleau | 0:f5a073ecafa6 | 197 | sprintf(command, "%c%c", 0x21, 0x04); |
jlpadiolleau | 0:f5a073ecafa6 | 198 | sendCommand(command, 2); |
jlpadiolleau | 0:f5a073ecafa6 | 199 | wait_ms(1); |
jlpadiolleau | 0:f5a073ecafa6 | 200 | } |
jlpadiolleau | 0:f5a073ecafa6 | 201 | |
jlpadiolleau | 0:f5a073ecafa6 | 202 | void Si4735::seekThresholds(uint8_t SNR, uint8_t RSSI) |
jlpadiolleau | 0:f5a073ecafa6 | 203 | { |
jlpadiolleau | 0:f5a073ecafa6 | 204 | //Use the current mode selection to set the threshold properties. |
jlpadiolleau | 0:f5a073ecafa6 | 205 | if(SNR>127)SNR=127; |
jlpadiolleau | 0:f5a073ecafa6 | 206 | else if(SNR<1)SNR=0; |
jlpadiolleau | 0:f5a073ecafa6 | 207 | if(RSSI>127)RSSI=127; |
jlpadiolleau | 0:f5a073ecafa6 | 208 | else if(RSSI<1)RSSI=0; |
jlpadiolleau | 0:f5a073ecafa6 | 209 | setProperty(0x1403, (uint16_t)SNR); |
jlpadiolleau | 0:f5a073ecafa6 | 210 | setProperty(0x1404, (uint16_t)RSSI); |
jlpadiolleau | 0:f5a073ecafa6 | 211 | } |
jlpadiolleau | 0:f5a073ecafa6 | 212 | |
jlpadiolleau | 0:f5a073ecafa6 | 213 | #if defined(USE_SI4735_RSQ) |
jlpadiolleau | 0:f5a073ecafa6 | 214 | void Si4735::getRSQ(Metrics * RSQ) |
jlpadiolleau | 0:f5a073ecafa6 | 215 | { |
jlpadiolleau | 0:f5a073ecafa6 | 216 | //This function gets the Received Signal Quality Information |
jlpadiolleau | 0:f5a073ecafa6 | 217 | char response [16]={0}; |
jlpadiolleau | 0:f5a073ecafa6 | 218 | |
jlpadiolleau | 0:f5a073ecafa6 | 219 | //The FM_RSQ_STATUS command |
jlpadiolleau | 0:f5a073ecafa6 | 220 | sprintf(command, "%c%c", 0x23, 0x00); |
jlpadiolleau | 0:f5a073ecafa6 | 221 | //Send the command |
jlpadiolleau | 0:f5a073ecafa6 | 222 | sendCommand(command, 2); |
jlpadiolleau | 0:f5a073ecafa6 | 223 | |
jlpadiolleau | 0:f5a073ecafa6 | 224 | //Now read the response |
jlpadiolleau | 0:f5a073ecafa6 | 225 | getResponse(response); |
jlpadiolleau | 0:f5a073ecafa6 | 226 | |
jlpadiolleau | 0:f5a073ecafa6 | 227 | //Pull the response data into their respecive fields |
jlpadiolleau | 0:f5a073ecafa6 | 228 | RSQ->RSSI=response[4]; |
jlpadiolleau | 0:f5a073ecafa6 | 229 | RSQ->SNR=response[5]; |
jlpadiolleau | 0:f5a073ecafa6 | 230 | |
jlpadiolleau | 0:f5a073ecafa6 | 231 | RSQ->STBLEND=response[3]&63; |
jlpadiolleau | 0:f5a073ecafa6 | 232 | RSQ->MULT=response[6]; |
jlpadiolleau | 0:f5a073ecafa6 | 233 | RSQ->FREQOFF=response[7]; |
jlpadiolleau | 0:f5a073ecafa6 | 234 | } |
jlpadiolleau | 0:f5a073ecafa6 | 235 | #endif |
jlpadiolleau | 0:f5a073ecafa6 | 236 | |
jlpadiolleau | 0:f5a073ecafa6 | 237 | uint8_t Si4735::volumeUp(void) |
jlpadiolleau | 0:f5a073ecafa6 | 238 | { |
jlpadiolleau | 0:f5a073ecafa6 | 239 | //63 est le volume maximum |
jlpadiolleau | 0:f5a073ecafa6 | 240 | if(_volume < 63) |
jlpadiolleau | 0:f5a073ecafa6 | 241 | { |
jlpadiolleau | 0:f5a073ecafa6 | 242 | _volume+=1; |
jlpadiolleau | 0:f5a073ecafa6 | 243 | //Set the volume to the current value. |
jlpadiolleau | 0:f5a073ecafa6 | 244 | setProperty(0x4000, (uint16_t)_volume); |
jlpadiolleau | 0:f5a073ecafa6 | 245 | } |
jlpadiolleau | 0:f5a073ecafa6 | 246 | return _volume; |
jlpadiolleau | 0:f5a073ecafa6 | 247 | } |
jlpadiolleau | 0:f5a073ecafa6 | 248 | |
jlpadiolleau | 0:f5a073ecafa6 | 249 | uint8_t Si4735::volumeDown(void) |
jlpadiolleau | 0:f5a073ecafa6 | 250 | { |
jlpadiolleau | 0:f5a073ecafa6 | 251 | //If we're not at the minimum volume yet, decrease the volume |
jlpadiolleau | 0:f5a073ecafa6 | 252 | if(_volume > 0){ |
jlpadiolleau | 0:f5a073ecafa6 | 253 | _volume-=1; |
jlpadiolleau | 0:f5a073ecafa6 | 254 | //Set the volume to the current value. |
jlpadiolleau | 0:f5a073ecafa6 | 255 | setProperty(0x4000, (uint16_t)_volume); |
jlpadiolleau | 0:f5a073ecafa6 | 256 | } |
jlpadiolleau | 0:f5a073ecafa6 | 257 | return _volume; |
jlpadiolleau | 0:f5a073ecafa6 | 258 | } |
jlpadiolleau | 0:f5a073ecafa6 | 259 | |
jlpadiolleau | 0:f5a073ecafa6 | 260 | uint8_t Si4735::setVolume(uint8_t value) |
jlpadiolleau | 0:f5a073ecafa6 | 261 | { |
jlpadiolleau | 0:f5a073ecafa6 | 262 | if(value <= 63 && value > 0){ |
jlpadiolleau | 0:f5a073ecafa6 | 263 | _volume=value; |
jlpadiolleau | 0:f5a073ecafa6 | 264 | //Set the volume to the current value. |
jlpadiolleau | 0:f5a073ecafa6 | 265 | setProperty(0x4000, (uint16_t)_volume); |
jlpadiolleau | 0:f5a073ecafa6 | 266 | } |
jlpadiolleau | 0:f5a073ecafa6 | 267 | return _volume; |
jlpadiolleau | 0:f5a073ecafa6 | 268 | } |
jlpadiolleau | 0:f5a073ecafa6 | 269 | |
jlpadiolleau | 0:f5a073ecafa6 | 270 | uint8_t Si4735::getVolume(void) |
jlpadiolleau | 0:f5a073ecafa6 | 271 | { |
jlpadiolleau | 0:f5a073ecafa6 | 272 | return _volume; |
jlpadiolleau | 0:f5a073ecafa6 | 273 | } |
jlpadiolleau | 0:f5a073ecafa6 | 274 | |
jlpadiolleau | 0:f5a073ecafa6 | 275 | void Si4735::mute(void) |
jlpadiolleau | 0:f5a073ecafa6 | 276 | { |
jlpadiolleau | 0:f5a073ecafa6 | 277 | setProperty(0x4001, 0x0003);//coupe le son sur les voies gauche et droite |
jlpadiolleau | 0:f5a073ecafa6 | 278 | } |
jlpadiolleau | 0:f5a073ecafa6 | 279 | |
jlpadiolleau | 0:f5a073ecafa6 | 280 | void Si4735::unmute(void) |
jlpadiolleau | 0:f5a073ecafa6 | 281 | { |
jlpadiolleau | 0:f5a073ecafa6 | 282 | setProperty(0x4001, 0x0000);//son L et R ok |
jlpadiolleau | 0:f5a073ecafa6 | 283 | } |
jlpadiolleau | 0:f5a073ecafa6 | 284 | |
jlpadiolleau | 0:f5a073ecafa6 | 285 | char Si4735::getStatus(void) |
jlpadiolleau | 0:f5a073ecafa6 | 286 | { |
jlpadiolleau | 0:f5a073ecafa6 | 287 | char response=0; |
jlpadiolleau | 0:f5a073ecafa6 | 288 | //SS au niveau bas |
jlpadiolleau | 0:f5a073ecafa6 | 289 | SPISS->write(0); |
jlpadiolleau | 0:f5a073ecafa6 | 290 | wait_ms(1); |
jlpadiolleau | 0:f5a073ecafa6 | 291 | spiTransfer(0xA0); //Commande de lecture sur GPO1 d'un octet (MISO du SPI) |
jlpadiolleau | 0:f5a073ecafa6 | 292 | wait_ms(1); |
jlpadiolleau | 0:f5a073ecafa6 | 293 | response = spiTransfer(0x00); //Lecture de la réponse |
jlpadiolleau | 0:f5a073ecafa6 | 294 | //SS au niveau haut, fin de lecture |
jlpadiolleau | 0:f5a073ecafa6 | 295 | SPISS->write(1); |
jlpadiolleau | 0:f5a073ecafa6 | 296 | return response; |
jlpadiolleau | 0:f5a073ecafa6 | 297 | } |
jlpadiolleau | 0:f5a073ecafa6 | 298 | |
jlpadiolleau | 0:f5a073ecafa6 | 299 | void Si4735::getResponse(char *response) |
jlpadiolleau | 0:f5a073ecafa6 | 300 | { |
jlpadiolleau | 0:f5a073ecafa6 | 301 | //char cmd=0xE0; |
jlpadiolleau | 0:f5a073ecafa6 | 302 | //SS au niveau bas |
jlpadiolleau | 0:f5a073ecafa6 | 303 | SPISS->write(0); |
jlpadiolleau | 0:f5a073ecafa6 | 304 | wait_ms(1); |
jlpadiolleau | 0:f5a073ecafa6 | 305 | busspi->write(0xE0);//Commande de lecture d'une série de 16 octets sur GPO1 (MISO) |
jlpadiolleau | 0:f5a073ecafa6 | 306 | //spiTransfer(0xE0); //Commande de lecture d'une série de 16 octets sur GPO1 (MISO) |
jlpadiolleau | 0:f5a073ecafa6 | 307 | //wait_ms(1); |
jlpadiolleau | 0:f5a073ecafa6 | 308 | //lecture de 16 octets - le premier est l'octet de contrôle, puis les 15 data |
jlpadiolleau | 0:f5a073ecafa6 | 309 | //busspi->write(&cmd,1,response,16); |
jlpadiolleau | 0:f5a073ecafa6 | 310 | for(int i=0; i<16; i++) *response++ = busspi->write(0x00); //Lecture des 16 octets |
jlpadiolleau | 0:f5a073ecafa6 | 311 | //SS au niveau haut, fin de lecture |
jlpadiolleau | 0:f5a073ecafa6 | 312 | SPISS->write(1); |
jlpadiolleau | 0:f5a073ecafa6 | 313 | } |
jlpadiolleau | 0:f5a073ecafa6 | 314 | |
jlpadiolleau | 0:f5a073ecafa6 | 315 | void Si4735::end(void) |
jlpadiolleau | 0:f5a073ecafa6 | 316 | { |
jlpadiolleau | 0:f5a073ecafa6 | 317 | sprintf(command, "%c", 0x11);//Commande POWER_DOWN |
jlpadiolleau | 0:f5a073ecafa6 | 318 | sendCommand(command, 1); |
jlpadiolleau | 0:f5a073ecafa6 | 319 | wait_ms(1); |
jlpadiolleau | 0:f5a073ecafa6 | 320 | } |
jlpadiolleau | 0:f5a073ecafa6 | 321 | |
jlpadiolleau | 0:f5a073ecafa6 | 322 | void Si4735::setLocale(uint8_t locale) |
jlpadiolleau | 0:f5a073ecafa6 | 323 | { |
jlpadiolleau | 0:f5a073ecafa6 | 324 | _locale=locale; |
jlpadiolleau | 0:f5a073ecafa6 | 325 | //Set the deemphasis to match the locale |
jlpadiolleau | 0:f5a073ecafa6 | 326 | switch(_locale) |
jlpadiolleau | 0:f5a073ecafa6 | 327 | { |
jlpadiolleau | 0:f5a073ecafa6 | 328 | case NA: |
jlpadiolleau | 0:f5a073ecafa6 | 329 | setProperty(0x1100, 0x0002); |
jlpadiolleau | 0:f5a073ecafa6 | 330 | break; |
jlpadiolleau | 0:f5a073ecafa6 | 331 | case EU: |
jlpadiolleau | 0:f5a073ecafa6 | 332 | setProperty(0x1100, 0x0001); |
jlpadiolleau | 0:f5a073ecafa6 | 333 | break; |
jlpadiolleau | 0:f5a073ecafa6 | 334 | default: |
jlpadiolleau | 0:f5a073ecafa6 | 335 | break; |
jlpadiolleau | 0:f5a073ecafa6 | 336 | } |
jlpadiolleau | 0:f5a073ecafa6 | 337 | } |
jlpadiolleau | 0:f5a073ecafa6 | 338 | |
jlpadiolleau | 0:f5a073ecafa6 | 339 | uint8_t Si4735::getLocale(void) |
jlpadiolleau | 0:f5a073ecafa6 | 340 | { |
jlpadiolleau | 0:f5a073ecafa6 | 341 | return _locale; |
jlpadiolleau | 0:f5a073ecafa6 | 342 | } |
jlpadiolleau | 0:f5a073ecafa6 | 343 | |
jlpadiolleau | 0:f5a073ecafa6 | 344 | |
jlpadiolleau | 0:f5a073ecafa6 | 345 | void Si4735::setMode(char mode) |
jlpadiolleau | 0:f5a073ecafa6 | 346 | { |
jlpadiolleau | 0:f5a073ecafa6 | 347 | end(); |
jlpadiolleau | 0:f5a073ecafa6 | 348 | begin(mode); |
jlpadiolleau | 0:f5a073ecafa6 | 349 | } |
jlpadiolleau | 0:f5a073ecafa6 | 350 | |
jlpadiolleau | 0:f5a073ecafa6 | 351 | char Si4735::getMode(void) |
jlpadiolleau | 0:f5a073ecafa6 | 352 | { |
jlpadiolleau | 0:f5a073ecafa6 | 353 | return _mode; |
jlpadiolleau | 0:f5a073ecafa6 | 354 | } |
jlpadiolleau | 0:f5a073ecafa6 | 355 | |
jlpadiolleau | 0:f5a073ecafa6 | 356 | //Envoie la commande 0x12 pour positionner la valeur value du paramètre situé à l'adresse address |
jlpadiolleau | 0:f5a073ecafa6 | 357 | void Si4735::setProperty(uint16_t address, uint16_t value) |
jlpadiolleau | 0:f5a073ecafa6 | 358 | { |
jlpadiolleau | 0:f5a073ecafa6 | 359 | sprintf(command, "%c%c%c%c%c%c", 0x12, 0x00, (address>>8)&255, address&255, (value>>8)&255, value&255); |
jlpadiolleau | 0:f5a073ecafa6 | 360 | sendCommand(command, 6); |
jlpadiolleau | 0:f5a073ecafa6 | 361 | wait_ms(1); |
jlpadiolleau | 0:f5a073ecafa6 | 362 | } |
jlpadiolleau | 0:f5a073ecafa6 | 363 | |
jlpadiolleau | 0:f5a073ecafa6 | 364 | //Envoie la commande 0x13 pour lire la valeur du paramètre situé à l'adresse address. |
jlpadiolleau | 0:f5a073ecafa6 | 365 | uint16_t Si4735::getProperty(uint16_t address) |
jlpadiolleau | 0:f5a073ecafa6 | 366 | { |
jlpadiolleau | 0:f5a073ecafa6 | 367 | char response [16]={0}; |
jlpadiolleau | 0:f5a073ecafa6 | 368 | sprintf(command, "%c%c%c%c", 0x13, 0x00, (address>>8)&255, address&255); |
jlpadiolleau | 0:f5a073ecafa6 | 369 | sendCommand(command, 4); |
jlpadiolleau | 0:f5a073ecafa6 | 370 | getResponse(response); |
jlpadiolleau | 0:f5a073ecafa6 | 371 | return response[2]<<8 | response[3]; |
jlpadiolleau | 0:f5a073ecafa6 | 372 | } |
jlpadiolleau | 0:f5a073ecafa6 | 373 | |
jlpadiolleau | 0:f5a073ecafa6 | 374 | /******************************************* |
jlpadiolleau | 0:f5a073ecafa6 | 375 | * |
jlpadiolleau | 0:f5a073ecafa6 | 376 | * Private Functions |
jlpadiolleau | 0:f5a073ecafa6 | 377 | * |
jlpadiolleau | 0:f5a073ecafa6 | 378 | *******************************************/ |
jlpadiolleau | 0:f5a073ecafa6 | 379 | //Ecriture ou lecture sur le bus SPI ! |
jlpadiolleau | 0:f5a073ecafa6 | 380 | char Si4735::spiTransfer(char value) |
jlpadiolleau | 0:f5a073ecafa6 | 381 | { |
jlpadiolleau | 0:f5a073ecafa6 | 382 | return busspi->write(value);//Emission d'un octet ou lecture |
jlpadiolleau | 0:f5a073ecafa6 | 383 | } |
jlpadiolleau | 0:f5a073ecafa6 | 384 | |
jlpadiolleau | 0:f5a073ecafa6 | 385 | //Ecriture de données dans le Si4735 |
jlpadiolleau | 0:f5a073ecafa6 | 386 | //length=8 octets maximum |
jlpadiolleau | 0:f5a073ecafa6 | 387 | void Si4735::sendCommand(char * command, int length) |
jlpadiolleau | 0:f5a073ecafa6 | 388 | { |
jlpadiolleau | 0:f5a073ecafa6 | 389 | //SS au niveau bas |
jlpadiolleau | 0:f5a073ecafa6 | 390 | SPISS->write(0); |
jlpadiolleau | 0:f5a073ecafa6 | 391 | wait_ms(1); |
jlpadiolleau | 0:f5a073ecafa6 | 392 | spiTransfer(0x48); //Octet de contrôle à 0x48 pour signifier une écriture dans le Si4735 (puis envoie de 8 octets de data) |
jlpadiolleau | 0:f5a073ecafa6 | 393 | for(int i=0; i<length; i++)spiTransfer(command[i]); |
jlpadiolleau | 0:f5a073ecafa6 | 394 | for(int i=length; i<8; i++)spiTransfer(0x00); //on compléte avec des 0 si la data est < à 8 octets. |
jlpadiolleau | 0:f5a073ecafa6 | 395 | //Fin de la séquence par SS au niveau haut |
jlpadiolleau | 0:f5a073ecafa6 | 396 | SPISS->write(1); |
jlpadiolleau | 0:f5a073ecafa6 | 397 | } |