[jaluino] r435 committed - clean lib, test samples (counting errors)

3 views
Skip to first unread message

jal...@googlecode.com

unread,
Jun 22, 2013, 11:34:16 AM6/22/13
to jal...@googlegroups.com
Revision: 435
Author: sebastienlelong
Date: Sat Jun 22 08:33:58 2013
Log: clean lib, test samples (counting errors)
http://code.google.com/p/jaluino/source/detail?r=435

Added:
/trunk/samples/jaluino_bee_nrf24_ping.jal
/trunk/samples/jaluino_bee_nrf24_pong.jal
Modified:
/trunk/lib/nrf24l01.jal

=======================================
--- /dev/null
+++ /trunk/samples/jaluino_bee_nrf24_ping.jal Sat Jun 22 08:33:58 2013
@@ -0,0 +1,112 @@
+-- Title: nRF24L01 sample, send data (ping) expecting a reply (pong).
+-- 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: ping request and pong reply are numbered to track lost
packet
+--
+-- 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,
+-- this is what you must connect to the PIC, not what's on NRF side)
+--
+
+include jaluino_bee
+include delay
+include print
+include format
+
+-- Assuming LED is enable on hardware (jumper)
+onboard_led_direction = output
+
+-- 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_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_exchange is spi_master_hw2_exchange
+
+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[] = {0x05,0x06,0x07,0x08,0x09}
+-- will send dword values
+const byte NRF24_PAYLOAD_SIZE = 4
+include nrf24l01
+
+nrf24_ce_pin = low
+nrf24_csn_pin = high
+spi2_init(SPI_MODE_00,SPI_RATE_FOSC_16) -- init spi, choose mode and speed
+
+delay_1ms(10)
+nrf24_init()
+
+const byte str[] = "Alive..."
+print_string(serial_hw_data,str)
+print_crlf(serial_hw_data)
+
+nrf24_print_registers()
+
+const byte str2[] = "Sending: "
+
+-- set destination address
+var byte taddr[] = {0x00,0x01,0x02,0x03,0x04} -- see receive sample addr
+nrf24_set_taddr(taddr)
+
+var dword counter = 1
+var byte n0 at counter
+var byte n1 at counter + 1
+var byte n2 at counter + 2
+var byte n3 at counter + 3
+
+forever loop
+
+ print_string(serial_hw_data,str2)
+ print_dword_dec(serial_hw_data,counter)
+ print_crlf(serial_hw_data)
+ onboard_led = high
+ nrf24_data = n0
+ nrf24_data = n1
+ nrf24_data = n2
+ nrf24_data = n3
+ counter = counter + 1
+ onboard_led = low
+ delay_1ms(100)
+
+end loop
+
=======================================
--- /dev/null
+++ /trunk/samples/jaluino_bee_nrf24_pong.jal Sat Jun 22 08:33:58 2013
@@ -0,0 +1,134 @@
+-- Title: nRF24L01 sample, receive data and reply (pong)
+-- 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:
+--
+-- 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
+include jascii
+
+-- 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_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_exchange is spi_master_hw2_exchange
+
+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}
+-- will receive one byte at a time
+const byte NRF24_PAYLOAD_SIZE = 4
+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()
+
+const byte str[] = "Alive..."
+print_string(serial_hw_data,str)
+print_crlf(serial_hw_data)
+
+nrf24_print_registers()
+
+var dword counter = 0
+var byte n0 at counter
+var byte n1 at counter + 1
+var byte n2 at counter + 2
+var byte n3 at counter + 3
+
+var dword expected = 0
+var dword errors = 0
+
+forever loop
+
+ onboard_led = high
+
+ n0 = nrf24_data
+ n1 = nrf24_data
+ n2 = nrf24_data
+ n3 = nrf24_data
+
+ onboard_led = low
+
+ if expected == 0 then
+ -- init expected value as current one
+ expected = counter
+ end if
+
+ print_string(serial_hw_data,"Received: ")
+ print_dword_dec(serial_hw_data,counter)
+ serial_hw_data = " "
+ print_string(serial_hw_data,"Expected: ")
+ print_dword_dec(serial_hw_data,expected)
+ serial_hw_data = " "
+
+ -- check
+
+ if expected == counter then
+ expected = expected + 1
+ else
+ errors = errors + counter - expected -- missed several messages ?
+ expected = counter + 1 -- reinit for next value
+ end if
+
+ print_string(serial_hw_data,"Errors: ")
+ print_dword_dec(serial_hw_data,errors)
+ for 20 loop
+ serial_hw_data = " "
+ end loop
+
+ ;serial_hw_data = ASCII_CR
+ print_crlf(serial_hw_data)
+
+
+
+end loop
+
=======================================
--- /trunk/lib/nrf24l01.jal Sat Jun 15 10:02:15 2013
+++ /trunk/lib/nrf24l01.jal Sat Jun 22 08:33:58 2013
@@ -18,9 +18,12 @@
-- Aaron Shrimpton and Stefan Engelke
--
-- TODO:
--- * remove serial_hw2_* hard-coded references
-- * remove delay between csn low/high ?
---
+-- * implement multiple pipeline (probably needs to revise buffers)
+-- * rename buffers, confusing (in/out)
+-- * implement channel scanner to pick the best available (based on
round-trip time ?)
+-- * implement helper such as speed select, power select,...
+-- ...

-- -----------------
-- CONSTANTS DEFS --
@@ -140,8 +143,8 @@
_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)"
+if !defined(spi_master_exchange) then
+ _error "Define spi_master_exchange as an alias to SPI full duplex
exchange function (spi_master_hw_exchange or spi_master_hw2_exchange)"
end if

-- internal state: in read mode ?
@@ -192,8 +195,8 @@
-- 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
+ var byte _trash = spi_master_exchange(NRF24_W_REGISTER |
(NRF24_REGISTER_MASK & reg))
+ _trash = spi_master_exchange(value)
nrf24_csn_pin = high
end procedure

@@ -207,7 +210,7 @@

procedure nrf24_flush_rx() is
nrf24_csn_pin = low
- spi_master_data = NRF24_FLUSH_RX_REG
+ var byte _trash = spi_master_exchange(NRF24_FLUSH_RX_REG)
nrf24_csn_pin = high
end procedure

@@ -226,17 +229,15 @@
-- Writes an array of bytes into inte the MiRF registers.
procedure nrf24_write_register(byte in reg, byte in len) is
nrf24_csn_pin = low
- spi_master_data = (NRF24_W_REGISTER | (NRF24_REGISTER_MASK & reg))
+ var byte _trash = spi_master_exchange(NRF24_W_REGISTER |
(NRF24_REGISTER_MASK & reg))
nrf24_transmit_sync(len)
nrf24_csn_pin = high
end procedure

procedure nrf24_transmit_sync(byte in len) is
var byte i
- var byte c
for len using i loop
- c = nrf24_bufin[i]
- spi_master_data = c
+ var byte _trash = spi_master_exchange(nrf24_bufin[i])
end loop
end procedure

@@ -244,7 +245,7 @@
-- Reads an array of bytes from the given start position in the MiRF
registers.
procedure nrf24_read_register(byte in reg, byte in len) is
nrf24_csn_pin = low
- spi_master_data = (NRF24_R_REGISTER | (NRF24_REGISTER_MASK & reg))
+ var byte _trash = spi_master_exchange(NRF24_R_REGISTER |
(NRF24_REGISTER_MASK & reg))
nrf24_transfer_sync(len)
nrf24_csn_pin = high
end procedure
@@ -254,7 +255,7 @@
nrf24_bufout[0] = 0x00

for len loop
- nrf24_bufout[i] = spi_master_hw2_exchange(nrf24_bufin[i])
+ nrf24_bufout[i] = spi_master_exchange(nrf24_bufin[i])
i = i + 1
end loop
end procedure
@@ -276,12 +277,12 @@
nrf24_power_up_tx()
nrf24_csn_pin = low

- spi_master_data = NRF24_FLUSH_TX
+ var byte _trash = spi_master_exchange(NRF24_FLUSH_TX)
nrf24_csn_pin = high
delay_1ms(1) ; delete ?
nrf24_csn_pin = low

- spi_master_data = NRF24_W_TX_PAYLOAD
+ _trash = spi_master_exchange(NRF24_W_TX_PAYLOAD)
-- send as many bytes as declared in payload
nrf24_transmit_sync(NRF24_PAYLOAD_SIZE)

@@ -352,7 +353,7 @@
-- Reads payload bytes into data array
procedure nrf24_get_data() is
nrf24_csn_pin = low -- Pull down chip select
- spi_master_data = NRF24_R_RX_PAYLOAD -- Send cmd to read rx payload
+ var byte _trash = spi_master_exchange(NRF24_R_RX_PAYLOAD)
nrf24_transfer_sync(NRF24_PAYLOAD_SIZE)
nrf24_csn_pin = high -- Pull up chip select
-- NVI: per product spec, p 67, note c:
Reply all
Reply to author
Forward
0 new messages