text chat demo application for sx1232

Dependencies:   SX1232 mbed

summary

This is a demo-example application of using SX1232 driver.

It provides text console over SerialPC.

Committer:
dudmuck
Date:
Thu May 02 01:27:18 2013 +0000
Revision:
1:e642eed3eac4
Parent:
0:5b0e70b88863
license

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dudmuck 0:5b0e70b88863 1 #include "sx1232.h"
dudmuck 0:5b0e70b88863 2
dudmuck 0:5b0e70b88863 3 /* Chat application over text console via SerialPC.
dudmuck 0:5b0e70b88863 4 * Tested with teraterm.
dudmuck 1:e642eed3eac4 5 *
dudmuck 1:e642eed3eac4 6 * Licensed under the Apache License, Version 2.0 (the "License");
dudmuck 1:e642eed3eac4 7 * you may not use this file except in compliance with the License.
dudmuck 1:e642eed3eac4 8 * You may obtain a copy of the License at
dudmuck 1:e642eed3eac4 9 *
dudmuck 1:e642eed3eac4 10 * http://www.apache.org/licenses/LICENSE-2.0
dudmuck 1:e642eed3eac4 11 *
dudmuck 1:e642eed3eac4 12 * Unless required by applicable law or agreed to in writing, software
dudmuck 1:e642eed3eac4 13 * distributed under the License is distributed on an "AS IS" BASIS,
dudmuck 1:e642eed3eac4 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dudmuck 1:e642eed3eac4 15 * See the License for the specific language governing permissions and
dudmuck 1:e642eed3eac4 16 * limitations under the License.
dudmuck 0:5b0e70b88863 17 */
dudmuck 0:5b0e70b88863 18
dudmuck 0:5b0e70b88863 19 // pins for freescale freedom:
dudmuck 0:5b0e70b88863 20 // mosi, miso, sclk, cs, rst, dio0
dudmuck 0:5b0e70b88863 21 SX1232 radio(PTD2, PTD3, PTD1, PTD0, PTD5, PTA13);
dudmuck 0:5b0e70b88863 22
dudmuck 0:5b0e70b88863 23 Serial pc(USBTX, USBRX);
dudmuck 0:5b0e70b88863 24
dudmuck 0:5b0e70b88863 25 void radio_init_user()
dudmuck 0:5b0e70b88863 26 {
dudmuck 0:5b0e70b88863 27 // default narrow-band settings
dudmuck 0:5b0e70b88863 28 radio.set_bitrate(4800);
dudmuck 0:5b0e70b88863 29 radio.set_tx_fdev_hz(5000); // tx deviation
dudmuck 0:5b0e70b88863 30 radio.set_rx_dcc_bw_hz(0, 10000); // rx bandwidth
dudmuck 0:5b0e70b88863 31 radio.enable_afc(1);
dudmuck 0:5b0e70b88863 32
dudmuck 0:5b0e70b88863 33 // pick your own frequency within your regulatory limits.
dudmuck 0:5b0e70b88863 34 radio.set_frf_MHz(914.1);
dudmuck 0:5b0e70b88863 35
dudmuck 0:5b0e70b88863 36 /* radio.RegPaConfig.bits.OutputPower = X
dudmuck 0:5b0e70b88863 37 * radio.RegPaConfig.bits.PaSelect = 1 PA_BOOST
dudmuck 0:5b0e70b88863 38 * radio.write_reg(REG_PACONFIG, RegPaConfig.octet); */
dudmuck 0:5b0e70b88863 39 }
dudmuck 0:5b0e70b88863 40
dudmuck 0:5b0e70b88863 41 void
dudmuck 0:5b0e70b88863 42 service_radio()
dudmuck 0:5b0e70b88863 43 {
dudmuck 0:5b0e70b88863 44 int len;
dudmuck 0:5b0e70b88863 45
dudmuck 0:5b0e70b88863 46 switch (radio.service_action) {
dudmuck 0:5b0e70b88863 47 case SERVICE_READ_FIFO: // this occurs when CrcOk in DIO1 pin from radio
dudmuck 0:5b0e70b88863 48 radio.service_action = SERVICE_NONE;
dudmuck 0:5b0e70b88863 49 len = radio.read_fifo();
dudmuck 0:5b0e70b88863 50 radio.rx_buf[len] = 0; // null terminate for printing text
dudmuck 0:5b0e70b88863 51 printf("%s\r\n", radio.rx_buf);
dudmuck 0:5b0e70b88863 52 break;
dudmuck 0:5b0e70b88863 53 case SERVICE_ERROR:
dudmuck 0:5b0e70b88863 54 radio.service_action = SERVICE_NONE;
dudmuck 0:5b0e70b88863 55 printf("dio0_callback() %d\r\n", radio.RegDioMapping1.bits.Dio0Mapping);
dudmuck 0:5b0e70b88863 56 break;
dudmuck 0:5b0e70b88863 57 case SERVICE_TX_DONE:
dudmuck 0:5b0e70b88863 58 radio.service_action = SERVICE_NONE;
dudmuck 0:5b0e70b88863 59 radio.start_rx();
dudmuck 0:5b0e70b88863 60 break;
dudmuck 0:5b0e70b88863 61 default: // nothing necessary to do
dudmuck 0:5b0e70b88863 62 break;
dudmuck 0:5b0e70b88863 63 } // ...switch (radio.service_action)
dudmuck 0:5b0e70b88863 64 }
dudmuck 0:5b0e70b88863 65
dudmuck 0:5b0e70b88863 66 int main()
dudmuck 0:5b0e70b88863 67 {
dudmuck 0:5b0e70b88863 68 int txbuf_idx = 0;
dudmuck 0:5b0e70b88863 69 const int maxmsg = sizeof(radio.tx_buf)-1;
dudmuck 0:5b0e70b88863 70
dudmuck 0:5b0e70b88863 71 radio_init_user();
dudmuck 0:5b0e70b88863 72 radio.start_rx();
dudmuck 0:5b0e70b88863 73
dudmuck 0:5b0e70b88863 74 printf("\r\nsx1232_chat\r\n");
dudmuck 0:5b0e70b88863 75 while (1) {
dudmuck 0:5b0e70b88863 76 if (pc.readable()) {
dudmuck 0:5b0e70b88863 77 char c = pc.getc();
dudmuck 0:5b0e70b88863 78 if (c == 8 && txbuf_idx > 0) { // backspace
dudmuck 0:5b0e70b88863 79 pc.putc(8);
dudmuck 0:5b0e70b88863 80 pc.putc(' ');
dudmuck 0:5b0e70b88863 81 pc.putc(8);
dudmuck 0:5b0e70b88863 82 txbuf_idx--;
dudmuck 0:5b0e70b88863 83 } else if (c == '\r') {
dudmuck 0:5b0e70b88863 84 //radio.tx_buf[txbuf_idx] = 0; // null terminate (if printing)
dudmuck 0:5b0e70b88863 85 radio.start_tx(txbuf_idx);
dudmuck 0:5b0e70b88863 86 printf("\r\n");
dudmuck 0:5b0e70b88863 87 txbuf_idx = 0;
dudmuck 0:5b0e70b88863 88 } else if (txbuf_idx < maxmsg) {
dudmuck 0:5b0e70b88863 89 radio.tx_buf[txbuf_idx++] = c;
dudmuck 0:5b0e70b88863 90 pc.putc(c);
dudmuck 0:5b0e70b88863 91 }
dudmuck 0:5b0e70b88863 92 } else
dudmuck 0:5b0e70b88863 93 service_radio();
dudmuck 0:5b0e70b88863 94 } // ...while(1)
dudmuck 0:5b0e70b88863 95
dudmuck 0:5b0e70b88863 96 }