Brett Wilson
/
Si4735
Si4735 Radio Library
Si4735.h@0:42c032fc907a, 2011-10-12 (annotated)
- Committer:
- bwilson30
- Date:
- Wed Oct 12 17:01:38 2011 +0000
- Revision:
- 0:42c032fc907a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bwilson30 | 0:42c032fc907a | 1 | /* mbed Si4735 Library |
bwilson30 | 0:42c032fc907a | 2 | * Brett Wilson and Brett Berry |
bwilson30 | 0:42c032fc907a | 3 | * Georgia Tech ECE 4180 |
bwilson30 | 0:42c032fc907a | 4 | * Ported from ... |
bwilson30 | 0:42c032fc907a | 5 | |
bwilson30 | 0:42c032fc907a | 6 | * Arduino Si4735 Library |
bwilson30 | 0:42c032fc907a | 7 | * Written by Ryan Owens for SparkFun Electronics |
bwilson30 | 0:42c032fc907a | 8 | * 5/17/11 |
bwilson30 | 0:42c032fc907a | 9 | * |
bwilson30 | 0:42c032fc907a | 10 | * This library is for use with the SparkFun Si4735 Shield |
bwilson30 | 0:42c032fc907a | 11 | * Released under the 'Buy Me a Beer' license |
bwilson30 | 0:42c032fc907a | 12 | * (If we ever meet, you buy me a beer) |
bwilson30 | 0:42c032fc907a | 13 | * |
bwilson30 | 0:42c032fc907a | 14 | * See the example sketches to learn how to use the library in your code. |
bwilson30 | 0:42c032fc907a | 15 | */ |
bwilson30 | 0:42c032fc907a | 16 | |
bwilson30 | 0:42c032fc907a | 17 | #include "mbed.h" |
bwilson30 | 0:42c032fc907a | 18 | |
bwilson30 | 0:42c032fc907a | 19 | #ifndef Si4735_h |
bwilson30 | 0:42c032fc907a | 20 | #define Si4735_h |
bwilson30 | 0:42c032fc907a | 21 | |
bwilson30 | 0:42c032fc907a | 22 | //Assign the radio pin numbers |
bwilson30 | 0:42c032fc907a | 23 | #define POWER_PIN 8 |
bwilson30 | 0:42c032fc907a | 24 | #define RADIO_RESET_PIN 9 |
bwilson30 | 0:42c032fc907a | 25 | #define INT_PIN 2 |
bwilson30 | 0:42c032fc907a | 26 | |
bwilson30 | 0:42c032fc907a | 27 | //Define the SPI Pin Numbers |
bwilson30 | 0:42c032fc907a | 28 | #define DATAOUT 11 //MOSI |
bwilson30 | 0:42c032fc907a | 29 | #define DATAIN 12 //MISO |
bwilson30 | 0:42c032fc907a | 30 | #define SPICLOCK 13 //sck |
bwilson30 | 0:42c032fc907a | 31 | #define SS 10 //ss |
bwilson30 | 0:42c032fc907a | 32 | |
bwilson30 | 0:42c032fc907a | 33 | //List of possible modes for the Si4735 Radio |
bwilson30 | 0:42c032fc907a | 34 | #define AM 0 |
bwilson30 | 0:42c032fc907a | 35 | #define FM 1 |
bwilson30 | 0:42c032fc907a | 36 | #define SW 2 |
bwilson30 | 0:42c032fc907a | 37 | #define LW 3 |
bwilson30 | 0:42c032fc907a | 38 | |
bwilson30 | 0:42c032fc907a | 39 | #define ON true |
bwilson30 | 0:42c032fc907a | 40 | #define OFF false |
bwilson30 | 0:42c032fc907a | 41 | |
bwilson30 | 0:42c032fc907a | 42 | #define address_write 34 |
bwilson30 | 0:42c032fc907a | 43 | #define address_read 35 |
bwilson30 | 0:42c032fc907a | 44 | |
bwilson30 | 0:42c032fc907a | 45 | class Si4735 |
bwilson30 | 0:42c032fc907a | 46 | { |
bwilson30 | 0:42c032fc907a | 47 | public: |
bwilson30 | 0:42c032fc907a | 48 | //This is just a constructor. |
bwilson30 | 0:42c032fc907a | 49 | Si4735(PinName sda, PinName scl, PinName RST_); |
bwilson30 | 0:42c032fc907a | 50 | /* |
bwilson30 | 0:42c032fc907a | 51 | * Description: |
bwilson30 | 0:42c032fc907a | 52 | * Initializes the Si4735, powers up the radio in the desired mode and limits the bandwidth appropriately. |
bwilson30 | 0:42c032fc907a | 53 | * This function must be called before any other radio command. |
bwilson30 | 0:42c032fc907a | 54 | * The bands are set as follows: |
bwilson30 | 0:42c032fc907a | 55 | * FM - 87.5 - 107.9 MHz |
bwilson30 | 0:42c032fc907a | 56 | * AM - 520 - 1710 kHz |
bwilson30 | 0:42c032fc907a | 57 | * SW - 2300 - 23000 khz |
bwilson30 | 0:42c032fc907a | 58 | * LW - 152 - 279 kHz |
bwilson30 | 0:42c032fc907a | 59 | * Parameters: |
bwilson30 | 0:42c032fc907a | 60 | * mode - The desired radio mode. Use AM(0), FM(1), SW(2) or LW(3). |
bwilson30 | 0:42c032fc907a | 61 | */ |
bwilson30 | 0:42c032fc907a | 62 | void begin(char mode); |
bwilson30 | 0:42c032fc907a | 63 | /* |
bwilson30 | 0:42c032fc907a | 64 | * Description: |
bwilson30 | 0:42c032fc907a | 65 | * Used to send an ascii command string to the radio. |
bwilson30 | 0:42c032fc907a | 66 | * Parameters: |
bwilson30 | 0:42c032fc907a | 67 | * myCommand - A null terminated ascii string limited to hexidecimal characters |
bwilson30 | 0:42c032fc907a | 68 | * to be sent to the radio module. Instructions for building commands can be found |
bwilson30 | 0:42c032fc907a | 69 | * in the Si4735 Programmers Guide. |
bwilson30 | 0:42c032fc907a | 70 | */ |
bwilson30 | 0:42c032fc907a | 71 | void sendCommand(char * myCommand); |
bwilson30 | 0:42c032fc907a | 72 | /* |
bwilson30 | 0:42c032fc907a | 73 | * Description: |
bwilson30 | 0:42c032fc907a | 74 | * Used to to tune the radio to a desired frequency. The library uses the mode indicated in the |
bwilson30 | 0:42c032fc907a | 75 | * begin() function to determine how to set the frequency. |
bwilson30 | 0:42c032fc907a | 76 | * Parameters: |
bwilson30 | 0:42c032fc907a | 77 | * frequency - The frequency to tune to, in kHz (or in 10kHz if using FM mode). |
bwilson30 | 0:42c032fc907a | 78 | * Returns: |
bwilson30 | 0:42c032fc907a | 79 | * True |
bwilson30 | 0:42c032fc907a | 80 | * TODO: |
bwilson30 | 0:42c032fc907a | 81 | * Make the function return true if the tune was successful, else return false. |
bwilson30 | 0:42c032fc907a | 82 | */ |
bwilson30 | 0:42c032fc907a | 83 | bool tuneFrequency(int frequency); |
bwilson30 | 0:42c032fc907a | 84 | /* |
bwilson30 | 0:42c032fc907a | 85 | * Description: |
bwilson30 | 0:42c032fc907a | 86 | * This function currently does not work! |
bwilson30 | 0:42c032fc907a | 87 | * TODO: |
bwilson30 | 0:42c032fc907a | 88 | * Make this function work. |
bwilson30 | 0:42c032fc907a | 89 | */ |
bwilson30 | 0:42c032fc907a | 90 | int getFrequency(void); |
bwilson30 | 0:42c032fc907a | 91 | /* |
bwilson30 | 0:42c032fc907a | 92 | * Description: |
bwilson30 | 0:42c032fc907a | 93 | * Commands the radio to seek up to the next valid channel. If the top of the band is reached, the seek |
bwilson30 | 0:42c032fc907a | 94 | * will continue from the bottom of the band. |
bwilson30 | 0:42c032fc907a | 95 | * Returns: |
bwilson30 | 0:42c032fc907a | 96 | * True |
bwilson30 | 0:42c032fc907a | 97 | * TODO: |
bwilson30 | 0:42c032fc907a | 98 | * Make the function return true if a valid channel was found, else return false. |
bwilson30 | 0:42c032fc907a | 99 | */ |
bwilson30 | 0:42c032fc907a | 100 | bool seekUp(void); |
bwilson30 | 0:42c032fc907a | 101 | /* |
bwilson30 | 0:42c032fc907a | 102 | * Description: |
bwilson30 | 0:42c032fc907a | 103 | * Commands the radio to seek down to the next valid channel. If the bottom of the band is reached, the seek |
bwilson30 | 0:42c032fc907a | 104 | * will continue from the top of the band. |
bwilson30 | 0:42c032fc907a | 105 | * Returns: |
bwilson30 | 0:42c032fc907a | 106 | * True |
bwilson30 | 0:42c032fc907a | 107 | * TODO: |
bwilson30 | 0:42c032fc907a | 108 | * Make the function return true if a valid channel was found, else return false. |
bwilson30 | 0:42c032fc907a | 109 | */ |
bwilson30 | 0:42c032fc907a | 110 | bool seekDown(void); |
bwilson30 | 0:42c032fc907a | 111 | /* |
bwilson30 | 0:42c032fc907a | 112 | * Description: |
bwilson30 | 0:42c032fc907a | 113 | * Increasese the volume by 1. If the maximum volume has been reached, no increase will take place. |
bwilson30 | 0:42c032fc907a | 114 | */ |
bwilson30 | 0:42c032fc907a | 115 | void volumeUp(void); |
bwilson30 | 0:42c032fc907a | 116 | /* |
bwilson30 | 0:42c032fc907a | 117 | * Description: |
bwilson30 | 0:42c032fc907a | 118 | * Decreases the volume by 1. If the minimum volume has been reached, no decrease will take place. |
bwilson30 | 0:42c032fc907a | 119 | */ |
bwilson30 | 0:42c032fc907a | 120 | void volumeDown(void); |
bwilson30 | 0:42c032fc907a | 121 | /* |
bwilson30 | 0:42c032fc907a | 122 | * Description: |
bwilson30 | 0:42c032fc907a | 123 | * Mutes the audio output |
bwilson30 | 0:42c032fc907a | 124 | */ |
bwilson30 | 0:42c032fc907a | 125 | void mute(void); |
bwilson30 | 0:42c032fc907a | 126 | /* |
bwilson30 | 0:42c032fc907a | 127 | * Description: |
bwilson30 | 0:42c032fc907a | 128 | * Disables the mute. |
bwilson30 | 0:42c032fc907a | 129 | */ |
bwilson30 | 0:42c032fc907a | 130 | void unmute(void); |
bwilson30 | 0:42c032fc907a | 131 | /* |
bwilson30 | 0:42c032fc907a | 132 | * Description: |
bwilson30 | 0:42c032fc907a | 133 | * Gets the current status of the radio. Learn more about the status in the Si4735 datasheet. |
bwilson30 | 0:42c032fc907a | 134 | * Returns: |
bwilson30 | 0:42c032fc907a | 135 | * The status of the radio. |
bwilson30 | 0:42c032fc907a | 136 | */ |
bwilson30 | 0:42c032fc907a | 137 | char getStatus(void); |
bwilson30 | 0:42c032fc907a | 138 | /* |
bwilson30 | 0:42c032fc907a | 139 | * Description: |
bwilson30 | 0:42c032fc907a | 140 | * Gets the long response (16 characters) from the radio. Learn more about the long response in the Si4735 datasheet. |
bwilson30 | 0:42c032fc907a | 141 | * Parameters: |
bwilson30 | 0:42c032fc907a | 142 | * response - A string for the response from the radio to be stored in. |
bwilson30 | 0:42c032fc907a | 143 | */ |
bwilson30 | 0:42c032fc907a | 144 | void getResponse(char * response); |
bwilson30 | 0:42c032fc907a | 145 | /* |
bwilson30 | 0:42c032fc907a | 146 | * Description: |
bwilson30 | 0:42c032fc907a | 147 | * Powers down the radio |
bwilson30 | 0:42c032fc907a | 148 | */ |
bwilson30 | 0:42c032fc907a | 149 | void end(void); |
bwilson30 | 0:42c032fc907a | 150 | |
bwilson30 | 0:42c032fc907a | 151 | private: |
bwilson30 | 0:42c032fc907a | 152 | // Pointer for the SPI bus |
bwilson30 | 0:42c032fc907a | 153 | I2C* i2c_; |
bwilson30 | 0:42c032fc907a | 154 | // Declare digital out pins |
bwilson30 | 0:42c032fc907a | 155 | DigitalOut _RST_; |
bwilson30 | 0:42c032fc907a | 156 | /* |
bwilson30 | 0:42c032fc907a | 157 | * A variable that is assigned the current mode of the radio (AM, FM, SW or LW) |
bwilson30 | 0:42c032fc907a | 158 | */ |
bwilson30 | 0:42c032fc907a | 159 | char _mode; |
bwilson30 | 0:42c032fc907a | 160 | /* |
bwilson30 | 0:42c032fc907a | 161 | * A variable the keeps the current volume level. |
bwilson30 | 0:42c032fc907a | 162 | */ |
bwilson30 | 0:42c032fc907a | 163 | char _currentVolume; |
bwilson30 | 0:42c032fc907a | 164 | /* |
bwilson30 | 0:42c032fc907a | 165 | * Command string that holds the binary command string to be sent to the Si4735. |
bwilson30 | 0:42c032fc907a | 166 | */ |
bwilson30 | 0:42c032fc907a | 167 | char command[9]; |
bwilson30 | 0:42c032fc907a | 168 | /* |
bwilson30 | 0:42c032fc907a | 169 | * Description: |
bwilson30 | 0:42c032fc907a | 170 | * Sends a binary command string to the Si4735. |
bwilson30 | 0:42c032fc907a | 171 | * Parameters: |
bwilson30 | 0:42c032fc907a | 172 | * command - Binary command to be sent to the radio. |
bwilson30 | 0:42c032fc907a | 173 | * length - The number of characters in the command string (since it can't be null terminated!) |
bwilson30 | 0:42c032fc907a | 174 | * TODO: |
bwilson30 | 0:42c032fc907a | 175 | * Make the command wait for a valid CTS response from the radio before releasing control of the CPU. |
bwilson30 | 0:42c032fc907a | 176 | */ |
bwilson30 | 0:42c032fc907a | 177 | void sendCommand(char * command, int length); |
bwilson30 | 0:42c032fc907a | 178 | /* |
bwilson30 | 0:42c032fc907a | 179 | * Description: |
bwilson30 | 0:42c032fc907a | 180 | * Sends/Receives a character from the SPI bus. |
bwilson30 | 0:42c032fc907a | 181 | * Parameters: |
bwilson30 | 0:42c032fc907a | 182 | * value - The character to be sent to the SPI bus. |
bwilson30 | 0:42c032fc907a | 183 | * Returns: |
bwilson30 | 0:42c032fc907a | 184 | * The character read from the SPI bus during the transfer. |
bwilson30 | 0:42c032fc907a | 185 | */ |
bwilson30 | 0:42c032fc907a | 186 | char spiTransfer(char value); |
bwilson30 | 0:42c032fc907a | 187 | |
bwilson30 | 0:42c032fc907a | 188 | }; |
bwilson30 | 0:42c032fc907a | 189 | |
bwilson30 | 0:42c032fc907a | 190 | #endif |