usb_bulk.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * =====================================================================================
  3. *
  4. * ________ .__ __ ________ ____ ________
  5. * \_____ \ __ __|__| ____ | | __\______ \ _______ _/_ |/ _____/
  6. * / / \ \| | \ |/ ___\| |/ / | | \_/ __ \ \/ /| / __ \
  7. * / \_/. \ | / \ \___| < | ` \ ___/\ / | \ |__\ \
  8. * \_____\ \_/____/|__|\___ >__|_ \/_______ /\___ >\_/ |___|\_____ /
  9. * \__> \/ \/ \/ \/ \/
  10. *
  11. * www.optixx.org
  12. *
  13. *
  14. * Version: 1.0
  15. * Created: 07/21/2009 03:32:16 PM
  16. * Author: david@optixx.org
  17. *
  18. * =====================================================================================
  19. */
  20. #include <avr/io.h>
  21. #include <avr/pgmspace.h> /* required by usbdrv.h */
  22. #include <avr/interrupt.h> /* for sei() */
  23. #include <util/delay.h> /* for _delay_ms() */
  24. #include <stdlib.h>
  25. #include "usbdrv.h"
  26. #include "oddebug.h" /* This is also an example for using debug
  27. * macros */
  28. #include "config.h"
  29. #include "requests.h" /* The custom request numbers we use */
  30. #include "uart.h"
  31. #include "sram.h"
  32. #include "debug.h"
  33. #include "info.h"
  34. #include "crc.h"
  35. #include "usb_bulk.h"
  36. extern uint8_t read_buffer[TRANSFER_BUFFER_SIZE];
  37. extern uint32_t req_addr;
  38. extern uint32_t req_size;
  39. extern uint8_t req_bank;
  40. extern uint32_t req_bank_size;
  41. extern uint8_t req_state;
  42. extern uint8_t rx_remaining;
  43. extern uint8_t tx_remaining;
  44. extern uint8_t tx_buffer[32];
  45. extern uint16_t crc;
  46. uint8_t usbFunctionWrite(uint8_t * data, uint8_t len)
  47. {
  48. uint8_t *ptr;
  49. uint8_t i;
  50. if (len > rx_remaining) {
  51. info("ERROR:usbFunctionWrite more data than expected remain: %i len: %i\n",
  52. rx_remaining, len);
  53. len = rx_remaining;
  54. }
  55. if (req_state == REQ_STATUS_UPLOAD) {
  56. rx_remaining -= len;
  57. debug(DEBUG_USB_TRANS,"usbFunctionWrite REQ_STATUS_UPLOAD addr: 0x%08lx len: %i rx_remaining=%i\n",
  58. req_addr, len, rx_remaining);
  59. debug(DEBUG_USB_TRANS,"usbFunctionWrite %02x %02x %02x %02x %02x %02x %02x %x\n",
  60. data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7]);
  61. sram_copy(req_addr, data, len);
  62. req_addr += len;
  63. } else if (req_state == REQ_STATUS_BULK_UPLOAD) {
  64. rx_remaining -= len;
  65. debug(DEBUG_USB_TRANS,"usbFunctionWrite REQ_STATUS_BULK_UPLOAD addr: 0x%08lx len: %i rx_remaining=%i\n",
  66. req_addr, len, rx_remaining);
  67. ptr = data;
  68. i = len;
  69. while(i--){
  70. sram_bulk_write(*ptr++);
  71. sram_bulk_write_next();
  72. }
  73. }
  74. /* test this */
  75. //return rx_remaining == 0
  76. return len;
  77. }
  78. uint8_t usbFunctionRead(uint8_t * data, uint8_t len)
  79. {
  80. uint8_t i;
  81. if (len > tx_remaining)
  82. len = tx_remaining;
  83. tx_remaining -= len;
  84. debug(DEBUG_USB_TRANS,"usbFunctionRead len=%i tx_remaining=%i \n", len, tx_remaining);
  85. for (i = 0; i < len; i++) {
  86. *data = tx_buffer[len];
  87. data++;
  88. }
  89. return len;
  90. }