PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Committer:
spinal
Date:
Sun Nov 18 15:47:54 2018 +0000
Revision:
64:6e6c6c2b664e
Parent:
5:ea7377f3d1af
added fix for directrectangle()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 5:ea7377f3d1af 1 /* mbed Microcontroller Library
Pokitto 5:ea7377f3d1af 2 * Copyright (c) 2006-2013 ARM Limited
Pokitto 5:ea7377f3d1af 3 *
Pokitto 5:ea7377f3d1af 4 * Licensed under the Apache License, Version 2.0 (the "License");
Pokitto 5:ea7377f3d1af 5 * you may not use this file except in compliance with the License.
Pokitto 5:ea7377f3d1af 6 * You may obtain a copy of the License at
Pokitto 5:ea7377f3d1af 7 *
Pokitto 5:ea7377f3d1af 8 * http://www.apache.org/licenses/LICENSE-2.0
Pokitto 5:ea7377f3d1af 9 *
Pokitto 5:ea7377f3d1af 10 * Unless required by applicable law or agreed to in writing, software
Pokitto 5:ea7377f3d1af 11 * distributed under the License is distributed on an "AS IS" BASIS,
Pokitto 5:ea7377f3d1af 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Pokitto 5:ea7377f3d1af 13 * See the License for the specific language governing permissions and
Pokitto 5:ea7377f3d1af 14 * limitations under the License.
Pokitto 5:ea7377f3d1af 15 */
Pokitto 5:ea7377f3d1af 16 #include "cmsis.h"
Pokitto 5:ea7377f3d1af 17 #include "semihost_api.h"
Pokitto 5:ea7377f3d1af 18
Pokitto 5:ea7377f3d1af 19 #include <stdint.h>
Pokitto 5:ea7377f3d1af 20 #include <string.h>
Pokitto 5:ea7377f3d1af 21
Pokitto 5:ea7377f3d1af 22 #if DEVICE_SEMIHOST
Pokitto 5:ea7377f3d1af 23
Pokitto 5:ea7377f3d1af 24 // ARM Semihosting Commands
Pokitto 5:ea7377f3d1af 25 #define SYS_OPEN (0x1)
Pokitto 5:ea7377f3d1af 26 #define SYS_CLOSE (0x2)
Pokitto 5:ea7377f3d1af 27 #define SYS_WRITE (0x5)
Pokitto 5:ea7377f3d1af 28 #define SYS_READ (0x6)
Pokitto 5:ea7377f3d1af 29 #define SYS_ISTTY (0x9)
Pokitto 5:ea7377f3d1af 30 #define SYS_SEEK (0xa)
Pokitto 5:ea7377f3d1af 31 #define SYS_ENSURE (0xb)
Pokitto 5:ea7377f3d1af 32 #define SYS_FLEN (0xc)
Pokitto 5:ea7377f3d1af 33 #define SYS_REMOVE (0xe)
Pokitto 5:ea7377f3d1af 34 #define SYS_RENAME (0xf)
Pokitto 5:ea7377f3d1af 35 #define SYS_EXIT (0x18)
Pokitto 5:ea7377f3d1af 36
Pokitto 5:ea7377f3d1af 37 // mbed Semihosting Commands
Pokitto 5:ea7377f3d1af 38 #define RESERVED_FOR_USER_APPLICATIONS (0x100) // 0x100 - 0x1ff
Pokitto 5:ea7377f3d1af 39 #define USR_XFFIND (RESERVED_FOR_USER_APPLICATIONS + 0)
Pokitto 5:ea7377f3d1af 40 #define USR_UID (RESERVED_FOR_USER_APPLICATIONS + 1)
Pokitto 5:ea7377f3d1af 41 #define USR_RESET (RESERVED_FOR_USER_APPLICATIONS + 2)
Pokitto 5:ea7377f3d1af 42 #define USR_VBUS (RESERVED_FOR_USER_APPLICATIONS + 3)
Pokitto 5:ea7377f3d1af 43 #define USR_POWERDOWN (RESERVED_FOR_USER_APPLICATIONS + 4)
Pokitto 5:ea7377f3d1af 44 #define USR_DISABLEDEBUG (RESERVED_FOR_USER_APPLICATIONS + 5)
Pokitto 5:ea7377f3d1af 45
Pokitto 5:ea7377f3d1af 46 #if DEVICE_LOCALFILESYSTEM
Pokitto 5:ea7377f3d1af 47 FILEHANDLE semihost_open(const char* name, int openmode) {
Pokitto 5:ea7377f3d1af 48 uint32_t args[3];
Pokitto 5:ea7377f3d1af 49 args[0] = (uint32_t)name;
Pokitto 5:ea7377f3d1af 50 args[1] = (uint32_t)openmode;
Pokitto 5:ea7377f3d1af 51 args[2] = (uint32_t)strlen(name);
Pokitto 5:ea7377f3d1af 52 return __semihost(SYS_OPEN, args);
Pokitto 5:ea7377f3d1af 53 }
Pokitto 5:ea7377f3d1af 54
Pokitto 5:ea7377f3d1af 55 int semihost_close(FILEHANDLE fh) {
Pokitto 5:ea7377f3d1af 56 return __semihost(SYS_CLOSE, &fh);
Pokitto 5:ea7377f3d1af 57 }
Pokitto 5:ea7377f3d1af 58
Pokitto 5:ea7377f3d1af 59 int semihost_write(FILEHANDLE fh, const unsigned char* buffer, unsigned int length, int mode) {
Pokitto 5:ea7377f3d1af 60 if (length == 0) return 0;
Pokitto 5:ea7377f3d1af 61
Pokitto 5:ea7377f3d1af 62 uint32_t args[3];
Pokitto 5:ea7377f3d1af 63 args[0] = (uint32_t)fh;
Pokitto 5:ea7377f3d1af 64 args[1] = (uint32_t)buffer;
Pokitto 5:ea7377f3d1af 65 args[2] = (uint32_t)length;
Pokitto 5:ea7377f3d1af 66 return __semihost(SYS_WRITE, args);
Pokitto 5:ea7377f3d1af 67 }
Pokitto 5:ea7377f3d1af 68
Pokitto 5:ea7377f3d1af 69 int semihost_read(FILEHANDLE fh, unsigned char* buffer, unsigned int length, int mode) {
Pokitto 5:ea7377f3d1af 70 uint32_t args[3];
Pokitto 5:ea7377f3d1af 71 args[0] = (uint32_t)fh;
Pokitto 5:ea7377f3d1af 72 args[1] = (uint32_t)buffer;
Pokitto 5:ea7377f3d1af 73 args[2] = (uint32_t)length;
Pokitto 5:ea7377f3d1af 74 return __semihost(SYS_READ, args);
Pokitto 5:ea7377f3d1af 75 }
Pokitto 5:ea7377f3d1af 76
Pokitto 5:ea7377f3d1af 77 int semihost_istty(FILEHANDLE fh) {
Pokitto 5:ea7377f3d1af 78 return __semihost(SYS_ISTTY, &fh);
Pokitto 5:ea7377f3d1af 79 }
Pokitto 5:ea7377f3d1af 80
Pokitto 5:ea7377f3d1af 81 int semihost_seek(FILEHANDLE fh, long position) {
Pokitto 5:ea7377f3d1af 82 uint32_t args[2];
Pokitto 5:ea7377f3d1af 83 args[0] = (uint32_t)fh;
Pokitto 5:ea7377f3d1af 84 args[1] = (uint32_t)position;
Pokitto 5:ea7377f3d1af 85 return __semihost(SYS_SEEK, args);
Pokitto 5:ea7377f3d1af 86 }
Pokitto 5:ea7377f3d1af 87
Pokitto 5:ea7377f3d1af 88 int semihost_ensure(FILEHANDLE fh) {
Pokitto 5:ea7377f3d1af 89 return __semihost(SYS_ENSURE, &fh);
Pokitto 5:ea7377f3d1af 90 }
Pokitto 5:ea7377f3d1af 91
Pokitto 5:ea7377f3d1af 92 long semihost_flen(FILEHANDLE fh) {
Pokitto 5:ea7377f3d1af 93 return __semihost(SYS_FLEN, &fh);
Pokitto 5:ea7377f3d1af 94 }
Pokitto 5:ea7377f3d1af 95
Pokitto 5:ea7377f3d1af 96 int semihost_remove(const char *name) {
Pokitto 5:ea7377f3d1af 97 uint32_t args[2];
Pokitto 5:ea7377f3d1af 98 args[0] = (uint32_t)name;
Pokitto 5:ea7377f3d1af 99 args[1] = (uint32_t)strlen(name);
Pokitto 5:ea7377f3d1af 100 return __semihost(SYS_REMOVE, args);
Pokitto 5:ea7377f3d1af 101 }
Pokitto 5:ea7377f3d1af 102
Pokitto 5:ea7377f3d1af 103 int semihost_rename(const char *old_name, const char *new_name) {
Pokitto 5:ea7377f3d1af 104 uint32_t args[4];
Pokitto 5:ea7377f3d1af 105 args[0] = (uint32_t)old_name;
Pokitto 5:ea7377f3d1af 106 args[1] = (uint32_t)strlen(old_name);
Pokitto 5:ea7377f3d1af 107 args[0] = (uint32_t)new_name;
Pokitto 5:ea7377f3d1af 108 args[1] = (uint32_t)strlen(new_name);
Pokitto 5:ea7377f3d1af 109 return __semihost(SYS_RENAME, args);
Pokitto 5:ea7377f3d1af 110 }
Pokitto 5:ea7377f3d1af 111 #endif
Pokitto 5:ea7377f3d1af 112
Pokitto 5:ea7377f3d1af 113 int semihost_exit(void) {
Pokitto 5:ea7377f3d1af 114 uint32_t args[4];
Pokitto 5:ea7377f3d1af 115 return __semihost(SYS_EXIT, args);
Pokitto 5:ea7377f3d1af 116 }
Pokitto 5:ea7377f3d1af 117
Pokitto 5:ea7377f3d1af 118 int semihost_uid(char *uid) {
Pokitto 5:ea7377f3d1af 119 uint32_t args[2];
Pokitto 5:ea7377f3d1af 120 args[0] = (uint32_t)uid;
Pokitto 5:ea7377f3d1af 121 args[1] = DEVICE_ID_LENGTH + 1;
Pokitto 5:ea7377f3d1af 122 return __semihost(USR_UID, &args);
Pokitto 5:ea7377f3d1af 123 }
Pokitto 5:ea7377f3d1af 124
Pokitto 5:ea7377f3d1af 125 int semihost_reset(void) {
Pokitto 5:ea7377f3d1af 126 // Does not normally return, however if used with older firmware versions
Pokitto 5:ea7377f3d1af 127 // that do not support this call it will return -1.
Pokitto 5:ea7377f3d1af 128 return __semihost(USR_RESET, NULL);
Pokitto 5:ea7377f3d1af 129 }
Pokitto 5:ea7377f3d1af 130
Pokitto 5:ea7377f3d1af 131 int semihost_vbus(void) {
Pokitto 5:ea7377f3d1af 132 return __semihost(USR_VBUS, NULL);
Pokitto 5:ea7377f3d1af 133 }
Pokitto 5:ea7377f3d1af 134
Pokitto 5:ea7377f3d1af 135 int semihost_powerdown(void) {
Pokitto 5:ea7377f3d1af 136 return __semihost(USR_POWERDOWN, NULL);
Pokitto 5:ea7377f3d1af 137 }
Pokitto 5:ea7377f3d1af 138
Pokitto 5:ea7377f3d1af 139 #if DEVICE_DEBUG_AWARENESS
Pokitto 5:ea7377f3d1af 140
Pokitto 5:ea7377f3d1af 141 int semihost_connected(void) {
Pokitto 5:ea7377f3d1af 142 return (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) ? 1 : 0;
Pokitto 5:ea7377f3d1af 143 }
Pokitto 5:ea7377f3d1af 144
Pokitto 5:ea7377f3d1af 145 #else
Pokitto 5:ea7377f3d1af 146 // These processors cannot know if the interface is connect, assume so:
Pokitto 5:ea7377f3d1af 147 static int is_debugger_attached = 1;
Pokitto 5:ea7377f3d1af 148
Pokitto 5:ea7377f3d1af 149 int semihost_connected(void) {
Pokitto 5:ea7377f3d1af 150 return is_debugger_attached;
Pokitto 5:ea7377f3d1af 151 }
Pokitto 5:ea7377f3d1af 152 #endif
Pokitto 5:ea7377f3d1af 153
Pokitto 5:ea7377f3d1af 154 int semihost_disabledebug(void) {
Pokitto 5:ea7377f3d1af 155 #if !(DEVICE_DEBUG_AWARENESS)
Pokitto 5:ea7377f3d1af 156 is_debugger_attached = 0;
Pokitto 5:ea7377f3d1af 157 #endif
Pokitto 5:ea7377f3d1af 158 return __semihost(USR_DISABLEDEBUG, NULL);
Pokitto 5:ea7377f3d1af 159 }
Pokitto 5:ea7377f3d1af 160
Pokitto 5:ea7377f3d1af 161 #endif
Pokitto 5:ea7377f3d1af 162