usb_bulk.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 "crc.h"
  34. #include "usb_bulk.h"
  35. extern uint8_t read_buffer[TRANSFER_BUFFER_SIZE];
  36. extern uint32_t req_addr;
  37. extern uint32_t req_size;
  38. extern uint8_t req_bank;
  39. extern uint32_t req_bank_size;
  40. extern uint8_t req_state;
  41. extern uint8_t rx_remaining;
  42. extern uint8_t tx_remaining;
  43. extern uint8_t tx_buffer[32];
  44. extern uint16_t crc;
  45. uint8_t usbFunctionWrite(uint8_t * data, uint8_t len)
  46. {
  47. uint8_t *ptr;
  48. uint8_t i;
  49. if (len > rx_remaining) {
  50. printf("ERROR:usbFunctionWrite more data than expected remain: %i len: %i\n",
  51. rx_remaining, len);
  52. len = rx_remaining;
  53. }
  54. if (req_state == REQ_STATUS_UPLOAD) {
  55. rx_remaining -= len;
  56. debug(DEBUG_USB_TRANS,"usbFunctionWrite REQ_STATUS_UPLOAD addr: 0x%08lx len: %i rx_remaining=%i\n",
  57. req_addr, len, rx_remaining);
  58. debug(DEBUG_USB_TRANS,"usbFunctionWrite %02x %02x %02x %02x %02x %02x %02x %x\n",
  59. data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7]);
  60. sram_copy(req_addr, data, len);
  61. req_addr += len;
  62. } else if (req_state == REQ_STATUS_BULK_UPLOAD) {
  63. rx_remaining -= len;
  64. debug(DEBUG_USB_TRANS,"usbFunctionWrite REQ_STATUS_BULK_UPLOAD addr: 0x%08lx len: %i rx_remaining=%i\n",
  65. req_addr, len, rx_remaining);
  66. ptr = data;
  67. i = len;
  68. while(i--){
  69. sram_bulk_write(*ptr++);
  70. sram_bulk_write_next();
  71. }
  72. }
  73. /* test this */
  74. //return rx_remaining == 0
  75. return len;
  76. }
  77. uint8_t usbFunctionRead(uint8_t * data, uint8_t len)
  78. {
  79. uint8_t i;
  80. if (len > tx_remaining)
  81. len = tx_remaining;
  82. tx_remaining -= len;
  83. debug(DEBUG_USB_TRANS,"usbFunctionRead len=%i tx_remaining=%i \n", len, tx_remaining);
  84. for (i = 0; i < len; i++) {
  85. *data = tx_buffer[len];
  86. data++;
  87. }
  88. return len;
  89. }