yas_mag_driver-yas530.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055
  1. /*
  2. * Copyright (c) 2010-2012 Yamaha Corporation
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. */
  20. #include "yas.h"
  21. struct utimeval {
  22. int32_t tv_sec;
  23. int32_t tv_msec;
  24. };
  25. struct utimer {
  26. struct utimeval prev_time;
  27. struct utimeval total_time;
  28. struct utimeval delay_ms;
  29. };
  30. static int utimeval_init(struct utimeval *val);
  31. static int utimeval_is_initial(struct utimeval *val);
  32. static int utimeval_is_overflow(struct utimeval *val);
  33. static struct utimeval utimeval_plus(struct utimeval *first,
  34. struct utimeval *second);
  35. static struct utimeval utimeval_minus(struct utimeval *first,
  36. struct utimeval *second);
  37. static int utimeval_greater_than(struct utimeval *first,
  38. struct utimeval *second);
  39. static int utimeval_greater_or_equal(struct utimeval *first,
  40. struct utimeval *second);
  41. static int utimeval_greater_than_zero(struct utimeval *val);
  42. static int utimeval_less_than_zero(struct utimeval *val);
  43. static struct utimeval *msec_to_utimeval(struct utimeval *result,
  44. uint32_t msec);
  45. static uint32_t utimeval_to_msec(struct utimeval *val);
  46. static struct utimeval utimer_calc_next_time(struct utimer *ut,
  47. struct utimeval *cur);
  48. static struct utimeval utimer_current_time(void);
  49. static int utimer_is_timeout(struct utimer *ut);
  50. static int utimer_clear_timeout(struct utimer *ut);
  51. static uint32_t utimer_get_total_time(struct utimer *ut);
  52. static uint32_t utimer_get_delay(struct utimer *ut);
  53. static int utimer_set_delay(struct utimer *ut, uint32_t delay_ms);
  54. static int utimer_update(struct utimer *ut);
  55. static int utimer_update_with_curtime(struct utimer *ut, struct utimeval *cur);
  56. static uint32_t utimer_sleep_time(struct utimer *ut);
  57. static uint32_t utimer_sleep_time_with_curtime(struct utimer *ut,
  58. struct utimeval *cur);
  59. static int utimer_init(struct utimer *ut, uint32_t delay_ms);
  60. static int utimer_clear(struct utimer *ut);
  61. static void utimer_lib_init(void (*func)(int *sec, int *msec));
  62. #define YAS_REGADDR_DEVICE_ID (0x80)
  63. #define YAS_REGADDR_ACTUATE_INIT_COIL (0x81)
  64. #define YAS_REGADDR_MEASURE_COMMAND (0x82)
  65. #define YAS_REGADDR_CONFIG (0x83)
  66. #define YAS_REGADDR_MEASURE_INTERVAL (0x84)
  67. #define YAS_REGADDR_OFFSET_X (0x85)
  68. #define YAS_REGADDR_OFFSET_Y1 (0x86)
  69. #define YAS_REGADDR_OFFSET_Y2 (0x87)
  70. #define YAS_REGADDR_TEST1 (0x88)
  71. #define YAS_REGADDR_TEST2 (0x89)
  72. #define YAS_REGADDR_CAL (0x90)
  73. #define YAS_REGADDR_MEASURE_DATA (0xb0)
  74. #define YAS_YAS530_DEVICE_ID (0x01) /* YAS530 (MS-3E) */
  75. #define YAS_YAS530_VERSION_A (0) /* YAS530 (MS-3E Aver) */
  76. #define YAS_YAS530_VERSION_B (1) /* YAS530B (MS-3E Bver) */
  77. #define YAS_YAS530_VERSION_A_COEF (380)
  78. #define YAS_YAS530_VERSION_B_COEF (550)
  79. #define YAS_YAS530_DATA_CENTER (2048)
  80. #define YAS_YAS530_DATA_OVERFLOW (4095)
  81. #define YAS_YAS532_DEVICE_ID (0x02) /* YAS532 (MS-3R) */
  82. #define YAS_YAS532_VERSION_AB (0) /* YAS532AB (MS-3R ABver) */
  83. #define YAS_YAS532_VERSION_AC (1) /* YAS532AC (MS-3R ACver) */
  84. #define YAS_YAS532_VERSION_AB_COEF (1800)
  85. #define YAS_YAS532_VERSION_AC_COEF (900)
  86. #define YAS_YAS532_DATA_CENTER (4096)
  87. #define YAS_YAS532_DATA_OVERFLOW (8190)
  88. #define YAS_YAS530_CAL_SINGLE_READ
  89. struct yas_machdep_func {
  90. int (*device_open)(void);
  91. int (*device_close)(void);
  92. int (*device_write)(uint8_t slave, uint8_t addr, const uint8_t *buf, int len);
  93. int (*device_read)(uint8_t slave, uint8_t addr, uint8_t *buf, int len);
  94. void (*msleep)(int msec);
  95. };
  96. static int yas_cdrv_actuate_initcoil(void);
  97. static int yas_cdrv_set_offset(const int8_t *offset);
  98. static int yas_cdrv_recalc_calib_offset(int32_t *prev_calib_offset,
  99. int32_t *new_calib_offset, int8_t *prev_offset,
  100. int8_t *new_offset);
  101. static int yas_cdrv_set_transformatiom_matrix(const int8_t *transform);
  102. static int yas_cdrv_measure_and_set_offset(int8_t *offset);
  103. static int yas_cdrv_measure(int32_t *msens, int32_t *raw, int16_t *t);
  104. static int yas_cdrv_init(const int8_t *transform,
  105. struct yas_machdep_func *func);
  106. static int yas_cdrv_term(void);
  107. static void (*current_time)(int *sec, int *msec);
  108. static int
  109. utimeval_init(struct utimeval *val)
  110. {
  111. if (val == NULL)
  112. return -1;
  113. val->tv_sec = val->tv_msec = 0;
  114. return 0;
  115. }
  116. static int
  117. utimeval_is_initial(struct utimeval *val)
  118. {
  119. if (val == NULL)
  120. return 0;
  121. return val->tv_sec == 0 && val->tv_msec == 0;
  122. }
  123. static int
  124. utimeval_is_overflow(struct utimeval *val)
  125. {
  126. int32_t max;
  127. if (val == NULL)
  128. return 0;
  129. max = (int32_t) ((uint32_t) 0xffffffff / (uint32_t) 1000);
  130. if (val->tv_sec > max) {
  131. return 1; /* overflow */
  132. } else if (val->tv_sec == max) {
  133. if (val->tv_msec > (int32_t)((uint32_t)0xffffffff
  134. % (uint32_t)1000))
  135. return 1; /* overflow */
  136. }
  137. return 0;
  138. }
  139. static struct utimeval
  140. utimeval_plus(struct utimeval *first, struct utimeval *second)
  141. {
  142. struct utimeval result = {0, 0};
  143. int32_t tmp;
  144. if (first == NULL || second == NULL)
  145. return result;
  146. tmp = first->tv_sec + second->tv_sec;
  147. if (first->tv_sec >= 0 && second->tv_sec >= 0 && tmp < 0)
  148. goto overflow;
  149. if (first->tv_sec < 0 && second->tv_sec < 0 && tmp >= 0)
  150. goto underflow;
  151. result.tv_sec = tmp;
  152. result.tv_msec = first->tv_msec + second->tv_msec;
  153. if (1000 <= result.tv_msec) {
  154. tmp = result.tv_sec + result.tv_msec / 1000;
  155. if (result.tv_sec >= 0 && result.tv_msec >= 0 && tmp < 0)
  156. goto overflow;
  157. result.tv_sec = tmp;
  158. result.tv_msec = result.tv_msec % 1000;
  159. }
  160. if (result.tv_msec < 0) {
  161. tmp = result.tv_sec + result.tv_msec / 1000 - 1;
  162. if (result.tv_sec < 0 && result.tv_msec < 0 && tmp >= 0)
  163. goto underflow;
  164. result.tv_sec = tmp;
  165. result.tv_msec = result.tv_msec % 1000 + 1000;
  166. }
  167. return result;
  168. overflow:
  169. result.tv_sec = (int32_t)0x7fffffff;
  170. result.tv_msec = 999;
  171. return result;
  172. underflow:
  173. result.tv_sec = (int32_t)0x80000000;
  174. result.tv_msec = 0;
  175. return result;
  176. }
  177. static struct utimeval
  178. utimeval_minus(struct utimeval *first, struct utimeval *second)
  179. {
  180. struct utimeval result = {0, 0}, tmp;
  181. if (first == NULL || second == NULL
  182. || second->tv_sec == (int)0x80000000)
  183. return result;
  184. tmp.tv_sec = -second->tv_sec;
  185. tmp.tv_msec = -second->tv_msec;
  186. return utimeval_plus(first, &tmp);
  187. }
  188. static int
  189. utimeval_less_than(struct utimeval *first, struct utimeval *second)
  190. {
  191. if (first == NULL || second == NULL)
  192. return 0;
  193. if (first->tv_sec > second->tv_sec) {
  194. return 1;
  195. } else if (first->tv_sec < second->tv_sec) {
  196. return 0;
  197. } else {
  198. if (first->tv_msec > second->tv_msec)
  199. return 1;
  200. else
  201. return 0;
  202. }
  203. }
  204. static int
  205. utimeval_greater_than(struct utimeval *first, struct utimeval *second)
  206. {
  207. if (first == NULL || second == NULL)
  208. return 0;
  209. if (first->tv_sec < second->tv_sec) {
  210. return 1;
  211. } else if (first->tv_sec > second->tv_sec) {
  212. return 0;
  213. } else {
  214. if (first->tv_msec < second->tv_msec)
  215. return 1;
  216. else
  217. return 0;
  218. }
  219. }
  220. static int
  221. utimeval_greater_or_equal(struct utimeval *first,
  222. struct utimeval *second)
  223. {
  224. return !utimeval_less_than(first, second);
  225. }
  226. static int
  227. utimeval_greater_than_zero(struct utimeval *val)
  228. {
  229. struct utimeval zero = {0, 0};
  230. return utimeval_greater_than(&zero, val);
  231. }
  232. static int
  233. utimeval_less_than_zero(struct utimeval *val)
  234. {
  235. struct utimeval zero = {0, 0};
  236. return utimeval_less_than(&zero, val);
  237. }
  238. static struct utimeval *
  239. msec_to_utimeval(struct utimeval *result, uint32_t msec)
  240. {
  241. if (result == NULL)
  242. return result;
  243. result->tv_sec = (int32_t)(msec / 1000);
  244. result->tv_msec = (int32_t)(msec % 1000);
  245. return result;
  246. }
  247. static uint32_t
  248. utimeval_to_msec(struct utimeval *val)
  249. {
  250. if (val == NULL)
  251. return 0;
  252. if (utimeval_less_than_zero(val))
  253. return 0;
  254. if (utimeval_is_overflow(val))
  255. return 0xffffffff;
  256. return (uint32_t)(val->tv_sec * 1000 + val->tv_msec);
  257. }
  258. static struct utimeval
  259. utimer_calc_next_time(struct utimer *ut, struct utimeval *cur)
  260. {
  261. struct utimeval result = {0, 0}, delay;
  262. if (ut == NULL || cur == NULL)
  263. return result;
  264. utimer_update_with_curtime(ut, cur);
  265. if (utimer_is_timeout(ut)) {
  266. result = *cur;
  267. } else {
  268. delay = utimeval_minus(&ut->delay_ms, &ut->total_time);
  269. result = utimeval_plus(cur, &delay);
  270. }
  271. return result;
  272. }
  273. static struct utimeval
  274. utimer_current_time(void)
  275. {
  276. struct utimeval tv;
  277. int sec, msec;
  278. if (current_time != NULL)
  279. current_time(&sec, &msec);
  280. else
  281. sec = 0, msec = 0;
  282. tv.tv_sec = sec;
  283. tv.tv_msec = msec;
  284. return tv;
  285. }
  286. static int
  287. utimer_clear(struct utimer *ut)
  288. {
  289. if (ut == NULL)
  290. return -1;
  291. utimeval_init(&ut->prev_time);
  292. utimeval_init(&ut->total_time);
  293. return 0;
  294. }
  295. static int
  296. utimer_update_with_curtime(struct utimer *ut, struct utimeval *cur)
  297. {
  298. struct utimeval tmp;
  299. if (ut == NULL || cur == NULL)
  300. return -1;
  301. if (utimeval_is_initial(&ut->prev_time))
  302. ut->prev_time = *cur;
  303. if (utimeval_greater_than_zero(&ut->delay_ms)) {
  304. tmp = utimeval_minus(cur, &ut->prev_time);
  305. if (utimeval_less_than_zero(&tmp)) {
  306. utimeval_init(&ut->total_time);
  307. } else {
  308. ut->total_time = utimeval_plus(&tmp, &ut->total_time);
  309. if (utimeval_is_overflow(&ut->total_time))
  310. utimeval_init(&ut->total_time);
  311. }
  312. ut->prev_time = *cur;
  313. }
  314. return 0;
  315. }
  316. static int
  317. utimer_update(struct utimer *ut)
  318. {
  319. struct utimeval cur;
  320. if (ut == NULL)
  321. return -1;
  322. cur = utimer_current_time();
  323. utimer_update_with_curtime(ut, &cur);
  324. return 0;
  325. }
  326. static int
  327. utimer_is_timeout(struct utimer *ut)
  328. {
  329. if (ut == NULL)
  330. return 0;
  331. if (utimeval_greater_than_zero(&ut->delay_ms))
  332. return utimeval_greater_or_equal(&ut->delay_ms,
  333. &ut->total_time);
  334. else
  335. return 1;
  336. }
  337. static int
  338. utimer_clear_timeout(struct utimer *ut)
  339. {
  340. uint32_t delay, total;
  341. if (ut == NULL)
  342. return -1;
  343. delay = utimeval_to_msec(&ut->delay_ms);
  344. if (delay == 0 || utimeval_is_overflow(&ut->total_time)) {
  345. total = 0;
  346. } else {
  347. if (utimeval_is_overflow(&ut->total_time)) {
  348. total = 0;
  349. } else {
  350. total = utimeval_to_msec(&ut->total_time);
  351. total = total % delay;
  352. }
  353. }
  354. msec_to_utimeval(&ut->total_time, total);
  355. return 0;
  356. }
  357. static uint32_t
  358. utimer_sleep_time_with_curtime(struct utimer *ut, struct utimeval *cur)
  359. {
  360. struct utimeval tv;
  361. if (ut == NULL || cur == NULL)
  362. return 0;
  363. tv = utimer_calc_next_time(ut, cur);
  364. tv = utimeval_minus(&tv, cur);
  365. if (utimeval_less_than_zero(&tv))
  366. return 0;
  367. return utimeval_to_msec(&tv);
  368. }
  369. static uint32_t
  370. utimer_sleep_time(struct utimer *ut)
  371. {
  372. struct utimeval cur;
  373. if (ut == NULL)
  374. return 0;
  375. cur = utimer_current_time();
  376. return utimer_sleep_time_with_curtime(ut, &cur);
  377. }
  378. static int
  379. utimer_init(struct utimer *ut, uint32_t delay_ms)
  380. {
  381. if (ut == NULL)
  382. return -1;
  383. utimer_clear(ut);
  384. msec_to_utimeval(&ut->delay_ms, delay_ms);
  385. return 0;
  386. }
  387. static uint32_t
  388. utimer_get_total_time(struct utimer *ut)
  389. {
  390. return utimeval_to_msec(&ut->total_time);
  391. }
  392. static uint32_t
  393. utimer_get_delay(struct utimer *ut)
  394. {
  395. if (ut == NULL)
  396. return 0;
  397. return utimeval_to_msec(&ut->delay_ms);
  398. }
  399. static int
  400. utimer_set_delay(struct utimer *ut, uint32_t delay_ms)
  401. {
  402. return utimer_init(ut, delay_ms);
  403. }
  404. static void
  405. utimer_lib_init(void (*func)(int *sec, int *msec))
  406. {
  407. current_time = func;
  408. }
  409. struct yas_cal_data {
  410. uint8_t dx, dy1, dy2;
  411. uint8_t d2, d3, d4, d5, d6, d7, d8, d9, d0;
  412. uint8_t dck;
  413. uint8_t ver;
  414. };
  415. struct yas_correction_data {
  416. int32_t Cx, Cy1, Cy2;
  417. int32_t a2, a3, a4, a5, a6, a7, a8, a9, k;
  418. };
  419. struct yas_cdriver {
  420. struct yas_cal_data cal;
  421. struct yas_correction_data correct;
  422. struct yas_machdep_func func;
  423. int8_t transform[9];
  424. int16_t temperature;
  425. uint8_t dev_id;
  426. int32_t coef;
  427. int16_t center;
  428. int16_t overflow;
  429. };
  430. static struct yas_cdriver cdriver;
  431. static int
  432. device_open(void)
  433. {
  434. if (cdriver.func.device_open == NULL)
  435. return -1;
  436. return cdriver.func.device_open();
  437. }
  438. static int
  439. device_close(void)
  440. {
  441. if (cdriver.func.device_close == NULL)
  442. return -1;
  443. return cdriver.func.device_close();
  444. }
  445. static int
  446. device_write(uint8_t addr, const uint8_t *buf, int len)
  447. {
  448. if (cdriver.func.device_write == NULL)
  449. return -1;
  450. return cdriver.func.device_write(YAS_MAG_I2C_SLAVEADDR, addr, buf, len);
  451. }
  452. static int
  453. device_read(uint8_t addr, uint8_t *buf, int len)
  454. {
  455. if (cdriver.func.device_read == NULL)
  456. return -1;
  457. return cdriver.func.device_read(YAS_MAG_I2C_SLAVEADDR, addr, buf, len);
  458. }
  459. static void
  460. sleep(int millisec)
  461. {
  462. if (cdriver.func.msleep == NULL)
  463. return;
  464. cdriver.func.msleep(millisec);
  465. }
  466. static int
  467. init_test_register(void)
  468. {
  469. uint8_t data;
  470. data = 0x00;
  471. if (device_write(YAS_REGADDR_TEST1, &data, 1) < 0)
  472. return YAS_ERROR_DEVICE_COMMUNICATION;
  473. data = 0x00;
  474. if (device_write(YAS_REGADDR_TEST2, &data, 1) < 0)
  475. return YAS_ERROR_DEVICE_COMMUNICATION;
  476. return YAS_NO_ERROR;
  477. }
  478. static int
  479. get_device_id(uint8_t *id)
  480. {
  481. uint8_t data = 0;
  482. if (device_read(YAS_REGADDR_DEVICE_ID, &data, 1) < 0)
  483. return YAS_ERROR_DEVICE_COMMUNICATION;
  484. *id = data;
  485. return YAS_NO_ERROR;
  486. }
  487. static int
  488. get_cal_data_yas530(struct yas_cal_data *cal)
  489. {
  490. uint8_t data[16];
  491. #ifdef YAS_YAS530_CAL_SINGLE_READ
  492. int i;
  493. for (i = 0; i < 16; i++) { /* dummy read */
  494. if (device_read(YAS_REGADDR_CAL + i, &data[i], 1) < 0)
  495. return YAS_ERROR_DEVICE_COMMUNICATION;
  496. }
  497. for (i = 0; i < 16; i++) {
  498. if (device_read(YAS_REGADDR_CAL + i, &data[i], 1) < 0)
  499. return YAS_ERROR_DEVICE_COMMUNICATION;
  500. }
  501. #else
  502. if (device_read(YAS_REGADDR_CAL, data, 16) < 0) /* dummy read */
  503. return YAS_ERROR_DEVICE_COMMUNICATION;
  504. if (device_read(YAS_REGADDR_CAL, data, 16) < 0)
  505. return YAS_ERROR_DEVICE_COMMUNICATION;
  506. #endif
  507. cal->dx = data[0];
  508. cal->dy1 = data[1];
  509. cal->dy2 = data[2];
  510. cal->d2 = (data[3]>>2) & 0x03f;
  511. cal->d3 = (uint8_t)(((data[3]<<2) & 0x0c) | ((data[4]>>6) & 0x03));
  512. cal->d4 = (uint8_t)(data[4] & 0x3f);
  513. cal->d5 = (data[5]>>2) & 0x3f;
  514. cal->d6 = (uint8_t)(((data[5]<<4) & 0x30) | ((data[6]>>4) & 0x0f));
  515. cal->d7 = (uint8_t)(((data[6]<<3) & 0x78) | ((data[7]>>5) & 0x07));
  516. cal->d8 = (uint8_t)(((data[7]<<1) & 0x3e) | ((data[8]>>7) & 0x01));
  517. cal->d9 = (uint8_t)(((data[8]<<1) & 0xfe) | ((data[9]>>7) & 0x01));
  518. cal->d0 = (uint8_t)((data[9]>>2) & 0x1f);
  519. cal->dck = (uint8_t)(((data[9]<<1) & 0x06) | ((data[10]>>7) & 0x01));
  520. cal->ver = (uint8_t)((data[15]) & 0x03);
  521. return YAS_NO_ERROR;
  522. }
  523. static void
  524. get_correction_value_yas530(struct yas_cal_data *cal,
  525. struct yas_correction_data *correct)
  526. {
  527. correct->Cx = cal->dx * 6 - 768;
  528. correct->Cy1 = cal->dy1 * 6 - 768;
  529. correct->Cy2 = cal->dy2 * 6 - 768;
  530. correct->a2 = cal->d2 - 32;
  531. correct->a3 = cal->d3 - 8;
  532. correct->a4 = cal->d4 - 32;
  533. correct->a5 = cal->d5 + 38;
  534. correct->a6 = cal->d6 - 32;
  535. correct->a7 = cal->d7 - 64;
  536. correct->a8 = cal->d8 - 32;
  537. correct->a9 = cal->d9;
  538. correct->k = cal->d0 + 10;
  539. }
  540. static int
  541. get_cal_data_yas532(struct yas_cal_data *cal)
  542. {
  543. uint8_t data[14];
  544. #ifdef YAS_YAS530_CAL_SINGLE_READ
  545. int i;
  546. for (i = 0; i < 14; i++) { /* dummy read */
  547. if (device_read(YAS_REGADDR_CAL + i, &data[i], 1) < 0)
  548. return YAS_ERROR_DEVICE_COMMUNICATION;
  549. }
  550. for (i = 0; i < 14; i++) {
  551. if (device_read(YAS_REGADDR_CAL + i, &data[i], 1) < 0)
  552. return YAS_ERROR_DEVICE_COMMUNICATION;
  553. }
  554. #else
  555. if (device_read(YAS_REGADDR_CAL, data, 14) < 0) /* dummy read */
  556. return YAS_ERROR_DEVICE_COMMUNICATION;
  557. if (device_read(YAS_REGADDR_CAL, data, 14) < 0)
  558. return YAS_ERROR_DEVICE_COMMUNICATION;
  559. #endif
  560. cal->dx = data[0];
  561. cal->dy1 = data[1];
  562. cal->dy2 = data[2];
  563. cal->d2 = (data[3]>>2) & 0x03f;
  564. cal->d3 = (uint8_t)(((data[3]<<2) & 0x0c) | ((data[4]>>6) & 0x03));
  565. cal->d4 = (uint8_t)(data[4] & 0x3f);
  566. cal->d5 = (data[5]>>2) & 0x3f;
  567. cal->d6 = (uint8_t)(((data[5]<<4) & 0x30) | ((data[6]>>4) & 0x0f));
  568. cal->d7 = (uint8_t)(((data[6]<<3) & 0x78) | ((data[7]>>5) & 0x07));
  569. cal->d8 = (uint8_t)(((data[7]<<1) & 0x3e) | ((data[8]>>7) & 0x01));
  570. cal->d9 = (uint8_t)(((data[8]<<1) & 0xfe) | ((data[9]>>7) & 0x01));
  571. cal->d0 = (uint8_t)((data[9]>>2) & 0x1f);
  572. cal->dck = (uint8_t)(((data[9]<<1) & 0x06) | ((data[10]>>7) & 0x01));
  573. cal->ver = (uint8_t)((data[13]) & 0x01);
  574. return YAS_NO_ERROR;
  575. }
  576. static void
  577. get_correction_value_yas532(struct yas_cal_data *cal,
  578. struct yas_correction_data *correct)
  579. {
  580. correct->Cx = cal->dx * 10 - 1280;
  581. correct->Cy1 = cal->dy1 * 10 - 1280;
  582. correct->Cy2 = cal->dy2 * 10 - 1280;
  583. correct->a2 = cal->d2 - 32;
  584. correct->a3 = cal->d3 - 8;
  585. correct->a4 = cal->d4 - 32;
  586. correct->a5 = cal->d5 + 38;
  587. correct->a6 = cal->d6 - 32;
  588. correct->a7 = cal->d7 - 64;
  589. correct->a8 = cal->d8 - 32;
  590. correct->a9 = cal->d9;
  591. correct->k = cal->d0;
  592. }
  593. static int
  594. set_configuration(int inton, int inthact, int cck)
  595. {
  596. uint8_t data = 0;
  597. data = (uint8_t)(data | ((!!inton) & 0x01));
  598. data = (uint8_t)(data | (((!!inthact)<<1) & 0x02));
  599. data = (uint8_t)(data | ((cck<<2) & 0x1c));
  600. if (device_write(YAS_REGADDR_CONFIG, &data, 1) < 0)
  601. return YAS_ERROR_DEVICE_COMMUNICATION;
  602. return YAS_NO_ERROR;
  603. }
  604. static int
  605. get_measure_interval(int32_t *msec)
  606. {
  607. uint8_t data;
  608. int mult = 7;
  609. if (device_read(YAS_REGADDR_MEASURE_INTERVAL, &data, 1) < 0)
  610. return YAS_ERROR_DEVICE_COMMUNICATION;
  611. switch (cdriver.dev_id) {
  612. case YAS_YAS532_DEVICE_ID:
  613. mult = 4;
  614. break;
  615. case YAS_YAS530_DEVICE_ID:
  616. default:
  617. mult = 7;
  618. break;
  619. }
  620. *msec = data * mult;
  621. return YAS_NO_ERROR;
  622. }
  623. static int
  624. set_measure_interval(int32_t msec)
  625. {
  626. uint8_t data = 0;
  627. int mult = 7;
  628. switch (cdriver.dev_id) {
  629. case YAS_YAS532_DEVICE_ID:
  630. mult = 4;
  631. break;
  632. case YAS_YAS530_DEVICE_ID:
  633. default:
  634. mult = 7;
  635. break;
  636. }
  637. if (msec > mult*0xff)
  638. data = 0xff;
  639. else
  640. if (msec % mult == 0)
  641. data = (uint8_t)(msec / mult);
  642. else
  643. data = (uint8_t)(msec / mult + 1);
  644. if (device_write(YAS_REGADDR_MEASURE_INTERVAL, &data, 1) < 0)
  645. return YAS_ERROR_DEVICE_COMMUNICATION;
  646. return YAS_NO_ERROR;
  647. }
  648. static int
  649. set_measure_command(int ldtc, int fors, int dlymes)
  650. {
  651. uint8_t data = 0;
  652. data = (uint8_t)(data | 0x01);
  653. data = (uint8_t)(data | (((!!ldtc)<<1) & 0x02));
  654. data = (uint8_t)(data | (((!!fors)<<2) & 0x04));
  655. data = (uint8_t)(data | (((!!dlymes)<<4) & 0x10));
  656. if (device_write(YAS_REGADDR_MEASURE_COMMAND, &data, 1) < 0)
  657. return YAS_ERROR_DEVICE_COMMUNICATION;
  658. return YAS_NO_ERROR;
  659. }
  660. static int
  661. measure_normal_yas530(int *busy, int16_t *t,
  662. int16_t *x, int16_t *y1, int16_t *y2)
  663. {
  664. uint8_t data[8];
  665. if (set_measure_command(0, 0, 0) < 0)
  666. return YAS_ERROR_DEVICE_COMMUNICATION;
  667. sleep(2);
  668. if (device_read(YAS_REGADDR_MEASURE_DATA, data, 8) < 0)
  669. return YAS_ERROR_DEVICE_COMMUNICATION;
  670. *busy = (data[0]>>7) & 0x01;
  671. *t = (int16_t)((((int32_t)data[0]<<2) & 0x1fc)
  672. | ((data[1]>>6) & 0x03));
  673. *x = (int16_t)((((int32_t)data[2]<<5) & 0xfe0)
  674. | ((data[3]>>3) & 0x1f));
  675. *y1 = (int16_t)((((int32_t)data[4]<<5) & 0xfe0)
  676. | ((data[5]>>3) & 0x1f));
  677. *y2 = (int16_t)((((int32_t)data[6]<<5) & 0xfe0)
  678. | ((data[7]>>3) & 0x1f));
  679. /*YLOGD(("f[%d] t[%d] x[%d] y1[%d] y2[%d]\n",
  680. *busy, *t, *x, *y1, *y2));*/
  681. return YAS_NO_ERROR;
  682. }
  683. static int
  684. measure_normal_yas532(int *busy, int16_t *t,
  685. int16_t *x, int16_t *y1, int16_t *y2)
  686. {
  687. uint8_t data[8];
  688. if (set_measure_command(0, 0, 0) < 0)
  689. return YAS_ERROR_DEVICE_COMMUNICATION;
  690. sleep(2);
  691. if (device_read(YAS_REGADDR_MEASURE_DATA, data, 8) < 0)
  692. return YAS_ERROR_DEVICE_COMMUNICATION;
  693. *busy = (data[0]>>7) & 0x01;
  694. *t = (int16_t)((((int32_t)data[0]<<3) & 0x3f8)
  695. | ((data[1]>>5) & 0x07));
  696. *x = (int16_t)((((int32_t)data[2]<<6) & 0x1fc0)
  697. | ((data[3]>>2) & 0x3f));
  698. *y1 = (int16_t)((((int32_t)data[4]<<6) & 0x1fc0)
  699. | ((data[5]>>2) & 0x3f));
  700. *y2 = (int16_t)((((int32_t)data[6]<<6) & 0x1fc0)
  701. | ((data[7]>>2) & 0x3f));
  702. /*YLOGD(("f[%d] t[%d] x[%d] y1[%d] y2[%d]\n",
  703. *busy, *t, *x, *y1, *y2));*/
  704. return YAS_NO_ERROR;
  705. }
  706. static int
  707. measure_normal(int *busy, int16_t *t, int16_t *x, int16_t *y1, int16_t *y2)
  708. {
  709. int result;
  710. switch (cdriver.dev_id) {
  711. case YAS_YAS532_DEVICE_ID:
  712. result = measure_normal_yas532(busy, t, x, y1, y2);
  713. break;
  714. case YAS_YAS530_DEVICE_ID:
  715. default:
  716. result = measure_normal_yas530(busy, t, x, y1, y2);
  717. break;
  718. }
  719. return result;
  720. }
  721. static int
  722. coordinate_conversion(int32_t x, int32_t y1, int32_t y2, int16_t t,
  723. int32_t *xo, int32_t *yo, int32_t *zo,
  724. struct yas_correction_data *c)
  725. {
  726. int32_t sx, sy1, sy2, sy, sz;
  727. int32_t hx, hy, hz;
  728. sx = x - (c->Cx * t) / 100;
  729. sy1 = y1 - (c->Cy1 * t) / 100;
  730. sy2 = y2 - (c->Cy2 * t) / 100;
  731. sy = sy1 - sy2;
  732. sz = -sy1 - sy2;
  733. hx = c->k * ((100 * sx + c->a2 * sy + c->a3 * sz) / 10);
  734. hy = c->k * ((c->a4 * sx + c->a5 * sy + c->a6 * sz) / 10);
  735. hz = c->k * ((c->a7 * sx + c->a8 * sy + c->a9 * sz) / 10);
  736. *xo = cdriver.transform[0] * hx
  737. + cdriver.transform[1] * hy
  738. + cdriver.transform[2] * hz;
  739. *yo = cdriver.transform[3] * hx
  740. + cdriver.transform[4] * hy
  741. + cdriver.transform[5] * hz;
  742. *zo = cdriver.transform[6] * hx
  743. + cdriver.transform[7] * hy
  744. + cdriver.transform[8] * hz;
  745. return YAS_NO_ERROR;
  746. }
  747. static int
  748. set_hardware_offset(int8_t offset_x, int8_t offset_y1, int8_t offset_y2)
  749. {
  750. uint8_t data;
  751. data = (uint8_t)(offset_x & 0x3f);
  752. if (device_write(YAS_REGADDR_OFFSET_X, &data, 1) < 0)
  753. return YAS_ERROR_DEVICE_COMMUNICATION;
  754. data = (uint8_t)(offset_y1 & 0x3f);
  755. if (device_write(YAS_REGADDR_OFFSET_Y1, &data, 1) < 0)
  756. return YAS_ERROR_DEVICE_COMMUNICATION;
  757. data = (uint8_t)(offset_y2 & 0x3f);
  758. if (device_write(YAS_REGADDR_OFFSET_Y2, &data, 1) < 0)
  759. return YAS_ERROR_DEVICE_COMMUNICATION;
  760. return YAS_NO_ERROR;
  761. }
  762. static int
  763. yas_cdrv_actuate_initcoil(void)
  764. {
  765. uint8_t data = 0;
  766. if (device_write(YAS_REGADDR_ACTUATE_INIT_COIL, &data, 1) < 0)
  767. return YAS_ERROR_DEVICE_COMMUNICATION;
  768. return YAS_NO_ERROR;
  769. }
  770. static int
  771. check_offset(int8_t offset_x, int8_t offset_y1, int8_t offset_y2,
  772. int *flag_x, int *flag_y1, int *flag_y2)
  773. {
  774. int busy;
  775. int16_t t, x, y1, y2;
  776. if (set_hardware_offset(offset_x, offset_y1, offset_y2) < 0)
  777. return YAS_ERROR_DEVICE_COMMUNICATION;
  778. if (measure_normal(&busy, &t, &x, &y1, &y2) < 0)
  779. return YAS_ERROR_DEVICE_COMMUNICATION;
  780. *flag_x = *flag_y1 = *flag_y2 = 0;
  781. if (x > cdriver.center)
  782. *flag_x = 1;
  783. if (y1 > cdriver.center)
  784. *flag_y1 = 1;
  785. if (y2 > cdriver.center)
  786. *flag_y2 = 1;
  787. if (x < cdriver.center)
  788. *flag_x = -1;
  789. if (y1 < cdriver.center)
  790. *flag_y1 = -1;
  791. if (y2 < cdriver.center)
  792. *flag_y2 = -1;
  793. return YAS_NO_ERROR;
  794. }
  795. static int
  796. yas_cdrv_measure_and_set_offset(int8_t *offset)
  797. {
  798. int i;
  799. int8_t offset_x = 0, offset_y1 = 0, offset_y2 = 0;
  800. int flag_x = 0, flag_y1 = 0, flag_y2 = 0;
  801. static const int correct[5] = {16, 8, 4, 2, 1};
  802. for (i = 0; i < 5; i++) {
  803. if (check_offset(offset_x, offset_y1, offset_y2,
  804. &flag_x, &flag_y1, &flag_y2) < 0) {
  805. return YAS_ERROR_DEVICE_COMMUNICATION;
  806. }
  807. YLOGD(("offset[%d][%d][%d] flag[%d][%d][%d]\n",
  808. offset_x, offset_y1, offset_y2,
  809. flag_x, flag_y1, flag_y2));
  810. if (flag_x)
  811. offset_x = (int8_t)(offset_x + flag_x * correct[i]);
  812. if (flag_y1)
  813. offset_y1 = (int8_t)(offset_y1 + flag_y1 * correct[i]);
  814. if (flag_y2)
  815. offset_y2 = (int8_t)(offset_y2 + flag_y2 * correct[i]);
  816. }
  817. if (set_hardware_offset(offset_x, offset_y1, offset_y2) < 0)
  818. return YAS_ERROR_DEVICE_COMMUNICATION;
  819. offset[0] = offset_x;
  820. offset[1] = offset_y1;
  821. offset[2] = offset_y2;
  822. return YAS_NO_ERROR;
  823. }
  824. static int
  825. yas_cdrv_set_offset(const int8_t *offset)
  826. {
  827. if (set_hardware_offset(offset[0], offset[1], offset[2]) < 0)
  828. return YAS_ERROR_DEVICE_COMMUNICATION;
  829. return YAS_NO_ERROR;
  830. }
  831. static int
  832. yas_cdrv_measure(int32_t *data, int32_t *raw, int16_t *temperature)
  833. {
  834. int busy;
  835. int16_t x, y1, y2, t;
  836. int32_t xx, yy1, yy2;
  837. int result = 0;
  838. if (measure_normal(&busy, &t, &x, &y1, &y2) < 0)
  839. return YAS_ERROR_DEVICE_COMMUNICATION;
  840. if (x == 0)
  841. result |= 0x01;
  842. if (x == cdriver.overflow)
  843. result |= 0x02;
  844. if (y1 == 0)
  845. result |= 0x04;
  846. if (y1 == cdriver.overflow)
  847. result |= 0x08;
  848. if (y2 == 0)
  849. result |= 0x10;
  850. if (y2 == cdriver.overflow)
  851. result |= 0x20;
  852. xx = x;
  853. yy1 = y1;
  854. yy2 = y2;
  855. if (coordinate_conversion(xx, yy1, yy2, t, &data[0], &data[1],
  856. &data[2], &cdriver.correct) < 0) {
  857. return YAS_ERROR_DEVICE_COMMUNICATION;
  858. }
  859. cdriver.temperature = t;
  860. if (raw != NULL)
  861. raw[0] = xx, raw[1] = yy1, raw[2] = yy2;
  862. if (temperature != NULL)
  863. *temperature = t;
  864. return result;
  865. }
  866. static int
  867. yas_cdrv_recalc_calib_offset(int32_t *prev_calib_offset,
  868. int32_t *new_calib_offset, int8_t *prev_offset,
  869. int8_t *new_offset)
  870. {
  871. int32_t tmp[3], resolution[9], base[3];
  872. int32_t raw[3];
  873. int32_t diff, i;
  874. if (prev_calib_offset == NULL || new_calib_offset == NULL
  875. || prev_offset == NULL || new_offset == NULL) {
  876. return YAS_ERROR_ARG;
  877. }
  878. raw[0] = raw[1] = raw[2] = 0;
  879. if (coordinate_conversion(raw[0], raw[1], raw[2], cdriver.temperature,
  880. &base[0], &base[1], &base[2],
  881. &cdriver.correct) < 0) {
  882. return YAS_ERROR_ERROR;
  883. }
  884. for (i = 0; i < 3; i++) {
  885. raw[0] = raw[1] = raw[2] = 0;
  886. raw[i] = cdriver.coef;
  887. if (coordinate_conversion(raw[0], raw[1], raw[2],
  888. cdriver.temperature,
  889. &resolution[i*3 + 0],
  890. &resolution[i*3 + 1],
  891. &resolution[i*3 + 2],
  892. &cdriver.correct) < 0) {
  893. return YAS_ERROR_ERROR;
  894. }
  895. resolution[i*3 + 0] -= base[0];
  896. resolution[i*3 + 1] -= base[1];
  897. resolution[i*3 + 2] -= base[2];
  898. }
  899. for (i = 0; i < 3; i++)
  900. tmp[i] = prev_calib_offset[i];
  901. for (i = 0; i < 3; i++) {
  902. diff = (int32_t)new_offset[i] - (int32_t)prev_offset[i];
  903. while (diff > 0) {
  904. tmp[0] -= resolution[i*3 + 0];
  905. tmp[1] -= resolution[i*3 + 1];
  906. tmp[2] -= resolution[i*3 + 2];
  907. diff--;
  908. }
  909. while (diff < 0) {
  910. tmp[0] += resolution[i*3 + 0];
  911. tmp[1] += resolution[i*3 + 1];
  912. tmp[2] += resolution[i*3 + 2];
  913. diff++;
  914. }
  915. }
  916. for (i = 0; i < 3; i++)
  917. new_calib_offset[i] = tmp[i];
  918. return YAS_NO_ERROR;
  919. }
  920. static int
  921. yas_cdrv_set_transformatiom_matrix(const int8_t *transform)
  922. {
  923. int i;
  924. if (transform == NULL)
  925. return YAS_ERROR_ARG;
  926. for (i = 0; i < 9; i++)
  927. cdriver.transform[i] = transform[i];
  928. return YAS_NO_ERROR;
  929. }
  930. static int
  931. yas_cdrv_init(const int8_t *transform, struct yas_machdep_func *func)
  932. {
  933. int interval, i;
  934. uint8_t id;
  935. if (transform == NULL || func == NULL)
  936. return YAS_ERROR_ARG;
  937. for (i = 0; i < 9; i++)
  938. cdriver.transform[i] = transform[i];
  939. cdriver.func = *func;
  940. if (device_open() < 0)
  941. return YAS_ERROR_DEVICE_COMMUNICATION;
  942. if (init_test_register() < 0)
  943. return YAS_ERROR_DEVICE_COMMUNICATION;
  944. if (get_device_id(&id) < 0)
  945. return YAS_ERROR_DEVICE_COMMUNICATION;
  946. YLOGD(("device id:%02x\n", id));
  947. switch (id) {
  948. case YAS_YAS530_DEVICE_ID:
  949. if (get_cal_data_yas530(&cdriver.cal) < 0)
  950. return YAS_ERROR_DEVICE_COMMUNICATION;
  951. get_correction_value_yas530(&cdriver.cal, &cdriver.correct);
  952. cdriver.center = YAS_YAS530_DATA_CENTER;
  953. cdriver.overflow = YAS_YAS530_DATA_OVERFLOW;
  954. switch (cdriver.cal.ver) {
  955. case YAS_YAS530_VERSION_B:
  956. cdriver.coef = YAS_YAS530_VERSION_B_COEF;
  957. break;
  958. case YAS_YAS530_VERSION_A:
  959. default:
  960. cdriver.coef = YAS_YAS530_VERSION_A_COEF;
  961. break;
  962. }
  963. break;
  964. case YAS_YAS532_DEVICE_ID:
  965. if (get_cal_data_yas532(&cdriver.cal) < 0)
  966. return YAS_ERROR_DEVICE_COMMUNICATION;
  967. get_correction_value_yas532(&cdriver.cal, &cdriver.correct);
  968. cdriver.center = YAS_YAS532_DATA_CENTER;
  969. cdriver.overflow = YAS_YAS532_DATA_OVERFLOW;
  970. switch (cdriver.cal.ver) {
  971. case YAS_YAS532_VERSION_AC:
  972. cdriver.coef = YAS_YAS532_VERSION_AC_COEF;
  973. break;
  974. case YAS_YAS532_VERSION_AB:
  975. default:
  976. cdriver.coef = YAS_YAS532_VERSION_AB_COEF;
  977. break;
  978. }
  979. break;
  980. default:
  981. return YAS_ERROR_DEVICE_COMMUNICATION;
  982. }
  983. cdriver.dev_id = id;
  984. YLOGD(("%s %s\n",
  985. cdriver.dev_id == YAS_YAS532_DEVICE_ID
  986. ? "MS-3R" : "MS-3E",
  987. cdriver.cal.ver == YAS_YAS532_VERSION_AC
  988. ? "ACver" : "ABver"));
  989. YLOGD(("dx[%d] dy1[%d] dy2[%d] d2[%d] d3[%d] d4[%d] d5[%d] d6[%d] "
  990. "d7[%d] d8[%d] d9[%d] d0[%d] dck[%d] "
  991. "ver[%d]\n",
  992. cdriver.cal.dx, cdriver.cal.dy1, cdriver.cal.dy2,
  993. cdriver.cal.d2, cdriver.cal.d3, cdriver.cal.d4,
  994. cdriver.cal.d5, cdriver.cal.d6, cdriver.cal.d7,
  995. cdriver.cal.d8, cdriver.cal.d9, cdriver.cal.d0,
  996. cdriver.cal.dck, cdriver.cal.ver));
  997. YLOGD(("Cx[%4d] Cy1[%4d] Cy2[%4d] a2[%4d] a3[%4d] a4[%4d] a5[%4d] "
  998. "a6[%4d] a7[%4d] a8[%4d] a9[%4d] k[%4d]\n",
  999. cdriver.correct.Cx, cdriver.correct.Cy1,
  1000. cdriver.correct.Cy2, cdriver.correct.a2,
  1001. cdriver.correct.a3, cdriver.correct.a4,
  1002. cdriver.correct.a5, cdriver.correct.a6,
  1003. cdriver.correct.a7, cdriver.correct.a8,
  1004. cdriver.correct.a9, cdriver.correct.k));
  1005. if (set_configuration(0, 0, cdriver.cal.dck) < 0)
  1006. return YAS_ERROR_DEVICE_COMMUNICATION;
  1007. if (set_measure_interval(0) < 0)
  1008. return YAS_ERROR_DEVICE_COMMUNICATION;
  1009. if (get_measure_interval(&interval) < 0)
  1010. return YAS_ERROR_DEVICE_COMMUNICATION;
  1011. YLOGD(("interval[%d]\n", interval));
  1012. return YAS_NO_ERROR;
  1013. }
  1014. static int
  1015. yas_cdrv_term(void)
  1016. {
  1017. device_close();
  1018. return YAS_NO_ERROR;
  1019. }
  1020. #define YAS_DEFAULT_CALIB_INTERVAL (50) /* 50 msecs */
  1021. #define YAS_DEFAULT_DATA_INTERVAL (200) /* 200 msecs */
  1022. #define YAS_INITCOIL_INTERVAL (3000) /* 3 seconds */
  1023. #define YAS_INITCOIL_GIVEUP_INTERVAL (180000) /* 180 seconds */
  1024. #define YAS_DETECT_OVERFLOW_INTERVAL (0) /* 0 second */
  1025. #define YAS_MAG_ERROR_DELAY (200)
  1026. #define YAS_MAG_STATE_NORMAL (0)
  1027. #define YAS_MAG_STATE_INIT_COIL (1)
  1028. #define YAS_MAG_STATE_MEASURE_OFFSET (2)
  1029. static const int8_t YAS_TRANSFORMATION[][9] = {
  1030. { 0, 1, 0, -1, 0, 0, 0, 0, 1 },
  1031. {-1, 0, 0, 0, -1, 0, 0, 0, 1 },
  1032. { 0, -1, 0, 1, 0, 0, 0, 0, 1 },
  1033. { 1, 0, 0, 0, 1, 0, 0, 0, 1 },
  1034. { 0, -1, 0, -1, 0, 0, 0, 0, -1 },
  1035. { 1, 0, 0, 0, -1, 0, 0, 0, -1 },
  1036. { 0, 1, 0, 1, 0, 0, 0, 0, -1 },
  1037. {-1, 0, 0, 0, 1, 0, 0, 0, -1 },
  1038. };
  1039. static const int supported_data_interval[] = {10, 20, 50, 60, 100, 200, 1000};
  1040. static const int supported_calib_interval[] = {60, 60, 50, 60, 50, 50, 50};
  1041. static const int32_t INVALID_CALIB_OFFSET[]
  1042. = {0x7fffffff, 0x7fffffff, 0x7fffffff};
  1043. static const int8_t INVALID_OFFSET[] = {0x7f, 0x7f, 0x7f};
  1044. struct yas_adaptive_filter {
  1045. int num;
  1046. int index;
  1047. int filter_len;
  1048. int filter_noise;
  1049. int32_t sequence[YAS_MAG_MAX_FILTER_LEN];
  1050. };
  1051. struct yas_thresh_filter {
  1052. int32_t threshold;
  1053. int32_t last;
  1054. };
  1055. struct yas_driver {
  1056. int initialized;
  1057. struct yas_mag_driver_callback callback;
  1058. struct utimer data_timer;
  1059. struct utimer initcoil_timer;
  1060. struct utimer initcoil_giveup_timer;
  1061. struct utimer detect_overflow_timer;
  1062. int32_t prev_mag[3];
  1063. int32_t prev_xy1y2[3];
  1064. int32_t prev_mag_w_offset[3];
  1065. int16_t prev_temperature;
  1066. int measure_state;
  1067. int active;
  1068. int overflow;
  1069. int initcoil_gaveup;
  1070. int position;
  1071. int delay_timer_use_data;
  1072. int delay_timer_interval;
  1073. int delay_timer_counter;
  1074. int filter_enable;
  1075. int filter_len;
  1076. int filter_thresh;
  1077. int filter_noise[3];
  1078. struct yas_adaptive_filter adap_filter[3];
  1079. struct yas_thresh_filter thresh_filter[3];
  1080. struct yas_mag_offset offset;
  1081. #ifdef YAS_MAG_MANUAL_OFFSET
  1082. struct yas_vector manual_offset;
  1083. #endif
  1084. struct yas_matrix static_matrix;
  1085. struct yas_matrix dynamic_matrix;
  1086. };
  1087. static struct yas_driver this_driver;
  1088. static int
  1089. lock(void)
  1090. {
  1091. if (this_driver.callback.lock != NULL) {
  1092. if (this_driver.callback.lock() < 0)
  1093. return YAS_ERROR_RESTARTSYS;
  1094. }
  1095. return 0;
  1096. }
  1097. static int
  1098. unlock(void)
  1099. {
  1100. if (this_driver.callback.unlock != NULL) {
  1101. if (this_driver.callback.unlock() < 0)
  1102. return YAS_ERROR_RESTARTSYS;
  1103. }
  1104. return 0;
  1105. }
  1106. static int32_t
  1107. square(int32_t data)
  1108. {
  1109. return data * data;
  1110. }
  1111. static void
  1112. adaptive_filter_init(struct yas_adaptive_filter *adap_filter, int len,
  1113. int noise)
  1114. {
  1115. int i;
  1116. adap_filter->num = 0;
  1117. adap_filter->index = 0;
  1118. adap_filter->filter_noise = noise;
  1119. adap_filter->filter_len = len;
  1120. for (i = 0; i < adap_filter->filter_len; ++i)
  1121. adap_filter->sequence[i] = 0;
  1122. }
  1123. static int32_t
  1124. adaptive_filter_filter(struct yas_adaptive_filter *adap_filter, int32_t in)
  1125. {
  1126. int32_t avg, sum;
  1127. int i;
  1128. if (adap_filter->filter_len == 0)
  1129. return in;
  1130. if (adap_filter->num < adap_filter->filter_len) {
  1131. adap_filter->sequence[adap_filter->index++] = in / 100;
  1132. adap_filter->num++;
  1133. return in;
  1134. }
  1135. if (adap_filter->filter_len <= adap_filter->index)
  1136. adap_filter->index = 0;
  1137. adap_filter->sequence[adap_filter->index++] = in / 100;
  1138. avg = 0;
  1139. for (i = 0; i < adap_filter->filter_len; i++)
  1140. avg += adap_filter->sequence[i];
  1141. avg /= adap_filter->filter_len;
  1142. sum = 0;
  1143. for (i = 0; i < adap_filter->filter_len; i++)
  1144. sum += square(avg - adap_filter->sequence[i]);
  1145. sum /= adap_filter->filter_len;
  1146. if (sum <= adap_filter->filter_noise)
  1147. return avg * 100;
  1148. return ((in/100 - avg) * (sum - adap_filter->filter_noise) / sum + avg)
  1149. * 100;
  1150. }
  1151. static void
  1152. thresh_filter_init(struct yas_thresh_filter *thresh_filter, int threshold)
  1153. {
  1154. thresh_filter->threshold = threshold;
  1155. thresh_filter->last = 0;
  1156. }
  1157. static int32_t
  1158. thresh_filter_filter(struct yas_thresh_filter *thresh_filter, int32_t in)
  1159. {
  1160. if (in < thresh_filter->last - thresh_filter->threshold
  1161. || thresh_filter->last
  1162. + thresh_filter->threshold < in) {
  1163. thresh_filter->last = in;
  1164. return in;
  1165. } else {
  1166. return thresh_filter->last;
  1167. }
  1168. }
  1169. static void
  1170. filter_init(struct yas_driver *d)
  1171. {
  1172. int i;
  1173. for (i = 0; i < 3; i++) {
  1174. adaptive_filter_init(&d->adap_filter[i], d->filter_len,
  1175. d->filter_noise[i]);
  1176. thresh_filter_init(&d->thresh_filter[i], d->filter_thresh);
  1177. }
  1178. }
  1179. static void
  1180. filter_filter(struct yas_driver *d, int32_t *orig, int32_t *filtered)
  1181. {
  1182. int i;
  1183. for (i = 0; i < 3; i++) {
  1184. filtered[i] = adaptive_filter_filter(&d->adap_filter[i],
  1185. orig[i]);
  1186. filtered[i] = thresh_filter_filter(&d->thresh_filter[i],
  1187. filtered[i]);
  1188. }
  1189. }
  1190. static int
  1191. is_valid_offset(const int8_t *p)
  1192. {
  1193. return p != NULL && (p[0] <= 31) && (p[1] <= 31) && (p[2] <= 31)
  1194. && (-31 <= p[0]) && (-31 <= p[1]) && (-31 <= p[2]);
  1195. }
  1196. int
  1197. is_valid_calib_offset(const int32_t *p)
  1198. {
  1199. int i;
  1200. for (i = 0; i < 3; i++) {
  1201. if (p[i] != INVALID_CALIB_OFFSET[i])
  1202. return 1;
  1203. }
  1204. return 0;
  1205. }
  1206. static int
  1207. is_offset_differ(const int8_t *p0, const int8_t *p1)
  1208. {
  1209. return p0[0] != p1[0] || p0[1] != p1[1] || p0[2] != p1[2];
  1210. }
  1211. static int
  1212. is_calib_offset_differ(const int32_t *p0, const int32_t *p1)
  1213. {
  1214. return p0[0] != p1[0] || p0[1] != p1[1] || p0[2] != p1[2];
  1215. }
  1216. static int
  1217. get_overflow(struct yas_driver *d)
  1218. {
  1219. return d->overflow;
  1220. }
  1221. static void
  1222. set_overflow(struct yas_driver *d, const int overflow)
  1223. {
  1224. if (d->overflow != overflow)
  1225. d->overflow = overflow;
  1226. }
  1227. static int
  1228. get_initcoil_gaveup(struct yas_driver *d)
  1229. {
  1230. return d->initcoil_gaveup;
  1231. }
  1232. static void
  1233. set_initcoil_gaveup(struct yas_driver *d, const int initcoil_gaveup)
  1234. {
  1235. d->initcoil_gaveup = initcoil_gaveup;
  1236. }
  1237. static int32_t *
  1238. get_calib_offset(struct yas_driver *d)
  1239. {
  1240. return d->offset.calib_offset.v;
  1241. }
  1242. static void
  1243. set_calib_offset(struct yas_driver *d, const int32_t *offset)
  1244. {
  1245. int i;
  1246. if (is_calib_offset_differ(d->offset.calib_offset.v, offset)) {
  1247. for (i = 0; i < 3; i++)
  1248. d->offset.calib_offset.v[i] = offset[i];
  1249. }
  1250. }
  1251. #ifdef YAS_MAG_MANUAL_OFFSET
  1252. static int32_t *
  1253. get_manual_offset(struct yas_driver *d)
  1254. {
  1255. return d->manual_offset.v;
  1256. }
  1257. static void
  1258. set_manual_offset(struct yas_driver *d, const int32_t *offset)
  1259. {
  1260. int i;
  1261. for (i = 0; i < 3; i++)
  1262. d->manual_offset.v[i] = offset[i];
  1263. }
  1264. #endif
  1265. static int32_t *
  1266. get_static_matrix(struct yas_driver *d)
  1267. {
  1268. return d->static_matrix.matrix;
  1269. }
  1270. static void
  1271. set_static_matrix(struct yas_driver *d, const int32_t *matrix)
  1272. {
  1273. int i;
  1274. for (i = 0; i < 9; i++)
  1275. d->static_matrix.matrix[i] = matrix[i];
  1276. }
  1277. static int32_t *
  1278. get_dynamic_matrix(struct yas_driver *d)
  1279. {
  1280. return d->dynamic_matrix.matrix;
  1281. }
  1282. static void
  1283. set_dynamic_matrix(struct yas_driver *d, const int32_t *matrix)
  1284. {
  1285. int i;
  1286. for (i = 0; i < 9; i++)
  1287. d->dynamic_matrix.matrix[i] = matrix[i];
  1288. }
  1289. static int8_t *
  1290. get_offset(struct yas_driver *d)
  1291. {
  1292. return d->offset.hard_offset;
  1293. }
  1294. static void
  1295. set_offset(struct yas_driver *d, const int8_t *offset)
  1296. {
  1297. int i;
  1298. if (is_offset_differ(d->offset.hard_offset, offset)) {
  1299. for (i = 0; i < 3; i++)
  1300. d->offset.hard_offset[i] = offset[i];
  1301. }
  1302. }
  1303. static int
  1304. get_active(struct yas_driver *d)
  1305. {
  1306. return d->active;
  1307. }
  1308. static void
  1309. set_active(struct yas_driver *d, const int active)
  1310. {
  1311. d->active = active;
  1312. }
  1313. static int
  1314. get_position(struct yas_driver *d)
  1315. {
  1316. return d->position;
  1317. }
  1318. static void
  1319. set_position(struct yas_driver *d, const int position)
  1320. {
  1321. d->position = position;
  1322. }
  1323. static int
  1324. get_measure_state(struct yas_driver *d)
  1325. {
  1326. return d->measure_state;
  1327. }
  1328. static void
  1329. set_measure_state(struct yas_driver *d, const int state)
  1330. {
  1331. YLOGI(("state(%d)\n", state));
  1332. d->measure_state = state;
  1333. }
  1334. static struct utimer *
  1335. get_data_timer(struct yas_driver *d)
  1336. {
  1337. return &d->data_timer;
  1338. }
  1339. static struct utimer *
  1340. get_initcoil_timer(struct yas_driver *d)
  1341. {
  1342. return &d->initcoil_timer;
  1343. }
  1344. static struct utimer *
  1345. get_initcoil_giveup_timer(struct yas_driver *d)
  1346. {
  1347. return &d->initcoil_giveup_timer;
  1348. }
  1349. static struct utimer *
  1350. get_detect_overflow_timer(struct yas_driver *d)
  1351. {
  1352. return &d->detect_overflow_timer;
  1353. }
  1354. static int
  1355. get_delay_timer_use_data(struct yas_driver *d)
  1356. {
  1357. return d->delay_timer_use_data;
  1358. }
  1359. static void
  1360. set_delay_timer_use_data(struct yas_driver *d, int flag)
  1361. {
  1362. d->delay_timer_use_data = !!flag;
  1363. }
  1364. static int
  1365. get_delay_timer_interval(struct yas_driver *d)
  1366. {
  1367. return d->delay_timer_interval;
  1368. }
  1369. static void
  1370. set_delay_timer_interval(struct yas_driver *d, int interval)
  1371. {
  1372. d->delay_timer_interval = interval;
  1373. }
  1374. static int
  1375. get_delay_timer_counter(struct yas_driver *d)
  1376. {
  1377. return d->delay_timer_counter;
  1378. }
  1379. static void
  1380. set_delay_timer_counter(struct yas_driver *d, int counter)
  1381. {
  1382. d->delay_timer_counter = counter;
  1383. }
  1384. static int
  1385. get_filter_enable(struct yas_driver *d)
  1386. {
  1387. return d->filter_enable;
  1388. }
  1389. static void
  1390. set_filter_enable(struct yas_driver *d, int enable)
  1391. {
  1392. if (enable)
  1393. filter_init(d);
  1394. d->filter_enable = !!enable;
  1395. }
  1396. static int
  1397. get_filter_len(struct yas_driver *d)
  1398. {
  1399. return d->filter_len;
  1400. }
  1401. static void
  1402. set_filter_len(struct yas_driver *d, int len)
  1403. {
  1404. if (len < 0)
  1405. return;
  1406. if (len > YAS_MAG_MAX_FILTER_LEN)
  1407. return;
  1408. d->filter_len = len;
  1409. filter_init(d);
  1410. }
  1411. static int
  1412. get_filter_noise(struct yas_driver *d, int *noise)
  1413. {
  1414. int i;
  1415. for (i = 0; i < 3; i++)
  1416. noise[i] = d->filter_noise[i];
  1417. return 0;
  1418. }
  1419. static void
  1420. set_filter_noise(struct yas_driver *d, int *noise)
  1421. {
  1422. int i;
  1423. if (noise == NULL)
  1424. return;
  1425. for (i = 0; i < 3; i++) {
  1426. if (noise[i] < 0)
  1427. noise[i] = 0;
  1428. d->filter_noise[i] = noise[i];
  1429. }
  1430. filter_init(d);
  1431. }
  1432. static int
  1433. get_filter_thresh(struct yas_driver *d)
  1434. {
  1435. return d->filter_thresh;
  1436. }
  1437. static void
  1438. set_filter_thresh(struct yas_driver *d, int threshold)
  1439. {
  1440. if (threshold < 0)
  1441. return;
  1442. d->filter_thresh = threshold;
  1443. filter_init(d);
  1444. }
  1445. static int32_t*
  1446. get_previous_mag(struct yas_driver *d)
  1447. {
  1448. return d->prev_mag;
  1449. }
  1450. static void
  1451. set_previous_mag(struct yas_driver *d, int32_t *data)
  1452. {
  1453. int i;
  1454. for (i = 0; i < 3; i++)
  1455. d->prev_mag[i] = data[i];
  1456. }
  1457. static int32_t*
  1458. get_previous_xy1y2(struct yas_driver *d)
  1459. {
  1460. return d->prev_xy1y2;
  1461. }
  1462. static void
  1463. set_previous_xy1y2(struct yas_driver *d, int32_t *data)
  1464. {
  1465. int i;
  1466. for (i = 0; i < 3; i++)
  1467. d->prev_xy1y2[i] = data[i];
  1468. }
  1469. static int32_t*
  1470. get_previous_mag_w_offset(struct yas_driver *d)
  1471. {
  1472. return d->prev_mag_w_offset;
  1473. }
  1474. static void
  1475. set_previous_mag_w_offset(struct yas_driver *d, int32_t *data)
  1476. {
  1477. int i;
  1478. for (i = 0; i < 3; i++)
  1479. d->prev_mag_w_offset[i] = data[i];
  1480. }
  1481. static int16_t
  1482. get_previous_temperature(struct yas_driver *d)
  1483. {
  1484. return d->prev_temperature;
  1485. }
  1486. static void
  1487. set_previous_temperature(struct yas_driver *d, int16_t temperature)
  1488. {
  1489. d->prev_temperature = temperature;
  1490. }
  1491. static int
  1492. init_coil(struct yas_driver *d)
  1493. {
  1494. int rt;
  1495. YLOGD(("init_coil IN\n"));
  1496. utimer_update(get_initcoil_timer(d));
  1497. if (!get_initcoil_gaveup(d)) {
  1498. utimer_update(get_initcoil_giveup_timer(d));
  1499. if (utimer_is_timeout(get_initcoil_giveup_timer(d))) {
  1500. utimer_clear_timeout(get_initcoil_giveup_timer(d));
  1501. set_initcoil_gaveup(d, TRUE);
  1502. }
  1503. }
  1504. if (utimer_is_timeout(get_initcoil_timer(d))
  1505. && !get_initcoil_gaveup(d)) {
  1506. utimer_clear_timeout(get_initcoil_timer(d));
  1507. YLOGI(("init_coil!\n"));
  1508. rt = yas_cdrv_actuate_initcoil();
  1509. if (rt < 0) {
  1510. YLOGE(("yas_cdrv_actuate_initcoil failed[%d]\n", rt));
  1511. return rt;
  1512. }
  1513. if (get_overflow(d) || !is_valid_offset(get_offset(d)))
  1514. set_measure_state(d, YAS_MAG_STATE_MEASURE_OFFSET);
  1515. else
  1516. set_measure_state(d, YAS_MAG_STATE_NORMAL);
  1517. }
  1518. YLOGD(("init_coil OUT\n"));
  1519. return 0;
  1520. }
  1521. static int
  1522. measure_offset(struct yas_driver *d)
  1523. {
  1524. int8_t offset[3];
  1525. int32_t moffset[3];
  1526. int rt, result = 0, i;
  1527. YLOGI(("measure_offset IN\n"));
  1528. rt = yas_cdrv_measure_and_set_offset(offset);
  1529. if (rt < 0) {
  1530. YLOGE(("yas_cdrv_measure_offset failed[%d]\n", rt));
  1531. return rt;
  1532. }
  1533. YLOGI(("offset[%d][%d][%d]\n", offset[0], offset[1], offset[2]));
  1534. for (i = 0; i < 3; i++)
  1535. moffset[i] = get_calib_offset(d)[i];
  1536. if (is_offset_differ(get_offset(d), offset)) {
  1537. if (is_valid_offset(get_offset(d))
  1538. && is_valid_calib_offset(get_calib_offset(d))) {
  1539. yas_cdrv_recalc_calib_offset(get_calib_offset(d),
  1540. moffset,
  1541. get_offset(d),
  1542. offset);
  1543. result |= YAS_REPORT_CALIB_OFFSET_CHANGED;
  1544. }
  1545. }
  1546. result |= YAS_REPORT_HARD_OFFSET_CHANGED;
  1547. set_offset(d, offset);
  1548. if (is_valid_calib_offset(moffset))
  1549. set_calib_offset(d, moffset);
  1550. set_measure_state(d, YAS_MAG_STATE_NORMAL);
  1551. YLOGI(("measure_offset OUT\n"));
  1552. return result;
  1553. }
  1554. static int
  1555. measure_msensor_normal(struct yas_driver *d, int32_t *magnetic,
  1556. int32_t *mag_w_offset, int32_t *xy1y2, int16_t *temperature)
  1557. {
  1558. int rt = 0, result, i;
  1559. int32_t tmp[3];
  1560. YLOGD(("measure_msensor_normal IN\n"));
  1561. result = 0;
  1562. rt = yas_cdrv_measure(mag_w_offset, tmp, temperature);
  1563. if (rt < 0) {
  1564. YLOGE(("yas_cdrv_measure failed[%d]\n", rt));
  1565. return rt;
  1566. }
  1567. for (i = 0; i < 3; i++)
  1568. xy1y2[i] = tmp[i];
  1569. #ifdef YAS_MAG_MANUAL_OFFSET
  1570. for (i = 0; i < 3; i++)
  1571. mag_w_offset[i] -= get_manual_offset(d)[i];
  1572. #endif
  1573. if (rt > 0) {
  1574. YLOGW(("yas_cdrv_measure under/overflow "
  1575. "x[%c%c] y[%c%c] z[%c%c]\n",
  1576. (rt&0x01) ? 'u' : ' ',
  1577. (rt&0x02) ? 'o' : ' ',
  1578. (rt&0x04) ? 'u' : ' ',
  1579. (rt&0x08) ? 'o' : ' ',
  1580. (rt&0x10) ? 'u' : ' ',
  1581. (rt&0x20) ? 'o' : ' '));
  1582. utimer_update(get_detect_overflow_timer(d));
  1583. set_overflow(d, TRUE);
  1584. if (utimer_is_timeout(get_detect_overflow_timer(d))) {
  1585. utimer_clear_timeout(get_detect_overflow_timer(d));
  1586. result |= YAS_REPORT_OVERFLOW_OCCURED;
  1587. }
  1588. if (get_measure_state(d) == YAS_MAG_STATE_NORMAL)
  1589. set_measure_state(d, YAS_MAG_STATE_INIT_COIL);
  1590. } else {
  1591. utimer_clear(get_detect_overflow_timer(d));
  1592. set_overflow(d, FALSE);
  1593. if (get_measure_state(d) == YAS_MAG_STATE_NORMAL) {
  1594. utimer_clear(get_initcoil_timer(d));
  1595. utimer_clear(get_initcoil_giveup_timer(d));
  1596. }
  1597. }
  1598. for (i = 0; i < 3; i++) {
  1599. tmp[i]
  1600. = (get_static_matrix(d)[i*3+0]/10
  1601. * (mag_w_offset[0]/10)) / 100
  1602. + (get_static_matrix(d)[i*3+1]/10
  1603. * (mag_w_offset[1]/10)) / 100
  1604. + (get_static_matrix(d)[i*3+2]/10
  1605. * (mag_w_offset[2]/10)) / 100;
  1606. }
  1607. for (i = 0; i < 3; i++)
  1608. magnetic[i] = mag_w_offset[i] = tmp[i];
  1609. if (is_valid_calib_offset(get_calib_offset(d))) {
  1610. for (i = 0; i < 3; i++)
  1611. magnetic[i] -= get_calib_offset(d)[i];
  1612. }
  1613. for (i = 0; i < 3; i++) {
  1614. tmp[i]
  1615. = (get_dynamic_matrix(d)[i*3+0]/10
  1616. * (magnetic[0]/10)) / 100
  1617. + (get_dynamic_matrix(d)[i*3+1]/10
  1618. * (magnetic[1]/10)) / 100
  1619. + (get_dynamic_matrix(d)[i*3+2]/10
  1620. * (magnetic[2]/10)) / 100;
  1621. }
  1622. for (i = 0; i < 3; i++)
  1623. magnetic[i] = tmp[i];
  1624. if (get_filter_enable(d))
  1625. filter_filter(d, magnetic, magnetic);
  1626. YLOGD(("measure_msensor_normal OUT\n"));
  1627. return result;
  1628. }
  1629. static int
  1630. measure_msensor(struct yas_driver *d, int32_t *magnetic, int32_t *mag_w_offset,
  1631. int32_t *xy1y2, int16_t *temperature)
  1632. {
  1633. int result, i;
  1634. YLOGD(("measure_msensor IN\n"));
  1635. for (i = 0; i < 3; i++) {
  1636. magnetic[i] = get_previous_mag(d)[i];
  1637. mag_w_offset[i] = get_previous_mag_w_offset(d)[i];
  1638. xy1y2[i] = get_previous_xy1y2(d)[i];
  1639. *temperature = get_previous_temperature(d);
  1640. }
  1641. result = 0;
  1642. switch (get_measure_state(d)) {
  1643. case YAS_MAG_STATE_INIT_COIL:
  1644. result = init_coil(d);
  1645. break;
  1646. case YAS_MAG_STATE_MEASURE_OFFSET:
  1647. result = measure_offset(d);
  1648. break;
  1649. case YAS_MAG_STATE_NORMAL:
  1650. result = 0;
  1651. break;
  1652. default:
  1653. result = -1;
  1654. break;
  1655. }
  1656. if (result < 0)
  1657. return result;
  1658. if (!(result & YAS_REPORT_OVERFLOW_OCCURED)) {
  1659. result |= measure_msensor_normal(d, magnetic, mag_w_offset,
  1660. xy1y2, temperature);
  1661. }
  1662. set_previous_mag(d, magnetic);
  1663. set_previous_xy1y2(d, xy1y2);
  1664. set_previous_mag_w_offset(d, mag_w_offset);
  1665. set_previous_temperature(d, *temperature);
  1666. YLOGD(("measure_msensor OUT\n"));
  1667. return result;
  1668. }
  1669. static int
  1670. measure(struct yas_driver *d, int32_t *magnetic, int32_t *mag_w_offset,
  1671. int32_t *xy1y2, int16_t *temperature, uint32_t *time_delay)
  1672. {
  1673. int result;
  1674. int counter;
  1675. uint32_t total = 0;
  1676. YLOGD(("measure IN\n"));
  1677. utimer_update(get_data_timer(d));
  1678. result = measure_msensor(d, magnetic, mag_w_offset, xy1y2, temperature);
  1679. if (result < 0)
  1680. return result;
  1681. counter = get_delay_timer_counter(d);
  1682. total = utimer_get_total_time(get_data_timer(d));
  1683. if (utimer_get_delay(get_data_timer(d)) > 0)
  1684. counter -= (int)(total / utimer_get_delay(get_data_timer(d)));
  1685. else
  1686. counter = 0;
  1687. if (utimer_is_timeout(get_data_timer(d))) {
  1688. utimer_clear_timeout(get_data_timer(d));
  1689. if (get_delay_timer_use_data(d)) {
  1690. result |= YAS_REPORT_DATA;
  1691. if (counter <= 0)
  1692. result |= YAS_REPORT_CALIB;
  1693. } else {
  1694. result |= YAS_REPORT_CALIB;
  1695. if (counter <= 0)
  1696. result |= YAS_REPORT_DATA;
  1697. }
  1698. }
  1699. if (counter <= 0)
  1700. set_delay_timer_counter(d, get_delay_timer_interval(d));
  1701. else
  1702. set_delay_timer_counter(d, counter);
  1703. *time_delay = utimer_sleep_time(get_data_timer(d));
  1704. YLOGD(("measure OUT [%d]\n", result));
  1705. return result;
  1706. }
  1707. static int
  1708. resume(struct yas_driver *d)
  1709. {
  1710. int32_t zero[] = {0, 0, 0};
  1711. struct yas_machdep_func func;
  1712. int rt;
  1713. YLOGI(("resume IN\n"));
  1714. func.device_open = d->callback.device_open;
  1715. func.device_close = d->callback.device_close;
  1716. func.device_write = d->callback.device_write;
  1717. func.device_read = d->callback.device_read;
  1718. func.msleep = d->callback.msleep;
  1719. rt = yas_cdrv_init(YAS_TRANSFORMATION[get_position(d)], &func);
  1720. if (rt < 0) {
  1721. YLOGE(("yas_cdrv_init failed[%d]\n", rt));
  1722. return rt;
  1723. }
  1724. utimer_clear(get_data_timer(d));
  1725. utimer_clear(get_initcoil_giveup_timer(d));
  1726. utimer_clear(get_initcoil_timer(d));
  1727. utimer_clear(get_detect_overflow_timer(d));
  1728. set_previous_mag(d, zero);
  1729. set_previous_xy1y2(d, zero);
  1730. set_previous_mag_w_offset(d, zero);
  1731. set_previous_temperature(d, 0);
  1732. set_overflow(d, FALSE);
  1733. set_initcoil_gaveup(d, FALSE);
  1734. filter_init(d);
  1735. if (is_valid_offset(d->offset.hard_offset)) {
  1736. yas_cdrv_set_offset(d->offset.hard_offset);
  1737. rt = yas_cdrv_actuate_initcoil();
  1738. if (rt < 0) {
  1739. YLOGE(("yas_cdrv_actuate_initcoil failed[%d]\n", rt));
  1740. set_measure_state(d, YAS_MAG_STATE_INIT_COIL);
  1741. } else {
  1742. set_measure_state(d, YAS_MAG_STATE_NORMAL);
  1743. }
  1744. } else {
  1745. rt = yas_cdrv_actuate_initcoil();
  1746. if (rt < 0) {
  1747. YLOGE(("yas_cdrv_actuate_initcoil failed[%d]\n", rt));
  1748. set_measure_state(d, YAS_MAG_STATE_INIT_COIL);
  1749. } else {
  1750. set_measure_state(d, YAS_MAG_STATE_MEASURE_OFFSET);
  1751. }
  1752. }
  1753. YLOGI(("resume OUT\n"));
  1754. return 0;
  1755. }
  1756. static int
  1757. suspend(struct yas_driver *d)
  1758. {
  1759. YLOGI(("suspend IN\n"));
  1760. (void) d;
  1761. yas_cdrv_term();
  1762. YLOGI(("suspend OUT\n"));
  1763. return 0;
  1764. }
  1765. static int
  1766. check_interval(int ms)
  1767. {
  1768. int index;
  1769. if (ms <= supported_data_interval[0])
  1770. ms = supported_data_interval[0];
  1771. for (index = 0; index < NELEMS(supported_data_interval); index++) {
  1772. if (ms == supported_data_interval[index])
  1773. return index;
  1774. }
  1775. return -1;
  1776. }
  1777. static int
  1778. yas_get_delay_nolock(struct yas_driver *d, int *ms)
  1779. {
  1780. if (!d->initialized)
  1781. return YAS_ERROR_NOT_INITIALIZED;
  1782. if (get_delay_timer_use_data(d))
  1783. *ms = (int)utimer_get_delay(get_data_timer(d));
  1784. else
  1785. *ms = (int)utimer_get_delay(get_data_timer(d))
  1786. * get_delay_timer_interval(d);
  1787. return YAS_NO_ERROR;
  1788. }
  1789. static int
  1790. yas_set_delay_nolock(struct yas_driver *d, int ms)
  1791. {
  1792. int index;
  1793. uint32_t delay_data, delay_calib;
  1794. if (!d->initialized)
  1795. return YAS_ERROR_NOT_INITIALIZED;
  1796. index = check_interval(ms);
  1797. if (index < 0)
  1798. return YAS_ERROR_ARG;
  1799. delay_data = (uint32_t)supported_data_interval[index];
  1800. delay_calib = (uint32_t)supported_calib_interval[index];
  1801. set_delay_timer_use_data(d, delay_data < delay_calib);
  1802. if (delay_data < delay_calib) {
  1803. set_delay_timer_interval(d, (int)(delay_calib / delay_data));
  1804. set_delay_timer_counter(d, (int)(delay_calib / delay_data));
  1805. utimer_set_delay(get_data_timer(d),
  1806. (uint32_t)supported_data_interval[index]);
  1807. } else {
  1808. set_delay_timer_interval(d, (int)(delay_data / delay_calib));
  1809. set_delay_timer_counter(d, (int)(delay_data / delay_calib));
  1810. utimer_set_delay(get_data_timer(d),
  1811. (uint32_t)supported_calib_interval[index]);
  1812. }
  1813. return YAS_NO_ERROR;
  1814. }
  1815. static int
  1816. yas_get_offset_nolock(struct yas_driver *d, struct yas_mag_offset *offset)
  1817. {
  1818. if (!d->initialized)
  1819. return YAS_ERROR_NOT_INITIALIZED;
  1820. *offset = d->offset;
  1821. return YAS_NO_ERROR;
  1822. }
  1823. static int
  1824. yas_set_offset_nolock(struct yas_driver *d, struct yas_mag_offset *offset)
  1825. {
  1826. int32_t zero[] = {0, 0, 0};
  1827. int rt;
  1828. if (!d->initialized)
  1829. return YAS_ERROR_NOT_INITIALIZED;
  1830. if (!get_active(d)) {
  1831. d->offset = *offset;
  1832. return YAS_NO_ERROR;
  1833. }
  1834. if (!is_valid_offset(offset->hard_offset)
  1835. || is_offset_differ(offset->hard_offset,
  1836. d->offset.hard_offset)) {
  1837. filter_init(d);
  1838. utimer_clear(get_data_timer(d));
  1839. utimer_clear(get_initcoil_giveup_timer(d));
  1840. utimer_clear(get_initcoil_timer(d));
  1841. utimer_clear(get_detect_overflow_timer(d));
  1842. set_previous_mag(d, zero);
  1843. set_previous_xy1y2(d, zero);
  1844. set_previous_mag_w_offset(d, zero);
  1845. set_previous_temperature(d, 0);
  1846. set_overflow(d, FALSE);
  1847. set_initcoil_gaveup(d, FALSE);
  1848. }
  1849. d->offset = *offset;
  1850. if (is_valid_offset(d->offset.hard_offset)) {
  1851. yas_cdrv_set_offset(d->offset.hard_offset);
  1852. } else {
  1853. rt = yas_cdrv_actuate_initcoil();
  1854. if (rt < 0) {
  1855. YLOGE(("yas_cdrv_actuate_initcoil failed[%d]\n", rt));
  1856. set_measure_state(d, YAS_MAG_STATE_INIT_COIL);
  1857. } else {
  1858. set_measure_state(d, YAS_MAG_STATE_MEASURE_OFFSET);
  1859. }
  1860. }
  1861. return YAS_NO_ERROR;
  1862. }
  1863. #ifdef YAS_MAG_MANUAL_OFFSET
  1864. static int
  1865. yas_get_manual_offset_nolock(struct yas_driver *d, struct yas_vector *offset)
  1866. {
  1867. if (!d->initialized)
  1868. return YAS_ERROR_NOT_INITIALIZED;
  1869. *offset = d->manual_offset;
  1870. return YAS_NO_ERROR;
  1871. }
  1872. static int
  1873. yas_set_manual_offset_nolock(struct yas_driver *d, struct yas_vector *offset)
  1874. {
  1875. if (!d->initialized)
  1876. return YAS_ERROR_NOT_INITIALIZED;
  1877. set_manual_offset(d, offset->v);
  1878. return YAS_NO_ERROR;
  1879. }
  1880. #endif
  1881. static int
  1882. yas_get_static_matrix_nolock(struct yas_driver *d, struct yas_matrix *matrix)
  1883. {
  1884. int i;
  1885. if (!d->initialized)
  1886. return YAS_ERROR_NOT_INITIALIZED;
  1887. for (i = 0; i < 9; i++)
  1888. matrix->matrix[i] = get_static_matrix(d)[i];
  1889. return YAS_NO_ERROR;
  1890. }
  1891. static int
  1892. yas_set_static_matrix_nolock(struct yas_driver *d, struct yas_matrix *matrix)
  1893. {
  1894. if (!d->initialized)
  1895. return YAS_ERROR_NOT_INITIALIZED;
  1896. set_static_matrix(d, matrix->matrix);
  1897. return YAS_NO_ERROR;
  1898. }
  1899. static int
  1900. yas_get_dynamic_matrix_nolock(struct yas_driver *d, struct yas_matrix *matrix)
  1901. {
  1902. int i;
  1903. if (!d->initialized)
  1904. return YAS_ERROR_NOT_INITIALIZED;
  1905. for (i = 0; i < 9; i++)
  1906. matrix->matrix[i] = get_dynamic_matrix(d)[i];
  1907. return YAS_NO_ERROR;
  1908. }
  1909. static int
  1910. yas_set_dynamic_matrix_nolock(struct yas_driver *d, struct yas_matrix *matrix)
  1911. {
  1912. if (!d->initialized)
  1913. return YAS_ERROR_NOT_INITIALIZED;
  1914. set_dynamic_matrix(d, matrix->matrix);
  1915. return YAS_NO_ERROR;
  1916. }
  1917. static int
  1918. yas_get_enable_nolock(struct yas_driver *d)
  1919. {
  1920. if (!d->initialized)
  1921. return YAS_ERROR_NOT_INITIALIZED;
  1922. return get_active(d);
  1923. }
  1924. static int
  1925. yas_set_enable_nolock(struct yas_driver *d, int active)
  1926. {
  1927. int rt;
  1928. if (!d->initialized)
  1929. return YAS_ERROR_NOT_INITIALIZED;
  1930. if (active) {
  1931. if (get_active(d))
  1932. return YAS_NO_ERROR;
  1933. rt = resume(d);
  1934. if (rt < 0)
  1935. return rt;
  1936. set_active(d, TRUE);
  1937. } else {
  1938. if (!get_active(d))
  1939. return YAS_NO_ERROR;
  1940. rt = suspend(d);
  1941. if (rt < 0)
  1942. return rt;
  1943. set_active(d, FALSE);
  1944. }
  1945. return YAS_NO_ERROR;
  1946. }
  1947. static int
  1948. yas_get_filter_nolock(struct yas_driver *d, struct yas_mag_filter *filter)
  1949. {
  1950. if (!d->initialized)
  1951. return YAS_ERROR_NOT_INITIALIZED;
  1952. filter->len = get_filter_len(d);
  1953. get_filter_noise(d, filter->noise);
  1954. filter->threshold = get_filter_thresh(d);
  1955. return YAS_NO_ERROR;
  1956. }
  1957. static int
  1958. yas_set_filter_nolock(struct yas_driver *d, struct yas_mag_filter *filter)
  1959. {
  1960. if (!d->initialized)
  1961. return YAS_ERROR_NOT_INITIALIZED;
  1962. set_filter_len(d, filter->len);
  1963. set_filter_noise(d, filter->noise);
  1964. set_filter_thresh(d, filter->threshold);
  1965. return YAS_NO_ERROR;
  1966. }
  1967. static int
  1968. yas_get_filter_enable_nolock(struct yas_driver *d)
  1969. {
  1970. if (!d->initialized)
  1971. return YAS_ERROR_NOT_INITIALIZED;
  1972. return get_filter_enable(d);
  1973. }
  1974. static int
  1975. yas_set_filter_enable_nolock(struct yas_driver *d, int enable)
  1976. {
  1977. if (!d->initialized)
  1978. return YAS_ERROR_NOT_INITIALIZED;
  1979. set_filter_enable(d, enable);
  1980. return YAS_NO_ERROR;
  1981. }
  1982. static int
  1983. yas_get_position_nolock(struct yas_driver *d, int *position)
  1984. {
  1985. if (!d->initialized)
  1986. return YAS_ERROR_NOT_INITIALIZED;
  1987. *position = get_position(d);
  1988. return YAS_NO_ERROR;
  1989. }
  1990. static int
  1991. yas_set_position_nolock(struct yas_driver *d, int position)
  1992. {
  1993. if (!d->initialized)
  1994. return YAS_ERROR_NOT_INITIALIZED;
  1995. if (get_active(d))
  1996. yas_cdrv_set_transformatiom_matrix(
  1997. YAS_TRANSFORMATION[position]);
  1998. set_position(d, position);
  1999. filter_init(d);
  2000. return YAS_NO_ERROR;
  2001. }
  2002. static int
  2003. yas_read_reg_nolock(struct yas_driver *d, uint8_t addr, uint8_t *buf, int len)
  2004. {
  2005. if (!d->initialized)
  2006. return YAS_ERROR_NOT_INITIALIZED;
  2007. if (!get_active(d)) {
  2008. if (d->callback.device_open() < 0)
  2009. return YAS_ERROR_DEVICE_COMMUNICATION;
  2010. }
  2011. if (d->callback.device_read(YAS_MAG_I2C_SLAVEADDR, addr, buf, len) < 0)
  2012. return YAS_ERROR_DEVICE_COMMUNICATION;
  2013. if (!get_active(d)) {
  2014. if (d->callback.device_close() < 0)
  2015. return YAS_ERROR_DEVICE_COMMUNICATION;
  2016. }
  2017. return YAS_NO_ERROR;
  2018. }
  2019. static int
  2020. yas_write_reg_nolock(struct yas_driver *d, uint8_t addr, const uint8_t *buf,
  2021. int len)
  2022. {
  2023. if (!d->initialized)
  2024. return YAS_ERROR_NOT_INITIALIZED;
  2025. if (!get_active(d)) {
  2026. if (d->callback.device_open() < 0)
  2027. return YAS_ERROR_DEVICE_COMMUNICATION;
  2028. }
  2029. if (d->callback.device_write(YAS_MAG_I2C_SLAVEADDR, addr, buf, len) < 0)
  2030. return YAS_ERROR_DEVICE_COMMUNICATION;
  2031. if (!get_active(d)) {
  2032. if (d->callback.device_close() < 0)
  2033. return YAS_ERROR_DEVICE_COMMUNICATION;
  2034. }
  2035. return YAS_NO_ERROR;
  2036. }
  2037. static int
  2038. yas_measure_nolock(struct yas_driver *d, struct yas_mag_data *data,
  2039. int *time_delay_ms)
  2040. {
  2041. uint32_t time_delay = YAS_MAG_ERROR_DELAY;
  2042. int rt, i;
  2043. if (!d->initialized)
  2044. return YAS_ERROR_NOT_INITIALIZED;
  2045. *time_delay_ms = YAS_MAG_ERROR_DELAY;
  2046. if (!get_active(d)) {
  2047. for (i = 0; i < 3; i++) {
  2048. data->xyz.v[i] = get_previous_mag(d)[i];
  2049. data->raw.v[i] = get_previous_mag_w_offset(d)[i];
  2050. data->xy1y2.v[i] = get_previous_xy1y2(d)[i];
  2051. }
  2052. data->temperature = get_previous_temperature(d);
  2053. return YAS_NO_ERROR;
  2054. }
  2055. rt = measure(d, data->xyz.v, data->raw.v, data->xy1y2.v,
  2056. &data->temperature, &time_delay);
  2057. if (rt >= 0) {
  2058. *time_delay_ms = (int)time_delay;
  2059. if (*time_delay_ms > 0)
  2060. *time_delay_ms += 1; /* for the system that the time is
  2061. in usec unit */
  2062. }
  2063. return rt;
  2064. }
  2065. static int
  2066. yas_init_nolock(struct yas_driver *d)
  2067. {
  2068. #ifdef YAS_MAG_MANUAL_OFFSET
  2069. int32_t zero[] = {0, 0, 0};
  2070. #endif
  2071. int32_t notransform[] = {10000, 0, 0, 0, 10000, 0, 0, 0, 10000};
  2072. int noise[] = {
  2073. YAS_MAG_DEFAULT_FILTER_NOISE_X,
  2074. YAS_MAG_DEFAULT_FILTER_NOISE_Y,
  2075. YAS_MAG_DEFAULT_FILTER_NOISE_Z
  2076. };
  2077. YLOGI(("yas_init_nolock IN\n"));
  2078. if (d->initialized)
  2079. return YAS_ERROR_NOT_INITIALIZED;
  2080. utimer_lib_init(this_driver.callback.current_time);
  2081. utimer_init(get_data_timer(d), 50);
  2082. utimer_init(get_initcoil_timer(d), YAS_INITCOIL_INTERVAL);
  2083. utimer_init(get_initcoil_giveup_timer(d), YAS_INITCOIL_GIVEUP_INTERVAL);
  2084. utimer_init(get_detect_overflow_timer(d), YAS_DETECT_OVERFLOW_INTERVAL);
  2085. set_delay_timer_use_data(d, 0);
  2086. set_delay_timer_interval(d,
  2087. YAS_DEFAULT_DATA_INTERVAL / YAS_DEFAULT_CALIB_INTERVAL);
  2088. set_delay_timer_counter(d,
  2089. YAS_DEFAULT_DATA_INTERVAL / YAS_DEFAULT_CALIB_INTERVAL);
  2090. set_filter_enable(d, FALSE);
  2091. set_filter_len(d, YAS_MAG_DEFAULT_FILTER_LEN);
  2092. set_filter_thresh(d, YAS_MAG_DEFAULT_FILTER_THRESH);
  2093. set_filter_noise(d, noise);
  2094. filter_init(d);
  2095. set_calib_offset(d, INVALID_CALIB_OFFSET);
  2096. #ifdef YAS_MAG_MANUAL_OFFSET
  2097. set_manual_offset(d, zero);
  2098. #endif
  2099. set_static_matrix(d, notransform);
  2100. set_dynamic_matrix(d, notransform);
  2101. set_offset(d, INVALID_OFFSET);
  2102. set_active(d, FALSE);
  2103. set_position(d, 0);
  2104. d->initialized = 1;
  2105. YLOGI(("yas_init_nolock OUT\n"));
  2106. return YAS_NO_ERROR;
  2107. }
  2108. static int
  2109. yas_term_nolock(struct yas_driver *d)
  2110. {
  2111. YLOGI(("yas_term_nolock\n"));
  2112. if (!d->initialized)
  2113. return YAS_ERROR_NOT_INITIALIZED;
  2114. if (get_active(d))
  2115. suspend(d);
  2116. d->initialized = 0;
  2117. YLOGI(("yas_term_nolock out\n"));
  2118. return YAS_NO_ERROR;
  2119. }
  2120. static int
  2121. yas_get_delay(void)
  2122. {
  2123. int ms = 0, rt;
  2124. YLOGI(("yas_get_delay\n"));
  2125. if (lock() < 0)
  2126. return YAS_ERROR_RESTARTSYS;
  2127. rt = yas_get_delay_nolock(&this_driver, &ms);
  2128. if (unlock() < 0)
  2129. return YAS_ERROR_RESTARTSYS;
  2130. YLOGI(("yas_get_delay[%d] OUT\n", ms));
  2131. return rt < 0 ? rt : ms;
  2132. }
  2133. static int
  2134. yas_set_delay(int delay)
  2135. {
  2136. int rt;
  2137. YLOGI(("yas_set_delay\n"));
  2138. if (lock() < 0)
  2139. return YAS_ERROR_RESTARTSYS;
  2140. rt = yas_set_delay_nolock(&this_driver, delay);
  2141. if (unlock() < 0)
  2142. return YAS_ERROR_RESTARTSYS;
  2143. YLOGI(("yas_set_delay OUT\n"));
  2144. return rt;
  2145. }
  2146. static int
  2147. yas_get_offset(struct yas_mag_offset *offset)
  2148. {
  2149. int rt;
  2150. YLOGI(("yas_get_offset\n"));
  2151. if (offset == NULL)
  2152. return YAS_ERROR_ARG;
  2153. if (lock() < 0)
  2154. return YAS_ERROR_RESTARTSYS;
  2155. rt = yas_get_offset_nolock(&this_driver, offset);
  2156. if (unlock() < 0)
  2157. return YAS_ERROR_RESTARTSYS;
  2158. YLOGI(("yas_get_offset[%d] OUT\n", rt));
  2159. return rt;
  2160. }
  2161. static int
  2162. yas_set_offset(struct yas_mag_offset *offset)
  2163. {
  2164. int rt;
  2165. YLOGI(("yas_set_offset IN\n"));
  2166. if (offset == NULL)
  2167. return YAS_ERROR_ARG;
  2168. if (lock() < 0)
  2169. return YAS_ERROR_RESTARTSYS;
  2170. rt = yas_set_offset_nolock(&this_driver, offset);
  2171. if (unlock() < 0)
  2172. return YAS_ERROR_RESTARTSYS;
  2173. YLOGI(("yas_set_offset OUT\n"));
  2174. return rt;
  2175. }
  2176. #ifdef YAS_MAG_MANUAL_OFFSET
  2177. static int
  2178. yas_get_manual_offset(struct yas_vector *offset)
  2179. {
  2180. int rt;
  2181. YLOGI(("yas_get_manual_offset\n"));
  2182. if (offset == NULL)
  2183. return YAS_ERROR_ARG;
  2184. if (lock() < 0)
  2185. return YAS_ERROR_RESTARTSYS;
  2186. rt = yas_get_manual_offset_nolock(&this_driver, offset);
  2187. if (unlock() < 0)
  2188. return YAS_ERROR_RESTARTSYS;
  2189. YLOGI(("yas_get_manual_offset[%d] OUT\n", rt));
  2190. return rt;
  2191. }
  2192. static int
  2193. yas_set_manual_offset(struct yas_vector *offset)
  2194. {
  2195. int rt;
  2196. YLOGI(("yas_set_manual_offset IN\n"));
  2197. if (offset == NULL)
  2198. return YAS_ERROR_ARG;
  2199. if (lock() < 0)
  2200. return YAS_ERROR_RESTARTSYS;
  2201. rt = yas_set_manual_offset_nolock(&this_driver, offset);
  2202. if (unlock() < 0)
  2203. return YAS_ERROR_RESTARTSYS;
  2204. YLOGI(("yas_set_manual_offset OUT\n"));
  2205. return rt;
  2206. }
  2207. #endif
  2208. static int
  2209. yas_get_static_matrix(struct yas_matrix *matrix)
  2210. {
  2211. int rt;
  2212. YLOGI(("yas_get_static_matrix\n"));
  2213. if (matrix == NULL)
  2214. return YAS_ERROR_ARG;
  2215. if (lock() < 0)
  2216. return YAS_ERROR_RESTARTSYS;
  2217. rt = yas_get_static_matrix_nolock(&this_driver, matrix);
  2218. if (unlock() < 0)
  2219. return YAS_ERROR_RESTARTSYS;
  2220. YLOGI(("yas_get_static_matrix[%d] OUT\n", rt));
  2221. return rt;
  2222. }
  2223. static int
  2224. yas_set_static_matrix(struct yas_matrix *matrix)
  2225. {
  2226. int rt;
  2227. YLOGI(("yas_set_static_matrix IN\n"));
  2228. if (matrix == NULL)
  2229. return YAS_ERROR_ARG;
  2230. if (lock() < 0)
  2231. return YAS_ERROR_RESTARTSYS;
  2232. rt = yas_set_static_matrix_nolock(&this_driver, matrix);
  2233. if (unlock() < 0)
  2234. return YAS_ERROR_RESTARTSYS;
  2235. YLOGI(("yas_set_static_matrix OUT\n"));
  2236. return rt;
  2237. }
  2238. static int
  2239. yas_get_dynamic_matrix(struct yas_matrix *matrix)
  2240. {
  2241. int rt;
  2242. YLOGI(("yas_get_dynamic_matrix\n"));
  2243. if (matrix == NULL)
  2244. return YAS_ERROR_ARG;
  2245. if (lock() < 0)
  2246. return YAS_ERROR_RESTARTSYS;
  2247. rt = yas_get_dynamic_matrix_nolock(&this_driver, matrix);
  2248. if (unlock() < 0)
  2249. return YAS_ERROR_RESTARTSYS;
  2250. YLOGI(("yas_get_dynamic_matrix[%d] OUT\n", rt));
  2251. return rt;
  2252. }
  2253. static int
  2254. yas_set_dynamic_matrix(struct yas_matrix *matrix)
  2255. {
  2256. int rt;
  2257. YLOGI(("yas_set_dynamic_matrix IN\n"));
  2258. if (matrix == NULL)
  2259. return YAS_ERROR_ARG;
  2260. if (lock() < 0)
  2261. return YAS_ERROR_RESTARTSYS;
  2262. rt = yas_set_dynamic_matrix_nolock(&this_driver, matrix);
  2263. if (unlock() < 0)
  2264. return YAS_ERROR_RESTARTSYS;
  2265. YLOGI(("yas_set_dynamic_matrix OUT\n"));
  2266. return rt;
  2267. }
  2268. static int
  2269. yas_get_enable(void)
  2270. {
  2271. int rt;
  2272. YLOGI(("yas_get_enable\n"));
  2273. if (lock() < 0)
  2274. return YAS_ERROR_RESTARTSYS;
  2275. rt = yas_get_enable_nolock(&this_driver);
  2276. if (unlock() < 0)
  2277. return YAS_ERROR_RESTARTSYS;
  2278. YLOGI(("yas_get_enable OUT[%d]\n", rt));
  2279. return rt;
  2280. }
  2281. static int
  2282. yas_set_enable(int enable)
  2283. {
  2284. int rt;
  2285. YLOGI(("yas_set_enable IN\n"));
  2286. if (lock() < 0)
  2287. return YAS_ERROR_RESTARTSYS;
  2288. rt = yas_set_enable_nolock(&this_driver, enable);
  2289. if (unlock() < 0)
  2290. return YAS_ERROR_RESTARTSYS;
  2291. YLOGI(("yas_set_enable OUT\n"));
  2292. return rt;
  2293. }
  2294. static int
  2295. yas_get_filter(struct yas_mag_filter *filter)
  2296. {
  2297. int rt;
  2298. YLOGI(("yas_get_filter\n"));
  2299. if (filter == NULL)
  2300. return YAS_ERROR_ARG;
  2301. if (lock() < 0)
  2302. return YAS_ERROR_RESTARTSYS;
  2303. rt = yas_get_filter_nolock(&this_driver, filter);
  2304. if (unlock() < 0)
  2305. return YAS_ERROR_RESTARTSYS;
  2306. YLOGI(("yas_get_filter[%d] OUT\n", rt));
  2307. return rt;
  2308. }
  2309. static int
  2310. yas_set_filter(struct yas_mag_filter *filter)
  2311. {
  2312. int rt, i;
  2313. YLOGI(("yas_set_filter IN\n"));
  2314. if (filter == NULL
  2315. || filter->len < 0
  2316. || YAS_MAG_MAX_FILTER_LEN < filter->len
  2317. || filter->threshold < 0) {
  2318. return YAS_ERROR_ARG;
  2319. }
  2320. for (i = 0; i < 3; i++) {
  2321. if (filter->noise[i] < 0)
  2322. return YAS_ERROR_ARG;
  2323. }
  2324. if (lock() < 0)
  2325. return YAS_ERROR_RESTARTSYS;
  2326. rt = yas_set_filter_nolock(&this_driver, filter);
  2327. if (unlock() < 0)
  2328. return YAS_ERROR_RESTARTSYS;
  2329. YLOGI(("yas_set_filter OUT\n"));
  2330. return rt;
  2331. }
  2332. static int
  2333. yas_get_filter_enable(void)
  2334. {
  2335. int rt;
  2336. YLOGI(("yas_get_filter_enable\n"));
  2337. if (lock() < 0)
  2338. return YAS_ERROR_RESTARTSYS;
  2339. rt = yas_get_filter_enable_nolock(&this_driver);
  2340. if (unlock() < 0)
  2341. return YAS_ERROR_RESTARTSYS;
  2342. YLOGI(("yas_get_filter_enable OUT[%d]\n", rt));
  2343. return rt;
  2344. }
  2345. static int
  2346. yas_set_filter_enable(int enable)
  2347. {
  2348. int rt;
  2349. YLOGI(("yas_set_filter_enable IN\n"));
  2350. if (lock() < 0)
  2351. return YAS_ERROR_RESTARTSYS;
  2352. rt = yas_set_filter_enable_nolock(&this_driver, enable);
  2353. if (unlock() < 0)
  2354. return YAS_ERROR_RESTARTSYS;
  2355. YLOGI(("yas_set_filter_enable OUT\n"));
  2356. return rt;
  2357. }
  2358. static int
  2359. yas_get_position(void)
  2360. {
  2361. int position = 0;
  2362. int rt;
  2363. YLOGI(("yas_get_position\n"));
  2364. if (lock() < 0)
  2365. return YAS_ERROR_RESTARTSYS;
  2366. rt = yas_get_position_nolock(&this_driver, &position);
  2367. if (unlock() < 0)
  2368. return YAS_ERROR_RESTARTSYS;
  2369. YLOGI(("yas_get_position[%d] OUT\n", position));
  2370. return rt < 0 ? rt : position;
  2371. }
  2372. static int
  2373. yas_set_position(int position)
  2374. {
  2375. int rt;
  2376. YLOGI(("yas_set_position\n"));
  2377. if (position < 0 || 7 < position)
  2378. return YAS_ERROR_ARG;
  2379. if (lock() < 0)
  2380. return YAS_ERROR_RESTARTSYS;
  2381. rt = yas_set_position_nolock(&this_driver, position);
  2382. if (unlock() < 0)
  2383. return YAS_ERROR_RESTARTSYS;
  2384. YLOGI(("yas_set_position[%d] OUT\n", position));
  2385. return rt;
  2386. }
  2387. static int
  2388. yas_read_reg(uint8_t addr, uint8_t *buf, int len)
  2389. {
  2390. int rt;
  2391. YLOGI(("yas_read_reg\n"));
  2392. if (buf == NULL || len <= 0)
  2393. return YAS_ERROR_ARG;
  2394. if (lock() < 0)
  2395. return YAS_ERROR_RESTARTSYS;
  2396. rt = yas_read_reg_nolock(&this_driver, addr, buf, len);
  2397. if (unlock() < 0)
  2398. return YAS_ERROR_RESTARTSYS;
  2399. YLOGI(("yas_read_reg[%d] OUT\n", rt));
  2400. return rt;
  2401. }
  2402. static int
  2403. yas_write_reg(uint8_t addr, const uint8_t *buf, int len)
  2404. {
  2405. int rt;
  2406. YLOGI(("yas_write_reg\n"));
  2407. if (buf == NULL || len <= 0)
  2408. return YAS_ERROR_ARG;
  2409. if (lock() < 0)
  2410. return YAS_ERROR_RESTARTSYS;
  2411. rt = yas_write_reg_nolock(&this_driver, addr, buf, len);
  2412. if (unlock() < 0)
  2413. return YAS_ERROR_RESTARTSYS;
  2414. YLOGI(("yas_write_reg[%d] OUT\n", rt));
  2415. return rt;
  2416. }
  2417. static int
  2418. yas_measure(struct yas_mag_data *data, int *time_delay_ms)
  2419. {
  2420. int rt;
  2421. YLOGD(("yas_measure IN\n"));
  2422. if (data == NULL || time_delay_ms == NULL)
  2423. return YAS_ERROR_ARG;
  2424. if (lock() < 0)
  2425. return YAS_ERROR_RESTARTSYS;
  2426. rt = yas_measure_nolock(&this_driver, data, time_delay_ms);
  2427. if (unlock() < 0)
  2428. return YAS_ERROR_RESTARTSYS;
  2429. YLOGD(("yas_measure OUT[%d]\n", rt));
  2430. return rt;
  2431. }
  2432. static int
  2433. yas_init(void)
  2434. {
  2435. int rt;
  2436. YLOGI(("yas_init\n"));
  2437. if (lock() < 0)
  2438. return YAS_ERROR_RESTARTSYS;
  2439. rt = yas_init_nolock(&this_driver);
  2440. if (unlock() < 0)
  2441. return YAS_ERROR_RESTARTSYS;
  2442. return rt;
  2443. }
  2444. static int
  2445. yas_term(void)
  2446. {
  2447. int rt;
  2448. YLOGI(("yas_term\n"));
  2449. if (lock() < 0)
  2450. return YAS_ERROR_RESTARTSYS;
  2451. rt = yas_term_nolock(&this_driver);
  2452. if (unlock() < 0)
  2453. return YAS_ERROR_RESTARTSYS;
  2454. return rt;
  2455. }
  2456. int
  2457. yas_mag_driver_init(struct yas_mag_driver *f)
  2458. {
  2459. if (f == NULL)
  2460. return YAS_ERROR_ARG;
  2461. if (f->callback.device_open == NULL
  2462. || f->callback.device_close == NULL
  2463. || f->callback.device_read == NULL
  2464. || f->callback.device_write == NULL
  2465. || f->callback.msleep == NULL
  2466. || f->callback.current_time == NULL) {
  2467. return YAS_ERROR_ARG;
  2468. }
  2469. f->init = yas_init;
  2470. f->term = yas_term;
  2471. f->get_delay = yas_get_delay;
  2472. f->set_delay = yas_set_delay;
  2473. f->get_offset = yas_get_offset;
  2474. f->set_offset = yas_set_offset;
  2475. #ifdef YAS_MAG_MANUAL_OFFSET
  2476. f->get_manual_offset = yas_get_manual_offset;
  2477. f->set_manual_offset = yas_set_manual_offset;
  2478. #endif
  2479. f->get_static_matrix = yas_get_static_matrix;
  2480. f->set_static_matrix = yas_set_static_matrix;
  2481. f->get_dynamic_matrix = yas_get_dynamic_matrix;
  2482. f->set_dynamic_matrix = yas_set_dynamic_matrix;
  2483. f->get_enable = yas_get_enable;
  2484. f->set_enable = yas_set_enable;
  2485. f->get_filter = yas_get_filter;
  2486. f->set_filter = yas_set_filter;
  2487. f->get_filter_enable = yas_get_filter_enable;
  2488. f->set_filter_enable = yas_set_filter_enable;
  2489. f->get_position = yas_get_position;
  2490. f->set_position = yas_set_position;
  2491. f->read_reg = yas_read_reg;
  2492. f->write_reg = yas_write_reg;
  2493. f->measure = yas_measure;
  2494. if ((f->callback.lock == NULL && f->callback.unlock != NULL)
  2495. || (f->callback.lock != NULL
  2496. && f->callback.unlock == NULL)) {
  2497. this_driver.callback.lock = NULL;
  2498. this_driver.callback.unlock = NULL;
  2499. } else {
  2500. this_driver.callback.lock = f->callback.lock;
  2501. this_driver.callback.unlock = f->callback.unlock;
  2502. }
  2503. this_driver.callback.device_open = f->callback.device_open;
  2504. this_driver.callback.device_close = f->callback.device_close;
  2505. this_driver.callback.device_write = f->callback.device_write;
  2506. this_driver.callback.device_read = f->callback.device_read;
  2507. this_driver.callback.msleep = f->callback.msleep;
  2508. this_driver.callback.current_time = f->callback.current_time;
  2509. yas_term();
  2510. return YAS_NO_ERROR;
  2511. }