1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/boards/avsextrem/drivers/avsextrem-cc1100.c

255 lines
5.0 KiB
C
Raw Normal View History

2013-08-19 16:17:58 +02:00
/*
* avsextrem-cc1100.c - CC100 Transceiver Driver for the AVSEXTREM-BOARD.
* Copyright (C) 2013 Heiko Will <hwill@inf.fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
2013-08-19 16:17:58 +02:00
/**
* @file
* @ingroup LPC2387
* @brief CC1100 LPC2387 dependend functions
*
* @author Heiko Will <hwill@inf.fu-berlin.de>
* @author Thomas Hillebrandt <hillebra@inf.fu-berlin.de>
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
*
* @version $Revision: 1781 $
*
* @note $Id: avsextrem-cc1100.c 1781 2013-08-14 13:39:36Z kasmi $
*/
#include <stdio.h>
#include <stddef.h>
// cpu
#include "cpu.h"
// sys
2013-12-24 16:31:45 +01:00
#include "cc110x_ng.h"
2013-08-19 16:17:58 +02:00
#include "gpioint.h"
2013-09-19 16:53:35 +02:00
#define CC1100_GDO0 (FIO2PIN & BIT6) // read serial I/O (GDO0)
#define CC1100_GDO1 (FIO0PIN & BIT8) // read serial I/O (GDO1)
#define CC1100_GDO2 (FIO0PIN & BIT28) // read serial I/O (GDO2)
2013-08-19 16:17:58 +02:00
2013-09-19 16:53:35 +02:00
#define SPI_TX_EMPTY (SSP1SR & SSPSR_TFE)
#define SPI_BUSY (SSP1SR & SSPSR_BSY)
#define SPI_RX_AVAIL (SSP1SR & SSPSR_RNE)
2013-08-19 16:17:58 +02:00
2013-09-19 16:53:35 +02:00
#define CC1100_GDO1_LOW_RETRY (100) // max. retries for GDO1 to go low
#define CC1100_GDO1_LOW_COUNT (2700) // loop count (timeout ~ 500 us) to wait
2013-08-19 16:17:58 +02:00
// for GDO1 to go low when CS low
//#define DEBUG
#ifdef DEBUG
static unsigned long time_value;
static void set_time(void)
{
time_value = 0;
}
static int test_time(int code)
{
time_value++;
if (time_value > 10000000) {
2014-05-12 02:44:57 +02:00
printf("CC1100 SPI alarm: %d!\n", code);
2013-08-19 16:17:58 +02:00
time_value = 0;
return 1;
}
return 0;
}
#endif
2014-02-12 00:08:38 +01:00
int cc110x_get_gdo0(void)
2013-08-19 16:17:58 +02:00
{
2013-09-19 16:53:35 +02:00
return CC1100_GDO0;
2013-08-19 16:17:58 +02:00
}
2014-02-12 00:08:38 +01:00
int cc110x_get_gdo1(void)
2013-08-19 16:17:58 +02:00
{
2013-09-19 16:53:35 +02:00
return CC1100_GDO1;
2013-08-19 16:17:58 +02:00
}
2014-02-12 00:08:38 +01:00
int cc110x_get_gdo2(void)
2013-08-19 16:17:58 +02:00
{
2013-09-19 16:53:35 +02:00
return CC1100_GDO2;
2013-08-19 16:17:58 +02:00
}
2014-02-12 00:08:38 +01:00
void cc110x_spi_init(void)
2013-08-19 16:17:58 +02:00
{
// configure chip-select
FIO0DIR |= BIT6;
FIO0SET = BIT6;
// Power
2013-09-19 16:53:35 +02:00
PCONP |= PCSSP1; // Enable power for SSP1 (default is on)
2013-08-19 16:17:58 +02:00
// PIN Setup
2013-09-19 16:53:35 +02:00
PINSEL0 |= BIT15; // Set CLK function to SSP1
2013-08-19 16:17:58 +02:00
PINSEL0 &= ~BIT14;
2013-09-19 16:53:35 +02:00
PINSEL0 |= BIT17 ; // Set MISO function to SSP1
2013-08-19 16:17:58 +02:00
PINSEL0 &= ~BIT16;
2013-09-19 16:53:35 +02:00
PINSEL0 |= BIT19; // Set MOSI function to SSP1
2013-08-19 16:17:58 +02:00
PINSEL0 &= ~BIT18;
// Interface Setup
SSP1CR0 = 7;
// Clock Setup
uint32_t pclksel;
uint32_t cpsr;
lpc2387_pclk_scale(F_CPU / 1000, 6000, &pclksel, &cpsr);
2013-09-19 16:53:35 +02:00
PCLKSEL0 &= ~(BIT21 | BIT20); // CCLK to PCLK divider
2013-08-19 16:17:58 +02:00
PCLKSEL0 |= pclksel << 20;
SSP1CPSR = cpsr;
// Enable
2013-09-19 16:53:35 +02:00
SSP1CR1 |= BIT1; // SSP-Enable
2013-08-19 16:17:58 +02:00
int dummy;
// Clear RxFIFO:
2013-09-19 16:53:35 +02:00
while (SPI_RX_AVAIL) { // while RNE (Receive FIFO Not Empty)...
dummy = SSP1DR; // read data
2013-08-19 16:17:58 +02:00
}
/* to suppress unused-but-set-variable */
(void) dummy;
2013-08-19 16:17:58 +02:00
}
uint8_t
2014-02-12 00:08:38 +01:00
cc110x_txrx(uint8_t c)
2013-08-19 16:17:58 +02:00
{
uint8_t result;
SSP1DR = c;
#ifdef DEBUG
set_time();
#endif
while (!SPI_TX_EMPTY) {
#ifdef DEBUG
test_time(0);
#endif
}
#ifdef DEBUG
set_time();
#endif
while (SPI_BUSY) {
#ifdef DEBUG
test_time(1);
#endif
}
#ifdef DEBUG
set_time();
#endif
while (!SPI_RX_AVAIL) {
#ifdef DEBUG
test_time(2);
#endif
}
result = (uint8_t)SSP1DR;
return result;
}
2014-02-12 00:08:38 +01:00
void cc110x_spi_cs(void)
2013-08-19 16:17:58 +02:00
{
FIO0CLR = BIT6;
}
void
2014-02-12 00:08:38 +01:00
cc110x_spi_select(void)
2013-08-19 16:17:58 +02:00
{
volatile int retry_count = 0;
volatile int abort_count;
// Switch to GDO mode input
2013-09-19 16:53:35 +02:00
PINSEL0 &= ~(BIT17 + BIT16); // Set MISO function to GPIO
2013-08-19 16:17:58 +02:00
FIO0DIR &= ~BIT8;
cs_low:
// CS to low
abort_count = 0;
FIO0CLR = BIT6;
// Wait for SO to go low (voltage regulator
// has stabilized and the crystal is running)
loop:
asm volatile("nop");
if (CC1100_GDO1) {
abort_count++;
if (abort_count > CC1100_GDO1_LOW_COUNT) {
retry_count++;
if (retry_count > CC1100_GDO1_LOW_RETRY) {
puts("[CC1100 SPI] fatal error\n");
goto final;
}
2013-09-19 16:53:35 +02:00
FIO0SET = BIT6; // CS to high
goto cs_low; // try again
2013-08-19 16:17:58 +02:00
}
goto loop;
}
final:
// Switch to SPI mode
2013-09-19 16:53:35 +02:00
PINSEL0 |= BIT17; // Set MISO function to SSP1
2013-08-19 16:17:58 +02:00
PINSEL0 &= ~BIT16;
}
void
2014-02-12 00:08:38 +01:00
cc110x_spi_unselect(void)
2013-08-19 16:17:58 +02:00
{
FIO0SET = BIT6;
}
2014-02-12 00:08:38 +01:00
void cc110x_gdo2_disable(void)
2013-08-19 16:17:58 +02:00
{
gpioint_set(0, BIT28, GPIOINT_DISABLE, NULL);
}
2014-02-12 00:08:38 +01:00
void cc110x_gdo2_enable(void)
2013-08-19 16:17:58 +02:00
{
2013-12-24 16:31:45 +01:00
gpioint_set(0, BIT28, GPIOINT_FALLING_EDGE, &cc110x_gdo2_irq);
2013-08-19 16:17:58 +02:00
}
2014-02-12 00:08:38 +01:00
void cc110x_before_send(void)
2013-08-19 16:17:58 +02:00
{
// Disable GDO2 interrupt before sending packet
2014-02-12 00:08:38 +01:00
cc110x_gdo2_disable();
2013-08-19 16:17:58 +02:00
}
2014-02-12 00:08:38 +01:00
void cc110x_after_send(void)
2013-08-19 16:17:58 +02:00
{
// Enable GDO2 interrupt after sending packet
2014-02-12 00:08:38 +01:00
cc110x_gdo2_enable();
2013-08-19 16:17:58 +02:00
}
2014-02-12 00:08:38 +01:00
void cc110x_gdo0_enable(void)
2013-08-19 16:17:58 +02:00
{
2013-12-24 16:31:45 +01:00
gpioint_set(2, BIT6, GPIOINT_RISING_EDGE, &cc110x_gdo0_irq);
2013-08-19 16:17:58 +02:00
}
2014-02-12 00:08:38 +01:00
void cc110x_gdo0_disable(void)
2013-08-19 16:17:58 +02:00
{
gpioint_set(2, BIT6, GPIOINT_DISABLE, NULL);
}
2014-02-12 00:08:38 +01:00
void cc110x_init_interrupts(void)
2013-08-19 16:17:58 +02:00
{
// Enable external interrupt on low edge (for GDO2)
FIO0DIR &= ~BIT28;
2014-02-12 00:08:38 +01:00
cc110x_gdo2_enable();
2013-08-19 16:17:58 +02:00
// Enable external interrupt on low edge (for GDO0)
FIO2DIR &= ~BIT6;
}