SimpleLink CC3120/CC3220 Host Driver  Version 2.0.1.15
Simplifies the implementation of Internet connectivity
cc_pal.c
1 /*
2  * Copyright (C) 2015 Texas Instruments Incorporated
3  *
4  * All rights reserved. Property of Texas Instruments Incorporated.
5  * Restricted rights to use, duplicate or disclose this code are
6  * granted through contract.
7  *
8  * The program may not be used without the written permission of
9  * Texas Instruments Incorporated or against the terms and conditions
10  * stipulated in the agreement under which this program has been supplied,
11  * and under no circumstances can it be used with non-TI connectivity device.
12  *
13  */
14 /******************************************************************************
15 * msp_pal.c
16 *
17 * Simplelink Wi-Fi platform abstraction file for MSP432
18 ******************************************************************************/
19 #include <ti/drivers/SPI.h>
20 #include <ti/drivers/GPIO.h>
21 #include <ti/drivers/net/wifi/porting/cc_pal.h>
22 #include <ti/drivers/net/wifi/simplelink.h>
23 
24 #define MSP_EXP432P401R_HOST_IRQ (2) /* Board P2.5 */
25 #define MSP_EXP432P401R_nHIB_pin (5) /* Board P4.1 */
26 #define MSP_EXP432P401R_CS_pin (6) /* Board P3.0 */
27 #define MAX_DMA_TRANSACTION_SIZE (1024)
28 #define SPI_RATE_8M (8000000)
29 #define SPI_RATE_10M (10000000)
30 #define SPI_RATE_13M (13000000)
31 #define SPI_RATE_20M (20000000)
32 #define ASSERT_CS() (GPIO_write(MSP_EXP432P401R_CS_pin, 0))
33 #define DEASSERT_CS() (GPIO_write(MSP_EXP432P401R_CS_pin, 1))
34 
35 /****************************************************************************
36  GLOBAL VARIABLES
37 ****************************************************************************/
38 
39 volatile Fd_t g_SpiFd = 0;
40 SL_P_EVENT_HANDLER g_Host_irq_Hndlr = NULL;
41 
42 /****************************************************************************
43  LOCAL FUNCTION DEFINITIONS
44 ****************************************************************************/
45 
46 Fd_t spi_Open(char *ifName, unsigned long flags)
47 {
48  void *lspi_hndl;
49  SPI_Params SPI_Config;
50 
51  /* configure the SPI settings */
52  SPI_Config.transferMode = SPI_MODE_BLOCKING;
53  SPI_Config.mode = SPI_MASTER;
54  SPI_Config.bitRate = 20000000;
55  SPI_Config.dataSize = 8;
56  SPI_Config.frameFormat = SPI_POL0_PHA0;
57 
58  /* Initialize the SPI config structure */
59  SPI_Params_init(&SPI_Config);
60 
61  /* Open SPI interface with SPI_Config parameters */
62  lspi_hndl = SPI_open(0, &SPI_Config);
63  if(NULL == lspi_hndl)
64  {
65  return -1;
66  }
67  else
68  {
69  return (Fd_t)lspi_hndl;
70  }
71 }
72 
73 
74 int spi_Close(Fd_t fd)
75 {
76  SPI_close((void *)fd);
77  return 0;
78 }
79 
80 
81 int spi_Read(Fd_t fd, unsigned char *pBuff, int len)
82 {
83  SPI_Transaction transact_details;
84  int read_size = 0;
85 
86  ASSERT_CS();
87 
88  /* check if the link SPI has been initialized successfully */
89  if(fd < 0)
90  {
91  DEASSERT_CS();
92  return -1;
93  }
94 
95  transact_details.txBuf = NULL;
96  transact_details.arg = NULL;
97  transact_details.rxBuf = (void*)(pBuff);
98 
99  while(len > 0)
100  {
101  if(len > MAX_DMA_TRANSACTION_SIZE)
102  {
103  transact_details.count = MAX_DMA_TRANSACTION_SIZE ;
104  }
105  else
106  {
107  transact_details.count = len ;
108  }
109 
110  if(SPI_transfer((SPI_Handle)fd, &transact_details))
111  {
112  read_size += transact_details.count;
113  len = len - transact_details.count;
114  transact_details.rxBuf = ((unsigned char *)(transact_details.rxBuf) + read_size);
115  }
116  else
117  {
118  DEASSERT_CS();
119  return -1;
120  }
121  }
122 
123  DEASSERT_CS();
124 
125  return(read_size);
126 }
127 
128 
129 int spi_Write(Fd_t fd, unsigned char *pBuff, int len)
130 {
131  SPI_Transaction transact_details;
132  int write_size = 0;
133 
134  ASSERT_CS();
135 
136  /* check if the link SPI has been initialized successfully */
137  if(fd < 0)
138  {
139  DEASSERT_CS();
140  return -1;
141  }
142 
143  transact_details.rxBuf = NULL;
144  transact_details.arg = NULL;
145  transact_details.txBuf = (void*)(pBuff);
146 
147  while(len > 0)
148  {
149  if(len > MAX_DMA_TRANSACTION_SIZE)
150  {
151  transact_details.count = MAX_DMA_TRANSACTION_SIZE ;
152  }
153  else
154  {
155  transact_details.count = len ;
156  }
157 
158  if(SPI_transfer((SPI_Handle)fd, &transact_details))
159  {
160  write_size += transact_details.count;
161  len = len - transact_details.count;
162  transact_details.txBuf = ((unsigned char *)(transact_details.txBuf) + write_size);
163  }
164  else
165  {
166  DEASSERT_CS();
167  return -1;
168  }
169  }
170 
171  DEASSERT_CS();
172 
173  return(write_size);
174 }
175 
176 
177 int NwpRegisterInterruptHandler(P_EVENT_HANDLER InterruptHdl , void* pValue)
178 {
179  /* Check for unregister condition */
180  if(NULL == InterruptHdl)
181  {
182  GPIO_disableInt(MSP_EXP432P401R_HOST_IRQ);
183  GPIO_clearInt(MSP_EXP432P401R_HOST_IRQ);
184  g_Host_irq_Hndlr = NULL;
185  return 0;
186  }
187  else if(NULL == g_Host_irq_Hndlr)
188  {
189  g_Host_irq_Hndlr = InterruptHdl;
190  GPIO_setCallback(MSP_EXP432P401R_HOST_IRQ, HostIrqGPIO_callback);
191  GPIO_enableInt(MSP_EXP432P401R_HOST_IRQ);
192  return 0;
193  }
194  else
195  {
196  /* An error occurred */
197  return -1;
198  }
199 }
200 
201 
202 void HostIrqGPIO_callback(uint_least8_t index)
203 {
204  if((index == MSP_EXP432P401R_HOST_IRQ) && (NULL != g_Host_irq_Hndlr))
205  {
206  g_Host_irq_Hndlr(0);
207  }
208 }
209 
210 
211 void NwpMaskInterrupt()
212 {
213 
214 }
215 
216 
217 void NwpUnMaskInterrupt()
218 {
219 
220 }
221 
222 
223 void NwpPowerOnPreamble(void)
224 {
225  /* Maybe start timer here? */
226 }
227 
228 
229 void NwpPowerOn(void)
230 {
231  GPIO_write(MSP_EXP432P401R_nHIB_pin, 1);
232  /* wait 5msec */
233  ClockP_usleep(5000);
234 }
235 
236 
237 void NwpPowerOff(void)
238 {
239  GPIO_write(MSP_EXP432P401R_nHIB_pin, 0);
240  /* wait 5msec */
241  ClockP_usleep(5000);
242 }
243 
244 
245 int Semaphore_create_handle(SemaphoreP_Handle* pSemHandle)
246 {
247  (*(pSemHandle)) = SemaphoreP_create(0, NULL);
248 
249  if(!(*(pSemHandle)))
250  {
251  return SemaphoreP_FAILURE ;
252  }
253 
254  return SemaphoreP_OK;
255 }
256 
257 
258 int Mutex_create_handle(MutexP_Handle* pMutexHandle)
259 {
260 
261  (*(pMutexHandle)) = MutexP_create(NULL);
262 
263  if(!(*(pMutexHandle)))
264  {
265  return MutexP_FAILURE ;
266  }
267 
268  return MutexP_OK;
269 }
270 
271 
272 int Mutex_unlock(MutexP_Handle pMutexHandle)
273 {
274  MutexP_unlock(pMutexHandle, 0);
275  return(MutexP_OK);
276 }
277 
278 
279 int Mutex_lock(MutexP_Handle pMutexHandle)
280 {
281  MutexP_lock(pMutexHandle);
282  return(MutexP_OK);
283 }
284 
285 unsigned long TimerGetCurrentTimestamp()
286 {
287  return (ClockP_getSystemTicks());
288 }
289