Norimasa Okamoto
/
pymite
python-on-a-chip online compiler
- http://pymbed.appspot.com/
- https://code.google.com/p/python-on-a-chip/
- http://www.youtube.com/watch?v=Oyqc2bFRW9I
- https://bitbucket.org/va009039/pymbed/
more info: python-on-a-chip
vm/sli.h@15:94ca5c8003e5, 2016-04-14 (annotated)
- Committer:
- va009039
- Date:
- Thu Apr 14 22:32:57 2016 +0000
- Revision:
- 15:94ca5c8003e5
- Parent:
- 0:65f1469d6bfb
update Nucleo-F401RE.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:65f1469d6bfb | 1 | /* |
va009039 | 0:65f1469d6bfb | 2 | # This file is Copyright 2002 Dean Hall. |
va009039 | 0:65f1469d6bfb | 3 | # This file is part of the PyMite VM. |
va009039 | 0:65f1469d6bfb | 4 | # This file is licensed under the MIT License. |
va009039 | 0:65f1469d6bfb | 5 | # See the LICENSE file for details. |
va009039 | 0:65f1469d6bfb | 6 | */ |
va009039 | 0:65f1469d6bfb | 7 | |
va009039 | 0:65f1469d6bfb | 8 | |
va009039 | 0:65f1469d6bfb | 9 | #ifndef __SLI_H__ |
va009039 | 0:65f1469d6bfb | 10 | #define __SLI_H__ |
va009039 | 0:65f1469d6bfb | 11 | |
va009039 | 0:65f1469d6bfb | 12 | |
va009039 | 0:65f1469d6bfb | 13 | /** |
va009039 | 0:65f1469d6bfb | 14 | * \file |
va009039 | 0:65f1469d6bfb | 15 | * \brief Standard Library Interface |
va009039 | 0:65f1469d6bfb | 16 | * |
va009039 | 0:65f1469d6bfb | 17 | * PyMite requires a few functions from a few different |
va009039 | 0:65f1469d6bfb | 18 | * standard C libraries (memory, string, etc). |
va009039 | 0:65f1469d6bfb | 19 | * If your microcontroller has these libraries, |
va009039 | 0:65f1469d6bfb | 20 | * set the constant to 1 for each library available. |
va009039 | 0:65f1469d6bfb | 21 | * This will cause a macro to be defined which wraps |
va009039 | 0:65f1469d6bfb | 22 | * the function for use by PyMite. |
va009039 | 0:65f1469d6bfb | 23 | * Otherwise, leave the constant as 0, and PyMite will |
va009039 | 0:65f1469d6bfb | 24 | * use the function defined in sli.c |
va009039 | 0:65f1469d6bfb | 25 | * Some of the functions in sli.c will need to be ported |
va009039 | 0:65f1469d6bfb | 26 | * to the target system. |
va009039 | 0:65f1469d6bfb | 27 | */ |
va009039 | 0:65f1469d6bfb | 28 | |
va009039 | 0:65f1469d6bfb | 29 | |
va009039 | 0:65f1469d6bfb | 30 | /** |
va009039 | 0:65f1469d6bfb | 31 | * If the compiler has string.h, set HAVE_STRING to 1; |
va009039 | 0:65f1469d6bfb | 32 | * otherwise, leave it 0 and the sli functions will be used. |
va009039 | 0:65f1469d6bfb | 33 | */ |
va009039 | 0:65f1469d6bfb | 34 | #define HAVE_STRING_H 0 |
va009039 | 0:65f1469d6bfb | 35 | |
va009039 | 0:65f1469d6bfb | 36 | |
va009039 | 0:65f1469d6bfb | 37 | /* |
va009039 | 0:65f1469d6bfb | 38 | * This section creates a macro or a function prototype |
va009039 | 0:65f1469d6bfb | 39 | * for each library based on the corresponding constant. |
va009039 | 0:65f1469d6bfb | 40 | * For example, if HAVE_STRING_H is defined to non-zero, |
va009039 | 0:65f1469d6bfb | 41 | * the system <string.h> file will be included, |
va009039 | 0:65f1469d6bfb | 42 | * and a macro "sli_strcmp" will be created to wrap the strcmp() |
va009039 | 0:65f1469d6bfb | 43 | * function. But if HAVE_STRING is zero, the sli_strcmp() |
va009039 | 0:65f1469d6bfb | 44 | * prototype will be declared and sli_strcmp() must be |
va009039 | 0:65f1469d6bfb | 45 | * implemented in sli.c |
va009039 | 0:65f1469d6bfb | 46 | */ |
va009039 | 0:65f1469d6bfb | 47 | |
va009039 | 0:65f1469d6bfb | 48 | #if HAVE_STRING_H |
va009039 | 0:65f1469d6bfb | 49 | |
va009039 | 0:65f1469d6bfb | 50 | #include <string.h> |
va009039 | 0:65f1469d6bfb | 51 | |
va009039 | 0:65f1469d6bfb | 52 | #define sli_memcpy(to, from, n) memcpy((to), (from), (n)) |
va009039 | 0:65f1469d6bfb | 53 | #define sli_strcmp(s1, s2) strcmp((s1),(s2)) |
va009039 | 0:65f1469d6bfb | 54 | #define sli_strlen(s) strlen(s) |
va009039 | 0:65f1469d6bfb | 55 | #define sli_strncmp(s1, s2, n) strncmp((s1),(s2),(n)) |
va009039 | 0:65f1469d6bfb | 56 | |
va009039 | 0:65f1469d6bfb | 57 | #else |
va009039 | 0:65f1469d6bfb | 58 | |
va009039 | 0:65f1469d6bfb | 59 | /** |
va009039 | 0:65f1469d6bfb | 60 | * Copies a block of memory in RAM. |
va009039 | 0:65f1469d6bfb | 61 | * |
va009039 | 0:65f1469d6bfb | 62 | * @param to The destination address. |
va009039 | 0:65f1469d6bfb | 63 | * @param from The source address. |
va009039 | 0:65f1469d6bfb | 64 | * @param n The number of bytes to copy. |
va009039 | 0:65f1469d6bfb | 65 | * @return The initial pointer value of the destination |
va009039 | 0:65f1469d6bfb | 66 | * @see mem_copy |
va009039 | 0:65f1469d6bfb | 67 | */ |
va009039 | 0:65f1469d6bfb | 68 | void *sli_memcpy(unsigned char *to, unsigned char const *from, unsigned int n); |
va009039 | 0:65f1469d6bfb | 69 | |
va009039 | 0:65f1469d6bfb | 70 | /** |
va009039 | 0:65f1469d6bfb | 71 | * Compares two strings. |
va009039 | 0:65f1469d6bfb | 72 | * |
va009039 | 0:65f1469d6bfb | 73 | * @param s1 Ptr to string 1. |
va009039 | 0:65f1469d6bfb | 74 | * @param s2 Ptr to string 2. |
va009039 | 0:65f1469d6bfb | 75 | * @return value that is less then, equal to or greater than 0 |
va009039 | 0:65f1469d6bfb | 76 | * depending on whether s1's encoding is |
va009039 | 0:65f1469d6bfb | 77 | * less than, equal to, or greater than s2's. |
va009039 | 0:65f1469d6bfb | 78 | */ |
va009039 | 0:65f1469d6bfb | 79 | int sli_strcmp(char const *s1, char const *s2); |
va009039 | 0:65f1469d6bfb | 80 | |
va009039 | 0:65f1469d6bfb | 81 | /** |
va009039 | 0:65f1469d6bfb | 82 | * Obtain string length. |
va009039 | 0:65f1469d6bfb | 83 | * |
va009039 | 0:65f1469d6bfb | 84 | * @param s ptr to string. |
va009039 | 0:65f1469d6bfb | 85 | * @return number of bytes in string. |
va009039 | 0:65f1469d6bfb | 86 | */ |
va009039 | 0:65f1469d6bfb | 87 | int sli_strlen(char const *s); |
va009039 | 0:65f1469d6bfb | 88 | |
va009039 | 0:65f1469d6bfb | 89 | /** |
va009039 | 0:65f1469d6bfb | 90 | * Compare strings for a specific length. |
va009039 | 0:65f1469d6bfb | 91 | * |
va009039 | 0:65f1469d6bfb | 92 | * @param s1 ptr to string 1. |
va009039 | 0:65f1469d6bfb | 93 | * @param s2 ptr to string 2. |
va009039 | 0:65f1469d6bfb | 94 | * @param n number of chars to compare |
va009039 | 0:65f1469d6bfb | 95 | * @return value that is less then, equal to or greater than 0 |
va009039 | 0:65f1469d6bfb | 96 | * depending on whether s1's encoding is |
va009039 | 0:65f1469d6bfb | 97 | * less than, equal to, or greater than s2's. |
va009039 | 0:65f1469d6bfb | 98 | */ |
va009039 | 0:65f1469d6bfb | 99 | int sli_strncmp(char const *s1, char const *s2, unsigned int n); |
va009039 | 0:65f1469d6bfb | 100 | |
va009039 | 0:65f1469d6bfb | 101 | #endif /* HAVE_STRING_H */ |
va009039 | 0:65f1469d6bfb | 102 | |
va009039 | 0:65f1469d6bfb | 103 | /** |
va009039 | 0:65f1469d6bfb | 104 | * Copy a value repeatedly into a block of memory |
va009039 | 0:65f1469d6bfb | 105 | * |
va009039 | 0:65f1469d6bfb | 106 | * @param dest the destination address. |
va009039 | 0:65f1469d6bfb | 107 | * @param val the value. |
va009039 | 0:65f1469d6bfb | 108 | * @param n the number of bytes to copy. |
va009039 | 0:65f1469d6bfb | 109 | * @return Nothing |
va009039 | 0:65f1469d6bfb | 110 | * @see memset |
va009039 | 0:65f1469d6bfb | 111 | */ |
va009039 | 0:65f1469d6bfb | 112 | void sli_memset(unsigned char *dest, const char val, unsigned int n); |
va009039 | 0:65f1469d6bfb | 113 | |
va009039 | 0:65f1469d6bfb | 114 | /** |
va009039 | 0:65f1469d6bfb | 115 | * Prints a string to stdout (using plat_putByte) |
va009039 | 0:65f1469d6bfb | 116 | * |
va009039 | 0:65f1469d6bfb | 117 | * @param s Pointer to the C string to print |
va009039 | 0:65f1469d6bfb | 118 | */ |
va009039 | 0:65f1469d6bfb | 119 | void sli_puts(uint8_t * s); |
va009039 | 0:65f1469d6bfb | 120 | |
va009039 | 0:65f1469d6bfb | 121 | /** |
va009039 | 0:65f1469d6bfb | 122 | * Formats a 32-bit signed int as a decimal value. |
va009039 | 0:65f1469d6bfb | 123 | * |
va009039 | 0:65f1469d6bfb | 124 | * @param value the 32-bit signed value |
va009039 | 0:65f1469d6bfb | 125 | * @param buf a pointer to where the formatted string goes |
va009039 | 0:65f1469d6bfb | 126 | * @param buflen the length of the given buffer in bytes |
va009039 | 0:65f1469d6bfb | 127 | * @return a pointer to the string. |
va009039 | 0:65f1469d6bfb | 128 | */ |
va009039 | 0:65f1469d6bfb | 129 | PmReturn_t sli_ltoa10(int32_t value, uint8_t *buf, uint8_t buflen); |
va009039 | 0:65f1469d6bfb | 130 | |
va009039 | 0:65f1469d6bfb | 131 | /** |
va009039 | 0:65f1469d6bfb | 132 | * Formats an 8-bit int as a hexadecimal value. |
va009039 | 0:65f1469d6bfb | 133 | * |
va009039 | 0:65f1469d6bfb | 134 | * @param value the 8-bit value |
va009039 | 0:65f1469d6bfb | 135 | * @param buf a pointer to where the formatted string goes |
va009039 | 0:65f1469d6bfb | 136 | * @param buflen the length of the given buffer in bytes |
va009039 | 0:65f1469d6bfb | 137 | * @param upperCase when zero, hex chars rendered lowercase, else uppercase |
va009039 | 0:65f1469d6bfb | 138 | * @return Always PM_RET_OK |
va009039 | 0:65f1469d6bfb | 139 | */ |
va009039 | 0:65f1469d6bfb | 140 | PmReturn_t sli_btoa16(uint8_t value, uint8_t *buf, uint8_t buflen, uint8_t upperCase); |
va009039 | 0:65f1469d6bfb | 141 | |
va009039 | 0:65f1469d6bfb | 142 | /** |
va009039 | 0:65f1469d6bfb | 143 | * Formats a 32-bit signed int as a hexadecimal value. |
va009039 | 0:65f1469d6bfb | 144 | * |
va009039 | 0:65f1469d6bfb | 145 | * @param value the 32-bit signed value |
va009039 | 0:65f1469d6bfb | 146 | * @param buf a pointer to where the formatted string goes |
va009039 | 0:65f1469d6bfb | 147 | * @param buflen the length of the given buffer in bytes |
va009039 | 0:65f1469d6bfb | 148 | * @param upperCase when zero, hex chars rendered lowercase, else uppercase |
va009039 | 0:65f1469d6bfb | 149 | * @return Always PM_RET_OK |
va009039 | 0:65f1469d6bfb | 150 | */ |
va009039 | 0:65f1469d6bfb | 151 | PmReturn_t sli_ltoa16(int32_t value, uint8_t *buf, uint8_t buflen, uint8_t upperCase); |
va009039 | 0:65f1469d6bfb | 152 | |
va009039 | 0:65f1469d6bfb | 153 | /** |
va009039 | 0:65f1469d6bfb | 154 | * Formats a pointer as a hexadecimal value. |
va009039 | 0:65f1469d6bfb | 155 | * |
va009039 | 0:65f1469d6bfb | 156 | * @param value the pointer |
va009039 | 0:65f1469d6bfb | 157 | * @param buf a pointer to where the formatted string goes |
va009039 | 0:65f1469d6bfb | 158 | * @param buflen the length of the given buffer in bytes |
va009039 | 0:65f1469d6bfb | 159 | * @param upperCase when zero, hex chars rendered lowercase, else uppercase |
va009039 | 0:65f1469d6bfb | 160 | * @return Always PM_RET_OK |
va009039 | 0:65f1469d6bfb | 161 | */ |
va009039 | 0:65f1469d6bfb | 162 | PmReturn_t sli_ptoa16(intptr_t value, uint8_t *buf, uint8_t buflen, uint8_t upperCase); |
va009039 | 0:65f1469d6bfb | 163 | |
va009039 | 0:65f1469d6bfb | 164 | /** |
va009039 | 0:65f1469d6bfb | 165 | * Formats a 32-bit (single-precision) float as an ascii string. |
va009039 | 0:65f1469d6bfb | 166 | * |
va009039 | 0:65f1469d6bfb | 167 | * @param f the float value |
va009039 | 0:65f1469d6bfb | 168 | * @param buf a pointer to where the formatted string goes |
va009039 | 0:65f1469d6bfb | 169 | * @param buflen the size of the buffer |
va009039 | 0:65f1469d6bfb | 170 | * @return Status |
va009039 | 0:65f1469d6bfb | 171 | */ |
va009039 | 0:65f1469d6bfb | 172 | PmReturn_t sli_ftoa(float f, uint8_t *buf, uint8_t buflen); |
va009039 | 0:65f1469d6bfb | 173 | |
va009039 | 0:65f1469d6bfb | 174 | #endif /* __SLI_H__ */ |