Advertisement
Guest User

Untitled

a guest
Nov 14th, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.06 KB | None | 0 0
  1. #ifndef __icsp_h_included__
  2. #define __icsp_h_included__
  3.  
  4. #ifndef ESP8266
  5. #include <util/delay.h>
  6. #include <avr/io.h>
  7. #else
  8. //#include "Arduino.h"
  9. #define _delay_ms(a) delay(a)
  10. #endif
  11. #include <stdio.h>
  12.  
  13. #if defined(ESP8266)
  14.   #include "SPI.h"
  15.   #define ICSP_SS                 16
  16.   #define ICSP_MOSI               13
  17.   #define ICSP_MISO               12
  18.   #define ICSP_SCK                14
  19.   #define ICSP_SPI_SPEED_SLOW     125000
  20.   #define ICSP_SPI_SPEED_FAST     2000000
  21.   #define ICSP_SPI_SPEED(d)       SPI.setFrequency(d);
  22.   #define ICSP_SPI_START()        SPI.begin(); ICSP_SPI_SPEED(ICSP_SPI_SPEED_FAST);
  23.   #define ICSP_SPI_STOP()         SPI.end();
  24.   #define ICSP_SPI_TRANSFER(b)    b = SPI.transfer(b);
  25.   #define ICSP_RESET_START()      pinMode(ICSP_SS, OUTPUT)
  26.   #define ICSP_RESET_STOP()       pinMode(ICSP_SS, INPUT_PULLUP)
  27.   #define ICSP_RESET_HIGH()       digitalWrite(ICSP_SS, HIGH)
  28.   #define ICSP_RESET_LOW()        digitalWrite(ICSP_SS, LOW)
  29. #else //ATmegaX8
  30.   #define ICSP_PIN                PINB
  31.   #define ICSP_PORT               PORTB
  32.   #define ICSP_DDR                DDRB
  33.   #define ICSP_SS                 0x04
  34.   #define ICSP_MOSI               0x08
  35.   #define ICSP_MISO               0x10
  36.   #define ICSP_SCK                0x20
  37.   #define ICSP_SPI_SPEED_SLOW     0x03  // 125KHz@16MHz
  38.   #define ICSP_SPI_SPEED_FAST     0x11  // 2MHz@16MHz
  39.   #define ICSP_SPI_SPEED(d)       SPCR = (SPCR & ~3) | ((d) & 3); if((d) & 0x10) SPSR |= 1; else SPSR &= ~1;
  40.   #define ICSP_SPI_START()        ICSP_DDR |= (ICSP_SCK | ICSP_MOSI); ICSP_DDR &= ~ICSP_MISO; SPCR = (1 << MSTR) | (1 << SPE); ICSP_SPI_SPEED(ICSP_SPI_SPEED_FAST);
  41.   #define ICSP_SPI_STOP()         SPCR = 0; SPSR = 0; ICSP_DDR &= ~(ICSP_MOSI | ICSP_SCK);
  42.   #define ICSP_SPI_TRANSFER(b)    SPDR = b; while (!(SPSR & (1 << SPIF))); b = SPDR;
  43.   #define ICSP_RESET_START()      ICSP_DDR |= ICSP_SS;
  44.   #define ICSP_RESET_STOP()       ICSP_DDR &= ~ICSP_SS
  45.   #define ICSP_RESET_HIGH()       ICSP_PORT |= ICSP_SS;
  46.   #define ICSP_RESET_LOW()        ICSP_PORT &= ~ICSP_SS;
  47. #endif
  48.  
  49. #define ICSP_SEND_FAST(a,b,c,d) (__icsp_transaction(a,b,c,d))
  50. #define ICSP_SEND_SLOW(a,b,c,d) (__icsp_slow_transaction(a,b,c,d))
  51. #define ICSP_SEND_BUSY(a,b,c,d) (__icsp_busy_transaction(a,b,c,d))
  52.  
  53. #define ICSP_BUSY()             (ICSP_SEND_FAST(0xF0, 0x00, 0x00, 0x00) & 0x01)
  54. #define ICSP_GET_SIG0()         (ICSP_SEND_FAST(0x30, 0x00, 0x00, 0x00))
  55. #define ICSP_GET_SIG1()         (ICSP_SEND_FAST(0x30, 0x00, 0x01, 0x00))
  56. #define ICSP_GET_SIG2()         (ICSP_SEND_FAST(0x30, 0x00, 0x02, 0x00))
  57. #define ICSP_GET_FUSE_LOCK()    (ICSP_SEND_FAST(0x58, 0x00, 0x00, 0x00))
  58. #define ICSP_GET_FUSE_LOW()     (ICSP_SEND_FAST(0x50, 0x00, 0x00, 0x00))
  59. #define ICSP_GET_FUSE_HIGH()    (ICSP_SEND_FAST(0x58, 0x08, 0x00, 0x00))
  60. #define ICSP_GET_FUSE_EXT()     (ICSP_SEND_FAST(0x50, 0x08, 0x00, 0x00))
  61.  
  62. #define ICSP_SET_FUSE_LOCK(f)   (ICSP_SEND_SLOW(0xAC, 0xE0, 0x00, (f)))
  63. #define ICSP_SET_FUSE_LOW(f)    (ICSP_SEND_SLOW(0xAC, 0xA0, 0x00, (f)))
  64. #define ICSP_SET_FUSE_HIGH(f)   (ICSP_SEND_SLOW(0xAC, 0xA8, 0x00, (f)))
  65. #define ICSP_SET_FUSE_EXT(f)    (ICSP_SEND_SLOW(0xAC, 0xA4, 0x00, (f)))
  66. #define ICSP_SET_LOCK_NONE()    (ICSP_SEND_SLOW(0xAC, 0xFF, 0x00, 0x00))
  67. #define ICSP_SET_LOCK_WRITE()   (ICSP_SEND_SLOW(0xAC, 0xFD, 0x00, 0x00))
  68. #define ICSP_SET_LOCK_BOTH()    (ICSP_SEND_SLOW(0xAC, 0xFC, 0x00, 0x00))
  69.  
  70. #define ICSP_READ_LOW(a)        (ICSP_SEND_FAST(0x20, ((a) >> 8) & 0xFF, (a) & 0xFF, 0))
  71. #define ICSP_READ_HIGH(a)       (ICSP_SEND_FAST(0x28, ((a) >> 8) & 0xFF, (a) & 0xFF, 0))
  72. #define ICSP_WRITE_LOW(a,v)     (ICSP_SEND_FAST(0x40, ((a) >> 8) & 0xFF, (a) & 0xFF, (v)))
  73. #define ICSP_WRITE_HIGH(a,v)    (ICSP_SEND_FAST(0x48, ((a) >> 8) & 0xFF, (a) & 0xFF, (v)))
  74. #define ICSP_READ_EEPROM(a)     (ICSP_SEND_FAST(0xA0, ((a) >> 8) & 0xFF, (a) & 0xFF, 0xFF))
  75. #define ICSP_WRITE_EEPROM(a,v)  (ICSP_SEND_BUSY(0xC0, ((a) >> 8) & 0xFF, (a) & 0xFF, (v)))
  76.  
  77. #define ICSP_START()            (ICSP_SEND_BUSY(0xAC, 0x53, 0x00, 0x00))
  78. #define ICSP_ERASE()            (ICSP_SEND_BUSY(0xAC, 0x80, 0x00, 0x00))
  79. #define ICSP_COMMIT_PAGE(a)     (ICSP_SEND_BUSY(0x4C, ((a) >> 8) & 0xFF, (a) & 0xFF, 0))
  80. #define ICSP_SET_EXTENDED(a)    (ICSP_SEND_BUSY(0x4D, 0x00, (a), 0x00))
  81.  
  82. #define ICSP_ENTER_PROG()       ICSP_RESET_HIGH(); ICSP_RESET_START(); ICSP_SPI_START(); ICSP_RESET_HIGH(); _delay_ms(70); ICSP_RESET_LOW(); _delay_ms(30); ICSP_START();
  83. #define ICSP_LEAVE_PROG()       ICSP_SPI_STOP(); ICSP_RESET_HIGH(); ICSP_RESET_STOP();
  84.  
  85. static inline uint8_t __icsp_transaction(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
  86.   ICSP_SPI_TRANSFER(a);
  87.   ICSP_SPI_TRANSFER(b);
  88.   ICSP_SPI_TRANSFER(c);
  89.   ICSP_SPI_TRANSFER(d);
  90.   return d;
  91. }
  92.  
  93. static inline uint8_t __icsp_busy_transaction(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
  94.   uint8_t result = ICSP_SEND_FAST(a,b,c,d);
  95.   while(ICSP_BUSY()) _delay_ms(0);
  96.   return result;
  97. }
  98.  
  99. static inline uint8_t __icsp_slow_transaction(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
  100.   ICSP_SPI_SPEED(ICSP_SPI_SPEED_SLOW);
  101.   uint8_t result = ICSP_SEND_BUSY(a,b,c,d);
  102.   ICSP_SPI_SPEED(ICSP_SPI_SPEED_FAST);
  103.   return result;
  104. }
  105.  
  106. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement