C8051F32x firmware infrastructure
Sign in or create your account | Project List | Help
C8051F32x firmware infrastructure Git Source Tree
Root/
| 1 | /* |
| 2 | * f32x/flash.c - Flash programming and reading |
| 3 | * |
| 4 | * Written 2008 by Werner Almesberger |
| 5 | * Copyright 2008 Werner Almesberger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #include <stdint.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <stdio.h> |
| 17 | #include <unistd.h> |
| 18 | |
| 19 | #include "c2.h" |
| 20 | #include "flash.h" |
| 21 | |
| 22 | |
| 23 | /* ----- Helper functions for common flash protocol idioms ----------------- */ |
| 24 | |
| 25 | |
| 26 | static uint8_t _c2_addr_read(void) |
| 27 | { |
| 28 | uint8_t x; |
| 29 | |
| 30 | usleep(1000); |
| 31 | x = c2_addr_read(); |
| 32 | // fprintf(stderr, "got 0x%02x\n", x); |
| 33 | return x; |
| 34 | } |
| 35 | |
| 36 | |
| 37 | static void wait_busy(void) |
| 38 | { |
| 39 | while (_c2_addr_read() & InBusy); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | static void wait_ready(void) |
| 44 | { |
| 45 | while (!(_c2_addr_read() & OutReady)); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | static void fpdat_write(uint8_t value) |
| 50 | { |
| 51 | c2_data_write(value, 1); |
| 52 | wait_busy(); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | static uint8_t fpdat_read(void) |
| 57 | { |
| 58 | wait_ready(); |
| 59 | return c2_data_read(1); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | static void check_status(void) |
| 64 | { |
| 65 | uint8_t status; |
| 66 | |
| 67 | status = fpdat_read(); |
| 68 | if (status != FLASH_STATUS_OK) { |
| 69 | fprintf(stderr, "status 0x%02x\n", status); |
| 70 | exit(1); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /* ----- Block/device-level flash operations ------------------------------- */ |
| 76 | |
| 77 | |
| 78 | void flash_device_erase(void) |
| 79 | { |
| 80 | c2_addr_write(FPDAT); |
| 81 | fpdat_write(FLASH_DEVICE_ERASE); |
| 82 | check_status(); |
| 83 | fpdat_write(FLASH_ERASE_MAGIC1); |
| 84 | fpdat_write(FLASH_ERASE_MAGIC2); |
| 85 | fpdat_write(FLASH_ERASE_MAGIC3); |
| 86 | check_status(); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | void flash_block_write(uint16_t addr, const void *data, size_t size) |
| 91 | { |
| 92 | int i; |
| 93 | |
| 94 | if (!size) |
| 95 | return; |
| 96 | c2_addr_write(FPDAT); |
| 97 | fpdat_write(FLASH_BLOCK_WRITE); |
| 98 | check_status(); |
| 99 | fpdat_write(addr >> 8); |
| 100 | fpdat_write(addr); |
| 101 | fpdat_write(size); |
| 102 | check_status(); |
| 103 | for (i = 0; i != size; i++) |
| 104 | fpdat_write(((uint8_t *) data)[i]); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void flash_block_read(uint16_t addr, void *data, size_t size) |
| 109 | { |
| 110 | int i; |
| 111 | |
| 112 | if (!size) |
| 113 | return; |
| 114 | c2_addr_write(FPDAT); |
| 115 | fpdat_write(FLASH_BLOCK_READ); |
| 116 | check_status(); |
| 117 | fpdat_write(addr >> 8); |
| 118 | fpdat_write(addr); |
| 119 | fpdat_write(size); |
| 120 | check_status(); |
| 121 | for (i = 0; i != size; i++) |
| 122 | ((uint8_t *) data)[i] = fpdat_read(); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | void flash_init(void) |
| 127 | { |
| 128 | c2_addr_write(FPCTL); |
| 129 | c2_data_write(FLASH_INIT_MAGIC1, 1); |
| 130 | c2_data_write(FLASH_INIT_MAGIC2, 1); |
| 131 | usleep(20000); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /* @@@ doesn't really seem to work */ |
| 136 | |
| 137 | uint8_t fp_reg_read(uint8_t addr) |
| 138 | { |
| 139 | c2_addr_write(FPDAT); |
| 140 | fpdat_write(REG_READ); |
| 141 | check_status(); |
| 142 | fpdat_write(addr); |
| 143 | fpdat_write(1); |
| 144 | return fpdat_read(); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | void fp_reg_write(uint8_t addr, uint8_t value) |
| 149 | { |
| 150 | c2_addr_write(FPDAT); |
| 151 | fpdat_write(REG_WRITE); |
| 152 | check_status(); |
| 153 | fpdat_write(addr); |
| 154 | fpdat_write(1); |
| 155 | fpdat_write(value); |
| 156 | } |
| 157 |
Branches:
master
