Contiki Rime stack how to get the broadcasting ID in Example_Broadcast - microcontroller

I am working on the broadcasting example of contiki's Rime stack :
https://github.com/contiki-os/contiki/blob/master/examples/rime/example-broadcast.c
When receiving a broadcast I can easily read out the senders id:
broadcast_recv(struct broadcast_conn *c, const linkaddr_t *from)
{
printf("Sender: %d.%d\n", from->u8[0], from->u8[1]);
}
But how can I get "my"/current node's ID?
Any help appreciated

The local address in Contiki is stored in linkaddr_node_addr:
#include "net/linkaddr.h"
...
printf("Local: %d.%d\n", linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
There is also another variable: uint16_t node_id, at least on most platforms. It is by default equal to linkaddr_node_addr.u8[0] + (linkaddr_node_addr.u8[1] << 8). Add #include "sys/node-id.h" for that.

add this header:#include "sys/node-id.h"
And then using"node_id" you can obtain the self id of the node!

Related

Make a "virtual port" which consists of multiple ports on Arduino

I started using ports on Arduino instead of setting every pin low or high by hand.
This is very useful and a lot faster. I am on a project, where i need at least one full port (8 bits) and at least one Serial Port.
I wanted to use the Arduino UNO but it has only one full port, port D.
PD0 and PD1 are used for Serial communication. This means i can't use port D.
I was wondering if there is a possibility for me to merge multiple ports into a "virtual port". In the end i want something like this:
PORTX = 0b11111111; // the first 2 bits are PB0/PB1 and bit 3-8 are PD3-PD8
Is this possible in any way???
I would say 'yes' it is possible, but maybe not in the way you want it (or maybe I just don't know how to do so^^)
First of all, the PORTS are macros from Atmel. Your Arduino-Uno is based on the AtMega328p and therefore uses the AVR-Toolchain with all those PORTS under the hood.
If you were about to program your microcontroller without the arduino-bootloader and all the fancy arduino-library-stuff, you would address all your GPIOs that way.
If you have a look into the code of the Atmel-AVR Toolchain (that arduino is sitting on top of), you would see, that the PORTS are defined in iom328p.h and are only addresses of internal IO-registers within the microcontroller.
So, just declaring a virtual-Port is not that easy (maybe with a kind of memory mapping with something similar to std::mmap() but I've never tried this one).
Anyway you are a programmer, so there is a solution to almost everthing ;)
I personally would suggest, to create your own Port-Class:
this class holds your required Pins as members and you have a setter, that overwrites your member-Pins according to the number you pass to it
(this code is not meant to be the 'perfect' solution, just a quick hint into the direction)
I would recommend you to stay with the arduino-library for this approach. If you do it with the plain PORTS, you might mess up something somewhere. So for example if you init your SerialPort and afterwards do something like PORTD |= (1<<PD0), you wont be able to receive any data and don't know why.
class MyPort
{
private:
uint8_t m_pin[8];
public:
MyPort(uint8_t pins[8])
{
for(int i=0; i<8; ++i)
{
m_pin[i] = pins[i]; //copy from constructor-argument into member-variable
pinMode(pins[i], OUTPUT); //setting pin as OUTPUT
}
}
void operator =(uint8_t val)
{
for(int i=0; i<8; ++i)
{
digitalWrite(m_pin[i], (val >> i)&1);
}
}
};
// B0,B1,D2,D3,D4,D5,D6,D7
// v v v v v v v v
uint8_t pins[]{8, 9, 2, 3, 4, 5, 6, 7};
MyPort PORTX(pins);
void setup()
{
PORTX = 0b11001100;
}
void loop()
{
// put your main code here, to run repeatedly:
}
please note that you will have to override the other operators as well, if you want bitwise addressing on your own port too

Microchip XC16 : Can we access Port using its address ?? for ex : &PortA?

As my question says, to access Port by its address, Can we write it as "&PORTA" ??
In my problem, I want to read/write port value from/to HMI, using Modbus Protocol.
I have an array of structure :
typedef struct func_code_reg {
volatile uint16_t addr;
volatile uint16_t *data;
}RW_REG_DATA;
// described as
RW_REG_DATA rwCoilStatusTbl[] = {
// Addr Data_Register
{ 0, &rwCoil_0000 },
{ 1, &rwCoil_0001 },
};
Whenever HMI reads data , it reads the current value of register &rwCoil_000x
Whenever HMI writes data, the register &rwCoil_000x gets updated.
Instead, I would like to use &PORTA to read Port status or to update Port Status.
Is it possible ?? & if possible, is it the correct way to update the Port status ??
Or any better way, please guide me.
(I am using dsPic33E series)
PORTx is already mapped to the contents of the PORTx register, you don't need its address. To read from a port, use the PORTx register. To write, use the LATx register.
So if you want the value rwCoil_000x to be reflected on a port (A), simply write:
LATA = rwCoil_000x;
And if you want to read from the port into the same variable, write:
rwCoil_000x = PORTA;
Of course, this assumes PORTA is set to be a general purpose output.
If you want to generalize over many different ports, you can build an array of volatile references to *PORT.
I did this once for the other way, the outputs, LAT registers, see Using an array of LATs to toggle outputs. type of (byte) pointer to lat

In Arduino, how do you write to port when port is a variable?

Examples of writing to a port seem to always use the port number as a constant, eg,
OCR2A = 180;
How do you write to the port when the port is unknown until run time. For example,
int port = (buttonPressed) ? 0x3b : 0x3c;
portWrite( port, 180 );
What I cannot find is the funtion portWrite(). Does something like that exist?
Robert's answer has some imprecise assertions and an incomplete answer.
Writing directly to port registers you can ruin other settings of the port and sometimes cause permanent damage to controller.
Can ruin other settings: true, you have to know what you are doing (for instance what pins are on the port you are manipulating, and know what are the functions you want to keep.
Can cause permanent damage: not really, or better not because of the port manipulation. If you wire a short circuit to ground and then set it as an output to 1, you can damage it whether you are using the port register or the digitalwrite. You have to be careful in both ways.
Now, returning to your problem, the enumeration is one way, but since the PORTB, PORTC, PORTD are just short name for values, you can set a variable and then use it to indirectly access it.
The type for this kind of variable is a volatile pointer to a byte (volatile means that write and read operations cannot be optimized by the compiler, since the value can change even between two operations):
volatile uint8_t *variablePortRegister;
You just have to load it with the address (so with the & symbol) of the register you want to change:
variablePortRegister = &PORTC;
then use the pointer to change the value
PORTC = 0x12;
becomes
(*variablePortRegister) = 0x12;
This is a short example. For it to work, connect a LED with resistor on arduino pin 5 (bit 5 of PORTD). The LED on the board (labeled L) is connected to pin 13 (bit 5 of PORTB).
The sketch will make one of the two leds blink for five times, then switch to the other. Only port manipulation instructions are used, and you can find that the way to read the port is the same as the one to write it.
volatile uint8_t *myportreg;
unsigned long lastTime;
uint8_t counter;
void setup() {
DDRB |= 0x20;
DDRD |= 0x20;
PORTB = 0;
PORTD = 0;
counter = 99; // trigger the register change immediately
}
void loop() {
if (counter >= 10)
{
counter = 0;
if (myportreg == &PORTD)
myportreg = &PORTB;
else
myportreg = &PORTD;
}
if ((millis() - lastTime) > 500)
{
lastTime = millis();
// change bit 5 of register
*myportreg = 0x20 ^ (*myportreg);
counter++;
}
}
EDIT: as Robert pointed out, it's much better to "use" just the pins you need (in this case, bit 5 of port B and D) rather than setting the whole port; this way you minimize the risk of screwing up something else. This edit is already included in the above code, so the code is correct
The port is a bit in one particular register. If you know the register and the position of the port in that particular register you can try this:
register = (1<<port) || register
to set the port to 1 and
register = (1<<port)^-1 && register
to set the port to 0.
Of course, you will need a switch somewhere to determine the register and the bit of the port in the register given the port name.

C - How to receive from the serial port for the device side (z1 mote)

I am trying to communicate with a z1 mote that is connected to my PC directly by using pyserial. What I am looking to do is to write to the mote, and upon receiving the command, the mote should reply the current reading for temperature, for example.
Python side can be something like this (iinm)
import serial
ser = serial.Serial(0)
ser.write("hello") # the mote will receive the message and do something
but I have no clue how to receive the message in the z1 mote side that uses C. Is there a special method to receive the commands or do I have to create my own?
Any tips and hints are a greatly appreciated.
If you want to just receive newline-terminated strings, Contiki already has functionality for that. Just wait for serial_line_event_message event in your protothread's loop:
#include "contiki.h"
#include "dev/serial-line.h"
PROCESS(main_process, "main process");
AUTOSTART_PROCESSES(&main_process);
PROCESS_THREAD(main_process, ev, data)
{
PROCESS_BEGIN();
for(;;) {
PROCESS_WAIT_EVENT();
if (ev == serial_line_event_message && data != NULL) {
printf("got input string: '%s'\n", (const char *) data);
}
}
PROCESS_END();
}
If on the other hand you want to customize the reception (e.g. to allow binary data, or to use custom framing, or to include checksums) you need to handle the input at the level of individual characters. Define and set a UART callback on the right UART (on the Z1 platform USB is connected to UART 0, but the number and the exact name of the function are platform-dependent). An example serial input handler function:
static int serial_input_byte(unsigned char c)
{
printf("got input byte: %d ('%c')\n", c, c);
}
And then put this in your initialization code:
uart0_set_input(serial_input_byte);

sending 'A' character by using bitbanging method

i am trying to send 'A' character by using bitbanging method. Can anyone help me to write this code in send_serial() function
void send_serial()
{
//send data
}
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_2);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab
//!!!!!!!!!!!deney!!!!!!!!!!!!!
//c6 pin using for data transfer
//config tris
//set_tris_c(??)
while(1)
{
//'A' on ascii 0x41
send_serial();
delay_ms(1000);
}
}
This is fully described in Microchip's Application Note AN510 for PICs without a built in UART. However, if you can change to one which has a UART you would find the code much easier and it eases the timing restriction since the bits are sent by the hardware. The description of how to use a built in UART is described in the PIC datasheet or AN774.

Resources