15 #include "inc/tm4c123gh6pm.h"
16 #include "inc/hw_memmap.h"
17 #include "inc/hw_gpio.h"
18 #include "inc/hw_types.h"
20 #include "driverlib/gpio.h"
21 #include "driverlib/ssi.h"
22 #include "driverlib/sysctl.h"
23 #include "driverlib/pin_map.h"
29 uint32_t peripheral_gpio;
30 uint32_t peripheral_spi;
33 uint32_t pin_clk_function;
34 uint32_t pin_miso_function;
35 uint32_t pin_mosi_function;
69 static SPI_t SPI_spis[3] =
88 #define NUMBER_OF_SPIS ( sizeof(SPI_spis) / sizeof(SPI_t) )
90 #define check_spi_num(x, y) if(x >= NUMBER_OF_SPIS) { return y; }
92 static void SPI_init(
SPI_t* spi)
95 if(!spi->port->initialised)
98 for(uint8_t i=0; i<NUMBER_OF_SPIS; i++)
104 if(!SysCtlPeripheralReady(spi->port->peripheral_spi))
106 SysCtlPeripheralEnable(spi->port->peripheral_spi);
107 while(!SysCtlPeripheralReady(spi->port->peripheral_spi)) { };
111 if(!SysCtlPeripheralReady(spi->port->peripheral_gpio))
113 SysCtlPeripheralEnable(spi->port->peripheral_gpio);
114 while(!SysCtlPeripheralReady(spi->port->peripheral_gpio)) { };
117 if(spi->port->base_gpio == GPIO_PORTF_BASE)
120 HWREG(GPIO_PORTF_BASE+GPIO_O_LOCK) = GPIO_LOCK_KEY;
121 HWREG(GPIO_PORTF_BASE+GPIO_O_CR) |= 0x01;
122 HWREG(GPIO_PORTF_BASE+GPIO_O_LOCK) = 0;
126 GPIOPinConfigure(spi->port->pin_clk_function);
127 GPIOPinConfigure(spi->port->pin_miso_function);
128 GPIOPinConfigure(spi->port->pin_mosi_function);
131 GPIOPinTypeSSI(spi->port->base_gpio, spi->port->pin_clk | spi->port->pin_miso | spi->port->pin_mosi);
132 SSIClockSourceSet(spi->port->base_spi, SSI_CLOCK_SYSTEM);
135 SSIConfigSetExpClk(spi->port->base_spi, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0,
136 SSI_MODE_MASTER, spi->port->clockrate, 8);
138 SSIEnable(spi->port->base_spi);
142 while(SSIDataGetNonBlocking(spi->port->base_spi, &d));
144 spi->port->initialised =
true;
152 check_spi_num(spi_num, 0);
153 SPI_t *spi = &SPI_spis[spi_num];
163 while(SSIDataGetNonBlocking(spi->port->base_spi, &d)) {};
168 while(GPIOPinRead(spi->port->base_gpio, spi->port->pin_miso) & spi->port->pin_miso) {};
172 SSIDataPut(spi->port->base_spi, (uint32_t)cmd);
173 SSIDataGet(spi->port->base_spi, &r);
176 while(SSIBusy(spi->port->base_spi)) {};
185 uint8_t
SPI_read8(uint8_t spi_num, uint8_t addr, uint8_t *data)
187 check_spi_num(spi_num, 0);
188 SPI_t *spi = &SPI_spis[spi_num];
198 while(SSIDataGetNonBlocking(spi->port->base_spi, &d)) {};
203 while(GPIOPinRead(spi->port->base_gpio, spi->port->pin_miso) & spi->port->pin_miso) {};
207 SSIDataPut(spi->port->base_spi, (uint32_t)(addr));
208 SSIDataGet(spi->port->base_spi, &r);
211 SSIDataPut(spi->port->base_spi, 0);
212 SSIDataGet(spi->port->base_spi, &d);
216 while(SSIBusy(spi->port->base_spi)) {};
225 uint8_t
SPI_burstread8(uint8_t spi_num, uint8_t addr, uint8_t *data, uint32_t len)
227 check_spi_num(spi_num, 0);
228 SPI_t *spi = &SPI_spis[spi_num];
238 while(SSIDataGetNonBlocking(spi->port->base_spi, &d)) {};
243 while(GPIOPinRead(spi->port->base_gpio, spi->port->pin_miso) & spi->port->pin_miso) {};
247 SSIDataPut(spi->port->base_spi, (uint32_t)(addr));
248 SSIDataGet(spi->port->base_spi, &r);
252 SSIDataPut(spi->port->base_spi, 0);
253 SSIDataGet(spi->port->base_spi, &d);
254 *data++ = (uint8_t)d;
258 while(SSIBusy(spi->port->base_spi)) {};
267 uint8_t
SPI_write8(uint8_t spi_num, uint8_t addr, uint8_t *data)
269 check_spi_num(spi_num, 0);
270 SPI_t *spi = &SPI_spis[spi_num];
280 while(SSIDataGetNonBlocking(spi->port->base_spi, &d)) {};
285 while(GPIOPinRead(spi->port->base_gpio, spi->port->pin_miso) & spi->port->pin_miso) {};
289 SSIDataPut(spi->port->base_spi, (uint32_t)addr);
290 SSIDataGet(spi->port->base_spi, &r);
296 SSIDataPut(spi->port->base_spi, w);
297 SSIDataGet(spi->port->base_spi, &d);
301 while(SSIBusy(spi->port->base_spi)) {};
312 check_spi_num(spi_num, 0);
313 SPI_t *spi = &SPI_spis[spi_num];
323 while(SSIDataGetNonBlocking(spi->port->base_spi, &d)) {};
328 while(GPIOPinRead(spi->port->base_gpio, spi->port->pin_miso) & spi->port->pin_miso) {};
332 SSIDataPut(spi->port->base_spi, (uint32_t)addr);
333 SSIDataGet(spi->port->base_spi, &r);
339 SSIDataPut(spi->port->base_spi, w);
340 SSIDataGet(spi->port->base_spi, &d);
345 while(SSIBusy(spi->port->base_spi)) {};
354 uint8_t
SPI_read16(uint8_t spi_num, uint16_t addr, uint8_t *data)
356 check_spi_num(spi_num, 0);
357 SPI_t *spi = &SPI_spis[spi_num];
363 uint8_t addr_msb = (uint8_t)(addr >> 8);
364 uint8_t addr_lsb = (uint8_t)(addr & 0x00FF);
370 while(SSIDataGetNonBlocking(spi->port->base_spi, &d)) {};
375 while(GPIOPinRead(spi->port->base_gpio, spi->port->pin_miso) & spi->port->pin_miso) {};
379 SSIDataPut(spi->port->base_spi, (uint32_t)addr_msb);
380 SSIDataGet(spi->port->base_spi, &r);
383 SSIDataPut(spi->port->base_spi, (uint32_t)addr_lsb);
384 SSIDataGet(spi->port->base_spi, &d);
387 SSIDataPut(spi->port->base_spi, 0);
388 SSIDataGet(spi->port->base_spi, &d);
392 while(SSIBusy(spi->port->base_spi)) {};
403 check_spi_num(spi_num, 0);
404 SPI_t *spi = &SPI_spis[spi_num];
410 uint8_t addr_msb = (uint8_t)(addr >> 8);
411 uint8_t addr_lsb = (uint8_t)(addr & 0x00FF);
417 while(SSIDataGetNonBlocking(spi->port->base_spi, &d)) {};
422 while(GPIOPinRead(spi->port->base_gpio, spi->port->pin_miso) & spi->port->pin_miso) {};
426 SSIDataPut(spi->port->base_spi, (uint32_t)addr_msb);
427 SSIDataGet(spi->port->base_spi, &r);
430 SSIDataPut(spi->port->base_spi, (uint32_t)addr_lsb);
431 SSIDataGet(spi->port->base_spi, &d);
435 SSIDataPut(spi->port->base_spi, 0);
436 SSIDataGet(spi->port->base_spi, &d);
437 *data++ = (uint8_t)d;
441 while(SSIBusy(spi->port->base_spi)) {};
450 uint8_t
SPI_write16(uint8_t spi_num, uint16_t addr, uint8_t *data)
452 check_spi_num(spi_num, 0);
453 SPI_t *spi = &SPI_spis[spi_num];
459 uint8_t addr_msb = (uint8_t)(addr >> 8);
460 uint8_t addr_lsb = (uint8_t)(addr & 0x00FF);
466 while(SSIDataGetNonBlocking(spi->port->base_spi, &d)) {};
471 while(GPIOPinRead(spi->port->base_gpio, spi->port->pin_miso) & spi->port->pin_miso) {};
475 SSIDataPut(spi->port->base_spi, (uint32_t)(0x7F & addr_msb));
476 SSIDataGet(spi->port->base_spi, &r);
479 SSIDataPut(spi->port->base_spi, (uint32_t)addr_lsb);
480 SSIDataGet(spi->port->base_spi, &d);
483 SSIDataPut(spi->port->base_spi, w);
484 SSIDataGet(spi->port->base_spi, &d);
487 while(SSIBusy(spi->port->base_spi)) {};
498 check_spi_num(spi_num, 0);
499 SPI_t *spi = &SPI_spis[spi_num];
505 uint8_t addr_msb = (uint8_t)(addr >> 8);
506 uint8_t addr_lsb = (uint8_t)(addr & 0x00FF);
512 while(SSIDataGetNonBlocking(spi->port->base_spi, &d)) {};
517 while(GPIOPinRead(spi->port->base_gpio, spi->port->pin_miso) & spi->port->pin_miso) {};
521 SSIDataPut(spi->port->base_spi, (uint32_t)addr_msb);
522 SSIDataGet(spi->port->base_spi, &r);
525 SSIDataPut(spi->port->base_spi, (uint32_t)addr_lsb);
526 SSIDataGet(spi->port->base_spi, &d);
531 SSIDataPut(spi->port->base_spi, w);
532 SSIDataGet(spi->port->base_spi, &d);
536 while(SSIBusy(spi->port->base_spi)) {};
545 uint8_t
SPI_read32(uint8_t spi_num, uint32_t addr, uint8_t *data)
547 check_spi_num(spi_num, 0);
548 SPI_t *spi = &SPI_spis[spi_num];
554 uint8_t addr_mmsb = (uint8_t)(addr >> 24);
555 uint8_t addr_msb = (uint8_t)(addr >> 16);
556 uint8_t addr_lsb = (uint8_t)(addr >> 8);
557 uint8_t addr_llsb = (uint8_t)(addr & 0xFF);
563 while(SSIDataGetNonBlocking(spi->port->base_spi, &d)) {};
568 while(GPIOPinRead(spi->port->base_gpio, spi->port->pin_miso) & spi->port->pin_miso) {};
572 SSIDataPut(spi->port->base_spi, (uint32_t)addr_mmsb);
573 SSIDataGet(spi->port->base_spi, &r);
576 SSIDataPut(spi->port->base_spi, (uint32_t)addr_msb);
577 SSIDataPut(spi->port->base_spi, (uint32_t)addr_lsb);
578 SSIDataPut(spi->port->base_spi, (uint32_t)addr_llsb);
581 while(SSIBusy(spi->port->base_spi)) {};
584 while(SSIDataGetNonBlocking(spi->port->base_spi, &d)) {};
587 SSIDataPut(spi->port->base_spi, 0);
588 SSIDataGet(spi->port->base_spi, &d);
592 while(SSIBusy(spi->port->base_spi)) {};
603 check_spi_num(spi_num, 0);
604 SPI_t *spi = &SPI_spis[spi_num];
610 uint8_t addr_mmsb = (uint8_t)(addr >> 24);
611 uint8_t addr_msb = (uint8_t)(addr >> 16);
612 uint8_t addr_lsb = (uint8_t)(addr >> 8);
613 uint8_t addr_llsb = (uint8_t)(addr & 0xFF);
619 while(SSIDataGetNonBlocking(spi->port->base_spi, &d)) {};
624 while(GPIOPinRead(spi->port->base_gpio, spi->port->pin_miso) & spi->port->pin_miso) {};
628 SSIDataPut(spi->port->base_spi, (uint32_t)addr_mmsb);
629 SSIDataGet(spi->port->base_spi, &r);
632 SSIDataPut(spi->port->base_spi, (uint32_t)addr_msb);
633 SSIDataPut(spi->port->base_spi, (uint32_t)addr_lsb);
634 SSIDataPut(spi->port->base_spi, (uint32_t)addr_llsb);
637 while(SSIBusy(spi->port->base_spi)) {};
640 while(SSIDataGetNonBlocking(spi->port->base_spi, &d)) {};
644 SSIDataPut(spi->port->base_spi, 0);
645 SSIDataGet(spi->port->base_spi, &d);
646 *data++ = (uint8_t)d;
650 while(SSIBusy(spi->port->base_spi)) {};
659 uint8_t
SPI_write32(uint8_t spi_num, uint32_t addr, uint8_t *data)
661 check_spi_num(spi_num, 0);
662 SPI_t *spi = &SPI_spis[spi_num];
668 uint8_t addr_mmsb = (uint8_t)(addr >> 24);
669 uint8_t addr_msb = (uint8_t)(addr >> 16);
670 uint8_t addr_lsb = (uint8_t)(addr >> 8);
671 uint8_t addr_llsb = (uint8_t)(addr & 0xFF);
677 while(SSIDataGetNonBlocking(spi->port->base_spi, &d)) {};
682 while(GPIOPinRead(spi->port->base_gpio, spi->port->pin_miso) & spi->port->pin_miso) {};
686 SSIDataPut(spi->port->base_spi, (uint32_t)addr_mmsb);
687 SSIDataGet(spi->port->base_spi, &r);
690 SSIDataPut(spi->port->base_spi, (uint32_t)addr_msb);
691 SSIDataPut(spi->port->base_spi, (uint32_t)addr_lsb);
692 SSIDataPut(spi->port->base_spi, (uint32_t)addr_llsb);
695 SSIDataPut(spi->port->base_spi, w);
698 while(SSIBusy(spi->port->base_spi)) {};
709 check_spi_num(spi_num, 0);
710 SPI_t *spi = &SPI_spis[spi_num];
716 uint8_t addr_mmsb = (uint8_t)(addr >> 24);
717 uint8_t addr_msb = (uint8_t)(addr >> 16);
718 uint8_t addr_lsb = (uint8_t)(addr >> 8);
719 uint8_t addr_llsb = (uint8_t)(addr & 0xFF);
725 while(SSIDataGetNonBlocking(spi->port->base_spi, &d)) {};
730 while(GPIOPinRead(spi->port->base_gpio, spi->port->pin_miso) & spi->port->pin_miso) {};
734 SSIDataPut(spi->port->base_spi, (uint32_t)addr_mmsb);
735 SSIDataGet(spi->port->base_spi, &r);
738 SSIDataPut(spi->port->base_spi, (uint32_t)addr_msb);
739 SSIDataPut(spi->port->base_spi, (uint32_t)addr_lsb);
740 SSIDataPut(spi->port->base_spi, (uint32_t)addr_llsb);
746 SSIDataPut(spi->port->base_spi, w);
750 while(SSIBusy(spi->port->base_spi)) {};
uint8_t SPI_write32(uint8_t spi_num, uint32_t addr, uint8_t *data)
uint8_t SPI_burstread8(uint8_t spi_num, uint8_t addr, uint8_t *data, uint32_t len)
uint8_t SPI_burstread32(uint8_t spi_num, uint32_t addr, uint8_t *data, uint32_t len)
uint8_t SPI_burstwrite8(uint8_t spi_num, uint8_t addr, uint8_t *data, uint32_t len)
uint8_t SPI_read32(uint8_t spi_num, uint32_t addr, uint8_t *data)
uint8_t SPI_burstread16(uint8_t spi_num, uint16_t addr, uint8_t *data, uint32_t len)
uint8_t SPI_cmd(uint8_t spi_num, uint8_t cmd)
uint8_t SPI_write8(uint8_t spi_num, uint8_t addr, uint8_t *data)
uint8_t SPI_burstwrite16(uint8_t spi_num, uint16_t addr, uint8_t *data, uint32_t len)
uint8_t SPI_write16(uint8_t spi_num, uint16_t addr, uint8_t *data)
uint8_t SPI_read8(uint8_t spi_num, uint8_t addr, uint8_t *data)
uint8_t SPI_read16(uint8_t spi_num, uint16_t addr, uint8_t *data)
void GPIO_write(uint8_t gpio_num, bool state)
uint8_t SPI_burstwrite32(uint8_t spi_num, uint32_t addr, uint8_t *data, uint32_t len)