Si4735 Library with RDS/RBDS control functions

Committer:
kpatel70
Date:
Thu Oct 11 20:10:09 2012 +0000
Revision:
0:ab340864b251
Si4735 IC library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kpatel70 0:ab340864b251 1 /* mbed Si4735 Library
kpatel70 0:ab340864b251 2 * Brett Wilson and Brett Berry
kpatel70 0:ab340864b251 3 * Georgia Tech ECE 4180
kpatel70 0:ab340864b251 4 * Ported from ...
kpatel70 0:ab340864b251 5
kpatel70 0:ab340864b251 6 * Arduino Si4735 Library
kpatel70 0:ab340864b251 7 * Written by Ryan Owens for SparkFun Electronics
kpatel70 0:ab340864b251 8 * 5/17/11
kpatel70 0:ab340864b251 9 *
kpatel70 0:ab340864b251 10 * This library is for use with the SparkFun Si4735 Shield
kpatel70 0:ab340864b251 11 * Released under the 'Buy Me a Beer' license
kpatel70 0:ab340864b251 12 * (If we ever meet, you buy me a beer)
kpatel70 0:ab340864b251 13 *
kpatel70 0:ab340864b251 14 * See the example sketches to learn how to use the library in your code.
kpatel70 0:ab340864b251 15 */
kpatel70 0:ab340864b251 16
kpatel70 0:ab340864b251 17 #include "mbed.h"
kpatel70 0:ab340864b251 18
kpatel70 0:ab340864b251 19 #ifndef Si4735_h
kpatel70 0:ab340864b251 20 #define Si4735_h
kpatel70 0:ab340864b251 21
kpatel70 0:ab340864b251 22 //Assign the radio pin numbers
kpatel70 0:ab340864b251 23 #define POWER_PIN 8
kpatel70 0:ab340864b251 24 #define RADIO_RESET_PIN 9
kpatel70 0:ab340864b251 25 #define INT_PIN 2
kpatel70 0:ab340864b251 26
kpatel70 0:ab340864b251 27 //Define the SPI Pin Numbers
kpatel70 0:ab340864b251 28 #define DATAOUT 11 //MOSI
kpatel70 0:ab340864b251 29 #define DATAIN 12 //MISO
kpatel70 0:ab340864b251 30 #define SPICLOCK 13 //sck
kpatel70 0:ab340864b251 31 #define SS 10 //ss
kpatel70 0:ab340864b251 32
kpatel70 0:ab340864b251 33 //List of possible modes for the Si4735 Radio
kpatel70 0:ab340864b251 34 #define AM 0
kpatel70 0:ab340864b251 35 #define FM 1
kpatel70 0:ab340864b251 36 #define SW 2
kpatel70 0:ab340864b251 37 #define LW 3
kpatel70 0:ab340864b251 38
kpatel70 0:ab340864b251 39
kpatel70 0:ab340864b251 40 #define NA 0
kpatel70 0:ab340864b251 41 #define EU 1
kpatel70 0:ab340864b251 42
kpatel70 0:ab340864b251 43 #define ON true
kpatel70 0:ab340864b251 44 #define OFF false
kpatel70 0:ab340864b251 45
kpatel70 0:ab340864b251 46 #define address_write 34
kpatel70 0:ab340864b251 47 #define address_read 35
kpatel70 0:ab340864b251 48
kpatel70 0:ab340864b251 49 typedef struct _Station {
kpatel70 0:ab340864b251 50 char callSign[5];
kpatel70 0:ab340864b251 51 char programType[17];
kpatel70 0:ab340864b251 52 char programService[9];
kpatel70 0:ab340864b251 53 char radioText[65];
kpatel70 0:ab340864b251 54 bool newRadioText;
kpatel70 0:ab340864b251 55 //Metrics signalQuality;
kpatel70 0:ab340864b251 56 //int frequency
kpatel70 0:ab340864b251 57 } Station;
kpatel70 0:ab340864b251 58
kpatel70 0:ab340864b251 59 typedef struct _Today {
kpatel70 0:ab340864b251 60 char year; //The 2-digit year
kpatel70 0:ab340864b251 61 char month;
kpatel70 0:ab340864b251 62 char day;
kpatel70 0:ab340864b251 63 char hour;
kpatel70 0:ab340864b251 64 char minute;
kpatel70 0:ab340864b251 65 } Today;
kpatel70 0:ab340864b251 66
kpatel70 0:ab340864b251 67
kpatel70 0:ab340864b251 68 class Si4735
kpatel70 0:ab340864b251 69 {
kpatel70 0:ab340864b251 70 public:
kpatel70 0:ab340864b251 71 //This is just a constructor.
kpatel70 0:ab340864b251 72 Si4735(PinName sda, PinName scl, PinName RST_, Serial *pc);
kpatel70 0:ab340864b251 73 /*
kpatel70 0:ab340864b251 74 * Description:
kpatel70 0:ab340864b251 75 * Initializes the Si4735, powers up the radio in the desired mode and limits the bandwidth appropriately.
kpatel70 0:ab340864b251 76 * This function must be called before any other radio command.
kpatel70 0:ab340864b251 77 * The bands are set as follows:
kpatel70 0:ab340864b251 78 * FM - 87.5 - 107.9 MHz
kpatel70 0:ab340864b251 79 * AM - 520 - 1710 kHz
kpatel70 0:ab340864b251 80 * SW - 2300 - 23000 khz
kpatel70 0:ab340864b251 81 * LW - 152 - 279 kHz
kpatel70 0:ab340864b251 82 * Parameters:
kpatel70 0:ab340864b251 83 * mode - The desired radio mode. Use AM(0), FM(1), SW(2) or LW(3).
kpatel70 0:ab340864b251 84 */
kpatel70 0:ab340864b251 85 void begin(char mode);
kpatel70 0:ab340864b251 86 /*
kpatel70 0:ab340864b251 87 * Description:
kpatel70 0:ab340864b251 88 * Used to send an ascii command string to the radio.
kpatel70 0:ab340864b251 89 * Parameters:
kpatel70 0:ab340864b251 90 * myCommand - A null terminated ascii string limited to hexidecimal characters
kpatel70 0:ab340864b251 91 * to be sent to the radio module. Instructions for building commands can be found
kpatel70 0:ab340864b251 92 * in the Si4735 Programmers Guide.
kpatel70 0:ab340864b251 93 */
kpatel70 0:ab340864b251 94 void sendCommand(char * myCommand);
kpatel70 0:ab340864b251 95 /*
kpatel70 0:ab340864b251 96 * Description:
kpatel70 0:ab340864b251 97 * Used to to tune the radio to a desired frequency. The library uses the mode indicated in the
kpatel70 0:ab340864b251 98 * begin() function to determine how to set the frequency.
kpatel70 0:ab340864b251 99 * Parameters:
kpatel70 0:ab340864b251 100 * frequency - The frequency to tune to, in kHz (or in 10kHz if using FM mode).
kpatel70 0:ab340864b251 101 * Returns:
kpatel70 0:ab340864b251 102 * True
kpatel70 0:ab340864b251 103 * TODO:
kpatel70 0:ab340864b251 104 * Make the function return true if the tune was successful, else return false.
kpatel70 0:ab340864b251 105 */
kpatel70 0:ab340864b251 106 bool tuneFrequency(int frequency);
kpatel70 0:ab340864b251 107 /*
kpatel70 0:ab340864b251 108 * Description:
kpatel70 0:ab340864b251 109 * This function currently does not work!
kpatel70 0:ab340864b251 110 * TODO:
kpatel70 0:ab340864b251 111 * Make this function work.
kpatel70 0:ab340864b251 112 */
kpatel70 0:ab340864b251 113 int getFrequency(void);
kpatel70 0:ab340864b251 114 /*
kpatel70 0:ab340864b251 115 * Description:
kpatel70 0:ab340864b251 116 * Commands the radio to seek up to the next valid channel. If the top of the band is reached, the seek
kpatel70 0:ab340864b251 117 * will continue from the bottom of the band.
kpatel70 0:ab340864b251 118 * Returns:
kpatel70 0:ab340864b251 119 * True
kpatel70 0:ab340864b251 120 * TODO:
kpatel70 0:ab340864b251 121 * Make the function return true if a valid channel was found, else return false.
kpatel70 0:ab340864b251 122 */
kpatel70 0:ab340864b251 123 bool seekUp(void);
kpatel70 0:ab340864b251 124 /*
kpatel70 0:ab340864b251 125 * Description:
kpatel70 0:ab340864b251 126 * Commands the radio to seek down to the next valid channel. If the bottom of the band is reached, the seek
kpatel70 0:ab340864b251 127 * will continue from the top of the band.
kpatel70 0:ab340864b251 128 * Returns:
kpatel70 0:ab340864b251 129 * True
kpatel70 0:ab340864b251 130 * TODO:
kpatel70 0:ab340864b251 131 * Make the function return true if a valid channel was found, else return false.
kpatel70 0:ab340864b251 132 */
kpatel70 0:ab340864b251 133 bool seekDown(void);
kpatel70 0:ab340864b251 134 /*
kpatel70 0:ab340864b251 135 * Description:
kpatel70 0:ab340864b251 136 * Increasese the volume by 1. If the maximum volume has been reached, no increase will take place.
kpatel70 0:ab340864b251 137 */
kpatel70 0:ab340864b251 138 void volumeUp(void);
kpatel70 0:ab340864b251 139 /*
kpatel70 0:ab340864b251 140 * Description:
kpatel70 0:ab340864b251 141 * Decreases the volume by 1. If the minimum volume has been reached, no decrease will take place.
kpatel70 0:ab340864b251 142 */
kpatel70 0:ab340864b251 143 void volumeDown(void);
kpatel70 0:ab340864b251 144 /*
kpatel70 0:ab340864b251 145 * Description:
kpatel70 0:ab340864b251 146 * Mutes the audio output
kpatel70 0:ab340864b251 147 */
kpatel70 0:ab340864b251 148 void mute(void);
kpatel70 0:ab340864b251 149 /*
kpatel70 0:ab340864b251 150 * Description:
kpatel70 0:ab340864b251 151 * Disables the mute.
kpatel70 0:ab340864b251 152 */
kpatel70 0:ab340864b251 153 void unmute(void);
kpatel70 0:ab340864b251 154 /*
kpatel70 0:ab340864b251 155 * Description:
kpatel70 0:ab340864b251 156 * Gets the current status of the radio. Learn more about the status in the Si4735 datasheet.
kpatel70 0:ab340864b251 157 * Returns:
kpatel70 0:ab340864b251 158 * The status of the radio.
kpatel70 0:ab340864b251 159 */
kpatel70 0:ab340864b251 160 char getStatus(void);
kpatel70 0:ab340864b251 161 /*
kpatel70 0:ab340864b251 162 * Description:
kpatel70 0:ab340864b251 163 * Gets the long response (16 characters) from the radio. Learn more about the long response in the Si4735 datasheet.
kpatel70 0:ab340864b251 164 * Parameters:
kpatel70 0:ab340864b251 165 * response - A string for the response from the radio to be stored in.
kpatel70 0:ab340864b251 166 */
kpatel70 0:ab340864b251 167 void getResponse(char * response);
kpatel70 0:ab340864b251 168 /*
kpatel70 0:ab340864b251 169 *Description:
kpatel70 0:ab340864b251 170 * Gets RDS/RBDS information fromt the radio. Learn more about the RDS information in the Si4745 datasheet and programming guide.
kpatel70 0:ab340864b251 171 * Parameters:
kpatel70 0:ab340864b251 172 * response - A string for the response from the radio to be stored in.
kpatel70 0:ab340864b251 173 */
kpatel70 0:ab340864b251 174 void getRDS(void);
kpatel70 0:ab340864b251 175 /*
kpatel70 0:ab340864b251 176 * Description:
kpatel70 0:ab340864b251 177 * Powers down the radio
kpatel70 0:ab340864b251 178 */
kpatel70 0:ab340864b251 179 void end(void);
kpatel70 0:ab340864b251 180
kpatel70 0:ab340864b251 181
kpatel70 0:ab340864b251 182 //**********
kpatel70 0:ab340864b251 183
kpatel70 0:ab340864b251 184 void setProperty(int address, int value);
kpatel70 0:ab340864b251 185 bool readRDS();
kpatel70 0:ab340864b251 186 bool getIntStatus(void);
kpatel70 0:ab340864b251 187 bool bitRead(char value, int index);
kpatel70 0:ab340864b251 188 void printable_str(char * str, int length);
kpatel70 0:ab340864b251 189 void clearRDS(void);
kpatel70 0:ab340864b251 190 void setLocale(char locale);
kpatel70 0:ab340864b251 191 char getLocale(void);
kpatel70 0:ab340864b251 192 void ptystr(char pty);
kpatel70 0:ab340864b251 193 void refreshDisplay(int next_stn);
kpatel70 0:ab340864b251 194 void getTime(void);
kpatel70 0:ab340864b251 195
kpatel70 0:ab340864b251 196
kpatel70 0:ab340864b251 197
kpatel70 0:ab340864b251 198 private:
kpatel70 0:ab340864b251 199 // Pointer for the SPI bus
kpatel70 0:ab340864b251 200 I2C* i2c_;
kpatel70 0:ab340864b251 201
kpatel70 0:ab340864b251 202 Serial *pc;
kpatel70 0:ab340864b251 203 // Declare digital out pins
kpatel70 0:ab340864b251 204 DigitalOut _RST_;
kpatel70 0:ab340864b251 205 /*
kpatel70 0:ab340864b251 206 * A variable that is assigned the current mode of the radio (AM, FM, SW or LW)
kpatel70 0:ab340864b251 207 */
kpatel70 0:ab340864b251 208 char _mode;
kpatel70 0:ab340864b251 209 /*
kpatel70 0:ab340864b251 210 * A variable the keeps the current volume level.
kpatel70 0:ab340864b251 211 */
kpatel70 0:ab340864b251 212 char _currentVolume;
kpatel70 0:ab340864b251 213 /*
kpatel70 0:ab340864b251 214 * Command string that holds the binary command string to be sent to the Si4735.
kpatel70 0:ab340864b251 215 */
kpatel70 0:ab340864b251 216 char command[9];
kpatel70 0:ab340864b251 217 /*
kpatel70 0:ab340864b251 218 * Description:
kpatel70 0:ab340864b251 219 * Sends a binary command string to the Si4735.
kpatel70 0:ab340864b251 220 * Parameters:
kpatel70 0:ab340864b251 221 * command - Binary command to be sent to the radio.
kpatel70 0:ab340864b251 222 * length - The number of characters in the command string (since it can't be null terminated!)
kpatel70 0:ab340864b251 223 * TODO:
kpatel70 0:ab340864b251 224 * Make the command wait for a valid CTS response from the radio before releasing control of the CPU.
kpatel70 0:ab340864b251 225 */
kpatel70 0:ab340864b251 226 void sendCommand(char * command, int length);
kpatel70 0:ab340864b251 227 /*
kpatel70 0:ab340864b251 228 * Description:
kpatel70 0:ab340864b251 229 * Sends/Receives a character from the SPI bus.
kpatel70 0:ab340864b251 230 * Parameters:
kpatel70 0:ab340864b251 231 * value - The character to be sent to the SPI bus.
kpatel70 0:ab340864b251 232 * Returns:
kpatel70 0:ab340864b251 233 * The character read from the SPI bus during the transfer.
kpatel70 0:ab340864b251 234 */
kpatel70 0:ab340864b251 235 char spiTransfer(char value);
kpatel70 0:ab340864b251 236
kpatel70 0:ab340864b251 237
kpatel70 0:ab340864b251 238 char _disp[65]; //Radio Text String
kpatel70 0:ab340864b251 239 char _ps[9]; //Program Service String
kpatel70 0:ab340864b251 240 char _csign[5]; //Call Sign
kpatel70 0:ab340864b251 241 bool _ab; //Indicates new radioText
kpatel70 0:ab340864b251 242 char _pty[17]; //Program Type String
kpatel70 0:ab340864b251 243 char _year; //Contains the month
kpatel70 0:ab340864b251 244 char _month; //Contains the year
kpatel70 0:ab340864b251 245 char _day; //Contains the day
kpatel70 0:ab340864b251 246 char _hour; //Contains the hour
kpatel70 0:ab340864b251 247 char _minute; //Contains the minute
kpatel70 0:ab340864b251 248 char _locale; //Contains the locale [NA, EU]
kpatel70 0:ab340864b251 249 bool _newRadioText; //Indicates that a new RadioText has been received
kpatel70 0:ab340864b251 250 Station tuned;
kpatel70 0:ab340864b251 251 Today date;
kpatel70 0:ab340864b251 252 bool ps_rdy;
kpatel70 0:ab340864b251 253 char RadioText[64];
kpatel70 0:ab340864b251 254 char ps_prev[9]; //previous ps
kpatel70 0:ab340864b251 255 char pty_prev[17];
kpatel70 0:ab340864b251 256
kpatel70 0:ab340864b251 257
kpatel70 0:ab340864b251 258 };
kpatel70 0:ab340864b251 259
kpatel70 0:ab340864b251 260 #endif