Simple example of RF commmunication for the RF-sub1GHz module. This one was called "dev_usb" in the rf_sub1G branch. This commit is part of manually merging the rf_sub1G branch.
This commit is contained in:
parent
90f7685bd2
commit
b682b3e37a
|
@ -0,0 +1,13 @@
|
|||
# Makefile for "rf_sub1G" apps
|
||||
# This includes all apps for the RF-sub1GHz Modules.
|
||||
|
||||
MODULE = $(shell basename $(shell cd .. && pwd && cd -))
|
||||
NAME = $(shell basename $(CURDIR))
|
||||
|
||||
.PHONY: $(NAME).bin
|
||||
$(NAME).bin:
|
||||
@make -C ../../.. --no-print-directory NAME=$(NAME) MODULE=$(MODULE) apps/$(MODULE)/$(NAME)/$@
|
||||
|
||||
clean mrproper:
|
||||
@make -C ../../.. --no-print-directory $@
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
Support for the Sub 1GHz module in USB or UEXT version
|
||||
|
||||
Copyright 2013 Nathael Pajani <nathael.pajani@ed3l.fr>
|
||||
|
||||
|
||||
/* ****************************************************************************
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*************************************************************************** */
|
||||
|
||||
This module uses the CC1101 transceiver to provide Sub 1GHz Rf connectivity.
|
||||
|
||||
The module also integrates a TMP101 temparature sensor, the usual bi-color led
|
||||
used for status, a RTC quartz, a DC-DC step-up for button battery power source
|
||||
support, and an extension port with a set of gpio, including ADC (2), PWM (2),
|
||||
I2C and SWD debug port.
|
||||
|
|
@ -0,0 +1,356 @@
|
|||
/****************************************************************************
|
||||
* apps/dev_usb/main.c
|
||||
*
|
||||
* sub1G_module support code - USB version
|
||||
*
|
||||
* Copyright 2013-2014 Nathael Pajani <nathael.pajani@ed3l.fr>
|
||||
*
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*************************************************************************** */
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include "core/lpc_regs_12xx.h"
|
||||
#include "core/lpc_core_cm0.h"
|
||||
#include "core/pio.h"
|
||||
#include "core/system.h"
|
||||
#include "core/systick.h"
|
||||
#include "lib/stdio.h"
|
||||
#include "drivers/serial.h"
|
||||
#include "drivers/gpio.h"
|
||||
#include "drivers/ssp.h"
|
||||
#include "drivers/i2c.h"
|
||||
#include "drivers/adc.h"
|
||||
#include "extdrv/cc1101.h"
|
||||
#include "extdrv/status_led.h"
|
||||
#include "extdrv/tmp101_temp_sensor.h"
|
||||
|
||||
|
||||
#define MODULE_VERSION 0x02
|
||||
#define MODULE_NAME "RF Sub1G - USB"
|
||||
|
||||
|
||||
#define RF_868MHz 1
|
||||
#define RF_915MHz 0
|
||||
#if ((RF_868MHz) + (RF_915MHz) != 1)
|
||||
#error Either RF_868MHz or RF_915MHz MUST be defined.
|
||||
#endif
|
||||
|
||||
|
||||
#define DEBUG 1
|
||||
#define BUFF_LEN 60
|
||||
|
||||
#define SELECTED_FREQ FREQ_SEL_48MHz
|
||||
|
||||
/***************************************************************************** */
|
||||
/* Pins configuration */
|
||||
/* pins blocks are passed to set_pins() for pins configuration.
|
||||
* Unused pin blocks can be removed safely with the corresponding set_pins() call
|
||||
* All pins blocks may be safelly merged in a single block for single set_pins() call..
|
||||
*/
|
||||
const struct pio_config common_pins[] = {
|
||||
/* UART 0 */
|
||||
{ LPC_UART0_RX_PIO_0_1, LPC_IO_DIGITAL },
|
||||
{ LPC_UART0_TX_PIO_0_2, LPC_IO_DIGITAL },
|
||||
/* I2C 0 */
|
||||
{ LPC_I2C0_SCL_PIO_0_10, (LPC_IO_DIGITAL | LPC_IO_OPEN_DRAIN_ENABLE) },
|
||||
{ LPC_I2C0_SDA_PIO_0_11, (LPC_IO_DIGITAL | LPC_IO_OPEN_DRAIN_ENABLE) },
|
||||
/* SPI */
|
||||
{ LPC_SSP0_SCLK_PIO_0_14, LPC_IO_DIGITAL },
|
||||
{ LPC_SSP0_MOSI_PIO_0_17, LPC_IO_DIGITAL },
|
||||
{ LPC_SSP0_MISO_PIO_0_16, LPC_IO_DIGITAL },
|
||||
/* ADC */
|
||||
{ LPC_ADC_AD0_PIO_0_30, LPC_IO_ANALOG },
|
||||
{ LPC_ADC_AD1_PIO_0_31, LPC_IO_ANALOG },
|
||||
{ LPC_ADC_AD2_PIO_1_0, LPC_IO_ANALOG },
|
||||
ARRAY_LAST_PIO,
|
||||
};
|
||||
|
||||
const struct pio cc1101_cs_pin = LPC_GPIO_0_15;
|
||||
const struct pio cc1101_miso_pin = LPC_SSP0_MISO_PIO_0_16;
|
||||
const struct pio cc1101_gdo0 = LPC_GPIO_0_6;
|
||||
const struct pio cc1101_gdo2 = LPC_GPIO_0_7;
|
||||
|
||||
#define TMP101_ADDR 0x94
|
||||
struct tmp101_sensor_config tmp101_sensor = {
|
||||
.addr = TMP101_ADDR,
|
||||
.resolution = TMP_RES_ELEVEN_BITS,
|
||||
};
|
||||
const struct pio temp_alert = LPC_GPIO_0_3;
|
||||
|
||||
const struct pio status_led_green = LPC_GPIO_0_28;
|
||||
const struct pio status_led_red = LPC_GPIO_0_29;
|
||||
|
||||
|
||||
#define ADC_VBAT LPC_ADC_NUM(0)
|
||||
#define ADC_EXT1 LPC_ADC_NUM(1)
|
||||
#define ADC_EXT2 LPC_ADC_NUM(2)
|
||||
|
||||
/***************************************************************************** */
|
||||
void system_init()
|
||||
{
|
||||
/* Stop the watchdog */
|
||||
stop_watchdog(); /* Do it right now, before it gets a chance to break in */
|
||||
|
||||
/* Note: Brown-Out detection must be powered to operate the ADC. adc_on() will power
|
||||
* it back on if called after system_init() */
|
||||
system_brown_out_detection_config(0);
|
||||
system_set_default_power_state();
|
||||
clock_config(SELECTED_FREQ);
|
||||
set_pins(common_pins);
|
||||
gpio_on();
|
||||
/* System tick timer MUST be configured and running in order to use the sleeping
|
||||
* functions */
|
||||
systick_timer_on(1); /* 1ms */
|
||||
systick_start();
|
||||
}
|
||||
|
||||
/* Define our fault handler. This one is not mandatory, the dummy fault handler
|
||||
* will be used when it's not overridden here.
|
||||
* Note : The default one does a simple infinite loop. If the watchdog is deactivated
|
||||
* the system will hang.
|
||||
*/
|
||||
void fault_info(const char* name, uint32_t len)
|
||||
{
|
||||
serial_write(0, name, len);
|
||||
/* Wait for end of Tx */
|
||||
serial_flush(0);
|
||||
/* FIXME : Perform soft reset of the micro-controller ! */
|
||||
while (1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***************************************************************************** */
|
||||
/* Temperature */
|
||||
/* The Temperature Alert pin is on GPIO Port 0, pin 7 (PIO0_7) */
|
||||
/* The I2C Temperature sensor is at address 0x94 */
|
||||
void WAKEUP_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
void temp_config()
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
/* Temp Alert */
|
||||
config_gpio(&temp_alert, LPC_IO_MODE_PULL_UP, GPIO_DIR_IN, 0);
|
||||
/* FIXME : add a callback on temp_alert edge */
|
||||
|
||||
/* Temp sensor */
|
||||
ret = tmp101_sensor_config(&tmp101_sensor);
|
||||
if (ret != 0) {
|
||||
serial_write(0, "Temp config error\r\n", 19);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/* RF Communication */
|
||||
#define RF_BUFF_LEN 64
|
||||
|
||||
static volatile int check_rx = 0;
|
||||
void rf_rx_calback(uint32_t gpio)
|
||||
{
|
||||
check_rx = 1;
|
||||
}
|
||||
|
||||
static uint8_t rf_specific_settings[] = {
|
||||
CC1101_REGS(gdo_config[2]), 0x07, /* GDO_0 - Assert on CRC OK | Disable temp sensor */
|
||||
CC1101_REGS(gdo_config[0]), 0x2E, /* GDO_2 - FIXME : do something usefull with it for tests */
|
||||
CC1101_REGS(pkt_ctrl[0]), 0x0F, /* Accept all sync, CRC err auto flush, Append, Addr check and Bcast */
|
||||
#if (RF_915MHz == 1)
|
||||
/* FIXME : Add here a define protected list of settings for 915MHz configuration */
|
||||
#endif
|
||||
};
|
||||
|
||||
/* RF config */
|
||||
void rf_config(void)
|
||||
{
|
||||
config_gpio(&cc1101_gdo0, LPC_IO_MODE_PULL_UP, GPIO_DIR_IN, 0);
|
||||
cc1101_init(0, &cc1101_cs_pin, &cc1101_miso_pin); /* ssp_num, cs_pin, miso_pin */
|
||||
/* Set default config */
|
||||
cc1101_config();
|
||||
/* And change application specific settings */
|
||||
cc1101_update_config(rf_specific_settings, sizeof(rf_specific_settings));
|
||||
set_gpio_callback(rf_rx_calback, &cc1101_gdo0, EDGE_RISING);
|
||||
|
||||
#ifdef DEBUG
|
||||
if (1) {
|
||||
char buff[BUFF_LEN];
|
||||
int len = 0;
|
||||
len = snprintf(buff, BUFF_LEN, "CC1101 RF link init done.\r\n");
|
||||
serial_write(0, buff, len);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void handle_rf_rx_data(void)
|
||||
{
|
||||
char buff[BUFF_LEN];
|
||||
int len = 0;
|
||||
uint8_t data[RF_BUFF_LEN];
|
||||
int8_t ret = 0;
|
||||
uint8_t status = 0;
|
||||
|
||||
/* Check for received packet (and get it if any) */
|
||||
ret = cc1101_receive_packet(data, RF_BUFF_LEN, &status);
|
||||
/* Go back to RX mode */
|
||||
cc1101_enter_rx_mode();
|
||||
|
||||
#ifdef DEBUG
|
||||
if (1) {
|
||||
len = snprintf(buff, BUFF_LEN, "RF: ret:%d, st: %d.\r\n", ret, status);
|
||||
serial_write(0, buff, len);
|
||||
}
|
||||
#endif
|
||||
|
||||
switch (data[2]) {
|
||||
case 'T':
|
||||
{
|
||||
uint16_t val = 0;
|
||||
int deci_degrees = 0;
|
||||
/* Read the temperature */
|
||||
if (tmp101_sensor_read(&tmp101_sensor, &val, &deci_degrees) != 0) {
|
||||
serial_write(0, "Temp read error\r\n", 19);
|
||||
} else {
|
||||
len = snprintf(buff, 40, "Temp read: %d,%d - raw: 0x%04x.\r\n",
|
||||
(deci_degrees/10), (deci_degrees%10), val);
|
||||
serial_write(0, buff, len);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'V':
|
||||
{
|
||||
uint16_t val = 0;
|
||||
/* Get and display the battery voltage */
|
||||
if (adc_get_value(&val, ADC_VBAT) >= 0) {
|
||||
int milli_volts = ((val * 32) / 10);
|
||||
len = snprintf(buff, 40, "Vbat: %d (raw: 0x%04x)\r\n", (milli_volts * 2), val);
|
||||
serial_write(0 , buff, len);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Data sent on radio comes from the UART, put any data received from UART in
|
||||
* cc_tx_buff and send when either '\r' or '\n' is received.
|
||||
* This function is very simple and data received between cc_tx flag set and
|
||||
* cc_ptr rewind to 0 may be lost. */
|
||||
static volatile uint32_t cc_tx = 0;
|
||||
static volatile uint8_t cc_tx_buff[RF_BUFF_LEN];
|
||||
static volatile uint8_t cc_ptr = 0;
|
||||
void handle_uart_cmd(uint8_t c)
|
||||
{
|
||||
if (cc_ptr < RF_BUFF_LEN) {
|
||||
cc_tx_buff[cc_ptr++] = c;
|
||||
} else {
|
||||
cc_ptr = 0;
|
||||
}
|
||||
if ((c == '\n') || (c == '\r')) {
|
||||
cc_tx = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void send_on_rf(void)
|
||||
{
|
||||
uint8_t cc_tx_data[RF_BUFF_LEN + 2];
|
||||
uint8_t tx_len = cc_ptr;
|
||||
int ret = 0;
|
||||
|
||||
/* Create a local copy */
|
||||
memcpy((char*)&(cc_tx_data[2]), (char*)cc_tx_buff, tx_len);
|
||||
/* "Free" the rx buffer as soon as possible */
|
||||
cc_ptr = 0;
|
||||
/* Prepare buffer for sending */
|
||||
cc_tx_data[0] = tx_len + 1;
|
||||
cc_tx_data[1] = 0; /* Broadcast */
|
||||
/* Send */
|
||||
if (cc1101_tx_fifo_state() != 0) {
|
||||
cc1101_flush_tx_fifo();
|
||||
}
|
||||
ret = cc1101_send_packet(cc_tx_data, (tx_len + 2));
|
||||
|
||||
#ifdef DEBUG
|
||||
{
|
||||
/* Give some feedback on UART 0 */
|
||||
char buff[BUFF_LEN];
|
||||
int len = 0;
|
||||
len = snprintf(buff, BUFF_LEN, "Tx ret: %d\r\n", ret);
|
||||
serial_write(0, buff, len);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/***************************************************************************** */
|
||||
int main(void) {
|
||||
system_init();
|
||||
uart_on(0, 115200, handle_uart_cmd);
|
||||
ssp_master_on(0, LPC_SSP_FRAME_SPI, 8, 4*1000*1000); /* bus_num, frame_type, data_width, rate */
|
||||
i2c_on(I2C_CLK_100KHz);
|
||||
adc_on();
|
||||
status_led_config(&status_led_green, &status_led_red);
|
||||
|
||||
/* Radio */
|
||||
rf_config();
|
||||
|
||||
/* Temperature sensor */
|
||||
temp_config();
|
||||
|
||||
while (1) {
|
||||
uint8_t status = 0;
|
||||
/* Request a Temp conversion on I2C TMP101 temperature sensor */
|
||||
tmp101_sensor_start_conversion(&tmp101_sensor); /* A conversion takes about 40ms */
|
||||
/* Start an ADC conversion to get battery voltage */
|
||||
adc_start_convertion_once(ADC_VBAT, 0);
|
||||
|
||||
/* Tell we are alive :) */
|
||||
chenillard(250);
|
||||
|
||||
/* RF */
|
||||
if (cc_tx == 1) {
|
||||
send_on_rf();
|
||||
cc_tx = 0;
|
||||
}
|
||||
/* Do not leave radio in an unknown or unwated state */
|
||||
do {
|
||||
status = (cc1101_read_status() & CC1101_STATE_MASK);
|
||||
} while (status == CC1101_STATE_TX);
|
||||
|
||||
if (status != CC1101_STATE_RX) {
|
||||
static uint8_t loop = 0;
|
||||
loop++;
|
||||
if (loop > 10) {
|
||||
if (cc1101_rx_fifo_state() != 0) {
|
||||
cc1101_flush_rx_fifo();
|
||||
}
|
||||
cc1101_enter_rx_mode();
|
||||
loop = 0;
|
||||
}
|
||||
}
|
||||
if (check_rx == 1) {
|
||||
check_rx = 0;
|
||||
handle_rf_rx_data();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue