uint16_t core_util_atomic_decr_u16(uint16_t *valuePtr, uint16_t delta) { uint16_t newValue; do { newValue = __LDREXH((volatile uint16_t*)valuePtr) - delta; } while (__STREXH(newValue, (volatile uint16_t*)valuePtr)); return newValue; }
uint16_t core_util_atomic_incr_u16(volatile uint16_t *valuePtr, uint16_t delta) { uint16_t newValue; do { newValue = __LDREXH(valuePtr) + delta; } while (__STREXH(newValue, valuePtr)); return newValue; }
bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue) { uint16_t currentValue = __LDREXH((volatile uint16_t*)ptr); if (currentValue != *expectedCurrentValue) { *expectedCurrentValue = currentValue; __CLREX(); return false; } return !__STREXH(desiredValue, (volatile uint16_t*)ptr); }
bool atomic_cas<uint16_t>(uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue) { uint16_t currentValue = __LDREXH(ptr); if (currentValue != *expectedCurrentValue) { *expectedCurrentValue = currentValue; __CLREX(); return false; } return !__STREXH(desiredValue, ptr); }
bool core_util_atomic_cas_u16(volatile uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue) { do { uint16_t currentValue = __LDREXH(ptr); if (currentValue != *expectedCurrentValue) { *expectedCurrentValue = currentValue; __CLREX(); return false; } } while (__STREXH(desiredValue, ptr)); return true; }
inline bool FIFO_write(FIFO_Data_TypeDef *inStruct, uint8_t inByte) { uint16_t workFree; if(inStruct->free <= 0) { inStruct->overrun++; return false; } inStruct->buffer[inStruct->head++] = inByte; if(inStruct->head >= inStruct->size) inStruct->head = 0; do { workFree = __LDREXH(&inStruct->free); workFree -= 1; } while( __STREXH(workFree, &inStruct->free)); return true; }
bool FIFO_read(FIFO_Data_TypeDef *inStruct, uint8_t *outByte, uint16_t count) { int i; uint16_t workFree; if( count > (inStruct->size - inStruct->free) ) return false; for(i = 0; i < count; i++) { outByte[i] = inStruct->buffer[inStruct->tail++]; if(inStruct->tail >= inStruct->size) { inStruct->tail = 0; } } do { workFree = __LDREXH(&inStruct->free); workFree += count; } while( __STREXH(workFree, &inStruct->free)); return true; }
bool FIFO_write_bytes(FIFO_Data_TypeDef *inStruct, uint8_t *inBytes, uint16_t count) { int i; uint16_t workFree; if(inStruct->free < count) { inStruct->overrun++; return false; } for(i = 0; i < count; i++) { inStruct->buffer[inStruct->head++] = inBytes[i]; if(inStruct->head >= inStruct->size) inStruct->head = 0; } do { workFree = __LDREXH(&inStruct->free); workFree -= count; } while( __STREXH(workFree, &inStruct->free)); return true; }