Appiko
irq_msg_util.c
1 
19 #include "irq_msg_util.h"
20 #include "stdbool.h"
21 #include "stddef.h"
22 #include "CBUF.h"
23 #include "nrf_util.h"
24 #include "string.h"
25 #include "nrf_assert.h"
26 
27 //For all the code not public for file, message is shortened to msg
28 
31 #define msgbuf_SIZE 16
32 
34 #if (!(!(msgbuf_SIZE & (msgbuf_SIZE-1)) && msgbuf_SIZE))
35 #error msgbuf_SIZE must be a power of 2
36 #endif
37 
38 struct message
39 {
40  irq_msg_types type;
41  void * more_data;
42 };
43 
44 volatile static struct
45 {
46  uint32_t m_getIdx;
47  uint32_t m_putIdx;
48  struct message m_entry[msgbuf_SIZE];
49 } msgbuf;
50 
51 irq_msg_callbacks cb_list = { NULL, NULL };
52 
53 void irq_msg_init(irq_msg_callbacks * cb_ptr)
54 {
55  CBUF_Init(msgbuf);
56 
57  ASSERT((cb_ptr->next_interval_cb != NULL)
58  && (cb_ptr->state_change_cb != NULL));
59 
60  cb_list.next_interval_cb = cb_ptr->next_interval_cb;
61  cb_list.state_change_cb = cb_ptr->state_change_cb;
62 }
63 
64 void irq_msg_push(irq_msg_types pushed_msg, void * more_data)
65 {
67 
68  struct message msg;
69  msg.type = pushed_msg;
70  msg.more_data = more_data;
71 
72  CBUF_Push(msgbuf, msg);
73 
75 }
76 
77 void irq_msg_process(void)
78 {
79  while (CBUF_Len(msgbuf))
80  {
81  struct message msg = CBUF_Pop(msgbuf);
82 
83 // log_printf("_%x\n",msg.type);
84  switch (msg.type)
85  {
86  case MSG_NEXT_INTERVAL:
87  cb_list.next_interval_cb(
88  (uint32_t) (msg.more_data));
89  break;
90  case MSG_STATE_CHANGE:
91  cb_list.state_change_cb(
92  (uint32_t) (msg.more_data));
93  break;
94  default:
95  break;
96  }
97  }
98 }
#define CBUF_Push(cbuf, elem)
Definition: CBUF.h:103
#define CRITICAL_REGION_ENTER()
Macro for entering a critical region.
Definition: nrf_util.h:103
#define CRITICAL_REGION_EXIT()
Macro for leaving a critical region.
Definition: nrf_util.h:113
#define CBUF_Init(cbuf)
Definition: CBUF.h:90
#define ASSERT(expression)
Macro for runtime assertion of an expression. If the expression is false the assert_nrf_callback func...
Definition: nrf_assert.h:48
#define CBUF_Pop(cbuf)
Definition: CBUF.h:109
void irq_msg_init(irq_msg_callbacks *cb_ptr)
Definition: irq_msg_util.c:53
void irq_msg_process(void)
Definition: irq_msg_util.c:77
#define CBUF_Len(cbuf)
Definition: CBUF.h:97
This file contains global definitions for circular buffer manipulation.
void irq_msg_push(irq_msg_types pushed_msg, void *more_data)
Definition: irq_msg_util.c:64