void main (void) { nx_systick_install_scheduler(security_hook); nx_rs485_init(); while (1) { lock = 1; nx_rs485_send(out_buffer, sizeof(out_buffer), send_callback); while (lock) { nx_systick_wait_ms(1000); } } }
void main() { /* We'll use this buffer to read data from the RS485 bus. This needs * to be volatile because it gets modified outside of the main code * path (in interrupt handlers). */ U8 buffer_hello[12] = "Hello world"; U8 buffer_empty[12] = {0}; /* Install our emergency shutdown hook. See above for details. */ nx_systick_install_scheduler(shutdown_hook); /* Initialize the RS485 driver for communication at 9600 bits per * second with no timeout. If you need special communication modes * (number of start/stop bits, parity checks...), you can specify an * explicit mode register value. */ nx_rs485_init(RS485_BR_9600, 0, 0, FALSE); for (int i = 0; i < 10; ++i) { /* This is just to alternate between sending two different * messages. One time we send "hello world", the other an empty * string. */ U8 *buffer = (i % 2 == 0) ? buffer_hello : buffer_empty; /* Display what we are transmitting, for the record */ nx_display_clear(); nx_display_string("Iteration "); nx_display_uint(i+1); nx_display_string("\n"); nx_display_string((char*)buffer); /* Wait a bit before actually sending, to get a nice obvious * delayed transmission effect. */ nx_systick_wait_ms(1000); /* Try to write 12 bytes to the RS485 bus. This call merely * instructs the RS485 driver to start writing data, and returns * immediately. * * Once the read of all 12 bytes is complete, the given callback * function will be called, with a status value, indicating * whether or not there was an error. */ nx_rs485_send(buffer, sizeof(buffer), send_callback); /* Since the above function returns immediately, we need a way to * wait for the receive to complete. How you do this depends on * how you want your application kernel to work, but in this case, * we'll just block on our spinlock, until the callback unlocks it * after the write completes. */ nx_spinlock_acquire(&lock); /* Wait before transmitting the next round, again purely for * effect. */ nx_systick_wait_ms(1000); } }