| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#include "platform_drivers.h" |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
int32_t ad_spi_init(spi_device *dev) |
| 52 |
{ |
| 53 |
|
| 54 |
#ifdef ZYNQ_PS7 |
| 55 |
dev->base_address = XPAR_PS7_SPI_0_BASEADDR; |
| 56 |
dev->device_id = XPAR_PS7_SPI_0_DEVICE_ID; |
| 57 |
#endif |
| 58 |
|
| 59 |
#ifdef ZYNQ_PSU |
| 60 |
dev->base_address = XPAR_PSU_SPI_0_BASEADDR; |
| 61 |
dev->device_id = XPAR_PSU_SPI_0_DEVICE_ID; |
| 62 |
#endif |
| 63 |
|
| 64 |
#ifdef MICROBLAZE |
| 65 |
#endif |
| 66 |
|
| 67 |
#ifdef NIOS_II |
| 68 |
#endif |
| 69 |
|
| 70 |
dev->chip_select = 0xff; |
| 71 |
dev->cpha = 0; |
| 72 |
dev->cpol = 0; |
| 73 |
|
| 74 |
return(0); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
int32_t ad_spi_xfer(spi_device *dev, uint8_t *data, uint8_t no_of_bytes) |
| 82 |
{ |
| 83 |
|
| 84 |
#ifdef ZYNQ |
| 85 |
|
| 86 |
uint32_t initss; |
| 87 |
|
| 88 |
XSpiPs m_spi; |
| 89 |
XSpiPs_Config *m_spi_config; |
| 90 |
|
| 91 |
m_spi_config = XSpiPs_LookupConfig(dev->device_id); |
| 92 |
|
| 93 |
if (m_spi_config == NULL) { |
| 94 |
return(-1); |
| 95 |
} |
| 96 |
|
| 97 |
if (XSpiPs_CfgInitialize(&m_spi, m_spi_config, m_spi_config->BaseAddress) != 0) { |
| 98 |
return(-1); |
| 99 |
} |
| 100 |
|
| 101 |
initss = XSpiPs_ReadReg(dev->base_address, XSPIPS_CR_OFFSET); |
| 102 |
initss = initss & (uint32_t)(~XSPIPS_CR_SSCTRL_MASK); |
| 103 |
initss = initss | (0x7 << XSPIPS_CR_SSCTRL_SHIFT); |
| 104 |
XSpiPs_WriteReg(dev->base_address, XSPIPS_CR_OFFSET, initss); |
| 105 |
XSpiPs_SetOptions(&m_spi, XSPIPS_MASTER_OPTION | |
| 106 |
XSPIPS_DECODE_SSELECT_OPTION | XSPIPS_FORCE_SSELECT_OPTION | |
| 107 |
((dev->cpol == 1) ? XSPIPS_CLK_ACTIVE_LOW_OPTION : 0) | |
| 108 |
((dev->cpha == 1) ? XSPIPS_CLK_PHASE_1_OPTION : 0)); |
| 109 |
XSpiPs_SetSlaveSelect(&m_spi, (uint8_t) 0x7); |
| 110 |
XSpiPs_SetClkPrescaler(&m_spi, XSPIPS_CLK_PRESCALE_64); |
| 111 |
XSpiPs_SetSlaveSelect(&m_spi, (uint8_t) dev->chip_select); |
| 112 |
XSpiPs_PolledTransfer(&m_spi, data, data, no_of_bytes); |
| 113 |
XSpiPs_SetSlaveSelect(&m_spi, (uint8_t) 0x7); |
| 114 |
|
| 115 |
#endif |
| 116 |
|
| 117 |
#ifdef NIOS_II |
| 118 |
|
| 119 |
uint32_t i; |
| 120 |
|
| 121 |
IOWR_32DIRECT(SYS_SPI_BASE, 0x0c, 0x400); |
| 122 |
IOWR_32DIRECT(SYS_SPI_BASE, 0x14, ~(dev->chip_select)); |
| 123 |
for (i = 0; i < no_of_bytes; i++) { |
| 124 |
while ((IORD_32DIRECT(SYS_SPI_BASE, 0x08) & 0x40) == 0x00) {} |
| 125 |
IOWR_32DIRECT(SYS_SPI_BASE, 0x04, *(data + i)); |
| 126 |
while ((IORD_32DIRECT(SYS_SPI_BASE, 0x08) & 0x80) == 0x00) {} |
| 127 |
*(data + i) = IORD_32DIRECT(SYS_SPI_BASE, 0x00); |
| 128 |
} |
| 129 |
IOWR_32DIRECT(SYS_SPI_BASE, 0x14, 0x000); |
| 130 |
IOWR_32DIRECT(SYS_SPI_BASE, 0x0c, 0x000); |
| 131 |
|
| 132 |
#endif |
| 133 |
|
| 134 |
#ifdef MICROBLAZE |
| 135 |
|
| 136 |
uint32_t i; |
| 137 |
|
| 138 |
Xil_Out32((XPAR_SPI_0_BASEADDR + 0x70), dev->chip_select); |
| 139 |
Xil_Out32((XPAR_SPI_0_BASEADDR + 0x60), (0x086 | (dev->cpol<<3) | (dev->cpha<<4))); |
| 140 |
for (i = 0; i < no_of_bytes; i++) { |
| 141 |
Xil_Out32((XPAR_SPI_0_BASEADDR + 0x68), *(data + i)); |
| 142 |
while ((Xil_In32(XPAR_SPI_0_BASEADDR + 0x64) & 0x1) == 0x1) {} |
| 143 |
*(data + i) = Xil_In32(XPAR_SPI_0_BASEADDR + 0x6c) & 0xff; |
| 144 |
} |
| 145 |
Xil_Out32((XPAR_SPI_0_BASEADDR + 0x70), 0xff); |
| 146 |
Xil_Out32((XPAR_SPI_0_BASEADDR + 0x60), (0x186 | (dev->cpol<<3) | (dev->cpha<<4))); |
| 147 |
|
| 148 |
#endif |
| 149 |
|
| 150 |
return(0); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
int32_t ad_gpio_set(uint8_t pin, uint8_t data) |
| 158 |
{ |
| 159 |
|
| 160 |
int32_t pstatus; |
| 161 |
uint32_t ppos; |
| 162 |
uint32_t pdata; |
| 163 |
uint32_t pmask; |
| 164 |
|
| 165 |
if (pin < 32) { |
| 166 |
return(-1); |
| 167 |
} |
| 168 |
|
| 169 |
pstatus = -1; |
| 170 |
ppos = pin - 32; |
| 171 |
pmask = 0x1 << ppos; |
| 172 |
|
| 173 |
#ifdef ZYNQ_PS7 |
| 174 |
|
| 175 |
pdata = Xil_In32(XPAR_PS7_GPIO_0_BASEADDR + 0x02c4); |
| 176 |
Xil_Out32((XPAR_PS7_GPIO_0_BASEADDR + 0x02c4), (pdata | pmask)); |
| 177 |
pdata = Xil_In32(XPAR_PS7_GPIO_0_BASEADDR + 0x02c8); |
| 178 |
Xil_Out32((XPAR_PS7_GPIO_0_BASEADDR + 0x02c8), (pdata | pmask)); |
| 179 |
pdata = Xil_In32(XPAR_PS7_GPIO_0_BASEADDR + 0x004c); |
| 180 |
Xil_Out32((XPAR_PS7_GPIO_0_BASEADDR + 0x004c), ((pdata & ~pmask) | (data << ppos))); |
| 181 |
pstatus = 0; |
| 182 |
|
| 183 |
#endif |
| 184 |
|
| 185 |
#ifdef ZYNQ_PSU |
| 186 |
|
| 187 |
pdata = Xil_In32(XPAR_PSU_GPIO_0_BASEADDR + 0x0304); |
| 188 |
Xil_Out32((XPAR_PSU_GPIO_0_BASEADDR + 0x0304), (pdata | pmask)); |
| 189 |
pdata = Xil_In32(XPAR_PSU_GPIO_0_BASEADDR + 0x0308); |
| 190 |
Xil_Out32((XPAR_PSU_GPIO_0_BASEADDR + 0x0308), (pdata | pmask)); |
| 191 |
pdata = Xil_In32(XPAR_PSU_GPIO_0_BASEADDR + 0x0050); |
| 192 |
Xil_Out32((XPAR_PSU_GPIO_0_BASEADDR + 0x0050), ((pdata & ~pmask) | (data << ppos))); |
| 193 |
pstatus = 0; |
| 194 |
|
| 195 |
#endif |
| 196 |
|
| 197 |
#ifdef NIOS_II |
| 198 |
|
| 199 |
pdata = IORD_32DIRECT(SYS_GPIO_OUT_BASE, 0x0); |
| 200 |
IOWR_32DIRECT(SYS_GPIO_OUT_BASE, 0x0, ((pdata & ~pmask) | (data << ppos))); |
| 201 |
pstatus = 0; |
| 202 |
|
| 203 |
#endif |
| 204 |
|
| 205 |
#ifdef MICROBLAZE |
| 206 |
|
| 207 |
pdata = Xil_In32(XPAR_AXI_GPIO_BASEADDR + 0xc); |
| 208 |
Xil_Out32((XPAR_AXI_GPIO_BASEADDR + 0xc), (pdata & ~pmask)); |
| 209 |
pdata = Xil_In32(XPAR_AXI_GPIO_BASEADDR + 0x8); |
| 210 |
Xil_Out32((XPAR_AXI_GPIO_BASEADDR + 0x8), ((pdata & ~pmask) | (data << ppos))); |
| 211 |
pstatus = 0; |
| 212 |
|
| 213 |
#endif |
| 214 |
|
| 215 |
return(pstatus); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
int32_t ad_gpio_get(uint8_t pin, uint8_t *data) |
| 223 |
{ |
| 224 |
|
| 225 |
int32_t pstatus; |
| 226 |
uint32_t ppos; |
| 227 |
uint32_t pdata; |
| 228 |
#ifdef ZYNQ |
| 229 |
uint32_t pmask; |
| 230 |
#endif |
| 231 |
|
| 232 |
if (pin < 32) { |
| 233 |
return(-1); |
| 234 |
} |
| 235 |
|
| 236 |
pstatus = -1; |
| 237 |
ppos = pin - 32; |
| 238 |
#ifdef ZYNQ |
| 239 |
pmask = 0x1 << ppos; |
| 240 |
#endif |
| 241 |
|
| 242 |
#ifdef ZYNQ_PS7 |
| 243 |
|
| 244 |
pdata = Xil_In32(XPAR_PS7_GPIO_0_BASEADDR + 0x02c4); |
| 245 |
Xil_Out32((XPAR_PS7_GPIO_0_BASEADDR + 0x02c4), (pdata & ~pmask)); |
| 246 |
pdata = Xil_In32(XPAR_PS7_GPIO_0_BASEADDR + 0x004c); |
| 247 |
*data = (pdata >> ppos) & 0x1; |
| 248 |
pstatus = 0; |
| 249 |
|
| 250 |
#endif |
| 251 |
|
| 252 |
#ifdef ZYNQ_PSU |
| 253 |
|
| 254 |
pdata = Xil_In32(XPAR_PSU_GPIO_0_BASEADDR + 0x0304); |
| 255 |
Xil_Out32((XPAR_PSU_GPIO_0_BASEADDR + 0x0304), (pdata & ~pmask)); |
| 256 |
pdata = Xil_In32(XPAR_PSU_GPIO_0_BASEADDR + 0x0050); |
| 257 |
*data = (pdata >> ppos) & 0x1; |
| 258 |
pstatus = 0; |
| 259 |
|
| 260 |
#endif |
| 261 |
|
| 262 |
#ifdef NIOS_II |
| 263 |
|
| 264 |
pdata = IORD_32DIRECT(SYS_GPIO_OUT_BASE, 0x0); |
| 265 |
*data = (pdata >> ppos) & 0x1; |
| 266 |
pstatus = 0; |
| 267 |
|
| 268 |
#endif |
| 269 |
|
| 270 |
#ifdef MICROBLAZE |
| 271 |
|
| 272 |
pdata = Xil_In32(XPAR_AXI_GPIO_BASEADDR + 0x8); |
| 273 |
*data = (pdata >> ppos) & 0x1; |
| 274 |
pstatus = 0; |
| 275 |
|
| 276 |
#endif |
| 277 |
|
| 278 |
return(pstatus); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
uint64_t do_div(uint64_t* n, uint64_t base) |
| 285 |
{ |
| 286 |
uint64_t mod = 0; |
| 287 |
|
| 288 |
mod = *n % base; |
| 289 |
*n = *n / base; |
| 290 |
|
| 291 |
return mod; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
void ad_reg_write_16(uint32_t addr, uint32_t data) |
| 298 |
{ |
| 299 |
uint32_t m_data; |
| 300 |
|
| 301 |
m_data = ad_reg_read(addr & ~0x3); |
| 302 |
if ((addr & 0x3) == 0) |
| 303 |
m_data = (m_data & ~0xffff) | data; |
| 304 |
else |
| 305 |
m_data = (m_data & 0xffff) | (data<<16); |
| 306 |
ad_reg_write((addr & ~0x3), m_data); |
| 307 |
} |
| 308 |
|
| 309 |
#ifdef ALTERA |
| 310 |
void ad_platform_init(void) |
| 311 |
{ |
| 312 |
return; |
| 313 |
} |
| 314 |
|
| 315 |
void ad_platform_close(void) |
| 316 |
{ |
| 317 |
alt_printf("%c", 0x04); |
| 318 |
return; |
| 319 |
} |
| 320 |
#endif |
| 321 |
|
| 322 |
#ifdef MICROBLAZE |
| 323 |
void usleep(uint32_t us_count) |
| 324 |
{ |
| 325 |
uint32_t count; |
| 326 |
|
| 327 |
ad_reg_write((XPAR_AXI_TIMER_BASEADDR + 0x4), 0x00); |
| 328 |
for (count = 0; count < us_count; count++) { |
| 329 |
ad_reg_write((XPAR_AXI_TIMER_BASEADDR + 0x0), 0x20); |
| 330 |
ad_reg_write((XPAR_AXI_TIMER_BASEADDR + 0x0), 0x80); |
| 331 |
while (ad_reg_read(XPAR_AXI_TIMER_BASEADDR + 0x8) < 100) {} |
| 332 |
} |
| 333 |
ad_reg_write((XPAR_AXI_TIMER_BASEADDR + 0x0), 0x20); |
| 334 |
} |
| 335 |
#endif |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
#ifdef ZYNQ_PS7 |
| 342 |
uint8_t ad_uart_read() |
| 343 |
{ |
| 344 |
u32 RecievedByte; |
| 345 |
int32_t timeout = 100000000; |
| 346 |
|
| 347 |
while (!XUartPs_IsReceiveData(STDIN_BASEADDRESS)&&timeout>0) { |
| 348 |
timeout--; |
| 349 |
} |
| 350 |
RecievedByte = XUartPs_ReadReg(STDIN_BASEADDRESS, XUARTPS_FIFO_OFFSET); |
| 351 |
|
| 352 |
return (uint8_t)RecievedByte; |
| 353 |
} |
| 354 |
#endif |