spi.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <comdef.h>
  2. #include <spi.h>
  3. #define SPI_CONTROLLER_NUM 1
  4. struct spi_operation *operations[SPI_CONTROLLER_NUM];
  5. int spi_register(unsigned int bus, struct spi_operation *operation)
  6. {
  7. if(bus> SPI_CONTROLLER_NUM-1)
  8. return -1;
  9. operations[bus] = operation;
  10. return 0;
  11. }
  12. int spi_unregister(unsigned int bus)
  13. {
  14. if(bus> SPI_CONTROLLER_NUM-1)
  15. return -1;
  16. operations[bus] = 0;
  17. return 0;
  18. }
  19. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  20. unsigned int max_hz, unsigned int mode, unsigned int bus_width)
  21. {
  22. if(bus> SPI_CONTROLLER_NUM-1)
  23. return NULL;
  24. if(operations[bus]->setup_slave)
  25. {
  26. return operations[bus]->setup_slave(bus,cs,max_hz,mode,bus_width);
  27. }
  28. return NULL;
  29. }
  30. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, void *dout,
  31. void *din, unsigned long flags,int bit_mode)
  32. {
  33. unsigned int bus = slave->bus;
  34. int ret = -1;
  35. if(bus> SPI_CONTROLLER_NUM-1)
  36. return -1;
  37. if(operations[bus]->spi_xfer)
  38. ret = operations[bus]->spi_xfer(slave, bitlen, dout, din, flags, bit_mode);
  39. return ret;
  40. }