[jaluino] r429 committed - first dirty sample for wireless module NRF24L01+, test SPI and module ...

11 views
Skip to first unread message

jal...@googlecode.com

unread,
May 26, 2013, 8:55:06 AM5/26/13
to jal...@googlegroups.com
Revision: 429
Author: sebastienlelong
Date: Sun May 26 05:54:49 2013
Log: first dirty sample for wireless module NRF24L01+, test SPI and
module connectivity
http://code.google.com/p/jaluino/source/detail?r=429

Added:
/trunk/lib/nrf24l01.jal
/trunk/samples/jaluino_bee_nrf24_readreg.jal

=======================================
--- /dev/null
+++ /trunk/lib/nrf24l01.jal Sun May 26 05:54:49 2013
@@ -0,0 +1,284 @@
+-- ------------------------------------------------------
+-- Title: nRF24L01 library (wireless)
+-- Author: Sebastien Lelong, Copyright (c) 2008..2013, all rights reserved.
+-- Adapted-by:
+-- Compiler: 2.4p
+-- Revision: $Revision: 373 $
+--
+-- This file is part of jaluino (http://jaluino.googlecode.com)
+-- Released under the BSD license
(http://www.opensource.org/licenses/bsd-license.php)
+--
+-- Description: this library can be used with Crumboard shield, and
declares some
+-- aliases for conveniency. It basically describe the shield with logical
name.
+-- (crumboard_led1 instead of pin_A0)
+--
+-- Sources: http://github.com/aaronds/arduino-nrf24l01/downloads
+--
+-- Notes: jalv2 port of MiRF lib for arduino. Greatly inspired from MiRF
lib port for arduino, by
+-- Aaron Shrimpton and Stefan Engelke
+--
+-- URL:
+--
+
+-- -----------------
+-- CONSTANTS DEFS --
+-- -----------------
+-- (from header file)
+
+-- Memory Map
+const byte NRF24_CONFIG_REG = 0x00
+const byte NRF24_EN_AA = 0x01
+const byte NRF24_EN_RXADDR = 0x02
+const byte NRF24_SETUP_AW = 0x03
+const byte NRF24_SETUP_RETR = 0x04
+const byte NRF24_RF_CH = 0x05
+const byte NRF24_RF_SETUP = 0x06
+const byte NRF24_STATUS = 0x07
+const byte NRF24_OBSERVE_TX = 0x08
+const byte NRF24_CD = 0x09
+const byte NRF24_RX_ADDR_P0 = 0x0A
+const byte NRF24_RX_ADDR_P1 = 0x0B
+const byte NRF24_RX_ADDR_P2 = 0x0C
+const byte NRF24_RX_ADDR_P3 = 0x0D
+const byte NRF24_RX_ADDR_P4 = 0x0E
+const byte NRF24_RX_ADDR_P5 = 0x0F
+const byte NRF24_TX_ADDR = 0x10
+const byte NRF24_RX_PW_P0 = 0x11
+const byte NRF24_RX_PW_P1 = 0x12
+const byte NRF24_RX_PW_P2 = 0x13
+const byte NRF24_RX_PW_P3 = 0x14
+const byte NRF24_RX_PW_P4 = 0x15
+const byte NRF24_RX_PW_P5 = 0x16
+const byte NRF24_FIFO_STATUS = 0x17
+
+-- Bit Mnemonics
+const byte NRF24_MASK_RX_DR = 6
+const byte NRF24_MASK_TX_DS = 5
+const byte NRF24_MASK_MAX_RT = 4
+const byte NRF24_EN_CRC = 3
+const byte NRF24_CRCO = 2
+const byte NRF24_PWR_UP = 1
+const byte NRF24_PRIM_RX = 0
+const byte NRF24_ENAA_P5 = 5
+const byte NRF24_ENAA_P4 = 4
+const byte NRF24_ENAA_P3 = 3
+const byte NRF24_ENAA_P2 = 2
+const byte NRF24_ENAA_P1 = 1
+const byte NRF24_ENAA_P0 = 0
+const byte NRF24_ERX_P5 = 5
+const byte NRF24_ERX_P4 = 4
+const byte NRF24_ERX_P3 = 3
+const byte NRF24_ERX_P2 = 2
+const byte NRF24_ERX_P1 = 1
+const byte NRF24_ERX_P0 = 0
+const byte NRF24_AW = 0
+const byte NRF24_ARD = 4
+const byte NRF24_ARC = 0
+const byte NRF24_PLL_LOCK = 4
+const byte NRF24_RF_DR = 3
+const byte NRF24_RF_PWR = 1
+const byte NRF24_LNA_HCURR = 0
+const byte NRF24_RX_DR = 6
+const byte NRF24_TX_DS = 5
+const byte NRF24_MAX_RT = 4
+const byte NRF24_RX_P_NO = 1
+const byte NRF24_TX_FULL = 0
+const byte NRF24_PLOS_CNT = 4
+const byte NRF24_ARC_CNT = 0
+const byte NRF24_TX_REUSE = 6
+const byte NRF24_FIFO_FULL = 5
+const byte NRF24_TX_EMPTY = 4
+const byte NRF24_RX_FULL = 1
+const byte NRF24_RX_EMPTY = 0
+
+-- Instruction Mnemonics --
+const byte NRF24_R_REGISTER = 0x00
+const byte NRF24_W_REGISTER = 0x20
+const byte NRF24_REGISTER_MASK = 0x1F
+const byte NRF24_R_RX_PAYLOAD = 0x61
+const byte NRF24_W_TX_PAYLOAD = 0xA0
+const byte NRF24_FLUSH_TX = 0xE1
+const byte NRF24_FLUSH_RX_REG = 0xE2
+const byte NRF24_REUSE_TX_PL = 0xE3
+const byte NRF24_NOP = 0xFF
+
+-- Defaulting
+--
+-- Nrf24l settings
+const byte NRF24_ADDR_LEN = 5 ; seb: was used to loop over some arrays,
still useful ? (we have count() in jalv2)
+const byte NRF24_CONFIG = ((1<<NRF24_EN_CRC) | (0<<NRF24_CRCO) )
+;const byte NRF24_CONFIG = (0<<NRF24_CRCO)
+
+-- Payload width in bytes default 16 max 32
+if !defined(NRF24_PAYLOAD_SIZE) then
+ const byte NRF24_PAYLOAD_SIZE = 16
+end if
+
+-- Channel 0 - 127 or 0 - 84 in the US.
+if !defined(NRF24_CHANNEL) then
+ const byte NRF24_CHANNEL = 1
+end if
+
+-- Fool proof checks
+--
+-- CE Pin controls RX / TX, default 8.
+if !defined(nrf24_ce_pin) then
+ _error "Define nrf24_ce_pin pin alias"
+end if
+
+-- CSN Pin Chip Select Not, default 7.
+if !defined(nrf24_csn_pin) then
+ _error "Define nrf24_csn_pin pin alias"
+end if
+
+if !defined(NRF24_RECEIVE_ADDR) then
+ _error "Define NRF24_RECEIVE_ADDR as the receiving address for this
module"
+end if
+
+if !defined(spi_master_data) then
+ _error "Define spi_master_data as an alias to SPI data pseudo vars
(spi_master_hw or spi_master_hw2)"
+end if
+
+-- internal state: in read mode ?
+var bit nrf24_ptx
+
+-- ------
+-- API --
+-- ------
+procedure nrf24_init()
+procedure nrf24_config_register(byte in reg, byte in value)
+procedure nrf24_power_up_rx()
+procedure nrf24_flush_rx()
+procedure nrf24_set_raddr()
+procedure nrf24_write_register(byte in reg, byte in value[])
+procedure nrf24_transmit_sync(byte in datain[])
+procedure nrf24_read_register(byte in reg, byte in value[])
+procedure nrf24_transfer_sync(byte out dataout[]);, byte in datain[])
+
+
+-- Initializes pins to communicate with the MiRF module
+-- Should be called in the early initializing phase at startup.
+procedure nrf24_init() is
+ ;nrf24_ce_pin = low
+ ;nrf24_csn_pin = high
+ -- Set receiving address
+ nrf24_set_raddr()
+ -- Set RF channel
+ nrf24_config_register(NRF24_RF_CH,NRF24_CHANNEL)
+ -- Set length of incoming payload
+ nrf24_config_register(NRF24_RX_PW_P0, NRF24_PAYLOAD_SIZE)
+ nrf24_config_register(NRF24_RX_PW_P1, NRF24_PAYLOAD_SIZE)
+ -- Start receiver
+ nrf24_power_up_rx()
+ nrf24_flush_rx()
+end procedure
+
+-- Clocks only one byte into the given MiRF register
+procedure nrf24_config_register(byte in reg, byte in value) is
+ nrf24_csn_pin = low
+ spi_master_data = (NRF24_W_REGISTER | (NRF24_REGISTER_MASK & reg))
+ spi_master_data = value
+ nrf24_csn_pin = high
+end procedure
+
+procedure nrf24_power_up_rx() is
+ nrf24_ptx = 0
+ nrf24_ce_pin = low
+ nrf24_config_register(NRF24_CONFIG_REG, NRF24_CONFIG | (
(1<<NRF24_PWR_UP) | (1<<NRF24_PRIM_RX) ) )
+ nrf24_ce_pin = high
+ nrf24_config_register(NRF24_STATUS,(1 << NRF24_TX_DS) | (1 <<
NRF24_MAX_RT));
+end procedure
+
+procedure nrf24_flush_rx() is
+ nrf24_csn_pin = low
+ spi_master_data = NRF24_FLUSH_RX_REG
+ nrf24_csn_pin = high
+end procedure
+
+-- sets the receiving address
+procedure nrf24_set_raddr() is
+ nrf24_ce_pin = low
+ nrf24_write_register(NRF24_RX_ADDR_P1,NRF24_RECEIVE_ADDR)
+ nrf24_ce_pin = high
+end procedure
+
+-- Writes an array of bytes into inte the MiRF registers.
+procedure nrf24_write_register(byte in reg, byte in value[]) is
+ nrf24_csn_pin = low
+ spi_master_data = (NRF24_W_REGISTER | (NRF24_REGISTER_MASK & reg))
+ nrf24_transmit_sync(value)
+ nrf24_csn_pin = high
+end procedure
+
+procedure nrf24_transmit_sync(byte in datain[]) is
+ var byte i
+ var byte c
+ print_string(serial_hw_data,"count datain: ")
+ print_byte_dec(serial_hw_data,byte(count(datain)))
+ print_crlf(serial_hw_data)
+ for count(datain) using i loop
+ c = datain[i]
+ spi_master_data = c
+ end loop
+end procedure
+
+
+var byte xfer_tmp[] = {0x2F}
+-- Reads an array of bytes from the given start position in the MiRF
registers.
+procedure nrf24_read_register(byte in reg, byte out value[]) is
+ nrf24_csn_pin = low
+ spi_master_data = (NRF24_R_REGISTER | (NRF24_REGISTER_MASK & reg))
+ var byte dummy[] = {0x00}
+
+ print_string(serial_hw_data,"count val in read: ")
+ print_byte_dec(serial_hw_data,byte(count(value)))
+ print_crlf(serial_hw_data)
+
+ nrf24_transfer_sync(value);,value)
+ nrf24_csn_pin = high
+end procedure
+
+procedure nrf24_transfer_sync(byte out dataout[]) is ;, byte in datain[])
is
+ var byte i
+ var byte c
+ -- assuming dataout/datein have same size
+ print_string(serial_hw_data,"count val in xfer: ")
+ print_byte_dec(serial_hw_data,byte(count(dataout)))
+ print_crlf(serial_hw_data)
+ ;for count(dataout) using i loop
+ for 1 using i loop
+ ;spi_master_data = 0x00
+ c = spi_master_data
+ xfer_tmp[0] = c
+ print_crlf(serial_hw_data)
+ print_string(serial_hw_data,"temp i: Ã ")
+ print_byte_dec(serial_hw_data,i)
+ serial_hw_data = ">"
+ print_byte_bin(serial_hw_data,c)
+ serial_hw_data = " "
+ print_byte_bin(serial_hw_data,xfer_tmp[0])
+ print_crlf(serial_hw_data)
+ end loop
+end procedure
+
+;void send(uint8_t *value);
+;void setRADDR(uint8_t * adr);
+;void setTADDR(uint8_t * adr);
+;bool dataReady();
+;bool isSending();
+;bool rxFifoEmpty();
+;bool txFifoEmpty();
+;void getData(uint8_t * data);
+;uint8_t getStatus();
+;
+;void transmitSync(uint8_t *dataout,uint8_t len);
+;void transferSync(uint8_t *dataout,uint8_t *datain,uint8_t len);
+;void configRegister(uint8_t reg, uint8_t value);
+;void readRegister(uint8_t reg, uint8_t * value, uint8_t len);
+;void writeRegister(uint8_t reg, uint8_t * value, uint8_t len);
+;void powerUpRx();
+;void powerUpTx();
+;void powerDown();
+;
+;void flushRx();
+;
=======================================
--- /dev/null
+++ /trunk/samples/jaluino_bee_nrf24_readreg.jal Sun May 26 05:54:49 2013
@@ -0,0 +1,102 @@
+-- Title: nRF24L01 wireless network module sample for for Jaluino Bee,
read register values
+-- Author: Sébastien Lelong, Copyright (c) 2008..2139, all rights reserved.
+-- Adapted-by:
+-- Compiler: 2.4q
+-- Revision: $Revision: 373 $
+--
+-- This file is part of jaluino (http://jaluino.googlecode.com)
+-- Released under the BSD license
(http://www.opensource.org/licenses/bsd-license.php)
+--
+-- Description: test nRF24L01 module and SPI connectivity by displaying
register values
+-- on serial
+--
+-- Notes: connect pins accordingly
+-- - Vcc: 3V3
+-- - GND: GND (yes)
+-- - CE: A2
+-- - CSN: A3
+-- - SCK: A5
+-- - MO: B3 (SDO)
+-- - MI: B2 (SDI)
+-- (carefull, MO/MI aren't crossed-over as usual when using SPI device)
+--
+
+include jaluino_bee
+include delay
+include print
+include format
+
+-- ok, now setup serial
+const serial_hw_baudrate = 9_600
+include serial_hardware
+serial_hw_init()
+
+-- Configure Remappable Pins on MSSP2
+-- PPS module writes are protected, we need to first unlock it
+pps_control_lock(false)
+-- RP2 <-> SCK2
+PPS_MAP_RP2 = PPS_SCK2
+PPS_MAP_SCK2IN = RP2
+-- RP5 <-> SDI2
+PPS_MAP_SDI2 = RP5
+-- RP6 <-> SDO2
+PPS_MAP_RP6 = PPS_SDO2
+-- PPS configuration is done, we can lock again
+pps_control_lock(true)
+
+;;alias pin_ss2_direction is pin_b0_direction
+;;alias pin_ss2 is pin_b0
+alias pin_sdi2_direction is pin_b2_direction
+alias pin_sdo2_direction is pin_b3_direction
+alias pin_sck2_direction is pin_a5_direction
+
+-- configure SPI to use MSSP2 module
+include spi_master_hw2
+pin_sdi2_direction = input -- spi input
+pin_sdo2_direction = output -- spi output
+pin_sck2_direction = output -- spi clock
+alias spi_master_data is spi_master_hw2
+
+pin_A2_direction = output
+pin_A3_direction = output
+alias nrf24_ce_pin is pin_A2
+alias nrf24_csn_pin is pin_A3
+-- Receving adress, size must be NRF24_ADDR_LEN
+const byte NRF24_RECEIVE_ADDR[] = {0x00,0x01,0x02,0x03,0x04}
+include nrf24l01
+
+nrf24_ce_pin = low
+nrf24_csn_pin = high
+spi2_init(SPI_MODE_00,SPI_RATE_FOSC_4) -- init spi, choose mode and speed
+
+nrf24_init()
+
+
+print_string(serial_hw_data,"Reading registers...")
+print_crlf(serial_hw_data)
+
+forever loop
+var byte rf_setup[] = {0x00}
+print_string(serial_hw_data,"count rf_setup: ")
+print_byte_dec(serial_hw_data,count(rf_setup))
+print_crlf(serial_hw_data)
+nrf24_read_register(NRF24_RF_SETUP,rf_setup)
+print_string(serial_hw_data,"RF_SETUP: ")
+print_byte_bin(serial_hw_data,xfer_tmp[0])
+print_crlf(serial_hw_data)
+
+nrf24_read_register(NRF24_CONFIG_REG,rf_setup)
+print_string(serial_hw_data,"CONFIG: ")
+print_byte_bin(serial_hw_data,xfer_tmp[0])
+print_crlf(serial_hw_data)
+
+nrf24_read_register(NRF24_RX_ADDR_P1,rf_setup)
+print_string(serial_hw_data,"NRF24_RX_ADDR_P1: ")
+print_byte_bin(serial_hw_data,xfer_tmp[0])
+print_crlf(serial_hw_data)
+
+delay_1s(2)
+
+end loop
+
+
Reply all
Reply to author
Forward
0 new messages