Appiko
hal_spi_rf.c
1 /******************************************************************************
2  * Filename: hal_spi_rf.c
3  *
4  * Description: Implementation file for common spi access with the CCxxxx
5  * transceiver radios using trxeb. Supports CC1101/CC112X radios
6  *
7  * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
8  *
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in the
19  * documentation and/or other materials provided with the distribution.
20  *
21  * Neither the name of Texas Instruments Incorporated nor the names of
22  * its contributors may be used to endorse or promote products derived
23  * from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  *******************************************************************************/
38 
39 
40 /******************************************************************************
41  * INCLUDES
42  */
43 #include "hal_spi_rf.h"
44 #include "hal_spim.h"
45 
46 #include "stdint.h"
47 #include "string.h"
48 #include "hal_gpio.h"
49 #include "log.h"
50 #include "hal_nop_delay.h"
51 
52 
53 /******************************************************************************
54  * LOCAL FUNCTIONS
55  */
56 //static void trxReadWriteBurstSingle(uint8_t addr,uint8_t *pData,uint16_t len) ;
57 
58 void trxRfSpiInterfaceInit()
59 {
60  log_printf("%s\n", __func__);
61  hal_gpio_cfg_output (RF_CS_PIN, 1);
62  hal_gpio_cfg_output (RF_SCK_PIN, 1);
63  hal_gpio_cfg_input (RF_MISO_PIN, HAL_GPIO_PULL_UP);
64  hal_gpio_cfg_output (RF_MOSI_PIN, 1);
65  hal_spim_init_t default_spim_config =
66  {
67  .csBar_pin = RF_CS_PIN,
68  .miso_pin = RF_MISO_PIN,
69  .mosi_pin = RF_MOSI_PIN,
70  .sck_pin = RF_SCK_PIN,
71  .freq = HAL_SPIM_FREQ_2M,
72  .spi_mode = HAL_SPIM_SPI_MODE0,
73  .byte_order = HAL_SPIM_MSB_FIRST,
74  .en_intr = 0,
75  .irq_priority = APP_IRQ_PRIORITY_HIGHEST,
76  .tx_done_handler = NULL,
77  .rx_done_handler = NULL,
78 
79  };
80  hal_spim_init (&default_spim_config);
81 
82 }
83 
84 
85 /*******************************************************************************
86  * @fn trx8BitRegAccess
87  *
88  * @brief This function performs a read or write from/to a 8bit register
89  * address space. The function handles burst and single read/write
90  * as specfied in addrByte. Function assumes that chip is ready.
91  *
92  * input parameters
93  *
94  * @param accessType - Specifies if this is a read or write and if it's
95  * a single or burst access. Bitmask made up of
96  * RADIO_BURST_ACCESS/RADIO_SINGLE_ACCESS/
97  * RADIO_WRITE_ACCESS/RADIO_READ_ACCESS.
98  * @param addrByte - address byte of register.
99  * @param pData - data array
100  * @param len - Length of array to be read(TX)/written(RX)
101  *
102  * output parameters
103  *
104  * @return chip status
105  */
106 rfStatus_t trx8BitRegAccess(uint8_t accessType, uint8_t addrByte, uint8_t *pData, uint16_t len)
107 {
108  uint8_t readValue;
109 
110 // /* Pull CS_N low and wait for SO to go low before communication starts */
111 // RF_SPI_BEGIN();
112 // while(RF_PORT_IN & RF_MISO_PIN);
113 // /* send register address byte */
114 // RF_SPI_TX(accessType|addrByte);
115 // RF_SPI_WAIT_DONE();
116 // /* Storing chip status */
117 // readValue = RF_SPI_RX();
118 // trxReadWriteBurstSingle(accessType|addrByte,pData,len);
119 // RF_SPI_END();
120 // /* return the status byte value */
121  uint8_t tx_buff[257];
122  tx_buff[0] = accessType|addrByte;
123  uint8_t rx_buff[257];
124 
125 
126  if((accessType&RADIO_READ_ACCESS) == RADIO_READ_ACCESS)
127  {
128  memset (&tx_buff[1], 0x00, len);
129  hal_spim_tx_rx (tx_buff, len+1, rx_buff, 1 + len);
130  while(hal_spim_is_busy ());
131  memcpy(pData,rx_buff + 1, len);
132  }
133  else if((accessType&RADIO_WRITE_ACCESS) == RADIO_WRITE_ACCESS)
134  {
135  memcpy (&tx_buff[1], pData, len);
136  hal_spim_tx_rx (tx_buff, len+1, rx_buff, 2);
137  while(hal_spim_is_busy ());
138  }
139 
140 // ((uint8_t*)&status)[1]=rx_buff[0];
141 // ((uint8_t*)&status)[0]=rx_buff[1];
142  readValue = rx_buff[0];
143 
144  return(readValue);
145 }
146 
147 /******************************************************************************
148  * @fn trx16BitRegAccess
149  *
150  * @brief This function performs a read or write in the extended adress
151  * space of CC112X.
152  *
153  * input parameters
154  *
155  * @param accessType - Specifies if this is a read or write and if it's
156  * a single or burst access. Bitmask made up of
157  * RADIO_BURST_ACCESS/RADIO_SINGLE_ACCESS/
158  * RADIO_WRITE_ACCESS/RADIO_READ_ACCESS.
159  * @param extAddr - Extended register space address = 0x2F.
160  * @param regAddr - Register address in the extended address space.
161  * @param *pData - Pointer to data array for communication
162  * @param len - Length of bytes to be read/written from/to radio
163  *
164  * output parameters
165  *
166  * @return rfStatus_t
167  */
168 rfStatus_t trx16BitRegAccess(uint8_t accessType, uint8_t extAddr, uint8_t regAddr, uint8_t *pData, uint8_t len)
169 {
170  uint8_t readValue = 0;
171 
172 // RF_SPI_BEGIN();
173 // while(RF_PORT_IN & RF_MISO_PIN);
174 // /* send extended address byte with access type bits set */
175 // RF_SPI_TX(accessType|extAddr);
176 // RF_SPI_WAIT_DONE();
177 // /* Storing chip status */
178 // readValue = RF_SPI_RX();
179 // RF_SPI_TX(regAddr);
180 // RF_SPI_WAIT_DONE();
181 // /* Communicate len number of bytes */
182 // trxReadWriteBurstSingle(accessType|extAddr,pData,len);
183 // RF_SPI_END();
184  /* return the status byte value */
185  uint8_t tx_buff[257];
186  tx_buff[0] = accessType|extAddr;
187  tx_buff[1] = regAddr;
188  uint8_t rx_buff[257];
189 
190 
191  if((accessType&RADIO_READ_ACCESS) == RADIO_READ_ACCESS)
192  {
193  memset (&tx_buff[2], 0x00, len);
194  hal_spim_tx_rx (tx_buff, len+2, rx_buff, len+2);
195  while(hal_spim_is_busy ());
196  memcpy(pData, &rx_buff[2], len);
197  }
198  else if((accessType&RADIO_WRITE_ACCESS) == RADIO_WRITE_ACCESS)
199  {
200  memcpy (&tx_buff[2], pData, len);
201  hal_spim_tx_rx (tx_buff, len+2, rx_buff, 2);
202  while(hal_spim_is_busy ());
203  }
204 
205 // ((uint8_t*)&status)[1]=rx_buff[0];
206 // ((uint8_t*)&status)[0]=rx_buff[1];
207  readValue = rx_buff[0];
208 
209  return(readValue);
210 }
211 
212 /*******************************************************************************
213  * @fn trxSpiCmdStrobe
214  *
215  * @brief Send command strobe to the radio. Returns status byte read
216  * during transfer of command strobe. Validation of provided
217  * is not done. Function assumes chip is ready.
218  *
219  * input parameters
220  *
221  * @param cmd - command strobe
222  *
223  * output parameters
224  *
225  * @return status byte
226  */
227 rfStatus_t trxSpiCmdStrobe(uint8_t cmd)
228 {
229  uint8_t rc;
230 // RF_SPI_BEGIN();
231 // while(RF_PORT_IN & RF_MISO_PIN);
232 // RF_SPI_TX(cmd);
233 // RF_SPI_WAIT_DONE();
234 // rc = RF_SPI_RX();
235 // RF_SPI_END();
236  hal_spim_tx_rx (&cmd, 1, NULL, 0);
237  while(hal_spim_is_busy ());
238  rc = 0;
239 
240  return(rc);
241 }
242 
243 /*******************************************************************************
244  * @fn trxReadWriteBurstSingle
245  *
246  * @brief When the address byte is sent to the SPI slave, the next byte
247  * communicated is the data to be written or read. The address
248  * byte that holds information about read/write -and single/
249  * burst-access is provided to this function.
250  *
251  * Depending on these two bits this function will write len bytes to
252  * the radio in burst mode or read len bytes from the radio in burst
253  * mode if the burst bit is set. If the burst bit is not set, only
254  * one data byte is communicated.
255  *
256  * NOTE: This function is used in the following way:
257  *
258  * RF_SPI_BEGIN();
259  * while(RF_PORT_IN & RF_SPI_MISO_PIN);
260  * ...[Depending on type of register access]
261  * trxReadWriteBurstSingle(uint8_t addr,uint8_t *pData,uint16_t len);
262  * RF_SPI_END();
263  *
264  * input parameters
265  *
266  * @param none
267  *
268  * output parameters
269  *
270  * @return void
271  */
272 //static void trxReadWriteBurstSingle(uint8_t addr,uint8_t *pData,uint16_t len)
273 //{
274 // uint16_t i;
275 // /* Communicate len number of bytes: if RX - the procedure sends 0x00 to push bytes from slave*/
276 // if(addr&RADIO_READ_ACCESS)
277 // {
278 // if(addr&RADIO_BURST_ACCESS)
279 // {
280 // for (i = 0; i < len; i++)
281 // {
282 // RF_SPI_TX(0); /* Possible to combining read and write as one access type */
283 // RF_SPI_WAIT_DONE();
284 // *pData = RF_SPI_RX(); /* Store pData from last pData RX */
285 // pData++;
286 // }
287 // }
288 // else
289 // {
290 // RF_SPI_TX(0);
291 // RF_SPI_WAIT_DONE();
292 // *pData = RF_SPI_RX();
293 // }
294 // }
295 // else
296 // {
297 // if(addr&RADIO_BURST_ACCESS)
298 // {
299 // /* Communicate len number of bytes: if TX - the procedure doesn't overwrite pData */
300 // for (i = 0; i < len; i++)
301 // {
302 // RF_SPI_TX(*pData);
303 // RF_SPI_WAIT_DONE();
304 // pData++;
305 // }
306 // }
307 // else
308 // {
309 // RF_SPI_TX(*pData);
310 // RF_SPI_WAIT_DONE();
311 // }
312 // }
313 // return;
314 //}
void hal_spim_init(hal_spim_init_t *spim_init)
Function to Initiate the SPIM module.
Definition: hal_spim.c:60
uint32_t csBar_pin
Definition: hal_spim.h:96
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
HAL_GPIO_PULL_UP.
Definition: hal_gpio.h:39
uint32_t hal_spim_is_busy()
Function to check if SIPM module is available or not.
Definition: hal_spim.c:135