Appiko
hal_spim.c
1 
19 #include <stddef.h>
20 
21 #include "hal_spim.h"
22 #include "common_util.h"
23 #include "hal_gpio.h"
24 #include "stddef.h"
25 #include "nrf_assert.h"
26 #include "log.h"
27 
28 #if ISR_MANAGER == 1
29 #include "isr_manager.h"
30 #endif
31 
35 #define SPIM_ID CONCAT_2(NRF_SPIM,SPIM_USED)
36 
37 #define SPIM_IRQN SPIM_IRQN_a(SPIM_USED)
38 #define SPIM_IRQ_Handler SPIM_IRQ_Handler_a(SPIM_USED)
39 
40 #define SPIM_IRQ_Handler_a(n) SPIM_IRQ_Handler_b(n)
41 #define SPIM_IRQ_Handler_b(n) SPIM##n##_SPIS##n##_TWIM##n##_TWIS##n##_SPI##n##_TWI##n##_IRQHandler
42 
43 #define SPIM_IRQN_a(n) SPIM_IRQN_b(n)
44 #define SPIM_IRQN_b(n) SPIM##n##_SPIS##n##_TWIM##n##_TWIS##n##_SPI##n##_TWI##n##_IRQn
45 
48 static uint32_t csBar = 0;
49 
51 static uint32_t intr_enabled = 0;
52 
54 static volatile bool mod_is_busy = false;
55 
57 void (*rx_done) (uint32_t last_byte_no);
58 void (*tx_done) (uint32_t last_byte_no);
59 
60 void hal_spim_init (hal_spim_init_t * spim_init)
61 {
62  hal_gpio_cfg_output (spim_init->csBar_pin, 1);
63  SPIM_ID->TASKS_SUSPEND = 1;
64  SPIM_ID->TASKS_STOP = 1;
65  SPIM_ID->CONFIG = spim_init->byte_order | ((spim_init->spi_mode)<<1);
66  SPIM_ID->FREQUENCY = spim_init->freq;
67  SPIM_ID->PSEL.MISO = spim_init->miso_pin;
68  SPIM_ID->PSEL.MOSI = spim_init->mosi_pin;
69  SPIM_ID->PSEL.SCK = spim_init->sck_pin;
70  csBar = spim_init->csBar_pin;
71  intr_enabled = spim_init->en_intr;
72  SPIM_ID->INTENSET = spim_init->en_intr | SPIM_INTENSET_END_Msk;
73  NVIC_SetPriority (SPIM_IRQN, APP_IRQ_PRIORITY_MID);
74  NVIC_EnableIRQ (SPIM_IRQN);
75  log_printf("Intr En : %d\n", intr_enabled);
76  if(intr_enabled != 0)
77  {
78  NVIC_SetPriority (SPIM_IRQN, spim_init->irq_priority);
79  NVIC_EnableIRQ (SPIM_IRQN);
80  }
81  else
82  {
83  }
84  if(spim_init->rx_done_handler != NULL)
85  {
86  rx_done = spim_init->rx_done_handler;
87  }
88  if(spim_init->tx_done_handler != NULL)
89  {
90  tx_done = spim_init->tx_done_handler;
91  }
92  SPIM_ID->TXD.LIST = 1;
93  SPIM_ID->RXD.LIST = 1;
94  mod_is_busy = false;
95 }
96 
98 {
99  SPIM_ID->ENABLE = (SPIM_ENABLE_ENABLE_Disabled << SPIM_ENABLE_ENABLE_Pos) &
100  SPIM_ENABLE_ENABLE_Msk;
101  SPIM_ID->INTENCLR = intr_enabled;
102  SPIM_ID->TASKS_SUSPEND = 1;
103  SPIM_ID->TASKS_STOP = 1;
104  mod_is_busy = true;
105  NVIC_DisableIRQ (SPIM_IRQN);
106 }
107 
108 void hal_spim_tx_rx (void * p_tx_data, uint32_t tx_len, void * p_rx_data, uint32_t rx_len)
109 {
110  if(p_tx_data == NULL)
111  {
112  ASSERT(tx_len == 0);
113  }
114  if(p_rx_data == NULL)
115  {
116  ASSERT(rx_len == 0);
117  }
118  hal_gpio_pin_clear (csBar);
119 
120  SPIM_ID->EVENTS_ENDTX = 0;
121  SPIM_ID->TXD.PTR = (uint32_t) p_tx_data;
122  SPIM_ID->TXD.MAXCNT = tx_len;
123 
124  SPIM_ID->EVENTS_ENDRX = 0;
125  SPIM_ID->RXD.PTR = (uint32_t) p_rx_data;
126  SPIM_ID->RXD.MAXCNT = rx_len;
127 
128  SPIM_ID->ENABLE = (SPIM_ENABLE_ENABLE_Enabled << SPIM_ENABLE_ENABLE_Pos) &
129  SPIM_ENABLE_ENABLE_Msk;
130  mod_is_busy = true;
131  SPIM_ID->TASKS_START = 1;
132  (void) SPIM_ID->TASKS_START;
133 }
134 
135 uint32_t hal_spim_is_busy ()
136 {
137  return (uint32_t)mod_is_busy;
138 }
139 
140 #if ISR_MANAGER == 1
141 void hal_spim_Handler (void)
142 #else
143 void SPIM_IRQ_Handler (void)
144 #endif
145 {
146  if(SPIM_ID->EVENTS_END == 1)
147  {
148 #if ISR_MANAGER == 0
149  SPIM_ID->EVENTS_END = 0;
150 #endif
151  mod_is_busy = false;
152  hal_gpio_pin_set (csBar);
153  }
154  if(SPIM_ID->EVENTS_ENDTX == 1 && ((intr_enabled & HAL_SPIM_TX_DONE) != 0))
155  {
156 #if ISR_MANAGER == 0
157  SPIM_ID->EVENTS_ENDTX = 0;
158 #endif
159  if(tx_done != NULL)
160  {
161  tx_done(SPIM_ID->TXD.AMOUNT);
162  }
163  }
164  if(SPIM_ID->EVENTS_ENDRX == 1 && ((intr_enabled & HAL_SPIM_RX_DONE) != 0))
165  {
166 #if ISR_MANAGER == 0
167  SPIM_ID->EVENTS_ENDRX = 0;
168 #endif
169  if(rx_done != NULL)
170  {
171  rx_done(SPIM_ID->RXD.AMOUNT);
172  }
173  }
174 }
hal_spim_byte_order_t byte_order
Definition: hal_spim.h:110
void hal_spim_init(hal_spim_init_t *spim_init)
Function to Initiate the SPIM module.
Definition: hal_spim.c:60
app_irq_priority_t irq_priority
Definition: hal_spim.h:112
uint32_t csBar_pin
Definition: hal_spim.h:96
hal_spim_spi_mode_t spi_mode
Definition: hal_spim.h:106
void hal_spim_tx_rx(void *p_tx_data, uint32_t tx_len, void *p_rx_data, uint32_t rx_len)
Function to start communication.
Definition: hal_spim.c:108
uint32_t miso_pin
Definition: hal_spim.h:98
#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
hal_spim_freq_t freq
Definition: hal_spim.h:104
void(* rx_done_handler)(uint32_t bytes_last_rx)
Definition: hal_spim.h:121
uint32_t en_intr
Definition: hal_spim.h:117
void hal_spim_deinit()
Function to de-initialize the SPIM module.
Definition: hal_spim.c:97
void(* tx_done_handler)(uint32_t bytes_last_tx)
Definition: hal_spim.h:119
uint32_t mosi_pin
Definition: hal_spim.h:100
uint32_t sck_pin
Definition: hal_spim.h:102
uint32_t hal_spim_is_busy()
Function to check if SIPM module is available or not.
Definition: hal_spim.c:135