Advanced Internet of Things firmware engineering with Thingsquare and Contiki - day 1, part 3

7,538 views
7,465 views

Published on

The third part of the first day of the Thingsquare advanced IoT firmware engineering course. http://thingsquare.com/training/

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,538
On SlideShare
0
From Embeds
0
Number of Embeds
4,309
Actions
Shares
0
Downloads
1
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Advanced Internet of Things firmware engineering with Thingsquare and Contiki - day 1, part 3

  1. 1. Contiki Libraries
  2. 2. Contiki ringbuf • Interrupt-safe way to pass bytes from interrupt to non-interrupt code • Ring buffer size must be even power of two • Ring buffer can only pass bytes (uint8_t)
  3. 3. ringbuf Void ringbuf_init (struct ringbuf *r, uint8_t *a, uint8_t size_power_of_two); int ringbuf_put (struct ringbuf *r, uint8_t c); int ringbuf_get (struct ringbuf *r); int ringbuf_size (struct ringbuf *r); int ringbuf_elements (struct ringbuf *r);
  4. 4. The list library • Convenient way to keep arbitrary structs on a list • Only requirement is a ->next pointer at beginning of the struct • Example: struct my_data { struct my_data *next; int some_data; uint8_t more_data[8]; }
  5. 5. The list library
  6. 6. The list library void list_init (list_t list) Initialize a list. void *list_head (list_t list) Get a pointer to the first element of a list. void list_push (list_t list, void *item) Add an item to the start of the list. void *list_pop (list_t list) Remove the first object on a list. void *list_item_next (void *item) Get the next item following this item.
  7. 7. The list library #include "lib/list.h" struct example_list_struct { struct *next; int number; }; LIST(example_list); static void my_function(void) { struct example_list_struct *s; list_init(example_list); list_add(example_list, &element1); list_add(example_list, &element2); for(s = list_head(example_list); s != NULL; s = list_item_next(s)) { printf("List element number %dn", s->number); } }
  8. 8. The memb library • Manage a chunk of memory • A fixed set of structs that can be allocated and deallocated • Size of set specified at compile time
  9. 9. The memb library • MEMB(name, structure, num) – Declare a memory block. • void memb_init(struct memb *m) – Initialize a memory block. • void *memb_alloc(struct memb *m) – Allocate a memory block. • int memb_free(struct memb *m, void *ptr) – Free a memory block.
  10. 10. The memb library struct my_structure { int a, b; } MEMB(my_mem, struct my_structure, NUM_STRUCTS); static void my_function(void) { memb_init(&my_mem); struct my_structure *s = memb_alloc(&my_mem); memb_free(s); }
  11. 11. Memory allocation with a list • Typical usage pattern: – Allocate memory from memb – Maintain on a list • Makes it easy to keep track of things to deallocate them later
  12. 12. Memory allocation with a list struct example_num { struct example_num *next; int num; }; #define MAX_NUMS 16 LIST(num_table); MEMB(num_mem, struct example_num, MAX_NUMS); void init_num(void) { memb_init(&num_mem); list_init(neighbor_table); }
  13. 13. Memory allocation with a list void add_num(int num) { e = memb_alloc(&neighbor_mem); if(e != NULL) { e->num = num; list_add(num_table, e); } } struct example_num { void remove_num(struct example_num *e) { list_remove(num_table, e); memb_free(&num_mem, e); }
  14. 14. Memory allocation with a list struct example_num *find_num(int num) { struct example_num *n; for(n = list_head(num_table); n != NULL; n = list_element_next(n)) { if(n->num == num) { return n; } } return NULL; }
  15. 15. The mmem library • Managed memory allcator • Maintains a fragmentation-free memory area • Sometimes useful • Somewhat tricky to use
  16. 16. The mmem library
  17. 17. The mmem library • MMEM_PTR(m) – Provide a pointer to managed memory. • int mmem_alloc(struct mmem *m, unsigned int size) – Allocated managed memory. • void mmem_free(struct mmem *) – Free managed memory. • void mmem_init(void) – Initialize the managed memory library.
  18. 18. The lower layers of the netstack
  19. 19. The radio driver • Input – Read packet from the radio into packetbuf – Call NETSTACK_RDC.input(); • Output – Prepare radio for sending • NETSTACK_RADIO.prepare() – Send the packet • NETSTACK_RADIO.transmit()
  20. 20. Radio driver gotchas • Radio driver works in two layers – Interrupt context – Contiki context • SPI bus must be protected – Disable interrupts during an SPI transfer • Radio core must be protected – Maintain flag to avoid interrupts when radio driver is active
  21. 21. Radio driver energy estimator • The Contiki energest module keeps track of energy consumption – – – – ENERGEST_ON(ENERGEST_TYPE_LISTEN); ENERGEST_OFF(ENERGEST_TYPE_LISTEN); ENERGEST_ON(ENERGEST_TYPE_TRANSMIT); ENERGEST_OFF(ENERGEST_TYPE_TRANSMIT);
  22. 22. queuebufs • “Enough” queuebufs are needed • Difficult to say beforehand how many that is • Trial and error may be needed
  23. 23. Hands-on: HTTP POST
  24. 24. Build big-red-button.c as a firmware image • • • • • Copy big-red-button.c from demo.thsq.io Compile and upload on the hardware Connect the button Sniff the packets on screen Code walk-through
  25. 25. More http://thingsquare.com

×