1 /**
2  * Copyright (c) 2017 IpTronix
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT atmel_winc1500
8 
9 #define LOG_LEVEL CONFIG_WIFI_LOG_LEVEL
10 #include <zephyr/logging/log.h>
11 LOG_MODULE_REGISTER(winc1500);
12 
13 #include <stdio.h>
14 #include <stdint.h>
15 
16 #include <zephyr/device.h>
17 #include <zephyr/drivers/spi.h>
18 
19 #include "wifi_winc1500_nm_bsp_internal.h"
20 
21 #include <bsp/include/nm_bsp.h>
22 #include <common/include/nm_common.h>
23 #include <bus_wrapper/include/nm_bus_wrapper.h>
24 
25 #include "wifi_winc1500_config.h"
26 
27 #define NM_BUS_MAX_TRX_SZ	256
28 
29 tstrNmBusCapabilities egstrNmBusCapabilities = {
30 	NM_BUS_MAX_TRX_SZ
31 };
32 
33 #ifdef CONF_WINC_USE_I2C
34 
35 #define SLAVE_ADDRESS		0x60
36 
37 /** Number of times to try to send packet if failed. */
38 #define I2C_TIMEOUT		100
39 
nm_i2c_write(uint8_t * b,uint16_t sz)40 static int8_t nm_i2c_write(uint8_t *b, uint16_t sz)
41 {
42 	/* Not implemented */
43 }
44 
nm_i2c_read(uint8_t * rb,uint16_t sz)45 static int8_t nm_i2c_read(uint8_t *rb, uint16_t sz)
46 {
47 	/* Not implemented */
48 }
49 
nm_i2c_write_special(uint8_t * wb1,uint16_t sz1,uint8_t * wb2,uint16_t sz2)50 static int8_t nm_i2c_write_special(uint8_t *wb1, uint16_t sz1,
51 				 uint8_t *wb2, uint16_t sz2)
52 {
53 	/* Not implemented */
54 }
55 #endif
56 
57 #ifdef CONF_WINC_USE_SPI
58 
spi_rw(uint8_t * mosi,uint8_t * miso,uint16_t size)59 static int8_t spi_rw(uint8_t *mosi, uint8_t *miso, uint16_t size)
60 {
61 	const struct spi_buf buf_tx = {
62 		.buf = mosi,
63 		.len = size
64 	};
65 	const struct spi_buf_set tx = {
66 		.buffers = &buf_tx,
67 		.count = 1
68 	};
69 	const struct spi_buf buf_rx = {
70 		.buf = miso,
71 		.len = miso ? size : 0
72 	};
73 	const struct spi_buf_set rx = {
74 		.buffers = &buf_rx,
75 		.count = 1
76 	};
77 
78 	if (spi_transceive_dt(&winc1500_config.spi, &tx, &rx)) {
79 		LOG_ERR("spi_transceive fail");
80 		return M2M_ERR_BUS_FAIL;
81 	}
82 
83 	return M2M_SUCCESS;
84 }
85 
86 #endif
87 
nm_bus_init(void * pvinit)88 int8_t nm_bus_init(void *pvinit)
89 {
90 	/* configure GPIOs */
91 	if (!gpio_is_ready_dt(&winc1500_config.chip_en_gpio)) {
92 		return -ENODEV;
93 	}
94 	gpio_pin_configure_dt(&winc1500_config.chip_en_gpio, GPIO_OUTPUT_LOW);
95 
96 
97 	if (!gpio_is_ready_dt(&winc1500_config.irq_gpio)) {
98 		return -ENODEV;
99 	}
100 	gpio_pin_configure_dt(&winc1500_config.irq_gpio, GPIO_INPUT);
101 
102 	if (!gpio_is_ready_dt(&winc1500_config.reset_gpio)) {
103 		return -ENODEV;
104 	}
105 	gpio_pin_configure_dt(&winc1500_config.reset_gpio, GPIO_OUTPUT_LOW);
106 
107 
108 #ifdef CONF_WINC_USE_I2C
109 	/* Not implemented */
110 #elif defined CONF_WINC_USE_SPI
111 	/* setup SPI device */
112 	if (!spi_is_ready_dt(&winc1500_config.spi)) {
113 		LOG_ERR("spi device binding");
114 		return -ENODEV;
115 	}
116 
117 	nm_bsp_reset();
118 	nm_bsp_sleep(1);
119 
120 	nm_bsp_interrupt_ctrl(1);
121 
122 	LOG_DBG("NOTICE:DONE");
123 #endif
124 	return 0;
125 }
126 
nm_bus_ioctl(uint8_t cmd,void * parameter)127 int8_t nm_bus_ioctl(uint8_t cmd, void *parameter)
128 {
129 	sint8 ret = 0;
130 
131 	switch (cmd) {
132 #ifdef CONF_WINC_USE_I2C
133 	case NM_BUS_IOCTL_R: {
134 		struct nm_i2c_default *param =
135 			(struct nm_i2c_default *)parameter;
136 
137 		ret = nm_i2c_read(param->buffer, param->size);
138 	}
139 	break;
140 	case NM_BUS_IOCTL_W: {
141 		struct nm_i2c_default *param =
142 			(struct nm_i2c_default *)parameter;
143 
144 		ret = nm_i2c_write(param->buffer, param->size);
145 	}
146 	break;
147 	case NM_BUS_IOCTL_W_SPECIAL: {
148 		struct nm_i2c_special *param =
149 			(struct nm_i2c_special *)parameter;
150 
151 		ret = nm_i2c_write_special(param->buffer1, param->size1,
152 					   param->buffer2, param->size2);
153 	}
154 	break;
155 #elif defined CONF_WINC_USE_SPI
156 	case NM_BUS_IOCTL_RW: {
157 		tstrNmSpiRw *param = (tstrNmSpiRw *)parameter;
158 
159 		ret = spi_rw(param->pu8InBuf, param->pu8OutBuf, param->u16Sz);
160 	}
161 	break;
162 #endif
163 	default:
164 		ret = -1;
165 		M2M_ERR("ERROR:invalid ioclt cmd\n");
166 		break;
167 	}
168 
169 	return ret;
170 }
171 
nm_bus_deinit(void)172 int8_t nm_bus_deinit(void)
173 {
174 	return M2M_SUCCESS;
175 }
176 
nm_bus_reinit(void * config)177 int8_t nm_bus_reinit(void *config)
178 {
179 	return 0;
180 }
181