Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

main.c 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Copyright (c) 2011,2012 Simon Schubert <[email protected]>.
  2. * Modifications by Jacob Alexander 2014 <[email protected]>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. // ----- Local Includes -----
  18. #include "mchck.h"
  19. #include "dfu.desc.h"
  20. // ----- Variables -----
  21. /**
  22. * Unfortunately we can't DMA directly to FlexRAM, so we'll have to stage here.
  23. */
  24. static char staging[FLASH_SECTOR_SIZE];
  25. // ----- Functions -----
  26. static enum dfu_status setup_write(size_t off, size_t len, void **buf)
  27. {
  28. static int last = 0;
  29. if (len > sizeof(staging))
  30. return (DFU_STATUS_errADDRESS);
  31. // We only allow the last write to be less than one sector size.
  32. if (off == 0)
  33. last = 0;
  34. if (last && len != 0)
  35. return (DFU_STATUS_errADDRESS);
  36. if (len != FLASH_SECTOR_SIZE) {
  37. last = 1;
  38. memset(staging, 0xff, sizeof(staging));
  39. }
  40. *buf = staging;
  41. return (DFU_STATUS_OK);
  42. }
  43. static enum dfu_status finish_write( void *buf, size_t off, size_t len )
  44. {
  45. void *target;
  46. if (len == 0)
  47. return (DFU_STATUS_OK);
  48. target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE);
  49. if (!target)
  50. return (DFU_STATUS_errADDRESS);
  51. memcpy(target, buf, len);
  52. if (flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) != 0)
  53. return (DFU_STATUS_errADDRESS);
  54. return (DFU_STATUS_OK);
  55. }
  56. static struct dfu_ctx dfu_ctx;
  57. void init_usb_bootloader( int config )
  58. {
  59. dfu_init(setup_write, finish_write, &dfu_ctx);
  60. }
  61. void main()
  62. {
  63. // Enabling LED to indicate we are in the bootloader
  64. GPIOA_PDDR |= (1<<19);
  65. // Setup pin - A19 - See Lib/pin_map.mchck for more details on pins
  66. PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  67. GPIOA_PSOR |= (1<<19);
  68. flash_prepare_flashing();
  69. usb_init( &dfu_device );
  70. for (;;)
  71. {
  72. usb_poll();
  73. }
  74. }